blob: a2e6c01dc2924b76b725ec64cef1192982a89a38 [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
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000074 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000075 public DeclVisitor<ASTNodeImporter, Decl *>,
76 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000077 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000078
Douglas Gregor96e578d2010-02-05 17:54:41 +000079 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000080 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Douglas Gregor96e578d2010-02-05 17:54:41 +000081
82 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000083 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000084 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000085
86 // Importing types
John McCall424cec92011-01-19 06:33:43 +000087 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000088 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000089 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000090 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000091 QualType VisitComplexType(const ComplexType *T);
92 QualType VisitPointerType(const PointerType *T);
93 QualType VisitBlockPointerType(const BlockPointerType *T);
94 QualType VisitLValueReferenceType(const LValueReferenceType *T);
95 QualType VisitRValueReferenceType(const RValueReferenceType *T);
96 QualType VisitMemberPointerType(const MemberPointerType *T);
97 QualType VisitConstantArrayType(const ConstantArrayType *T);
98 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
99 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000100 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000101 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000102 QualType VisitVectorType(const VectorType *T);
103 QualType VisitExtVectorType(const ExtVectorType *T);
104 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
105 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000106 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000107 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000108 QualType VisitTypedefType(const TypedefType *T);
109 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000110 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000111 QualType VisitTypeOfType(const TypeOfType *T);
112 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000113 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000114 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000115 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000116 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000117 QualType VisitRecordType(const RecordType *T);
118 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000119 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000120 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000121 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000122 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
123 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000124 // FIXME: DependentNameType
Gabor Horvath7a91c082017-11-14 11:30:38 +0000125 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000126 QualType VisitDependentTemplateSpecializationType(
127 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000128 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
129 QualType VisitObjCObjectType(const ObjCObjectType *T);
130 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000131
Douglas Gregor95d82832012-01-24 18:36:04 +0000132 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000133 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
134 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000135 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000136 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000137 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
138 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000139 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000140
Aleksei Sidorina693b372016-09-28 10:16:56 +0000141 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
142
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000143 using Designator = DesignatedInitExpr::Designator;
144
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000145 Designator ImportDesignator(const Designator &D);
146
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000147 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Douglas Gregor2e15c842012-02-01 21:00:38 +0000148
Douglas Gregor95d82832012-01-24 18:36:04 +0000149 /// \brief What we should import from the definition.
150 enum ImportDefinitionKind {
151 /// \brief Import the default subset of the definition, which might be
152 /// nothing (if minimal import is set) or might be everything (if minimal
153 /// import is not set).
154 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000155
Douglas Gregor95d82832012-01-24 18:36:04 +0000156 /// \brief Import everything.
157 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000158
Douglas Gregor95d82832012-01-24 18:36:04 +0000159 /// \brief Import only the bare bones needed to establish a valid
160 /// DeclContext.
161 IDK_Basic
162 };
163
Douglas Gregor2e15c842012-02-01 21:00:38 +0000164 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
165 return IDK == IDK_Everything ||
166 (IDK == IDK_Default && !Importer.isMinimalImport());
167 }
168
Douglas Gregord451ea92011-07-29 23:31:30 +0000169 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000170 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000171 bool ImportDefinition(VarDecl *From, VarDecl *To,
172 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000173 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000174 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000175 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000176 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000177 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000178 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000179 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000180 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000181 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000182 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
183 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000184 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
185 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000186 SmallVectorImpl<TemplateArgument> &ToArgs);
187
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000188 template <typename InContainerTy>
189 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
190 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000191
192 template<typename InContainerTy>
193 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
194 SourceLocation FromRAngleLoc,
195 const InContainerTy &Container,
196 TemplateArgumentListInfo &Result);
197
198 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
199
Douglas Gregordd6006f2012-07-17 21:16:27 +0000200 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
201 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000202 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
203 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000204 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000205 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000206 bool IsStructuralMatch(FunctionTemplateDecl *From,
207 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000208 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000209 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000210 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000211 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000212 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000213 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000214 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000215 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000216 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000217 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000218 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000219 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000220 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000221 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000222 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000223 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000224 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000225 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000226 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
227 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
228 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
229 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000230 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000231 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000232 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000233 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000234 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000235 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000236 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000237 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000238 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000239 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000240 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000241 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000242 Decl *VisitUsingDecl(UsingDecl *D);
243 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
244 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
245 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
246 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
247
Douglas Gregor85f3f952015-07-07 03:57:15 +0000248 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000249 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000250 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000251 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000252 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000253 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000254 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
255 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
256 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
257 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000258 Decl *VisitClassTemplateSpecializationDecl(
259 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000260 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
261 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000262 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000263
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000264 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000265 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
266
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000267 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000268 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000269 Stmt *VisitDeclStmt(DeclStmt *S);
270 Stmt *VisitNullStmt(NullStmt *S);
271 Stmt *VisitCompoundStmt(CompoundStmt *S);
272 Stmt *VisitCaseStmt(CaseStmt *S);
273 Stmt *VisitDefaultStmt(DefaultStmt *S);
274 Stmt *VisitLabelStmt(LabelStmt *S);
275 Stmt *VisitAttributedStmt(AttributedStmt *S);
276 Stmt *VisitIfStmt(IfStmt *S);
277 Stmt *VisitSwitchStmt(SwitchStmt *S);
278 Stmt *VisitWhileStmt(WhileStmt *S);
279 Stmt *VisitDoStmt(DoStmt *S);
280 Stmt *VisitForStmt(ForStmt *S);
281 Stmt *VisitGotoStmt(GotoStmt *S);
282 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
283 Stmt *VisitContinueStmt(ContinueStmt *S);
284 Stmt *VisitBreakStmt(BreakStmt *S);
285 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000286 // FIXME: MSAsmStmt
287 // FIXME: SEHExceptStmt
288 // FIXME: SEHFinallyStmt
289 // FIXME: SEHTryStmt
290 // FIXME: SEHLeaveStmt
291 // FIXME: CapturedStmt
292 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
293 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
294 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
295 // FIXME: MSDependentExistsStmt
296 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
297 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
298 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
299 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
300 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
301 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
302 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000303
304 // Importing expressions
305 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000306 Expr *VisitVAArgExpr(VAArgExpr *E);
307 Expr *VisitGNUNullExpr(GNUNullExpr *E);
308 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000309 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000310 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
311 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
312 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000313 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000314 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000315 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000316 Expr *VisitStringLiteral(StringLiteral *E);
317 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
318 Expr *VisitAtomicExpr(AtomicExpr *E);
319 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000320 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000321 Expr *VisitParenListExpr(ParenListExpr *E);
322 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000323 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000324 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000325 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000326 Expr *VisitConditionalOperator(ConditionalOperator *E);
327 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
328 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000329 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
330 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
331 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000332 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000333 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000334 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
335 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
336 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
337 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
338 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
339 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
340 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
341 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
342 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000343 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000344 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000345 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
346 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000347 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000348 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000349 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000350 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
351 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000352 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000353 Expr *VisitCXXThisExpr(CXXThisExpr *E);
354 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000355 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000356 Expr *VisitMemberExpr(MemberExpr *E);
357 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000358 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000359 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000360 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
361 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000362 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
363 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000364 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000365 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000366 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000367
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000368 template<typename IIter, typename OIter>
369 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000370 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
371
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000372 ASTImporter &ImporterRef = Importer;
373 std::transform(Ibegin, Iend, Obegin,
374 [&ImporterRef](ItemT From) -> ItemT {
375 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000376 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000377 }
378
379 template<typename IIter, typename OIter>
380 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000381 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
382
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000383 ASTImporter &ImporterRef = Importer;
384 bool Failed = false;
385 std::transform(Ibegin, Iend, Obegin,
386 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000387 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000388 if (!To && From)
389 Failed = true;
390 return To;
391 });
392 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000393 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000394
395 template<typename InContainerTy, typename OutContainerTy>
396 bool ImportContainerChecked(const InContainerTy &InContainer,
397 OutContainerTy &OutContainer) {
398 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
399 OutContainer.begin());
400 }
401
402 template<typename InContainerTy, typename OIter>
403 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
404 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
405 }
Lang Hames19e07e12017-06-20 21:06:00 +0000406
407 // Importing overrides.
408 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000409 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000410
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000411template <typename InContainerTy>
412bool ASTNodeImporter::ImportTemplateArgumentListInfo(
413 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
414 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
415 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
416 Importer.Import(FromRAngleLoc));
417 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
418 return true;
419 Result = ToTAInfo;
420 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000421}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000422
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000423template <>
424bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
425 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
426 return ImportTemplateArgumentListInfo(
427 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
428}
429
430template <>
431bool ASTNodeImporter::ImportTemplateArgumentListInfo<
432 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
433 TemplateArgumentListInfo &Result) {
434 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
435 From.arguments(), Result);
436}
437
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000438} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000439
Douglas Gregor3996e242010-02-15 22:01:00 +0000440//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000441// Import Types
442//----------------------------------------------------------------------------
443
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000444using namespace clang;
445
John McCall424cec92011-01-19 06:33:43 +0000446QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000447 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
448 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000449 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000450}
451
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000452QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
453 QualType UnderlyingType = Importer.Import(T->getValueType());
454 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000455 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000456
457 return Importer.getToContext().getAtomicType(UnderlyingType);
458}
459
John McCall424cec92011-01-19 06:33:43 +0000460QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000461 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000462#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
463 case BuiltinType::Id: \
464 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000465#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000466#define SHARED_SINGLETON_TYPE(Expansion)
467#define BUILTIN_TYPE(Id, SingletonId) \
468 case BuiltinType::Id: return Importer.getToContext().SingletonId;
469#include "clang/AST/BuiltinTypes.def"
470
471 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
472 // context supports C++.
473
474 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
475 // context supports ObjC.
476
Douglas Gregor96e578d2010-02-05 17:54:41 +0000477 case BuiltinType::Char_U:
478 // The context we're importing from has an unsigned 'char'. If we're
479 // importing into a context with a signed 'char', translate to
480 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000481 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000482 return Importer.getToContext().UnsignedCharTy;
483
484 return Importer.getToContext().CharTy;
485
Douglas Gregor96e578d2010-02-05 17:54:41 +0000486 case BuiltinType::Char_S:
487 // The context we're importing from has an unsigned 'char'. If we're
488 // importing into a context with a signed 'char', translate to
489 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000490 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000491 return Importer.getToContext().SignedCharTy;
492
493 return Importer.getToContext().CharTy;
494
Chris Lattnerad3467e2010-12-25 23:25:43 +0000495 case BuiltinType::WChar_S:
496 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000497 // FIXME: If not in C++, shall we translate to the C equivalent of
498 // wchar_t?
499 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000500 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000501
502 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000503}
504
Aleksei Sidorina693b372016-09-28 10:16:56 +0000505QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
506 QualType OrigT = Importer.Import(T->getOriginalType());
507 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000508 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000509
510 return Importer.getToContext().getDecayedType(OrigT);
511}
512
John McCall424cec92011-01-19 06:33:43 +0000513QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000514 QualType ToElementType = Importer.Import(T->getElementType());
515 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000516 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000517
518 return Importer.getToContext().getComplexType(ToElementType);
519}
520
John McCall424cec92011-01-19 06:33:43 +0000521QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000522 QualType ToPointeeType = Importer.Import(T->getPointeeType());
523 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000524 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000525
526 return Importer.getToContext().getPointerType(ToPointeeType);
527}
528
John McCall424cec92011-01-19 06:33:43 +0000529QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000530 // FIXME: Check for blocks support in "to" context.
531 QualType ToPointeeType = Importer.Import(T->getPointeeType());
532 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000533 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000534
535 return Importer.getToContext().getBlockPointerType(ToPointeeType);
536}
537
John McCall424cec92011-01-19 06:33:43 +0000538QualType
539ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000540 // FIXME: Check for C++ support in "to" context.
541 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
542 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000543 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000544
545 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
546}
547
John McCall424cec92011-01-19 06:33:43 +0000548QualType
549ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000550 // FIXME: Check for C++0x support in "to" context.
551 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
552 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000553 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000554
555 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
556}
557
John McCall424cec92011-01-19 06:33:43 +0000558QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000559 // FIXME: Check for C++ support in "to" context.
560 QualType ToPointeeType = Importer.Import(T->getPointeeType());
561 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000562 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000563
564 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
565 return Importer.getToContext().getMemberPointerType(ToPointeeType,
566 ClassType.getTypePtr());
567}
568
John McCall424cec92011-01-19 06:33:43 +0000569QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000570 QualType ToElementType = Importer.Import(T->getElementType());
571 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000572 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000573
574 return Importer.getToContext().getConstantArrayType(ToElementType,
575 T->getSize(),
576 T->getSizeModifier(),
577 T->getIndexTypeCVRQualifiers());
578}
579
John McCall424cec92011-01-19 06:33:43 +0000580QualType
581ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000582 QualType ToElementType = Importer.Import(T->getElementType());
583 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000584 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000585
586 return Importer.getToContext().getIncompleteArrayType(ToElementType,
587 T->getSizeModifier(),
588 T->getIndexTypeCVRQualifiers());
589}
590
John McCall424cec92011-01-19 06:33:43 +0000591QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000592 QualType ToElementType = Importer.Import(T->getElementType());
593 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000594 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000595
596 Expr *Size = Importer.Import(T->getSizeExpr());
597 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000598 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000599
600 SourceRange Brackets = Importer.Import(T->getBracketsRange());
601 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
602 T->getSizeModifier(),
603 T->getIndexTypeCVRQualifiers(),
604 Brackets);
605}
606
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000607QualType ASTNodeImporter::VisitDependentSizedArrayType(
608 const DependentSizedArrayType *T) {
609 QualType ToElementType = Importer.Import(T->getElementType());
610 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000611 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000612
613 // SizeExpr may be null if size is not specified directly.
614 // For example, 'int a[]'.
615 Expr *Size = Importer.Import(T->getSizeExpr());
616 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000617 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000618
619 SourceRange Brackets = Importer.Import(T->getBracketsRange());
620 return Importer.getToContext().getDependentSizedArrayType(
621 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
622 Brackets);
623}
624
John McCall424cec92011-01-19 06:33:43 +0000625QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000626 QualType ToElementType = Importer.Import(T->getElementType());
627 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000628 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000629
630 return Importer.getToContext().getVectorType(ToElementType,
631 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000632 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000633}
634
John McCall424cec92011-01-19 06:33:43 +0000635QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000636 QualType ToElementType = Importer.Import(T->getElementType());
637 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000638 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000639
640 return Importer.getToContext().getExtVectorType(ToElementType,
641 T->getNumElements());
642}
643
John McCall424cec92011-01-19 06:33:43 +0000644QualType
645ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000646 // FIXME: What happens if we're importing a function without a prototype
647 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000648 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000649 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000650 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000651
Douglas Gregor96e578d2010-02-05 17:54:41 +0000652 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000653 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000654}
655
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000656QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000657 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000658 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000659 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000660
661 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000662 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000663 for (const auto &A : T->param_types()) {
664 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000665 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000666 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 ArgTypes.push_back(ArgType);
668 }
669
670 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000671 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000672 for (const auto &E : T->exceptions()) {
673 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000674 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000675 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000676 ExceptionTypes.push_back(ExceptionType);
677 }
John McCalldb40c7f2010-12-14 08:05:40 +0000678
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000679 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
680 FunctionProtoType::ExtProtoInfo ToEPI;
681
682 ToEPI.ExtInfo = FromEPI.ExtInfo;
683 ToEPI.Variadic = FromEPI.Variadic;
684 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
685 ToEPI.TypeQuals = FromEPI.TypeQuals;
686 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000687 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
688 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
689 ToEPI.ExceptionSpec.NoexceptExpr =
690 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
691 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
692 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
693 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
694 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000695
Jordan Rose5c382722013-03-08 21:51:21 +0000696 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000697}
698
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000699QualType ASTNodeImporter::VisitUnresolvedUsingType(
700 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000701 const auto *ToD =
702 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000703 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000704 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000705
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000706 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000707 cast_or_null<UnresolvedUsingTypenameDecl>(
708 Importer.Import(T->getDecl()->getPreviousDecl()));
709 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000710 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000711
712 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
713}
714
Sean Callananda6df8a2011-08-11 16:56:07 +0000715QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
716 QualType ToInnerType = Importer.Import(T->getInnerType());
717 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000718 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000719
720 return Importer.getToContext().getParenType(ToInnerType);
721}
722
John McCall424cec92011-01-19 06:33:43 +0000723QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000724 auto *ToDecl =
725 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000726 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000727 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000728
729 return Importer.getToContext().getTypeDeclType(ToDecl);
730}
731
John McCall424cec92011-01-19 06:33:43 +0000732QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000733 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
734 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000735 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000736
737 return Importer.getToContext().getTypeOfExprType(ToExpr);
738}
739
John McCall424cec92011-01-19 06:33:43 +0000740QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000741 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
742 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000743 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000744
745 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
746}
747
John McCall424cec92011-01-19 06:33:43 +0000748QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000749 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000750 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
751 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000752 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000753
Douglas Gregor81495f32012-02-12 18:42:33 +0000754 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
755 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000756 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000757
758 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000759}
760
Alexis Hunte852b102011-05-24 22:41:36 +0000761QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
762 QualType ToBaseType = Importer.Import(T->getBaseType());
763 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
764 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000765 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000766
767 return Importer.getToContext().getUnaryTransformType(ToBaseType,
768 ToUnderlyingType,
769 T->getUTTKind());
770}
771
Richard Smith30482bc2011-02-20 03:19:35 +0000772QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000773 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000774 QualType FromDeduced = T->getDeducedType();
775 QualType ToDeduced;
776 if (!FromDeduced.isNull()) {
777 ToDeduced = Importer.Import(FromDeduced);
778 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000779 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000780 }
781
Richard Smithe301ba22015-11-11 02:02:15 +0000782 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000783 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000784}
785
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000786QualType ASTNodeImporter::VisitInjectedClassNameType(
787 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000788 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000789 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000790 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000791
792 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
793 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000794 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000795
796 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
797 // See comments in InjectedClassNameType definition for details
798 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
799 enum {
800 TypeAlignmentInBits = 4,
801 TypeAlignment = 1 << TypeAlignmentInBits
802 };
803
804 return QualType(new (Importer.getToContext(), TypeAlignment)
805 InjectedClassNameType(D, InjType), 0);
806}
807
John McCall424cec92011-01-19 06:33:43 +0000808QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000809 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000810 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000811 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000812
813 return Importer.getToContext().getTagDeclType(ToDecl);
814}
815
John McCall424cec92011-01-19 06:33:43 +0000816QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000817 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000818 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000819 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000820
821 return Importer.getToContext().getTagDeclType(ToDecl);
822}
823
Sean Callanan72fe0852015-04-02 23:50:08 +0000824QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
825 QualType FromModifiedType = T->getModifiedType();
826 QualType FromEquivalentType = T->getEquivalentType();
827 QualType ToModifiedType;
828 QualType ToEquivalentType;
829
830 if (!FromModifiedType.isNull()) {
831 ToModifiedType = Importer.Import(FromModifiedType);
832 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000833 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000834 }
835 if (!FromEquivalentType.isNull()) {
836 ToEquivalentType = Importer.Import(FromEquivalentType);
837 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000838 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000839 }
840
841 return Importer.getToContext().getAttributedType(T->getAttrKind(),
842 ToModifiedType, ToEquivalentType);
843}
844
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000845QualType ASTNodeImporter::VisitTemplateTypeParmType(
846 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000847 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000848 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
849 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000850 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000851
852 return Importer.getToContext().getTemplateTypeParmType(
853 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
854}
855
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000856QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
857 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000858 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000859 cast_or_null<TemplateTypeParmType>(Importer.Import(
860 QualType(T->getReplacedParameter(), 0)).getTypePtr());
861 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000862 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000863
864 QualType Replacement = Importer.Import(T->getReplacementType());
865 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000866 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000867 Replacement = Replacement.getCanonicalType();
868
869 return Importer.getToContext().getSubstTemplateTypeParmType(
870 Replaced, Replacement);
871}
872
Douglas Gregore2e50d332010-12-01 01:36:18 +0000873QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000874 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000875 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
876 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000877 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000878
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000879 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000880 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000881 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000882
883 QualType ToCanonType;
884 if (!QualType(T, 0).isCanonical()) {
885 QualType FromCanonType
886 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
887 ToCanonType =Importer.Import(FromCanonType);
888 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000889 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000890 }
891 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000892 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000893 ToCanonType);
894}
895
John McCall424cec92011-01-19 06:33:43 +0000896QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000897 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000898 // Note: the qualifier in an ElaboratedType is optional.
899 if (T->getQualifier()) {
900 ToQualifier = Importer.Import(T->getQualifier());
901 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000902 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +0000903 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000904
905 QualType ToNamedType = Importer.Import(T->getNamedType());
906 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000907 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000908
Abramo Bagnara6150c882010-05-11 21:36:43 +0000909 return Importer.getToContext().getElaboratedType(T->getKeyword(),
910 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000911}
912
Gabor Horvath7a91c082017-11-14 11:30:38 +0000913QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
914 QualType Pattern = Importer.Import(T->getPattern());
915 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000916 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +0000917
918 return Importer.getToContext().getPackExpansionType(Pattern,
919 T->getNumExpansions());
920}
921
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000922QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
923 const DependentTemplateSpecializationType *T) {
924 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
925 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000926 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000927
928 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
929 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000930 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000931
932 SmallVector<TemplateArgument, 2> ToPack;
933 ToPack.reserve(T->getNumArgs());
934 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000935 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000936
937 return Importer.getToContext().getDependentTemplateSpecializationType(
938 T->getKeyword(), Qualifier, Name, ToPack);
939}
940
John McCall424cec92011-01-19 06:33:43 +0000941QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000942 auto *Class =
943 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000944 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000945 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000946
John McCall8b07ec22010-05-15 11:32:37 +0000947 return Importer.getToContext().getObjCInterfaceType(Class);
948}
949
John McCall424cec92011-01-19 06:33:43 +0000950QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000951 QualType ToBaseType = Importer.Import(T->getBaseType());
952 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000953 return {};
John McCall8b07ec22010-05-15 11:32:37 +0000954
Douglas Gregore9d95f12015-07-07 03:57:35 +0000955 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000956 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000957 QualType ImportedTypeArg = Importer.Import(TypeArg);
958 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000959 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +0000960
961 TypeArgs.push_back(ImportedTypeArg);
962 }
963
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000964 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000965 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000966 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000968 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000969 Protocols.push_back(Protocol);
970 }
971
Douglas Gregore9d95f12015-07-07 03:57:35 +0000972 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000973 Protocols,
974 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000975}
976
John McCall424cec92011-01-19 06:33:43 +0000977QualType
978ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000979 QualType ToPointeeType = Importer.Import(T->getPointeeType());
980 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000981 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000982
John McCall8b07ec22010-05-15 11:32:37 +0000983 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000984}
985
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000986//----------------------------------------------------------------------------
987// Import Declarations
988//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000989bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
990 DeclContext *&LexicalDC,
991 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000992 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000993 SourceLocation &Loc) {
994 // Import the context of this declaration.
995 DC = Importer.ImportContext(D->getDeclContext());
996 if (!DC)
997 return true;
998
999 LexicalDC = DC;
1000 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1001 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1002 if (!LexicalDC)
1003 return true;
1004 }
1005
1006 // Import the name of this declaration.
1007 Name = Importer.Import(D->getDeclName());
1008 if (D->getDeclName() && !Name)
1009 return true;
1010
1011 // Import the location of this declaration.
1012 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001013 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001014 return false;
1015}
1016
Douglas Gregord451ea92011-07-29 23:31:30 +00001017void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1018 if (!FromD)
1019 return;
1020
1021 if (!ToD) {
1022 ToD = Importer.Import(FromD);
1023 if (!ToD)
1024 return;
1025 }
1026
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001027 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1028 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001029 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001030 ImportDefinition(FromRecord, ToRecord);
1031 }
1032 }
1033 return;
1034 }
1035
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001036 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1037 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001038 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1039 ImportDefinition(FromEnum, ToEnum);
1040 }
1041 }
1042 return;
1043 }
1044}
1045
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001046void
1047ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1048 DeclarationNameInfo& To) {
1049 // NOTE: To.Name and To.Loc are already imported.
1050 // We only have to import To.LocInfo.
1051 switch (To.getName().getNameKind()) {
1052 case DeclarationName::Identifier:
1053 case DeclarationName::ObjCZeroArgSelector:
1054 case DeclarationName::ObjCOneArgSelector:
1055 case DeclarationName::ObjCMultiArgSelector:
1056 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001057 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001058 return;
1059
1060 case DeclarationName::CXXOperatorName: {
1061 SourceRange Range = From.getCXXOperatorNameRange();
1062 To.setCXXOperatorNameRange(Importer.Import(Range));
1063 return;
1064 }
1065 case DeclarationName::CXXLiteralOperatorName: {
1066 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1067 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1068 return;
1069 }
1070 case DeclarationName::CXXConstructorName:
1071 case DeclarationName::CXXDestructorName:
1072 case DeclarationName::CXXConversionFunctionName: {
1073 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1074 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1075 return;
1076 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001077 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001078 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001079}
1080
Douglas Gregor2e15c842012-02-01 21:00:38 +00001081void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001082 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001083 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001084 return;
1085 }
1086
Aaron Ballman629afae2014-03-07 19:56:05 +00001087 for (auto *From : FromDC->decls())
1088 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001089}
1090
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001091static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1092 ASTImporter &Importer) {
1093 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1094 auto *ToTypedef =
1095 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1096 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1097
1098 To->setTypedefNameForAnonDecl(ToTypedef);
1099 }
1100}
1101
Douglas Gregord451ea92011-07-29 23:31:30 +00001102bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001103 ImportDefinitionKind Kind) {
1104 if (To->getDefinition() || To->isBeingDefined()) {
1105 if (Kind == IDK_Everything)
1106 ImportDeclContext(From, /*ForceImport=*/true);
1107
Douglas Gregore2e50d332010-12-01 01:36:18 +00001108 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001109 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001110
1111 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001112
1113 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001114
1115 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001116 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1117 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001118
1119 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1120 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1121 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001122 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001123 ToData.Aggregate = FromData.Aggregate;
1124 ToData.PlainOldData = FromData.PlainOldData;
1125 ToData.Empty = FromData.Empty;
1126 ToData.Polymorphic = FromData.Polymorphic;
1127 ToData.Abstract = FromData.Abstract;
1128 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001129 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1130 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1131 ToData.HasBasesWithNonStaticDataMembers =
1132 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001133 ToData.HasPrivateFields = FromData.HasPrivateFields;
1134 ToData.HasProtectedFields = FromData.HasProtectedFields;
1135 ToData.HasPublicFields = FromData.HasPublicFields;
1136 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001137 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001138 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001139 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001140 ToData.HasUninitializedReferenceMember
1141 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001142 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001143 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1144 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001145 ToData.NeedOverloadResolutionForCopyConstructor
1146 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001147 ToData.NeedOverloadResolutionForMoveConstructor
1148 = FromData.NeedOverloadResolutionForMoveConstructor;
1149 ToData.NeedOverloadResolutionForMoveAssignment
1150 = FromData.NeedOverloadResolutionForMoveAssignment;
1151 ToData.NeedOverloadResolutionForDestructor
1152 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001153 ToData.DefaultedCopyConstructorIsDeleted
1154 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001155 ToData.DefaultedMoveConstructorIsDeleted
1156 = FromData.DefaultedMoveConstructorIsDeleted;
1157 ToData.DefaultedMoveAssignmentIsDeleted
1158 = FromData.DefaultedMoveAssignmentIsDeleted;
1159 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001160 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1161 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001162 ToData.HasConstexprNonCopyMoveConstructor
1163 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001164 ToData.HasDefaultedDefaultConstructor
1165 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001166 ToData.DefaultedDefaultConstructorIsConstexpr
1167 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001168 ToData.HasConstexprDefaultConstructor
1169 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001170 ToData.HasNonLiteralTypeFieldsOrBases
1171 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001172 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001173 ToData.UserProvidedDefaultConstructor
1174 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001175 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001176 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1177 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1178 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1179 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001180 ToData.ImplicitCopyAssignmentHasConstParam
1181 = FromData.ImplicitCopyAssignmentHasConstParam;
1182 ToData.HasDeclaredCopyConstructorWithConstParam
1183 = FromData.HasDeclaredCopyConstructorWithConstParam;
1184 ToData.HasDeclaredCopyAssignmentWithConstParam
1185 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001186
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001187 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001188 for (const auto &Base1 : FromCXX->bases()) {
1189 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001190 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001191 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001192
1193 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001194 if (Base1.isPackExpansion())
1195 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001196
1197 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001198 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001199
Douglas Gregore2e50d332010-12-01 01:36:18 +00001200 Bases.push_back(
1201 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001202 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1203 Base1.isVirtual(),
1204 Base1.isBaseOfClass(),
1205 Base1.getAccessSpecifierAsWritten(),
1206 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001207 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001208 }
1209 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001210 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001211 }
1212
Douglas Gregor2e15c842012-02-01 21:00:38 +00001213 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001214 ImportDeclContext(From, /*ForceImport=*/true);
1215
Douglas Gregore2e50d332010-12-01 01:36:18 +00001216 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001217 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001218}
1219
Larisse Voufo39a1e502013-08-06 01:03:05 +00001220bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1221 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001222 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001223 return false;
1224
1225 // FIXME: Can we really import any initializer? Alternatively, we could force
1226 // ourselves to import every declaration of a variable and then only use
1227 // getInit() here.
1228 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1229
1230 // FIXME: Other bits to merge?
1231
1232 return false;
1233}
1234
Douglas Gregord451ea92011-07-29 23:31:30 +00001235bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001236 ImportDefinitionKind Kind) {
1237 if (To->getDefinition() || To->isBeingDefined()) {
1238 if (Kind == IDK_Everything)
1239 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001240 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001241 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001242
1243 To->startDefinition();
1244
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001245 setTypedefNameForAnonDecl(From, To, Importer);
1246
Douglas Gregord451ea92011-07-29 23:31:30 +00001247 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1248 if (T.isNull())
1249 return true;
1250
1251 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1252 if (ToPromotionType.isNull())
1253 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001254
1255 if (shouldForceImportDeclContext(Kind))
1256 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001257
1258 // FIXME: we might need to merge the number of positive or negative bits
1259 // if the enumerator lists don't match.
1260 To->completeDefinition(T, ToPromotionType,
1261 From->getNumPositiveBits(),
1262 From->getNumNegativeBits());
1263 return false;
1264}
1265
Douglas Gregora082a492010-11-30 19:14:50 +00001266TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1267 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001268 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1269 if (ImportContainerChecked(*Params, ToParams))
1270 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001271
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001272 Expr *ToRequiresClause;
1273 if (Expr *const R = Params->getRequiresClause()) {
1274 ToRequiresClause = Importer.Import(R);
1275 if (!ToRequiresClause)
1276 return nullptr;
1277 } else {
1278 ToRequiresClause = nullptr;
1279 }
1280
Douglas Gregora082a492010-11-30 19:14:50 +00001281 return TemplateParameterList::Create(Importer.getToContext(),
1282 Importer.Import(Params->getTemplateLoc()),
1283 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001284 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001285 Importer.Import(Params->getRAngleLoc()),
1286 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001287}
1288
Douglas Gregore2e50d332010-12-01 01:36:18 +00001289TemplateArgument
1290ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1291 switch (From.getKind()) {
1292 case TemplateArgument::Null:
1293 return TemplateArgument();
1294
1295 case TemplateArgument::Type: {
1296 QualType ToType = Importer.Import(From.getAsType());
1297 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001298 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001299 return TemplateArgument(ToType);
1300 }
1301
1302 case TemplateArgument::Integral: {
1303 QualType ToType = Importer.Import(From.getIntegralType());
1304 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001305 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001306 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001307 }
1308
Eli Friedmanb826a002012-09-26 02:36:12 +00001309 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001310 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001311 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1312 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001313 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001314 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001315 }
1316
1317 case TemplateArgument::NullPtr: {
1318 QualType ToType = Importer.Import(From.getNullPtrType());
1319 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001320 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001321 return TemplateArgument(ToType, /*isNullPtr*/true);
1322 }
1323
Douglas Gregore2e50d332010-12-01 01:36:18 +00001324 case TemplateArgument::Template: {
1325 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1326 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001327 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001328
1329 return TemplateArgument(ToTemplate);
1330 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001331
1332 case TemplateArgument::TemplateExpansion: {
1333 TemplateName ToTemplate
1334 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1335 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001336 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001337
Douglas Gregore1d60df2011-01-14 23:41:42 +00001338 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001339 }
1340
Douglas Gregore2e50d332010-12-01 01:36:18 +00001341 case TemplateArgument::Expression:
1342 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1343 return TemplateArgument(ToExpr);
1344 return TemplateArgument();
1345
1346 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001347 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001348 ToPack.reserve(From.pack_size());
1349 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001350 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001351
1352 return TemplateArgument(
1353 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001354 }
1355 }
1356
1357 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001358}
1359
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001360Optional<TemplateArgumentLoc>
1361ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001362 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1363 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1364 TemplateArgumentLocInfo ToInfo;
1365 if (Arg.getKind() == TemplateArgument::Expression) {
1366 Expr *E = Importer.Import(FromInfo.getAsExpr());
1367 ToInfo = TemplateArgumentLocInfo(E);
1368 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001369 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001370 } else if (Arg.getKind() == TemplateArgument::Type) {
1371 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1372 ToInfo = TemplateArgumentLocInfo(TSI);
1373 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001374 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001375 } else {
1376 ToInfo = TemplateArgumentLocInfo(
1377 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1378 Importer.Import(FromInfo.getTemplateNameLoc()),
1379 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1380 }
1381 return TemplateArgumentLoc(Arg, ToInfo);
1382}
1383
Douglas Gregore2e50d332010-12-01 01:36:18 +00001384bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1385 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001386 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001387 for (unsigned I = 0; I != NumFromArgs; ++I) {
1388 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1389 if (To.isNull() && !FromArgs[I].isNull())
1390 return true;
1391
1392 ToArgs.push_back(To);
1393 }
1394
1395 return false;
1396}
1397
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001398// We cannot use Optional<> pattern here and below because
1399// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1400// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001401template <typename InContainerTy>
1402bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1403 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1404 for (const auto &FromLoc : Container) {
1405 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1406 ToTAInfo.addArgument(*ToLoc);
1407 else
1408 return true;
1409 }
1410 return false;
1411}
1412
Douglas Gregor5c73e912010-02-11 00:48:18 +00001413bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001414 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001415 // Eliminate a potential failure point where we attempt to re-import
1416 // something we're trying to import while completing ToRecord.
1417 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1418 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001419 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001420 if (ToOriginRecord)
1421 ToRecord = ToOriginRecord;
1422 }
1423
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001424 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001425 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001426 Importer.getNonEquivalentDecls(),
1427 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001428 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001429}
1430
Larisse Voufo39a1e502013-08-06 01:03:05 +00001431bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1432 bool Complain) {
1433 StructuralEquivalenceContext Ctx(
1434 Importer.getFromContext(), Importer.getToContext(),
1435 Importer.getNonEquivalentDecls(), false, Complain);
1436 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1437}
1438
Douglas Gregor98c10182010-02-12 22:17:39 +00001439bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001440 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001441 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001442 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001443 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001444}
1445
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001446bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1447 FunctionTemplateDecl *To) {
1448 StructuralEquivalenceContext Ctx(
1449 Importer.getFromContext(), Importer.getToContext(),
1450 Importer.getNonEquivalentDecls(), false, false);
1451 return Ctx.IsStructurallyEquivalent(From, To);
1452}
1453
Douglas Gregor91155082012-11-14 22:29:20 +00001454bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001455 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001456 const llvm::APSInt &FromVal = FromEC->getInitVal();
1457 const llvm::APSInt &ToVal = ToEC->getInitVal();
1458
1459 return FromVal.isSigned() == ToVal.isSigned() &&
1460 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1461 FromVal == ToVal;
1462}
1463
1464bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001465 ClassTemplateDecl *To) {
1466 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1467 Importer.getToContext(),
1468 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001469 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001470}
1471
Larisse Voufo39a1e502013-08-06 01:03:05 +00001472bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1473 VarTemplateDecl *To) {
1474 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1475 Importer.getToContext(),
1476 Importer.getNonEquivalentDecls());
1477 return Ctx.IsStructurallyEquivalent(From, To);
1478}
1479
Douglas Gregore4c83e42010-02-09 22:48:33 +00001480Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001481 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001482 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001483 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001484}
1485
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001486Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1487 // Import the context of this declaration.
1488 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1489 if (!DC)
1490 return nullptr;
1491
1492 DeclContext *LexicalDC = DC;
1493 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1494 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1495 if (!LexicalDC)
1496 return nullptr;
1497 }
1498
1499 // Import the location of this declaration.
1500 SourceLocation Loc = Importer.Import(D->getLocation());
1501
1502 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1503 ToD->setLexicalDeclContext(LexicalDC);
1504 Importer.Imported(D, ToD);
1505 LexicalDC->addDeclInternal(ToD);
1506 return ToD;
1507}
1508
Sean Callanan65198272011-11-17 23:20:56 +00001509Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1510 TranslationUnitDecl *ToD =
1511 Importer.getToContext().getTranslationUnitDecl();
1512
1513 Importer.Imported(D, ToD);
1514
1515 return ToD;
1516}
1517
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001518Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001519 SourceLocation Loc = Importer.Import(D->getLocation());
1520 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1521
1522 // Import the context of this declaration.
1523 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1524 if (!DC)
1525 return nullptr;
1526
1527 AccessSpecDecl *accessSpecDecl
1528 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1529 DC, Loc, ColonLoc);
1530
1531 if (!accessSpecDecl)
1532 return nullptr;
1533
1534 // Lexical DeclContext and Semantic DeclContext
1535 // is always the same for the accessSpec.
1536 accessSpecDecl->setLexicalDeclContext(DC);
1537 DC->addDeclInternal(accessSpecDecl);
1538
1539 return accessSpecDecl;
1540}
1541
Aleksei Sidorina693b372016-09-28 10:16:56 +00001542Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1543 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1544 if (!DC)
1545 return nullptr;
1546
1547 DeclContext *LexicalDC = DC;
1548
1549 // Import the location of this declaration.
1550 SourceLocation Loc = Importer.Import(D->getLocation());
1551
1552 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1553 if (!AssertExpr)
1554 return nullptr;
1555
1556 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001557 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001558 if (!ToMsg && FromMsg)
1559 return nullptr;
1560
1561 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1562 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1563 Importer.Import(D->getRParenLoc()), D->isFailed());
1564
1565 ToD->setLexicalDeclContext(LexicalDC);
1566 LexicalDC->addDeclInternal(ToD);
1567 Importer.Imported(D, ToD);
1568 return ToD;
1569}
1570
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001571Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1572 // Import the major distinguishing characteristics of this namespace.
1573 DeclContext *DC, *LexicalDC;
1574 DeclarationName Name;
1575 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001576 NamedDecl *ToD;
1577 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001578 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001579 if (ToD)
1580 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001581
1582 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001583 if (!Name) {
1584 // This is an anonymous namespace. Adopt an existing anonymous
1585 // namespace if we can.
1586 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001587 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001588 MergeWithNamespace = TU->getAnonymousNamespace();
1589 else
1590 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1591 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001592 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001593 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001594 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001595 for (auto *FoundDecl : FoundDecls) {
1596 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001597 continue;
1598
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001599 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001600 MergeWithNamespace = FoundNS;
1601 ConflictingDecls.clear();
1602 break;
1603 }
1604
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001605 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001606 }
1607
1608 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001609 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001610 ConflictingDecls.data(),
1611 ConflictingDecls.size());
1612 }
1613 }
1614
1615 // Create the "to" namespace, if needed.
1616 NamespaceDecl *ToNamespace = MergeWithNamespace;
1617 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001618 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001619 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001620 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001621 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001622 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001623 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001624 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001625
1626 // If this is an anonymous namespace, register it as the anonymous
1627 // namespace within its context.
1628 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001629 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001630 TU->setAnonymousNamespace(ToNamespace);
1631 else
1632 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1633 }
1634 }
1635 Importer.Imported(D, ToNamespace);
1636
1637 ImportDeclContext(D);
1638
1639 return ToNamespace;
1640}
1641
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001642Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1643 // Import the major distinguishing characteristics of this namespace.
1644 DeclContext *DC, *LexicalDC;
1645 DeclarationName Name;
1646 SourceLocation Loc;
1647 NamedDecl *LookupD;
1648 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1649 return nullptr;
1650 if (LookupD)
1651 return LookupD;
1652
1653 // NOTE: No conflict resolution is done for namespace aliases now.
1654
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001655 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1656 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001657 if (!TargetDecl)
1658 return nullptr;
1659
1660 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1661 if (!ToII)
1662 return nullptr;
1663
1664 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1665 if (D->getQualifierLoc() && !ToQLoc)
1666 return nullptr;
1667
1668 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1669 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1670 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1671 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1672
1673 ToD->setLexicalDeclContext(LexicalDC);
1674 Importer.Imported(D, ToD);
1675 LexicalDC->addDeclInternal(ToD);
1676
1677 return ToD;
1678}
1679
Richard Smithdda56e42011-04-15 14:24:37 +00001680Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001681 // Import the major distinguishing characteristics of this typedef.
1682 DeclContext *DC, *LexicalDC;
1683 DeclarationName Name;
1684 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001685 NamedDecl *ToD;
1686 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001687 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001688 if (ToD)
1689 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001690
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001691 // If this typedef is not in block scope, determine whether we've
1692 // seen a typedef with the same name (that we can merge with) or any
1693 // other entity by that name (which name lookup could conflict with).
1694 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001695 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001696 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001697 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001698 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001699 for (auto *FoundDecl : FoundDecls) {
1700 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001701 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001702 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001703 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1704 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001705 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001706 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001707
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001708 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001709 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001710
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001711 if (!ConflictingDecls.empty()) {
1712 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1713 ConflictingDecls.data(),
1714 ConflictingDecls.size());
1715 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001716 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001717 }
1718 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001719
Douglas Gregorb4964f72010-02-15 23:54:17 +00001720 // Import the underlying type of this typedef;
1721 QualType T = Importer.Import(D->getUnderlyingType());
1722 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001723 return nullptr;
1724
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001725 // Some nodes (like anonymous tags referred by typedefs) are allowed to
1726 // import their enclosing typedef directly. Check if this is the case.
1727 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
1728 return AlreadyImported;
1729
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001730 // Create the new typedef node.
1731 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001732 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001733 TypedefNameDecl *ToTypedef;
1734 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001735 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1736 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001737 else
Richard Smithdda56e42011-04-15 14:24:37 +00001738 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1739 StartL, Loc,
1740 Name.getAsIdentifierInfo(),
1741 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001742
Douglas Gregordd483172010-02-22 17:42:47 +00001743 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001744 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001745 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001746
1747 // Templated declarations should not appear in DeclContext.
1748 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1749 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1750 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001751
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001752 return ToTypedef;
1753}
1754
Richard Smithdda56e42011-04-15 14:24:37 +00001755Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1756 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1757}
1758
1759Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1760 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1761}
1762
Gabor Horvath7a91c082017-11-14 11:30:38 +00001763Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1764 // Import the major distinguishing characteristics of this typedef.
1765 DeclContext *DC, *LexicalDC;
1766 DeclarationName Name;
1767 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001768 NamedDecl *FoundD;
1769 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001770 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001771 if (FoundD)
1772 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001773
1774 // If this typedef is not in block scope, determine whether we've
1775 // seen a typedef with the same name (that we can merge with) or any
1776 // other entity by that name (which name lookup could conflict with).
1777 if (!DC->isFunctionOrMethod()) {
1778 SmallVector<NamedDecl *, 4> ConflictingDecls;
1779 unsigned IDNS = Decl::IDNS_Ordinary;
1780 SmallVector<NamedDecl *, 2> FoundDecls;
1781 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001782 for (auto *FoundDecl : FoundDecls) {
1783 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001784 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001785 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001786 return Importer.Imported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001787 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001788 }
1789
1790 if (!ConflictingDecls.empty()) {
1791 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1792 ConflictingDecls.data(),
1793 ConflictingDecls.size());
1794 if (!Name)
1795 return nullptr;
1796 }
1797 }
1798
1799 TemplateParameterList *Params = ImportTemplateParameterList(
1800 D->getTemplateParameters());
1801 if (!Params)
1802 return nullptr;
1803
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001804 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001805 Importer.Import(D->getTemplatedDecl()));
1806 if (!TemplDecl)
1807 return nullptr;
1808
1809 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1810 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1811
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001812 TemplDecl->setDescribedAliasTemplate(ToAlias);
1813
Gabor Horvath7a91c082017-11-14 11:30:38 +00001814 ToAlias->setAccess(D->getAccess());
1815 ToAlias->setLexicalDeclContext(LexicalDC);
1816 Importer.Imported(D, ToAlias);
1817 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001818 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001819}
1820
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001821Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1822 // Import the major distinguishing characteristics of this label.
1823 DeclContext *DC, *LexicalDC;
1824 DeclarationName Name;
1825 SourceLocation Loc;
1826 NamedDecl *ToD;
1827 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1828 return nullptr;
1829 if (ToD)
1830 return ToD;
1831
1832 assert(LexicalDC->isFunctionOrMethod());
1833
1834 LabelDecl *ToLabel = D->isGnuLocal()
1835 ? LabelDecl::Create(Importer.getToContext(),
1836 DC, Importer.Import(D->getLocation()),
1837 Name.getAsIdentifierInfo(),
1838 Importer.Import(D->getLocStart()))
1839 : LabelDecl::Create(Importer.getToContext(),
1840 DC, Importer.Import(D->getLocation()),
1841 Name.getAsIdentifierInfo());
1842 Importer.Imported(D, ToLabel);
1843
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001844 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001845 if (!Label)
1846 return nullptr;
1847
1848 ToLabel->setStmt(Label);
1849 ToLabel->setLexicalDeclContext(LexicalDC);
1850 LexicalDC->addDeclInternal(ToLabel);
1851 return ToLabel;
1852}
1853
Douglas Gregor98c10182010-02-12 22:17:39 +00001854Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1855 // Import the major distinguishing characteristics of this enum.
1856 DeclContext *DC, *LexicalDC;
1857 DeclarationName Name;
1858 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001859 NamedDecl *ToD;
1860 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001861 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001862 if (ToD)
1863 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001864
Douglas Gregor98c10182010-02-12 22:17:39 +00001865 // Figure out what enum name we're looking for.
1866 unsigned IDNS = Decl::IDNS_Tag;
1867 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001868 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1869 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001870 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001871 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001872 IDNS |= Decl::IDNS_Ordinary;
1873
1874 // We may already have an enum of the same name; try to find and match it.
1875 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001876 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001877 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001878 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001879 for (auto *FoundDecl : FoundDecls) {
1880 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001881 continue;
1882
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001883 Decl *Found = FoundDecl;
1884 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1885 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00001886 Found = Tag->getDecl();
1887 }
1888
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001889 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001890 if (IsStructuralMatch(D, FoundEnum))
1891 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001892 }
1893
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001894 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00001895 }
1896
1897 if (!ConflictingDecls.empty()) {
1898 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1899 ConflictingDecls.data(),
1900 ConflictingDecls.size());
1901 }
1902 }
1903
1904 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001905 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1906 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001907 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001908 D->isScoped(), D->isScopedUsingClassTag(),
1909 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001910 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001911 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001912 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001913 D2->setLexicalDeclContext(LexicalDC);
1914 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001915 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001916
1917 // Import the integer type.
1918 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1919 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001920 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001921 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001922
1923 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001924 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001925 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001926
Douglas Gregor3996e242010-02-15 22:01:00 +00001927 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001928}
1929
Douglas Gregor5c73e912010-02-11 00:48:18 +00001930Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1931 // If this record has a definition in the translation unit we're coming from,
1932 // but this particular declaration is not that definition, import the
1933 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001934 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001935 if (Definition && Definition != D) {
1936 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001937 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001938 return nullptr;
1939
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001940 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001941 }
1942
1943 // Import the major distinguishing characteristics of this record.
1944 DeclContext *DC, *LexicalDC;
1945 DeclarationName Name;
1946 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001947 NamedDecl *ToD;
1948 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001949 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001950 if (ToD)
1951 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001952
Douglas Gregor5c73e912010-02-11 00:48:18 +00001953 // Figure out what structure name we're looking for.
1954 unsigned IDNS = Decl::IDNS_Tag;
1955 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001956 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1957 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001958 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001959 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001960 IDNS |= Decl::IDNS_Ordinary;
1961
1962 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001963 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001964 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001965 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001966 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001967 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001968 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001969
1970 if (!FoundDecls.empty()) {
1971 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1972 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1973 D->getASTContext().getExternalSource()->CompleteType(D);
1974 }
1975
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001976 for (auto *FoundDecl : FoundDecls) {
1977 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001978 continue;
1979
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001980 Decl *Found = FoundDecl;
1981 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1982 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00001983 Found = Tag->getDecl();
1984 }
1985
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001986 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00001987 if (!SearchName) {
1988 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001989 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001990 if (Optional<unsigned> Index1 =
1991 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1992 D)) {
1993 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001994 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001995 if (*Index1 != *Index2)
1996 continue;
1997 }
1998 }
1999 }
2000
Sean Callanan9092d472017-05-13 00:46:33 +00002001 PrevDecl = FoundRecord;
2002
Douglas Gregor25791052010-02-12 00:09:27 +00002003 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002004 if ((SearchName && !D->isCompleteDefinition())
2005 || (D->isCompleteDefinition() &&
2006 D->isAnonymousStructOrUnion()
2007 == FoundDef->isAnonymousStructOrUnion() &&
2008 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002009 // The record types structurally match, or the "from" translation
2010 // unit only had a forward declaration anyway; call it the same
2011 // function.
2012 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002013 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002014 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002015 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002016 // We have a forward declaration of this type, so adopt that forward
2017 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002018
2019 // If one or both can be completed from external storage then try one
2020 // last time to complete and compare them before doing this.
2021
2022 if (FoundRecord->hasExternalLexicalStorage() &&
2023 !FoundRecord->isCompleteDefinition())
2024 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2025 if (D->hasExternalLexicalStorage())
2026 D->getASTContext().getExternalSource()->CompleteType(D);
2027
2028 if (FoundRecord->isCompleteDefinition() &&
2029 D->isCompleteDefinition() &&
2030 !IsStructuralMatch(D, FoundRecord))
2031 continue;
2032
Douglas Gregor25791052010-02-12 00:09:27 +00002033 AdoptDecl = FoundRecord;
2034 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002035 } else if (!SearchName) {
2036 continue;
2037 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002038 }
2039
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002040 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002041 }
2042
Douglas Gregordd6006f2012-07-17 21:16:27 +00002043 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002044 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2045 ConflictingDecls.data(),
2046 ConflictingDecls.size());
2047 }
2048 }
2049
2050 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002051 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002052 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002053 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002054 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002055 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002056 if (DCXX->isLambda()) {
2057 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2058 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2059 DC, TInfo, Loc,
2060 DCXX->isDependentLambda(),
2061 DCXX->isGenericLambda(),
2062 DCXX->getLambdaCaptureDefault());
2063 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2064 if (DCXX->getLambdaContextDecl() && !CDecl)
2065 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002066 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002067 } else if (DCXX->isInjectedClassName()) {
2068 // We have to be careful to do a similar dance to the one in
2069 // Sema::ActOnStartCXXMemberDeclarations
2070 CXXRecordDecl *const PrevDecl = nullptr;
2071 const bool DelayTypeCreation = true;
2072 D2CXX = CXXRecordDecl::Create(
2073 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2074 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2075 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002076 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002077 } else {
2078 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2079 D->getTagKind(),
2080 DC, StartLoc, Loc,
2081 Name.getAsIdentifierInfo());
2082 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002083 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002084 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002085 D2->setLexicalDeclContext(LexicalDC);
2086 if (!DCXX->getDescribedClassTemplate())
2087 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002088
2089 Importer.Imported(D, D2);
2090
2091 if (ClassTemplateDecl *FromDescribed =
2092 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002093 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2094 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002095 if (!ToDescribed)
2096 return nullptr;
2097 D2CXX->setDescribedClassTemplate(ToDescribed);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002098 } else if (MemberSpecializationInfo *MemberInfo =
2099 DCXX->getMemberSpecializationInfo()) {
2100 TemplateSpecializationKind SK =
2101 MemberInfo->getTemplateSpecializationKind();
2102 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002103 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002104 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2105 if (FromInst && !ToInst)
2106 return nullptr;
2107 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2108 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2109 Importer.Import(MemberInfo->getPointOfInstantiation()));
2110 }
Douglas Gregor25791052010-02-12 00:09:27 +00002111 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002112 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002113 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002114 D2->setLexicalDeclContext(LexicalDC);
2115 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002116 }
Douglas Gregor14454802011-02-25 02:25:35 +00002117
2118 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002119 if (D->isAnonymousStructOrUnion())
2120 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002121 if (PrevDecl) {
2122 // FIXME: do this for all Redeclarables, not just RecordDecls.
2123 D2->setPreviousDecl(PrevDecl);
2124 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002125 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002126
Douglas Gregor3996e242010-02-15 22:01:00 +00002127 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002128
Douglas Gregor95d82832012-01-24 18:36:04 +00002129 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002130 return nullptr;
2131
Douglas Gregor3996e242010-02-15 22:01:00 +00002132 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002133}
2134
Douglas Gregor98c10182010-02-12 22:17:39 +00002135Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2136 // Import the major distinguishing characteristics of this enumerator.
2137 DeclContext *DC, *LexicalDC;
2138 DeclarationName Name;
2139 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002140 NamedDecl *ToD;
2141 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002142 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002143 if (ToD)
2144 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002145
2146 QualType T = Importer.Import(D->getType());
2147 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002148 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002149
Douglas Gregor98c10182010-02-12 22:17:39 +00002150 // Determine whether there are any other declarations with the same name and
2151 // in the same context.
2152 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002153 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002154 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002155 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002156 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002157 for (auto *FoundDecl : FoundDecls) {
2158 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002159 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002160
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002161 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002162 if (IsStructuralMatch(D, FoundEnumConstant))
2163 return Importer.Imported(D, FoundEnumConstant);
2164 }
2165
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002166 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002167 }
2168
2169 if (!ConflictingDecls.empty()) {
2170 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2171 ConflictingDecls.data(),
2172 ConflictingDecls.size());
2173 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002174 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002175 }
2176 }
2177
2178 Expr *Init = Importer.Import(D->getInitExpr());
2179 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002180 return nullptr;
2181
Douglas Gregor98c10182010-02-12 22:17:39 +00002182 EnumConstantDecl *ToEnumerator
2183 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2184 Name.getAsIdentifierInfo(), T,
2185 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002186 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002187 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002188 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002189 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002190 return ToEnumerator;
2191}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002192
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002193bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2194 FunctionDecl *ToFD) {
2195 switch (FromFD->getTemplatedKind()) {
2196 case FunctionDecl::TK_NonTemplate:
2197 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002198 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002199
2200 case FunctionDecl::TK_MemberSpecialization: {
2201 auto *InstFD = cast_or_null<FunctionDecl>(
2202 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2203 if (!InstFD)
2204 return true;
2205
2206 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2207 SourceLocation POI = Importer.Import(
2208 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2209 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2210 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002211 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002212 }
2213
2214 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2215 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2216 auto *Template = cast_or_null<FunctionTemplateDecl>(
2217 Importer.Import(FTSInfo->getTemplate()));
2218 if (!Template)
2219 return true;
2220 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2221
2222 // Import template arguments.
2223 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2224 SmallVector<TemplateArgument, 8> ToTemplArgs;
2225 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2226 ToTemplArgs))
2227 return true;
2228
2229 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2230 Importer.getToContext(), ToTemplArgs);
2231
2232 TemplateArgumentListInfo ToTAInfo;
2233 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002234 if (FromTAArgsAsWritten)
2235 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002236 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002237
2238 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2239
2240 ToFD->setFunctionTemplateSpecialization(
2241 Template, ToTAList, /* InsertPos= */ nullptr,
2242 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002243 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002244 }
2245
2246 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2247 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2248 UnresolvedSet<8> TemplDecls;
2249 unsigned NumTemplates = FromInfo->getNumTemplates();
2250 for (unsigned I = 0; I < NumTemplates; I++) {
2251 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2252 Importer.Import(FromInfo->getTemplate(I))))
2253 TemplDecls.addDecl(ToFTD);
2254 else
2255 return true;
2256 }
2257
2258 // Import TemplateArgumentListInfo.
2259 TemplateArgumentListInfo ToTAInfo;
2260 if (ImportTemplateArgumentListInfo(
2261 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2262 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2263 FromInfo->getNumTemplateArgs()),
2264 ToTAInfo))
2265 return true;
2266
2267 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2268 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002269 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002270 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002271 }
Sam McCallfdc32072018-01-26 12:06:44 +00002272 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002273}
2274
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002275Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2276 // Import the major distinguishing characteristics of this function.
2277 DeclContext *DC, *LexicalDC;
2278 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002279 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002280 NamedDecl *ToD;
2281 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002282 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002283 if (ToD)
2284 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002285
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002286 const FunctionDecl *FoundWithoutBody = nullptr;
2287
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002288 // Try to find a function in our own ("to") context with the same name, same
2289 // type, and in the same context as the function we're importing.
2290 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002291 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002292 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002293 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002294 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002295 for (auto *FoundDecl : FoundDecls) {
2296 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002297 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002298
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002299 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002300 if (FoundFunction->hasExternalFormalLinkage() &&
2301 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002302 if (Importer.IsStructurallyEquivalent(D->getType(),
2303 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002304 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002305 const FunctionDecl *FromBodyDecl = nullptr;
2306 D->hasBody(FromBodyDecl);
2307 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2308 // This function is needed to merge completely.
2309 FoundWithoutBody = FoundFunction;
2310 break;
2311 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002312 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002313 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002314
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002315 // FIXME: Check for overloading more carefully, e.g., by boosting
2316 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002317
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002318 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002319 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002320 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002321
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002322 // Complain about inconsistent function types.
2323 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002324 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002325 Importer.ToDiag(FoundFunction->getLocation(),
2326 diag::note_odr_value_here)
2327 << FoundFunction->getType();
2328 }
2329 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002330
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002331 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002332 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002333
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002334 if (!ConflictingDecls.empty()) {
2335 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2336 ConflictingDecls.data(),
2337 ConflictingDecls.size());
2338 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002339 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002340 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002341 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002342
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002343 DeclarationNameInfo NameInfo(Name, Loc);
2344 // Import additional name location/type info.
2345 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2346
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002347 QualType FromTy = D->getType();
2348 bool usedDifferentExceptionSpec = false;
2349
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002350 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002351 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2352 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2353 // FunctionDecl that we are importing the FunctionProtoType for.
2354 // To avoid an infinite recursion when importing, create the FunctionDecl
2355 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002356 if (FromEPI.ExceptionSpec.SourceDecl ||
2357 FromEPI.ExceptionSpec.SourceTemplate ||
2358 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002359 FunctionProtoType::ExtProtoInfo DefaultEPI;
2360 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002361 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002362 usedDifferentExceptionSpec = true;
2363 }
2364 }
2365
Douglas Gregorb4964f72010-02-15 23:54:17 +00002366 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002367 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002368 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002369 return nullptr;
2370
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002371 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002372 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002373 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002374 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002375 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002376 return nullptr;
2377
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002378 Parameters.push_back(ToP);
2379 }
2380
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002381 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002382 if (D->getTypeSourceInfo() && !TInfo)
2383 return nullptr;
2384
2385 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002386 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002387 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002388 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002389 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2390 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002391 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002392 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002393 FromConstructor->isExplicit(),
2394 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002395 D->isImplicit(),
2396 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002397 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2398 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002399 for (auto *I : FromConstructor->inits()) {
2400 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002401 if (!ToI && I)
2402 return nullptr;
2403 CtorInitializers.push_back(ToI);
2404 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002405 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002406 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2407 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002408 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002409 ToCtor->setCtorInitializers(Memory);
2410 ToCtor->setNumCtorInitializers(NumInitializers);
2411 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002412 } else if (isa<CXXDestructorDecl>(D)) {
2413 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2414 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002415 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002416 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002417 D->isInlineSpecified(),
2418 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002419 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002420 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2421 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002422 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002423 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002424 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002425 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002426 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002427 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002428 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002429 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2430 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002431 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002432 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002433 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002434 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002435 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002436 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002437 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002438 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002439 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002440 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002441 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002442 D->hasWrittenPrototype(),
2443 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002444 }
John McCall3e11ebe2010-03-15 10:12:16 +00002445
2446 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002447 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002448 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002449 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002450 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2451 ToFunction->setTrivial(D->isTrivial());
2452 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002453 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002454
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002455 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002456 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002457 Param->setOwningFunction(ToFunction);
2458 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002459 }
David Blaikie9c70e042011-09-21 18:16:56 +00002460 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002461
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002462 if (FoundWithoutBody) {
2463 auto *Recent = const_cast<FunctionDecl *>(
2464 FoundWithoutBody->getMostRecentDecl());
2465 ToFunction->setPreviousDecl(Recent);
2466 }
2467
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002468 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2469 // params it refers to.
2470 if (TInfo) {
2471 if (auto ProtoLoc =
2472 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2473 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2474 ProtoLoc.setParam(I, Parameters[I]);
2475 }
2476 }
2477
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002478 if (usedDifferentExceptionSpec) {
2479 // Update FunctionProtoType::ExtProtoInfo.
2480 QualType T = Importer.Import(D->getType());
2481 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002482 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002483 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002484 }
2485
Sean Callanan59721b32015-04-28 18:41:46 +00002486 // Import the body, if any.
2487 if (Stmt *FromBody = D->getBody()) {
2488 if (Stmt *ToBody = Importer.Import(FromBody)) {
2489 ToFunction->setBody(ToBody);
2490 }
2491 }
2492
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002493 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002494
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002495 // If it is a template, import all related things.
2496 if (ImportTemplateInformation(D, ToFunction))
2497 return nullptr;
2498
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002499 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002500 // NOTE: If the function is templated declaration, it should be not added into
2501 // LexicalDC. But described template is imported during import of
2502 // FunctionTemplateDecl (it happens later). So, we use source declaration
2503 // to determine if we should add the result function.
2504 if (!D->getDescribedFunctionTemplate())
2505 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002506
Lang Hames19e07e12017-06-20 21:06:00 +00002507 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2508 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2509
Douglas Gregor43f54792010-02-17 02:12:47 +00002510 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002511}
2512
Douglas Gregor00eace12010-02-21 18:29:16 +00002513Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2514 return VisitFunctionDecl(D);
2515}
2516
2517Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2518 return VisitCXXMethodDecl(D);
2519}
2520
2521Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2522 return VisitCXXMethodDecl(D);
2523}
2524
2525Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2526 return VisitCXXMethodDecl(D);
2527}
2528
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002529static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002530 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002531 if (!Owner)
2532 return 0;
2533
2534 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002535 for (const auto *D : Owner->noload_decls()) {
2536 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002537 return Index;
2538
2539 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2540 ++Index;
2541 }
2542
2543 return Index;
2544}
2545
Douglas Gregor5c73e912010-02-11 00:48:18 +00002546Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2547 // Import the major distinguishing characteristics of a variable.
2548 DeclContext *DC, *LexicalDC;
2549 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002550 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002551 NamedDecl *ToD;
2552 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002553 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002554 if (ToD)
2555 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002556
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002557 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002558 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002559 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002560 for (auto *FoundDecl : FoundDecls) {
2561 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002562 // For anonymous fields, match up by index.
2563 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2564 continue;
2565
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002566 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002567 FoundField->getType())) {
2568 Importer.Imported(D, FoundField);
2569 return FoundField;
2570 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002571
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002572 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2573 << Name << D->getType() << FoundField->getType();
2574 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2575 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002576 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002577 }
2578 }
2579
Douglas Gregorb4964f72010-02-15 23:54:17 +00002580 // Import the type.
2581 QualType T = Importer.Import(D->getType());
2582 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002583 return nullptr;
2584
Douglas Gregor5c73e912010-02-11 00:48:18 +00002585 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2586 Expr *BitWidth = Importer.Import(D->getBitWidth());
2587 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002588 return nullptr;
2589
Abramo Bagnaradff19302011-03-08 08:55:46 +00002590 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2591 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002592 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002593 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002594 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002595 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002596 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002597 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002598 Expr *ToInitializer = Importer.Import(FromInitializer);
2599 if (ToInitializer)
2600 ToField->setInClassInitializer(ToInitializer);
2601 else
2602 return nullptr;
2603 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002604 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002605 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002606 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002607 return ToField;
2608}
2609
Francois Pichet783dd6e2010-11-21 06:08:52 +00002610Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2611 // Import the major distinguishing characteristics of a variable.
2612 DeclContext *DC, *LexicalDC;
2613 DeclarationName Name;
2614 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002615 NamedDecl *ToD;
2616 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002617 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002618 if (ToD)
2619 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002620
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002621 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002622 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002623 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002624 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002625 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002626 // For anonymous indirect fields, match up by index.
2627 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2628 continue;
2629
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002630 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002631 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002632 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002633 Importer.Imported(D, FoundField);
2634 return FoundField;
2635 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002636
2637 // If there are more anonymous fields to check, continue.
2638 if (!Name && I < N-1)
2639 continue;
2640
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002641 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2642 << Name << D->getType() << FoundField->getType();
2643 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2644 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002645 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002646 }
2647 }
2648
Francois Pichet783dd6e2010-11-21 06:08:52 +00002649 // Import the type.
2650 QualType T = Importer.Import(D->getType());
2651 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002652 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002653
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002654 auto **NamedChain =
2655 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002656
2657 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002658 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002659 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002660 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002661 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002662 NamedChain[i++] = cast<NamedDecl>(D);
2663 }
2664
2665 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002666 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002667 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002668
2669 for (const auto *Attr : D->attrs())
2670 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2671
Francois Pichet783dd6e2010-11-21 06:08:52 +00002672 ToIndirectField->setAccess(D->getAccess());
2673 ToIndirectField->setLexicalDeclContext(LexicalDC);
2674 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002675 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002676 return ToIndirectField;
2677}
2678
Aleksei Sidorina693b372016-09-28 10:16:56 +00002679Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2680 // Import the major distinguishing characteristics of a declaration.
2681 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2682 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2683 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2684 if (!DC || !LexicalDC)
2685 return nullptr;
2686
2687 // Determine whether we've already imported this decl.
2688 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2689 auto *RD = cast<CXXRecordDecl>(DC);
2690 FriendDecl *ImportedFriend = RD->getFirstFriend();
2691 StructuralEquivalenceContext Context(
2692 Importer.getFromContext(), Importer.getToContext(),
2693 Importer.getNonEquivalentDecls(), false, false);
2694
2695 while (ImportedFriend) {
2696 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2697 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2698 ImportedFriend->getFriendDecl()))
2699 return Importer.Imported(D, ImportedFriend);
2700
2701 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2702 if (Importer.IsStructurallyEquivalent(
2703 D->getFriendType()->getType(),
2704 ImportedFriend->getFriendType()->getType(), true))
2705 return Importer.Imported(D, ImportedFriend);
2706 }
2707 ImportedFriend = ImportedFriend->getNextFriend();
2708 }
2709
2710 // Not found. Create it.
2711 FriendDecl::FriendUnion ToFU;
2712 if (NamedDecl *FriendD = D->getFriendDecl())
2713 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2714 else
2715 ToFU = Importer.Import(D->getFriendType());
2716 if (!ToFU)
2717 return nullptr;
2718
2719 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002720 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002721 for (unsigned I = 0; I < D->NumTPLists; I++) {
2722 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2723 if (!List)
2724 return nullptr;
2725 ToTPLists[I] = List;
2726 }
2727
2728 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2729 Importer.Import(D->getLocation()),
2730 ToFU, Importer.Import(D->getFriendLoc()),
2731 ToTPLists);
2732
2733 Importer.Imported(D, FrD);
2734 RD->pushFriendDecl(FrD);
2735
2736 FrD->setAccess(D->getAccess());
2737 FrD->setLexicalDeclContext(LexicalDC);
2738 LexicalDC->addDeclInternal(FrD);
2739 return FrD;
2740}
2741
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002742Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2743 // Import the major distinguishing characteristics of an ivar.
2744 DeclContext *DC, *LexicalDC;
2745 DeclarationName Name;
2746 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002747 NamedDecl *ToD;
2748 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002749 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002750 if (ToD)
2751 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002752
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002753 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002754 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002755 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002756 for (auto *FoundDecl : FoundDecls) {
2757 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002758 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002759 FoundIvar->getType())) {
2760 Importer.Imported(D, FoundIvar);
2761 return FoundIvar;
2762 }
2763
2764 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2765 << Name << D->getType() << FoundIvar->getType();
2766 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2767 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002768 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002769 }
2770 }
2771
2772 // Import the type.
2773 QualType T = Importer.Import(D->getType());
2774 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002775 return nullptr;
2776
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002777 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2778 Expr *BitWidth = Importer.Import(D->getBitWidth());
2779 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002780 return nullptr;
2781
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002782 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2783 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002784 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002785 Loc, Name.getAsIdentifierInfo(),
2786 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002787 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002788 ToIvar->setLexicalDeclContext(LexicalDC);
2789 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002790 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002791 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002792}
2793
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002794Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2795 // Import the major distinguishing characteristics of a variable.
2796 DeclContext *DC, *LexicalDC;
2797 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002798 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002799 NamedDecl *ToD;
2800 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002801 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002802 if (ToD)
2803 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002804
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002805 // Try to find a variable in our own ("to") context with the same name and
2806 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002807 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002808 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002809 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002810 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002811 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002812 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002813 for (auto *FoundDecl : FoundDecls) {
2814 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002815 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002816
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002817 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002818 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002819 if (FoundVar->hasExternalFormalLinkage() &&
2820 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002821 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002822 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002823 MergeWithVar = FoundVar;
2824 break;
2825 }
2826
Douglas Gregor56521c52010-02-12 17:23:39 +00002827 const ArrayType *FoundArray
2828 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2829 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002830 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002831 if (FoundArray && TArray) {
2832 if (isa<IncompleteArrayType>(FoundArray) &&
2833 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002834 // Import the type.
2835 QualType T = Importer.Import(D->getType());
2836 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002837 return nullptr;
2838
Douglas Gregor56521c52010-02-12 17:23:39 +00002839 FoundVar->setType(T);
2840 MergeWithVar = FoundVar;
2841 break;
2842 } else if (isa<IncompleteArrayType>(TArray) &&
2843 isa<ConstantArrayType>(FoundArray)) {
2844 MergeWithVar = FoundVar;
2845 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002846 }
2847 }
2848
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002849 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002850 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002851 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2852 << FoundVar->getType();
2853 }
2854 }
2855
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002856 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002857 }
2858
2859 if (MergeWithVar) {
2860 // An equivalent variable with external linkage has been found. Link
2861 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002862 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002863
2864 if (VarDecl *DDef = D->getDefinition()) {
2865 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2866 Importer.ToDiag(ExistingDef->getLocation(),
2867 diag::err_odr_variable_multiple_def)
2868 << Name;
2869 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2870 } else {
2871 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002872 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002873 if (DDef->isInitKnownICE()) {
2874 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2875 Eval->CheckedICE = true;
2876 Eval->IsICE = DDef->isInitICE();
2877 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002878 }
2879 }
2880
2881 return MergeWithVar;
2882 }
2883
2884 if (!ConflictingDecls.empty()) {
2885 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2886 ConflictingDecls.data(),
2887 ConflictingDecls.size());
2888 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002889 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002890 }
2891 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002892
Douglas Gregorb4964f72010-02-15 23:54:17 +00002893 // Import the type.
2894 QualType T = Importer.Import(D->getType());
2895 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002896 return nullptr;
2897
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002898 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002899 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002900 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2901 Importer.Import(D->getInnerLocStart()),
2902 Loc, Name.getAsIdentifierInfo(),
2903 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002904 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002905 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002906 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002907 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002908 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002909
2910 // Templated declarations should never appear in the enclosing DeclContext.
2911 if (!D->getDescribedVarTemplate())
2912 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002913
Sean Callanan59721b32015-04-28 18:41:46 +00002914 if (!D->isFileVarDecl() &&
2915 D->isUsed())
2916 ToVar->setIsUsed();
2917
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002918 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002919 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002920 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002921
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002922 if (D->isConstexpr())
2923 ToVar->setConstexpr(true);
2924
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002925 return ToVar;
2926}
2927
Douglas Gregor8b228d72010-02-17 21:22:52 +00002928Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2929 // Parameters are created in the translation unit's context, then moved
2930 // into the function declaration's context afterward.
2931 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2932
2933 // Import the name of this declaration.
2934 DeclarationName Name = Importer.Import(D->getDeclName());
2935 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002936 return nullptr;
2937
Douglas Gregor8b228d72010-02-17 21:22:52 +00002938 // Import the location of this declaration.
2939 SourceLocation Loc = Importer.Import(D->getLocation());
2940
2941 // Import the parameter's type.
2942 QualType T = Importer.Import(D->getType());
2943 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002944 return nullptr;
2945
Douglas Gregor8b228d72010-02-17 21:22:52 +00002946 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002947 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2948 Name.getAsIdentifierInfo(), T,
2949 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002950 return Importer.Imported(D, ToParm);
2951}
2952
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002953Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2954 // Parameters are created in the translation unit's context, then moved
2955 // into the function declaration's context afterward.
2956 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2957
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002958 // Import the name of this declaration.
2959 DeclarationName Name = Importer.Import(D->getDeclName());
2960 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002961 return nullptr;
2962
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002963 // Import the location of this declaration.
2964 SourceLocation Loc = Importer.Import(D->getLocation());
2965
2966 // Import the parameter's type.
2967 QualType T = Importer.Import(D->getType());
2968 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002969 return nullptr;
2970
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002971 // Create the imported parameter.
2972 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2973 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002974 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002975 Loc, Name.getAsIdentifierInfo(),
2976 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002977 /*DefaultArg*/ nullptr);
2978
2979 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002980 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002981 ToParm->setKNRPromoted(D->isKNRPromoted());
2982
2983 Expr *ToDefArg = nullptr;
2984 Expr *FromDefArg = nullptr;
2985 if (D->hasUninstantiatedDefaultArg()) {
2986 FromDefArg = D->getUninstantiatedDefaultArg();
2987 ToDefArg = Importer.Import(FromDefArg);
2988 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2989 } else if (D->hasUnparsedDefaultArg()) {
2990 ToParm->setUnparsedDefaultArg();
2991 } else if (D->hasDefaultArg()) {
2992 FromDefArg = D->getDefaultArg();
2993 ToDefArg = Importer.Import(FromDefArg);
2994 ToParm->setDefaultArg(ToDefArg);
2995 }
2996 if (FromDefArg && !ToDefArg)
2997 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002998
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002999 if (D->isObjCMethodParameter()) {
3000 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3001 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3002 } else {
3003 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3004 D->getFunctionScopeIndex());
3005 }
3006
Sean Callanan59721b32015-04-28 18:41:46 +00003007 if (D->isUsed())
3008 ToParm->setIsUsed();
3009
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003010 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003011}
3012
Douglas Gregor43f54792010-02-17 02:12:47 +00003013Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3014 // Import the major distinguishing characteristics of a method.
3015 DeclContext *DC, *LexicalDC;
3016 DeclarationName Name;
3017 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003018 NamedDecl *ToD;
3019 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003020 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003021 if (ToD)
3022 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003023
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003024 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003025 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003026 for (auto *FoundDecl : FoundDecls) {
3027 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003028 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3029 continue;
3030
3031 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003032 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3033 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003034 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003035 << D->isInstanceMethod() << Name << D->getReturnType()
3036 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003037 Importer.ToDiag(FoundMethod->getLocation(),
3038 diag::note_odr_objc_method_here)
3039 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003040 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003041 }
3042
3043 // Check the number of parameters.
3044 if (D->param_size() != FoundMethod->param_size()) {
3045 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3046 << D->isInstanceMethod() << Name
3047 << D->param_size() << FoundMethod->param_size();
3048 Importer.ToDiag(FoundMethod->getLocation(),
3049 diag::note_odr_objc_method_here)
3050 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003051 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003052 }
3053
3054 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003055 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003056 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3057 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003058 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003060 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003061 diag::err_odr_objc_method_param_type_inconsistent)
3062 << D->isInstanceMethod() << Name
3063 << (*P)->getType() << (*FoundP)->getType();
3064 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3065 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003066 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003067 }
3068 }
3069
3070 // Check variadic/non-variadic.
3071 // Check the number of parameters.
3072 if (D->isVariadic() != FoundMethod->isVariadic()) {
3073 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3074 << D->isInstanceMethod() << Name;
3075 Importer.ToDiag(FoundMethod->getLocation(),
3076 diag::note_odr_objc_method_here)
3077 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003078 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003079 }
3080
3081 // FIXME: Any other bits we need to merge?
3082 return Importer.Imported(D, FoundMethod);
3083 }
3084 }
3085
3086 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003087 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003088 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003089 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003090
Alp Toker314cc812014-01-25 16:55:45 +00003091 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003092
Alp Toker314cc812014-01-25 16:55:45 +00003093 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3094 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3095 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3096 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3097 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003098
3099 // FIXME: When we decide to merge method definitions, we'll need to
3100 // deal with implicit parameters.
3101
3102 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003103 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003104 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003105 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003106 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003107 return nullptr;
3108
Douglas Gregor43f54792010-02-17 02:12:47 +00003109 ToParams.push_back(ToP);
3110 }
3111
3112 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003113 for (auto *ToParam : ToParams) {
3114 ToParam->setOwningFunction(ToMethod);
3115 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003116 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003117
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003118 SmallVector<SourceLocation, 12> SelLocs;
3119 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003120 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003121 Loc = Importer.Import(Loc);
3122
3123 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003124
3125 ToMethod->setLexicalDeclContext(LexicalDC);
3126 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003127 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003128 return ToMethod;
3129}
3130
Douglas Gregor85f3f952015-07-07 03:57:15 +00003131Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3132 // Import the major distinguishing characteristics of a category.
3133 DeclContext *DC, *LexicalDC;
3134 DeclarationName Name;
3135 SourceLocation Loc;
3136 NamedDecl *ToD;
3137 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3138 return nullptr;
3139 if (ToD)
3140 return ToD;
3141
3142 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3143 if (!BoundInfo)
3144 return nullptr;
3145
3146 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3147 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003148 D->getVariance(),
3149 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003150 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003151 Importer.Import(D->getLocation()),
3152 Name.getAsIdentifierInfo(),
3153 Importer.Import(D->getColonLoc()),
3154 BoundInfo);
3155 Importer.Imported(D, Result);
3156 Result->setLexicalDeclContext(LexicalDC);
3157 return Result;
3158}
3159
Douglas Gregor84c51c32010-02-18 01:47:50 +00003160Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3161 // Import the major distinguishing characteristics of a category.
3162 DeclContext *DC, *LexicalDC;
3163 DeclarationName Name;
3164 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003165 NamedDecl *ToD;
3166 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003167 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003168 if (ToD)
3169 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003170
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003171 auto *ToInterface =
3172 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003173 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003174 return nullptr;
3175
Douglas Gregor84c51c32010-02-18 01:47:50 +00003176 // Determine if we've already encountered this category.
3177 ObjCCategoryDecl *MergeWithCategory
3178 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3179 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3180 if (!ToCategory) {
3181 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003182 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003183 Loc,
3184 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003185 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003186 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003187 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003188 Importer.Import(D->getIvarLBraceLoc()),
3189 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003190 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003191 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003192 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003193 // Import the type parameter list after calling Imported, to avoid
3194 // loops when bringing in their DeclContext.
3195 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3196 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003197
Douglas Gregor84c51c32010-02-18 01:47:50 +00003198 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003199 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3200 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003201 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3202 = D->protocol_loc_begin();
3203 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3204 FromProtoEnd = D->protocol_end();
3205 FromProto != FromProtoEnd;
3206 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003207 auto *ToProto =
3208 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003209 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003210 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003211 Protocols.push_back(ToProto);
3212 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3213 }
3214
3215 // FIXME: If we're merging, make sure that the protocol list is the same.
3216 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3217 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003218 } else {
3219 Importer.Imported(D, ToCategory);
3220 }
3221
3222 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003223 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003224
3225 // If we have an implementation, import it as well.
3226 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003227 auto *Impl =
3228 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003229 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003230 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003231 return nullptr;
3232
Douglas Gregor84c51c32010-02-18 01:47:50 +00003233 ToCategory->setImplementation(Impl);
3234 }
3235
3236 return ToCategory;
3237}
3238
Douglas Gregor2aa53772012-01-24 17:42:07 +00003239bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3240 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003241 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003242 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003243 if (shouldForceImportDeclContext(Kind))
3244 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003245 return false;
3246 }
3247
3248 // Start the protocol definition
3249 To->startDefinition();
3250
3251 // Import protocols
3252 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3253 SmallVector<SourceLocation, 4> ProtocolLocs;
3254 ObjCProtocolDecl::protocol_loc_iterator
3255 FromProtoLoc = From->protocol_loc_begin();
3256 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3257 FromProtoEnd = From->protocol_end();
3258 FromProto != FromProtoEnd;
3259 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003260 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003261 if (!ToProto)
3262 return true;
3263 Protocols.push_back(ToProto);
3264 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3265 }
3266
3267 // FIXME: If we're merging, make sure that the protocol list is the same.
3268 To->setProtocolList(Protocols.data(), Protocols.size(),
3269 ProtocolLocs.data(), Importer.getToContext());
3270
Douglas Gregor2e15c842012-02-01 21:00:38 +00003271 if (shouldForceImportDeclContext(Kind)) {
3272 // Import all of the members of this protocol.
3273 ImportDeclContext(From, /*ForceImport=*/true);
3274 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003275 return false;
3276}
3277
Douglas Gregor98d156a2010-02-17 16:12:00 +00003278Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003279 // If this protocol has a definition in the translation unit we're coming
3280 // from, but this particular declaration is not that definition, import the
3281 // definition and map to that.
3282 ObjCProtocolDecl *Definition = D->getDefinition();
3283 if (Definition && Definition != D) {
3284 Decl *ImportedDef = Importer.Import(Definition);
3285 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003286 return nullptr;
3287
Douglas Gregor2aa53772012-01-24 17:42:07 +00003288 return Importer.Imported(D, ImportedDef);
3289 }
3290
Douglas Gregor84c51c32010-02-18 01:47:50 +00003291 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003292 DeclContext *DC, *LexicalDC;
3293 DeclarationName Name;
3294 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003295 NamedDecl *ToD;
3296 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003297 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003298 if (ToD)
3299 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003300
Craig Topper36250ad2014-05-12 05:36:57 +00003301 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003302 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003303 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003304 for (auto *FoundDecl : FoundDecls) {
3305 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003306 continue;
3307
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003308 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003309 break;
3310 }
3311
3312 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003313 if (!ToProto) {
3314 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3315 Name.getAsIdentifierInfo(), Loc,
3316 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003317 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003318 ToProto->setLexicalDeclContext(LexicalDC);
3319 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003320 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003321
3322 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003323
Douglas Gregor2aa53772012-01-24 17:42:07 +00003324 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003325 return nullptr;
3326
Douglas Gregor98d156a2010-02-17 16:12:00 +00003327 return ToProto;
3328}
3329
Sean Callanan0aae0412014-12-10 00:00:37 +00003330Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3331 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3332 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3333
3334 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3335 SourceLocation LangLoc = Importer.Import(D->getLocation());
3336
3337 bool HasBraces = D->hasBraces();
3338
Sean Callananb12a8552014-12-10 21:22:20 +00003339 LinkageSpecDecl *ToLinkageSpec =
3340 LinkageSpecDecl::Create(Importer.getToContext(),
3341 DC,
3342 ExternLoc,
3343 LangLoc,
3344 D->getLanguage(),
3345 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003346
3347 if (HasBraces) {
3348 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3349 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3350 }
3351
3352 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3353 LexicalDC->addDeclInternal(ToLinkageSpec);
3354
3355 Importer.Imported(D, ToLinkageSpec);
3356
3357 return ToLinkageSpec;
3358}
3359
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003360Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3361 DeclContext *DC, *LexicalDC;
3362 DeclarationName Name;
3363 SourceLocation Loc;
3364 NamedDecl *ToD = nullptr;
3365 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3366 return nullptr;
3367 if (ToD)
3368 return ToD;
3369
3370 DeclarationNameInfo NameInfo(Name,
3371 Importer.Import(D->getNameInfo().getLoc()));
3372 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3373
3374 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3375 Importer.Import(D->getUsingLoc()),
3376 Importer.Import(D->getQualifierLoc()),
3377 NameInfo, D->hasTypename());
3378 ToUsing->setLexicalDeclContext(LexicalDC);
3379 LexicalDC->addDeclInternal(ToUsing);
3380 Importer.Imported(D, ToUsing);
3381
3382 if (NamedDecl *FromPattern =
3383 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003384 if (auto *ToPattern =
3385 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003386 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3387 else
3388 return nullptr;
3389 }
3390
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003391 for (auto *FromShadow : D->shadows()) {
3392 if (auto *ToShadow =
3393 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003394 ToUsing->addShadowDecl(ToShadow);
3395 else
3396 // FIXME: We return a nullptr here but the definition is already created
3397 // and available with lookups. How to fix this?..
3398 return nullptr;
3399 }
3400 return ToUsing;
3401}
3402
3403Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3404 DeclContext *DC, *LexicalDC;
3405 DeclarationName Name;
3406 SourceLocation Loc;
3407 NamedDecl *ToD = nullptr;
3408 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3409 return nullptr;
3410 if (ToD)
3411 return ToD;
3412
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003413 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3414 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003415 if (!ToUsing)
3416 return nullptr;
3417
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003418 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3419 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003420 if (!ToTarget)
3421 return nullptr;
3422
3423 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3424 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3425
3426 ToShadow->setLexicalDeclContext(LexicalDC);
3427 ToShadow->setAccess(D->getAccess());
3428 Importer.Imported(D, ToShadow);
3429
3430 if (UsingShadowDecl *FromPattern =
3431 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003432 if (auto *ToPattern =
3433 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003434 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3435 ToPattern);
3436 else
3437 // FIXME: We return a nullptr here but the definition is already created
3438 // and available with lookups. How to fix this?..
3439 return nullptr;
3440 }
3441
3442 LexicalDC->addDeclInternal(ToShadow);
3443
3444 return ToShadow;
3445}
3446
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003447Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3448 DeclContext *DC, *LexicalDC;
3449 DeclarationName Name;
3450 SourceLocation Loc;
3451 NamedDecl *ToD = nullptr;
3452 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3453 return nullptr;
3454 if (ToD)
3455 return ToD;
3456
3457 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3458 if (!ToComAncestor)
3459 return nullptr;
3460
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003461 auto *ToNominated = cast_or_null<NamespaceDecl>(
3462 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003463 if (!ToNominated)
3464 return nullptr;
3465
3466 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3467 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3468 Importer.Import(D->getNamespaceKeyLocation()),
3469 Importer.Import(D->getQualifierLoc()),
3470 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3471 ToUsingDir->setLexicalDeclContext(LexicalDC);
3472 LexicalDC->addDeclInternal(ToUsingDir);
3473 Importer.Imported(D, ToUsingDir);
3474
3475 return ToUsingDir;
3476}
3477
3478Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3479 UnresolvedUsingValueDecl *D) {
3480 DeclContext *DC, *LexicalDC;
3481 DeclarationName Name;
3482 SourceLocation Loc;
3483 NamedDecl *ToD = nullptr;
3484 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3485 return nullptr;
3486 if (ToD)
3487 return ToD;
3488
3489 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3490 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3491
3492 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3493 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3494 Importer.Import(D->getQualifierLoc()), NameInfo,
3495 Importer.Import(D->getEllipsisLoc()));
3496
3497 Importer.Imported(D, ToUsingValue);
3498 ToUsingValue->setAccess(D->getAccess());
3499 ToUsingValue->setLexicalDeclContext(LexicalDC);
3500 LexicalDC->addDeclInternal(ToUsingValue);
3501
3502 return ToUsingValue;
3503}
3504
3505Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3506 UnresolvedUsingTypenameDecl *D) {
3507 DeclContext *DC, *LexicalDC;
3508 DeclarationName Name;
3509 SourceLocation Loc;
3510 NamedDecl *ToD = nullptr;
3511 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3512 return nullptr;
3513 if (ToD)
3514 return ToD;
3515
3516 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3517 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3518 Importer.Import(D->getTypenameLoc()),
3519 Importer.Import(D->getQualifierLoc()), Loc, Name,
3520 Importer.Import(D->getEllipsisLoc()));
3521
3522 Importer.Imported(D, ToUsing);
3523 ToUsing->setAccess(D->getAccess());
3524 ToUsing->setLexicalDeclContext(LexicalDC);
3525 LexicalDC->addDeclInternal(ToUsing);
3526
3527 return ToUsing;
3528}
3529
Douglas Gregor2aa53772012-01-24 17:42:07 +00003530bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3531 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003532 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003533 if (To->getDefinition()) {
3534 // Check consistency of superclass.
3535 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3536 if (FromSuper) {
3537 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3538 if (!FromSuper)
3539 return true;
3540 }
3541
3542 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3543 if ((bool)FromSuper != (bool)ToSuper ||
3544 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3545 Importer.ToDiag(To->getLocation(),
3546 diag::err_odr_objc_superclass_inconsistent)
3547 << To->getDeclName();
3548 if (ToSuper)
3549 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3550 << To->getSuperClass()->getDeclName();
3551 else
3552 Importer.ToDiag(To->getLocation(),
3553 diag::note_odr_objc_missing_superclass);
3554 if (From->getSuperClass())
3555 Importer.FromDiag(From->getSuperClassLoc(),
3556 diag::note_odr_objc_superclass)
3557 << From->getSuperClass()->getDeclName();
3558 else
3559 Importer.FromDiag(From->getLocation(),
3560 diag::note_odr_objc_missing_superclass);
3561 }
3562
Douglas Gregor2e15c842012-02-01 21:00:38 +00003563 if (shouldForceImportDeclContext(Kind))
3564 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003565 return false;
3566 }
3567
3568 // Start the definition.
3569 To->startDefinition();
3570
3571 // If this class has a superclass, import it.
3572 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003573 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3574 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003575 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003576
3577 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003578 }
3579
3580 // Import protocols
3581 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3582 SmallVector<SourceLocation, 4> ProtocolLocs;
3583 ObjCInterfaceDecl::protocol_loc_iterator
3584 FromProtoLoc = From->protocol_loc_begin();
3585
3586 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3587 FromProtoEnd = From->protocol_end();
3588 FromProto != FromProtoEnd;
3589 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003590 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003591 if (!ToProto)
3592 return true;
3593 Protocols.push_back(ToProto);
3594 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3595 }
3596
3597 // FIXME: If we're merging, make sure that the protocol list is the same.
3598 To->setProtocolList(Protocols.data(), Protocols.size(),
3599 ProtocolLocs.data(), Importer.getToContext());
3600
3601 // Import categories. When the categories themselves are imported, they'll
3602 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003603 for (auto *Cat : From->known_categories())
3604 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003605
Douglas Gregor2aa53772012-01-24 17:42:07 +00003606 // If we have an @implementation, import it as well.
3607 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003608 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3609 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003610 if (!Impl)
3611 return true;
3612
3613 To->setImplementation(Impl);
3614 }
3615
Douglas Gregor2e15c842012-02-01 21:00:38 +00003616 if (shouldForceImportDeclContext(Kind)) {
3617 // Import all of the members of this class.
3618 ImportDeclContext(From, /*ForceImport=*/true);
3619 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003620 return false;
3621}
3622
Douglas Gregor85f3f952015-07-07 03:57:15 +00003623ObjCTypeParamList *
3624ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3625 if (!list)
3626 return nullptr;
3627
3628 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3629 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003630 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3631 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003632 if (!toTypeParam)
3633 return nullptr;
3634
3635 toTypeParams.push_back(toTypeParam);
3636 }
3637
3638 return ObjCTypeParamList::create(Importer.getToContext(),
3639 Importer.Import(list->getLAngleLoc()),
3640 toTypeParams,
3641 Importer.Import(list->getRAngleLoc()));
3642}
3643
Douglas Gregor45635322010-02-16 01:20:57 +00003644Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003645 // If this class has a definition in the translation unit we're coming from,
3646 // but this particular declaration is not that definition, import the
3647 // definition and map to that.
3648 ObjCInterfaceDecl *Definition = D->getDefinition();
3649 if (Definition && Definition != D) {
3650 Decl *ImportedDef = Importer.Import(Definition);
3651 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003652 return nullptr;
3653
Douglas Gregor2aa53772012-01-24 17:42:07 +00003654 return Importer.Imported(D, ImportedDef);
3655 }
3656
Douglas Gregor45635322010-02-16 01:20:57 +00003657 // Import the major distinguishing characteristics of an @interface.
3658 DeclContext *DC, *LexicalDC;
3659 DeclarationName Name;
3660 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003661 NamedDecl *ToD;
3662 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003663 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003664 if (ToD)
3665 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003666
Douglas Gregor2aa53772012-01-24 17:42:07 +00003667 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003668 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003669 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003670 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003671 for (auto *FoundDecl : FoundDecls) {
3672 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003673 continue;
3674
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003675 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003676 break;
3677 }
3678
Douglas Gregor2aa53772012-01-24 17:42:07 +00003679 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003680 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003681 if (!ToIface) {
3682 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3683 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003684 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003685 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003686 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003687 D->isImplicitInterfaceDecl());
3688 ToIface->setLexicalDeclContext(LexicalDC);
3689 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003690 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003691 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003692 // Import the type parameter list after calling Imported, to avoid
3693 // loops when bringing in their DeclContext.
3694 ToIface->setTypeParamList(ImportObjCTypeParamList(
3695 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003696
Douglas Gregor2aa53772012-01-24 17:42:07 +00003697 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003698 return nullptr;
3699
Douglas Gregor98d156a2010-02-17 16:12:00 +00003700 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003701}
3702
Douglas Gregor4da9d682010-12-07 15:32:12 +00003703Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003704 auto *Category = cast_or_null<ObjCCategoryDecl>(
3705 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003706 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003707 return nullptr;
3708
Douglas Gregor4da9d682010-12-07 15:32:12 +00003709 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3710 if (!ToImpl) {
3711 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3712 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003713 return nullptr;
3714
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003715 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003716 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003717 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003718 Category->getClassInterface(),
3719 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003720 Importer.Import(D->getAtStartLoc()),
3721 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003722
3723 DeclContext *LexicalDC = DC;
3724 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3725 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3726 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003727 return nullptr;
3728
Douglas Gregor4da9d682010-12-07 15:32:12 +00003729 ToImpl->setLexicalDeclContext(LexicalDC);
3730 }
3731
Sean Callanan95e74be2011-10-21 02:57:43 +00003732 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003733 Category->setImplementation(ToImpl);
3734 }
3735
3736 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003737 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003738 return ToImpl;
3739}
3740
Douglas Gregorda8025c2010-12-07 01:26:03 +00003741Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3742 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003743 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3744 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003745 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003746 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003747
3748 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003749 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003750 if (D->getSuperClass()) {
3751 Super = cast_or_null<ObjCInterfaceDecl>(
3752 Importer.Import(D->getSuperClass()));
3753 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003754 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003755 }
3756
3757 ObjCImplementationDecl *Impl = Iface->getImplementation();
3758 if (!Impl) {
3759 // We haven't imported an implementation yet. Create a new @implementation
3760 // now.
3761 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3762 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003763 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003764 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003765 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003766 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003767 Importer.Import(D->getIvarLBraceLoc()),
3768 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003769
3770 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3771 DeclContext *LexicalDC
3772 = Importer.ImportContext(D->getLexicalDeclContext());
3773 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003774 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003775 Impl->setLexicalDeclContext(LexicalDC);
3776 }
3777
3778 // Associate the implementation with the class it implements.
3779 Iface->setImplementation(Impl);
3780 Importer.Imported(D, Iface->getImplementation());
3781 } else {
3782 Importer.Imported(D, Iface->getImplementation());
3783
3784 // Verify that the existing @implementation has the same superclass.
3785 if ((Super && !Impl->getSuperClass()) ||
3786 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003787 (Super && Impl->getSuperClass() &&
3788 !declaresSameEntity(Super->getCanonicalDecl(),
3789 Impl->getSuperClass()))) {
3790 Importer.ToDiag(Impl->getLocation(),
3791 diag::err_odr_objc_superclass_inconsistent)
3792 << Iface->getDeclName();
3793 // FIXME: It would be nice to have the location of the superclass
3794 // below.
3795 if (Impl->getSuperClass())
3796 Importer.ToDiag(Impl->getLocation(),
3797 diag::note_odr_objc_superclass)
3798 << Impl->getSuperClass()->getDeclName();
3799 else
3800 Importer.ToDiag(Impl->getLocation(),
3801 diag::note_odr_objc_missing_superclass);
3802 if (D->getSuperClass())
3803 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003804 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003805 << D->getSuperClass()->getDeclName();
3806 else
3807 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003808 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003809 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003810 }
3811 }
3812
3813 // Import all of the members of this @implementation.
3814 ImportDeclContext(D);
3815
3816 return Impl;
3817}
3818
Douglas Gregora11c4582010-02-17 18:02:10 +00003819Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3820 // Import the major distinguishing characteristics of an @property.
3821 DeclContext *DC, *LexicalDC;
3822 DeclarationName Name;
3823 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003824 NamedDecl *ToD;
3825 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003826 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003827 if (ToD)
3828 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003829
3830 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003831 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003832 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003833 for (auto *FoundDecl : FoundDecls) {
3834 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003835 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003836 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003837 FoundProp->getType())) {
3838 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3839 << Name << D->getType() << FoundProp->getType();
3840 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3841 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003842 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003843 }
3844
3845 // FIXME: Check property attributes, getters, setters, etc.?
3846
3847 // Consider these properties to be equivalent.
3848 Importer.Imported(D, FoundProp);
3849 return FoundProp;
3850 }
3851 }
3852
3853 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003854 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3855 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003856 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003857
3858 // Create the new property.
3859 ObjCPropertyDecl *ToProperty
3860 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3861 Name.getAsIdentifierInfo(),
3862 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003863 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003864 Importer.Import(D->getType()),
3865 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003866 D->getPropertyImplementation());
3867 Importer.Imported(D, ToProperty);
3868 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003869 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003870
3871 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003872 ToProperty->setPropertyAttributesAsWritten(
3873 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003874 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3875 Importer.Import(D->getGetterNameLoc()));
3876 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3877 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003878 ToProperty->setGetterMethodDecl(
3879 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3880 ToProperty->setSetterMethodDecl(
3881 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3882 ToProperty->setPropertyIvarDecl(
3883 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3884 return ToProperty;
3885}
3886
Douglas Gregor14a49e22010-12-07 18:32:03 +00003887Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003888 auto *Property = cast_or_null<ObjCPropertyDecl>(
3889 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003890 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003891 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003892
3893 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3894 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003895 return nullptr;
3896
Douglas Gregor14a49e22010-12-07 18:32:03 +00003897 // Import the lexical declaration context.
3898 DeclContext *LexicalDC = DC;
3899 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3900 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3901 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003902 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003903 }
3904
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003905 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003906 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003907 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003908
3909 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003910 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003911 if (D->getPropertyIvarDecl()) {
3912 Ivar = cast_or_null<ObjCIvarDecl>(
3913 Importer.Import(D->getPropertyIvarDecl()));
3914 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003915 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003916 }
3917
3918 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003919 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3920 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003921 if (!ToImpl) {
3922 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3923 Importer.Import(D->getLocStart()),
3924 Importer.Import(D->getLocation()),
3925 Property,
3926 D->getPropertyImplementation(),
3927 Ivar,
3928 Importer.Import(D->getPropertyIvarDeclLoc()));
3929 ToImpl->setLexicalDeclContext(LexicalDC);
3930 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003931 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003932 } else {
3933 // Check that we have the same kind of property implementation (@synthesize
3934 // vs. @dynamic).
3935 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3936 Importer.ToDiag(ToImpl->getLocation(),
3937 diag::err_odr_objc_property_impl_kind_inconsistent)
3938 << Property->getDeclName()
3939 << (ToImpl->getPropertyImplementation()
3940 == ObjCPropertyImplDecl::Dynamic);
3941 Importer.FromDiag(D->getLocation(),
3942 diag::note_odr_objc_property_impl_kind)
3943 << D->getPropertyDecl()->getDeclName()
3944 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003945 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003946 }
3947
3948 // For @synthesize, check that we have the same
3949 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3950 Ivar != ToImpl->getPropertyIvarDecl()) {
3951 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3952 diag::err_odr_objc_synthesize_ivar_inconsistent)
3953 << Property->getDeclName()
3954 << ToImpl->getPropertyIvarDecl()->getDeclName()
3955 << Ivar->getDeclName();
3956 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3957 diag::note_odr_objc_synthesize_ivar_here)
3958 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003959 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003960 }
3961
3962 // Merge the existing implementation with the new implementation.
3963 Importer.Imported(D, ToImpl);
3964 }
3965
3966 return ToImpl;
3967}
3968
Douglas Gregora082a492010-11-30 19:14:50 +00003969Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3970 // For template arguments, we adopt the translation unit as our declaration
3971 // context. This context will be fixed when the actual template declaration
3972 // is created.
3973
3974 // FIXME: Import default argument.
3975 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3976 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003977 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003978 Importer.Import(D->getLocation()),
3979 D->getDepth(),
3980 D->getIndex(),
3981 Importer.Import(D->getIdentifier()),
3982 D->wasDeclaredWithTypename(),
3983 D->isParameterPack());
3984}
3985
3986Decl *
3987ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3988 // Import the name of this declaration.
3989 DeclarationName Name = Importer.Import(D->getDeclName());
3990 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003991 return nullptr;
3992
Douglas Gregora082a492010-11-30 19:14:50 +00003993 // Import the location of this declaration.
3994 SourceLocation Loc = Importer.Import(D->getLocation());
3995
3996 // Import the type of this declaration.
3997 QualType T = Importer.Import(D->getType());
3998 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003999 return nullptr;
4000
Douglas Gregora082a492010-11-30 19:14:50 +00004001 // Import type-source information.
4002 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4003 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004004 return nullptr;
4005
Douglas Gregora082a492010-11-30 19:14:50 +00004006 // FIXME: Import default argument.
4007
4008 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4009 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004010 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004011 Loc, D->getDepth(), D->getPosition(),
4012 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004013 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004014}
4015
4016Decl *
4017ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4018 // Import the name of this declaration.
4019 DeclarationName Name = Importer.Import(D->getDeclName());
4020 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004021 return nullptr;
4022
Douglas Gregora082a492010-11-30 19:14:50 +00004023 // Import the location of this declaration.
4024 SourceLocation Loc = Importer.Import(D->getLocation());
4025
4026 // Import template parameters.
4027 TemplateParameterList *TemplateParams
4028 = ImportTemplateParameterList(D->getTemplateParameters());
4029 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004030 return nullptr;
4031
Douglas Gregora082a492010-11-30 19:14:50 +00004032 // FIXME: Import default argument.
4033
4034 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4035 Importer.getToContext().getTranslationUnitDecl(),
4036 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004037 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004038 Name.getAsIdentifierInfo(),
4039 TemplateParams);
4040}
4041
4042Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4043 // If this record has a definition in the translation unit we're coming from,
4044 // but this particular declaration is not that definition, import the
4045 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004046 auto *Definition =
4047 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004048 if (Definition && Definition != D->getTemplatedDecl()) {
4049 Decl *ImportedDef
4050 = Importer.Import(Definition->getDescribedClassTemplate());
4051 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004052 return nullptr;
4053
Douglas Gregora082a492010-11-30 19:14:50 +00004054 return Importer.Imported(D, ImportedDef);
4055 }
4056
4057 // Import the major distinguishing characteristics of this class template.
4058 DeclContext *DC, *LexicalDC;
4059 DeclarationName Name;
4060 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004061 NamedDecl *ToD;
4062 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004063 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004064 if (ToD)
4065 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004066
Douglas Gregora082a492010-11-30 19:14:50 +00004067 // We may already have a template of the same name; try to find and match it.
4068 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004069 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004070 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004071 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004072 for (auto *FoundDecl : FoundDecls) {
4073 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004074 continue;
4075
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004076 Decl *Found = FoundDecl;
4077 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Douglas Gregora082a492010-11-30 19:14:50 +00004078 if (IsStructuralMatch(D, FoundTemplate)) {
4079 // The class templates structurally match; call it the same template.
4080 // FIXME: We may be filling in a forward declaration here. Handle
4081 // this case!
4082 Importer.Imported(D->getTemplatedDecl(),
4083 FoundTemplate->getTemplatedDecl());
4084 return Importer.Imported(D, FoundTemplate);
4085 }
4086 }
4087
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004088 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004089 }
4090
4091 if (!ConflictingDecls.empty()) {
4092 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4093 ConflictingDecls.data(),
4094 ConflictingDecls.size());
4095 }
4096
4097 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004098 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004099 }
4100
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004101 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4102
Douglas Gregora082a492010-11-30 19:14:50 +00004103 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004104 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4105 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004106 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004107 return nullptr;
4108
4109 // Resolve possible cyclic import.
4110 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4111 return AlreadyImported;
4112
Douglas Gregora082a492010-11-30 19:14:50 +00004113 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004114 TemplateParameterList *TemplateParams =
4115 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004116 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004117 return nullptr;
4118
Douglas Gregora082a492010-11-30 19:14:50 +00004119 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4120 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004121 ToTemplated);
4122 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004123
4124 D2->setAccess(D->getAccess());
4125 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004126 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004127
4128 // Note the relationship between the class templates.
4129 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004130 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004131
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004132 if (FromTemplated->isCompleteDefinition() &&
4133 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004134 // FIXME: Import definition!
4135 }
4136
4137 return D2;
4138}
4139
Douglas Gregore2e50d332010-12-01 01:36:18 +00004140Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4141 ClassTemplateSpecializationDecl *D) {
4142 // If this record has a definition in the translation unit we're coming from,
4143 // but this particular declaration is not that definition, import the
4144 // definition and map to that.
4145 TagDecl *Definition = D->getDefinition();
4146 if (Definition && Definition != D) {
4147 Decl *ImportedDef = Importer.Import(Definition);
4148 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004149 return nullptr;
4150
Douglas Gregore2e50d332010-12-01 01:36:18 +00004151 return Importer.Imported(D, ImportedDef);
4152 }
4153
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004154 auto *ClassTemplate =
4155 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004156 D->getSpecializedTemplate()));
4157 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004158 return nullptr;
4159
Douglas Gregore2e50d332010-12-01 01:36:18 +00004160 // Import the context of this declaration.
4161 DeclContext *DC = ClassTemplate->getDeclContext();
4162 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004163 return nullptr;
4164
Douglas Gregore2e50d332010-12-01 01:36:18 +00004165 DeclContext *LexicalDC = DC;
4166 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4167 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4168 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004169 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004170 }
4171
4172 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004173 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4174 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004175
4176 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004177 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004178 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4179 D->getTemplateArgs().size(),
4180 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004181 return nullptr;
4182
Douglas Gregore2e50d332010-12-01 01:36:18 +00004183 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004184 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004185 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004186 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004187 if (D2) {
4188 // We already have a class template specialization with these template
4189 // arguments.
4190
4191 // FIXME: Check for specialization vs. instantiation errors.
4192
4193 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004194 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004195 // The record types structurally match, or the "from" translation
4196 // unit only had a forward declaration anyway; call it the same
4197 // function.
4198 return Importer.Imported(D, FoundDef);
4199 }
4200 }
4201 } else {
4202 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004203 if (auto *PartialSpec =
4204 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004205 // Import TemplateArgumentListInfo
4206 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004207 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4208 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004209 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004210
4211 QualType CanonInjType = Importer.Import(
4212 PartialSpec->getInjectedSpecializationType());
4213 if (CanonInjType.isNull())
4214 return nullptr;
4215 CanonInjType = CanonInjType.getCanonicalType();
4216
4217 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4218 PartialSpec->getTemplateParameters());
4219 if (!ToTPList && PartialSpec->getTemplateParameters())
4220 return nullptr;
4221
4222 D2 = ClassTemplatePartialSpecializationDecl::Create(
4223 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4224 ToTPList, ClassTemplate,
4225 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4226 ToTAInfo, CanonInjType, nullptr);
4227
4228 } else {
4229 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4230 D->getTagKind(), DC,
4231 StartLoc, IdLoc,
4232 ClassTemplate,
4233 TemplateArgs,
4234 /*PrevDecl=*/nullptr);
4235 }
4236
Douglas Gregore2e50d332010-12-01 01:36:18 +00004237 D2->setSpecializationKind(D->getSpecializationKind());
4238
4239 // Add this specialization to the class template.
4240 ClassTemplate->AddSpecialization(D2, InsertPos);
4241
4242 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004243 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004244
4245 Importer.Imported(D, D2);
4246
4247 if (auto *TSI = D->getTypeAsWritten()) {
4248 TypeSourceInfo *TInfo = Importer.Import(TSI);
4249 if (!TInfo)
4250 return nullptr;
4251 D2->setTypeAsWritten(TInfo);
4252 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4253 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4254 }
4255
4256 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4257 if (POI.isValid())
4258 D2->setPointOfInstantiation(POI);
4259 else if (D->getPointOfInstantiation().isValid())
4260 return nullptr;
4261
4262 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4263
Douglas Gregore2e50d332010-12-01 01:36:18 +00004264 // Add the specialization to this context.
4265 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004266 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004267 }
4268 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004269 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004270 return nullptr;
4271
Douglas Gregore2e50d332010-12-01 01:36:18 +00004272 return D2;
4273}
4274
Larisse Voufo39a1e502013-08-06 01:03:05 +00004275Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4276 // If this variable has a definition in the translation unit we're coming
4277 // from,
4278 // but this particular declaration is not that definition, import the
4279 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004280 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004281 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4282 if (Definition && Definition != D->getTemplatedDecl()) {
4283 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4284 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004285 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004286
4287 return Importer.Imported(D, ImportedDef);
4288 }
4289
4290 // Import the major distinguishing characteristics of this variable template.
4291 DeclContext *DC, *LexicalDC;
4292 DeclarationName Name;
4293 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004294 NamedDecl *ToD;
4295 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004296 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004297 if (ToD)
4298 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004299
4300 // We may already have a template of the same name; try to find and match it.
4301 assert(!DC->isFunctionOrMethod() &&
4302 "Variable templates cannot be declared at function scope");
4303 SmallVector<NamedDecl *, 4> ConflictingDecls;
4304 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004305 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004306 for (auto *FoundDecl : FoundDecls) {
4307 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004308 continue;
4309
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004310 Decl *Found = FoundDecl;
4311 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004312 if (IsStructuralMatch(D, FoundTemplate)) {
4313 // The variable templates structurally match; call it the same template.
4314 Importer.Imported(D->getTemplatedDecl(),
4315 FoundTemplate->getTemplatedDecl());
4316 return Importer.Imported(D, FoundTemplate);
4317 }
4318 }
4319
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004320 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004321 }
4322
4323 if (!ConflictingDecls.empty()) {
4324 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4325 ConflictingDecls.data(),
4326 ConflictingDecls.size());
4327 }
4328
4329 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004330 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004331
4332 VarDecl *DTemplated = D->getTemplatedDecl();
4333
4334 // Import the type.
4335 QualType T = Importer.Import(DTemplated->getType());
4336 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004337 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004338
4339 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004340 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4341 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004342 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004343
4344 // Create the variable template declaration itself.
4345 TemplateParameterList *TemplateParams =
4346 ImportTemplateParameterList(D->getTemplateParameters());
4347 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004348 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004349
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004350 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4351 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4352 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004353
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004354 ToVarTD->setAccess(D->getAccess());
4355 ToVarTD->setLexicalDeclContext(LexicalDC);
4356 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004357
4358 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004359 Importer.Imported(D, ToVarTD);
4360 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004361
4362 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004363 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004364 // FIXME: Import definition!
4365 }
4366
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004367 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004368}
4369
4370Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4371 VarTemplateSpecializationDecl *D) {
4372 // If this record has a definition in the translation unit we're coming from,
4373 // but this particular declaration is not that definition, import the
4374 // definition and map to that.
4375 VarDecl *Definition = D->getDefinition();
4376 if (Definition && Definition != D) {
4377 Decl *ImportedDef = Importer.Import(Definition);
4378 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004379 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004380
4381 return Importer.Imported(D, ImportedDef);
4382 }
4383
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004384 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004385 Importer.Import(D->getSpecializedTemplate()));
4386 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004387 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004388
4389 // Import the context of this declaration.
4390 DeclContext *DC = VarTemplate->getDeclContext();
4391 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004392 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004393
4394 DeclContext *LexicalDC = DC;
4395 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4396 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4397 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004398 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004399 }
4400
4401 // Import the location of this declaration.
4402 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4403 SourceLocation IdLoc = Importer.Import(D->getLocation());
4404
4405 // Import template arguments.
4406 SmallVector<TemplateArgument, 2> TemplateArgs;
4407 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4408 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004409 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004410
4411 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004412 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004413 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004414 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004415 if (D2) {
4416 // We already have a variable template specialization with these template
4417 // arguments.
4418
4419 // FIXME: Check for specialization vs. instantiation errors.
4420
4421 if (VarDecl *FoundDef = D2->getDefinition()) {
4422 if (!D->isThisDeclarationADefinition() ||
4423 IsStructuralMatch(D, FoundDef)) {
4424 // The record types structurally match, or the "from" translation
4425 // unit only had a forward declaration anyway; call it the same
4426 // variable.
4427 return Importer.Imported(D, FoundDef);
4428 }
4429 }
4430 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004431 // Import the type.
4432 QualType T = Importer.Import(D->getType());
4433 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004434 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004435
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004436 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4437 if (D->getTypeSourceInfo() && !TInfo)
4438 return nullptr;
4439
4440 TemplateArgumentListInfo ToTAInfo;
4441 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4442 return nullptr;
4443
4444 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004445 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004446 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4447 // Import TemplateArgumentListInfo
4448 TemplateArgumentListInfo ArgInfos;
4449 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4450 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4451 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4452 return nullptr;
4453
4454 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4455 FromPartial->getTemplateParameters());
4456 if (!ToTPList)
4457 return nullptr;
4458
4459 auto *ToPartial = PartVarSpecDecl::Create(
4460 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4461 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4462
4463 auto *FromInst = FromPartial->getInstantiatedFromMember();
4464 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4465 if (FromInst && !ToInst)
4466 return nullptr;
4467
4468 ToPartial->setInstantiatedFromMember(ToInst);
4469 if (FromPartial->isMemberSpecialization())
4470 ToPartial->setMemberSpecialization();
4471
4472 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004473 } else { // Full specialization
4474 D2 = VarTemplateSpecializationDecl::Create(
4475 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4476 D->getStorageClass(), TemplateArgs);
4477 }
4478
4479 SourceLocation POI = D->getPointOfInstantiation();
4480 if (POI.isValid())
4481 D2->setPointOfInstantiation(Importer.Import(POI));
4482
Larisse Voufo39a1e502013-08-06 01:03:05 +00004483 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004484 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004485
4486 // Add this specialization to the class template.
4487 VarTemplate->AddSpecialization(D2, InsertPos);
4488
4489 // Import the qualifier, if any.
4490 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4491
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004492 if (D->isConstexpr())
4493 D2->setConstexpr(true);
4494
Larisse Voufo39a1e502013-08-06 01:03:05 +00004495 // Add the specialization to this context.
4496 D2->setLexicalDeclContext(LexicalDC);
4497 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004498
4499 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004500 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004501
Larisse Voufo39a1e502013-08-06 01:03:05 +00004502 Importer.Imported(D, D2);
4503
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004504 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4505 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4506 // we have to check the declaration for initializer - otherwise, it won't be
4507 // imported.
4508 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4509 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004510 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004511
4512 return D2;
4513}
4514
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004515Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4516 DeclContext *DC, *LexicalDC;
4517 DeclarationName Name;
4518 SourceLocation Loc;
4519 NamedDecl *ToD;
4520
4521 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4522 return nullptr;
4523
4524 if (ToD)
4525 return ToD;
4526
4527 // Try to find a function in our own ("to") context with the same name, same
4528 // type, and in the same context as the function we're importing.
4529 if (!LexicalDC->isFunctionOrMethod()) {
4530 unsigned IDNS = Decl::IDNS_Ordinary;
4531 SmallVector<NamedDecl *, 2> FoundDecls;
4532 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004533 for (auto *FoundDecl : FoundDecls) {
4534 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004535 continue;
4536
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004537 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004538 if (FoundFunction->hasExternalFormalLinkage() &&
4539 D->hasExternalFormalLinkage()) {
4540 if (IsStructuralMatch(D, FoundFunction)) {
4541 Importer.Imported(D, FoundFunction);
4542 // FIXME: Actually try to merge the body and other attributes.
4543 return FoundFunction;
4544 }
4545 }
4546 }
4547 }
4548 }
4549
4550 TemplateParameterList *Params =
4551 ImportTemplateParameterList(D->getTemplateParameters());
4552 if (!Params)
4553 return nullptr;
4554
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004555 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004556 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4557 if (!TemplatedFD)
4558 return nullptr;
4559
4560 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4561 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4562
4563 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4564 ToFunc->setAccess(D->getAccess());
4565 ToFunc->setLexicalDeclContext(LexicalDC);
4566 Importer.Imported(D, ToFunc);
4567
4568 LexicalDC->addDeclInternal(ToFunc);
4569 return ToFunc;
4570}
4571
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004572//----------------------------------------------------------------------------
4573// Import Statements
4574//----------------------------------------------------------------------------
4575
Sean Callanan59721b32015-04-28 18:41:46 +00004576DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4577 if (DG.isNull())
4578 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4579 size_t NumDecls = DG.end() - DG.begin();
4580 SmallVector<Decl *, 1> ToDecls(NumDecls);
4581 auto &_Importer = this->Importer;
4582 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4583 [&_Importer](Decl *D) -> Decl * {
4584 return _Importer.Import(D);
4585 });
4586 return DeclGroupRef::Create(Importer.getToContext(),
4587 ToDecls.begin(),
4588 NumDecls);
4589}
4590
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004591Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4592 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4593 << S->getStmtClassName();
4594 return nullptr;
4595}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004596
4597Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4598 SmallVector<IdentifierInfo *, 4> Names;
4599 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4600 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004601 // ToII is nullptr when no symbolic name is given for output operand
4602 // see ParseStmtAsm::ParseAsmOperandsOpt
4603 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004604 return nullptr;
4605 Names.push_back(ToII);
4606 }
4607 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4608 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004609 // ToII is nullptr when no symbolic name is given for input operand
4610 // see ParseStmtAsm::ParseAsmOperandsOpt
4611 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004612 return nullptr;
4613 Names.push_back(ToII);
4614 }
4615
4616 SmallVector<StringLiteral *, 4> Clobbers;
4617 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004618 auto *Clobber = cast_or_null<StringLiteral>(
4619 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004620 if (!Clobber)
4621 return nullptr;
4622 Clobbers.push_back(Clobber);
4623 }
4624
4625 SmallVector<StringLiteral *, 4> Constraints;
4626 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004627 auto *Output = cast_or_null<StringLiteral>(
4628 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004629 if (!Output)
4630 return nullptr;
4631 Constraints.push_back(Output);
4632 }
4633
4634 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004635 auto *Input = cast_or_null<StringLiteral>(
4636 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004637 if (!Input)
4638 return nullptr;
4639 Constraints.push_back(Input);
4640 }
4641
4642 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004643 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004644 return nullptr;
4645
Aleksei Sidorina693b372016-09-28 10:16:56 +00004646 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004647 return nullptr;
4648
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004649 auto *AsmStr = cast_or_null<StringLiteral>(
4650 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004651 if (!AsmStr)
4652 return nullptr;
4653
4654 return new (Importer.getToContext()) GCCAsmStmt(
4655 Importer.getToContext(),
4656 Importer.Import(S->getAsmLoc()),
4657 S->isSimple(),
4658 S->isVolatile(),
4659 S->getNumOutputs(),
4660 S->getNumInputs(),
4661 Names.data(),
4662 Constraints.data(),
4663 Exprs.data(),
4664 AsmStr,
4665 S->getNumClobbers(),
4666 Clobbers.data(),
4667 Importer.Import(S->getRParenLoc()));
4668}
4669
Sean Callanan59721b32015-04-28 18:41:46 +00004670Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4671 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004672 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004673 if (!ToD)
4674 return nullptr;
4675 }
4676 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4677 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4678 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4679}
4680
4681Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4682 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4683 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4684 S->hasLeadingEmptyMacro());
4685}
4686
4687Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004688 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004689
4690 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004691 return nullptr;
4692
Sean Callanan59721b32015-04-28 18:41:46 +00004693 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4694 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004695 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4696 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004697}
4698
4699Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4700 Expr *ToLHS = Importer.Import(S->getLHS());
4701 if (!ToLHS)
4702 return nullptr;
4703 Expr *ToRHS = Importer.Import(S->getRHS());
4704 if (!ToRHS && S->getRHS())
4705 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004706 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4707 if (!ToSubStmt && S->getSubStmt())
4708 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004709 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4710 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4711 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004712 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004713 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4714 ToStmt->setSubStmt(ToSubStmt);
4715 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004716}
4717
4718Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4719 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4720 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4721 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4722 if (!ToSubStmt && S->getSubStmt())
4723 return nullptr;
4724 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4725 ToSubStmt);
4726}
4727
4728Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4729 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004730 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004731 if (!ToLabelDecl && S->getDecl())
4732 return nullptr;
4733 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4734 if (!ToSubStmt && S->getSubStmt())
4735 return nullptr;
4736 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4737 ToSubStmt);
4738}
4739
4740Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4741 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4742 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4743 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4744 ASTContext &_ToContext = Importer.getToContext();
4745 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4746 [&_ToContext](const Attr *A) -> const Attr * {
4747 return A->clone(_ToContext);
4748 });
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004749 for (const auto *ToA : ToAttrs) {
Sean Callanan59721b32015-04-28 18:41:46 +00004750 if (!ToA)
4751 return nullptr;
4752 }
4753 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4754 if (!ToSubStmt && S->getSubStmt())
4755 return nullptr;
4756 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4757 ToAttrs, ToSubStmt);
4758}
4759
4760Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4761 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004762 Stmt *ToInit = Importer.Import(S->getInit());
4763 if (!ToInit && S->getInit())
4764 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004765 VarDecl *ToConditionVariable = nullptr;
4766 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4767 ToConditionVariable =
4768 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4769 if (!ToConditionVariable)
4770 return nullptr;
4771 }
4772 Expr *ToCondition = Importer.Import(S->getCond());
4773 if (!ToCondition && S->getCond())
4774 return nullptr;
4775 Stmt *ToThenStmt = Importer.Import(S->getThen());
4776 if (!ToThenStmt && S->getThen())
4777 return nullptr;
4778 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4779 Stmt *ToElseStmt = Importer.Import(S->getElse());
4780 if (!ToElseStmt && S->getElse())
4781 return nullptr;
4782 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004783 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004784 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004785 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004786 ToCondition, ToThenStmt,
4787 ToElseLoc, ToElseStmt);
4788}
4789
4790Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004791 Stmt *ToInit = Importer.Import(S->getInit());
4792 if (!ToInit && S->getInit())
4793 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004794 VarDecl *ToConditionVariable = nullptr;
4795 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4796 ToConditionVariable =
4797 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4798 if (!ToConditionVariable)
4799 return nullptr;
4800 }
4801 Expr *ToCondition = Importer.Import(S->getCond());
4802 if (!ToCondition && S->getCond())
4803 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004804 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004805 Importer.getToContext(), ToInit,
4806 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004807 Stmt *ToBody = Importer.Import(S->getBody());
4808 if (!ToBody && S->getBody())
4809 return nullptr;
4810 ToStmt->setBody(ToBody);
4811 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4812 // Now we have to re-chain the cases.
4813 SwitchCase *LastChainedSwitchCase = nullptr;
4814 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4815 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004816 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004817 if (!ToSC)
4818 return nullptr;
4819 if (LastChainedSwitchCase)
4820 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4821 else
4822 ToStmt->setSwitchCaseList(ToSC);
4823 LastChainedSwitchCase = ToSC;
4824 }
4825 return ToStmt;
4826}
4827
4828Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4829 VarDecl *ToConditionVariable = nullptr;
4830 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4831 ToConditionVariable =
4832 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4833 if (!ToConditionVariable)
4834 return nullptr;
4835 }
4836 Expr *ToCondition = Importer.Import(S->getCond());
4837 if (!ToCondition && S->getCond())
4838 return nullptr;
4839 Stmt *ToBody = Importer.Import(S->getBody());
4840 if (!ToBody && S->getBody())
4841 return nullptr;
4842 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4843 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4844 ToConditionVariable,
4845 ToCondition, ToBody,
4846 ToWhileLoc);
4847}
4848
4849Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4850 Stmt *ToBody = Importer.Import(S->getBody());
4851 if (!ToBody && S->getBody())
4852 return nullptr;
4853 Expr *ToCondition = Importer.Import(S->getCond());
4854 if (!ToCondition && S->getCond())
4855 return nullptr;
4856 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4857 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4858 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4859 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4860 ToDoLoc, ToWhileLoc,
4861 ToRParenLoc);
4862}
4863
4864Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4865 Stmt *ToInit = Importer.Import(S->getInit());
4866 if (!ToInit && S->getInit())
4867 return nullptr;
4868 Expr *ToCondition = Importer.Import(S->getCond());
4869 if (!ToCondition && S->getCond())
4870 return nullptr;
4871 VarDecl *ToConditionVariable = nullptr;
4872 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4873 ToConditionVariable =
4874 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4875 if (!ToConditionVariable)
4876 return nullptr;
4877 }
4878 Expr *ToInc = Importer.Import(S->getInc());
4879 if (!ToInc && S->getInc())
4880 return nullptr;
4881 Stmt *ToBody = Importer.Import(S->getBody());
4882 if (!ToBody && S->getBody())
4883 return nullptr;
4884 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4885 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4886 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4887 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4888 ToInit, ToCondition,
4889 ToConditionVariable,
4890 ToInc, ToBody,
4891 ToForLoc, ToLParenLoc,
4892 ToRParenLoc);
4893}
4894
4895Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4896 LabelDecl *ToLabel = nullptr;
4897 if (LabelDecl *FromLabel = S->getLabel()) {
4898 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4899 if (!ToLabel)
4900 return nullptr;
4901 }
4902 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4903 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4904 return new (Importer.getToContext()) GotoStmt(ToLabel,
4905 ToGotoLoc, ToLabelLoc);
4906}
4907
4908Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4909 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4910 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4911 Expr *ToTarget = Importer.Import(S->getTarget());
4912 if (!ToTarget && S->getTarget())
4913 return nullptr;
4914 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4915 ToTarget);
4916}
4917
4918Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4919 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4920 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4921}
4922
4923Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4924 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4925 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4926}
4927
4928Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4929 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4930 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4931 if (!ToRetExpr && S->getRetValue())
4932 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004933 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4934 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004935 if (!ToNRVOCandidate && NRVOCandidate)
4936 return nullptr;
4937 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4938 ToNRVOCandidate);
4939}
4940
4941Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4942 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4943 VarDecl *ToExceptionDecl = nullptr;
4944 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4945 ToExceptionDecl =
4946 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4947 if (!ToExceptionDecl)
4948 return nullptr;
4949 }
4950 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4951 if (!ToHandlerBlock && S->getHandlerBlock())
4952 return nullptr;
4953 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4954 ToExceptionDecl,
4955 ToHandlerBlock);
4956}
4957
4958Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4959 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4960 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4961 if (!ToTryBlock && S->getTryBlock())
4962 return nullptr;
4963 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4964 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4965 CXXCatchStmt *FromHandler = S->getHandler(HI);
4966 if (Stmt *ToHandler = Importer.Import(FromHandler))
4967 ToHandlers[HI] = ToHandler;
4968 else
4969 return nullptr;
4970 }
4971 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4972 ToHandlers);
4973}
4974
4975Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004976 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00004977 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4978 if (!ToRange && S->getRangeStmt())
4979 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004980 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00004981 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4982 if (!ToBegin && S->getBeginStmt())
4983 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004984 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00004985 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4986 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004987 return nullptr;
4988 Expr *ToCond = Importer.Import(S->getCond());
4989 if (!ToCond && S->getCond())
4990 return nullptr;
4991 Expr *ToInc = Importer.Import(S->getInc());
4992 if (!ToInc && S->getInc())
4993 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004994 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00004995 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4996 if (!ToLoopVar && S->getLoopVarStmt())
4997 return nullptr;
4998 Stmt *ToBody = Importer.Import(S->getBody());
4999 if (!ToBody && S->getBody())
5000 return nullptr;
5001 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005002 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005003 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5004 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005005 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005006 ToCond, ToInc,
5007 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005008 ToForLoc, ToCoawaitLoc,
5009 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005010}
5011
5012Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5013 Stmt *ToElem = Importer.Import(S->getElement());
5014 if (!ToElem && S->getElement())
5015 return nullptr;
5016 Expr *ToCollect = Importer.Import(S->getCollection());
5017 if (!ToCollect && S->getCollection())
5018 return nullptr;
5019 Stmt *ToBody = Importer.Import(S->getBody());
5020 if (!ToBody && S->getBody())
5021 return nullptr;
5022 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5023 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5024 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5025 ToCollect,
5026 ToBody, ToForLoc,
5027 ToRParenLoc);
5028}
5029
5030Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5031 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5032 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5033 VarDecl *ToExceptionDecl = nullptr;
5034 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5035 ToExceptionDecl =
5036 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5037 if (!ToExceptionDecl)
5038 return nullptr;
5039 }
5040 Stmt *ToBody = Importer.Import(S->getCatchBody());
5041 if (!ToBody && S->getCatchBody())
5042 return nullptr;
5043 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5044 ToRParenLoc,
5045 ToExceptionDecl,
5046 ToBody);
5047}
5048
5049Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5050 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5051 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5052 if (!ToAtFinallyStmt && S->getFinallyBody())
5053 return nullptr;
5054 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5055 ToAtFinallyStmt);
5056}
5057
5058Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5059 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5060 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5061 if (!ToAtTryStmt && S->getTryBody())
5062 return nullptr;
5063 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5064 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5065 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5066 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5067 ToCatchStmts[CI] = ToCatchStmt;
5068 else
5069 return nullptr;
5070 }
5071 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5072 if (!ToAtFinallyStmt && S->getFinallyStmt())
5073 return nullptr;
5074 return ObjCAtTryStmt::Create(Importer.getToContext(),
5075 ToAtTryLoc, ToAtTryStmt,
5076 ToCatchStmts.begin(), ToCatchStmts.size(),
5077 ToAtFinallyStmt);
5078}
5079
5080Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5081 (ObjCAtSynchronizedStmt *S) {
5082 SourceLocation ToAtSynchronizedLoc =
5083 Importer.Import(S->getAtSynchronizedLoc());
5084 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5085 if (!ToSynchExpr && S->getSynchExpr())
5086 return nullptr;
5087 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5088 if (!ToSynchBody && S->getSynchBody())
5089 return nullptr;
5090 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5091 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5092}
5093
5094Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5095 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5096 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5097 if (!ToThrow && S->getThrowExpr())
5098 return nullptr;
5099 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5100}
5101
5102Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5103 (ObjCAutoreleasePoolStmt *S) {
5104 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5105 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5106 if (!ToSubStmt && S->getSubStmt())
5107 return nullptr;
5108 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5109 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005110}
5111
5112//----------------------------------------------------------------------------
5113// Import Expressions
5114//----------------------------------------------------------------------------
5115Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5116 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5117 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005118 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005119}
5120
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005121Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5122 QualType T = Importer.Import(E->getType());
5123 if (T.isNull())
5124 return nullptr;
5125
5126 Expr *SubExpr = Importer.Import(E->getSubExpr());
5127 if (!SubExpr && E->getSubExpr())
5128 return nullptr;
5129
5130 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5131 if (!TInfo)
5132 return nullptr;
5133
5134 return new (Importer.getToContext()) VAArgExpr(
5135 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5136 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5137}
5138
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005139Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5140 QualType T = Importer.Import(E->getType());
5141 if (T.isNull())
5142 return nullptr;
5143
5144 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005145 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005146}
5147
5148Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5149 QualType T = Importer.Import(E->getType());
5150 if (T.isNull())
5151 return nullptr;
5152
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005153 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005154 if (!SL && E->getFunctionName())
5155 return nullptr;
5156
5157 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005158 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005159}
5160
Douglas Gregor52f820e2010-02-19 01:17:02 +00005161Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005162 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005163 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005164 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005165
Craig Topper36250ad2014-05-12 05:36:57 +00005166 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005167 if (E->getDecl() != E->getFoundDecl()) {
5168 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5169 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005170 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005171 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005172
5173 QualType T = Importer.Import(E->getType());
5174 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005175 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005176
Aleksei Sidorina693b372016-09-28 10:16:56 +00005177 TemplateArgumentListInfo ToTAInfo;
5178 TemplateArgumentListInfo *ResInfo = nullptr;
5179 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005180 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5181 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005182 ResInfo = &ToTAInfo;
5183 }
5184
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005185 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5186 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005187 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005188 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005189 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005190 Importer.Import(E->getLocation()),
5191 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005192 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005193 if (E->hadMultipleCandidates())
5194 DRE->setHadMultipleCandidates(true);
5195 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005196}
5197
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005198Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5199 QualType T = Importer.Import(E->getType());
5200 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005201 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005202
5203 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5204}
5205
5206ASTNodeImporter::Designator
5207ASTNodeImporter::ImportDesignator(const Designator &D) {
5208 if (D.isFieldDesignator()) {
5209 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5210 // Caller checks for import error
5211 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5212 Importer.Import(D.getFieldLoc()));
5213 }
5214 if (D.isArrayDesignator())
5215 return Designator(D.getFirstExprIndex(),
5216 Importer.Import(D.getLBracketLoc()),
5217 Importer.Import(D.getRBracketLoc()));
5218
5219 assert(D.isArrayRangeDesignator());
5220 return Designator(D.getFirstExprIndex(),
5221 Importer.Import(D.getLBracketLoc()),
5222 Importer.Import(D.getEllipsisLoc()),
5223 Importer.Import(D.getRBracketLoc()));
5224}
5225
5226
5227Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005228 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005229 if (!Init)
5230 return nullptr;
5231
5232 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5233 // List elements from the second, the first is Init itself
5234 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005235 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005236 IndexExprs[I - 1] = Arg;
5237 else
5238 return nullptr;
5239 }
5240
5241 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005242 llvm::transform(DIE->designators(), Designators.begin(),
5243 [this](const Designator &D) -> Designator {
5244 return ImportDesignator(D);
5245 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005246
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005247 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005248 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005249 return nullptr;
5250
5251 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005252 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005253 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5254 DIE->usesGNUSyntax(), Init);
5255}
5256
5257Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5258 QualType T = Importer.Import(E->getType());
5259 if (T.isNull())
5260 return nullptr;
5261
5262 return new (Importer.getToContext())
5263 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5264}
5265
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005266Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5267 QualType T = Importer.Import(E->getType());
5268 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005269 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005270
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005271 return IntegerLiteral::Create(Importer.getToContext(),
5272 E->getValue(), T,
5273 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005274}
5275
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005276Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5277 QualType T = Importer.Import(E->getType());
5278 if (T.isNull())
5279 return nullptr;
5280
5281 return FloatingLiteral::Create(Importer.getToContext(),
5282 E->getValue(), E->isExact(), T,
5283 Importer.Import(E->getLocation()));
5284}
5285
Douglas Gregor623421d2010-02-18 02:21:22 +00005286Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5287 QualType T = Importer.Import(E->getType());
5288 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005289 return nullptr;
5290
Douglas Gregorfb65e592011-07-27 05:40:30 +00005291 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5292 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005293 Importer.Import(E->getLocation()));
5294}
5295
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005296Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5297 QualType T = Importer.Import(E->getType());
5298 if (T.isNull())
5299 return nullptr;
5300
5301 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5302 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5303
5304 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5305 E->getKind(), E->isPascal(), T,
5306 Locations.data(), Locations.size());
5307}
5308
5309Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5310 QualType T = Importer.Import(E->getType());
5311 if (T.isNull())
5312 return nullptr;
5313
5314 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5315 if (!TInfo)
5316 return nullptr;
5317
5318 Expr *Init = Importer.Import(E->getInitializer());
5319 if (!Init)
5320 return nullptr;
5321
5322 return new (Importer.getToContext()) CompoundLiteralExpr(
5323 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5324 Init, E->isFileScope());
5325}
5326
5327Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5328 QualType T = Importer.Import(E->getType());
5329 if (T.isNull())
5330 return nullptr;
5331
5332 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5333 if (ImportArrayChecked(
5334 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5335 Exprs.begin()))
5336 return nullptr;
5337
5338 return new (Importer.getToContext()) AtomicExpr(
5339 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5340 Importer.Import(E->getRParenLoc()));
5341}
5342
5343Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5344 QualType T = Importer.Import(E->getType());
5345 if (T.isNull())
5346 return nullptr;
5347
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005348 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005349 if (!ToLabel)
5350 return nullptr;
5351
5352 return new (Importer.getToContext()) AddrLabelExpr(
5353 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5354 ToLabel, T);
5355}
5356
Douglas Gregorc74247e2010-02-19 01:07:06 +00005357Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5358 Expr *SubExpr = Importer.Import(E->getSubExpr());
5359 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005360 return nullptr;
5361
Douglas Gregorc74247e2010-02-19 01:07:06 +00005362 return new (Importer.getToContext())
5363 ParenExpr(Importer.Import(E->getLParen()),
5364 Importer.Import(E->getRParen()),
5365 SubExpr);
5366}
5367
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005368Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5369 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005370 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005371 return nullptr;
5372
5373 return new (Importer.getToContext()) ParenListExpr(
5374 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5375 Exprs, Importer.Import(E->getLParenLoc()));
5376}
5377
5378Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5379 QualType T = Importer.Import(E->getType());
5380 if (T.isNull())
5381 return nullptr;
5382
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005383 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5384 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005385 if (!ToSubStmt && E->getSubStmt())
5386 return nullptr;
5387
5388 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5389 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5390}
5391
Douglas Gregorc74247e2010-02-19 01:07:06 +00005392Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5393 QualType T = Importer.Import(E->getType());
5394 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005395 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005396
5397 Expr *SubExpr = Importer.Import(E->getSubExpr());
5398 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005399 return nullptr;
5400
Aaron Ballmana5038552018-01-09 13:07:03 +00005401 return new (Importer.getToContext()) UnaryOperator(
5402 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5403 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005404}
5405
Aaron Ballmana5038552018-01-09 13:07:03 +00005406Expr *
5407ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005408 QualType ResultType = Importer.Import(E->getType());
5409
5410 if (E->isArgumentType()) {
5411 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5412 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005413 return nullptr;
5414
Peter Collingbournee190dee2011-03-11 19:24:49 +00005415 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5416 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005417 Importer.Import(E->getOperatorLoc()),
5418 Importer.Import(E->getRParenLoc()));
5419 }
5420
5421 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5422 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005423 return nullptr;
5424
Peter Collingbournee190dee2011-03-11 19:24:49 +00005425 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5426 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005427 Importer.Import(E->getOperatorLoc()),
5428 Importer.Import(E->getRParenLoc()));
5429}
5430
Douglas Gregorc74247e2010-02-19 01:07:06 +00005431Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5432 QualType T = Importer.Import(E->getType());
5433 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005434 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005435
5436 Expr *LHS = Importer.Import(E->getLHS());
5437 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005438 return nullptr;
5439
Douglas Gregorc74247e2010-02-19 01:07:06 +00005440 Expr *RHS = Importer.Import(E->getRHS());
5441 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005442 return nullptr;
5443
Douglas Gregorc74247e2010-02-19 01:07:06 +00005444 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005445 T, E->getValueKind(),
5446 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005447 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005448 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005449}
5450
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005451Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5452 QualType T = Importer.Import(E->getType());
5453 if (T.isNull())
5454 return nullptr;
5455
5456 Expr *ToLHS = Importer.Import(E->getLHS());
5457 if (!ToLHS)
5458 return nullptr;
5459
5460 Expr *ToRHS = Importer.Import(E->getRHS());
5461 if (!ToRHS)
5462 return nullptr;
5463
5464 Expr *ToCond = Importer.Import(E->getCond());
5465 if (!ToCond)
5466 return nullptr;
5467
5468 return new (Importer.getToContext()) ConditionalOperator(
5469 ToCond, Importer.Import(E->getQuestionLoc()),
5470 ToLHS, Importer.Import(E->getColonLoc()),
5471 ToRHS, T, E->getValueKind(), E->getObjectKind());
5472}
5473
5474Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5475 BinaryConditionalOperator *E) {
5476 QualType T = Importer.Import(E->getType());
5477 if (T.isNull())
5478 return nullptr;
5479
5480 Expr *Common = Importer.Import(E->getCommon());
5481 if (!Common)
5482 return nullptr;
5483
5484 Expr *Cond = Importer.Import(E->getCond());
5485 if (!Cond)
5486 return nullptr;
5487
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005488 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5489 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005490 if (!OpaqueValue)
5491 return nullptr;
5492
5493 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5494 if (!TrueExpr)
5495 return nullptr;
5496
5497 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5498 if (!FalseExpr)
5499 return nullptr;
5500
5501 return new (Importer.getToContext()) BinaryConditionalOperator(
5502 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5503 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5504 T, E->getValueKind(), E->getObjectKind());
5505}
5506
Aleksei Sidorina693b372016-09-28 10:16:56 +00005507Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5508 QualType T = Importer.Import(E->getType());
5509 if (T.isNull())
5510 return nullptr;
5511
5512 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5513 if (!ToQueried)
5514 return nullptr;
5515
5516 Expr *Dim = Importer.Import(E->getDimensionExpression());
5517 if (!Dim && E->getDimensionExpression())
5518 return nullptr;
5519
5520 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5521 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5522 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5523}
5524
5525Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5526 QualType T = Importer.Import(E->getType());
5527 if (T.isNull())
5528 return nullptr;
5529
5530 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5531 if (!ToQueried)
5532 return nullptr;
5533
5534 return new (Importer.getToContext()) ExpressionTraitExpr(
5535 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5536 E->getValue(), Importer.Import(E->getLocEnd()), T);
5537}
5538
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005539Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5540 QualType T = Importer.Import(E->getType());
5541 if (T.isNull())
5542 return nullptr;
5543
5544 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5545 if (!SourceExpr && E->getSourceExpr())
5546 return nullptr;
5547
5548 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005549 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005550 E->getObjectKind(), SourceExpr);
5551}
5552
Aleksei Sidorina693b372016-09-28 10:16:56 +00005553Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5554 QualType T = Importer.Import(E->getType());
5555 if (T.isNull())
5556 return nullptr;
5557
5558 Expr *ToLHS = Importer.Import(E->getLHS());
5559 if (!ToLHS)
5560 return nullptr;
5561
5562 Expr *ToRHS = Importer.Import(E->getRHS());
5563 if (!ToRHS)
5564 return nullptr;
5565
5566 return new (Importer.getToContext()) ArraySubscriptExpr(
5567 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5568 Importer.Import(E->getRBracketLoc()));
5569}
5570
Douglas Gregorc74247e2010-02-19 01:07:06 +00005571Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5572 QualType T = Importer.Import(E->getType());
5573 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005574 return nullptr;
5575
Douglas Gregorc74247e2010-02-19 01:07:06 +00005576 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5577 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005578 return nullptr;
5579
Douglas Gregorc74247e2010-02-19 01:07:06 +00005580 QualType CompResultType = Importer.Import(E->getComputationResultType());
5581 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005582 return nullptr;
5583
Douglas Gregorc74247e2010-02-19 01:07:06 +00005584 Expr *LHS = Importer.Import(E->getLHS());
5585 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005586 return nullptr;
5587
Douglas Gregorc74247e2010-02-19 01:07:06 +00005588 Expr *RHS = Importer.Import(E->getRHS());
5589 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005590 return nullptr;
5591
Douglas Gregorc74247e2010-02-19 01:07:06 +00005592 return new (Importer.getToContext())
5593 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005594 T, E->getValueKind(),
5595 E->getObjectKind(),
5596 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005597 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005598 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005599}
5600
Aleksei Sidorina693b372016-09-28 10:16:56 +00005601bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5602 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5603 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5604 Path.push_back(Spec);
5605 else
5606 return true;
5607 }
5608 return false;
John McCallcf142162010-08-07 06:22:56 +00005609}
5610
Douglas Gregor98c10182010-02-12 22:17:39 +00005611Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5612 QualType T = Importer.Import(E->getType());
5613 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005614 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005615
5616 Expr *SubExpr = Importer.Import(E->getSubExpr());
5617 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005618 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005619
5620 CXXCastPath BasePath;
5621 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005622 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005623
5624 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005625 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005626}
5627
Aleksei Sidorina693b372016-09-28 10:16:56 +00005628Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005629 QualType T = Importer.Import(E->getType());
5630 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005631 return nullptr;
5632
Douglas Gregor5481d322010-02-19 01:32:14 +00005633 Expr *SubExpr = Importer.Import(E->getSubExpr());
5634 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005635 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005636
5637 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5638 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005639 return nullptr;
5640
John McCallcf142162010-08-07 06:22:56 +00005641 CXXCastPath BasePath;
5642 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005643 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005644
Aleksei Sidorina693b372016-09-28 10:16:56 +00005645 switch (E->getStmtClass()) {
5646 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005647 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005648 return CStyleCastExpr::Create(Importer.getToContext(), T,
5649 E->getValueKind(), E->getCastKind(),
5650 SubExpr, &BasePath, TInfo,
5651 Importer.Import(CCE->getLParenLoc()),
5652 Importer.Import(CCE->getRParenLoc()));
5653 }
5654
5655 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005656 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005657 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5658 E->getValueKind(), TInfo,
5659 E->getCastKind(), SubExpr, &BasePath,
5660 Importer.Import(FCE->getLParenLoc()),
5661 Importer.Import(FCE->getRParenLoc()));
5662 }
5663
5664 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005665 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005666 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5667 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5668 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5669 TInfo, SubExpr);
5670 }
5671 default:
5672 break; // just fall through
5673 }
5674
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005675 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005676 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5677 RParenLoc = Importer.Import(Named->getRParenLoc());
5678 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5679
5680 switch (E->getStmtClass()) {
5681 case Stmt::CXXStaticCastExprClass:
5682 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5683 E->getValueKind(), E->getCastKind(),
5684 SubExpr, &BasePath, TInfo,
5685 ExprLoc, RParenLoc, Brackets);
5686
5687 case Stmt::CXXDynamicCastExprClass:
5688 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5689 E->getValueKind(), E->getCastKind(),
5690 SubExpr, &BasePath, TInfo,
5691 ExprLoc, RParenLoc, Brackets);
5692
5693 case Stmt::CXXReinterpretCastExprClass:
5694 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5695 E->getValueKind(), E->getCastKind(),
5696 SubExpr, &BasePath, TInfo,
5697 ExprLoc, RParenLoc, Brackets);
5698
5699 case Stmt::CXXConstCastExprClass:
5700 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5701 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5702 RParenLoc, Brackets);
5703 default:
5704 llvm_unreachable("Cast expression of unsupported type!");
5705 return nullptr;
5706 }
5707}
5708
5709Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5710 QualType T = Importer.Import(OE->getType());
5711 if (T.isNull())
5712 return nullptr;
5713
5714 SmallVector<OffsetOfNode, 4> Nodes;
5715 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5716 const OffsetOfNode &Node = OE->getComponent(I);
5717
5718 switch (Node.getKind()) {
5719 case OffsetOfNode::Array:
5720 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5721 Node.getArrayExprIndex(),
5722 Importer.Import(Node.getLocEnd())));
5723 break;
5724
5725 case OffsetOfNode::Base: {
5726 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5727 if (!BS && Node.getBase())
5728 return nullptr;
5729 Nodes.push_back(OffsetOfNode(BS));
5730 break;
5731 }
5732 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005733 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005734 if (!FD)
5735 return nullptr;
5736 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5737 Importer.Import(Node.getLocEnd())));
5738 break;
5739 }
5740 case OffsetOfNode::Identifier: {
5741 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5742 if (!ToII)
5743 return nullptr;
5744 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5745 Importer.Import(Node.getLocEnd())));
5746 break;
5747 }
5748 }
5749 }
5750
5751 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5752 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5753 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5754 if (!ToIndexExpr)
5755 return nullptr;
5756 Exprs[I] = ToIndexExpr;
5757 }
5758
5759 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5760 if (!TInfo && OE->getTypeSourceInfo())
5761 return nullptr;
5762
5763 return OffsetOfExpr::Create(Importer.getToContext(), T,
5764 Importer.Import(OE->getOperatorLoc()),
5765 TInfo, Nodes, Exprs,
5766 Importer.Import(OE->getRParenLoc()));
5767}
5768
5769Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5770 QualType T = Importer.Import(E->getType());
5771 if (T.isNull())
5772 return nullptr;
5773
5774 Expr *Operand = Importer.Import(E->getOperand());
5775 if (!Operand)
5776 return nullptr;
5777
5778 CanThrowResult CanThrow;
5779 if (E->isValueDependent())
5780 CanThrow = CT_Dependent;
5781 else
5782 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5783
5784 return new (Importer.getToContext()) CXXNoexceptExpr(
5785 T, Operand, CanThrow,
5786 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5787}
5788
5789Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5790 QualType T = Importer.Import(E->getType());
5791 if (T.isNull())
5792 return nullptr;
5793
5794 Expr *SubExpr = Importer.Import(E->getSubExpr());
5795 if (!SubExpr && E->getSubExpr())
5796 return nullptr;
5797
5798 return new (Importer.getToContext()) CXXThrowExpr(
5799 SubExpr, T, Importer.Import(E->getThrowLoc()),
5800 E->isThrownVariableInScope());
5801}
5802
5803Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005804 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005805 if (!Param)
5806 return nullptr;
5807
5808 return CXXDefaultArgExpr::Create(
5809 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5810}
5811
5812Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5813 QualType T = Importer.Import(E->getType());
5814 if (T.isNull())
5815 return nullptr;
5816
5817 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5818 if (!TypeInfo)
5819 return nullptr;
5820
5821 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5822 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5823}
5824
5825Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5826 Expr *SubExpr = Importer.Import(E->getSubExpr());
5827 if (!SubExpr)
5828 return nullptr;
5829
5830 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5831 Importer.Import(const_cast<CXXDestructorDecl *>(
5832 E->getTemporary()->getDestructor())));
5833 if (!Dtor)
5834 return nullptr;
5835
5836 ASTContext &ToCtx = Importer.getToContext();
5837 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5838 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5839}
5840
5841Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5842 QualType T = Importer.Import(CE->getType());
5843 if (T.isNull())
5844 return nullptr;
5845
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005846 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5847 if (!TInfo)
5848 return nullptr;
5849
Aleksei Sidorina693b372016-09-28 10:16:56 +00005850 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5851 if (ImportContainerChecked(CE->arguments(), Args))
5852 return nullptr;
5853
5854 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5855 Importer.Import(CE->getConstructor()));
5856 if (!Ctor)
5857 return nullptr;
5858
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005859 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5860 Importer.getToContext(), Ctor, T, TInfo, Args,
5861 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5862 CE->isListInitialization(), CE->isStdInitListInitialization(),
5863 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005864}
5865
5866Expr *
5867ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5868 QualType T = Importer.Import(E->getType());
5869 if (T.isNull())
5870 return nullptr;
5871
5872 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5873 if (!TempE)
5874 return nullptr;
5875
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005876 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005877 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5878 if (!ExtendedBy && E->getExtendingDecl())
5879 return nullptr;
5880
5881 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5882 T, TempE, E->isBoundToLvalueReference());
5883
5884 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5885 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5886 return ToMTE;
5887}
5888
Gabor Horvath7a91c082017-11-14 11:30:38 +00005889Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5890 QualType T = Importer.Import(E->getType());
5891 if (T.isNull())
5892 return nullptr;
5893
5894 Expr *Pattern = Importer.Import(E->getPattern());
5895 if (!Pattern)
5896 return nullptr;
5897
5898 return new (Importer.getToContext()) PackExpansionExpr(
5899 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5900 E->getNumExpansions());
5901}
5902
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005903Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5904 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5905 if (!Pack)
5906 return nullptr;
5907
5908 Optional<unsigned> Length;
5909
5910 if (!E->isValueDependent())
5911 Length = E->getPackLength();
5912
5913 SmallVector<TemplateArgument, 8> PartialArguments;
5914 if (E->isPartiallySubstituted()) {
5915 if (ImportTemplateArguments(E->getPartialArguments().data(),
5916 E->getPartialArguments().size(),
5917 PartialArguments))
5918 return nullptr;
5919 }
5920
5921 return SizeOfPackExpr::Create(
5922 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5923 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5924 Length, PartialArguments);
5925}
5926
Aleksei Sidorina693b372016-09-28 10:16:56 +00005927Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5928 QualType T = Importer.Import(CE->getType());
5929 if (T.isNull())
5930 return nullptr;
5931
5932 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5933 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5934 return nullptr;
5935
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005936 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005937 Importer.Import(CE->getOperatorNew()));
5938 if (!OperatorNewDecl && CE->getOperatorNew())
5939 return nullptr;
5940
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005941 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005942 Importer.Import(CE->getOperatorDelete()));
5943 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5944 return nullptr;
5945
5946 Expr *ToInit = Importer.Import(CE->getInitializer());
5947 if (!ToInit && CE->getInitializer())
5948 return nullptr;
5949
5950 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5951 if (!TInfo)
5952 return nullptr;
5953
5954 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5955 if (!ToArrSize && CE->getArraySize())
5956 return nullptr;
5957
5958 return new (Importer.getToContext()) CXXNewExpr(
5959 Importer.getToContext(),
5960 CE->isGlobalNew(),
5961 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005962 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005963 CE->doesUsualArrayDeleteWantSize(),
5964 PlacementArgs,
5965 Importer.Import(CE->getTypeIdParens()),
5966 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5967 Importer.Import(CE->getSourceRange()),
5968 Importer.Import(CE->getDirectInitRange()));
5969}
5970
5971Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5972 QualType T = Importer.Import(E->getType());
5973 if (T.isNull())
5974 return nullptr;
5975
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005976 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005977 Importer.Import(E->getOperatorDelete()));
5978 if (!OperatorDeleteDecl && E->getOperatorDelete())
5979 return nullptr;
5980
5981 Expr *ToArg = Importer.Import(E->getArgument());
5982 if (!ToArg && E->getArgument())
5983 return nullptr;
5984
5985 return new (Importer.getToContext()) CXXDeleteExpr(
5986 T, E->isGlobalDelete(),
5987 E->isArrayForm(),
5988 E->isArrayFormAsWritten(),
5989 E->doesUsualArrayDeleteWantSize(),
5990 OperatorDeleteDecl,
5991 ToArg,
5992 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005993}
5994
Sean Callanan59721b32015-04-28 18:41:46 +00005995Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5996 QualType T = Importer.Import(E->getType());
5997 if (T.isNull())
5998 return nullptr;
5999
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006000 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006001 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006002 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006003 return nullptr;
6004
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006005 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006006 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006007 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006008
6009 return CXXConstructExpr::Create(Importer.getToContext(), T,
6010 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006011 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006012 ToArgs, E->hadMultipleCandidates(),
6013 E->isListInitialization(),
6014 E->isStdInitListInitialization(),
6015 E->requiresZeroInitialization(),
6016 E->getConstructionKind(),
6017 Importer.Import(E->getParenOrBraceRange()));
6018}
6019
Aleksei Sidorina693b372016-09-28 10:16:56 +00006020Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6021 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6022 if (!SubExpr && EWC->getSubExpr())
6023 return nullptr;
6024
6025 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6026 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6027 if (ExprWithCleanups::CleanupObject Obj =
6028 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6029 Objs[I] = Obj;
6030 else
6031 return nullptr;
6032
6033 return ExprWithCleanups::Create(Importer.getToContext(),
6034 SubExpr, EWC->cleanupsHaveSideEffects(),
6035 Objs);
6036}
6037
Sean Callanan8bca9962016-03-28 21:43:01 +00006038Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6039 QualType T = Importer.Import(E->getType());
6040 if (T.isNull())
6041 return nullptr;
6042
6043 Expr *ToFn = Importer.Import(E->getCallee());
6044 if (!ToFn)
6045 return nullptr;
6046
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006047 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006048 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006049 return nullptr;
6050
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006051 return new (Importer.getToContext()) CXXMemberCallExpr(
6052 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6053 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006054}
6055
6056Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6057 QualType T = Importer.Import(E->getType());
6058 if (T.isNull())
6059 return nullptr;
6060
6061 return new (Importer.getToContext())
6062 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6063}
6064
6065Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6066 QualType T = Importer.Import(E->getType());
6067 if (T.isNull())
6068 return nullptr;
6069
6070 return new (Importer.getToContext())
6071 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6072}
6073
6074
Sean Callanan59721b32015-04-28 18:41:46 +00006075Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6076 QualType T = Importer.Import(E->getType());
6077 if (T.isNull())
6078 return nullptr;
6079
6080 Expr *ToBase = Importer.Import(E->getBase());
6081 if (!ToBase && E->getBase())
6082 return nullptr;
6083
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006084 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006085 if (!ToMember && E->getMemberDecl())
6086 return nullptr;
6087
6088 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6089 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6090 E->getFoundDecl().getAccess());
6091
6092 DeclarationNameInfo ToMemberNameInfo(
6093 Importer.Import(E->getMemberNameInfo().getName()),
6094 Importer.Import(E->getMemberNameInfo().getLoc()));
6095
6096 if (E->hasExplicitTemplateArgs()) {
6097 return nullptr; // FIXME: handle template arguments
6098 }
6099
6100 return MemberExpr::Create(Importer.getToContext(), ToBase,
6101 E->isArrow(),
6102 Importer.Import(E->getOperatorLoc()),
6103 Importer.Import(E->getQualifierLoc()),
6104 Importer.Import(E->getTemplateKeywordLoc()),
6105 ToMember, ToFoundDecl, ToMemberNameInfo,
6106 nullptr, T, E->getValueKind(),
6107 E->getObjectKind());
6108}
6109
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006110Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6111 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006112 Expr *BaseE = Importer.Import(E->getBase());
6113 if (!BaseE)
6114 return nullptr;
6115
6116 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6117 if (!ScopeInfo && E->getScopeTypeInfo())
6118 return nullptr;
6119
6120 PseudoDestructorTypeStorage Storage;
6121 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6122 IdentifierInfo *ToII = Importer.Import(FromII);
6123 if (!ToII)
6124 return nullptr;
6125 Storage = PseudoDestructorTypeStorage(
6126 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6127 } else {
6128 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6129 if (!TI)
6130 return nullptr;
6131 Storage = PseudoDestructorTypeStorage(TI);
6132 }
6133
6134 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6135 Importer.getToContext(), BaseE, E->isArrow(),
6136 Importer.Import(E->getOperatorLoc()),
6137 Importer.Import(E->getQualifierLoc()),
6138 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6139 Importer.Import(E->getTildeLoc()), Storage);
6140}
6141
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006142Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6143 CXXDependentScopeMemberExpr *E) {
6144 Expr *Base = nullptr;
6145 if (!E->isImplicitAccess()) {
6146 Base = Importer.Import(E->getBase());
6147 if (!Base)
6148 return nullptr;
6149 }
6150
6151 QualType BaseType = Importer.Import(E->getBaseType());
6152 if (BaseType.isNull())
6153 return nullptr;
6154
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006155 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006156 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006157 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6158 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006159 return nullptr;
6160 ResInfo = &ToTAInfo;
6161 }
6162
6163 DeclarationName Name = Importer.Import(E->getMember());
6164 if (!E->getMember().isEmpty() && Name.isEmpty())
6165 return nullptr;
6166
6167 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6168 // Import additional name location/type info.
6169 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6170 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6171 if (!ToFQ && E->getFirstQualifierFoundInScope())
6172 return nullptr;
6173
6174 return CXXDependentScopeMemberExpr::Create(
6175 Importer.getToContext(), Base, BaseType, E->isArrow(),
6176 Importer.Import(E->getOperatorLoc()),
6177 Importer.Import(E->getQualifierLoc()),
6178 Importer.Import(E->getTemplateKeywordLoc()),
6179 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6180}
6181
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006182Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6183 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006184 unsigned NumArgs = CE->arg_size();
6185
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006186 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006187 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6188 return nullptr;
6189
6190 return CXXUnresolvedConstructExpr::Create(
6191 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6192 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6193 Importer.Import(CE->getRParenLoc()));
6194}
6195
6196Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006197 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006198 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6199 if (E->getNamingClass() && !NamingClass)
6200 return nullptr;
6201
6202 DeclarationName Name = Importer.Import(E->getName());
6203 if (E->getName() && !Name)
6204 return nullptr;
6205
6206 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6207 // Import additional name location/type info.
6208 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6209
6210 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006211 for (auto *D : E->decls()) {
6212 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006213 ToDecls.addDecl(To);
6214 else
6215 return nullptr;
6216 }
6217
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006218 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006219 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006220 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6221 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006222 return nullptr;
6223 ResInfo = &ToTAInfo;
6224 }
6225
6226 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6227 return UnresolvedLookupExpr::Create(
6228 Importer.getToContext(), NamingClass,
6229 Importer.Import(E->getQualifierLoc()),
6230 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6231 ResInfo, ToDecls.begin(), ToDecls.end());
6232
6233 return UnresolvedLookupExpr::Create(
6234 Importer.getToContext(), NamingClass,
6235 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6236 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6237}
6238
Sean Callanan59721b32015-04-28 18:41:46 +00006239Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6240 QualType T = Importer.Import(E->getType());
6241 if (T.isNull())
6242 return nullptr;
6243
6244 Expr *ToCallee = Importer.Import(E->getCallee());
6245 if (!ToCallee && E->getCallee())
6246 return nullptr;
6247
6248 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006249 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006250 if (ImportContainerChecked(E->arguments(), ToArgs))
6251 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006252
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006253 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006254
6255 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6256 ToArgs_Copied[ai] = ToArgs[ai];
6257
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006258 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6259 return new (Importer.getToContext()) CXXOperatorCallExpr(
6260 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6261 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6262 OCE->getFPFeatures());
6263 }
6264
Sean Callanan59721b32015-04-28 18:41:46 +00006265 return new (Importer.getToContext())
6266 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006267 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006268 Importer.Import(E->getRParenLoc()));
6269}
6270
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006271Optional<LambdaCapture>
6272ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6273 VarDecl *Var = nullptr;
6274 if (From.capturesVariable()) {
6275 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6276 if (!Var)
6277 return None;
6278 }
6279
6280 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6281 From.getCaptureKind(), Var,
6282 From.isPackExpansion()
6283 ? Importer.Import(From.getEllipsisLoc())
6284 : SourceLocation());
6285}
6286
6287Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6288 CXXRecordDecl *FromClass = LE->getLambdaClass();
6289 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6290 if (!ToClass)
6291 return nullptr;
6292
6293 // NOTE: lambda classes are created with BeingDefined flag set up.
6294 // It means that ImportDefinition doesn't work for them and we should fill it
6295 // manually.
6296 if (ToClass->isBeingDefined()) {
6297 for (auto FromField : FromClass->fields()) {
6298 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6299 if (!ToField)
6300 return nullptr;
6301 }
6302 }
6303
6304 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6305 Importer.Import(LE->getCallOperator()));
6306 if (!ToCallOp)
6307 return nullptr;
6308
6309 ToClass->completeDefinition();
6310
6311 unsigned NumCaptures = LE->capture_size();
6312 SmallVector<LambdaCapture, 8> Captures;
6313 Captures.reserve(NumCaptures);
6314 for (const auto &FromCapture : LE->captures()) {
6315 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6316 Captures.push_back(*ToCapture);
6317 else
6318 return nullptr;
6319 }
6320
6321 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6322 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6323 return nullptr;
6324
6325 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6326 Importer.Import(LE->getIntroducerRange()),
6327 LE->getCaptureDefault(),
6328 Importer.Import(LE->getCaptureDefaultLoc()),
6329 Captures,
6330 LE->hasExplicitParameters(),
6331 LE->hasExplicitResultType(),
6332 InitCaptures,
6333 Importer.Import(LE->getLocEnd()),
6334 LE->containsUnexpandedParameterPack());
6335}
6336
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006337Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6338 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006339 if (T.isNull())
6340 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006341
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006342 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006343 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006344 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006345
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006346 ASTContext &ToCtx = Importer.getToContext();
6347 InitListExpr *To = new (ToCtx) InitListExpr(
6348 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6349 Exprs, Importer.Import(ILE->getLBraceLoc()));
6350 To->setType(T);
6351
6352 if (ILE->hasArrayFiller()) {
6353 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6354 if (!Filler)
6355 return nullptr;
6356 To->setArrayFiller(Filler);
6357 }
6358
6359 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006360 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006361 if (!ToFD)
6362 return nullptr;
6363 To->setInitializedFieldInUnion(ToFD);
6364 }
6365
6366 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006367 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006368 if (!ToSyntForm)
6369 return nullptr;
6370 To->setSyntacticForm(ToSyntForm);
6371 }
6372
6373 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6374 To->setValueDependent(ILE->isValueDependent());
6375 To->setInstantiationDependent(ILE->isInstantiationDependent());
6376
6377 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006378}
6379
Richard Smith30e304e2016-12-14 00:03:17 +00006380Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6381 QualType ToType = Importer.Import(E->getType());
6382 if (ToType.isNull())
6383 return nullptr;
6384
6385 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6386 if (!ToCommon && E->getCommonExpr())
6387 return nullptr;
6388
6389 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6390 if (!ToSubExpr && E->getSubExpr())
6391 return nullptr;
6392
6393 return new (Importer.getToContext())
6394 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6395}
6396
6397Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6398 QualType ToType = Importer.Import(E->getType());
6399 if (ToType.isNull())
6400 return nullptr;
6401 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6402}
6403
Sean Callanandd2c1742016-05-16 20:48:03 +00006404Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006405 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006406 if (!ToField && DIE->getField())
6407 return nullptr;
6408
6409 return CXXDefaultInitExpr::Create(
6410 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6411}
6412
6413Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6414 QualType ToType = Importer.Import(E->getType());
6415 if (ToType.isNull() && !E->getType().isNull())
6416 return nullptr;
6417 ExprValueKind VK = E->getValueKind();
6418 CastKind CK = E->getCastKind();
6419 Expr *ToOp = Importer.Import(E->getSubExpr());
6420 if (!ToOp && E->getSubExpr())
6421 return nullptr;
6422 CXXCastPath BasePath;
6423 if (ImportCastPath(E, BasePath))
6424 return nullptr;
6425 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6426 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6427 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6428 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6429
6430 if (isa<CXXStaticCastExpr>(E)) {
6431 return CXXStaticCastExpr::Create(
6432 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6433 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6434 } else if (isa<CXXDynamicCastExpr>(E)) {
6435 return CXXDynamicCastExpr::Create(
6436 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6437 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6438 } else if (isa<CXXReinterpretCastExpr>(E)) {
6439 return CXXReinterpretCastExpr::Create(
6440 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6441 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6442 } else {
6443 return nullptr;
6444 }
6445}
6446
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006447Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6448 SubstNonTypeTemplateParmExpr *E) {
6449 QualType T = Importer.Import(E->getType());
6450 if (T.isNull())
6451 return nullptr;
6452
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006453 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006454 Importer.Import(E->getParameter()));
6455 if (!Param)
6456 return nullptr;
6457
6458 Expr *Replacement = Importer.Import(E->getReplacement());
6459 if (!Replacement)
6460 return nullptr;
6461
6462 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6463 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6464 Replacement);
6465}
6466
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006467Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6468 QualType ToType = Importer.Import(E->getType());
6469 if (ToType.isNull())
6470 return nullptr;
6471
6472 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6473 if (ImportContainerChecked(E->getArgs(), ToArgs))
6474 return nullptr;
6475
6476 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6477 // Value is always false.
6478 bool ToValue = false;
6479 if (!E->isValueDependent())
6480 ToValue = E->getValue();
6481
6482 return TypeTraitExpr::Create(
6483 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6484 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6485}
6486
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006487Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6488 QualType ToType = Importer.Import(E->getType());
6489 if (ToType.isNull())
6490 return nullptr;
6491
6492 if (E->isTypeOperand()) {
6493 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6494 if (!TSI)
6495 return nullptr;
6496
6497 return new (Importer.getToContext())
6498 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6499 }
6500
6501 Expr *Op = Importer.Import(E->getExprOperand());
6502 if (!Op)
6503 return nullptr;
6504
6505 return new (Importer.getToContext())
6506 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6507}
6508
Lang Hames19e07e12017-06-20 21:06:00 +00006509void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6510 CXXMethodDecl *FromMethod) {
6511 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6512 ToMethod->addOverriddenMethod(
6513 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6514 FromOverriddenMethod))));
6515}
6516
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006517ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006518 ASTContext &FromContext, FileManager &FromFileManager,
6519 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006520 : ToContext(ToContext), FromContext(FromContext),
6521 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6522 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006523 ImportedDecls[FromContext.getTranslationUnitDecl()]
6524 = ToContext.getTranslationUnitDecl();
6525}
6526
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006527ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006528
6529QualType ASTImporter::Import(QualType FromT) {
6530 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006531 return {};
John McCall424cec92011-01-19 06:33:43 +00006532
6533 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006534
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006535 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006536 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6537 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006538 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006539 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006540
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006541 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006542 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006543 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006544 if (ToT.isNull())
6545 return ToT;
6546
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006547 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006548 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006549
John McCall424cec92011-01-19 06:33:43 +00006550 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006551}
6552
Douglas Gregor62d311f2010-02-09 19:21:46 +00006553TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006554 if (!FromTSI)
6555 return FromTSI;
6556
6557 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006558 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006559 QualType T = Import(FromTSI->getType());
6560 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006561 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006562
6563 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006564 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006565}
6566
Sean Callanan59721b32015-04-28 18:41:46 +00006567Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6568 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6569 if (Pos != ImportedDecls.end()) {
6570 Decl *ToD = Pos->second;
6571 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6572 return ToD;
6573 } else {
6574 return nullptr;
6575 }
6576}
6577
Douglas Gregor62d311f2010-02-09 19:21:46 +00006578Decl *ASTImporter::Import(Decl *FromD) {
6579 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006580 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006581
Douglas Gregord451ea92011-07-29 23:31:30 +00006582 ASTNodeImporter Importer(*this);
6583
Douglas Gregor62d311f2010-02-09 19:21:46 +00006584 // Check whether we've already imported this declaration.
6585 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006586 if (Pos != ImportedDecls.end()) {
6587 Decl *ToD = Pos->second;
6588 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6589 return ToD;
6590 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006591
6592 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006593 Decl *ToD = Importer.Visit(FromD);
6594 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006595 return nullptr;
6596
Douglas Gregor62d311f2010-02-09 19:21:46 +00006597 // Record the imported declaration.
6598 ImportedDecls[FromD] = ToD;
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00006599
Douglas Gregor62d311f2010-02-09 19:21:46 +00006600 return ToD;
6601}
6602
6603DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6604 if (!FromDC)
6605 return FromDC;
6606
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006607 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006608 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006609 return nullptr;
6610
Douglas Gregor2e15c842012-02-01 21:00:38 +00006611 // When we're using a record/enum/Objective-C class/protocol as a context, we
6612 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006613 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6614 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006615 if (ToRecord->isCompleteDefinition()) {
6616 // Do nothing.
6617 } else if (FromRecord->isCompleteDefinition()) {
6618 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6619 ASTNodeImporter::IDK_Basic);
6620 } else {
6621 CompleteDecl(ToRecord);
6622 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006623 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6624 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006625 if (ToEnum->isCompleteDefinition()) {
6626 // Do nothing.
6627 } else if (FromEnum->isCompleteDefinition()) {
6628 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6629 ASTNodeImporter::IDK_Basic);
6630 } else {
6631 CompleteDecl(ToEnum);
6632 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006633 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6634 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006635 if (ToClass->getDefinition()) {
6636 // Do nothing.
6637 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6638 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6639 ASTNodeImporter::IDK_Basic);
6640 } else {
6641 CompleteDecl(ToClass);
6642 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006643 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6644 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006645 if (ToProto->getDefinition()) {
6646 // Do nothing.
6647 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6648 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6649 ASTNodeImporter::IDK_Basic);
6650 } else {
6651 CompleteDecl(ToProto);
6652 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006653 }
6654
6655 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006656}
6657
6658Expr *ASTImporter::Import(Expr *FromE) {
6659 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006660 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006661
6662 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6663}
6664
6665Stmt *ASTImporter::Import(Stmt *FromS) {
6666 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006667 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006668
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006669 // Check whether we've already imported this declaration.
6670 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6671 if (Pos != ImportedStmts.end())
6672 return Pos->second;
6673
6674 // Import the type
6675 ASTNodeImporter Importer(*this);
6676 Stmt *ToS = Importer.Visit(FromS);
6677 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006678 return nullptr;
6679
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006680 // Record the imported declaration.
6681 ImportedStmts[FromS] = ToS;
6682 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006683}
6684
6685NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6686 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006687 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006688
Douglas Gregor90ebf252011-04-27 16:48:40 +00006689 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6690
6691 switch (FromNNS->getKind()) {
6692 case NestedNameSpecifier::Identifier:
6693 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6694 return NestedNameSpecifier::Create(ToContext, prefix, II);
6695 }
Craig Topper36250ad2014-05-12 05:36:57 +00006696 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006697
6698 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006699 if (auto *NS =
6700 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006701 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6702 }
Craig Topper36250ad2014-05-12 05:36:57 +00006703 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006704
6705 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006706 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006707 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006708 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6709 }
Craig Topper36250ad2014-05-12 05:36:57 +00006710 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006711
6712 case NestedNameSpecifier::Global:
6713 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6714
Nikola Smiljanic67860242014-09-26 00:28:20 +00006715 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006716 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006717 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006718 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6719 }
6720 return nullptr;
6721
Douglas Gregor90ebf252011-04-27 16:48:40 +00006722 case NestedNameSpecifier::TypeSpec:
6723 case NestedNameSpecifier::TypeSpecWithTemplate: {
6724 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6725 if (!T.isNull()) {
6726 bool bTemplate = FromNNS->getKind() ==
6727 NestedNameSpecifier::TypeSpecWithTemplate;
6728 return NestedNameSpecifier::Create(ToContext, prefix,
6729 bTemplate, T.getTypePtr());
6730 }
6731 }
Craig Topper36250ad2014-05-12 05:36:57 +00006732 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006733 }
6734
6735 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006736}
6737
Douglas Gregor14454802011-02-25 02:25:35 +00006738NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006739 // Copied from NestedNameSpecifier mostly.
6740 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6741 NestedNameSpecifierLoc NNS = FromNNS;
6742
6743 // Push each of the nested-name-specifiers's onto a stack for
6744 // serialization in reverse order.
6745 while (NNS) {
6746 NestedNames.push_back(NNS);
6747 NNS = NNS.getPrefix();
6748 }
6749
6750 NestedNameSpecifierLocBuilder Builder;
6751
6752 while (!NestedNames.empty()) {
6753 NNS = NestedNames.pop_back_val();
6754 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6755 if (!Spec)
6756 return NestedNameSpecifierLoc();
6757
6758 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6759 switch (Kind) {
6760 case NestedNameSpecifier::Identifier:
6761 Builder.Extend(getToContext(),
6762 Spec->getAsIdentifier(),
6763 Import(NNS.getLocalBeginLoc()),
6764 Import(NNS.getLocalEndLoc()));
6765 break;
6766
6767 case NestedNameSpecifier::Namespace:
6768 Builder.Extend(getToContext(),
6769 Spec->getAsNamespace(),
6770 Import(NNS.getLocalBeginLoc()),
6771 Import(NNS.getLocalEndLoc()));
6772 break;
6773
6774 case NestedNameSpecifier::NamespaceAlias:
6775 Builder.Extend(getToContext(),
6776 Spec->getAsNamespaceAlias(),
6777 Import(NNS.getLocalBeginLoc()),
6778 Import(NNS.getLocalEndLoc()));
6779 break;
6780
6781 case NestedNameSpecifier::TypeSpec:
6782 case NestedNameSpecifier::TypeSpecWithTemplate: {
6783 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6784 QualType(Spec->getAsType(), 0));
6785 Builder.Extend(getToContext(),
6786 Import(NNS.getLocalBeginLoc()),
6787 TSI->getTypeLoc(),
6788 Import(NNS.getLocalEndLoc()));
6789 break;
6790 }
6791
6792 case NestedNameSpecifier::Global:
6793 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6794 break;
6795
6796 case NestedNameSpecifier::Super: {
6797 SourceRange ToRange = Import(NNS.getSourceRange());
6798 Builder.MakeSuper(getToContext(),
6799 Spec->getAsRecordDecl(),
6800 ToRange.getBegin(),
6801 ToRange.getEnd());
6802 }
6803 }
6804 }
6805
6806 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006807}
6808
Douglas Gregore2e50d332010-12-01 01:36:18 +00006809TemplateName ASTImporter::Import(TemplateName From) {
6810 switch (From.getKind()) {
6811 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006812 if (auto *ToTemplate =
6813 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006814 return TemplateName(ToTemplate);
6815
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006816 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006817
6818 case TemplateName::OverloadedTemplate: {
6819 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6820 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006821 for (auto *I : *FromStorage) {
6822 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006823 ToTemplates.addDecl(To);
6824 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006825 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006826 }
6827 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6828 ToTemplates.end());
6829 }
6830
6831 case TemplateName::QualifiedTemplate: {
6832 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6833 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6834 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006835 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006836
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006837 if (auto *ToTemplate =
6838 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006839 return ToContext.getQualifiedTemplateName(Qualifier,
6840 QTN->hasTemplateKeyword(),
6841 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006842
6843 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006844 }
6845
6846 case TemplateName::DependentTemplate: {
6847 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6848 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6849 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006850 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006851
6852 if (DTN->isIdentifier()) {
6853 return ToContext.getDependentTemplateName(Qualifier,
6854 Import(DTN->getIdentifier()));
6855 }
6856
6857 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6858 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006859
6860 case TemplateName::SubstTemplateTemplateParm: {
6861 SubstTemplateTemplateParmStorage *subst
6862 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006863 auto *param =
6864 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006865 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006866 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006867
6868 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006869 if (replacement.isNull())
6870 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006871
6872 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6873 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006874
6875 case TemplateName::SubstTemplateTemplateParmPack: {
6876 SubstTemplateTemplateParmPackStorage *SubstPack
6877 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006878 auto *Param =
6879 cast_or_null<TemplateTemplateParmDecl>(
6880 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00006881 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006882 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006883
6884 ASTNodeImporter Importer(*this);
6885 TemplateArgument ArgPack
6886 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6887 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006888 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006889
6890 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6891 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006892 }
6893
6894 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006895}
6896
Douglas Gregor62d311f2010-02-09 19:21:46 +00006897SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6898 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006899 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00006900
Douglas Gregor811663e2010-02-10 00:15:17 +00006901 SourceManager &FromSM = FromContext.getSourceManager();
6902
Sean Callanan24c5fe62016-11-07 20:42:25 +00006903 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006904 // don't have to import macro expansions.
6905 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006906 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006907 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6908 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006909 FileID ToFileID = Import(Decomposed.first);
6910 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006911 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00006912 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6913 .getLocWithOffset(Decomposed.second);
6914 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006915}
6916
6917SourceRange ASTImporter::Import(SourceRange FromRange) {
6918 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6919}
6920
Douglas Gregor811663e2010-02-10 00:15:17 +00006921FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006922 llvm::DenseMap<FileID, FileID>::iterator Pos
6923 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006924 if (Pos != ImportedFileIDs.end())
6925 return Pos->second;
6926
6927 SourceManager &FromSM = FromContext.getSourceManager();
6928 SourceManager &ToSM = ToContext.getSourceManager();
6929 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006930 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006931
6932 // Include location of this file.
6933 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6934
6935 // Map the FileID for to the "to" source manager.
6936 FileID ToID;
6937 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006938 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006939 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6940 // disk again
6941 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6942 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006943 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006944 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006945 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00006946 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6947 FromSLoc.getFile().getFileCharacteristic());
6948 } else {
6949 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006950 const llvm::MemoryBuffer *
6951 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006952 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006953 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006954 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006955 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006956 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006957 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006958
Sebastian Redl99219f12010-09-30 01:03:06 +00006959 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006960 return ToID;
6961}
6962
Sean Callanandd2c1742016-05-16 20:48:03 +00006963CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6964 Expr *ToExpr = Import(From->getInit());
6965 if (!ToExpr && From->getInit())
6966 return nullptr;
6967
6968 if (From->isBaseInitializer()) {
6969 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6970 if (!ToTInfo && From->getTypeSourceInfo())
6971 return nullptr;
6972
6973 return new (ToContext) CXXCtorInitializer(
6974 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6975 ToExpr, Import(From->getRParenLoc()),
6976 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6977 : SourceLocation());
6978 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006979 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006980 if (!ToField && From->getMember())
6981 return nullptr;
6982
6983 return new (ToContext) CXXCtorInitializer(
6984 ToContext, ToField, Import(From->getMemberLocation()),
6985 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6986 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006987 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00006988 Import(From->getIndirectMember()));
6989 if (!ToIField && From->getIndirectMember())
6990 return nullptr;
6991
6992 return new (ToContext) CXXCtorInitializer(
6993 ToContext, ToIField, Import(From->getMemberLocation()),
6994 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6995 } else if (From->isDelegatingInitializer()) {
6996 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6997 if (!ToTInfo && From->getTypeSourceInfo())
6998 return nullptr;
6999
7000 return new (ToContext)
7001 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7002 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007003 } else {
7004 return nullptr;
7005 }
7006}
7007
Aleksei Sidorina693b372016-09-28 10:16:56 +00007008CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7009 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7010 if (Pos != ImportedCXXBaseSpecifiers.end())
7011 return Pos->second;
7012
7013 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7014 Import(BaseSpec->getSourceRange()),
7015 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7016 BaseSpec->getAccessSpecifierAsWritten(),
7017 Import(BaseSpec->getTypeSourceInfo()),
7018 Import(BaseSpec->getEllipsisLoc()));
7019 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7020 return Imported;
7021}
7022
Douglas Gregor0a791672011-01-18 03:11:38 +00007023void ASTImporter::ImportDefinition(Decl *From) {
7024 Decl *To = Import(From);
7025 if (!To)
7026 return;
7027
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007028 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007029 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007030
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007031 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007032 if (!ToRecord->getDefinition()) {
7033 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007034 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007035 return;
7036 }
7037 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007038
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007039 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007040 if (!ToEnum->getDefinition()) {
7041 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007042 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007043 return;
7044 }
7045 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007046
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007047 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007048 if (!ToIFace->getDefinition()) {
7049 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007050 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007051 return;
7052 }
7053 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007054
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007055 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007056 if (!ToProto->getDefinition()) {
7057 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007058 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007059 return;
7060 }
7061 }
7062
Douglas Gregor0a791672011-01-18 03:11:38 +00007063 Importer.ImportDeclContext(FromDC, true);
7064 }
7065}
7066
Douglas Gregor96e578d2010-02-05 17:54:41 +00007067DeclarationName ASTImporter::Import(DeclarationName FromName) {
7068 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007069 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007070
7071 switch (FromName.getNameKind()) {
7072 case DeclarationName::Identifier:
7073 return Import(FromName.getAsIdentifierInfo());
7074
7075 case DeclarationName::ObjCZeroArgSelector:
7076 case DeclarationName::ObjCOneArgSelector:
7077 case DeclarationName::ObjCMultiArgSelector:
7078 return Import(FromName.getObjCSelector());
7079
7080 case DeclarationName::CXXConstructorName: {
7081 QualType T = Import(FromName.getCXXNameType());
7082 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007083 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007084
7085 return ToContext.DeclarationNames.getCXXConstructorName(
7086 ToContext.getCanonicalType(T));
7087 }
7088
7089 case DeclarationName::CXXDestructorName: {
7090 QualType T = Import(FromName.getCXXNameType());
7091 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007092 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007093
7094 return ToContext.DeclarationNames.getCXXDestructorName(
7095 ToContext.getCanonicalType(T));
7096 }
7097
Richard Smith35845152017-02-07 01:37:30 +00007098 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007099 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007100 Import(FromName.getCXXDeductionGuideTemplate()));
7101 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007102 return {};
Richard Smith35845152017-02-07 01:37:30 +00007103 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7104 }
7105
Douglas Gregor96e578d2010-02-05 17:54:41 +00007106 case DeclarationName::CXXConversionFunctionName: {
7107 QualType T = Import(FromName.getCXXNameType());
7108 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007109 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007110
7111 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7112 ToContext.getCanonicalType(T));
7113 }
7114
7115 case DeclarationName::CXXOperatorName:
7116 return ToContext.DeclarationNames.getCXXOperatorName(
7117 FromName.getCXXOverloadedOperator());
7118
7119 case DeclarationName::CXXLiteralOperatorName:
7120 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7121 Import(FromName.getCXXLiteralIdentifier()));
7122
7123 case DeclarationName::CXXUsingDirective:
7124 // FIXME: STATICS!
7125 return DeclarationName::getUsingDirectiveName();
7126 }
7127
David Blaikiee4d798f2012-01-20 21:50:17 +00007128 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007129}
7130
Douglas Gregore2e50d332010-12-01 01:36:18 +00007131IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007132 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007133 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007134
Sean Callananf94ef1d2016-05-14 06:11:19 +00007135 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7136
7137 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7138 ToId->setBuiltinID(FromId->getBuiltinID());
7139
7140 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007141}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007142
Douglas Gregor43f54792010-02-17 02:12:47 +00007143Selector ASTImporter::Import(Selector FromSel) {
7144 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007145 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007146
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007147 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007148 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7149 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7150 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7151 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7152}
7153
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007154DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7155 DeclContext *DC,
7156 unsigned IDNS,
7157 NamedDecl **Decls,
7158 unsigned NumDecls) {
7159 return Name;
7160}
7161
7162DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007163 if (LastDiagFromFrom)
7164 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7165 FromContext.getDiagnostics());
7166 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007167 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007168}
7169
7170DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007171 if (!LastDiagFromFrom)
7172 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7173 ToContext.getDiagnostics());
7174 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007175 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007176}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007177
Douglas Gregor2e15c842012-02-01 21:00:38 +00007178void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007179 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007180 if (!ID->getDefinition())
7181 ID->startDefinition();
7182 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007183 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007184 if (!PD->getDefinition())
7185 PD->startDefinition();
7186 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007187 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007188 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7189 TD->startDefinition();
7190 TD->setCompleteDefinition(true);
7191 }
7192 }
7193 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007194 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007195 }
7196}
7197
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007198Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007199 if (From->hasAttrs()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007200 for (auto *FromAttr : From->getAttrs())
Sean Callanan8bca9962016-03-28 21:43:01 +00007201 To->addAttr(FromAttr->clone(To->getASTContext()));
7202 }
7203 if (From->isUsed()) {
7204 To->setIsUsed();
7205 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007206 if (From->isImplicit()) {
7207 To->setImplicit();
7208 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007209 ImportedDecls[From] = To;
7210 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007211}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007212
Douglas Gregordd6006f2012-07-17 21:16:27 +00007213bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7214 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007215 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007216 = ImportedTypes.find(From.getTypePtr());
7217 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7218 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007219
Douglas Gregordd6006f2012-07-17 21:16:27 +00007220 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7221 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007222 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007223}