blob: d64b64ce43bc46d248218652002a641eb380abea [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 }
2018
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002019 if (auto *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Aleksei Sidorin499de6c2018-04-05 15:31:49 +00002020 if (!SearchName) {
2021 // If both unnamed structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002022 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002023 if (Optional<unsigned> Index1 =
2024 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
2025 D)) {
2026 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00002027 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002028 if (*Index1 != *Index2)
2029 continue;
2030 }
2031 }
2032 }
2033
Sean Callanan9092d472017-05-13 00:46:33 +00002034 PrevDecl = FoundRecord;
2035
Douglas Gregor25791052010-02-12 00:09:27 +00002036 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002037 if ((SearchName && !D->isCompleteDefinition())
2038 || (D->isCompleteDefinition() &&
2039 D->isAnonymousStructOrUnion()
2040 == FoundDef->isAnonymousStructOrUnion() &&
2041 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002042 // The record types structurally match, or the "from" translation
2043 // unit only had a forward declaration anyway; call it the same
2044 // function.
2045 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002046 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002047 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002048 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002049 // We have a forward declaration of this type, so adopt that forward
2050 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002051
2052 // If one or both can be completed from external storage then try one
2053 // last time to complete and compare them before doing this.
2054
2055 if (FoundRecord->hasExternalLexicalStorage() &&
2056 !FoundRecord->isCompleteDefinition())
2057 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2058 if (D->hasExternalLexicalStorage())
2059 D->getASTContext().getExternalSource()->CompleteType(D);
2060
2061 if (FoundRecord->isCompleteDefinition() &&
2062 D->isCompleteDefinition() &&
2063 !IsStructuralMatch(D, FoundRecord))
2064 continue;
2065
Douglas Gregor25791052010-02-12 00:09:27 +00002066 AdoptDecl = FoundRecord;
2067 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002068 } else if (!SearchName) {
2069 continue;
2070 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002071 }
2072
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002073 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002074 }
2075
Douglas Gregordd6006f2012-07-17 21:16:27 +00002076 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002077 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2078 ConflictingDecls.data(),
2079 ConflictingDecls.size());
2080 }
2081 }
2082
2083 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002084 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002085 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002086 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002087 CXXRecordDecl *D2CXX = nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002088 if (auto *DCXX = dyn_cast<CXXRecordDecl>(D)) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002089 if (DCXX->isLambda()) {
2090 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2091 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2092 DC, TInfo, Loc,
2093 DCXX->isDependentLambda(),
2094 DCXX->isGenericLambda(),
2095 DCXX->getLambdaCaptureDefault());
2096 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2097 if (DCXX->getLambdaContextDecl() && !CDecl)
2098 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00002099 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002100 } else if (DCXX->isInjectedClassName()) {
2101 // We have to be careful to do a similar dance to the one in
2102 // Sema::ActOnStartCXXMemberDeclarations
2103 CXXRecordDecl *const PrevDecl = nullptr;
2104 const bool DelayTypeCreation = true;
2105 D2CXX = CXXRecordDecl::Create(
2106 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
2107 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
2108 Importer.getToContext().getTypeDeclType(
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002109 D2CXX, dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00002110 } else {
2111 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2112 D->getTagKind(),
2113 DC, StartLoc, Loc,
2114 Name.getAsIdentifierInfo());
2115 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002116 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002117 D2->setAccess(D->getAccess());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002118 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonde8bf262018-05-17 09:46:07 +00002119 if (!DCXX->getDescribedClassTemplate() || DCXX->isImplicit())
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002120 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002121
2122 Importer.Imported(D, D2);
2123
2124 if (ClassTemplateDecl *FromDescribed =
2125 DCXX->getDescribedClassTemplate()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002126 auto *ToDescribed = cast_or_null<ClassTemplateDecl>(
2127 Importer.Import(FromDescribed));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002128 if (!ToDescribed)
2129 return nullptr;
2130 D2CXX->setDescribedClassTemplate(ToDescribed);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002131 } else if (MemberSpecializationInfo *MemberInfo =
2132 DCXX->getMemberSpecializationInfo()) {
2133 TemplateSpecializationKind SK =
2134 MemberInfo->getTemplateSpecializationKind();
2135 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002136 auto *ToInst =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00002137 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
2138 if (FromInst && !ToInst)
2139 return nullptr;
2140 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
2141 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
2142 Importer.Import(MemberInfo->getPointOfInstantiation()));
2143 }
Douglas Gregor25791052010-02-12 00:09:27 +00002144 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002145 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002146 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002147 D2->setLexicalDeclContext(LexicalDC);
2148 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002149 }
Douglas Gregor14454802011-02-25 02:25:35 +00002150
2151 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd6006f2012-07-17 21:16:27 +00002152 if (D->isAnonymousStructOrUnion())
2153 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00002154 if (PrevDecl) {
2155 // FIXME: do this for all Redeclarables, not just RecordDecls.
2156 D2->setPreviousDecl(PrevDecl);
2157 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002158 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002159
Douglas Gregor3996e242010-02-15 22:01:00 +00002160 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002161
Douglas Gregor95d82832012-01-24 18:36:04 +00002162 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002163 return nullptr;
2164
Douglas Gregor3996e242010-02-15 22:01:00 +00002165 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002166}
2167
Douglas Gregor98c10182010-02-12 22:17:39 +00002168Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2169 // Import the major distinguishing characteristics of this enumerator.
2170 DeclContext *DC, *LexicalDC;
2171 DeclarationName Name;
2172 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002173 NamedDecl *ToD;
2174 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002175 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002176 if (ToD)
2177 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002178
2179 QualType T = Importer.Import(D->getType());
2180 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002181 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002182
Douglas Gregor98c10182010-02-12 22:17:39 +00002183 // Determine whether there are any other declarations with the same name and
2184 // in the same context.
2185 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002186 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002187 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002188 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002189 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002190 for (auto *FoundDecl : FoundDecls) {
2191 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002192 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002193
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002194 if (auto *FoundEnumConstant = dyn_cast<EnumConstantDecl>(FoundDecl)) {
Douglas Gregor91155082012-11-14 22:29:20 +00002195 if (IsStructuralMatch(D, FoundEnumConstant))
2196 return Importer.Imported(D, FoundEnumConstant);
2197 }
2198
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002199 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00002200 }
2201
2202 if (!ConflictingDecls.empty()) {
2203 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2204 ConflictingDecls.data(),
2205 ConflictingDecls.size());
2206 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002207 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002208 }
2209 }
2210
2211 Expr *Init = Importer.Import(D->getInitExpr());
2212 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002213 return nullptr;
2214
Douglas Gregor98c10182010-02-12 22:17:39 +00002215 EnumConstantDecl *ToEnumerator
2216 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2217 Name.getAsIdentifierInfo(), T,
2218 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002219 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002220 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002221 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002222 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002223 return ToEnumerator;
2224}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002225
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002226bool ASTNodeImporter::ImportTemplateInformation(FunctionDecl *FromFD,
2227 FunctionDecl *ToFD) {
2228 switch (FromFD->getTemplatedKind()) {
2229 case FunctionDecl::TK_NonTemplate:
2230 case FunctionDecl::TK_FunctionTemplate:
Sam McCallfdc32072018-01-26 12:06:44 +00002231 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002232
2233 case FunctionDecl::TK_MemberSpecialization: {
2234 auto *InstFD = cast_or_null<FunctionDecl>(
2235 Importer.Import(FromFD->getInstantiatedFromMemberFunction()));
2236 if (!InstFD)
2237 return true;
2238
2239 TemplateSpecializationKind TSK = FromFD->getTemplateSpecializationKind();
2240 SourceLocation POI = Importer.Import(
2241 FromFD->getMemberSpecializationInfo()->getPointOfInstantiation());
2242 ToFD->setInstantiationOfMemberFunction(InstFD, TSK);
2243 ToFD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002244 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002245 }
2246
2247 case FunctionDecl::TK_FunctionTemplateSpecialization: {
2248 auto *FTSInfo = FromFD->getTemplateSpecializationInfo();
2249 auto *Template = cast_or_null<FunctionTemplateDecl>(
2250 Importer.Import(FTSInfo->getTemplate()));
2251 if (!Template)
2252 return true;
2253 TemplateSpecializationKind TSK = FTSInfo->getTemplateSpecializationKind();
2254
2255 // Import template arguments.
2256 auto TemplArgs = FTSInfo->TemplateArguments->asArray();
2257 SmallVector<TemplateArgument, 8> ToTemplArgs;
2258 if (ImportTemplateArguments(TemplArgs.data(), TemplArgs.size(),
2259 ToTemplArgs))
2260 return true;
2261
2262 TemplateArgumentList *ToTAList = TemplateArgumentList::CreateCopy(
2263 Importer.getToContext(), ToTemplArgs);
2264
2265 TemplateArgumentListInfo ToTAInfo;
2266 const auto *FromTAArgsAsWritten = FTSInfo->TemplateArgumentsAsWritten;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002267 if (FromTAArgsAsWritten)
2268 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002269 return true;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002270
2271 SourceLocation POI = Importer.Import(FTSInfo->getPointOfInstantiation());
2272
2273 ToFD->setFunctionTemplateSpecialization(
2274 Template, ToTAList, /* InsertPos= */ nullptr,
2275 TSK, FromTAArgsAsWritten ? &ToTAInfo : nullptr, POI);
Sam McCallfdc32072018-01-26 12:06:44 +00002276 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002277 }
2278
2279 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
2280 auto *FromInfo = FromFD->getDependentSpecializationInfo();
2281 UnresolvedSet<8> TemplDecls;
2282 unsigned NumTemplates = FromInfo->getNumTemplates();
2283 for (unsigned I = 0; I < NumTemplates; I++) {
2284 if (auto *ToFTD = cast_or_null<FunctionTemplateDecl>(
2285 Importer.Import(FromInfo->getTemplate(I))))
2286 TemplDecls.addDecl(ToFTD);
2287 else
2288 return true;
2289 }
2290
2291 // Import TemplateArgumentListInfo.
2292 TemplateArgumentListInfo ToTAInfo;
2293 if (ImportTemplateArgumentListInfo(
2294 FromInfo->getLAngleLoc(), FromInfo->getRAngleLoc(),
2295 llvm::makeArrayRef(FromInfo->getTemplateArgs(),
2296 FromInfo->getNumTemplateArgs()),
2297 ToTAInfo))
2298 return true;
2299
2300 ToFD->setDependentTemplateSpecialization(Importer.getToContext(),
2301 TemplDecls, ToTAInfo);
Sam McCallfdc32072018-01-26 12:06:44 +00002302 return false;
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002303 }
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002304 }
Sam McCallfdc32072018-01-26 12:06:44 +00002305 llvm_unreachable("All cases should be covered!");
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002306}
2307
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002308Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2309 // Import the major distinguishing characteristics of this function.
2310 DeclContext *DC, *LexicalDC;
2311 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002312 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002313 NamedDecl *ToD;
2314 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002315 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002316 if (ToD)
2317 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002318
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002319 const FunctionDecl *FoundWithoutBody = nullptr;
2320
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002321 // Try to find a function in our own ("to") context with the same name, same
2322 // type, and in the same context as the function we're importing.
2323 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002324 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002325 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002326 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002327 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002328 for (auto *FoundDecl : FoundDecls) {
2329 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002330 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002331
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002332 if (auto *FoundFunction = dyn_cast<FunctionDecl>(FoundDecl)) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002333 if (FoundFunction->hasExternalFormalLinkage() &&
2334 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002335 if (Importer.IsStructurallyEquivalent(D->getType(),
2336 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002337 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002338 const FunctionDecl *FromBodyDecl = nullptr;
2339 D->hasBody(FromBodyDecl);
2340 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2341 // This function is needed to merge completely.
2342 FoundWithoutBody = FoundFunction;
2343 break;
2344 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002345 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002346 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002347
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002348 // FIXME: Check for overloading more carefully, e.g., by boosting
2349 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002350
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002351 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002352 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002353 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002354
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002355 // Complain about inconsistent function types.
2356 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002357 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002358 Importer.ToDiag(FoundFunction->getLocation(),
2359 diag::note_odr_value_here)
2360 << FoundFunction->getType();
2361 }
2362 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002363
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002364 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002365 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002366
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002367 if (!ConflictingDecls.empty()) {
2368 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2369 ConflictingDecls.data(),
2370 ConflictingDecls.size());
2371 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002372 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002373 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002374 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002375
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002376 DeclarationNameInfo NameInfo(Name, Loc);
2377 // Import additional name location/type info.
2378 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2379
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002380 QualType FromTy = D->getType();
2381 bool usedDifferentExceptionSpec = false;
2382
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002383 if (const auto *FromFPT = D->getType()->getAs<FunctionProtoType>()) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002384 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2385 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2386 // FunctionDecl that we are importing the FunctionProtoType for.
2387 // To avoid an infinite recursion when importing, create the FunctionDecl
2388 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002389 if (FromEPI.ExceptionSpec.SourceDecl ||
2390 FromEPI.ExceptionSpec.SourceTemplate ||
2391 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002392 FunctionProtoType::ExtProtoInfo DefaultEPI;
2393 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002394 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002395 usedDifferentExceptionSpec = true;
2396 }
2397 }
2398
Douglas Gregorb4964f72010-02-15 23:54:17 +00002399 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002400 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002401 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002402 return nullptr;
2403
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002404 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002405 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002406 for (auto P : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002407 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002408 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002409 return nullptr;
2410
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002411 Parameters.push_back(ToP);
2412 }
2413
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002414 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002415 if (D->getTypeSourceInfo() && !TInfo)
2416 return nullptr;
2417
2418 // Create the imported function.
Craig Topper36250ad2014-05-12 05:36:57 +00002419 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002420 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002421 if (auto *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002422 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2423 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002424 InnerLocStart,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002425 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002426 FromConstructor->isExplicit(),
2427 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002428 D->isImplicit(),
2429 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002430 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2431 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002432 for (auto *I : FromConstructor->inits()) {
2433 auto *ToI = cast_or_null<CXXCtorInitializer>(Importer.Import(I));
Sean Callanandd2c1742016-05-16 20:48:03 +00002434 if (!ToI && I)
2435 return nullptr;
2436 CtorInitializers.push_back(ToI);
2437 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002438 auto **Memory =
Sean Callanandd2c1742016-05-16 20:48:03 +00002439 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2440 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002441 auto *ToCtor = cast<CXXConstructorDecl>(ToFunction);
Sean Callanandd2c1742016-05-16 20:48:03 +00002442 ToCtor->setCtorInitializers(Memory);
2443 ToCtor->setNumCtorInitializers(NumInitializers);
2444 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002445 } else if (isa<CXXDestructorDecl>(D)) {
2446 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2447 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002448 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002449 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002450 D->isInlineSpecified(),
2451 D->isImplicit());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002452 } else if (auto *FromConversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor00eace12010-02-21 18:29:16 +00002453 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2454 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002455 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002456 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002457 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002458 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002459 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002460 Importer.Import(D->getLocEnd()));
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002461 } else if (auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora50ad132010-11-29 16:04:58 +00002462 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2463 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002464 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002465 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002466 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002467 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002468 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002469 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002470 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002471 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002472 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002473 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002474 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002475 D->hasWrittenPrototype(),
2476 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002477 }
John McCall3e11ebe2010-03-15 10:12:16 +00002478
2479 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002480 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002481 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002482 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002483 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2484 ToFunction->setTrivial(D->isTrivial());
2485 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002486 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002487
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002488 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002489 for (auto *Param : Parameters) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002490 Param->setOwningFunction(ToFunction);
2491 ToFunction->addDeclInternal(Param);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002492 }
David Blaikie9c70e042011-09-21 18:16:56 +00002493 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002494
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002495 if (FoundWithoutBody) {
2496 auto *Recent = const_cast<FunctionDecl *>(
2497 FoundWithoutBody->getMostRecentDecl());
2498 ToFunction->setPreviousDecl(Recent);
2499 }
2500
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002501 // We need to complete creation of FunctionProtoTypeLoc manually with setting
2502 // params it refers to.
2503 if (TInfo) {
2504 if (auto ProtoLoc =
2505 TInfo->getTypeLoc().IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
2506 for (unsigned I = 0, N = Parameters.size(); I != N; ++I)
2507 ProtoLoc.setParam(I, Parameters[I]);
2508 }
2509 }
2510
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002511 if (usedDifferentExceptionSpec) {
2512 // Update FunctionProtoType::ExtProtoInfo.
2513 QualType T = Importer.Import(D->getType());
2514 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002515 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002516 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002517 }
2518
Sean Callanan59721b32015-04-28 18:41:46 +00002519 // Import the body, if any.
2520 if (Stmt *FromBody = D->getBody()) {
2521 if (Stmt *ToBody = Importer.Import(FromBody)) {
2522 ToFunction->setBody(ToBody);
2523 }
2524 }
2525
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002526 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002527
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002528 // If it is a template, import all related things.
2529 if (ImportTemplateInformation(D, ToFunction))
2530 return nullptr;
2531
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002532 // Add this function to the lexical context.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00002533 // NOTE: If the function is templated declaration, it should be not added into
2534 // LexicalDC. But described template is imported during import of
2535 // FunctionTemplateDecl (it happens later). So, we use source declaration
2536 // to determine if we should add the result function.
2537 if (!D->getDescribedFunctionTemplate())
2538 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002539
Lang Hames19e07e12017-06-20 21:06:00 +00002540 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2541 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2542
Douglas Gregor43f54792010-02-17 02:12:47 +00002543 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002544}
2545
Douglas Gregor00eace12010-02-21 18:29:16 +00002546Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2547 return VisitFunctionDecl(D);
2548}
2549
2550Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2551 return VisitCXXMethodDecl(D);
2552}
2553
2554Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2555 return VisitCXXMethodDecl(D);
2556}
2557
2558Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2559 return VisitCXXMethodDecl(D);
2560}
2561
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002562static unsigned getFieldIndex(Decl *F) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002563 auto *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002564 if (!Owner)
2565 return 0;
2566
2567 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002568 for (const auto *D : Owner->noload_decls()) {
2569 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002570 return Index;
2571
2572 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2573 ++Index;
2574 }
2575
2576 return Index;
2577}
2578
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2580 // Import the major distinguishing characteristics of a variable.
2581 DeclContext *DC, *LexicalDC;
2582 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002583 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002584 NamedDecl *ToD;
2585 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002586 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002587 if (ToD)
2588 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002589
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002590 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002591 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002592 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002593 for (auto *FoundDecl : FoundDecls) {
2594 if (auto *FoundField = dyn_cast<FieldDecl>(FoundDecl)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002595 // For anonymous fields, match up by index.
2596 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2597 continue;
2598
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002599 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002600 FoundField->getType())) {
2601 Importer.Imported(D, FoundField);
2602 return FoundField;
2603 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002604
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002605 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2606 << Name << D->getType() << FoundField->getType();
2607 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2608 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002609 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002610 }
2611 }
2612
Douglas Gregorb4964f72010-02-15 23:54:17 +00002613 // Import the type.
2614 QualType T = Importer.Import(D->getType());
2615 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002616 return nullptr;
2617
Douglas Gregor5c73e912010-02-11 00:48:18 +00002618 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2619 Expr *BitWidth = Importer.Import(D->getBitWidth());
2620 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002621 return nullptr;
2622
Abramo Bagnaradff19302011-03-08 08:55:46 +00002623 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2624 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002625 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002626 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002627 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002628 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002629 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002630 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002631 Expr *ToInitializer = Importer.Import(FromInitializer);
2632 if (ToInitializer)
2633 ToField->setInClassInitializer(ToInitializer);
2634 else
2635 return nullptr;
2636 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002637 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002638 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002639 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002640 return ToField;
2641}
2642
Francois Pichet783dd6e2010-11-21 06:08:52 +00002643Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2644 // Import the major distinguishing characteristics of a variable.
2645 DeclContext *DC, *LexicalDC;
2646 DeclarationName Name;
2647 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002648 NamedDecl *ToD;
2649 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002650 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002651 if (ToD)
2652 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002653
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002654 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002655 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002656 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002657 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002658 if (auto *FoundField = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002659 // For anonymous indirect fields, match up by index.
2660 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2661 continue;
2662
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002663 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002664 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002665 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002666 Importer.Imported(D, FoundField);
2667 return FoundField;
2668 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002669
2670 // If there are more anonymous fields to check, continue.
2671 if (!Name && I < N-1)
2672 continue;
2673
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002674 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2675 << Name << D->getType() << FoundField->getType();
2676 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2677 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002678 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002679 }
2680 }
2681
Francois Pichet783dd6e2010-11-21 06:08:52 +00002682 // Import the type.
2683 QualType T = Importer.Import(D->getType());
2684 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002685 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002686
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002687 auto **NamedChain =
2688 new (Importer.getToContext()) NamedDecl*[D->getChainingSize()];
Francois Pichet783dd6e2010-11-21 06:08:52 +00002689
2690 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002691 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002692 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002693 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002694 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002695 NamedChain[i++] = cast<NamedDecl>(D);
2696 }
2697
2698 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002699 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002700 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002701
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00002702 for (const auto *A : D->attrs())
2703 ToIndirectField->addAttr(Importer.Import(A));
Aaron Ballman260995b2014-10-15 16:58:18 +00002704
Francois Pichet783dd6e2010-11-21 06:08:52 +00002705 ToIndirectField->setAccess(D->getAccess());
2706 ToIndirectField->setLexicalDeclContext(LexicalDC);
2707 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002708 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002709 return ToIndirectField;
2710}
2711
Aleksei Sidorina693b372016-09-28 10:16:56 +00002712Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2713 // Import the major distinguishing characteristics of a declaration.
2714 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2715 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2716 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2717 if (!DC || !LexicalDC)
2718 return nullptr;
2719
2720 // Determine whether we've already imported this decl.
2721 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2722 auto *RD = cast<CXXRecordDecl>(DC);
2723 FriendDecl *ImportedFriend = RD->getFirstFriend();
2724 StructuralEquivalenceContext Context(
2725 Importer.getFromContext(), Importer.getToContext(),
2726 Importer.getNonEquivalentDecls(), false, false);
2727
2728 while (ImportedFriend) {
2729 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2730 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2731 ImportedFriend->getFriendDecl()))
2732 return Importer.Imported(D, ImportedFriend);
2733
2734 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2735 if (Importer.IsStructurallyEquivalent(
2736 D->getFriendType()->getType(),
2737 ImportedFriend->getFriendType()->getType(), true))
2738 return Importer.Imported(D, ImportedFriend);
2739 }
2740 ImportedFriend = ImportedFriend->getNextFriend();
2741 }
2742
2743 // Not found. Create it.
2744 FriendDecl::FriendUnion ToFU;
Peter Szecsib180eeb2018-04-25 17:28:03 +00002745 if (NamedDecl *FriendD = D->getFriendDecl()) {
2746 auto *ToFriendD = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2747 if (ToFriendD && FriendD->getFriendObjectKind() != Decl::FOK_None &&
2748 !(FriendD->isInIdentifierNamespace(Decl::IDNS_NonMemberOperator)))
2749 ToFriendD->setObjectOfFriendDecl(false);
2750
2751 ToFU = ToFriendD;
2752 } else // The friend is a type, not a decl.
Aleksei Sidorina693b372016-09-28 10:16:56 +00002753 ToFU = Importer.Import(D->getFriendType());
2754 if (!ToFU)
2755 return nullptr;
2756
2757 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002758 auto **FromTPLists = D->getTrailingObjects<TemplateParameterList *>();
Aleksei Sidorina693b372016-09-28 10:16:56 +00002759 for (unsigned I = 0; I < D->NumTPLists; I++) {
2760 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2761 if (!List)
2762 return nullptr;
2763 ToTPLists[I] = List;
2764 }
2765
2766 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2767 Importer.Import(D->getLocation()),
2768 ToFU, Importer.Import(D->getFriendLoc()),
2769 ToTPLists);
2770
2771 Importer.Imported(D, FrD);
Aleksei Sidorina693b372016-09-28 10:16:56 +00002772
2773 FrD->setAccess(D->getAccess());
2774 FrD->setLexicalDeclContext(LexicalDC);
2775 LexicalDC->addDeclInternal(FrD);
2776 return FrD;
2777}
2778
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002779Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2780 // Import the major distinguishing characteristics of an ivar.
2781 DeclContext *DC, *LexicalDC;
2782 DeclarationName Name;
2783 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002784 NamedDecl *ToD;
2785 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002786 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002787 if (ToD)
2788 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002789
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002790 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002791 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002792 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002793 for (auto *FoundDecl : FoundDecls) {
2794 if (auto *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecl)) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002795 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002796 FoundIvar->getType())) {
2797 Importer.Imported(D, FoundIvar);
2798 return FoundIvar;
2799 }
2800
2801 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2802 << Name << D->getType() << FoundIvar->getType();
2803 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2804 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002805 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002806 }
2807 }
2808
2809 // Import the type.
2810 QualType T = Importer.Import(D->getType());
2811 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002812 return nullptr;
2813
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002814 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2815 Expr *BitWidth = Importer.Import(D->getBitWidth());
2816 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002817 return nullptr;
2818
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002819 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2820 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002821 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002822 Loc, Name.getAsIdentifierInfo(),
2823 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002824 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002825 ToIvar->setLexicalDeclContext(LexicalDC);
2826 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002827 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002828 return ToIvar;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002829}
2830
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002831Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2832 // Import the major distinguishing characteristics of a variable.
2833 DeclContext *DC, *LexicalDC;
2834 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002835 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002836 NamedDecl *ToD;
2837 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002838 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002839 if (ToD)
2840 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002841
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002842 // Try to find a variable in our own ("to") context with the same name and
2843 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002844 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002845 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002846 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002847 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002848 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002849 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002850 for (auto *FoundDecl : FoundDecls) {
2851 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002852 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002853
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002854 if (auto *FoundVar = dyn_cast<VarDecl>(FoundDecl)) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002855 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002856 if (FoundVar->hasExternalFormalLinkage() &&
2857 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002858 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002859 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002860 MergeWithVar = FoundVar;
2861 break;
2862 }
2863
Douglas Gregor56521c52010-02-12 17:23:39 +00002864 const ArrayType *FoundArray
2865 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2866 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002867 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002868 if (FoundArray && TArray) {
2869 if (isa<IncompleteArrayType>(FoundArray) &&
2870 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002871 // Import the type.
2872 QualType T = Importer.Import(D->getType());
2873 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002874 return nullptr;
2875
Douglas Gregor56521c52010-02-12 17:23:39 +00002876 FoundVar->setType(T);
2877 MergeWithVar = FoundVar;
2878 break;
2879 } else if (isa<IncompleteArrayType>(TArray) &&
2880 isa<ConstantArrayType>(FoundArray)) {
2881 MergeWithVar = FoundVar;
2882 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002883 }
2884 }
2885
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002886 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002887 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002888 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2889 << FoundVar->getType();
2890 }
2891 }
2892
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00002893 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002894 }
2895
2896 if (MergeWithVar) {
2897 // An equivalent variable with external linkage has been found. Link
2898 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002899 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002900
2901 if (VarDecl *DDef = D->getDefinition()) {
2902 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2903 Importer.ToDiag(ExistingDef->getLocation(),
2904 diag::err_odr_variable_multiple_def)
2905 << Name;
2906 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2907 } else {
2908 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002909 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002910 if (DDef->isInitKnownICE()) {
2911 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2912 Eval->CheckedICE = true;
2913 Eval->IsICE = DDef->isInitICE();
2914 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002915 }
2916 }
2917
2918 return MergeWithVar;
2919 }
2920
2921 if (!ConflictingDecls.empty()) {
2922 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2923 ConflictingDecls.data(),
2924 ConflictingDecls.size());
2925 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002926 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002927 }
2928 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002929
Douglas Gregorb4964f72010-02-15 23:54:17 +00002930 // Import the type.
2931 QualType T = Importer.Import(D->getType());
2932 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002933 return nullptr;
2934
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002935 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002936 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002937 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2938 Importer.Import(D->getInnerLocStart()),
2939 Loc, Name.getAsIdentifierInfo(),
2940 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002941 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002942 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002943 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002944 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002945 Importer.Imported(D, ToVar);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00002946
2947 // Templated declarations should never appear in the enclosing DeclContext.
2948 if (!D->getDescribedVarTemplate())
2949 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002950
Sean Callanan59721b32015-04-28 18:41:46 +00002951 if (!D->isFileVarDecl() &&
2952 D->isUsed())
2953 ToVar->setIsUsed();
2954
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002955 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002956 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002957 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002958
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002959 if (D->isConstexpr())
2960 ToVar->setConstexpr(true);
2961
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002962 return ToVar;
2963}
2964
Douglas Gregor8b228d72010-02-17 21:22:52 +00002965Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2966 // Parameters are created in the translation unit's context, then moved
2967 // into the function declaration's context afterward.
2968 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2969
2970 // Import the name of this declaration.
2971 DeclarationName Name = Importer.Import(D->getDeclName());
2972 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002973 return nullptr;
2974
Douglas Gregor8b228d72010-02-17 21:22:52 +00002975 // Import the location of this declaration.
2976 SourceLocation Loc = Importer.Import(D->getLocation());
2977
2978 // Import the parameter's type.
2979 QualType T = Importer.Import(D->getType());
2980 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002981 return nullptr;
2982
Douglas Gregor8b228d72010-02-17 21:22:52 +00002983 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002984 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2985 Name.getAsIdentifierInfo(), T,
2986 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002987 return Importer.Imported(D, ToParm);
2988}
2989
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002990Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2991 // Parameters are created in the translation unit's context, then moved
2992 // into the function declaration's context afterward.
2993 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2994
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002995 // Import the name of this declaration.
2996 DeclarationName Name = Importer.Import(D->getDeclName());
2997 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002998 return nullptr;
2999
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003000 // Import the location of this declaration.
3001 SourceLocation Loc = Importer.Import(D->getLocation());
3002
3003 // Import the parameter's type.
3004 QualType T = Importer.Import(D->getType());
3005 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003006 return nullptr;
3007
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003008 // Create the imported parameter.
3009 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3010 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003011 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003012 Loc, Name.getAsIdentifierInfo(),
3013 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003014 /*DefaultArg*/ nullptr);
3015
3016 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00003017 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00003018 ToParm->setKNRPromoted(D->isKNRPromoted());
3019
3020 Expr *ToDefArg = nullptr;
3021 Expr *FromDefArg = nullptr;
3022 if (D->hasUninstantiatedDefaultArg()) {
3023 FromDefArg = D->getUninstantiatedDefaultArg();
3024 ToDefArg = Importer.Import(FromDefArg);
3025 ToParm->setUninstantiatedDefaultArg(ToDefArg);
3026 } else if (D->hasUnparsedDefaultArg()) {
3027 ToParm->setUnparsedDefaultArg();
3028 } else if (D->hasDefaultArg()) {
3029 FromDefArg = D->getDefaultArg();
3030 ToDefArg = Importer.Import(FromDefArg);
3031 ToParm->setDefaultArg(ToDefArg);
3032 }
3033 if (FromDefArg && !ToDefArg)
3034 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003035
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00003036 if (D->isObjCMethodParameter()) {
3037 ToParm->setObjCMethodScopeInfo(D->getFunctionScopeIndex());
3038 ToParm->setObjCDeclQualifier(D->getObjCDeclQualifier());
3039 } else {
3040 ToParm->setScopeInfo(D->getFunctionScopeDepth(),
3041 D->getFunctionScopeIndex());
3042 }
3043
Sean Callanan59721b32015-04-28 18:41:46 +00003044 if (D->isUsed())
3045 ToParm->setIsUsed();
3046
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003047 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003048}
3049
Douglas Gregor43f54792010-02-17 02:12:47 +00003050Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3051 // Import the major distinguishing characteristics of a method.
3052 DeclContext *DC, *LexicalDC;
3053 DeclarationName Name;
3054 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003055 NamedDecl *ToD;
3056 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003057 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003058 if (ToD)
3059 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003060
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003061 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003062 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003063 for (auto *FoundDecl : FoundDecls) {
3064 if (auto *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecl)) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3066 continue;
3067
3068 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003069 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3070 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003071 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003072 << D->isInstanceMethod() << Name << D->getReturnType()
3073 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003074 Importer.ToDiag(FoundMethod->getLocation(),
3075 diag::note_odr_objc_method_here)
3076 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003077 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003078 }
3079
3080 // Check the number of parameters.
3081 if (D->param_size() != FoundMethod->param_size()) {
3082 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3083 << D->isInstanceMethod() << Name
3084 << D->param_size() << FoundMethod->param_size();
3085 Importer.ToDiag(FoundMethod->getLocation(),
3086 diag::note_odr_objc_method_here)
3087 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003088 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003089 }
3090
3091 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003092 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003093 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3094 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003095 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003096 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003097 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00003098 diag::err_odr_objc_method_param_type_inconsistent)
3099 << D->isInstanceMethod() << Name
3100 << (*P)->getType() << (*FoundP)->getType();
3101 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3102 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003103 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003104 }
3105 }
3106
3107 // Check variadic/non-variadic.
3108 // Check the number of parameters.
3109 if (D->isVariadic() != FoundMethod->isVariadic()) {
3110 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3111 << D->isInstanceMethod() << Name;
3112 Importer.ToDiag(FoundMethod->getLocation(),
3113 diag::note_odr_objc_method_here)
3114 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003115 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003116 }
3117
3118 // FIXME: Any other bits we need to merge?
3119 return Importer.Imported(D, FoundMethod);
3120 }
3121 }
3122
3123 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003124 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003125 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003126 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003127
Alp Toker314cc812014-01-25 16:55:45 +00003128 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003129
Alp Toker314cc812014-01-25 16:55:45 +00003130 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3131 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3132 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3133 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3134 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003135
3136 // FIXME: When we decide to merge method definitions, we'll need to
3137 // deal with implicit parameters.
3138
3139 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003140 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00003141 for (auto *FromP : D->parameters()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003142 auto *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003143 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003144 return nullptr;
3145
Douglas Gregor43f54792010-02-17 02:12:47 +00003146 ToParams.push_back(ToP);
3147 }
3148
3149 // Set the parameters.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003150 for (auto *ToParam : ToParams) {
3151 ToParam->setOwningFunction(ToMethod);
3152 ToMethod->addDeclInternal(ToParam);
Douglas Gregor43f54792010-02-17 02:12:47 +00003153 }
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003154
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003155 SmallVector<SourceLocation, 12> SelLocs;
3156 D->getSelectorLocs(SelLocs);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003157 for (auto &Loc : SelLocs)
Aleksei Sidorin47dbaf62018-01-09 14:25:05 +00003158 Loc = Importer.Import(Loc);
3159
3160 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003161
3162 ToMethod->setLexicalDeclContext(LexicalDC);
3163 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003164 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003165 return ToMethod;
3166}
3167
Douglas Gregor85f3f952015-07-07 03:57:15 +00003168Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3169 // Import the major distinguishing characteristics of a category.
3170 DeclContext *DC, *LexicalDC;
3171 DeclarationName Name;
3172 SourceLocation Loc;
3173 NamedDecl *ToD;
3174 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3175 return nullptr;
3176 if (ToD)
3177 return ToD;
3178
3179 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3180 if (!BoundInfo)
3181 return nullptr;
3182
3183 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3184 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003185 D->getVariance(),
3186 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003187 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003188 Importer.Import(D->getLocation()),
3189 Name.getAsIdentifierInfo(),
3190 Importer.Import(D->getColonLoc()),
3191 BoundInfo);
3192 Importer.Imported(D, Result);
3193 Result->setLexicalDeclContext(LexicalDC);
3194 return Result;
3195}
3196
Douglas Gregor84c51c32010-02-18 01:47:50 +00003197Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3198 // Import the major distinguishing characteristics of a category.
3199 DeclContext *DC, *LexicalDC;
3200 DeclarationName Name;
3201 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003202 NamedDecl *ToD;
3203 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003204 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003205 if (ToD)
3206 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003207
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003208 auto *ToInterface =
3209 cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003210 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003211 return nullptr;
3212
Douglas Gregor84c51c32010-02-18 01:47:50 +00003213 // Determine if we've already encountered this category.
3214 ObjCCategoryDecl *MergeWithCategory
3215 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3216 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3217 if (!ToCategory) {
3218 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003219 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003220 Loc,
3221 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003222 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003223 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003224 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003225 Importer.Import(D->getIvarLBraceLoc()),
3226 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003227 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003228 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003229 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003230 // Import the type parameter list after calling Imported, to avoid
3231 // loops when bringing in their DeclContext.
3232 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3233 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003234
Douglas Gregor84c51c32010-02-18 01:47:50 +00003235 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003236 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3237 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003238 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3239 = D->protocol_loc_begin();
3240 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3241 FromProtoEnd = D->protocol_end();
3242 FromProto != FromProtoEnd;
3243 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003244 auto *ToProto =
3245 cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003246 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003247 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003248 Protocols.push_back(ToProto);
3249 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3250 }
3251
3252 // FIXME: If we're merging, make sure that the protocol list is the same.
3253 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3254 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregor84c51c32010-02-18 01:47:50 +00003255 } else {
3256 Importer.Imported(D, ToCategory);
3257 }
3258
3259 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003260 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003261
3262 // If we have an implementation, import it as well.
3263 if (D->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003264 auto *Impl =
3265 cast_or_null<ObjCCategoryImplDecl>(
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003266 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003267 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003268 return nullptr;
3269
Douglas Gregor84c51c32010-02-18 01:47:50 +00003270 ToCategory->setImplementation(Impl);
3271 }
3272
3273 return ToCategory;
3274}
3275
Douglas Gregor2aa53772012-01-24 17:42:07 +00003276bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3277 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003278 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003279 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003280 if (shouldForceImportDeclContext(Kind))
3281 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003282 return false;
3283 }
3284
3285 // Start the protocol definition
3286 To->startDefinition();
3287
3288 // Import protocols
3289 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3290 SmallVector<SourceLocation, 4> ProtocolLocs;
3291 ObjCProtocolDecl::protocol_loc_iterator
3292 FromProtoLoc = From->protocol_loc_begin();
3293 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3294 FromProtoEnd = From->protocol_end();
3295 FromProto != FromProtoEnd;
3296 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003297 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003298 if (!ToProto)
3299 return true;
3300 Protocols.push_back(ToProto);
3301 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3302 }
3303
3304 // FIXME: If we're merging, make sure that the protocol list is the same.
3305 To->setProtocolList(Protocols.data(), Protocols.size(),
3306 ProtocolLocs.data(), Importer.getToContext());
3307
Douglas Gregor2e15c842012-02-01 21:00:38 +00003308 if (shouldForceImportDeclContext(Kind)) {
3309 // Import all of the members of this protocol.
3310 ImportDeclContext(From, /*ForceImport=*/true);
3311 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003312 return false;
3313}
3314
Douglas Gregor98d156a2010-02-17 16:12:00 +00003315Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003316 // If this protocol has a definition in the translation unit we're coming
3317 // from, but this particular declaration is not that definition, import the
3318 // definition and map to that.
3319 ObjCProtocolDecl *Definition = D->getDefinition();
3320 if (Definition && Definition != D) {
3321 Decl *ImportedDef = Importer.Import(Definition);
3322 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003323 return nullptr;
3324
Douglas Gregor2aa53772012-01-24 17:42:07 +00003325 return Importer.Imported(D, ImportedDef);
3326 }
3327
Douglas Gregor84c51c32010-02-18 01:47:50 +00003328 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003329 DeclContext *DC, *LexicalDC;
3330 DeclarationName Name;
3331 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003332 NamedDecl *ToD;
3333 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003334 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003335 if (ToD)
3336 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003337
Craig Topper36250ad2014-05-12 05:36:57 +00003338 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003339 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003340 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003341 for (auto *FoundDecl : FoundDecls) {
3342 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003343 continue;
3344
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003345 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecl)))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003346 break;
3347 }
3348
3349 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003350 if (!ToProto) {
3351 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3352 Name.getAsIdentifierInfo(), Loc,
3353 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003354 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003355 ToProto->setLexicalDeclContext(LexicalDC);
3356 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003357 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003358
3359 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003360
Douglas Gregor2aa53772012-01-24 17:42:07 +00003361 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003362 return nullptr;
3363
Douglas Gregor98d156a2010-02-17 16:12:00 +00003364 return ToProto;
3365}
3366
Sean Callanan0aae0412014-12-10 00:00:37 +00003367Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3368 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3369 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3370
3371 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3372 SourceLocation LangLoc = Importer.Import(D->getLocation());
3373
3374 bool HasBraces = D->hasBraces();
3375
Sean Callananb12a8552014-12-10 21:22:20 +00003376 LinkageSpecDecl *ToLinkageSpec =
3377 LinkageSpecDecl::Create(Importer.getToContext(),
3378 DC,
3379 ExternLoc,
3380 LangLoc,
3381 D->getLanguage(),
3382 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003383
3384 if (HasBraces) {
3385 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3386 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3387 }
3388
3389 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3390 LexicalDC->addDeclInternal(ToLinkageSpec);
3391
3392 Importer.Imported(D, ToLinkageSpec);
3393
3394 return ToLinkageSpec;
3395}
3396
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003397Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3398 DeclContext *DC, *LexicalDC;
3399 DeclarationName Name;
3400 SourceLocation Loc;
3401 NamedDecl *ToD = nullptr;
3402 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3403 return nullptr;
3404 if (ToD)
3405 return ToD;
3406
3407 DeclarationNameInfo NameInfo(Name,
3408 Importer.Import(D->getNameInfo().getLoc()));
3409 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3410
3411 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3412 Importer.Import(D->getUsingLoc()),
3413 Importer.Import(D->getQualifierLoc()),
3414 NameInfo, D->hasTypename());
3415 ToUsing->setLexicalDeclContext(LexicalDC);
3416 LexicalDC->addDeclInternal(ToUsing);
3417 Importer.Imported(D, ToUsing);
3418
3419 if (NamedDecl *FromPattern =
3420 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003421 if (auto *ToPattern =
3422 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003423 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3424 else
3425 return nullptr;
3426 }
3427
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003428 for (auto *FromShadow : D->shadows()) {
3429 if (auto *ToShadow =
3430 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003431 ToUsing->addShadowDecl(ToShadow);
3432 else
3433 // FIXME: We return a nullptr here but the definition is already created
3434 // and available with lookups. How to fix this?..
3435 return nullptr;
3436 }
3437 return ToUsing;
3438}
3439
3440Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3441 DeclContext *DC, *LexicalDC;
3442 DeclarationName Name;
3443 SourceLocation Loc;
3444 NamedDecl *ToD = nullptr;
3445 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3446 return nullptr;
3447 if (ToD)
3448 return ToD;
3449
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003450 auto *ToUsing = dyn_cast_or_null<UsingDecl>(
3451 Importer.Import(D->getUsingDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003452 if (!ToUsing)
3453 return nullptr;
3454
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003455 auto *ToTarget = dyn_cast_or_null<NamedDecl>(
3456 Importer.Import(D->getTargetDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003457 if (!ToTarget)
3458 return nullptr;
3459
3460 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3461 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3462
3463 ToShadow->setLexicalDeclContext(LexicalDC);
3464 ToShadow->setAccess(D->getAccess());
3465 Importer.Imported(D, ToShadow);
3466
3467 if (UsingShadowDecl *FromPattern =
3468 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003469 if (auto *ToPattern =
3470 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003471 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3472 ToPattern);
3473 else
3474 // FIXME: We return a nullptr here but the definition is already created
3475 // and available with lookups. How to fix this?..
3476 return nullptr;
3477 }
3478
3479 LexicalDC->addDeclInternal(ToShadow);
3480
3481 return ToShadow;
3482}
3483
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003484Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3485 DeclContext *DC, *LexicalDC;
3486 DeclarationName Name;
3487 SourceLocation Loc;
3488 NamedDecl *ToD = nullptr;
3489 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3490 return nullptr;
3491 if (ToD)
3492 return ToD;
3493
3494 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3495 if (!ToComAncestor)
3496 return nullptr;
3497
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003498 auto *ToNominated = cast_or_null<NamespaceDecl>(
3499 Importer.Import(D->getNominatedNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003500 if (!ToNominated)
3501 return nullptr;
3502
3503 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3504 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3505 Importer.Import(D->getNamespaceKeyLocation()),
3506 Importer.Import(D->getQualifierLoc()),
3507 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3508 ToUsingDir->setLexicalDeclContext(LexicalDC);
3509 LexicalDC->addDeclInternal(ToUsingDir);
3510 Importer.Imported(D, ToUsingDir);
3511
3512 return ToUsingDir;
3513}
3514
3515Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3516 UnresolvedUsingValueDecl *D) {
3517 DeclContext *DC, *LexicalDC;
3518 DeclarationName Name;
3519 SourceLocation Loc;
3520 NamedDecl *ToD = nullptr;
3521 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3522 return nullptr;
3523 if (ToD)
3524 return ToD;
3525
3526 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3527 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3528
3529 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3530 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3531 Importer.Import(D->getQualifierLoc()), NameInfo,
3532 Importer.Import(D->getEllipsisLoc()));
3533
3534 Importer.Imported(D, ToUsingValue);
3535 ToUsingValue->setAccess(D->getAccess());
3536 ToUsingValue->setLexicalDeclContext(LexicalDC);
3537 LexicalDC->addDeclInternal(ToUsingValue);
3538
3539 return ToUsingValue;
3540}
3541
3542Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3543 UnresolvedUsingTypenameDecl *D) {
3544 DeclContext *DC, *LexicalDC;
3545 DeclarationName Name;
3546 SourceLocation Loc;
3547 NamedDecl *ToD = nullptr;
3548 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3549 return nullptr;
3550 if (ToD)
3551 return ToD;
3552
3553 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3554 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3555 Importer.Import(D->getTypenameLoc()),
3556 Importer.Import(D->getQualifierLoc()), Loc, Name,
3557 Importer.Import(D->getEllipsisLoc()));
3558
3559 Importer.Imported(D, ToUsing);
3560 ToUsing->setAccess(D->getAccess());
3561 ToUsing->setLexicalDeclContext(LexicalDC);
3562 LexicalDC->addDeclInternal(ToUsing);
3563
3564 return ToUsing;
3565}
3566
Douglas Gregor2aa53772012-01-24 17:42:07 +00003567bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3568 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003569 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003570 if (To->getDefinition()) {
3571 // Check consistency of superclass.
3572 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3573 if (FromSuper) {
3574 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3575 if (!FromSuper)
3576 return true;
3577 }
3578
3579 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3580 if ((bool)FromSuper != (bool)ToSuper ||
3581 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3582 Importer.ToDiag(To->getLocation(),
3583 diag::err_odr_objc_superclass_inconsistent)
3584 << To->getDeclName();
3585 if (ToSuper)
3586 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3587 << To->getSuperClass()->getDeclName();
3588 else
3589 Importer.ToDiag(To->getLocation(),
3590 diag::note_odr_objc_missing_superclass);
3591 if (From->getSuperClass())
3592 Importer.FromDiag(From->getSuperClassLoc(),
3593 diag::note_odr_objc_superclass)
3594 << From->getSuperClass()->getDeclName();
3595 else
3596 Importer.FromDiag(From->getLocation(),
3597 diag::note_odr_objc_missing_superclass);
3598 }
3599
Douglas Gregor2e15c842012-02-01 21:00:38 +00003600 if (shouldForceImportDeclContext(Kind))
3601 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003602 return false;
3603 }
3604
3605 // Start the definition.
3606 To->startDefinition();
3607
3608 // If this class has a superclass, import it.
3609 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003610 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3611 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003612 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003613
3614 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003615 }
3616
3617 // Import protocols
3618 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3619 SmallVector<SourceLocation, 4> ProtocolLocs;
3620 ObjCInterfaceDecl::protocol_loc_iterator
3621 FromProtoLoc = From->protocol_loc_begin();
3622
3623 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3624 FromProtoEnd = From->protocol_end();
3625 FromProto != FromProtoEnd;
3626 ++FromProto, ++FromProtoLoc) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003627 auto *ToProto = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003628 if (!ToProto)
3629 return true;
3630 Protocols.push_back(ToProto);
3631 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3632 }
3633
3634 // FIXME: If we're merging, make sure that the protocol list is the same.
3635 To->setProtocolList(Protocols.data(), Protocols.size(),
3636 ProtocolLocs.data(), Importer.getToContext());
3637
3638 // Import categories. When the categories themselves are imported, they'll
3639 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003640 for (auto *Cat : From->known_categories())
3641 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003642
Douglas Gregor2aa53772012-01-24 17:42:07 +00003643 // If we have an @implementation, import it as well.
3644 if (From->getImplementation()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003645 auto *Impl = cast_or_null<ObjCImplementationDecl>(
3646 Importer.Import(From->getImplementation()));
Douglas Gregor2aa53772012-01-24 17:42:07 +00003647 if (!Impl)
3648 return true;
3649
3650 To->setImplementation(Impl);
3651 }
3652
Douglas Gregor2e15c842012-02-01 21:00:38 +00003653 if (shouldForceImportDeclContext(Kind)) {
3654 // Import all of the members of this class.
3655 ImportDeclContext(From, /*ForceImport=*/true);
3656 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003657 return false;
3658}
3659
Douglas Gregor85f3f952015-07-07 03:57:15 +00003660ObjCTypeParamList *
3661ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3662 if (!list)
3663 return nullptr;
3664
3665 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3666 for (auto fromTypeParam : *list) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003667 auto *toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3668 Importer.Import(fromTypeParam));
Douglas Gregor85f3f952015-07-07 03:57:15 +00003669 if (!toTypeParam)
3670 return nullptr;
3671
3672 toTypeParams.push_back(toTypeParam);
3673 }
3674
3675 return ObjCTypeParamList::create(Importer.getToContext(),
3676 Importer.Import(list->getLAngleLoc()),
3677 toTypeParams,
3678 Importer.Import(list->getRAngleLoc()));
3679}
3680
Douglas Gregor45635322010-02-16 01:20:57 +00003681Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003682 // If this class has a definition in the translation unit we're coming from,
3683 // but this particular declaration is not that definition, import the
3684 // definition and map to that.
3685 ObjCInterfaceDecl *Definition = D->getDefinition();
3686 if (Definition && Definition != D) {
3687 Decl *ImportedDef = Importer.Import(Definition);
3688 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003689 return nullptr;
3690
Douglas Gregor2aa53772012-01-24 17:42:07 +00003691 return Importer.Imported(D, ImportedDef);
3692 }
3693
Douglas Gregor45635322010-02-16 01:20:57 +00003694 // Import the major distinguishing characteristics of an @interface.
3695 DeclContext *DC, *LexicalDC;
3696 DeclarationName Name;
3697 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003698 NamedDecl *ToD;
3699 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003700 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003701 if (ToD)
3702 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003703
Douglas Gregor2aa53772012-01-24 17:42:07 +00003704 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003705 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003706 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003707 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003708 for (auto *FoundDecl : FoundDecls) {
3709 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003710 continue;
3711
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003712 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecl)))
Douglas Gregor45635322010-02-16 01:20:57 +00003713 break;
3714 }
3715
Douglas Gregor2aa53772012-01-24 17:42:07 +00003716 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003717 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003718 if (!ToIface) {
3719 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3720 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003721 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003722 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003723 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003724 D->isImplicitInterfaceDecl());
3725 ToIface->setLexicalDeclContext(LexicalDC);
3726 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003727 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003728 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003729 // Import the type parameter list after calling Imported, to avoid
3730 // loops when bringing in their DeclContext.
3731 ToIface->setTypeParamList(ImportObjCTypeParamList(
3732 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003733
Douglas Gregor2aa53772012-01-24 17:42:07 +00003734 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003735 return nullptr;
3736
Douglas Gregor98d156a2010-02-17 16:12:00 +00003737 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003738}
3739
Douglas Gregor4da9d682010-12-07 15:32:12 +00003740Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003741 auto *Category = cast_or_null<ObjCCategoryDecl>(
3742 Importer.Import(D->getCategoryDecl()));
Douglas Gregor4da9d682010-12-07 15:32:12 +00003743 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003744 return nullptr;
3745
Douglas Gregor4da9d682010-12-07 15:32:12 +00003746 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3747 if (!ToImpl) {
3748 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3749 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003750 return nullptr;
3751
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003752 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003753 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003754 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003755 Category->getClassInterface(),
3756 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003757 Importer.Import(D->getAtStartLoc()),
3758 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003759
3760 DeclContext *LexicalDC = DC;
3761 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3762 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3763 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003764 return nullptr;
3765
Douglas Gregor4da9d682010-12-07 15:32:12 +00003766 ToImpl->setLexicalDeclContext(LexicalDC);
3767 }
3768
Sean Callanan95e74be2011-10-21 02:57:43 +00003769 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003770 Category->setImplementation(ToImpl);
3771 }
3772
3773 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003774 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003775 return ToImpl;
3776}
3777
Douglas Gregorda8025c2010-12-07 01:26:03 +00003778Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3779 // Find the corresponding interface.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003780 auto *Iface = cast_or_null<ObjCInterfaceDecl>(
3781 Importer.Import(D->getClassInterface()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003782 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003783 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003784
3785 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003786 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003787 if (D->getSuperClass()) {
3788 Super = cast_or_null<ObjCInterfaceDecl>(
3789 Importer.Import(D->getSuperClass()));
3790 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003791 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003792 }
3793
3794 ObjCImplementationDecl *Impl = Iface->getImplementation();
3795 if (!Impl) {
3796 // We haven't imported an implementation yet. Create a new @implementation
3797 // now.
3798 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3799 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003800 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003801 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003802 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003803 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003804 Importer.Import(D->getIvarLBraceLoc()),
3805 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003806
3807 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3808 DeclContext *LexicalDC
3809 = Importer.ImportContext(D->getLexicalDeclContext());
3810 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003811 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003812 Impl->setLexicalDeclContext(LexicalDC);
3813 }
3814
3815 // Associate the implementation with the class it implements.
3816 Iface->setImplementation(Impl);
3817 Importer.Imported(D, Iface->getImplementation());
3818 } else {
3819 Importer.Imported(D, Iface->getImplementation());
3820
3821 // Verify that the existing @implementation has the same superclass.
3822 if ((Super && !Impl->getSuperClass()) ||
3823 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003824 (Super && Impl->getSuperClass() &&
3825 !declaresSameEntity(Super->getCanonicalDecl(),
3826 Impl->getSuperClass()))) {
3827 Importer.ToDiag(Impl->getLocation(),
3828 diag::err_odr_objc_superclass_inconsistent)
3829 << Iface->getDeclName();
3830 // FIXME: It would be nice to have the location of the superclass
3831 // below.
3832 if (Impl->getSuperClass())
3833 Importer.ToDiag(Impl->getLocation(),
3834 diag::note_odr_objc_superclass)
3835 << Impl->getSuperClass()->getDeclName();
3836 else
3837 Importer.ToDiag(Impl->getLocation(),
3838 diag::note_odr_objc_missing_superclass);
3839 if (D->getSuperClass())
3840 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003841 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003842 << D->getSuperClass()->getDeclName();
3843 else
3844 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003845 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003846 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003847 }
3848 }
3849
3850 // Import all of the members of this @implementation.
3851 ImportDeclContext(D);
3852
3853 return Impl;
3854}
3855
Douglas Gregora11c4582010-02-17 18:02:10 +00003856Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3857 // Import the major distinguishing characteristics of an @property.
3858 DeclContext *DC, *LexicalDC;
3859 DeclarationName Name;
3860 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003861 NamedDecl *ToD;
3862 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003863 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003864 if (ToD)
3865 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003866
3867 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003868 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003869 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003870 for (auto *FoundDecl : FoundDecls) {
3871 if (auto *FoundProp = dyn_cast<ObjCPropertyDecl>(FoundDecl)) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003872 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003873 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003874 FoundProp->getType())) {
3875 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3876 << Name << D->getType() << FoundProp->getType();
3877 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3878 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003879 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003880 }
3881
3882 // FIXME: Check property attributes, getters, setters, etc.?
3883
3884 // Consider these properties to be equivalent.
3885 Importer.Imported(D, FoundProp);
3886 return FoundProp;
3887 }
3888 }
3889
3890 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003891 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3892 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003893 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003894
3895 // Create the new property.
3896 ObjCPropertyDecl *ToProperty
3897 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3898 Name.getAsIdentifierInfo(),
3899 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003900 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003901 Importer.Import(D->getType()),
3902 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003903 D->getPropertyImplementation());
3904 Importer.Imported(D, ToProperty);
3905 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003906 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003907
3908 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003909 ToProperty->setPropertyAttributesAsWritten(
3910 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003911 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3912 Importer.Import(D->getGetterNameLoc()));
3913 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3914 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003915 ToProperty->setGetterMethodDecl(
3916 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3917 ToProperty->setSetterMethodDecl(
3918 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3919 ToProperty->setPropertyIvarDecl(
3920 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3921 return ToProperty;
3922}
3923
Douglas Gregor14a49e22010-12-07 18:32:03 +00003924Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003925 auto *Property = cast_or_null<ObjCPropertyDecl>(
3926 Importer.Import(D->getPropertyDecl()));
Douglas Gregor14a49e22010-12-07 18:32:03 +00003927 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003928 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003929
3930 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3931 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003932 return nullptr;
3933
Douglas Gregor14a49e22010-12-07 18:32:03 +00003934 // Import the lexical declaration context.
3935 DeclContext *LexicalDC = DC;
3936 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3937 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3938 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003939 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003940 }
3941
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00003942 auto *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003943 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003944 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003945
3946 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003947 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003948 if (D->getPropertyIvarDecl()) {
3949 Ivar = cast_or_null<ObjCIvarDecl>(
3950 Importer.Import(D->getPropertyIvarDecl()));
3951 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003952 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003953 }
3954
3955 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003956 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3957 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003958 if (!ToImpl) {
3959 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3960 Importer.Import(D->getLocStart()),
3961 Importer.Import(D->getLocation()),
3962 Property,
3963 D->getPropertyImplementation(),
3964 Ivar,
3965 Importer.Import(D->getPropertyIvarDeclLoc()));
3966 ToImpl->setLexicalDeclContext(LexicalDC);
3967 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003968 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003969 } else {
3970 // Check that we have the same kind of property implementation (@synthesize
3971 // vs. @dynamic).
3972 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3973 Importer.ToDiag(ToImpl->getLocation(),
3974 diag::err_odr_objc_property_impl_kind_inconsistent)
3975 << Property->getDeclName()
3976 << (ToImpl->getPropertyImplementation()
3977 == ObjCPropertyImplDecl::Dynamic);
3978 Importer.FromDiag(D->getLocation(),
3979 diag::note_odr_objc_property_impl_kind)
3980 << D->getPropertyDecl()->getDeclName()
3981 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003982 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003983 }
3984
3985 // For @synthesize, check that we have the same
3986 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3987 Ivar != ToImpl->getPropertyIvarDecl()) {
3988 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3989 diag::err_odr_objc_synthesize_ivar_inconsistent)
3990 << Property->getDeclName()
3991 << ToImpl->getPropertyIvarDecl()->getDeclName()
3992 << Ivar->getDeclName();
3993 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3994 diag::note_odr_objc_synthesize_ivar_here)
3995 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003996 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003997 }
3998
3999 // Merge the existing implementation with the new implementation.
4000 Importer.Imported(D, ToImpl);
4001 }
4002
4003 return ToImpl;
4004}
4005
Douglas Gregora082a492010-11-30 19:14:50 +00004006Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4007 // For template arguments, we adopt the translation unit as our declaration
4008 // context. This context will be fixed when the actual template declaration
4009 // is created.
4010
4011 // FIXME: Import default argument.
4012 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4013 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004014 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004015 Importer.Import(D->getLocation()),
4016 D->getDepth(),
4017 D->getIndex(),
4018 Importer.Import(D->getIdentifier()),
4019 D->wasDeclaredWithTypename(),
4020 D->isParameterPack());
4021}
4022
4023Decl *
4024ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4025 // Import the name of this declaration.
4026 DeclarationName Name = Importer.Import(D->getDeclName());
4027 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004028 return nullptr;
4029
Douglas Gregora082a492010-11-30 19:14:50 +00004030 // Import the location of this declaration.
4031 SourceLocation Loc = Importer.Import(D->getLocation());
4032
4033 // Import the type of this declaration.
4034 QualType T = Importer.Import(D->getType());
4035 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004036 return nullptr;
4037
Douglas Gregora082a492010-11-30 19:14:50 +00004038 // Import type-source information.
4039 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4040 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004041 return nullptr;
4042
Douglas Gregora082a492010-11-30 19:14:50 +00004043 // FIXME: Import default argument.
4044
4045 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4046 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004047 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004048 Loc, D->getDepth(), D->getPosition(),
4049 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004050 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004051}
4052
4053Decl *
4054ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *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 template parameters.
4064 TemplateParameterList *TemplateParams
4065 = ImportTemplateParameterList(D->getTemplateParameters());
4066 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004067 return nullptr;
4068
Douglas Gregora082a492010-11-30 19:14:50 +00004069 // FIXME: Import default argument.
4070
4071 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4072 Importer.getToContext().getTranslationUnitDecl(),
4073 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004074 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004075 Name.getAsIdentifierInfo(),
4076 TemplateParams);
4077}
4078
Gabor Marton9581c332018-05-23 13:53:36 +00004079// Returns the definition for a (forward) declaration of a ClassTemplateDecl, if
4080// it has any definition in the redecl chain.
4081static ClassTemplateDecl *getDefinition(ClassTemplateDecl *D) {
4082 CXXRecordDecl *ToTemplatedDef = D->getTemplatedDecl()->getDefinition();
4083 if (!ToTemplatedDef)
4084 return nullptr;
4085 ClassTemplateDecl *TemplateWithDef =
4086 ToTemplatedDef->getDescribedClassTemplate();
4087 return TemplateWithDef;
4088}
4089
Douglas Gregora082a492010-11-30 19:14:50 +00004090Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4091 // If this record has a definition in the translation unit we're coming from,
4092 // but this particular declaration is not that definition, import the
4093 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004094 auto *Definition =
4095 cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
Douglas Gregora082a492010-11-30 19:14:50 +00004096 if (Definition && Definition != D->getTemplatedDecl()) {
4097 Decl *ImportedDef
4098 = Importer.Import(Definition->getDescribedClassTemplate());
4099 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004100 return nullptr;
4101
Douglas Gregora082a492010-11-30 19:14:50 +00004102 return Importer.Imported(D, ImportedDef);
4103 }
Gabor Marton9581c332018-05-23 13:53:36 +00004104
Douglas Gregora082a492010-11-30 19:14:50 +00004105 // Import the major distinguishing characteristics of this class template.
4106 DeclContext *DC, *LexicalDC;
4107 DeclarationName Name;
4108 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004109 NamedDecl *ToD;
4110 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004111 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004112 if (ToD)
4113 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004114
Douglas Gregora082a492010-11-30 19:14:50 +00004115 // We may already have a template of the same name; try to find and match it.
4116 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004117 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004118 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004119 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004120 for (auto *FoundDecl : FoundDecls) {
4121 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004122 continue;
Gabor Marton9581c332018-05-23 13:53:36 +00004123
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004124 Decl *Found = FoundDecl;
4125 if (auto *FoundTemplate = dyn_cast<ClassTemplateDecl>(Found)) {
Gabor Marton9581c332018-05-23 13:53:36 +00004126
4127 // The class to be imported is a definition.
4128 if (D->isThisDeclarationADefinition()) {
4129 // Lookup will find the fwd decl only if that is more recent than the
4130 // definition. So, try to get the definition if that is available in
4131 // the redecl chain.
4132 ClassTemplateDecl *TemplateWithDef = getDefinition(FoundTemplate);
4133 if (!TemplateWithDef)
4134 continue;
4135 FoundTemplate = TemplateWithDef; // Continue with the definition.
4136 }
4137
Douglas Gregora082a492010-11-30 19:14:50 +00004138 if (IsStructuralMatch(D, FoundTemplate)) {
4139 // The class templates structurally match; call it the same template.
Aleksei Sidorin761c2242018-05-15 11:09:07 +00004140
Gabor Marton9581c332018-05-23 13:53:36 +00004141 Importer.Imported(D->getTemplatedDecl(),
Douglas Gregora082a492010-11-30 19:14:50 +00004142 FoundTemplate->getTemplatedDecl());
4143 return Importer.Imported(D, FoundTemplate);
Gabor Marton9581c332018-05-23 13:53:36 +00004144 }
Douglas Gregora082a492010-11-30 19:14:50 +00004145 }
Gabor Marton9581c332018-05-23 13:53:36 +00004146
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004147 ConflictingDecls.push_back(FoundDecl);
Douglas Gregora082a492010-11-30 19:14:50 +00004148 }
Gabor Marton9581c332018-05-23 13:53:36 +00004149
Douglas Gregora082a492010-11-30 19:14:50 +00004150 if (!ConflictingDecls.empty()) {
4151 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
Gabor Marton9581c332018-05-23 13:53:36 +00004152 ConflictingDecls.data(),
Douglas Gregora082a492010-11-30 19:14:50 +00004153 ConflictingDecls.size());
4154 }
Gabor Marton9581c332018-05-23 13:53:36 +00004155
Douglas Gregora082a492010-11-30 19:14:50 +00004156 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004157 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004158 }
4159
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004160 CXXRecordDecl *FromTemplated = D->getTemplatedDecl();
4161
Douglas Gregora082a492010-11-30 19:14:50 +00004162 // Create the declaration that is being templated.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004163 auto *ToTemplated = cast_or_null<CXXRecordDecl>(
4164 Importer.Import(FromTemplated));
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004165 if (!ToTemplated)
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004166 return nullptr;
4167
4168 // Resolve possible cyclic import.
4169 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4170 return AlreadyImported;
4171
Douglas Gregora082a492010-11-30 19:14:50 +00004172 // Create the class template declaration itself.
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004173 TemplateParameterList *TemplateParams =
4174 ImportTemplateParameterList(D->getTemplateParameters());
Douglas Gregora082a492010-11-30 19:14:50 +00004175 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004176 return nullptr;
4177
Douglas Gregora082a492010-11-30 19:14:50 +00004178 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4179 Loc, Name, TemplateParams,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004180 ToTemplated);
4181 ToTemplated->setDescribedClassTemplate(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004182
4183 D2->setAccess(D->getAccess());
4184 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004185 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004186
4187 // Note the relationship between the class templates.
4188 Importer.Imported(D, D2);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004189 Importer.Imported(FromTemplated, ToTemplated);
Douglas Gregora082a492010-11-30 19:14:50 +00004190
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004191 if (FromTemplated->isCompleteDefinition() &&
4192 !ToTemplated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004193 // FIXME: Import definition!
4194 }
4195
4196 return D2;
4197}
4198
Douglas Gregore2e50d332010-12-01 01:36:18 +00004199Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4200 ClassTemplateSpecializationDecl *D) {
4201 // If this record has a definition in the translation unit we're coming from,
4202 // but this particular declaration is not that definition, import the
4203 // definition and map to that.
4204 TagDecl *Definition = D->getDefinition();
4205 if (Definition && Definition != D) {
4206 Decl *ImportedDef = Importer.Import(Definition);
4207 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004208 return nullptr;
4209
Douglas Gregore2e50d332010-12-01 01:36:18 +00004210 return Importer.Imported(D, ImportedDef);
4211 }
4212
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004213 auto *ClassTemplate =
4214 cast_or_null<ClassTemplateDecl>(Importer.Import(
Douglas Gregore2e50d332010-12-01 01:36:18 +00004215 D->getSpecializedTemplate()));
4216 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004217 return nullptr;
4218
Douglas Gregore2e50d332010-12-01 01:36:18 +00004219 // Import the context of this declaration.
4220 DeclContext *DC = ClassTemplate->getDeclContext();
4221 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004222 return nullptr;
4223
Douglas Gregore2e50d332010-12-01 01:36:18 +00004224 DeclContext *LexicalDC = DC;
4225 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4226 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4227 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004228 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004229 }
4230
4231 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004232 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4233 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004234
4235 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004236 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004237 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4238 D->getTemplateArgs().size(),
4239 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004240 return nullptr;
4241
Douglas Gregore2e50d332010-12-01 01:36:18 +00004242 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004243 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004244 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004245 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004246 if (D2) {
4247 // We already have a class template specialization with these template
4248 // arguments.
4249
4250 // FIXME: Check for specialization vs. instantiation errors.
4251
4252 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004253 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004254 // The record types structurally match, or the "from" translation
4255 // unit only had a forward declaration anyway; call it the same
4256 // function.
4257 return Importer.Imported(D, FoundDef);
4258 }
4259 }
4260 } else {
4261 // Create a new specialization.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004262 if (auto *PartialSpec =
4263 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004264 // Import TemplateArgumentListInfo
4265 TemplateArgumentListInfo ToTAInfo;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004266 const auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
4267 if (ImportTemplateArgumentListInfo(ASTTemplateArgs, ToTAInfo))
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00004268 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004269
4270 QualType CanonInjType = Importer.Import(
4271 PartialSpec->getInjectedSpecializationType());
4272 if (CanonInjType.isNull())
4273 return nullptr;
4274 CanonInjType = CanonInjType.getCanonicalType();
4275
4276 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4277 PartialSpec->getTemplateParameters());
4278 if (!ToTPList && PartialSpec->getTemplateParameters())
4279 return nullptr;
4280
4281 D2 = ClassTemplatePartialSpecializationDecl::Create(
4282 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
4283 ToTPList, ClassTemplate,
4284 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
4285 ToTAInfo, CanonInjType, nullptr);
4286
4287 } else {
4288 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4289 D->getTagKind(), DC,
4290 StartLoc, IdLoc,
4291 ClassTemplate,
4292 TemplateArgs,
4293 /*PrevDecl=*/nullptr);
4294 }
4295
Douglas Gregore2e50d332010-12-01 01:36:18 +00004296 D2->setSpecializationKind(D->getSpecializationKind());
4297
4298 // Add this specialization to the class template.
4299 ClassTemplate->AddSpecialization(D2, InsertPos);
4300
4301 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004302 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00004303
4304 Importer.Imported(D, D2);
4305
4306 if (auto *TSI = D->getTypeAsWritten()) {
4307 TypeSourceInfo *TInfo = Importer.Import(TSI);
4308 if (!TInfo)
4309 return nullptr;
4310 D2->setTypeAsWritten(TInfo);
4311 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
4312 D2->setExternLoc(Importer.Import(D->getExternLoc()));
4313 }
4314
4315 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
4316 if (POI.isValid())
4317 D2->setPointOfInstantiation(POI);
4318 else if (D->getPointOfInstantiation().isValid())
4319 return nullptr;
4320
4321 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
4322
Gabor Martonb14056b2018-05-25 11:21:24 +00004323 // Set the context of this specialization/instantiation.
Douglas Gregore2e50d332010-12-01 01:36:18 +00004324 D2->setLexicalDeclContext(LexicalDC);
Gabor Martonb14056b2018-05-25 11:21:24 +00004325
4326 // Add to the DC only if it was an explicit specialization/instantiation.
4327 if (D2->isExplicitInstantiationOrSpecialization()) {
4328 LexicalDC->addDeclInternal(D2);
4329 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004330 }
4331 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004332 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004333 return nullptr;
4334
Douglas Gregore2e50d332010-12-01 01:36:18 +00004335 return D2;
4336}
4337
Larisse Voufo39a1e502013-08-06 01:03:05 +00004338Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4339 // If this variable has a definition in the translation unit we're coming
4340 // from,
4341 // but this particular declaration is not that definition, import the
4342 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004343 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004344 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4345 if (Definition && Definition != D->getTemplatedDecl()) {
4346 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4347 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004348 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004349
4350 return Importer.Imported(D, ImportedDef);
4351 }
4352
4353 // Import the major distinguishing characteristics of this variable template.
4354 DeclContext *DC, *LexicalDC;
4355 DeclarationName Name;
4356 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004357 NamedDecl *ToD;
4358 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004359 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004360 if (ToD)
4361 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004362
4363 // We may already have a template of the same name; try to find and match it.
4364 assert(!DC->isFunctionOrMethod() &&
4365 "Variable templates cannot be declared at function scope");
4366 SmallVector<NamedDecl *, 4> ConflictingDecls;
4367 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004368 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004369 for (auto *FoundDecl : FoundDecls) {
4370 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004371 continue;
4372
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004373 Decl *Found = FoundDecl;
4374 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004375 if (IsStructuralMatch(D, FoundTemplate)) {
4376 // The variable templates structurally match; call it the same template.
4377 Importer.Imported(D->getTemplatedDecl(),
4378 FoundTemplate->getTemplatedDecl());
4379 return Importer.Imported(D, FoundTemplate);
4380 }
4381 }
4382
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004383 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004384 }
4385
4386 if (!ConflictingDecls.empty()) {
4387 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4388 ConflictingDecls.data(),
4389 ConflictingDecls.size());
4390 }
4391
4392 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004393 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004394
4395 VarDecl *DTemplated = D->getTemplatedDecl();
4396
4397 // Import the type.
4398 QualType T = Importer.Import(DTemplated->getType());
4399 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004400 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004401
4402 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004403 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4404 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004405 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004406
4407 // Create the variable template declaration itself.
4408 TemplateParameterList *TemplateParams =
4409 ImportTemplateParameterList(D->getTemplateParameters());
4410 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004411 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004412
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004413 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4414 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4415 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004416
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004417 ToVarTD->setAccess(D->getAccess());
4418 ToVarTD->setLexicalDeclContext(LexicalDC);
4419 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004420
4421 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004422 Importer.Imported(D, ToVarTD);
4423 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004424
4425 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004426 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004427 // FIXME: Import definition!
4428 }
4429
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004430 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004431}
4432
4433Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4434 VarTemplateSpecializationDecl *D) {
4435 // If this record has a definition in the translation unit we're coming from,
4436 // but this particular declaration is not that definition, import the
4437 // definition and map to that.
4438 VarDecl *Definition = D->getDefinition();
4439 if (Definition && Definition != D) {
4440 Decl *ImportedDef = Importer.Import(Definition);
4441 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004442 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004443
4444 return Importer.Imported(D, ImportedDef);
4445 }
4446
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004447 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004448 Importer.Import(D->getSpecializedTemplate()));
4449 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004450 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004451
4452 // Import the context of this declaration.
4453 DeclContext *DC = VarTemplate->getDeclContext();
4454 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004455 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004456
4457 DeclContext *LexicalDC = DC;
4458 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4459 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4460 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004461 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004462 }
4463
4464 // Import the location of this declaration.
4465 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4466 SourceLocation IdLoc = Importer.Import(D->getLocation());
4467
4468 // Import template arguments.
4469 SmallVector<TemplateArgument, 2> TemplateArgs;
4470 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4471 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004472 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004473
4474 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004475 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004476 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004477 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004478 if (D2) {
4479 // We already have a variable template specialization with these template
4480 // arguments.
4481
4482 // FIXME: Check for specialization vs. instantiation errors.
4483
4484 if (VarDecl *FoundDef = D2->getDefinition()) {
4485 if (!D->isThisDeclarationADefinition() ||
4486 IsStructuralMatch(D, FoundDef)) {
4487 // The record types structurally match, or the "from" translation
4488 // unit only had a forward declaration anyway; call it the same
4489 // variable.
4490 return Importer.Imported(D, FoundDef);
4491 }
4492 }
4493 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004494 // Import the type.
4495 QualType T = Importer.Import(D->getType());
4496 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004497 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004498
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004499 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4500 if (D->getTypeSourceInfo() && !TInfo)
4501 return nullptr;
4502
4503 TemplateArgumentListInfo ToTAInfo;
4504 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4505 return nullptr;
4506
4507 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004508 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004509 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4510 // Import TemplateArgumentListInfo
4511 TemplateArgumentListInfo ArgInfos;
4512 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4513 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4514 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4515 return nullptr;
4516
4517 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4518 FromPartial->getTemplateParameters());
4519 if (!ToTPList)
4520 return nullptr;
4521
4522 auto *ToPartial = PartVarSpecDecl::Create(
4523 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4524 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4525
4526 auto *FromInst = FromPartial->getInstantiatedFromMember();
4527 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4528 if (FromInst && !ToInst)
4529 return nullptr;
4530
4531 ToPartial->setInstantiatedFromMember(ToInst);
4532 if (FromPartial->isMemberSpecialization())
4533 ToPartial->setMemberSpecialization();
4534
4535 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004536 } else { // Full specialization
4537 D2 = VarTemplateSpecializationDecl::Create(
4538 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4539 D->getStorageClass(), TemplateArgs);
4540 }
4541
4542 SourceLocation POI = D->getPointOfInstantiation();
4543 if (POI.isValid())
4544 D2->setPointOfInstantiation(Importer.Import(POI));
4545
Larisse Voufo39a1e502013-08-06 01:03:05 +00004546 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004547 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004548
4549 // Add this specialization to the class template.
4550 VarTemplate->AddSpecialization(D2, InsertPos);
4551
4552 // Import the qualifier, if any.
4553 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4554
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004555 if (D->isConstexpr())
4556 D2->setConstexpr(true);
4557
Larisse Voufo39a1e502013-08-06 01:03:05 +00004558 // Add the specialization to this context.
4559 D2->setLexicalDeclContext(LexicalDC);
4560 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004561
4562 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004563 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004564
Larisse Voufo39a1e502013-08-06 01:03:05 +00004565 Importer.Imported(D, D2);
4566
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004567 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4568 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4569 // we have to check the declaration for initializer - otherwise, it won't be
4570 // imported.
4571 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4572 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004573 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004574
4575 return D2;
4576}
4577
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004578Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4579 DeclContext *DC, *LexicalDC;
4580 DeclarationName Name;
4581 SourceLocation Loc;
4582 NamedDecl *ToD;
4583
4584 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4585 return nullptr;
4586
4587 if (ToD)
4588 return ToD;
4589
4590 // Try to find a function in our own ("to") context with the same name, same
4591 // type, and in the same context as the function we're importing.
4592 if (!LexicalDC->isFunctionOrMethod()) {
4593 unsigned IDNS = Decl::IDNS_Ordinary;
4594 SmallVector<NamedDecl *, 2> FoundDecls;
4595 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004596 for (auto *FoundDecl : FoundDecls) {
4597 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004598 continue;
4599
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004600 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004601 if (FoundFunction->hasExternalFormalLinkage() &&
4602 D->hasExternalFormalLinkage()) {
4603 if (IsStructuralMatch(D, FoundFunction)) {
4604 Importer.Imported(D, FoundFunction);
4605 // FIXME: Actually try to merge the body and other attributes.
4606 return FoundFunction;
4607 }
4608 }
4609 }
4610 }
4611 }
4612
4613 TemplateParameterList *Params =
4614 ImportTemplateParameterList(D->getTemplateParameters());
4615 if (!Params)
4616 return nullptr;
4617
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004618 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004619 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4620 if (!TemplatedFD)
4621 return nullptr;
4622
4623 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4624 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4625
4626 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4627 ToFunc->setAccess(D->getAccess());
4628 ToFunc->setLexicalDeclContext(LexicalDC);
4629 Importer.Imported(D, ToFunc);
4630
4631 LexicalDC->addDeclInternal(ToFunc);
4632 return ToFunc;
4633}
4634
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004635//----------------------------------------------------------------------------
4636// Import Statements
4637//----------------------------------------------------------------------------
4638
Sean Callanan59721b32015-04-28 18:41:46 +00004639DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4640 if (DG.isNull())
4641 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4642 size_t NumDecls = DG.end() - DG.begin();
4643 SmallVector<Decl *, 1> ToDecls(NumDecls);
4644 auto &_Importer = this->Importer;
4645 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4646 [&_Importer](Decl *D) -> Decl * {
4647 return _Importer.Import(D);
4648 });
4649 return DeclGroupRef::Create(Importer.getToContext(),
4650 ToDecls.begin(),
4651 NumDecls);
4652}
4653
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004654Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4655 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4656 << S->getStmtClassName();
4657 return nullptr;
4658}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004659
4660Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4661 SmallVector<IdentifierInfo *, 4> Names;
4662 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4663 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004664 // ToII is nullptr when no symbolic name is given for output operand
4665 // see ParseStmtAsm::ParseAsmOperandsOpt
4666 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004667 return nullptr;
4668 Names.push_back(ToII);
4669 }
4670 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4671 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004672 // ToII is nullptr when no symbolic name is given for input operand
4673 // see ParseStmtAsm::ParseAsmOperandsOpt
4674 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004675 return nullptr;
4676 Names.push_back(ToII);
4677 }
4678
4679 SmallVector<StringLiteral *, 4> Clobbers;
4680 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004681 auto *Clobber = cast_or_null<StringLiteral>(
4682 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004683 if (!Clobber)
4684 return nullptr;
4685 Clobbers.push_back(Clobber);
4686 }
4687
4688 SmallVector<StringLiteral *, 4> Constraints;
4689 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004690 auto *Output = cast_or_null<StringLiteral>(
4691 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004692 if (!Output)
4693 return nullptr;
4694 Constraints.push_back(Output);
4695 }
4696
4697 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004698 auto *Input = cast_or_null<StringLiteral>(
4699 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004700 if (!Input)
4701 return nullptr;
4702 Constraints.push_back(Input);
4703 }
4704
4705 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004706 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004707 return nullptr;
4708
Aleksei Sidorina693b372016-09-28 10:16:56 +00004709 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004710 return nullptr;
4711
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004712 auto *AsmStr = cast_or_null<StringLiteral>(
4713 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004714 if (!AsmStr)
4715 return nullptr;
4716
4717 return new (Importer.getToContext()) GCCAsmStmt(
4718 Importer.getToContext(),
4719 Importer.Import(S->getAsmLoc()),
4720 S->isSimple(),
4721 S->isVolatile(),
4722 S->getNumOutputs(),
4723 S->getNumInputs(),
4724 Names.data(),
4725 Constraints.data(),
4726 Exprs.data(),
4727 AsmStr,
4728 S->getNumClobbers(),
4729 Clobbers.data(),
4730 Importer.Import(S->getRParenLoc()));
4731}
4732
Sean Callanan59721b32015-04-28 18:41:46 +00004733Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4734 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004735 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004736 if (!ToD)
4737 return nullptr;
4738 }
4739 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4740 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4741 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4742}
4743
4744Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4745 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4746 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4747 S->hasLeadingEmptyMacro());
4748}
4749
4750Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004751 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004752
4753 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004754 return nullptr;
4755
Sean Callanan59721b32015-04-28 18:41:46 +00004756 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4757 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004758 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4759 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004760}
4761
4762Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4763 Expr *ToLHS = Importer.Import(S->getLHS());
4764 if (!ToLHS)
4765 return nullptr;
4766 Expr *ToRHS = Importer.Import(S->getRHS());
4767 if (!ToRHS && S->getRHS())
4768 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004769 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4770 if (!ToSubStmt && S->getSubStmt())
4771 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004772 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4773 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4774 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004775 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004776 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4777 ToStmt->setSubStmt(ToSubStmt);
4778 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004779}
4780
4781Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4782 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4783 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4784 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4785 if (!ToSubStmt && S->getSubStmt())
4786 return nullptr;
4787 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4788 ToSubStmt);
4789}
4790
4791Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4792 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004793 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004794 if (!ToLabelDecl && S->getDecl())
4795 return nullptr;
4796 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4797 if (!ToSubStmt && S->getSubStmt())
4798 return nullptr;
4799 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4800 ToSubStmt);
4801}
4802
4803Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4804 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4805 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4806 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00004807 if (ImportContainerChecked(FromAttrs, ToAttrs))
4808 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004809 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4810 if (!ToSubStmt && S->getSubStmt())
4811 return nullptr;
4812 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4813 ToAttrs, ToSubStmt);
4814}
4815
4816Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4817 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004818 Stmt *ToInit = Importer.Import(S->getInit());
4819 if (!ToInit && S->getInit())
4820 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004821 VarDecl *ToConditionVariable = nullptr;
4822 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4823 ToConditionVariable =
4824 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4825 if (!ToConditionVariable)
4826 return nullptr;
4827 }
4828 Expr *ToCondition = Importer.Import(S->getCond());
4829 if (!ToCondition && S->getCond())
4830 return nullptr;
4831 Stmt *ToThenStmt = Importer.Import(S->getThen());
4832 if (!ToThenStmt && S->getThen())
4833 return nullptr;
4834 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4835 Stmt *ToElseStmt = Importer.Import(S->getElse());
4836 if (!ToElseStmt && S->getElse())
4837 return nullptr;
4838 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004839 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004840 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004841 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004842 ToCondition, ToThenStmt,
4843 ToElseLoc, ToElseStmt);
4844}
4845
4846Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004847 Stmt *ToInit = Importer.Import(S->getInit());
4848 if (!ToInit && S->getInit())
4849 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004850 VarDecl *ToConditionVariable = nullptr;
4851 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4852 ToConditionVariable =
4853 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4854 if (!ToConditionVariable)
4855 return nullptr;
4856 }
4857 Expr *ToCondition = Importer.Import(S->getCond());
4858 if (!ToCondition && S->getCond())
4859 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004860 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004861 Importer.getToContext(), ToInit,
4862 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004863 Stmt *ToBody = Importer.Import(S->getBody());
4864 if (!ToBody && S->getBody())
4865 return nullptr;
4866 ToStmt->setBody(ToBody);
4867 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4868 // Now we have to re-chain the cases.
4869 SwitchCase *LastChainedSwitchCase = nullptr;
4870 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4871 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004872 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004873 if (!ToSC)
4874 return nullptr;
4875 if (LastChainedSwitchCase)
4876 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4877 else
4878 ToStmt->setSwitchCaseList(ToSC);
4879 LastChainedSwitchCase = ToSC;
4880 }
4881 return ToStmt;
4882}
4883
4884Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4885 VarDecl *ToConditionVariable = nullptr;
4886 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4887 ToConditionVariable =
4888 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4889 if (!ToConditionVariable)
4890 return nullptr;
4891 }
4892 Expr *ToCondition = Importer.Import(S->getCond());
4893 if (!ToCondition && S->getCond())
4894 return nullptr;
4895 Stmt *ToBody = Importer.Import(S->getBody());
4896 if (!ToBody && S->getBody())
4897 return nullptr;
4898 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4899 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4900 ToConditionVariable,
4901 ToCondition, ToBody,
4902 ToWhileLoc);
4903}
4904
4905Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4906 Stmt *ToBody = Importer.Import(S->getBody());
4907 if (!ToBody && S->getBody())
4908 return nullptr;
4909 Expr *ToCondition = Importer.Import(S->getCond());
4910 if (!ToCondition && S->getCond())
4911 return nullptr;
4912 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4913 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4914 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4915 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4916 ToDoLoc, ToWhileLoc,
4917 ToRParenLoc);
4918}
4919
4920Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4921 Stmt *ToInit = Importer.Import(S->getInit());
4922 if (!ToInit && S->getInit())
4923 return nullptr;
4924 Expr *ToCondition = Importer.Import(S->getCond());
4925 if (!ToCondition && S->getCond())
4926 return nullptr;
4927 VarDecl *ToConditionVariable = nullptr;
4928 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4929 ToConditionVariable =
4930 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4931 if (!ToConditionVariable)
4932 return nullptr;
4933 }
4934 Expr *ToInc = Importer.Import(S->getInc());
4935 if (!ToInc && S->getInc())
4936 return nullptr;
4937 Stmt *ToBody = Importer.Import(S->getBody());
4938 if (!ToBody && S->getBody())
4939 return nullptr;
4940 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4941 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4942 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4943 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4944 ToInit, ToCondition,
4945 ToConditionVariable,
4946 ToInc, ToBody,
4947 ToForLoc, ToLParenLoc,
4948 ToRParenLoc);
4949}
4950
4951Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4952 LabelDecl *ToLabel = nullptr;
4953 if (LabelDecl *FromLabel = S->getLabel()) {
4954 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4955 if (!ToLabel)
4956 return nullptr;
4957 }
4958 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4959 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4960 return new (Importer.getToContext()) GotoStmt(ToLabel,
4961 ToGotoLoc, ToLabelLoc);
4962}
4963
4964Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4965 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4966 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4967 Expr *ToTarget = Importer.Import(S->getTarget());
4968 if (!ToTarget && S->getTarget())
4969 return nullptr;
4970 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4971 ToTarget);
4972}
4973
4974Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4975 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4976 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4977}
4978
4979Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4980 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4981 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4982}
4983
4984Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4985 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4986 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4987 if (!ToRetExpr && S->getRetValue())
4988 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004989 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4990 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004991 if (!ToNRVOCandidate && NRVOCandidate)
4992 return nullptr;
4993 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4994 ToNRVOCandidate);
4995}
4996
4997Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4998 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4999 VarDecl *ToExceptionDecl = nullptr;
5000 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5001 ToExceptionDecl =
5002 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5003 if (!ToExceptionDecl)
5004 return nullptr;
5005 }
5006 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5007 if (!ToHandlerBlock && S->getHandlerBlock())
5008 return nullptr;
5009 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5010 ToExceptionDecl,
5011 ToHandlerBlock);
5012}
5013
5014Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5015 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5016 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5017 if (!ToTryBlock && S->getTryBlock())
5018 return nullptr;
5019 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5020 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5021 CXXCatchStmt *FromHandler = S->getHandler(HI);
5022 if (Stmt *ToHandler = Importer.Import(FromHandler))
5023 ToHandlers[HI] = ToHandler;
5024 else
5025 return nullptr;
5026 }
5027 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5028 ToHandlers);
5029}
5030
5031Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005032 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005033 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5034 if (!ToRange && S->getRangeStmt())
5035 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005036 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005037 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5038 if (!ToBegin && S->getBeginStmt())
5039 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005040 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005041 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5042 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005043 return nullptr;
5044 Expr *ToCond = Importer.Import(S->getCond());
5045 if (!ToCond && S->getCond())
5046 return nullptr;
5047 Expr *ToInc = Importer.Import(S->getInc());
5048 if (!ToInc && S->getInc())
5049 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005050 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005051 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5052 if (!ToLoopVar && S->getLoopVarStmt())
5053 return nullptr;
5054 Stmt *ToBody = Importer.Import(S->getBody());
5055 if (!ToBody && S->getBody())
5056 return nullptr;
5057 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005058 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005059 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5060 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005061 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005062 ToCond, ToInc,
5063 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005064 ToForLoc, ToCoawaitLoc,
5065 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005066}
5067
5068Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5069 Stmt *ToElem = Importer.Import(S->getElement());
5070 if (!ToElem && S->getElement())
5071 return nullptr;
5072 Expr *ToCollect = Importer.Import(S->getCollection());
5073 if (!ToCollect && S->getCollection())
5074 return nullptr;
5075 Stmt *ToBody = Importer.Import(S->getBody());
5076 if (!ToBody && S->getBody())
5077 return nullptr;
5078 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5079 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5080 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5081 ToCollect,
5082 ToBody, ToForLoc,
5083 ToRParenLoc);
5084}
5085
5086Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5087 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5088 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5089 VarDecl *ToExceptionDecl = nullptr;
5090 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5091 ToExceptionDecl =
5092 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5093 if (!ToExceptionDecl)
5094 return nullptr;
5095 }
5096 Stmt *ToBody = Importer.Import(S->getCatchBody());
5097 if (!ToBody && S->getCatchBody())
5098 return nullptr;
5099 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5100 ToRParenLoc,
5101 ToExceptionDecl,
5102 ToBody);
5103}
5104
5105Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5106 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5107 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5108 if (!ToAtFinallyStmt && S->getFinallyBody())
5109 return nullptr;
5110 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5111 ToAtFinallyStmt);
5112}
5113
5114Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5115 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5116 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5117 if (!ToAtTryStmt && S->getTryBody())
5118 return nullptr;
5119 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5120 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5121 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5122 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5123 ToCatchStmts[CI] = ToCatchStmt;
5124 else
5125 return nullptr;
5126 }
5127 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5128 if (!ToAtFinallyStmt && S->getFinallyStmt())
5129 return nullptr;
5130 return ObjCAtTryStmt::Create(Importer.getToContext(),
5131 ToAtTryLoc, ToAtTryStmt,
5132 ToCatchStmts.begin(), ToCatchStmts.size(),
5133 ToAtFinallyStmt);
5134}
5135
5136Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5137 (ObjCAtSynchronizedStmt *S) {
5138 SourceLocation ToAtSynchronizedLoc =
5139 Importer.Import(S->getAtSynchronizedLoc());
5140 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5141 if (!ToSynchExpr && S->getSynchExpr())
5142 return nullptr;
5143 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5144 if (!ToSynchBody && S->getSynchBody())
5145 return nullptr;
5146 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5147 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5148}
5149
5150Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5151 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5152 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5153 if (!ToThrow && S->getThrowExpr())
5154 return nullptr;
5155 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5156}
5157
5158Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5159 (ObjCAutoreleasePoolStmt *S) {
5160 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5161 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5162 if (!ToSubStmt && S->getSubStmt())
5163 return nullptr;
5164 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5165 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005166}
5167
5168//----------------------------------------------------------------------------
5169// Import Expressions
5170//----------------------------------------------------------------------------
5171Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5172 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5173 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005174 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005175}
5176
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005177Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5178 QualType T = Importer.Import(E->getType());
5179 if (T.isNull())
5180 return nullptr;
5181
5182 Expr *SubExpr = Importer.Import(E->getSubExpr());
5183 if (!SubExpr && E->getSubExpr())
5184 return nullptr;
5185
5186 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5187 if (!TInfo)
5188 return nullptr;
5189
5190 return new (Importer.getToContext()) VAArgExpr(
5191 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5192 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5193}
5194
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005195Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5196 QualType T = Importer.Import(E->getType());
5197 if (T.isNull())
5198 return nullptr;
5199
5200 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005201 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005202}
5203
5204Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5205 QualType T = Importer.Import(E->getType());
5206 if (T.isNull())
5207 return nullptr;
5208
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005209 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005210 if (!SL && E->getFunctionName())
5211 return nullptr;
5212
5213 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005214 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005215}
5216
Douglas Gregor52f820e2010-02-19 01:17:02 +00005217Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005218 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005219 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005220 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005221
Craig Topper36250ad2014-05-12 05:36:57 +00005222 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005223 if (E->getDecl() != E->getFoundDecl()) {
5224 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5225 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005226 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005227 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005228
5229 QualType T = Importer.Import(E->getType());
5230 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005231 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005232
Aleksei Sidorina693b372016-09-28 10:16:56 +00005233 TemplateArgumentListInfo ToTAInfo;
5234 TemplateArgumentListInfo *ResInfo = nullptr;
5235 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005236 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5237 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005238 ResInfo = &ToTAInfo;
5239 }
5240
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005241 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5242 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005243 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005244 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005245 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005246 Importer.Import(E->getLocation()),
5247 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005248 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005249 if (E->hadMultipleCandidates())
5250 DRE->setHadMultipleCandidates(true);
5251 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005252}
5253
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005254Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5255 QualType T = Importer.Import(E->getType());
5256 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005257 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005258
5259 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5260}
5261
5262ASTNodeImporter::Designator
5263ASTNodeImporter::ImportDesignator(const Designator &D) {
5264 if (D.isFieldDesignator()) {
5265 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5266 // Caller checks for import error
5267 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5268 Importer.Import(D.getFieldLoc()));
5269 }
5270 if (D.isArrayDesignator())
5271 return Designator(D.getFirstExprIndex(),
5272 Importer.Import(D.getLBracketLoc()),
5273 Importer.Import(D.getRBracketLoc()));
5274
5275 assert(D.isArrayRangeDesignator());
5276 return Designator(D.getFirstExprIndex(),
5277 Importer.Import(D.getLBracketLoc()),
5278 Importer.Import(D.getEllipsisLoc()),
5279 Importer.Import(D.getRBracketLoc()));
5280}
5281
5282
5283Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005284 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005285 if (!Init)
5286 return nullptr;
5287
5288 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5289 // List elements from the second, the first is Init itself
5290 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005291 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005292 IndexExprs[I - 1] = Arg;
5293 else
5294 return nullptr;
5295 }
5296
5297 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005298 llvm::transform(DIE->designators(), Designators.begin(),
5299 [this](const Designator &D) -> Designator {
5300 return ImportDesignator(D);
5301 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005302
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005303 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005304 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005305 return nullptr;
5306
5307 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005308 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005309 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5310 DIE->usesGNUSyntax(), Init);
5311}
5312
5313Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5314 QualType T = Importer.Import(E->getType());
5315 if (T.isNull())
5316 return nullptr;
5317
5318 return new (Importer.getToContext())
5319 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5320}
5321
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005322Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5323 QualType T = Importer.Import(E->getType());
5324 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005325 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005326
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005327 return IntegerLiteral::Create(Importer.getToContext(),
5328 E->getValue(), T,
5329 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005330}
5331
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005332Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5333 QualType T = Importer.Import(E->getType());
5334 if (T.isNull())
5335 return nullptr;
5336
5337 return FloatingLiteral::Create(Importer.getToContext(),
5338 E->getValue(), E->isExact(), T,
5339 Importer.Import(E->getLocation()));
5340}
5341
Douglas Gregor623421d2010-02-18 02:21:22 +00005342Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5343 QualType T = Importer.Import(E->getType());
5344 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005345 return nullptr;
5346
Douglas Gregorfb65e592011-07-27 05:40:30 +00005347 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5348 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005349 Importer.Import(E->getLocation()));
5350}
5351
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005352Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5353 QualType T = Importer.Import(E->getType());
5354 if (T.isNull())
5355 return nullptr;
5356
5357 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5358 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5359
5360 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5361 E->getKind(), E->isPascal(), T,
5362 Locations.data(), Locations.size());
5363}
5364
5365Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5366 QualType T = Importer.Import(E->getType());
5367 if (T.isNull())
5368 return nullptr;
5369
5370 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5371 if (!TInfo)
5372 return nullptr;
5373
5374 Expr *Init = Importer.Import(E->getInitializer());
5375 if (!Init)
5376 return nullptr;
5377
5378 return new (Importer.getToContext()) CompoundLiteralExpr(
5379 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5380 Init, E->isFileScope());
5381}
5382
5383Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5384 QualType T = Importer.Import(E->getType());
5385 if (T.isNull())
5386 return nullptr;
5387
5388 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5389 if (ImportArrayChecked(
5390 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5391 Exprs.begin()))
5392 return nullptr;
5393
5394 return new (Importer.getToContext()) AtomicExpr(
5395 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5396 Importer.Import(E->getRParenLoc()));
5397}
5398
5399Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5400 QualType T = Importer.Import(E->getType());
5401 if (T.isNull())
5402 return nullptr;
5403
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005404 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005405 if (!ToLabel)
5406 return nullptr;
5407
5408 return new (Importer.getToContext()) AddrLabelExpr(
5409 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5410 ToLabel, T);
5411}
5412
Douglas Gregorc74247e2010-02-19 01:07:06 +00005413Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5414 Expr *SubExpr = Importer.Import(E->getSubExpr());
5415 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005416 return nullptr;
5417
Douglas Gregorc74247e2010-02-19 01:07:06 +00005418 return new (Importer.getToContext())
5419 ParenExpr(Importer.Import(E->getLParen()),
5420 Importer.Import(E->getRParen()),
5421 SubExpr);
5422}
5423
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005424Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5425 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005426 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005427 return nullptr;
5428
5429 return new (Importer.getToContext()) ParenListExpr(
5430 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5431 Exprs, Importer.Import(E->getLParenLoc()));
5432}
5433
5434Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5435 QualType T = Importer.Import(E->getType());
5436 if (T.isNull())
5437 return nullptr;
5438
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005439 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5440 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005441 if (!ToSubStmt && E->getSubStmt())
5442 return nullptr;
5443
5444 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5445 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5446}
5447
Douglas Gregorc74247e2010-02-19 01:07:06 +00005448Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5449 QualType T = Importer.Import(E->getType());
5450 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005452
5453 Expr *SubExpr = Importer.Import(E->getSubExpr());
5454 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005455 return nullptr;
5456
Aaron Ballmana5038552018-01-09 13:07:03 +00005457 return new (Importer.getToContext()) UnaryOperator(
5458 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5459 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005460}
5461
Aaron Ballmana5038552018-01-09 13:07:03 +00005462Expr *
5463ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005464 QualType ResultType = Importer.Import(E->getType());
5465
5466 if (E->isArgumentType()) {
5467 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5468 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005469 return nullptr;
5470
Peter Collingbournee190dee2011-03-11 19:24:49 +00005471 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5472 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005473 Importer.Import(E->getOperatorLoc()),
5474 Importer.Import(E->getRParenLoc()));
5475 }
5476
5477 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5478 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005479 return nullptr;
5480
Peter Collingbournee190dee2011-03-11 19:24:49 +00005481 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5482 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005483 Importer.Import(E->getOperatorLoc()),
5484 Importer.Import(E->getRParenLoc()));
5485}
5486
Douglas Gregorc74247e2010-02-19 01:07:06 +00005487Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5488 QualType T = Importer.Import(E->getType());
5489 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005490 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005491
5492 Expr *LHS = Importer.Import(E->getLHS());
5493 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005494 return nullptr;
5495
Douglas Gregorc74247e2010-02-19 01:07:06 +00005496 Expr *RHS = Importer.Import(E->getRHS());
5497 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005498 return nullptr;
5499
Douglas Gregorc74247e2010-02-19 01:07:06 +00005500 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005501 T, E->getValueKind(),
5502 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005503 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005504 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005505}
5506
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005507Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5508 QualType T = Importer.Import(E->getType());
5509 if (T.isNull())
5510 return nullptr;
5511
5512 Expr *ToLHS = Importer.Import(E->getLHS());
5513 if (!ToLHS)
5514 return nullptr;
5515
5516 Expr *ToRHS = Importer.Import(E->getRHS());
5517 if (!ToRHS)
5518 return nullptr;
5519
5520 Expr *ToCond = Importer.Import(E->getCond());
5521 if (!ToCond)
5522 return nullptr;
5523
5524 return new (Importer.getToContext()) ConditionalOperator(
5525 ToCond, Importer.Import(E->getQuestionLoc()),
5526 ToLHS, Importer.Import(E->getColonLoc()),
5527 ToRHS, T, E->getValueKind(), E->getObjectKind());
5528}
5529
5530Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5531 BinaryConditionalOperator *E) {
5532 QualType T = Importer.Import(E->getType());
5533 if (T.isNull())
5534 return nullptr;
5535
5536 Expr *Common = Importer.Import(E->getCommon());
5537 if (!Common)
5538 return nullptr;
5539
5540 Expr *Cond = Importer.Import(E->getCond());
5541 if (!Cond)
5542 return nullptr;
5543
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005544 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5545 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005546 if (!OpaqueValue)
5547 return nullptr;
5548
5549 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5550 if (!TrueExpr)
5551 return nullptr;
5552
5553 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5554 if (!FalseExpr)
5555 return nullptr;
5556
5557 return new (Importer.getToContext()) BinaryConditionalOperator(
5558 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5559 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5560 T, E->getValueKind(), E->getObjectKind());
5561}
5562
Aleksei Sidorina693b372016-09-28 10:16:56 +00005563Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5564 QualType T = Importer.Import(E->getType());
5565 if (T.isNull())
5566 return nullptr;
5567
5568 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5569 if (!ToQueried)
5570 return nullptr;
5571
5572 Expr *Dim = Importer.Import(E->getDimensionExpression());
5573 if (!Dim && E->getDimensionExpression())
5574 return nullptr;
5575
5576 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5577 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5578 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5579}
5580
5581Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5582 QualType T = Importer.Import(E->getType());
5583 if (T.isNull())
5584 return nullptr;
5585
5586 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5587 if (!ToQueried)
5588 return nullptr;
5589
5590 return new (Importer.getToContext()) ExpressionTraitExpr(
5591 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5592 E->getValue(), Importer.Import(E->getLocEnd()), T);
5593}
5594
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005595Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5596 QualType T = Importer.Import(E->getType());
5597 if (T.isNull())
5598 return nullptr;
5599
5600 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5601 if (!SourceExpr && E->getSourceExpr())
5602 return nullptr;
5603
5604 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005605 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005606 E->getObjectKind(), SourceExpr);
5607}
5608
Aleksei Sidorina693b372016-09-28 10:16:56 +00005609Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5610 QualType T = Importer.Import(E->getType());
5611 if (T.isNull())
5612 return nullptr;
5613
5614 Expr *ToLHS = Importer.Import(E->getLHS());
5615 if (!ToLHS)
5616 return nullptr;
5617
5618 Expr *ToRHS = Importer.Import(E->getRHS());
5619 if (!ToRHS)
5620 return nullptr;
5621
5622 return new (Importer.getToContext()) ArraySubscriptExpr(
5623 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5624 Importer.Import(E->getRBracketLoc()));
5625}
5626
Douglas Gregorc74247e2010-02-19 01:07:06 +00005627Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5628 QualType T = Importer.Import(E->getType());
5629 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005630 return nullptr;
5631
Douglas Gregorc74247e2010-02-19 01:07:06 +00005632 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5633 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005634 return nullptr;
5635
Douglas Gregorc74247e2010-02-19 01:07:06 +00005636 QualType CompResultType = Importer.Import(E->getComputationResultType());
5637 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005638 return nullptr;
5639
Douglas Gregorc74247e2010-02-19 01:07:06 +00005640 Expr *LHS = Importer.Import(E->getLHS());
5641 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005642 return nullptr;
5643
Douglas Gregorc74247e2010-02-19 01:07:06 +00005644 Expr *RHS = Importer.Import(E->getRHS());
5645 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005646 return nullptr;
5647
Douglas Gregorc74247e2010-02-19 01:07:06 +00005648 return new (Importer.getToContext())
5649 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005650 T, E->getValueKind(),
5651 E->getObjectKind(),
5652 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005653 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005654 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005655}
5656
Aleksei Sidorina693b372016-09-28 10:16:56 +00005657bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5658 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5659 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5660 Path.push_back(Spec);
5661 else
5662 return true;
5663 }
5664 return false;
John McCallcf142162010-08-07 06:22:56 +00005665}
5666
Douglas Gregor98c10182010-02-12 22:17:39 +00005667Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5668 QualType T = Importer.Import(E->getType());
5669 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005670 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005671
5672 Expr *SubExpr = Importer.Import(E->getSubExpr());
5673 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005674 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005675
5676 CXXCastPath BasePath;
5677 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005678 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005679
5680 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005681 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005682}
5683
Aleksei Sidorina693b372016-09-28 10:16:56 +00005684Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005685 QualType T = Importer.Import(E->getType());
5686 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005687 return nullptr;
5688
Douglas Gregor5481d322010-02-19 01:32:14 +00005689 Expr *SubExpr = Importer.Import(E->getSubExpr());
5690 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005691 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005692
5693 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5694 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005695 return nullptr;
5696
John McCallcf142162010-08-07 06:22:56 +00005697 CXXCastPath BasePath;
5698 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005699 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005700
Aleksei Sidorina693b372016-09-28 10:16:56 +00005701 switch (E->getStmtClass()) {
5702 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005703 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005704 return CStyleCastExpr::Create(Importer.getToContext(), T,
5705 E->getValueKind(), E->getCastKind(),
5706 SubExpr, &BasePath, TInfo,
5707 Importer.Import(CCE->getLParenLoc()),
5708 Importer.Import(CCE->getRParenLoc()));
5709 }
5710
5711 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005712 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005713 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5714 E->getValueKind(), TInfo,
5715 E->getCastKind(), SubExpr, &BasePath,
5716 Importer.Import(FCE->getLParenLoc()),
5717 Importer.Import(FCE->getRParenLoc()));
5718 }
5719
5720 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005721 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005722 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5723 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5724 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5725 TInfo, SubExpr);
5726 }
5727 default:
5728 break; // just fall through
5729 }
5730
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005731 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005732 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5733 RParenLoc = Importer.Import(Named->getRParenLoc());
5734 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5735
5736 switch (E->getStmtClass()) {
5737 case Stmt::CXXStaticCastExprClass:
5738 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5739 E->getValueKind(), E->getCastKind(),
5740 SubExpr, &BasePath, TInfo,
5741 ExprLoc, RParenLoc, Brackets);
5742
5743 case Stmt::CXXDynamicCastExprClass:
5744 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5745 E->getValueKind(), E->getCastKind(),
5746 SubExpr, &BasePath, TInfo,
5747 ExprLoc, RParenLoc, Brackets);
5748
5749 case Stmt::CXXReinterpretCastExprClass:
5750 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5751 E->getValueKind(), E->getCastKind(),
5752 SubExpr, &BasePath, TInfo,
5753 ExprLoc, RParenLoc, Brackets);
5754
5755 case Stmt::CXXConstCastExprClass:
5756 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5757 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5758 RParenLoc, Brackets);
5759 default:
5760 llvm_unreachable("Cast expression of unsupported type!");
5761 return nullptr;
5762 }
5763}
5764
5765Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5766 QualType T = Importer.Import(OE->getType());
5767 if (T.isNull())
5768 return nullptr;
5769
5770 SmallVector<OffsetOfNode, 4> Nodes;
5771 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5772 const OffsetOfNode &Node = OE->getComponent(I);
5773
5774 switch (Node.getKind()) {
5775 case OffsetOfNode::Array:
5776 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5777 Node.getArrayExprIndex(),
5778 Importer.Import(Node.getLocEnd())));
5779 break;
5780
5781 case OffsetOfNode::Base: {
5782 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5783 if (!BS && Node.getBase())
5784 return nullptr;
5785 Nodes.push_back(OffsetOfNode(BS));
5786 break;
5787 }
5788 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005789 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005790 if (!FD)
5791 return nullptr;
5792 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5793 Importer.Import(Node.getLocEnd())));
5794 break;
5795 }
5796 case OffsetOfNode::Identifier: {
5797 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5798 if (!ToII)
5799 return nullptr;
5800 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5801 Importer.Import(Node.getLocEnd())));
5802 break;
5803 }
5804 }
5805 }
5806
5807 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5808 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5809 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5810 if (!ToIndexExpr)
5811 return nullptr;
5812 Exprs[I] = ToIndexExpr;
5813 }
5814
5815 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5816 if (!TInfo && OE->getTypeSourceInfo())
5817 return nullptr;
5818
5819 return OffsetOfExpr::Create(Importer.getToContext(), T,
5820 Importer.Import(OE->getOperatorLoc()),
5821 TInfo, Nodes, Exprs,
5822 Importer.Import(OE->getRParenLoc()));
5823}
5824
5825Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5826 QualType T = Importer.Import(E->getType());
5827 if (T.isNull())
5828 return nullptr;
5829
5830 Expr *Operand = Importer.Import(E->getOperand());
5831 if (!Operand)
5832 return nullptr;
5833
5834 CanThrowResult CanThrow;
5835 if (E->isValueDependent())
5836 CanThrow = CT_Dependent;
5837 else
5838 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5839
5840 return new (Importer.getToContext()) CXXNoexceptExpr(
5841 T, Operand, CanThrow,
5842 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5843}
5844
5845Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5846 QualType T = Importer.Import(E->getType());
5847 if (T.isNull())
5848 return nullptr;
5849
5850 Expr *SubExpr = Importer.Import(E->getSubExpr());
5851 if (!SubExpr && E->getSubExpr())
5852 return nullptr;
5853
5854 return new (Importer.getToContext()) CXXThrowExpr(
5855 SubExpr, T, Importer.Import(E->getThrowLoc()),
5856 E->isThrownVariableInScope());
5857}
5858
5859Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005860 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005861 if (!Param)
5862 return nullptr;
5863
5864 return CXXDefaultArgExpr::Create(
5865 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5866}
5867
5868Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5869 QualType T = Importer.Import(E->getType());
5870 if (T.isNull())
5871 return nullptr;
5872
5873 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5874 if (!TypeInfo)
5875 return nullptr;
5876
5877 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5878 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5879}
5880
5881Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5882 Expr *SubExpr = Importer.Import(E->getSubExpr());
5883 if (!SubExpr)
5884 return nullptr;
5885
5886 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5887 Importer.Import(const_cast<CXXDestructorDecl *>(
5888 E->getTemporary()->getDestructor())));
5889 if (!Dtor)
5890 return nullptr;
5891
5892 ASTContext &ToCtx = Importer.getToContext();
5893 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5894 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5895}
5896
5897Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5898 QualType T = Importer.Import(CE->getType());
5899 if (T.isNull())
5900 return nullptr;
5901
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005902 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5903 if (!TInfo)
5904 return nullptr;
5905
Aleksei Sidorina693b372016-09-28 10:16:56 +00005906 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5907 if (ImportContainerChecked(CE->arguments(), Args))
5908 return nullptr;
5909
5910 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5911 Importer.Import(CE->getConstructor()));
5912 if (!Ctor)
5913 return nullptr;
5914
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005915 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5916 Importer.getToContext(), Ctor, T, TInfo, Args,
5917 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5918 CE->isListInitialization(), CE->isStdInitListInitialization(),
5919 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005920}
5921
5922Expr *
5923ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5924 QualType T = Importer.Import(E->getType());
5925 if (T.isNull())
5926 return nullptr;
5927
5928 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5929 if (!TempE)
5930 return nullptr;
5931
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005932 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005933 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5934 if (!ExtendedBy && E->getExtendingDecl())
5935 return nullptr;
5936
5937 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5938 T, TempE, E->isBoundToLvalueReference());
5939
5940 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5941 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5942 return ToMTE;
5943}
5944
Gabor Horvath7a91c082017-11-14 11:30:38 +00005945Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5946 QualType T = Importer.Import(E->getType());
5947 if (T.isNull())
5948 return nullptr;
5949
5950 Expr *Pattern = Importer.Import(E->getPattern());
5951 if (!Pattern)
5952 return nullptr;
5953
5954 return new (Importer.getToContext()) PackExpansionExpr(
5955 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5956 E->getNumExpansions());
5957}
5958
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005959Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5960 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5961 if (!Pack)
5962 return nullptr;
5963
5964 Optional<unsigned> Length;
5965
5966 if (!E->isValueDependent())
5967 Length = E->getPackLength();
5968
5969 SmallVector<TemplateArgument, 8> PartialArguments;
5970 if (E->isPartiallySubstituted()) {
5971 if (ImportTemplateArguments(E->getPartialArguments().data(),
5972 E->getPartialArguments().size(),
5973 PartialArguments))
5974 return nullptr;
5975 }
5976
5977 return SizeOfPackExpr::Create(
5978 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5979 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5980 Length, PartialArguments);
5981}
5982
Aleksei Sidorina693b372016-09-28 10:16:56 +00005983Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5984 QualType T = Importer.Import(CE->getType());
5985 if (T.isNull())
5986 return nullptr;
5987
5988 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5989 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5990 return nullptr;
5991
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005992 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005993 Importer.Import(CE->getOperatorNew()));
5994 if (!OperatorNewDecl && CE->getOperatorNew())
5995 return nullptr;
5996
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005997 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005998 Importer.Import(CE->getOperatorDelete()));
5999 if (!OperatorDeleteDecl && CE->getOperatorDelete())
6000 return nullptr;
6001
6002 Expr *ToInit = Importer.Import(CE->getInitializer());
6003 if (!ToInit && CE->getInitializer())
6004 return nullptr;
6005
6006 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6007 if (!TInfo)
6008 return nullptr;
6009
6010 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6011 if (!ToArrSize && CE->getArraySize())
6012 return nullptr;
6013
6014 return new (Importer.getToContext()) CXXNewExpr(
6015 Importer.getToContext(),
6016 CE->isGlobalNew(),
6017 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006018 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006019 CE->doesUsualArrayDeleteWantSize(),
6020 PlacementArgs,
6021 Importer.Import(CE->getTypeIdParens()),
6022 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6023 Importer.Import(CE->getSourceRange()),
6024 Importer.Import(CE->getDirectInitRange()));
6025}
6026
6027Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6028 QualType T = Importer.Import(E->getType());
6029 if (T.isNull())
6030 return nullptr;
6031
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006032 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006033 Importer.Import(E->getOperatorDelete()));
6034 if (!OperatorDeleteDecl && E->getOperatorDelete())
6035 return nullptr;
6036
6037 Expr *ToArg = Importer.Import(E->getArgument());
6038 if (!ToArg && E->getArgument())
6039 return nullptr;
6040
6041 return new (Importer.getToContext()) CXXDeleteExpr(
6042 T, E->isGlobalDelete(),
6043 E->isArrayForm(),
6044 E->isArrayFormAsWritten(),
6045 E->doesUsualArrayDeleteWantSize(),
6046 OperatorDeleteDecl,
6047 ToArg,
6048 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006049}
6050
Sean Callanan59721b32015-04-28 18:41:46 +00006051Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6052 QualType T = Importer.Import(E->getType());
6053 if (T.isNull())
6054 return nullptr;
6055
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006056 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006057 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006058 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006059 return nullptr;
6060
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006061 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006062 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006063 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006064
6065 return CXXConstructExpr::Create(Importer.getToContext(), T,
6066 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006067 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006068 ToArgs, E->hadMultipleCandidates(),
6069 E->isListInitialization(),
6070 E->isStdInitListInitialization(),
6071 E->requiresZeroInitialization(),
6072 E->getConstructionKind(),
6073 Importer.Import(E->getParenOrBraceRange()));
6074}
6075
Aleksei Sidorina693b372016-09-28 10:16:56 +00006076Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6077 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6078 if (!SubExpr && EWC->getSubExpr())
6079 return nullptr;
6080
6081 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6082 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6083 if (ExprWithCleanups::CleanupObject Obj =
6084 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6085 Objs[I] = Obj;
6086 else
6087 return nullptr;
6088
6089 return ExprWithCleanups::Create(Importer.getToContext(),
6090 SubExpr, EWC->cleanupsHaveSideEffects(),
6091 Objs);
6092}
6093
Sean Callanan8bca9962016-03-28 21:43:01 +00006094Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6095 QualType T = Importer.Import(E->getType());
6096 if (T.isNull())
6097 return nullptr;
6098
6099 Expr *ToFn = Importer.Import(E->getCallee());
6100 if (!ToFn)
6101 return nullptr;
6102
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006103 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006104 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006105 return nullptr;
6106
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006107 return new (Importer.getToContext()) CXXMemberCallExpr(
6108 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6109 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006110}
6111
6112Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6113 QualType T = Importer.Import(E->getType());
6114 if (T.isNull())
6115 return nullptr;
6116
6117 return new (Importer.getToContext())
6118 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6119}
6120
6121Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6122 QualType T = Importer.Import(E->getType());
6123 if (T.isNull())
6124 return nullptr;
6125
6126 return new (Importer.getToContext())
6127 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6128}
6129
6130
Sean Callanan59721b32015-04-28 18:41:46 +00006131Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6132 QualType T = Importer.Import(E->getType());
6133 if (T.isNull())
6134 return nullptr;
6135
6136 Expr *ToBase = Importer.Import(E->getBase());
6137 if (!ToBase && E->getBase())
6138 return nullptr;
6139
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006140 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006141 if (!ToMember && E->getMemberDecl())
6142 return nullptr;
6143
Peter Szecsief972522018-05-02 11:52:54 +00006144 auto *ToDecl =
6145 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6146 if (!ToDecl && E->getFoundDecl().getDecl())
6147 return nullptr;
6148
6149 DeclAccessPair ToFoundDecl =
6150 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006151
6152 DeclarationNameInfo ToMemberNameInfo(
6153 Importer.Import(E->getMemberNameInfo().getName()),
6154 Importer.Import(E->getMemberNameInfo().getLoc()));
6155
6156 if (E->hasExplicitTemplateArgs()) {
6157 return nullptr; // FIXME: handle template arguments
6158 }
6159
6160 return MemberExpr::Create(Importer.getToContext(), ToBase,
6161 E->isArrow(),
6162 Importer.Import(E->getOperatorLoc()),
6163 Importer.Import(E->getQualifierLoc()),
6164 Importer.Import(E->getTemplateKeywordLoc()),
6165 ToMember, ToFoundDecl, ToMemberNameInfo,
6166 nullptr, T, E->getValueKind(),
6167 E->getObjectKind());
6168}
6169
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006170Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6171 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006172 Expr *BaseE = Importer.Import(E->getBase());
6173 if (!BaseE)
6174 return nullptr;
6175
6176 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6177 if (!ScopeInfo && E->getScopeTypeInfo())
6178 return nullptr;
6179
6180 PseudoDestructorTypeStorage Storage;
6181 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6182 IdentifierInfo *ToII = Importer.Import(FromII);
6183 if (!ToII)
6184 return nullptr;
6185 Storage = PseudoDestructorTypeStorage(
6186 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6187 } else {
6188 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6189 if (!TI)
6190 return nullptr;
6191 Storage = PseudoDestructorTypeStorage(TI);
6192 }
6193
6194 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6195 Importer.getToContext(), BaseE, E->isArrow(),
6196 Importer.Import(E->getOperatorLoc()),
6197 Importer.Import(E->getQualifierLoc()),
6198 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6199 Importer.Import(E->getTildeLoc()), Storage);
6200}
6201
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006202Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6203 CXXDependentScopeMemberExpr *E) {
6204 Expr *Base = nullptr;
6205 if (!E->isImplicitAccess()) {
6206 Base = Importer.Import(E->getBase());
6207 if (!Base)
6208 return nullptr;
6209 }
6210
6211 QualType BaseType = Importer.Import(E->getBaseType());
6212 if (BaseType.isNull())
6213 return nullptr;
6214
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006215 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006216 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006217 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6218 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006219 return nullptr;
6220 ResInfo = &ToTAInfo;
6221 }
6222
6223 DeclarationName Name = Importer.Import(E->getMember());
6224 if (!E->getMember().isEmpty() && Name.isEmpty())
6225 return nullptr;
6226
6227 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6228 // Import additional name location/type info.
6229 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6230 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6231 if (!ToFQ && E->getFirstQualifierFoundInScope())
6232 return nullptr;
6233
6234 return CXXDependentScopeMemberExpr::Create(
6235 Importer.getToContext(), Base, BaseType, E->isArrow(),
6236 Importer.Import(E->getOperatorLoc()),
6237 Importer.Import(E->getQualifierLoc()),
6238 Importer.Import(E->getTemplateKeywordLoc()),
6239 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6240}
6241
Peter Szecsice7f3182018-05-07 12:08:27 +00006242Expr *
6243ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6244 DeclarationName Name = Importer.Import(E->getDeclName());
6245 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6246 return nullptr;
6247
6248 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6249 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6250
6251 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6252 Importer.Import(E->getRAngleLoc()));
6253 TemplateArgumentListInfo *ResInfo = nullptr;
6254 if (E->hasExplicitTemplateArgs()) {
6255 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6256 return nullptr;
6257 ResInfo = &ToTAInfo;
6258 }
6259
6260 return DependentScopeDeclRefExpr::Create(
6261 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6262 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6263}
6264
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006265Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6266 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006267 unsigned NumArgs = CE->arg_size();
6268
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006269 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006270 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6271 return nullptr;
6272
6273 return CXXUnresolvedConstructExpr::Create(
6274 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6275 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6276 Importer.Import(CE->getRParenLoc()));
6277}
6278
6279Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006280 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006281 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6282 if (E->getNamingClass() && !NamingClass)
6283 return nullptr;
6284
6285 DeclarationName Name = Importer.Import(E->getName());
6286 if (E->getName() && !Name)
6287 return nullptr;
6288
6289 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6290 // Import additional name location/type info.
6291 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6292
6293 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006294 for (auto *D : E->decls()) {
6295 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006296 ToDecls.addDecl(To);
6297 else
6298 return nullptr;
6299 }
6300
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006301 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006302 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006303 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6304 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006305 return nullptr;
6306 ResInfo = &ToTAInfo;
6307 }
6308
6309 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6310 return UnresolvedLookupExpr::Create(
6311 Importer.getToContext(), NamingClass,
6312 Importer.Import(E->getQualifierLoc()),
6313 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6314 ResInfo, ToDecls.begin(), ToDecls.end());
6315
6316 return UnresolvedLookupExpr::Create(
6317 Importer.getToContext(), NamingClass,
6318 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6319 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6320}
6321
Peter Szecsice7f3182018-05-07 12:08:27 +00006322Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6323 DeclarationName Name = Importer.Import(E->getName());
6324 if (!E->getName().isEmpty() && Name.isEmpty())
6325 return nullptr;
6326 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6327 // Import additional name location/type info.
6328 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6329
6330 QualType BaseType = Importer.Import(E->getType());
6331 if (!E->getType().isNull() && BaseType.isNull())
6332 return nullptr;
6333
6334 UnresolvedSet<8> ToDecls;
6335 for (Decl *D : E->decls()) {
6336 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6337 ToDecls.addDecl(To);
6338 else
6339 return nullptr;
6340 }
6341
6342 TemplateArgumentListInfo ToTAInfo;
6343 TemplateArgumentListInfo *ResInfo = nullptr;
6344 if (E->hasExplicitTemplateArgs()) {
6345 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6346 return nullptr;
6347 ResInfo = &ToTAInfo;
6348 }
6349
6350 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6351 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6352 return nullptr;
6353 }
6354
6355 return UnresolvedMemberExpr::Create(
6356 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6357 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6358 Importer.Import(E->getQualifierLoc()),
6359 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6360 ToDecls.begin(), ToDecls.end());
6361}
6362
Sean Callanan59721b32015-04-28 18:41:46 +00006363Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6364 QualType T = Importer.Import(E->getType());
6365 if (T.isNull())
6366 return nullptr;
6367
6368 Expr *ToCallee = Importer.Import(E->getCallee());
6369 if (!ToCallee && E->getCallee())
6370 return nullptr;
6371
6372 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006373 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006374 if (ImportContainerChecked(E->arguments(), ToArgs))
6375 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006376
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006377 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006378
6379 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6380 ToArgs_Copied[ai] = ToArgs[ai];
6381
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006382 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6383 return new (Importer.getToContext()) CXXOperatorCallExpr(
6384 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6385 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6386 OCE->getFPFeatures());
6387 }
6388
Sean Callanan59721b32015-04-28 18:41:46 +00006389 return new (Importer.getToContext())
6390 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006391 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006392 Importer.Import(E->getRParenLoc()));
6393}
6394
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006395Optional<LambdaCapture>
6396ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6397 VarDecl *Var = nullptr;
6398 if (From.capturesVariable()) {
6399 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6400 if (!Var)
6401 return None;
6402 }
6403
6404 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6405 From.getCaptureKind(), Var,
6406 From.isPackExpansion()
6407 ? Importer.Import(From.getEllipsisLoc())
6408 : SourceLocation());
6409}
6410
6411Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6412 CXXRecordDecl *FromClass = LE->getLambdaClass();
6413 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6414 if (!ToClass)
6415 return nullptr;
6416
6417 // NOTE: lambda classes are created with BeingDefined flag set up.
6418 // It means that ImportDefinition doesn't work for them and we should fill it
6419 // manually.
6420 if (ToClass->isBeingDefined()) {
6421 for (auto FromField : FromClass->fields()) {
6422 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6423 if (!ToField)
6424 return nullptr;
6425 }
6426 }
6427
6428 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6429 Importer.Import(LE->getCallOperator()));
6430 if (!ToCallOp)
6431 return nullptr;
6432
6433 ToClass->completeDefinition();
6434
6435 unsigned NumCaptures = LE->capture_size();
6436 SmallVector<LambdaCapture, 8> Captures;
6437 Captures.reserve(NumCaptures);
6438 for (const auto &FromCapture : LE->captures()) {
6439 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6440 Captures.push_back(*ToCapture);
6441 else
6442 return nullptr;
6443 }
6444
6445 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6446 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6447 return nullptr;
6448
6449 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6450 Importer.Import(LE->getIntroducerRange()),
6451 LE->getCaptureDefault(),
6452 Importer.Import(LE->getCaptureDefaultLoc()),
6453 Captures,
6454 LE->hasExplicitParameters(),
6455 LE->hasExplicitResultType(),
6456 InitCaptures,
6457 Importer.Import(LE->getLocEnd()),
6458 LE->containsUnexpandedParameterPack());
6459}
6460
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006461Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6462 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006463 if (T.isNull())
6464 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006465
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006466 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006467 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006468 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006469
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006470 ASTContext &ToCtx = Importer.getToContext();
6471 InitListExpr *To = new (ToCtx) InitListExpr(
6472 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6473 Exprs, Importer.Import(ILE->getLBraceLoc()));
6474 To->setType(T);
6475
6476 if (ILE->hasArrayFiller()) {
6477 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6478 if (!Filler)
6479 return nullptr;
6480 To->setArrayFiller(Filler);
6481 }
6482
6483 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006484 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006485 if (!ToFD)
6486 return nullptr;
6487 To->setInitializedFieldInUnion(ToFD);
6488 }
6489
6490 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006491 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006492 if (!ToSyntForm)
6493 return nullptr;
6494 To->setSyntacticForm(ToSyntForm);
6495 }
6496
6497 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6498 To->setValueDependent(ILE->isValueDependent());
6499 To->setInstantiationDependent(ILE->isInstantiationDependent());
6500
6501 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006502}
6503
Richard Smith30e304e2016-12-14 00:03:17 +00006504Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6505 QualType ToType = Importer.Import(E->getType());
6506 if (ToType.isNull())
6507 return nullptr;
6508
6509 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6510 if (!ToCommon && E->getCommonExpr())
6511 return nullptr;
6512
6513 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6514 if (!ToSubExpr && E->getSubExpr())
6515 return nullptr;
6516
6517 return new (Importer.getToContext())
6518 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6519}
6520
6521Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6522 QualType ToType = Importer.Import(E->getType());
6523 if (ToType.isNull())
6524 return nullptr;
6525 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6526}
6527
Sean Callanandd2c1742016-05-16 20:48:03 +00006528Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006529 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006530 if (!ToField && DIE->getField())
6531 return nullptr;
6532
6533 return CXXDefaultInitExpr::Create(
6534 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6535}
6536
6537Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6538 QualType ToType = Importer.Import(E->getType());
6539 if (ToType.isNull() && !E->getType().isNull())
6540 return nullptr;
6541 ExprValueKind VK = E->getValueKind();
6542 CastKind CK = E->getCastKind();
6543 Expr *ToOp = Importer.Import(E->getSubExpr());
6544 if (!ToOp && E->getSubExpr())
6545 return nullptr;
6546 CXXCastPath BasePath;
6547 if (ImportCastPath(E, BasePath))
6548 return nullptr;
6549 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6550 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6551 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6552 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6553
6554 if (isa<CXXStaticCastExpr>(E)) {
6555 return CXXStaticCastExpr::Create(
6556 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6557 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6558 } else if (isa<CXXDynamicCastExpr>(E)) {
6559 return CXXDynamicCastExpr::Create(
6560 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6561 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6562 } else if (isa<CXXReinterpretCastExpr>(E)) {
6563 return CXXReinterpretCastExpr::Create(
6564 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6565 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6566 } else {
6567 return nullptr;
6568 }
6569}
6570
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006571Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6572 SubstNonTypeTemplateParmExpr *E) {
6573 QualType T = Importer.Import(E->getType());
6574 if (T.isNull())
6575 return nullptr;
6576
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006577 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006578 Importer.Import(E->getParameter()));
6579 if (!Param)
6580 return nullptr;
6581
6582 Expr *Replacement = Importer.Import(E->getReplacement());
6583 if (!Replacement)
6584 return nullptr;
6585
6586 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6587 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6588 Replacement);
6589}
6590
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006591Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6592 QualType ToType = Importer.Import(E->getType());
6593 if (ToType.isNull())
6594 return nullptr;
6595
6596 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6597 if (ImportContainerChecked(E->getArgs(), ToArgs))
6598 return nullptr;
6599
6600 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6601 // Value is always false.
6602 bool ToValue = false;
6603 if (!E->isValueDependent())
6604 ToValue = E->getValue();
6605
6606 return TypeTraitExpr::Create(
6607 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6608 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6609}
6610
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006611Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6612 QualType ToType = Importer.Import(E->getType());
6613 if (ToType.isNull())
6614 return nullptr;
6615
6616 if (E->isTypeOperand()) {
6617 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6618 if (!TSI)
6619 return nullptr;
6620
6621 return new (Importer.getToContext())
6622 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6623 }
6624
6625 Expr *Op = Importer.Import(E->getExprOperand());
6626 if (!Op)
6627 return nullptr;
6628
6629 return new (Importer.getToContext())
6630 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6631}
6632
Lang Hames19e07e12017-06-20 21:06:00 +00006633void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6634 CXXMethodDecl *FromMethod) {
6635 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6636 ToMethod->addOverriddenMethod(
6637 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6638 FromOverriddenMethod))));
6639}
6640
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006641ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006642 ASTContext &FromContext, FileManager &FromFileManager,
6643 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006644 : ToContext(ToContext), FromContext(FromContext),
6645 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6646 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006647 ImportedDecls[FromContext.getTranslationUnitDecl()]
6648 = ToContext.getTranslationUnitDecl();
6649}
6650
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006651ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006652
6653QualType ASTImporter::Import(QualType FromT) {
6654 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006655 return {};
John McCall424cec92011-01-19 06:33:43 +00006656
6657 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006658
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006659 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006660 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6661 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006662 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006663 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006664
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006665 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006666 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006667 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006668 if (ToT.isNull())
6669 return ToT;
6670
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006671 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006672 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006673
John McCall424cec92011-01-19 06:33:43 +00006674 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006675}
6676
Douglas Gregor62d311f2010-02-09 19:21:46 +00006677TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006678 if (!FromTSI)
6679 return FromTSI;
6680
6681 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006682 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006683 QualType T = Import(FromTSI->getType());
6684 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006685 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006686
6687 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006688 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006689}
6690
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006691Attr *ASTImporter::Import(const Attr *FromAttr) {
6692 Attr *ToAttr = FromAttr->clone(ToContext);
6693 ToAttr->setRange(Import(FromAttr->getRange()));
6694 return ToAttr;
6695}
6696
Sean Callanan59721b32015-04-28 18:41:46 +00006697Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6698 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6699 if (Pos != ImportedDecls.end()) {
6700 Decl *ToD = Pos->second;
6701 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6702 return ToD;
6703 } else {
6704 return nullptr;
6705 }
6706}
6707
Douglas Gregor62d311f2010-02-09 19:21:46 +00006708Decl *ASTImporter::Import(Decl *FromD) {
6709 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006710 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006711
Douglas Gregord451ea92011-07-29 23:31:30 +00006712 ASTNodeImporter Importer(*this);
6713
Douglas Gregor62d311f2010-02-09 19:21:46 +00006714 // Check whether we've already imported this declaration.
6715 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006716 if (Pos != ImportedDecls.end()) {
6717 Decl *ToD = Pos->second;
6718 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6719 return ToD;
6720 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006721
6722 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006723 Decl *ToD = Importer.Visit(FromD);
6724 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006725 return nullptr;
6726
Douglas Gregor62d311f2010-02-09 19:21:46 +00006727 // Record the imported declaration.
6728 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006729 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006730 return ToD;
6731}
6732
6733DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6734 if (!FromDC)
6735 return FromDC;
6736
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006737 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006738 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006739 return nullptr;
6740
Douglas Gregor2e15c842012-02-01 21:00:38 +00006741 // When we're using a record/enum/Objective-C class/protocol as a context, we
6742 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006743 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6744 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006745 if (ToRecord->isCompleteDefinition()) {
6746 // Do nothing.
6747 } else if (FromRecord->isCompleteDefinition()) {
6748 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6749 ASTNodeImporter::IDK_Basic);
6750 } else {
6751 CompleteDecl(ToRecord);
6752 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006753 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6754 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006755 if (ToEnum->isCompleteDefinition()) {
6756 // Do nothing.
6757 } else if (FromEnum->isCompleteDefinition()) {
6758 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6759 ASTNodeImporter::IDK_Basic);
6760 } else {
6761 CompleteDecl(ToEnum);
6762 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006763 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6764 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006765 if (ToClass->getDefinition()) {
6766 // Do nothing.
6767 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6768 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6769 ASTNodeImporter::IDK_Basic);
6770 } else {
6771 CompleteDecl(ToClass);
6772 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006773 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6774 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006775 if (ToProto->getDefinition()) {
6776 // Do nothing.
6777 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6778 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6779 ASTNodeImporter::IDK_Basic);
6780 } else {
6781 CompleteDecl(ToProto);
6782 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006783 }
6784
6785 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006786}
6787
6788Expr *ASTImporter::Import(Expr *FromE) {
6789 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006790 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006791
6792 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6793}
6794
6795Stmt *ASTImporter::Import(Stmt *FromS) {
6796 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006797 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006798
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006799 // Check whether we've already imported this declaration.
6800 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6801 if (Pos != ImportedStmts.end())
6802 return Pos->second;
6803
6804 // Import the type
6805 ASTNodeImporter Importer(*this);
6806 Stmt *ToS = Importer.Visit(FromS);
6807 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006808 return nullptr;
6809
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006810 // Record the imported declaration.
6811 ImportedStmts[FromS] = ToS;
6812 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006813}
6814
6815NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6816 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006817 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006818
Douglas Gregor90ebf252011-04-27 16:48:40 +00006819 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6820
6821 switch (FromNNS->getKind()) {
6822 case NestedNameSpecifier::Identifier:
6823 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6824 return NestedNameSpecifier::Create(ToContext, prefix, II);
6825 }
Craig Topper36250ad2014-05-12 05:36:57 +00006826 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006827
6828 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006829 if (auto *NS =
6830 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006831 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6832 }
Craig Topper36250ad2014-05-12 05:36:57 +00006833 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006834
6835 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006836 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006837 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006838 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6839 }
Craig Topper36250ad2014-05-12 05:36:57 +00006840 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006841
6842 case NestedNameSpecifier::Global:
6843 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6844
Nikola Smiljanic67860242014-09-26 00:28:20 +00006845 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006846 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006847 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006848 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6849 }
6850 return nullptr;
6851
Douglas Gregor90ebf252011-04-27 16:48:40 +00006852 case NestedNameSpecifier::TypeSpec:
6853 case NestedNameSpecifier::TypeSpecWithTemplate: {
6854 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6855 if (!T.isNull()) {
6856 bool bTemplate = FromNNS->getKind() ==
6857 NestedNameSpecifier::TypeSpecWithTemplate;
6858 return NestedNameSpecifier::Create(ToContext, prefix,
6859 bTemplate, T.getTypePtr());
6860 }
6861 }
Craig Topper36250ad2014-05-12 05:36:57 +00006862 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006863 }
6864
6865 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006866}
6867
Douglas Gregor14454802011-02-25 02:25:35 +00006868NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006869 // Copied from NestedNameSpecifier mostly.
6870 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6871 NestedNameSpecifierLoc NNS = FromNNS;
6872
6873 // Push each of the nested-name-specifiers's onto a stack for
6874 // serialization in reverse order.
6875 while (NNS) {
6876 NestedNames.push_back(NNS);
6877 NNS = NNS.getPrefix();
6878 }
6879
6880 NestedNameSpecifierLocBuilder Builder;
6881
6882 while (!NestedNames.empty()) {
6883 NNS = NestedNames.pop_back_val();
6884 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6885 if (!Spec)
6886 return NestedNameSpecifierLoc();
6887
6888 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6889 switch (Kind) {
6890 case NestedNameSpecifier::Identifier:
6891 Builder.Extend(getToContext(),
6892 Spec->getAsIdentifier(),
6893 Import(NNS.getLocalBeginLoc()),
6894 Import(NNS.getLocalEndLoc()));
6895 break;
6896
6897 case NestedNameSpecifier::Namespace:
6898 Builder.Extend(getToContext(),
6899 Spec->getAsNamespace(),
6900 Import(NNS.getLocalBeginLoc()),
6901 Import(NNS.getLocalEndLoc()));
6902 break;
6903
6904 case NestedNameSpecifier::NamespaceAlias:
6905 Builder.Extend(getToContext(),
6906 Spec->getAsNamespaceAlias(),
6907 Import(NNS.getLocalBeginLoc()),
6908 Import(NNS.getLocalEndLoc()));
6909 break;
6910
6911 case NestedNameSpecifier::TypeSpec:
6912 case NestedNameSpecifier::TypeSpecWithTemplate: {
6913 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6914 QualType(Spec->getAsType(), 0));
6915 Builder.Extend(getToContext(),
6916 Import(NNS.getLocalBeginLoc()),
6917 TSI->getTypeLoc(),
6918 Import(NNS.getLocalEndLoc()));
6919 break;
6920 }
6921
6922 case NestedNameSpecifier::Global:
6923 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6924 break;
6925
6926 case NestedNameSpecifier::Super: {
6927 SourceRange ToRange = Import(NNS.getSourceRange());
6928 Builder.MakeSuper(getToContext(),
6929 Spec->getAsRecordDecl(),
6930 ToRange.getBegin(),
6931 ToRange.getEnd());
6932 }
6933 }
6934 }
6935
6936 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006937}
6938
Douglas Gregore2e50d332010-12-01 01:36:18 +00006939TemplateName ASTImporter::Import(TemplateName From) {
6940 switch (From.getKind()) {
6941 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006942 if (auto *ToTemplate =
6943 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006944 return TemplateName(ToTemplate);
6945
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006946 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006947
6948 case TemplateName::OverloadedTemplate: {
6949 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6950 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006951 for (auto *I : *FromStorage) {
6952 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006953 ToTemplates.addDecl(To);
6954 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006955 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006956 }
6957 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6958 ToTemplates.end());
6959 }
6960
6961 case TemplateName::QualifiedTemplate: {
6962 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6963 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6964 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006965 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006966
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006967 if (auto *ToTemplate =
6968 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006969 return ToContext.getQualifiedTemplateName(Qualifier,
6970 QTN->hasTemplateKeyword(),
6971 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006972
6973 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006974 }
6975
6976 case TemplateName::DependentTemplate: {
6977 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6978 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6979 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006980 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006981
6982 if (DTN->isIdentifier()) {
6983 return ToContext.getDependentTemplateName(Qualifier,
6984 Import(DTN->getIdentifier()));
6985 }
6986
6987 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6988 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006989
6990 case TemplateName::SubstTemplateTemplateParm: {
6991 SubstTemplateTemplateParmStorage *subst
6992 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006993 auto *param =
6994 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006995 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006996 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006997
6998 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006999 if (replacement.isNull())
7000 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00007001
7002 return ToContext.getSubstTemplateTemplateParm(param, replacement);
7003 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007004
7005 case TemplateName::SubstTemplateTemplateParmPack: {
7006 SubstTemplateTemplateParmPackStorage *SubstPack
7007 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007008 auto *Param =
7009 cast_or_null<TemplateTemplateParmDecl>(
7010 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007011 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007012 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007013
7014 ASTNodeImporter Importer(*this);
7015 TemplateArgument ArgPack
7016 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7017 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007018 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007019
7020 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7021 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007022 }
7023
7024 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007025}
7026
Douglas Gregor62d311f2010-02-09 19:21:46 +00007027SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7028 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007029 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007030
Douglas Gregor811663e2010-02-10 00:15:17 +00007031 SourceManager &FromSM = FromContext.getSourceManager();
7032
Sean Callanan24c5fe62016-11-07 20:42:25 +00007033 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007034 // don't have to import macro expansions.
7035 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007036 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007037 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7038 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007039 FileID ToFileID = Import(Decomposed.first);
7040 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007041 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007042 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7043 .getLocWithOffset(Decomposed.second);
7044 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007045}
7046
7047SourceRange ASTImporter::Import(SourceRange FromRange) {
7048 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7049}
7050
Douglas Gregor811663e2010-02-10 00:15:17 +00007051FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007052 llvm::DenseMap<FileID, FileID>::iterator Pos
7053 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007054 if (Pos != ImportedFileIDs.end())
7055 return Pos->second;
7056
7057 SourceManager &FromSM = FromContext.getSourceManager();
7058 SourceManager &ToSM = ToContext.getSourceManager();
7059 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007060 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007061
7062 // Include location of this file.
7063 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7064
7065 // Map the FileID for to the "to" source manager.
7066 FileID ToID;
7067 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007068 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007069 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7070 // disk again
7071 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7072 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007073 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007074 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007075 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007076 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7077 FromSLoc.getFile().getFileCharacteristic());
7078 } else {
7079 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007080 const llvm::MemoryBuffer *
7081 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007082 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007083 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007084 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007085 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007086 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007087 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007088
Sebastian Redl99219f12010-09-30 01:03:06 +00007089 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007090 return ToID;
7091}
7092
Sean Callanandd2c1742016-05-16 20:48:03 +00007093CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7094 Expr *ToExpr = Import(From->getInit());
7095 if (!ToExpr && From->getInit())
7096 return nullptr;
7097
7098 if (From->isBaseInitializer()) {
7099 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7100 if (!ToTInfo && From->getTypeSourceInfo())
7101 return nullptr;
7102
7103 return new (ToContext) CXXCtorInitializer(
7104 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7105 ToExpr, Import(From->getRParenLoc()),
7106 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7107 : SourceLocation());
7108 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007109 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007110 if (!ToField && From->getMember())
7111 return nullptr;
7112
7113 return new (ToContext) CXXCtorInitializer(
7114 ToContext, ToField, Import(From->getMemberLocation()),
7115 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7116 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007117 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007118 Import(From->getIndirectMember()));
7119 if (!ToIField && From->getIndirectMember())
7120 return nullptr;
7121
7122 return new (ToContext) CXXCtorInitializer(
7123 ToContext, ToIField, Import(From->getMemberLocation()),
7124 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7125 } else if (From->isDelegatingInitializer()) {
7126 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7127 if (!ToTInfo && From->getTypeSourceInfo())
7128 return nullptr;
7129
7130 return new (ToContext)
7131 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7132 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007133 } else {
7134 return nullptr;
7135 }
7136}
7137
Aleksei Sidorina693b372016-09-28 10:16:56 +00007138CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7139 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7140 if (Pos != ImportedCXXBaseSpecifiers.end())
7141 return Pos->second;
7142
7143 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7144 Import(BaseSpec->getSourceRange()),
7145 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7146 BaseSpec->getAccessSpecifierAsWritten(),
7147 Import(BaseSpec->getTypeSourceInfo()),
7148 Import(BaseSpec->getEllipsisLoc()));
7149 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7150 return Imported;
7151}
7152
Douglas Gregor0a791672011-01-18 03:11:38 +00007153void ASTImporter::ImportDefinition(Decl *From) {
7154 Decl *To = Import(From);
7155 if (!To)
7156 return;
7157
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007158 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007159 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007160
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007161 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007162 if (!ToRecord->getDefinition()) {
7163 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007164 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007165 return;
7166 }
7167 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007168
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007169 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007170 if (!ToEnum->getDefinition()) {
7171 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007172 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007173 return;
7174 }
7175 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007176
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007177 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007178 if (!ToIFace->getDefinition()) {
7179 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007180 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007181 return;
7182 }
7183 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007184
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007185 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007186 if (!ToProto->getDefinition()) {
7187 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007188 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007189 return;
7190 }
7191 }
7192
Douglas Gregor0a791672011-01-18 03:11:38 +00007193 Importer.ImportDeclContext(FromDC, true);
7194 }
7195}
7196
Douglas Gregor96e578d2010-02-05 17:54:41 +00007197DeclarationName ASTImporter::Import(DeclarationName FromName) {
7198 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007199 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007200
7201 switch (FromName.getNameKind()) {
7202 case DeclarationName::Identifier:
7203 return Import(FromName.getAsIdentifierInfo());
7204
7205 case DeclarationName::ObjCZeroArgSelector:
7206 case DeclarationName::ObjCOneArgSelector:
7207 case DeclarationName::ObjCMultiArgSelector:
7208 return Import(FromName.getObjCSelector());
7209
7210 case DeclarationName::CXXConstructorName: {
7211 QualType T = Import(FromName.getCXXNameType());
7212 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007213 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007214
7215 return ToContext.DeclarationNames.getCXXConstructorName(
7216 ToContext.getCanonicalType(T));
7217 }
7218
7219 case DeclarationName::CXXDestructorName: {
7220 QualType T = Import(FromName.getCXXNameType());
7221 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007222 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007223
7224 return ToContext.DeclarationNames.getCXXDestructorName(
7225 ToContext.getCanonicalType(T));
7226 }
7227
Richard Smith35845152017-02-07 01:37:30 +00007228 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007229 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007230 Import(FromName.getCXXDeductionGuideTemplate()));
7231 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007232 return {};
Richard Smith35845152017-02-07 01:37:30 +00007233 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7234 }
7235
Douglas Gregor96e578d2010-02-05 17:54:41 +00007236 case DeclarationName::CXXConversionFunctionName: {
7237 QualType T = Import(FromName.getCXXNameType());
7238 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007239 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007240
7241 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7242 ToContext.getCanonicalType(T));
7243 }
7244
7245 case DeclarationName::CXXOperatorName:
7246 return ToContext.DeclarationNames.getCXXOperatorName(
7247 FromName.getCXXOverloadedOperator());
7248
7249 case DeclarationName::CXXLiteralOperatorName:
7250 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7251 Import(FromName.getCXXLiteralIdentifier()));
7252
7253 case DeclarationName::CXXUsingDirective:
7254 // FIXME: STATICS!
7255 return DeclarationName::getUsingDirectiveName();
7256 }
7257
David Blaikiee4d798f2012-01-20 21:50:17 +00007258 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007259}
7260
Douglas Gregore2e50d332010-12-01 01:36:18 +00007261IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007262 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007263 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007264
Sean Callananf94ef1d2016-05-14 06:11:19 +00007265 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7266
7267 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7268 ToId->setBuiltinID(FromId->getBuiltinID());
7269
7270 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007271}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007272
Douglas Gregor43f54792010-02-17 02:12:47 +00007273Selector ASTImporter::Import(Selector FromSel) {
7274 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007275 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007276
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007277 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007278 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7279 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7280 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7281 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7282}
7283
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007284DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7285 DeclContext *DC,
7286 unsigned IDNS,
7287 NamedDecl **Decls,
7288 unsigned NumDecls) {
7289 return Name;
7290}
7291
7292DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007293 if (LastDiagFromFrom)
7294 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7295 FromContext.getDiagnostics());
7296 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007297 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007298}
7299
7300DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007301 if (!LastDiagFromFrom)
7302 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7303 ToContext.getDiagnostics());
7304 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007305 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007306}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007307
Douglas Gregor2e15c842012-02-01 21:00:38 +00007308void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007309 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007310 if (!ID->getDefinition())
7311 ID->startDefinition();
7312 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007313 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007314 if (!PD->getDefinition())
7315 PD->startDefinition();
7316 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007317 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007318 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7319 TD->startDefinition();
7320 TD->setCompleteDefinition(true);
7321 }
7322 }
7323 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007324 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007325 }
7326}
7327
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007328Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007329 if (From->hasAttrs()) {
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007330 for (const auto *FromAttr : From->getAttrs())
7331 To->addAttr(Import(FromAttr));
Sean Callanan8bca9962016-03-28 21:43:01 +00007332 }
7333 if (From->isUsed()) {
7334 To->setIsUsed();
7335 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007336 if (From->isImplicit()) {
7337 To->setImplicit();
7338 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007339 ImportedDecls[From] = To;
7340 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007341}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007342
Douglas Gregordd6006f2012-07-17 21:16:27 +00007343bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7344 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007345 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007346 = ImportedTypes.find(From.getTypePtr());
7347 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7348 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007349
Douglas Gregordd6006f2012-07-17 21:16:27 +00007350 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7351 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007352 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007353}