blob: 17ff23c5fe099a75142c1783221453900c26294c [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;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002712 if (NamedDecl *FriendD = D->getFriendDecl()) {
2713 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2714 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2715 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2716 ToFriendD->setObjectOfFriendDecl(false);
2717
2718 ToFU = ToFriendD;
2719 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002720 ToFU = Importer.Import(D->getFriendType());
2721 if (!ToFU)
2722 return nullptr;
2723
2724 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002725 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002726 for (unsigned I = 0; I < D->NumTPLists; I++) {
2727 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2728 if (!List)
2729 return nullptr;
2730 ToTPLists[I] = List;
2731 }
2732
2733 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2734 Importer.Import(D->getLocation()),
2735 ToFU, Importer.Import(D->getFriendLoc()),
2736 ToTPLists);
2737
2738 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002739
2740 FrD->setAccess(D->getAccess());
2741 FrD->setLexicalDeclContext(LexicalDC);
2742 LexicalDC->addDeclInternal(FrD);
2743 return FrD;
2744}
2745
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002746Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2747 // Import the major distinguishing characteristics of an ivar.
2748 DeclContext *DC, *LexicalDC;
2749 DeclarationName Name;
2750 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002751 NamedDecl *ToD;
2752 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002753 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002754 if (ToD)
2755 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002756
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002757 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002758 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002759 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002760 for (auto *FoundDecl : FoundDecls) {
2761 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002762 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002763 FoundIvar->getType())) {
2764 Importer.Imported(D, FoundIvar);
2765 return FoundIvar;
2766 }
2767
2768 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2769 << Name << D->getType() << FoundIvar->getType();
2770 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2771 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002772 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002773 }
2774 }
2775
2776 // Import the type.
2777 QualType T = Importer.Import(D->getType());
2778 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002779 return nullptr;
2780
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002781 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2782 Expr *BitWidth = Importer.Import(D->getBitWidth());
2783 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002784 return nullptr;
2785
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002786 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2787 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002788 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002789 Loc, Name.getAsIdentifierInfo(),
2790 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002791 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002792 ToIvar->setLexicalDeclContext(LexicalDC);
2793 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002794 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002795 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002796}
2797
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002798Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2799 // Import the major distinguishing characteristics of a variable.
2800 DeclContext *DC, *LexicalDC;
2801 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002802 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002803 NamedDecl *ToD;
2804 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002805 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002806 if (ToD)
2807 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002808
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002809 // Try to find a variable in our own ("to") context with the same name and
2810 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002811 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002812 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002813 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002814 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002815 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002816 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002817 for (auto *FoundDecl : FoundDecls) {
2818 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002819 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002820
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002821 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002822 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002823 if (FoundVar->hasExternalFormalLinkage() &&
2824 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002825 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002826 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002827 MergeWithVar = FoundVar;
2828 break;
2829 }
2830
Douglas Gregor56521c52010-02-12 17:23:39 +00002831 const ArrayType *FoundArray
2832 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2833 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002834 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002835 if (FoundArray && TArray) {
2836 if (isa<IncompleteArrayType>(FoundArray) &&
2837 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002838 // Import the type.
2839 QualType T = Importer.Import(D->getType());
2840 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002841 return nullptr;
2842
Douglas Gregor56521c52010-02-12 17:23:39 +00002843 FoundVar->setType(T);
2844 MergeWithVar = FoundVar;
2845 break;
2846 } else if (isa<IncompleteArrayType>(TArray) &&
2847 isa<ConstantArrayType>(FoundArray)) {
2848 MergeWithVar = FoundVar;
2849 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002850 }
2851 }
2852
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002853 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002854 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002855 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2856 << FoundVar->getType();
2857 }
2858 }
2859
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002860 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002861 }
2862
2863 if (MergeWithVar) {
2864 // An equivalent variable with external linkage has been found. Link
2865 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002866 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002867
2868 if (VarDecl *DDef = D->getDefinition()) {
2869 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2870 Importer.ToDiag(ExistingDef->getLocation(),
2871 diag::err_odr_variable_multiple_def)
2872 << Name;
2873 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2874 } else {
2875 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002876 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002877 if (DDef->isInitKnownICE()) {
2878 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2879 Eval->CheckedICE = true;
2880 Eval->IsICE = DDef->isInitICE();
2881 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002882 }
2883 }
2884
2885 return MergeWithVar;
2886 }
2887
2888 if (!ConflictingDecls.empty()) {
2889 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2890 ConflictingDecls.data(),
2891 ConflictingDecls.size());
2892 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002893 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002894 }
2895 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002896
Douglas Gregorb4964f72010-02-15 23:54:17 +00002897 // Import the type.
2898 QualType T = Importer.Import(D->getType());
2899 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002900 return nullptr;
2901
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002902 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002903 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002904 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2905 Importer.Import(D->getInnerLocStart()),
2906 Loc, Name.getAsIdentifierInfo(),
2907 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002908 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002909 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002910 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002911 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002912 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002913
2914 // Templated declarations should never appear in the enclosing DeclContext.
2915 if (!D->getDescribedVarTemplate())
2916 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002917
Sean Callanan59721b32015-04-28 18:41:46 +00002918 if (!D->isFileVarDecl() &&
2919 D->isUsed())
2920 ToVar->setIsUsed();
2921
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002922 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002923 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002924 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002925
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002926 if (D->isConstexpr())
2927 ToVar->setConstexpr(true);
2928
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002929 return ToVar;
2930}
2931
Douglas Gregor8b228d72010-02-17 21:22:52 +00002932Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2933 // Parameters are created in the translation unit's context, then moved
2934 // into the function declaration's context afterward.
2935 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2936
2937 // Import the name of this declaration.
2938 DeclarationName Name = Importer.Import(D->getDeclName());
2939 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002940 return nullptr;
2941
Douglas Gregor8b228d72010-02-17 21:22:52 +00002942 // Import the location of this declaration.
2943 SourceLocation Loc = Importer.Import(D->getLocation());
2944
2945 // Import the parameter's type.
2946 QualType T = Importer.Import(D->getType());
2947 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002948 return nullptr;
2949
Douglas Gregor8b228d72010-02-17 21:22:52 +00002950 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002951 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2952 Name.getAsIdentifierInfo(), T,
2953 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002954 return Importer.Imported(D, ToParm);
2955}
2956
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002957Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2958 // Parameters are created in the translation unit's context, then moved
2959 // into the function declaration's context afterward.
2960 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2961
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002962 // Import the name of this declaration.
2963 DeclarationName Name = Importer.Import(D->getDeclName());
2964 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002965 return nullptr;
2966
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002967 // Import the location of this declaration.
2968 SourceLocation Loc = Importer.Import(D->getLocation());
2969
2970 // Import the parameter's type.
2971 QualType T = Importer.Import(D->getType());
2972 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002973 return nullptr;
2974
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002975 // Create the imported parameter.
2976 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2977 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002978 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002979 Loc, Name.getAsIdentifierInfo(),
2980 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002981 /*DefaultArg*/ nullptr);
2982
2983 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002984 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002985 ToParm->setKNRPromoted(D->isKNRPromoted());
2986
2987 Expr *ToDefArg = nullptr;
2988 Expr *FromDefArg = nullptr;
2989 if (D->hasUninstantiatedDefaultArg()) {
2990 FromDefArg = D->getUninstantiatedDefaultArg();
2991 ToDefArg = Importer.Import(FromDefArg);
2992 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2993 } else if (D->hasUnparsedDefaultArg()) {
2994 ToParm->setUnparsedDefaultArg();
2995 } else if (D->hasDefaultArg()) {
2996 FromDefArg = D->getDefaultArg();
2997 ToDefArg = Importer.Import(FromDefArg);
2998 ToParm->setDefaultArg(ToDefArg);
2999 }
3000 if (FromDefArg && !ToDefArg)
3001 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003002
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003003 if (D->isObjCMethodParameter()) {
3004 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3005 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3006 } else {
3007 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3008 D->getFunctionScopeIndex());
3009 }
3010
Sean Callanan59721b32015-04-28 18:41:46 +00003011 if (D->isUsed())
3012 ToParm->setIsUsed();
3013
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003014 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003015}
3016
Douglas Gregor43f54792010-02-17 02:12:47 +00003017Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3018 // Import the major distinguishing characteristics of a method.
3019 DeclContext *DC, *LexicalDC;
3020 DeclarationName Name;
3021 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003022 NamedDecl *ToD;
3023 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003024 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003025 if (ToD)
3026 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003027
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003028 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003029 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003030 for (auto *FoundDecl : FoundDecls) {
3031 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003032 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3033 continue;
3034
3035 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003036 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3037 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003038 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003039 << D->isInstanceMethod() << Name << D->getReturnType()
3040 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003041 Importer.ToDiag(FoundMethod->getLocation(),
3042 diag::note_odr_objc_method_here)
3043 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003044 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003045 }
3046
3047 // Check the number of parameters.
3048 if (D->param_size() != FoundMethod->param_size()) {
3049 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3050 << D->isInstanceMethod() << Name
3051 << D->param_size() << FoundMethod->param_size();
3052 Importer.ToDiag(FoundMethod->getLocation(),
3053 diag::note_odr_objc_method_here)
3054 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003055 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003056 }
3057
3058 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003059 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003060 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3061 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003062 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003063 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003064 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 diag::err_odr_objc_method_param_type_inconsistent)
3066 << D->isInstanceMethod() << Name
3067 << (*P)->getType() << (*FoundP)->getType();
3068 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3069 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003070 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003071 }
3072 }
3073
3074 // Check variadic/non-variadic.
3075 // Check the number of parameters.
3076 if (D->isVariadic() != FoundMethod->isVariadic()) {
3077 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3078 << D->isInstanceMethod() << Name;
3079 Importer.ToDiag(FoundMethod->getLocation(),
3080 diag::note_odr_objc_method_here)
3081 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003082 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003083 }
3084
3085 // FIXME: Any other bits we need to merge?
3086 return Importer.Imported(D, FoundMethod);
3087 }
3088 }
3089
3090 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003091 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003092 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003093 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003094
Alp Toker314cc812014-01-25 16:55:45 +00003095 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003096
Alp Toker314cc812014-01-25 16:55:45 +00003097 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3098 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3099 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3100 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3101 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003102
3103 // FIXME: When we decide to merge method definitions, we'll need to
3104 // deal with implicit parameters.
3105
3106 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003107 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003108 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003109 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003110 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003111 return nullptr;
3112
Douglas Gregor43f54792010-02-17 02:12:47 +00003113 ToParams.push_back(ToP);
3114 }
3115
3116 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003117 for (auto *ToParam : ToParams) {
3118 ToParam->setOwningFunction(ToMethod);
3119 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003120 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003121
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003122 SmallVector<SourceLocation, 12> SelLocs;
3123 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003124 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003125 Loc = Importer.Import(Loc);
3126
3127 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003128
3129 ToMethod->setLexicalDeclContext(LexicalDC);
3130 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003131 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003132 return ToMethod;
3133}
3134
Douglas Gregor85f3f952015-07-07 03:57:15 +00003135Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3136 // Import the major distinguishing characteristics of a category.
3137 DeclContext *DC, *LexicalDC;
3138 DeclarationName Name;
3139 SourceLocation Loc;
3140 NamedDecl *ToD;
3141 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3142 return nullptr;
3143 if (ToD)
3144 return ToD;
3145
3146 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3147 if (!BoundInfo)
3148 return nullptr;
3149
3150 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3151 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003152 D->getVariance(),
3153 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003154 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003155 Importer.Import(D->getLocation()),
3156 Name.getAsIdentifierInfo(),
3157 Importer.Import(D->getColonLoc()),
3158 BoundInfo);
3159 Importer.Imported(D, Result);
3160 Result->setLexicalDeclContext(LexicalDC);
3161 return Result;
3162}
3163
Douglas Gregor84c51c32010-02-18 01:47:50 +00003164Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3165 // Import the major distinguishing characteristics of a category.
3166 DeclContext *DC, *LexicalDC;
3167 DeclarationName Name;
3168 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003169 NamedDecl *ToD;
3170 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003171 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003172 if (ToD)
3173 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003174
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003175 auto *ToInterface =
3176 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003177 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
3179
Douglas Gregor84c51c32010-02-18 01:47:50 +00003180 // Determine if we've already encountered this category.
3181 ObjCCategoryDecl *MergeWithCategory
3182 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3183 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3184 if (!ToCategory) {
3185 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003186 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003187 Loc,
3188 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003189 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003190 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003191 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003192 Importer.Import(D->getIvarLBraceLoc()),
3193 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003194 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003195 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003196 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003197 // Import the type parameter list after calling Imported, to avoid
3198 // loops when bringing in their DeclContext.
3199 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3200 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003201
Douglas Gregor84c51c32010-02-18 01:47:50 +00003202 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003203 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3204 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003205 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3206 = D->protocol_loc_begin();
3207 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3208 FromProtoEnd = D->protocol_end();
3209 FromProto != FromProtoEnd;
3210 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003211 auto *ToProto =
3212 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003213 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003214 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003215 Protocols.push_back(ToProto);
3216 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3217 }
3218
3219 // FIXME: If we're merging, make sure that the protocol list is the same.
3220 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3221 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003222 } else {
3223 Importer.Imported(D, ToCategory);
3224 }
3225
3226 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003227 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003228
3229 // If we have an implementation, import it as well.
3230 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003231 auto *Impl =
3232 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003233 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003234 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003235 return nullptr;
3236
Douglas Gregor84c51c32010-02-18 01:47:50 +00003237 ToCategory->setImplementation(Impl);
3238 }
3239
3240 return ToCategory;
3241}
3242
Douglas Gregor2aa53772012-01-24 17:42:07 +00003243bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3244 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003245 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003246 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003247 if (shouldForceImportDeclContext(Kind))
3248 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003249 return false;
3250 }
3251
3252 // Start the protocol definition
3253 To->startDefinition();
3254
3255 // Import protocols
3256 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3257 SmallVector<SourceLocation, 4> ProtocolLocs;
3258 ObjCProtocolDecl::protocol_loc_iterator
3259 FromProtoLoc = From->protocol_loc_begin();
3260 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3261 FromProtoEnd = From->protocol_end();
3262 FromProto != FromProtoEnd;
3263 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003264 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003265 if (!ToProto)
3266 return true;
3267 Protocols.push_back(ToProto);
3268 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3269 }
3270
3271 // FIXME: If we're merging, make sure that the protocol list is the same.
3272 To->setProtocolList(Protocols.data(), Protocols.size(),
3273 ProtocolLocs.data(), Importer.getToContext());
3274
Douglas Gregor2e15c842012-02-01 21:00:38 +00003275 if (shouldForceImportDeclContext(Kind)) {
3276 // Import all of the members of this protocol.
3277 ImportDeclContext(From, /*ForceImport=*/true);
3278 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003279 return false;
3280}
3281
Douglas Gregor98d156a2010-02-17 16:12:00 +00003282Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003283 // If this protocol has a definition in the translation unit we're coming
3284 // from, but this particular declaration is not that definition, import the
3285 // definition and map to that.
3286 ObjCProtocolDecl *Definition = D->getDefinition();
3287 if (Definition && Definition != D) {
3288 Decl *ImportedDef = Importer.Import(Definition);
3289 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003290 return nullptr;
3291
Douglas Gregor2aa53772012-01-24 17:42:07 +00003292 return Importer.Imported(D, ImportedDef);
3293 }
3294
Douglas Gregor84c51c32010-02-18 01:47:50 +00003295 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003296 DeclContext *DC, *LexicalDC;
3297 DeclarationName Name;
3298 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003299 NamedDecl *ToD;
3300 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003301 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003302 if (ToD)
3303 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003304
Craig Topper36250ad2014-05-12 05:36:57 +00003305 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003306 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003307 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003308 for (auto *FoundDecl : FoundDecls) {
3309 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003310 continue;
3311
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003312 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003313 break;
3314 }
3315
3316 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003317 if (!ToProto) {
3318 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3319 Name.getAsIdentifierInfo(), Loc,
3320 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003321 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003322 ToProto->setLexicalDeclContext(LexicalDC);
3323 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003324 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003325
3326 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003327
Douglas Gregor2aa53772012-01-24 17:42:07 +00003328 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003329 return nullptr;
3330
Douglas Gregor98d156a2010-02-17 16:12:00 +00003331 return ToProto;
3332}
3333
Sean Callanan0aae0412014-12-10 00:00:37 +00003334Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3335 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3336 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3337
3338 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3339 SourceLocation LangLoc = Importer.Import(D->getLocation());
3340
3341 bool HasBraces = D->hasBraces();
3342
Sean Callananb12a8552014-12-10 21:22:20 +00003343 LinkageSpecDecl *ToLinkageSpec =
3344 LinkageSpecDecl::Create(Importer.getToContext(),
3345 DC,
3346 ExternLoc,
3347 LangLoc,
3348 D->getLanguage(),
3349 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003350
3351 if (HasBraces) {
3352 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3353 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3354 }
3355
3356 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3357 LexicalDC->addDeclInternal(ToLinkageSpec);
3358
3359 Importer.Imported(D, ToLinkageSpec);
3360
3361 return ToLinkageSpec;
3362}
3363
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003364Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3365 DeclContext *DC, *LexicalDC;
3366 DeclarationName Name;
3367 SourceLocation Loc;
3368 NamedDecl *ToD = nullptr;
3369 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3370 return nullptr;
3371 if (ToD)
3372 return ToD;
3373
3374 DeclarationNameInfo NameInfo(Name,
3375 Importer.Import(D->getNameInfo().getLoc()));
3376 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3377
3378 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3379 Importer.Import(D->getUsingLoc()),
3380 Importer.Import(D->getQualifierLoc()),
3381 NameInfo, D->hasTypename());
3382 ToUsing->setLexicalDeclContext(LexicalDC);
3383 LexicalDC->addDeclInternal(ToUsing);
3384 Importer.Imported(D, ToUsing);
3385
3386 if (NamedDecl *FromPattern =
3387 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003388 if (auto *ToPattern =
3389 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003390 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3391 else
3392 return nullptr;
3393 }
3394
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003395 for (auto *FromShadow : D->shadows()) {
3396 if (auto *ToShadow =
3397 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003398 ToUsing->addShadowDecl(ToShadow);
3399 else
3400 // FIXME: We return a nullptr here but the definition is already created
3401 // and available with lookups. How to fix this?..
3402 return nullptr;
3403 }
3404 return ToUsing;
3405}
3406
3407Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3408 DeclContext *DC, *LexicalDC;
3409 DeclarationName Name;
3410 SourceLocation Loc;
3411 NamedDecl *ToD = nullptr;
3412 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3413 return nullptr;
3414 if (ToD)
3415 return ToD;
3416
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003417 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3418 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003419 if (!ToUsing)
3420 return nullptr;
3421
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003422 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3423 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003424 if (!ToTarget)
3425 return nullptr;
3426
3427 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3428 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3429
3430 ToShadow->setLexicalDeclContext(LexicalDC);
3431 ToShadow->setAccess(D->getAccess());
3432 Importer.Imported(D, ToShadow);
3433
3434 if (UsingShadowDecl *FromPattern =
3435 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003436 if (auto *ToPattern =
3437 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003438 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3439 ToPattern);
3440 else
3441 // FIXME: We return a nullptr here but the definition is already created
3442 // and available with lookups. How to fix this?..
3443 return nullptr;
3444 }
3445
3446 LexicalDC->addDeclInternal(ToShadow);
3447
3448 return ToShadow;
3449}
3450
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003451Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3452 DeclContext *DC, *LexicalDC;
3453 DeclarationName Name;
3454 SourceLocation Loc;
3455 NamedDecl *ToD = nullptr;
3456 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3457 return nullptr;
3458 if (ToD)
3459 return ToD;
3460
3461 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3462 if (!ToComAncestor)
3463 return nullptr;
3464
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003465 auto *ToNominated = cast_or_null<NamespaceDecl>(
3466 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003467 if (!ToNominated)
3468 return nullptr;
3469
3470 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3471 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3472 Importer.Import(D->getNamespaceKeyLocation()),
3473 Importer.Import(D->getQualifierLoc()),
3474 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3475 ToUsingDir->setLexicalDeclContext(LexicalDC);
3476 LexicalDC->addDeclInternal(ToUsingDir);
3477 Importer.Imported(D, ToUsingDir);
3478
3479 return ToUsingDir;
3480}
3481
3482Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3483 UnresolvedUsingValueDecl *D) {
3484 DeclContext *DC, *LexicalDC;
3485 DeclarationName Name;
3486 SourceLocation Loc;
3487 NamedDecl *ToD = nullptr;
3488 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3489 return nullptr;
3490 if (ToD)
3491 return ToD;
3492
3493 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3494 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3495
3496 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3497 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3498 Importer.Import(D->getQualifierLoc()), NameInfo,
3499 Importer.Import(D->getEllipsisLoc()));
3500
3501 Importer.Imported(D, ToUsingValue);
3502 ToUsingValue->setAccess(D->getAccess());
3503 ToUsingValue->setLexicalDeclContext(LexicalDC);
3504 LexicalDC->addDeclInternal(ToUsingValue);
3505
3506 return ToUsingValue;
3507}
3508
3509Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3510 UnresolvedUsingTypenameDecl *D) {
3511 DeclContext *DC, *LexicalDC;
3512 DeclarationName Name;
3513 SourceLocation Loc;
3514 NamedDecl *ToD = nullptr;
3515 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3516 return nullptr;
3517 if (ToD)
3518 return ToD;
3519
3520 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3521 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3522 Importer.Import(D->getTypenameLoc()),
3523 Importer.Import(D->getQualifierLoc()), Loc, Name,
3524 Importer.Import(D->getEllipsisLoc()));
3525
3526 Importer.Imported(D, ToUsing);
3527 ToUsing->setAccess(D->getAccess());
3528 ToUsing->setLexicalDeclContext(LexicalDC);
3529 LexicalDC->addDeclInternal(ToUsing);
3530
3531 return ToUsing;
3532}
3533
Douglas Gregor2aa53772012-01-24 17:42:07 +00003534bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3535 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003536 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003537 if (To->getDefinition()) {
3538 // Check consistency of superclass.
3539 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3540 if (FromSuper) {
3541 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3542 if (!FromSuper)
3543 return true;
3544 }
3545
3546 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3547 if ((bool)FromSuper != (bool)ToSuper ||
3548 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3549 Importer.ToDiag(To->getLocation(),
3550 diag::err_odr_objc_superclass_inconsistent)
3551 << To->getDeclName();
3552 if (ToSuper)
3553 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3554 << To->getSuperClass()->getDeclName();
3555 else
3556 Importer.ToDiag(To->getLocation(),
3557 diag::note_odr_objc_missing_superclass);
3558 if (From->getSuperClass())
3559 Importer.FromDiag(From->getSuperClassLoc(),
3560 diag::note_odr_objc_superclass)
3561 << From->getSuperClass()->getDeclName();
3562 else
3563 Importer.FromDiag(From->getLocation(),
3564 diag::note_odr_objc_missing_superclass);
3565 }
3566
Douglas Gregor2e15c842012-02-01 21:00:38 +00003567 if (shouldForceImportDeclContext(Kind))
3568 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003569 return false;
3570 }
3571
3572 // Start the definition.
3573 To->startDefinition();
3574
3575 // If this class has a superclass, import it.
3576 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003577 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3578 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003579 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003580
3581 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003582 }
3583
3584 // Import protocols
3585 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3586 SmallVector<SourceLocation, 4> ProtocolLocs;
3587 ObjCInterfaceDecl::protocol_loc_iterator
3588 FromProtoLoc = From->protocol_loc_begin();
3589
3590 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3591 FromProtoEnd = From->protocol_end();
3592 FromProto != FromProtoEnd;
3593 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003594 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003595 if (!ToProto)
3596 return true;
3597 Protocols.push_back(ToProto);
3598 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3599 }
3600
3601 // FIXME: If we're merging, make sure that the protocol list is the same.
3602 To->setProtocolList(Protocols.data(), Protocols.size(),
3603 ProtocolLocs.data(), Importer.getToContext());
3604
3605 // Import categories. When the categories themselves are imported, they'll
3606 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003607 for (auto *Cat : From->known_categories())
3608 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003609
Douglas Gregor2aa53772012-01-24 17:42:07 +00003610 // If we have an @implementation, import it as well.
3611 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003612 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3613 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003614 if (!Impl)
3615 return true;
3616
3617 To->setImplementation(Impl);
3618 }
3619
Douglas Gregor2e15c842012-02-01 21:00:38 +00003620 if (shouldForceImportDeclContext(Kind)) {
3621 // Import all of the members of this class.
3622 ImportDeclContext(From, /*ForceImport=*/true);
3623 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003624 return false;
3625}
3626
Douglas Gregor85f3f952015-07-07 03:57:15 +00003627ObjCTypeParamList *
3628ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3629 if (!list)
3630 return nullptr;
3631
3632 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3633 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003634 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3635 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003636 if (!toTypeParam)
3637 return nullptr;
3638
3639 toTypeParams.push_back(toTypeParam);
3640 }
3641
3642 return ObjCTypeParamList::create(Importer.getToContext(),
3643 Importer.Import(list->getLAngleLoc()),
3644 toTypeParams,
3645 Importer.Import(list->getRAngleLoc()));
3646}
3647
Douglas Gregor45635322010-02-16 01:20:57 +00003648Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003649 // If this class has a definition in the translation unit we're coming from,
3650 // but this particular declaration is not that definition, import the
3651 // definition and map to that.
3652 ObjCInterfaceDecl *Definition = D->getDefinition();
3653 if (Definition && Definition != D) {
3654 Decl *ImportedDef = Importer.Import(Definition);
3655 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003656 return nullptr;
3657
Douglas Gregor2aa53772012-01-24 17:42:07 +00003658 return Importer.Imported(D, ImportedDef);
3659 }
3660
Douglas Gregor45635322010-02-16 01:20:57 +00003661 // Import the major distinguishing characteristics of an @interface.
3662 DeclContext *DC, *LexicalDC;
3663 DeclarationName Name;
3664 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003665 NamedDecl *ToD;
3666 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003667 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003668 if (ToD)
3669 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003670
Douglas Gregor2aa53772012-01-24 17:42:07 +00003671 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003672 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003673 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003674 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003675 for (auto *FoundDecl : FoundDecls) {
3676 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003677 continue;
3678
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003679 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003680 break;
3681 }
3682
Douglas Gregor2aa53772012-01-24 17:42:07 +00003683 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003684 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003685 if (!ToIface) {
3686 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3687 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003688 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003689 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003690 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003691 D->isImplicitInterfaceDecl());
3692 ToIface->setLexicalDeclContext(LexicalDC);
3693 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003694 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003695 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003696 // Import the type parameter list after calling Imported, to avoid
3697 // loops when bringing in their DeclContext.
3698 ToIface->setTypeParamList(ImportObjCTypeParamList(
3699 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003700
Douglas Gregor2aa53772012-01-24 17:42:07 +00003701 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003702 return nullptr;
3703
Douglas Gregor98d156a2010-02-17 16:12:00 +00003704 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003705}
3706
Douglas Gregor4da9d682010-12-07 15:32:12 +00003707Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003708 auto *Category = cast_or_null<ObjCCategoryDecl>(
3709 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003710 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003711 return nullptr;
3712
Douglas Gregor4da9d682010-12-07 15:32:12 +00003713 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3714 if (!ToImpl) {
3715 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3716 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003717 return nullptr;
3718
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003719 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003720 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003721 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003722 Category->getClassInterface(),
3723 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003724 Importer.Import(D->getAtStartLoc()),
3725 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003726
3727 DeclContext *LexicalDC = DC;
3728 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3729 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3730 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003731 return nullptr;
3732
Douglas Gregor4da9d682010-12-07 15:32:12 +00003733 ToImpl->setLexicalDeclContext(LexicalDC);
3734 }
3735
Sean Callanan95e74be2011-10-21 02:57:43 +00003736 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003737 Category->setImplementation(ToImpl);
3738 }
3739
3740 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003741 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003742 return ToImpl;
3743}
3744
Douglas Gregorda8025c2010-12-07 01:26:03 +00003745Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3746 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003747 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3748 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003749 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003750 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003751
3752 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003753 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003754 if (D->getSuperClass()) {
3755 Super = cast_or_null<ObjCInterfaceDecl>(
3756 Importer.Import(D->getSuperClass()));
3757 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003758 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003759 }
3760
3761 ObjCImplementationDecl *Impl = Iface->getImplementation();
3762 if (!Impl) {
3763 // We haven't imported an implementation yet. Create a new @implementation
3764 // now.
3765 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3766 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003767 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003768 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003769 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003770 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003771 Importer.Import(D->getIvarLBraceLoc()),
3772 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003773
3774 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3775 DeclContext *LexicalDC
3776 = Importer.ImportContext(D->getLexicalDeclContext());
3777 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003778 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003779 Impl->setLexicalDeclContext(LexicalDC);
3780 }
3781
3782 // Associate the implementation with the class it implements.
3783 Iface->setImplementation(Impl);
3784 Importer.Imported(D, Iface->getImplementation());
3785 } else {
3786 Importer.Imported(D, Iface->getImplementation());
3787
3788 // Verify that the existing @implementation has the same superclass.
3789 if ((Super && !Impl->getSuperClass()) ||
3790 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003791 (Super && Impl->getSuperClass() &&
3792 !declaresSameEntity(Super->getCanonicalDecl(),
3793 Impl->getSuperClass()))) {
3794 Importer.ToDiag(Impl->getLocation(),
3795 diag::err_odr_objc_superclass_inconsistent)
3796 << Iface->getDeclName();
3797 // FIXME: It would be nice to have the location of the superclass
3798 // below.
3799 if (Impl->getSuperClass())
3800 Importer.ToDiag(Impl->getLocation(),
3801 diag::note_odr_objc_superclass)
3802 << Impl->getSuperClass()->getDeclName();
3803 else
3804 Importer.ToDiag(Impl->getLocation(),
3805 diag::note_odr_objc_missing_superclass);
3806 if (D->getSuperClass())
3807 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003808 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003809 << D->getSuperClass()->getDeclName();
3810 else
3811 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003812 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003813 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003814 }
3815 }
3816
3817 // Import all of the members of this @implementation.
3818 ImportDeclContext(D);
3819
3820 return Impl;
3821}
3822
Douglas Gregora11c4582010-02-17 18:02:10 +00003823Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3824 // Import the major distinguishing characteristics of an @property.
3825 DeclContext *DC, *LexicalDC;
3826 DeclarationName Name;
3827 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003828 NamedDecl *ToD;
3829 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003830 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003831 if (ToD)
3832 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003833
3834 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003835 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003836 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003837 for (auto *FoundDecl : FoundDecls) {
3838 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003839 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003840 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003841 FoundProp->getType())) {
3842 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3843 << Name << D->getType() << FoundProp->getType();
3844 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3845 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003846 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003847 }
3848
3849 // FIXME: Check property attributes, getters, setters, etc.?
3850
3851 // Consider these properties to be equivalent.
3852 Importer.Imported(D, FoundProp);
3853 return FoundProp;
3854 }
3855 }
3856
3857 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003858 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3859 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003860 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003861
3862 // Create the new property.
3863 ObjCPropertyDecl *ToProperty
3864 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3865 Name.getAsIdentifierInfo(),
3866 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003867 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003868 Importer.Import(D->getType()),
3869 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003870 D->getPropertyImplementation());
3871 Importer.Imported(D, ToProperty);
3872 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003873 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003874
3875 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003876 ToProperty->setPropertyAttributesAsWritten(
3877 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003878 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3879 Importer.Import(D->getGetterNameLoc()));
3880 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3881 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003882 ToProperty->setGetterMethodDecl(
3883 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3884 ToProperty->setSetterMethodDecl(
3885 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3886 ToProperty->setPropertyIvarDecl(
3887 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3888 return ToProperty;
3889}
3890
Douglas Gregor14a49e22010-12-07 18:32:03 +00003891Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003892 auto *Property = cast_or_null<ObjCPropertyDecl>(
3893 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003894 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003895 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003896
3897 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3898 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003899 return nullptr;
3900
Douglas Gregor14a49e22010-12-07 18:32:03 +00003901 // Import the lexical declaration context.
3902 DeclContext *LexicalDC = DC;
3903 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3904 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3905 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003906 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003907 }
3908
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003909 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003910 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003911 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003912
3913 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003914 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003915 if (D->getPropertyIvarDecl()) {
3916 Ivar = cast_or_null<ObjCIvarDecl>(
3917 Importer.Import(D->getPropertyIvarDecl()));
3918 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003919 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003920 }
3921
3922 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003923 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3924 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003925 if (!ToImpl) {
3926 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3927 Importer.Import(D->getLocStart()),
3928 Importer.Import(D->getLocation()),
3929 Property,
3930 D->getPropertyImplementation(),
3931 Ivar,
3932 Importer.Import(D->getPropertyIvarDeclLoc()));
3933 ToImpl->setLexicalDeclContext(LexicalDC);
3934 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003935 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003936 } else {
3937 // Check that we have the same kind of property implementation (@synthesize
3938 // vs. @dynamic).
3939 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3940 Importer.ToDiag(ToImpl->getLocation(),
3941 diag::err_odr_objc_property_impl_kind_inconsistent)
3942 << Property->getDeclName()
3943 << (ToImpl->getPropertyImplementation()
3944 == ObjCPropertyImplDecl::Dynamic);
3945 Importer.FromDiag(D->getLocation(),
3946 diag::note_odr_objc_property_impl_kind)
3947 << D->getPropertyDecl()->getDeclName()
3948 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003949 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003950 }
3951
3952 // For @synthesize, check that we have the same
3953 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3954 Ivar != ToImpl->getPropertyIvarDecl()) {
3955 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3956 diag::err_odr_objc_synthesize_ivar_inconsistent)
3957 << Property->getDeclName()
3958 << ToImpl->getPropertyIvarDecl()->getDeclName()
3959 << Ivar->getDeclName();
3960 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3961 diag::note_odr_objc_synthesize_ivar_here)
3962 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003963 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003964 }
3965
3966 // Merge the existing implementation with the new implementation.
3967 Importer.Imported(D, ToImpl);
3968 }
3969
3970 return ToImpl;
3971}
3972
Douglas Gregora082a492010-11-30 19:14:50 +00003973Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3974 // For template arguments, we adopt the translation unit as our declaration
3975 // context. This context will be fixed when the actual template declaration
3976 // is created.
3977
3978 // FIXME: Import default argument.
3979 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3980 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003981 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003982 Importer.Import(D->getLocation()),
3983 D->getDepth(),
3984 D->getIndex(),
3985 Importer.Import(D->getIdentifier()),
3986 D->wasDeclaredWithTypename(),
3987 D->isParameterPack());
3988}
3989
3990Decl *
3991ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3992 // Import the name of this declaration.
3993 DeclarationName Name = Importer.Import(D->getDeclName());
3994 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003995 return nullptr;
3996
Douglas Gregora082a492010-11-30 19:14:50 +00003997 // Import the location of this declaration.
3998 SourceLocation Loc = Importer.Import(D->getLocation());
3999
4000 // Import the type of this declaration.
4001 QualType T = Importer.Import(D->getType());
4002 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004003 return nullptr;
4004
Douglas Gregora082a492010-11-30 19:14:50 +00004005 // Import type-source information.
4006 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4007 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004008 return nullptr;
4009
Douglas Gregora082a492010-11-30 19:14:50 +00004010 // FIXME: Import default argument.
4011
4012 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4013 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004014 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004015 Loc, D->getDepth(), D->getPosition(),
4016 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004017 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004018}
4019
4020Decl *
4021ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4022 // Import the name of this declaration.
4023 DeclarationName Name = Importer.Import(D->getDeclName());
4024 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004025 return nullptr;
4026
Douglas Gregora082a492010-11-30 19:14:50 +00004027 // Import the location of this declaration.
4028 SourceLocation Loc = Importer.Import(D->getLocation());
4029
4030 // Import template parameters.
4031 TemplateParameterList *TemplateParams
4032 = ImportTemplateParameterList(D->getTemplateParameters());
4033 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004034 return nullptr;
4035
Douglas Gregora082a492010-11-30 19:14:50 +00004036 // FIXME: Import default argument.
4037
4038 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4039 Importer.getToContext().getTranslationUnitDecl(),
4040 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004041 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004042 Name.getAsIdentifierInfo(),
4043 TemplateParams);
4044}
4045
4046Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4047 // If this record has a definition in the translation unit we're coming from,
4048 // but this particular declaration is not that definition, import the
4049 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004050 auto *Definition =
4051 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004052 if (Definition && Definition != D->getTemplatedDecl()) {
4053 Decl *ImportedDef
4054 = Importer.Import(Definition->getDescribedClassTemplate());
4055 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004056 return nullptr;
4057
Douglas Gregora082a492010-11-30 19:14:50 +00004058 return Importer.Imported(D, ImportedDef);
4059 }
4060
4061 // Import the major distinguishing characteristics of this class template.
4062 DeclContext *DC, *LexicalDC;
4063 DeclarationName Name;
4064 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004065 NamedDecl *ToD;
4066 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004067 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004068 if (ToD)
4069 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004070
Douglas Gregora082a492010-11-30 19:14:50 +00004071 // We may already have a template of the same name; try to find and match it.
4072 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004073 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004074 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004075 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004076 for (auto *FoundDecl : FoundDecls) {
4077 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004078 continue;
4079
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004080 Decl *Found = FoundDecl;
4081 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Douglas Gregora082a492010-11-30 19:14:50 +00004082 if (IsStructuralMatch(D, FoundTemplate)) {
4083 // The class templates structurally match; call it the same template.
4084 // FIXME: We may be filling in a forward declaration here. Handle
4085 // this case!
4086 Importer.Imported(D->getTemplatedDecl(),
4087 FoundTemplate->getTemplatedDecl());
4088 return Importer.Imported(D, FoundTemplate);
4089 }
4090 }
4091
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004092 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004093 }
4094
4095 if (!ConflictingDecls.empty()) {
4096 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4097 ConflictingDecls.data(),
4098 ConflictingDecls.size());
4099 }
4100
4101 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004102 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004103 }
4104
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004105 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4106
Douglas Gregora082a492010-11-30 19:14:50 +00004107 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004108 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4109 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004110 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004111 return nullptr;
4112
4113 // Resolve possible cyclic import.
4114 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4115 return AlreadyImported;
4116
Douglas Gregora082a492010-11-30 19:14:50 +00004117 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004118 TemplateParameterList *TemplateParams =
4119 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004120 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004121 return nullptr;
4122
Douglas Gregora082a492010-11-30 19:14:50 +00004123 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4124 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004125 ToTemplated);
4126 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004127
4128 D2->setAccess(D->getAccess());
4129 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004130 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004131
4132 // Note the relationship between the class templates.
4133 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004134 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004135
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004136 if (FromTemplated->isCompleteDefinition() &&
4137 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004138 // FIXME: Import definition!
4139 }
4140
4141 return D2;
4142}
4143
Douglas Gregore2e50d332010-12-01 01:36:18 +00004144Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4145 ClassTemplateSpecializationDecl *D) {
4146 // If this record has a definition in the translation unit we're coming from,
4147 // but this particular declaration is not that definition, import the
4148 // definition and map to that.
4149 TagDecl *Definition = D->getDefinition();
4150 if (Definition && Definition != D) {
4151 Decl *ImportedDef = Importer.Import(Definition);
4152 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004153 return nullptr;
4154
Douglas Gregore2e50d332010-12-01 01:36:18 +00004155 return Importer.Imported(D, ImportedDef);
4156 }
4157
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004158 auto *ClassTemplate =
4159 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004160 D->getSpecializedTemplate()));
4161 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
4163
Douglas Gregore2e50d332010-12-01 01:36:18 +00004164 // Import the context of this declaration.
4165 DeclContext *DC = ClassTemplate->getDeclContext();
4166 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004167 return nullptr;
4168
Douglas Gregore2e50d332010-12-01 01:36:18 +00004169 DeclContext *LexicalDC = DC;
4170 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4171 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4172 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004173 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004174 }
4175
4176 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004177 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4178 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004179
4180 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004181 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004182 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4183 D->getTemplateArgs().size(),
4184 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004185 return nullptr;
4186
Douglas Gregore2e50d332010-12-01 01:36:18 +00004187 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004188 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004189 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004190 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004191 if (D2) {
4192 // We already have a class template specialization with these template
4193 // arguments.
4194
4195 // FIXME: Check for specialization vs. instantiation errors.
4196
4197 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004198 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004199 // The record types structurally match, or the "from" translation
4200 // unit only had a forward declaration anyway; call it the same
4201 // function.
4202 return Importer.Imported(D, FoundDef);
4203 }
4204 }
4205 } else {
4206 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004207 if (auto *PartialSpec =
4208 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004209 // Import TemplateArgumentListInfo
4210 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004211 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4212 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004213 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004214
4215 QualType CanonInjType = Importer.Import(
4216 PartialSpec->getInjectedSpecializationType());
4217 if (CanonInjType.isNull())
4218 return nullptr;
4219 CanonInjType = CanonInjType.getCanonicalType();
4220
4221 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4222 PartialSpec->getTemplateParameters());
4223 if (!ToTPList && PartialSpec->getTemplateParameters())
4224 return nullptr;
4225
4226 D2 = ClassTemplatePartialSpecializationDecl::Create(
4227 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4228 ToTPList, ClassTemplate,
4229 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4230 ToTAInfo, CanonInjType, nullptr);
4231
4232 } else {
4233 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4234 D->getTagKind(), DC,
4235 StartLoc, IdLoc,
4236 ClassTemplate,
4237 TemplateArgs,
4238 /*PrevDecl=*/nullptr);
4239 }
4240
Douglas Gregore2e50d332010-12-01 01:36:18 +00004241 D2->setSpecializationKind(D->getSpecializationKind());
4242
4243 // Add this specialization to the class template.
4244 ClassTemplate->AddSpecialization(D2, InsertPos);
4245
4246 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004247 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004248
4249 Importer.Imported(D, D2);
4250
4251 if (auto *TSI = D->getTypeAsWritten()) {
4252 TypeSourceInfo *TInfo = Importer.Import(TSI);
4253 if (!TInfo)
4254 return nullptr;
4255 D2->setTypeAsWritten(TInfo);
4256 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4257 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4258 }
4259
4260 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4261 if (POI.isValid())
4262 D2->setPointOfInstantiation(POI);
4263 else if (D->getPointOfInstantiation().isValid())
4264 return nullptr;
4265
4266 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4267
Douglas Gregore2e50d332010-12-01 01:36:18 +00004268 // Add the specialization to this context.
4269 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004270 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004271 }
4272 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004273 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004274 return nullptr;
4275
Douglas Gregore2e50d332010-12-01 01:36:18 +00004276 return D2;
4277}
4278
Larisse Voufo39a1e502013-08-06 01:03:05 +00004279Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4280 // If this variable has a definition in the translation unit we're coming
4281 // from,
4282 // but this particular declaration is not that definition, import the
4283 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004284 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004285 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4286 if (Definition && Definition != D->getTemplatedDecl()) {
4287 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4288 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004289 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004290
4291 return Importer.Imported(D, ImportedDef);
4292 }
4293
4294 // Import the major distinguishing characteristics of this variable template.
4295 DeclContext *DC, *LexicalDC;
4296 DeclarationName Name;
4297 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004298 NamedDecl *ToD;
4299 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004300 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004301 if (ToD)
4302 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004303
4304 // We may already have a template of the same name; try to find and match it.
4305 assert(!DC->isFunctionOrMethod() &&
4306 "Variable templates cannot be declared at function scope");
4307 SmallVector<NamedDecl *, 4> ConflictingDecls;
4308 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004309 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004310 for (auto *FoundDecl : FoundDecls) {
4311 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004312 continue;
4313
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004314 Decl *Found = FoundDecl;
4315 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004316 if (IsStructuralMatch(D, FoundTemplate)) {
4317 // The variable templates structurally match; call it the same template.
4318 Importer.Imported(D->getTemplatedDecl(),
4319 FoundTemplate->getTemplatedDecl());
4320 return Importer.Imported(D, FoundTemplate);
4321 }
4322 }
4323
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004324 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004325 }
4326
4327 if (!ConflictingDecls.empty()) {
4328 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4329 ConflictingDecls.data(),
4330 ConflictingDecls.size());
4331 }
4332
4333 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004334 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004335
4336 VarDecl *DTemplated = D->getTemplatedDecl();
4337
4338 // Import the type.
4339 QualType T = Importer.Import(DTemplated->getType());
4340 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004341 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004342
4343 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004344 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4345 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004346 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004347
4348 // Create the variable template declaration itself.
4349 TemplateParameterList *TemplateParams =
4350 ImportTemplateParameterList(D->getTemplateParameters());
4351 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004352 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004353
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004354 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4355 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4356 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004357
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004358 ToVarTD->setAccess(D->getAccess());
4359 ToVarTD->setLexicalDeclContext(LexicalDC);
4360 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004361
4362 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004363 Importer.Imported(D, ToVarTD);
4364 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004365
4366 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004367 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004368 // FIXME: Import definition!
4369 }
4370
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004371 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004372}
4373
4374Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4375 VarTemplateSpecializationDecl *D) {
4376 // If this record has a definition in the translation unit we're coming from,
4377 // but this particular declaration is not that definition, import the
4378 // definition and map to that.
4379 VarDecl *Definition = D->getDefinition();
4380 if (Definition && Definition != D) {
4381 Decl *ImportedDef = Importer.Import(Definition);
4382 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004383 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004384
4385 return Importer.Imported(D, ImportedDef);
4386 }
4387
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004388 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004389 Importer.Import(D->getSpecializedTemplate()));
4390 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004391 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004392
4393 // Import the context of this declaration.
4394 DeclContext *DC = VarTemplate->getDeclContext();
4395 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004396 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004397
4398 DeclContext *LexicalDC = DC;
4399 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4400 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4401 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004402 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004403 }
4404
4405 // Import the location of this declaration.
4406 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4407 SourceLocation IdLoc = Importer.Import(D->getLocation());
4408
4409 // Import template arguments.
4410 SmallVector<TemplateArgument, 2> TemplateArgs;
4411 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4412 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004413 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004414
4415 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004416 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004417 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004418 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004419 if (D2) {
4420 // We already have a variable template specialization with these template
4421 // arguments.
4422
4423 // FIXME: Check for specialization vs. instantiation errors.
4424
4425 if (VarDecl *FoundDef = D2->getDefinition()) {
4426 if (!D->isThisDeclarationADefinition() ||
4427 IsStructuralMatch(D, FoundDef)) {
4428 // The record types structurally match, or the "from" translation
4429 // unit only had a forward declaration anyway; call it the same
4430 // variable.
4431 return Importer.Imported(D, FoundDef);
4432 }
4433 }
4434 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004435 // Import the type.
4436 QualType T = Importer.Import(D->getType());
4437 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004438 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004439
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004440 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4441 if (D->getTypeSourceInfo() && !TInfo)
4442 return nullptr;
4443
4444 TemplateArgumentListInfo ToTAInfo;
4445 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4446 return nullptr;
4447
4448 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004449 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004450 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4451 // Import TemplateArgumentListInfo
4452 TemplateArgumentListInfo ArgInfos;
4453 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4454 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4455 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4456 return nullptr;
4457
4458 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4459 FromPartial->getTemplateParameters());
4460 if (!ToTPList)
4461 return nullptr;
4462
4463 auto *ToPartial = PartVarSpecDecl::Create(
4464 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4465 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4466
4467 auto *FromInst = FromPartial->getInstantiatedFromMember();
4468 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4469 if (FromInst && !ToInst)
4470 return nullptr;
4471
4472 ToPartial->setInstantiatedFromMember(ToInst);
4473 if (FromPartial->isMemberSpecialization())
4474 ToPartial->setMemberSpecialization();
4475
4476 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004477 } else { // Full specialization
4478 D2 = VarTemplateSpecializationDecl::Create(
4479 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4480 D->getStorageClass(), TemplateArgs);
4481 }
4482
4483 SourceLocation POI = D->getPointOfInstantiation();
4484 if (POI.isValid())
4485 D2->setPointOfInstantiation(Importer.Import(POI));
4486
Larisse Voufo39a1e502013-08-06 01:03:05 +00004487 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004488 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004489
4490 // Add this specialization to the class template.
4491 VarTemplate->AddSpecialization(D2, InsertPos);
4492
4493 // Import the qualifier, if any.
4494 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4495
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004496 if (D->isConstexpr())
4497 D2->setConstexpr(true);
4498
Larisse Voufo39a1e502013-08-06 01:03:05 +00004499 // Add the specialization to this context.
4500 D2->setLexicalDeclContext(LexicalDC);
4501 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004502
4503 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004504 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004505
Larisse Voufo39a1e502013-08-06 01:03:05 +00004506 Importer.Imported(D, D2);
4507
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004508 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4509 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4510 // we have to check the declaration for initializer - otherwise, it won't be
4511 // imported.
4512 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4513 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004514 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004515
4516 return D2;
4517}
4518
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004519Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4520 DeclContext *DC, *LexicalDC;
4521 DeclarationName Name;
4522 SourceLocation Loc;
4523 NamedDecl *ToD;
4524
4525 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4526 return nullptr;
4527
4528 if (ToD)
4529 return ToD;
4530
4531 // Try to find a function in our own ("to") context with the same name, same
4532 // type, and in the same context as the function we're importing.
4533 if (!LexicalDC->isFunctionOrMethod()) {
4534 unsigned IDNS = Decl::IDNS_Ordinary;
4535 SmallVector<NamedDecl *, 2> FoundDecls;
4536 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004537 for (auto *FoundDecl : FoundDecls) {
4538 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004539 continue;
4540
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004541 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004542 if (FoundFunction->hasExternalFormalLinkage() &&
4543 D->hasExternalFormalLinkage()) {
4544 if (IsStructuralMatch(D, FoundFunction)) {
4545 Importer.Imported(D, FoundFunction);
4546 // FIXME: Actually try to merge the body and other attributes.
4547 return FoundFunction;
4548 }
4549 }
4550 }
4551 }
4552 }
4553
4554 TemplateParameterList *Params =
4555 ImportTemplateParameterList(D->getTemplateParameters());
4556 if (!Params)
4557 return nullptr;
4558
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004559 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004560 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4561 if (!TemplatedFD)
4562 return nullptr;
4563
4564 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4565 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4566
4567 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4568 ToFunc->setAccess(D->getAccess());
4569 ToFunc->setLexicalDeclContext(LexicalDC);
4570 Importer.Imported(D, ToFunc);
4571
4572 LexicalDC->addDeclInternal(ToFunc);
4573 return ToFunc;
4574}
4575
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004576//----------------------------------------------------------------------------
4577// Import Statements
4578//----------------------------------------------------------------------------
4579
Sean Callanan59721b32015-04-28 18:41:46 +00004580DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4581 if (DG.isNull())
4582 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4583 size_t NumDecls = DG.end() - DG.begin();
4584 SmallVector<Decl *, 1> ToDecls(NumDecls);
4585 auto &_Importer = this->Importer;
4586 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4587 [&_Importer](Decl *D) -> Decl * {
4588 return _Importer.Import(D);
4589 });
4590 return DeclGroupRef::Create(Importer.getToContext(),
4591 ToDecls.begin(),
4592 NumDecls);
4593}
4594
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004595Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4596 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4597 << S->getStmtClassName();
4598 return nullptr;
4599}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004600
4601Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4602 SmallVector<IdentifierInfo *, 4> Names;
4603 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4604 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004605 // ToII is nullptr when no symbolic name is given for output operand
4606 // see ParseStmtAsm::ParseAsmOperandsOpt
4607 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004608 return nullptr;
4609 Names.push_back(ToII);
4610 }
4611 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4612 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004613 // ToII is nullptr when no symbolic name is given for input operand
4614 // see ParseStmtAsm::ParseAsmOperandsOpt
4615 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004616 return nullptr;
4617 Names.push_back(ToII);
4618 }
4619
4620 SmallVector<StringLiteral *, 4> Clobbers;
4621 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004622 auto *Clobber = cast_or_null<StringLiteral>(
4623 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004624 if (!Clobber)
4625 return nullptr;
4626 Clobbers.push_back(Clobber);
4627 }
4628
4629 SmallVector<StringLiteral *, 4> Constraints;
4630 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004631 auto *Output = cast_or_null<StringLiteral>(
4632 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004633 if (!Output)
4634 return nullptr;
4635 Constraints.push_back(Output);
4636 }
4637
4638 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004639 auto *Input = cast_or_null<StringLiteral>(
4640 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004641 if (!Input)
4642 return nullptr;
4643 Constraints.push_back(Input);
4644 }
4645
4646 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004647 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004648 return nullptr;
4649
Aleksei Sidorina693b372016-09-28 10:16:56 +00004650 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004651 return nullptr;
4652
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004653 auto *AsmStr = cast_or_null<StringLiteral>(
4654 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004655 if (!AsmStr)
4656 return nullptr;
4657
4658 return new (Importer.getToContext()) GCCAsmStmt(
4659 Importer.getToContext(),
4660 Importer.Import(S->getAsmLoc()),
4661 S->isSimple(),
4662 S->isVolatile(),
4663 S->getNumOutputs(),
4664 S->getNumInputs(),
4665 Names.data(),
4666 Constraints.data(),
4667 Exprs.data(),
4668 AsmStr,
4669 S->getNumClobbers(),
4670 Clobbers.data(),
4671 Importer.Import(S->getRParenLoc()));
4672}
4673
Sean Callanan59721b32015-04-28 18:41:46 +00004674Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4675 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004676 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004677 if (!ToD)
4678 return nullptr;
4679 }
4680 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4681 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4682 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4683}
4684
4685Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4686 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4687 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4688 S->hasLeadingEmptyMacro());
4689}
4690
4691Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004692 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004693
4694 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004695 return nullptr;
4696
Sean Callanan59721b32015-04-28 18:41:46 +00004697 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4698 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004699 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4700 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004701}
4702
4703Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4704 Expr *ToLHS = Importer.Import(S->getLHS());
4705 if (!ToLHS)
4706 return nullptr;
4707 Expr *ToRHS = Importer.Import(S->getRHS());
4708 if (!ToRHS && S->getRHS())
4709 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004710 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4711 if (!ToSubStmt && S->getSubStmt())
4712 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004713 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4714 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4715 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004716 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004717 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4718 ToStmt->setSubStmt(ToSubStmt);
4719 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004720}
4721
4722Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4723 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4724 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4725 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4726 if (!ToSubStmt && S->getSubStmt())
4727 return nullptr;
4728 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4729 ToSubStmt);
4730}
4731
4732Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4733 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004734 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004735 if (!ToLabelDecl && S->getDecl())
4736 return nullptr;
4737 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4738 if (!ToSubStmt && S->getSubStmt())
4739 return nullptr;
4740 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4741 ToSubStmt);
4742}
4743
4744Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4745 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4746 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4747 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4748 ASTContext &_ToContext = Importer.getToContext();
4749 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4750 [&_ToContext](const Attr *A) -> const Attr * {
4751 return A->clone(_ToContext);
4752 });
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004753 for (const auto *ToA : ToAttrs) {
Sean Callanan59721b32015-04-28 18:41:46 +00004754 if (!ToA)
4755 return nullptr;
4756 }
4757 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4758 if (!ToSubStmt && S->getSubStmt())
4759 return nullptr;
4760 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4761 ToAttrs, ToSubStmt);
4762}
4763
4764Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4765 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004766 Stmt *ToInit = Importer.Import(S->getInit());
4767 if (!ToInit && S->getInit())
4768 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004769 VarDecl *ToConditionVariable = nullptr;
4770 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4771 ToConditionVariable =
4772 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4773 if (!ToConditionVariable)
4774 return nullptr;
4775 }
4776 Expr *ToCondition = Importer.Import(S->getCond());
4777 if (!ToCondition && S->getCond())
4778 return nullptr;
4779 Stmt *ToThenStmt = Importer.Import(S->getThen());
4780 if (!ToThenStmt && S->getThen())
4781 return nullptr;
4782 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4783 Stmt *ToElseStmt = Importer.Import(S->getElse());
4784 if (!ToElseStmt && S->getElse())
4785 return nullptr;
4786 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004787 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004788 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004789 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004790 ToCondition, ToThenStmt,
4791 ToElseLoc, ToElseStmt);
4792}
4793
4794Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004795 Stmt *ToInit = Importer.Import(S->getInit());
4796 if (!ToInit && S->getInit())
4797 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004798 VarDecl *ToConditionVariable = nullptr;
4799 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4800 ToConditionVariable =
4801 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4802 if (!ToConditionVariable)
4803 return nullptr;
4804 }
4805 Expr *ToCondition = Importer.Import(S->getCond());
4806 if (!ToCondition && S->getCond())
4807 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004808 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004809 Importer.getToContext(), ToInit,
4810 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004811 Stmt *ToBody = Importer.Import(S->getBody());
4812 if (!ToBody && S->getBody())
4813 return nullptr;
4814 ToStmt->setBody(ToBody);
4815 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4816 // Now we have to re-chain the cases.
4817 SwitchCase *LastChainedSwitchCase = nullptr;
4818 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4819 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004820 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004821 if (!ToSC)
4822 return nullptr;
4823 if (LastChainedSwitchCase)
4824 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4825 else
4826 ToStmt->setSwitchCaseList(ToSC);
4827 LastChainedSwitchCase = ToSC;
4828 }
4829 return ToStmt;
4830}
4831
4832Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4833 VarDecl *ToConditionVariable = nullptr;
4834 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4835 ToConditionVariable =
4836 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4837 if (!ToConditionVariable)
4838 return nullptr;
4839 }
4840 Expr *ToCondition = Importer.Import(S->getCond());
4841 if (!ToCondition && S->getCond())
4842 return nullptr;
4843 Stmt *ToBody = Importer.Import(S->getBody());
4844 if (!ToBody && S->getBody())
4845 return nullptr;
4846 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4847 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4848 ToConditionVariable,
4849 ToCondition, ToBody,
4850 ToWhileLoc);
4851}
4852
4853Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4854 Stmt *ToBody = Importer.Import(S->getBody());
4855 if (!ToBody && S->getBody())
4856 return nullptr;
4857 Expr *ToCondition = Importer.Import(S->getCond());
4858 if (!ToCondition && S->getCond())
4859 return nullptr;
4860 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4861 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4862 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4863 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4864 ToDoLoc, ToWhileLoc,
4865 ToRParenLoc);
4866}
4867
4868Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4869 Stmt *ToInit = Importer.Import(S->getInit());
4870 if (!ToInit && S->getInit())
4871 return nullptr;
4872 Expr *ToCondition = Importer.Import(S->getCond());
4873 if (!ToCondition && S->getCond())
4874 return nullptr;
4875 VarDecl *ToConditionVariable = nullptr;
4876 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4877 ToConditionVariable =
4878 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4879 if (!ToConditionVariable)
4880 return nullptr;
4881 }
4882 Expr *ToInc = Importer.Import(S->getInc());
4883 if (!ToInc && S->getInc())
4884 return nullptr;
4885 Stmt *ToBody = Importer.Import(S->getBody());
4886 if (!ToBody && S->getBody())
4887 return nullptr;
4888 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4889 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4890 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4891 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4892 ToInit, ToCondition,
4893 ToConditionVariable,
4894 ToInc, ToBody,
4895 ToForLoc, ToLParenLoc,
4896 ToRParenLoc);
4897}
4898
4899Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4900 LabelDecl *ToLabel = nullptr;
4901 if (LabelDecl *FromLabel = S->getLabel()) {
4902 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4903 if (!ToLabel)
4904 return nullptr;
4905 }
4906 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4907 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4908 return new (Importer.getToContext()) GotoStmt(ToLabel,
4909 ToGotoLoc, ToLabelLoc);
4910}
4911
4912Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4913 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4914 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4915 Expr *ToTarget = Importer.Import(S->getTarget());
4916 if (!ToTarget && S->getTarget())
4917 return nullptr;
4918 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4919 ToTarget);
4920}
4921
4922Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4923 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4924 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4925}
4926
4927Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4928 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4929 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4930}
4931
4932Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4933 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4934 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4935 if (!ToRetExpr && S->getRetValue())
4936 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004937 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4938 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004939 if (!ToNRVOCandidate && NRVOCandidate)
4940 return nullptr;
4941 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4942 ToNRVOCandidate);
4943}
4944
4945Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4946 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4947 VarDecl *ToExceptionDecl = nullptr;
4948 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4949 ToExceptionDecl =
4950 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4951 if (!ToExceptionDecl)
4952 return nullptr;
4953 }
4954 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4955 if (!ToHandlerBlock && S->getHandlerBlock())
4956 return nullptr;
4957 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4958 ToExceptionDecl,
4959 ToHandlerBlock);
4960}
4961
4962Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4963 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4964 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4965 if (!ToTryBlock && S->getTryBlock())
4966 return nullptr;
4967 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4968 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4969 CXXCatchStmt *FromHandler = S->getHandler(HI);
4970 if (Stmt *ToHandler = Importer.Import(FromHandler))
4971 ToHandlers[HI] = ToHandler;
4972 else
4973 return nullptr;
4974 }
4975 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4976 ToHandlers);
4977}
4978
4979Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004980 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00004981 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4982 if (!ToRange && S->getRangeStmt())
4983 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004984 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00004985 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4986 if (!ToBegin && S->getBeginStmt())
4987 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004988 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00004989 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4990 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004991 return nullptr;
4992 Expr *ToCond = Importer.Import(S->getCond());
4993 if (!ToCond && S->getCond())
4994 return nullptr;
4995 Expr *ToInc = Importer.Import(S->getInc());
4996 if (!ToInc && S->getInc())
4997 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004998 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00004999 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5000 if (!ToLoopVar && S->getLoopVarStmt())
5001 return nullptr;
5002 Stmt *ToBody = Importer.Import(S->getBody());
5003 if (!ToBody && S->getBody())
5004 return nullptr;
5005 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005006 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005007 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5008 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005009 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005010 ToCond, ToInc,
5011 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005012 ToForLoc, ToCoawaitLoc,
5013 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005014}
5015
5016Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5017 Stmt *ToElem = Importer.Import(S->getElement());
5018 if (!ToElem && S->getElement())
5019 return nullptr;
5020 Expr *ToCollect = Importer.Import(S->getCollection());
5021 if (!ToCollect && S->getCollection())
5022 return nullptr;
5023 Stmt *ToBody = Importer.Import(S->getBody());
5024 if (!ToBody && S->getBody())
5025 return nullptr;
5026 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5027 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5028 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5029 ToCollect,
5030 ToBody, ToForLoc,
5031 ToRParenLoc);
5032}
5033
5034Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5035 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5036 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5037 VarDecl *ToExceptionDecl = nullptr;
5038 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5039 ToExceptionDecl =
5040 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5041 if (!ToExceptionDecl)
5042 return nullptr;
5043 }
5044 Stmt *ToBody = Importer.Import(S->getCatchBody());
5045 if (!ToBody && S->getCatchBody())
5046 return nullptr;
5047 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5048 ToRParenLoc,
5049 ToExceptionDecl,
5050 ToBody);
5051}
5052
5053Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5054 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5055 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5056 if (!ToAtFinallyStmt && S->getFinallyBody())
5057 return nullptr;
5058 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5059 ToAtFinallyStmt);
5060}
5061
5062Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5063 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5064 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5065 if (!ToAtTryStmt && S->getTryBody())
5066 return nullptr;
5067 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5068 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5069 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5070 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5071 ToCatchStmts[CI] = ToCatchStmt;
5072 else
5073 return nullptr;
5074 }
5075 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5076 if (!ToAtFinallyStmt && S->getFinallyStmt())
5077 return nullptr;
5078 return ObjCAtTryStmt::Create(Importer.getToContext(),
5079 ToAtTryLoc, ToAtTryStmt,
5080 ToCatchStmts.begin(), ToCatchStmts.size(),
5081 ToAtFinallyStmt);
5082}
5083
5084Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5085 (ObjCAtSynchronizedStmt *S) {
5086 SourceLocation ToAtSynchronizedLoc =
5087 Importer.Import(S->getAtSynchronizedLoc());
5088 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5089 if (!ToSynchExpr && S->getSynchExpr())
5090 return nullptr;
5091 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5092 if (!ToSynchBody && S->getSynchBody())
5093 return nullptr;
5094 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5095 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5096}
5097
5098Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5099 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5100 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5101 if (!ToThrow && S->getThrowExpr())
5102 return nullptr;
5103 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5104}
5105
5106Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5107 (ObjCAutoreleasePoolStmt *S) {
5108 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5109 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5110 if (!ToSubStmt && S->getSubStmt())
5111 return nullptr;
5112 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5113 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005114}
5115
5116//----------------------------------------------------------------------------
5117// Import Expressions
5118//----------------------------------------------------------------------------
5119Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5120 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5121 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005122 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005123}
5124
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005125Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5126 QualType T = Importer.Import(E->getType());
5127 if (T.isNull())
5128 return nullptr;
5129
5130 Expr *SubExpr = Importer.Import(E->getSubExpr());
5131 if (!SubExpr && E->getSubExpr())
5132 return nullptr;
5133
5134 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5135 if (!TInfo)
5136 return nullptr;
5137
5138 return new (Importer.getToContext()) VAArgExpr(
5139 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5140 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5141}
5142
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005143Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5144 QualType T = Importer.Import(E->getType());
5145 if (T.isNull())
5146 return nullptr;
5147
5148 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005149 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005150}
5151
5152Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5153 QualType T = Importer.Import(E->getType());
5154 if (T.isNull())
5155 return nullptr;
5156
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005157 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005158 if (!SL && E->getFunctionName())
5159 return nullptr;
5160
5161 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005162 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005163}
5164
Douglas Gregor52f820e2010-02-19 01:17:02 +00005165Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005166 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005167 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005168 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005169
Craig Topper36250ad2014-05-12 05:36:57 +00005170 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005171 if (E->getDecl() != E->getFoundDecl()) {
5172 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5173 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005174 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005175 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005176
5177 QualType T = Importer.Import(E->getType());
5178 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005179 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005180
Aleksei Sidorina693b372016-09-28 10:16:56 +00005181 TemplateArgumentListInfo ToTAInfo;
5182 TemplateArgumentListInfo *ResInfo = nullptr;
5183 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005184 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5185 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005186 ResInfo = &ToTAInfo;
5187 }
5188
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005189 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5190 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005191 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005192 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005193 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005194 Importer.Import(E->getLocation()),
5195 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005196 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005197 if (E->hadMultipleCandidates())
5198 DRE->setHadMultipleCandidates(true);
5199 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005200}
5201
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005202Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5203 QualType T = Importer.Import(E->getType());
5204 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005205 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005206
5207 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5208}
5209
5210ASTNodeImporter::Designator
5211ASTNodeImporter::ImportDesignator(const Designator &D) {
5212 if (D.isFieldDesignator()) {
5213 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5214 // Caller checks for import error
5215 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5216 Importer.Import(D.getFieldLoc()));
5217 }
5218 if (D.isArrayDesignator())
5219 return Designator(D.getFirstExprIndex(),
5220 Importer.Import(D.getLBracketLoc()),
5221 Importer.Import(D.getRBracketLoc()));
5222
5223 assert(D.isArrayRangeDesignator());
5224 return Designator(D.getFirstExprIndex(),
5225 Importer.Import(D.getLBracketLoc()),
5226 Importer.Import(D.getEllipsisLoc()),
5227 Importer.Import(D.getRBracketLoc()));
5228}
5229
5230
5231Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005232 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005233 if (!Init)
5234 return nullptr;
5235
5236 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5237 // List elements from the second, the first is Init itself
5238 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005239 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005240 IndexExprs[I - 1] = Arg;
5241 else
5242 return nullptr;
5243 }
5244
5245 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005246 llvm::transform(DIE->designators(), Designators.begin(),
5247 [this](const Designator &D) -> Designator {
5248 return ImportDesignator(D);
5249 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005250
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005251 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005252 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005253 return nullptr;
5254
5255 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005256 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005257 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5258 DIE->usesGNUSyntax(), Init);
5259}
5260
5261Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5262 QualType T = Importer.Import(E->getType());
5263 if (T.isNull())
5264 return nullptr;
5265
5266 return new (Importer.getToContext())
5267 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5268}
5269
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005270Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5271 QualType T = Importer.Import(E->getType());
5272 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005273 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005274
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005275 return IntegerLiteral::Create(Importer.getToContext(),
5276 E->getValue(), T,
5277 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005278}
5279
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005280Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5281 QualType T = Importer.Import(E->getType());
5282 if (T.isNull())
5283 return nullptr;
5284
5285 return FloatingLiteral::Create(Importer.getToContext(),
5286 E->getValue(), E->isExact(), T,
5287 Importer.Import(E->getLocation()));
5288}
5289
Douglas Gregor623421d2010-02-18 02:21:22 +00005290Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5291 QualType T = Importer.Import(E->getType());
5292 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005293 return nullptr;
5294
Douglas Gregorfb65e592011-07-27 05:40:30 +00005295 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5296 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005297 Importer.Import(E->getLocation()));
5298}
5299
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005300Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5301 QualType T = Importer.Import(E->getType());
5302 if (T.isNull())
5303 return nullptr;
5304
5305 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5306 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5307
5308 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5309 E->getKind(), E->isPascal(), T,
5310 Locations.data(), Locations.size());
5311}
5312
5313Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5314 QualType T = Importer.Import(E->getType());
5315 if (T.isNull())
5316 return nullptr;
5317
5318 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5319 if (!TInfo)
5320 return nullptr;
5321
5322 Expr *Init = Importer.Import(E->getInitializer());
5323 if (!Init)
5324 return nullptr;
5325
5326 return new (Importer.getToContext()) CompoundLiteralExpr(
5327 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5328 Init, E->isFileScope());
5329}
5330
5331Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5332 QualType T = Importer.Import(E->getType());
5333 if (T.isNull())
5334 return nullptr;
5335
5336 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5337 if (ImportArrayChecked(
5338 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5339 Exprs.begin()))
5340 return nullptr;
5341
5342 return new (Importer.getToContext()) AtomicExpr(
5343 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5344 Importer.Import(E->getRParenLoc()));
5345}
5346
5347Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5348 QualType T = Importer.Import(E->getType());
5349 if (T.isNull())
5350 return nullptr;
5351
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005352 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005353 if (!ToLabel)
5354 return nullptr;
5355
5356 return new (Importer.getToContext()) AddrLabelExpr(
5357 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5358 ToLabel, T);
5359}
5360
Douglas Gregorc74247e2010-02-19 01:07:06 +00005361Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5362 Expr *SubExpr = Importer.Import(E->getSubExpr());
5363 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005364 return nullptr;
5365
Douglas Gregorc74247e2010-02-19 01:07:06 +00005366 return new (Importer.getToContext())
5367 ParenExpr(Importer.Import(E->getLParen()),
5368 Importer.Import(E->getRParen()),
5369 SubExpr);
5370}
5371
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005372Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5373 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005374 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005375 return nullptr;
5376
5377 return new (Importer.getToContext()) ParenListExpr(
5378 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5379 Exprs, Importer.Import(E->getLParenLoc()));
5380}
5381
5382Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5383 QualType T = Importer.Import(E->getType());
5384 if (T.isNull())
5385 return nullptr;
5386
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005387 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5388 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005389 if (!ToSubStmt && E->getSubStmt())
5390 return nullptr;
5391
5392 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5393 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5394}
5395
Douglas Gregorc74247e2010-02-19 01:07:06 +00005396Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5397 QualType T = Importer.Import(E->getType());
5398 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005399 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005400
5401 Expr *SubExpr = Importer.Import(E->getSubExpr());
5402 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005403 return nullptr;
5404
Aaron Ballmana5038552018-01-09 13:07:03 +00005405 return new (Importer.getToContext()) UnaryOperator(
5406 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5407 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005408}
5409
Aaron Ballmana5038552018-01-09 13:07:03 +00005410Expr *
5411ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005412 QualType ResultType = Importer.Import(E->getType());
5413
5414 if (E->isArgumentType()) {
5415 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5416 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005417 return nullptr;
5418
Peter Collingbournee190dee2011-03-11 19:24:49 +00005419 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5420 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005421 Importer.Import(E->getOperatorLoc()),
5422 Importer.Import(E->getRParenLoc()));
5423 }
5424
5425 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5426 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005427 return nullptr;
5428
Peter Collingbournee190dee2011-03-11 19:24:49 +00005429 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5430 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005431 Importer.Import(E->getOperatorLoc()),
5432 Importer.Import(E->getRParenLoc()));
5433}
5434
Douglas Gregorc74247e2010-02-19 01:07:06 +00005435Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5436 QualType T = Importer.Import(E->getType());
5437 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005438 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005439
5440 Expr *LHS = Importer.Import(E->getLHS());
5441 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005442 return nullptr;
5443
Douglas Gregorc74247e2010-02-19 01:07:06 +00005444 Expr *RHS = Importer.Import(E->getRHS());
5445 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005446 return nullptr;
5447
Douglas Gregorc74247e2010-02-19 01:07:06 +00005448 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005449 T, E->getValueKind(),
5450 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005451 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005452 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005453}
5454
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005455Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5456 QualType T = Importer.Import(E->getType());
5457 if (T.isNull())
5458 return nullptr;
5459
5460 Expr *ToLHS = Importer.Import(E->getLHS());
5461 if (!ToLHS)
5462 return nullptr;
5463
5464 Expr *ToRHS = Importer.Import(E->getRHS());
5465 if (!ToRHS)
5466 return nullptr;
5467
5468 Expr *ToCond = Importer.Import(E->getCond());
5469 if (!ToCond)
5470 return nullptr;
5471
5472 return new (Importer.getToContext()) ConditionalOperator(
5473 ToCond, Importer.Import(E->getQuestionLoc()),
5474 ToLHS, Importer.Import(E->getColonLoc()),
5475 ToRHS, T, E->getValueKind(), E->getObjectKind());
5476}
5477
5478Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5479 BinaryConditionalOperator *E) {
5480 QualType T = Importer.Import(E->getType());
5481 if (T.isNull())
5482 return nullptr;
5483
5484 Expr *Common = Importer.Import(E->getCommon());
5485 if (!Common)
5486 return nullptr;
5487
5488 Expr *Cond = Importer.Import(E->getCond());
5489 if (!Cond)
5490 return nullptr;
5491
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005492 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5493 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005494 if (!OpaqueValue)
5495 return nullptr;
5496
5497 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5498 if (!TrueExpr)
5499 return nullptr;
5500
5501 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5502 if (!FalseExpr)
5503 return nullptr;
5504
5505 return new (Importer.getToContext()) BinaryConditionalOperator(
5506 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5507 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5508 T, E->getValueKind(), E->getObjectKind());
5509}
5510
Aleksei Sidorina693b372016-09-28 10:16:56 +00005511Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5512 QualType T = Importer.Import(E->getType());
5513 if (T.isNull())
5514 return nullptr;
5515
5516 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5517 if (!ToQueried)
5518 return nullptr;
5519
5520 Expr *Dim = Importer.Import(E->getDimensionExpression());
5521 if (!Dim && E->getDimensionExpression())
5522 return nullptr;
5523
5524 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5525 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5526 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5527}
5528
5529Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5530 QualType T = Importer.Import(E->getType());
5531 if (T.isNull())
5532 return nullptr;
5533
5534 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5535 if (!ToQueried)
5536 return nullptr;
5537
5538 return new (Importer.getToContext()) ExpressionTraitExpr(
5539 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5540 E->getValue(), Importer.Import(E->getLocEnd()), T);
5541}
5542
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005543Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5544 QualType T = Importer.Import(E->getType());
5545 if (T.isNull())
5546 return nullptr;
5547
5548 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5549 if (!SourceExpr && E->getSourceExpr())
5550 return nullptr;
5551
5552 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005553 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005554 E->getObjectKind(), SourceExpr);
5555}
5556
Aleksei Sidorina693b372016-09-28 10:16:56 +00005557Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5558 QualType T = Importer.Import(E->getType());
5559 if (T.isNull())
5560 return nullptr;
5561
5562 Expr *ToLHS = Importer.Import(E->getLHS());
5563 if (!ToLHS)
5564 return nullptr;
5565
5566 Expr *ToRHS = Importer.Import(E->getRHS());
5567 if (!ToRHS)
5568 return nullptr;
5569
5570 return new (Importer.getToContext()) ArraySubscriptExpr(
5571 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5572 Importer.Import(E->getRBracketLoc()));
5573}
5574
Douglas Gregorc74247e2010-02-19 01:07:06 +00005575Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5576 QualType T = Importer.Import(E->getType());
5577 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005578 return nullptr;
5579
Douglas Gregorc74247e2010-02-19 01:07:06 +00005580 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5581 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005582 return nullptr;
5583
Douglas Gregorc74247e2010-02-19 01:07:06 +00005584 QualType CompResultType = Importer.Import(E->getComputationResultType());
5585 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005586 return nullptr;
5587
Douglas Gregorc74247e2010-02-19 01:07:06 +00005588 Expr *LHS = Importer.Import(E->getLHS());
5589 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005590 return nullptr;
5591
Douglas Gregorc74247e2010-02-19 01:07:06 +00005592 Expr *RHS = Importer.Import(E->getRHS());
5593 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005594 return nullptr;
5595
Douglas Gregorc74247e2010-02-19 01:07:06 +00005596 return new (Importer.getToContext())
5597 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005598 T, E->getValueKind(),
5599 E->getObjectKind(),
5600 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005601 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005602 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005603}
5604
Aleksei Sidorina693b372016-09-28 10:16:56 +00005605bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5606 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5607 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5608 Path.push_back(Spec);
5609 else
5610 return true;
5611 }
5612 return false;
John McCallcf142162010-08-07 06:22:56 +00005613}
5614
Douglas Gregor98c10182010-02-12 22:17:39 +00005615Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5616 QualType T = Importer.Import(E->getType());
5617 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005618 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005619
5620 Expr *SubExpr = Importer.Import(E->getSubExpr());
5621 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005622 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005623
5624 CXXCastPath BasePath;
5625 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005626 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005627
5628 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005629 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005630}
5631
Aleksei Sidorina693b372016-09-28 10:16:56 +00005632Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005633 QualType T = Importer.Import(E->getType());
5634 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005635 return nullptr;
5636
Douglas Gregor5481d322010-02-19 01:32:14 +00005637 Expr *SubExpr = Importer.Import(E->getSubExpr());
5638 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005639 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005640
5641 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5642 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005643 return nullptr;
5644
John McCallcf142162010-08-07 06:22:56 +00005645 CXXCastPath BasePath;
5646 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005647 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005648
Aleksei Sidorina693b372016-09-28 10:16:56 +00005649 switch (E->getStmtClass()) {
5650 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005651 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005652 return CStyleCastExpr::Create(Importer.getToContext(), T,
5653 E->getValueKind(), E->getCastKind(),
5654 SubExpr, &BasePath, TInfo,
5655 Importer.Import(CCE->getLParenLoc()),
5656 Importer.Import(CCE->getRParenLoc()));
5657 }
5658
5659 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005660 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005661 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5662 E->getValueKind(), TInfo,
5663 E->getCastKind(), SubExpr, &BasePath,
5664 Importer.Import(FCE->getLParenLoc()),
5665 Importer.Import(FCE->getRParenLoc()));
5666 }
5667
5668 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005669 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005670 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5671 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5672 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5673 TInfo, SubExpr);
5674 }
5675 default:
5676 break; // just fall through
5677 }
5678
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005679 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005680 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5681 RParenLoc = Importer.Import(Named->getRParenLoc());
5682 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5683
5684 switch (E->getStmtClass()) {
5685 case Stmt::CXXStaticCastExprClass:
5686 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5687 E->getValueKind(), E->getCastKind(),
5688 SubExpr, &BasePath, TInfo,
5689 ExprLoc, RParenLoc, Brackets);
5690
5691 case Stmt::CXXDynamicCastExprClass:
5692 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5693 E->getValueKind(), E->getCastKind(),
5694 SubExpr, &BasePath, TInfo,
5695 ExprLoc, RParenLoc, Brackets);
5696
5697 case Stmt::CXXReinterpretCastExprClass:
5698 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5699 E->getValueKind(), E->getCastKind(),
5700 SubExpr, &BasePath, TInfo,
5701 ExprLoc, RParenLoc, Brackets);
5702
5703 case Stmt::CXXConstCastExprClass:
5704 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5705 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5706 RParenLoc, Brackets);
5707 default:
5708 llvm_unreachable("Cast expression of unsupported type!");
5709 return nullptr;
5710 }
5711}
5712
5713Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5714 QualType T = Importer.Import(OE->getType());
5715 if (T.isNull())
5716 return nullptr;
5717
5718 SmallVector<OffsetOfNode, 4> Nodes;
5719 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5720 const OffsetOfNode &Node = OE->getComponent(I);
5721
5722 switch (Node.getKind()) {
5723 case OffsetOfNode::Array:
5724 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5725 Node.getArrayExprIndex(),
5726 Importer.Import(Node.getLocEnd())));
5727 break;
5728
5729 case OffsetOfNode::Base: {
5730 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5731 if (!BS && Node.getBase())
5732 return nullptr;
5733 Nodes.push_back(OffsetOfNode(BS));
5734 break;
5735 }
5736 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005737 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005738 if (!FD)
5739 return nullptr;
5740 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5741 Importer.Import(Node.getLocEnd())));
5742 break;
5743 }
5744 case OffsetOfNode::Identifier: {
5745 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5746 if (!ToII)
5747 return nullptr;
5748 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5749 Importer.Import(Node.getLocEnd())));
5750 break;
5751 }
5752 }
5753 }
5754
5755 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5756 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5757 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5758 if (!ToIndexExpr)
5759 return nullptr;
5760 Exprs[I] = ToIndexExpr;
5761 }
5762
5763 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5764 if (!TInfo && OE->getTypeSourceInfo())
5765 return nullptr;
5766
5767 return OffsetOfExpr::Create(Importer.getToContext(), T,
5768 Importer.Import(OE->getOperatorLoc()),
5769 TInfo, Nodes, Exprs,
5770 Importer.Import(OE->getRParenLoc()));
5771}
5772
5773Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5774 QualType T = Importer.Import(E->getType());
5775 if (T.isNull())
5776 return nullptr;
5777
5778 Expr *Operand = Importer.Import(E->getOperand());
5779 if (!Operand)
5780 return nullptr;
5781
5782 CanThrowResult CanThrow;
5783 if (E->isValueDependent())
5784 CanThrow = CT_Dependent;
5785 else
5786 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5787
5788 return new (Importer.getToContext()) CXXNoexceptExpr(
5789 T, Operand, CanThrow,
5790 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5791}
5792
5793Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5794 QualType T = Importer.Import(E->getType());
5795 if (T.isNull())
5796 return nullptr;
5797
5798 Expr *SubExpr = Importer.Import(E->getSubExpr());
5799 if (!SubExpr && E->getSubExpr())
5800 return nullptr;
5801
5802 return new (Importer.getToContext()) CXXThrowExpr(
5803 SubExpr, T, Importer.Import(E->getThrowLoc()),
5804 E->isThrownVariableInScope());
5805}
5806
5807Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005808 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005809 if (!Param)
5810 return nullptr;
5811
5812 return CXXDefaultArgExpr::Create(
5813 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5814}
5815
5816Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5817 QualType T = Importer.Import(E->getType());
5818 if (T.isNull())
5819 return nullptr;
5820
5821 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5822 if (!TypeInfo)
5823 return nullptr;
5824
5825 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5826 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5827}
5828
5829Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5830 Expr *SubExpr = Importer.Import(E->getSubExpr());
5831 if (!SubExpr)
5832 return nullptr;
5833
5834 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5835 Importer.Import(const_cast<CXXDestructorDecl *>(
5836 E->getTemporary()->getDestructor())));
5837 if (!Dtor)
5838 return nullptr;
5839
5840 ASTContext &ToCtx = Importer.getToContext();
5841 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5842 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5843}
5844
5845Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5846 QualType T = Importer.Import(CE->getType());
5847 if (T.isNull())
5848 return nullptr;
5849
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005850 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5851 if (!TInfo)
5852 return nullptr;
5853
Aleksei Sidorina693b372016-09-28 10:16:56 +00005854 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5855 if (ImportContainerChecked(CE->arguments(), Args))
5856 return nullptr;
5857
5858 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5859 Importer.Import(CE->getConstructor()));
5860 if (!Ctor)
5861 return nullptr;
5862
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005863 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5864 Importer.getToContext(), Ctor, T, TInfo, Args,
5865 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5866 CE->isListInitialization(), CE->isStdInitListInitialization(),
5867 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005868}
5869
5870Expr *
5871ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5872 QualType T = Importer.Import(E->getType());
5873 if (T.isNull())
5874 return nullptr;
5875
5876 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5877 if (!TempE)
5878 return nullptr;
5879
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005880 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005881 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5882 if (!ExtendedBy && E->getExtendingDecl())
5883 return nullptr;
5884
5885 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5886 T, TempE, E->isBoundToLvalueReference());
5887
5888 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5889 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5890 return ToMTE;
5891}
5892
Gabor Horvath7a91c082017-11-14 11:30:38 +00005893Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5894 QualType T = Importer.Import(E->getType());
5895 if (T.isNull())
5896 return nullptr;
5897
5898 Expr *Pattern = Importer.Import(E->getPattern());
5899 if (!Pattern)
5900 return nullptr;
5901
5902 return new (Importer.getToContext()) PackExpansionExpr(
5903 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5904 E->getNumExpansions());
5905}
5906
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005907Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5908 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5909 if (!Pack)
5910 return nullptr;
5911
5912 Optional<unsigned> Length;
5913
5914 if (!E->isValueDependent())
5915 Length = E->getPackLength();
5916
5917 SmallVector<TemplateArgument, 8> PartialArguments;
5918 if (E->isPartiallySubstituted()) {
5919 if (ImportTemplateArguments(E->getPartialArguments().data(),
5920 E->getPartialArguments().size(),
5921 PartialArguments))
5922 return nullptr;
5923 }
5924
5925 return SizeOfPackExpr::Create(
5926 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5927 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5928 Length, PartialArguments);
5929}
5930
Aleksei Sidorina693b372016-09-28 10:16:56 +00005931Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5932 QualType T = Importer.Import(CE->getType());
5933 if (T.isNull())
5934 return nullptr;
5935
5936 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5937 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5938 return nullptr;
5939
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005940 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005941 Importer.Import(CE->getOperatorNew()));
5942 if (!OperatorNewDecl && CE->getOperatorNew())
5943 return nullptr;
5944
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005945 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005946 Importer.Import(CE->getOperatorDelete()));
5947 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5948 return nullptr;
5949
5950 Expr *ToInit = Importer.Import(CE->getInitializer());
5951 if (!ToInit && CE->getInitializer())
5952 return nullptr;
5953
5954 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5955 if (!TInfo)
5956 return nullptr;
5957
5958 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5959 if (!ToArrSize && CE->getArraySize())
5960 return nullptr;
5961
5962 return new (Importer.getToContext()) CXXNewExpr(
5963 Importer.getToContext(),
5964 CE->isGlobalNew(),
5965 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005966 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005967 CE->doesUsualArrayDeleteWantSize(),
5968 PlacementArgs,
5969 Importer.Import(CE->getTypeIdParens()),
5970 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5971 Importer.Import(CE->getSourceRange()),
5972 Importer.Import(CE->getDirectInitRange()));
5973}
5974
5975Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5976 QualType T = Importer.Import(E->getType());
5977 if (T.isNull())
5978 return nullptr;
5979
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005980 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005981 Importer.Import(E->getOperatorDelete()));
5982 if (!OperatorDeleteDecl && E->getOperatorDelete())
5983 return nullptr;
5984
5985 Expr *ToArg = Importer.Import(E->getArgument());
5986 if (!ToArg && E->getArgument())
5987 return nullptr;
5988
5989 return new (Importer.getToContext()) CXXDeleteExpr(
5990 T, E->isGlobalDelete(),
5991 E->isArrayForm(),
5992 E->isArrayFormAsWritten(),
5993 E->doesUsualArrayDeleteWantSize(),
5994 OperatorDeleteDecl,
5995 ToArg,
5996 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005997}
5998
Sean Callanan59721b32015-04-28 18:41:46 +00005999Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6000 QualType T = Importer.Import(E->getType());
6001 if (T.isNull())
6002 return nullptr;
6003
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006004 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006005 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006006 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006007 return nullptr;
6008
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006009 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006010 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006011 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006012
6013 return CXXConstructExpr::Create(Importer.getToContext(), T,
6014 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006015 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006016 ToArgs, E->hadMultipleCandidates(),
6017 E->isListInitialization(),
6018 E->isStdInitListInitialization(),
6019 E->requiresZeroInitialization(),
6020 E->getConstructionKind(),
6021 Importer.Import(E->getParenOrBraceRange()));
6022}
6023
Aleksei Sidorina693b372016-09-28 10:16:56 +00006024Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6025 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6026 if (!SubExpr && EWC->getSubExpr())
6027 return nullptr;
6028
6029 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6030 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6031 if (ExprWithCleanups::CleanupObject Obj =
6032 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6033 Objs[I] = Obj;
6034 else
6035 return nullptr;
6036
6037 return ExprWithCleanups::Create(Importer.getToContext(),
6038 SubExpr, EWC->cleanupsHaveSideEffects(),
6039 Objs);
6040}
6041
Sean Callanan8bca9962016-03-28 21:43:01 +00006042Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6043 QualType T = Importer.Import(E->getType());
6044 if (T.isNull())
6045 return nullptr;
6046
6047 Expr *ToFn = Importer.Import(E->getCallee());
6048 if (!ToFn)
6049 return nullptr;
6050
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006051 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006052 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006053 return nullptr;
6054
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006055 return new (Importer.getToContext()) CXXMemberCallExpr(
6056 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6057 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006058}
6059
6060Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6061 QualType T = Importer.Import(E->getType());
6062 if (T.isNull())
6063 return nullptr;
6064
6065 return new (Importer.getToContext())
6066 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6067}
6068
6069Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6070 QualType T = Importer.Import(E->getType());
6071 if (T.isNull())
6072 return nullptr;
6073
6074 return new (Importer.getToContext())
6075 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6076}
6077
6078
Sean Callanan59721b32015-04-28 18:41:46 +00006079Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6080 QualType T = Importer.Import(E->getType());
6081 if (T.isNull())
6082 return nullptr;
6083
6084 Expr *ToBase = Importer.Import(E->getBase());
6085 if (!ToBase && E->getBase())
6086 return nullptr;
6087
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006088 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006089 if (!ToMember && E->getMemberDecl())
6090 return nullptr;
6091
6092 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
6093 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
6094 E->getFoundDecl().getAccess());
6095
6096 DeclarationNameInfo ToMemberNameInfo(
6097 Importer.Import(E->getMemberNameInfo().getName()),
6098 Importer.Import(E->getMemberNameInfo().getLoc()));
6099
6100 if (E->hasExplicitTemplateArgs()) {
6101 return nullptr; // FIXME: handle template arguments
6102 }
6103
6104 return MemberExpr::Create(Importer.getToContext(), ToBase,
6105 E->isArrow(),
6106 Importer.Import(E->getOperatorLoc()),
6107 Importer.Import(E->getQualifierLoc()),
6108 Importer.Import(E->getTemplateKeywordLoc()),
6109 ToMember, ToFoundDecl, ToMemberNameInfo,
6110 nullptr, T, E->getValueKind(),
6111 E->getObjectKind());
6112}
6113
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006114Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6115 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006116 Expr *BaseE = Importer.Import(E->getBase());
6117 if (!BaseE)
6118 return nullptr;
6119
6120 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6121 if (!ScopeInfo && E->getScopeTypeInfo())
6122 return nullptr;
6123
6124 PseudoDestructorTypeStorage Storage;
6125 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6126 IdentifierInfo *ToII = Importer.Import(FromII);
6127 if (!ToII)
6128 return nullptr;
6129 Storage = PseudoDestructorTypeStorage(
6130 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6131 } else {
6132 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6133 if (!TI)
6134 return nullptr;
6135 Storage = PseudoDestructorTypeStorage(TI);
6136 }
6137
6138 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6139 Importer.getToContext(), BaseE, E->isArrow(),
6140 Importer.Import(E->getOperatorLoc()),
6141 Importer.Import(E->getQualifierLoc()),
6142 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6143 Importer.Import(E->getTildeLoc()), Storage);
6144}
6145
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006146Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6147 CXXDependentScopeMemberExpr *E) {
6148 Expr *Base = nullptr;
6149 if (!E->isImplicitAccess()) {
6150 Base = Importer.Import(E->getBase());
6151 if (!Base)
6152 return nullptr;
6153 }
6154
6155 QualType BaseType = Importer.Import(E->getBaseType());
6156 if (BaseType.isNull())
6157 return nullptr;
6158
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006159 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006160 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006161 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6162 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006163 return nullptr;
6164 ResInfo = &ToTAInfo;
6165 }
6166
6167 DeclarationName Name = Importer.Import(E->getMember());
6168 if (!E->getMember().isEmpty() && Name.isEmpty())
6169 return nullptr;
6170
6171 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6172 // Import additional name location/type info.
6173 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6174 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6175 if (!ToFQ && E->getFirstQualifierFoundInScope())
6176 return nullptr;
6177
6178 return CXXDependentScopeMemberExpr::Create(
6179 Importer.getToContext(), Base, BaseType, E->isArrow(),
6180 Importer.Import(E->getOperatorLoc()),
6181 Importer.Import(E->getQualifierLoc()),
6182 Importer.Import(E->getTemplateKeywordLoc()),
6183 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6184}
6185
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006186Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6187 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006188 unsigned NumArgs = CE->arg_size();
6189
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006190 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006191 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6192 return nullptr;
6193
6194 return CXXUnresolvedConstructExpr::Create(
6195 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6196 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6197 Importer.Import(CE->getRParenLoc()));
6198}
6199
6200Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006201 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006202 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6203 if (E->getNamingClass() && !NamingClass)
6204 return nullptr;
6205
6206 DeclarationName Name = Importer.Import(E->getName());
6207 if (E->getName() && !Name)
6208 return nullptr;
6209
6210 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6211 // Import additional name location/type info.
6212 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6213
6214 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006215 for (auto *D : E->decls()) {
6216 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006217 ToDecls.addDecl(To);
6218 else
6219 return nullptr;
6220 }
6221
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006222 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006223 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006224 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6225 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006226 return nullptr;
6227 ResInfo = &ToTAInfo;
6228 }
6229
6230 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6231 return UnresolvedLookupExpr::Create(
6232 Importer.getToContext(), NamingClass,
6233 Importer.Import(E->getQualifierLoc()),
6234 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6235 ResInfo, ToDecls.begin(), ToDecls.end());
6236
6237 return UnresolvedLookupExpr::Create(
6238 Importer.getToContext(), NamingClass,
6239 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6240 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6241}
6242
Sean Callanan59721b32015-04-28 18:41:46 +00006243Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6244 QualType T = Importer.Import(E->getType());
6245 if (T.isNull())
6246 return nullptr;
6247
6248 Expr *ToCallee = Importer.Import(E->getCallee());
6249 if (!ToCallee && E->getCallee())
6250 return nullptr;
6251
6252 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006253 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006254 if (ImportContainerChecked(E->arguments(), ToArgs))
6255 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006256
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006257 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006258
6259 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6260 ToArgs_Copied[ai] = ToArgs[ai];
6261
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006262 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6263 return new (Importer.getToContext()) CXXOperatorCallExpr(
6264 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6265 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6266 OCE->getFPFeatures());
6267 }
6268
Sean Callanan59721b32015-04-28 18:41:46 +00006269 return new (Importer.getToContext())
6270 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006271 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006272 Importer.Import(E->getRParenLoc()));
6273}
6274
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006275Optional<LambdaCapture>
6276ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6277 VarDecl *Var = nullptr;
6278 if (From.capturesVariable()) {
6279 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6280 if (!Var)
6281 return None;
6282 }
6283
6284 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6285 From.getCaptureKind(), Var,
6286 From.isPackExpansion()
6287 ? Importer.Import(From.getEllipsisLoc())
6288 : SourceLocation());
6289}
6290
6291Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6292 CXXRecordDecl *FromClass = LE->getLambdaClass();
6293 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6294 if (!ToClass)
6295 return nullptr;
6296
6297 // NOTE: lambda classes are created with BeingDefined flag set up.
6298 // It means that ImportDefinition doesn't work for them and we should fill it
6299 // manually.
6300 if (ToClass->isBeingDefined()) {
6301 for (auto FromField : FromClass->fields()) {
6302 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6303 if (!ToField)
6304 return nullptr;
6305 }
6306 }
6307
6308 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6309 Importer.Import(LE->getCallOperator()));
6310 if (!ToCallOp)
6311 return nullptr;
6312
6313 ToClass->completeDefinition();
6314
6315 unsigned NumCaptures = LE->capture_size();
6316 SmallVector<LambdaCapture, 8> Captures;
6317 Captures.reserve(NumCaptures);
6318 for (const auto &FromCapture : LE->captures()) {
6319 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6320 Captures.push_back(*ToCapture);
6321 else
6322 return nullptr;
6323 }
6324
6325 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6326 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6327 return nullptr;
6328
6329 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6330 Importer.Import(LE->getIntroducerRange()),
6331 LE->getCaptureDefault(),
6332 Importer.Import(LE->getCaptureDefaultLoc()),
6333 Captures,
6334 LE->hasExplicitParameters(),
6335 LE->hasExplicitResultType(),
6336 InitCaptures,
6337 Importer.Import(LE->getLocEnd()),
6338 LE->containsUnexpandedParameterPack());
6339}
6340
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006341Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6342 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006343 if (T.isNull())
6344 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006345
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006346 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006347 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006348 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006349
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006350 ASTContext &ToCtx = Importer.getToContext();
6351 InitListExpr *To = new (ToCtx) InitListExpr(
6352 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6353 Exprs, Importer.Import(ILE->getLBraceLoc()));
6354 To->setType(T);
6355
6356 if (ILE->hasArrayFiller()) {
6357 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6358 if (!Filler)
6359 return nullptr;
6360 To->setArrayFiller(Filler);
6361 }
6362
6363 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006364 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006365 if (!ToFD)
6366 return nullptr;
6367 To->setInitializedFieldInUnion(ToFD);
6368 }
6369
6370 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006371 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006372 if (!ToSyntForm)
6373 return nullptr;
6374 To->setSyntacticForm(ToSyntForm);
6375 }
6376
6377 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6378 To->setValueDependent(ILE->isValueDependent());
6379 To->setInstantiationDependent(ILE->isInstantiationDependent());
6380
6381 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006382}
6383
Richard Smith30e304e2016-12-14 00:03:17 +00006384Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6385 QualType ToType = Importer.Import(E->getType());
6386 if (ToType.isNull())
6387 return nullptr;
6388
6389 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6390 if (!ToCommon && E->getCommonExpr())
6391 return nullptr;
6392
6393 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6394 if (!ToSubExpr && E->getSubExpr())
6395 return nullptr;
6396
6397 return new (Importer.getToContext())
6398 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6399}
6400
6401Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6402 QualType ToType = Importer.Import(E->getType());
6403 if (ToType.isNull())
6404 return nullptr;
6405 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6406}
6407
Sean Callanandd2c1742016-05-16 20:48:03 +00006408Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006409 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006410 if (!ToField && DIE->getField())
6411 return nullptr;
6412
6413 return CXXDefaultInitExpr::Create(
6414 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6415}
6416
6417Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6418 QualType ToType = Importer.Import(E->getType());
6419 if (ToType.isNull() && !E->getType().isNull())
6420 return nullptr;
6421 ExprValueKind VK = E->getValueKind();
6422 CastKind CK = E->getCastKind();
6423 Expr *ToOp = Importer.Import(E->getSubExpr());
6424 if (!ToOp && E->getSubExpr())
6425 return nullptr;
6426 CXXCastPath BasePath;
6427 if (ImportCastPath(E, BasePath))
6428 return nullptr;
6429 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6430 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6431 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6432 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6433
6434 if (isa<CXXStaticCastExpr>(E)) {
6435 return CXXStaticCastExpr::Create(
6436 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6437 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6438 } else if (isa<CXXDynamicCastExpr>(E)) {
6439 return CXXDynamicCastExpr::Create(
6440 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6441 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6442 } else if (isa<CXXReinterpretCastExpr>(E)) {
6443 return CXXReinterpretCastExpr::Create(
6444 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6445 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6446 } else {
6447 return nullptr;
6448 }
6449}
6450
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006451Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6452 SubstNonTypeTemplateParmExpr *E) {
6453 QualType T = Importer.Import(E->getType());
6454 if (T.isNull())
6455 return nullptr;
6456
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006457 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006458 Importer.Import(E->getParameter()));
6459 if (!Param)
6460 return nullptr;
6461
6462 Expr *Replacement = Importer.Import(E->getReplacement());
6463 if (!Replacement)
6464 return nullptr;
6465
6466 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6467 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6468 Replacement);
6469}
6470
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006471Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6472 QualType ToType = Importer.Import(E->getType());
6473 if (ToType.isNull())
6474 return nullptr;
6475
6476 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6477 if (ImportContainerChecked(E->getArgs(), ToArgs))
6478 return nullptr;
6479
6480 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6481 // Value is always false.
6482 bool ToValue = false;
6483 if (!E->isValueDependent())
6484 ToValue = E->getValue();
6485
6486 return TypeTraitExpr::Create(
6487 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6488 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6489}
6490
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006491Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6492 QualType ToType = Importer.Import(E->getType());
6493 if (ToType.isNull())
6494 return nullptr;
6495
6496 if (E->isTypeOperand()) {
6497 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6498 if (!TSI)
6499 return nullptr;
6500
6501 return new (Importer.getToContext())
6502 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6503 }
6504
6505 Expr *Op = Importer.Import(E->getExprOperand());
6506 if (!Op)
6507 return nullptr;
6508
6509 return new (Importer.getToContext())
6510 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6511}
6512
Lang Hames19e07e12017-06-20 21:06:00 +00006513void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6514 CXXMethodDecl *FromMethod) {
6515 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6516 ToMethod->addOverriddenMethod(
6517 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6518 FromOverriddenMethod))));
6519}
6520
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006521ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006522 ASTContext &FromContext, FileManager &FromFileManager,
6523 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006524 : ToContext(ToContext), FromContext(FromContext),
6525 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6526 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006527 ImportedDecls[FromContext.getTranslationUnitDecl()]
6528 = ToContext.getTranslationUnitDecl();
6529}
6530
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006531ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006532
6533QualType ASTImporter::Import(QualType FromT) {
6534 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006535 return {};
John McCall424cec92011-01-19 06:33:43 +00006536
6537 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006538
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006539 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006540 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6541 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006542 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006543 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006544
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006545 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006546 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006547 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006548 if (ToT.isNull())
6549 return ToT;
6550
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006551 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006552 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006553
John McCall424cec92011-01-19 06:33:43 +00006554 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006555}
6556
Douglas Gregor62d311f2010-02-09 19:21:46 +00006557TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006558 if (!FromTSI)
6559 return FromTSI;
6560
6561 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006562 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006563 QualType T = Import(FromTSI->getType());
6564 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006565 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006566
6567 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006568 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006569}
6570
Sean Callanan59721b32015-04-28 18:41:46 +00006571Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6572 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6573 if (Pos != ImportedDecls.end()) {
6574 Decl *ToD = Pos->second;
6575 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6576 return ToD;
6577 } else {
6578 return nullptr;
6579 }
6580}
6581
Douglas Gregor62d311f2010-02-09 19:21:46 +00006582Decl *ASTImporter::Import(Decl *FromD) {
6583 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006584 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006585
Douglas Gregord451ea92011-07-29 23:31:30 +00006586 ASTNodeImporter Importer(*this);
6587
Douglas Gregor62d311f2010-02-09 19:21:46 +00006588 // Check whether we've already imported this declaration.
6589 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006590 if (Pos != ImportedDecls.end()) {
6591 Decl *ToD = Pos->second;
6592 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6593 return ToD;
6594 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006595
6596 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006597 Decl *ToD = Importer.Visit(FromD);
6598 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006599 return nullptr;
6600
Douglas Gregor62d311f2010-02-09 19:21:46 +00006601 // Record the imported declaration.
6602 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006603 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006604 return ToD;
6605}
6606
6607DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6608 if (!FromDC)
6609 return FromDC;
6610
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006611 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006612 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006613 return nullptr;
6614
Douglas Gregor2e15c842012-02-01 21:00:38 +00006615 // When we're using a record/enum/Objective-C class/protocol as a context, we
6616 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006617 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6618 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006619 if (ToRecord->isCompleteDefinition()) {
6620 // Do nothing.
6621 } else if (FromRecord->isCompleteDefinition()) {
6622 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6623 ASTNodeImporter::IDK_Basic);
6624 } else {
6625 CompleteDecl(ToRecord);
6626 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006627 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6628 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006629 if (ToEnum->isCompleteDefinition()) {
6630 // Do nothing.
6631 } else if (FromEnum->isCompleteDefinition()) {
6632 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6633 ASTNodeImporter::IDK_Basic);
6634 } else {
6635 CompleteDecl(ToEnum);
6636 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006637 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6638 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006639 if (ToClass->getDefinition()) {
6640 // Do nothing.
6641 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6642 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6643 ASTNodeImporter::IDK_Basic);
6644 } else {
6645 CompleteDecl(ToClass);
6646 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006647 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6648 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006649 if (ToProto->getDefinition()) {
6650 // Do nothing.
6651 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6652 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6653 ASTNodeImporter::IDK_Basic);
6654 } else {
6655 CompleteDecl(ToProto);
6656 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006657 }
6658
6659 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006660}
6661
6662Expr *ASTImporter::Import(Expr *FromE) {
6663 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006664 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006665
6666 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6667}
6668
6669Stmt *ASTImporter::Import(Stmt *FromS) {
6670 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006671 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006672
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006673 // Check whether we've already imported this declaration.
6674 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6675 if (Pos != ImportedStmts.end())
6676 return Pos->second;
6677
6678 // Import the type
6679 ASTNodeImporter Importer(*this);
6680 Stmt *ToS = Importer.Visit(FromS);
6681 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006682 return nullptr;
6683
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006684 // Record the imported declaration.
6685 ImportedStmts[FromS] = ToS;
6686 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006687}
6688
6689NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6690 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006691 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006692
Douglas Gregor90ebf252011-04-27 16:48:40 +00006693 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6694
6695 switch (FromNNS->getKind()) {
6696 case NestedNameSpecifier::Identifier:
6697 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6698 return NestedNameSpecifier::Create(ToContext, prefix, II);
6699 }
Craig Topper36250ad2014-05-12 05:36:57 +00006700 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006701
6702 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006703 if (auto *NS =
6704 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006705 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6706 }
Craig Topper36250ad2014-05-12 05:36:57 +00006707 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006708
6709 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006710 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006711 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006712 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6713 }
Craig Topper36250ad2014-05-12 05:36:57 +00006714 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006715
6716 case NestedNameSpecifier::Global:
6717 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6718
Nikola Smiljanic67860242014-09-26 00:28:20 +00006719 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006720 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006721 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006722 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6723 }
6724 return nullptr;
6725
Douglas Gregor90ebf252011-04-27 16:48:40 +00006726 case NestedNameSpecifier::TypeSpec:
6727 case NestedNameSpecifier::TypeSpecWithTemplate: {
6728 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6729 if (!T.isNull()) {
6730 bool bTemplate = FromNNS->getKind() ==
6731 NestedNameSpecifier::TypeSpecWithTemplate;
6732 return NestedNameSpecifier::Create(ToContext, prefix,
6733 bTemplate, T.getTypePtr());
6734 }
6735 }
Craig Topper36250ad2014-05-12 05:36:57 +00006736 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006737 }
6738
6739 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006740}
6741
Douglas Gregor14454802011-02-25 02:25:35 +00006742NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006743 // Copied from NestedNameSpecifier mostly.
6744 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6745 NestedNameSpecifierLoc NNS = FromNNS;
6746
6747 // Push each of the nested-name-specifiers's onto a stack for
6748 // serialization in reverse order.
6749 while (NNS) {
6750 NestedNames.push_back(NNS);
6751 NNS = NNS.getPrefix();
6752 }
6753
6754 NestedNameSpecifierLocBuilder Builder;
6755
6756 while (!NestedNames.empty()) {
6757 NNS = NestedNames.pop_back_val();
6758 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6759 if (!Spec)
6760 return NestedNameSpecifierLoc();
6761
6762 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6763 switch (Kind) {
6764 case NestedNameSpecifier::Identifier:
6765 Builder.Extend(getToContext(),
6766 Spec->getAsIdentifier(),
6767 Import(NNS.getLocalBeginLoc()),
6768 Import(NNS.getLocalEndLoc()));
6769 break;
6770
6771 case NestedNameSpecifier::Namespace:
6772 Builder.Extend(getToContext(),
6773 Spec->getAsNamespace(),
6774 Import(NNS.getLocalBeginLoc()),
6775 Import(NNS.getLocalEndLoc()));
6776 break;
6777
6778 case NestedNameSpecifier::NamespaceAlias:
6779 Builder.Extend(getToContext(),
6780 Spec->getAsNamespaceAlias(),
6781 Import(NNS.getLocalBeginLoc()),
6782 Import(NNS.getLocalEndLoc()));
6783 break;
6784
6785 case NestedNameSpecifier::TypeSpec:
6786 case NestedNameSpecifier::TypeSpecWithTemplate: {
6787 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6788 QualType(Spec->getAsType(), 0));
6789 Builder.Extend(getToContext(),
6790 Import(NNS.getLocalBeginLoc()),
6791 TSI->getTypeLoc(),
6792 Import(NNS.getLocalEndLoc()));
6793 break;
6794 }
6795
6796 case NestedNameSpecifier::Global:
6797 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6798 break;
6799
6800 case NestedNameSpecifier::Super: {
6801 SourceRange ToRange = Import(NNS.getSourceRange());
6802 Builder.MakeSuper(getToContext(),
6803 Spec->getAsRecordDecl(),
6804 ToRange.getBegin(),
6805 ToRange.getEnd());
6806 }
6807 }
6808 }
6809
6810 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006811}
6812
Douglas Gregore2e50d332010-12-01 01:36:18 +00006813TemplateName ASTImporter::Import(TemplateName From) {
6814 switch (From.getKind()) {
6815 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006816 if (auto *ToTemplate =
6817 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006818 return TemplateName(ToTemplate);
6819
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006820 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006821
6822 case TemplateName::OverloadedTemplate: {
6823 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6824 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006825 for (auto *I : *FromStorage) {
6826 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006827 ToTemplates.addDecl(To);
6828 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006829 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006830 }
6831 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6832 ToTemplates.end());
6833 }
6834
6835 case TemplateName::QualifiedTemplate: {
6836 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6837 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6838 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006839 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006840
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006841 if (auto *ToTemplate =
6842 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006843 return ToContext.getQualifiedTemplateName(Qualifier,
6844 QTN->hasTemplateKeyword(),
6845 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006846
6847 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006848 }
6849
6850 case TemplateName::DependentTemplate: {
6851 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6852 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6853 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006854 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006855
6856 if (DTN->isIdentifier()) {
6857 return ToContext.getDependentTemplateName(Qualifier,
6858 Import(DTN->getIdentifier()));
6859 }
6860
6861 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6862 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006863
6864 case TemplateName::SubstTemplateTemplateParm: {
6865 SubstTemplateTemplateParmStorage *subst
6866 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006867 auto *param =
6868 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006869 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006870 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006871
6872 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006873 if (replacement.isNull())
6874 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006875
6876 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6877 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006878
6879 case TemplateName::SubstTemplateTemplateParmPack: {
6880 SubstTemplateTemplateParmPackStorage *SubstPack
6881 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006882 auto *Param =
6883 cast_or_null<TemplateTemplateParmDecl>(
6884 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00006885 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006886 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006887
6888 ASTNodeImporter Importer(*this);
6889 TemplateArgument ArgPack
6890 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6891 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006892 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006893
6894 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6895 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006896 }
6897
6898 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006899}
6900
Douglas Gregor62d311f2010-02-09 19:21:46 +00006901SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6902 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006903 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00006904
Douglas Gregor811663e2010-02-10 00:15:17 +00006905 SourceManager &FromSM = FromContext.getSourceManager();
6906
Sean Callanan24c5fe62016-11-07 20:42:25 +00006907 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006908 // don't have to import macro expansions.
6909 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006910 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006911 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6912 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006913 FileID ToFileID = Import(Decomposed.first);
6914 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006915 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00006916 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6917 .getLocWithOffset(Decomposed.second);
6918 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006919}
6920
6921SourceRange ASTImporter::Import(SourceRange FromRange) {
6922 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6923}
6924
Douglas Gregor811663e2010-02-10 00:15:17 +00006925FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006926 llvm::DenseMap<FileID, FileID>::iterator Pos
6927 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006928 if (Pos != ImportedFileIDs.end())
6929 return Pos->second;
6930
6931 SourceManager &FromSM = FromContext.getSourceManager();
6932 SourceManager &ToSM = ToContext.getSourceManager();
6933 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006934 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006935
6936 // Include location of this file.
6937 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6938
6939 // Map the FileID for to the "to" source manager.
6940 FileID ToID;
6941 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006942 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006943 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6944 // disk again
6945 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6946 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006947 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006948 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006949 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00006950 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6951 FromSLoc.getFile().getFileCharacteristic());
6952 } else {
6953 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006954 const llvm::MemoryBuffer *
6955 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006956 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006957 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006958 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006959 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006960 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006961 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006962
Sebastian Redl99219f12010-09-30 01:03:06 +00006963 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006964 return ToID;
6965}
6966
Sean Callanandd2c1742016-05-16 20:48:03 +00006967CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6968 Expr *ToExpr = Import(From->getInit());
6969 if (!ToExpr && From->getInit())
6970 return nullptr;
6971
6972 if (From->isBaseInitializer()) {
6973 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6974 if (!ToTInfo && From->getTypeSourceInfo())
6975 return nullptr;
6976
6977 return new (ToContext) CXXCtorInitializer(
6978 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6979 ToExpr, Import(From->getRParenLoc()),
6980 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6981 : SourceLocation());
6982 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006983 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006984 if (!ToField && From->getMember())
6985 return nullptr;
6986
6987 return new (ToContext) CXXCtorInitializer(
6988 ToContext, ToField, Import(From->getMemberLocation()),
6989 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6990 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006991 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00006992 Import(From->getIndirectMember()));
6993 if (!ToIField && From->getIndirectMember())
6994 return nullptr;
6995
6996 return new (ToContext) CXXCtorInitializer(
6997 ToContext, ToIField, Import(From->getMemberLocation()),
6998 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6999 } else if (From->isDelegatingInitializer()) {
7000 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7001 if (!ToTInfo && From->getTypeSourceInfo())
7002 return nullptr;
7003
7004 return new (ToContext)
7005 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7006 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007007 } else {
7008 return nullptr;
7009 }
7010}
7011
Aleksei Sidorina693b372016-09-28 10:16:56 +00007012CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7013 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7014 if (Pos != ImportedCXXBaseSpecifiers.end())
7015 return Pos->second;
7016
7017 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7018 Import(BaseSpec->getSourceRange()),
7019 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7020 BaseSpec->getAccessSpecifierAsWritten(),
7021 Import(BaseSpec->getTypeSourceInfo()),
7022 Import(BaseSpec->getEllipsisLoc()));
7023 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7024 return Imported;
7025}
7026
Douglas Gregor0a791672011-01-18 03:11:38 +00007027void ASTImporter::ImportDefinition(Decl *From) {
7028 Decl *To = Import(From);
7029 if (!To)
7030 return;
7031
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007032 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007033 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007034
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007035 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007036 if (!ToRecord->getDefinition()) {
7037 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007038 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007039 return;
7040 }
7041 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007042
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007043 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007044 if (!ToEnum->getDefinition()) {
7045 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007046 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007047 return;
7048 }
7049 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007050
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007051 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007052 if (!ToIFace->getDefinition()) {
7053 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007054 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007055 return;
7056 }
7057 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007058
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007059 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007060 if (!ToProto->getDefinition()) {
7061 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007062 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007063 return;
7064 }
7065 }
7066
Douglas Gregor0a791672011-01-18 03:11:38 +00007067 Importer.ImportDeclContext(FromDC, true);
7068 }
7069}
7070
Douglas Gregor96e578d2010-02-05 17:54:41 +00007071DeclarationName ASTImporter::Import(DeclarationName FromName) {
7072 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007073 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007074
7075 switch (FromName.getNameKind()) {
7076 case DeclarationName::Identifier:
7077 return Import(FromName.getAsIdentifierInfo());
7078
7079 case DeclarationName::ObjCZeroArgSelector:
7080 case DeclarationName::ObjCOneArgSelector:
7081 case DeclarationName::ObjCMultiArgSelector:
7082 return Import(FromName.getObjCSelector());
7083
7084 case DeclarationName::CXXConstructorName: {
7085 QualType T = Import(FromName.getCXXNameType());
7086 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007087 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007088
7089 return ToContext.DeclarationNames.getCXXConstructorName(
7090 ToContext.getCanonicalType(T));
7091 }
7092
7093 case DeclarationName::CXXDestructorName: {
7094 QualType T = Import(FromName.getCXXNameType());
7095 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007096 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007097
7098 return ToContext.DeclarationNames.getCXXDestructorName(
7099 ToContext.getCanonicalType(T));
7100 }
7101
Richard Smith35845152017-02-07 01:37:30 +00007102 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007103 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007104 Import(FromName.getCXXDeductionGuideTemplate()));
7105 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007106 return {};
Richard Smith35845152017-02-07 01:37:30 +00007107 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7108 }
7109
Douglas Gregor96e578d2010-02-05 17:54:41 +00007110 case DeclarationName::CXXConversionFunctionName: {
7111 QualType T = Import(FromName.getCXXNameType());
7112 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007113 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007114
7115 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7116 ToContext.getCanonicalType(T));
7117 }
7118
7119 case DeclarationName::CXXOperatorName:
7120 return ToContext.DeclarationNames.getCXXOperatorName(
7121 FromName.getCXXOverloadedOperator());
7122
7123 case DeclarationName::CXXLiteralOperatorName:
7124 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7125 Import(FromName.getCXXLiteralIdentifier()));
7126
7127 case DeclarationName::CXXUsingDirective:
7128 // FIXME: STATICS!
7129 return DeclarationName::getUsingDirectiveName();
7130 }
7131
David Blaikiee4d798f2012-01-20 21:50:17 +00007132 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007133}
7134
Douglas Gregore2e50d332010-12-01 01:36:18 +00007135IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007136 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007137 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007138
Sean Callananf94ef1d2016-05-14 06:11:19 +00007139 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7140
7141 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7142 ToId->setBuiltinID(FromId->getBuiltinID());
7143
7144 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007145}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007146
Douglas Gregor43f54792010-02-17 02:12:47 +00007147Selector ASTImporter::Import(Selector FromSel) {
7148 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007149 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007150
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007151 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007152 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7153 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7154 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7155 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7156}
7157
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007158DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7159 DeclContext *DC,
7160 unsigned IDNS,
7161 NamedDecl **Decls,
7162 unsigned NumDecls) {
7163 return Name;
7164}
7165
7166DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007167 if (LastDiagFromFrom)
7168 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7169 FromContext.getDiagnostics());
7170 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007171 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007172}
7173
7174DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007175 if (!LastDiagFromFrom)
7176 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7177 ToContext.getDiagnostics());
7178 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007179 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007180}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007181
Douglas Gregor2e15c842012-02-01 21:00:38 +00007182void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007183 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007184 if (!ID->getDefinition())
7185 ID->startDefinition();
7186 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007187 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007188 if (!PD->getDefinition())
7189 PD->startDefinition();
7190 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007191 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007192 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7193 TD->startDefinition();
7194 TD->setCompleteDefinition(true);
7195 }
7196 }
7197 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007198 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007199 }
7200}
7201
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007202Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007203 if (From->hasAttrs()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007204 for (auto *FromAttr : From->getAttrs())
Sean Callanan8bca9962016-03-28 21:43:01 +00007205 To->addAttr(FromAttr->clone(To->getASTContext()));
7206 }
7207 if (From->isUsed()) {
7208 To->setIsUsed();
7209 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007210 if (From->isImplicit()) {
7211 To->setImplicit();
7212 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007213 ImportedDecls[From] = To;
7214 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007215}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007216
Douglas Gregordd6006f2012-07-17 21:16:27 +00007217bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7218 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007219 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007220 = ImportedTypes.find(From.getTypePtr());
7221 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7222 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007223
Douglas Gregordd6006f2012-07-17 21:16:27 +00007224 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7225 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007226 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007227}