blob: 254eb7643d4ec8a9c76abe32c449096409e265b8 [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) {}
Gabor Marton344b0992018-05-16 11:48:11 +000081
Douglas Gregor96e578d2010-02-05 17:54:41 +000082 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000149 /// What we should import from the definition.
Douglas Gregor95d82832012-01-24 18:36:04 +0000150 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000151 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000152 /// 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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000157 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000159 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000160 /// 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
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000911 TagDecl *OwnedTagDecl =
912 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
913 if (!OwnedTagDecl && T->getOwnedTagDecl())
914 return {};
915
Abramo Bagnara6150c882010-05-11 21:36:43 +0000916 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000917 ToQualifier, ToNamedType,
918 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000919}
920
Gabor Horvath7a91c082017-11-14 11:30:38 +0000921QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
922 QualType Pattern = Importer.Import(T->getPattern());
923 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000924 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +0000925
926 return Importer.getToContext().getPackExpansionType(Pattern,
927 T->getNumExpansions());
928}
929
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000930QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
931 const DependentTemplateSpecializationType *T) {
932 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
933 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000934 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000935
936 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
937 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000938 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000939
940 SmallVector<TemplateArgument, 2> ToPack;
941 ToPack.reserve(T->getNumArgs());
942 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000943 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000944
945 return Importer.getToContext().getDependentTemplateSpecializationType(
946 T->getKeyword(), Qualifier, Name, ToPack);
947}
948
Peter Szecsice7f3182018-05-07 12:08:27 +0000949QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
950 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
951 if (!NNS && T->getQualifier())
952 return QualType();
953
954 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
955 if (!Name && T->getIdentifier())
956 return QualType();
957
958 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
959 ? QualType()
960 : Importer.Import(T->getCanonicalTypeInternal());
961 if (!Canon.isNull())
962 Canon = Canon.getCanonicalType();
963
964 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
965 Name, Canon);
966}
967
John McCall424cec92011-01-19 06:33:43 +0000968QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000969 auto *Class =
970 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000971 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000972 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973
John McCall8b07ec22010-05-15 11:32:37 +0000974 return Importer.getToContext().getObjCInterfaceType(Class);
975}
976
John McCall424cec92011-01-19 06:33:43 +0000977QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000978 QualType ToBaseType = Importer.Import(T->getBaseType());
979 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000980 return {};
John McCall8b07ec22010-05-15 11:32:37 +0000981
Douglas Gregore9d95f12015-07-07 03:57:35 +0000982 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000983 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000984 QualType ImportedTypeArg = Importer.Import(TypeArg);
985 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000986 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +0000987
988 TypeArgs.push_back(ImportedTypeArg);
989 }
990
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000991 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000992 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000993 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000994 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000995 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000996 Protocols.push_back(Protocol);
997 }
998
Douglas Gregore9d95f12015-07-07 03:57:35 +0000999 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001000 Protocols,
1001 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001002}
1003
John McCall424cec92011-01-19 06:33:43 +00001004QualType
1005ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001006 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1007 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001008 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001009
John McCall8b07ec22010-05-15 11:32:37 +00001010 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001011}
1012
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001013//----------------------------------------------------------------------------
1014// Import Declarations
1015//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001016bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1017 DeclContext *&LexicalDC,
1018 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001019 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001020 SourceLocation &Loc) {
1021 // Import the context of this declaration.
1022 DC = Importer.ImportContext(D->getDeclContext());
1023 if (!DC)
1024 return true;
1025
1026 LexicalDC = DC;
1027 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1028 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1029 if (!LexicalDC)
1030 return true;
1031 }
1032
1033 // Import the name of this declaration.
1034 Name = Importer.Import(D->getDeclName());
1035 if (D->getDeclName() && !Name)
1036 return true;
1037
1038 // Import the location of this declaration.
1039 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001040 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001041 return false;
1042}
1043
Douglas Gregord451ea92011-07-29 23:31:30 +00001044void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1045 if (!FromD)
1046 return;
1047
1048 if (!ToD) {
1049 ToD = Importer.Import(FromD);
1050 if (!ToD)
1051 return;
1052 }
1053
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001054 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1055 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001056 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001057 ImportDefinition(FromRecord, ToRecord);
1058 }
1059 }
1060 return;
1061 }
1062
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001063 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1064 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001065 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1066 ImportDefinition(FromEnum, ToEnum);
1067 }
1068 }
1069 return;
1070 }
1071}
1072
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001073void
1074ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1075 DeclarationNameInfo& To) {
1076 // NOTE: To.Name and To.Loc are already imported.
1077 // We only have to import To.LocInfo.
1078 switch (To.getName().getNameKind()) {
1079 case DeclarationName::Identifier:
1080 case DeclarationName::ObjCZeroArgSelector:
1081 case DeclarationName::ObjCOneArgSelector:
1082 case DeclarationName::ObjCMultiArgSelector:
1083 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001084 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001085 return;
1086
1087 case DeclarationName::CXXOperatorName: {
1088 SourceRange Range = From.getCXXOperatorNameRange();
1089 To.setCXXOperatorNameRange(Importer.Import(Range));
1090 return;
1091 }
1092 case DeclarationName::CXXLiteralOperatorName: {
1093 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1094 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1095 return;
1096 }
1097 case DeclarationName::CXXConstructorName:
1098 case DeclarationName::CXXDestructorName:
1099 case DeclarationName::CXXConversionFunctionName: {
1100 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1101 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1102 return;
1103 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001104 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001105 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001106}
1107
Douglas Gregor2e15c842012-02-01 21:00:38 +00001108void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001109 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001110 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001111 return;
1112 }
1113
Aaron Ballman629afae2014-03-07 19:56:05 +00001114 for (auto *From : FromDC->decls())
1115 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001116}
1117
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001118static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1119 ASTImporter &Importer) {
1120 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1121 auto *ToTypedef =
1122 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1123 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1124
1125 To->setTypedefNameForAnonDecl(ToTypedef);
1126 }
1127}
1128
Douglas Gregord451ea92011-07-29 23:31:30 +00001129bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001130 ImportDefinitionKind Kind) {
1131 if (To->getDefinition() || To->isBeingDefined()) {
1132 if (Kind == IDK_Everything)
1133 ImportDeclContext(From, /*ForceImport=*/true);
1134
Douglas Gregore2e50d332010-12-01 01:36:18 +00001135 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001136 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001137
1138 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001139
1140 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001141
1142 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001143 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1144 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001145
1146 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1147 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1148 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001149 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001150 ToData.Aggregate = FromData.Aggregate;
1151 ToData.PlainOldData = FromData.PlainOldData;
1152 ToData.Empty = FromData.Empty;
1153 ToData.Polymorphic = FromData.Polymorphic;
1154 ToData.Abstract = FromData.Abstract;
1155 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001156 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1157 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1158 ToData.HasBasesWithNonStaticDataMembers =
1159 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001160 ToData.HasPrivateFields = FromData.HasPrivateFields;
1161 ToData.HasProtectedFields = FromData.HasProtectedFields;
1162 ToData.HasPublicFields = FromData.HasPublicFields;
1163 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001164 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001165 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001166 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001167 ToData.HasUninitializedReferenceMember
1168 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001169 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001170 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1171 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001172 ToData.NeedOverloadResolutionForCopyConstructor
1173 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001174 ToData.NeedOverloadResolutionForMoveConstructor
1175 = FromData.NeedOverloadResolutionForMoveConstructor;
1176 ToData.NeedOverloadResolutionForMoveAssignment
1177 = FromData.NeedOverloadResolutionForMoveAssignment;
1178 ToData.NeedOverloadResolutionForDestructor
1179 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001180 ToData.DefaultedCopyConstructorIsDeleted
1181 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001182 ToData.DefaultedMoveConstructorIsDeleted
1183 = FromData.DefaultedMoveConstructorIsDeleted;
1184 ToData.DefaultedMoveAssignmentIsDeleted
1185 = FromData.DefaultedMoveAssignmentIsDeleted;
1186 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001187 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1188 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001189 ToData.HasConstexprNonCopyMoveConstructor
1190 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001191 ToData.HasDefaultedDefaultConstructor
1192 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001193 ToData.DefaultedDefaultConstructorIsConstexpr
1194 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001195 ToData.HasConstexprDefaultConstructor
1196 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001197 ToData.HasNonLiteralTypeFieldsOrBases
1198 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001199 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001200 ToData.UserProvidedDefaultConstructor
1201 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001202 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001203 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1204 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1205 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1206 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001207 ToData.ImplicitCopyAssignmentHasConstParam
1208 = FromData.ImplicitCopyAssignmentHasConstParam;
1209 ToData.HasDeclaredCopyConstructorWithConstParam
1210 = FromData.HasDeclaredCopyConstructorWithConstParam;
1211 ToData.HasDeclaredCopyAssignmentWithConstParam
1212 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001213
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001214 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 for (const auto &Base1 : FromCXX->bases()) {
1216 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001217 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001218 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001219
1220 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001221 if (Base1.isPackExpansion())
1222 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001223
1224 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001225 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001226
Douglas Gregore2e50d332010-12-01 01:36:18 +00001227 Bases.push_back(
1228 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001229 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1230 Base1.isVirtual(),
1231 Base1.isBaseOfClass(),
1232 Base1.getAccessSpecifierAsWritten(),
1233 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001234 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001235 }
1236 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001237 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001238 }
1239
Douglas Gregor2e15c842012-02-01 21:00:38 +00001240 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001241 ImportDeclContext(From, /*ForceImport=*/true);
1242
Douglas Gregore2e50d332010-12-01 01:36:18 +00001243 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001244 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001245}
1246
Larisse Voufo39a1e502013-08-06 01:03:05 +00001247bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1248 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001249 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001250 return false;
1251
1252 // FIXME: Can we really import any initializer? Alternatively, we could force
1253 // ourselves to import every declaration of a variable and then only use
1254 // getInit() here.
1255 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1256
1257 // FIXME: Other bits to merge?
1258
1259 return false;
1260}
1261
Douglas Gregord451ea92011-07-29 23:31:30 +00001262bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001263 ImportDefinitionKind Kind) {
1264 if (To->getDefinition() || To->isBeingDefined()) {
1265 if (Kind == IDK_Everything)
1266 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001267 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001268 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001269
1270 To->startDefinition();
1271
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001272 setTypedefNameForAnonDecl(From, To, Importer);
1273
Douglas Gregord451ea92011-07-29 23:31:30 +00001274 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1275 if (T.isNull())
1276 return true;
1277
1278 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1279 if (ToPromotionType.isNull())
1280 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001281
1282 if (shouldForceImportDeclContext(Kind))
1283 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001284
1285 // FIXME: we might need to merge the number of positive or negative bits
1286 // if the enumerator lists don't match.
1287 To->completeDefinition(T, ToPromotionType,
1288 From->getNumPositiveBits(),
1289 From->getNumNegativeBits());
1290 return false;
1291}
1292
Douglas Gregora082a492010-11-30 19:14:50 +00001293TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1294 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001295 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1296 if (ImportContainerChecked(*Params, ToParams))
1297 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001298
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001299 Expr *ToRequiresClause;
1300 if (Expr *const R = Params->getRequiresClause()) {
1301 ToRequiresClause = Importer.Import(R);
1302 if (!ToRequiresClause)
1303 return nullptr;
1304 } else {
1305 ToRequiresClause = nullptr;
1306 }
1307
Douglas Gregora082a492010-11-30 19:14:50 +00001308 return TemplateParameterList::Create(Importer.getToContext(),
1309 Importer.Import(Params->getTemplateLoc()),
1310 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001311 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001312 Importer.Import(Params->getRAngleLoc()),
1313 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001314}
1315
Douglas Gregore2e50d332010-12-01 01:36:18 +00001316TemplateArgument
1317ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1318 switch (From.getKind()) {
1319 case TemplateArgument::Null:
1320 return TemplateArgument();
1321
1322 case TemplateArgument::Type: {
1323 QualType ToType = Importer.Import(From.getAsType());
1324 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001325 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001326 return TemplateArgument(ToType);
1327 }
1328
1329 case TemplateArgument::Integral: {
1330 QualType ToType = Importer.Import(From.getIntegralType());
1331 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001332 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001333 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001334 }
1335
Eli Friedmanb826a002012-09-26 02:36:12 +00001336 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001337 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001338 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1339 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001340 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001341 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001342 }
1343
1344 case TemplateArgument::NullPtr: {
1345 QualType ToType = Importer.Import(From.getNullPtrType());
1346 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001347 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001348 return TemplateArgument(ToType, /*isNullPtr*/true);
1349 }
1350
Douglas Gregore2e50d332010-12-01 01:36:18 +00001351 case TemplateArgument::Template: {
1352 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1353 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001354 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001355
1356 return TemplateArgument(ToTemplate);
1357 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001358
1359 case TemplateArgument::TemplateExpansion: {
1360 TemplateName ToTemplate
1361 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1362 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001363 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001364
Douglas Gregore1d60df2011-01-14 23:41:42 +00001365 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001366 }
1367
Douglas Gregore2e50d332010-12-01 01:36:18 +00001368 case TemplateArgument::Expression:
1369 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1370 return TemplateArgument(ToExpr);
1371 return TemplateArgument();
1372
1373 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001374 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001375 ToPack.reserve(From.pack_size());
1376 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001377 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001378
1379 return TemplateArgument(
1380 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001381 }
1382 }
1383
1384 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001385}
1386
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001387Optional<TemplateArgumentLoc>
1388ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001389 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1390 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1391 TemplateArgumentLocInfo ToInfo;
1392 if (Arg.getKind() == TemplateArgument::Expression) {
1393 Expr *E = Importer.Import(FromInfo.getAsExpr());
1394 ToInfo = TemplateArgumentLocInfo(E);
1395 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001396 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001397 } else if (Arg.getKind() == TemplateArgument::Type) {
1398 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1399 ToInfo = TemplateArgumentLocInfo(TSI);
1400 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001401 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001402 } else {
1403 ToInfo = TemplateArgumentLocInfo(
1404 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1405 Importer.Import(FromInfo.getTemplateNameLoc()),
1406 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1407 }
1408 return TemplateArgumentLoc(Arg, ToInfo);
1409}
1410
Douglas Gregore2e50d332010-12-01 01:36:18 +00001411bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1412 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001413 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001414 for (unsigned I = 0; I != NumFromArgs; ++I) {
1415 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1416 if (To.isNull() && !FromArgs[I].isNull())
1417 return true;
1418
1419 ToArgs.push_back(To);
1420 }
1421
1422 return false;
1423}
1424
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001425// We cannot use Optional<> pattern here and below because
1426// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1427// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001428template <typename InContainerTy>
1429bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1430 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1431 for (const auto &FromLoc : Container) {
1432 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1433 ToTAInfo.addArgument(*ToLoc);
1434 else
1435 return true;
1436 }
1437 return false;
1438}
1439
Douglas Gregor5c73e912010-02-11 00:48:18 +00001440bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001441 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001442 // Eliminate a potential failure point where we attempt to re-import
1443 // something we're trying to import while completing ToRecord.
1444 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1445 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001446 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001447 if (ToOriginRecord)
1448 ToRecord = ToOriginRecord;
1449 }
1450
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001451 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001452 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001453 Importer.getNonEquivalentDecls(),
1454 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001455 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001456}
1457
Larisse Voufo39a1e502013-08-06 01:03:05 +00001458bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1459 bool Complain) {
1460 StructuralEquivalenceContext Ctx(
1461 Importer.getFromContext(), Importer.getToContext(),
1462 Importer.getNonEquivalentDecls(), false, Complain);
1463 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1464}
1465
Douglas Gregor98c10182010-02-12 22:17:39 +00001466bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001467 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001468 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001469 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001470 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001471}
1472
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001473bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1474 FunctionTemplateDecl *To) {
1475 StructuralEquivalenceContext Ctx(
1476 Importer.getFromContext(), Importer.getToContext(),
1477 Importer.getNonEquivalentDecls(), false, false);
1478 return Ctx.IsStructurallyEquivalent(From, To);
1479}
1480
Douglas Gregor91155082012-11-14 22:29:20 +00001481bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001482 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001483 const llvm::APSInt &FromVal = FromEC->getInitVal();
1484 const llvm::APSInt &ToVal = ToEC->getInitVal();
1485
1486 return FromVal.isSigned() == ToVal.isSigned() &&
1487 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1488 FromVal == ToVal;
1489}
1490
1491bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001492 ClassTemplateDecl *To) {
1493 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1494 Importer.getToContext(),
1495 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001496 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001497}
1498
Larisse Voufo39a1e502013-08-06 01:03:05 +00001499bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1500 VarTemplateDecl *To) {
1501 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1502 Importer.getToContext(),
1503 Importer.getNonEquivalentDecls());
1504 return Ctx.IsStructurallyEquivalent(From, To);
1505}
1506
Douglas Gregore4c83e42010-02-09 22:48:33 +00001507Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001508 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001509 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001510 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001511}
1512
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001513Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1514 // Import the context of this declaration.
1515 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1516 if (!DC)
1517 return nullptr;
1518
1519 DeclContext *LexicalDC = DC;
1520 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1521 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1522 if (!LexicalDC)
1523 return nullptr;
1524 }
1525
1526 // Import the location of this declaration.
1527 SourceLocation Loc = Importer.Import(D->getLocation());
1528
1529 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1530 ToD->setLexicalDeclContext(LexicalDC);
1531 Importer.Imported(D, ToD);
1532 LexicalDC->addDeclInternal(ToD);
1533 return ToD;
1534}
1535
Sean Callanan65198272011-11-17 23:20:56 +00001536Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1537 TranslationUnitDecl *ToD =
1538 Importer.getToContext().getTranslationUnitDecl();
1539
1540 Importer.Imported(D, ToD);
1541
1542 return ToD;
1543}
1544
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001545Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001546 SourceLocation Loc = Importer.Import(D->getLocation());
1547 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1548
1549 // Import the context of this declaration.
1550 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1551 if (!DC)
1552 return nullptr;
1553
1554 AccessSpecDecl *accessSpecDecl
1555 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1556 DC, Loc, ColonLoc);
1557
1558 if (!accessSpecDecl)
1559 return nullptr;
1560
1561 // Lexical DeclContext and Semantic DeclContext
1562 // is always the same for the accessSpec.
1563 accessSpecDecl->setLexicalDeclContext(DC);
1564 DC->addDeclInternal(accessSpecDecl);
1565
1566 return accessSpecDecl;
1567}
1568
Aleksei Sidorina693b372016-09-28 10:16:56 +00001569Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1570 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1571 if (!DC)
1572 return nullptr;
1573
1574 DeclContext *LexicalDC = DC;
1575
1576 // Import the location of this declaration.
1577 SourceLocation Loc = Importer.Import(D->getLocation());
1578
1579 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1580 if (!AssertExpr)
1581 return nullptr;
1582
1583 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001584 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001585 if (!ToMsg && FromMsg)
1586 return nullptr;
1587
1588 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1589 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1590 Importer.Import(D->getRParenLoc()), D->isFailed());
1591
1592 ToD->setLexicalDeclContext(LexicalDC);
1593 LexicalDC->addDeclInternal(ToD);
1594 Importer.Imported(D, ToD);
1595 return ToD;
1596}
1597
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001598Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1599 // Import the major distinguishing characteristics of this namespace.
1600 DeclContext *DC, *LexicalDC;
1601 DeclarationName Name;
1602 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001603 NamedDecl *ToD;
1604 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001605 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001606 if (ToD)
1607 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001608
1609 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001610 if (!Name) {
1611 // This is an anonymous namespace. Adopt an existing anonymous
1612 // namespace if we can.
1613 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001614 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001615 MergeWithNamespace = TU->getAnonymousNamespace();
1616 else
1617 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1618 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001619 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001620 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001621 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001622 for (auto *FoundDecl : FoundDecls) {
1623 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001624 continue;
1625
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001626 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001627 MergeWithNamespace = FoundNS;
1628 ConflictingDecls.clear();
1629 break;
1630 }
1631
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001632 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001633 }
1634
1635 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001636 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001637 ConflictingDecls.data(),
1638 ConflictingDecls.size());
1639 }
1640 }
1641
1642 // Create the "to" namespace, if needed.
1643 NamespaceDecl *ToNamespace = MergeWithNamespace;
1644 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001645 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001646 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001647 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001648 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001649 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001650 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001651 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001652
1653 // If this is an anonymous namespace, register it as the anonymous
1654 // namespace within its context.
1655 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001656 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001657 TU->setAnonymousNamespace(ToNamespace);
1658 else
1659 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1660 }
1661 }
1662 Importer.Imported(D, ToNamespace);
1663
1664 ImportDeclContext(D);
1665
1666 return ToNamespace;
1667}
1668
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001669Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1670 // Import the major distinguishing characteristics of this namespace.
1671 DeclContext *DC, *LexicalDC;
1672 DeclarationName Name;
1673 SourceLocation Loc;
1674 NamedDecl *LookupD;
1675 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1676 return nullptr;
1677 if (LookupD)
1678 return LookupD;
1679
1680 // NOTE: No conflict resolution is done for namespace aliases now.
1681
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001682 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1683 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001684 if (!TargetDecl)
1685 return nullptr;
1686
1687 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1688 if (!ToII)
1689 return nullptr;
1690
1691 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1692 if (D->getQualifierLoc() && !ToQLoc)
1693 return nullptr;
1694
1695 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1696 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1697 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1698 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1699
1700 ToD->setLexicalDeclContext(LexicalDC);
1701 Importer.Imported(D, ToD);
1702 LexicalDC->addDeclInternal(ToD);
1703
1704 return ToD;
1705}
1706
Richard Smithdda56e42011-04-15 14:24:37 +00001707Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001708 // Import the major distinguishing characteristics of this typedef.
1709 DeclContext *DC, *LexicalDC;
1710 DeclarationName Name;
1711 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001712 NamedDecl *ToD;
1713 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001714 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001715 if (ToD)
1716 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001717
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001718 // If this typedef is not in block scope, determine whether we've
1719 // seen a typedef with the same name (that we can merge with) or any
1720 // other entity by that name (which name lookup could conflict with).
1721 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001722 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001723 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001724 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001725 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001726 for (auto *FoundDecl : FoundDecls) {
1727 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001728 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001729 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001730 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1731 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001732 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001733 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001734
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001735 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001736 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001737
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001738 if (!ConflictingDecls.empty()) {
1739 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1740 ConflictingDecls.data(),
1741 ConflictingDecls.size());
1742 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001743 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001744 }
1745 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001746
Douglas Gregorb4964f72010-02-15 23:54:17 +00001747 // Import the underlying type of this typedef;
1748 QualType T = Importer.Import(D->getUnderlyingType());
1749 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001750 return nullptr;
1751
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001752 // Some nodes (like anonymous tags referred by typedefs) are allowed to
1753 // import their enclosing typedef directly. Check if this is the case.
1754 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
1755 return AlreadyImported;
1756
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001757 // Create the new typedef node.
1758 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001759 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001760 TypedefNameDecl *ToTypedef;
1761 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001762 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1763 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001764 else
Richard Smithdda56e42011-04-15 14:24:37 +00001765 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1766 StartL, Loc,
1767 Name.getAsIdentifierInfo(),
1768 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001769
Douglas Gregordd483172010-02-22 17:42:47 +00001770 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001771 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001772 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001773
1774 // Templated declarations should not appear in DeclContext.
1775 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1776 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1777 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001778
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001779 return ToTypedef;
1780}
1781
Richard Smithdda56e42011-04-15 14:24:37 +00001782Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1783 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1784}
1785
1786Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1787 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1788}
1789
Gabor Horvath7a91c082017-11-14 11:30:38 +00001790Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1791 // Import the major distinguishing characteristics of this typedef.
1792 DeclContext *DC, *LexicalDC;
1793 DeclarationName Name;
1794 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001795 NamedDecl *FoundD;
1796 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001797 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001798 if (FoundD)
1799 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001800
1801 // If this typedef is not in block scope, determine whether we've
1802 // seen a typedef with the same name (that we can merge with) or any
1803 // other entity by that name (which name lookup could conflict with).
1804 if (!DC->isFunctionOrMethod()) {
1805 SmallVector<NamedDecl *, 4> ConflictingDecls;
1806 unsigned IDNS = Decl::IDNS_Ordinary;
1807 SmallVector<NamedDecl *, 2> FoundDecls;
1808 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001809 for (auto *FoundDecl : FoundDecls) {
1810 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001811 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001812 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001813 return Importer.Imported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001814 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001815 }
1816
1817 if (!ConflictingDecls.empty()) {
1818 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1819 ConflictingDecls.data(),
1820 ConflictingDecls.size());
1821 if (!Name)
1822 return nullptr;
1823 }
1824 }
1825
1826 TemplateParameterList *Params = ImportTemplateParameterList(
1827 D->getTemplateParameters());
1828 if (!Params)
1829 return nullptr;
1830
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001831 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001832 Importer.Import(D->getTemplatedDecl()));
1833 if (!TemplDecl)
1834 return nullptr;
1835
1836 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1837 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1838
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001839 TemplDecl->setDescribedAliasTemplate(ToAlias);
1840
Gabor Horvath7a91c082017-11-14 11:30:38 +00001841 ToAlias->setAccess(D->getAccess());
1842 ToAlias->setLexicalDeclContext(LexicalDC);
1843 Importer.Imported(D, ToAlias);
1844 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001845 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001846}
1847
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001848Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1849 // Import the major distinguishing characteristics of this label.
1850 DeclContext *DC, *LexicalDC;
1851 DeclarationName Name;
1852 SourceLocation Loc;
1853 NamedDecl *ToD;
1854 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1855 return nullptr;
1856 if (ToD)
1857 return ToD;
1858
1859 assert(LexicalDC->isFunctionOrMethod());
1860
1861 LabelDecl *ToLabel = D->isGnuLocal()
1862 ? LabelDecl::Create(Importer.getToContext(),
1863 DC, Importer.Import(D->getLocation()),
1864 Name.getAsIdentifierInfo(),
1865 Importer.Import(D->getLocStart()))
1866 : LabelDecl::Create(Importer.getToContext(),
1867 DC, Importer.Import(D->getLocation()),
1868 Name.getAsIdentifierInfo());
1869 Importer.Imported(D, ToLabel);
1870
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001871 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001872 if (!Label)
1873 return nullptr;
1874
1875 ToLabel->setStmt(Label);
1876 ToLabel->setLexicalDeclContext(LexicalDC);
1877 LexicalDC->addDeclInternal(ToLabel);
1878 return ToLabel;
1879}
1880
Douglas Gregor98c10182010-02-12 22:17:39 +00001881Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1882 // Import the major distinguishing characteristics of this enum.
1883 DeclContext *DC, *LexicalDC;
1884 DeclarationName Name;
1885 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001886 NamedDecl *ToD;
1887 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001888 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001889 if (ToD)
1890 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001891
Douglas Gregor98c10182010-02-12 22:17:39 +00001892 // Figure out what enum name we're looking for.
1893 unsigned IDNS = Decl::IDNS_Tag;
1894 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001895 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1896 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001897 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001898 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001899 IDNS |= Decl::IDNS_Ordinary;
1900
1901 // We may already have an enum of the same name; try to find and match it.
1902 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001903 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001904 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001905 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001906 for (auto *FoundDecl : FoundDecls) {
1907 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001908 continue;
1909
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001910 Decl *Found = FoundDecl;
1911 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1912 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00001913 Found = Tag->getDecl();
1914 }
1915
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001916 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001917 if (IsStructuralMatch(D, FoundEnum))
1918 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001919 }
1920
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001921 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00001922 }
1923
1924 if (!ConflictingDecls.empty()) {
1925 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1926 ConflictingDecls.data(),
1927 ConflictingDecls.size());
1928 }
1929 }
1930
1931 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001932 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1933 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001934 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001935 D->isScoped(), D->isScopedUsingClassTag(),
1936 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001937 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001938 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001939 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001940 D2->setLexicalDeclContext(LexicalDC);
1941 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001942 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001943
1944 // Import the integer type.
1945 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1946 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001947 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001948 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001949
1950 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001951 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001952 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001953
Douglas Gregor3996e242010-02-15 22:01:00 +00001954 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001955}
1956
Douglas Gregor5c73e912010-02-11 00:48:18 +00001957Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1958 // If this record has a definition in the translation unit we're coming from,
1959 // but this particular declaration is not that definition, import the
1960 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001961 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001962 if (Definition && Definition != D) {
1963 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001964 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001965 return nullptr;
1966
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001967 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001968 }
1969
1970 // Import the major distinguishing characteristics of this record.
1971 DeclContext *DC, *LexicalDC;
1972 DeclarationName Name;
1973 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001974 NamedDecl *ToD;
1975 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001976 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001977 if (ToD)
1978 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001979
Douglas Gregor5c73e912010-02-11 00:48:18 +00001980 // Figure out what structure name we're looking for.
1981 unsigned IDNS = Decl::IDNS_Tag;
1982 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001983 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1984 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001985 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001986 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001987 IDNS |= Decl::IDNS_Ordinary;
1988
1989 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001990 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001991 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001992 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001993 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001994 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001995 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001996
1997 if (!FoundDecls.empty()) {
1998 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1999 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2000 D->getASTContext().getExternalSource()->CompleteType(D);
2001 }
2002
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002003 for (auto *FoundDecl : FoundDecls) {
2004 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002005 continue;
2006
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002007 Decl *Found = FoundDecl;
2008 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2009 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002010 Found = Tag->getDecl();
2011 }
2012
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002013 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002014 if (!SearchName) {
2015 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002016 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002017 if (Optional<unsigned> Index1 =
2018 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
2019 D)) {
2020 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00002021 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002022 if (*Index1 != *Index2)
2023 continue;
2024 }
2025 }
2026 }
2027
Sean Callanan9092d472017-05-13 00:46:33 +00002028 PrevDecl = FoundRecord;
2029
Douglas Gregor25791052010-02-12 00:09:27 +00002030 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002031 if ((SearchName && !D->isCompleteDefinition())
2032 || (D->isCompleteDefinition() &&
2033 D->isAnonymousStructOrUnion()
2034 == FoundDef->isAnonymousStructOrUnion() &&
2035 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002036 // The record types structurally match, or the "from" translation
2037 // unit only had a forward declaration anyway; call it the same
2038 // function.
2039 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002040 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002041 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002042 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002043 // We have a forward declaration of this type, so adopt that forward
2044 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002045
2046 // If one or both can be completed from external storage then try one
2047 // last time to complete and compare them before doing this.
2048
2049 if (FoundRecord->hasExternalLexicalStorage() &&
2050 !FoundRecord->isCompleteDefinition())
2051 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2052 if (D->hasExternalLexicalStorage())
2053 D->getASTContext().getExternalSource()->CompleteType(D);
2054
2055 if (FoundRecord->isCompleteDefinition() &&
2056 D->isCompleteDefinition() &&
2057 !IsStructuralMatch(D, FoundRecord))
2058 continue;
2059
Douglas Gregor25791052010-02-12 00:09:27 +00002060 AdoptDecl = FoundRecord;
2061 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002062 } else if (!SearchName) {
2063 continue;
2064 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002065 }
2066
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002067 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002068 }
2069
Douglas Gregordd6006f2012-07-17 21:16:27 +00002070 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002071 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2072 ConflictingDecls.data(),
2073 ConflictingDecls.size());
2074 }
2075 }
2076
2077 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002078 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002079 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002080 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002081 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002082 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002083 if (DCXX->isLambda()) {
2084 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2085 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2086 DC, TInfo, Loc,
2087 DCXX->isDependentLambda(),
2088 DCXX->isGenericLambda(),
2089 DCXX->getLambdaCaptureDefault());
2090 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2091 if (DCXX->getLambdaContextDecl() && !CDecl)
2092 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002093 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002094 } else if (DCXX->isInjectedClassName()) {
2095 // We have to be careful to do a similar dance to the one in
2096 // Sema::ActOnStartCXXMemberDeclarations
2097 CXXRecordDecl *const PrevDecl = nullptr;
2098 const bool DelayTypeCreation = true;
2099 D2CXX = CXXRecordDecl::Create(
2100 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2101 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2102 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002103 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002104 } else {
2105 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2106 D->getTagKind(),
2107 DC, StartLoc, Loc,
2108 Name.getAsIdentifierInfo());
2109 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002110 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002111 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002112 D2->setLexicalDeclContext(LexicalDC);
2113 if (!DCXX->getDescribedClassTemplate())
2114 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002115
2116 Importer.Imported(D, D2);
2117
2118 if (ClassTemplateDecl *FromDescribed =
2119 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002120 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2121 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002122 if (!ToDescribed)
2123 return nullptr;
2124 D2CXX->setDescribedClassTemplate(ToDescribed);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002125 } else if (MemberSpecializationInfo *MemberInfo =
2126 DCXX->getMemberSpecializationInfo()) {
2127 TemplateSpecializationKind SK =
2128 MemberInfo->getTemplateSpecializationKind();
2129 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002130 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002131 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2132 if (FromInst && !ToInst)
2133 return nullptr;
2134 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2135 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2136 Importer.Import(MemberInfo->getPointOfInstantiation()));
2137 }
Douglas Gregor25791052010-02-12 00:09:27 +00002138 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002139 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002140 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002141 D2->setLexicalDeclContext(LexicalDC);
2142 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002143 }
Douglas Gregor14454802011-02-25 02:25:35 +00002144
2145 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002146 if (D->isAnonymousStructOrUnion())
2147 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002148 if (PrevDecl) {
2149 // FIXME: do this for all Redeclarables, not just RecordDecls.
2150 D2->setPreviousDecl(PrevDecl);
2151 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002152 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002153
Douglas Gregor3996e242010-02-15 22:01:00 +00002154 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002155
Douglas Gregor95d82832012-01-24 18:36:04 +00002156 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002157 return nullptr;
2158
Douglas Gregor3996e242010-02-15 22:01:00 +00002159 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002160}
2161
Douglas Gregor98c10182010-02-12 22:17:39 +00002162Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2163 // Import the major distinguishing characteristics of this enumerator.
2164 DeclContext *DC, *LexicalDC;
2165 DeclarationName Name;
2166 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002167 NamedDecl *ToD;
2168 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002169 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002170 if (ToD)
2171 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002172
2173 QualType T = Importer.Import(D->getType());
2174 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002175 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002176
Douglas Gregor98c10182010-02-12 22:17:39 +00002177 // Determine whether there are any other declarations with the same name and
2178 // in the same context.
2179 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002180 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002181 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002182 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002183 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002184 for (auto *FoundDecl : FoundDecls) {
2185 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002186 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002187
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002188 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002189 if (IsStructuralMatch(D, FoundEnumConstant))
2190 return Importer.Imported(D, FoundEnumConstant);
2191 }
2192
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002193 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002194 }
2195
2196 if (!ConflictingDecls.empty()) {
2197 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2198 ConflictingDecls.data(),
2199 ConflictingDecls.size());
2200 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002201 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002202 }
2203 }
2204
2205 Expr *Init = Importer.Import(D->getInitExpr());
2206 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002207 return nullptr;
2208
Douglas Gregor98c10182010-02-12 22:17:39 +00002209 EnumConstantDecl *ToEnumerator
2210 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2211 Name.getAsIdentifierInfo(), T,
2212 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002213 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002214 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002215 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002216 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002217 return ToEnumerator;
2218}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002219
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002220bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2221 FunctionDecl *ToFD) {
2222 switch (FromFD->getTemplatedKind()) {
2223 case FunctionDecl::TK_NonTemplate:
2224 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002225 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002226
2227 case FunctionDecl::TK_MemberSpecialization: {
2228 auto *InstFD = cast_or_null<FunctionDecl>(
2229 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2230 if (!InstFD)
2231 return true;
2232
2233 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2234 SourceLocation POI = Importer.Import(
2235 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2236 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2237 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002238 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002239 }
2240
2241 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2242 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2243 auto *Template = cast_or_null<FunctionTemplateDecl>(
2244 Importer.Import(FTSInfo->getTemplate()));
2245 if (!Template)
2246 return true;
2247 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2248
2249 // Import template arguments.
2250 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2251 SmallVector<TemplateArgument, 8> ToTemplArgs;
2252 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2253 ToTemplArgs))
2254 return true;
2255
2256 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2257 Importer.getToContext(), ToTemplArgs);
2258
2259 TemplateArgumentListInfo ToTAInfo;
2260 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002261 if (FromTAArgsAsWritten)
2262 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002263 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002264
2265 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2266
2267 ToFD->setFunctionTemplateSpecialization(
2268 Template, ToTAList, /* InsertPos= */ nullptr,
2269 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002270 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002271 }
2272
2273 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2274 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2275 UnresolvedSet<8> TemplDecls;
2276 unsigned NumTemplates = FromInfo->getNumTemplates();
2277 for (unsigned I = 0; I < NumTemplates; I++) {
2278 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2279 Importer.Import(FromInfo->getTemplate(I))))
2280 TemplDecls.addDecl(ToFTD);
2281 else
2282 return true;
2283 }
2284
2285 // Import TemplateArgumentListInfo.
2286 TemplateArgumentListInfo ToTAInfo;
2287 if (ImportTemplateArgumentListInfo(
2288 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2289 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2290 FromInfo->getNumTemplateArgs()),
2291 ToTAInfo))
2292 return true;
2293
2294 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2295 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002296 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002297 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002298 }
Sam McCallfdc32072018-01-26 12:06:44 +00002299 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002300}
2301
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002302Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2303 // Import the major distinguishing characteristics of this function.
2304 DeclContext *DC, *LexicalDC;
2305 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002306 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002307 NamedDecl *ToD;
2308 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002309 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002310 if (ToD)
2311 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002312
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002313 const FunctionDecl *FoundWithoutBody = nullptr;
2314
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002315 // Try to find a function in our own ("to") context with the same name, same
2316 // type, and in the same context as the function we're importing.
2317 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002318 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002319 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002320 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002321 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002322 for (auto *FoundDecl : FoundDecls) {
2323 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002324 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002325
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002326 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002327 if (FoundFunction->hasExternalFormalLinkage() &&
2328 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002329 if (Importer.IsStructurallyEquivalent(D->getType(),
2330 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002331 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002332 const FunctionDecl *FromBodyDecl = nullptr;
2333 D->hasBody(FromBodyDecl);
2334 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2335 // This function is needed to merge completely.
2336 FoundWithoutBody = FoundFunction;
2337 break;
2338 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002339 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002340 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002341
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002342 // FIXME: Check for overloading more carefully, e.g., by boosting
2343 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002344
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002345 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002346 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002347 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002348
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002349 // Complain about inconsistent function types.
2350 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002351 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002352 Importer.ToDiag(FoundFunction->getLocation(),
2353 diag::note_odr_value_here)
2354 << FoundFunction->getType();
2355 }
2356 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002357
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002358 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002359 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002360
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002361 if (!ConflictingDecls.empty()) {
2362 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2363 ConflictingDecls.data(),
2364 ConflictingDecls.size());
2365 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002366 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002367 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002368 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002369
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002370 DeclarationNameInfo NameInfo(Name, Loc);
2371 // Import additional name location/type info.
2372 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2373
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002374 QualType FromTy = D->getType();
2375 bool usedDifferentExceptionSpec = false;
2376
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002377 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002378 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2379 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2380 // FunctionDecl that we are importing the FunctionProtoType for.
2381 // To avoid an infinite recursion when importing, create the FunctionDecl
2382 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002383 if (FromEPI.ExceptionSpec.SourceDecl ||
2384 FromEPI.ExceptionSpec.SourceTemplate ||
2385 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002386 FunctionProtoType::ExtProtoInfo DefaultEPI;
2387 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002388 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002389 usedDifferentExceptionSpec = true;
2390 }
2391 }
2392
Douglas Gregorb4964f72010-02-15 23:54:17 +00002393 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002394 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002395 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002396 return nullptr;
2397
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002398 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002399 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002400 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002401 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002402 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002403 return nullptr;
2404
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002405 Parameters.push_back(ToP);
2406 }
2407
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002408 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002409 if (D->getTypeSourceInfo() && !TInfo)
2410 return nullptr;
2411
2412 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002413 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002414 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002415 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002416 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2417 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002418 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002419 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002420 FromConstructor->isExplicit(),
2421 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002422 D->isImplicit(),
2423 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002424 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2425 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002426 for (auto *I : FromConstructor->inits()) {
2427 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002428 if (!ToI && I)
2429 return nullptr;
2430 CtorInitializers.push_back(ToI);
2431 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002432 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002433 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2434 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002435 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002436 ToCtor->setCtorInitializers(Memory);
2437 ToCtor->setNumCtorInitializers(NumInitializers);
2438 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002439 } else if (isa<CXXDestructorDecl>(D)) {
2440 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2441 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002442 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002443 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002444 D->isInlineSpecified(),
2445 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002446 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002447 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2448 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002449 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002450 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002451 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002452 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002453 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002454 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002455 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002456 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2457 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002458 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002459 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002460 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002461 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002462 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002463 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002464 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002465 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002466 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002467 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002468 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002469 D->hasWrittenPrototype(),
2470 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002471 }
John McCall3e11ebe2010-03-15 10:12:16 +00002472
2473 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002474 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002475 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002476 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002477 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2478 ToFunction->setTrivial(D->isTrivial());
2479 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002480 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002481
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002482 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002483 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002484 Param->setOwningFunction(ToFunction);
2485 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002486 }
David Blaikie9c70e042011-09-21 18:16:56 +00002487 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002488
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002489 if (FoundWithoutBody) {
2490 auto *Recent = const_cast<FunctionDecl *>(
2491 FoundWithoutBody->getMostRecentDecl());
2492 ToFunction->setPreviousDecl(Recent);
2493 }
2494
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002495 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2496 // params it refers to.
2497 if (TInfo) {
2498 if (auto ProtoLoc =
2499 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2500 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2501 ProtoLoc.setParam(I, Parameters[I]);
2502 }
2503 }
2504
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002505 if (usedDifferentExceptionSpec) {
2506 // Update FunctionProtoType::ExtProtoInfo.
2507 QualType T = Importer.Import(D->getType());
2508 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002509 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002510 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002511 }
2512
Sean Callanan59721b32015-04-28 18:41:46 +00002513 // Import the body, if any.
2514 if (Stmt *FromBody = D->getBody()) {
2515 if (Stmt *ToBody = Importer.Import(FromBody)) {
2516 ToFunction->setBody(ToBody);
2517 }
2518 }
2519
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002520 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002521
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002522 // If it is a template, import all related things.
2523 if (ImportTemplateInformation(D, ToFunction))
2524 return nullptr;
2525
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002526 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002527 // NOTE: If the function is templated declaration, it should be not added into
2528 // LexicalDC. But described template is imported during import of
2529 // FunctionTemplateDecl (it happens later). So, we use source declaration
2530 // to determine if we should add the result function.
2531 if (!D->getDescribedFunctionTemplate())
2532 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002533
Lang Hames19e07e12017-06-20 21:06:00 +00002534 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2535 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2536
Douglas Gregor43f54792010-02-17 02:12:47 +00002537 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002538}
2539
Douglas Gregor00eace12010-02-21 18:29:16 +00002540Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2541 return VisitFunctionDecl(D);
2542}
2543
2544Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2545 return VisitCXXMethodDecl(D);
2546}
2547
2548Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2549 return VisitCXXMethodDecl(D);
2550}
2551
2552Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2553 return VisitCXXMethodDecl(D);
2554}
2555
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002556static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002557 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002558 if (!Owner)
2559 return 0;
2560
2561 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002562 for (const auto *D : Owner->noload_decls()) {
2563 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002564 return Index;
2565
2566 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2567 ++Index;
2568 }
2569
2570 return Index;
2571}
2572
Douglas Gregor5c73e912010-02-11 00:48:18 +00002573Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2574 // Import the major distinguishing characteristics of a variable.
2575 DeclContext *DC, *LexicalDC;
2576 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002577 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002578 NamedDecl *ToD;
2579 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002580 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002581 if (ToD)
2582 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002583
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002584 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002585 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002586 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002587 for (auto *FoundDecl : FoundDecls) {
2588 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002589 // For anonymous fields, match up by index.
2590 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2591 continue;
2592
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002593 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002594 FoundField->getType())) {
2595 Importer.Imported(D, FoundField);
2596 return FoundField;
2597 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002598
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002599 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2600 << Name << D->getType() << FoundField->getType();
2601 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2602 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002603 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002604 }
2605 }
2606
Douglas Gregorb4964f72010-02-15 23:54:17 +00002607 // Import the type.
2608 QualType T = Importer.Import(D->getType());
2609 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002610 return nullptr;
2611
Douglas Gregor5c73e912010-02-11 00:48:18 +00002612 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2613 Expr *BitWidth = Importer.Import(D->getBitWidth());
2614 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002615 return nullptr;
2616
Abramo Bagnaradff19302011-03-08 08:55:46 +00002617 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2618 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002619 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002620 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002621 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002622 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002623 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002624 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002625 Expr *ToInitializer = Importer.Import(FromInitializer);
2626 if (ToInitializer)
2627 ToField->setInClassInitializer(ToInitializer);
2628 else
2629 return nullptr;
2630 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002631 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002632 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002633 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002634 return ToField;
2635}
2636
Francois Pichet783dd6e2010-11-21 06:08:52 +00002637Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2638 // Import the major distinguishing characteristics of a variable.
2639 DeclContext *DC, *LexicalDC;
2640 DeclarationName Name;
2641 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002642 NamedDecl *ToD;
2643 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002644 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002645 if (ToD)
2646 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002647
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002648 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002649 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002650 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002651 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002652 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002653 // For anonymous indirect fields, match up by index.
2654 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2655 continue;
2656
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002657 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002658 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002659 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002660 Importer.Imported(D, FoundField);
2661 return FoundField;
2662 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002663
2664 // If there are more anonymous fields to check, continue.
2665 if (!Name && I < N-1)
2666 continue;
2667
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002668 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2669 << Name << D->getType() << FoundField->getType();
2670 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2671 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002672 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002673 }
2674 }
2675
Francois Pichet783dd6e2010-11-21 06:08:52 +00002676 // Import the type.
2677 QualType T = Importer.Import(D->getType());
2678 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002679 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002680
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002681 auto **NamedChain =
2682 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002683
2684 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002685 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002686 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002687 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002688 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002689 NamedChain[i++] = cast<NamedDecl>(D);
2690 }
2691
2692 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002693 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002694 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002695
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002696 for (const auto *A : D->attrs())
2697 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002698
Francois Pichet783dd6e2010-11-21 06:08:52 +00002699 ToIndirectField->setAccess(D->getAccess());
2700 ToIndirectField->setLexicalDeclContext(LexicalDC);
2701 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002702 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002703 return ToIndirectField;
2704}
2705
Aleksei Sidorina693b372016-09-28 10:16:56 +00002706Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2707 // Import the major distinguishing characteristics of a declaration.
2708 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2709 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2710 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2711 if (!DC || !LexicalDC)
2712 return nullptr;
2713
2714 // Determine whether we've already imported this decl.
2715 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2716 auto *RD = cast<CXXRecordDecl>(DC);
2717 FriendDecl *ImportedFriend = RD->getFirstFriend();
2718 StructuralEquivalenceContext Context(
2719 Importer.getFromContext(), Importer.getToContext(),
2720 Importer.getNonEquivalentDecls(), false, false);
2721
2722 while (ImportedFriend) {
2723 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2724 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2725 ImportedFriend->getFriendDecl()))
2726 return Importer.Imported(D, ImportedFriend);
2727
2728 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2729 if (Importer.IsStructurallyEquivalent(
2730 D->getFriendType()->getType(),
2731 ImportedFriend->getFriendType()->getType(), true))
2732 return Importer.Imported(D, ImportedFriend);
2733 }
2734 ImportedFriend = ImportedFriend->getNextFriend();
2735 }
2736
2737 // Not found. Create it.
2738 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002739 if (NamedDecl *FriendD = D->getFriendDecl()) {
2740 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2741 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2742 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2743 ToFriendD->setObjectOfFriendDecl(false);
2744
2745 ToFU = ToFriendD;
2746 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002747 ToFU = Importer.Import(D->getFriendType());
2748 if (!ToFU)
2749 return nullptr;
2750
2751 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002752 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002753 for (unsigned I = 0; I < D->NumTPLists; I++) {
2754 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2755 if (!List)
2756 return nullptr;
2757 ToTPLists[I] = List;
2758 }
2759
2760 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2761 Importer.Import(D->getLocation()),
2762 ToFU, Importer.Import(D->getFriendLoc()),
2763 ToTPLists);
2764
2765 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002766
2767 FrD->setAccess(D->getAccess());
2768 FrD->setLexicalDeclContext(LexicalDC);
2769 LexicalDC->addDeclInternal(FrD);
2770 return FrD;
2771}
2772
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002773Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2774 // Import the major distinguishing characteristics of an ivar.
2775 DeclContext *DC, *LexicalDC;
2776 DeclarationName Name;
2777 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002778 NamedDecl *ToD;
2779 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002780 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002781 if (ToD)
2782 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002783
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002784 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002785 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002786 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002787 for (auto *FoundDecl : FoundDecls) {
2788 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002789 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002790 FoundIvar->getType())) {
2791 Importer.Imported(D, FoundIvar);
2792 return FoundIvar;
2793 }
2794
2795 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2796 << Name << D->getType() << FoundIvar->getType();
2797 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2798 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002799 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002800 }
2801 }
2802
2803 // Import the type.
2804 QualType T = Importer.Import(D->getType());
2805 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002806 return nullptr;
2807
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002808 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2809 Expr *BitWidth = Importer.Import(D->getBitWidth());
2810 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002811 return nullptr;
2812
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002813 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2814 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002815 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002816 Loc, Name.getAsIdentifierInfo(),
2817 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002818 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002819 ToIvar->setLexicalDeclContext(LexicalDC);
2820 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002821 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002822 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002823}
2824
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002825Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2826 // Import the major distinguishing characteristics of a variable.
2827 DeclContext *DC, *LexicalDC;
2828 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002829 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002830 NamedDecl *ToD;
2831 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002832 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002833 if (ToD)
2834 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002835
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002836 // Try to find a variable in our own ("to") context with the same name and
2837 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002838 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002839 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002840 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002841 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002842 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002843 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002844 for (auto *FoundDecl : FoundDecls) {
2845 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002846 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002847
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002848 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002849 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002850 if (FoundVar->hasExternalFormalLinkage() &&
2851 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002852 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002853 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002854 MergeWithVar = FoundVar;
2855 break;
2856 }
2857
Douglas Gregor56521c52010-02-12 17:23:39 +00002858 const ArrayType *FoundArray
2859 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2860 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002861 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002862 if (FoundArray && TArray) {
2863 if (isa<IncompleteArrayType>(FoundArray) &&
2864 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002865 // Import the type.
2866 QualType T = Importer.Import(D->getType());
2867 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002868 return nullptr;
2869
Douglas Gregor56521c52010-02-12 17:23:39 +00002870 FoundVar->setType(T);
2871 MergeWithVar = FoundVar;
2872 break;
2873 } else if (isa<IncompleteArrayType>(TArray) &&
2874 isa<ConstantArrayType>(FoundArray)) {
2875 MergeWithVar = FoundVar;
2876 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002877 }
2878 }
2879
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002880 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002881 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002882 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2883 << FoundVar->getType();
2884 }
2885 }
2886
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002887 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002888 }
2889
2890 if (MergeWithVar) {
2891 // An equivalent variable with external linkage has been found. Link
2892 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002893 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002894
2895 if (VarDecl *DDef = D->getDefinition()) {
2896 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2897 Importer.ToDiag(ExistingDef->getLocation(),
2898 diag::err_odr_variable_multiple_def)
2899 << Name;
2900 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2901 } else {
2902 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002903 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002904 if (DDef->isInitKnownICE()) {
2905 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2906 Eval->CheckedICE = true;
2907 Eval->IsICE = DDef->isInitICE();
2908 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002909 }
2910 }
2911
2912 return MergeWithVar;
2913 }
2914
2915 if (!ConflictingDecls.empty()) {
2916 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2917 ConflictingDecls.data(),
2918 ConflictingDecls.size());
2919 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002920 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002921 }
2922 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002923
Douglas Gregorb4964f72010-02-15 23:54:17 +00002924 // Import the type.
2925 QualType T = Importer.Import(D->getType());
2926 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002927 return nullptr;
2928
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002929 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002930 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002931 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2932 Importer.Import(D->getInnerLocStart()),
2933 Loc, Name.getAsIdentifierInfo(),
2934 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002935 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002936 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002937 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002938 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002939 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002940
2941 // Templated declarations should never appear in the enclosing DeclContext.
2942 if (!D->getDescribedVarTemplate())
2943 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002944
Sean Callanan59721b32015-04-28 18:41:46 +00002945 if (!D->isFileVarDecl() &&
2946 D->isUsed())
2947 ToVar->setIsUsed();
2948
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002949 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002950 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002951 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002952
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002953 if (D->isConstexpr())
2954 ToVar->setConstexpr(true);
2955
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002956 return ToVar;
2957}
2958
Douglas Gregor8b228d72010-02-17 21:22:52 +00002959Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2960 // Parameters are created in the translation unit's context, then moved
2961 // into the function declaration's context afterward.
2962 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2963
2964 // Import the name of this declaration.
2965 DeclarationName Name = Importer.Import(D->getDeclName());
2966 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002967 return nullptr;
2968
Douglas Gregor8b228d72010-02-17 21:22:52 +00002969 // Import the location of this declaration.
2970 SourceLocation Loc = Importer.Import(D->getLocation());
2971
2972 // Import the parameter's type.
2973 QualType T = Importer.Import(D->getType());
2974 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002975 return nullptr;
2976
Douglas Gregor8b228d72010-02-17 21:22:52 +00002977 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002978 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2979 Name.getAsIdentifierInfo(), T,
2980 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002981 return Importer.Imported(D, ToParm);
2982}
2983
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002984Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2985 // Parameters are created in the translation unit's context, then moved
2986 // into the function declaration's context afterward.
2987 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2988
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002989 // Import the name of this declaration.
2990 DeclarationName Name = Importer.Import(D->getDeclName());
2991 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002992 return nullptr;
2993
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002994 // Import the location of this declaration.
2995 SourceLocation Loc = Importer.Import(D->getLocation());
2996
2997 // Import the parameter's type.
2998 QualType T = Importer.Import(D->getType());
2999 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003000 return nullptr;
3001
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 // Create the imported parameter.
3003 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3004 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003005 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003006 Loc, Name.getAsIdentifierInfo(),
3007 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003008 /*DefaultArg*/ nullptr);
3009
3010 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003011 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003012 ToParm->setKNRPromoted(D->isKNRPromoted());
3013
3014 Expr *ToDefArg = nullptr;
3015 Expr *FromDefArg = nullptr;
3016 if (D->hasUninstantiatedDefaultArg()) {
3017 FromDefArg = D->getUninstantiatedDefaultArg();
3018 ToDefArg = Importer.Import(FromDefArg);
3019 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3020 } else if (D->hasUnparsedDefaultArg()) {
3021 ToParm->setUnparsedDefaultArg();
3022 } else if (D->hasDefaultArg()) {
3023 FromDefArg = D->getDefaultArg();
3024 ToDefArg = Importer.Import(FromDefArg);
3025 ToParm->setDefaultArg(ToDefArg);
3026 }
3027 if (FromDefArg && !ToDefArg)
3028 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003029
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003030 if (D->isObjCMethodParameter()) {
3031 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3032 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3033 } else {
3034 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3035 D->getFunctionScopeIndex());
3036 }
3037
Sean Callanan59721b32015-04-28 18:41:46 +00003038 if (D->isUsed())
3039 ToParm->setIsUsed();
3040
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003041 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003042}
3043
Douglas Gregor43f54792010-02-17 02:12:47 +00003044Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3045 // Import the major distinguishing characteristics of a method.
3046 DeclContext *DC, *LexicalDC;
3047 DeclarationName Name;
3048 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003049 NamedDecl *ToD;
3050 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003051 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003052 if (ToD)
3053 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003054
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003055 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003056 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003057 for (auto *FoundDecl : FoundDecls) {
3058 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3060 continue;
3061
3062 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003063 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3064 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003066 << D->isInstanceMethod() << Name << D->getReturnType()
3067 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003068 Importer.ToDiag(FoundMethod->getLocation(),
3069 diag::note_odr_objc_method_here)
3070 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003071 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003072 }
3073
3074 // Check the number of parameters.
3075 if (D->param_size() != FoundMethod->param_size()) {
3076 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3077 << D->isInstanceMethod() << Name
3078 << D->param_size() << FoundMethod->param_size();
3079 Importer.ToDiag(FoundMethod->getLocation(),
3080 diag::note_odr_objc_method_here)
3081 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003082 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003083 }
3084
3085 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003086 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003087 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3088 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003089 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003090 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003091 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003092 diag::err_odr_objc_method_param_type_inconsistent)
3093 << D->isInstanceMethod() << Name
3094 << (*P)->getType() << (*FoundP)->getType();
3095 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3096 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003097 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003098 }
3099 }
3100
3101 // Check variadic/non-variadic.
3102 // Check the number of parameters.
3103 if (D->isVariadic() != FoundMethod->isVariadic()) {
3104 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3105 << D->isInstanceMethod() << Name;
3106 Importer.ToDiag(FoundMethod->getLocation(),
3107 diag::note_odr_objc_method_here)
3108 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003109 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003110 }
3111
3112 // FIXME: Any other bits we need to merge?
3113 return Importer.Imported(D, FoundMethod);
3114 }
3115 }
3116
3117 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003118 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003119 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003120 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003121
Alp Toker314cc812014-01-25 16:55:45 +00003122 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003123
Alp Toker314cc812014-01-25 16:55:45 +00003124 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3125 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3126 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3127 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3128 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003129
3130 // FIXME: When we decide to merge method definitions, we'll need to
3131 // deal with implicit parameters.
3132
3133 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003134 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003135 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003136 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003137 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003138 return nullptr;
3139
Douglas Gregor43f54792010-02-17 02:12:47 +00003140 ToParams.push_back(ToP);
3141 }
3142
3143 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003144 for (auto *ToParam : ToParams) {
3145 ToParam->setOwningFunction(ToMethod);
3146 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003147 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003148
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003149 SmallVector<SourceLocation, 12> SelLocs;
3150 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003151 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003152 Loc = Importer.Import(Loc);
3153
3154 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003155
3156 ToMethod->setLexicalDeclContext(LexicalDC);
3157 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003158 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003159 return ToMethod;
3160}
3161
Douglas Gregor85f3f952015-07-07 03:57:15 +00003162Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3163 // Import the major distinguishing characteristics of a category.
3164 DeclContext *DC, *LexicalDC;
3165 DeclarationName Name;
3166 SourceLocation Loc;
3167 NamedDecl *ToD;
3168 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3169 return nullptr;
3170 if (ToD)
3171 return ToD;
3172
3173 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3174 if (!BoundInfo)
3175 return nullptr;
3176
3177 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3178 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003179 D->getVariance(),
3180 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003181 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003182 Importer.Import(D->getLocation()),
3183 Name.getAsIdentifierInfo(),
3184 Importer.Import(D->getColonLoc()),
3185 BoundInfo);
3186 Importer.Imported(D, Result);
3187 Result->setLexicalDeclContext(LexicalDC);
3188 return Result;
3189}
3190
Douglas Gregor84c51c32010-02-18 01:47:50 +00003191Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3192 // Import the major distinguishing characteristics of a category.
3193 DeclContext *DC, *LexicalDC;
3194 DeclarationName Name;
3195 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003196 NamedDecl *ToD;
3197 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003198 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003199 if (ToD)
3200 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003201
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003202 auto *ToInterface =
3203 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003204 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003205 return nullptr;
3206
Douglas Gregor84c51c32010-02-18 01:47:50 +00003207 // Determine if we've already encountered this category.
3208 ObjCCategoryDecl *MergeWithCategory
3209 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3210 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3211 if (!ToCategory) {
3212 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003213 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003214 Loc,
3215 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003216 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003217 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003218 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003219 Importer.Import(D->getIvarLBraceLoc()),
3220 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003221 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003222 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003223 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003224 // Import the type parameter list after calling Imported, to avoid
3225 // loops when bringing in their DeclContext.
3226 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3227 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003228
Douglas Gregor84c51c32010-02-18 01:47:50 +00003229 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003230 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3231 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003232 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3233 = D->protocol_loc_begin();
3234 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3235 FromProtoEnd = D->protocol_end();
3236 FromProto != FromProtoEnd;
3237 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003238 auto *ToProto =
3239 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003240 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003241 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003242 Protocols.push_back(ToProto);
3243 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3244 }
3245
3246 // FIXME: If we're merging, make sure that the protocol list is the same.
3247 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3248 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003249 } else {
3250 Importer.Imported(D, ToCategory);
3251 }
3252
3253 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003254 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003255
3256 // If we have an implementation, import it as well.
3257 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003258 auto *Impl =
3259 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003260 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003261 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003262 return nullptr;
3263
Douglas Gregor84c51c32010-02-18 01:47:50 +00003264 ToCategory->setImplementation(Impl);
3265 }
3266
3267 return ToCategory;
3268}
3269
Douglas Gregor2aa53772012-01-24 17:42:07 +00003270bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3271 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003272 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003273 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003274 if (shouldForceImportDeclContext(Kind))
3275 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003276 return false;
3277 }
3278
3279 // Start the protocol definition
3280 To->startDefinition();
3281
3282 // Import protocols
3283 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3284 SmallVector<SourceLocation, 4> ProtocolLocs;
3285 ObjCProtocolDecl::protocol_loc_iterator
3286 FromProtoLoc = From->protocol_loc_begin();
3287 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3288 FromProtoEnd = From->protocol_end();
3289 FromProto != FromProtoEnd;
3290 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003291 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003292 if (!ToProto)
3293 return true;
3294 Protocols.push_back(ToProto);
3295 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3296 }
3297
3298 // FIXME: If we're merging, make sure that the protocol list is the same.
3299 To->setProtocolList(Protocols.data(), Protocols.size(),
3300 ProtocolLocs.data(), Importer.getToContext());
3301
Douglas Gregor2e15c842012-02-01 21:00:38 +00003302 if (shouldForceImportDeclContext(Kind)) {
3303 // Import all of the members of this protocol.
3304 ImportDeclContext(From, /*ForceImport=*/true);
3305 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003306 return false;
3307}
3308
Douglas Gregor98d156a2010-02-17 16:12:00 +00003309Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003310 // If this protocol has a definition in the translation unit we're coming
3311 // from, but this particular declaration is not that definition, import the
3312 // definition and map to that.
3313 ObjCProtocolDecl *Definition = D->getDefinition();
3314 if (Definition && Definition != D) {
3315 Decl *ImportedDef = Importer.Import(Definition);
3316 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003317 return nullptr;
3318
Douglas Gregor2aa53772012-01-24 17:42:07 +00003319 return Importer.Imported(D, ImportedDef);
3320 }
3321
Douglas Gregor84c51c32010-02-18 01:47:50 +00003322 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003323 DeclContext *DC, *LexicalDC;
3324 DeclarationName Name;
3325 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003326 NamedDecl *ToD;
3327 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003328 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003329 if (ToD)
3330 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003331
Craig Topper36250ad2014-05-12 05:36:57 +00003332 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003333 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003334 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003335 for (auto *FoundDecl : FoundDecls) {
3336 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003337 continue;
3338
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003339 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003340 break;
3341 }
3342
3343 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003344 if (!ToProto) {
3345 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3346 Name.getAsIdentifierInfo(), Loc,
3347 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003348 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003349 ToProto->setLexicalDeclContext(LexicalDC);
3350 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003351 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003352
3353 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003354
Douglas Gregor2aa53772012-01-24 17:42:07 +00003355 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003356 return nullptr;
3357
Douglas Gregor98d156a2010-02-17 16:12:00 +00003358 return ToProto;
3359}
3360
Sean Callanan0aae0412014-12-10 00:00:37 +00003361Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3362 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3363 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3364
3365 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3366 SourceLocation LangLoc = Importer.Import(D->getLocation());
3367
3368 bool HasBraces = D->hasBraces();
3369
Sean Callananb12a8552014-12-10 21:22:20 +00003370 LinkageSpecDecl *ToLinkageSpec =
3371 LinkageSpecDecl::Create(Importer.getToContext(),
3372 DC,
3373 ExternLoc,
3374 LangLoc,
3375 D->getLanguage(),
3376 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003377
3378 if (HasBraces) {
3379 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3380 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3381 }
3382
3383 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3384 LexicalDC->addDeclInternal(ToLinkageSpec);
3385
3386 Importer.Imported(D, ToLinkageSpec);
3387
3388 return ToLinkageSpec;
3389}
3390
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003391Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3392 DeclContext *DC, *LexicalDC;
3393 DeclarationName Name;
3394 SourceLocation Loc;
3395 NamedDecl *ToD = nullptr;
3396 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3397 return nullptr;
3398 if (ToD)
3399 return ToD;
3400
3401 DeclarationNameInfo NameInfo(Name,
3402 Importer.Import(D->getNameInfo().getLoc()));
3403 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3404
3405 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3406 Importer.Import(D->getUsingLoc()),
3407 Importer.Import(D->getQualifierLoc()),
3408 NameInfo, D->hasTypename());
3409 ToUsing->setLexicalDeclContext(LexicalDC);
3410 LexicalDC->addDeclInternal(ToUsing);
3411 Importer.Imported(D, ToUsing);
3412
3413 if (NamedDecl *FromPattern =
3414 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003415 if (auto *ToPattern =
3416 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003417 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3418 else
3419 return nullptr;
3420 }
3421
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003422 for (auto *FromShadow : D->shadows()) {
3423 if (auto *ToShadow =
3424 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003425 ToUsing->addShadowDecl(ToShadow);
3426 else
3427 // FIXME: We return a nullptr here but the definition is already created
3428 // and available with lookups. How to fix this?..
3429 return nullptr;
3430 }
3431 return ToUsing;
3432}
3433
3434Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3435 DeclContext *DC, *LexicalDC;
3436 DeclarationName Name;
3437 SourceLocation Loc;
3438 NamedDecl *ToD = nullptr;
3439 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3440 return nullptr;
3441 if (ToD)
3442 return ToD;
3443
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003444 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3445 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003446 if (!ToUsing)
3447 return nullptr;
3448
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003449 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3450 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003451 if (!ToTarget)
3452 return nullptr;
3453
3454 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3455 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3456
3457 ToShadow->setLexicalDeclContext(LexicalDC);
3458 ToShadow->setAccess(D->getAccess());
3459 Importer.Imported(D, ToShadow);
3460
3461 if (UsingShadowDecl *FromPattern =
3462 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003463 if (auto *ToPattern =
3464 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003465 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3466 ToPattern);
3467 else
3468 // FIXME: We return a nullptr here but the definition is already created
3469 // and available with lookups. How to fix this?..
3470 return nullptr;
3471 }
3472
3473 LexicalDC->addDeclInternal(ToShadow);
3474
3475 return ToShadow;
3476}
3477
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003478Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3479 DeclContext *DC, *LexicalDC;
3480 DeclarationName Name;
3481 SourceLocation Loc;
3482 NamedDecl *ToD = nullptr;
3483 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3484 return nullptr;
3485 if (ToD)
3486 return ToD;
3487
3488 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3489 if (!ToComAncestor)
3490 return nullptr;
3491
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003492 auto *ToNominated = cast_or_null<NamespaceDecl>(
3493 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003494 if (!ToNominated)
3495 return nullptr;
3496
3497 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3498 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3499 Importer.Import(D->getNamespaceKeyLocation()),
3500 Importer.Import(D->getQualifierLoc()),
3501 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3502 ToUsingDir->setLexicalDeclContext(LexicalDC);
3503 LexicalDC->addDeclInternal(ToUsingDir);
3504 Importer.Imported(D, ToUsingDir);
3505
3506 return ToUsingDir;
3507}
3508
3509Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3510 UnresolvedUsingValueDecl *D) {
3511 DeclContext *DC, *LexicalDC;
3512 DeclarationName Name;
3513 SourceLocation Loc;
3514 NamedDecl *ToD = nullptr;
3515 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3516 return nullptr;
3517 if (ToD)
3518 return ToD;
3519
3520 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3521 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3522
3523 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3524 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3525 Importer.Import(D->getQualifierLoc()), NameInfo,
3526 Importer.Import(D->getEllipsisLoc()));
3527
3528 Importer.Imported(D, ToUsingValue);
3529 ToUsingValue->setAccess(D->getAccess());
3530 ToUsingValue->setLexicalDeclContext(LexicalDC);
3531 LexicalDC->addDeclInternal(ToUsingValue);
3532
3533 return ToUsingValue;
3534}
3535
3536Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3537 UnresolvedUsingTypenameDecl *D) {
3538 DeclContext *DC, *LexicalDC;
3539 DeclarationName Name;
3540 SourceLocation Loc;
3541 NamedDecl *ToD = nullptr;
3542 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3543 return nullptr;
3544 if (ToD)
3545 return ToD;
3546
3547 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3548 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3549 Importer.Import(D->getTypenameLoc()),
3550 Importer.Import(D->getQualifierLoc()), Loc, Name,
3551 Importer.Import(D->getEllipsisLoc()));
3552
3553 Importer.Imported(D, ToUsing);
3554 ToUsing->setAccess(D->getAccess());
3555 ToUsing->setLexicalDeclContext(LexicalDC);
3556 LexicalDC->addDeclInternal(ToUsing);
3557
3558 return ToUsing;
3559}
3560
Douglas Gregor2aa53772012-01-24 17:42:07 +00003561bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3562 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003563 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003564 if (To->getDefinition()) {
3565 // Check consistency of superclass.
3566 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3567 if (FromSuper) {
3568 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3569 if (!FromSuper)
3570 return true;
3571 }
3572
3573 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3574 if ((bool)FromSuper != (bool)ToSuper ||
3575 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3576 Importer.ToDiag(To->getLocation(),
3577 diag::err_odr_objc_superclass_inconsistent)
3578 << To->getDeclName();
3579 if (ToSuper)
3580 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3581 << To->getSuperClass()->getDeclName();
3582 else
3583 Importer.ToDiag(To->getLocation(),
3584 diag::note_odr_objc_missing_superclass);
3585 if (From->getSuperClass())
3586 Importer.FromDiag(From->getSuperClassLoc(),
3587 diag::note_odr_objc_superclass)
3588 << From->getSuperClass()->getDeclName();
3589 else
3590 Importer.FromDiag(From->getLocation(),
3591 diag::note_odr_objc_missing_superclass);
3592 }
3593
Douglas Gregor2e15c842012-02-01 21:00:38 +00003594 if (shouldForceImportDeclContext(Kind))
3595 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003596 return false;
3597 }
3598
3599 // Start the definition.
3600 To->startDefinition();
3601
3602 // If this class has a superclass, import it.
3603 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003604 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3605 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003606 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003607
3608 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003609 }
3610
3611 // Import protocols
3612 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3613 SmallVector<SourceLocation, 4> ProtocolLocs;
3614 ObjCInterfaceDecl::protocol_loc_iterator
3615 FromProtoLoc = From->protocol_loc_begin();
3616
3617 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3618 FromProtoEnd = From->protocol_end();
3619 FromProto != FromProtoEnd;
3620 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003621 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003622 if (!ToProto)
3623 return true;
3624 Protocols.push_back(ToProto);
3625 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3626 }
3627
3628 // FIXME: If we're merging, make sure that the protocol list is the same.
3629 To->setProtocolList(Protocols.data(), Protocols.size(),
3630 ProtocolLocs.data(), Importer.getToContext());
3631
3632 // Import categories. When the categories themselves are imported, they'll
3633 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003634 for (auto *Cat : From->known_categories())
3635 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003636
Douglas Gregor2aa53772012-01-24 17:42:07 +00003637 // If we have an @implementation, import it as well.
3638 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003639 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3640 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003641 if (!Impl)
3642 return true;
3643
3644 To->setImplementation(Impl);
3645 }
3646
Douglas Gregor2e15c842012-02-01 21:00:38 +00003647 if (shouldForceImportDeclContext(Kind)) {
3648 // Import all of the members of this class.
3649 ImportDeclContext(From, /*ForceImport=*/true);
3650 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003651 return false;
3652}
3653
Douglas Gregor85f3f952015-07-07 03:57:15 +00003654ObjCTypeParamList *
3655ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3656 if (!list)
3657 return nullptr;
3658
3659 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3660 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003661 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3662 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003663 if (!toTypeParam)
3664 return nullptr;
3665
3666 toTypeParams.push_back(toTypeParam);
3667 }
3668
3669 return ObjCTypeParamList::create(Importer.getToContext(),
3670 Importer.Import(list->getLAngleLoc()),
3671 toTypeParams,
3672 Importer.Import(list->getRAngleLoc()));
3673}
3674
Douglas Gregor45635322010-02-16 01:20:57 +00003675Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003676 // If this class has a definition in the translation unit we're coming from,
3677 // but this particular declaration is not that definition, import the
3678 // definition and map to that.
3679 ObjCInterfaceDecl *Definition = D->getDefinition();
3680 if (Definition && Definition != D) {
3681 Decl *ImportedDef = Importer.Import(Definition);
3682 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003683 return nullptr;
3684
Douglas Gregor2aa53772012-01-24 17:42:07 +00003685 return Importer.Imported(D, ImportedDef);
3686 }
3687
Douglas Gregor45635322010-02-16 01:20:57 +00003688 // Import the major distinguishing characteristics of an @interface.
3689 DeclContext *DC, *LexicalDC;
3690 DeclarationName Name;
3691 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003692 NamedDecl *ToD;
3693 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003694 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003695 if (ToD)
3696 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003697
Douglas Gregor2aa53772012-01-24 17:42:07 +00003698 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003699 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003700 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003701 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003702 for (auto *FoundDecl : FoundDecls) {
3703 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003704 continue;
3705
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003706 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003707 break;
3708 }
3709
Douglas Gregor2aa53772012-01-24 17:42:07 +00003710 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003711 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003712 if (!ToIface) {
3713 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3714 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003715 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003716 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003717 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003718 D->isImplicitInterfaceDecl());
3719 ToIface->setLexicalDeclContext(LexicalDC);
3720 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003721 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003722 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003723 // Import the type parameter list after calling Imported, to avoid
3724 // loops when bringing in their DeclContext.
3725 ToIface->setTypeParamList(ImportObjCTypeParamList(
3726 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003727
Douglas Gregor2aa53772012-01-24 17:42:07 +00003728 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003729 return nullptr;
3730
Douglas Gregor98d156a2010-02-17 16:12:00 +00003731 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003732}
3733
Douglas Gregor4da9d682010-12-07 15:32:12 +00003734Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003735 auto *Category = cast_or_null<ObjCCategoryDecl>(
3736 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003737 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
3739
Douglas Gregor4da9d682010-12-07 15:32:12 +00003740 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3741 if (!ToImpl) {
3742 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3743 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003744 return nullptr;
3745
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003746 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003747 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003748 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003749 Category->getClassInterface(),
3750 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003751 Importer.Import(D->getAtStartLoc()),
3752 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003753
3754 DeclContext *LexicalDC = DC;
3755 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3756 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3757 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003758 return nullptr;
3759
Douglas Gregor4da9d682010-12-07 15:32:12 +00003760 ToImpl->setLexicalDeclContext(LexicalDC);
3761 }
3762
Sean Callanan95e74be2011-10-21 02:57:43 +00003763 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003764 Category->setImplementation(ToImpl);
3765 }
3766
3767 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003768 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003769 return ToImpl;
3770}
3771
Douglas Gregorda8025c2010-12-07 01:26:03 +00003772Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3773 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003774 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3775 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003776 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003777 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003778
3779 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003780 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003781 if (D->getSuperClass()) {
3782 Super = cast_or_null<ObjCInterfaceDecl>(
3783 Importer.Import(D->getSuperClass()));
3784 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003785 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003786 }
3787
3788 ObjCImplementationDecl *Impl = Iface->getImplementation();
3789 if (!Impl) {
3790 // We haven't imported an implementation yet. Create a new @implementation
3791 // now.
3792 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3793 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003794 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003795 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003796 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003797 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003798 Importer.Import(D->getIvarLBraceLoc()),
3799 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003800
3801 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3802 DeclContext *LexicalDC
3803 = Importer.ImportContext(D->getLexicalDeclContext());
3804 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003805 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003806 Impl->setLexicalDeclContext(LexicalDC);
3807 }
3808
3809 // Associate the implementation with the class it implements.
3810 Iface->setImplementation(Impl);
3811 Importer.Imported(D, Iface->getImplementation());
3812 } else {
3813 Importer.Imported(D, Iface->getImplementation());
3814
3815 // Verify that the existing @implementation has the same superclass.
3816 if ((Super && !Impl->getSuperClass()) ||
3817 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003818 (Super && Impl->getSuperClass() &&
3819 !declaresSameEntity(Super->getCanonicalDecl(),
3820 Impl->getSuperClass()))) {
3821 Importer.ToDiag(Impl->getLocation(),
3822 diag::err_odr_objc_superclass_inconsistent)
3823 << Iface->getDeclName();
3824 // FIXME: It would be nice to have the location of the superclass
3825 // below.
3826 if (Impl->getSuperClass())
3827 Importer.ToDiag(Impl->getLocation(),
3828 diag::note_odr_objc_superclass)
3829 << Impl->getSuperClass()->getDeclName();
3830 else
3831 Importer.ToDiag(Impl->getLocation(),
3832 diag::note_odr_objc_missing_superclass);
3833 if (D->getSuperClass())
3834 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003835 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003836 << D->getSuperClass()->getDeclName();
3837 else
3838 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003839 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003840 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003841 }
3842 }
3843
3844 // Import all of the members of this @implementation.
3845 ImportDeclContext(D);
3846
3847 return Impl;
3848}
3849
Douglas Gregora11c4582010-02-17 18:02:10 +00003850Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3851 // Import the major distinguishing characteristics of an @property.
3852 DeclContext *DC, *LexicalDC;
3853 DeclarationName Name;
3854 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003855 NamedDecl *ToD;
3856 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003857 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003858 if (ToD)
3859 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003860
3861 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003862 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003863 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003864 for (auto *FoundDecl : FoundDecls) {
3865 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003866 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003867 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003868 FoundProp->getType())) {
3869 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3870 << Name << D->getType() << FoundProp->getType();
3871 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3872 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003873 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003874 }
3875
3876 // FIXME: Check property attributes, getters, setters, etc.?
3877
3878 // Consider these properties to be equivalent.
3879 Importer.Imported(D, FoundProp);
3880 return FoundProp;
3881 }
3882 }
3883
3884 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003885 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3886 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003887 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003888
3889 // Create the new property.
3890 ObjCPropertyDecl *ToProperty
3891 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3892 Name.getAsIdentifierInfo(),
3893 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003894 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003895 Importer.Import(D->getType()),
3896 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003897 D->getPropertyImplementation());
3898 Importer.Imported(D, ToProperty);
3899 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003900 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003901
3902 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003903 ToProperty->setPropertyAttributesAsWritten(
3904 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003905 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3906 Importer.Import(D->getGetterNameLoc()));
3907 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3908 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003909 ToProperty->setGetterMethodDecl(
3910 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3911 ToProperty->setSetterMethodDecl(
3912 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3913 ToProperty->setPropertyIvarDecl(
3914 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3915 return ToProperty;
3916}
3917
Douglas Gregor14a49e22010-12-07 18:32:03 +00003918Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003919 auto *Property = cast_or_null<ObjCPropertyDecl>(
3920 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003921 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003922 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003923
3924 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3925 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003926 return nullptr;
3927
Douglas Gregor14a49e22010-12-07 18:32:03 +00003928 // Import the lexical declaration context.
3929 DeclContext *LexicalDC = DC;
3930 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3931 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3932 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003933 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003934 }
3935
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003936 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003937 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003938 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003939
3940 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003941 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003942 if (D->getPropertyIvarDecl()) {
3943 Ivar = cast_or_null<ObjCIvarDecl>(
3944 Importer.Import(D->getPropertyIvarDecl()));
3945 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003946 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003947 }
3948
3949 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003950 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3951 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003952 if (!ToImpl) {
3953 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3954 Importer.Import(D->getLocStart()),
3955 Importer.Import(D->getLocation()),
3956 Property,
3957 D->getPropertyImplementation(),
3958 Ivar,
3959 Importer.Import(D->getPropertyIvarDeclLoc()));
3960 ToImpl->setLexicalDeclContext(LexicalDC);
3961 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003962 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003963 } else {
3964 // Check that we have the same kind of property implementation (@synthesize
3965 // vs. @dynamic).
3966 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3967 Importer.ToDiag(ToImpl->getLocation(),
3968 diag::err_odr_objc_property_impl_kind_inconsistent)
3969 << Property->getDeclName()
3970 << (ToImpl->getPropertyImplementation()
3971 == ObjCPropertyImplDecl::Dynamic);
3972 Importer.FromDiag(D->getLocation(),
3973 diag::note_odr_objc_property_impl_kind)
3974 << D->getPropertyDecl()->getDeclName()
3975 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003976 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003977 }
3978
3979 // For @synthesize, check that we have the same
3980 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3981 Ivar != ToImpl->getPropertyIvarDecl()) {
3982 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3983 diag::err_odr_objc_synthesize_ivar_inconsistent)
3984 << Property->getDeclName()
3985 << ToImpl->getPropertyIvarDecl()->getDeclName()
3986 << Ivar->getDeclName();
3987 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3988 diag::note_odr_objc_synthesize_ivar_here)
3989 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003990 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003991 }
3992
3993 // Merge the existing implementation with the new implementation.
3994 Importer.Imported(D, ToImpl);
3995 }
3996
3997 return ToImpl;
3998}
3999
Douglas Gregora082a492010-11-30 19:14:50 +00004000Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4001 // For template arguments, we adopt the translation unit as our declaration
4002 // context. This context will be fixed when the actual template declaration
4003 // is created.
4004
4005 // FIXME: Import default argument.
4006 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4007 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004008 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004009 Importer.Import(D->getLocation()),
4010 D->getDepth(),
4011 D->getIndex(),
4012 Importer.Import(D->getIdentifier()),
4013 D->wasDeclaredWithTypename(),
4014 D->isParameterPack());
4015}
4016
4017Decl *
4018ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4019 // Import the name of this declaration.
4020 DeclarationName Name = Importer.Import(D->getDeclName());
4021 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004022 return nullptr;
4023
Douglas Gregora082a492010-11-30 19:14:50 +00004024 // Import the location of this declaration.
4025 SourceLocation Loc = Importer.Import(D->getLocation());
4026
4027 // Import the type of this declaration.
4028 QualType T = Importer.Import(D->getType());
4029 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004030 return nullptr;
4031
Douglas Gregora082a492010-11-30 19:14:50 +00004032 // Import type-source information.
4033 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4034 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004035 return nullptr;
4036
Douglas Gregora082a492010-11-30 19:14:50 +00004037 // FIXME: Import default argument.
4038
4039 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4040 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004041 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004042 Loc, D->getDepth(), D->getPosition(),
4043 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004044 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004045}
4046
4047Decl *
4048ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4049 // Import the name of this declaration.
4050 DeclarationName Name = Importer.Import(D->getDeclName());
4051 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004052 return nullptr;
4053
Douglas Gregora082a492010-11-30 19:14:50 +00004054 // Import the location of this declaration.
4055 SourceLocation Loc = Importer.Import(D->getLocation());
4056
4057 // Import template parameters.
4058 TemplateParameterList *TemplateParams
4059 = ImportTemplateParameterList(D->getTemplateParameters());
4060 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004061 return nullptr;
4062
Douglas Gregora082a492010-11-30 19:14:50 +00004063 // FIXME: Import default argument.
4064
4065 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4066 Importer.getToContext().getTranslationUnitDecl(),
4067 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004068 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004069 Name.getAsIdentifierInfo(),
4070 TemplateParams);
4071}
4072
4073Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4074 // If this record has a definition in the translation unit we're coming from,
4075 // but this particular declaration is not that definition, import the
4076 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004077 auto *Definition =
4078 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004079 if (Definition && Definition != D->getTemplatedDecl()) {
4080 Decl *ImportedDef
4081 = Importer.Import(Definition->getDescribedClassTemplate());
4082 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004083 return nullptr;
4084
Douglas Gregora082a492010-11-30 19:14:50 +00004085 return Importer.Imported(D, ImportedDef);
4086 }
4087
4088 // Import the major distinguishing characteristics of this class template.
4089 DeclContext *DC, *LexicalDC;
4090 DeclarationName Name;
4091 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004092 NamedDecl *ToD;
4093 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004094 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004095 if (ToD)
4096 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004097
Douglas Gregora082a492010-11-30 19:14:50 +00004098 // We may already have a template of the same name; try to find and match it.
4099 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004100 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004101 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004102 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004103 for (auto *FoundDecl : FoundDecls) {
4104 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004105 continue;
4106
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004107 Decl *Found = FoundDecl;
4108 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Douglas Gregora082a492010-11-30 19:14:50 +00004109 if (IsStructuralMatch(D, FoundTemplate)) {
4110 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004111
4112 // We found a forward declaration but the class to be imported has a
4113 // definition.
4114 // FIXME Add this forward declaration to the redeclaration chain.
4115 if (D->isThisDeclarationADefinition() &&
4116 !FoundTemplate->isThisDeclarationADefinition())
4117 continue;
4118
Douglas Gregora082a492010-11-30 19:14:50 +00004119 Importer.Imported(D->getTemplatedDecl(),
4120 FoundTemplate->getTemplatedDecl());
4121 return Importer.Imported(D, FoundTemplate);
4122 }
4123 }
4124
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004125 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004126 }
4127
4128 if (!ConflictingDecls.empty()) {
4129 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4130 ConflictingDecls.data(),
4131 ConflictingDecls.size());
4132 }
4133
4134 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004135 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004136 }
4137
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004138 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4139
Douglas Gregora082a492010-11-30 19:14:50 +00004140 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004141 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4142 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004143 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004144 return nullptr;
4145
4146 // Resolve possible cyclic import.
4147 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4148 return AlreadyImported;
4149
Douglas Gregora082a492010-11-30 19:14:50 +00004150 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004151 TemplateParameterList *TemplateParams =
4152 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004153 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004154 return nullptr;
4155
Douglas Gregora082a492010-11-30 19:14:50 +00004156 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4157 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004158 ToTemplated);
4159 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004160
4161 D2->setAccess(D->getAccess());
4162 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004163 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004164
4165 // Note the relationship between the class templates.
4166 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004167 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004168
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004169 if (FromTemplated->isCompleteDefinition() &&
4170 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004171 // FIXME: Import definition!
4172 }
4173
4174 return D2;
4175}
4176
Douglas Gregore2e50d332010-12-01 01:36:18 +00004177Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4178 ClassTemplateSpecializationDecl *D) {
4179 // If this record has a definition in the translation unit we're coming from,
4180 // but this particular declaration is not that definition, import the
4181 // definition and map to that.
4182 TagDecl *Definition = D->getDefinition();
4183 if (Definition && Definition != D) {
4184 Decl *ImportedDef = Importer.Import(Definition);
4185 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004186 return nullptr;
4187
Douglas Gregore2e50d332010-12-01 01:36:18 +00004188 return Importer.Imported(D, ImportedDef);
4189 }
4190
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004191 auto *ClassTemplate =
4192 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004193 D->getSpecializedTemplate()));
4194 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004195 return nullptr;
4196
Douglas Gregore2e50d332010-12-01 01:36:18 +00004197 // Import the context of this declaration.
4198 DeclContext *DC = ClassTemplate->getDeclContext();
4199 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004200 return nullptr;
4201
Douglas Gregore2e50d332010-12-01 01:36:18 +00004202 DeclContext *LexicalDC = DC;
4203 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4204 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4205 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004206 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004207 }
4208
4209 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004210 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4211 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004212
4213 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004214 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004215 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4216 D->getTemplateArgs().size(),
4217 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004218 return nullptr;
4219
Douglas Gregore2e50d332010-12-01 01:36:18 +00004220 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004221 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004222 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004223 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004224 if (D2) {
4225 // We already have a class template specialization with these template
4226 // arguments.
4227
4228 // FIXME: Check for specialization vs. instantiation errors.
4229
4230 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004231 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004232 // The record types structurally match, or the "from" translation
4233 // unit only had a forward declaration anyway; call it the same
4234 // function.
4235 return Importer.Imported(D, FoundDef);
4236 }
4237 }
4238 } else {
4239 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004240 if (auto *PartialSpec =
4241 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004242 // Import TemplateArgumentListInfo
4243 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004244 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4245 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004246 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004247
4248 QualType CanonInjType = Importer.Import(
4249 PartialSpec->getInjectedSpecializationType());
4250 if (CanonInjType.isNull())
4251 return nullptr;
4252 CanonInjType = CanonInjType.getCanonicalType();
4253
4254 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4255 PartialSpec->getTemplateParameters());
4256 if (!ToTPList && PartialSpec->getTemplateParameters())
4257 return nullptr;
4258
4259 D2 = ClassTemplatePartialSpecializationDecl::Create(
4260 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4261 ToTPList, ClassTemplate,
4262 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4263 ToTAInfo, CanonInjType, nullptr);
4264
4265 } else {
4266 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4267 D->getTagKind(), DC,
4268 StartLoc, IdLoc,
4269 ClassTemplate,
4270 TemplateArgs,
4271 /*PrevDecl=*/nullptr);
4272 }
4273
Douglas Gregore2e50d332010-12-01 01:36:18 +00004274 D2->setSpecializationKind(D->getSpecializationKind());
4275
4276 // Add this specialization to the class template.
4277 ClassTemplate->AddSpecialization(D2, InsertPos);
4278
4279 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004280 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004281
4282 Importer.Imported(D, D2);
4283
4284 if (auto *TSI = D->getTypeAsWritten()) {
4285 TypeSourceInfo *TInfo = Importer.Import(TSI);
4286 if (!TInfo)
4287 return nullptr;
4288 D2->setTypeAsWritten(TInfo);
4289 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4290 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4291 }
4292
4293 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4294 if (POI.isValid())
4295 D2->setPointOfInstantiation(POI);
4296 else if (D->getPointOfInstantiation().isValid())
4297 return nullptr;
4298
4299 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4300
Douglas Gregore2e50d332010-12-01 01:36:18 +00004301 // Add the specialization to this context.
4302 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004303 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004304 }
4305 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004306 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004307 return nullptr;
4308
Douglas Gregore2e50d332010-12-01 01:36:18 +00004309 return D2;
4310}
4311
Larisse Voufo39a1e502013-08-06 01:03:05 +00004312Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4313 // If this variable has a definition in the translation unit we're coming
4314 // from,
4315 // but this particular declaration is not that definition, import the
4316 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004317 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004318 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4319 if (Definition && Definition != D->getTemplatedDecl()) {
4320 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4321 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004322 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004323
4324 return Importer.Imported(D, ImportedDef);
4325 }
4326
4327 // Import the major distinguishing characteristics of this variable template.
4328 DeclContext *DC, *LexicalDC;
4329 DeclarationName Name;
4330 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004331 NamedDecl *ToD;
4332 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004333 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004334 if (ToD)
4335 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004336
4337 // We may already have a template of the same name; try to find and match it.
4338 assert(!DC->isFunctionOrMethod() &&
4339 "Variable templates cannot be declared at function scope");
4340 SmallVector<NamedDecl *, 4> ConflictingDecls;
4341 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004342 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004343 for (auto *FoundDecl : FoundDecls) {
4344 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004345 continue;
4346
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004347 Decl *Found = FoundDecl;
4348 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004349 if (IsStructuralMatch(D, FoundTemplate)) {
4350 // The variable templates structurally match; call it the same template.
4351 Importer.Imported(D->getTemplatedDecl(),
4352 FoundTemplate->getTemplatedDecl());
4353 return Importer.Imported(D, FoundTemplate);
4354 }
4355 }
4356
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004357 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004358 }
4359
4360 if (!ConflictingDecls.empty()) {
4361 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4362 ConflictingDecls.data(),
4363 ConflictingDecls.size());
4364 }
4365
4366 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004367 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004368
4369 VarDecl *DTemplated = D->getTemplatedDecl();
4370
4371 // Import the type.
4372 QualType T = Importer.Import(DTemplated->getType());
4373 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004374 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004375
4376 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004377 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4378 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004379 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004380
4381 // Create the variable template declaration itself.
4382 TemplateParameterList *TemplateParams =
4383 ImportTemplateParameterList(D->getTemplateParameters());
4384 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004385 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004386
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004387 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4388 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4389 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004390
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004391 ToVarTD->setAccess(D->getAccess());
4392 ToVarTD->setLexicalDeclContext(LexicalDC);
4393 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004394
4395 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004396 Importer.Imported(D, ToVarTD);
4397 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004398
4399 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004400 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004401 // FIXME: Import definition!
4402 }
4403
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004404 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004405}
4406
4407Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4408 VarTemplateSpecializationDecl *D) {
4409 // If this record has a definition in the translation unit we're coming from,
4410 // but this particular declaration is not that definition, import the
4411 // definition and map to that.
4412 VarDecl *Definition = D->getDefinition();
4413 if (Definition && Definition != D) {
4414 Decl *ImportedDef = Importer.Import(Definition);
4415 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004416 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004417
4418 return Importer.Imported(D, ImportedDef);
4419 }
4420
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004421 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004422 Importer.Import(D->getSpecializedTemplate()));
4423 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004424 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004425
4426 // Import the context of this declaration.
4427 DeclContext *DC = VarTemplate->getDeclContext();
4428 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004429 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004430
4431 DeclContext *LexicalDC = DC;
4432 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4433 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4434 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004435 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004436 }
4437
4438 // Import the location of this declaration.
4439 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4440 SourceLocation IdLoc = Importer.Import(D->getLocation());
4441
4442 // Import template arguments.
4443 SmallVector<TemplateArgument, 2> TemplateArgs;
4444 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4445 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004446 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004447
4448 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004449 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004450 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004451 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004452 if (D2) {
4453 // We already have a variable template specialization with these template
4454 // arguments.
4455
4456 // FIXME: Check for specialization vs. instantiation errors.
4457
4458 if (VarDecl *FoundDef = D2->getDefinition()) {
4459 if (!D->isThisDeclarationADefinition() ||
4460 IsStructuralMatch(D, FoundDef)) {
4461 // The record types structurally match, or the "from" translation
4462 // unit only had a forward declaration anyway; call it the same
4463 // variable.
4464 return Importer.Imported(D, FoundDef);
4465 }
4466 }
4467 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004468 // Import the type.
4469 QualType T = Importer.Import(D->getType());
4470 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004471 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004472
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004473 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4474 if (D->getTypeSourceInfo() && !TInfo)
4475 return nullptr;
4476
4477 TemplateArgumentListInfo ToTAInfo;
4478 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4479 return nullptr;
4480
4481 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004482 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004483 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4484 // Import TemplateArgumentListInfo
4485 TemplateArgumentListInfo ArgInfos;
4486 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4487 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4488 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4489 return nullptr;
4490
4491 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4492 FromPartial->getTemplateParameters());
4493 if (!ToTPList)
4494 return nullptr;
4495
4496 auto *ToPartial = PartVarSpecDecl::Create(
4497 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4498 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4499
4500 auto *FromInst = FromPartial->getInstantiatedFromMember();
4501 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4502 if (FromInst && !ToInst)
4503 return nullptr;
4504
4505 ToPartial->setInstantiatedFromMember(ToInst);
4506 if (FromPartial->isMemberSpecialization())
4507 ToPartial->setMemberSpecialization();
4508
4509 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004510 } else { // Full specialization
4511 D2 = VarTemplateSpecializationDecl::Create(
4512 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4513 D->getStorageClass(), TemplateArgs);
4514 }
4515
4516 SourceLocation POI = D->getPointOfInstantiation();
4517 if (POI.isValid())
4518 D2->setPointOfInstantiation(Importer.Import(POI));
4519
Larisse Voufo39a1e502013-08-06 01:03:05 +00004520 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004521 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004522
4523 // Add this specialization to the class template.
4524 VarTemplate->AddSpecialization(D2, InsertPos);
4525
4526 // Import the qualifier, if any.
4527 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4528
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004529 if (D->isConstexpr())
4530 D2->setConstexpr(true);
4531
Larisse Voufo39a1e502013-08-06 01:03:05 +00004532 // Add the specialization to this context.
4533 D2->setLexicalDeclContext(LexicalDC);
4534 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004535
4536 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004537 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004538
Larisse Voufo39a1e502013-08-06 01:03:05 +00004539 Importer.Imported(D, D2);
4540
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004541 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4542 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4543 // we have to check the declaration for initializer - otherwise, it won't be
4544 // imported.
4545 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4546 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004547 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004548
4549 return D2;
4550}
4551
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004552Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4553 DeclContext *DC, *LexicalDC;
4554 DeclarationName Name;
4555 SourceLocation Loc;
4556 NamedDecl *ToD;
4557
4558 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4559 return nullptr;
4560
4561 if (ToD)
4562 return ToD;
4563
4564 // Try to find a function in our own ("to") context with the same name, same
4565 // type, and in the same context as the function we're importing.
4566 if (!LexicalDC->isFunctionOrMethod()) {
4567 unsigned IDNS = Decl::IDNS_Ordinary;
4568 SmallVector<NamedDecl *, 2> FoundDecls;
4569 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004570 for (auto *FoundDecl : FoundDecls) {
4571 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004572 continue;
4573
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004574 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004575 if (FoundFunction->hasExternalFormalLinkage() &&
4576 D->hasExternalFormalLinkage()) {
4577 if (IsStructuralMatch(D, FoundFunction)) {
4578 Importer.Imported(D, FoundFunction);
4579 // FIXME: Actually try to merge the body and other attributes.
4580 return FoundFunction;
4581 }
4582 }
4583 }
4584 }
4585 }
4586
4587 TemplateParameterList *Params =
4588 ImportTemplateParameterList(D->getTemplateParameters());
4589 if (!Params)
4590 return nullptr;
4591
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004592 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004593 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4594 if (!TemplatedFD)
4595 return nullptr;
4596
4597 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4598 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4599
4600 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4601 ToFunc->setAccess(D->getAccess());
4602 ToFunc->setLexicalDeclContext(LexicalDC);
4603 Importer.Imported(D, ToFunc);
4604
4605 LexicalDC->addDeclInternal(ToFunc);
4606 return ToFunc;
4607}
4608
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004609//----------------------------------------------------------------------------
4610// Import Statements
4611//----------------------------------------------------------------------------
4612
Sean Callanan59721b32015-04-28 18:41:46 +00004613DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4614 if (DG.isNull())
4615 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4616 size_t NumDecls = DG.end() - DG.begin();
4617 SmallVector<Decl *, 1> ToDecls(NumDecls);
4618 auto &_Importer = this->Importer;
4619 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4620 [&_Importer](Decl *D) -> Decl * {
4621 return _Importer.Import(D);
4622 });
4623 return DeclGroupRef::Create(Importer.getToContext(),
4624 ToDecls.begin(),
4625 NumDecls);
4626}
4627
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004628Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4629 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4630 << S->getStmtClassName();
4631 return nullptr;
4632}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004633
4634Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4635 SmallVector<IdentifierInfo *, 4> Names;
4636 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4637 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004638 // ToII is nullptr when no symbolic name is given for output operand
4639 // see ParseStmtAsm::ParseAsmOperandsOpt
4640 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004641 return nullptr;
4642 Names.push_back(ToII);
4643 }
4644 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4645 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004646 // ToII is nullptr when no symbolic name is given for input operand
4647 // see ParseStmtAsm::ParseAsmOperandsOpt
4648 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004649 return nullptr;
4650 Names.push_back(ToII);
4651 }
4652
4653 SmallVector<StringLiteral *, 4> Clobbers;
4654 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004655 auto *Clobber = cast_or_null<StringLiteral>(
4656 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004657 if (!Clobber)
4658 return nullptr;
4659 Clobbers.push_back(Clobber);
4660 }
4661
4662 SmallVector<StringLiteral *, 4> Constraints;
4663 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004664 auto *Output = cast_or_null<StringLiteral>(
4665 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004666 if (!Output)
4667 return nullptr;
4668 Constraints.push_back(Output);
4669 }
4670
4671 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004672 auto *Input = cast_or_null<StringLiteral>(
4673 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004674 if (!Input)
4675 return nullptr;
4676 Constraints.push_back(Input);
4677 }
4678
4679 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004680 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004681 return nullptr;
4682
Aleksei Sidorina693b372016-09-28 10:16:56 +00004683 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004684 return nullptr;
4685
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004686 auto *AsmStr = cast_or_null<StringLiteral>(
4687 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004688 if (!AsmStr)
4689 return nullptr;
4690
4691 return new (Importer.getToContext()) GCCAsmStmt(
4692 Importer.getToContext(),
4693 Importer.Import(S->getAsmLoc()),
4694 S->isSimple(),
4695 S->isVolatile(),
4696 S->getNumOutputs(),
4697 S->getNumInputs(),
4698 Names.data(),
4699 Constraints.data(),
4700 Exprs.data(),
4701 AsmStr,
4702 S->getNumClobbers(),
4703 Clobbers.data(),
4704 Importer.Import(S->getRParenLoc()));
4705}
4706
Sean Callanan59721b32015-04-28 18:41:46 +00004707Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4708 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004709 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004710 if (!ToD)
4711 return nullptr;
4712 }
4713 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4714 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4715 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4716}
4717
4718Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4719 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4720 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4721 S->hasLeadingEmptyMacro());
4722}
4723
4724Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004725 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004726
4727 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004728 return nullptr;
4729
Sean Callanan59721b32015-04-28 18:41:46 +00004730 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4731 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004732 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4733 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004734}
4735
4736Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4737 Expr *ToLHS = Importer.Import(S->getLHS());
4738 if (!ToLHS)
4739 return nullptr;
4740 Expr *ToRHS = Importer.Import(S->getRHS());
4741 if (!ToRHS && S->getRHS())
4742 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004743 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4744 if (!ToSubStmt && S->getSubStmt())
4745 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004746 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4747 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4748 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004749 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004750 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4751 ToStmt->setSubStmt(ToSubStmt);
4752 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004753}
4754
4755Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4756 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4757 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4758 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4759 if (!ToSubStmt && S->getSubStmt())
4760 return nullptr;
4761 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4762 ToSubStmt);
4763}
4764
4765Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4766 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004767 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004768 if (!ToLabelDecl && S->getDecl())
4769 return nullptr;
4770 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4771 if (!ToSubStmt && S->getSubStmt())
4772 return nullptr;
4773 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4774 ToSubStmt);
4775}
4776
4777Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4778 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4779 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4780 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00004781 if (ImportContainerChecked(FromAttrs, ToAttrs))
4782 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004783 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4784 if (!ToSubStmt && S->getSubStmt())
4785 return nullptr;
4786 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4787 ToAttrs, ToSubStmt);
4788}
4789
4790Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4791 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004792 Stmt *ToInit = Importer.Import(S->getInit());
4793 if (!ToInit && S->getInit())
4794 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004795 VarDecl *ToConditionVariable = nullptr;
4796 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4797 ToConditionVariable =
4798 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4799 if (!ToConditionVariable)
4800 return nullptr;
4801 }
4802 Expr *ToCondition = Importer.Import(S->getCond());
4803 if (!ToCondition && S->getCond())
4804 return nullptr;
4805 Stmt *ToThenStmt = Importer.Import(S->getThen());
4806 if (!ToThenStmt && S->getThen())
4807 return nullptr;
4808 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4809 Stmt *ToElseStmt = Importer.Import(S->getElse());
4810 if (!ToElseStmt && S->getElse())
4811 return nullptr;
4812 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004813 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004814 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004815 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004816 ToCondition, ToThenStmt,
4817 ToElseLoc, ToElseStmt);
4818}
4819
4820Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004821 Stmt *ToInit = Importer.Import(S->getInit());
4822 if (!ToInit && S->getInit())
4823 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004824 VarDecl *ToConditionVariable = nullptr;
4825 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4826 ToConditionVariable =
4827 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4828 if (!ToConditionVariable)
4829 return nullptr;
4830 }
4831 Expr *ToCondition = Importer.Import(S->getCond());
4832 if (!ToCondition && S->getCond())
4833 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004834 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004835 Importer.getToContext(), ToInit,
4836 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004837 Stmt *ToBody = Importer.Import(S->getBody());
4838 if (!ToBody && S->getBody())
4839 return nullptr;
4840 ToStmt->setBody(ToBody);
4841 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4842 // Now we have to re-chain the cases.
4843 SwitchCase *LastChainedSwitchCase = nullptr;
4844 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4845 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004846 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004847 if (!ToSC)
4848 return nullptr;
4849 if (LastChainedSwitchCase)
4850 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4851 else
4852 ToStmt->setSwitchCaseList(ToSC);
4853 LastChainedSwitchCase = ToSC;
4854 }
4855 return ToStmt;
4856}
4857
4858Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4859 VarDecl *ToConditionVariable = nullptr;
4860 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4861 ToConditionVariable =
4862 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4863 if (!ToConditionVariable)
4864 return nullptr;
4865 }
4866 Expr *ToCondition = Importer.Import(S->getCond());
4867 if (!ToCondition && S->getCond())
4868 return nullptr;
4869 Stmt *ToBody = Importer.Import(S->getBody());
4870 if (!ToBody && S->getBody())
4871 return nullptr;
4872 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4873 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4874 ToConditionVariable,
4875 ToCondition, ToBody,
4876 ToWhileLoc);
4877}
4878
4879Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4880 Stmt *ToBody = Importer.Import(S->getBody());
4881 if (!ToBody && S->getBody())
4882 return nullptr;
4883 Expr *ToCondition = Importer.Import(S->getCond());
4884 if (!ToCondition && S->getCond())
4885 return nullptr;
4886 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4887 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4888 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4889 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4890 ToDoLoc, ToWhileLoc,
4891 ToRParenLoc);
4892}
4893
4894Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4895 Stmt *ToInit = Importer.Import(S->getInit());
4896 if (!ToInit && S->getInit())
4897 return nullptr;
4898 Expr *ToCondition = Importer.Import(S->getCond());
4899 if (!ToCondition && S->getCond())
4900 return nullptr;
4901 VarDecl *ToConditionVariable = nullptr;
4902 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4903 ToConditionVariable =
4904 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4905 if (!ToConditionVariable)
4906 return nullptr;
4907 }
4908 Expr *ToInc = Importer.Import(S->getInc());
4909 if (!ToInc && S->getInc())
4910 return nullptr;
4911 Stmt *ToBody = Importer.Import(S->getBody());
4912 if (!ToBody && S->getBody())
4913 return nullptr;
4914 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4915 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4916 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4917 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4918 ToInit, ToCondition,
4919 ToConditionVariable,
4920 ToInc, ToBody,
4921 ToForLoc, ToLParenLoc,
4922 ToRParenLoc);
4923}
4924
4925Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4926 LabelDecl *ToLabel = nullptr;
4927 if (LabelDecl *FromLabel = S->getLabel()) {
4928 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4929 if (!ToLabel)
4930 return nullptr;
4931 }
4932 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4933 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4934 return new (Importer.getToContext()) GotoStmt(ToLabel,
4935 ToGotoLoc, ToLabelLoc);
4936}
4937
4938Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4939 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4940 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4941 Expr *ToTarget = Importer.Import(S->getTarget());
4942 if (!ToTarget && S->getTarget())
4943 return nullptr;
4944 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4945 ToTarget);
4946}
4947
4948Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4949 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4950 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4951}
4952
4953Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4954 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4955 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4956}
4957
4958Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4959 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4960 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4961 if (!ToRetExpr && S->getRetValue())
4962 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004963 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4964 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004965 if (!ToNRVOCandidate && NRVOCandidate)
4966 return nullptr;
4967 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4968 ToNRVOCandidate);
4969}
4970
4971Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4972 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4973 VarDecl *ToExceptionDecl = nullptr;
4974 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4975 ToExceptionDecl =
4976 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4977 if (!ToExceptionDecl)
4978 return nullptr;
4979 }
4980 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4981 if (!ToHandlerBlock && S->getHandlerBlock())
4982 return nullptr;
4983 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4984 ToExceptionDecl,
4985 ToHandlerBlock);
4986}
4987
4988Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4989 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4990 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4991 if (!ToTryBlock && S->getTryBlock())
4992 return nullptr;
4993 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4994 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4995 CXXCatchStmt *FromHandler = S->getHandler(HI);
4996 if (Stmt *ToHandler = Importer.Import(FromHandler))
4997 ToHandlers[HI] = ToHandler;
4998 else
4999 return nullptr;
5000 }
5001 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5002 ToHandlers);
5003}
5004
5005Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005006 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005007 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5008 if (!ToRange && S->getRangeStmt())
5009 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005010 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005011 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5012 if (!ToBegin && S->getBeginStmt())
5013 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005014 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005015 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5016 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005017 return nullptr;
5018 Expr *ToCond = Importer.Import(S->getCond());
5019 if (!ToCond && S->getCond())
5020 return nullptr;
5021 Expr *ToInc = Importer.Import(S->getInc());
5022 if (!ToInc && S->getInc())
5023 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005024 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005025 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5026 if (!ToLoopVar && S->getLoopVarStmt())
5027 return nullptr;
5028 Stmt *ToBody = Importer.Import(S->getBody());
5029 if (!ToBody && S->getBody())
5030 return nullptr;
5031 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005032 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005033 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5034 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005035 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005036 ToCond, ToInc,
5037 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005038 ToForLoc, ToCoawaitLoc,
5039 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005040}
5041
5042Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5043 Stmt *ToElem = Importer.Import(S->getElement());
5044 if (!ToElem && S->getElement())
5045 return nullptr;
5046 Expr *ToCollect = Importer.Import(S->getCollection());
5047 if (!ToCollect && S->getCollection())
5048 return nullptr;
5049 Stmt *ToBody = Importer.Import(S->getBody());
5050 if (!ToBody && S->getBody())
5051 return nullptr;
5052 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5053 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5054 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5055 ToCollect,
5056 ToBody, ToForLoc,
5057 ToRParenLoc);
5058}
5059
5060Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5061 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5062 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5063 VarDecl *ToExceptionDecl = nullptr;
5064 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5065 ToExceptionDecl =
5066 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5067 if (!ToExceptionDecl)
5068 return nullptr;
5069 }
5070 Stmt *ToBody = Importer.Import(S->getCatchBody());
5071 if (!ToBody && S->getCatchBody())
5072 return nullptr;
5073 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5074 ToRParenLoc,
5075 ToExceptionDecl,
5076 ToBody);
5077}
5078
5079Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5080 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5081 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5082 if (!ToAtFinallyStmt && S->getFinallyBody())
5083 return nullptr;
5084 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5085 ToAtFinallyStmt);
5086}
5087
5088Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5089 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5090 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5091 if (!ToAtTryStmt && S->getTryBody())
5092 return nullptr;
5093 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5094 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5095 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5096 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5097 ToCatchStmts[CI] = ToCatchStmt;
5098 else
5099 return nullptr;
5100 }
5101 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5102 if (!ToAtFinallyStmt && S->getFinallyStmt())
5103 return nullptr;
5104 return ObjCAtTryStmt::Create(Importer.getToContext(),
5105 ToAtTryLoc, ToAtTryStmt,
5106 ToCatchStmts.begin(), ToCatchStmts.size(),
5107 ToAtFinallyStmt);
5108}
5109
5110Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5111 (ObjCAtSynchronizedStmt *S) {
5112 SourceLocation ToAtSynchronizedLoc =
5113 Importer.Import(S->getAtSynchronizedLoc());
5114 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5115 if (!ToSynchExpr && S->getSynchExpr())
5116 return nullptr;
5117 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5118 if (!ToSynchBody && S->getSynchBody())
5119 return nullptr;
5120 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5121 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5122}
5123
5124Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5125 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5126 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5127 if (!ToThrow && S->getThrowExpr())
5128 return nullptr;
5129 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5130}
5131
5132Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5133 (ObjCAutoreleasePoolStmt *S) {
5134 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5135 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5136 if (!ToSubStmt && S->getSubStmt())
5137 return nullptr;
5138 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5139 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005140}
5141
5142//----------------------------------------------------------------------------
5143// Import Expressions
5144//----------------------------------------------------------------------------
5145Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5146 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5147 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005148 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005149}
5150
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005151Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5152 QualType T = Importer.Import(E->getType());
5153 if (T.isNull())
5154 return nullptr;
5155
5156 Expr *SubExpr = Importer.Import(E->getSubExpr());
5157 if (!SubExpr && E->getSubExpr())
5158 return nullptr;
5159
5160 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5161 if (!TInfo)
5162 return nullptr;
5163
5164 return new (Importer.getToContext()) VAArgExpr(
5165 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5166 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5167}
5168
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005169Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5170 QualType T = Importer.Import(E->getType());
5171 if (T.isNull())
5172 return nullptr;
5173
5174 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005175 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005176}
5177
5178Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5179 QualType T = Importer.Import(E->getType());
5180 if (T.isNull())
5181 return nullptr;
5182
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005183 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005184 if (!SL && E->getFunctionName())
5185 return nullptr;
5186
5187 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005188 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005189}
5190
Douglas Gregor52f820e2010-02-19 01:17:02 +00005191Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005192 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005193 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005194 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005195
Craig Topper36250ad2014-05-12 05:36:57 +00005196 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005197 if (E->getDecl() != E->getFoundDecl()) {
5198 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5199 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005200 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005201 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005202
5203 QualType T = Importer.Import(E->getType());
5204 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005205 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005206
Aleksei Sidorina693b372016-09-28 10:16:56 +00005207 TemplateArgumentListInfo ToTAInfo;
5208 TemplateArgumentListInfo *ResInfo = nullptr;
5209 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005210 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5211 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005212 ResInfo = &ToTAInfo;
5213 }
5214
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005215 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5216 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005217 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005218 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005219 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005220 Importer.Import(E->getLocation()),
5221 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005222 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005223 if (E->hadMultipleCandidates())
5224 DRE->setHadMultipleCandidates(true);
5225 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005226}
5227
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005228Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5229 QualType T = Importer.Import(E->getType());
5230 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005231 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005232
5233 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5234}
5235
5236ASTNodeImporter::Designator
5237ASTNodeImporter::ImportDesignator(const Designator &D) {
5238 if (D.isFieldDesignator()) {
5239 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5240 // Caller checks for import error
5241 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5242 Importer.Import(D.getFieldLoc()));
5243 }
5244 if (D.isArrayDesignator())
5245 return Designator(D.getFirstExprIndex(),
5246 Importer.Import(D.getLBracketLoc()),
5247 Importer.Import(D.getRBracketLoc()));
5248
5249 assert(D.isArrayRangeDesignator());
5250 return Designator(D.getFirstExprIndex(),
5251 Importer.Import(D.getLBracketLoc()),
5252 Importer.Import(D.getEllipsisLoc()),
5253 Importer.Import(D.getRBracketLoc()));
5254}
5255
5256
5257Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005258 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005259 if (!Init)
5260 return nullptr;
5261
5262 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5263 // List elements from the second, the first is Init itself
5264 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005265 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005266 IndexExprs[I - 1] = Arg;
5267 else
5268 return nullptr;
5269 }
5270
5271 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005272 llvm::transform(DIE->designators(), Designators.begin(),
5273 [this](const Designator &D) -> Designator {
5274 return ImportDesignator(D);
5275 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005276
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005277 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005278 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005279 return nullptr;
5280
5281 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005282 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005283 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5284 DIE->usesGNUSyntax(), Init);
5285}
5286
5287Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5288 QualType T = Importer.Import(E->getType());
5289 if (T.isNull())
5290 return nullptr;
5291
5292 return new (Importer.getToContext())
5293 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5294}
5295
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005296Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5297 QualType T = Importer.Import(E->getType());
5298 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005299 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005300
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005301 return IntegerLiteral::Create(Importer.getToContext(),
5302 E->getValue(), T,
5303 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005304}
5305
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005306Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5307 QualType T = Importer.Import(E->getType());
5308 if (T.isNull())
5309 return nullptr;
5310
5311 return FloatingLiteral::Create(Importer.getToContext(),
5312 E->getValue(), E->isExact(), T,
5313 Importer.Import(E->getLocation()));
5314}
5315
Douglas Gregor623421d2010-02-18 02:21:22 +00005316Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005319 return nullptr;
5320
Douglas Gregorfb65e592011-07-27 05:40:30 +00005321 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5322 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005323 Importer.Import(E->getLocation()));
5324}
5325
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005326Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5327 QualType T = Importer.Import(E->getType());
5328 if (T.isNull())
5329 return nullptr;
5330
5331 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5332 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5333
5334 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5335 E->getKind(), E->isPascal(), T,
5336 Locations.data(), Locations.size());
5337}
5338
5339Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5340 QualType T = Importer.Import(E->getType());
5341 if (T.isNull())
5342 return nullptr;
5343
5344 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5345 if (!TInfo)
5346 return nullptr;
5347
5348 Expr *Init = Importer.Import(E->getInitializer());
5349 if (!Init)
5350 return nullptr;
5351
5352 return new (Importer.getToContext()) CompoundLiteralExpr(
5353 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5354 Init, E->isFileScope());
5355}
5356
5357Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5358 QualType T = Importer.Import(E->getType());
5359 if (T.isNull())
5360 return nullptr;
5361
5362 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5363 if (ImportArrayChecked(
5364 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5365 Exprs.begin()))
5366 return nullptr;
5367
5368 return new (Importer.getToContext()) AtomicExpr(
5369 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5370 Importer.Import(E->getRParenLoc()));
5371}
5372
5373Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5374 QualType T = Importer.Import(E->getType());
5375 if (T.isNull())
5376 return nullptr;
5377
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005378 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005379 if (!ToLabel)
5380 return nullptr;
5381
5382 return new (Importer.getToContext()) AddrLabelExpr(
5383 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5384 ToLabel, T);
5385}
5386
Douglas Gregorc74247e2010-02-19 01:07:06 +00005387Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5388 Expr *SubExpr = Importer.Import(E->getSubExpr());
5389 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005390 return nullptr;
5391
Douglas Gregorc74247e2010-02-19 01:07:06 +00005392 return new (Importer.getToContext())
5393 ParenExpr(Importer.Import(E->getLParen()),
5394 Importer.Import(E->getRParen()),
5395 SubExpr);
5396}
5397
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005398Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5399 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005400 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005401 return nullptr;
5402
5403 return new (Importer.getToContext()) ParenListExpr(
5404 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5405 Exprs, Importer.Import(E->getLParenLoc()));
5406}
5407
5408Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5409 QualType T = Importer.Import(E->getType());
5410 if (T.isNull())
5411 return nullptr;
5412
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005413 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5414 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005415 if (!ToSubStmt && E->getSubStmt())
5416 return nullptr;
5417
5418 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5419 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5420}
5421
Douglas Gregorc74247e2010-02-19 01:07:06 +00005422Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5423 QualType T = Importer.Import(E->getType());
5424 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005425 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005426
5427 Expr *SubExpr = Importer.Import(E->getSubExpr());
5428 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005429 return nullptr;
5430
Aaron Ballmana5038552018-01-09 13:07:03 +00005431 return new (Importer.getToContext()) UnaryOperator(
5432 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5433 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005434}
5435
Aaron Ballmana5038552018-01-09 13:07:03 +00005436Expr *
5437ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005438 QualType ResultType = Importer.Import(E->getType());
5439
5440 if (E->isArgumentType()) {
5441 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5442 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005443 return nullptr;
5444
Peter Collingbournee190dee2011-03-11 19:24:49 +00005445 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5446 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005447 Importer.Import(E->getOperatorLoc()),
5448 Importer.Import(E->getRParenLoc()));
5449 }
5450
5451 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5452 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005453 return nullptr;
5454
Peter Collingbournee190dee2011-03-11 19:24:49 +00005455 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5456 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005457 Importer.Import(E->getOperatorLoc()),
5458 Importer.Import(E->getRParenLoc()));
5459}
5460
Douglas Gregorc74247e2010-02-19 01:07:06 +00005461Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5462 QualType T = Importer.Import(E->getType());
5463 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005464 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005465
5466 Expr *LHS = Importer.Import(E->getLHS());
5467 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005468 return nullptr;
5469
Douglas Gregorc74247e2010-02-19 01:07:06 +00005470 Expr *RHS = Importer.Import(E->getRHS());
5471 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005472 return nullptr;
5473
Douglas Gregorc74247e2010-02-19 01:07:06 +00005474 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005475 T, E->getValueKind(),
5476 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005477 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005478 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005479}
5480
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005481Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5482 QualType T = Importer.Import(E->getType());
5483 if (T.isNull())
5484 return nullptr;
5485
5486 Expr *ToLHS = Importer.Import(E->getLHS());
5487 if (!ToLHS)
5488 return nullptr;
5489
5490 Expr *ToRHS = Importer.Import(E->getRHS());
5491 if (!ToRHS)
5492 return nullptr;
5493
5494 Expr *ToCond = Importer.Import(E->getCond());
5495 if (!ToCond)
5496 return nullptr;
5497
5498 return new (Importer.getToContext()) ConditionalOperator(
5499 ToCond, Importer.Import(E->getQuestionLoc()),
5500 ToLHS, Importer.Import(E->getColonLoc()),
5501 ToRHS, T, E->getValueKind(), E->getObjectKind());
5502}
5503
5504Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5505 BinaryConditionalOperator *E) {
5506 QualType T = Importer.Import(E->getType());
5507 if (T.isNull())
5508 return nullptr;
5509
5510 Expr *Common = Importer.Import(E->getCommon());
5511 if (!Common)
5512 return nullptr;
5513
5514 Expr *Cond = Importer.Import(E->getCond());
5515 if (!Cond)
5516 return nullptr;
5517
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005518 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5519 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005520 if (!OpaqueValue)
5521 return nullptr;
5522
5523 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5524 if (!TrueExpr)
5525 return nullptr;
5526
5527 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5528 if (!FalseExpr)
5529 return nullptr;
5530
5531 return new (Importer.getToContext()) BinaryConditionalOperator(
5532 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5533 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5534 T, E->getValueKind(), E->getObjectKind());
5535}
5536
Aleksei Sidorina693b372016-09-28 10:16:56 +00005537Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5538 QualType T = Importer.Import(E->getType());
5539 if (T.isNull())
5540 return nullptr;
5541
5542 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5543 if (!ToQueried)
5544 return nullptr;
5545
5546 Expr *Dim = Importer.Import(E->getDimensionExpression());
5547 if (!Dim && E->getDimensionExpression())
5548 return nullptr;
5549
5550 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5551 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5552 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5553}
5554
5555Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5556 QualType T = Importer.Import(E->getType());
5557 if (T.isNull())
5558 return nullptr;
5559
5560 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5561 if (!ToQueried)
5562 return nullptr;
5563
5564 return new (Importer.getToContext()) ExpressionTraitExpr(
5565 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5566 E->getValue(), Importer.Import(E->getLocEnd()), T);
5567}
5568
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005569Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5570 QualType T = Importer.Import(E->getType());
5571 if (T.isNull())
5572 return nullptr;
5573
5574 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5575 if (!SourceExpr && E->getSourceExpr())
5576 return nullptr;
5577
5578 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005579 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005580 E->getObjectKind(), SourceExpr);
5581}
5582
Aleksei Sidorina693b372016-09-28 10:16:56 +00005583Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5584 QualType T = Importer.Import(E->getType());
5585 if (T.isNull())
5586 return nullptr;
5587
5588 Expr *ToLHS = Importer.Import(E->getLHS());
5589 if (!ToLHS)
5590 return nullptr;
5591
5592 Expr *ToRHS = Importer.Import(E->getRHS());
5593 if (!ToRHS)
5594 return nullptr;
5595
5596 return new (Importer.getToContext()) ArraySubscriptExpr(
5597 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5598 Importer.Import(E->getRBracketLoc()));
5599}
5600
Douglas Gregorc74247e2010-02-19 01:07:06 +00005601Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5602 QualType T = Importer.Import(E->getType());
5603 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005604 return nullptr;
5605
Douglas Gregorc74247e2010-02-19 01:07:06 +00005606 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5607 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005608 return nullptr;
5609
Douglas Gregorc74247e2010-02-19 01:07:06 +00005610 QualType CompResultType = Importer.Import(E->getComputationResultType());
5611 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005612 return nullptr;
5613
Douglas Gregorc74247e2010-02-19 01:07:06 +00005614 Expr *LHS = Importer.Import(E->getLHS());
5615 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005616 return nullptr;
5617
Douglas Gregorc74247e2010-02-19 01:07:06 +00005618 Expr *RHS = Importer.Import(E->getRHS());
5619 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005620 return nullptr;
5621
Douglas Gregorc74247e2010-02-19 01:07:06 +00005622 return new (Importer.getToContext())
5623 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005624 T, E->getValueKind(),
5625 E->getObjectKind(),
5626 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005627 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005628 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005629}
5630
Aleksei Sidorina693b372016-09-28 10:16:56 +00005631bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5632 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5633 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5634 Path.push_back(Spec);
5635 else
5636 return true;
5637 }
5638 return false;
John McCallcf142162010-08-07 06:22:56 +00005639}
5640
Douglas Gregor98c10182010-02-12 22:17:39 +00005641Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5642 QualType T = Importer.Import(E->getType());
5643 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005644 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005645
5646 Expr *SubExpr = Importer.Import(E->getSubExpr());
5647 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005648 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005649
5650 CXXCastPath BasePath;
5651 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005652 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005653
5654 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005655 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005656}
5657
Aleksei Sidorina693b372016-09-28 10:16:56 +00005658Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005659 QualType T = Importer.Import(E->getType());
5660 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005661 return nullptr;
5662
Douglas Gregor5481d322010-02-19 01:32:14 +00005663 Expr *SubExpr = Importer.Import(E->getSubExpr());
5664 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005665 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005666
5667 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5668 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005669 return nullptr;
5670
John McCallcf142162010-08-07 06:22:56 +00005671 CXXCastPath BasePath;
5672 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005673 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005674
Aleksei Sidorina693b372016-09-28 10:16:56 +00005675 switch (E->getStmtClass()) {
5676 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005677 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005678 return CStyleCastExpr::Create(Importer.getToContext(), T,
5679 E->getValueKind(), E->getCastKind(),
5680 SubExpr, &BasePath, TInfo,
5681 Importer.Import(CCE->getLParenLoc()),
5682 Importer.Import(CCE->getRParenLoc()));
5683 }
5684
5685 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005686 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005687 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5688 E->getValueKind(), TInfo,
5689 E->getCastKind(), SubExpr, &BasePath,
5690 Importer.Import(FCE->getLParenLoc()),
5691 Importer.Import(FCE->getRParenLoc()));
5692 }
5693
5694 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005695 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005696 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5697 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5698 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5699 TInfo, SubExpr);
5700 }
5701 default:
5702 break; // just fall through
5703 }
5704
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005705 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005706 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5707 RParenLoc = Importer.Import(Named->getRParenLoc());
5708 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5709
5710 switch (E->getStmtClass()) {
5711 case Stmt::CXXStaticCastExprClass:
5712 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5713 E->getValueKind(), E->getCastKind(),
5714 SubExpr, &BasePath, TInfo,
5715 ExprLoc, RParenLoc, Brackets);
5716
5717 case Stmt::CXXDynamicCastExprClass:
5718 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5719 E->getValueKind(), E->getCastKind(),
5720 SubExpr, &BasePath, TInfo,
5721 ExprLoc, RParenLoc, Brackets);
5722
5723 case Stmt::CXXReinterpretCastExprClass:
5724 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5725 E->getValueKind(), E->getCastKind(),
5726 SubExpr, &BasePath, TInfo,
5727 ExprLoc, RParenLoc, Brackets);
5728
5729 case Stmt::CXXConstCastExprClass:
5730 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5731 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5732 RParenLoc, Brackets);
5733 default:
5734 llvm_unreachable("Cast expression of unsupported type!");
5735 return nullptr;
5736 }
5737}
5738
5739Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5740 QualType T = Importer.Import(OE->getType());
5741 if (T.isNull())
5742 return nullptr;
5743
5744 SmallVector<OffsetOfNode, 4> Nodes;
5745 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5746 const OffsetOfNode &Node = OE->getComponent(I);
5747
5748 switch (Node.getKind()) {
5749 case OffsetOfNode::Array:
5750 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5751 Node.getArrayExprIndex(),
5752 Importer.Import(Node.getLocEnd())));
5753 break;
5754
5755 case OffsetOfNode::Base: {
5756 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5757 if (!BS && Node.getBase())
5758 return nullptr;
5759 Nodes.push_back(OffsetOfNode(BS));
5760 break;
5761 }
5762 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005763 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005764 if (!FD)
5765 return nullptr;
5766 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5767 Importer.Import(Node.getLocEnd())));
5768 break;
5769 }
5770 case OffsetOfNode::Identifier: {
5771 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5772 if (!ToII)
5773 return nullptr;
5774 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5775 Importer.Import(Node.getLocEnd())));
5776 break;
5777 }
5778 }
5779 }
5780
5781 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5782 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5783 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5784 if (!ToIndexExpr)
5785 return nullptr;
5786 Exprs[I] = ToIndexExpr;
5787 }
5788
5789 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5790 if (!TInfo && OE->getTypeSourceInfo())
5791 return nullptr;
5792
5793 return OffsetOfExpr::Create(Importer.getToContext(), T,
5794 Importer.Import(OE->getOperatorLoc()),
5795 TInfo, Nodes, Exprs,
5796 Importer.Import(OE->getRParenLoc()));
5797}
5798
5799Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5800 QualType T = Importer.Import(E->getType());
5801 if (T.isNull())
5802 return nullptr;
5803
5804 Expr *Operand = Importer.Import(E->getOperand());
5805 if (!Operand)
5806 return nullptr;
5807
5808 CanThrowResult CanThrow;
5809 if (E->isValueDependent())
5810 CanThrow = CT_Dependent;
5811 else
5812 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5813
5814 return new (Importer.getToContext()) CXXNoexceptExpr(
5815 T, Operand, CanThrow,
5816 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5817}
5818
5819Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5820 QualType T = Importer.Import(E->getType());
5821 if (T.isNull())
5822 return nullptr;
5823
5824 Expr *SubExpr = Importer.Import(E->getSubExpr());
5825 if (!SubExpr && E->getSubExpr())
5826 return nullptr;
5827
5828 return new (Importer.getToContext()) CXXThrowExpr(
5829 SubExpr, T, Importer.Import(E->getThrowLoc()),
5830 E->isThrownVariableInScope());
5831}
5832
5833Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005834 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005835 if (!Param)
5836 return nullptr;
5837
5838 return CXXDefaultArgExpr::Create(
5839 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5840}
5841
5842Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5843 QualType T = Importer.Import(E->getType());
5844 if (T.isNull())
5845 return nullptr;
5846
5847 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5848 if (!TypeInfo)
5849 return nullptr;
5850
5851 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5852 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5853}
5854
5855Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5856 Expr *SubExpr = Importer.Import(E->getSubExpr());
5857 if (!SubExpr)
5858 return nullptr;
5859
5860 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5861 Importer.Import(const_cast<CXXDestructorDecl *>(
5862 E->getTemporary()->getDestructor())));
5863 if (!Dtor)
5864 return nullptr;
5865
5866 ASTContext &ToCtx = Importer.getToContext();
5867 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5868 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5869}
5870
5871Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5872 QualType T = Importer.Import(CE->getType());
5873 if (T.isNull())
5874 return nullptr;
5875
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005876 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5877 if (!TInfo)
5878 return nullptr;
5879
Aleksei Sidorina693b372016-09-28 10:16:56 +00005880 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5881 if (ImportContainerChecked(CE->arguments(), Args))
5882 return nullptr;
5883
5884 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5885 Importer.Import(CE->getConstructor()));
5886 if (!Ctor)
5887 return nullptr;
5888
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005889 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5890 Importer.getToContext(), Ctor, T, TInfo, Args,
5891 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5892 CE->isListInitialization(), CE->isStdInitListInitialization(),
5893 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005894}
5895
5896Expr *
5897ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5898 QualType T = Importer.Import(E->getType());
5899 if (T.isNull())
5900 return nullptr;
5901
5902 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5903 if (!TempE)
5904 return nullptr;
5905
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005906 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005907 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5908 if (!ExtendedBy && E->getExtendingDecl())
5909 return nullptr;
5910
5911 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5912 T, TempE, E->isBoundToLvalueReference());
5913
5914 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5915 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5916 return ToMTE;
5917}
5918
Gabor Horvath7a91c082017-11-14 11:30:38 +00005919Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5920 QualType T = Importer.Import(E->getType());
5921 if (T.isNull())
5922 return nullptr;
5923
5924 Expr *Pattern = Importer.Import(E->getPattern());
5925 if (!Pattern)
5926 return nullptr;
5927
5928 return new (Importer.getToContext()) PackExpansionExpr(
5929 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5930 E->getNumExpansions());
5931}
5932
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005933Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5934 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5935 if (!Pack)
5936 return nullptr;
5937
5938 Optional<unsigned> Length;
5939
5940 if (!E->isValueDependent())
5941 Length = E->getPackLength();
5942
5943 SmallVector<TemplateArgument, 8> PartialArguments;
5944 if (E->isPartiallySubstituted()) {
5945 if (ImportTemplateArguments(E->getPartialArguments().data(),
5946 E->getPartialArguments().size(),
5947 PartialArguments))
5948 return nullptr;
5949 }
5950
5951 return SizeOfPackExpr::Create(
5952 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5953 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5954 Length, PartialArguments);
5955}
5956
Aleksei Sidorina693b372016-09-28 10:16:56 +00005957Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5958 QualType T = Importer.Import(CE->getType());
5959 if (T.isNull())
5960 return nullptr;
5961
5962 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5963 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5964 return nullptr;
5965
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005966 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005967 Importer.Import(CE->getOperatorNew()));
5968 if (!OperatorNewDecl && CE->getOperatorNew())
5969 return nullptr;
5970
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005971 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005972 Importer.Import(CE->getOperatorDelete()));
5973 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5974 return nullptr;
5975
5976 Expr *ToInit = Importer.Import(CE->getInitializer());
5977 if (!ToInit && CE->getInitializer())
5978 return nullptr;
5979
5980 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5981 if (!TInfo)
5982 return nullptr;
5983
5984 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5985 if (!ToArrSize && CE->getArraySize())
5986 return nullptr;
5987
5988 return new (Importer.getToContext()) CXXNewExpr(
5989 Importer.getToContext(),
5990 CE->isGlobalNew(),
5991 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005992 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005993 CE->doesUsualArrayDeleteWantSize(),
5994 PlacementArgs,
5995 Importer.Import(CE->getTypeIdParens()),
5996 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5997 Importer.Import(CE->getSourceRange()),
5998 Importer.Import(CE->getDirectInitRange()));
5999}
6000
6001Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6002 QualType T = Importer.Import(E->getType());
6003 if (T.isNull())
6004 return nullptr;
6005
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006006 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006007 Importer.Import(E->getOperatorDelete()));
6008 if (!OperatorDeleteDecl && E->getOperatorDelete())
6009 return nullptr;
6010
6011 Expr *ToArg = Importer.Import(E->getArgument());
6012 if (!ToArg && E->getArgument())
6013 return nullptr;
6014
6015 return new (Importer.getToContext()) CXXDeleteExpr(
6016 T, E->isGlobalDelete(),
6017 E->isArrayForm(),
6018 E->isArrayFormAsWritten(),
6019 E->doesUsualArrayDeleteWantSize(),
6020 OperatorDeleteDecl,
6021 ToArg,
6022 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006023}
6024
Sean Callanan59721b32015-04-28 18:41:46 +00006025Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6026 QualType T = Importer.Import(E->getType());
6027 if (T.isNull())
6028 return nullptr;
6029
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006030 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006031 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006032 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006033 return nullptr;
6034
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006035 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006036 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006037 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006038
6039 return CXXConstructExpr::Create(Importer.getToContext(), T,
6040 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006041 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006042 ToArgs, E->hadMultipleCandidates(),
6043 E->isListInitialization(),
6044 E->isStdInitListInitialization(),
6045 E->requiresZeroInitialization(),
6046 E->getConstructionKind(),
6047 Importer.Import(E->getParenOrBraceRange()));
6048}
6049
Aleksei Sidorina693b372016-09-28 10:16:56 +00006050Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6051 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6052 if (!SubExpr && EWC->getSubExpr())
6053 return nullptr;
6054
6055 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6056 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6057 if (ExprWithCleanups::CleanupObject Obj =
6058 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6059 Objs[I] = Obj;
6060 else
6061 return nullptr;
6062
6063 return ExprWithCleanups::Create(Importer.getToContext(),
6064 SubExpr, EWC->cleanupsHaveSideEffects(),
6065 Objs);
6066}
6067
Sean Callanan8bca9962016-03-28 21:43:01 +00006068Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6069 QualType T = Importer.Import(E->getType());
6070 if (T.isNull())
6071 return nullptr;
6072
6073 Expr *ToFn = Importer.Import(E->getCallee());
6074 if (!ToFn)
6075 return nullptr;
6076
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006077 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006078 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006079 return nullptr;
6080
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006081 return new (Importer.getToContext()) CXXMemberCallExpr(
6082 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6083 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006084}
6085
6086Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6087 QualType T = Importer.Import(E->getType());
6088 if (T.isNull())
6089 return nullptr;
6090
6091 return new (Importer.getToContext())
6092 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6093}
6094
6095Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6096 QualType T = Importer.Import(E->getType());
6097 if (T.isNull())
6098 return nullptr;
6099
6100 return new (Importer.getToContext())
6101 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6102}
6103
6104
Sean Callanan59721b32015-04-28 18:41:46 +00006105Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6106 QualType T = Importer.Import(E->getType());
6107 if (T.isNull())
6108 return nullptr;
6109
6110 Expr *ToBase = Importer.Import(E->getBase());
6111 if (!ToBase && E->getBase())
6112 return nullptr;
6113
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006114 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006115 if (!ToMember && E->getMemberDecl())
6116 return nullptr;
6117
Peter Szecsief972522018-05-02 11:52:54 +00006118 auto *ToDecl =
6119 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6120 if (!ToDecl && E->getFoundDecl().getDecl())
6121 return nullptr;
6122
6123 DeclAccessPair ToFoundDecl =
6124 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006125
6126 DeclarationNameInfo ToMemberNameInfo(
6127 Importer.Import(E->getMemberNameInfo().getName()),
6128 Importer.Import(E->getMemberNameInfo().getLoc()));
6129
6130 if (E->hasExplicitTemplateArgs()) {
6131 return nullptr; // FIXME: handle template arguments
6132 }
6133
6134 return MemberExpr::Create(Importer.getToContext(), ToBase,
6135 E->isArrow(),
6136 Importer.Import(E->getOperatorLoc()),
6137 Importer.Import(E->getQualifierLoc()),
6138 Importer.Import(E->getTemplateKeywordLoc()),
6139 ToMember, ToFoundDecl, ToMemberNameInfo,
6140 nullptr, T, E->getValueKind(),
6141 E->getObjectKind());
6142}
6143
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006144Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6145 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006146 Expr *BaseE = Importer.Import(E->getBase());
6147 if (!BaseE)
6148 return nullptr;
6149
6150 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6151 if (!ScopeInfo && E->getScopeTypeInfo())
6152 return nullptr;
6153
6154 PseudoDestructorTypeStorage Storage;
6155 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6156 IdentifierInfo *ToII = Importer.Import(FromII);
6157 if (!ToII)
6158 return nullptr;
6159 Storage = PseudoDestructorTypeStorage(
6160 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6161 } else {
6162 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6163 if (!TI)
6164 return nullptr;
6165 Storage = PseudoDestructorTypeStorage(TI);
6166 }
6167
6168 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6169 Importer.getToContext(), BaseE, E->isArrow(),
6170 Importer.Import(E->getOperatorLoc()),
6171 Importer.Import(E->getQualifierLoc()),
6172 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6173 Importer.Import(E->getTildeLoc()), Storage);
6174}
6175
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006176Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6177 CXXDependentScopeMemberExpr *E) {
6178 Expr *Base = nullptr;
6179 if (!E->isImplicitAccess()) {
6180 Base = Importer.Import(E->getBase());
6181 if (!Base)
6182 return nullptr;
6183 }
6184
6185 QualType BaseType = Importer.Import(E->getBaseType());
6186 if (BaseType.isNull())
6187 return nullptr;
6188
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006189 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006190 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006191 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6192 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006193 return nullptr;
6194 ResInfo = &ToTAInfo;
6195 }
6196
6197 DeclarationName Name = Importer.Import(E->getMember());
6198 if (!E->getMember().isEmpty() && Name.isEmpty())
6199 return nullptr;
6200
6201 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6202 // Import additional name location/type info.
6203 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6204 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6205 if (!ToFQ && E->getFirstQualifierFoundInScope())
6206 return nullptr;
6207
6208 return CXXDependentScopeMemberExpr::Create(
6209 Importer.getToContext(), Base, BaseType, E->isArrow(),
6210 Importer.Import(E->getOperatorLoc()),
6211 Importer.Import(E->getQualifierLoc()),
6212 Importer.Import(E->getTemplateKeywordLoc()),
6213 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6214}
6215
Peter Szecsice7f3182018-05-07 12:08:27 +00006216Expr *
6217ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6218 DeclarationName Name = Importer.Import(E->getDeclName());
6219 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6220 return nullptr;
6221
6222 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6223 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6224
6225 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6226 Importer.Import(E->getRAngleLoc()));
6227 TemplateArgumentListInfo *ResInfo = nullptr;
6228 if (E->hasExplicitTemplateArgs()) {
6229 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6230 return nullptr;
6231 ResInfo = &ToTAInfo;
6232 }
6233
6234 return DependentScopeDeclRefExpr::Create(
6235 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6236 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6237}
6238
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006239Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6240 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006241 unsigned NumArgs = CE->arg_size();
6242
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006243 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006244 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6245 return nullptr;
6246
6247 return CXXUnresolvedConstructExpr::Create(
6248 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6249 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6250 Importer.Import(CE->getRParenLoc()));
6251}
6252
6253Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006254 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006255 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6256 if (E->getNamingClass() && !NamingClass)
6257 return nullptr;
6258
6259 DeclarationName Name = Importer.Import(E->getName());
6260 if (E->getName() && !Name)
6261 return nullptr;
6262
6263 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6264 // Import additional name location/type info.
6265 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6266
6267 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006268 for (auto *D : E->decls()) {
6269 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006270 ToDecls.addDecl(To);
6271 else
6272 return nullptr;
6273 }
6274
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006275 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006276 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006277 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6278 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006279 return nullptr;
6280 ResInfo = &ToTAInfo;
6281 }
6282
6283 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6284 return UnresolvedLookupExpr::Create(
6285 Importer.getToContext(), NamingClass,
6286 Importer.Import(E->getQualifierLoc()),
6287 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6288 ResInfo, ToDecls.begin(), ToDecls.end());
6289
6290 return UnresolvedLookupExpr::Create(
6291 Importer.getToContext(), NamingClass,
6292 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6293 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6294}
6295
Peter Szecsice7f3182018-05-07 12:08:27 +00006296Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6297 DeclarationName Name = Importer.Import(E->getName());
6298 if (!E->getName().isEmpty() && Name.isEmpty())
6299 return nullptr;
6300 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6301 // Import additional name location/type info.
6302 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6303
6304 QualType BaseType = Importer.Import(E->getType());
6305 if (!E->getType().isNull() && BaseType.isNull())
6306 return nullptr;
6307
6308 UnresolvedSet<8> ToDecls;
6309 for (Decl *D : E->decls()) {
6310 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6311 ToDecls.addDecl(To);
6312 else
6313 return nullptr;
6314 }
6315
6316 TemplateArgumentListInfo ToTAInfo;
6317 TemplateArgumentListInfo *ResInfo = nullptr;
6318 if (E->hasExplicitTemplateArgs()) {
6319 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6320 return nullptr;
6321 ResInfo = &ToTAInfo;
6322 }
6323
6324 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6325 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6326 return nullptr;
6327 }
6328
6329 return UnresolvedMemberExpr::Create(
6330 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6331 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6332 Importer.Import(E->getQualifierLoc()),
6333 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6334 ToDecls.begin(), ToDecls.end());
6335}
6336
Sean Callanan59721b32015-04-28 18:41:46 +00006337Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6338 QualType T = Importer.Import(E->getType());
6339 if (T.isNull())
6340 return nullptr;
6341
6342 Expr *ToCallee = Importer.Import(E->getCallee());
6343 if (!ToCallee && E->getCallee())
6344 return nullptr;
6345
6346 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006347 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006348 if (ImportContainerChecked(E->arguments(), ToArgs))
6349 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006350
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006351 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006352
6353 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6354 ToArgs_Copied[ai] = ToArgs[ai];
6355
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006356 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6357 return new (Importer.getToContext()) CXXOperatorCallExpr(
6358 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6359 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6360 OCE->getFPFeatures());
6361 }
6362
Sean Callanan59721b32015-04-28 18:41:46 +00006363 return new (Importer.getToContext())
6364 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006365 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006366 Importer.Import(E->getRParenLoc()));
6367}
6368
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006369Optional<LambdaCapture>
6370ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6371 VarDecl *Var = nullptr;
6372 if (From.capturesVariable()) {
6373 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6374 if (!Var)
6375 return None;
6376 }
6377
6378 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6379 From.getCaptureKind(), Var,
6380 From.isPackExpansion()
6381 ? Importer.Import(From.getEllipsisLoc())
6382 : SourceLocation());
6383}
6384
6385Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6386 CXXRecordDecl *FromClass = LE->getLambdaClass();
6387 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6388 if (!ToClass)
6389 return nullptr;
6390
6391 // NOTE: lambda classes are created with BeingDefined flag set up.
6392 // It means that ImportDefinition doesn't work for them and we should fill it
6393 // manually.
6394 if (ToClass->isBeingDefined()) {
6395 for (auto FromField : FromClass->fields()) {
6396 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6397 if (!ToField)
6398 return nullptr;
6399 }
6400 }
6401
6402 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6403 Importer.Import(LE->getCallOperator()));
6404 if (!ToCallOp)
6405 return nullptr;
6406
6407 ToClass->completeDefinition();
6408
6409 unsigned NumCaptures = LE->capture_size();
6410 SmallVector<LambdaCapture, 8> Captures;
6411 Captures.reserve(NumCaptures);
6412 for (const auto &FromCapture : LE->captures()) {
6413 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6414 Captures.push_back(*ToCapture);
6415 else
6416 return nullptr;
6417 }
6418
6419 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6420 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6421 return nullptr;
6422
6423 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6424 Importer.Import(LE->getIntroducerRange()),
6425 LE->getCaptureDefault(),
6426 Importer.Import(LE->getCaptureDefaultLoc()),
6427 Captures,
6428 LE->hasExplicitParameters(),
6429 LE->hasExplicitResultType(),
6430 InitCaptures,
6431 Importer.Import(LE->getLocEnd()),
6432 LE->containsUnexpandedParameterPack());
6433}
6434
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006435Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6436 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006437 if (T.isNull())
6438 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006439
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006440 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006441 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006442 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006443
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006444 ASTContext &ToCtx = Importer.getToContext();
6445 InitListExpr *To = new (ToCtx) InitListExpr(
6446 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6447 Exprs, Importer.Import(ILE->getLBraceLoc()));
6448 To->setType(T);
6449
6450 if (ILE->hasArrayFiller()) {
6451 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6452 if (!Filler)
6453 return nullptr;
6454 To->setArrayFiller(Filler);
6455 }
6456
6457 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006458 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006459 if (!ToFD)
6460 return nullptr;
6461 To->setInitializedFieldInUnion(ToFD);
6462 }
6463
6464 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006465 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006466 if (!ToSyntForm)
6467 return nullptr;
6468 To->setSyntacticForm(ToSyntForm);
6469 }
6470
6471 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6472 To->setValueDependent(ILE->isValueDependent());
6473 To->setInstantiationDependent(ILE->isInstantiationDependent());
6474
6475 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006476}
6477
Richard Smith30e304e2016-12-14 00:03:17 +00006478Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6479 QualType ToType = Importer.Import(E->getType());
6480 if (ToType.isNull())
6481 return nullptr;
6482
6483 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6484 if (!ToCommon && E->getCommonExpr())
6485 return nullptr;
6486
6487 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6488 if (!ToSubExpr && E->getSubExpr())
6489 return nullptr;
6490
6491 return new (Importer.getToContext())
6492 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6493}
6494
6495Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6496 QualType ToType = Importer.Import(E->getType());
6497 if (ToType.isNull())
6498 return nullptr;
6499 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6500}
6501
Sean Callanandd2c1742016-05-16 20:48:03 +00006502Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006503 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006504 if (!ToField && DIE->getField())
6505 return nullptr;
6506
6507 return CXXDefaultInitExpr::Create(
6508 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6509}
6510
6511Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6512 QualType ToType = Importer.Import(E->getType());
6513 if (ToType.isNull() && !E->getType().isNull())
6514 return nullptr;
6515 ExprValueKind VK = E->getValueKind();
6516 CastKind CK = E->getCastKind();
6517 Expr *ToOp = Importer.Import(E->getSubExpr());
6518 if (!ToOp && E->getSubExpr())
6519 return nullptr;
6520 CXXCastPath BasePath;
6521 if (ImportCastPath(E, BasePath))
6522 return nullptr;
6523 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6524 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6525 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6526 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6527
6528 if (isa<CXXStaticCastExpr>(E)) {
6529 return CXXStaticCastExpr::Create(
6530 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6531 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6532 } else if (isa<CXXDynamicCastExpr>(E)) {
6533 return CXXDynamicCastExpr::Create(
6534 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6535 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6536 } else if (isa<CXXReinterpretCastExpr>(E)) {
6537 return CXXReinterpretCastExpr::Create(
6538 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6539 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6540 } else {
6541 return nullptr;
6542 }
6543}
6544
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006545Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6546 SubstNonTypeTemplateParmExpr *E) {
6547 QualType T = Importer.Import(E->getType());
6548 if (T.isNull())
6549 return nullptr;
6550
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006551 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006552 Importer.Import(E->getParameter()));
6553 if (!Param)
6554 return nullptr;
6555
6556 Expr *Replacement = Importer.Import(E->getReplacement());
6557 if (!Replacement)
6558 return nullptr;
6559
6560 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6561 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6562 Replacement);
6563}
6564
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006565Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6566 QualType ToType = Importer.Import(E->getType());
6567 if (ToType.isNull())
6568 return nullptr;
6569
6570 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6571 if (ImportContainerChecked(E->getArgs(), ToArgs))
6572 return nullptr;
6573
6574 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6575 // Value is always false.
6576 bool ToValue = false;
6577 if (!E->isValueDependent())
6578 ToValue = E->getValue();
6579
6580 return TypeTraitExpr::Create(
6581 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6582 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6583}
6584
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006585Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6586 QualType ToType = Importer.Import(E->getType());
6587 if (ToType.isNull())
6588 return nullptr;
6589
6590 if (E->isTypeOperand()) {
6591 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6592 if (!TSI)
6593 return nullptr;
6594
6595 return new (Importer.getToContext())
6596 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6597 }
6598
6599 Expr *Op = Importer.Import(E->getExprOperand());
6600 if (!Op)
6601 return nullptr;
6602
6603 return new (Importer.getToContext())
6604 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6605}
6606
Lang Hames19e07e12017-06-20 21:06:00 +00006607void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6608 CXXMethodDecl *FromMethod) {
6609 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6610 ToMethod->addOverriddenMethod(
6611 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6612 FromOverriddenMethod))));
6613}
6614
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006615ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006616 ASTContext &FromContext, FileManager &FromFileManager,
6617 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006618 : ToContext(ToContext), FromContext(FromContext),
6619 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6620 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006621 ImportedDecls[FromContext.getTranslationUnitDecl()]
6622 = ToContext.getTranslationUnitDecl();
6623}
6624
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006625ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006626
6627QualType ASTImporter::Import(QualType FromT) {
6628 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006629 return {};
John McCall424cec92011-01-19 06:33:43 +00006630
6631 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006632
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006633 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006634 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6635 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006636 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006637 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006638
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006639 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006640 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006641 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006642 if (ToT.isNull())
6643 return ToT;
6644
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006645 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006646 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006647
John McCall424cec92011-01-19 06:33:43 +00006648 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006649}
6650
Douglas Gregor62d311f2010-02-09 19:21:46 +00006651TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006652 if (!FromTSI)
6653 return FromTSI;
6654
6655 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006656 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006657 QualType T = Import(FromTSI->getType());
6658 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006659 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006660
6661 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006662 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006663}
6664
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006665Attr *ASTImporter::Import(const Attr *FromAttr) {
6666 Attr *ToAttr = FromAttr->clone(ToContext);
6667 ToAttr->setRange(Import(FromAttr->getRange()));
6668 return ToAttr;
6669}
6670
Sean Callanan59721b32015-04-28 18:41:46 +00006671Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6672 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6673 if (Pos != ImportedDecls.end()) {
6674 Decl *ToD = Pos->second;
6675 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6676 return ToD;
6677 } else {
6678 return nullptr;
6679 }
6680}
6681
Douglas Gregor62d311f2010-02-09 19:21:46 +00006682Decl *ASTImporter::Import(Decl *FromD) {
6683 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006684 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006685
Douglas Gregord451ea92011-07-29 23:31:30 +00006686 ASTNodeImporter Importer(*this);
6687
Douglas Gregor62d311f2010-02-09 19:21:46 +00006688 // Check whether we've already imported this declaration.
6689 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006690 if (Pos != ImportedDecls.end()) {
6691 Decl *ToD = Pos->second;
6692 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6693 return ToD;
6694 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006695
6696 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006697 Decl *ToD = Importer.Visit(FromD);
6698 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006699 return nullptr;
6700
Douglas Gregor62d311f2010-02-09 19:21:46 +00006701 // Record the imported declaration.
6702 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006703 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006704 return ToD;
6705}
6706
6707DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6708 if (!FromDC)
6709 return FromDC;
6710
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006711 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006712 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006713 return nullptr;
6714
Douglas Gregor2e15c842012-02-01 21:00:38 +00006715 // When we're using a record/enum/Objective-C class/protocol as a context, we
6716 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006717 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6718 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006719 if (ToRecord->isCompleteDefinition()) {
6720 // Do nothing.
6721 } else if (FromRecord->isCompleteDefinition()) {
6722 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6723 ASTNodeImporter::IDK_Basic);
6724 } else {
6725 CompleteDecl(ToRecord);
6726 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006727 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6728 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006729 if (ToEnum->isCompleteDefinition()) {
6730 // Do nothing.
6731 } else if (FromEnum->isCompleteDefinition()) {
6732 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6733 ASTNodeImporter::IDK_Basic);
6734 } else {
6735 CompleteDecl(ToEnum);
6736 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006737 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6738 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006739 if (ToClass->getDefinition()) {
6740 // Do nothing.
6741 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6742 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6743 ASTNodeImporter::IDK_Basic);
6744 } else {
6745 CompleteDecl(ToClass);
6746 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006747 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6748 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006749 if (ToProto->getDefinition()) {
6750 // Do nothing.
6751 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6752 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6753 ASTNodeImporter::IDK_Basic);
6754 } else {
6755 CompleteDecl(ToProto);
6756 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006757 }
6758
6759 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006760}
6761
6762Expr *ASTImporter::Import(Expr *FromE) {
6763 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006764 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006765
6766 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6767}
6768
6769Stmt *ASTImporter::Import(Stmt *FromS) {
6770 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006771 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006772
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006773 // Check whether we've already imported this declaration.
6774 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6775 if (Pos != ImportedStmts.end())
6776 return Pos->second;
6777
6778 // Import the type
6779 ASTNodeImporter Importer(*this);
6780 Stmt *ToS = Importer.Visit(FromS);
6781 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006782 return nullptr;
6783
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006784 // Record the imported declaration.
6785 ImportedStmts[FromS] = ToS;
6786 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006787}
6788
6789NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6790 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006791 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006792
Douglas Gregor90ebf252011-04-27 16:48:40 +00006793 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6794
6795 switch (FromNNS->getKind()) {
6796 case NestedNameSpecifier::Identifier:
6797 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6798 return NestedNameSpecifier::Create(ToContext, prefix, II);
6799 }
Craig Topper36250ad2014-05-12 05:36:57 +00006800 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006801
6802 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006803 if (auto *NS =
6804 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006805 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6806 }
Craig Topper36250ad2014-05-12 05:36:57 +00006807 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006808
6809 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006810 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006811 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006812 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6813 }
Craig Topper36250ad2014-05-12 05:36:57 +00006814 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006815
6816 case NestedNameSpecifier::Global:
6817 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6818
Nikola Smiljanic67860242014-09-26 00:28:20 +00006819 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006820 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006821 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006822 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6823 }
6824 return nullptr;
6825
Douglas Gregor90ebf252011-04-27 16:48:40 +00006826 case NestedNameSpecifier::TypeSpec:
6827 case NestedNameSpecifier::TypeSpecWithTemplate: {
6828 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6829 if (!T.isNull()) {
6830 bool bTemplate = FromNNS->getKind() ==
6831 NestedNameSpecifier::TypeSpecWithTemplate;
6832 return NestedNameSpecifier::Create(ToContext, prefix,
6833 bTemplate, T.getTypePtr());
6834 }
6835 }
Craig Topper36250ad2014-05-12 05:36:57 +00006836 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006837 }
6838
6839 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006840}
6841
Douglas Gregor14454802011-02-25 02:25:35 +00006842NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006843 // Copied from NestedNameSpecifier mostly.
6844 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6845 NestedNameSpecifierLoc NNS = FromNNS;
6846
6847 // Push each of the nested-name-specifiers's onto a stack for
6848 // serialization in reverse order.
6849 while (NNS) {
6850 NestedNames.push_back(NNS);
6851 NNS = NNS.getPrefix();
6852 }
6853
6854 NestedNameSpecifierLocBuilder Builder;
6855
6856 while (!NestedNames.empty()) {
6857 NNS = NestedNames.pop_back_val();
6858 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6859 if (!Spec)
6860 return NestedNameSpecifierLoc();
6861
6862 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6863 switch (Kind) {
6864 case NestedNameSpecifier::Identifier:
6865 Builder.Extend(getToContext(),
6866 Spec->getAsIdentifier(),
6867 Import(NNS.getLocalBeginLoc()),
6868 Import(NNS.getLocalEndLoc()));
6869 break;
6870
6871 case NestedNameSpecifier::Namespace:
6872 Builder.Extend(getToContext(),
6873 Spec->getAsNamespace(),
6874 Import(NNS.getLocalBeginLoc()),
6875 Import(NNS.getLocalEndLoc()));
6876 break;
6877
6878 case NestedNameSpecifier::NamespaceAlias:
6879 Builder.Extend(getToContext(),
6880 Spec->getAsNamespaceAlias(),
6881 Import(NNS.getLocalBeginLoc()),
6882 Import(NNS.getLocalEndLoc()));
6883 break;
6884
6885 case NestedNameSpecifier::TypeSpec:
6886 case NestedNameSpecifier::TypeSpecWithTemplate: {
6887 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6888 QualType(Spec->getAsType(), 0));
6889 Builder.Extend(getToContext(),
6890 Import(NNS.getLocalBeginLoc()),
6891 TSI->getTypeLoc(),
6892 Import(NNS.getLocalEndLoc()));
6893 break;
6894 }
6895
6896 case NestedNameSpecifier::Global:
6897 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6898 break;
6899
6900 case NestedNameSpecifier::Super: {
6901 SourceRange ToRange = Import(NNS.getSourceRange());
6902 Builder.MakeSuper(getToContext(),
6903 Spec->getAsRecordDecl(),
6904 ToRange.getBegin(),
6905 ToRange.getEnd());
6906 }
6907 }
6908 }
6909
6910 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006911}
6912
Douglas Gregore2e50d332010-12-01 01:36:18 +00006913TemplateName ASTImporter::Import(TemplateName From) {
6914 switch (From.getKind()) {
6915 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006916 if (auto *ToTemplate =
6917 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006918 return TemplateName(ToTemplate);
6919
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006920 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006921
6922 case TemplateName::OverloadedTemplate: {
6923 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6924 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006925 for (auto *I : *FromStorage) {
6926 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006927 ToTemplates.addDecl(To);
6928 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006929 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006930 }
6931 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6932 ToTemplates.end());
6933 }
6934
6935 case TemplateName::QualifiedTemplate: {
6936 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6937 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6938 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006939 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006940
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006941 if (auto *ToTemplate =
6942 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006943 return ToContext.getQualifiedTemplateName(Qualifier,
6944 QTN->hasTemplateKeyword(),
6945 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006946
6947 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006948 }
6949
6950 case TemplateName::DependentTemplate: {
6951 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6952 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6953 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006954 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006955
6956 if (DTN->isIdentifier()) {
6957 return ToContext.getDependentTemplateName(Qualifier,
6958 Import(DTN->getIdentifier()));
6959 }
6960
6961 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6962 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006963
6964 case TemplateName::SubstTemplateTemplateParm: {
6965 SubstTemplateTemplateParmStorage *subst
6966 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006967 auto *param =
6968 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006969 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006970 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006971
6972 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006973 if (replacement.isNull())
6974 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006975
6976 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6977 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006978
6979 case TemplateName::SubstTemplateTemplateParmPack: {
6980 SubstTemplateTemplateParmPackStorage *SubstPack
6981 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006982 auto *Param =
6983 cast_or_null<TemplateTemplateParmDecl>(
6984 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00006985 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006986 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006987
6988 ASTNodeImporter Importer(*this);
6989 TemplateArgument ArgPack
6990 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6991 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006992 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00006993
6994 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6995 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006996 }
6997
6998 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006999}
7000
Douglas Gregor62d311f2010-02-09 19:21:46 +00007001SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7002 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007003 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007004
Douglas Gregor811663e2010-02-10 00:15:17 +00007005 SourceManager &FromSM = FromContext.getSourceManager();
7006
Sean Callanan24c5fe62016-11-07 20:42:25 +00007007 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007008 // don't have to import macro expansions.
7009 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007010 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007011 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7012 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007013 FileID ToFileID = Import(Decomposed.first);
7014 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007015 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007016 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7017 .getLocWithOffset(Decomposed.second);
7018 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007019}
7020
7021SourceRange ASTImporter::Import(SourceRange FromRange) {
7022 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7023}
7024
Douglas Gregor811663e2010-02-10 00:15:17 +00007025FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007026 llvm::DenseMap<FileID, FileID>::iterator Pos
7027 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007028 if (Pos != ImportedFileIDs.end())
7029 return Pos->second;
7030
7031 SourceManager &FromSM = FromContext.getSourceManager();
7032 SourceManager &ToSM = ToContext.getSourceManager();
7033 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007034 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007035
7036 // Include location of this file.
7037 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7038
7039 // Map the FileID for to the "to" source manager.
7040 FileID ToID;
7041 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007042 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007043 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7044 // disk again
7045 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7046 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007047 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007048 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007049 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007050 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7051 FromSLoc.getFile().getFileCharacteristic());
7052 } else {
7053 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007054 const llvm::MemoryBuffer *
7055 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007056 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007057 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007058 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007059 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007060 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007061 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007062
Sebastian Redl99219f12010-09-30 01:03:06 +00007063 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007064 return ToID;
7065}
7066
Sean Callanandd2c1742016-05-16 20:48:03 +00007067CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7068 Expr *ToExpr = Import(From->getInit());
7069 if (!ToExpr && From->getInit())
7070 return nullptr;
7071
7072 if (From->isBaseInitializer()) {
7073 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7074 if (!ToTInfo && From->getTypeSourceInfo())
7075 return nullptr;
7076
7077 return new (ToContext) CXXCtorInitializer(
7078 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7079 ToExpr, Import(From->getRParenLoc()),
7080 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7081 : SourceLocation());
7082 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007083 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007084 if (!ToField && From->getMember())
7085 return nullptr;
7086
7087 return new (ToContext) CXXCtorInitializer(
7088 ToContext, ToField, Import(From->getMemberLocation()),
7089 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7090 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007091 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007092 Import(From->getIndirectMember()));
7093 if (!ToIField && From->getIndirectMember())
7094 return nullptr;
7095
7096 return new (ToContext) CXXCtorInitializer(
7097 ToContext, ToIField, Import(From->getMemberLocation()),
7098 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7099 } else if (From->isDelegatingInitializer()) {
7100 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7101 if (!ToTInfo && From->getTypeSourceInfo())
7102 return nullptr;
7103
7104 return new (ToContext)
7105 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7106 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007107 } else {
7108 return nullptr;
7109 }
7110}
7111
Aleksei Sidorina693b372016-09-28 10:16:56 +00007112CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7113 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7114 if (Pos != ImportedCXXBaseSpecifiers.end())
7115 return Pos->second;
7116
7117 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7118 Import(BaseSpec->getSourceRange()),
7119 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7120 BaseSpec->getAccessSpecifierAsWritten(),
7121 Import(BaseSpec->getTypeSourceInfo()),
7122 Import(BaseSpec->getEllipsisLoc()));
7123 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7124 return Imported;
7125}
7126
Douglas Gregor0a791672011-01-18 03:11:38 +00007127void ASTImporter::ImportDefinition(Decl *From) {
7128 Decl *To = Import(From);
7129 if (!To)
7130 return;
7131
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007132 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007133 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007134
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007135 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007136 if (!ToRecord->getDefinition()) {
7137 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007138 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007139 return;
7140 }
7141 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007142
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007143 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007144 if (!ToEnum->getDefinition()) {
7145 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007146 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007147 return;
7148 }
7149 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007150
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007151 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007152 if (!ToIFace->getDefinition()) {
7153 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007154 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007155 return;
7156 }
7157 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007158
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007159 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007160 if (!ToProto->getDefinition()) {
7161 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007162 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007163 return;
7164 }
7165 }
7166
Douglas Gregor0a791672011-01-18 03:11:38 +00007167 Importer.ImportDeclContext(FromDC, true);
7168 }
7169}
7170
Douglas Gregor96e578d2010-02-05 17:54:41 +00007171DeclarationName ASTImporter::Import(DeclarationName FromName) {
7172 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007173 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007174
7175 switch (FromName.getNameKind()) {
7176 case DeclarationName::Identifier:
7177 return Import(FromName.getAsIdentifierInfo());
7178
7179 case DeclarationName::ObjCZeroArgSelector:
7180 case DeclarationName::ObjCOneArgSelector:
7181 case DeclarationName::ObjCMultiArgSelector:
7182 return Import(FromName.getObjCSelector());
7183
7184 case DeclarationName::CXXConstructorName: {
7185 QualType T = Import(FromName.getCXXNameType());
7186 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007187 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007188
7189 return ToContext.DeclarationNames.getCXXConstructorName(
7190 ToContext.getCanonicalType(T));
7191 }
7192
7193 case DeclarationName::CXXDestructorName: {
7194 QualType T = Import(FromName.getCXXNameType());
7195 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007196 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007197
7198 return ToContext.DeclarationNames.getCXXDestructorName(
7199 ToContext.getCanonicalType(T));
7200 }
7201
Richard Smith35845152017-02-07 01:37:30 +00007202 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007203 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007204 Import(FromName.getCXXDeductionGuideTemplate()));
7205 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007206 return {};
Richard Smith35845152017-02-07 01:37:30 +00007207 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7208 }
7209
Douglas Gregor96e578d2010-02-05 17:54:41 +00007210 case DeclarationName::CXXConversionFunctionName: {
7211 QualType T = Import(FromName.getCXXNameType());
7212 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007213 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007214
7215 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7216 ToContext.getCanonicalType(T));
7217 }
7218
7219 case DeclarationName::CXXOperatorName:
7220 return ToContext.DeclarationNames.getCXXOperatorName(
7221 FromName.getCXXOverloadedOperator());
7222
7223 case DeclarationName::CXXLiteralOperatorName:
7224 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7225 Import(FromName.getCXXLiteralIdentifier()));
7226
7227 case DeclarationName::CXXUsingDirective:
7228 // FIXME: STATICS!
7229 return DeclarationName::getUsingDirectiveName();
7230 }
7231
David Blaikiee4d798f2012-01-20 21:50:17 +00007232 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007233}
7234
Douglas Gregore2e50d332010-12-01 01:36:18 +00007235IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007236 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007237 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007238
Sean Callananf94ef1d2016-05-14 06:11:19 +00007239 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7240
7241 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7242 ToId->setBuiltinID(FromId->getBuiltinID());
7243
7244 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007245}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007246
Douglas Gregor43f54792010-02-17 02:12:47 +00007247Selector ASTImporter::Import(Selector FromSel) {
7248 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007249 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007250
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007251 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007252 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7253 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7254 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7255 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7256}
7257
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007258DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7259 DeclContext *DC,
7260 unsigned IDNS,
7261 NamedDecl **Decls,
7262 unsigned NumDecls) {
7263 return Name;
7264}
7265
7266DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007267 if (LastDiagFromFrom)
7268 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7269 FromContext.getDiagnostics());
7270 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007271 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007272}
7273
7274DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007275 if (!LastDiagFromFrom)
7276 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7277 ToContext.getDiagnostics());
7278 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007279 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007280}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007281
Douglas Gregor2e15c842012-02-01 21:00:38 +00007282void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007283 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007284 if (!ID->getDefinition())
7285 ID->startDefinition();
7286 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007287 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007288 if (!PD->getDefinition())
7289 PD->startDefinition();
7290 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007291 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007292 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7293 TD->startDefinition();
7294 TD->setCompleteDefinition(true);
7295 }
7296 }
7297 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007298 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007299 }
7300}
7301
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007302Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007303 if (From->hasAttrs()) {
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007304 for (const auto *FromAttr : From->getAttrs())
7305 To->addAttr(Import(FromAttr));
Sean Callanan8bca9962016-03-28 21:43:01 +00007306 }
7307 if (From->isUsed()) {
7308 To->setIsUsed();
7309 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007310 if (From->isImplicit()) {
7311 To->setImplicit();
7312 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007313 ImportedDecls[From] = To;
7314 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007315}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007316
Douglas Gregordd6006f2012-07-17 21:16:27 +00007317bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7318 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007319 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007320 = ImportedTypes.find(From.getTypePtr());
7321 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7322 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007323
Douglas Gregordd6006f2012-07-17 21:16:27 +00007324 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7325 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007326 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007327}