blob: 6f14ab02ea8ce6b701826dd6e5e88d2c54f7ba58 [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);
Rafael Stahldf556202018-05-29 08:12:15 +0000131
132 // 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();
Gabor Martona3af5672018-05-23 14:24:02 +00001962 if (Definition && Definition != D &&
1963 // In contrast to a normal CXXRecordDecl, the implicit
1964 // CXXRecordDecl of ClassTemplateSpecializationDecl is its redeclaration.
1965 // The definition of the implicit CXXRecordDecl in this case is the
1966 // ClassTemplateSpecializationDecl itself. Thus, we start with an extra
1967 // condition in order to be able to import the implict Decl.
1968 !D->isImplicit()) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001969 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001970 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001971 return nullptr;
1972
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001973 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001974 }
Gabor Martona3af5672018-05-23 14:24:02 +00001975
Douglas Gregor5c73e912010-02-11 00:48:18 +00001976 // Import the major distinguishing characteristics of this record.
1977 DeclContext *DC, *LexicalDC;
1978 DeclarationName Name;
1979 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001980 NamedDecl *ToD;
1981 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001982 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001983 if (ToD)
1984 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001985
Douglas Gregor5c73e912010-02-11 00:48:18 +00001986 // Figure out what structure name we're looking for.
1987 unsigned IDNS = Decl::IDNS_Tag;
1988 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001989 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1990 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001991 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001992 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001993 IDNS |= Decl::IDNS_Ordinary;
1994
1995 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001996 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001997 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001998 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001999 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002000 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00002001 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00002002
2003 if (!FoundDecls.empty()) {
2004 // We're going to have to compare D against potentially conflicting Decls, so complete it.
2005 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
2006 D->getASTContext().getExternalSource()->CompleteType(D);
2007 }
2008
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002009 for (auto *FoundDecl : FoundDecls) {
2010 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002011 continue;
2012
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002013 Decl *Found = FoundDecl;
2014 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
2015 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002016 Found = Tag->getDecl();
2017 }
Gabor Martona0df7a92018-05-30 09:19:26 +00002018
2019 if (D->getDescribedTemplate()) {
2020 if (auto *Template = dyn_cast<ClassTemplateDecl>(Found))
2021 Found = Template->getTemplatedDecl();
2022 else
2023 continue;
2024 }
2025
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002026 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002027 if (!SearchName) {
2028 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002029 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002030 if (Optional<unsigned> Index1 =
2031 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
2032 D)) {
2033 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00002034 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002035 if (*Index1 != *Index2)
2036 continue;
2037 }
2038 }
2039 }
2040
Sean Callanan9092d472017-05-13 00:46:33 +00002041 PrevDecl = FoundRecord;
2042
Douglas Gregor25791052010-02-12 00:09:27 +00002043 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002044 if ((SearchName && !D->isCompleteDefinition())
2045 || (D->isCompleteDefinition() &&
2046 D->isAnonymousStructOrUnion()
2047 == FoundDef->isAnonymousStructOrUnion() &&
2048 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002049 // The record types structurally match, or the "from" translation
2050 // unit only had a forward declaration anyway; call it the same
2051 // function.
2052 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002053 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002054 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002055 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002056 // We have a forward declaration of this type, so adopt that forward
2057 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002058
2059 // If one or both can be completed from external storage then try one
2060 // last time to complete and compare them before doing this.
2061
2062 if (FoundRecord->hasExternalLexicalStorage() &&
2063 !FoundRecord->isCompleteDefinition())
2064 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2065 if (D->hasExternalLexicalStorage())
2066 D->getASTContext().getExternalSource()->CompleteType(D);
2067
2068 if (FoundRecord->isCompleteDefinition() &&
2069 D->isCompleteDefinition() &&
2070 !IsStructuralMatch(D, FoundRecord))
2071 continue;
2072
Douglas Gregor25791052010-02-12 00:09:27 +00002073 AdoptDecl = FoundRecord;
2074 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002075 } else if (!SearchName) {
2076 continue;
2077 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002078 }
2079
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002080 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002081 }
2082
Douglas Gregordd6006f2012-07-17 21:16:27 +00002083 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002084 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2085 ConflictingDecls.data(),
2086 ConflictingDecls.size());
2087 }
2088 }
2089
2090 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002091 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002092 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002093 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002094 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002095 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002096 if (DCXX->isLambda()) {
2097 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2098 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2099 DC, TInfo, Loc,
2100 DCXX->isDependentLambda(),
2101 DCXX->isGenericLambda(),
2102 DCXX->getLambdaCaptureDefault());
2103 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2104 if (DCXX->getLambdaContextDecl() && !CDecl)
2105 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002106 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002107 } else if (DCXX->isInjectedClassName()) {
2108 // We have to be careful to do a similar dance to the one in
2109 // Sema::ActOnStartCXXMemberDeclarations
2110 CXXRecordDecl *const PrevDecl = nullptr;
2111 const bool DelayTypeCreation = true;
2112 D2CXX = CXXRecordDecl::Create(
2113 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2114 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2115 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002116 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002117 } else {
2118 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2119 D->getTagKind(),
2120 DC, StartLoc, Loc,
2121 Name.getAsIdentifierInfo());
2122 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002123 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002124 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002125 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002126 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002127 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002128
2129 Importer.Imported(D, D2);
2130
2131 if (ClassTemplateDecl *FromDescribed =
2132 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002133 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2134 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002135 if (!ToDescribed)
2136 return nullptr;
2137 D2CXX->setDescribedClassTemplate(ToDescribed);
Gabor Marton5915777e2018-06-26 13:44:24 +00002138 if (!DCXX->isInjectedClassName()) {
2139 // In a record describing a template the type should be an
2140 // InjectedClassNameType (see Sema::CheckClassTemplate). Update the
2141 // previously set type to the correct value here (ToDescribed is not
2142 // available at record create).
2143 // FIXME: The previous type is cleared but not removed from
2144 // ASTContext's internal storage.
2145 CXXRecordDecl *Injected = nullptr;
2146 for (NamedDecl *Found : D2CXX->noload_lookup(Name)) {
2147 auto *Record = dyn_cast<CXXRecordDecl>(Found);
2148 if (Record && Record->isInjectedClassName()) {
2149 Injected = Record;
2150 break;
2151 }
2152 }
2153 D2CXX->setTypeForDecl(nullptr);
2154 Importer.getToContext().getInjectedClassNameType(D2CXX,
2155 ToDescribed->getInjectedClassNameSpecialization());
2156 if (Injected) {
2157 Injected->setTypeForDecl(nullptr);
2158 Importer.getToContext().getTypeDeclType(Injected, D2CXX);
2159 }
2160 }
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002161 } else if (MemberSpecializationInfo *MemberInfo =
2162 DCXX->getMemberSpecializationInfo()) {
2163 TemplateSpecializationKind SK =
2164 MemberInfo->getTemplateSpecializationKind();
2165 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002166 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002167 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2168 if (FromInst && !ToInst)
2169 return nullptr;
2170 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2171 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2172 Importer.Import(MemberInfo->getPointOfInstantiation()));
2173 }
Douglas Gregor25791052010-02-12 00:09:27 +00002174 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002175 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002176 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002177 D2->setLexicalDeclContext(LexicalDC);
2178 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002179 }
Douglas Gregor14454802011-02-25 02:25:35 +00002180
2181 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002182 if (D->isAnonymousStructOrUnion())
2183 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002184 if (PrevDecl) {
2185 // FIXME: do this for all Redeclarables, not just RecordDecls.
2186 D2->setPreviousDecl(PrevDecl);
2187 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002188 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002189
Douglas Gregor3996e242010-02-15 22:01:00 +00002190 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002191
Douglas Gregor95d82832012-01-24 18:36:04 +00002192 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002193 return nullptr;
2194
Douglas Gregor3996e242010-02-15 22:01:00 +00002195 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002196}
2197
Douglas Gregor98c10182010-02-12 22:17:39 +00002198Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2199 // Import the major distinguishing characteristics of this enumerator.
2200 DeclContext *DC, *LexicalDC;
2201 DeclarationName Name;
2202 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002203 NamedDecl *ToD;
2204 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002205 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002206 if (ToD)
2207 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002208
2209 QualType T = Importer.Import(D->getType());
2210 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002211 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002212
Douglas Gregor98c10182010-02-12 22:17:39 +00002213 // Determine whether there are any other declarations with the same name and
2214 // in the same context.
2215 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002216 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002217 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002218 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002219 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002220 for (auto *FoundDecl : FoundDecls) {
2221 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002222 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002223
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002224 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002225 if (IsStructuralMatch(D, FoundEnumConstant))
2226 return Importer.Imported(D, FoundEnumConstant);
2227 }
2228
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002229 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002230 }
2231
2232 if (!ConflictingDecls.empty()) {
2233 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2234 ConflictingDecls.data(),
2235 ConflictingDecls.size());
2236 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002237 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002238 }
2239 }
2240
2241 Expr *Init = Importer.Import(D->getInitExpr());
2242 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002243 return nullptr;
2244
Douglas Gregor98c10182010-02-12 22:17:39 +00002245 EnumConstantDecl *ToEnumerator
2246 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2247 Name.getAsIdentifierInfo(), T,
2248 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002249 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002250 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002251 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002252 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002253 return ToEnumerator;
2254}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002255
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002256bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2257 FunctionDecl *ToFD) {
2258 switch (FromFD->getTemplatedKind()) {
2259 case FunctionDecl::TK_NonTemplate:
2260 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002261 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002262
2263 case FunctionDecl::TK_MemberSpecialization: {
2264 auto *InstFD = cast_or_null<FunctionDecl>(
2265 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2266 if (!InstFD)
2267 return true;
2268
2269 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2270 SourceLocation POI = Importer.Import(
2271 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2272 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2273 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002274 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002275 }
2276
2277 case FunctionDecl::TK_FunctionTemplateSpecialization: {
Gabor Martone4788172018-06-25 16:25:30 +00002278 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2279 auto *Template = cast_or_null<FunctionTemplateDecl>(
2280 Importer.Import(FTSInfo->getTemplate()));
2281 if (!Template)
2282 return true;
2283 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2284
2285 // Import template arguments.
2286 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2287 SmallVector<TemplateArgument, 8> ToTemplArgs;
2288 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2289 ToTemplArgs))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002290 return true;
2291
2292 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
Gabor Martone4788172018-06-25 16:25:30 +00002293 Importer.getToContext(), ToTemplArgs);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002294
2295 TemplateArgumentListInfo ToTAInfo;
2296 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002297 if (FromTAArgsAsWritten)
2298 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002299 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002300
2301 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2302
2303 ToFD->setFunctionTemplateSpecialization(
2304 Template, ToTAList, /* InsertPos= */ nullptr,
2305 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002306 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002307 }
2308
2309 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2310 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2311 UnresolvedSet<8> TemplDecls;
2312 unsigned NumTemplates = FromInfo->getNumTemplates();
2313 for (unsigned I = 0; I < NumTemplates; I++) {
2314 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2315 Importer.Import(FromInfo->getTemplate(I))))
2316 TemplDecls.addDecl(ToFTD);
2317 else
2318 return true;
2319 }
2320
2321 // Import TemplateArgumentListInfo.
2322 TemplateArgumentListInfo ToTAInfo;
2323 if (ImportTemplateArgumentListInfo(
2324 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2325 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2326 FromInfo->getNumTemplateArgs()),
2327 ToTAInfo))
2328 return true;
2329
2330 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2331 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002332 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002333 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002334 }
Sam McCallfdc32072018-01-26 12:06:44 +00002335 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002336}
2337
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002338Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2339 // Import the major distinguishing characteristics of this function.
2340 DeclContext *DC, *LexicalDC;
2341 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002342 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002343 NamedDecl *ToD;
2344 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002345 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002346 if (ToD)
2347 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002348
Gabor Martone4788172018-06-25 16:25:30 +00002349 const FunctionDecl *FoundWithoutBody = nullptr;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002350
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002351 // Try to find a function in our own ("to") context with the same name, same
2352 // type, and in the same context as the function we're importing.
Gabor Martone4788172018-06-25 16:25:30 +00002353 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002354 SmallVector<NamedDecl *, 4> ConflictingDecls;
Gabor Martone4788172018-06-25 16:25:30 +00002355 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002356 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002357 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002358 for (auto *FoundDecl : FoundDecls) {
2359 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002360 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002361
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002362 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002363 if (FoundFunction->hasExternalFormalLinkage() &&
2364 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002365 if (Importer.IsStructurallyEquivalent(D->getType(),
2366 FoundFunction->getType())) {
Gabor Martone4788172018-06-25 16:25:30 +00002367 // FIXME: Actually try to merge the body and other attributes.
2368 const FunctionDecl *FromBodyDecl = nullptr;
2369 D->hasBody(FromBodyDecl);
2370 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2371 // This function is needed to merge completely.
2372 FoundWithoutBody = FoundFunction;
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002373 break;
Gabor Martone4788172018-06-25 16:25:30 +00002374 }
2375 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002376 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002377
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002378 // FIXME: Check for overloading more carefully, e.g., by boosting
2379 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002380
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002381 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002382 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002383 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002384
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002385 // Complain about inconsistent function types.
2386 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002387 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002388 Importer.ToDiag(FoundFunction->getLocation(),
2389 diag::note_odr_value_here)
2390 << FoundFunction->getType();
2391 }
2392 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002393
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002394 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002395 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002396
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002397 if (!ConflictingDecls.empty()) {
2398 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2399 ConflictingDecls.data(),
2400 ConflictingDecls.size());
2401 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002402 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002403 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002404 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002405
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002406 DeclarationNameInfo NameInfo(Name, Loc);
2407 // Import additional name location/type info.
2408 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2409
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002410 QualType FromTy = D->getType();
2411 bool usedDifferentExceptionSpec = false;
2412
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002413 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002414 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2415 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2416 // FunctionDecl that we are importing the FunctionProtoType for.
2417 // To avoid an infinite recursion when importing, create the FunctionDecl
2418 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002419 if (FromEPI.ExceptionSpec.SourceDecl ||
2420 FromEPI.ExceptionSpec.SourceTemplate ||
2421 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002422 FunctionProtoType::ExtProtoInfo DefaultEPI;
2423 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002424 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002425 usedDifferentExceptionSpec = true;
2426 }
2427 }
2428
Douglas Gregorb4964f72010-02-15 23:54:17 +00002429 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002430 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002431 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002432 return nullptr;
2433
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002434 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002435 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002436 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002437 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002438 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002439 return nullptr;
2440
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002441 Parameters.push_back(ToP);
2442 }
2443
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002444 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002445 if (D->getTypeSourceInfo() && !TInfo)
2446 return nullptr;
2447
2448 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002449 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002450 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002451 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002452 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2453 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002454 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002455 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002456 FromConstructor->isExplicit(),
2457 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002458 D->isImplicit(),
2459 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002460 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2461 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002462 for (auto *I : FromConstructor->inits()) {
2463 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002464 if (!ToI && I)
2465 return nullptr;
2466 CtorInitializers.push_back(ToI);
2467 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002468 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002469 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2470 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002471 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002472 ToCtor->setCtorInitializers(Memory);
2473 ToCtor->setNumCtorInitializers(NumInitializers);
2474 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002475 } else if (isa<CXXDestructorDecl>(D)) {
2476 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2477 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002478 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002479 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002480 D->isInlineSpecified(),
2481 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002482 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002483 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2484 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002485 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002486 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002487 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002488 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002489 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002490 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002491 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002492 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2493 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002494 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002495 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002496 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002497 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002498 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002499 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002500 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002501 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002502 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002503 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002504 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002505 D->hasWrittenPrototype(),
2506 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002507 }
John McCall3e11ebe2010-03-15 10:12:16 +00002508
2509 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002510 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002511 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002512 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002513 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2514 ToFunction->setTrivial(D->isTrivial());
2515 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002516 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002517
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002518 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002519 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002520 Param->setOwningFunction(ToFunction);
2521 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002522 }
David Blaikie9c70e042011-09-21 18:16:56 +00002523 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002524
Gabor Martone4788172018-06-25 16:25:30 +00002525 if (FoundWithoutBody) {
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002526 auto *Recent = const_cast<FunctionDecl *>(
Gabor Martone4788172018-06-25 16:25:30 +00002527 FoundWithoutBody->getMostRecentDecl());
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002528 ToFunction->setPreviousDecl(Recent);
2529 }
2530
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002531 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2532 // params it refers to.
2533 if (TInfo) {
2534 if (auto ProtoLoc =
2535 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2536 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2537 ProtoLoc.setParam(I, Parameters[I]);
2538 }
2539 }
2540
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002541 if (usedDifferentExceptionSpec) {
2542 // Update FunctionProtoType::ExtProtoInfo.
2543 QualType T = Importer.Import(D->getType());
2544 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002545 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002546 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002547 }
2548
Gabor Martone4788172018-06-25 16:25:30 +00002549 // Import the body, if any.
2550 if (Stmt *FromBody = D->getBody()) {
2551 if (Stmt *ToBody = Importer.Import(FromBody)) {
2552 ToFunction->setBody(ToBody);
Sean Callanan59721b32015-04-28 18:41:46 +00002553 }
2554 }
2555
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002556 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002557
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002558 // If it is a template, import all related things.
2559 if (ImportTemplateInformation(D, ToFunction))
2560 return nullptr;
2561
Gabor Martone4788172018-06-25 16:25:30 +00002562 // Add this function to the lexical context.
2563 // NOTE: If the function is templated declaration, it should be not added into
2564 // LexicalDC. But described template is imported during import of
2565 // FunctionTemplateDecl (it happens later). So, we use source declaration
2566 // to determine if we should add the result function.
2567 if (!D->getDescribedFunctionTemplate())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002568 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002569
Lang Hames19e07e12017-06-20 21:06:00 +00002570 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2571 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2572
Douglas Gregor43f54792010-02-17 02:12:47 +00002573 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002574}
2575
Douglas Gregor00eace12010-02-21 18:29:16 +00002576Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2577 return VisitFunctionDecl(D);
2578}
2579
2580Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2581 return VisitCXXMethodDecl(D);
2582}
2583
2584Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2585 return VisitCXXMethodDecl(D);
2586}
2587
2588Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2589 return VisitCXXMethodDecl(D);
2590}
2591
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002592static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002593 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002594 if (!Owner)
2595 return 0;
2596
2597 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002598 for (const auto *D : Owner->noload_decls()) {
2599 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002600 return Index;
2601
2602 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2603 ++Index;
2604 }
2605
2606 return Index;
2607}
2608
Douglas Gregor5c73e912010-02-11 00:48:18 +00002609Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2610 // Import the major distinguishing characteristics of a variable.
2611 DeclContext *DC, *LexicalDC;
2612 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002613 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002614 NamedDecl *ToD;
2615 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002616 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002617 if (ToD)
2618 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002619
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002620 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002621 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002622 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002623 for (auto *FoundDecl : FoundDecls) {
2624 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002625 // For anonymous fields, match up by index.
2626 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2627 continue;
2628
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002629 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002630 FoundField->getType())) {
2631 Importer.Imported(D, FoundField);
2632 return FoundField;
2633 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002634
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002635 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2636 << Name << D->getType() << FoundField->getType();
2637 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2638 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002639 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002640 }
2641 }
2642
Douglas Gregorb4964f72010-02-15 23:54:17 +00002643 // Import the type.
2644 QualType T = Importer.Import(D->getType());
2645 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002646 return nullptr;
2647
Douglas Gregor5c73e912010-02-11 00:48:18 +00002648 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2649 Expr *BitWidth = Importer.Import(D->getBitWidth());
2650 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002651 return nullptr;
2652
Abramo Bagnaradff19302011-03-08 08:55:46 +00002653 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2654 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002655 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002656 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002657 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002658 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002659 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002660 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002661 Expr *ToInitializer = Importer.Import(FromInitializer);
2662 if (ToInitializer)
2663 ToField->setInClassInitializer(ToInitializer);
2664 else
2665 return nullptr;
2666 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002667 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002668 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002669 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002670 return ToField;
2671}
2672
Francois Pichet783dd6e2010-11-21 06:08:52 +00002673Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2674 // Import the major distinguishing characteristics of a variable.
2675 DeclContext *DC, *LexicalDC;
2676 DeclarationName Name;
2677 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002678 NamedDecl *ToD;
2679 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002680 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002681 if (ToD)
2682 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002683
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002684 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002685 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002686 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002687 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002688 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002689 // For anonymous indirect fields, match up by index.
2690 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2691 continue;
2692
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002693 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002694 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002695 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002696 Importer.Imported(D, FoundField);
2697 return FoundField;
2698 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002699
2700 // If there are more anonymous fields to check, continue.
2701 if (!Name && I < N-1)
2702 continue;
2703
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002704 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2705 << Name << D->getType() << FoundField->getType();
2706 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2707 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002708 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002709 }
2710 }
2711
Francois Pichet783dd6e2010-11-21 06:08:52 +00002712 // Import the type.
2713 QualType T = Importer.Import(D->getType());
2714 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002715 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002716
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002717 auto **NamedChain =
2718 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002719
2720 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002721 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002722 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002723 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002724 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002725 NamedChain[i++] = cast<NamedDecl>(D);
2726 }
2727
2728 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002729 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002730 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002731
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002732 for (const auto *A : D->attrs())
2733 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002734
Francois Pichet783dd6e2010-11-21 06:08:52 +00002735 ToIndirectField->setAccess(D->getAccess());
2736 ToIndirectField->setLexicalDeclContext(LexicalDC);
2737 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002738 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002739 return ToIndirectField;
2740}
2741
Aleksei Sidorina693b372016-09-28 10:16:56 +00002742Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2743 // Import the major distinguishing characteristics of a declaration.
2744 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2745 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2746 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2747 if (!DC || !LexicalDC)
2748 return nullptr;
2749
2750 // Determine whether we've already imported this decl.
2751 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2752 auto *RD = cast<CXXRecordDecl>(DC);
2753 FriendDecl *ImportedFriend = RD->getFirstFriend();
2754 StructuralEquivalenceContext Context(
2755 Importer.getFromContext(), Importer.getToContext(),
2756 Importer.getNonEquivalentDecls(), false, false);
2757
2758 while (ImportedFriend) {
2759 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2760 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2761 ImportedFriend->getFriendDecl()))
2762 return Importer.Imported(D, ImportedFriend);
2763
2764 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2765 if (Importer.IsStructurallyEquivalent(
2766 D->getFriendType()->getType(),
2767 ImportedFriend->getFriendType()->getType(), true))
2768 return Importer.Imported(D, ImportedFriend);
2769 }
2770 ImportedFriend = ImportedFriend->getNextFriend();
2771 }
2772
2773 // Not found. Create it.
2774 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002775 if (NamedDecl *FriendD = D->getFriendDecl()) {
2776 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2777 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2778 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2779 ToFriendD->setObjectOfFriendDecl(false);
2780
2781 ToFU = ToFriendD;
2782 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002783 ToFU = Importer.Import(D->getFriendType());
2784 if (!ToFU)
2785 return nullptr;
2786
2787 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002788 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002789 for (unsigned I = 0; I < D->NumTPLists; I++) {
2790 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2791 if (!List)
2792 return nullptr;
2793 ToTPLists[I] = List;
2794 }
2795
2796 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2797 Importer.Import(D->getLocation()),
2798 ToFU, Importer.Import(D->getFriendLoc()),
2799 ToTPLists);
2800
2801 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002802
2803 FrD->setAccess(D->getAccess());
2804 FrD->setLexicalDeclContext(LexicalDC);
2805 LexicalDC->addDeclInternal(FrD);
2806 return FrD;
2807}
2808
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002809Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2810 // Import the major distinguishing characteristics of an ivar.
2811 DeclContext *DC, *LexicalDC;
2812 DeclarationName Name;
2813 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002814 NamedDecl *ToD;
2815 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002816 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002817 if (ToD)
2818 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002819
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002820 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002821 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002822 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002823 for (auto *FoundDecl : FoundDecls) {
2824 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002825 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002826 FoundIvar->getType())) {
2827 Importer.Imported(D, FoundIvar);
2828 return FoundIvar;
2829 }
2830
2831 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2832 << Name << D->getType() << FoundIvar->getType();
2833 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2834 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002835 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002836 }
2837 }
2838
2839 // Import the type.
2840 QualType T = Importer.Import(D->getType());
2841 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002842 return nullptr;
2843
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002844 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2845 Expr *BitWidth = Importer.Import(D->getBitWidth());
2846 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002847 return nullptr;
2848
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002849 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2850 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002851 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002852 Loc, Name.getAsIdentifierInfo(),
2853 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002854 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002855 ToIvar->setLexicalDeclContext(LexicalDC);
2856 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002857 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002858 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002859}
2860
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002861Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2862 // Import the major distinguishing characteristics of a variable.
2863 DeclContext *DC, *LexicalDC;
2864 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002865 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002866 NamedDecl *ToD;
2867 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002868 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002869 if (ToD)
2870 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002871
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002872 // Try to find a variable in our own ("to") context with the same name and
2873 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002874 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002875 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002876 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002877 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002878 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002879 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002880 for (auto *FoundDecl : FoundDecls) {
2881 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002882 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002883
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002884 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002885 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002886 if (FoundVar->hasExternalFormalLinkage() &&
2887 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002888 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002889 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002890 MergeWithVar = FoundVar;
2891 break;
2892 }
2893
Douglas Gregor56521c52010-02-12 17:23:39 +00002894 const ArrayType *FoundArray
2895 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2896 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002897 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002898 if (FoundArray && TArray) {
2899 if (isa<IncompleteArrayType>(FoundArray) &&
2900 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002901 // Import the type.
2902 QualType T = Importer.Import(D->getType());
2903 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002904 return nullptr;
2905
Douglas Gregor56521c52010-02-12 17:23:39 +00002906 FoundVar->setType(T);
2907 MergeWithVar = FoundVar;
2908 break;
2909 } else if (isa<IncompleteArrayType>(TArray) &&
2910 isa<ConstantArrayType>(FoundArray)) {
2911 MergeWithVar = FoundVar;
2912 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002913 }
2914 }
2915
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002916 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002917 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002918 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2919 << FoundVar->getType();
2920 }
2921 }
2922
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002923 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002924 }
2925
2926 if (MergeWithVar) {
2927 // An equivalent variable with external linkage has been found. Link
2928 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002929 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002930
2931 if (VarDecl *DDef = D->getDefinition()) {
2932 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2933 Importer.ToDiag(ExistingDef->getLocation(),
2934 diag::err_odr_variable_multiple_def)
2935 << Name;
2936 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2937 } else {
2938 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002939 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002940 if (DDef->isInitKnownICE()) {
2941 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2942 Eval->CheckedICE = true;
2943 Eval->IsICE = DDef->isInitICE();
2944 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002945 }
2946 }
2947
2948 return MergeWithVar;
2949 }
2950
2951 if (!ConflictingDecls.empty()) {
2952 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2953 ConflictingDecls.data(),
2954 ConflictingDecls.size());
2955 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002956 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002957 }
2958 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002959
Douglas Gregorb4964f72010-02-15 23:54:17 +00002960 // Import the type.
2961 QualType T = Importer.Import(D->getType());
2962 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002963 return nullptr;
2964
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002965 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002966 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002967 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2968 Importer.Import(D->getInnerLocStart()),
2969 Loc, Name.getAsIdentifierInfo(),
2970 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002971 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002972 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002973 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002974 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002975 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002976
2977 // Templated declarations should never appear in the enclosing DeclContext.
2978 if (!D->getDescribedVarTemplate())
2979 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002980
Sean Callanan59721b32015-04-28 18:41:46 +00002981 if (!D->isFileVarDecl() &&
2982 D->isUsed())
2983 ToVar->setIsUsed();
2984
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002985 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002986 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002987 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002988
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002989 if (D->isConstexpr())
2990 ToVar->setConstexpr(true);
2991
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002992 return ToVar;
2993}
2994
Douglas Gregor8b228d72010-02-17 21:22:52 +00002995Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2996 // Parameters are created in the translation unit's context, then moved
2997 // into the function declaration's context afterward.
2998 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2999
3000 // Import the name of this declaration.
3001 DeclarationName Name = Importer.Import(D->getDeclName());
3002 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003003 return nullptr;
3004
Douglas Gregor8b228d72010-02-17 21:22:52 +00003005 // Import the location of this declaration.
3006 SourceLocation Loc = Importer.Import(D->getLocation());
3007
3008 // Import the parameter's type.
3009 QualType T = Importer.Import(D->getType());
3010 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003011 return nullptr;
3012
Douglas Gregor8b228d72010-02-17 21:22:52 +00003013 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00003014 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
3015 Name.getAsIdentifierInfo(), T,
3016 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00003017 return Importer.Imported(D, ToParm);
3018}
3019
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003020Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3021 // Parameters are created in the translation unit's context, then moved
3022 // into the function declaration's context afterward.
3023 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3024
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003025 // Import the name of this declaration.
3026 DeclarationName Name = Importer.Import(D->getDeclName());
3027 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003028 return nullptr;
3029
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003030 // Import the location of this declaration.
3031 SourceLocation Loc = Importer.Import(D->getLocation());
3032
3033 // Import the parameter's type.
3034 QualType T = Importer.Import(D->getType());
3035 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003036 return nullptr;
3037
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003038 // Create the imported parameter.
3039 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3040 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003041 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003042 Loc, Name.getAsIdentifierInfo(),
3043 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003044 /*DefaultArg*/ nullptr);
3045
3046 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003047 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003048 ToParm->setKNRPromoted(D->isKNRPromoted());
3049
3050 Expr *ToDefArg = nullptr;
3051 Expr *FromDefArg = nullptr;
3052 if (D->hasUninstantiatedDefaultArg()) {
3053 FromDefArg = D->getUninstantiatedDefaultArg();
3054 ToDefArg = Importer.Import(FromDefArg);
3055 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3056 } else if (D->hasUnparsedDefaultArg()) {
3057 ToParm->setUnparsedDefaultArg();
3058 } else if (D->hasDefaultArg()) {
3059 FromDefArg = D->getDefaultArg();
3060 ToDefArg = Importer.Import(FromDefArg);
3061 ToParm->setDefaultArg(ToDefArg);
3062 }
3063 if (FromDefArg && !ToDefArg)
3064 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003065
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003066 if (D->isObjCMethodParameter()) {
3067 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3068 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3069 } else {
3070 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3071 D->getFunctionScopeIndex());
3072 }
3073
Sean Callanan59721b32015-04-28 18:41:46 +00003074 if (D->isUsed())
3075 ToParm->setIsUsed();
3076
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003077 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003078}
3079
Douglas Gregor43f54792010-02-17 02:12:47 +00003080Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3081 // Import the major distinguishing characteristics of a method.
3082 DeclContext *DC, *LexicalDC;
3083 DeclarationName Name;
3084 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003085 NamedDecl *ToD;
3086 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003087 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003088 if (ToD)
3089 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003090
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003091 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003092 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003093 for (auto *FoundDecl : FoundDecls) {
3094 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003095 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3096 continue;
3097
3098 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003099 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3100 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003101 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003102 << D->isInstanceMethod() << Name << D->getReturnType()
3103 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003104 Importer.ToDiag(FoundMethod->getLocation(),
3105 diag::note_odr_objc_method_here)
3106 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003107 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003108 }
3109
3110 // Check the number of parameters.
3111 if (D->param_size() != FoundMethod->param_size()) {
3112 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3113 << D->isInstanceMethod() << Name
3114 << D->param_size() << FoundMethod->param_size();
3115 Importer.ToDiag(FoundMethod->getLocation(),
3116 diag::note_odr_objc_method_here)
3117 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003118 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003119 }
3120
3121 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003122 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003123 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3124 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003125 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003126 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003127 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003128 diag::err_odr_objc_method_param_type_inconsistent)
3129 << D->isInstanceMethod() << Name
3130 << (*P)->getType() << (*FoundP)->getType();
3131 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3132 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003133 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003134 }
3135 }
3136
3137 // Check variadic/non-variadic.
3138 // Check the number of parameters.
3139 if (D->isVariadic() != FoundMethod->isVariadic()) {
3140 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3141 << D->isInstanceMethod() << Name;
3142 Importer.ToDiag(FoundMethod->getLocation(),
3143 diag::note_odr_objc_method_here)
3144 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003145 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003146 }
3147
3148 // FIXME: Any other bits we need to merge?
3149 return Importer.Imported(D, FoundMethod);
3150 }
3151 }
3152
3153 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003154 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003155 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003156 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003157
Alp Toker314cc812014-01-25 16:55:45 +00003158 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003159
Alp Toker314cc812014-01-25 16:55:45 +00003160 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3161 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3162 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3163 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3164 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003165
3166 // FIXME: When we decide to merge method definitions, we'll need to
3167 // deal with implicit parameters.
3168
3169 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003170 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003171 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003172 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003173 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003174 return nullptr;
3175
Douglas Gregor43f54792010-02-17 02:12:47 +00003176 ToParams.push_back(ToP);
3177 }
3178
3179 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003180 for (auto *ToParam : ToParams) {
3181 ToParam->setOwningFunction(ToMethod);
3182 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003183 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003184
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003185 SmallVector<SourceLocation, 12> SelLocs;
3186 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003187 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003188 Loc = Importer.Import(Loc);
3189
3190 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003191
3192 ToMethod->setLexicalDeclContext(LexicalDC);
3193 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003194 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003195 return ToMethod;
3196}
3197
Douglas Gregor85f3f952015-07-07 03:57:15 +00003198Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3199 // Import the major distinguishing characteristics of a category.
3200 DeclContext *DC, *LexicalDC;
3201 DeclarationName Name;
3202 SourceLocation Loc;
3203 NamedDecl *ToD;
3204 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3205 return nullptr;
3206 if (ToD)
3207 return ToD;
3208
3209 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3210 if (!BoundInfo)
3211 return nullptr;
3212
3213 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3214 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003215 D->getVariance(),
3216 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003217 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003218 Importer.Import(D->getLocation()),
3219 Name.getAsIdentifierInfo(),
3220 Importer.Import(D->getColonLoc()),
3221 BoundInfo);
3222 Importer.Imported(D, Result);
3223 Result->setLexicalDeclContext(LexicalDC);
3224 return Result;
3225}
3226
Douglas Gregor84c51c32010-02-18 01:47:50 +00003227Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3228 // Import the major distinguishing characteristics of a category.
3229 DeclContext *DC, *LexicalDC;
3230 DeclarationName Name;
3231 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003232 NamedDecl *ToD;
3233 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003234 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003235 if (ToD)
3236 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003237
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003238 auto *ToInterface =
3239 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003240 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003241 return nullptr;
3242
Douglas Gregor84c51c32010-02-18 01:47:50 +00003243 // Determine if we've already encountered this category.
3244 ObjCCategoryDecl *MergeWithCategory
3245 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3246 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3247 if (!ToCategory) {
3248 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003249 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003250 Loc,
3251 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003252 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003253 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003254 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003255 Importer.Import(D->getIvarLBraceLoc()),
3256 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003257 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003258 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003259 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003260 // Import the type parameter list after calling Imported, to avoid
3261 // loops when bringing in their DeclContext.
3262 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3263 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003264
Douglas Gregor84c51c32010-02-18 01:47:50 +00003265 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003266 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3267 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003268 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3269 = D->protocol_loc_begin();
3270 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3271 FromProtoEnd = D->protocol_end();
3272 FromProto != FromProtoEnd;
3273 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003274 auto *ToProto =
3275 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003276 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003277 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003278 Protocols.push_back(ToProto);
3279 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3280 }
3281
3282 // FIXME: If we're merging, make sure that the protocol list is the same.
3283 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3284 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003285 } else {
3286 Importer.Imported(D, ToCategory);
3287 }
3288
3289 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003290 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003291
3292 // If we have an implementation, import it as well.
3293 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003294 auto *Impl =
3295 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003296 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003297 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003298 return nullptr;
3299
Douglas Gregor84c51c32010-02-18 01:47:50 +00003300 ToCategory->setImplementation(Impl);
3301 }
3302
3303 return ToCategory;
3304}
3305
Douglas Gregor2aa53772012-01-24 17:42:07 +00003306bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3307 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003308 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003309 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003310 if (shouldForceImportDeclContext(Kind))
3311 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003312 return false;
3313 }
3314
3315 // Start the protocol definition
3316 To->startDefinition();
3317
3318 // Import protocols
3319 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3320 SmallVector<SourceLocation, 4> ProtocolLocs;
3321 ObjCProtocolDecl::protocol_loc_iterator
3322 FromProtoLoc = From->protocol_loc_begin();
3323 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3324 FromProtoEnd = From->protocol_end();
3325 FromProto != FromProtoEnd;
3326 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003327 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003328 if (!ToProto)
3329 return true;
3330 Protocols.push_back(ToProto);
3331 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3332 }
3333
3334 // FIXME: If we're merging, make sure that the protocol list is the same.
3335 To->setProtocolList(Protocols.data(), Protocols.size(),
3336 ProtocolLocs.data(), Importer.getToContext());
3337
Douglas Gregor2e15c842012-02-01 21:00:38 +00003338 if (shouldForceImportDeclContext(Kind)) {
3339 // Import all of the members of this protocol.
3340 ImportDeclContext(From, /*ForceImport=*/true);
3341 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003342 return false;
3343}
3344
Douglas Gregor98d156a2010-02-17 16:12:00 +00003345Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003346 // If this protocol has a definition in the translation unit we're coming
3347 // from, but this particular declaration is not that definition, import the
3348 // definition and map to that.
3349 ObjCProtocolDecl *Definition = D->getDefinition();
3350 if (Definition && Definition != D) {
3351 Decl *ImportedDef = Importer.Import(Definition);
3352 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003353 return nullptr;
3354
Douglas Gregor2aa53772012-01-24 17:42:07 +00003355 return Importer.Imported(D, ImportedDef);
3356 }
3357
Douglas Gregor84c51c32010-02-18 01:47:50 +00003358 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003359 DeclContext *DC, *LexicalDC;
3360 DeclarationName Name;
3361 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003362 NamedDecl *ToD;
3363 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003364 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003365 if (ToD)
3366 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003367
Craig Topper36250ad2014-05-12 05:36:57 +00003368 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003369 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003370 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003371 for (auto *FoundDecl : FoundDecls) {
3372 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003373 continue;
3374
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003375 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003376 break;
3377 }
3378
3379 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003380 if (!ToProto) {
3381 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3382 Name.getAsIdentifierInfo(), Loc,
3383 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003384 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003385 ToProto->setLexicalDeclContext(LexicalDC);
3386 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003387 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003388
3389 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003390
Douglas Gregor2aa53772012-01-24 17:42:07 +00003391 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003392 return nullptr;
3393
Douglas Gregor98d156a2010-02-17 16:12:00 +00003394 return ToProto;
3395}
3396
Sean Callanan0aae0412014-12-10 00:00:37 +00003397Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3398 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3399 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3400
3401 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3402 SourceLocation LangLoc = Importer.Import(D->getLocation());
3403
3404 bool HasBraces = D->hasBraces();
3405
Sean Callananb12a8552014-12-10 21:22:20 +00003406 LinkageSpecDecl *ToLinkageSpec =
3407 LinkageSpecDecl::Create(Importer.getToContext(),
3408 DC,
3409 ExternLoc,
3410 LangLoc,
3411 D->getLanguage(),
3412 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003413
3414 if (HasBraces) {
3415 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3416 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3417 }
3418
3419 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3420 LexicalDC->addDeclInternal(ToLinkageSpec);
3421
3422 Importer.Imported(D, ToLinkageSpec);
3423
3424 return ToLinkageSpec;
3425}
3426
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003427Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3428 DeclContext *DC, *LexicalDC;
3429 DeclarationName Name;
3430 SourceLocation Loc;
3431 NamedDecl *ToD = nullptr;
3432 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3433 return nullptr;
3434 if (ToD)
3435 return ToD;
3436
3437 DeclarationNameInfo NameInfo(Name,
3438 Importer.Import(D->getNameInfo().getLoc()));
3439 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3440
3441 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3442 Importer.Import(D->getUsingLoc()),
3443 Importer.Import(D->getQualifierLoc()),
3444 NameInfo, D->hasTypename());
3445 ToUsing->setLexicalDeclContext(LexicalDC);
3446 LexicalDC->addDeclInternal(ToUsing);
3447 Importer.Imported(D, ToUsing);
3448
3449 if (NamedDecl *FromPattern =
3450 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003451 if (auto *ToPattern =
3452 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003453 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3454 else
3455 return nullptr;
3456 }
3457
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003458 for (auto *FromShadow : D->shadows()) {
3459 if (auto *ToShadow =
3460 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003461 ToUsing->addShadowDecl(ToShadow);
3462 else
3463 // FIXME: We return a nullptr here but the definition is already created
3464 // and available with lookups. How to fix this?..
3465 return nullptr;
3466 }
3467 return ToUsing;
3468}
3469
3470Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3471 DeclContext *DC, *LexicalDC;
3472 DeclarationName Name;
3473 SourceLocation Loc;
3474 NamedDecl *ToD = nullptr;
3475 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3476 return nullptr;
3477 if (ToD)
3478 return ToD;
3479
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003480 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3481 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003482 if (!ToUsing)
3483 return nullptr;
3484
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003485 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3486 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003487 if (!ToTarget)
3488 return nullptr;
3489
3490 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3491 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3492
3493 ToShadow->setLexicalDeclContext(LexicalDC);
3494 ToShadow->setAccess(D->getAccess());
3495 Importer.Imported(D, ToShadow);
3496
3497 if (UsingShadowDecl *FromPattern =
3498 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003499 if (auto *ToPattern =
3500 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003501 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3502 ToPattern);
3503 else
3504 // FIXME: We return a nullptr here but the definition is already created
3505 // and available with lookups. How to fix this?..
3506 return nullptr;
3507 }
3508
3509 LexicalDC->addDeclInternal(ToShadow);
3510
3511 return ToShadow;
3512}
3513
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003514Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3515 DeclContext *DC, *LexicalDC;
3516 DeclarationName Name;
3517 SourceLocation Loc;
3518 NamedDecl *ToD = nullptr;
3519 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3520 return nullptr;
3521 if (ToD)
3522 return ToD;
3523
3524 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3525 if (!ToComAncestor)
3526 return nullptr;
3527
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003528 auto *ToNominated = cast_or_null<NamespaceDecl>(
3529 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003530 if (!ToNominated)
3531 return nullptr;
3532
3533 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3534 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3535 Importer.Import(D->getNamespaceKeyLocation()),
3536 Importer.Import(D->getQualifierLoc()),
3537 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3538 ToUsingDir->setLexicalDeclContext(LexicalDC);
3539 LexicalDC->addDeclInternal(ToUsingDir);
3540 Importer.Imported(D, ToUsingDir);
3541
3542 return ToUsingDir;
3543}
3544
3545Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3546 UnresolvedUsingValueDecl *D) {
3547 DeclContext *DC, *LexicalDC;
3548 DeclarationName Name;
3549 SourceLocation Loc;
3550 NamedDecl *ToD = nullptr;
3551 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3552 return nullptr;
3553 if (ToD)
3554 return ToD;
3555
3556 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3557 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3558
3559 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3560 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3561 Importer.Import(D->getQualifierLoc()), NameInfo,
3562 Importer.Import(D->getEllipsisLoc()));
3563
3564 Importer.Imported(D, ToUsingValue);
3565 ToUsingValue->setAccess(D->getAccess());
3566 ToUsingValue->setLexicalDeclContext(LexicalDC);
3567 LexicalDC->addDeclInternal(ToUsingValue);
3568
3569 return ToUsingValue;
3570}
3571
3572Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3573 UnresolvedUsingTypenameDecl *D) {
3574 DeclContext *DC, *LexicalDC;
3575 DeclarationName Name;
3576 SourceLocation Loc;
3577 NamedDecl *ToD = nullptr;
3578 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3579 return nullptr;
3580 if (ToD)
3581 return ToD;
3582
3583 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3584 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3585 Importer.Import(D->getTypenameLoc()),
3586 Importer.Import(D->getQualifierLoc()), Loc, Name,
3587 Importer.Import(D->getEllipsisLoc()));
3588
3589 Importer.Imported(D, ToUsing);
3590 ToUsing->setAccess(D->getAccess());
3591 ToUsing->setLexicalDeclContext(LexicalDC);
3592 LexicalDC->addDeclInternal(ToUsing);
3593
3594 return ToUsing;
3595}
3596
Douglas Gregor2aa53772012-01-24 17:42:07 +00003597bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3598 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003599 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003600 if (To->getDefinition()) {
3601 // Check consistency of superclass.
3602 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3603 if (FromSuper) {
3604 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3605 if (!FromSuper)
3606 return true;
3607 }
3608
3609 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3610 if ((bool)FromSuper != (bool)ToSuper ||
3611 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3612 Importer.ToDiag(To->getLocation(),
3613 diag::err_odr_objc_superclass_inconsistent)
3614 << To->getDeclName();
3615 if (ToSuper)
3616 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3617 << To->getSuperClass()->getDeclName();
3618 else
3619 Importer.ToDiag(To->getLocation(),
3620 diag::note_odr_objc_missing_superclass);
3621 if (From->getSuperClass())
3622 Importer.FromDiag(From->getSuperClassLoc(),
3623 diag::note_odr_objc_superclass)
3624 << From->getSuperClass()->getDeclName();
3625 else
3626 Importer.FromDiag(From->getLocation(),
3627 diag::note_odr_objc_missing_superclass);
3628 }
3629
Douglas Gregor2e15c842012-02-01 21:00:38 +00003630 if (shouldForceImportDeclContext(Kind))
3631 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003632 return false;
3633 }
3634
3635 // Start the definition.
3636 To->startDefinition();
3637
3638 // If this class has a superclass, import it.
3639 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003640 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3641 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003642 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003643
3644 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003645 }
3646
3647 // Import protocols
3648 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3649 SmallVector<SourceLocation, 4> ProtocolLocs;
3650 ObjCInterfaceDecl::protocol_loc_iterator
3651 FromProtoLoc = From->protocol_loc_begin();
3652
3653 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3654 FromProtoEnd = From->protocol_end();
3655 FromProto != FromProtoEnd;
3656 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003657 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003658 if (!ToProto)
3659 return true;
3660 Protocols.push_back(ToProto);
3661 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3662 }
3663
3664 // FIXME: If we're merging, make sure that the protocol list is the same.
3665 To->setProtocolList(Protocols.data(), Protocols.size(),
3666 ProtocolLocs.data(), Importer.getToContext());
3667
3668 // Import categories. When the categories themselves are imported, they'll
3669 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003670 for (auto *Cat : From->known_categories())
3671 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003672
Douglas Gregor2aa53772012-01-24 17:42:07 +00003673 // If we have an @implementation, import it as well.
3674 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003675 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3676 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003677 if (!Impl)
3678 return true;
3679
3680 To->setImplementation(Impl);
3681 }
3682
Douglas Gregor2e15c842012-02-01 21:00:38 +00003683 if (shouldForceImportDeclContext(Kind)) {
3684 // Import all of the members of this class.
3685 ImportDeclContext(From, /*ForceImport=*/true);
3686 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003687 return false;
3688}
3689
Douglas Gregor85f3f952015-07-07 03:57:15 +00003690ObjCTypeParamList *
3691ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3692 if (!list)
3693 return nullptr;
3694
3695 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3696 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003697 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3698 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003699 if (!toTypeParam)
3700 return nullptr;
3701
3702 toTypeParams.push_back(toTypeParam);
3703 }
3704
3705 return ObjCTypeParamList::create(Importer.getToContext(),
3706 Importer.Import(list->getLAngleLoc()),
3707 toTypeParams,
3708 Importer.Import(list->getRAngleLoc()));
3709}
3710
Douglas Gregor45635322010-02-16 01:20:57 +00003711Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003712 // If this class has a definition in the translation unit we're coming from,
3713 // but this particular declaration is not that definition, import the
3714 // definition and map to that.
3715 ObjCInterfaceDecl *Definition = D->getDefinition();
3716 if (Definition && Definition != D) {
3717 Decl *ImportedDef = Importer.Import(Definition);
3718 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003719 return nullptr;
3720
Douglas Gregor2aa53772012-01-24 17:42:07 +00003721 return Importer.Imported(D, ImportedDef);
3722 }
3723
Douglas Gregor45635322010-02-16 01:20:57 +00003724 // Import the major distinguishing characteristics of an @interface.
3725 DeclContext *DC, *LexicalDC;
3726 DeclarationName Name;
3727 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003728 NamedDecl *ToD;
3729 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003730 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003731 if (ToD)
3732 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003733
Douglas Gregor2aa53772012-01-24 17:42:07 +00003734 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003735 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003736 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003737 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003738 for (auto *FoundDecl : FoundDecls) {
3739 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003740 continue;
3741
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003742 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003743 break;
3744 }
3745
Douglas Gregor2aa53772012-01-24 17:42:07 +00003746 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003747 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003748 if (!ToIface) {
3749 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3750 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003751 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003752 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003753 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003754 D->isImplicitInterfaceDecl());
3755 ToIface->setLexicalDeclContext(LexicalDC);
3756 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003757 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003758 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003759 // Import the type parameter list after calling Imported, to avoid
3760 // loops when bringing in their DeclContext.
3761 ToIface->setTypeParamList(ImportObjCTypeParamList(
3762 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003763
Douglas Gregor2aa53772012-01-24 17:42:07 +00003764 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003765 return nullptr;
3766
Douglas Gregor98d156a2010-02-17 16:12:00 +00003767 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003768}
3769
Douglas Gregor4da9d682010-12-07 15:32:12 +00003770Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003771 auto *Category = cast_or_null<ObjCCategoryDecl>(
3772 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003773 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003774 return nullptr;
3775
Douglas Gregor4da9d682010-12-07 15:32:12 +00003776 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3777 if (!ToImpl) {
3778 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3779 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003780 return nullptr;
3781
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003782 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003783 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003784 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003785 Category->getClassInterface(),
3786 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003787 Importer.Import(D->getAtStartLoc()),
3788 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003789
3790 DeclContext *LexicalDC = DC;
3791 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3792 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3793 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003794 return nullptr;
3795
Douglas Gregor4da9d682010-12-07 15:32:12 +00003796 ToImpl->setLexicalDeclContext(LexicalDC);
3797 }
3798
Sean Callanan95e74be2011-10-21 02:57:43 +00003799 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003800 Category->setImplementation(ToImpl);
3801 }
3802
3803 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003804 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003805 return ToImpl;
3806}
3807
Douglas Gregorda8025c2010-12-07 01:26:03 +00003808Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3809 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003810 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3811 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003812 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003813 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003814
3815 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003816 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003817 if (D->getSuperClass()) {
3818 Super = cast_or_null<ObjCInterfaceDecl>(
3819 Importer.Import(D->getSuperClass()));
3820 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003821 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003822 }
3823
3824 ObjCImplementationDecl *Impl = Iface->getImplementation();
3825 if (!Impl) {
3826 // We haven't imported an implementation yet. Create a new @implementation
3827 // now.
3828 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3829 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003830 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003831 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003832 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003833 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003834 Importer.Import(D->getIvarLBraceLoc()),
3835 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003836
3837 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3838 DeclContext *LexicalDC
3839 = Importer.ImportContext(D->getLexicalDeclContext());
3840 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003841 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003842 Impl->setLexicalDeclContext(LexicalDC);
3843 }
3844
3845 // Associate the implementation with the class it implements.
3846 Iface->setImplementation(Impl);
3847 Importer.Imported(D, Iface->getImplementation());
3848 } else {
3849 Importer.Imported(D, Iface->getImplementation());
3850
3851 // Verify that the existing @implementation has the same superclass.
3852 if ((Super && !Impl->getSuperClass()) ||
3853 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003854 (Super && Impl->getSuperClass() &&
3855 !declaresSameEntity(Super->getCanonicalDecl(),
3856 Impl->getSuperClass()))) {
3857 Importer.ToDiag(Impl->getLocation(),
3858 diag::err_odr_objc_superclass_inconsistent)
3859 << Iface->getDeclName();
3860 // FIXME: It would be nice to have the location of the superclass
3861 // below.
3862 if (Impl->getSuperClass())
3863 Importer.ToDiag(Impl->getLocation(),
3864 diag::note_odr_objc_superclass)
3865 << Impl->getSuperClass()->getDeclName();
3866 else
3867 Importer.ToDiag(Impl->getLocation(),
3868 diag::note_odr_objc_missing_superclass);
3869 if (D->getSuperClass())
3870 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003871 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003872 << D->getSuperClass()->getDeclName();
3873 else
3874 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003875 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003876 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003877 }
3878 }
3879
3880 // Import all of the members of this @implementation.
3881 ImportDeclContext(D);
3882
3883 return Impl;
3884}
3885
Douglas Gregora11c4582010-02-17 18:02:10 +00003886Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3887 // Import the major distinguishing characteristics of an @property.
3888 DeclContext *DC, *LexicalDC;
3889 DeclarationName Name;
3890 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003891 NamedDecl *ToD;
3892 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003893 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003894 if (ToD)
3895 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003896
3897 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003898 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003899 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003900 for (auto *FoundDecl : FoundDecls) {
3901 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003902 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003903 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003904 FoundProp->getType())) {
3905 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3906 << Name << D->getType() << FoundProp->getType();
3907 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3908 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003909 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003910 }
3911
3912 // FIXME: Check property attributes, getters, setters, etc.?
3913
3914 // Consider these properties to be equivalent.
3915 Importer.Imported(D, FoundProp);
3916 return FoundProp;
3917 }
3918 }
3919
3920 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003921 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3922 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003923 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003924
3925 // Create the new property.
3926 ObjCPropertyDecl *ToProperty
3927 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3928 Name.getAsIdentifierInfo(),
3929 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003930 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003931 Importer.Import(D->getType()),
3932 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003933 D->getPropertyImplementation());
3934 Importer.Imported(D, ToProperty);
3935 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003936 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003937
3938 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003939 ToProperty->setPropertyAttributesAsWritten(
3940 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003941 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3942 Importer.Import(D->getGetterNameLoc()));
3943 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3944 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003945 ToProperty->setGetterMethodDecl(
3946 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3947 ToProperty->setSetterMethodDecl(
3948 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3949 ToProperty->setPropertyIvarDecl(
3950 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3951 return ToProperty;
3952}
3953
Douglas Gregor14a49e22010-12-07 18:32:03 +00003954Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003955 auto *Property = cast_or_null<ObjCPropertyDecl>(
3956 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003957 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003958 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003959
3960 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3961 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003962 return nullptr;
3963
Douglas Gregor14a49e22010-12-07 18:32:03 +00003964 // Import the lexical declaration context.
3965 DeclContext *LexicalDC = DC;
3966 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3967 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3968 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003969 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003970 }
3971
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003972 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003973 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003974 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003975
3976 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003977 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003978 if (D->getPropertyIvarDecl()) {
3979 Ivar = cast_or_null<ObjCIvarDecl>(
3980 Importer.Import(D->getPropertyIvarDecl()));
3981 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003982 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003983 }
3984
3985 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003986 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3987 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003988 if (!ToImpl) {
3989 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3990 Importer.Import(D->getLocStart()),
3991 Importer.Import(D->getLocation()),
3992 Property,
3993 D->getPropertyImplementation(),
3994 Ivar,
3995 Importer.Import(D->getPropertyIvarDeclLoc()));
3996 ToImpl->setLexicalDeclContext(LexicalDC);
3997 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003998 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003999 } else {
4000 // Check that we have the same kind of property implementation (@synthesize
4001 // vs. @dynamic).
4002 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4003 Importer.ToDiag(ToImpl->getLocation(),
4004 diag::err_odr_objc_property_impl_kind_inconsistent)
4005 << Property->getDeclName()
4006 << (ToImpl->getPropertyImplementation()
4007 == ObjCPropertyImplDecl::Dynamic);
4008 Importer.FromDiag(D->getLocation(),
4009 diag::note_odr_objc_property_impl_kind)
4010 << D->getPropertyDecl()->getDeclName()
4011 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004012 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004013 }
4014
4015 // For @synthesize, check that we have the same
4016 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4017 Ivar != ToImpl->getPropertyIvarDecl()) {
4018 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4019 diag::err_odr_objc_synthesize_ivar_inconsistent)
4020 << Property->getDeclName()
4021 << ToImpl->getPropertyIvarDecl()->getDeclName()
4022 << Ivar->getDeclName();
4023 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4024 diag::note_odr_objc_synthesize_ivar_here)
4025 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004026 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004027 }
4028
4029 // Merge the existing implementation with the new implementation.
4030 Importer.Imported(D, ToImpl);
4031 }
4032
4033 return ToImpl;
4034}
4035
Douglas Gregora082a492010-11-30 19:14:50 +00004036Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4037 // For template arguments, we adopt the translation unit as our declaration
4038 // context. This context will be fixed when the actual template declaration
4039 // is created.
4040
4041 // FIXME: Import default argument.
4042 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4043 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004044 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004045 Importer.Import(D->getLocation()),
4046 D->getDepth(),
4047 D->getIndex(),
4048 Importer.Import(D->getIdentifier()),
4049 D->wasDeclaredWithTypename(),
4050 D->isParameterPack());
4051}
4052
4053Decl *
4054ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4055 // Import the name of this declaration.
4056 DeclarationName Name = Importer.Import(D->getDeclName());
4057 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004058 return nullptr;
4059
Douglas Gregora082a492010-11-30 19:14:50 +00004060 // Import the location of this declaration.
4061 SourceLocation Loc = Importer.Import(D->getLocation());
4062
4063 // Import the type of this declaration.
4064 QualType T = Importer.Import(D->getType());
4065 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004066 return nullptr;
4067
Douglas Gregora082a492010-11-30 19:14:50 +00004068 // Import type-source information.
4069 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4070 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004071 return nullptr;
4072
Douglas Gregora082a492010-11-30 19:14:50 +00004073 // FIXME: Import default argument.
4074
4075 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4076 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004077 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004078 Loc, D->getDepth(), D->getPosition(),
4079 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004080 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004081}
4082
4083Decl *
4084ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4085 // Import the name of this declaration.
4086 DeclarationName Name = Importer.Import(D->getDeclName());
4087 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004088 return nullptr;
4089
Douglas Gregora082a492010-11-30 19:14:50 +00004090 // Import the location of this declaration.
4091 SourceLocation Loc = Importer.Import(D->getLocation());
4092
4093 // Import template parameters.
4094 TemplateParameterList *TemplateParams
4095 = ImportTemplateParameterList(D->getTemplateParameters());
4096 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004097 return nullptr;
4098
Douglas Gregora082a492010-11-30 19:14:50 +00004099 // FIXME: Import default argument.
4100
4101 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4102 Importer.getToContext().getTranslationUnitDecl(),
4103 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004104 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004105 Name.getAsIdentifierInfo(),
4106 TemplateParams);
4107}
4108
Gabor Marton9581c332018-05-23 13:53:36 +00004109// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4110// it has any definition in the redecl chain.
4111static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4112 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4113 if (!ToTemplatedDef)
4114 return nullptr;
4115 ClassTemplateDecl *TemplateWithDef =
4116 ToTemplatedDef->getDescribedClassTemplate();
4117 return TemplateWithDef;
4118}
4119
Douglas Gregora082a492010-11-30 19:14:50 +00004120Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4121 // If this record has a definition in the translation unit we're coming from,
4122 // but this particular declaration is not that definition, import the
4123 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004124 auto *Definition =
4125 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004126 if (Definition && Definition != D->getTemplatedDecl()) {
4127 Decl *ImportedDef
4128 = Importer.Import(Definition->getDescribedClassTemplate());
4129 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004130 return nullptr;
4131
Douglas Gregora082a492010-11-30 19:14:50 +00004132 return Importer.Imported(D, ImportedDef);
4133 }
Gabor Marton9581c332018-05-23 13:53:36 +00004134
Douglas Gregora082a492010-11-30 19:14:50 +00004135 // Import the major distinguishing characteristics of this class template.
4136 DeclContext *DC, *LexicalDC;
4137 DeclarationName Name;
4138 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004139 NamedDecl *ToD;
4140 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004141 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004142 if (ToD)
4143 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004144
Douglas Gregora082a492010-11-30 19:14:50 +00004145 // We may already have a template of the same name; try to find and match it.
4146 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004147 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004148 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004149 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004150 for (auto *FoundDecl : FoundDecls) {
4151 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004152 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004153
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004154 Decl *Found = FoundDecl;
4155 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004156
4157 // The class to be imported is a definition.
4158 if (D->isThisDeclarationADefinition()) {
4159 // Lookup will find the fwd decl only if that is more recent than the
4160 // definition. So, try to get the definition if that is available in
4161 // the redecl chain.
4162 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4163 if (!TemplateWithDef)
4164 continue;
4165 FoundTemplate = TemplateWithDef; // Continue with the definition.
4166 }
4167
Douglas Gregora082a492010-11-30 19:14:50 +00004168 if (IsStructuralMatch(D, FoundTemplate)) {
4169 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004170
Gabor Marton9581c332018-05-23 13:53:36 +00004171 Importer.Imported(D->getTemplatedDecl(),
Douglas Gregora082a492010-11-30 19:14:50 +00004172 FoundTemplate->getTemplatedDecl());
4173 return Importer.Imported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004174 }
Douglas Gregora082a492010-11-30 19:14:50 +00004175 }
Gabor Marton9581c332018-05-23 13:53:36 +00004176
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004177 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004178 }
Gabor Marton9581c332018-05-23 13:53:36 +00004179
Douglas Gregora082a492010-11-30 19:14:50 +00004180 if (!ConflictingDecls.empty()) {
4181 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004182 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004183 ConflictingDecls.size());
4184 }
Gabor Marton9581c332018-05-23 13:53:36 +00004185
Douglas Gregora082a492010-11-30 19:14:50 +00004186 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004187 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004188 }
4189
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004190 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4191
Douglas Gregora082a492010-11-30 19:14:50 +00004192 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004193 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4194 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004195 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004196 return nullptr;
4197
4198 // Resolve possible cyclic import.
4199 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4200 return AlreadyImported;
4201
Douglas Gregora082a492010-11-30 19:14:50 +00004202 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004203 TemplateParameterList *TemplateParams =
4204 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004205 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004206 return nullptr;
4207
Douglas Gregora082a492010-11-30 19:14:50 +00004208 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4209 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004210 ToTemplated);
4211 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004212
4213 D2->setAccess(D->getAccess());
4214 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004215 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004216
4217 // Note the relationship between the class templates.
4218 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004219 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004220
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004221 if (FromTemplated->isCompleteDefinition() &&
4222 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004223 // FIXME: Import definition!
4224 }
4225
4226 return D2;
4227}
4228
Douglas Gregore2e50d332010-12-01 01:36:18 +00004229Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4230 ClassTemplateSpecializationDecl *D) {
4231 // If this record has a definition in the translation unit we're coming from,
4232 // but this particular declaration is not that definition, import the
4233 // definition and map to that.
4234 TagDecl *Definition = D->getDefinition();
4235 if (Definition && Definition != D) {
4236 Decl *ImportedDef = Importer.Import(Definition);
4237 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004238 return nullptr;
4239
Douglas Gregore2e50d332010-12-01 01:36:18 +00004240 return Importer.Imported(D, ImportedDef);
4241 }
4242
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004243 auto *ClassTemplate =
4244 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004245 D->getSpecializedTemplate()));
4246 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004247 return nullptr;
4248
Douglas Gregore2e50d332010-12-01 01:36:18 +00004249 // Import the context of this declaration.
4250 DeclContext *DC = ClassTemplate->getDeclContext();
4251 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004252 return nullptr;
4253
Douglas Gregore2e50d332010-12-01 01:36:18 +00004254 DeclContext *LexicalDC = DC;
4255 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4256 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4257 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004258 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004259 }
4260
4261 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004262 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4263 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004264
4265 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004266 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004267 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4268 D->getTemplateArgs().size(),
4269 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004270 return nullptr;
4271
Douglas Gregore2e50d332010-12-01 01:36:18 +00004272 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004273 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004274 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004275 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004276 if (D2) {
4277 // We already have a class template specialization with these template
4278 // arguments.
4279
4280 // FIXME: Check for specialization vs. instantiation errors.
4281
4282 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004283 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004284 // The record types structurally match, or the "from" translation
4285 // unit only had a forward declaration anyway; call it the same
4286 // function.
4287 return Importer.Imported(D, FoundDef);
4288 }
4289 }
4290 } else {
4291 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004292 if (auto *PartialSpec =
4293 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004294 // Import TemplateArgumentListInfo
4295 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004296 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4297 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004298 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004299
4300 QualType CanonInjType = Importer.Import(
4301 PartialSpec->getInjectedSpecializationType());
4302 if (CanonInjType.isNull())
4303 return nullptr;
4304 CanonInjType = CanonInjType.getCanonicalType();
4305
4306 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4307 PartialSpec->getTemplateParameters());
4308 if (!ToTPList && PartialSpec->getTemplateParameters())
4309 return nullptr;
4310
4311 D2 = ClassTemplatePartialSpecializationDecl::Create(
4312 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4313 ToTPList, ClassTemplate,
4314 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4315 ToTAInfo, CanonInjType, nullptr);
4316
4317 } else {
4318 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4319 D->getTagKind(), DC,
4320 StartLoc, IdLoc,
4321 ClassTemplate,
4322 TemplateArgs,
4323 /*PrevDecl=*/nullptr);
4324 }
4325
Douglas Gregore2e50d332010-12-01 01:36:18 +00004326 D2->setSpecializationKind(D->getSpecializationKind());
4327
4328 // Add this specialization to the class template.
4329 ClassTemplate->AddSpecialization(D2, InsertPos);
4330
4331 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004332 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004333
4334 Importer.Imported(D, D2);
4335
4336 if (auto *TSI = D->getTypeAsWritten()) {
4337 TypeSourceInfo *TInfo = Importer.Import(TSI);
4338 if (!TInfo)
4339 return nullptr;
4340 D2->setTypeAsWritten(TInfo);
4341 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4342 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4343 }
4344
4345 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4346 if (POI.isValid())
4347 D2->setPointOfInstantiation(POI);
4348 else if (D->getPointOfInstantiation().isValid())
4349 return nullptr;
4350
4351 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4352
Gabor Martonb14056b2018-05-25 11:21:24 +00004353 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004354 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004355
4356 // Add to the DC only if it was an explicit specialization/instantiation.
4357 if (D2->isExplicitInstantiationOrSpecialization()) {
4358 LexicalDC->addDeclInternal(D2);
4359 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004360 }
4361 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004362 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004363 return nullptr;
4364
Douglas Gregore2e50d332010-12-01 01:36:18 +00004365 return D2;
4366}
4367
Larisse Voufo39a1e502013-08-06 01:03:05 +00004368Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4369 // If this variable has a definition in the translation unit we're coming
4370 // from,
4371 // but this particular declaration is not that definition, import the
4372 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004373 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004374 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4375 if (Definition && Definition != D->getTemplatedDecl()) {
4376 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4377 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004378 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004379
4380 return Importer.Imported(D, ImportedDef);
4381 }
4382
4383 // Import the major distinguishing characteristics of this variable template.
4384 DeclContext *DC, *LexicalDC;
4385 DeclarationName Name;
4386 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004387 NamedDecl *ToD;
4388 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004389 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004390 if (ToD)
4391 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004392
4393 // We may already have a template of the same name; try to find and match it.
4394 assert(!DC->isFunctionOrMethod() &&
4395 "Variable templates cannot be declared at function scope");
4396 SmallVector<NamedDecl *, 4> ConflictingDecls;
4397 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004398 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004399 for (auto *FoundDecl : FoundDecls) {
4400 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004401 continue;
4402
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004403 Decl *Found = FoundDecl;
4404 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004405 if (IsStructuralMatch(D, FoundTemplate)) {
4406 // The variable templates structurally match; call it the same template.
4407 Importer.Imported(D->getTemplatedDecl(),
4408 FoundTemplate->getTemplatedDecl());
4409 return Importer.Imported(D, FoundTemplate);
4410 }
4411 }
4412
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004413 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004414 }
4415
4416 if (!ConflictingDecls.empty()) {
4417 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4418 ConflictingDecls.data(),
4419 ConflictingDecls.size());
4420 }
4421
4422 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004423 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004424
4425 VarDecl *DTemplated = D->getTemplatedDecl();
4426
4427 // Import the type.
4428 QualType T = Importer.Import(DTemplated->getType());
4429 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004430 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004431
4432 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004433 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4434 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004435 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004436
4437 // Create the variable template declaration itself.
4438 TemplateParameterList *TemplateParams =
4439 ImportTemplateParameterList(D->getTemplateParameters());
4440 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004441 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004442
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004443 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4444 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4445 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004446
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004447 ToVarTD->setAccess(D->getAccess());
4448 ToVarTD->setLexicalDeclContext(LexicalDC);
4449 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004450
4451 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004452 Importer.Imported(D, ToVarTD);
4453 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004454
4455 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004456 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004457 // FIXME: Import definition!
4458 }
4459
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004460 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004461}
4462
4463Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4464 VarTemplateSpecializationDecl *D) {
4465 // If this record has a definition in the translation unit we're coming from,
4466 // but this particular declaration is not that definition, import the
4467 // definition and map to that.
4468 VarDecl *Definition = D->getDefinition();
4469 if (Definition && Definition != D) {
4470 Decl *ImportedDef = Importer.Import(Definition);
4471 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004472 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004473
4474 return Importer.Imported(D, ImportedDef);
4475 }
4476
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004477 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004478 Importer.Import(D->getSpecializedTemplate()));
4479 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004480 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004481
4482 // Import the context of this declaration.
4483 DeclContext *DC = VarTemplate->getDeclContext();
4484 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004485 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004486
4487 DeclContext *LexicalDC = DC;
4488 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4489 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4490 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004491 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004492 }
4493
4494 // Import the location of this declaration.
4495 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4496 SourceLocation IdLoc = Importer.Import(D->getLocation());
4497
4498 // Import template arguments.
4499 SmallVector<TemplateArgument, 2> TemplateArgs;
4500 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4501 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004502 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004503
4504 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004505 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004506 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004507 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004508 if (D2) {
4509 // We already have a variable template specialization with these template
4510 // arguments.
4511
4512 // FIXME: Check for specialization vs. instantiation errors.
4513
4514 if (VarDecl *FoundDef = D2->getDefinition()) {
4515 if (!D->isThisDeclarationADefinition() ||
4516 IsStructuralMatch(D, FoundDef)) {
4517 // The record types structurally match, or the "from" translation
4518 // unit only had a forward declaration anyway; call it the same
4519 // variable.
4520 return Importer.Imported(D, FoundDef);
4521 }
4522 }
4523 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004524 // Import the type.
4525 QualType T = Importer.Import(D->getType());
4526 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004527 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004528
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004529 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4530 if (D->getTypeSourceInfo() && !TInfo)
4531 return nullptr;
4532
4533 TemplateArgumentListInfo ToTAInfo;
4534 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4535 return nullptr;
4536
4537 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004538 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004539 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4540 // Import TemplateArgumentListInfo
4541 TemplateArgumentListInfo ArgInfos;
4542 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4543 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4544 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4545 return nullptr;
4546
4547 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4548 FromPartial->getTemplateParameters());
4549 if (!ToTPList)
4550 return nullptr;
4551
4552 auto *ToPartial = PartVarSpecDecl::Create(
4553 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4554 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4555
4556 auto *FromInst = FromPartial->getInstantiatedFromMember();
4557 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4558 if (FromInst && !ToInst)
4559 return nullptr;
4560
4561 ToPartial->setInstantiatedFromMember(ToInst);
4562 if (FromPartial->isMemberSpecialization())
4563 ToPartial->setMemberSpecialization();
4564
4565 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004566 } else { // Full specialization
4567 D2 = VarTemplateSpecializationDecl::Create(
4568 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4569 D->getStorageClass(), TemplateArgs);
4570 }
4571
4572 SourceLocation POI = D->getPointOfInstantiation();
4573 if (POI.isValid())
4574 D2->setPointOfInstantiation(Importer.Import(POI));
4575
Larisse Voufo39a1e502013-08-06 01:03:05 +00004576 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004577 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004578
4579 // Add this specialization to the class template.
4580 VarTemplate->AddSpecialization(D2, InsertPos);
4581
4582 // Import the qualifier, if any.
4583 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4584
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004585 if (D->isConstexpr())
4586 D2->setConstexpr(true);
4587
Larisse Voufo39a1e502013-08-06 01:03:05 +00004588 // Add the specialization to this context.
4589 D2->setLexicalDeclContext(LexicalDC);
4590 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004591
4592 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004593 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004594
Larisse Voufo39a1e502013-08-06 01:03:05 +00004595 Importer.Imported(D, D2);
4596
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004597 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4598 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4599 // we have to check the declaration for initializer - otherwise, it won't be
4600 // imported.
4601 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4602 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004603 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004604
4605 return D2;
4606}
4607
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004608Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4609 DeclContext *DC, *LexicalDC;
4610 DeclarationName Name;
4611 SourceLocation Loc;
4612 NamedDecl *ToD;
4613
4614 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4615 return nullptr;
4616
4617 if (ToD)
4618 return ToD;
4619
4620 // Try to find a function in our own ("to") context with the same name, same
4621 // type, and in the same context as the function we're importing.
4622 if (!LexicalDC->isFunctionOrMethod()) {
4623 unsigned IDNS = Decl::IDNS_Ordinary;
4624 SmallVector<NamedDecl *, 2> FoundDecls;
4625 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004626 for (auto *FoundDecl : FoundDecls) {
4627 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004628 continue;
4629
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004630 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004631 if (FoundFunction->hasExternalFormalLinkage() &&
4632 D->hasExternalFormalLinkage()) {
4633 if (IsStructuralMatch(D, FoundFunction)) {
4634 Importer.Imported(D, FoundFunction);
4635 // FIXME: Actually try to merge the body and other attributes.
4636 return FoundFunction;
4637 }
4638 }
4639 }
4640 }
4641 }
4642
4643 TemplateParameterList *Params =
4644 ImportTemplateParameterList(D->getTemplateParameters());
4645 if (!Params)
4646 return nullptr;
4647
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004648 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004649 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4650 if (!TemplatedFD)
4651 return nullptr;
4652
4653 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4654 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4655
4656 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4657 ToFunc->setAccess(D->getAccess());
4658 ToFunc->setLexicalDeclContext(LexicalDC);
4659 Importer.Imported(D, ToFunc);
4660
4661 LexicalDC->addDeclInternal(ToFunc);
4662 return ToFunc;
4663}
4664
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004665//----------------------------------------------------------------------------
4666// Import Statements
4667//----------------------------------------------------------------------------
4668
Sean Callanan59721b32015-04-28 18:41:46 +00004669DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4670 if (DG.isNull())
4671 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4672 size_t NumDecls = DG.end() - DG.begin();
4673 SmallVector<Decl *, 1> ToDecls(NumDecls);
4674 auto &_Importer = this->Importer;
4675 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4676 [&_Importer](Decl *D) -> Decl * {
4677 return _Importer.Import(D);
4678 });
4679 return DeclGroupRef::Create(Importer.getToContext(),
4680 ToDecls.begin(),
4681 NumDecls);
4682}
4683
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004684Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4685 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4686 << S->getStmtClassName();
4687 return nullptr;
4688}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004689
4690Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4691 SmallVector<IdentifierInfo *, 4> Names;
4692 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4693 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004694 // ToII is nullptr when no symbolic name is given for output operand
4695 // see ParseStmtAsm::ParseAsmOperandsOpt
4696 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004697 return nullptr;
4698 Names.push_back(ToII);
4699 }
4700 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4701 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004702 // ToII is nullptr when no symbolic name is given for input operand
4703 // see ParseStmtAsm::ParseAsmOperandsOpt
4704 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004705 return nullptr;
4706 Names.push_back(ToII);
4707 }
4708
4709 SmallVector<StringLiteral *, 4> Clobbers;
4710 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004711 auto *Clobber = cast_or_null<StringLiteral>(
4712 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004713 if (!Clobber)
4714 return nullptr;
4715 Clobbers.push_back(Clobber);
4716 }
4717
4718 SmallVector<StringLiteral *, 4> Constraints;
4719 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004720 auto *Output = cast_or_null<StringLiteral>(
4721 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004722 if (!Output)
4723 return nullptr;
4724 Constraints.push_back(Output);
4725 }
4726
4727 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004728 auto *Input = cast_or_null<StringLiteral>(
4729 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004730 if (!Input)
4731 return nullptr;
4732 Constraints.push_back(Input);
4733 }
4734
4735 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004736 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004737 return nullptr;
4738
Aleksei Sidorina693b372016-09-28 10:16:56 +00004739 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004740 return nullptr;
4741
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004742 auto *AsmStr = cast_or_null<StringLiteral>(
4743 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004744 if (!AsmStr)
4745 return nullptr;
4746
4747 return new (Importer.getToContext()) GCCAsmStmt(
4748 Importer.getToContext(),
4749 Importer.Import(S->getAsmLoc()),
4750 S->isSimple(),
4751 S->isVolatile(),
4752 S->getNumOutputs(),
4753 S->getNumInputs(),
4754 Names.data(),
4755 Constraints.data(),
4756 Exprs.data(),
4757 AsmStr,
4758 S->getNumClobbers(),
4759 Clobbers.data(),
4760 Importer.Import(S->getRParenLoc()));
4761}
4762
Sean Callanan59721b32015-04-28 18:41:46 +00004763Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4764 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004765 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004766 if (!ToD)
4767 return nullptr;
4768 }
4769 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4770 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4771 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4772}
4773
4774Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4775 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4776 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4777 S->hasLeadingEmptyMacro());
4778}
4779
4780Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004781 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004782
4783 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004784 return nullptr;
4785
Sean Callanan59721b32015-04-28 18:41:46 +00004786 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4787 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004788 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4789 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004790}
4791
4792Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4793 Expr *ToLHS = Importer.Import(S->getLHS());
4794 if (!ToLHS)
4795 return nullptr;
4796 Expr *ToRHS = Importer.Import(S->getRHS());
4797 if (!ToRHS && S->getRHS())
4798 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004799 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4800 if (!ToSubStmt && S->getSubStmt())
4801 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004802 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4803 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4804 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004805 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004806 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4807 ToStmt->setSubStmt(ToSubStmt);
4808 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004809}
4810
4811Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4812 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4813 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4814 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4815 if (!ToSubStmt && S->getSubStmt())
4816 return nullptr;
4817 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4818 ToSubStmt);
4819}
4820
4821Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4822 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004823 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004824 if (!ToLabelDecl && S->getDecl())
4825 return nullptr;
4826 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4827 if (!ToSubStmt && S->getSubStmt())
4828 return nullptr;
4829 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4830 ToSubStmt);
4831}
4832
4833Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4834 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4835 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4836 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00004837 if (ImportContainerChecked(FromAttrs, ToAttrs))
4838 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004839 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4840 if (!ToSubStmt && S->getSubStmt())
4841 return nullptr;
4842 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4843 ToAttrs, ToSubStmt);
4844}
4845
4846Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4847 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004848 Stmt *ToInit = Importer.Import(S->getInit());
4849 if (!ToInit && S->getInit())
4850 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004851 VarDecl *ToConditionVariable = nullptr;
4852 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4853 ToConditionVariable =
4854 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4855 if (!ToConditionVariable)
4856 return nullptr;
4857 }
4858 Expr *ToCondition = Importer.Import(S->getCond());
4859 if (!ToCondition && S->getCond())
4860 return nullptr;
4861 Stmt *ToThenStmt = Importer.Import(S->getThen());
4862 if (!ToThenStmt && S->getThen())
4863 return nullptr;
4864 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4865 Stmt *ToElseStmt = Importer.Import(S->getElse());
4866 if (!ToElseStmt && S->getElse())
4867 return nullptr;
4868 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004869 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004870 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004871 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004872 ToCondition, ToThenStmt,
4873 ToElseLoc, ToElseStmt);
4874}
4875
4876Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004877 Stmt *ToInit = Importer.Import(S->getInit());
4878 if (!ToInit && S->getInit())
4879 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004880 VarDecl *ToConditionVariable = nullptr;
4881 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4882 ToConditionVariable =
4883 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4884 if (!ToConditionVariable)
4885 return nullptr;
4886 }
4887 Expr *ToCondition = Importer.Import(S->getCond());
4888 if (!ToCondition && S->getCond())
4889 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004890 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004891 Importer.getToContext(), ToInit,
4892 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004893 Stmt *ToBody = Importer.Import(S->getBody());
4894 if (!ToBody && S->getBody())
4895 return nullptr;
4896 ToStmt->setBody(ToBody);
4897 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4898 // Now we have to re-chain the cases.
4899 SwitchCase *LastChainedSwitchCase = nullptr;
4900 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4901 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004902 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004903 if (!ToSC)
4904 return nullptr;
4905 if (LastChainedSwitchCase)
4906 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4907 else
4908 ToStmt->setSwitchCaseList(ToSC);
4909 LastChainedSwitchCase = ToSC;
4910 }
4911 return ToStmt;
4912}
4913
4914Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4915 VarDecl *ToConditionVariable = nullptr;
4916 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4917 ToConditionVariable =
4918 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4919 if (!ToConditionVariable)
4920 return nullptr;
4921 }
4922 Expr *ToCondition = Importer.Import(S->getCond());
4923 if (!ToCondition && S->getCond())
4924 return nullptr;
4925 Stmt *ToBody = Importer.Import(S->getBody());
4926 if (!ToBody && S->getBody())
4927 return nullptr;
4928 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4929 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4930 ToConditionVariable,
4931 ToCondition, ToBody,
4932 ToWhileLoc);
4933}
4934
4935Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4936 Stmt *ToBody = Importer.Import(S->getBody());
4937 if (!ToBody && S->getBody())
4938 return nullptr;
4939 Expr *ToCondition = Importer.Import(S->getCond());
4940 if (!ToCondition && S->getCond())
4941 return nullptr;
4942 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4943 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4944 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4945 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4946 ToDoLoc, ToWhileLoc,
4947 ToRParenLoc);
4948}
4949
4950Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4951 Stmt *ToInit = Importer.Import(S->getInit());
4952 if (!ToInit && S->getInit())
4953 return nullptr;
4954 Expr *ToCondition = Importer.Import(S->getCond());
4955 if (!ToCondition && S->getCond())
4956 return nullptr;
4957 VarDecl *ToConditionVariable = nullptr;
4958 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4959 ToConditionVariable =
4960 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4961 if (!ToConditionVariable)
4962 return nullptr;
4963 }
4964 Expr *ToInc = Importer.Import(S->getInc());
4965 if (!ToInc && S->getInc())
4966 return nullptr;
4967 Stmt *ToBody = Importer.Import(S->getBody());
4968 if (!ToBody && S->getBody())
4969 return nullptr;
4970 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4971 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4972 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4973 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4974 ToInit, ToCondition,
4975 ToConditionVariable,
4976 ToInc, ToBody,
4977 ToForLoc, ToLParenLoc,
4978 ToRParenLoc);
4979}
4980
4981Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4982 LabelDecl *ToLabel = nullptr;
4983 if (LabelDecl *FromLabel = S->getLabel()) {
4984 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4985 if (!ToLabel)
4986 return nullptr;
4987 }
4988 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4989 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4990 return new (Importer.getToContext()) GotoStmt(ToLabel,
4991 ToGotoLoc, ToLabelLoc);
4992}
4993
4994Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4995 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4996 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4997 Expr *ToTarget = Importer.Import(S->getTarget());
4998 if (!ToTarget && S->getTarget())
4999 return nullptr;
5000 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5001 ToTarget);
5002}
5003
5004Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5005 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5006 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5007}
5008
5009Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5010 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5011 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5012}
5013
5014Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5015 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5016 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5017 if (!ToRetExpr && S->getRetValue())
5018 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005019 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
5020 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00005021 if (!ToNRVOCandidate && NRVOCandidate)
5022 return nullptr;
5023 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5024 ToNRVOCandidate);
5025}
5026
5027Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5028 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5029 VarDecl *ToExceptionDecl = nullptr;
5030 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5031 ToExceptionDecl =
5032 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5033 if (!ToExceptionDecl)
5034 return nullptr;
5035 }
5036 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5037 if (!ToHandlerBlock && S->getHandlerBlock())
5038 return nullptr;
5039 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5040 ToExceptionDecl,
5041 ToHandlerBlock);
5042}
5043
5044Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5045 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5046 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5047 if (!ToTryBlock && S->getTryBlock())
5048 return nullptr;
5049 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5050 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5051 CXXCatchStmt *FromHandler = S->getHandler(HI);
5052 if (Stmt *ToHandler = Importer.Import(FromHandler))
5053 ToHandlers[HI] = ToHandler;
5054 else
5055 return nullptr;
5056 }
5057 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5058 ToHandlers);
5059}
5060
5061Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005062 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005063 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5064 if (!ToRange && S->getRangeStmt())
5065 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005066 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005067 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5068 if (!ToBegin && S->getBeginStmt())
5069 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005070 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005071 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5072 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005073 return nullptr;
5074 Expr *ToCond = Importer.Import(S->getCond());
5075 if (!ToCond && S->getCond())
5076 return nullptr;
5077 Expr *ToInc = Importer.Import(S->getInc());
5078 if (!ToInc && S->getInc())
5079 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005080 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005081 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5082 if (!ToLoopVar && S->getLoopVarStmt())
5083 return nullptr;
5084 Stmt *ToBody = Importer.Import(S->getBody());
5085 if (!ToBody && S->getBody())
5086 return nullptr;
5087 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005088 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005089 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5090 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005091 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005092 ToCond, ToInc,
5093 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005094 ToForLoc, ToCoawaitLoc,
5095 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005096}
5097
5098Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5099 Stmt *ToElem = Importer.Import(S->getElement());
5100 if (!ToElem && S->getElement())
5101 return nullptr;
5102 Expr *ToCollect = Importer.Import(S->getCollection());
5103 if (!ToCollect && S->getCollection())
5104 return nullptr;
5105 Stmt *ToBody = Importer.Import(S->getBody());
5106 if (!ToBody && S->getBody())
5107 return nullptr;
5108 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5109 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5110 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5111 ToCollect,
5112 ToBody, ToForLoc,
5113 ToRParenLoc);
5114}
5115
5116Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5117 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5118 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5119 VarDecl *ToExceptionDecl = nullptr;
5120 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5121 ToExceptionDecl =
5122 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5123 if (!ToExceptionDecl)
5124 return nullptr;
5125 }
5126 Stmt *ToBody = Importer.Import(S->getCatchBody());
5127 if (!ToBody && S->getCatchBody())
5128 return nullptr;
5129 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5130 ToRParenLoc,
5131 ToExceptionDecl,
5132 ToBody);
5133}
5134
5135Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5136 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5137 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5138 if (!ToAtFinallyStmt && S->getFinallyBody())
5139 return nullptr;
5140 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5141 ToAtFinallyStmt);
5142}
5143
5144Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5145 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5146 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5147 if (!ToAtTryStmt && S->getTryBody())
5148 return nullptr;
5149 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5150 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5151 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5152 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5153 ToCatchStmts[CI] = ToCatchStmt;
5154 else
5155 return nullptr;
5156 }
5157 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5158 if (!ToAtFinallyStmt && S->getFinallyStmt())
5159 return nullptr;
5160 return ObjCAtTryStmt::Create(Importer.getToContext(),
5161 ToAtTryLoc, ToAtTryStmt,
5162 ToCatchStmts.begin(), ToCatchStmts.size(),
5163 ToAtFinallyStmt);
5164}
5165
5166Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5167 (ObjCAtSynchronizedStmt *S) {
5168 SourceLocation ToAtSynchronizedLoc =
5169 Importer.Import(S->getAtSynchronizedLoc());
5170 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5171 if (!ToSynchExpr && S->getSynchExpr())
5172 return nullptr;
5173 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5174 if (!ToSynchBody && S->getSynchBody())
5175 return nullptr;
5176 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5177 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5178}
5179
5180Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5181 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5182 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5183 if (!ToThrow && S->getThrowExpr())
5184 return nullptr;
5185 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5186}
5187
5188Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5189 (ObjCAutoreleasePoolStmt *S) {
5190 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5191 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5192 if (!ToSubStmt && S->getSubStmt())
5193 return nullptr;
5194 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5195 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005196}
5197
5198//----------------------------------------------------------------------------
5199// Import Expressions
5200//----------------------------------------------------------------------------
5201Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5202 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5203 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005204 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005205}
5206
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005207Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5208 QualType T = Importer.Import(E->getType());
5209 if (T.isNull())
5210 return nullptr;
5211
5212 Expr *SubExpr = Importer.Import(E->getSubExpr());
5213 if (!SubExpr && E->getSubExpr())
5214 return nullptr;
5215
5216 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5217 if (!TInfo)
5218 return nullptr;
5219
5220 return new (Importer.getToContext()) VAArgExpr(
5221 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5222 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5223}
5224
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005225Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5226 QualType T = Importer.Import(E->getType());
5227 if (T.isNull())
5228 return nullptr;
5229
5230 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005231 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005232}
5233
5234Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5235 QualType T = Importer.Import(E->getType());
5236 if (T.isNull())
5237 return nullptr;
5238
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005239 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005240 if (!SL && E->getFunctionName())
5241 return nullptr;
5242
5243 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005244 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005245}
5246
Douglas Gregor52f820e2010-02-19 01:17:02 +00005247Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005248 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005249 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005250 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005251
Craig Topper36250ad2014-05-12 05:36:57 +00005252 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005253 if (E->getDecl() != E->getFoundDecl()) {
5254 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5255 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005256 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005257 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005258
5259 QualType T = Importer.Import(E->getType());
5260 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005261 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005262
Aleksei Sidorina693b372016-09-28 10:16:56 +00005263 TemplateArgumentListInfo ToTAInfo;
5264 TemplateArgumentListInfo *ResInfo = nullptr;
5265 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005266 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5267 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005268 ResInfo = &ToTAInfo;
5269 }
5270
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005271 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5272 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005273 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005274 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005275 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005276 Importer.Import(E->getLocation()),
5277 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005278 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005279 if (E->hadMultipleCandidates())
5280 DRE->setHadMultipleCandidates(true);
5281 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005282}
5283
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005284Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5285 QualType T = Importer.Import(E->getType());
5286 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005287 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005288
5289 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5290}
5291
5292ASTNodeImporter::Designator
5293ASTNodeImporter::ImportDesignator(const Designator &D) {
5294 if (D.isFieldDesignator()) {
5295 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5296 // Caller checks for import error
5297 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5298 Importer.Import(D.getFieldLoc()));
5299 }
5300 if (D.isArrayDesignator())
5301 return Designator(D.getFirstExprIndex(),
5302 Importer.Import(D.getLBracketLoc()),
5303 Importer.Import(D.getRBracketLoc()));
5304
5305 assert(D.isArrayRangeDesignator());
5306 return Designator(D.getFirstExprIndex(),
5307 Importer.Import(D.getLBracketLoc()),
5308 Importer.Import(D.getEllipsisLoc()),
5309 Importer.Import(D.getRBracketLoc()));
5310}
5311
5312
5313Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005314 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005315 if (!Init)
5316 return nullptr;
5317
5318 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5319 // List elements from the second, the first is Init itself
5320 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005321 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005322 IndexExprs[I - 1] = Arg;
5323 else
5324 return nullptr;
5325 }
5326
5327 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005328 llvm::transform(DIE->designators(), Designators.begin(),
5329 [this](const Designator &D) -> Designator {
5330 return ImportDesignator(D);
5331 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005332
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005333 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005334 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005335 return nullptr;
5336
5337 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005338 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005339 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5340 DIE->usesGNUSyntax(), Init);
5341}
5342
5343Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5344 QualType T = Importer.Import(E->getType());
5345 if (T.isNull())
5346 return nullptr;
5347
5348 return new (Importer.getToContext())
5349 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5350}
5351
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005352Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5353 QualType T = Importer.Import(E->getType());
5354 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005355 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005356
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005357 return IntegerLiteral::Create(Importer.getToContext(),
5358 E->getValue(), T,
5359 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005360}
5361
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005362Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5363 QualType T = Importer.Import(E->getType());
5364 if (T.isNull())
5365 return nullptr;
5366
5367 return FloatingLiteral::Create(Importer.getToContext(),
5368 E->getValue(), E->isExact(), T,
5369 Importer.Import(E->getLocation()));
5370}
5371
Douglas Gregor623421d2010-02-18 02:21:22 +00005372Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5373 QualType T = Importer.Import(E->getType());
5374 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005375 return nullptr;
5376
Douglas Gregorfb65e592011-07-27 05:40:30 +00005377 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5378 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005379 Importer.Import(E->getLocation()));
5380}
5381
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005382Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5383 QualType T = Importer.Import(E->getType());
5384 if (T.isNull())
5385 return nullptr;
5386
5387 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5388 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5389
5390 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5391 E->getKind(), E->isPascal(), T,
5392 Locations.data(), Locations.size());
5393}
5394
5395Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5396 QualType T = Importer.Import(E->getType());
5397 if (T.isNull())
5398 return nullptr;
5399
5400 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5401 if (!TInfo)
5402 return nullptr;
5403
5404 Expr *Init = Importer.Import(E->getInitializer());
5405 if (!Init)
5406 return nullptr;
5407
5408 return new (Importer.getToContext()) CompoundLiteralExpr(
5409 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5410 Init, E->isFileScope());
5411}
5412
5413Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5414 QualType T = Importer.Import(E->getType());
5415 if (T.isNull())
5416 return nullptr;
5417
5418 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5419 if (ImportArrayChecked(
5420 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5421 Exprs.begin()))
5422 return nullptr;
5423
5424 return new (Importer.getToContext()) AtomicExpr(
5425 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5426 Importer.Import(E->getRParenLoc()));
5427}
5428
5429Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5430 QualType T = Importer.Import(E->getType());
5431 if (T.isNull())
5432 return nullptr;
5433
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005434 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005435 if (!ToLabel)
5436 return nullptr;
5437
5438 return new (Importer.getToContext()) AddrLabelExpr(
5439 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5440 ToLabel, T);
5441}
5442
Douglas Gregorc74247e2010-02-19 01:07:06 +00005443Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5444 Expr *SubExpr = Importer.Import(E->getSubExpr());
5445 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005446 return nullptr;
5447
Douglas Gregorc74247e2010-02-19 01:07:06 +00005448 return new (Importer.getToContext())
5449 ParenExpr(Importer.Import(E->getLParen()),
5450 Importer.Import(E->getRParen()),
5451 SubExpr);
5452}
5453
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005454Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5455 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005456 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005457 return nullptr;
5458
5459 return new (Importer.getToContext()) ParenListExpr(
5460 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5461 Exprs, Importer.Import(E->getLParenLoc()));
5462}
5463
5464Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5465 QualType T = Importer.Import(E->getType());
5466 if (T.isNull())
5467 return nullptr;
5468
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005469 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5470 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005471 if (!ToSubStmt && E->getSubStmt())
5472 return nullptr;
5473
5474 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5475 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5476}
5477
Douglas Gregorc74247e2010-02-19 01:07:06 +00005478Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5479 QualType T = Importer.Import(E->getType());
5480 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005481 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005482
5483 Expr *SubExpr = Importer.Import(E->getSubExpr());
5484 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005485 return nullptr;
5486
Aaron Ballmana5038552018-01-09 13:07:03 +00005487 return new (Importer.getToContext()) UnaryOperator(
5488 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5489 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005490}
5491
Aaron Ballmana5038552018-01-09 13:07:03 +00005492Expr *
5493ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005494 QualType ResultType = Importer.Import(E->getType());
5495
5496 if (E->isArgumentType()) {
5497 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5498 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005499 return nullptr;
5500
Peter Collingbournee190dee2011-03-11 19:24:49 +00005501 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5502 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005503 Importer.Import(E->getOperatorLoc()),
5504 Importer.Import(E->getRParenLoc()));
5505 }
5506
5507 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5508 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005509 return nullptr;
5510
Peter Collingbournee190dee2011-03-11 19:24:49 +00005511 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5512 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005513 Importer.Import(E->getOperatorLoc()),
5514 Importer.Import(E->getRParenLoc()));
5515}
5516
Douglas Gregorc74247e2010-02-19 01:07:06 +00005517Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5518 QualType T = Importer.Import(E->getType());
5519 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005520 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005521
5522 Expr *LHS = Importer.Import(E->getLHS());
5523 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005524 return nullptr;
5525
Douglas Gregorc74247e2010-02-19 01:07:06 +00005526 Expr *RHS = Importer.Import(E->getRHS());
5527 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005528 return nullptr;
5529
Douglas Gregorc74247e2010-02-19 01:07:06 +00005530 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005531 T, E->getValueKind(),
5532 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005533 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005534 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005535}
5536
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005537Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5538 QualType T = Importer.Import(E->getType());
5539 if (T.isNull())
5540 return nullptr;
5541
5542 Expr *ToLHS = Importer.Import(E->getLHS());
5543 if (!ToLHS)
5544 return nullptr;
5545
5546 Expr *ToRHS = Importer.Import(E->getRHS());
5547 if (!ToRHS)
5548 return nullptr;
5549
5550 Expr *ToCond = Importer.Import(E->getCond());
5551 if (!ToCond)
5552 return nullptr;
5553
5554 return new (Importer.getToContext()) ConditionalOperator(
5555 ToCond, Importer.Import(E->getQuestionLoc()),
5556 ToLHS, Importer.Import(E->getColonLoc()),
5557 ToRHS, T, E->getValueKind(), E->getObjectKind());
5558}
5559
5560Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5561 BinaryConditionalOperator *E) {
5562 QualType T = Importer.Import(E->getType());
5563 if (T.isNull())
5564 return nullptr;
5565
5566 Expr *Common = Importer.Import(E->getCommon());
5567 if (!Common)
5568 return nullptr;
5569
5570 Expr *Cond = Importer.Import(E->getCond());
5571 if (!Cond)
5572 return nullptr;
5573
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005574 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5575 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005576 if (!OpaqueValue)
5577 return nullptr;
5578
5579 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5580 if (!TrueExpr)
5581 return nullptr;
5582
5583 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5584 if (!FalseExpr)
5585 return nullptr;
5586
5587 return new (Importer.getToContext()) BinaryConditionalOperator(
5588 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5589 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5590 T, E->getValueKind(), E->getObjectKind());
5591}
5592
Aleksei Sidorina693b372016-09-28 10:16:56 +00005593Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5594 QualType T = Importer.Import(E->getType());
5595 if (T.isNull())
5596 return nullptr;
5597
5598 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5599 if (!ToQueried)
5600 return nullptr;
5601
5602 Expr *Dim = Importer.Import(E->getDimensionExpression());
5603 if (!Dim && E->getDimensionExpression())
5604 return nullptr;
5605
5606 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5607 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5608 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5609}
5610
5611Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5612 QualType T = Importer.Import(E->getType());
5613 if (T.isNull())
5614 return nullptr;
5615
5616 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5617 if (!ToQueried)
5618 return nullptr;
5619
5620 return new (Importer.getToContext()) ExpressionTraitExpr(
5621 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5622 E->getValue(), Importer.Import(E->getLocEnd()), T);
5623}
5624
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005625Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5626 QualType T = Importer.Import(E->getType());
5627 if (T.isNull())
5628 return nullptr;
5629
5630 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5631 if (!SourceExpr && E->getSourceExpr())
5632 return nullptr;
5633
5634 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005635 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005636 E->getObjectKind(), SourceExpr);
5637}
5638
Aleksei Sidorina693b372016-09-28 10:16:56 +00005639Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5640 QualType T = Importer.Import(E->getType());
5641 if (T.isNull())
5642 return nullptr;
5643
5644 Expr *ToLHS = Importer.Import(E->getLHS());
5645 if (!ToLHS)
5646 return nullptr;
5647
5648 Expr *ToRHS = Importer.Import(E->getRHS());
5649 if (!ToRHS)
5650 return nullptr;
5651
5652 return new (Importer.getToContext()) ArraySubscriptExpr(
5653 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5654 Importer.Import(E->getRBracketLoc()));
5655}
5656
Douglas Gregorc74247e2010-02-19 01:07:06 +00005657Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5658 QualType T = Importer.Import(E->getType());
5659 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005660 return nullptr;
5661
Douglas Gregorc74247e2010-02-19 01:07:06 +00005662 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5663 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005664 return nullptr;
5665
Douglas Gregorc74247e2010-02-19 01:07:06 +00005666 QualType CompResultType = Importer.Import(E->getComputationResultType());
5667 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005668 return nullptr;
5669
Douglas Gregorc74247e2010-02-19 01:07:06 +00005670 Expr *LHS = Importer.Import(E->getLHS());
5671 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005672 return nullptr;
5673
Douglas Gregorc74247e2010-02-19 01:07:06 +00005674 Expr *RHS = Importer.Import(E->getRHS());
5675 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005676 return nullptr;
5677
Douglas Gregorc74247e2010-02-19 01:07:06 +00005678 return new (Importer.getToContext())
5679 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005680 T, E->getValueKind(),
5681 E->getObjectKind(),
5682 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005683 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005684 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005685}
5686
Aleksei Sidorina693b372016-09-28 10:16:56 +00005687bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5688 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5689 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5690 Path.push_back(Spec);
5691 else
5692 return true;
5693 }
5694 return false;
John McCallcf142162010-08-07 06:22:56 +00005695}
5696
Douglas Gregor98c10182010-02-12 22:17:39 +00005697Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5698 QualType T = Importer.Import(E->getType());
5699 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005700 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005701
5702 Expr *SubExpr = Importer.Import(E->getSubExpr());
5703 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005704 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005705
5706 CXXCastPath BasePath;
5707 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005708 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005709
5710 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005711 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005712}
5713
Aleksei Sidorina693b372016-09-28 10:16:56 +00005714Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005715 QualType T = Importer.Import(E->getType());
5716 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005717 return nullptr;
5718
Douglas Gregor5481d322010-02-19 01:32:14 +00005719 Expr *SubExpr = Importer.Import(E->getSubExpr());
5720 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005721 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005722
5723 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5724 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005725 return nullptr;
5726
John McCallcf142162010-08-07 06:22:56 +00005727 CXXCastPath BasePath;
5728 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005729 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005730
Aleksei Sidorina693b372016-09-28 10:16:56 +00005731 switch (E->getStmtClass()) {
5732 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005733 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005734 return CStyleCastExpr::Create(Importer.getToContext(), T,
5735 E->getValueKind(), E->getCastKind(),
5736 SubExpr, &BasePath, TInfo,
5737 Importer.Import(CCE->getLParenLoc()),
5738 Importer.Import(CCE->getRParenLoc()));
5739 }
5740
5741 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005742 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005743 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5744 E->getValueKind(), TInfo,
5745 E->getCastKind(), SubExpr, &BasePath,
5746 Importer.Import(FCE->getLParenLoc()),
5747 Importer.Import(FCE->getRParenLoc()));
5748 }
5749
5750 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005751 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005752 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5753 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5754 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5755 TInfo, SubExpr);
5756 }
5757 default:
5758 break; // just fall through
5759 }
5760
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005761 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005762 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5763 RParenLoc = Importer.Import(Named->getRParenLoc());
5764 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5765
5766 switch (E->getStmtClass()) {
5767 case Stmt::CXXStaticCastExprClass:
5768 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5769 E->getValueKind(), E->getCastKind(),
5770 SubExpr, &BasePath, TInfo,
5771 ExprLoc, RParenLoc, Brackets);
5772
5773 case Stmt::CXXDynamicCastExprClass:
5774 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5775 E->getValueKind(), E->getCastKind(),
5776 SubExpr, &BasePath, TInfo,
5777 ExprLoc, RParenLoc, Brackets);
5778
5779 case Stmt::CXXReinterpretCastExprClass:
5780 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5781 E->getValueKind(), E->getCastKind(),
5782 SubExpr, &BasePath, TInfo,
5783 ExprLoc, RParenLoc, Brackets);
5784
5785 case Stmt::CXXConstCastExprClass:
5786 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5787 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5788 RParenLoc, Brackets);
5789 default:
5790 llvm_unreachable("Cast expression of unsupported type!");
5791 return nullptr;
5792 }
5793}
5794
5795Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5796 QualType T = Importer.Import(OE->getType());
5797 if (T.isNull())
5798 return nullptr;
5799
5800 SmallVector<OffsetOfNode, 4> Nodes;
5801 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5802 const OffsetOfNode &Node = OE->getComponent(I);
5803
5804 switch (Node.getKind()) {
5805 case OffsetOfNode::Array:
5806 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5807 Node.getArrayExprIndex(),
5808 Importer.Import(Node.getLocEnd())));
5809 break;
5810
5811 case OffsetOfNode::Base: {
5812 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5813 if (!BS && Node.getBase())
5814 return nullptr;
5815 Nodes.push_back(OffsetOfNode(BS));
5816 break;
5817 }
5818 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005819 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005820 if (!FD)
5821 return nullptr;
5822 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5823 Importer.Import(Node.getLocEnd())));
5824 break;
5825 }
5826 case OffsetOfNode::Identifier: {
5827 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5828 if (!ToII)
5829 return nullptr;
5830 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5831 Importer.Import(Node.getLocEnd())));
5832 break;
5833 }
5834 }
5835 }
5836
5837 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5838 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5839 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5840 if (!ToIndexExpr)
5841 return nullptr;
5842 Exprs[I] = ToIndexExpr;
5843 }
5844
5845 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5846 if (!TInfo && OE->getTypeSourceInfo())
5847 return nullptr;
5848
5849 return OffsetOfExpr::Create(Importer.getToContext(), T,
5850 Importer.Import(OE->getOperatorLoc()),
5851 TInfo, Nodes, Exprs,
5852 Importer.Import(OE->getRParenLoc()));
5853}
5854
5855Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5856 QualType T = Importer.Import(E->getType());
5857 if (T.isNull())
5858 return nullptr;
5859
5860 Expr *Operand = Importer.Import(E->getOperand());
5861 if (!Operand)
5862 return nullptr;
5863
5864 CanThrowResult CanThrow;
5865 if (E->isValueDependent())
5866 CanThrow = CT_Dependent;
5867 else
5868 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5869
5870 return new (Importer.getToContext()) CXXNoexceptExpr(
5871 T, Operand, CanThrow,
5872 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5873}
5874
5875Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5876 QualType T = Importer.Import(E->getType());
5877 if (T.isNull())
5878 return nullptr;
5879
5880 Expr *SubExpr = Importer.Import(E->getSubExpr());
5881 if (!SubExpr && E->getSubExpr())
5882 return nullptr;
5883
5884 return new (Importer.getToContext()) CXXThrowExpr(
5885 SubExpr, T, Importer.Import(E->getThrowLoc()),
5886 E->isThrownVariableInScope());
5887}
5888
5889Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005890 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005891 if (!Param)
5892 return nullptr;
5893
5894 return CXXDefaultArgExpr::Create(
5895 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5896}
5897
5898Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5899 QualType T = Importer.Import(E->getType());
5900 if (T.isNull())
5901 return nullptr;
5902
5903 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5904 if (!TypeInfo)
5905 return nullptr;
5906
5907 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5908 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5909}
5910
5911Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5912 Expr *SubExpr = Importer.Import(E->getSubExpr());
5913 if (!SubExpr)
5914 return nullptr;
5915
5916 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5917 Importer.Import(const_cast<CXXDestructorDecl *>(
5918 E->getTemporary()->getDestructor())));
5919 if (!Dtor)
5920 return nullptr;
5921
5922 ASTContext &ToCtx = Importer.getToContext();
5923 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5924 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5925}
5926
5927Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5928 QualType T = Importer.Import(CE->getType());
5929 if (T.isNull())
5930 return nullptr;
5931
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005932 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5933 if (!TInfo)
5934 return nullptr;
5935
Aleksei Sidorina693b372016-09-28 10:16:56 +00005936 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5937 if (ImportContainerChecked(CE->arguments(), Args))
5938 return nullptr;
5939
5940 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5941 Importer.Import(CE->getConstructor()));
5942 if (!Ctor)
5943 return nullptr;
5944
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005945 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5946 Importer.getToContext(), Ctor, T, TInfo, Args,
5947 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5948 CE->isListInitialization(), CE->isStdInitListInitialization(),
5949 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005950}
5951
5952Expr *
5953ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5954 QualType T = Importer.Import(E->getType());
5955 if (T.isNull())
5956 return nullptr;
5957
5958 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5959 if (!TempE)
5960 return nullptr;
5961
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005962 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005963 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5964 if (!ExtendedBy && E->getExtendingDecl())
5965 return nullptr;
5966
5967 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5968 T, TempE, E->isBoundToLvalueReference());
5969
5970 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5971 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5972 return ToMTE;
5973}
5974
Gabor Horvath7a91c082017-11-14 11:30:38 +00005975Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5976 QualType T = Importer.Import(E->getType());
5977 if (T.isNull())
5978 return nullptr;
5979
5980 Expr *Pattern = Importer.Import(E->getPattern());
5981 if (!Pattern)
5982 return nullptr;
5983
5984 return new (Importer.getToContext()) PackExpansionExpr(
5985 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5986 E->getNumExpansions());
5987}
5988
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005989Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5990 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5991 if (!Pack)
5992 return nullptr;
5993
5994 Optional<unsigned> Length;
5995
5996 if (!E->isValueDependent())
5997 Length = E->getPackLength();
5998
5999 SmallVector<TemplateArgument, 8> PartialArguments;
6000 if (E->isPartiallySubstituted()) {
6001 if (ImportTemplateArguments(E->getPartialArguments().data(),
6002 E->getPartialArguments().size(),
6003 PartialArguments))
6004 return nullptr;
6005 }
6006
6007 return SizeOfPackExpr::Create(
6008 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
6009 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
6010 Length, PartialArguments);
6011}
6012
Aleksei Sidorina693b372016-09-28 10:16:56 +00006013Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
6014 QualType T = Importer.Import(CE->getType());
6015 if (T.isNull())
6016 return nullptr;
6017
6018 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
6019 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
6020 return nullptr;
6021
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006022 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006023 Importer.Import(CE->getOperatorNew()));
6024 if (!OperatorNewDecl && CE->getOperatorNew())
6025 return nullptr;
6026
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006027 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006028 Importer.Import(CE->getOperatorDelete()));
6029 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6030 return nullptr;
6031
6032 Expr *ToInit = Importer.Import(CE->getInitializer());
6033 if (!ToInit && CE->getInitializer())
6034 return nullptr;
6035
6036 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6037 if (!TInfo)
6038 return nullptr;
6039
6040 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6041 if (!ToArrSize && CE->getArraySize())
6042 return nullptr;
6043
6044 return new (Importer.getToContext()) CXXNewExpr(
6045 Importer.getToContext(),
6046 CE->isGlobalNew(),
6047 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006048 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006049 CE->doesUsualArrayDeleteWantSize(),
6050 PlacementArgs,
6051 Importer.Import(CE->getTypeIdParens()),
6052 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6053 Importer.Import(CE->getSourceRange()),
6054 Importer.Import(CE->getDirectInitRange()));
6055}
6056
6057Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6058 QualType T = Importer.Import(E->getType());
6059 if (T.isNull())
6060 return nullptr;
6061
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006062 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006063 Importer.Import(E->getOperatorDelete()));
6064 if (!OperatorDeleteDecl && E->getOperatorDelete())
6065 return nullptr;
6066
6067 Expr *ToArg = Importer.Import(E->getArgument());
6068 if (!ToArg && E->getArgument())
6069 return nullptr;
6070
6071 return new (Importer.getToContext()) CXXDeleteExpr(
6072 T, E->isGlobalDelete(),
6073 E->isArrayForm(),
6074 E->isArrayFormAsWritten(),
6075 E->doesUsualArrayDeleteWantSize(),
6076 OperatorDeleteDecl,
6077 ToArg,
6078 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006079}
6080
Sean Callanan59721b32015-04-28 18:41:46 +00006081Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6082 QualType T = Importer.Import(E->getType());
6083 if (T.isNull())
6084 return nullptr;
6085
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006086 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006087 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006088 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006089 return nullptr;
6090
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006091 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006092 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006093 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006094
6095 return CXXConstructExpr::Create(Importer.getToContext(), T,
6096 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006097 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006098 ToArgs, E->hadMultipleCandidates(),
6099 E->isListInitialization(),
6100 E->isStdInitListInitialization(),
6101 E->requiresZeroInitialization(),
6102 E->getConstructionKind(),
6103 Importer.Import(E->getParenOrBraceRange()));
6104}
6105
Aleksei Sidorina693b372016-09-28 10:16:56 +00006106Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6107 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6108 if (!SubExpr && EWC->getSubExpr())
6109 return nullptr;
6110
6111 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6112 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6113 if (ExprWithCleanups::CleanupObject Obj =
6114 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6115 Objs[I] = Obj;
6116 else
6117 return nullptr;
6118
6119 return ExprWithCleanups::Create(Importer.getToContext(),
6120 SubExpr, EWC->cleanupsHaveSideEffects(),
6121 Objs);
6122}
6123
Sean Callanan8bca9962016-03-28 21:43:01 +00006124Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6125 QualType T = Importer.Import(E->getType());
6126 if (T.isNull())
6127 return nullptr;
6128
6129 Expr *ToFn = Importer.Import(E->getCallee());
6130 if (!ToFn)
6131 return nullptr;
6132
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006133 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006134 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006135 return nullptr;
6136
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006137 return new (Importer.getToContext()) CXXMemberCallExpr(
6138 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6139 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006140}
6141
6142Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6143 QualType T = Importer.Import(E->getType());
6144 if (T.isNull())
6145 return nullptr;
6146
6147 return new (Importer.getToContext())
6148 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6149}
6150
6151Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6152 QualType T = Importer.Import(E->getType());
6153 if (T.isNull())
6154 return nullptr;
6155
6156 return new (Importer.getToContext())
6157 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6158}
6159
6160
Sean Callanan59721b32015-04-28 18:41:46 +00006161Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6162 QualType T = Importer.Import(E->getType());
6163 if (T.isNull())
6164 return nullptr;
6165
6166 Expr *ToBase = Importer.Import(E->getBase());
6167 if (!ToBase && E->getBase())
6168 return nullptr;
6169
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006170 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006171 if (!ToMember && E->getMemberDecl())
6172 return nullptr;
6173
Peter Szecsief972522018-05-02 11:52:54 +00006174 auto *ToDecl =
6175 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6176 if (!ToDecl && E->getFoundDecl().getDecl())
6177 return nullptr;
6178
6179 DeclAccessPair ToFoundDecl =
6180 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006181
6182 DeclarationNameInfo ToMemberNameInfo(
6183 Importer.Import(E->getMemberNameInfo().getName()),
6184 Importer.Import(E->getMemberNameInfo().getLoc()));
6185
6186 if (E->hasExplicitTemplateArgs()) {
6187 return nullptr; // FIXME: handle template arguments
6188 }
6189
6190 return MemberExpr::Create(Importer.getToContext(), ToBase,
6191 E->isArrow(),
6192 Importer.Import(E->getOperatorLoc()),
6193 Importer.Import(E->getQualifierLoc()),
6194 Importer.Import(E->getTemplateKeywordLoc()),
6195 ToMember, ToFoundDecl, ToMemberNameInfo,
6196 nullptr, T, E->getValueKind(),
6197 E->getObjectKind());
6198}
6199
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006200Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6201 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006202 Expr *BaseE = Importer.Import(E->getBase());
6203 if (!BaseE)
6204 return nullptr;
6205
6206 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6207 if (!ScopeInfo && E->getScopeTypeInfo())
6208 return nullptr;
6209
6210 PseudoDestructorTypeStorage Storage;
6211 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6212 IdentifierInfo *ToII = Importer.Import(FromII);
6213 if (!ToII)
6214 return nullptr;
6215 Storage = PseudoDestructorTypeStorage(
6216 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6217 } else {
6218 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6219 if (!TI)
6220 return nullptr;
6221 Storage = PseudoDestructorTypeStorage(TI);
6222 }
6223
6224 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6225 Importer.getToContext(), BaseE, E->isArrow(),
6226 Importer.Import(E->getOperatorLoc()),
6227 Importer.Import(E->getQualifierLoc()),
6228 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6229 Importer.Import(E->getTildeLoc()), Storage);
6230}
6231
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006232Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6233 CXXDependentScopeMemberExpr *E) {
6234 Expr *Base = nullptr;
6235 if (!E->isImplicitAccess()) {
6236 Base = Importer.Import(E->getBase());
6237 if (!Base)
6238 return nullptr;
6239 }
6240
6241 QualType BaseType = Importer.Import(E->getBaseType());
6242 if (BaseType.isNull())
6243 return nullptr;
6244
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006245 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006246 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006247 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6248 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006249 return nullptr;
6250 ResInfo = &ToTAInfo;
6251 }
6252
6253 DeclarationName Name = Importer.Import(E->getMember());
6254 if (!E->getMember().isEmpty() && Name.isEmpty())
6255 return nullptr;
6256
6257 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6258 // Import additional name location/type info.
6259 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6260 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6261 if (!ToFQ && E->getFirstQualifierFoundInScope())
6262 return nullptr;
6263
6264 return CXXDependentScopeMemberExpr::Create(
6265 Importer.getToContext(), Base, BaseType, E->isArrow(),
6266 Importer.Import(E->getOperatorLoc()),
6267 Importer.Import(E->getQualifierLoc()),
6268 Importer.Import(E->getTemplateKeywordLoc()),
6269 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6270}
6271
Peter Szecsice7f3182018-05-07 12:08:27 +00006272Expr *
6273ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6274 DeclarationName Name = Importer.Import(E->getDeclName());
6275 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6276 return nullptr;
6277
6278 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6279 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6280
6281 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6282 Importer.Import(E->getRAngleLoc()));
6283 TemplateArgumentListInfo *ResInfo = nullptr;
6284 if (E->hasExplicitTemplateArgs()) {
6285 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6286 return nullptr;
6287 ResInfo = &ToTAInfo;
6288 }
6289
6290 return DependentScopeDeclRefExpr::Create(
6291 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6292 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6293}
6294
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006295Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6296 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006297 unsigned NumArgs = CE->arg_size();
6298
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006299 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006300 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6301 return nullptr;
6302
6303 return CXXUnresolvedConstructExpr::Create(
6304 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6305 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6306 Importer.Import(CE->getRParenLoc()));
6307}
6308
6309Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006310 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006311 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6312 if (E->getNamingClass() && !NamingClass)
6313 return nullptr;
6314
6315 DeclarationName Name = Importer.Import(E->getName());
6316 if (E->getName() && !Name)
6317 return nullptr;
6318
6319 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6320 // Import additional name location/type info.
6321 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6322
6323 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006324 for (auto *D : E->decls()) {
6325 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006326 ToDecls.addDecl(To);
6327 else
6328 return nullptr;
6329 }
6330
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006331 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006332 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006333 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6334 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006335 return nullptr;
6336 ResInfo = &ToTAInfo;
6337 }
6338
6339 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6340 return UnresolvedLookupExpr::Create(
6341 Importer.getToContext(), NamingClass,
6342 Importer.Import(E->getQualifierLoc()),
6343 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6344 ResInfo, ToDecls.begin(), ToDecls.end());
6345
6346 return UnresolvedLookupExpr::Create(
6347 Importer.getToContext(), NamingClass,
6348 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6349 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6350}
6351
Peter Szecsice7f3182018-05-07 12:08:27 +00006352Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6353 DeclarationName Name = Importer.Import(E->getName());
6354 if (!E->getName().isEmpty() && Name.isEmpty())
6355 return nullptr;
6356 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6357 // Import additional name location/type info.
6358 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6359
6360 QualType BaseType = Importer.Import(E->getType());
6361 if (!E->getType().isNull() && BaseType.isNull())
6362 return nullptr;
6363
6364 UnresolvedSet<8> ToDecls;
6365 for (Decl *D : E->decls()) {
6366 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6367 ToDecls.addDecl(To);
6368 else
6369 return nullptr;
6370 }
6371
6372 TemplateArgumentListInfo ToTAInfo;
6373 TemplateArgumentListInfo *ResInfo = nullptr;
6374 if (E->hasExplicitTemplateArgs()) {
6375 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6376 return nullptr;
6377 ResInfo = &ToTAInfo;
6378 }
6379
6380 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6381 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6382 return nullptr;
6383 }
6384
6385 return UnresolvedMemberExpr::Create(
6386 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6387 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6388 Importer.Import(E->getQualifierLoc()),
6389 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6390 ToDecls.begin(), ToDecls.end());
6391}
6392
Sean Callanan59721b32015-04-28 18:41:46 +00006393Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6394 QualType T = Importer.Import(E->getType());
6395 if (T.isNull())
6396 return nullptr;
6397
6398 Expr *ToCallee = Importer.Import(E->getCallee());
6399 if (!ToCallee && E->getCallee())
6400 return nullptr;
6401
6402 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006403 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006404 if (ImportContainerChecked(E->arguments(), ToArgs))
6405 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006406
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006407 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006408
6409 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6410 ToArgs_Copied[ai] = ToArgs[ai];
6411
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006412 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6413 return new (Importer.getToContext()) CXXOperatorCallExpr(
6414 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6415 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6416 OCE->getFPFeatures());
6417 }
6418
Sean Callanan59721b32015-04-28 18:41:46 +00006419 return new (Importer.getToContext())
6420 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006421 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006422 Importer.Import(E->getRParenLoc()));
6423}
6424
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006425Optional<LambdaCapture>
6426ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6427 VarDecl *Var = nullptr;
6428 if (From.capturesVariable()) {
6429 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6430 if (!Var)
6431 return None;
6432 }
6433
6434 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6435 From.getCaptureKind(), Var,
6436 From.isPackExpansion()
6437 ? Importer.Import(From.getEllipsisLoc())
6438 : SourceLocation());
6439}
6440
6441Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6442 CXXRecordDecl *FromClass = LE->getLambdaClass();
6443 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6444 if (!ToClass)
6445 return nullptr;
6446
6447 // NOTE: lambda classes are created with BeingDefined flag set up.
6448 // It means that ImportDefinition doesn't work for them and we should fill it
6449 // manually.
6450 if (ToClass->isBeingDefined()) {
6451 for (auto FromField : FromClass->fields()) {
6452 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6453 if (!ToField)
6454 return nullptr;
6455 }
6456 }
6457
6458 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6459 Importer.Import(LE->getCallOperator()));
6460 if (!ToCallOp)
6461 return nullptr;
6462
6463 ToClass->completeDefinition();
6464
6465 unsigned NumCaptures = LE->capture_size();
6466 SmallVector<LambdaCapture, 8> Captures;
6467 Captures.reserve(NumCaptures);
6468 for (const auto &FromCapture : LE->captures()) {
6469 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6470 Captures.push_back(*ToCapture);
6471 else
6472 return nullptr;
6473 }
6474
6475 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6476 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6477 return nullptr;
6478
6479 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6480 Importer.Import(LE->getIntroducerRange()),
6481 LE->getCaptureDefault(),
6482 Importer.Import(LE->getCaptureDefaultLoc()),
6483 Captures,
6484 LE->hasExplicitParameters(),
6485 LE->hasExplicitResultType(),
6486 InitCaptures,
6487 Importer.Import(LE->getLocEnd()),
6488 LE->containsUnexpandedParameterPack());
6489}
6490
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006491Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6492 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006493 if (T.isNull())
6494 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006495
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006496 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006497 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006498 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006499
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006500 ASTContext &ToCtx = Importer.getToContext();
6501 InitListExpr *To = new (ToCtx) InitListExpr(
6502 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6503 Exprs, Importer.Import(ILE->getLBraceLoc()));
6504 To->setType(T);
6505
6506 if (ILE->hasArrayFiller()) {
6507 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6508 if (!Filler)
6509 return nullptr;
6510 To->setArrayFiller(Filler);
6511 }
6512
6513 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006514 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006515 if (!ToFD)
6516 return nullptr;
6517 To->setInitializedFieldInUnion(ToFD);
6518 }
6519
6520 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006521 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006522 if (!ToSyntForm)
6523 return nullptr;
6524 To->setSyntacticForm(ToSyntForm);
6525 }
6526
6527 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6528 To->setValueDependent(ILE->isValueDependent());
6529 To->setInstantiationDependent(ILE->isInstantiationDependent());
6530
6531 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006532}
6533
Richard Smith30e304e2016-12-14 00:03:17 +00006534Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6535 QualType ToType = Importer.Import(E->getType());
6536 if (ToType.isNull())
6537 return nullptr;
6538
6539 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6540 if (!ToCommon && E->getCommonExpr())
6541 return nullptr;
6542
6543 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6544 if (!ToSubExpr && E->getSubExpr())
6545 return nullptr;
6546
6547 return new (Importer.getToContext())
6548 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6549}
6550
6551Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6552 QualType ToType = Importer.Import(E->getType());
6553 if (ToType.isNull())
6554 return nullptr;
6555 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6556}
6557
Sean Callanandd2c1742016-05-16 20:48:03 +00006558Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006559 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006560 if (!ToField && DIE->getField())
6561 return nullptr;
6562
6563 return CXXDefaultInitExpr::Create(
6564 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6565}
6566
6567Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6568 QualType ToType = Importer.Import(E->getType());
6569 if (ToType.isNull() && !E->getType().isNull())
6570 return nullptr;
6571 ExprValueKind VK = E->getValueKind();
6572 CastKind CK = E->getCastKind();
6573 Expr *ToOp = Importer.Import(E->getSubExpr());
6574 if (!ToOp && E->getSubExpr())
6575 return nullptr;
6576 CXXCastPath BasePath;
6577 if (ImportCastPath(E, BasePath))
6578 return nullptr;
6579 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6580 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6581 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6582 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6583
6584 if (isa<CXXStaticCastExpr>(E)) {
6585 return CXXStaticCastExpr::Create(
6586 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6587 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6588 } else if (isa<CXXDynamicCastExpr>(E)) {
6589 return CXXDynamicCastExpr::Create(
6590 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6591 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6592 } else if (isa<CXXReinterpretCastExpr>(E)) {
6593 return CXXReinterpretCastExpr::Create(
6594 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6595 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6596 } else {
6597 return nullptr;
6598 }
6599}
6600
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006601Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6602 SubstNonTypeTemplateParmExpr *E) {
6603 QualType T = Importer.Import(E->getType());
6604 if (T.isNull())
6605 return nullptr;
6606
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006607 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006608 Importer.Import(E->getParameter()));
6609 if (!Param)
6610 return nullptr;
6611
6612 Expr *Replacement = Importer.Import(E->getReplacement());
6613 if (!Replacement)
6614 return nullptr;
6615
6616 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6617 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6618 Replacement);
6619}
6620
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006621Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6622 QualType ToType = Importer.Import(E->getType());
6623 if (ToType.isNull())
6624 return nullptr;
6625
6626 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6627 if (ImportContainerChecked(E->getArgs(), ToArgs))
6628 return nullptr;
6629
6630 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6631 // Value is always false.
6632 bool ToValue = false;
6633 if (!E->isValueDependent())
6634 ToValue = E->getValue();
6635
6636 return TypeTraitExpr::Create(
6637 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6638 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6639}
6640
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006641Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6642 QualType ToType = Importer.Import(E->getType());
6643 if (ToType.isNull())
6644 return nullptr;
6645
6646 if (E->isTypeOperand()) {
6647 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6648 if (!TSI)
6649 return nullptr;
6650
6651 return new (Importer.getToContext())
6652 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6653 }
6654
6655 Expr *Op = Importer.Import(E->getExprOperand());
6656 if (!Op)
6657 return nullptr;
6658
6659 return new (Importer.getToContext())
6660 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6661}
6662
Lang Hames19e07e12017-06-20 21:06:00 +00006663void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6664 CXXMethodDecl *FromMethod) {
6665 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6666 ToMethod->addOverriddenMethod(
6667 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6668 FromOverriddenMethod))));
6669}
6670
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006671ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006672 ASTContext &FromContext, FileManager &FromFileManager,
6673 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006674 : ToContext(ToContext), FromContext(FromContext),
6675 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6676 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006677 ImportedDecls[FromContext.getTranslationUnitDecl()]
6678 = ToContext.getTranslationUnitDecl();
6679}
6680
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006681ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006682
6683QualType ASTImporter::Import(QualType FromT) {
6684 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006685 return {};
John McCall424cec92011-01-19 06:33:43 +00006686
6687 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006688
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006689 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006690 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6691 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006692 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006693 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006694
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006695 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006696 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006697 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006698 if (ToT.isNull())
6699 return ToT;
6700
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006701 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006702 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006703
John McCall424cec92011-01-19 06:33:43 +00006704 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006705}
6706
Douglas Gregor62d311f2010-02-09 19:21:46 +00006707TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006708 if (!FromTSI)
6709 return FromTSI;
6710
6711 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006712 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006713 QualType T = Import(FromTSI->getType());
6714 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006715 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006716
6717 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006718 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006719}
6720
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006721Attr *ASTImporter::Import(const Attr *FromAttr) {
6722 Attr *ToAttr = FromAttr->clone(ToContext);
6723 ToAttr->setRange(Import(FromAttr->getRange()));
6724 return ToAttr;
6725}
6726
Sean Callanan59721b32015-04-28 18:41:46 +00006727Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6728 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6729 if (Pos != ImportedDecls.end()) {
6730 Decl *ToD = Pos->second;
6731 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6732 return ToD;
6733 } else {
6734 return nullptr;
6735 }
6736}
6737
Douglas Gregor62d311f2010-02-09 19:21:46 +00006738Decl *ASTImporter::Import(Decl *FromD) {
6739 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006740 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006741
Douglas Gregord451ea92011-07-29 23:31:30 +00006742 ASTNodeImporter Importer(*this);
6743
Douglas Gregor62d311f2010-02-09 19:21:46 +00006744 // Check whether we've already imported this declaration.
6745 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006746 if (Pos != ImportedDecls.end()) {
6747 Decl *ToD = Pos->second;
6748 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6749 return ToD;
6750 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006751
6752 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006753 Decl *ToD = Importer.Visit(FromD);
6754 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006755 return nullptr;
6756
Douglas Gregor62d311f2010-02-09 19:21:46 +00006757 // Record the imported declaration.
6758 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006759 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006760 return ToD;
6761}
6762
6763DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6764 if (!FromDC)
6765 return FromDC;
6766
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006767 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006768 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006769 return nullptr;
6770
Douglas Gregor2e15c842012-02-01 21:00:38 +00006771 // When we're using a record/enum/Objective-C class/protocol as a context, we
6772 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006773 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6774 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006775 if (ToRecord->isCompleteDefinition()) {
6776 // Do nothing.
6777 } else if (FromRecord->isCompleteDefinition()) {
6778 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6779 ASTNodeImporter::IDK_Basic);
6780 } else {
6781 CompleteDecl(ToRecord);
6782 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006783 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6784 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006785 if (ToEnum->isCompleteDefinition()) {
6786 // Do nothing.
6787 } else if (FromEnum->isCompleteDefinition()) {
6788 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6789 ASTNodeImporter::IDK_Basic);
6790 } else {
6791 CompleteDecl(ToEnum);
6792 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006793 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6794 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006795 if (ToClass->getDefinition()) {
6796 // Do nothing.
6797 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6798 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6799 ASTNodeImporter::IDK_Basic);
6800 } else {
6801 CompleteDecl(ToClass);
6802 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006803 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6804 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006805 if (ToProto->getDefinition()) {
6806 // Do nothing.
6807 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6808 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6809 ASTNodeImporter::IDK_Basic);
6810 } else {
6811 CompleteDecl(ToProto);
6812 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006813 }
6814
6815 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006816}
6817
6818Expr *ASTImporter::Import(Expr *FromE) {
6819 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006820 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006821
6822 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6823}
6824
6825Stmt *ASTImporter::Import(Stmt *FromS) {
6826 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006827 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006828
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006829 // Check whether we've already imported this declaration.
6830 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6831 if (Pos != ImportedStmts.end())
6832 return Pos->second;
6833
6834 // Import the type
6835 ASTNodeImporter Importer(*this);
6836 Stmt *ToS = Importer.Visit(FromS);
6837 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006838 return nullptr;
6839
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006840 // Record the imported declaration.
6841 ImportedStmts[FromS] = ToS;
6842 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006843}
6844
6845NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6846 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006847 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006848
Douglas Gregor90ebf252011-04-27 16:48:40 +00006849 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6850
6851 switch (FromNNS->getKind()) {
6852 case NestedNameSpecifier::Identifier:
6853 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6854 return NestedNameSpecifier::Create(ToContext, prefix, II);
6855 }
Craig Topper36250ad2014-05-12 05:36:57 +00006856 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006857
6858 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006859 if (auto *NS =
6860 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006861 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6862 }
Craig Topper36250ad2014-05-12 05:36:57 +00006863 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006864
6865 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006866 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006867 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006868 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6869 }
Craig Topper36250ad2014-05-12 05:36:57 +00006870 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006871
6872 case NestedNameSpecifier::Global:
6873 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6874
Nikola Smiljanic67860242014-09-26 00:28:20 +00006875 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006876 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006877 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006878 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6879 }
6880 return nullptr;
6881
Douglas Gregor90ebf252011-04-27 16:48:40 +00006882 case NestedNameSpecifier::TypeSpec:
6883 case NestedNameSpecifier::TypeSpecWithTemplate: {
6884 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6885 if (!T.isNull()) {
6886 bool bTemplate = FromNNS->getKind() ==
6887 NestedNameSpecifier::TypeSpecWithTemplate;
6888 return NestedNameSpecifier::Create(ToContext, prefix,
6889 bTemplate, T.getTypePtr());
6890 }
6891 }
Craig Topper36250ad2014-05-12 05:36:57 +00006892 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006893 }
6894
6895 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006896}
6897
Douglas Gregor14454802011-02-25 02:25:35 +00006898NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006899 // Copied from NestedNameSpecifier mostly.
6900 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6901 NestedNameSpecifierLoc NNS = FromNNS;
6902
6903 // Push each of the nested-name-specifiers's onto a stack for
6904 // serialization in reverse order.
6905 while (NNS) {
6906 NestedNames.push_back(NNS);
6907 NNS = NNS.getPrefix();
6908 }
6909
6910 NestedNameSpecifierLocBuilder Builder;
6911
6912 while (!NestedNames.empty()) {
6913 NNS = NestedNames.pop_back_val();
6914 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6915 if (!Spec)
6916 return NestedNameSpecifierLoc();
6917
6918 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6919 switch (Kind) {
6920 case NestedNameSpecifier::Identifier:
6921 Builder.Extend(getToContext(),
6922 Spec->getAsIdentifier(),
6923 Import(NNS.getLocalBeginLoc()),
6924 Import(NNS.getLocalEndLoc()));
6925 break;
6926
6927 case NestedNameSpecifier::Namespace:
6928 Builder.Extend(getToContext(),
6929 Spec->getAsNamespace(),
6930 Import(NNS.getLocalBeginLoc()),
6931 Import(NNS.getLocalEndLoc()));
6932 break;
6933
6934 case NestedNameSpecifier::NamespaceAlias:
6935 Builder.Extend(getToContext(),
6936 Spec->getAsNamespaceAlias(),
6937 Import(NNS.getLocalBeginLoc()),
6938 Import(NNS.getLocalEndLoc()));
6939 break;
6940
6941 case NestedNameSpecifier::TypeSpec:
6942 case NestedNameSpecifier::TypeSpecWithTemplate: {
6943 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6944 QualType(Spec->getAsType(), 0));
6945 Builder.Extend(getToContext(),
6946 Import(NNS.getLocalBeginLoc()),
6947 TSI->getTypeLoc(),
6948 Import(NNS.getLocalEndLoc()));
6949 break;
6950 }
6951
6952 case NestedNameSpecifier::Global:
6953 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6954 break;
6955
6956 case NestedNameSpecifier::Super: {
6957 SourceRange ToRange = Import(NNS.getSourceRange());
6958 Builder.MakeSuper(getToContext(),
6959 Spec->getAsRecordDecl(),
6960 ToRange.getBegin(),
6961 ToRange.getEnd());
6962 }
6963 }
6964 }
6965
6966 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006967}
6968
Douglas Gregore2e50d332010-12-01 01:36:18 +00006969TemplateName ASTImporter::Import(TemplateName From) {
6970 switch (From.getKind()) {
6971 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006972 if (auto *ToTemplate =
6973 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006974 return TemplateName(ToTemplate);
6975
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006976 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006977
6978 case TemplateName::OverloadedTemplate: {
6979 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6980 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006981 for (auto *I : *FromStorage) {
6982 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006983 ToTemplates.addDecl(To);
6984 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006985 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006986 }
6987 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6988 ToTemplates.end());
6989 }
6990
6991 case TemplateName::QualifiedTemplate: {
6992 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6993 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6994 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006995 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006996
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006997 if (auto *ToTemplate =
6998 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006999 return ToContext.getQualifiedTemplateName(Qualifier,
7000 QTN->hasTemplateKeyword(),
7001 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007002
7003 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007004 }
7005
7006 case TemplateName::DependentTemplate: {
7007 DependentTemplateName *DTN = From.getAsDependentTemplateName();
7008 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
7009 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007010 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00007011
7012 if (DTN->isIdentifier()) {
7013 return ToContext.getDependentTemplateName(Qualifier,
7014 Import(DTN->getIdentifier()));
7015 }
7016
7017 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
7018 }
John McCalld9dfe3a2011-06-30 08:33:18 +00007019
7020 case TemplateName::SubstTemplateTemplateParm: {
7021 SubstTemplateTemplateParmStorage *subst
7022 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007023 auto *param =
7024 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00007025 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007026 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007027
7028 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007029 if (replacement.isNull())
7030 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007031
7032 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7033 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007034
7035 case TemplateName::SubstTemplateTemplateParmPack: {
7036 SubstTemplateTemplateParmPackStorage *SubstPack
7037 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007038 auto *Param =
7039 cast_or_null<TemplateTemplateParmDecl>(
7040 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007041 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007042 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007043
7044 ASTNodeImporter Importer(*this);
7045 TemplateArgument ArgPack
7046 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7047 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007048 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007049
7050 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7051 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007052 }
7053
7054 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007055}
7056
Douglas Gregor62d311f2010-02-09 19:21:46 +00007057SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7058 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007059 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007060
Douglas Gregor811663e2010-02-10 00:15:17 +00007061 SourceManager &FromSM = FromContext.getSourceManager();
7062
Sean Callanan24c5fe62016-11-07 20:42:25 +00007063 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007064 // don't have to import macro expansions.
7065 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007066 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007067 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7068 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007069 FileID ToFileID = Import(Decomposed.first);
7070 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007071 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007072 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7073 .getLocWithOffset(Decomposed.second);
7074 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007075}
7076
7077SourceRange ASTImporter::Import(SourceRange FromRange) {
7078 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7079}
7080
Douglas Gregor811663e2010-02-10 00:15:17 +00007081FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007082 llvm::DenseMap<FileID, FileID>::iterator Pos
7083 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007084 if (Pos != ImportedFileIDs.end())
7085 return Pos->second;
7086
7087 SourceManager &FromSM = FromContext.getSourceManager();
7088 SourceManager &ToSM = ToContext.getSourceManager();
7089 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007090 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007091
7092 // Include location of this file.
7093 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7094
7095 // Map the FileID for to the "to" source manager.
7096 FileID ToID;
7097 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007098 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007099 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7100 // disk again
7101 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7102 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007103 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007104 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007105 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007106 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7107 FromSLoc.getFile().getFileCharacteristic());
7108 } else {
7109 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007110 const llvm::MemoryBuffer *
7111 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007112 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007113 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007114 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007115 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007116 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007117 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007118
Sebastian Redl99219f12010-09-30 01:03:06 +00007119 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007120 return ToID;
7121}
7122
Sean Callanandd2c1742016-05-16 20:48:03 +00007123CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7124 Expr *ToExpr = Import(From->getInit());
7125 if (!ToExpr && From->getInit())
7126 return nullptr;
7127
7128 if (From->isBaseInitializer()) {
7129 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7130 if (!ToTInfo && From->getTypeSourceInfo())
7131 return nullptr;
7132
7133 return new (ToContext) CXXCtorInitializer(
7134 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7135 ToExpr, Import(From->getRParenLoc()),
7136 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7137 : SourceLocation());
7138 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007139 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007140 if (!ToField && From->getMember())
7141 return nullptr;
7142
7143 return new (ToContext) CXXCtorInitializer(
7144 ToContext, ToField, Import(From->getMemberLocation()),
7145 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7146 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007147 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007148 Import(From->getIndirectMember()));
7149 if (!ToIField && From->getIndirectMember())
7150 return nullptr;
7151
7152 return new (ToContext) CXXCtorInitializer(
7153 ToContext, ToIField, Import(From->getMemberLocation()),
7154 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7155 } else if (From->isDelegatingInitializer()) {
7156 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7157 if (!ToTInfo && From->getTypeSourceInfo())
7158 return nullptr;
7159
7160 return new (ToContext)
7161 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7162 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007163 } else {
7164 return nullptr;
7165 }
7166}
7167
Aleksei Sidorina693b372016-09-28 10:16:56 +00007168CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7169 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7170 if (Pos != ImportedCXXBaseSpecifiers.end())
7171 return Pos->second;
7172
7173 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7174 Import(BaseSpec->getSourceRange()),
7175 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7176 BaseSpec->getAccessSpecifierAsWritten(),
7177 Import(BaseSpec->getTypeSourceInfo()),
7178 Import(BaseSpec->getEllipsisLoc()));
7179 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7180 return Imported;
7181}
7182
Douglas Gregor0a791672011-01-18 03:11:38 +00007183void ASTImporter::ImportDefinition(Decl *From) {
7184 Decl *To = Import(From);
7185 if (!To)
7186 return;
7187
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007188 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007189 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007190
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007191 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007192 if (!ToRecord->getDefinition()) {
7193 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007194 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007195 return;
7196 }
7197 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007198
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007199 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007200 if (!ToEnum->getDefinition()) {
7201 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007202 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007203 return;
7204 }
7205 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007206
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007207 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007208 if (!ToIFace->getDefinition()) {
7209 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007210 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007211 return;
7212 }
7213 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007214
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007215 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007216 if (!ToProto->getDefinition()) {
7217 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007218 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007219 return;
7220 }
7221 }
7222
Douglas Gregor0a791672011-01-18 03:11:38 +00007223 Importer.ImportDeclContext(FromDC, true);
7224 }
7225}
7226
Douglas Gregor96e578d2010-02-05 17:54:41 +00007227DeclarationName ASTImporter::Import(DeclarationName FromName) {
7228 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007229 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007230
7231 switch (FromName.getNameKind()) {
7232 case DeclarationName::Identifier:
7233 return Import(FromName.getAsIdentifierInfo());
7234
7235 case DeclarationName::ObjCZeroArgSelector:
7236 case DeclarationName::ObjCOneArgSelector:
7237 case DeclarationName::ObjCMultiArgSelector:
7238 return Import(FromName.getObjCSelector());
7239
7240 case DeclarationName::CXXConstructorName: {
7241 QualType T = Import(FromName.getCXXNameType());
7242 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007243 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007244
7245 return ToContext.DeclarationNames.getCXXConstructorName(
7246 ToContext.getCanonicalType(T));
7247 }
7248
7249 case DeclarationName::CXXDestructorName: {
7250 QualType T = Import(FromName.getCXXNameType());
7251 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007252 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007253
7254 return ToContext.DeclarationNames.getCXXDestructorName(
7255 ToContext.getCanonicalType(T));
7256 }
7257
Richard Smith35845152017-02-07 01:37:30 +00007258 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007259 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007260 Import(FromName.getCXXDeductionGuideTemplate()));
7261 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007262 return {};
Richard Smith35845152017-02-07 01:37:30 +00007263 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7264 }
7265
Douglas Gregor96e578d2010-02-05 17:54:41 +00007266 case DeclarationName::CXXConversionFunctionName: {
7267 QualType T = Import(FromName.getCXXNameType());
7268 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007269 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007270
7271 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7272 ToContext.getCanonicalType(T));
7273 }
7274
7275 case DeclarationName::CXXOperatorName:
7276 return ToContext.DeclarationNames.getCXXOperatorName(
7277 FromName.getCXXOverloadedOperator());
7278
7279 case DeclarationName::CXXLiteralOperatorName:
7280 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7281 Import(FromName.getCXXLiteralIdentifier()));
7282
7283 case DeclarationName::CXXUsingDirective:
7284 // FIXME: STATICS!
7285 return DeclarationName::getUsingDirectiveName();
7286 }
7287
David Blaikiee4d798f2012-01-20 21:50:17 +00007288 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007289}
7290
Douglas Gregore2e50d332010-12-01 01:36:18 +00007291IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007292 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007293 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007294
Sean Callananf94ef1d2016-05-14 06:11:19 +00007295 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7296
7297 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7298 ToId->setBuiltinID(FromId->getBuiltinID());
7299
7300 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007301}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007302
Douglas Gregor43f54792010-02-17 02:12:47 +00007303Selector ASTImporter::Import(Selector FromSel) {
7304 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007305 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007306
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007307 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007308 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7309 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7310 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7311 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7312}
7313
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007314DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7315 DeclContext *DC,
7316 unsigned IDNS,
7317 NamedDecl **Decls,
7318 unsigned NumDecls) {
7319 return Name;
7320}
7321
7322DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007323 if (LastDiagFromFrom)
7324 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7325 FromContext.getDiagnostics());
7326 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007327 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007328}
7329
7330DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007331 if (!LastDiagFromFrom)
7332 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7333 ToContext.getDiagnostics());
7334 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007335 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007336}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007337
Douglas Gregor2e15c842012-02-01 21:00:38 +00007338void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007339 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007340 if (!ID->getDefinition())
7341 ID->startDefinition();
7342 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007343 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007344 if (!PD->getDefinition())
7345 PD->startDefinition();
7346 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007347 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007348 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7349 TD->startDefinition();
7350 TD->setCompleteDefinition(true);
7351 }
7352 }
7353 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007354 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007355 }
7356}
7357
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007358Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007359 if (From->hasAttrs()) {
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007360 for (const auto *FromAttr : From->getAttrs())
7361 To->addAttr(Import(FromAttr));
Sean Callanan8bca9962016-03-28 21:43:01 +00007362 }
7363 if (From->isUsed()) {
7364 To->setIsUsed();
7365 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007366 if (From->isImplicit()) {
7367 To->setImplicit();
7368 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007369 ImportedDecls[From] = To;
7370 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007371}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007372
Douglas Gregordd6006f2012-07-17 21:16:27 +00007373bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7374 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007375 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007376 = ImportedTypes.find(From.getTypePtr());
7377 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7378 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007379
Douglas Gregordd6006f2012-07-17 21:16:27 +00007380 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7381 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007382 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007383}