blob: 66b596041fb29ad0595c896344a376839e40f852 [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);
Peter Szecsice7f3182018-05-07 12:08:27 +0000124 QualType VisitDependentNameType(const DependentNameType *T);
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);
Peter Szecsice7f3182018-05-07 12:08:27 +0000350 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000351 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
352 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000353 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000354 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000355 Expr *VisitCXXThisExpr(CXXThisExpr *E);
356 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000357 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000358 Expr *VisitMemberExpr(MemberExpr *E);
359 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000360 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000361 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000362 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
363 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000364 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
365 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000366 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000367 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000368 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000369
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000370 template<typename IIter, typename OIter>
371 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000372 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
373
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000374 ASTImporter &ImporterRef = Importer;
375 std::transform(Ibegin, Iend, Obegin,
376 [&ImporterRef](ItemT From) -> ItemT {
377 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000378 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000379 }
380
381 template<typename IIter, typename OIter>
382 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000383 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
384
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000385 ASTImporter &ImporterRef = Importer;
386 bool Failed = false;
387 std::transform(Ibegin, Iend, Obegin,
388 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000389 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000390 if (!To && From)
391 Failed = true;
392 return To;
393 });
394 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000395 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000396
397 template<typename InContainerTy, typename OutContainerTy>
398 bool ImportContainerChecked(const InContainerTy &InContainer,
399 OutContainerTy &OutContainer) {
400 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
401 OutContainer.begin());
402 }
403
404 template<typename InContainerTy, typename OIter>
405 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
406 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
407 }
Lang Hames19e07e12017-06-20 21:06:00 +0000408
409 // Importing overrides.
410 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000411 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000412
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000413template <typename InContainerTy>
414bool ASTNodeImporter::ImportTemplateArgumentListInfo(
415 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
416 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
417 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
418 Importer.Import(FromRAngleLoc));
419 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
420 return true;
421 Result = ToTAInfo;
422 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000423}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000424
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000425template <>
426bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
427 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
428 return ImportTemplateArgumentListInfo(
429 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
430}
431
432template <>
433bool ASTNodeImporter::ImportTemplateArgumentListInfo<
434 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
435 TemplateArgumentListInfo &Result) {
436 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
437 From.arguments(), Result);
438}
439
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000440} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000441
Douglas Gregor3996e242010-02-15 22:01:00 +0000442//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000443// Import Types
444//----------------------------------------------------------------------------
445
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000446using namespace clang;
447
John McCall424cec92011-01-19 06:33:43 +0000448QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000449 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
450 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000451 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000452}
453
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000454QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
455 QualType UnderlyingType = Importer.Import(T->getValueType());
456 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000457 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000458
459 return Importer.getToContext().getAtomicType(UnderlyingType);
460}
461
John McCall424cec92011-01-19 06:33:43 +0000462QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000463 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000464#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
465 case BuiltinType::Id: \
466 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000467#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000468#define SHARED_SINGLETON_TYPE(Expansion)
469#define BUILTIN_TYPE(Id, SingletonId) \
470 case BuiltinType::Id: return Importer.getToContext().SingletonId;
471#include "clang/AST/BuiltinTypes.def"
472
473 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
474 // context supports C++.
475
476 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
477 // context supports ObjC.
478
Douglas Gregor96e578d2010-02-05 17:54:41 +0000479 case BuiltinType::Char_U:
480 // The context we're importing from has an unsigned 'char'. If we're
481 // importing into a context with a signed 'char', translate to
482 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000483 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000484 return Importer.getToContext().UnsignedCharTy;
485
486 return Importer.getToContext().CharTy;
487
Douglas Gregor96e578d2010-02-05 17:54:41 +0000488 case BuiltinType::Char_S:
489 // The context we're importing from has an unsigned 'char'. If we're
490 // importing into a context with a signed 'char', translate to
491 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000492 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000493 return Importer.getToContext().SignedCharTy;
494
495 return Importer.getToContext().CharTy;
496
Chris Lattnerad3467e2010-12-25 23:25:43 +0000497 case BuiltinType::WChar_S:
498 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000499 // FIXME: If not in C++, shall we translate to the C equivalent of
500 // wchar_t?
501 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000502 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000503
504 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000505}
506
Aleksei Sidorina693b372016-09-28 10:16:56 +0000507QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
508 QualType OrigT = Importer.Import(T->getOriginalType());
509 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000510 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000511
512 return Importer.getToContext().getDecayedType(OrigT);
513}
514
John McCall424cec92011-01-19 06:33:43 +0000515QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000516 QualType ToElementType = Importer.Import(T->getElementType());
517 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000518 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000519
520 return Importer.getToContext().getComplexType(ToElementType);
521}
522
John McCall424cec92011-01-19 06:33:43 +0000523QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000524 QualType ToPointeeType = Importer.Import(T->getPointeeType());
525 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000526 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000527
528 return Importer.getToContext().getPointerType(ToPointeeType);
529}
530
John McCall424cec92011-01-19 06:33:43 +0000531QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000532 // FIXME: Check for blocks support in "to" context.
533 QualType ToPointeeType = Importer.Import(T->getPointeeType());
534 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000535 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000536
537 return Importer.getToContext().getBlockPointerType(ToPointeeType);
538}
539
John McCall424cec92011-01-19 06:33:43 +0000540QualType
541ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000542 // FIXME: Check for C++ support in "to" context.
543 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
544 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000545 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000546
547 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
548}
549
John McCall424cec92011-01-19 06:33:43 +0000550QualType
551ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000552 // FIXME: Check for C++0x support in "to" context.
553 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
554 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000555 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000556
557 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
558}
559
John McCall424cec92011-01-19 06:33:43 +0000560QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000561 // FIXME: Check for C++ support in "to" context.
562 QualType ToPointeeType = Importer.Import(T->getPointeeType());
563 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000564 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000565
566 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
567 return Importer.getToContext().getMemberPointerType(ToPointeeType,
568 ClassType.getTypePtr());
569}
570
John McCall424cec92011-01-19 06:33:43 +0000571QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000572 QualType ToElementType = Importer.Import(T->getElementType());
573 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000574 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000575
576 return Importer.getToContext().getConstantArrayType(ToElementType,
577 T->getSize(),
578 T->getSizeModifier(),
579 T->getIndexTypeCVRQualifiers());
580}
581
John McCall424cec92011-01-19 06:33:43 +0000582QualType
583ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000584 QualType ToElementType = Importer.Import(T->getElementType());
585 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000586 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000587
588 return Importer.getToContext().getIncompleteArrayType(ToElementType,
589 T->getSizeModifier(),
590 T->getIndexTypeCVRQualifiers());
591}
592
John McCall424cec92011-01-19 06:33:43 +0000593QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000594 QualType ToElementType = Importer.Import(T->getElementType());
595 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000596 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000597
598 Expr *Size = Importer.Import(T->getSizeExpr());
599 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000600 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000601
602 SourceRange Brackets = Importer.Import(T->getBracketsRange());
603 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
604 T->getSizeModifier(),
605 T->getIndexTypeCVRQualifiers(),
606 Brackets);
607}
608
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000609QualType ASTNodeImporter::VisitDependentSizedArrayType(
610 const DependentSizedArrayType *T) {
611 QualType ToElementType = Importer.Import(T->getElementType());
612 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000613 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000614
615 // SizeExpr may be null if size is not specified directly.
616 // For example, 'int a[]'.
617 Expr *Size = Importer.Import(T->getSizeExpr());
618 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000619 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000620
621 SourceRange Brackets = Importer.Import(T->getBracketsRange());
622 return Importer.getToContext().getDependentSizedArrayType(
623 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
624 Brackets);
625}
626
John McCall424cec92011-01-19 06:33:43 +0000627QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000628 QualType ToElementType = Importer.Import(T->getElementType());
629 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000630 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000631
632 return Importer.getToContext().getVectorType(ToElementType,
633 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000634 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000635}
636
John McCall424cec92011-01-19 06:33:43 +0000637QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000638 QualType ToElementType = Importer.Import(T->getElementType());
639 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000640 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000641
642 return Importer.getToContext().getExtVectorType(ToElementType,
643 T->getNumElements());
644}
645
John McCall424cec92011-01-19 06:33:43 +0000646QualType
647ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000648 // FIXME: What happens if we're importing a function without a prototype
649 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000650 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000651 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000652 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000653
Douglas Gregor96e578d2010-02-05 17:54:41 +0000654 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000655 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000656}
657
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000658QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000659 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000660 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000661 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000662
663 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000664 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000665 for (const auto &A : T->param_types()) {
666 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000668 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000669 ArgTypes.push_back(ArgType);
670 }
671
672 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000673 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000674 for (const auto &E : T->exceptions()) {
675 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000676 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000677 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000678 ExceptionTypes.push_back(ExceptionType);
679 }
John McCalldb40c7f2010-12-14 08:05:40 +0000680
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000681 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
682 FunctionProtoType::ExtProtoInfo ToEPI;
683
684 ToEPI.ExtInfo = FromEPI.ExtInfo;
685 ToEPI.Variadic = FromEPI.Variadic;
686 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
687 ToEPI.TypeQuals = FromEPI.TypeQuals;
688 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000689 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
690 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
691 ToEPI.ExceptionSpec.NoexceptExpr =
692 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
693 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
694 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
695 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
696 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000697
Jordan Rose5c382722013-03-08 21:51:21 +0000698 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000699}
700
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000701QualType ASTNodeImporter::VisitUnresolvedUsingType(
702 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000703 const auto *ToD =
704 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000705 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000706 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000707
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000708 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000709 cast_or_null<UnresolvedUsingTypenameDecl>(
710 Importer.Import(T->getDecl()->getPreviousDecl()));
711 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000712 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000713
714 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
715}
716
Sean Callananda6df8a2011-08-11 16:56:07 +0000717QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
718 QualType ToInnerType = Importer.Import(T->getInnerType());
719 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000720 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000721
722 return Importer.getToContext().getParenType(ToInnerType);
723}
724
John McCall424cec92011-01-19 06:33:43 +0000725QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000726 auto *ToDecl =
727 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000728 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000729 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000730
731 return Importer.getToContext().getTypeDeclType(ToDecl);
732}
733
John McCall424cec92011-01-19 06:33:43 +0000734QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000735 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
736 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000737 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000738
739 return Importer.getToContext().getTypeOfExprType(ToExpr);
740}
741
John McCall424cec92011-01-19 06:33:43 +0000742QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000743 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
744 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000745 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000746
747 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
748}
749
John McCall424cec92011-01-19 06:33:43 +0000750QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000751 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000752 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
753 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000754 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000755
Douglas Gregor81495f32012-02-12 18:42:33 +0000756 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
757 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000758 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000759
760 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000761}
762
Alexis Hunte852b102011-05-24 22:41:36 +0000763QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
764 QualType ToBaseType = Importer.Import(T->getBaseType());
765 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
766 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000767 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000768
769 return Importer.getToContext().getUnaryTransformType(ToBaseType,
770 ToUnderlyingType,
771 T->getUTTKind());
772}
773
Richard Smith30482bc2011-02-20 03:19:35 +0000774QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000775 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000776 QualType FromDeduced = T->getDeducedType();
777 QualType ToDeduced;
778 if (!FromDeduced.isNull()) {
779 ToDeduced = Importer.Import(FromDeduced);
780 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000781 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000782 }
783
Richard Smithe301ba22015-11-11 02:02:15 +0000784 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000785 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000786}
787
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000788QualType ASTNodeImporter::VisitInjectedClassNameType(
789 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000790 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000791 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000792 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000793
794 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
795 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000796 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000797
798 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
799 // See comments in InjectedClassNameType definition for details
800 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
801 enum {
802 TypeAlignmentInBits = 4,
803 TypeAlignment = 1 << TypeAlignmentInBits
804 };
805
806 return QualType(new (Importer.getToContext(), TypeAlignment)
807 InjectedClassNameType(D, InjType), 0);
808}
809
John McCall424cec92011-01-19 06:33:43 +0000810QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000811 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000812 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000813 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000814
815 return Importer.getToContext().getTagDeclType(ToDecl);
816}
817
John McCall424cec92011-01-19 06:33:43 +0000818QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000819 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000820 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000821 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000822
823 return Importer.getToContext().getTagDeclType(ToDecl);
824}
825
Sean Callanan72fe0852015-04-02 23:50:08 +0000826QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
827 QualType FromModifiedType = T->getModifiedType();
828 QualType FromEquivalentType = T->getEquivalentType();
829 QualType ToModifiedType;
830 QualType ToEquivalentType;
831
832 if (!FromModifiedType.isNull()) {
833 ToModifiedType = Importer.Import(FromModifiedType);
834 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000835 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000836 }
837 if (!FromEquivalentType.isNull()) {
838 ToEquivalentType = Importer.Import(FromEquivalentType);
839 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000840 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000841 }
842
843 return Importer.getToContext().getAttributedType(T->getAttrKind(),
844 ToModifiedType, ToEquivalentType);
845}
846
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000847QualType ASTNodeImporter::VisitTemplateTypeParmType(
848 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000849 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000850 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
851 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000852 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000853
854 return Importer.getToContext().getTemplateTypeParmType(
855 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
856}
857
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000858QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
859 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000860 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000861 cast_or_null<TemplateTypeParmType>(Importer.Import(
862 QualType(T->getReplacedParameter(), 0)).getTypePtr());
863 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000864 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000865
866 QualType Replacement = Importer.Import(T->getReplacementType());
867 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000868 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000869 Replacement = Replacement.getCanonicalType();
870
871 return Importer.getToContext().getSubstTemplateTypeParmType(
872 Replaced, Replacement);
873}
874
Douglas Gregore2e50d332010-12-01 01:36:18 +0000875QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000876 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000877 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
878 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000879 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000880
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000881 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000882 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000883 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000884
885 QualType ToCanonType;
886 if (!QualType(T, 0).isCanonical()) {
887 QualType FromCanonType
888 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
889 ToCanonType =Importer.Import(FromCanonType);
890 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000891 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000892 }
893 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000894 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000895 ToCanonType);
896}
897
John McCall424cec92011-01-19 06:33:43 +0000898QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000899 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000900 // Note: the qualifier in an ElaboratedType is optional.
901 if (T->getQualifier()) {
902 ToQualifier = Importer.Import(T->getQualifier());
903 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000904 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +0000905 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000906
907 QualType ToNamedType = Importer.Import(T->getNamedType());
908 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000909 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000910
Abramo Bagnara6150c882010-05-11 21:36:43 +0000911 return Importer.getToContext().getElaboratedType(T->getKeyword(),
912 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000913}
914
Gabor Horvath7a91c082017-11-14 11:30:38 +0000915QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
916 QualType Pattern = Importer.Import(T->getPattern());
917 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000918 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +0000919
920 return Importer.getToContext().getPackExpansionType(Pattern,
921 T->getNumExpansions());
922}
923
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000924QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
925 const DependentTemplateSpecializationType *T) {
926 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
927 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000928 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000929
930 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
931 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000932 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000933
934 SmallVector<TemplateArgument, 2> ToPack;
935 ToPack.reserve(T->getNumArgs());
936 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000937 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000938
939 return Importer.getToContext().getDependentTemplateSpecializationType(
940 T->getKeyword(), Qualifier, Name, ToPack);
941}
942
Peter Szecsice7f3182018-05-07 12:08:27 +0000943QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
944 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
945 if (!NNS && T->getQualifier())
946 return QualType();
947
948 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
949 if (!Name && T->getIdentifier())
950 return QualType();
951
952 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
953 ? QualType()
954 : Importer.Import(T->getCanonicalTypeInternal());
955 if (!Canon.isNull())
956 Canon = Canon.getCanonicalType();
957
958 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
959 Name, Canon);
960}
961
John McCall424cec92011-01-19 06:33:43 +0000962QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000963 auto *Class =
964 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000965 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000966 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000967
John McCall8b07ec22010-05-15 11:32:37 +0000968 return Importer.getToContext().getObjCInterfaceType(Class);
969}
970
John McCall424cec92011-01-19 06:33:43 +0000971QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000972 QualType ToBaseType = Importer.Import(T->getBaseType());
973 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000974 return {};
John McCall8b07ec22010-05-15 11:32:37 +0000975
Douglas Gregore9d95f12015-07-07 03:57:35 +0000976 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000977 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000978 QualType ImportedTypeArg = Importer.Import(TypeArg);
979 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000980 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +0000981
982 TypeArgs.push_back(ImportedTypeArg);
983 }
984
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000985 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000986 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000987 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000988 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000989 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000990 Protocols.push_back(Protocol);
991 }
992
Douglas Gregore9d95f12015-07-07 03:57:35 +0000993 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000994 Protocols,
995 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000996}
997
John McCall424cec92011-01-19 06:33:43 +0000998QualType
999ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001000 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1001 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001002 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001003
John McCall8b07ec22010-05-15 11:32:37 +00001004 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001005}
1006
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001007//----------------------------------------------------------------------------
1008// Import Declarations
1009//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001010bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1011 DeclContext *&LexicalDC,
1012 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001013 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001014 SourceLocation &Loc) {
1015 // Import the context of this declaration.
1016 DC = Importer.ImportContext(D->getDeclContext());
1017 if (!DC)
1018 return true;
1019
1020 LexicalDC = DC;
1021 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1022 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1023 if (!LexicalDC)
1024 return true;
1025 }
1026
1027 // Import the name of this declaration.
1028 Name = Importer.Import(D->getDeclName());
1029 if (D->getDeclName() && !Name)
1030 return true;
1031
1032 // Import the location of this declaration.
1033 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001034 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001035 return false;
1036}
1037
Douglas Gregord451ea92011-07-29 23:31:30 +00001038void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1039 if (!FromD)
1040 return;
1041
1042 if (!ToD) {
1043 ToD = Importer.Import(FromD);
1044 if (!ToD)
1045 return;
1046 }
1047
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001048 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1049 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001050 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001051 ImportDefinition(FromRecord, ToRecord);
1052 }
1053 }
1054 return;
1055 }
1056
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001057 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1058 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001059 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1060 ImportDefinition(FromEnum, ToEnum);
1061 }
1062 }
1063 return;
1064 }
1065}
1066
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001067void
1068ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1069 DeclarationNameInfo& To) {
1070 // NOTE: To.Name and To.Loc are already imported.
1071 // We only have to import To.LocInfo.
1072 switch (To.getName().getNameKind()) {
1073 case DeclarationName::Identifier:
1074 case DeclarationName::ObjCZeroArgSelector:
1075 case DeclarationName::ObjCOneArgSelector:
1076 case DeclarationName::ObjCMultiArgSelector:
1077 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001078 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001079 return;
1080
1081 case DeclarationName::CXXOperatorName: {
1082 SourceRange Range = From.getCXXOperatorNameRange();
1083 To.setCXXOperatorNameRange(Importer.Import(Range));
1084 return;
1085 }
1086 case DeclarationName::CXXLiteralOperatorName: {
1087 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1088 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1089 return;
1090 }
1091 case DeclarationName::CXXConstructorName:
1092 case DeclarationName::CXXDestructorName:
1093 case DeclarationName::CXXConversionFunctionName: {
1094 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1095 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1096 return;
1097 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001098 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001099 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001100}
1101
Douglas Gregor2e15c842012-02-01 21:00:38 +00001102void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001103 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001104 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001105 return;
1106 }
1107
Aaron Ballman629afae2014-03-07 19:56:05 +00001108 for (auto *From : FromDC->decls())
1109 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001110}
1111
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001112static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1113 ASTImporter &Importer) {
1114 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1115 auto *ToTypedef =
1116 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1117 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1118
1119 To->setTypedefNameForAnonDecl(ToTypedef);
1120 }
1121}
1122
Douglas Gregord451ea92011-07-29 23:31:30 +00001123bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001124 ImportDefinitionKind Kind) {
1125 if (To->getDefinition() || To->isBeingDefined()) {
1126 if (Kind == IDK_Everything)
1127 ImportDeclContext(From, /*ForceImport=*/true);
1128
Douglas Gregore2e50d332010-12-01 01:36:18 +00001129 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001130 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001131
1132 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001133
1134 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001135
1136 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001137 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1138 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001139
1140 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1141 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1142 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001143 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001144 ToData.Aggregate = FromData.Aggregate;
1145 ToData.PlainOldData = FromData.PlainOldData;
1146 ToData.Empty = FromData.Empty;
1147 ToData.Polymorphic = FromData.Polymorphic;
1148 ToData.Abstract = FromData.Abstract;
1149 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001150 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1151 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1152 ToData.HasBasesWithNonStaticDataMembers =
1153 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001154 ToData.HasPrivateFields = FromData.HasPrivateFields;
1155 ToData.HasProtectedFields = FromData.HasProtectedFields;
1156 ToData.HasPublicFields = FromData.HasPublicFields;
1157 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001158 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001159 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001160 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001161 ToData.HasUninitializedReferenceMember
1162 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001163 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001164 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1165 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001166 ToData.NeedOverloadResolutionForCopyConstructor
1167 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001168 ToData.NeedOverloadResolutionForMoveConstructor
1169 = FromData.NeedOverloadResolutionForMoveConstructor;
1170 ToData.NeedOverloadResolutionForMoveAssignment
1171 = FromData.NeedOverloadResolutionForMoveAssignment;
1172 ToData.NeedOverloadResolutionForDestructor
1173 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001174 ToData.DefaultedCopyConstructorIsDeleted
1175 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001176 ToData.DefaultedMoveConstructorIsDeleted
1177 = FromData.DefaultedMoveConstructorIsDeleted;
1178 ToData.DefaultedMoveAssignmentIsDeleted
1179 = FromData.DefaultedMoveAssignmentIsDeleted;
1180 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001181 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1182 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001183 ToData.HasConstexprNonCopyMoveConstructor
1184 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001185 ToData.HasDefaultedDefaultConstructor
1186 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001187 ToData.DefaultedDefaultConstructorIsConstexpr
1188 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001189 ToData.HasConstexprDefaultConstructor
1190 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001191 ToData.HasNonLiteralTypeFieldsOrBases
1192 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001193 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001194 ToData.UserProvidedDefaultConstructor
1195 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001196 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001197 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1198 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1199 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1200 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001201 ToData.ImplicitCopyAssignmentHasConstParam
1202 = FromData.ImplicitCopyAssignmentHasConstParam;
1203 ToData.HasDeclaredCopyConstructorWithConstParam
1204 = FromData.HasDeclaredCopyConstructorWithConstParam;
1205 ToData.HasDeclaredCopyAssignmentWithConstParam
1206 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001207
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001208 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001209 for (const auto &Base1 : FromCXX->bases()) {
1210 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001211 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001212 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001213
1214 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 if (Base1.isPackExpansion())
1216 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001217
1218 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001219 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001220
Douglas Gregore2e50d332010-12-01 01:36:18 +00001221 Bases.push_back(
1222 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001223 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1224 Base1.isVirtual(),
1225 Base1.isBaseOfClass(),
1226 Base1.getAccessSpecifierAsWritten(),
1227 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001228 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001229 }
1230 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001231 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001232 }
1233
Douglas Gregor2e15c842012-02-01 21:00:38 +00001234 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001235 ImportDeclContext(From, /*ForceImport=*/true);
1236
Douglas Gregore2e50d332010-12-01 01:36:18 +00001237 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001238 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001239}
1240
Larisse Voufo39a1e502013-08-06 01:03:05 +00001241bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1242 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001243 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001244 return false;
1245
1246 // FIXME: Can we really import any initializer? Alternatively, we could force
1247 // ourselves to import every declaration of a variable and then only use
1248 // getInit() here.
1249 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1250
1251 // FIXME: Other bits to merge?
1252
1253 return false;
1254}
1255
Douglas Gregord451ea92011-07-29 23:31:30 +00001256bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001257 ImportDefinitionKind Kind) {
1258 if (To->getDefinition() || To->isBeingDefined()) {
1259 if (Kind == IDK_Everything)
1260 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001261 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001262 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001263
1264 To->startDefinition();
1265
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001266 setTypedefNameForAnonDecl(From, To, Importer);
1267
Douglas Gregord451ea92011-07-29 23:31:30 +00001268 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1269 if (T.isNull())
1270 return true;
1271
1272 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1273 if (ToPromotionType.isNull())
1274 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001275
1276 if (shouldForceImportDeclContext(Kind))
1277 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001278
1279 // FIXME: we might need to merge the number of positive or negative bits
1280 // if the enumerator lists don't match.
1281 To->completeDefinition(T, ToPromotionType,
1282 From->getNumPositiveBits(),
1283 From->getNumNegativeBits());
1284 return false;
1285}
1286
Douglas Gregora082a492010-11-30 19:14:50 +00001287TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1288 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001289 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1290 if (ImportContainerChecked(*Params, ToParams))
1291 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001292
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001293 Expr *ToRequiresClause;
1294 if (Expr *const R = Params->getRequiresClause()) {
1295 ToRequiresClause = Importer.Import(R);
1296 if (!ToRequiresClause)
1297 return nullptr;
1298 } else {
1299 ToRequiresClause = nullptr;
1300 }
1301
Douglas Gregora082a492010-11-30 19:14:50 +00001302 return TemplateParameterList::Create(Importer.getToContext(),
1303 Importer.Import(Params->getTemplateLoc()),
1304 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001305 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001306 Importer.Import(Params->getRAngleLoc()),
1307 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001308}
1309
Douglas Gregore2e50d332010-12-01 01:36:18 +00001310TemplateArgument
1311ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1312 switch (From.getKind()) {
1313 case TemplateArgument::Null:
1314 return TemplateArgument();
1315
1316 case TemplateArgument::Type: {
1317 QualType ToType = Importer.Import(From.getAsType());
1318 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001319 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001320 return TemplateArgument(ToType);
1321 }
1322
1323 case TemplateArgument::Integral: {
1324 QualType ToType = Importer.Import(From.getIntegralType());
1325 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001326 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001327 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001328 }
1329
Eli Friedmanb826a002012-09-26 02:36:12 +00001330 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001331 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001332 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1333 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001334 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001335 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001336 }
1337
1338 case TemplateArgument::NullPtr: {
1339 QualType ToType = Importer.Import(From.getNullPtrType());
1340 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001341 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001342 return TemplateArgument(ToType, /*isNullPtr*/true);
1343 }
1344
Douglas Gregore2e50d332010-12-01 01:36:18 +00001345 case TemplateArgument::Template: {
1346 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1347 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001348 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001349
1350 return TemplateArgument(ToTemplate);
1351 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001352
1353 case TemplateArgument::TemplateExpansion: {
1354 TemplateName ToTemplate
1355 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1356 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001357 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001358
Douglas Gregore1d60df2011-01-14 23:41:42 +00001359 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001360 }
1361
Douglas Gregore2e50d332010-12-01 01:36:18 +00001362 case TemplateArgument::Expression:
1363 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1364 return TemplateArgument(ToExpr);
1365 return TemplateArgument();
1366
1367 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001368 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001369 ToPack.reserve(From.pack_size());
1370 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001371 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001372
1373 return TemplateArgument(
1374 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001375 }
1376 }
1377
1378 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001379}
1380
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001381Optional<TemplateArgumentLoc>
1382ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001383 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1384 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1385 TemplateArgumentLocInfo ToInfo;
1386 if (Arg.getKind() == TemplateArgument::Expression) {
1387 Expr *E = Importer.Import(FromInfo.getAsExpr());
1388 ToInfo = TemplateArgumentLocInfo(E);
1389 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001390 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001391 } else if (Arg.getKind() == TemplateArgument::Type) {
1392 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1393 ToInfo = TemplateArgumentLocInfo(TSI);
1394 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001395 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001396 } else {
1397 ToInfo = TemplateArgumentLocInfo(
1398 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1399 Importer.Import(FromInfo.getTemplateNameLoc()),
1400 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1401 }
1402 return TemplateArgumentLoc(Arg, ToInfo);
1403}
1404
Douglas Gregore2e50d332010-12-01 01:36:18 +00001405bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1406 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001407 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001408 for (unsigned I = 0; I != NumFromArgs; ++I) {
1409 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1410 if (To.isNull() && !FromArgs[I].isNull())
1411 return true;
1412
1413 ToArgs.push_back(To);
1414 }
1415
1416 return false;
1417}
1418
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001419// We cannot use Optional<> pattern here and below because
1420// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1421// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001422template <typename InContainerTy>
1423bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1424 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1425 for (const auto &FromLoc : Container) {
1426 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1427 ToTAInfo.addArgument(*ToLoc);
1428 else
1429 return true;
1430 }
1431 return false;
1432}
1433
Douglas Gregor5c73e912010-02-11 00:48:18 +00001434bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001435 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001436 // Eliminate a potential failure point where we attempt to re-import
1437 // something we're trying to import while completing ToRecord.
1438 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1439 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001440 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001441 if (ToOriginRecord)
1442 ToRecord = ToOriginRecord;
1443 }
1444
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001445 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001446 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001447 Importer.getNonEquivalentDecls(),
1448 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001449 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001450}
1451
Larisse Voufo39a1e502013-08-06 01:03:05 +00001452bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1453 bool Complain) {
1454 StructuralEquivalenceContext Ctx(
1455 Importer.getFromContext(), Importer.getToContext(),
1456 Importer.getNonEquivalentDecls(), false, Complain);
1457 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1458}
1459
Douglas Gregor98c10182010-02-12 22:17:39 +00001460bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001461 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001462 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001463 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001464 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001465}
1466
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001467bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1468 FunctionTemplateDecl *To) {
1469 StructuralEquivalenceContext Ctx(
1470 Importer.getFromContext(), Importer.getToContext(),
1471 Importer.getNonEquivalentDecls(), false, false);
1472 return Ctx.IsStructurallyEquivalent(From, To);
1473}
1474
Douglas Gregor91155082012-11-14 22:29:20 +00001475bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001476 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001477 const llvm::APSInt &FromVal = FromEC->getInitVal();
1478 const llvm::APSInt &ToVal = ToEC->getInitVal();
1479
1480 return FromVal.isSigned() == ToVal.isSigned() &&
1481 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1482 FromVal == ToVal;
1483}
1484
1485bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001486 ClassTemplateDecl *To) {
1487 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1488 Importer.getToContext(),
1489 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001490 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001491}
1492
Larisse Voufo39a1e502013-08-06 01:03:05 +00001493bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1494 VarTemplateDecl *To) {
1495 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1496 Importer.getToContext(),
1497 Importer.getNonEquivalentDecls());
1498 return Ctx.IsStructurallyEquivalent(From, To);
1499}
1500
Douglas Gregore4c83e42010-02-09 22:48:33 +00001501Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001502 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001503 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001504 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001505}
1506
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001507Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1508 // Import the context of this declaration.
1509 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1510 if (!DC)
1511 return nullptr;
1512
1513 DeclContext *LexicalDC = DC;
1514 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1515 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1516 if (!LexicalDC)
1517 return nullptr;
1518 }
1519
1520 // Import the location of this declaration.
1521 SourceLocation Loc = Importer.Import(D->getLocation());
1522
1523 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1524 ToD->setLexicalDeclContext(LexicalDC);
1525 Importer.Imported(D, ToD);
1526 LexicalDC->addDeclInternal(ToD);
1527 return ToD;
1528}
1529
Sean Callanan65198272011-11-17 23:20:56 +00001530Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1531 TranslationUnitDecl *ToD =
1532 Importer.getToContext().getTranslationUnitDecl();
1533
1534 Importer.Imported(D, ToD);
1535
1536 return ToD;
1537}
1538
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001539Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001540 SourceLocation Loc = Importer.Import(D->getLocation());
1541 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1542
1543 // Import the context of this declaration.
1544 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1545 if (!DC)
1546 return nullptr;
1547
1548 AccessSpecDecl *accessSpecDecl
1549 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1550 DC, Loc, ColonLoc);
1551
1552 if (!accessSpecDecl)
1553 return nullptr;
1554
1555 // Lexical DeclContext and Semantic DeclContext
1556 // is always the same for the accessSpec.
1557 accessSpecDecl->setLexicalDeclContext(DC);
1558 DC->addDeclInternal(accessSpecDecl);
1559
1560 return accessSpecDecl;
1561}
1562
Aleksei Sidorina693b372016-09-28 10:16:56 +00001563Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1564 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1565 if (!DC)
1566 return nullptr;
1567
1568 DeclContext *LexicalDC = DC;
1569
1570 // Import the location of this declaration.
1571 SourceLocation Loc = Importer.Import(D->getLocation());
1572
1573 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1574 if (!AssertExpr)
1575 return nullptr;
1576
1577 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001578 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001579 if (!ToMsg && FromMsg)
1580 return nullptr;
1581
1582 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1583 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1584 Importer.Import(D->getRParenLoc()), D->isFailed());
1585
1586 ToD->setLexicalDeclContext(LexicalDC);
1587 LexicalDC->addDeclInternal(ToD);
1588 Importer.Imported(D, ToD);
1589 return ToD;
1590}
1591
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001592Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1593 // Import the major distinguishing characteristics of this namespace.
1594 DeclContext *DC, *LexicalDC;
1595 DeclarationName Name;
1596 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001597 NamedDecl *ToD;
1598 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001599 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001600 if (ToD)
1601 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001602
1603 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001604 if (!Name) {
1605 // This is an anonymous namespace. Adopt an existing anonymous
1606 // namespace if we can.
1607 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001608 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001609 MergeWithNamespace = TU->getAnonymousNamespace();
1610 else
1611 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1612 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001613 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001614 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001615 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001616 for (auto *FoundDecl : FoundDecls) {
1617 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001618 continue;
1619
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001620 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001621 MergeWithNamespace = FoundNS;
1622 ConflictingDecls.clear();
1623 break;
1624 }
1625
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001626 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001627 }
1628
1629 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001630 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001631 ConflictingDecls.data(),
1632 ConflictingDecls.size());
1633 }
1634 }
1635
1636 // Create the "to" namespace, if needed.
1637 NamespaceDecl *ToNamespace = MergeWithNamespace;
1638 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001639 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001640 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001641 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001642 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001643 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001644 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001645 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001646
1647 // If this is an anonymous namespace, register it as the anonymous
1648 // namespace within its context.
1649 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001650 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001651 TU->setAnonymousNamespace(ToNamespace);
1652 else
1653 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1654 }
1655 }
1656 Importer.Imported(D, ToNamespace);
1657
1658 ImportDeclContext(D);
1659
1660 return ToNamespace;
1661}
1662
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001663Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1664 // Import the major distinguishing characteristics of this namespace.
1665 DeclContext *DC, *LexicalDC;
1666 DeclarationName Name;
1667 SourceLocation Loc;
1668 NamedDecl *LookupD;
1669 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1670 return nullptr;
1671 if (LookupD)
1672 return LookupD;
1673
1674 // NOTE: No conflict resolution is done for namespace aliases now.
1675
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001676 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1677 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001678 if (!TargetDecl)
1679 return nullptr;
1680
1681 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1682 if (!ToII)
1683 return nullptr;
1684
1685 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1686 if (D->getQualifierLoc() && !ToQLoc)
1687 return nullptr;
1688
1689 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1690 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1691 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1692 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1693
1694 ToD->setLexicalDeclContext(LexicalDC);
1695 Importer.Imported(D, ToD);
1696 LexicalDC->addDeclInternal(ToD);
1697
1698 return ToD;
1699}
1700
Richard Smithdda56e42011-04-15 14:24:37 +00001701Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001702 // Import the major distinguishing characteristics of this typedef.
1703 DeclContext *DC, *LexicalDC;
1704 DeclarationName Name;
1705 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001706 NamedDecl *ToD;
1707 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001708 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001709 if (ToD)
1710 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001711
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001712 // If this typedef is not in block scope, determine whether we've
1713 // seen a typedef with the same name (that we can merge with) or any
1714 // other entity by that name (which name lookup could conflict with).
1715 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001716 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001717 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001718 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001719 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001720 for (auto *FoundDecl : FoundDecls) {
1721 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001722 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001723 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001724 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1725 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001726 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001727 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001728
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001729 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001730 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001731
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001732 if (!ConflictingDecls.empty()) {
1733 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1734 ConflictingDecls.data(),
1735 ConflictingDecls.size());
1736 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001737 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001738 }
1739 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001740
Douglas Gregorb4964f72010-02-15 23:54:17 +00001741 // Import the underlying type of this typedef;
1742 QualType T = Importer.Import(D->getUnderlyingType());
1743 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001744 return nullptr;
1745
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001746 // Some nodes (like anonymous tags referred by typedefs) are allowed to
1747 // import their enclosing typedef directly. Check if this is the case.
1748 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
1749 return AlreadyImported;
1750
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001751 // Create the new typedef node.
1752 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001753 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001754 TypedefNameDecl *ToTypedef;
1755 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001756 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1757 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001758 else
Richard Smithdda56e42011-04-15 14:24:37 +00001759 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1760 StartL, Loc,
1761 Name.getAsIdentifierInfo(),
1762 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001763
Douglas Gregordd483172010-02-22 17:42:47 +00001764 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001765 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001766 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001767
1768 // Templated declarations should not appear in DeclContext.
1769 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1770 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1771 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001772
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001773 return ToTypedef;
1774}
1775
Richard Smithdda56e42011-04-15 14:24:37 +00001776Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1777 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1778}
1779
1780Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1781 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1782}
1783
Gabor Horvath7a91c082017-11-14 11:30:38 +00001784Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1785 // Import the major distinguishing characteristics of this typedef.
1786 DeclContext *DC, *LexicalDC;
1787 DeclarationName Name;
1788 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001789 NamedDecl *FoundD;
1790 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001791 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001792 if (FoundD)
1793 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001794
1795 // If this typedef is not in block scope, determine whether we've
1796 // seen a typedef with the same name (that we can merge with) or any
1797 // other entity by that name (which name lookup could conflict with).
1798 if (!DC->isFunctionOrMethod()) {
1799 SmallVector<NamedDecl *, 4> ConflictingDecls;
1800 unsigned IDNS = Decl::IDNS_Ordinary;
1801 SmallVector<NamedDecl *, 2> FoundDecls;
1802 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001803 for (auto *FoundDecl : FoundDecls) {
1804 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001805 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001806 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001807 return Importer.Imported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001808 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001809 }
1810
1811 if (!ConflictingDecls.empty()) {
1812 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1813 ConflictingDecls.data(),
1814 ConflictingDecls.size());
1815 if (!Name)
1816 return nullptr;
1817 }
1818 }
1819
1820 TemplateParameterList *Params = ImportTemplateParameterList(
1821 D->getTemplateParameters());
1822 if (!Params)
1823 return nullptr;
1824
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001825 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001826 Importer.Import(D->getTemplatedDecl()));
1827 if (!TemplDecl)
1828 return nullptr;
1829
1830 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1831 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1832
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001833 TemplDecl->setDescribedAliasTemplate(ToAlias);
1834
Gabor Horvath7a91c082017-11-14 11:30:38 +00001835 ToAlias->setAccess(D->getAccess());
1836 ToAlias->setLexicalDeclContext(LexicalDC);
1837 Importer.Imported(D, ToAlias);
1838 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001839 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001840}
1841
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001842Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1843 // Import the major distinguishing characteristics of this label.
1844 DeclContext *DC, *LexicalDC;
1845 DeclarationName Name;
1846 SourceLocation Loc;
1847 NamedDecl *ToD;
1848 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1849 return nullptr;
1850 if (ToD)
1851 return ToD;
1852
1853 assert(LexicalDC->isFunctionOrMethod());
1854
1855 LabelDecl *ToLabel = D->isGnuLocal()
1856 ? LabelDecl::Create(Importer.getToContext(),
1857 DC, Importer.Import(D->getLocation()),
1858 Name.getAsIdentifierInfo(),
1859 Importer.Import(D->getLocStart()))
1860 : LabelDecl::Create(Importer.getToContext(),
1861 DC, Importer.Import(D->getLocation()),
1862 Name.getAsIdentifierInfo());
1863 Importer.Imported(D, ToLabel);
1864
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001865 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001866 if (!Label)
1867 return nullptr;
1868
1869 ToLabel->setStmt(Label);
1870 ToLabel->setLexicalDeclContext(LexicalDC);
1871 LexicalDC->addDeclInternal(ToLabel);
1872 return ToLabel;
1873}
1874
Douglas Gregor98c10182010-02-12 22:17:39 +00001875Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1876 // Import the major distinguishing characteristics of this enum.
1877 DeclContext *DC, *LexicalDC;
1878 DeclarationName Name;
1879 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001880 NamedDecl *ToD;
1881 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001882 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001883 if (ToD)
1884 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001885
Douglas Gregor98c10182010-02-12 22:17:39 +00001886 // Figure out what enum name we're looking for.
1887 unsigned IDNS = Decl::IDNS_Tag;
1888 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001889 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1890 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001891 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001892 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001893 IDNS |= Decl::IDNS_Ordinary;
1894
1895 // We may already have an enum of the same name; try to find and match it.
1896 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001898 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001899 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001900 for (auto *FoundDecl : FoundDecls) {
1901 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001902 continue;
1903
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001904 Decl *Found = FoundDecl;
1905 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1906 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00001907 Found = Tag->getDecl();
1908 }
1909
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001910 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001911 if (IsStructuralMatch(D, FoundEnum))
1912 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001913 }
1914
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001915 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00001916 }
1917
1918 if (!ConflictingDecls.empty()) {
1919 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1920 ConflictingDecls.data(),
1921 ConflictingDecls.size());
1922 }
1923 }
1924
1925 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001926 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1927 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001928 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001929 D->isScoped(), D->isScopedUsingClassTag(),
1930 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001931 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001932 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001933 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001934 D2->setLexicalDeclContext(LexicalDC);
1935 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001936 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001937
1938 // Import the integer type.
1939 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1940 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001941 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001942 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001943
1944 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001945 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001946 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001947
Douglas Gregor3996e242010-02-15 22:01:00 +00001948 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001949}
1950
Douglas Gregor5c73e912010-02-11 00:48:18 +00001951Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1952 // If this record has a definition in the translation unit we're coming from,
1953 // but this particular declaration is not that definition, import the
1954 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001955 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001956 if (Definition && Definition != D) {
1957 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001958 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001959 return nullptr;
1960
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001961 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001962 }
1963
1964 // Import the major distinguishing characteristics of this record.
1965 DeclContext *DC, *LexicalDC;
1966 DeclarationName Name;
1967 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001968 NamedDecl *ToD;
1969 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001970 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001971 if (ToD)
1972 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001973
Douglas Gregor5c73e912010-02-11 00:48:18 +00001974 // Figure out what structure name we're looking for.
1975 unsigned IDNS = Decl::IDNS_Tag;
1976 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001977 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1978 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001979 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001980 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001981 IDNS |= Decl::IDNS_Ordinary;
1982
1983 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001984 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001985 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001986 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001987 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001988 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001989 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001990
1991 if (!FoundDecls.empty()) {
1992 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1993 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1994 D->getASTContext().getExternalSource()->CompleteType(D);
1995 }
1996
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001997 for (auto *FoundDecl : FoundDecls) {
1998 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001999 continue;
2000
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002001 Decl *Found = FoundDecl;
2002 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2003 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002004 Found = Tag->getDecl();
2005 }
2006
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002007 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002008 if (!SearchName) {
2009 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002010 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002011 if (Optional<unsigned> Index1 =
2012 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
2013 D)) {
2014 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00002015 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002016 if (*Index1 != *Index2)
2017 continue;
2018 }
2019 }
2020 }
2021
Sean Callanan9092d472017-05-13 00:46:33 +00002022 PrevDecl = FoundRecord;
2023
Douglas Gregor25791052010-02-12 00:09:27 +00002024 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002025 if ((SearchName && !D->isCompleteDefinition())
2026 || (D->isCompleteDefinition() &&
2027 D->isAnonymousStructOrUnion()
2028 == FoundDef->isAnonymousStructOrUnion() &&
2029 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002030 // The record types structurally match, or the "from" translation
2031 // unit only had a forward declaration anyway; call it the same
2032 // function.
2033 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002034 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002035 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002036 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002037 // We have a forward declaration of this type, so adopt that forward
2038 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002039
2040 // If one or both can be completed from external storage then try one
2041 // last time to complete and compare them before doing this.
2042
2043 if (FoundRecord->hasExternalLexicalStorage() &&
2044 !FoundRecord->isCompleteDefinition())
2045 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2046 if (D->hasExternalLexicalStorage())
2047 D->getASTContext().getExternalSource()->CompleteType(D);
2048
2049 if (FoundRecord->isCompleteDefinition() &&
2050 D->isCompleteDefinition() &&
2051 !IsStructuralMatch(D, FoundRecord))
2052 continue;
2053
Douglas Gregor25791052010-02-12 00:09:27 +00002054 AdoptDecl = FoundRecord;
2055 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002056 } else if (!SearchName) {
2057 continue;
2058 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002059 }
2060
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002061 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002062 }
2063
Douglas Gregordd6006f2012-07-17 21:16:27 +00002064 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002065 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2066 ConflictingDecls.data(),
2067 ConflictingDecls.size());
2068 }
2069 }
2070
2071 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002072 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002073 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002074 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002075 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002076 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002077 if (DCXX->isLambda()) {
2078 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2079 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2080 DC, TInfo, Loc,
2081 DCXX->isDependentLambda(),
2082 DCXX->isGenericLambda(),
2083 DCXX->getLambdaCaptureDefault());
2084 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2085 if (DCXX->getLambdaContextDecl() && !CDecl)
2086 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002087 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002088 } else if (DCXX->isInjectedClassName()) {
2089 // We have to be careful to do a similar dance to the one in
2090 // Sema::ActOnStartCXXMemberDeclarations
2091 CXXRecordDecl *const PrevDecl = nullptr;
2092 const bool DelayTypeCreation = true;
2093 D2CXX = CXXRecordDecl::Create(
2094 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2095 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2096 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002097 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002098 } else {
2099 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2100 D->getTagKind(),
2101 DC, StartLoc, Loc,
2102 Name.getAsIdentifierInfo());
2103 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002104 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002105 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002106 D2->setLexicalDeclContext(LexicalDC);
2107 if (!DCXX->getDescribedClassTemplate())
2108 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002109
2110 Importer.Imported(D, D2);
2111
2112 if (ClassTemplateDecl *FromDescribed =
2113 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002114 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2115 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002116 if (!ToDescribed)
2117 return nullptr;
2118 D2CXX->setDescribedClassTemplate(ToDescribed);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002119 } else if (MemberSpecializationInfo *MemberInfo =
2120 DCXX->getMemberSpecializationInfo()) {
2121 TemplateSpecializationKind SK =
2122 MemberInfo->getTemplateSpecializationKind();
2123 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002124 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002125 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2126 if (FromInst && !ToInst)
2127 return nullptr;
2128 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2129 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2130 Importer.Import(MemberInfo->getPointOfInstantiation()));
2131 }
Douglas Gregor25791052010-02-12 00:09:27 +00002132 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002133 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002134 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002135 D2->setLexicalDeclContext(LexicalDC);
2136 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002137 }
Douglas Gregor14454802011-02-25 02:25:35 +00002138
2139 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002140 if (D->isAnonymousStructOrUnion())
2141 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002142 if (PrevDecl) {
2143 // FIXME: do this for all Redeclarables, not just RecordDecls.
2144 D2->setPreviousDecl(PrevDecl);
2145 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002146 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002147
Douglas Gregor3996e242010-02-15 22:01:00 +00002148 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002149
Douglas Gregor95d82832012-01-24 18:36:04 +00002150 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002151 return nullptr;
2152
Douglas Gregor3996e242010-02-15 22:01:00 +00002153 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002154}
2155
Douglas Gregor98c10182010-02-12 22:17:39 +00002156Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2157 // Import the major distinguishing characteristics of this enumerator.
2158 DeclContext *DC, *LexicalDC;
2159 DeclarationName Name;
2160 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002161 NamedDecl *ToD;
2162 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002163 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002164 if (ToD)
2165 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002166
2167 QualType T = Importer.Import(D->getType());
2168 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002169 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002170
Douglas Gregor98c10182010-02-12 22:17:39 +00002171 // Determine whether there are any other declarations with the same name and
2172 // in the same context.
2173 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002174 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002175 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002176 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002177 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002178 for (auto *FoundDecl : FoundDecls) {
2179 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002180 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002181
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002182 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002183 if (IsStructuralMatch(D, FoundEnumConstant))
2184 return Importer.Imported(D, FoundEnumConstant);
2185 }
2186
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002187 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002188 }
2189
2190 if (!ConflictingDecls.empty()) {
2191 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2192 ConflictingDecls.data(),
2193 ConflictingDecls.size());
2194 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002195 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002196 }
2197 }
2198
2199 Expr *Init = Importer.Import(D->getInitExpr());
2200 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002201 return nullptr;
2202
Douglas Gregor98c10182010-02-12 22:17:39 +00002203 EnumConstantDecl *ToEnumerator
2204 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2205 Name.getAsIdentifierInfo(), T,
2206 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002207 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002208 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002209 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002210 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002211 return ToEnumerator;
2212}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002213
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002214bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2215 FunctionDecl *ToFD) {
2216 switch (FromFD->getTemplatedKind()) {
2217 case FunctionDecl::TK_NonTemplate:
2218 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002219 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002220
2221 case FunctionDecl::TK_MemberSpecialization: {
2222 auto *InstFD = cast_or_null<FunctionDecl>(
2223 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2224 if (!InstFD)
2225 return true;
2226
2227 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2228 SourceLocation POI = Importer.Import(
2229 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2230 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2231 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002232 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002233 }
2234
2235 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2236 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2237 auto *Template = cast_or_null<FunctionTemplateDecl>(
2238 Importer.Import(FTSInfo->getTemplate()));
2239 if (!Template)
2240 return true;
2241 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2242
2243 // Import template arguments.
2244 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2245 SmallVector<TemplateArgument, 8> ToTemplArgs;
2246 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2247 ToTemplArgs))
2248 return true;
2249
2250 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2251 Importer.getToContext(), ToTemplArgs);
2252
2253 TemplateArgumentListInfo ToTAInfo;
2254 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002255 if (FromTAArgsAsWritten)
2256 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002257 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002258
2259 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2260
2261 ToFD->setFunctionTemplateSpecialization(
2262 Template, ToTAList, /* InsertPos= */ nullptr,
2263 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002264 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002265 }
2266
2267 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2268 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2269 UnresolvedSet<8> TemplDecls;
2270 unsigned NumTemplates = FromInfo->getNumTemplates();
2271 for (unsigned I = 0; I < NumTemplates; I++) {
2272 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2273 Importer.Import(FromInfo->getTemplate(I))))
2274 TemplDecls.addDecl(ToFTD);
2275 else
2276 return true;
2277 }
2278
2279 // Import TemplateArgumentListInfo.
2280 TemplateArgumentListInfo ToTAInfo;
2281 if (ImportTemplateArgumentListInfo(
2282 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2283 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2284 FromInfo->getNumTemplateArgs()),
2285 ToTAInfo))
2286 return true;
2287
2288 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2289 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002290 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002291 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002292 }
Sam McCallfdc32072018-01-26 12:06:44 +00002293 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002294}
2295
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002296Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2297 // Import the major distinguishing characteristics of this function.
2298 DeclContext *DC, *LexicalDC;
2299 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002300 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002301 NamedDecl *ToD;
2302 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002303 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002304 if (ToD)
2305 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002306
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002307 const FunctionDecl *FoundWithoutBody = nullptr;
2308
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002309 // Try to find a function in our own ("to") context with the same name, same
2310 // type, and in the same context as the function we're importing.
2311 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002312 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002313 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002314 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002315 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002316 for (auto *FoundDecl : FoundDecls) {
2317 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002318 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002319
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002320 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002321 if (FoundFunction->hasExternalFormalLinkage() &&
2322 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002323 if (Importer.IsStructurallyEquivalent(D->getType(),
2324 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002325 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002326 const FunctionDecl *FromBodyDecl = nullptr;
2327 D->hasBody(FromBodyDecl);
2328 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2329 // This function is needed to merge completely.
2330 FoundWithoutBody = FoundFunction;
2331 break;
2332 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002333 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002334 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002335
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002336 // FIXME: Check for overloading more carefully, e.g., by boosting
2337 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002338
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002339 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002340 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002341 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002342
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002343 // Complain about inconsistent function types.
2344 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002345 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002346 Importer.ToDiag(FoundFunction->getLocation(),
2347 diag::note_odr_value_here)
2348 << FoundFunction->getType();
2349 }
2350 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002351
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002352 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002353 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002354
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002355 if (!ConflictingDecls.empty()) {
2356 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2357 ConflictingDecls.data(),
2358 ConflictingDecls.size());
2359 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002360 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002361 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002362 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002363
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002364 DeclarationNameInfo NameInfo(Name, Loc);
2365 // Import additional name location/type info.
2366 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2367
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002368 QualType FromTy = D->getType();
2369 bool usedDifferentExceptionSpec = false;
2370
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002371 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002372 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2373 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2374 // FunctionDecl that we are importing the FunctionProtoType for.
2375 // To avoid an infinite recursion when importing, create the FunctionDecl
2376 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002377 if (FromEPI.ExceptionSpec.SourceDecl ||
2378 FromEPI.ExceptionSpec.SourceTemplate ||
2379 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002380 FunctionProtoType::ExtProtoInfo DefaultEPI;
2381 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002382 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002383 usedDifferentExceptionSpec = true;
2384 }
2385 }
2386
Douglas Gregorb4964f72010-02-15 23:54:17 +00002387 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002388 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002389 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002390 return nullptr;
2391
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002392 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002393 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002394 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002395 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002396 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002397 return nullptr;
2398
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002399 Parameters.push_back(ToP);
2400 }
2401
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002402 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002403 if (D->getTypeSourceInfo() && !TInfo)
2404 return nullptr;
2405
2406 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002407 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002408 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002409 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002410 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2411 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002412 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002413 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002414 FromConstructor->isExplicit(),
2415 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002416 D->isImplicit(),
2417 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002418 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2419 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002420 for (auto *I : FromConstructor->inits()) {
2421 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002422 if (!ToI && I)
2423 return nullptr;
2424 CtorInitializers.push_back(ToI);
2425 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002426 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002427 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2428 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002429 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002430 ToCtor->setCtorInitializers(Memory);
2431 ToCtor->setNumCtorInitializers(NumInitializers);
2432 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002433 } else if (isa<CXXDestructorDecl>(D)) {
2434 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2435 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002436 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002437 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002438 D->isInlineSpecified(),
2439 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002440 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002441 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2442 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002443 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002444 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002445 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002446 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002447 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002448 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002449 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002450 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2451 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002452 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002453 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002454 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002455 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002456 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002457 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002458 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002459 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002460 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002461 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002462 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002463 D->hasWrittenPrototype(),
2464 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002465 }
John McCall3e11ebe2010-03-15 10:12:16 +00002466
2467 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002468 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002469 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002470 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002471 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2472 ToFunction->setTrivial(D->isTrivial());
2473 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002474 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002475
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002476 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002477 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002478 Param->setOwningFunction(ToFunction);
2479 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002480 }
David Blaikie9c70e042011-09-21 18:16:56 +00002481 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002482
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002483 if (FoundWithoutBody) {
2484 auto *Recent = const_cast<FunctionDecl *>(
2485 FoundWithoutBody->getMostRecentDecl());
2486 ToFunction->setPreviousDecl(Recent);
2487 }
2488
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002489 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2490 // params it refers to.
2491 if (TInfo) {
2492 if (auto ProtoLoc =
2493 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2494 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2495 ProtoLoc.setParam(I, Parameters[I]);
2496 }
2497 }
2498
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002499 if (usedDifferentExceptionSpec) {
2500 // Update FunctionProtoType::ExtProtoInfo.
2501 QualType T = Importer.Import(D->getType());
2502 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002503 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002504 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002505 }
2506
Sean Callanan59721b32015-04-28 18:41:46 +00002507 // Import the body, if any.
2508 if (Stmt *FromBody = D->getBody()) {
2509 if (Stmt *ToBody = Importer.Import(FromBody)) {
2510 ToFunction->setBody(ToBody);
2511 }
2512 }
2513
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002514 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002515
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002516 // If it is a template, import all related things.
2517 if (ImportTemplateInformation(D, ToFunction))
2518 return nullptr;
2519
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002520 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002521 // NOTE: If the function is templated declaration, it should be not added into
2522 // LexicalDC. But described template is imported during import of
2523 // FunctionTemplateDecl (it happens later). So, we use source declaration
2524 // to determine if we should add the result function.
2525 if (!D->getDescribedFunctionTemplate())
2526 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002527
Lang Hames19e07e12017-06-20 21:06:00 +00002528 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2529 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2530
Douglas Gregor43f54792010-02-17 02:12:47 +00002531 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002532}
2533
Douglas Gregor00eace12010-02-21 18:29:16 +00002534Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2535 return VisitFunctionDecl(D);
2536}
2537
2538Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2539 return VisitCXXMethodDecl(D);
2540}
2541
2542Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2543 return VisitCXXMethodDecl(D);
2544}
2545
2546Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2547 return VisitCXXMethodDecl(D);
2548}
2549
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002550static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002551 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002552 if (!Owner)
2553 return 0;
2554
2555 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002556 for (const auto *D : Owner->noload_decls()) {
2557 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002558 return Index;
2559
2560 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2561 ++Index;
2562 }
2563
2564 return Index;
2565}
2566
Douglas Gregor5c73e912010-02-11 00:48:18 +00002567Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2568 // Import the major distinguishing characteristics of a variable.
2569 DeclContext *DC, *LexicalDC;
2570 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002571 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002572 NamedDecl *ToD;
2573 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002574 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002575 if (ToD)
2576 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002577
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002578 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002579 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002580 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002581 for (auto *FoundDecl : FoundDecls) {
2582 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002583 // For anonymous fields, match up by index.
2584 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2585 continue;
2586
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002587 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002588 FoundField->getType())) {
2589 Importer.Imported(D, FoundField);
2590 return FoundField;
2591 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002592
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002593 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2594 << Name << D->getType() << FoundField->getType();
2595 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2596 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002597 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002598 }
2599 }
2600
Douglas Gregorb4964f72010-02-15 23:54:17 +00002601 // Import the type.
2602 QualType T = Importer.Import(D->getType());
2603 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002604 return nullptr;
2605
Douglas Gregor5c73e912010-02-11 00:48:18 +00002606 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2607 Expr *BitWidth = Importer.Import(D->getBitWidth());
2608 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002609 return nullptr;
2610
Abramo Bagnaradff19302011-03-08 08:55:46 +00002611 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2612 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002613 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002614 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002615 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002616 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002617 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002618 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002619 Expr *ToInitializer = Importer.Import(FromInitializer);
2620 if (ToInitializer)
2621 ToField->setInClassInitializer(ToInitializer);
2622 else
2623 return nullptr;
2624 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002625 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002626 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002627 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002628 return ToField;
2629}
2630
Francois Pichet783dd6e2010-11-21 06:08:52 +00002631Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2632 // Import the major distinguishing characteristics of a variable.
2633 DeclContext *DC, *LexicalDC;
2634 DeclarationName Name;
2635 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002636 NamedDecl *ToD;
2637 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002638 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002639 if (ToD)
2640 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002641
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002642 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002643 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002644 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002645 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002646 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002647 // For anonymous indirect fields, match up by index.
2648 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2649 continue;
2650
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002651 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002652 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002653 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002654 Importer.Imported(D, FoundField);
2655 return FoundField;
2656 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002657
2658 // If there are more anonymous fields to check, continue.
2659 if (!Name && I < N-1)
2660 continue;
2661
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002662 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2663 << Name << D->getType() << FoundField->getType();
2664 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2665 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002666 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002667 }
2668 }
2669
Francois Pichet783dd6e2010-11-21 06:08:52 +00002670 // Import the type.
2671 QualType T = Importer.Import(D->getType());
2672 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002673 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002674
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002675 auto **NamedChain =
2676 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002677
2678 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002679 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002680 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002681 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002682 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002683 NamedChain[i++] = cast<NamedDecl>(D);
2684 }
2685
2686 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002687 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002688 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002689
2690 for (const auto *Attr : D->attrs())
2691 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2692
Francois Pichet783dd6e2010-11-21 06:08:52 +00002693 ToIndirectField->setAccess(D->getAccess());
2694 ToIndirectField->setLexicalDeclContext(LexicalDC);
2695 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002696 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002697 return ToIndirectField;
2698}
2699
Aleksei Sidorina693b372016-09-28 10:16:56 +00002700Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2701 // Import the major distinguishing characteristics of a declaration.
2702 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2703 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2704 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2705 if (!DC || !LexicalDC)
2706 return nullptr;
2707
2708 // Determine whether we've already imported this decl.
2709 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2710 auto *RD = cast<CXXRecordDecl>(DC);
2711 FriendDecl *ImportedFriend = RD->getFirstFriend();
2712 StructuralEquivalenceContext Context(
2713 Importer.getFromContext(), Importer.getToContext(),
2714 Importer.getNonEquivalentDecls(), false, false);
2715
2716 while (ImportedFriend) {
2717 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2718 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2719 ImportedFriend->getFriendDecl()))
2720 return Importer.Imported(D, ImportedFriend);
2721
2722 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2723 if (Importer.IsStructurallyEquivalent(
2724 D->getFriendType()->getType(),
2725 ImportedFriend->getFriendType()->getType(), true))
2726 return Importer.Imported(D, ImportedFriend);
2727 }
2728 ImportedFriend = ImportedFriend->getNextFriend();
2729 }
2730
2731 // Not found. Create it.
2732 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002733 if (NamedDecl *FriendD = D->getFriendDecl()) {
2734 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2735 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2736 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2737 ToFriendD->setObjectOfFriendDecl(false);
2738
2739 ToFU = ToFriendD;
2740 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002741 ToFU = Importer.Import(D->getFriendType());
2742 if (!ToFU)
2743 return nullptr;
2744
2745 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002746 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002747 for (unsigned I = 0; I < D->NumTPLists; I++) {
2748 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2749 if (!List)
2750 return nullptr;
2751 ToTPLists[I] = List;
2752 }
2753
2754 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2755 Importer.Import(D->getLocation()),
2756 ToFU, Importer.Import(D->getFriendLoc()),
2757 ToTPLists);
2758
2759 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002760
2761 FrD->setAccess(D->getAccess());
2762 FrD->setLexicalDeclContext(LexicalDC);
2763 LexicalDC->addDeclInternal(FrD);
2764 return FrD;
2765}
2766
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002767Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2768 // Import the major distinguishing characteristics of an ivar.
2769 DeclContext *DC, *LexicalDC;
2770 DeclarationName Name;
2771 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002772 NamedDecl *ToD;
2773 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002774 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002775 if (ToD)
2776 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002777
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002778 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002779 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002780 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002781 for (auto *FoundDecl : FoundDecls) {
2782 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002783 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002784 FoundIvar->getType())) {
2785 Importer.Imported(D, FoundIvar);
2786 return FoundIvar;
2787 }
2788
2789 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2790 << Name << D->getType() << FoundIvar->getType();
2791 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2792 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002793 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002794 }
2795 }
2796
2797 // Import the type.
2798 QualType T = Importer.Import(D->getType());
2799 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002800 return nullptr;
2801
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002802 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2803 Expr *BitWidth = Importer.Import(D->getBitWidth());
2804 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002805 return nullptr;
2806
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002807 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2808 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002809 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002810 Loc, Name.getAsIdentifierInfo(),
2811 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002812 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002813 ToIvar->setLexicalDeclContext(LexicalDC);
2814 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002815 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002816 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002817}
2818
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002819Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2820 // Import the major distinguishing characteristics of a variable.
2821 DeclContext *DC, *LexicalDC;
2822 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002823 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002824 NamedDecl *ToD;
2825 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002826 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002827 if (ToD)
2828 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002829
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002830 // Try to find a variable in our own ("to") context with the same name and
2831 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002832 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002833 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002834 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002835 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002836 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002837 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002838 for (auto *FoundDecl : FoundDecls) {
2839 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002840 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002841
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002842 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002843 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002844 if (FoundVar->hasExternalFormalLinkage() &&
2845 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002846 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002847 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002848 MergeWithVar = FoundVar;
2849 break;
2850 }
2851
Douglas Gregor56521c52010-02-12 17:23:39 +00002852 const ArrayType *FoundArray
2853 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2854 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002855 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002856 if (FoundArray && TArray) {
2857 if (isa<IncompleteArrayType>(FoundArray) &&
2858 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002859 // Import the type.
2860 QualType T = Importer.Import(D->getType());
2861 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002862 return nullptr;
2863
Douglas Gregor56521c52010-02-12 17:23:39 +00002864 FoundVar->setType(T);
2865 MergeWithVar = FoundVar;
2866 break;
2867 } else if (isa<IncompleteArrayType>(TArray) &&
2868 isa<ConstantArrayType>(FoundArray)) {
2869 MergeWithVar = FoundVar;
2870 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002871 }
2872 }
2873
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002874 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002875 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002876 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2877 << FoundVar->getType();
2878 }
2879 }
2880
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002881 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002882 }
2883
2884 if (MergeWithVar) {
2885 // An equivalent variable with external linkage has been found. Link
2886 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002887 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002888
2889 if (VarDecl *DDef = D->getDefinition()) {
2890 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2891 Importer.ToDiag(ExistingDef->getLocation(),
2892 diag::err_odr_variable_multiple_def)
2893 << Name;
2894 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2895 } else {
2896 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002897 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002898 if (DDef->isInitKnownICE()) {
2899 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2900 Eval->CheckedICE = true;
2901 Eval->IsICE = DDef->isInitICE();
2902 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002903 }
2904 }
2905
2906 return MergeWithVar;
2907 }
2908
2909 if (!ConflictingDecls.empty()) {
2910 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2911 ConflictingDecls.data(),
2912 ConflictingDecls.size());
2913 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002914 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002915 }
2916 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002917
Douglas Gregorb4964f72010-02-15 23:54:17 +00002918 // Import the type.
2919 QualType T = Importer.Import(D->getType());
2920 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002921 return nullptr;
2922
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002923 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002924 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002925 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2926 Importer.Import(D->getInnerLocStart()),
2927 Loc, Name.getAsIdentifierInfo(),
2928 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002929 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002930 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002931 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002932 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002933 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002934
2935 // Templated declarations should never appear in the enclosing DeclContext.
2936 if (!D->getDescribedVarTemplate())
2937 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002938
Sean Callanan59721b32015-04-28 18:41:46 +00002939 if (!D->isFileVarDecl() &&
2940 D->isUsed())
2941 ToVar->setIsUsed();
2942
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002943 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002944 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002945 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002946
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002947 if (D->isConstexpr())
2948 ToVar->setConstexpr(true);
2949
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002950 return ToVar;
2951}
2952
Douglas Gregor8b228d72010-02-17 21:22:52 +00002953Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2954 // Parameters are created in the translation unit's context, then moved
2955 // into the function declaration's context afterward.
2956 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2957
2958 // Import the name of this declaration.
2959 DeclarationName Name = Importer.Import(D->getDeclName());
2960 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002961 return nullptr;
2962
Douglas Gregor8b228d72010-02-17 21:22:52 +00002963 // Import the location of this declaration.
2964 SourceLocation Loc = Importer.Import(D->getLocation());
2965
2966 // Import the parameter's type.
2967 QualType T = Importer.Import(D->getType());
2968 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002969 return nullptr;
2970
Douglas Gregor8b228d72010-02-17 21:22:52 +00002971 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002972 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2973 Name.getAsIdentifierInfo(), T,
2974 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002975 return Importer.Imported(D, ToParm);
2976}
2977
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2979 // Parameters are created in the translation unit's context, then moved
2980 // into the function declaration's context afterward.
2981 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2982
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002983 // Import the name of this declaration.
2984 DeclarationName Name = Importer.Import(D->getDeclName());
2985 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002986 return nullptr;
2987
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002988 // Import the location of this declaration.
2989 SourceLocation Loc = Importer.Import(D->getLocation());
2990
2991 // Import the parameter's type.
2992 QualType T = Importer.Import(D->getType());
2993 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002994 return nullptr;
2995
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002996 // Create the imported parameter.
2997 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2998 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002999 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003000 Loc, Name.getAsIdentifierInfo(),
3001 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003002 /*DefaultArg*/ nullptr);
3003
3004 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003005 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003006 ToParm->setKNRPromoted(D->isKNRPromoted());
3007
3008 Expr *ToDefArg = nullptr;
3009 Expr *FromDefArg = nullptr;
3010 if (D->hasUninstantiatedDefaultArg()) {
3011 FromDefArg = D->getUninstantiatedDefaultArg();
3012 ToDefArg = Importer.Import(FromDefArg);
3013 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3014 } else if (D->hasUnparsedDefaultArg()) {
3015 ToParm->setUnparsedDefaultArg();
3016 } else if (D->hasDefaultArg()) {
3017 FromDefArg = D->getDefaultArg();
3018 ToDefArg = Importer.Import(FromDefArg);
3019 ToParm->setDefaultArg(ToDefArg);
3020 }
3021 if (FromDefArg && !ToDefArg)
3022 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003023
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003024 if (D->isObjCMethodParameter()) {
3025 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3026 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3027 } else {
3028 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3029 D->getFunctionScopeIndex());
3030 }
3031
Sean Callanan59721b32015-04-28 18:41:46 +00003032 if (D->isUsed())
3033 ToParm->setIsUsed();
3034
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003035 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003036}
3037
Douglas Gregor43f54792010-02-17 02:12:47 +00003038Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3039 // Import the major distinguishing characteristics of a method.
3040 DeclContext *DC, *LexicalDC;
3041 DeclarationName Name;
3042 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003043 NamedDecl *ToD;
3044 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003045 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003046 if (ToD)
3047 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003048
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003049 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003050 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003051 for (auto *FoundDecl : FoundDecls) {
3052 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003053 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3054 continue;
3055
3056 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003057 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3058 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003060 << D->isInstanceMethod() << Name << D->getReturnType()
3061 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003062 Importer.ToDiag(FoundMethod->getLocation(),
3063 diag::note_odr_objc_method_here)
3064 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003065 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003066 }
3067
3068 // Check the number of parameters.
3069 if (D->param_size() != FoundMethod->param_size()) {
3070 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3071 << D->isInstanceMethod() << Name
3072 << D->param_size() << FoundMethod->param_size();
3073 Importer.ToDiag(FoundMethod->getLocation(),
3074 diag::note_odr_objc_method_here)
3075 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003076 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003077 }
3078
3079 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003080 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003081 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3082 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003083 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003084 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003085 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003086 diag::err_odr_objc_method_param_type_inconsistent)
3087 << D->isInstanceMethod() << Name
3088 << (*P)->getType() << (*FoundP)->getType();
3089 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3090 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003091 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003092 }
3093 }
3094
3095 // Check variadic/non-variadic.
3096 // Check the number of parameters.
3097 if (D->isVariadic() != FoundMethod->isVariadic()) {
3098 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3099 << D->isInstanceMethod() << Name;
3100 Importer.ToDiag(FoundMethod->getLocation(),
3101 diag::note_odr_objc_method_here)
3102 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003103 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003104 }
3105
3106 // FIXME: Any other bits we need to merge?
3107 return Importer.Imported(D, FoundMethod);
3108 }
3109 }
3110
3111 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003112 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003113 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003114 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003115
Alp Toker314cc812014-01-25 16:55:45 +00003116 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003117
Alp Toker314cc812014-01-25 16:55:45 +00003118 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3119 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3120 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3121 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3122 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003123
3124 // FIXME: When we decide to merge method definitions, we'll need to
3125 // deal with implicit parameters.
3126
3127 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003128 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003129 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003130 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003131 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003132 return nullptr;
3133
Douglas Gregor43f54792010-02-17 02:12:47 +00003134 ToParams.push_back(ToP);
3135 }
3136
3137 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003138 for (auto *ToParam : ToParams) {
3139 ToParam->setOwningFunction(ToMethod);
3140 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003141 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003142
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003143 SmallVector<SourceLocation, 12> SelLocs;
3144 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003145 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003146 Loc = Importer.Import(Loc);
3147
3148 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003149
3150 ToMethod->setLexicalDeclContext(LexicalDC);
3151 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003152 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003153 return ToMethod;
3154}
3155
Douglas Gregor85f3f952015-07-07 03:57:15 +00003156Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3157 // Import the major distinguishing characteristics of a category.
3158 DeclContext *DC, *LexicalDC;
3159 DeclarationName Name;
3160 SourceLocation Loc;
3161 NamedDecl *ToD;
3162 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3163 return nullptr;
3164 if (ToD)
3165 return ToD;
3166
3167 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3168 if (!BoundInfo)
3169 return nullptr;
3170
3171 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3172 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003173 D->getVariance(),
3174 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003175 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003176 Importer.Import(D->getLocation()),
3177 Name.getAsIdentifierInfo(),
3178 Importer.Import(D->getColonLoc()),
3179 BoundInfo);
3180 Importer.Imported(D, Result);
3181 Result->setLexicalDeclContext(LexicalDC);
3182 return Result;
3183}
3184
Douglas Gregor84c51c32010-02-18 01:47:50 +00003185Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3186 // Import the major distinguishing characteristics of a category.
3187 DeclContext *DC, *LexicalDC;
3188 DeclarationName Name;
3189 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003190 NamedDecl *ToD;
3191 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003192 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003193 if (ToD)
3194 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003195
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003196 auto *ToInterface =
3197 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003198 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003199 return nullptr;
3200
Douglas Gregor84c51c32010-02-18 01:47:50 +00003201 // Determine if we've already encountered this category.
3202 ObjCCategoryDecl *MergeWithCategory
3203 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3204 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3205 if (!ToCategory) {
3206 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003207 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003208 Loc,
3209 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003210 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003211 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003212 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003213 Importer.Import(D->getIvarLBraceLoc()),
3214 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003215 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003216 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003217 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003218 // Import the type parameter list after calling Imported, to avoid
3219 // loops when bringing in their DeclContext.
3220 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3221 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003222
Douglas Gregor84c51c32010-02-18 01:47:50 +00003223 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003224 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3225 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003226 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3227 = D->protocol_loc_begin();
3228 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3229 FromProtoEnd = D->protocol_end();
3230 FromProto != FromProtoEnd;
3231 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003232 auto *ToProto =
3233 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003234 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003235 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003236 Protocols.push_back(ToProto);
3237 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3238 }
3239
3240 // FIXME: If we're merging, make sure that the protocol list is the same.
3241 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3242 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003243 } else {
3244 Importer.Imported(D, ToCategory);
3245 }
3246
3247 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003248 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003249
3250 // If we have an implementation, import it as well.
3251 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003252 auto *Impl =
3253 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003254 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003255 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003256 return nullptr;
3257
Douglas Gregor84c51c32010-02-18 01:47:50 +00003258 ToCategory->setImplementation(Impl);
3259 }
3260
3261 return ToCategory;
3262}
3263
Douglas Gregor2aa53772012-01-24 17:42:07 +00003264bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3265 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003266 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003267 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003268 if (shouldForceImportDeclContext(Kind))
3269 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003270 return false;
3271 }
3272
3273 // Start the protocol definition
3274 To->startDefinition();
3275
3276 // Import protocols
3277 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3278 SmallVector<SourceLocation, 4> ProtocolLocs;
3279 ObjCProtocolDecl::protocol_loc_iterator
3280 FromProtoLoc = From->protocol_loc_begin();
3281 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3282 FromProtoEnd = From->protocol_end();
3283 FromProto != FromProtoEnd;
3284 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003285 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003286 if (!ToProto)
3287 return true;
3288 Protocols.push_back(ToProto);
3289 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3290 }
3291
3292 // FIXME: If we're merging, make sure that the protocol list is the same.
3293 To->setProtocolList(Protocols.data(), Protocols.size(),
3294 ProtocolLocs.data(), Importer.getToContext());
3295
Douglas Gregor2e15c842012-02-01 21:00:38 +00003296 if (shouldForceImportDeclContext(Kind)) {
3297 // Import all of the members of this protocol.
3298 ImportDeclContext(From, /*ForceImport=*/true);
3299 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003300 return false;
3301}
3302
Douglas Gregor98d156a2010-02-17 16:12:00 +00003303Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003304 // If this protocol has a definition in the translation unit we're coming
3305 // from, but this particular declaration is not that definition, import the
3306 // definition and map to that.
3307 ObjCProtocolDecl *Definition = D->getDefinition();
3308 if (Definition && Definition != D) {
3309 Decl *ImportedDef = Importer.Import(Definition);
3310 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003311 return nullptr;
3312
Douglas Gregor2aa53772012-01-24 17:42:07 +00003313 return Importer.Imported(D, ImportedDef);
3314 }
3315
Douglas Gregor84c51c32010-02-18 01:47:50 +00003316 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003317 DeclContext *DC, *LexicalDC;
3318 DeclarationName Name;
3319 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003320 NamedDecl *ToD;
3321 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003322 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003323 if (ToD)
3324 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003325
Craig Topper36250ad2014-05-12 05:36:57 +00003326 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003327 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003328 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003329 for (auto *FoundDecl : FoundDecls) {
3330 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003331 continue;
3332
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003333 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003334 break;
3335 }
3336
3337 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003338 if (!ToProto) {
3339 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3340 Name.getAsIdentifierInfo(), Loc,
3341 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003342 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003343 ToProto->setLexicalDeclContext(LexicalDC);
3344 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003345 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003346
3347 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003348
Douglas Gregor2aa53772012-01-24 17:42:07 +00003349 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
3351
Douglas Gregor98d156a2010-02-17 16:12:00 +00003352 return ToProto;
3353}
3354
Sean Callanan0aae0412014-12-10 00:00:37 +00003355Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3356 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3357 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3358
3359 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3360 SourceLocation LangLoc = Importer.Import(D->getLocation());
3361
3362 bool HasBraces = D->hasBraces();
3363
Sean Callananb12a8552014-12-10 21:22:20 +00003364 LinkageSpecDecl *ToLinkageSpec =
3365 LinkageSpecDecl::Create(Importer.getToContext(),
3366 DC,
3367 ExternLoc,
3368 LangLoc,
3369 D->getLanguage(),
3370 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003371
3372 if (HasBraces) {
3373 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3374 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3375 }
3376
3377 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3378 LexicalDC->addDeclInternal(ToLinkageSpec);
3379
3380 Importer.Imported(D, ToLinkageSpec);
3381
3382 return ToLinkageSpec;
3383}
3384
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003385Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3386 DeclContext *DC, *LexicalDC;
3387 DeclarationName Name;
3388 SourceLocation Loc;
3389 NamedDecl *ToD = nullptr;
3390 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3391 return nullptr;
3392 if (ToD)
3393 return ToD;
3394
3395 DeclarationNameInfo NameInfo(Name,
3396 Importer.Import(D->getNameInfo().getLoc()));
3397 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3398
3399 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3400 Importer.Import(D->getUsingLoc()),
3401 Importer.Import(D->getQualifierLoc()),
3402 NameInfo, D->hasTypename());
3403 ToUsing->setLexicalDeclContext(LexicalDC);
3404 LexicalDC->addDeclInternal(ToUsing);
3405 Importer.Imported(D, ToUsing);
3406
3407 if (NamedDecl *FromPattern =
3408 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003409 if (auto *ToPattern =
3410 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003411 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3412 else
3413 return nullptr;
3414 }
3415
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003416 for (auto *FromShadow : D->shadows()) {
3417 if (auto *ToShadow =
3418 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003419 ToUsing->addShadowDecl(ToShadow);
3420 else
3421 // FIXME: We return a nullptr here but the definition is already created
3422 // and available with lookups. How to fix this?..
3423 return nullptr;
3424 }
3425 return ToUsing;
3426}
3427
3428Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3429 DeclContext *DC, *LexicalDC;
3430 DeclarationName Name;
3431 SourceLocation Loc;
3432 NamedDecl *ToD = nullptr;
3433 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3434 return nullptr;
3435 if (ToD)
3436 return ToD;
3437
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003438 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3439 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003440 if (!ToUsing)
3441 return nullptr;
3442
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003443 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3444 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003445 if (!ToTarget)
3446 return nullptr;
3447
3448 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3449 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3450
3451 ToShadow->setLexicalDeclContext(LexicalDC);
3452 ToShadow->setAccess(D->getAccess());
3453 Importer.Imported(D, ToShadow);
3454
3455 if (UsingShadowDecl *FromPattern =
3456 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003457 if (auto *ToPattern =
3458 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003459 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3460 ToPattern);
3461 else
3462 // FIXME: We return a nullptr here but the definition is already created
3463 // and available with lookups. How to fix this?..
3464 return nullptr;
3465 }
3466
3467 LexicalDC->addDeclInternal(ToShadow);
3468
3469 return ToShadow;
3470}
3471
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003472Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3473 DeclContext *DC, *LexicalDC;
3474 DeclarationName Name;
3475 SourceLocation Loc;
3476 NamedDecl *ToD = nullptr;
3477 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3478 return nullptr;
3479 if (ToD)
3480 return ToD;
3481
3482 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3483 if (!ToComAncestor)
3484 return nullptr;
3485
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003486 auto *ToNominated = cast_or_null<NamespaceDecl>(
3487 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003488 if (!ToNominated)
3489 return nullptr;
3490
3491 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3492 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3493 Importer.Import(D->getNamespaceKeyLocation()),
3494 Importer.Import(D->getQualifierLoc()),
3495 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3496 ToUsingDir->setLexicalDeclContext(LexicalDC);
3497 LexicalDC->addDeclInternal(ToUsingDir);
3498 Importer.Imported(D, ToUsingDir);
3499
3500 return ToUsingDir;
3501}
3502
3503Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3504 UnresolvedUsingValueDecl *D) {
3505 DeclContext *DC, *LexicalDC;
3506 DeclarationName Name;
3507 SourceLocation Loc;
3508 NamedDecl *ToD = nullptr;
3509 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3510 return nullptr;
3511 if (ToD)
3512 return ToD;
3513
3514 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3515 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3516
3517 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3518 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3519 Importer.Import(D->getQualifierLoc()), NameInfo,
3520 Importer.Import(D->getEllipsisLoc()));
3521
3522 Importer.Imported(D, ToUsingValue);
3523 ToUsingValue->setAccess(D->getAccess());
3524 ToUsingValue->setLexicalDeclContext(LexicalDC);
3525 LexicalDC->addDeclInternal(ToUsingValue);
3526
3527 return ToUsingValue;
3528}
3529
3530Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3531 UnresolvedUsingTypenameDecl *D) {
3532 DeclContext *DC, *LexicalDC;
3533 DeclarationName Name;
3534 SourceLocation Loc;
3535 NamedDecl *ToD = nullptr;
3536 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3537 return nullptr;
3538 if (ToD)
3539 return ToD;
3540
3541 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3542 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3543 Importer.Import(D->getTypenameLoc()),
3544 Importer.Import(D->getQualifierLoc()), Loc, Name,
3545 Importer.Import(D->getEllipsisLoc()));
3546
3547 Importer.Imported(D, ToUsing);
3548 ToUsing->setAccess(D->getAccess());
3549 ToUsing->setLexicalDeclContext(LexicalDC);
3550 LexicalDC->addDeclInternal(ToUsing);
3551
3552 return ToUsing;
3553}
3554
Douglas Gregor2aa53772012-01-24 17:42:07 +00003555bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3556 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003557 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003558 if (To->getDefinition()) {
3559 // Check consistency of superclass.
3560 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3561 if (FromSuper) {
3562 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3563 if (!FromSuper)
3564 return true;
3565 }
3566
3567 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3568 if ((bool)FromSuper != (bool)ToSuper ||
3569 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3570 Importer.ToDiag(To->getLocation(),
3571 diag::err_odr_objc_superclass_inconsistent)
3572 << To->getDeclName();
3573 if (ToSuper)
3574 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3575 << To->getSuperClass()->getDeclName();
3576 else
3577 Importer.ToDiag(To->getLocation(),
3578 diag::note_odr_objc_missing_superclass);
3579 if (From->getSuperClass())
3580 Importer.FromDiag(From->getSuperClassLoc(),
3581 diag::note_odr_objc_superclass)
3582 << From->getSuperClass()->getDeclName();
3583 else
3584 Importer.FromDiag(From->getLocation(),
3585 diag::note_odr_objc_missing_superclass);
3586 }
3587
Douglas Gregor2e15c842012-02-01 21:00:38 +00003588 if (shouldForceImportDeclContext(Kind))
3589 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003590 return false;
3591 }
3592
3593 // Start the definition.
3594 To->startDefinition();
3595
3596 // If this class has a superclass, import it.
3597 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003598 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3599 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003600 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003601
3602 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003603 }
3604
3605 // Import protocols
3606 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3607 SmallVector<SourceLocation, 4> ProtocolLocs;
3608 ObjCInterfaceDecl::protocol_loc_iterator
3609 FromProtoLoc = From->protocol_loc_begin();
3610
3611 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3612 FromProtoEnd = From->protocol_end();
3613 FromProto != FromProtoEnd;
3614 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003615 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003616 if (!ToProto)
3617 return true;
3618 Protocols.push_back(ToProto);
3619 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3620 }
3621
3622 // FIXME: If we're merging, make sure that the protocol list is the same.
3623 To->setProtocolList(Protocols.data(), Protocols.size(),
3624 ProtocolLocs.data(), Importer.getToContext());
3625
3626 // Import categories. When the categories themselves are imported, they'll
3627 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003628 for (auto *Cat : From->known_categories())
3629 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003630
Douglas Gregor2aa53772012-01-24 17:42:07 +00003631 // If we have an @implementation, import it as well.
3632 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003633 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3634 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003635 if (!Impl)
3636 return true;
3637
3638 To->setImplementation(Impl);
3639 }
3640
Douglas Gregor2e15c842012-02-01 21:00:38 +00003641 if (shouldForceImportDeclContext(Kind)) {
3642 // Import all of the members of this class.
3643 ImportDeclContext(From, /*ForceImport=*/true);
3644 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003645 return false;
3646}
3647
Douglas Gregor85f3f952015-07-07 03:57:15 +00003648ObjCTypeParamList *
3649ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3650 if (!list)
3651 return nullptr;
3652
3653 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3654 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003655 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3656 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003657 if (!toTypeParam)
3658 return nullptr;
3659
3660 toTypeParams.push_back(toTypeParam);
3661 }
3662
3663 return ObjCTypeParamList::create(Importer.getToContext(),
3664 Importer.Import(list->getLAngleLoc()),
3665 toTypeParams,
3666 Importer.Import(list->getRAngleLoc()));
3667}
3668
Douglas Gregor45635322010-02-16 01:20:57 +00003669Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003670 // If this class has a definition in the translation unit we're coming from,
3671 // but this particular declaration is not that definition, import the
3672 // definition and map to that.
3673 ObjCInterfaceDecl *Definition = D->getDefinition();
3674 if (Definition && Definition != D) {
3675 Decl *ImportedDef = Importer.Import(Definition);
3676 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003677 return nullptr;
3678
Douglas Gregor2aa53772012-01-24 17:42:07 +00003679 return Importer.Imported(D, ImportedDef);
3680 }
3681
Douglas Gregor45635322010-02-16 01:20:57 +00003682 // Import the major distinguishing characteristics of an @interface.
3683 DeclContext *DC, *LexicalDC;
3684 DeclarationName Name;
3685 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003686 NamedDecl *ToD;
3687 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003688 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003689 if (ToD)
3690 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003691
Douglas Gregor2aa53772012-01-24 17:42:07 +00003692 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003693 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003694 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003695 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003696 for (auto *FoundDecl : FoundDecls) {
3697 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003698 continue;
3699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003700 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003701 break;
3702 }
3703
Douglas Gregor2aa53772012-01-24 17:42:07 +00003704 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003705 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003706 if (!ToIface) {
3707 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3708 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003709 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003710 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003711 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003712 D->isImplicitInterfaceDecl());
3713 ToIface->setLexicalDeclContext(LexicalDC);
3714 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003715 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003716 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003717 // Import the type parameter list after calling Imported, to avoid
3718 // loops when bringing in their DeclContext.
3719 ToIface->setTypeParamList(ImportObjCTypeParamList(
3720 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003721
Douglas Gregor2aa53772012-01-24 17:42:07 +00003722 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003723 return nullptr;
3724
Douglas Gregor98d156a2010-02-17 16:12:00 +00003725 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003726}
3727
Douglas Gregor4da9d682010-12-07 15:32:12 +00003728Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003729 auto *Category = cast_or_null<ObjCCategoryDecl>(
3730 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003731 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003732 return nullptr;
3733
Douglas Gregor4da9d682010-12-07 15:32:12 +00003734 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3735 if (!ToImpl) {
3736 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3737 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
3739
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003740 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003741 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003742 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003743 Category->getClassInterface(),
3744 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003745 Importer.Import(D->getAtStartLoc()),
3746 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003747
3748 DeclContext *LexicalDC = DC;
3749 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3750 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3751 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003752 return nullptr;
3753
Douglas Gregor4da9d682010-12-07 15:32:12 +00003754 ToImpl->setLexicalDeclContext(LexicalDC);
3755 }
3756
Sean Callanan95e74be2011-10-21 02:57:43 +00003757 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003758 Category->setImplementation(ToImpl);
3759 }
3760
3761 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003762 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003763 return ToImpl;
3764}
3765
Douglas Gregorda8025c2010-12-07 01:26:03 +00003766Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3767 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003768 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3769 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003770 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003771 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003772
3773 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003774 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003775 if (D->getSuperClass()) {
3776 Super = cast_or_null<ObjCInterfaceDecl>(
3777 Importer.Import(D->getSuperClass()));
3778 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003779 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003780 }
3781
3782 ObjCImplementationDecl *Impl = Iface->getImplementation();
3783 if (!Impl) {
3784 // We haven't imported an implementation yet. Create a new @implementation
3785 // now.
3786 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3787 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003788 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003789 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003790 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003791 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003792 Importer.Import(D->getIvarLBraceLoc()),
3793 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003794
3795 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3796 DeclContext *LexicalDC
3797 = Importer.ImportContext(D->getLexicalDeclContext());
3798 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003799 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003800 Impl->setLexicalDeclContext(LexicalDC);
3801 }
3802
3803 // Associate the implementation with the class it implements.
3804 Iface->setImplementation(Impl);
3805 Importer.Imported(D, Iface->getImplementation());
3806 } else {
3807 Importer.Imported(D, Iface->getImplementation());
3808
3809 // Verify that the existing @implementation has the same superclass.
3810 if ((Super && !Impl->getSuperClass()) ||
3811 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003812 (Super && Impl->getSuperClass() &&
3813 !declaresSameEntity(Super->getCanonicalDecl(),
3814 Impl->getSuperClass()))) {
3815 Importer.ToDiag(Impl->getLocation(),
3816 diag::err_odr_objc_superclass_inconsistent)
3817 << Iface->getDeclName();
3818 // FIXME: It would be nice to have the location of the superclass
3819 // below.
3820 if (Impl->getSuperClass())
3821 Importer.ToDiag(Impl->getLocation(),
3822 diag::note_odr_objc_superclass)
3823 << Impl->getSuperClass()->getDeclName();
3824 else
3825 Importer.ToDiag(Impl->getLocation(),
3826 diag::note_odr_objc_missing_superclass);
3827 if (D->getSuperClass())
3828 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003829 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003830 << D->getSuperClass()->getDeclName();
3831 else
3832 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003833 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003834 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003835 }
3836 }
3837
3838 // Import all of the members of this @implementation.
3839 ImportDeclContext(D);
3840
3841 return Impl;
3842}
3843
Douglas Gregora11c4582010-02-17 18:02:10 +00003844Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3845 // Import the major distinguishing characteristics of an @property.
3846 DeclContext *DC, *LexicalDC;
3847 DeclarationName Name;
3848 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003849 NamedDecl *ToD;
3850 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003851 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003852 if (ToD)
3853 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003854
3855 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003856 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003857 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003858 for (auto *FoundDecl : FoundDecls) {
3859 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003860 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003861 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003862 FoundProp->getType())) {
3863 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3864 << Name << D->getType() << FoundProp->getType();
3865 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3866 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003867 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003868 }
3869
3870 // FIXME: Check property attributes, getters, setters, etc.?
3871
3872 // Consider these properties to be equivalent.
3873 Importer.Imported(D, FoundProp);
3874 return FoundProp;
3875 }
3876 }
3877
3878 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003879 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3880 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003881 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003882
3883 // Create the new property.
3884 ObjCPropertyDecl *ToProperty
3885 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3886 Name.getAsIdentifierInfo(),
3887 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003888 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003889 Importer.Import(D->getType()),
3890 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003891 D->getPropertyImplementation());
3892 Importer.Imported(D, ToProperty);
3893 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003894 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003895
3896 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003897 ToProperty->setPropertyAttributesAsWritten(
3898 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003899 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3900 Importer.Import(D->getGetterNameLoc()));
3901 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3902 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003903 ToProperty->setGetterMethodDecl(
3904 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3905 ToProperty->setSetterMethodDecl(
3906 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3907 ToProperty->setPropertyIvarDecl(
3908 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3909 return ToProperty;
3910}
3911
Douglas Gregor14a49e22010-12-07 18:32:03 +00003912Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003913 auto *Property = cast_or_null<ObjCPropertyDecl>(
3914 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003915 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003916 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003917
3918 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3919 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003920 return nullptr;
3921
Douglas Gregor14a49e22010-12-07 18:32:03 +00003922 // Import the lexical declaration context.
3923 DeclContext *LexicalDC = DC;
3924 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3925 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3926 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003927 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003928 }
3929
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003930 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003931 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003932 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003933
3934 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003935 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003936 if (D->getPropertyIvarDecl()) {
3937 Ivar = cast_or_null<ObjCIvarDecl>(
3938 Importer.Import(D->getPropertyIvarDecl()));
3939 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003940 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003941 }
3942
3943 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003944 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3945 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003946 if (!ToImpl) {
3947 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3948 Importer.Import(D->getLocStart()),
3949 Importer.Import(D->getLocation()),
3950 Property,
3951 D->getPropertyImplementation(),
3952 Ivar,
3953 Importer.Import(D->getPropertyIvarDeclLoc()));
3954 ToImpl->setLexicalDeclContext(LexicalDC);
3955 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003956 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003957 } else {
3958 // Check that we have the same kind of property implementation (@synthesize
3959 // vs. @dynamic).
3960 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3961 Importer.ToDiag(ToImpl->getLocation(),
3962 diag::err_odr_objc_property_impl_kind_inconsistent)
3963 << Property->getDeclName()
3964 << (ToImpl->getPropertyImplementation()
3965 == ObjCPropertyImplDecl::Dynamic);
3966 Importer.FromDiag(D->getLocation(),
3967 diag::note_odr_objc_property_impl_kind)
3968 << D->getPropertyDecl()->getDeclName()
3969 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003970 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003971 }
3972
3973 // For @synthesize, check that we have the same
3974 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3975 Ivar != ToImpl->getPropertyIvarDecl()) {
3976 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3977 diag::err_odr_objc_synthesize_ivar_inconsistent)
3978 << Property->getDeclName()
3979 << ToImpl->getPropertyIvarDecl()->getDeclName()
3980 << Ivar->getDeclName();
3981 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3982 diag::note_odr_objc_synthesize_ivar_here)
3983 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003984 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003985 }
3986
3987 // Merge the existing implementation with the new implementation.
3988 Importer.Imported(D, ToImpl);
3989 }
3990
3991 return ToImpl;
3992}
3993
Douglas Gregora082a492010-11-30 19:14:50 +00003994Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3995 // For template arguments, we adopt the translation unit as our declaration
3996 // context. This context will be fixed when the actual template declaration
3997 // is created.
3998
3999 // FIXME: Import default argument.
4000 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4001 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004002 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004003 Importer.Import(D->getLocation()),
4004 D->getDepth(),
4005 D->getIndex(),
4006 Importer.Import(D->getIdentifier()),
4007 D->wasDeclaredWithTypename(),
4008 D->isParameterPack());
4009}
4010
4011Decl *
4012ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4013 // Import the name of this declaration.
4014 DeclarationName Name = Importer.Import(D->getDeclName());
4015 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004016 return nullptr;
4017
Douglas Gregora082a492010-11-30 19:14:50 +00004018 // Import the location of this declaration.
4019 SourceLocation Loc = Importer.Import(D->getLocation());
4020
4021 // Import the type of this declaration.
4022 QualType T = Importer.Import(D->getType());
4023 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004024 return nullptr;
4025
Douglas Gregora082a492010-11-30 19:14:50 +00004026 // Import type-source information.
4027 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4028 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004029 return nullptr;
4030
Douglas Gregora082a492010-11-30 19:14:50 +00004031 // FIXME: Import default argument.
4032
4033 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4034 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004035 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004036 Loc, D->getDepth(), D->getPosition(),
4037 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004038 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004039}
4040
4041Decl *
4042ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4043 // Import the name of this declaration.
4044 DeclarationName Name = Importer.Import(D->getDeclName());
4045 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004046 return nullptr;
4047
Douglas Gregora082a492010-11-30 19:14:50 +00004048 // Import the location of this declaration.
4049 SourceLocation Loc = Importer.Import(D->getLocation());
4050
4051 // Import template parameters.
4052 TemplateParameterList *TemplateParams
4053 = ImportTemplateParameterList(D->getTemplateParameters());
4054 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004055 return nullptr;
4056
Douglas Gregora082a492010-11-30 19:14:50 +00004057 // FIXME: Import default argument.
4058
4059 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4060 Importer.getToContext().getTranslationUnitDecl(),
4061 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004062 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004063 Name.getAsIdentifierInfo(),
4064 TemplateParams);
4065}
4066
4067Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4068 // If this record has a definition in the translation unit we're coming from,
4069 // but this particular declaration is not that definition, import the
4070 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004071 auto *Definition =
4072 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004073 if (Definition && Definition != D->getTemplatedDecl()) {
4074 Decl *ImportedDef
4075 = Importer.Import(Definition->getDescribedClassTemplate());
4076 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004077 return nullptr;
4078
Douglas Gregora082a492010-11-30 19:14:50 +00004079 return Importer.Imported(D, ImportedDef);
4080 }
4081
4082 // Import the major distinguishing characteristics of this class template.
4083 DeclContext *DC, *LexicalDC;
4084 DeclarationName Name;
4085 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004086 NamedDecl *ToD;
4087 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004088 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004089 if (ToD)
4090 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004091
Douglas Gregora082a492010-11-30 19:14:50 +00004092 // We may already have a template of the same name; try to find and match it.
4093 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004094 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004095 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004096 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004097 for (auto *FoundDecl : FoundDecls) {
4098 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004099 continue;
4100
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004101 Decl *Found = FoundDecl;
4102 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Douglas Gregora082a492010-11-30 19:14:50 +00004103 if (IsStructuralMatch(D, FoundTemplate)) {
4104 // The class templates structurally match; call it the same template.
4105 // FIXME: We may be filling in a forward declaration here. Handle
4106 // this case!
4107 Importer.Imported(D->getTemplatedDecl(),
4108 FoundTemplate->getTemplatedDecl());
4109 return Importer.Imported(D, FoundTemplate);
4110 }
4111 }
4112
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004113 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004114 }
4115
4116 if (!ConflictingDecls.empty()) {
4117 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4118 ConflictingDecls.data(),
4119 ConflictingDecls.size());
4120 }
4121
4122 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004123 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004124 }
4125
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004126 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4127
Douglas Gregora082a492010-11-30 19:14:50 +00004128 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004129 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4130 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004131 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004132 return nullptr;
4133
4134 // Resolve possible cyclic import.
4135 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4136 return AlreadyImported;
4137
Douglas Gregora082a492010-11-30 19:14:50 +00004138 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004139 TemplateParameterList *TemplateParams =
4140 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004141 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004142 return nullptr;
4143
Douglas Gregora082a492010-11-30 19:14:50 +00004144 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4145 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004146 ToTemplated);
4147 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004148
4149 D2->setAccess(D->getAccess());
4150 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004151 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004152
4153 // Note the relationship between the class templates.
4154 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004155 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004156
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004157 if (FromTemplated->isCompleteDefinition() &&
4158 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004159 // FIXME: Import definition!
4160 }
4161
4162 return D2;
4163}
4164
Douglas Gregore2e50d332010-12-01 01:36:18 +00004165Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4166 ClassTemplateSpecializationDecl *D) {
4167 // If this record has a definition in the translation unit we're coming from,
4168 // but this particular declaration is not that definition, import the
4169 // definition and map to that.
4170 TagDecl *Definition = D->getDefinition();
4171 if (Definition && Definition != D) {
4172 Decl *ImportedDef = Importer.Import(Definition);
4173 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004174 return nullptr;
4175
Douglas Gregore2e50d332010-12-01 01:36:18 +00004176 return Importer.Imported(D, ImportedDef);
4177 }
4178
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004179 auto *ClassTemplate =
4180 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004181 D->getSpecializedTemplate()));
4182 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004183 return nullptr;
4184
Douglas Gregore2e50d332010-12-01 01:36:18 +00004185 // Import the context of this declaration.
4186 DeclContext *DC = ClassTemplate->getDeclContext();
4187 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004188 return nullptr;
4189
Douglas Gregore2e50d332010-12-01 01:36:18 +00004190 DeclContext *LexicalDC = DC;
4191 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4192 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4193 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004194 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004195 }
4196
4197 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004198 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4199 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004200
4201 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004202 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004203 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4204 D->getTemplateArgs().size(),
4205 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004206 return nullptr;
4207
Douglas Gregore2e50d332010-12-01 01:36:18 +00004208 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004209 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004210 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004211 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004212 if (D2) {
4213 // We already have a class template specialization with these template
4214 // arguments.
4215
4216 // FIXME: Check for specialization vs. instantiation errors.
4217
4218 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004219 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004220 // The record types structurally match, or the "from" translation
4221 // unit only had a forward declaration anyway; call it the same
4222 // function.
4223 return Importer.Imported(D, FoundDef);
4224 }
4225 }
4226 } else {
4227 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004228 if (auto *PartialSpec =
4229 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004230 // Import TemplateArgumentListInfo
4231 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004232 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4233 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004234 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004235
4236 QualType CanonInjType = Importer.Import(
4237 PartialSpec->getInjectedSpecializationType());
4238 if (CanonInjType.isNull())
4239 return nullptr;
4240 CanonInjType = CanonInjType.getCanonicalType();
4241
4242 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4243 PartialSpec->getTemplateParameters());
4244 if (!ToTPList && PartialSpec->getTemplateParameters())
4245 return nullptr;
4246
4247 D2 = ClassTemplatePartialSpecializationDecl::Create(
4248 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4249 ToTPList, ClassTemplate,
4250 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4251 ToTAInfo, CanonInjType, nullptr);
4252
4253 } else {
4254 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4255 D->getTagKind(), DC,
4256 StartLoc, IdLoc,
4257 ClassTemplate,
4258 TemplateArgs,
4259 /*PrevDecl=*/nullptr);
4260 }
4261
Douglas Gregore2e50d332010-12-01 01:36:18 +00004262 D2->setSpecializationKind(D->getSpecializationKind());
4263
4264 // Add this specialization to the class template.
4265 ClassTemplate->AddSpecialization(D2, InsertPos);
4266
4267 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004268 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004269
4270 Importer.Imported(D, D2);
4271
4272 if (auto *TSI = D->getTypeAsWritten()) {
4273 TypeSourceInfo *TInfo = Importer.Import(TSI);
4274 if (!TInfo)
4275 return nullptr;
4276 D2->setTypeAsWritten(TInfo);
4277 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4278 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4279 }
4280
4281 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4282 if (POI.isValid())
4283 D2->setPointOfInstantiation(POI);
4284 else if (D->getPointOfInstantiation().isValid())
4285 return nullptr;
4286
4287 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4288
Douglas Gregore2e50d332010-12-01 01:36:18 +00004289 // Add the specialization to this context.
4290 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004291 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004292 }
4293 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004294 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004295 return nullptr;
4296
Douglas Gregore2e50d332010-12-01 01:36:18 +00004297 return D2;
4298}
4299
Larisse Voufo39a1e502013-08-06 01:03:05 +00004300Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4301 // If this variable has a definition in the translation unit we're coming
4302 // from,
4303 // but this particular declaration is not that definition, import the
4304 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004305 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004306 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4307 if (Definition && Definition != D->getTemplatedDecl()) {
4308 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4309 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004310 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004311
4312 return Importer.Imported(D, ImportedDef);
4313 }
4314
4315 // Import the major distinguishing characteristics of this variable template.
4316 DeclContext *DC, *LexicalDC;
4317 DeclarationName Name;
4318 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004319 NamedDecl *ToD;
4320 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004321 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004322 if (ToD)
4323 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004324
4325 // We may already have a template of the same name; try to find and match it.
4326 assert(!DC->isFunctionOrMethod() &&
4327 "Variable templates cannot be declared at function scope");
4328 SmallVector<NamedDecl *, 4> ConflictingDecls;
4329 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004330 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004331 for (auto *FoundDecl : FoundDecls) {
4332 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004333 continue;
4334
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004335 Decl *Found = FoundDecl;
4336 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004337 if (IsStructuralMatch(D, FoundTemplate)) {
4338 // The variable templates structurally match; call it the same template.
4339 Importer.Imported(D->getTemplatedDecl(),
4340 FoundTemplate->getTemplatedDecl());
4341 return Importer.Imported(D, FoundTemplate);
4342 }
4343 }
4344
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004345 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004346 }
4347
4348 if (!ConflictingDecls.empty()) {
4349 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4350 ConflictingDecls.data(),
4351 ConflictingDecls.size());
4352 }
4353
4354 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004355 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004356
4357 VarDecl *DTemplated = D->getTemplatedDecl();
4358
4359 // Import the type.
4360 QualType T = Importer.Import(DTemplated->getType());
4361 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004362 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004363
4364 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004365 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4366 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004367 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004368
4369 // Create the variable template declaration itself.
4370 TemplateParameterList *TemplateParams =
4371 ImportTemplateParameterList(D->getTemplateParameters());
4372 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004373 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004374
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004375 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4376 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4377 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004378
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004379 ToVarTD->setAccess(D->getAccess());
4380 ToVarTD->setLexicalDeclContext(LexicalDC);
4381 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004382
4383 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004384 Importer.Imported(D, ToVarTD);
4385 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004386
4387 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004388 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004389 // FIXME: Import definition!
4390 }
4391
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004392 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004393}
4394
4395Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4396 VarTemplateSpecializationDecl *D) {
4397 // If this record has a definition in the translation unit we're coming from,
4398 // but this particular declaration is not that definition, import the
4399 // definition and map to that.
4400 VarDecl *Definition = D->getDefinition();
4401 if (Definition && Definition != D) {
4402 Decl *ImportedDef = Importer.Import(Definition);
4403 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004404 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004405
4406 return Importer.Imported(D, ImportedDef);
4407 }
4408
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004409 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004410 Importer.Import(D->getSpecializedTemplate()));
4411 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004412 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004413
4414 // Import the context of this declaration.
4415 DeclContext *DC = VarTemplate->getDeclContext();
4416 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004417 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004418
4419 DeclContext *LexicalDC = DC;
4420 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4421 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4422 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004423 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004424 }
4425
4426 // Import the location of this declaration.
4427 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4428 SourceLocation IdLoc = Importer.Import(D->getLocation());
4429
4430 // Import template arguments.
4431 SmallVector<TemplateArgument, 2> TemplateArgs;
4432 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4433 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004434 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004435
4436 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004437 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004438 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004439 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004440 if (D2) {
4441 // We already have a variable template specialization with these template
4442 // arguments.
4443
4444 // FIXME: Check for specialization vs. instantiation errors.
4445
4446 if (VarDecl *FoundDef = D2->getDefinition()) {
4447 if (!D->isThisDeclarationADefinition() ||
4448 IsStructuralMatch(D, FoundDef)) {
4449 // The record types structurally match, or the "from" translation
4450 // unit only had a forward declaration anyway; call it the same
4451 // variable.
4452 return Importer.Imported(D, FoundDef);
4453 }
4454 }
4455 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004456 // Import the type.
4457 QualType T = Importer.Import(D->getType());
4458 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004459 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004460
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004461 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4462 if (D->getTypeSourceInfo() && !TInfo)
4463 return nullptr;
4464
4465 TemplateArgumentListInfo ToTAInfo;
4466 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4467 return nullptr;
4468
4469 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004470 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004471 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4472 // Import TemplateArgumentListInfo
4473 TemplateArgumentListInfo ArgInfos;
4474 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4475 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4476 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4477 return nullptr;
4478
4479 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4480 FromPartial->getTemplateParameters());
4481 if (!ToTPList)
4482 return nullptr;
4483
4484 auto *ToPartial = PartVarSpecDecl::Create(
4485 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4486 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4487
4488 auto *FromInst = FromPartial->getInstantiatedFromMember();
4489 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4490 if (FromInst && !ToInst)
4491 return nullptr;
4492
4493 ToPartial->setInstantiatedFromMember(ToInst);
4494 if (FromPartial->isMemberSpecialization())
4495 ToPartial->setMemberSpecialization();
4496
4497 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004498 } else { // Full specialization
4499 D2 = VarTemplateSpecializationDecl::Create(
4500 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4501 D->getStorageClass(), TemplateArgs);
4502 }
4503
4504 SourceLocation POI = D->getPointOfInstantiation();
4505 if (POI.isValid())
4506 D2->setPointOfInstantiation(Importer.Import(POI));
4507
Larisse Voufo39a1e502013-08-06 01:03:05 +00004508 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004509 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004510
4511 // Add this specialization to the class template.
4512 VarTemplate->AddSpecialization(D2, InsertPos);
4513
4514 // Import the qualifier, if any.
4515 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4516
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004517 if (D->isConstexpr())
4518 D2->setConstexpr(true);
4519
Larisse Voufo39a1e502013-08-06 01:03:05 +00004520 // Add the specialization to this context.
4521 D2->setLexicalDeclContext(LexicalDC);
4522 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004523
4524 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004525 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004526
Larisse Voufo39a1e502013-08-06 01:03:05 +00004527 Importer.Imported(D, D2);
4528
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004529 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4530 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4531 // we have to check the declaration for initializer - otherwise, it won't be
4532 // imported.
4533 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4534 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004535 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004536
4537 return D2;
4538}
4539
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004540Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4541 DeclContext *DC, *LexicalDC;
4542 DeclarationName Name;
4543 SourceLocation Loc;
4544 NamedDecl *ToD;
4545
4546 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4547 return nullptr;
4548
4549 if (ToD)
4550 return ToD;
4551
4552 // Try to find a function in our own ("to") context with the same name, same
4553 // type, and in the same context as the function we're importing.
4554 if (!LexicalDC->isFunctionOrMethod()) {
4555 unsigned IDNS = Decl::IDNS_Ordinary;
4556 SmallVector<NamedDecl *, 2> FoundDecls;
4557 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004558 for (auto *FoundDecl : FoundDecls) {
4559 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004560 continue;
4561
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004562 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004563 if (FoundFunction->hasExternalFormalLinkage() &&
4564 D->hasExternalFormalLinkage()) {
4565 if (IsStructuralMatch(D, FoundFunction)) {
4566 Importer.Imported(D, FoundFunction);
4567 // FIXME: Actually try to merge the body and other attributes.
4568 return FoundFunction;
4569 }
4570 }
4571 }
4572 }
4573 }
4574
4575 TemplateParameterList *Params =
4576 ImportTemplateParameterList(D->getTemplateParameters());
4577 if (!Params)
4578 return nullptr;
4579
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004580 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004581 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4582 if (!TemplatedFD)
4583 return nullptr;
4584
4585 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4586 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4587
4588 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4589 ToFunc->setAccess(D->getAccess());
4590 ToFunc->setLexicalDeclContext(LexicalDC);
4591 Importer.Imported(D, ToFunc);
4592
4593 LexicalDC->addDeclInternal(ToFunc);
4594 return ToFunc;
4595}
4596
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004597//----------------------------------------------------------------------------
4598// Import Statements
4599//----------------------------------------------------------------------------
4600
Sean Callanan59721b32015-04-28 18:41:46 +00004601DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4602 if (DG.isNull())
4603 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4604 size_t NumDecls = DG.end() - DG.begin();
4605 SmallVector<Decl *, 1> ToDecls(NumDecls);
4606 auto &_Importer = this->Importer;
4607 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4608 [&_Importer](Decl *D) -> Decl * {
4609 return _Importer.Import(D);
4610 });
4611 return DeclGroupRef::Create(Importer.getToContext(),
4612 ToDecls.begin(),
4613 NumDecls);
4614}
4615
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004616Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4617 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4618 << S->getStmtClassName();
4619 return nullptr;
4620}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004621
4622Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4623 SmallVector<IdentifierInfo *, 4> Names;
4624 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4625 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004626 // ToII is nullptr when no symbolic name is given for output operand
4627 // see ParseStmtAsm::ParseAsmOperandsOpt
4628 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004629 return nullptr;
4630 Names.push_back(ToII);
4631 }
4632 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4633 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004634 // ToII is nullptr when no symbolic name is given for input operand
4635 // see ParseStmtAsm::ParseAsmOperandsOpt
4636 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004637 return nullptr;
4638 Names.push_back(ToII);
4639 }
4640
4641 SmallVector<StringLiteral *, 4> Clobbers;
4642 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004643 auto *Clobber = cast_or_null<StringLiteral>(
4644 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004645 if (!Clobber)
4646 return nullptr;
4647 Clobbers.push_back(Clobber);
4648 }
4649
4650 SmallVector<StringLiteral *, 4> Constraints;
4651 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004652 auto *Output = cast_or_null<StringLiteral>(
4653 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004654 if (!Output)
4655 return nullptr;
4656 Constraints.push_back(Output);
4657 }
4658
4659 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004660 auto *Input = cast_or_null<StringLiteral>(
4661 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004662 if (!Input)
4663 return nullptr;
4664 Constraints.push_back(Input);
4665 }
4666
4667 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004668 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004669 return nullptr;
4670
Aleksei Sidorina693b372016-09-28 10:16:56 +00004671 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004672 return nullptr;
4673
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004674 auto *AsmStr = cast_or_null<StringLiteral>(
4675 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004676 if (!AsmStr)
4677 return nullptr;
4678
4679 return new (Importer.getToContext()) GCCAsmStmt(
4680 Importer.getToContext(),
4681 Importer.Import(S->getAsmLoc()),
4682 S->isSimple(),
4683 S->isVolatile(),
4684 S->getNumOutputs(),
4685 S->getNumInputs(),
4686 Names.data(),
4687 Constraints.data(),
4688 Exprs.data(),
4689 AsmStr,
4690 S->getNumClobbers(),
4691 Clobbers.data(),
4692 Importer.Import(S->getRParenLoc()));
4693}
4694
Sean Callanan59721b32015-04-28 18:41:46 +00004695Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4696 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004697 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004698 if (!ToD)
4699 return nullptr;
4700 }
4701 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4702 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4703 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4704}
4705
4706Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4707 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4708 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4709 S->hasLeadingEmptyMacro());
4710}
4711
4712Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004713 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004714
4715 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004716 return nullptr;
4717
Sean Callanan59721b32015-04-28 18:41:46 +00004718 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4719 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004720 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4721 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004722}
4723
4724Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4725 Expr *ToLHS = Importer.Import(S->getLHS());
4726 if (!ToLHS)
4727 return nullptr;
4728 Expr *ToRHS = Importer.Import(S->getRHS());
4729 if (!ToRHS && S->getRHS())
4730 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004731 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4732 if (!ToSubStmt && S->getSubStmt())
4733 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004734 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4735 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4736 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004737 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004738 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4739 ToStmt->setSubStmt(ToSubStmt);
4740 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004741}
4742
4743Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4744 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4745 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4746 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4747 if (!ToSubStmt && S->getSubStmt())
4748 return nullptr;
4749 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4750 ToSubStmt);
4751}
4752
4753Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4754 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004755 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004756 if (!ToLabelDecl && S->getDecl())
4757 return nullptr;
4758 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4759 if (!ToSubStmt && S->getSubStmt())
4760 return nullptr;
4761 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4762 ToSubStmt);
4763}
4764
4765Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4766 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4767 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4768 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4769 ASTContext &_ToContext = Importer.getToContext();
4770 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4771 [&_ToContext](const Attr *A) -> const Attr * {
4772 return A->clone(_ToContext);
4773 });
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004774 for (const auto *ToA : ToAttrs) {
Sean Callanan59721b32015-04-28 18:41:46 +00004775 if (!ToA)
4776 return nullptr;
4777 }
4778 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4779 if (!ToSubStmt && S->getSubStmt())
4780 return nullptr;
4781 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4782 ToAttrs, ToSubStmt);
4783}
4784
4785Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4786 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004787 Stmt *ToInit = Importer.Import(S->getInit());
4788 if (!ToInit && S->getInit())
4789 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004790 VarDecl *ToConditionVariable = nullptr;
4791 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4792 ToConditionVariable =
4793 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4794 if (!ToConditionVariable)
4795 return nullptr;
4796 }
4797 Expr *ToCondition = Importer.Import(S->getCond());
4798 if (!ToCondition && S->getCond())
4799 return nullptr;
4800 Stmt *ToThenStmt = Importer.Import(S->getThen());
4801 if (!ToThenStmt && S->getThen())
4802 return nullptr;
4803 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4804 Stmt *ToElseStmt = Importer.Import(S->getElse());
4805 if (!ToElseStmt && S->getElse())
4806 return nullptr;
4807 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004808 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004809 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004810 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004811 ToCondition, ToThenStmt,
4812 ToElseLoc, ToElseStmt);
4813}
4814
4815Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004816 Stmt *ToInit = Importer.Import(S->getInit());
4817 if (!ToInit && S->getInit())
4818 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004819 VarDecl *ToConditionVariable = nullptr;
4820 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4821 ToConditionVariable =
4822 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4823 if (!ToConditionVariable)
4824 return nullptr;
4825 }
4826 Expr *ToCondition = Importer.Import(S->getCond());
4827 if (!ToCondition && S->getCond())
4828 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004829 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004830 Importer.getToContext(), ToInit,
4831 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004832 Stmt *ToBody = Importer.Import(S->getBody());
4833 if (!ToBody && S->getBody())
4834 return nullptr;
4835 ToStmt->setBody(ToBody);
4836 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4837 // Now we have to re-chain the cases.
4838 SwitchCase *LastChainedSwitchCase = nullptr;
4839 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4840 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004841 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004842 if (!ToSC)
4843 return nullptr;
4844 if (LastChainedSwitchCase)
4845 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4846 else
4847 ToStmt->setSwitchCaseList(ToSC);
4848 LastChainedSwitchCase = ToSC;
4849 }
4850 return ToStmt;
4851}
4852
4853Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4854 VarDecl *ToConditionVariable = nullptr;
4855 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4856 ToConditionVariable =
4857 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4858 if (!ToConditionVariable)
4859 return nullptr;
4860 }
4861 Expr *ToCondition = Importer.Import(S->getCond());
4862 if (!ToCondition && S->getCond())
4863 return nullptr;
4864 Stmt *ToBody = Importer.Import(S->getBody());
4865 if (!ToBody && S->getBody())
4866 return nullptr;
4867 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4868 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4869 ToConditionVariable,
4870 ToCondition, ToBody,
4871 ToWhileLoc);
4872}
4873
4874Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4875 Stmt *ToBody = Importer.Import(S->getBody());
4876 if (!ToBody && S->getBody())
4877 return nullptr;
4878 Expr *ToCondition = Importer.Import(S->getCond());
4879 if (!ToCondition && S->getCond())
4880 return nullptr;
4881 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4882 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4883 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4884 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4885 ToDoLoc, ToWhileLoc,
4886 ToRParenLoc);
4887}
4888
4889Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4890 Stmt *ToInit = Importer.Import(S->getInit());
4891 if (!ToInit && S->getInit())
4892 return nullptr;
4893 Expr *ToCondition = Importer.Import(S->getCond());
4894 if (!ToCondition && S->getCond())
4895 return nullptr;
4896 VarDecl *ToConditionVariable = nullptr;
4897 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4898 ToConditionVariable =
4899 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4900 if (!ToConditionVariable)
4901 return nullptr;
4902 }
4903 Expr *ToInc = Importer.Import(S->getInc());
4904 if (!ToInc && S->getInc())
4905 return nullptr;
4906 Stmt *ToBody = Importer.Import(S->getBody());
4907 if (!ToBody && S->getBody())
4908 return nullptr;
4909 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4910 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4911 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4912 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4913 ToInit, ToCondition,
4914 ToConditionVariable,
4915 ToInc, ToBody,
4916 ToForLoc, ToLParenLoc,
4917 ToRParenLoc);
4918}
4919
4920Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4921 LabelDecl *ToLabel = nullptr;
4922 if (LabelDecl *FromLabel = S->getLabel()) {
4923 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4924 if (!ToLabel)
4925 return nullptr;
4926 }
4927 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4928 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4929 return new (Importer.getToContext()) GotoStmt(ToLabel,
4930 ToGotoLoc, ToLabelLoc);
4931}
4932
4933Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4934 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4935 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4936 Expr *ToTarget = Importer.Import(S->getTarget());
4937 if (!ToTarget && S->getTarget())
4938 return nullptr;
4939 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4940 ToTarget);
4941}
4942
4943Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4944 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4945 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4946}
4947
4948Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4949 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4950 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4951}
4952
4953Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4954 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4955 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4956 if (!ToRetExpr && S->getRetValue())
4957 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004958 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4959 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004960 if (!ToNRVOCandidate && NRVOCandidate)
4961 return nullptr;
4962 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4963 ToNRVOCandidate);
4964}
4965
4966Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4967 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4968 VarDecl *ToExceptionDecl = nullptr;
4969 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4970 ToExceptionDecl =
4971 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4972 if (!ToExceptionDecl)
4973 return nullptr;
4974 }
4975 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4976 if (!ToHandlerBlock && S->getHandlerBlock())
4977 return nullptr;
4978 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4979 ToExceptionDecl,
4980 ToHandlerBlock);
4981}
4982
4983Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4984 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4985 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4986 if (!ToTryBlock && S->getTryBlock())
4987 return nullptr;
4988 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4989 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4990 CXXCatchStmt *FromHandler = S->getHandler(HI);
4991 if (Stmt *ToHandler = Importer.Import(FromHandler))
4992 ToHandlers[HI] = ToHandler;
4993 else
4994 return nullptr;
4995 }
4996 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4997 ToHandlers);
4998}
4999
5000Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005001 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005002 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5003 if (!ToRange && S->getRangeStmt())
5004 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005005 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005006 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5007 if (!ToBegin && S->getBeginStmt())
5008 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005009 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005010 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5011 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005012 return nullptr;
5013 Expr *ToCond = Importer.Import(S->getCond());
5014 if (!ToCond && S->getCond())
5015 return nullptr;
5016 Expr *ToInc = Importer.Import(S->getInc());
5017 if (!ToInc && S->getInc())
5018 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005019 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005020 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5021 if (!ToLoopVar && S->getLoopVarStmt())
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());
Richard Smith9f690bd2015-10-27 06:02:45 +00005027 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005028 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5029 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005030 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005031 ToCond, ToInc,
5032 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005033 ToForLoc, ToCoawaitLoc,
5034 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005035}
5036
5037Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5038 Stmt *ToElem = Importer.Import(S->getElement());
5039 if (!ToElem && S->getElement())
5040 return nullptr;
5041 Expr *ToCollect = Importer.Import(S->getCollection());
5042 if (!ToCollect && S->getCollection())
5043 return nullptr;
5044 Stmt *ToBody = Importer.Import(S->getBody());
5045 if (!ToBody && S->getBody())
5046 return nullptr;
5047 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5048 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5049 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5050 ToCollect,
5051 ToBody, ToForLoc,
5052 ToRParenLoc);
5053}
5054
5055Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5056 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5057 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5058 VarDecl *ToExceptionDecl = nullptr;
5059 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5060 ToExceptionDecl =
5061 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5062 if (!ToExceptionDecl)
5063 return nullptr;
5064 }
5065 Stmt *ToBody = Importer.Import(S->getCatchBody());
5066 if (!ToBody && S->getCatchBody())
5067 return nullptr;
5068 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5069 ToRParenLoc,
5070 ToExceptionDecl,
5071 ToBody);
5072}
5073
5074Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5075 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5076 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5077 if (!ToAtFinallyStmt && S->getFinallyBody())
5078 return nullptr;
5079 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5080 ToAtFinallyStmt);
5081}
5082
5083Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5084 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5085 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5086 if (!ToAtTryStmt && S->getTryBody())
5087 return nullptr;
5088 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5089 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5090 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5091 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5092 ToCatchStmts[CI] = ToCatchStmt;
5093 else
5094 return nullptr;
5095 }
5096 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5097 if (!ToAtFinallyStmt && S->getFinallyStmt())
5098 return nullptr;
5099 return ObjCAtTryStmt::Create(Importer.getToContext(),
5100 ToAtTryLoc, ToAtTryStmt,
5101 ToCatchStmts.begin(), ToCatchStmts.size(),
5102 ToAtFinallyStmt);
5103}
5104
5105Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5106 (ObjCAtSynchronizedStmt *S) {
5107 SourceLocation ToAtSynchronizedLoc =
5108 Importer.Import(S->getAtSynchronizedLoc());
5109 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5110 if (!ToSynchExpr && S->getSynchExpr())
5111 return nullptr;
5112 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5113 if (!ToSynchBody && S->getSynchBody())
5114 return nullptr;
5115 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5116 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5117}
5118
5119Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5120 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5121 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5122 if (!ToThrow && S->getThrowExpr())
5123 return nullptr;
5124 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5125}
5126
5127Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5128 (ObjCAutoreleasePoolStmt *S) {
5129 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5130 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5131 if (!ToSubStmt && S->getSubStmt())
5132 return nullptr;
5133 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5134 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005135}
5136
5137//----------------------------------------------------------------------------
5138// Import Expressions
5139//----------------------------------------------------------------------------
5140Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5141 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5142 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005143 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005144}
5145
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005146Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5147 QualType T = Importer.Import(E->getType());
5148 if (T.isNull())
5149 return nullptr;
5150
5151 Expr *SubExpr = Importer.Import(E->getSubExpr());
5152 if (!SubExpr && E->getSubExpr())
5153 return nullptr;
5154
5155 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5156 if (!TInfo)
5157 return nullptr;
5158
5159 return new (Importer.getToContext()) VAArgExpr(
5160 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5161 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5162}
5163
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005164Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5165 QualType T = Importer.Import(E->getType());
5166 if (T.isNull())
5167 return nullptr;
5168
5169 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005170 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005171}
5172
5173Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5174 QualType T = Importer.Import(E->getType());
5175 if (T.isNull())
5176 return nullptr;
5177
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005178 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005179 if (!SL && E->getFunctionName())
5180 return nullptr;
5181
5182 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005183 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005184}
5185
Douglas Gregor52f820e2010-02-19 01:17:02 +00005186Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005187 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005188 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005189 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005190
Craig Topper36250ad2014-05-12 05:36:57 +00005191 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005192 if (E->getDecl() != E->getFoundDecl()) {
5193 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5194 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005195 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005196 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005197
5198 QualType T = Importer.Import(E->getType());
5199 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005200 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005201
Aleksei Sidorina693b372016-09-28 10:16:56 +00005202 TemplateArgumentListInfo ToTAInfo;
5203 TemplateArgumentListInfo *ResInfo = nullptr;
5204 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005205 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5206 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005207 ResInfo = &ToTAInfo;
5208 }
5209
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005210 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5211 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005212 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005213 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005214 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005215 Importer.Import(E->getLocation()),
5216 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005217 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005218 if (E->hadMultipleCandidates())
5219 DRE->setHadMultipleCandidates(true);
5220 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005221}
5222
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005223Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5224 QualType T = Importer.Import(E->getType());
5225 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005226 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005227
5228 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5229}
5230
5231ASTNodeImporter::Designator
5232ASTNodeImporter::ImportDesignator(const Designator &D) {
5233 if (D.isFieldDesignator()) {
5234 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5235 // Caller checks for import error
5236 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5237 Importer.Import(D.getFieldLoc()));
5238 }
5239 if (D.isArrayDesignator())
5240 return Designator(D.getFirstExprIndex(),
5241 Importer.Import(D.getLBracketLoc()),
5242 Importer.Import(D.getRBracketLoc()));
5243
5244 assert(D.isArrayRangeDesignator());
5245 return Designator(D.getFirstExprIndex(),
5246 Importer.Import(D.getLBracketLoc()),
5247 Importer.Import(D.getEllipsisLoc()),
5248 Importer.Import(D.getRBracketLoc()));
5249}
5250
5251
5252Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005253 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005254 if (!Init)
5255 return nullptr;
5256
5257 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5258 // List elements from the second, the first is Init itself
5259 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005260 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005261 IndexExprs[I - 1] = Arg;
5262 else
5263 return nullptr;
5264 }
5265
5266 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005267 llvm::transform(DIE->designators(), Designators.begin(),
5268 [this](const Designator &D) -> Designator {
5269 return ImportDesignator(D);
5270 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005271
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005272 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005273 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005274 return nullptr;
5275
5276 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005277 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005278 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5279 DIE->usesGNUSyntax(), Init);
5280}
5281
5282Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5283 QualType T = Importer.Import(E->getType());
5284 if (T.isNull())
5285 return nullptr;
5286
5287 return new (Importer.getToContext())
5288 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5289}
5290
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005291Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5292 QualType T = Importer.Import(E->getType());
5293 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005294 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005295
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005296 return IntegerLiteral::Create(Importer.getToContext(),
5297 E->getValue(), T,
5298 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005299}
5300
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005301Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5302 QualType T = Importer.Import(E->getType());
5303 if (T.isNull())
5304 return nullptr;
5305
5306 return FloatingLiteral::Create(Importer.getToContext(),
5307 E->getValue(), E->isExact(), T,
5308 Importer.Import(E->getLocation()));
5309}
5310
Douglas Gregor623421d2010-02-18 02:21:22 +00005311Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5312 QualType T = Importer.Import(E->getType());
5313 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005314 return nullptr;
5315
Douglas Gregorfb65e592011-07-27 05:40:30 +00005316 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5317 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005318 Importer.Import(E->getLocation()));
5319}
5320
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005321Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5322 QualType T = Importer.Import(E->getType());
5323 if (T.isNull())
5324 return nullptr;
5325
5326 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5327 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5328
5329 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5330 E->getKind(), E->isPascal(), T,
5331 Locations.data(), Locations.size());
5332}
5333
5334Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5335 QualType T = Importer.Import(E->getType());
5336 if (T.isNull())
5337 return nullptr;
5338
5339 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5340 if (!TInfo)
5341 return nullptr;
5342
5343 Expr *Init = Importer.Import(E->getInitializer());
5344 if (!Init)
5345 return nullptr;
5346
5347 return new (Importer.getToContext()) CompoundLiteralExpr(
5348 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5349 Init, E->isFileScope());
5350}
5351
5352Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5353 QualType T = Importer.Import(E->getType());
5354 if (T.isNull())
5355 return nullptr;
5356
5357 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5358 if (ImportArrayChecked(
5359 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5360 Exprs.begin()))
5361 return nullptr;
5362
5363 return new (Importer.getToContext()) AtomicExpr(
5364 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5365 Importer.Import(E->getRParenLoc()));
5366}
5367
5368Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5369 QualType T = Importer.Import(E->getType());
5370 if (T.isNull())
5371 return nullptr;
5372
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005373 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005374 if (!ToLabel)
5375 return nullptr;
5376
5377 return new (Importer.getToContext()) AddrLabelExpr(
5378 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5379 ToLabel, T);
5380}
5381
Douglas Gregorc74247e2010-02-19 01:07:06 +00005382Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5383 Expr *SubExpr = Importer.Import(E->getSubExpr());
5384 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005385 return nullptr;
5386
Douglas Gregorc74247e2010-02-19 01:07:06 +00005387 return new (Importer.getToContext())
5388 ParenExpr(Importer.Import(E->getLParen()),
5389 Importer.Import(E->getRParen()),
5390 SubExpr);
5391}
5392
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005393Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5394 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005395 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005396 return nullptr;
5397
5398 return new (Importer.getToContext()) ParenListExpr(
5399 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5400 Exprs, Importer.Import(E->getLParenLoc()));
5401}
5402
5403Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5404 QualType T = Importer.Import(E->getType());
5405 if (T.isNull())
5406 return nullptr;
5407
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005408 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5409 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005410 if (!ToSubStmt && E->getSubStmt())
5411 return nullptr;
5412
5413 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5414 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5415}
5416
Douglas Gregorc74247e2010-02-19 01:07:06 +00005417Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5418 QualType T = Importer.Import(E->getType());
5419 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005420 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005421
5422 Expr *SubExpr = Importer.Import(E->getSubExpr());
5423 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005424 return nullptr;
5425
Aaron Ballmana5038552018-01-09 13:07:03 +00005426 return new (Importer.getToContext()) UnaryOperator(
5427 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5428 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005429}
5430
Aaron Ballmana5038552018-01-09 13:07:03 +00005431Expr *
5432ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005433 QualType ResultType = Importer.Import(E->getType());
5434
5435 if (E->isArgumentType()) {
5436 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5437 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005438 return nullptr;
5439
Peter Collingbournee190dee2011-03-11 19:24:49 +00005440 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5441 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005442 Importer.Import(E->getOperatorLoc()),
5443 Importer.Import(E->getRParenLoc()));
5444 }
5445
5446 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5447 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005448 return nullptr;
5449
Peter Collingbournee190dee2011-03-11 19:24:49 +00005450 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5451 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005452 Importer.Import(E->getOperatorLoc()),
5453 Importer.Import(E->getRParenLoc()));
5454}
5455
Douglas Gregorc74247e2010-02-19 01:07:06 +00005456Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5457 QualType T = Importer.Import(E->getType());
5458 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005459 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005460
5461 Expr *LHS = Importer.Import(E->getLHS());
5462 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005463 return nullptr;
5464
Douglas Gregorc74247e2010-02-19 01:07:06 +00005465 Expr *RHS = Importer.Import(E->getRHS());
5466 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005467 return nullptr;
5468
Douglas Gregorc74247e2010-02-19 01:07:06 +00005469 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005470 T, E->getValueKind(),
5471 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005472 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005473 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005474}
5475
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005476Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5477 QualType T = Importer.Import(E->getType());
5478 if (T.isNull())
5479 return nullptr;
5480
5481 Expr *ToLHS = Importer.Import(E->getLHS());
5482 if (!ToLHS)
5483 return nullptr;
5484
5485 Expr *ToRHS = Importer.Import(E->getRHS());
5486 if (!ToRHS)
5487 return nullptr;
5488
5489 Expr *ToCond = Importer.Import(E->getCond());
5490 if (!ToCond)
5491 return nullptr;
5492
5493 return new (Importer.getToContext()) ConditionalOperator(
5494 ToCond, Importer.Import(E->getQuestionLoc()),
5495 ToLHS, Importer.Import(E->getColonLoc()),
5496 ToRHS, T, E->getValueKind(), E->getObjectKind());
5497}
5498
5499Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5500 BinaryConditionalOperator *E) {
5501 QualType T = Importer.Import(E->getType());
5502 if (T.isNull())
5503 return nullptr;
5504
5505 Expr *Common = Importer.Import(E->getCommon());
5506 if (!Common)
5507 return nullptr;
5508
5509 Expr *Cond = Importer.Import(E->getCond());
5510 if (!Cond)
5511 return nullptr;
5512
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005513 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5514 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005515 if (!OpaqueValue)
5516 return nullptr;
5517
5518 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5519 if (!TrueExpr)
5520 return nullptr;
5521
5522 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5523 if (!FalseExpr)
5524 return nullptr;
5525
5526 return new (Importer.getToContext()) BinaryConditionalOperator(
5527 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5528 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5529 T, E->getValueKind(), E->getObjectKind());
5530}
5531
Aleksei Sidorina693b372016-09-28 10:16:56 +00005532Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5533 QualType T = Importer.Import(E->getType());
5534 if (T.isNull())
5535 return nullptr;
5536
5537 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5538 if (!ToQueried)
5539 return nullptr;
5540
5541 Expr *Dim = Importer.Import(E->getDimensionExpression());
5542 if (!Dim && E->getDimensionExpression())
5543 return nullptr;
5544
5545 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5546 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5547 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5548}
5549
5550Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5551 QualType T = Importer.Import(E->getType());
5552 if (T.isNull())
5553 return nullptr;
5554
5555 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5556 if (!ToQueried)
5557 return nullptr;
5558
5559 return new (Importer.getToContext()) ExpressionTraitExpr(
5560 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5561 E->getValue(), Importer.Import(E->getLocEnd()), T);
5562}
5563
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005564Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5565 QualType T = Importer.Import(E->getType());
5566 if (T.isNull())
5567 return nullptr;
5568
5569 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5570 if (!SourceExpr && E->getSourceExpr())
5571 return nullptr;
5572
5573 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005574 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005575 E->getObjectKind(), SourceExpr);
5576}
5577
Aleksei Sidorina693b372016-09-28 10:16:56 +00005578Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5579 QualType T = Importer.Import(E->getType());
5580 if (T.isNull())
5581 return nullptr;
5582
5583 Expr *ToLHS = Importer.Import(E->getLHS());
5584 if (!ToLHS)
5585 return nullptr;
5586
5587 Expr *ToRHS = Importer.Import(E->getRHS());
5588 if (!ToRHS)
5589 return nullptr;
5590
5591 return new (Importer.getToContext()) ArraySubscriptExpr(
5592 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5593 Importer.Import(E->getRBracketLoc()));
5594}
5595
Douglas Gregorc74247e2010-02-19 01:07:06 +00005596Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5597 QualType T = Importer.Import(E->getType());
5598 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005599 return nullptr;
5600
Douglas Gregorc74247e2010-02-19 01:07:06 +00005601 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5602 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005603 return nullptr;
5604
Douglas Gregorc74247e2010-02-19 01:07:06 +00005605 QualType CompResultType = Importer.Import(E->getComputationResultType());
5606 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005607 return nullptr;
5608
Douglas Gregorc74247e2010-02-19 01:07:06 +00005609 Expr *LHS = Importer.Import(E->getLHS());
5610 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005611 return nullptr;
5612
Douglas Gregorc74247e2010-02-19 01:07:06 +00005613 Expr *RHS = Importer.Import(E->getRHS());
5614 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005615 return nullptr;
5616
Douglas Gregorc74247e2010-02-19 01:07:06 +00005617 return new (Importer.getToContext())
5618 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005619 T, E->getValueKind(),
5620 E->getObjectKind(),
5621 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005622 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005623 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005624}
5625
Aleksei Sidorina693b372016-09-28 10:16:56 +00005626bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5627 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5628 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5629 Path.push_back(Spec);
5630 else
5631 return true;
5632 }
5633 return false;
John McCallcf142162010-08-07 06:22:56 +00005634}
5635
Douglas Gregor98c10182010-02-12 22:17:39 +00005636Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5637 QualType T = Importer.Import(E->getType());
5638 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005639 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005640
5641 Expr *SubExpr = Importer.Import(E->getSubExpr());
5642 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005643 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005644
5645 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
5649 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005650 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005651}
5652
Aleksei Sidorina693b372016-09-28 10:16:56 +00005653Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005654 QualType T = Importer.Import(E->getType());
5655 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005656 return nullptr;
5657
Douglas Gregor5481d322010-02-19 01:32:14 +00005658 Expr *SubExpr = Importer.Import(E->getSubExpr());
5659 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005660 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005661
5662 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5663 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005664 return nullptr;
5665
John McCallcf142162010-08-07 06:22:56 +00005666 CXXCastPath BasePath;
5667 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005668 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005669
Aleksei Sidorina693b372016-09-28 10:16:56 +00005670 switch (E->getStmtClass()) {
5671 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005672 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005673 return CStyleCastExpr::Create(Importer.getToContext(), T,
5674 E->getValueKind(), E->getCastKind(),
5675 SubExpr, &BasePath, TInfo,
5676 Importer.Import(CCE->getLParenLoc()),
5677 Importer.Import(CCE->getRParenLoc()));
5678 }
5679
5680 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005681 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005682 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5683 E->getValueKind(), TInfo,
5684 E->getCastKind(), SubExpr, &BasePath,
5685 Importer.Import(FCE->getLParenLoc()),
5686 Importer.Import(FCE->getRParenLoc()));
5687 }
5688
5689 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005690 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005691 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5692 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5693 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5694 TInfo, SubExpr);
5695 }
5696 default:
5697 break; // just fall through
5698 }
5699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005700 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005701 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5702 RParenLoc = Importer.Import(Named->getRParenLoc());
5703 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5704
5705 switch (E->getStmtClass()) {
5706 case Stmt::CXXStaticCastExprClass:
5707 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5708 E->getValueKind(), E->getCastKind(),
5709 SubExpr, &BasePath, TInfo,
5710 ExprLoc, RParenLoc, Brackets);
5711
5712 case Stmt::CXXDynamicCastExprClass:
5713 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5714 E->getValueKind(), E->getCastKind(),
5715 SubExpr, &BasePath, TInfo,
5716 ExprLoc, RParenLoc, Brackets);
5717
5718 case Stmt::CXXReinterpretCastExprClass:
5719 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5720 E->getValueKind(), E->getCastKind(),
5721 SubExpr, &BasePath, TInfo,
5722 ExprLoc, RParenLoc, Brackets);
5723
5724 case Stmt::CXXConstCastExprClass:
5725 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5726 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5727 RParenLoc, Brackets);
5728 default:
5729 llvm_unreachable("Cast expression of unsupported type!");
5730 return nullptr;
5731 }
5732}
5733
5734Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5735 QualType T = Importer.Import(OE->getType());
5736 if (T.isNull())
5737 return nullptr;
5738
5739 SmallVector<OffsetOfNode, 4> Nodes;
5740 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5741 const OffsetOfNode &Node = OE->getComponent(I);
5742
5743 switch (Node.getKind()) {
5744 case OffsetOfNode::Array:
5745 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5746 Node.getArrayExprIndex(),
5747 Importer.Import(Node.getLocEnd())));
5748 break;
5749
5750 case OffsetOfNode::Base: {
5751 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5752 if (!BS && Node.getBase())
5753 return nullptr;
5754 Nodes.push_back(OffsetOfNode(BS));
5755 break;
5756 }
5757 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005758 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005759 if (!FD)
5760 return nullptr;
5761 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5762 Importer.Import(Node.getLocEnd())));
5763 break;
5764 }
5765 case OffsetOfNode::Identifier: {
5766 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5767 if (!ToII)
5768 return nullptr;
5769 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5770 Importer.Import(Node.getLocEnd())));
5771 break;
5772 }
5773 }
5774 }
5775
5776 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5777 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5778 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5779 if (!ToIndexExpr)
5780 return nullptr;
5781 Exprs[I] = ToIndexExpr;
5782 }
5783
5784 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5785 if (!TInfo && OE->getTypeSourceInfo())
5786 return nullptr;
5787
5788 return OffsetOfExpr::Create(Importer.getToContext(), T,
5789 Importer.Import(OE->getOperatorLoc()),
5790 TInfo, Nodes, Exprs,
5791 Importer.Import(OE->getRParenLoc()));
5792}
5793
5794Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5795 QualType T = Importer.Import(E->getType());
5796 if (T.isNull())
5797 return nullptr;
5798
5799 Expr *Operand = Importer.Import(E->getOperand());
5800 if (!Operand)
5801 return nullptr;
5802
5803 CanThrowResult CanThrow;
5804 if (E->isValueDependent())
5805 CanThrow = CT_Dependent;
5806 else
5807 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5808
5809 return new (Importer.getToContext()) CXXNoexceptExpr(
5810 T, Operand, CanThrow,
5811 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5812}
5813
5814Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5815 QualType T = Importer.Import(E->getType());
5816 if (T.isNull())
5817 return nullptr;
5818
5819 Expr *SubExpr = Importer.Import(E->getSubExpr());
5820 if (!SubExpr && E->getSubExpr())
5821 return nullptr;
5822
5823 return new (Importer.getToContext()) CXXThrowExpr(
5824 SubExpr, T, Importer.Import(E->getThrowLoc()),
5825 E->isThrownVariableInScope());
5826}
5827
5828Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005829 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005830 if (!Param)
5831 return nullptr;
5832
5833 return CXXDefaultArgExpr::Create(
5834 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5835}
5836
5837Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5838 QualType T = Importer.Import(E->getType());
5839 if (T.isNull())
5840 return nullptr;
5841
5842 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5843 if (!TypeInfo)
5844 return nullptr;
5845
5846 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5847 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5848}
5849
5850Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5851 Expr *SubExpr = Importer.Import(E->getSubExpr());
5852 if (!SubExpr)
5853 return nullptr;
5854
5855 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5856 Importer.Import(const_cast<CXXDestructorDecl *>(
5857 E->getTemporary()->getDestructor())));
5858 if (!Dtor)
5859 return nullptr;
5860
5861 ASTContext &ToCtx = Importer.getToContext();
5862 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5863 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5864}
5865
5866Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5867 QualType T = Importer.Import(CE->getType());
5868 if (T.isNull())
5869 return nullptr;
5870
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005871 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5872 if (!TInfo)
5873 return nullptr;
5874
Aleksei Sidorina693b372016-09-28 10:16:56 +00005875 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5876 if (ImportContainerChecked(CE->arguments(), Args))
5877 return nullptr;
5878
5879 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5880 Importer.Import(CE->getConstructor()));
5881 if (!Ctor)
5882 return nullptr;
5883
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005884 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5885 Importer.getToContext(), Ctor, T, TInfo, Args,
5886 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5887 CE->isListInitialization(), CE->isStdInitListInitialization(),
5888 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005889}
5890
5891Expr *
5892ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5893 QualType T = Importer.Import(E->getType());
5894 if (T.isNull())
5895 return nullptr;
5896
5897 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5898 if (!TempE)
5899 return nullptr;
5900
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005901 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005902 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5903 if (!ExtendedBy && E->getExtendingDecl())
5904 return nullptr;
5905
5906 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5907 T, TempE, E->isBoundToLvalueReference());
5908
5909 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5910 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5911 return ToMTE;
5912}
5913
Gabor Horvath7a91c082017-11-14 11:30:38 +00005914Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5915 QualType T = Importer.Import(E->getType());
5916 if (T.isNull())
5917 return nullptr;
5918
5919 Expr *Pattern = Importer.Import(E->getPattern());
5920 if (!Pattern)
5921 return nullptr;
5922
5923 return new (Importer.getToContext()) PackExpansionExpr(
5924 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5925 E->getNumExpansions());
5926}
5927
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005928Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5929 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5930 if (!Pack)
5931 return nullptr;
5932
5933 Optional<unsigned> Length;
5934
5935 if (!E->isValueDependent())
5936 Length = E->getPackLength();
5937
5938 SmallVector<TemplateArgument, 8> PartialArguments;
5939 if (E->isPartiallySubstituted()) {
5940 if (ImportTemplateArguments(E->getPartialArguments().data(),
5941 E->getPartialArguments().size(),
5942 PartialArguments))
5943 return nullptr;
5944 }
5945
5946 return SizeOfPackExpr::Create(
5947 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5948 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5949 Length, PartialArguments);
5950}
5951
Aleksei Sidorina693b372016-09-28 10:16:56 +00005952Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5953 QualType T = Importer.Import(CE->getType());
5954 if (T.isNull())
5955 return nullptr;
5956
5957 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5958 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5959 return nullptr;
5960
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005961 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005962 Importer.Import(CE->getOperatorNew()));
5963 if (!OperatorNewDecl && CE->getOperatorNew())
5964 return nullptr;
5965
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005966 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005967 Importer.Import(CE->getOperatorDelete()));
5968 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5969 return nullptr;
5970
5971 Expr *ToInit = Importer.Import(CE->getInitializer());
5972 if (!ToInit && CE->getInitializer())
5973 return nullptr;
5974
5975 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5976 if (!TInfo)
5977 return nullptr;
5978
5979 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5980 if (!ToArrSize && CE->getArraySize())
5981 return nullptr;
5982
5983 return new (Importer.getToContext()) CXXNewExpr(
5984 Importer.getToContext(),
5985 CE->isGlobalNew(),
5986 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005987 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005988 CE->doesUsualArrayDeleteWantSize(),
5989 PlacementArgs,
5990 Importer.Import(CE->getTypeIdParens()),
5991 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5992 Importer.Import(CE->getSourceRange()),
5993 Importer.Import(CE->getDirectInitRange()));
5994}
5995
5996Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5997 QualType T = Importer.Import(E->getType());
5998 if (T.isNull())
5999 return nullptr;
6000
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006001 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006002 Importer.Import(E->getOperatorDelete()));
6003 if (!OperatorDeleteDecl && E->getOperatorDelete())
6004 return nullptr;
6005
6006 Expr *ToArg = Importer.Import(E->getArgument());
6007 if (!ToArg && E->getArgument())
6008 return nullptr;
6009
6010 return new (Importer.getToContext()) CXXDeleteExpr(
6011 T, E->isGlobalDelete(),
6012 E->isArrayForm(),
6013 E->isArrayFormAsWritten(),
6014 E->doesUsualArrayDeleteWantSize(),
6015 OperatorDeleteDecl,
6016 ToArg,
6017 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006018}
6019
Sean Callanan59721b32015-04-28 18:41:46 +00006020Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6021 QualType T = Importer.Import(E->getType());
6022 if (T.isNull())
6023 return nullptr;
6024
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006025 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006026 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006027 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006028 return nullptr;
6029
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006030 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006031 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006032 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006033
6034 return CXXConstructExpr::Create(Importer.getToContext(), T,
6035 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006036 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006037 ToArgs, E->hadMultipleCandidates(),
6038 E->isListInitialization(),
6039 E->isStdInitListInitialization(),
6040 E->requiresZeroInitialization(),
6041 E->getConstructionKind(),
6042 Importer.Import(E->getParenOrBraceRange()));
6043}
6044
Aleksei Sidorina693b372016-09-28 10:16:56 +00006045Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6046 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6047 if (!SubExpr && EWC->getSubExpr())
6048 return nullptr;
6049
6050 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6051 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6052 if (ExprWithCleanups::CleanupObject Obj =
6053 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6054 Objs[I] = Obj;
6055 else
6056 return nullptr;
6057
6058 return ExprWithCleanups::Create(Importer.getToContext(),
6059 SubExpr, EWC->cleanupsHaveSideEffects(),
6060 Objs);
6061}
6062
Sean Callanan8bca9962016-03-28 21:43:01 +00006063Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6064 QualType T = Importer.Import(E->getType());
6065 if (T.isNull())
6066 return nullptr;
6067
6068 Expr *ToFn = Importer.Import(E->getCallee());
6069 if (!ToFn)
6070 return nullptr;
6071
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006072 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006073 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006074 return nullptr;
6075
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006076 return new (Importer.getToContext()) CXXMemberCallExpr(
6077 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6078 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006079}
6080
6081Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6082 QualType T = Importer.Import(E->getType());
6083 if (T.isNull())
6084 return nullptr;
6085
6086 return new (Importer.getToContext())
6087 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6088}
6089
6090Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6091 QualType T = Importer.Import(E->getType());
6092 if (T.isNull())
6093 return nullptr;
6094
6095 return new (Importer.getToContext())
6096 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6097}
6098
6099
Sean Callanan59721b32015-04-28 18:41:46 +00006100Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6101 QualType T = Importer.Import(E->getType());
6102 if (T.isNull())
6103 return nullptr;
6104
6105 Expr *ToBase = Importer.Import(E->getBase());
6106 if (!ToBase && E->getBase())
6107 return nullptr;
6108
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006109 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006110 if (!ToMember && E->getMemberDecl())
6111 return nullptr;
6112
Peter Szecsief972522018-05-02 11:52:54 +00006113 auto *ToDecl =
6114 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6115 if (!ToDecl && E->getFoundDecl().getDecl())
6116 return nullptr;
6117
6118 DeclAccessPair ToFoundDecl =
6119 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006120
6121 DeclarationNameInfo ToMemberNameInfo(
6122 Importer.Import(E->getMemberNameInfo().getName()),
6123 Importer.Import(E->getMemberNameInfo().getLoc()));
6124
6125 if (E->hasExplicitTemplateArgs()) {
6126 return nullptr; // FIXME: handle template arguments
6127 }
6128
6129 return MemberExpr::Create(Importer.getToContext(), ToBase,
6130 E->isArrow(),
6131 Importer.Import(E->getOperatorLoc()),
6132 Importer.Import(E->getQualifierLoc()),
6133 Importer.Import(E->getTemplateKeywordLoc()),
6134 ToMember, ToFoundDecl, ToMemberNameInfo,
6135 nullptr, T, E->getValueKind(),
6136 E->getObjectKind());
6137}
6138
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006139Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6140 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006141 Expr *BaseE = Importer.Import(E->getBase());
6142 if (!BaseE)
6143 return nullptr;
6144
6145 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6146 if (!ScopeInfo && E->getScopeTypeInfo())
6147 return nullptr;
6148
6149 PseudoDestructorTypeStorage Storage;
6150 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6151 IdentifierInfo *ToII = Importer.Import(FromII);
6152 if (!ToII)
6153 return nullptr;
6154 Storage = PseudoDestructorTypeStorage(
6155 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6156 } else {
6157 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6158 if (!TI)
6159 return nullptr;
6160 Storage = PseudoDestructorTypeStorage(TI);
6161 }
6162
6163 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6164 Importer.getToContext(), BaseE, E->isArrow(),
6165 Importer.Import(E->getOperatorLoc()),
6166 Importer.Import(E->getQualifierLoc()),
6167 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6168 Importer.Import(E->getTildeLoc()), Storage);
6169}
6170
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006171Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6172 CXXDependentScopeMemberExpr *E) {
6173 Expr *Base = nullptr;
6174 if (!E->isImplicitAccess()) {
6175 Base = Importer.Import(E->getBase());
6176 if (!Base)
6177 return nullptr;
6178 }
6179
6180 QualType BaseType = Importer.Import(E->getBaseType());
6181 if (BaseType.isNull())
6182 return nullptr;
6183
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006184 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006185 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006186 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6187 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006188 return nullptr;
6189 ResInfo = &ToTAInfo;
6190 }
6191
6192 DeclarationName Name = Importer.Import(E->getMember());
6193 if (!E->getMember().isEmpty() && Name.isEmpty())
6194 return nullptr;
6195
6196 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6197 // Import additional name location/type info.
6198 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6199 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6200 if (!ToFQ && E->getFirstQualifierFoundInScope())
6201 return nullptr;
6202
6203 return CXXDependentScopeMemberExpr::Create(
6204 Importer.getToContext(), Base, BaseType, E->isArrow(),
6205 Importer.Import(E->getOperatorLoc()),
6206 Importer.Import(E->getQualifierLoc()),
6207 Importer.Import(E->getTemplateKeywordLoc()),
6208 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6209}
6210
Peter Szecsice7f3182018-05-07 12:08:27 +00006211Expr *
6212ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6213 DeclarationName Name = Importer.Import(E->getDeclName());
6214 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6215 return nullptr;
6216
6217 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6218 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6219
6220 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6221 Importer.Import(E->getRAngleLoc()));
6222 TemplateArgumentListInfo *ResInfo = nullptr;
6223 if (E->hasExplicitTemplateArgs()) {
6224 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6225 return nullptr;
6226 ResInfo = &ToTAInfo;
6227 }
6228
6229 return DependentScopeDeclRefExpr::Create(
6230 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6231 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6232}
6233
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006234Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6235 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006236 unsigned NumArgs = CE->arg_size();
6237
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006238 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006239 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6240 return nullptr;
6241
6242 return CXXUnresolvedConstructExpr::Create(
6243 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6244 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6245 Importer.Import(CE->getRParenLoc()));
6246}
6247
6248Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006249 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006250 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6251 if (E->getNamingClass() && !NamingClass)
6252 return nullptr;
6253
6254 DeclarationName Name = Importer.Import(E->getName());
6255 if (E->getName() && !Name)
6256 return nullptr;
6257
6258 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6259 // Import additional name location/type info.
6260 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6261
6262 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006263 for (auto *D : E->decls()) {
6264 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006265 ToDecls.addDecl(To);
6266 else
6267 return nullptr;
6268 }
6269
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006270 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006271 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006272 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6273 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006274 return nullptr;
6275 ResInfo = &ToTAInfo;
6276 }
6277
6278 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6279 return UnresolvedLookupExpr::Create(
6280 Importer.getToContext(), NamingClass,
6281 Importer.Import(E->getQualifierLoc()),
6282 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6283 ResInfo, ToDecls.begin(), ToDecls.end());
6284
6285 return UnresolvedLookupExpr::Create(
6286 Importer.getToContext(), NamingClass,
6287 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6288 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6289}
6290
Peter Szecsice7f3182018-05-07 12:08:27 +00006291Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6292 DeclarationName Name = Importer.Import(E->getName());
6293 if (!E->getName().isEmpty() && Name.isEmpty())
6294 return nullptr;
6295 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6296 // Import additional name location/type info.
6297 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6298
6299 QualType BaseType = Importer.Import(E->getType());
6300 if (!E->getType().isNull() && BaseType.isNull())
6301 return nullptr;
6302
6303 UnresolvedSet<8> ToDecls;
6304 for (Decl *D : E->decls()) {
6305 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6306 ToDecls.addDecl(To);
6307 else
6308 return nullptr;
6309 }
6310
6311 TemplateArgumentListInfo ToTAInfo;
6312 TemplateArgumentListInfo *ResInfo = nullptr;
6313 if (E->hasExplicitTemplateArgs()) {
6314 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6315 return nullptr;
6316 ResInfo = &ToTAInfo;
6317 }
6318
6319 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6320 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6321 return nullptr;
6322 }
6323
6324 return UnresolvedMemberExpr::Create(
6325 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6326 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6327 Importer.Import(E->getQualifierLoc()),
6328 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6329 ToDecls.begin(), ToDecls.end());
6330}
6331
Sean Callanan59721b32015-04-28 18:41:46 +00006332Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6333 QualType T = Importer.Import(E->getType());
6334 if (T.isNull())
6335 return nullptr;
6336
6337 Expr *ToCallee = Importer.Import(E->getCallee());
6338 if (!ToCallee && E->getCallee())
6339 return nullptr;
6340
6341 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006342 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006343 if (ImportContainerChecked(E->arguments(), ToArgs))
6344 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006345
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006346 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006347
6348 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6349 ToArgs_Copied[ai] = ToArgs[ai];
6350
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006351 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6352 return new (Importer.getToContext()) CXXOperatorCallExpr(
6353 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6354 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6355 OCE->getFPFeatures());
6356 }
6357
Sean Callanan59721b32015-04-28 18:41:46 +00006358 return new (Importer.getToContext())
6359 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006360 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006361 Importer.Import(E->getRParenLoc()));
6362}
6363
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006364Optional<LambdaCapture>
6365ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6366 VarDecl *Var = nullptr;
6367 if (From.capturesVariable()) {
6368 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6369 if (!Var)
6370 return None;
6371 }
6372
6373 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6374 From.getCaptureKind(), Var,
6375 From.isPackExpansion()
6376 ? Importer.Import(From.getEllipsisLoc())
6377 : SourceLocation());
6378}
6379
6380Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6381 CXXRecordDecl *FromClass = LE->getLambdaClass();
6382 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6383 if (!ToClass)
6384 return nullptr;
6385
6386 // NOTE: lambda classes are created with BeingDefined flag set up.
6387 // It means that ImportDefinition doesn't work for them and we should fill it
6388 // manually.
6389 if (ToClass->isBeingDefined()) {
6390 for (auto FromField : FromClass->fields()) {
6391 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6392 if (!ToField)
6393 return nullptr;
6394 }
6395 }
6396
6397 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6398 Importer.Import(LE->getCallOperator()));
6399 if (!ToCallOp)
6400 return nullptr;
6401
6402 ToClass->completeDefinition();
6403
6404 unsigned NumCaptures = LE->capture_size();
6405 SmallVector<LambdaCapture, 8> Captures;
6406 Captures.reserve(NumCaptures);
6407 for (const auto &FromCapture : LE->captures()) {
6408 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6409 Captures.push_back(*ToCapture);
6410 else
6411 return nullptr;
6412 }
6413
6414 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6415 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6416 return nullptr;
6417
6418 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6419 Importer.Import(LE->getIntroducerRange()),
6420 LE->getCaptureDefault(),
6421 Importer.Import(LE->getCaptureDefaultLoc()),
6422 Captures,
6423 LE->hasExplicitParameters(),
6424 LE->hasExplicitResultType(),
6425 InitCaptures,
6426 Importer.Import(LE->getLocEnd()),
6427 LE->containsUnexpandedParameterPack());
6428}
6429
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006430Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6431 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006432 if (T.isNull())
6433 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006434
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006435 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006436 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006437 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006438
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006439 ASTContext &ToCtx = Importer.getToContext();
6440 InitListExpr *To = new (ToCtx) InitListExpr(
6441 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6442 Exprs, Importer.Import(ILE->getLBraceLoc()));
6443 To->setType(T);
6444
6445 if (ILE->hasArrayFiller()) {
6446 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6447 if (!Filler)
6448 return nullptr;
6449 To->setArrayFiller(Filler);
6450 }
6451
6452 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006453 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006454 if (!ToFD)
6455 return nullptr;
6456 To->setInitializedFieldInUnion(ToFD);
6457 }
6458
6459 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006460 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006461 if (!ToSyntForm)
6462 return nullptr;
6463 To->setSyntacticForm(ToSyntForm);
6464 }
6465
6466 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6467 To->setValueDependent(ILE->isValueDependent());
6468 To->setInstantiationDependent(ILE->isInstantiationDependent());
6469
6470 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006471}
6472
Richard Smith30e304e2016-12-14 00:03:17 +00006473Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6474 QualType ToType = Importer.Import(E->getType());
6475 if (ToType.isNull())
6476 return nullptr;
6477
6478 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6479 if (!ToCommon && E->getCommonExpr())
6480 return nullptr;
6481
6482 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6483 if (!ToSubExpr && E->getSubExpr())
6484 return nullptr;
6485
6486 return new (Importer.getToContext())
6487 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6488}
6489
6490Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6491 QualType ToType = Importer.Import(E->getType());
6492 if (ToType.isNull())
6493 return nullptr;
6494 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6495}
6496
Sean Callanandd2c1742016-05-16 20:48:03 +00006497Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006498 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006499 if (!ToField && DIE->getField())
6500 return nullptr;
6501
6502 return CXXDefaultInitExpr::Create(
6503 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6504}
6505
6506Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6507 QualType ToType = Importer.Import(E->getType());
6508 if (ToType.isNull() && !E->getType().isNull())
6509 return nullptr;
6510 ExprValueKind VK = E->getValueKind();
6511 CastKind CK = E->getCastKind();
6512 Expr *ToOp = Importer.Import(E->getSubExpr());
6513 if (!ToOp && E->getSubExpr())
6514 return nullptr;
6515 CXXCastPath BasePath;
6516 if (ImportCastPath(E, BasePath))
6517 return nullptr;
6518 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6519 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6520 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6521 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6522
6523 if (isa<CXXStaticCastExpr>(E)) {
6524 return CXXStaticCastExpr::Create(
6525 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6526 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6527 } else if (isa<CXXDynamicCastExpr>(E)) {
6528 return CXXDynamicCastExpr::Create(
6529 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6530 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6531 } else if (isa<CXXReinterpretCastExpr>(E)) {
6532 return CXXReinterpretCastExpr::Create(
6533 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6534 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6535 } else {
6536 return nullptr;
6537 }
6538}
6539
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006540Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6541 SubstNonTypeTemplateParmExpr *E) {
6542 QualType T = Importer.Import(E->getType());
6543 if (T.isNull())
6544 return nullptr;
6545
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006546 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006547 Importer.Import(E->getParameter()));
6548 if (!Param)
6549 return nullptr;
6550
6551 Expr *Replacement = Importer.Import(E->getReplacement());
6552 if (!Replacement)
6553 return nullptr;
6554
6555 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6556 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6557 Replacement);
6558}
6559
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006560Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6561 QualType ToType = Importer.Import(E->getType());
6562 if (ToType.isNull())
6563 return nullptr;
6564
6565 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6566 if (ImportContainerChecked(E->getArgs(), ToArgs))
6567 return nullptr;
6568
6569 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6570 // Value is always false.
6571 bool ToValue = false;
6572 if (!E->isValueDependent())
6573 ToValue = E->getValue();
6574
6575 return TypeTraitExpr::Create(
6576 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6577 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6578}
6579
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006580Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6581 QualType ToType = Importer.Import(E->getType());
6582 if (ToType.isNull())
6583 return nullptr;
6584
6585 if (E->isTypeOperand()) {
6586 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6587 if (!TSI)
6588 return nullptr;
6589
6590 return new (Importer.getToContext())
6591 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6592 }
6593
6594 Expr *Op = Importer.Import(E->getExprOperand());
6595 if (!Op)
6596 return nullptr;
6597
6598 return new (Importer.getToContext())
6599 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6600}
6601
Lang Hames19e07e12017-06-20 21:06:00 +00006602void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6603 CXXMethodDecl *FromMethod) {
6604 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6605 ToMethod->addOverriddenMethod(
6606 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6607 FromOverriddenMethod))));
6608}
6609
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006610ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006611 ASTContext &FromContext, FileManager &FromFileManager,
6612 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006613 : ToContext(ToContext), FromContext(FromContext),
6614 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6615 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006616 ImportedDecls[FromContext.getTranslationUnitDecl()]
6617 = ToContext.getTranslationUnitDecl();
6618}
6619
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006620ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006621
6622QualType ASTImporter::Import(QualType FromT) {
6623 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006624 return {};
John McCall424cec92011-01-19 06:33:43 +00006625
6626 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006627
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006628 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006629 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6630 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006631 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006632 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006633
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006634 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006635 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006636 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006637 if (ToT.isNull())
6638 return ToT;
6639
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006640 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006641 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006642
John McCall424cec92011-01-19 06:33:43 +00006643 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006644}
6645
Douglas Gregor62d311f2010-02-09 19:21:46 +00006646TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006647 if (!FromTSI)
6648 return FromTSI;
6649
6650 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006651 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006652 QualType T = Import(FromTSI->getType());
6653 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006654 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006655
6656 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006657 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006658}
6659
Sean Callanan59721b32015-04-28 18:41:46 +00006660Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6661 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6662 if (Pos != ImportedDecls.end()) {
6663 Decl *ToD = Pos->second;
6664 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6665 return ToD;
6666 } else {
6667 return nullptr;
6668 }
6669}
6670
Douglas Gregor62d311f2010-02-09 19:21:46 +00006671Decl *ASTImporter::Import(Decl *FromD) {
6672 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006673 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006674
Douglas Gregord451ea92011-07-29 23:31:30 +00006675 ASTNodeImporter Importer(*this);
6676
Douglas Gregor62d311f2010-02-09 19:21:46 +00006677 // Check whether we've already imported this declaration.
6678 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006679 if (Pos != ImportedDecls.end()) {
6680 Decl *ToD = Pos->second;
6681 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6682 return ToD;
6683 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006684
6685 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006686 Decl *ToD = Importer.Visit(FromD);
6687 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006688 return nullptr;
6689
Douglas Gregor62d311f2010-02-09 19:21:46 +00006690 // Record the imported declaration.
6691 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006692 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006693 return ToD;
6694}
6695
6696DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6697 if (!FromDC)
6698 return FromDC;
6699
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006700 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006701 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006702 return nullptr;
6703
Douglas Gregor2e15c842012-02-01 21:00:38 +00006704 // When we're using a record/enum/Objective-C class/protocol as a context, we
6705 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006706 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6707 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006708 if (ToRecord->isCompleteDefinition()) {
6709 // Do nothing.
6710 } else if (FromRecord->isCompleteDefinition()) {
6711 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6712 ASTNodeImporter::IDK_Basic);
6713 } else {
6714 CompleteDecl(ToRecord);
6715 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006716 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6717 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006718 if (ToEnum->isCompleteDefinition()) {
6719 // Do nothing.
6720 } else if (FromEnum->isCompleteDefinition()) {
6721 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6722 ASTNodeImporter::IDK_Basic);
6723 } else {
6724 CompleteDecl(ToEnum);
6725 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006726 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6727 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006728 if (ToClass->getDefinition()) {
6729 // Do nothing.
6730 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6731 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6732 ASTNodeImporter::IDK_Basic);
6733 } else {
6734 CompleteDecl(ToClass);
6735 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006736 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6737 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006738 if (ToProto->getDefinition()) {
6739 // Do nothing.
6740 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6741 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6742 ASTNodeImporter::IDK_Basic);
6743 } else {
6744 CompleteDecl(ToProto);
6745 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006746 }
6747
6748 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006749}
6750
6751Expr *ASTImporter::Import(Expr *FromE) {
6752 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006753 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006754
6755 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6756}
6757
6758Stmt *ASTImporter::Import(Stmt *FromS) {
6759 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006760 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006761
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006762 // Check whether we've already imported this declaration.
6763 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6764 if (Pos != ImportedStmts.end())
6765 return Pos->second;
6766
6767 // Import the type
6768 ASTNodeImporter Importer(*this);
6769 Stmt *ToS = Importer.Visit(FromS);
6770 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006771 return nullptr;
6772
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006773 // Record the imported declaration.
6774 ImportedStmts[FromS] = ToS;
6775 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006776}
6777
6778NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6779 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006780 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006781
Douglas Gregor90ebf252011-04-27 16:48:40 +00006782 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6783
6784 switch (FromNNS->getKind()) {
6785 case NestedNameSpecifier::Identifier:
6786 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6787 return NestedNameSpecifier::Create(ToContext, prefix, II);
6788 }
Craig Topper36250ad2014-05-12 05:36:57 +00006789 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006790
6791 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006792 if (auto *NS =
6793 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006794 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6795 }
Craig Topper36250ad2014-05-12 05:36:57 +00006796 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006797
6798 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006799 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006800 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006801 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6802 }
Craig Topper36250ad2014-05-12 05:36:57 +00006803 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006804
6805 case NestedNameSpecifier::Global:
6806 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6807
Nikola Smiljanic67860242014-09-26 00:28:20 +00006808 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006809 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006810 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006811 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6812 }
6813 return nullptr;
6814
Douglas Gregor90ebf252011-04-27 16:48:40 +00006815 case NestedNameSpecifier::TypeSpec:
6816 case NestedNameSpecifier::TypeSpecWithTemplate: {
6817 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6818 if (!T.isNull()) {
6819 bool bTemplate = FromNNS->getKind() ==
6820 NestedNameSpecifier::TypeSpecWithTemplate;
6821 return NestedNameSpecifier::Create(ToContext, prefix,
6822 bTemplate, T.getTypePtr());
6823 }
6824 }
Craig Topper36250ad2014-05-12 05:36:57 +00006825 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006826 }
6827
6828 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006829}
6830
Douglas Gregor14454802011-02-25 02:25:35 +00006831NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006832 // Copied from NestedNameSpecifier mostly.
6833 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6834 NestedNameSpecifierLoc NNS = FromNNS;
6835
6836 // Push each of the nested-name-specifiers's onto a stack for
6837 // serialization in reverse order.
6838 while (NNS) {
6839 NestedNames.push_back(NNS);
6840 NNS = NNS.getPrefix();
6841 }
6842
6843 NestedNameSpecifierLocBuilder Builder;
6844
6845 while (!NestedNames.empty()) {
6846 NNS = NestedNames.pop_back_val();
6847 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6848 if (!Spec)
6849 return NestedNameSpecifierLoc();
6850
6851 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6852 switch (Kind) {
6853 case NestedNameSpecifier::Identifier:
6854 Builder.Extend(getToContext(),
6855 Spec->getAsIdentifier(),
6856 Import(NNS.getLocalBeginLoc()),
6857 Import(NNS.getLocalEndLoc()));
6858 break;
6859
6860 case NestedNameSpecifier::Namespace:
6861 Builder.Extend(getToContext(),
6862 Spec->getAsNamespace(),
6863 Import(NNS.getLocalBeginLoc()),
6864 Import(NNS.getLocalEndLoc()));
6865 break;
6866
6867 case NestedNameSpecifier::NamespaceAlias:
6868 Builder.Extend(getToContext(),
6869 Spec->getAsNamespaceAlias(),
6870 Import(NNS.getLocalBeginLoc()),
6871 Import(NNS.getLocalEndLoc()));
6872 break;
6873
6874 case NestedNameSpecifier::TypeSpec:
6875 case NestedNameSpecifier::TypeSpecWithTemplate: {
6876 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6877 QualType(Spec->getAsType(), 0));
6878 Builder.Extend(getToContext(),
6879 Import(NNS.getLocalBeginLoc()),
6880 TSI->getTypeLoc(),
6881 Import(NNS.getLocalEndLoc()));
6882 break;
6883 }
6884
6885 case NestedNameSpecifier::Global:
6886 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6887 break;
6888
6889 case NestedNameSpecifier::Super: {
6890 SourceRange ToRange = Import(NNS.getSourceRange());
6891 Builder.MakeSuper(getToContext(),
6892 Spec->getAsRecordDecl(),
6893 ToRange.getBegin(),
6894 ToRange.getEnd());
6895 }
6896 }
6897 }
6898
6899 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006900}
6901
Douglas Gregore2e50d332010-12-01 01:36:18 +00006902TemplateName ASTImporter::Import(TemplateName From) {
6903 switch (From.getKind()) {
6904 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006905 if (auto *ToTemplate =
6906 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006907 return TemplateName(ToTemplate);
6908
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006909 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006910
6911 case TemplateName::OverloadedTemplate: {
6912 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6913 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006914 for (auto *I : *FromStorage) {
6915 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006916 ToTemplates.addDecl(To);
6917 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006918 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006919 }
6920 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6921 ToTemplates.end());
6922 }
6923
6924 case TemplateName::QualifiedTemplate: {
6925 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6926 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6927 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006928 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006929
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006930 if (auto *ToTemplate =
6931 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006932 return ToContext.getQualifiedTemplateName(Qualifier,
6933 QTN->hasTemplateKeyword(),
6934 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006935
6936 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006937 }
6938
6939 case TemplateName::DependentTemplate: {
6940 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6941 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6942 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006943 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006944
6945 if (DTN->isIdentifier()) {
6946 return ToContext.getDependentTemplateName(Qualifier,
6947 Import(DTN->getIdentifier()));
6948 }
6949
6950 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6951 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006952
6953 case TemplateName::SubstTemplateTemplateParm: {
6954 SubstTemplateTemplateParmStorage *subst
6955 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006956 auto *param =
6957 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006958 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006959 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006960
6961 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006962 if (replacement.isNull())
6963 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006964
6965 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6966 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006967
6968 case TemplateName::SubstTemplateTemplateParmPack: {
6969 SubstTemplateTemplateParmPackStorage *SubstPack
6970 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006971 auto *Param =
6972 cast_or_null<TemplateTemplateParmDecl>(
6973 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00006974 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006975 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006976
6977 ASTNodeImporter Importer(*this);
6978 TemplateArgument ArgPack
6979 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6980 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006981 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006982
6983 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6984 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006985 }
6986
6987 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006988}
6989
Douglas Gregor62d311f2010-02-09 19:21:46 +00006990SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6991 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006992 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00006993
Douglas Gregor811663e2010-02-10 00:15:17 +00006994 SourceManager &FromSM = FromContext.getSourceManager();
6995
Sean Callanan24c5fe62016-11-07 20:42:25 +00006996 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006997 // don't have to import macro expansions.
6998 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006999 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007000 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7001 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007002 FileID ToFileID = Import(Decomposed.first);
7003 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007004 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007005 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7006 .getLocWithOffset(Decomposed.second);
7007 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007008}
7009
7010SourceRange ASTImporter::Import(SourceRange FromRange) {
7011 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7012}
7013
Douglas Gregor811663e2010-02-10 00:15:17 +00007014FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007015 llvm::DenseMap<FileID, FileID>::iterator Pos
7016 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007017 if (Pos != ImportedFileIDs.end())
7018 return Pos->second;
7019
7020 SourceManager &FromSM = FromContext.getSourceManager();
7021 SourceManager &ToSM = ToContext.getSourceManager();
7022 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007023 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007024
7025 // Include location of this file.
7026 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7027
7028 // Map the FileID for to the "to" source manager.
7029 FileID ToID;
7030 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007031 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007032 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7033 // disk again
7034 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7035 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007036 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007037 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007038 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007039 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7040 FromSLoc.getFile().getFileCharacteristic());
7041 } else {
7042 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007043 const llvm::MemoryBuffer *
7044 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007045 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007046 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007047 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007048 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007049 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007050 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007051
Sebastian Redl99219f12010-09-30 01:03:06 +00007052 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007053 return ToID;
7054}
7055
Sean Callanandd2c1742016-05-16 20:48:03 +00007056CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7057 Expr *ToExpr = Import(From->getInit());
7058 if (!ToExpr && From->getInit())
7059 return nullptr;
7060
7061 if (From->isBaseInitializer()) {
7062 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7063 if (!ToTInfo && From->getTypeSourceInfo())
7064 return nullptr;
7065
7066 return new (ToContext) CXXCtorInitializer(
7067 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7068 ToExpr, Import(From->getRParenLoc()),
7069 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7070 : SourceLocation());
7071 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007072 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007073 if (!ToField && From->getMember())
7074 return nullptr;
7075
7076 return new (ToContext) CXXCtorInitializer(
7077 ToContext, ToField, Import(From->getMemberLocation()),
7078 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7079 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007080 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007081 Import(From->getIndirectMember()));
7082 if (!ToIField && From->getIndirectMember())
7083 return nullptr;
7084
7085 return new (ToContext) CXXCtorInitializer(
7086 ToContext, ToIField, Import(From->getMemberLocation()),
7087 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7088 } else if (From->isDelegatingInitializer()) {
7089 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7090 if (!ToTInfo && From->getTypeSourceInfo())
7091 return nullptr;
7092
7093 return new (ToContext)
7094 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7095 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007096 } else {
7097 return nullptr;
7098 }
7099}
7100
Aleksei Sidorina693b372016-09-28 10:16:56 +00007101CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7102 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7103 if (Pos != ImportedCXXBaseSpecifiers.end())
7104 return Pos->second;
7105
7106 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7107 Import(BaseSpec->getSourceRange()),
7108 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7109 BaseSpec->getAccessSpecifierAsWritten(),
7110 Import(BaseSpec->getTypeSourceInfo()),
7111 Import(BaseSpec->getEllipsisLoc()));
7112 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7113 return Imported;
7114}
7115
Douglas Gregor0a791672011-01-18 03:11:38 +00007116void ASTImporter::ImportDefinition(Decl *From) {
7117 Decl *To = Import(From);
7118 if (!To)
7119 return;
7120
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007121 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007122 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007123
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007124 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007125 if (!ToRecord->getDefinition()) {
7126 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007127 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007128 return;
7129 }
7130 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007131
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007132 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007133 if (!ToEnum->getDefinition()) {
7134 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007135 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007136 return;
7137 }
7138 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007139
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007140 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007141 if (!ToIFace->getDefinition()) {
7142 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007143 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007144 return;
7145 }
7146 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007147
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007148 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007149 if (!ToProto->getDefinition()) {
7150 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007151 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007152 return;
7153 }
7154 }
7155
Douglas Gregor0a791672011-01-18 03:11:38 +00007156 Importer.ImportDeclContext(FromDC, true);
7157 }
7158}
7159
Douglas Gregor96e578d2010-02-05 17:54:41 +00007160DeclarationName ASTImporter::Import(DeclarationName FromName) {
7161 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007162 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007163
7164 switch (FromName.getNameKind()) {
7165 case DeclarationName::Identifier:
7166 return Import(FromName.getAsIdentifierInfo());
7167
7168 case DeclarationName::ObjCZeroArgSelector:
7169 case DeclarationName::ObjCOneArgSelector:
7170 case DeclarationName::ObjCMultiArgSelector:
7171 return Import(FromName.getObjCSelector());
7172
7173 case DeclarationName::CXXConstructorName: {
7174 QualType T = Import(FromName.getCXXNameType());
7175 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007176 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007177
7178 return ToContext.DeclarationNames.getCXXConstructorName(
7179 ToContext.getCanonicalType(T));
7180 }
7181
7182 case DeclarationName::CXXDestructorName: {
7183 QualType T = Import(FromName.getCXXNameType());
7184 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007185 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007186
7187 return ToContext.DeclarationNames.getCXXDestructorName(
7188 ToContext.getCanonicalType(T));
7189 }
7190
Richard Smith35845152017-02-07 01:37:30 +00007191 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007192 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007193 Import(FromName.getCXXDeductionGuideTemplate()));
7194 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007195 return {};
Richard Smith35845152017-02-07 01:37:30 +00007196 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7197 }
7198
Douglas Gregor96e578d2010-02-05 17:54:41 +00007199 case DeclarationName::CXXConversionFunctionName: {
7200 QualType T = Import(FromName.getCXXNameType());
7201 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007202 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007203
7204 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7205 ToContext.getCanonicalType(T));
7206 }
7207
7208 case DeclarationName::CXXOperatorName:
7209 return ToContext.DeclarationNames.getCXXOperatorName(
7210 FromName.getCXXOverloadedOperator());
7211
7212 case DeclarationName::CXXLiteralOperatorName:
7213 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7214 Import(FromName.getCXXLiteralIdentifier()));
7215
7216 case DeclarationName::CXXUsingDirective:
7217 // FIXME: STATICS!
7218 return DeclarationName::getUsingDirectiveName();
7219 }
7220
David Blaikiee4d798f2012-01-20 21:50:17 +00007221 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007222}
7223
Douglas Gregore2e50d332010-12-01 01:36:18 +00007224IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007225 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007226 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007227
Sean Callananf94ef1d2016-05-14 06:11:19 +00007228 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7229
7230 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7231 ToId->setBuiltinID(FromId->getBuiltinID());
7232
7233 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007234}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007235
Douglas Gregor43f54792010-02-17 02:12:47 +00007236Selector ASTImporter::Import(Selector FromSel) {
7237 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007238 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007239
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007240 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007241 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7242 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7243 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7244 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7245}
7246
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007247DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7248 DeclContext *DC,
7249 unsigned IDNS,
7250 NamedDecl **Decls,
7251 unsigned NumDecls) {
7252 return Name;
7253}
7254
7255DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007256 if (LastDiagFromFrom)
7257 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7258 FromContext.getDiagnostics());
7259 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007260 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007261}
7262
7263DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007264 if (!LastDiagFromFrom)
7265 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7266 ToContext.getDiagnostics());
7267 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007268 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007269}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007270
Douglas Gregor2e15c842012-02-01 21:00:38 +00007271void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007272 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007273 if (!ID->getDefinition())
7274 ID->startDefinition();
7275 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007276 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007277 if (!PD->getDefinition())
7278 PD->startDefinition();
7279 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007280 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007281 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7282 TD->startDefinition();
7283 TD->setCompleteDefinition(true);
7284 }
7285 }
7286 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007287 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007288 }
7289}
7290
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007291Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007292 if (From->hasAttrs()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007293 for (auto *FromAttr : From->getAttrs())
Sean Callanan8bca9962016-03-28 21:43:01 +00007294 To->addAttr(FromAttr->clone(To->getASTContext()));
7295 }
7296 if (From->isUsed()) {
7297 To->setIsUsed();
7298 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007299 if (From->isImplicit()) {
7300 To->setImplicit();
7301 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007302 ImportedDecls[From] = To;
7303 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007304}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007305
Douglas Gregordd6006f2012-07-17 21:16:27 +00007306bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7307 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007308 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007309 = ImportedTypes.find(From.getTypePtr());
7310 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7311 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007312
Douglas Gregordd6006f2012-07-17 21:16:27 +00007313 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7314 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007315 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007316}