blob: 7e402722b19d1a33569472ebe11cf668a75e5272 [file] [log] [blame]
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001//===- ASTImporter.cpp - Importing ASTs from other Contexts ---------------===//
Douglas Gregor96e578d2010-02-05 17:54:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000014
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000016#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000018#include "clang/AST/ASTStructuralEquivalence.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000019#include "clang/AST/Attr.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclAccessPair.h"
22#include "clang/AST/DeclBase.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000023#include "clang/AST/DeclCXX.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000024#include "clang/AST/DeclFriend.h"
25#include "clang/AST/DeclGroup.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000026#include "clang/AST/DeclObjC.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000027#include "clang/AST/DeclTemplate.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028#include "clang/AST/DeclVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000029#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExprObjC.h"
33#include "clang/AST/ExternalASTSource.h"
34#include "clang/AST/LambdaCapture.h"
35#include "clang/AST/NestedNameSpecifier.h"
36#include "clang/AST/OperationKinds.h"
37#include "clang/AST/Stmt.h"
38#include "clang/AST/StmtCXX.h"
39#include "clang/AST/StmtObjC.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000040#include "clang/AST/StmtVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000041#include "clang/AST/TemplateBase.h"
42#include "clang/AST/TemplateName.h"
43#include "clang/AST/Type.h"
44#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000045#include "clang/AST/TypeVisitor.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000046#include "clang/AST/UnresolvedSet.h"
47#include "clang/Basic/ExceptionSpecificationType.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000048#include "clang/Basic/FileManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000049#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/SourceLocation.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000053#include "clang/Basic/SourceManager.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000054#include "clang/Basic/Specifiers.h"
55#include "llvm/ADT/APSInt.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/None.h"
59#include "llvm/ADT/Optional.h"
60#include "llvm/ADT/STLExtras.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000064#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000065#include <algorithm>
66#include <cassert>
67#include <cstddef>
68#include <memory>
69#include <type_traits>
70#include <utility>
Douglas Gregor96e578d2010-02-05 17:54:41 +000071
Douglas Gregor3c2404b2011-11-03 18:07:07 +000072namespace clang {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000073
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000074 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000075 public DeclVisitor<ASTNodeImporter, Decl *>,
76 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000077 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000078
Douglas Gregor96e578d2010-02-05 17:54:41 +000079 public:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +000080 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) {}
Gabor Marton344b0992018-05-16 11:48:11 +000081
Douglas Gregor96e578d2010-02-05 17:54:41 +000082 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000083 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000084 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000085
86 // Importing types
John McCall424cec92011-01-19 06:33:43 +000087 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000088 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000089 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000090 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000091 QualType VisitComplexType(const ComplexType *T);
92 QualType VisitPointerType(const PointerType *T);
93 QualType VisitBlockPointerType(const BlockPointerType *T);
94 QualType VisitLValueReferenceType(const LValueReferenceType *T);
95 QualType VisitRValueReferenceType(const RValueReferenceType *T);
96 QualType VisitMemberPointerType(const MemberPointerType *T);
97 QualType VisitConstantArrayType(const ConstantArrayType *T);
98 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
99 QualType VisitVariableArrayType(const VariableArrayType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000100 QualType VisitDependentSizedArrayType(const DependentSizedArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000101 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +0000102 QualType VisitVectorType(const VectorType *T);
103 QualType VisitExtVectorType(const ExtVectorType *T);
104 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
105 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000106 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +0000107 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +0000108 QualType VisitTypedefType(const TypedefType *T);
109 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000110 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +0000111 QualType VisitTypeOfType(const TypeOfType *T);
112 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +0000113 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +0000114 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000115 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000116 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +0000117 QualType VisitRecordType(const RecordType *T);
118 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +0000119 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000120 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000121 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +0000122 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
123 QualType VisitElaboratedType(const ElaboratedType *T);
Peter Szecsice7f3182018-05-07 12:08:27 +0000124 QualType VisitDependentNameType(const DependentNameType *T);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000125 QualType VisitPackExpansionType(const PackExpansionType *T);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000126 QualType VisitDependentTemplateSpecializationType(
127 const DependentTemplateSpecializationType *T);
John McCall424cec92011-01-19 06:33:43 +0000128 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
129 QualType VisitObjCObjectType(const ObjCObjectType *T);
130 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000131
Douglas Gregor95d82832012-01-24 18:36:04 +0000132 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000133 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
134 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000135 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +0000136 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000137 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
138 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +0000139 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000140
Aleksei Sidorina693b372016-09-28 10:16:56 +0000141 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
142
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000143 using Designator = DesignatedInitExpr::Designator;
144
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000145 Designator ImportDesignator(const Designator &D);
146
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000147 Optional<LambdaCapture> ImportLambdaCapture(const LambdaCapture &From);
Douglas Gregor2e15c842012-02-01 21:00:38 +0000148
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000149 /// What we should import from the definition.
Douglas Gregor95d82832012-01-24 18:36:04 +0000150 enum ImportDefinitionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000151 /// Import the default subset of the definition, which might be
Douglas Gregor95d82832012-01-24 18:36:04 +0000152 /// nothing (if minimal import is set) or might be everything (if minimal
153 /// import is not set).
154 IDK_Default,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Import everything.
Douglas Gregor95d82832012-01-24 18:36:04 +0000157 IDK_Everything,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000159 /// Import only the bare bones needed to establish a valid
Douglas Gregor95d82832012-01-24 18:36:04 +0000160 /// DeclContext.
161 IDK_Basic
162 };
163
Douglas Gregor2e15c842012-02-01 21:00:38 +0000164 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
165 return IDK == IDK_Everything ||
166 (IDK == IDK_Default && !Importer.isMinimalImport());
167 }
168
Douglas Gregord451ea92011-07-29 23:31:30 +0000169 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000170 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000171 bool ImportDefinition(VarDecl *From, VarDecl *To,
172 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000173 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000174 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000175 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000176 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000177 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000178 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000179 TemplateParameterList *ImportTemplateParameterList(
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000180 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000181 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000182 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
183 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000184 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
185 unsigned NumFromArgs,
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000186 SmallVectorImpl<TemplateArgument> &ToArgs);
187
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000188 template <typename InContainerTy>
189 bool ImportTemplateArgumentListInfo(const InContainerTy &Container,
190 TemplateArgumentListInfo &ToTAInfo);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000191
192 template<typename InContainerTy>
193 bool ImportTemplateArgumentListInfo(SourceLocation FromLAngleLoc,
194 SourceLocation FromRAngleLoc,
195 const InContainerTy &Container,
196 TemplateArgumentListInfo &Result);
197
198 bool ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD);
199
Douglas Gregordd6006f2012-07-17 21:16:27 +0000200 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
201 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000202 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
203 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000204 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000205 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000206 bool IsStructuralMatch(FunctionTemplateDecl *From,
207 FunctionTemplateDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +0000208 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000209 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000210 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000211 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000212 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000213 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000214 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000215 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000216 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000217 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000218 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000219 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000220 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000221 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000222 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000223 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000224 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000225 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000226 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
227 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
228 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
229 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000230 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000231 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000232 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000233 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000234 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000235 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000236 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000237 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000238 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000239 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000240 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000241 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000242 Decl *VisitUsingDecl(UsingDecl *D);
243 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
244 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
245 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
246 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
247
Douglas Gregor85f3f952015-07-07 03:57:15 +0000248 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000249 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000250 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000251 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000252 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000253 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000254 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
255 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
256 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
257 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000258 Decl *VisitClassTemplateSpecializationDecl(
259 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000260 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
261 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000262 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000263
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000264 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000265 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
266
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000267 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000268 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000269 Stmt *VisitDeclStmt(DeclStmt *S);
270 Stmt *VisitNullStmt(NullStmt *S);
271 Stmt *VisitCompoundStmt(CompoundStmt *S);
272 Stmt *VisitCaseStmt(CaseStmt *S);
273 Stmt *VisitDefaultStmt(DefaultStmt *S);
274 Stmt *VisitLabelStmt(LabelStmt *S);
275 Stmt *VisitAttributedStmt(AttributedStmt *S);
276 Stmt *VisitIfStmt(IfStmt *S);
277 Stmt *VisitSwitchStmt(SwitchStmt *S);
278 Stmt *VisitWhileStmt(WhileStmt *S);
279 Stmt *VisitDoStmt(DoStmt *S);
280 Stmt *VisitForStmt(ForStmt *S);
281 Stmt *VisitGotoStmt(GotoStmt *S);
282 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
283 Stmt *VisitContinueStmt(ContinueStmt *S);
284 Stmt *VisitBreakStmt(BreakStmt *S);
285 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000286 // FIXME: MSAsmStmt
287 // FIXME: SEHExceptStmt
288 // FIXME: SEHFinallyStmt
289 // FIXME: SEHTryStmt
290 // FIXME: SEHLeaveStmt
291 // FIXME: CapturedStmt
292 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
293 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
294 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
295 // FIXME: MSDependentExistsStmt
296 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
297 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
298 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
299 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
300 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
301 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
302 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000303
304 // Importing expressions
305 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000306 Expr *VisitVAArgExpr(VAArgExpr *E);
307 Expr *VisitGNUNullExpr(GNUNullExpr *E);
308 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000309 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000310 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
311 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
312 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000313 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000314 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000315 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000316 Expr *VisitStringLiteral(StringLiteral *E);
317 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
318 Expr *VisitAtomicExpr(AtomicExpr *E);
319 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000320 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000321 Expr *VisitParenListExpr(ParenListExpr *E);
322 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000323 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000324 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000325 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000326 Expr *VisitConditionalOperator(ConditionalOperator *E);
327 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
328 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000329 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
330 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
331 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000332 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000333 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000334 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
335 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
336 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
337 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
338 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
339 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
340 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
341 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
342 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000343 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000344 Expr *VisitSizeOfPackExpr(SizeOfPackExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000345 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
346 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000347 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000348 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorin7f758b62017-12-27 17:04:42 +0000349 Expr *VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000350 Expr *VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +0000351 Expr *VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *CE);
352 Expr *VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
Peter Szecsice7f3182018-05-07 12:08:27 +0000353 Expr *VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000354 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000355 Expr *VisitCXXThisExpr(CXXThisExpr *E);
356 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +0000357 Expr *VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000358 Expr *VisitMemberExpr(MemberExpr *E);
359 Expr *VisitCallExpr(CallExpr *E);
Aleksei Sidorin8fc85102018-01-26 11:36:54 +0000360 Expr *VisitLambdaExpr(LambdaExpr *LE);
Sean Callanan8bca9962016-03-28 21:43:01 +0000361 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000362 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
363 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000364 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
365 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000366 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000367 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000368 Expr *VisitCXXTypeidExpr(CXXTypeidExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000369
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000370 template<typename IIter, typename OIter>
371 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000372 using ItemT = typename std::remove_reference<decltype(*Obegin)>::type;
373
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000374 ASTImporter &ImporterRef = Importer;
375 std::transform(Ibegin, Iend, Obegin,
376 [&ImporterRef](ItemT From) -> ItemT {
377 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000378 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000379 }
380
381 template<typename IIter, typename OIter>
382 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000383 using ItemT = typename std::remove_reference<decltype(**Obegin)>::type;
384
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000385 ASTImporter &ImporterRef = Importer;
386 bool Failed = false;
387 std::transform(Ibegin, Iend, Obegin,
388 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000389 auto *To = cast_or_null<ItemT>(ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000390 if (!To && From)
391 Failed = true;
392 return To;
393 });
394 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000395 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000396
397 template<typename InContainerTy, typename OutContainerTy>
398 bool ImportContainerChecked(const InContainerTy &InContainer,
399 OutContainerTy &OutContainer) {
400 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
401 OutContainer.begin());
402 }
403
404 template<typename InContainerTy, typename OIter>
405 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
406 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
407 }
Lang Hames19e07e12017-06-20 21:06:00 +0000408
409 // Importing overrides.
410 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000411 };
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000412
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000413template <typename InContainerTy>
414bool ASTNodeImporter::ImportTemplateArgumentListInfo(
415 SourceLocation FromLAngleLoc, SourceLocation FromRAngleLoc,
416 const InContainerTy &Container, TemplateArgumentListInfo &Result) {
417 TemplateArgumentListInfo ToTAInfo(Importer.Import(FromLAngleLoc),
418 Importer.Import(FromRAngleLoc));
419 if (ImportTemplateArgumentListInfo(Container, ToTAInfo))
420 return true;
421 Result = ToTAInfo;
422 return false;
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000423}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000424
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000425template <>
426bool ASTNodeImporter::ImportTemplateArgumentListInfo<TemplateArgumentListInfo>(
427 const TemplateArgumentListInfo &From, TemplateArgumentListInfo &Result) {
428 return ImportTemplateArgumentListInfo(
429 From.getLAngleLoc(), From.getRAngleLoc(), From.arguments(), Result);
430}
431
432template <>
433bool ASTNodeImporter::ImportTemplateArgumentListInfo<
434 ASTTemplateArgumentListInfo>(const ASTTemplateArgumentListInfo &From,
435 TemplateArgumentListInfo &Result) {
436 return ImportTemplateArgumentListInfo(From.LAngleLoc, From.RAngleLoc,
437 From.arguments(), Result);
438}
439
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000440} // namespace clang
Aleksei Sidorin782bfcf2018-02-14 11:39:33 +0000441
Douglas Gregor3996e242010-02-15 22:01:00 +0000442//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000443// Import Types
444//----------------------------------------------------------------------------
445
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000446using namespace clang;
447
John McCall424cec92011-01-19 06:33:43 +0000448QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000449 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
450 << T->getTypeClassName();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000451 return {};
Douglas Gregore4c83e42010-02-09 22:48:33 +0000452}
453
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000454QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
455 QualType UnderlyingType = Importer.Import(T->getValueType());
456 if(UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000457 return {};
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000458
459 return Importer.getToContext().getAtomicType(UnderlyingType);
460}
461
John McCall424cec92011-01-19 06:33:43 +0000462QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000463 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000464#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
465 case BuiltinType::Id: \
466 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000467#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000468#define SHARED_SINGLETON_TYPE(Expansion)
469#define BUILTIN_TYPE(Id, SingletonId) \
470 case BuiltinType::Id: return Importer.getToContext().SingletonId;
471#include "clang/AST/BuiltinTypes.def"
472
473 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
474 // context supports C++.
475
476 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
477 // context supports ObjC.
478
Douglas Gregor96e578d2010-02-05 17:54:41 +0000479 case BuiltinType::Char_U:
480 // The context we're importing from has an unsigned 'char'. If we're
481 // importing into a context with a signed 'char', translate to
482 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000483 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000484 return Importer.getToContext().UnsignedCharTy;
485
486 return Importer.getToContext().CharTy;
487
Douglas Gregor96e578d2010-02-05 17:54:41 +0000488 case BuiltinType::Char_S:
489 // The context we're importing from has an unsigned 'char'. If we're
490 // importing into a context with a signed 'char', translate to
491 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000492 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000493 return Importer.getToContext().SignedCharTy;
494
495 return Importer.getToContext().CharTy;
496
Chris Lattnerad3467e2010-12-25 23:25:43 +0000497 case BuiltinType::WChar_S:
498 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000499 // FIXME: If not in C++, shall we translate to the C equivalent of
500 // wchar_t?
501 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000502 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000503
504 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000505}
506
Aleksei Sidorina693b372016-09-28 10:16:56 +0000507QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
508 QualType OrigT = Importer.Import(T->getOriginalType());
509 if (OrigT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000510 return {};
Aleksei Sidorina693b372016-09-28 10:16:56 +0000511
512 return Importer.getToContext().getDecayedType(OrigT);
513}
514
John McCall424cec92011-01-19 06:33:43 +0000515QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000516 QualType ToElementType = Importer.Import(T->getElementType());
517 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000518 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000519
520 return Importer.getToContext().getComplexType(ToElementType);
521}
522
John McCall424cec92011-01-19 06:33:43 +0000523QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000524 QualType ToPointeeType = Importer.Import(T->getPointeeType());
525 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000526 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000527
528 return Importer.getToContext().getPointerType(ToPointeeType);
529}
530
John McCall424cec92011-01-19 06:33:43 +0000531QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000532 // FIXME: Check for blocks support in "to" context.
533 QualType ToPointeeType = Importer.Import(T->getPointeeType());
534 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000535 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000536
537 return Importer.getToContext().getBlockPointerType(ToPointeeType);
538}
539
John McCall424cec92011-01-19 06:33:43 +0000540QualType
541ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000542 // FIXME: Check for C++ support in "to" context.
543 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
544 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000545 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000546
547 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
548}
549
John McCall424cec92011-01-19 06:33:43 +0000550QualType
551ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000552 // FIXME: Check for C++0x support in "to" context.
553 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
554 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000555 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000556
557 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
558}
559
John McCall424cec92011-01-19 06:33:43 +0000560QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000561 // FIXME: Check for C++ support in "to" context.
562 QualType ToPointeeType = Importer.Import(T->getPointeeType());
563 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000564 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000565
566 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
567 return Importer.getToContext().getMemberPointerType(ToPointeeType,
568 ClassType.getTypePtr());
569}
570
John McCall424cec92011-01-19 06:33:43 +0000571QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000572 QualType ToElementType = Importer.Import(T->getElementType());
573 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000574 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000575
576 return Importer.getToContext().getConstantArrayType(ToElementType,
577 T->getSize(),
578 T->getSizeModifier(),
579 T->getIndexTypeCVRQualifiers());
580}
581
John McCall424cec92011-01-19 06:33:43 +0000582QualType
583ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000584 QualType ToElementType = Importer.Import(T->getElementType());
585 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000586 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000587
588 return Importer.getToContext().getIncompleteArrayType(ToElementType,
589 T->getSizeModifier(),
590 T->getIndexTypeCVRQualifiers());
591}
592
John McCall424cec92011-01-19 06:33:43 +0000593QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000594 QualType ToElementType = Importer.Import(T->getElementType());
595 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000596 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000597
598 Expr *Size = Importer.Import(T->getSizeExpr());
599 if (!Size)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000600 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000601
602 SourceRange Brackets = Importer.Import(T->getBracketsRange());
603 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
604 T->getSizeModifier(),
605 T->getIndexTypeCVRQualifiers(),
606 Brackets);
607}
608
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000609QualType ASTNodeImporter::VisitDependentSizedArrayType(
610 const DependentSizedArrayType *T) {
611 QualType ToElementType = Importer.Import(T->getElementType());
612 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000613 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000614
615 // SizeExpr may be null if size is not specified directly.
616 // For example, 'int a[]'.
617 Expr *Size = Importer.Import(T->getSizeExpr());
618 if (!Size && T->getSizeExpr())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000619 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000620
621 SourceRange Brackets = Importer.Import(T->getBracketsRange());
622 return Importer.getToContext().getDependentSizedArrayType(
623 ToElementType, Size, T->getSizeModifier(), T->getIndexTypeCVRQualifiers(),
624 Brackets);
625}
626
John McCall424cec92011-01-19 06:33:43 +0000627QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000628 QualType ToElementType = Importer.Import(T->getElementType());
629 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000630 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000631
632 return Importer.getToContext().getVectorType(ToElementType,
633 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000634 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000635}
636
John McCall424cec92011-01-19 06:33:43 +0000637QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000638 QualType ToElementType = Importer.Import(T->getElementType());
639 if (ToElementType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000640 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000641
642 return Importer.getToContext().getExtVectorType(ToElementType,
643 T->getNumElements());
644}
645
John McCall424cec92011-01-19 06:33:43 +0000646QualType
647ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000648 // FIXME: What happens if we're importing a function without a prototype
649 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000650 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000651 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000652 return {};
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000653
Douglas Gregor96e578d2010-02-05 17:54:41 +0000654 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000655 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000656}
657
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000658QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000659 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000660 if (ToResultType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000661 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000662
663 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000664 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000665 for (const auto &A : T->param_types()) {
666 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000667 if (ArgType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000668 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000669 ArgTypes.push_back(ArgType);
670 }
671
672 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000673 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000674 for (const auto &E : T->exceptions()) {
675 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000676 if (ExceptionType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000677 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000678 ExceptionTypes.push_back(ExceptionType);
679 }
John McCalldb40c7f2010-12-14 08:05:40 +0000680
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000681 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
682 FunctionProtoType::ExtProtoInfo ToEPI;
683
684 ToEPI.ExtInfo = FromEPI.ExtInfo;
685 ToEPI.Variadic = FromEPI.Variadic;
686 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
687 ToEPI.TypeQuals = FromEPI.TypeQuals;
688 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000689 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
690 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
691 ToEPI.ExceptionSpec.NoexceptExpr =
692 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
693 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
694 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
695 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
696 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000697
Jordan Rose5c382722013-03-08 21:51:21 +0000698 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000699}
700
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000701QualType ASTNodeImporter::VisitUnresolvedUsingType(
702 const UnresolvedUsingType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000703 const auto *ToD =
704 cast_or_null<UnresolvedUsingTypenameDecl>(Importer.Import(T->getDecl()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000705 if (!ToD)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000706 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000707
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000708 auto *ToPrevD =
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000709 cast_or_null<UnresolvedUsingTypenameDecl>(
710 Importer.Import(T->getDecl()->getPreviousDecl()));
711 if (!ToPrevD && T->getDecl()->getPreviousDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000712 return {};
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000713
714 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
715}
716
Sean Callananda6df8a2011-08-11 16:56:07 +0000717QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
718 QualType ToInnerType = Importer.Import(T->getInnerType());
719 if (ToInnerType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000720 return {};
Sean Callananda6df8a2011-08-11 16:56:07 +0000721
722 return Importer.getToContext().getParenType(ToInnerType);
723}
724
John McCall424cec92011-01-19 06:33:43 +0000725QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000726 auto *ToDecl =
727 dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000728 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000729 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000730
731 return Importer.getToContext().getTypeDeclType(ToDecl);
732}
733
John McCall424cec92011-01-19 06:33:43 +0000734QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000735 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
736 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000737 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000738
739 return Importer.getToContext().getTypeOfExprType(ToExpr);
740}
741
John McCall424cec92011-01-19 06:33:43 +0000742QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000743 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
744 if (ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000745 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000746
747 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
748}
749
John McCall424cec92011-01-19 06:33:43 +0000750QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000751 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000752 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
753 if (!ToExpr)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000754 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000755
Douglas Gregor81495f32012-02-12 18:42:33 +0000756 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
757 if (UnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000758 return {};
Douglas Gregor81495f32012-02-12 18:42:33 +0000759
760 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000761}
762
Alexis Hunte852b102011-05-24 22:41:36 +0000763QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
764 QualType ToBaseType = Importer.Import(T->getBaseType());
765 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
766 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000767 return {};
Alexis Hunte852b102011-05-24 22:41:36 +0000768
769 return Importer.getToContext().getUnaryTransformType(ToBaseType,
770 ToUnderlyingType,
771 T->getUTTKind());
772}
773
Richard Smith30482bc2011-02-20 03:19:35 +0000774QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000775 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000776 QualType FromDeduced = T->getDeducedType();
777 QualType ToDeduced;
778 if (!FromDeduced.isNull()) {
779 ToDeduced = Importer.Import(FromDeduced);
780 if (ToDeduced.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000781 return {};
Richard Smith30482bc2011-02-20 03:19:35 +0000782 }
783
Richard Smithe301ba22015-11-11 02:02:15 +0000784 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000785 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000786}
787
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000788QualType ASTNodeImporter::VisitInjectedClassNameType(
789 const InjectedClassNameType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000790 auto *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000791 if (!D)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000792 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000793
794 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
795 if (InjType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000796 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000797
798 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
799 // See comments in InjectedClassNameType definition for details
800 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
801 enum {
802 TypeAlignmentInBits = 4,
803 TypeAlignment = 1 << TypeAlignmentInBits
804 };
805
806 return QualType(new (Importer.getToContext(), TypeAlignment)
807 InjectedClassNameType(D, InjType), 0);
808}
809
John McCall424cec92011-01-19 06:33:43 +0000810QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000811 auto *ToDecl = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000812 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000813 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000814
815 return Importer.getToContext().getTagDeclType(ToDecl);
816}
817
John McCall424cec92011-01-19 06:33:43 +0000818QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000819 auto *ToDecl = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000820 if (!ToDecl)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000821 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000822
823 return Importer.getToContext().getTagDeclType(ToDecl);
824}
825
Sean Callanan72fe0852015-04-02 23:50:08 +0000826QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
827 QualType FromModifiedType = T->getModifiedType();
828 QualType FromEquivalentType = T->getEquivalentType();
829 QualType ToModifiedType;
830 QualType ToEquivalentType;
831
832 if (!FromModifiedType.isNull()) {
833 ToModifiedType = Importer.Import(FromModifiedType);
834 if (ToModifiedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000835 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000836 }
837 if (!FromEquivalentType.isNull()) {
838 ToEquivalentType = Importer.Import(FromEquivalentType);
839 if (ToEquivalentType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000840 return {};
Sean Callanan72fe0852015-04-02 23:50:08 +0000841 }
842
843 return Importer.getToContext().getAttributedType(T->getAttrKind(),
844 ToModifiedType, ToEquivalentType);
845}
846
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000847QualType ASTNodeImporter::VisitTemplateTypeParmType(
848 const TemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000849 auto *ParmDecl =
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000850 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
851 if (!ParmDecl && T->getDecl())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000852 return {};
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000853
854 return Importer.getToContext().getTemplateTypeParmType(
855 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
856}
857
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000858QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
859 const SubstTemplateTypeParmType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000860 const auto *Replaced =
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000861 cast_or_null<TemplateTypeParmType>(Importer.Import(
862 QualType(T->getReplacedParameter(), 0)).getTypePtr());
863 if (!Replaced)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000864 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000865
866 QualType Replacement = Importer.Import(T->getReplacementType());
867 if (Replacement.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000868 return {};
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000869 Replacement = Replacement.getCanonicalType();
870
871 return Importer.getToContext().getSubstTemplateTypeParmType(
872 Replaced, Replacement);
873}
874
Douglas Gregore2e50d332010-12-01 01:36:18 +0000875QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000876 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000877 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
878 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000879 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000880
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000881 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000882 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000883 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000884
885 QualType ToCanonType;
886 if (!QualType(T, 0).isCanonical()) {
887 QualType FromCanonType
888 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
889 ToCanonType =Importer.Import(FromCanonType);
890 if (ToCanonType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000891 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +0000892 }
893 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000894 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000895 ToCanonType);
896}
897
John McCall424cec92011-01-19 06:33:43 +0000898QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000899 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000900 // Note: the qualifier in an ElaboratedType is optional.
901 if (T->getQualifier()) {
902 ToQualifier = Importer.Import(T->getQualifier());
903 if (!ToQualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000904 return {};
Abramo Bagnara6150c882010-05-11 21:36:43 +0000905 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000906
907 QualType ToNamedType = Importer.Import(T->getNamedType());
908 if (ToNamedType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000909 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000910
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000911 TagDecl *OwnedTagDecl =
912 cast_or_null<TagDecl>(Importer.Import(T->getOwnedTagDecl()));
913 if (!OwnedTagDecl && T->getOwnedTagDecl())
914 return {};
915
Abramo Bagnara6150c882010-05-11 21:36:43 +0000916 return Importer.getToContext().getElaboratedType(T->getKeyword(),
Joel E. Denny7509a2f2018-05-14 19:36:45 +0000917 ToQualifier, ToNamedType,
918 OwnedTagDecl);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000919}
920
Gabor Horvath7a91c082017-11-14 11:30:38 +0000921QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
922 QualType Pattern = Importer.Import(T->getPattern());
923 if (Pattern.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000924 return {};
Gabor Horvath7a91c082017-11-14 11:30:38 +0000925
926 return Importer.getToContext().getPackExpansionType(Pattern,
927 T->getNumExpansions());
928}
929
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000930QualType ASTNodeImporter::VisitDependentTemplateSpecializationType(
931 const DependentTemplateSpecializationType *T) {
932 NestedNameSpecifier *Qualifier = Importer.Import(T->getQualifier());
933 if (!Qualifier && T->getQualifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000934 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000935
936 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
937 if (!Name && T->getIdentifier())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000938 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000939
940 SmallVector<TemplateArgument, 2> ToPack;
941 ToPack.reserve(T->getNumArgs());
942 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000943 return {};
Gabor Horvathc78d99a2018-01-27 16:11:45 +0000944
945 return Importer.getToContext().getDependentTemplateSpecializationType(
946 T->getKeyword(), Qualifier, Name, ToPack);
947}
948
Peter Szecsice7f3182018-05-07 12:08:27 +0000949QualType ASTNodeImporter::VisitDependentNameType(const DependentNameType *T) {
950 NestedNameSpecifier *NNS = Importer.Import(T->getQualifier());
951 if (!NNS && T->getQualifier())
952 return QualType();
953
954 IdentifierInfo *Name = Importer.Import(T->getIdentifier());
955 if (!Name && T->getIdentifier())
956 return QualType();
957
958 QualType Canon = (T == T->getCanonicalTypeInternal().getTypePtr())
959 ? QualType()
960 : Importer.Import(T->getCanonicalTypeInternal());
961 if (!Canon.isNull())
962 Canon = Canon.getCanonicalType();
963
964 return Importer.getToContext().getDependentNameType(T->getKeyword(), NNS,
965 Name, Canon);
966}
967
John McCall424cec92011-01-19 06:33:43 +0000968QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000969 auto *Class =
970 dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000971 if (!Class)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000972 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973
John McCall8b07ec22010-05-15 11:32:37 +0000974 return Importer.getToContext().getObjCInterfaceType(Class);
975}
976
John McCall424cec92011-01-19 06:33:43 +0000977QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000978 QualType ToBaseType = Importer.Import(T->getBaseType());
979 if (ToBaseType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000980 return {};
John McCall8b07ec22010-05-15 11:32:37 +0000981
Douglas Gregore9d95f12015-07-07 03:57:35 +0000982 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000983 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000984 QualType ImportedTypeArg = Importer.Import(TypeArg);
985 if (ImportedTypeArg.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000986 return {};
Douglas Gregore9d95f12015-07-07 03:57:35 +0000987
988 TypeArgs.push_back(ImportedTypeArg);
989 }
990
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000991 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000992 for (auto *P : T->quals()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000993 auto *Protocol = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000994 if (!Protocol)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +0000995 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +0000996 Protocols.push_back(Protocol);
997 }
998
Douglas Gregore9d95f12015-07-07 03:57:35 +0000999 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001000 Protocols,
1001 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001002}
1003
John McCall424cec92011-01-19 06:33:43 +00001004QualType
1005ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001006 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1007 if (ToPointeeType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001008 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00001009
John McCall8b07ec22010-05-15 11:32:37 +00001010 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001011}
1012
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001013//----------------------------------------------------------------------------
1014// Import Declarations
1015//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001016bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1017 DeclContext *&LexicalDC,
1018 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001019 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001020 SourceLocation &Loc) {
1021 // Import the context of this declaration.
1022 DC = Importer.ImportContext(D->getDeclContext());
1023 if (!DC)
1024 return true;
1025
1026 LexicalDC = DC;
1027 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1028 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1029 if (!LexicalDC)
1030 return true;
1031 }
1032
1033 // Import the name of this declaration.
1034 Name = Importer.Import(D->getDeclName());
1035 if (D->getDeclName() && !Name)
1036 return true;
1037
1038 // Import the location of this declaration.
1039 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001040 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001041 return false;
1042}
1043
Douglas Gregord451ea92011-07-29 23:31:30 +00001044void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1045 if (!FromD)
1046 return;
1047
1048 if (!ToD) {
1049 ToD = Importer.Import(FromD);
1050 if (!ToD)
1051 return;
1052 }
1053
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001054 if (auto *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1055 if (auto *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001056 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001057 ImportDefinition(FromRecord, ToRecord);
1058 }
1059 }
1060 return;
1061 }
1062
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001063 if (auto *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1064 if (auto *ToEnum = cast_or_null<EnumDecl>(ToD)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001065 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1066 ImportDefinition(FromEnum, ToEnum);
1067 }
1068 }
1069 return;
1070 }
1071}
1072
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001073void
1074ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1075 DeclarationNameInfo& To) {
1076 // NOTE: To.Name and To.Loc are already imported.
1077 // We only have to import To.LocInfo.
1078 switch (To.getName().getNameKind()) {
1079 case DeclarationName::Identifier:
1080 case DeclarationName::ObjCZeroArgSelector:
1081 case DeclarationName::ObjCOneArgSelector:
1082 case DeclarationName::ObjCMultiArgSelector:
1083 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00001084 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001085 return;
1086
1087 case DeclarationName::CXXOperatorName: {
1088 SourceRange Range = From.getCXXOperatorNameRange();
1089 To.setCXXOperatorNameRange(Importer.Import(Range));
1090 return;
1091 }
1092 case DeclarationName::CXXLiteralOperatorName: {
1093 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1094 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1095 return;
1096 }
1097 case DeclarationName::CXXConstructorName:
1098 case DeclarationName::CXXDestructorName:
1099 case DeclarationName::CXXConversionFunctionName: {
1100 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1101 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1102 return;
1103 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001104 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001105 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001106}
1107
Douglas Gregor2e15c842012-02-01 21:00:38 +00001108void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001109 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001110 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001111 return;
1112 }
1113
Aaron Ballman629afae2014-03-07 19:56:05 +00001114 for (auto *From : FromDC->decls())
1115 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001116}
1117
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001118static void setTypedefNameForAnonDecl(TagDecl *From, TagDecl *To,
1119 ASTImporter &Importer) {
1120 if (TypedefNameDecl *FromTypedef = From->getTypedefNameForAnonDecl()) {
1121 auto *ToTypedef =
1122 cast_or_null<TypedefNameDecl>(Importer.Import(FromTypedef));
1123 assert (ToTypedef && "Failed to import typedef of an anonymous structure");
1124
1125 To->setTypedefNameForAnonDecl(ToTypedef);
1126 }
1127}
1128
Douglas Gregord451ea92011-07-29 23:31:30 +00001129bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001130 ImportDefinitionKind Kind) {
1131 if (To->getDefinition() || To->isBeingDefined()) {
1132 if (Kind == IDK_Everything)
1133 ImportDeclContext(From, /*ForceImport=*/true);
1134
Douglas Gregore2e50d332010-12-01 01:36:18 +00001135 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001136 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001137
1138 To->startDefinition();
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001139
1140 setTypedefNameForAnonDecl(From, To, Importer);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001141
1142 // Add base classes.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001143 if (auto *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1144 auto *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001145
1146 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1147 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1148 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001149 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001150 ToData.Aggregate = FromData.Aggregate;
1151 ToData.PlainOldData = FromData.PlainOldData;
1152 ToData.Empty = FromData.Empty;
1153 ToData.Polymorphic = FromData.Polymorphic;
1154 ToData.Abstract = FromData.Abstract;
1155 ToData.IsStandardLayout = FromData.IsStandardLayout;
Richard Smithb6070db2018-04-05 18:55:37 +00001156 ToData.IsCXX11StandardLayout = FromData.IsCXX11StandardLayout;
1157 ToData.HasBasesWithFields = FromData.HasBasesWithFields;
1158 ToData.HasBasesWithNonStaticDataMembers =
1159 FromData.HasBasesWithNonStaticDataMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001160 ToData.HasPrivateFields = FromData.HasPrivateFields;
1161 ToData.HasProtectedFields = FromData.HasProtectedFields;
1162 ToData.HasPublicFields = FromData.HasPublicFields;
1163 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001164 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001165 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001166 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001167 ToData.HasUninitializedReferenceMember
1168 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00001169 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +00001170 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
1171 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +00001172 ToData.NeedOverloadResolutionForCopyConstructor
1173 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +00001174 ToData.NeedOverloadResolutionForMoveConstructor
1175 = FromData.NeedOverloadResolutionForMoveConstructor;
1176 ToData.NeedOverloadResolutionForMoveAssignment
1177 = FromData.NeedOverloadResolutionForMoveAssignment;
1178 ToData.NeedOverloadResolutionForDestructor
1179 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001180 ToData.DefaultedCopyConstructorIsDeleted
1181 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001182 ToData.DefaultedMoveConstructorIsDeleted
1183 = FromData.DefaultedMoveConstructorIsDeleted;
1184 ToData.DefaultedMoveAssignmentIsDeleted
1185 = FromData.DefaultedMoveAssignmentIsDeleted;
1186 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001187 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1188 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001189 ToData.HasConstexprNonCopyMoveConstructor
1190 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001191 ToData.HasDefaultedDefaultConstructor
1192 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001193 ToData.DefaultedDefaultConstructorIsConstexpr
1194 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001195 ToData.HasConstexprDefaultConstructor
1196 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001197 ToData.HasNonLiteralTypeFieldsOrBases
1198 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001199 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001200 ToData.UserProvidedDefaultConstructor
1201 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001202 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001203 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1204 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1205 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1206 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001207 ToData.ImplicitCopyAssignmentHasConstParam
1208 = FromData.ImplicitCopyAssignmentHasConstParam;
1209 ToData.HasDeclaredCopyConstructorWithConstParam
1210 = FromData.HasDeclaredCopyConstructorWithConstParam;
1211 ToData.HasDeclaredCopyAssignmentWithConstParam
1212 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001213
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001214 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001215 for (const auto &Base1 : FromCXX->bases()) {
1216 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001217 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001218 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001219
1220 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001221 if (Base1.isPackExpansion())
1222 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001223
1224 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001225 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001226
Douglas Gregore2e50d332010-12-01 01:36:18 +00001227 Bases.push_back(
1228 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001229 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1230 Base1.isVirtual(),
1231 Base1.isBaseOfClass(),
1232 Base1.getAccessSpecifierAsWritten(),
1233 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001234 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001235 }
1236 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001237 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001238 }
1239
Douglas Gregor2e15c842012-02-01 21:00:38 +00001240 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001241 ImportDeclContext(From, /*ForceImport=*/true);
1242
Douglas Gregore2e50d332010-12-01 01:36:18 +00001243 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001244 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001245}
1246
Larisse Voufo39a1e502013-08-06 01:03:05 +00001247bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1248 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001249 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001250 return false;
1251
1252 // FIXME: Can we really import any initializer? Alternatively, we could force
1253 // ourselves to import every declaration of a variable and then only use
1254 // getInit() here.
1255 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1256
1257 // FIXME: Other bits to merge?
1258
1259 return false;
1260}
1261
Douglas Gregord451ea92011-07-29 23:31:30 +00001262bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001263 ImportDefinitionKind Kind) {
1264 if (To->getDefinition() || To->isBeingDefined()) {
1265 if (Kind == IDK_Everything)
1266 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001267 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001268 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001269
1270 To->startDefinition();
1271
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001272 setTypedefNameForAnonDecl(From, To, Importer);
1273
Douglas Gregord451ea92011-07-29 23:31:30 +00001274 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1275 if (T.isNull())
1276 return true;
1277
1278 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1279 if (ToPromotionType.isNull())
1280 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001281
1282 if (shouldForceImportDeclContext(Kind))
1283 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001284
1285 // FIXME: we might need to merge the number of positive or negative bits
1286 // if the enumerator lists don't match.
1287 To->completeDefinition(T, ToPromotionType,
1288 From->getNumPositiveBits(),
1289 From->getNumNegativeBits());
1290 return false;
1291}
1292
Douglas Gregora082a492010-11-30 19:14:50 +00001293TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1294 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001295 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1296 if (ImportContainerChecked(*Params, ToParams))
1297 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001298
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001299 Expr *ToRequiresClause;
1300 if (Expr *const R = Params->getRequiresClause()) {
1301 ToRequiresClause = Importer.Import(R);
1302 if (!ToRequiresClause)
1303 return nullptr;
1304 } else {
1305 ToRequiresClause = nullptr;
1306 }
1307
Douglas Gregora082a492010-11-30 19:14:50 +00001308 return TemplateParameterList::Create(Importer.getToContext(),
1309 Importer.Import(Params->getTemplateLoc()),
1310 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001311 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001312 Importer.Import(Params->getRAngleLoc()),
1313 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001314}
1315
Douglas Gregore2e50d332010-12-01 01:36:18 +00001316TemplateArgument
1317ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1318 switch (From.getKind()) {
1319 case TemplateArgument::Null:
1320 return TemplateArgument();
1321
1322 case TemplateArgument::Type: {
1323 QualType ToType = Importer.Import(From.getAsType());
1324 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001325 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001326 return TemplateArgument(ToType);
1327 }
1328
1329 case TemplateArgument::Integral: {
1330 QualType ToType = Importer.Import(From.getIntegralType());
1331 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001332 return {};
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001333 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001334 }
1335
Eli Friedmanb826a002012-09-26 02:36:12 +00001336 case TemplateArgument::Declaration: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001337 auto *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001338 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1339 if (!To || ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001340 return {};
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001341 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001342 }
1343
1344 case TemplateArgument::NullPtr: {
1345 QualType ToType = Importer.Import(From.getNullPtrType());
1346 if (ToType.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001347 return {};
Eli Friedmanb826a002012-09-26 02:36:12 +00001348 return TemplateArgument(ToType, /*isNullPtr*/true);
1349 }
1350
Douglas Gregore2e50d332010-12-01 01:36:18 +00001351 case TemplateArgument::Template: {
1352 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1353 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001354 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00001355
1356 return TemplateArgument(ToTemplate);
1357 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001358
1359 case TemplateArgument::TemplateExpansion: {
1360 TemplateName ToTemplate
1361 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1362 if (ToTemplate.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001363 return {};
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001364
Douglas Gregore1d60df2011-01-14 23:41:42 +00001365 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001366 }
1367
Douglas Gregore2e50d332010-12-01 01:36:18 +00001368 case TemplateArgument::Expression:
1369 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1370 return TemplateArgument(ToExpr);
1371 return TemplateArgument();
1372
1373 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001374 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001375 ToPack.reserve(From.pack_size());
1376 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001377 return {};
Benjamin Kramercce63472015-08-05 09:40:22 +00001378
1379 return TemplateArgument(
1380 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001381 }
1382 }
1383
1384 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001385}
1386
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001387Optional<TemplateArgumentLoc>
1388ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001389 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1390 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1391 TemplateArgumentLocInfo ToInfo;
1392 if (Arg.getKind() == TemplateArgument::Expression) {
1393 Expr *E = Importer.Import(FromInfo.getAsExpr());
1394 ToInfo = TemplateArgumentLocInfo(E);
1395 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001396 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001397 } else if (Arg.getKind() == TemplateArgument::Type) {
1398 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1399 ToInfo = TemplateArgumentLocInfo(TSI);
1400 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001401 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001402 } else {
1403 ToInfo = TemplateArgumentLocInfo(
1404 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1405 Importer.Import(FromInfo.getTemplateNameLoc()),
1406 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1407 }
1408 return TemplateArgumentLoc(Arg, ToInfo);
1409}
1410
Douglas Gregore2e50d332010-12-01 01:36:18 +00001411bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1412 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001413 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001414 for (unsigned I = 0; I != NumFromArgs; ++I) {
1415 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1416 if (To.isNull() && !FromArgs[I].isNull())
1417 return true;
1418
1419 ToArgs.push_back(To);
1420 }
1421
1422 return false;
1423}
1424
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00001425// We cannot use Optional<> pattern here and below because
1426// TemplateArgumentListInfo's operator new is declared as deleted so it cannot
1427// be stored in Optional.
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001428template <typename InContainerTy>
1429bool ASTNodeImporter::ImportTemplateArgumentListInfo(
1430 const InContainerTy &Container, TemplateArgumentListInfo &ToTAInfo) {
1431 for (const auto &FromLoc : Container) {
1432 if (auto ToLoc = ImportTemplateArgumentLoc(FromLoc))
1433 ToTAInfo.addArgument(*ToLoc);
1434 else
1435 return true;
1436 }
1437 return false;
1438}
1439
Douglas Gregor5c73e912010-02-11 00:48:18 +00001440bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001441 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001442 // Eliminate a potential failure point where we attempt to re-import
1443 // something we're trying to import while completing ToRecord.
1444 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1445 if (ToOrigin) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001446 auto *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
Sean Callananc665c9e2013-10-09 21:45:11 +00001447 if (ToOriginRecord)
1448 ToRecord = ToOriginRecord;
1449 }
1450
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001451 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001452 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001453 Importer.getNonEquivalentDecls(),
1454 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001455 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001456}
1457
Larisse Voufo39a1e502013-08-06 01:03:05 +00001458bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1459 bool Complain) {
1460 StructuralEquivalenceContext Ctx(
1461 Importer.getFromContext(), Importer.getToContext(),
1462 Importer.getNonEquivalentDecls(), false, Complain);
1463 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1464}
1465
Douglas Gregor98c10182010-02-12 22:17:39 +00001466bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001467 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001468 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001469 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001470 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001471}
1472
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00001473bool ASTNodeImporter::IsStructuralMatch(FunctionTemplateDecl *From,
1474 FunctionTemplateDecl *To) {
1475 StructuralEquivalenceContext Ctx(
1476 Importer.getFromContext(), Importer.getToContext(),
1477 Importer.getNonEquivalentDecls(), false, false);
1478 return Ctx.IsStructurallyEquivalent(From, To);
1479}
1480
Douglas Gregor91155082012-11-14 22:29:20 +00001481bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001482 EnumConstantDecl *ToEC) {
Douglas Gregor91155082012-11-14 22:29:20 +00001483 const llvm::APSInt &FromVal = FromEC->getInitVal();
1484 const llvm::APSInt &ToVal = ToEC->getInitVal();
1485
1486 return FromVal.isSigned() == ToVal.isSigned() &&
1487 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1488 FromVal == ToVal;
1489}
1490
1491bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001492 ClassTemplateDecl *To) {
1493 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1494 Importer.getToContext(),
1495 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001496 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001497}
1498
Larisse Voufo39a1e502013-08-06 01:03:05 +00001499bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1500 VarTemplateDecl *To) {
1501 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1502 Importer.getToContext(),
1503 Importer.getNonEquivalentDecls());
1504 return Ctx.IsStructurallyEquivalent(From, To);
1505}
1506
Douglas Gregore4c83e42010-02-09 22:48:33 +00001507Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001508 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001509 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001510 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001511}
1512
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001513Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1514 // Import the context of this declaration.
1515 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1516 if (!DC)
1517 return nullptr;
1518
1519 DeclContext *LexicalDC = DC;
1520 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1521 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1522 if (!LexicalDC)
1523 return nullptr;
1524 }
1525
1526 // Import the location of this declaration.
1527 SourceLocation Loc = Importer.Import(D->getLocation());
1528
1529 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1530 ToD->setLexicalDeclContext(LexicalDC);
1531 Importer.Imported(D, ToD);
1532 LexicalDC->addDeclInternal(ToD);
1533 return ToD;
1534}
1535
Sean Callanan65198272011-11-17 23:20:56 +00001536Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1537 TranslationUnitDecl *ToD =
1538 Importer.getToContext().getTranslationUnitDecl();
1539
1540 Importer.Imported(D, ToD);
1541
1542 return ToD;
1543}
1544
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001545Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001546 SourceLocation Loc = Importer.Import(D->getLocation());
1547 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1548
1549 // Import the context of this declaration.
1550 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1551 if (!DC)
1552 return nullptr;
1553
1554 AccessSpecDecl *accessSpecDecl
1555 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1556 DC, Loc, ColonLoc);
1557
1558 if (!accessSpecDecl)
1559 return nullptr;
1560
1561 // Lexical DeclContext and Semantic DeclContext
1562 // is always the same for the accessSpec.
1563 accessSpecDecl->setLexicalDeclContext(DC);
1564 DC->addDeclInternal(accessSpecDecl);
1565
1566 return accessSpecDecl;
1567}
1568
Aleksei Sidorina693b372016-09-28 10:16:56 +00001569Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1570 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1571 if (!DC)
1572 return nullptr;
1573
1574 DeclContext *LexicalDC = DC;
1575
1576 // Import the location of this declaration.
1577 SourceLocation Loc = Importer.Import(D->getLocation());
1578
1579 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1580 if (!AssertExpr)
1581 return nullptr;
1582
1583 StringLiteral *FromMsg = D->getMessage();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001584 auto *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
Aleksei Sidorina693b372016-09-28 10:16:56 +00001585 if (!ToMsg && FromMsg)
1586 return nullptr;
1587
1588 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1589 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1590 Importer.Import(D->getRParenLoc()), D->isFailed());
1591
1592 ToD->setLexicalDeclContext(LexicalDC);
1593 LexicalDC->addDeclInternal(ToD);
1594 Importer.Imported(D, ToD);
1595 return ToD;
1596}
1597
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001598Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1599 // Import the major distinguishing characteristics of this namespace.
1600 DeclContext *DC, *LexicalDC;
1601 DeclarationName Name;
1602 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001603 NamedDecl *ToD;
1604 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001605 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001606 if (ToD)
1607 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001608
1609 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001610 if (!Name) {
1611 // This is an anonymous namespace. Adopt an existing anonymous
1612 // namespace if we can.
1613 // FIXME: Not testable.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001614 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001615 MergeWithNamespace = TU->getAnonymousNamespace();
1616 else
1617 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1618 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001619 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001620 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001621 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001622 for (auto *FoundDecl : FoundDecls) {
1623 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001624 continue;
1625
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001626 if (auto *FoundNS = dyn_cast<NamespaceDecl>(FoundDecl)) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001627 MergeWithNamespace = FoundNS;
1628 ConflictingDecls.clear();
1629 break;
1630 }
1631
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001632 ConflictingDecls.push_back(FoundDecl);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001633 }
1634
1635 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001636 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001637 ConflictingDecls.data(),
1638 ConflictingDecls.size());
1639 }
1640 }
1641
1642 // Create the "to" namespace, if needed.
1643 NamespaceDecl *ToNamespace = MergeWithNamespace;
1644 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001645 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001646 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001647 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001648 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001649 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001650 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001651 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001652
1653 // If this is an anonymous namespace, register it as the anonymous
1654 // namespace within its context.
1655 if (!Name) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001656 if (auto *TU = dyn_cast<TranslationUnitDecl>(DC))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001657 TU->setAnonymousNamespace(ToNamespace);
1658 else
1659 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1660 }
1661 }
1662 Importer.Imported(D, ToNamespace);
1663
1664 ImportDeclContext(D);
1665
1666 return ToNamespace;
1667}
1668
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001669Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1670 // Import the major distinguishing characteristics of this namespace.
1671 DeclContext *DC, *LexicalDC;
1672 DeclarationName Name;
1673 SourceLocation Loc;
1674 NamedDecl *LookupD;
1675 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1676 return nullptr;
1677 if (LookupD)
1678 return LookupD;
1679
1680 // NOTE: No conflict resolution is done for namespace aliases now.
1681
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001682 auto *TargetDecl = cast_or_null<NamespaceDecl>(
1683 Importer.Import(D->getNamespace()));
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001684 if (!TargetDecl)
1685 return nullptr;
1686
1687 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1688 if (!ToII)
1689 return nullptr;
1690
1691 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1692 if (D->getQualifierLoc() && !ToQLoc)
1693 return nullptr;
1694
1695 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1696 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1697 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1698 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1699
1700 ToD->setLexicalDeclContext(LexicalDC);
1701 Importer.Imported(D, ToD);
1702 LexicalDC->addDeclInternal(ToD);
1703
1704 return ToD;
1705}
1706
Richard Smithdda56e42011-04-15 14:24:37 +00001707Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001708 // Import the major distinguishing characteristics of this typedef.
1709 DeclContext *DC, *LexicalDC;
1710 DeclarationName Name;
1711 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001712 NamedDecl *ToD;
1713 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001714 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001715 if (ToD)
1716 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001717
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001718 // If this typedef is not in block scope, determine whether we've
1719 // seen a typedef with the same name (that we can merge with) or any
1720 // other entity by that name (which name lookup could conflict with).
1721 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001722 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001723 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001724 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001725 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001726 for (auto *FoundDecl : FoundDecls) {
1727 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001728 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001729 if (auto *FoundTypedef = dyn_cast<TypedefNameDecl>(FoundDecl)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001730 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1731 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001732 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001733 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001734
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001735 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001736 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001737
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001738 if (!ConflictingDecls.empty()) {
1739 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1740 ConflictingDecls.data(),
1741 ConflictingDecls.size());
1742 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001743 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001744 }
1745 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001746
Douglas Gregorb4964f72010-02-15 23:54:17 +00001747 // Import the underlying type of this typedef;
1748 QualType T = Importer.Import(D->getUnderlyingType());
1749 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001750 return nullptr;
1751
Aleksei Sidorin04fbffc2018-04-24 10:11:53 +00001752 // Some nodes (like anonymous tags referred by typedefs) are allowed to
1753 // import their enclosing typedef directly. Check if this is the case.
1754 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
1755 return AlreadyImported;
1756
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001757 // Create the new typedef node.
1758 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001759 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001760 TypedefNameDecl *ToTypedef;
1761 if (IsAlias)
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001762 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC, StartL, Loc,
1763 Name.getAsIdentifierInfo(), TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001764 else
Richard Smithdda56e42011-04-15 14:24:37 +00001765 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1766 StartL, Loc,
1767 Name.getAsIdentifierInfo(),
1768 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001769
Douglas Gregordd483172010-02-22 17:42:47 +00001770 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001771 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001772 Importer.Imported(D, ToTypedef);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001773
1774 // Templated declarations should not appear in DeclContext.
1775 TypeAliasDecl *FromAlias = IsAlias ? cast<TypeAliasDecl>(D) : nullptr;
1776 if (!FromAlias || !FromAlias->getDescribedAliasTemplate())
1777 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001778
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001779 return ToTypedef;
1780}
1781
Richard Smithdda56e42011-04-15 14:24:37 +00001782Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1783 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1784}
1785
1786Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1787 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1788}
1789
Gabor Horvath7a91c082017-11-14 11:30:38 +00001790Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1791 // Import the major distinguishing characteristics of this typedef.
1792 DeclContext *DC, *LexicalDC;
1793 DeclarationName Name;
1794 SourceLocation Loc;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001795 NamedDecl *FoundD;
1796 if (ImportDeclParts(D, DC, LexicalDC, Name, FoundD, Loc))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001797 return nullptr;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001798 if (FoundD)
1799 return FoundD;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001800
1801 // If this typedef is not in block scope, determine whether we've
1802 // seen a typedef with the same name (that we can merge with) or any
1803 // other entity by that name (which name lookup could conflict with).
1804 if (!DC->isFunctionOrMethod()) {
1805 SmallVector<NamedDecl *, 4> ConflictingDecls;
1806 unsigned IDNS = Decl::IDNS_Ordinary;
1807 SmallVector<NamedDecl *, 2> FoundDecls;
1808 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001809 for (auto *FoundDecl : FoundDecls) {
1810 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001811 continue;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001812 if (auto *FoundAlias = dyn_cast<TypeAliasTemplateDecl>(FoundDecl))
Gabor Horvath7a91c082017-11-14 11:30:38 +00001813 return Importer.Imported(D, FoundAlias);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001814 ConflictingDecls.push_back(FoundDecl);
Gabor Horvath7a91c082017-11-14 11:30:38 +00001815 }
1816
1817 if (!ConflictingDecls.empty()) {
1818 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1819 ConflictingDecls.data(),
1820 ConflictingDecls.size());
1821 if (!Name)
1822 return nullptr;
1823 }
1824 }
1825
1826 TemplateParameterList *Params = ImportTemplateParameterList(
1827 D->getTemplateParameters());
1828 if (!Params)
1829 return nullptr;
1830
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001831 auto *TemplDecl = cast_or_null<TypeAliasDecl>(
Gabor Horvath7a91c082017-11-14 11:30:38 +00001832 Importer.Import(D->getTemplatedDecl()));
1833 if (!TemplDecl)
1834 return nullptr;
1835
1836 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1837 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1838
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001839 TemplDecl->setDescribedAliasTemplate(ToAlias);
1840
Gabor Horvath7a91c082017-11-14 11:30:38 +00001841 ToAlias->setAccess(D->getAccess());
1842 ToAlias->setLexicalDeclContext(LexicalDC);
1843 Importer.Imported(D, ToAlias);
1844 LexicalDC->addDeclInternal(ToAlias);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00001845 return ToAlias;
Gabor Horvath7a91c082017-11-14 11:30:38 +00001846}
1847
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001848Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1849 // Import the major distinguishing characteristics of this label.
1850 DeclContext *DC, *LexicalDC;
1851 DeclarationName Name;
1852 SourceLocation Loc;
1853 NamedDecl *ToD;
1854 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1855 return nullptr;
1856 if (ToD)
1857 return ToD;
1858
1859 assert(LexicalDC->isFunctionOrMethod());
1860
1861 LabelDecl *ToLabel = D->isGnuLocal()
1862 ? LabelDecl::Create(Importer.getToContext(),
1863 DC, Importer.Import(D->getLocation()),
1864 Name.getAsIdentifierInfo(),
1865 Importer.Import(D->getLocStart()))
1866 : LabelDecl::Create(Importer.getToContext(),
1867 DC, Importer.Import(D->getLocation()),
1868 Name.getAsIdentifierInfo());
1869 Importer.Imported(D, ToLabel);
1870
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001871 auto *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001872 if (!Label)
1873 return nullptr;
1874
1875 ToLabel->setStmt(Label);
1876 ToLabel->setLexicalDeclContext(LexicalDC);
1877 LexicalDC->addDeclInternal(ToLabel);
1878 return ToLabel;
1879}
1880
Douglas Gregor98c10182010-02-12 22:17:39 +00001881Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1882 // Import the major distinguishing characteristics of this enum.
1883 DeclContext *DC, *LexicalDC;
1884 DeclarationName Name;
1885 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001886 NamedDecl *ToD;
1887 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001888 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001889 if (ToD)
1890 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001891
Douglas Gregor98c10182010-02-12 22:17:39 +00001892 // Figure out what enum name we're looking for.
1893 unsigned IDNS = Decl::IDNS_Tag;
1894 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001895 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1896 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001897 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001898 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001899 IDNS |= Decl::IDNS_Ordinary;
1900
1901 // We may already have an enum of the same name; try to find and match it.
1902 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001903 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001904 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001905 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001906 for (auto *FoundDecl : FoundDecls) {
1907 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001908 continue;
1909
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001910 Decl *Found = FoundDecl;
1911 if (auto *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
1912 if (const auto *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
Douglas Gregor98c10182010-02-12 22:17:39 +00001913 Found = Tag->getDecl();
1914 }
1915
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001916 if (auto *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001917 if (IsStructuralMatch(D, FoundEnum))
1918 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001919 }
1920
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00001921 ConflictingDecls.push_back(FoundDecl);
Douglas Gregor98c10182010-02-12 22:17:39 +00001922 }
1923
1924 if (!ConflictingDecls.empty()) {
1925 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1926 ConflictingDecls.data(),
1927 ConflictingDecls.size());
1928 }
1929 }
1930
1931 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001932 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1933 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001934 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001935 D->isScoped(), D->isScopedUsingClassTag(),
1936 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001937 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001938 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001939 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001940 D2->setLexicalDeclContext(LexicalDC);
1941 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001942 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001943
1944 // Import the integer type.
1945 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1946 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001947 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001948 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001949
1950 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001951 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001952 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001953
Douglas Gregor3996e242010-02-15 22:01:00 +00001954 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001955}
1956
Douglas Gregor5c73e912010-02-11 00:48:18 +00001957Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1958 // If this record has a definition in the translation unit we're coming from,
1959 // but this particular declaration is not that definition, import the
1960 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001961 TagDecl *Definition = D->getDefinition();
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
Douglas Gregore2e50d332010-12-01 01:36:18 +00004323 // Add the specialization to this context.
4324 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004325 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004326 }
4327 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00004328 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004329 return nullptr;
4330
Douglas Gregore2e50d332010-12-01 01:36:18 +00004331 return D2;
4332}
4333
Larisse Voufo39a1e502013-08-06 01:03:05 +00004334Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4335 // If this variable has a definition in the translation unit we're coming
4336 // from,
4337 // but this particular declaration is not that definition, import the
4338 // definition and map to that.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004339 auto *Definition =
Larisse Voufo39a1e502013-08-06 01:03:05 +00004340 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4341 if (Definition && Definition != D->getTemplatedDecl()) {
4342 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4343 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004344 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004345
4346 return Importer.Imported(D, ImportedDef);
4347 }
4348
4349 // Import the major distinguishing characteristics of this variable template.
4350 DeclContext *DC, *LexicalDC;
4351 DeclarationName Name;
4352 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004353 NamedDecl *ToD;
4354 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004355 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004356 if (ToD)
4357 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004358
4359 // We may already have a template of the same name; try to find and match it.
4360 assert(!DC->isFunctionOrMethod() &&
4361 "Variable templates cannot be declared at function scope");
4362 SmallVector<NamedDecl *, 4> ConflictingDecls;
4363 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004364 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004365 for (auto *FoundDecl : FoundDecls) {
4366 if (!FoundDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Larisse Voufo39a1e502013-08-06 01:03:05 +00004367 continue;
4368
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004369 Decl *Found = FoundDecl;
4370 if (auto *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004371 if (IsStructuralMatch(D, FoundTemplate)) {
4372 // The variable templates structurally match; call it the same template.
4373 Importer.Imported(D->getTemplatedDecl(),
4374 FoundTemplate->getTemplatedDecl());
4375 return Importer.Imported(D, FoundTemplate);
4376 }
4377 }
4378
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004379 ConflictingDecls.push_back(FoundDecl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004380 }
4381
4382 if (!ConflictingDecls.empty()) {
4383 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4384 ConflictingDecls.data(),
4385 ConflictingDecls.size());
4386 }
4387
4388 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004389 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004390
4391 VarDecl *DTemplated = D->getTemplatedDecl();
4392
4393 // Import the type.
4394 QualType T = Importer.Import(DTemplated->getType());
4395 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004396 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004397
4398 // Create the declaration that is being templated.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004399 auto *ToTemplated = dyn_cast_or_null<VarDecl>(Importer.Import(DTemplated));
4400 if (!ToTemplated)
Craig Topper36250ad2014-05-12 05:36:57 +00004401 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004402
4403 // Create the variable template declaration itself.
4404 TemplateParameterList *TemplateParams =
4405 ImportTemplateParameterList(D->getTemplateParameters());
4406 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004407 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004408
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004409 VarTemplateDecl *ToVarTD = VarTemplateDecl::Create(
4410 Importer.getToContext(), DC, Loc, Name, TemplateParams, ToTemplated);
4411 ToTemplated->setDescribedVarTemplate(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004412
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004413 ToVarTD->setAccess(D->getAccess());
4414 ToVarTD->setLexicalDeclContext(LexicalDC);
4415 LexicalDC->addDeclInternal(ToVarTD);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004416
4417 // Note the relationship between the variable templates.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004418 Importer.Imported(D, ToVarTD);
4419 Importer.Imported(DTemplated, ToTemplated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004420
4421 if (DTemplated->isThisDeclarationADefinition() &&
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004422 !ToTemplated->isThisDeclarationADefinition()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004423 // FIXME: Import definition!
4424 }
4425
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004426 return ToVarTD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004427}
4428
4429Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4430 VarTemplateSpecializationDecl *D) {
4431 // If this record has a definition in the translation unit we're coming from,
4432 // but this particular declaration is not that definition, import the
4433 // definition and map to that.
4434 VarDecl *Definition = D->getDefinition();
4435 if (Definition && Definition != D) {
4436 Decl *ImportedDef = Importer.Import(Definition);
4437 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004438 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004439
4440 return Importer.Imported(D, ImportedDef);
4441 }
4442
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004443 auto *VarTemplate = cast_or_null<VarTemplateDecl>(
Larisse Voufo39a1e502013-08-06 01:03:05 +00004444 Importer.Import(D->getSpecializedTemplate()));
4445 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004446 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004447
4448 // Import the context of this declaration.
4449 DeclContext *DC = VarTemplate->getDeclContext();
4450 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004451 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004452
4453 DeclContext *LexicalDC = DC;
4454 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4455 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4456 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004457 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004458 }
4459
4460 // Import the location of this declaration.
4461 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4462 SourceLocation IdLoc = Importer.Import(D->getLocation());
4463
4464 // Import template arguments.
4465 SmallVector<TemplateArgument, 2> TemplateArgs;
4466 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4467 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004468 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004469
4470 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004471 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004472 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004473 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004474 if (D2) {
4475 // We already have a variable template specialization with these template
4476 // arguments.
4477
4478 // FIXME: Check for specialization vs. instantiation errors.
4479
4480 if (VarDecl *FoundDef = D2->getDefinition()) {
4481 if (!D->isThisDeclarationADefinition() ||
4482 IsStructuralMatch(D, FoundDef)) {
4483 // The record types structurally match, or the "from" translation
4484 // unit only had a forward declaration anyway; call it the same
4485 // variable.
4486 return Importer.Imported(D, FoundDef);
4487 }
4488 }
4489 } else {
Larisse Voufo39a1e502013-08-06 01:03:05 +00004490 // Import the type.
4491 QualType T = Importer.Import(D->getType());
4492 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004493 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004494
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004495 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4496 if (D->getTypeSourceInfo() && !TInfo)
4497 return nullptr;
4498
4499 TemplateArgumentListInfo ToTAInfo;
4500 if (ImportTemplateArgumentListInfo(D->getTemplateArgsInfo(), ToTAInfo))
4501 return nullptr;
4502
4503 using PartVarSpecDecl = VarTemplatePartialSpecializationDecl;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004504 // Create a new specialization.
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004505 if (auto *FromPartial = dyn_cast<PartVarSpecDecl>(D)) {
4506 // Import TemplateArgumentListInfo
4507 TemplateArgumentListInfo ArgInfos;
4508 const auto *FromTAArgsAsWritten = FromPartial->getTemplateArgsAsWritten();
4509 // NOTE: FromTAArgsAsWritten and template parameter list are non-null.
4510 if (ImportTemplateArgumentListInfo(*FromTAArgsAsWritten, ArgInfos))
4511 return nullptr;
4512
4513 TemplateParameterList *ToTPList = ImportTemplateParameterList(
4514 FromPartial->getTemplateParameters());
4515 if (!ToTPList)
4516 return nullptr;
4517
4518 auto *ToPartial = PartVarSpecDecl::Create(
4519 Importer.getToContext(), DC, StartLoc, IdLoc, ToTPList, VarTemplate,
4520 T, TInfo, D->getStorageClass(), TemplateArgs, ArgInfos);
4521
4522 auto *FromInst = FromPartial->getInstantiatedFromMember();
4523 auto *ToInst = cast_or_null<PartVarSpecDecl>(Importer.Import(FromInst));
4524 if (FromInst && !ToInst)
4525 return nullptr;
4526
4527 ToPartial->setInstantiatedFromMember(ToInst);
4528 if (FromPartial->isMemberSpecialization())
4529 ToPartial->setMemberSpecialization();
4530
4531 D2 = ToPartial;
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004532 } else { // Full specialization
4533 D2 = VarTemplateSpecializationDecl::Create(
4534 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4535 D->getStorageClass(), TemplateArgs);
4536 }
4537
4538 SourceLocation POI = D->getPointOfInstantiation();
4539 if (POI.isValid())
4540 D2->setPointOfInstantiation(Importer.Import(POI));
4541
Larisse Voufo39a1e502013-08-06 01:03:05 +00004542 D2->setSpecializationKind(D->getSpecializationKind());
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004543 D2->setTemplateArgsInfo(ToTAInfo);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004544
4545 // Add this specialization to the class template.
4546 VarTemplate->AddSpecialization(D2, InsertPos);
4547
4548 // Import the qualifier, if any.
4549 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4550
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004551 if (D->isConstexpr())
4552 D2->setConstexpr(true);
4553
Larisse Voufo39a1e502013-08-06 01:03:05 +00004554 // Add the specialization to this context.
4555 D2->setLexicalDeclContext(LexicalDC);
4556 LexicalDC->addDeclInternal(D2);
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004557
4558 D2->setAccess(D->getAccess());
Larisse Voufo39a1e502013-08-06 01:03:05 +00004559 }
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004560
Larisse Voufo39a1e502013-08-06 01:03:05 +00004561 Importer.Imported(D, D2);
4562
Aleksei Sidorin4c05f142018-02-14 11:18:00 +00004563 // NOTE: isThisDeclarationADefinition() can return DeclarationOnly even if
4564 // declaration has initializer. Should this be fixed in the AST?.. Anyway,
4565 // we have to check the declaration for initializer - otherwise, it won't be
4566 // imported.
4567 if ((D->isThisDeclarationADefinition() || D->hasInit()) &&
4568 ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004569 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004570
4571 return D2;
4572}
4573
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004574Decl *ASTNodeImporter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
4575 DeclContext *DC, *LexicalDC;
4576 DeclarationName Name;
4577 SourceLocation Loc;
4578 NamedDecl *ToD;
4579
4580 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
4581 return nullptr;
4582
4583 if (ToD)
4584 return ToD;
4585
4586 // Try to find a function in our own ("to") context with the same name, same
4587 // type, and in the same context as the function we're importing.
4588 if (!LexicalDC->isFunctionOrMethod()) {
4589 unsigned IDNS = Decl::IDNS_Ordinary;
4590 SmallVector<NamedDecl *, 2> FoundDecls;
4591 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004592 for (auto *FoundDecl : FoundDecls) {
4593 if (!FoundDecl->isInIdentifierNamespace(IDNS))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004594 continue;
4595
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004596 if (auto *FoundFunction = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004597 if (FoundFunction->hasExternalFormalLinkage() &&
4598 D->hasExternalFormalLinkage()) {
4599 if (IsStructuralMatch(D, FoundFunction)) {
4600 Importer.Imported(D, FoundFunction);
4601 // FIXME: Actually try to merge the body and other attributes.
4602 return FoundFunction;
4603 }
4604 }
4605 }
4606 }
4607 }
4608
4609 TemplateParameterList *Params =
4610 ImportTemplateParameterList(D->getTemplateParameters());
4611 if (!Params)
4612 return nullptr;
4613
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004614 auto *TemplatedFD =
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00004615 cast_or_null<FunctionDecl>(Importer.Import(D->getTemplatedDecl()));
4616 if (!TemplatedFD)
4617 return nullptr;
4618
4619 FunctionTemplateDecl *ToFunc = FunctionTemplateDecl::Create(
4620 Importer.getToContext(), DC, Loc, Name, Params, TemplatedFD);
4621
4622 TemplatedFD->setDescribedFunctionTemplate(ToFunc);
4623 ToFunc->setAccess(D->getAccess());
4624 ToFunc->setLexicalDeclContext(LexicalDC);
4625 Importer.Imported(D, ToFunc);
4626
4627 LexicalDC->addDeclInternal(ToFunc);
4628 return ToFunc;
4629}
4630
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004631//----------------------------------------------------------------------------
4632// Import Statements
4633//----------------------------------------------------------------------------
4634
Sean Callanan59721b32015-04-28 18:41:46 +00004635DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4636 if (DG.isNull())
4637 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4638 size_t NumDecls = DG.end() - DG.begin();
4639 SmallVector<Decl *, 1> ToDecls(NumDecls);
4640 auto &_Importer = this->Importer;
4641 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4642 [&_Importer](Decl *D) -> Decl * {
4643 return _Importer.Import(D);
4644 });
4645 return DeclGroupRef::Create(Importer.getToContext(),
4646 ToDecls.begin(),
4647 NumDecls);
4648}
4649
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004650Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4651 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4652 << S->getStmtClassName();
4653 return nullptr;
4654}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004655
4656Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4657 SmallVector<IdentifierInfo *, 4> Names;
4658 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4659 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004660 // ToII is nullptr when no symbolic name is given for output operand
4661 // see ParseStmtAsm::ParseAsmOperandsOpt
4662 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004663 return nullptr;
4664 Names.push_back(ToII);
4665 }
4666 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4667 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004668 // ToII is nullptr when no symbolic name is given for input operand
4669 // see ParseStmtAsm::ParseAsmOperandsOpt
4670 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004671 return nullptr;
4672 Names.push_back(ToII);
4673 }
4674
4675 SmallVector<StringLiteral *, 4> Clobbers;
4676 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004677 auto *Clobber = cast_or_null<StringLiteral>(
4678 Importer.Import(S->getClobberStringLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004679 if (!Clobber)
4680 return nullptr;
4681 Clobbers.push_back(Clobber);
4682 }
4683
4684 SmallVector<StringLiteral *, 4> Constraints;
4685 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004686 auto *Output = cast_or_null<StringLiteral>(
4687 Importer.Import(S->getOutputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004688 if (!Output)
4689 return nullptr;
4690 Constraints.push_back(Output);
4691 }
4692
4693 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004694 auto *Input = cast_or_null<StringLiteral>(
4695 Importer.Import(S->getInputConstraintLiteral(I)));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004696 if (!Input)
4697 return nullptr;
4698 Constraints.push_back(Input);
4699 }
4700
4701 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004702 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004703 return nullptr;
4704
Aleksei Sidorina693b372016-09-28 10:16:56 +00004705 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004706 return nullptr;
4707
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004708 auto *AsmStr = cast_or_null<StringLiteral>(
4709 Importer.Import(S->getAsmString()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004710 if (!AsmStr)
4711 return nullptr;
4712
4713 return new (Importer.getToContext()) GCCAsmStmt(
4714 Importer.getToContext(),
4715 Importer.Import(S->getAsmLoc()),
4716 S->isSimple(),
4717 S->isVolatile(),
4718 S->getNumOutputs(),
4719 S->getNumInputs(),
4720 Names.data(),
4721 Constraints.data(),
4722 Exprs.data(),
4723 AsmStr,
4724 S->getNumClobbers(),
4725 Clobbers.data(),
4726 Importer.Import(S->getRParenLoc()));
4727}
4728
Sean Callanan59721b32015-04-28 18:41:46 +00004729Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4730 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004731 for (auto *ToD : ToDG) {
Sean Callanan59721b32015-04-28 18:41:46 +00004732 if (!ToD)
4733 return nullptr;
4734 }
4735 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4736 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4737 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4738}
4739
4740Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4741 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4742 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4743 S->hasLeadingEmptyMacro());
4744}
4745
4746Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004747 SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004748
4749 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004750 return nullptr;
4751
Sean Callanan59721b32015-04-28 18:41:46 +00004752 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4753 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
Benjamin Kramer07420902017-12-24 16:24:20 +00004754 return CompoundStmt::Create(Importer.getToContext(), ToStmts, ToLBraceLoc,
4755 ToRBraceLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004756}
4757
4758Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4759 Expr *ToLHS = Importer.Import(S->getLHS());
4760 if (!ToLHS)
4761 return nullptr;
4762 Expr *ToRHS = Importer.Import(S->getRHS());
4763 if (!ToRHS && S->getRHS())
4764 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004765 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4766 if (!ToSubStmt && S->getSubStmt())
4767 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004768 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4769 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4770 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004771 auto *ToStmt = new (Importer.getToContext())
Gabor Horvath480892b2017-10-18 09:25:18 +00004772 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4773 ToStmt->setSubStmt(ToSubStmt);
4774 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004775}
4776
4777Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4778 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4779 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4780 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4781 if (!ToSubStmt && S->getSubStmt())
4782 return nullptr;
4783 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4784 ToSubStmt);
4785}
4786
4787Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4788 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004789 auto *ToLabelDecl = cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00004790 if (!ToLabelDecl && S->getDecl())
4791 return nullptr;
4792 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4793 if (!ToSubStmt && S->getSubStmt())
4794 return nullptr;
4795 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4796 ToSubStmt);
4797}
4798
4799Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4800 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4801 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4802 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00004803 if (ImportContainerChecked(FromAttrs, ToAttrs))
4804 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004805 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4806 if (!ToSubStmt && S->getSubStmt())
4807 return nullptr;
4808 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4809 ToAttrs, ToSubStmt);
4810}
4811
4812Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4813 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004814 Stmt *ToInit = Importer.Import(S->getInit());
4815 if (!ToInit && S->getInit())
4816 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004817 VarDecl *ToConditionVariable = nullptr;
4818 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4819 ToConditionVariable =
4820 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4821 if (!ToConditionVariable)
4822 return nullptr;
4823 }
4824 Expr *ToCondition = Importer.Import(S->getCond());
4825 if (!ToCondition && S->getCond())
4826 return nullptr;
4827 Stmt *ToThenStmt = Importer.Import(S->getThen());
4828 if (!ToThenStmt && S->getThen())
4829 return nullptr;
4830 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4831 Stmt *ToElseStmt = Importer.Import(S->getElse());
4832 if (!ToElseStmt && S->getElse())
4833 return nullptr;
4834 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004835 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004836 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004837 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004838 ToCondition, ToThenStmt,
4839 ToElseLoc, ToElseStmt);
4840}
4841
4842Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004843 Stmt *ToInit = Importer.Import(S->getInit());
4844 if (!ToInit && S->getInit())
4845 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004846 VarDecl *ToConditionVariable = nullptr;
4847 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4848 ToConditionVariable =
4849 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4850 if (!ToConditionVariable)
4851 return nullptr;
4852 }
4853 Expr *ToCondition = Importer.Import(S->getCond());
4854 if (!ToCondition && S->getCond())
4855 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004856 auto *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004857 Importer.getToContext(), ToInit,
4858 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004859 Stmt *ToBody = Importer.Import(S->getBody());
4860 if (!ToBody && S->getBody())
4861 return nullptr;
4862 ToStmt->setBody(ToBody);
4863 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4864 // Now we have to re-chain the cases.
4865 SwitchCase *LastChainedSwitchCase = nullptr;
4866 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4867 SC = SC->getNextSwitchCase()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004868 auto *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
Sean Callanan59721b32015-04-28 18:41:46 +00004869 if (!ToSC)
4870 return nullptr;
4871 if (LastChainedSwitchCase)
4872 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4873 else
4874 ToStmt->setSwitchCaseList(ToSC);
4875 LastChainedSwitchCase = ToSC;
4876 }
4877 return ToStmt;
4878}
4879
4880Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4881 VarDecl *ToConditionVariable = nullptr;
4882 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4883 ToConditionVariable =
4884 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4885 if (!ToConditionVariable)
4886 return nullptr;
4887 }
4888 Expr *ToCondition = Importer.Import(S->getCond());
4889 if (!ToCondition && S->getCond())
4890 return nullptr;
4891 Stmt *ToBody = Importer.Import(S->getBody());
4892 if (!ToBody && S->getBody())
4893 return nullptr;
4894 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4895 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4896 ToConditionVariable,
4897 ToCondition, ToBody,
4898 ToWhileLoc);
4899}
4900
4901Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4902 Stmt *ToBody = Importer.Import(S->getBody());
4903 if (!ToBody && S->getBody())
4904 return nullptr;
4905 Expr *ToCondition = Importer.Import(S->getCond());
4906 if (!ToCondition && S->getCond())
4907 return nullptr;
4908 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4909 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4910 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4911 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4912 ToDoLoc, ToWhileLoc,
4913 ToRParenLoc);
4914}
4915
4916Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4917 Stmt *ToInit = Importer.Import(S->getInit());
4918 if (!ToInit && S->getInit())
4919 return nullptr;
4920 Expr *ToCondition = Importer.Import(S->getCond());
4921 if (!ToCondition && S->getCond())
4922 return nullptr;
4923 VarDecl *ToConditionVariable = nullptr;
4924 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4925 ToConditionVariable =
4926 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4927 if (!ToConditionVariable)
4928 return nullptr;
4929 }
4930 Expr *ToInc = Importer.Import(S->getInc());
4931 if (!ToInc && S->getInc())
4932 return nullptr;
4933 Stmt *ToBody = Importer.Import(S->getBody());
4934 if (!ToBody && S->getBody())
4935 return nullptr;
4936 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4937 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4938 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4939 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4940 ToInit, ToCondition,
4941 ToConditionVariable,
4942 ToInc, ToBody,
4943 ToForLoc, ToLParenLoc,
4944 ToRParenLoc);
4945}
4946
4947Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4948 LabelDecl *ToLabel = nullptr;
4949 if (LabelDecl *FromLabel = S->getLabel()) {
4950 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4951 if (!ToLabel)
4952 return nullptr;
4953 }
4954 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4955 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4956 return new (Importer.getToContext()) GotoStmt(ToLabel,
4957 ToGotoLoc, ToLabelLoc);
4958}
4959
4960Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4961 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4962 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4963 Expr *ToTarget = Importer.Import(S->getTarget());
4964 if (!ToTarget && S->getTarget())
4965 return nullptr;
4966 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4967 ToTarget);
4968}
4969
4970Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4971 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4972 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4973}
4974
4975Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4976 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4977 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4978}
4979
4980Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4981 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4982 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4983 if (!ToRetExpr && S->getRetValue())
4984 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00004985 auto *NRVOCandidate = const_cast<VarDecl *>(S->getNRVOCandidate());
4986 auto *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
Sean Callanan59721b32015-04-28 18:41:46 +00004987 if (!ToNRVOCandidate && NRVOCandidate)
4988 return nullptr;
4989 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4990 ToNRVOCandidate);
4991}
4992
4993Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4994 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4995 VarDecl *ToExceptionDecl = nullptr;
4996 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4997 ToExceptionDecl =
4998 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4999 if (!ToExceptionDecl)
5000 return nullptr;
5001 }
5002 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5003 if (!ToHandlerBlock && S->getHandlerBlock())
5004 return nullptr;
5005 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5006 ToExceptionDecl,
5007 ToHandlerBlock);
5008}
5009
5010Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5011 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5012 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5013 if (!ToTryBlock && S->getTryBlock())
5014 return nullptr;
5015 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5016 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5017 CXXCatchStmt *FromHandler = S->getHandler(HI);
5018 if (Stmt *ToHandler = Importer.Import(FromHandler))
5019 ToHandlers[HI] = ToHandler;
5020 else
5021 return nullptr;
5022 }
5023 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5024 ToHandlers);
5025}
5026
5027Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005028 auto *ToRange =
Sean Callanan59721b32015-04-28 18:41:46 +00005029 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5030 if (!ToRange && S->getRangeStmt())
5031 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005032 auto *ToBegin =
Richard Smith01694c32016-03-20 10:33:40 +00005033 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5034 if (!ToBegin && S->getBeginStmt())
5035 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005036 auto *ToEnd =
Richard Smith01694c32016-03-20 10:33:40 +00005037 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5038 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005039 return nullptr;
5040 Expr *ToCond = Importer.Import(S->getCond());
5041 if (!ToCond && S->getCond())
5042 return nullptr;
5043 Expr *ToInc = Importer.Import(S->getInc());
5044 if (!ToInc && S->getInc())
5045 return nullptr;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005046 auto *ToLoopVar =
Sean Callanan59721b32015-04-28 18:41:46 +00005047 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5048 if (!ToLoopVar && S->getLoopVarStmt())
5049 return nullptr;
5050 Stmt *ToBody = Importer.Import(S->getBody());
5051 if (!ToBody && S->getBody())
5052 return nullptr;
5053 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005054 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005055 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5056 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005057 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005058 ToCond, ToInc,
5059 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005060 ToForLoc, ToCoawaitLoc,
5061 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005062}
5063
5064Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5065 Stmt *ToElem = Importer.Import(S->getElement());
5066 if (!ToElem && S->getElement())
5067 return nullptr;
5068 Expr *ToCollect = Importer.Import(S->getCollection());
5069 if (!ToCollect && S->getCollection())
5070 return nullptr;
5071 Stmt *ToBody = Importer.Import(S->getBody());
5072 if (!ToBody && S->getBody())
5073 return nullptr;
5074 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5075 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5076 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5077 ToCollect,
5078 ToBody, ToForLoc,
5079 ToRParenLoc);
5080}
5081
5082Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5083 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5084 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5085 VarDecl *ToExceptionDecl = nullptr;
5086 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5087 ToExceptionDecl =
5088 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5089 if (!ToExceptionDecl)
5090 return nullptr;
5091 }
5092 Stmt *ToBody = Importer.Import(S->getCatchBody());
5093 if (!ToBody && S->getCatchBody())
5094 return nullptr;
5095 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5096 ToRParenLoc,
5097 ToExceptionDecl,
5098 ToBody);
5099}
5100
5101Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5102 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5103 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5104 if (!ToAtFinallyStmt && S->getFinallyBody())
5105 return nullptr;
5106 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5107 ToAtFinallyStmt);
5108}
5109
5110Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5111 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5112 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5113 if (!ToAtTryStmt && S->getTryBody())
5114 return nullptr;
5115 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5116 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5117 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5118 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5119 ToCatchStmts[CI] = ToCatchStmt;
5120 else
5121 return nullptr;
5122 }
5123 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5124 if (!ToAtFinallyStmt && S->getFinallyStmt())
5125 return nullptr;
5126 return ObjCAtTryStmt::Create(Importer.getToContext(),
5127 ToAtTryLoc, ToAtTryStmt,
5128 ToCatchStmts.begin(), ToCatchStmts.size(),
5129 ToAtFinallyStmt);
5130}
5131
5132Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5133 (ObjCAtSynchronizedStmt *S) {
5134 SourceLocation ToAtSynchronizedLoc =
5135 Importer.Import(S->getAtSynchronizedLoc());
5136 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5137 if (!ToSynchExpr && S->getSynchExpr())
5138 return nullptr;
5139 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5140 if (!ToSynchBody && S->getSynchBody())
5141 return nullptr;
5142 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5143 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5144}
5145
5146Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5147 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5148 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5149 if (!ToThrow && S->getThrowExpr())
5150 return nullptr;
5151 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5152}
5153
5154Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5155 (ObjCAutoreleasePoolStmt *S) {
5156 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5157 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5158 if (!ToSubStmt && S->getSubStmt())
5159 return nullptr;
5160 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5161 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005162}
5163
5164//----------------------------------------------------------------------------
5165// Import Expressions
5166//----------------------------------------------------------------------------
5167Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5168 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5169 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005170 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005171}
5172
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005173Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5174 QualType T = Importer.Import(E->getType());
5175 if (T.isNull())
5176 return nullptr;
5177
5178 Expr *SubExpr = Importer.Import(E->getSubExpr());
5179 if (!SubExpr && E->getSubExpr())
5180 return nullptr;
5181
5182 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5183 if (!TInfo)
5184 return nullptr;
5185
5186 return new (Importer.getToContext()) VAArgExpr(
5187 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5188 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5189}
5190
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005191Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5192 QualType T = Importer.Import(E->getType());
5193 if (T.isNull())
5194 return nullptr;
5195
5196 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005197 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005198}
5199
5200Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5201 QualType T = Importer.Import(E->getType());
5202 if (T.isNull())
5203 return nullptr;
5204
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005205 auto *SL = cast_or_null<StringLiteral>(Importer.Import(E->getFunctionName()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005206 if (!SL && E->getFunctionName())
5207 return nullptr;
5208
5209 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005210 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005211}
5212
Douglas Gregor52f820e2010-02-19 01:17:02 +00005213Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005214 auto *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
Douglas Gregor52f820e2010-02-19 01:17:02 +00005215 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005216 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005217
Craig Topper36250ad2014-05-12 05:36:57 +00005218 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005219 if (E->getDecl() != E->getFoundDecl()) {
5220 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5221 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005222 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005223 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005224
5225 QualType T = Importer.Import(E->getType());
5226 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005227 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005228
Aleksei Sidorina693b372016-09-28 10:16:56 +00005229 TemplateArgumentListInfo ToTAInfo;
5230 TemplateArgumentListInfo *ResInfo = nullptr;
5231 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00005232 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
5233 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00005234 ResInfo = &ToTAInfo;
5235 }
5236
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005237 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5238 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005239 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005240 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005241 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005242 Importer.Import(E->getLocation()),
5243 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005244 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005245 if (E->hadMultipleCandidates())
5246 DRE->setHadMultipleCandidates(true);
5247 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005248}
5249
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005250Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5251 QualType T = Importer.Import(E->getType());
5252 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00005253 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005254
5255 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5256}
5257
5258ASTNodeImporter::Designator
5259ASTNodeImporter::ImportDesignator(const Designator &D) {
5260 if (D.isFieldDesignator()) {
5261 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5262 // Caller checks for import error
5263 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5264 Importer.Import(D.getFieldLoc()));
5265 }
5266 if (D.isArrayDesignator())
5267 return Designator(D.getFirstExprIndex(),
5268 Importer.Import(D.getLBracketLoc()),
5269 Importer.Import(D.getRBracketLoc()));
5270
5271 assert(D.isArrayRangeDesignator());
5272 return Designator(D.getFirstExprIndex(),
5273 Importer.Import(D.getLBracketLoc()),
5274 Importer.Import(D.getEllipsisLoc()),
5275 Importer.Import(D.getRBracketLoc()));
5276}
5277
5278
5279Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005280 auto *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005281 if (!Init)
5282 return nullptr;
5283
5284 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5285 // List elements from the second, the first is Init itself
5286 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005287 if (auto *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005288 IndexExprs[I - 1] = Arg;
5289 else
5290 return nullptr;
5291 }
5292
5293 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00005294 llvm::transform(DIE->designators(), Designators.begin(),
5295 [this](const Designator &D) -> Designator {
5296 return ImportDesignator(D);
5297 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005298
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005299 for (const auto &D : DIE->designators())
David Majnemerf7e36092016-06-23 00:15:04 +00005300 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005301 return nullptr;
5302
5303 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00005304 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005305 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5306 DIE->usesGNUSyntax(), Init);
5307}
5308
5309Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5310 QualType T = Importer.Import(E->getType());
5311 if (T.isNull())
5312 return nullptr;
5313
5314 return new (Importer.getToContext())
5315 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5316}
5317
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005318Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5319 QualType T = Importer.Import(E->getType());
5320 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005321 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005322
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005323 return IntegerLiteral::Create(Importer.getToContext(),
5324 E->getValue(), T,
5325 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005326}
5327
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005328Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5329 QualType T = Importer.Import(E->getType());
5330 if (T.isNull())
5331 return nullptr;
5332
5333 return FloatingLiteral::Create(Importer.getToContext(),
5334 E->getValue(), E->isExact(), T,
5335 Importer.Import(E->getLocation()));
5336}
5337
Douglas Gregor623421d2010-02-18 02:21:22 +00005338Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5339 QualType T = Importer.Import(E->getType());
5340 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005341 return nullptr;
5342
Douglas Gregorfb65e592011-07-27 05:40:30 +00005343 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5344 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005345 Importer.Import(E->getLocation()));
5346}
5347
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005348Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5349 QualType T = Importer.Import(E->getType());
5350 if (T.isNull())
5351 return nullptr;
5352
5353 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5354 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5355
5356 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5357 E->getKind(), E->isPascal(), T,
5358 Locations.data(), Locations.size());
5359}
5360
5361Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5362 QualType T = Importer.Import(E->getType());
5363 if (T.isNull())
5364 return nullptr;
5365
5366 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5367 if (!TInfo)
5368 return nullptr;
5369
5370 Expr *Init = Importer.Import(E->getInitializer());
5371 if (!Init)
5372 return nullptr;
5373
5374 return new (Importer.getToContext()) CompoundLiteralExpr(
5375 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5376 Init, E->isFileScope());
5377}
5378
5379Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5380 QualType T = Importer.Import(E->getType());
5381 if (T.isNull())
5382 return nullptr;
5383
5384 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5385 if (ImportArrayChecked(
5386 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5387 Exprs.begin()))
5388 return nullptr;
5389
5390 return new (Importer.getToContext()) AtomicExpr(
5391 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5392 Importer.Import(E->getRParenLoc()));
5393}
5394
5395Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5396 QualType T = Importer.Import(E->getType());
5397 if (T.isNull())
5398 return nullptr;
5399
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005400 auto *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005401 if (!ToLabel)
5402 return nullptr;
5403
5404 return new (Importer.getToContext()) AddrLabelExpr(
5405 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5406 ToLabel, T);
5407}
5408
Douglas Gregorc74247e2010-02-19 01:07:06 +00005409Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5410 Expr *SubExpr = Importer.Import(E->getSubExpr());
5411 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005412 return nullptr;
5413
Douglas Gregorc74247e2010-02-19 01:07:06 +00005414 return new (Importer.getToContext())
5415 ParenExpr(Importer.Import(E->getLParen()),
5416 Importer.Import(E->getRParen()),
5417 SubExpr);
5418}
5419
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005420Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5421 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005422 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005423 return nullptr;
5424
5425 return new (Importer.getToContext()) ParenListExpr(
5426 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5427 Exprs, Importer.Import(E->getLParenLoc()));
5428}
5429
5430Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5431 QualType T = Importer.Import(E->getType());
5432 if (T.isNull())
5433 return nullptr;
5434
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005435 auto *ToSubStmt = cast_or_null<CompoundStmt>(
5436 Importer.Import(E->getSubStmt()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005437 if (!ToSubStmt && E->getSubStmt())
5438 return nullptr;
5439
5440 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5441 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5442}
5443
Douglas Gregorc74247e2010-02-19 01:07:06 +00005444Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5445 QualType T = Importer.Import(E->getType());
5446 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005447 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005448
5449 Expr *SubExpr = Importer.Import(E->getSubExpr());
5450 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
5452
Aaron Ballmana5038552018-01-09 13:07:03 +00005453 return new (Importer.getToContext()) UnaryOperator(
5454 SubExpr, E->getOpcode(), T, E->getValueKind(), E->getObjectKind(),
5455 Importer.Import(E->getOperatorLoc()), E->canOverflow());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005456}
5457
Aaron Ballmana5038552018-01-09 13:07:03 +00005458Expr *
5459ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005460 QualType ResultType = Importer.Import(E->getType());
5461
5462 if (E->isArgumentType()) {
5463 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5464 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005465 return nullptr;
5466
Peter Collingbournee190dee2011-03-11 19:24:49 +00005467 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5468 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005469 Importer.Import(E->getOperatorLoc()),
5470 Importer.Import(E->getRParenLoc()));
5471 }
5472
5473 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5474 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005475 return nullptr;
5476
Peter Collingbournee190dee2011-03-11 19:24:49 +00005477 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5478 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005479 Importer.Import(E->getOperatorLoc()),
5480 Importer.Import(E->getRParenLoc()));
5481}
5482
Douglas Gregorc74247e2010-02-19 01:07:06 +00005483Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5484 QualType T = Importer.Import(E->getType());
5485 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005486 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005487
5488 Expr *LHS = Importer.Import(E->getLHS());
5489 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005490 return nullptr;
5491
Douglas Gregorc74247e2010-02-19 01:07:06 +00005492 Expr *RHS = Importer.Import(E->getRHS());
5493 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005494 return nullptr;
5495
Douglas Gregorc74247e2010-02-19 01:07:06 +00005496 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005497 T, E->getValueKind(),
5498 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005499 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005500 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005501}
5502
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005503Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5504 QualType T = Importer.Import(E->getType());
5505 if (T.isNull())
5506 return nullptr;
5507
5508 Expr *ToLHS = Importer.Import(E->getLHS());
5509 if (!ToLHS)
5510 return nullptr;
5511
5512 Expr *ToRHS = Importer.Import(E->getRHS());
5513 if (!ToRHS)
5514 return nullptr;
5515
5516 Expr *ToCond = Importer.Import(E->getCond());
5517 if (!ToCond)
5518 return nullptr;
5519
5520 return new (Importer.getToContext()) ConditionalOperator(
5521 ToCond, Importer.Import(E->getQuestionLoc()),
5522 ToLHS, Importer.Import(E->getColonLoc()),
5523 ToRHS, T, E->getValueKind(), E->getObjectKind());
5524}
5525
5526Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5527 BinaryConditionalOperator *E) {
5528 QualType T = Importer.Import(E->getType());
5529 if (T.isNull())
5530 return nullptr;
5531
5532 Expr *Common = Importer.Import(E->getCommon());
5533 if (!Common)
5534 return nullptr;
5535
5536 Expr *Cond = Importer.Import(E->getCond());
5537 if (!Cond)
5538 return nullptr;
5539
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005540 auto *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5541 Importer.Import(E->getOpaqueValue()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005542 if (!OpaqueValue)
5543 return nullptr;
5544
5545 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5546 if (!TrueExpr)
5547 return nullptr;
5548
5549 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5550 if (!FalseExpr)
5551 return nullptr;
5552
5553 return new (Importer.getToContext()) BinaryConditionalOperator(
5554 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5555 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5556 T, E->getValueKind(), E->getObjectKind());
5557}
5558
Aleksei Sidorina693b372016-09-28 10:16:56 +00005559Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5560 QualType T = Importer.Import(E->getType());
5561 if (T.isNull())
5562 return nullptr;
5563
5564 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5565 if (!ToQueried)
5566 return nullptr;
5567
5568 Expr *Dim = Importer.Import(E->getDimensionExpression());
5569 if (!Dim && E->getDimensionExpression())
5570 return nullptr;
5571
5572 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5573 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5574 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5575}
5576
5577Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5578 QualType T = Importer.Import(E->getType());
5579 if (T.isNull())
5580 return nullptr;
5581
5582 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5583 if (!ToQueried)
5584 return nullptr;
5585
5586 return new (Importer.getToContext()) ExpressionTraitExpr(
5587 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5588 E->getValue(), Importer.Import(E->getLocEnd()), T);
5589}
5590
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005591Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5592 QualType T = Importer.Import(E->getType());
5593 if (T.isNull())
5594 return nullptr;
5595
5596 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5597 if (!SourceExpr && E->getSourceExpr())
5598 return nullptr;
5599
5600 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005601 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005602 E->getObjectKind(), SourceExpr);
5603}
5604
Aleksei Sidorina693b372016-09-28 10:16:56 +00005605Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5606 QualType T = Importer.Import(E->getType());
5607 if (T.isNull())
5608 return nullptr;
5609
5610 Expr *ToLHS = Importer.Import(E->getLHS());
5611 if (!ToLHS)
5612 return nullptr;
5613
5614 Expr *ToRHS = Importer.Import(E->getRHS());
5615 if (!ToRHS)
5616 return nullptr;
5617
5618 return new (Importer.getToContext()) ArraySubscriptExpr(
5619 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5620 Importer.Import(E->getRBracketLoc()));
5621}
5622
Douglas Gregorc74247e2010-02-19 01:07:06 +00005623Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5624 QualType T = Importer.Import(E->getType());
5625 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005626 return nullptr;
5627
Douglas Gregorc74247e2010-02-19 01:07:06 +00005628 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5629 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005630 return nullptr;
5631
Douglas Gregorc74247e2010-02-19 01:07:06 +00005632 QualType CompResultType = Importer.Import(E->getComputationResultType());
5633 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005634 return nullptr;
5635
Douglas Gregorc74247e2010-02-19 01:07:06 +00005636 Expr *LHS = Importer.Import(E->getLHS());
5637 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005638 return nullptr;
5639
Douglas Gregorc74247e2010-02-19 01:07:06 +00005640 Expr *RHS = Importer.Import(E->getRHS());
5641 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005642 return nullptr;
5643
Douglas Gregorc74247e2010-02-19 01:07:06 +00005644 return new (Importer.getToContext())
5645 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005646 T, E->getValueKind(),
5647 E->getObjectKind(),
5648 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005649 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005650 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005651}
5652
Aleksei Sidorina693b372016-09-28 10:16:56 +00005653bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5654 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5655 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5656 Path.push_back(Spec);
5657 else
5658 return true;
5659 }
5660 return false;
John McCallcf142162010-08-07 06:22:56 +00005661}
5662
Douglas Gregor98c10182010-02-12 22:17:39 +00005663Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5664 QualType T = Importer.Import(E->getType());
5665 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005666 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005667
5668 Expr *SubExpr = Importer.Import(E->getSubExpr());
5669 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005670 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005671
5672 CXXCastPath BasePath;
5673 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005674 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005675
5676 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005677 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005678}
5679
Aleksei Sidorina693b372016-09-28 10:16:56 +00005680Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005681 QualType T = Importer.Import(E->getType());
5682 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005683 return nullptr;
5684
Douglas Gregor5481d322010-02-19 01:32:14 +00005685 Expr *SubExpr = Importer.Import(E->getSubExpr());
5686 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005687 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005688
5689 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5690 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005691 return nullptr;
5692
John McCallcf142162010-08-07 06:22:56 +00005693 CXXCastPath BasePath;
5694 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005695 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005696
Aleksei Sidorina693b372016-09-28 10:16:56 +00005697 switch (E->getStmtClass()) {
5698 case Stmt::CStyleCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005699 auto *CCE = cast<CStyleCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005700 return CStyleCastExpr::Create(Importer.getToContext(), T,
5701 E->getValueKind(), E->getCastKind(),
5702 SubExpr, &BasePath, TInfo,
5703 Importer.Import(CCE->getLParenLoc()),
5704 Importer.Import(CCE->getRParenLoc()));
5705 }
5706
5707 case Stmt::CXXFunctionalCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005708 auto *FCE = cast<CXXFunctionalCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005709 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5710 E->getValueKind(), TInfo,
5711 E->getCastKind(), SubExpr, &BasePath,
5712 Importer.Import(FCE->getLParenLoc()),
5713 Importer.Import(FCE->getRParenLoc()));
5714 }
5715
5716 case Stmt::ObjCBridgedCastExprClass: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005717 auto *OCE = cast<ObjCBridgedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005718 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5719 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5720 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5721 TInfo, SubExpr);
5722 }
5723 default:
5724 break; // just fall through
5725 }
5726
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005727 auto *Named = cast<CXXNamedCastExpr>(E);
Aleksei Sidorina693b372016-09-28 10:16:56 +00005728 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5729 RParenLoc = Importer.Import(Named->getRParenLoc());
5730 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5731
5732 switch (E->getStmtClass()) {
5733 case Stmt::CXXStaticCastExprClass:
5734 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5735 E->getValueKind(), E->getCastKind(),
5736 SubExpr, &BasePath, TInfo,
5737 ExprLoc, RParenLoc, Brackets);
5738
5739 case Stmt::CXXDynamicCastExprClass:
5740 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5741 E->getValueKind(), E->getCastKind(),
5742 SubExpr, &BasePath, TInfo,
5743 ExprLoc, RParenLoc, Brackets);
5744
5745 case Stmt::CXXReinterpretCastExprClass:
5746 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5747 E->getValueKind(), E->getCastKind(),
5748 SubExpr, &BasePath, TInfo,
5749 ExprLoc, RParenLoc, Brackets);
5750
5751 case Stmt::CXXConstCastExprClass:
5752 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5753 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5754 RParenLoc, Brackets);
5755 default:
5756 llvm_unreachable("Cast expression of unsupported type!");
5757 return nullptr;
5758 }
5759}
5760
5761Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5762 QualType T = Importer.Import(OE->getType());
5763 if (T.isNull())
5764 return nullptr;
5765
5766 SmallVector<OffsetOfNode, 4> Nodes;
5767 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5768 const OffsetOfNode &Node = OE->getComponent(I);
5769
5770 switch (Node.getKind()) {
5771 case OffsetOfNode::Array:
5772 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5773 Node.getArrayExprIndex(),
5774 Importer.Import(Node.getLocEnd())));
5775 break;
5776
5777 case OffsetOfNode::Base: {
5778 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5779 if (!BS && Node.getBase())
5780 return nullptr;
5781 Nodes.push_back(OffsetOfNode(BS));
5782 break;
5783 }
5784 case OffsetOfNode::Field: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005785 auto *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005786 if (!FD)
5787 return nullptr;
5788 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5789 Importer.Import(Node.getLocEnd())));
5790 break;
5791 }
5792 case OffsetOfNode::Identifier: {
5793 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5794 if (!ToII)
5795 return nullptr;
5796 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5797 Importer.Import(Node.getLocEnd())));
5798 break;
5799 }
5800 }
5801 }
5802
5803 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5804 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5805 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5806 if (!ToIndexExpr)
5807 return nullptr;
5808 Exprs[I] = ToIndexExpr;
5809 }
5810
5811 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5812 if (!TInfo && OE->getTypeSourceInfo())
5813 return nullptr;
5814
5815 return OffsetOfExpr::Create(Importer.getToContext(), T,
5816 Importer.Import(OE->getOperatorLoc()),
5817 TInfo, Nodes, Exprs,
5818 Importer.Import(OE->getRParenLoc()));
5819}
5820
5821Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5822 QualType T = Importer.Import(E->getType());
5823 if (T.isNull())
5824 return nullptr;
5825
5826 Expr *Operand = Importer.Import(E->getOperand());
5827 if (!Operand)
5828 return nullptr;
5829
5830 CanThrowResult CanThrow;
5831 if (E->isValueDependent())
5832 CanThrow = CT_Dependent;
5833 else
5834 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5835
5836 return new (Importer.getToContext()) CXXNoexceptExpr(
5837 T, Operand, CanThrow,
5838 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5839}
5840
5841Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5842 QualType T = Importer.Import(E->getType());
5843 if (T.isNull())
5844 return nullptr;
5845
5846 Expr *SubExpr = Importer.Import(E->getSubExpr());
5847 if (!SubExpr && E->getSubExpr())
5848 return nullptr;
5849
5850 return new (Importer.getToContext()) CXXThrowExpr(
5851 SubExpr, T, Importer.Import(E->getThrowLoc()),
5852 E->isThrownVariableInScope());
5853}
5854
5855Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005856 auto *Param = cast_or_null<ParmVarDecl>(Importer.Import(E->getParam()));
Aleksei Sidorina693b372016-09-28 10:16:56 +00005857 if (!Param)
5858 return nullptr;
5859
5860 return CXXDefaultArgExpr::Create(
5861 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5862}
5863
5864Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5865 QualType T = Importer.Import(E->getType());
5866 if (T.isNull())
5867 return nullptr;
5868
5869 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5870 if (!TypeInfo)
5871 return nullptr;
5872
5873 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5874 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5875}
5876
5877Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5878 Expr *SubExpr = Importer.Import(E->getSubExpr());
5879 if (!SubExpr)
5880 return nullptr;
5881
5882 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5883 Importer.Import(const_cast<CXXDestructorDecl *>(
5884 E->getTemporary()->getDestructor())));
5885 if (!Dtor)
5886 return nullptr;
5887
5888 ASTContext &ToCtx = Importer.getToContext();
5889 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5890 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5891}
5892
5893Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5894 QualType T = Importer.Import(CE->getType());
5895 if (T.isNull())
5896 return nullptr;
5897
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005898 TypeSourceInfo *TInfo = Importer.Import(CE->getTypeSourceInfo());
5899 if (!TInfo)
5900 return nullptr;
5901
Aleksei Sidorina693b372016-09-28 10:16:56 +00005902 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5903 if (ImportContainerChecked(CE->arguments(), Args))
5904 return nullptr;
5905
5906 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5907 Importer.Import(CE->getConstructor()));
5908 if (!Ctor)
5909 return nullptr;
5910
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005911 return new (Importer.getToContext()) CXXTemporaryObjectExpr(
5912 Importer.getToContext(), Ctor, T, TInfo, Args,
5913 Importer.Import(CE->getParenOrBraceRange()), CE->hadMultipleCandidates(),
5914 CE->isListInitialization(), CE->isStdInitListInitialization(),
5915 CE->requiresZeroInitialization());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005916}
5917
5918Expr *
5919ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5920 QualType T = Importer.Import(E->getType());
5921 if (T.isNull())
5922 return nullptr;
5923
5924 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5925 if (!TempE)
5926 return nullptr;
5927
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005928 auto *ExtendedBy = cast_or_null<ValueDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005929 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5930 if (!ExtendedBy && E->getExtendingDecl())
5931 return nullptr;
5932
5933 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5934 T, TempE, E->isBoundToLvalueReference());
5935
5936 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5937 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5938 return ToMTE;
5939}
5940
Gabor Horvath7a91c082017-11-14 11:30:38 +00005941Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5942 QualType T = Importer.Import(E->getType());
5943 if (T.isNull())
5944 return nullptr;
5945
5946 Expr *Pattern = Importer.Import(E->getPattern());
5947 if (!Pattern)
5948 return nullptr;
5949
5950 return new (Importer.getToContext()) PackExpansionExpr(
5951 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5952 E->getNumExpansions());
5953}
5954
Gabor Horvathc78d99a2018-01-27 16:11:45 +00005955Expr *ASTNodeImporter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
5956 auto *Pack = cast_or_null<NamedDecl>(Importer.Import(E->getPack()));
5957 if (!Pack)
5958 return nullptr;
5959
5960 Optional<unsigned> Length;
5961
5962 if (!E->isValueDependent())
5963 Length = E->getPackLength();
5964
5965 SmallVector<TemplateArgument, 8> PartialArguments;
5966 if (E->isPartiallySubstituted()) {
5967 if (ImportTemplateArguments(E->getPartialArguments().data(),
5968 E->getPartialArguments().size(),
5969 PartialArguments))
5970 return nullptr;
5971 }
5972
5973 return SizeOfPackExpr::Create(
5974 Importer.getToContext(), Importer.Import(E->getOperatorLoc()), Pack,
5975 Importer.Import(E->getPackLoc()), Importer.Import(E->getRParenLoc()),
5976 Length, PartialArguments);
5977}
5978
Aleksei Sidorina693b372016-09-28 10:16:56 +00005979Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5980 QualType T = Importer.Import(CE->getType());
5981 if (T.isNull())
5982 return nullptr;
5983
5984 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5985 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5986 return nullptr;
5987
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005988 auto *OperatorNewDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005989 Importer.Import(CE->getOperatorNew()));
5990 if (!OperatorNewDecl && CE->getOperatorNew())
5991 return nullptr;
5992
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00005993 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005994 Importer.Import(CE->getOperatorDelete()));
5995 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5996 return nullptr;
5997
5998 Expr *ToInit = Importer.Import(CE->getInitializer());
5999 if (!ToInit && CE->getInitializer())
6000 return nullptr;
6001
6002 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
6003 if (!TInfo)
6004 return nullptr;
6005
6006 Expr *ToArrSize = Importer.Import(CE->getArraySize());
6007 if (!ToArrSize && CE->getArraySize())
6008 return nullptr;
6009
6010 return new (Importer.getToContext()) CXXNewExpr(
6011 Importer.getToContext(),
6012 CE->isGlobalNew(),
6013 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00006014 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00006015 CE->doesUsualArrayDeleteWantSize(),
6016 PlacementArgs,
6017 Importer.Import(CE->getTypeIdParens()),
6018 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
6019 Importer.Import(CE->getSourceRange()),
6020 Importer.Import(CE->getDirectInitRange()));
6021}
6022
6023Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
6024 QualType T = Importer.Import(E->getType());
6025 if (T.isNull())
6026 return nullptr;
6027
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006028 auto *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
Aleksei Sidorina693b372016-09-28 10:16:56 +00006029 Importer.Import(E->getOperatorDelete()));
6030 if (!OperatorDeleteDecl && E->getOperatorDelete())
6031 return nullptr;
6032
6033 Expr *ToArg = Importer.Import(E->getArgument());
6034 if (!ToArg && E->getArgument())
6035 return nullptr;
6036
6037 return new (Importer.getToContext()) CXXDeleteExpr(
6038 T, E->isGlobalDelete(),
6039 E->isArrayForm(),
6040 E->isArrayFormAsWritten(),
6041 E->doesUsualArrayDeleteWantSize(),
6042 OperatorDeleteDecl,
6043 ToArg,
6044 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00006045}
6046
Sean Callanan59721b32015-04-28 18:41:46 +00006047Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
6048 QualType T = Importer.Import(E->getType());
6049 if (T.isNull())
6050 return nullptr;
6051
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006052 auto *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00006053 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00006054 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00006055 return nullptr;
6056
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006057 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006058 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006059 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006060
6061 return CXXConstructExpr::Create(Importer.getToContext(), T,
6062 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00006063 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00006064 ToArgs, E->hadMultipleCandidates(),
6065 E->isListInitialization(),
6066 E->isStdInitListInitialization(),
6067 E->requiresZeroInitialization(),
6068 E->getConstructionKind(),
6069 Importer.Import(E->getParenOrBraceRange()));
6070}
6071
Aleksei Sidorina693b372016-09-28 10:16:56 +00006072Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
6073 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
6074 if (!SubExpr && EWC->getSubExpr())
6075 return nullptr;
6076
6077 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
6078 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
6079 if (ExprWithCleanups::CleanupObject Obj =
6080 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
6081 Objs[I] = Obj;
6082 else
6083 return nullptr;
6084
6085 return ExprWithCleanups::Create(Importer.getToContext(),
6086 SubExpr, EWC->cleanupsHaveSideEffects(),
6087 Objs);
6088}
6089
Sean Callanan8bca9962016-03-28 21:43:01 +00006090Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
6091 QualType T = Importer.Import(E->getType());
6092 if (T.isNull())
6093 return nullptr;
6094
6095 Expr *ToFn = Importer.Import(E->getCallee());
6096 if (!ToFn)
6097 return nullptr;
6098
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006099 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006100 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006101 return nullptr;
6102
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006103 return new (Importer.getToContext()) CXXMemberCallExpr(
6104 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
6105 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00006106}
6107
6108Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
6109 QualType T = Importer.Import(E->getType());
6110 if (T.isNull())
6111 return nullptr;
6112
6113 return new (Importer.getToContext())
6114 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
6115}
6116
6117Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
6118 QualType T = Importer.Import(E->getType());
6119 if (T.isNull())
6120 return nullptr;
6121
6122 return new (Importer.getToContext())
6123 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
6124}
6125
6126
Sean Callanan59721b32015-04-28 18:41:46 +00006127Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
6128 QualType T = Importer.Import(E->getType());
6129 if (T.isNull())
6130 return nullptr;
6131
6132 Expr *ToBase = Importer.Import(E->getBase());
6133 if (!ToBase && E->getBase())
6134 return nullptr;
6135
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006136 auto *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
Sean Callanan59721b32015-04-28 18:41:46 +00006137 if (!ToMember && E->getMemberDecl())
6138 return nullptr;
6139
Peter Szecsief972522018-05-02 11:52:54 +00006140 auto *ToDecl =
6141 dyn_cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl()));
6142 if (!ToDecl && E->getFoundDecl().getDecl())
6143 return nullptr;
6144
6145 DeclAccessPair ToFoundDecl =
6146 DeclAccessPair::make(ToDecl, E->getFoundDecl().getAccess());
Sean Callanan59721b32015-04-28 18:41:46 +00006147
6148 DeclarationNameInfo ToMemberNameInfo(
6149 Importer.Import(E->getMemberNameInfo().getName()),
6150 Importer.Import(E->getMemberNameInfo().getLoc()));
6151
6152 if (E->hasExplicitTemplateArgs()) {
6153 return nullptr; // FIXME: handle template arguments
6154 }
6155
6156 return MemberExpr::Create(Importer.getToContext(), ToBase,
6157 E->isArrow(),
6158 Importer.Import(E->getOperatorLoc()),
6159 Importer.Import(E->getQualifierLoc()),
6160 Importer.Import(E->getTemplateKeywordLoc()),
6161 ToMember, ToFoundDecl, ToMemberNameInfo,
6162 nullptr, T, E->getValueKind(),
6163 E->getObjectKind());
6164}
6165
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006166Expr *ASTNodeImporter::VisitCXXPseudoDestructorExpr(
6167 CXXPseudoDestructorExpr *E) {
Aleksei Sidorin60ccb7d2017-11-27 10:30:00 +00006168 Expr *BaseE = Importer.Import(E->getBase());
6169 if (!BaseE)
6170 return nullptr;
6171
6172 TypeSourceInfo *ScopeInfo = Importer.Import(E->getScopeTypeInfo());
6173 if (!ScopeInfo && E->getScopeTypeInfo())
6174 return nullptr;
6175
6176 PseudoDestructorTypeStorage Storage;
6177 if (IdentifierInfo *FromII = E->getDestroyedTypeIdentifier()) {
6178 IdentifierInfo *ToII = Importer.Import(FromII);
6179 if (!ToII)
6180 return nullptr;
6181 Storage = PseudoDestructorTypeStorage(
6182 ToII, Importer.Import(E->getDestroyedTypeLoc()));
6183 } else {
6184 TypeSourceInfo *TI = Importer.Import(E->getDestroyedTypeInfo());
6185 if (!TI)
6186 return nullptr;
6187 Storage = PseudoDestructorTypeStorage(TI);
6188 }
6189
6190 return new (Importer.getToContext()) CXXPseudoDestructorExpr(
6191 Importer.getToContext(), BaseE, E->isArrow(),
6192 Importer.Import(E->getOperatorLoc()),
6193 Importer.Import(E->getQualifierLoc()),
6194 ScopeInfo, Importer.Import(E->getColonColonLoc()),
6195 Importer.Import(E->getTildeLoc()), Storage);
6196}
6197
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006198Expr *ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
6199 CXXDependentScopeMemberExpr *E) {
6200 Expr *Base = nullptr;
6201 if (!E->isImplicitAccess()) {
6202 Base = Importer.Import(E->getBase());
6203 if (!Base)
6204 return nullptr;
6205 }
6206
6207 QualType BaseType = Importer.Import(E->getBaseType());
6208 if (BaseType.isNull())
6209 return nullptr;
6210
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006211 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006212 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006213 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6214 E->template_arguments(), ToTAInfo))
Aleksei Sidorin7f758b62017-12-27 17:04:42 +00006215 return nullptr;
6216 ResInfo = &ToTAInfo;
6217 }
6218
6219 DeclarationName Name = Importer.Import(E->getMember());
6220 if (!E->getMember().isEmpty() && Name.isEmpty())
6221 return nullptr;
6222
6223 DeclarationNameInfo MemberNameInfo(Name, Importer.Import(E->getMemberLoc()));
6224 // Import additional name location/type info.
6225 ImportDeclarationNameLoc(E->getMemberNameInfo(), MemberNameInfo);
6226 auto ToFQ = Importer.Import(E->getFirstQualifierFoundInScope());
6227 if (!ToFQ && E->getFirstQualifierFoundInScope())
6228 return nullptr;
6229
6230 return CXXDependentScopeMemberExpr::Create(
6231 Importer.getToContext(), Base, BaseType, E->isArrow(),
6232 Importer.Import(E->getOperatorLoc()),
6233 Importer.Import(E->getQualifierLoc()),
6234 Importer.Import(E->getTemplateKeywordLoc()),
6235 cast_or_null<NamedDecl>(ToFQ), MemberNameInfo, ResInfo);
6236}
6237
Peter Szecsice7f3182018-05-07 12:08:27 +00006238Expr *
6239ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
6240 DeclarationName Name = Importer.Import(E->getDeclName());
6241 if (!E->getDeclName().isEmpty() && Name.isEmpty())
6242 return nullptr;
6243
6244 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getExprLoc()));
6245 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6246
6247 TemplateArgumentListInfo ToTAInfo(Importer.Import(E->getLAngleLoc()),
6248 Importer.Import(E->getRAngleLoc()));
6249 TemplateArgumentListInfo *ResInfo = nullptr;
6250 if (E->hasExplicitTemplateArgs()) {
6251 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6252 return nullptr;
6253 ResInfo = &ToTAInfo;
6254 }
6255
6256 return DependentScopeDeclRefExpr::Create(
6257 Importer.getToContext(), Importer.Import(E->getQualifierLoc()),
6258 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo);
6259}
6260
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006261Expr *ASTNodeImporter::VisitCXXUnresolvedConstructExpr(
6262 CXXUnresolvedConstructExpr *CE) {
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006263 unsigned NumArgs = CE->arg_size();
6264
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006265 SmallVector<Expr *, 8> ToArgs(NumArgs);
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006266 if (ImportArrayChecked(CE->arg_begin(), CE->arg_end(), ToArgs.begin()))
6267 return nullptr;
6268
6269 return CXXUnresolvedConstructExpr::Create(
6270 Importer.getToContext(), Importer.Import(CE->getTypeSourceInfo()),
6271 Importer.Import(CE->getLParenLoc()), llvm::makeArrayRef(ToArgs),
6272 Importer.Import(CE->getRParenLoc()));
6273}
6274
6275Expr *ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006276 auto *NamingClass =
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006277 cast_or_null<CXXRecordDecl>(Importer.Import(E->getNamingClass()));
6278 if (E->getNamingClass() && !NamingClass)
6279 return nullptr;
6280
6281 DeclarationName Name = Importer.Import(E->getName());
6282 if (E->getName() && !Name)
6283 return nullptr;
6284
6285 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6286 // Import additional name location/type info.
6287 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6288
6289 UnresolvedSet<8> ToDecls;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006290 for (auto *D : E->decls()) {
6291 if (auto *To = cast_or_null<NamedDecl>(Importer.Import(D)))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006292 ToDecls.addDecl(To);
6293 else
6294 return nullptr;
6295 }
6296
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006297 TemplateArgumentListInfo ToTAInfo, *ResInfo = nullptr;
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006298 if (E->hasExplicitTemplateArgs()) {
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006299 if (ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6300 E->template_arguments(), ToTAInfo))
Aleksei Sidorine267a0f2018-01-09 16:40:40 +00006301 return nullptr;
6302 ResInfo = &ToTAInfo;
6303 }
6304
6305 if (ResInfo || E->getTemplateKeywordLoc().isValid())
6306 return UnresolvedLookupExpr::Create(
6307 Importer.getToContext(), NamingClass,
6308 Importer.Import(E->getQualifierLoc()),
6309 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, E->requiresADL(),
6310 ResInfo, ToDecls.begin(), ToDecls.end());
6311
6312 return UnresolvedLookupExpr::Create(
6313 Importer.getToContext(), NamingClass,
6314 Importer.Import(E->getQualifierLoc()), NameInfo, E->requiresADL(),
6315 E->isOverloaded(), ToDecls.begin(), ToDecls.end());
6316}
6317
Peter Szecsice7f3182018-05-07 12:08:27 +00006318Expr *ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
6319 DeclarationName Name = Importer.Import(E->getName());
6320 if (!E->getName().isEmpty() && Name.isEmpty())
6321 return nullptr;
6322 DeclarationNameInfo NameInfo(Name, Importer.Import(E->getNameLoc()));
6323 // Import additional name location/type info.
6324 ImportDeclarationNameLoc(E->getNameInfo(), NameInfo);
6325
6326 QualType BaseType = Importer.Import(E->getType());
6327 if (!E->getType().isNull() && BaseType.isNull())
6328 return nullptr;
6329
6330 UnresolvedSet<8> ToDecls;
6331 for (Decl *D : E->decls()) {
6332 if (NamedDecl *To = cast_or_null<NamedDecl>(Importer.Import(D)))
6333 ToDecls.addDecl(To);
6334 else
6335 return nullptr;
6336 }
6337
6338 TemplateArgumentListInfo ToTAInfo;
6339 TemplateArgumentListInfo *ResInfo = nullptr;
6340 if (E->hasExplicitTemplateArgs()) {
6341 if (ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6342 return nullptr;
6343 ResInfo = &ToTAInfo;
6344 }
6345
6346 Expr *BaseE = E->isImplicitAccess() ? nullptr : Importer.Import(E->getBase());
6347 if (!BaseE && !E->isImplicitAccess() && E->getBase()) {
6348 return nullptr;
6349 }
6350
6351 return UnresolvedMemberExpr::Create(
6352 Importer.getToContext(), E->hasUnresolvedUsing(), BaseE, BaseType,
6353 E->isArrow(), Importer.Import(E->getOperatorLoc()),
6354 Importer.Import(E->getQualifierLoc()),
6355 Importer.Import(E->getTemplateKeywordLoc()), NameInfo, ResInfo,
6356 ToDecls.begin(), ToDecls.end());
6357}
6358
Sean Callanan59721b32015-04-28 18:41:46 +00006359Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
6360 QualType T = Importer.Import(E->getType());
6361 if (T.isNull())
6362 return nullptr;
6363
6364 Expr *ToCallee = Importer.Import(E->getCallee());
6365 if (!ToCallee && E->getCallee())
6366 return nullptr;
6367
6368 unsigned NumArgs = E->getNumArgs();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006369 SmallVector<Expr *, 2> ToArgs(NumArgs);
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006370 if (ImportContainerChecked(E->arguments(), ToArgs))
6371 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00006372
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006373 auto **ToArgs_Copied = new (Importer.getToContext()) Expr*[NumArgs];
Sean Callanan59721b32015-04-28 18:41:46 +00006374
6375 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
6376 ToArgs_Copied[ai] = ToArgs[ai];
6377
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006378 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E)) {
6379 return new (Importer.getToContext()) CXXOperatorCallExpr(
6380 Importer.getToContext(), OCE->getOperator(), ToCallee, ToArgs, T,
6381 OCE->getValueKind(), Importer.Import(OCE->getRParenLoc()),
6382 OCE->getFPFeatures());
6383 }
6384
Sean Callanan59721b32015-04-28 18:41:46 +00006385 return new (Importer.getToContext())
6386 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00006387 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00006388 Importer.Import(E->getRParenLoc()));
6389}
6390
Aleksei Sidorin8fc85102018-01-26 11:36:54 +00006391Optional<LambdaCapture>
6392ASTNodeImporter::ImportLambdaCapture(const LambdaCapture &From) {
6393 VarDecl *Var = nullptr;
6394 if (From.capturesVariable()) {
6395 Var = cast_or_null<VarDecl>(Importer.Import(From.getCapturedVar()));
6396 if (!Var)
6397 return None;
6398 }
6399
6400 return LambdaCapture(Importer.Import(From.getLocation()), From.isImplicit(),
6401 From.getCaptureKind(), Var,
6402 From.isPackExpansion()
6403 ? Importer.Import(From.getEllipsisLoc())
6404 : SourceLocation());
6405}
6406
6407Expr *ASTNodeImporter::VisitLambdaExpr(LambdaExpr *LE) {
6408 CXXRecordDecl *FromClass = LE->getLambdaClass();
6409 auto *ToClass = dyn_cast_or_null<CXXRecordDecl>(Importer.Import(FromClass));
6410 if (!ToClass)
6411 return nullptr;
6412
6413 // NOTE: lambda classes are created with BeingDefined flag set up.
6414 // It means that ImportDefinition doesn't work for them and we should fill it
6415 // manually.
6416 if (ToClass->isBeingDefined()) {
6417 for (auto FromField : FromClass->fields()) {
6418 auto *ToField = cast_or_null<FieldDecl>(Importer.Import(FromField));
6419 if (!ToField)
6420 return nullptr;
6421 }
6422 }
6423
6424 auto *ToCallOp = dyn_cast_or_null<CXXMethodDecl>(
6425 Importer.Import(LE->getCallOperator()));
6426 if (!ToCallOp)
6427 return nullptr;
6428
6429 ToClass->completeDefinition();
6430
6431 unsigned NumCaptures = LE->capture_size();
6432 SmallVector<LambdaCapture, 8> Captures;
6433 Captures.reserve(NumCaptures);
6434 for (const auto &FromCapture : LE->captures()) {
6435 if (auto ToCapture = ImportLambdaCapture(FromCapture))
6436 Captures.push_back(*ToCapture);
6437 else
6438 return nullptr;
6439 }
6440
6441 SmallVector<Expr *, 8> InitCaptures(NumCaptures);
6442 if (ImportContainerChecked(LE->capture_inits(), InitCaptures))
6443 return nullptr;
6444
6445 return LambdaExpr::Create(Importer.getToContext(), ToClass,
6446 Importer.Import(LE->getIntroducerRange()),
6447 LE->getCaptureDefault(),
6448 Importer.Import(LE->getCaptureDefaultLoc()),
6449 Captures,
6450 LE->hasExplicitParameters(),
6451 LE->hasExplicitResultType(),
6452 InitCaptures,
6453 Importer.Import(LE->getLocEnd()),
6454 LE->containsUnexpandedParameterPack());
6455}
6456
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006457Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
6458 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00006459 if (T.isNull())
6460 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006461
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006462 SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00006463 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00006464 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00006465
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006466 ASTContext &ToCtx = Importer.getToContext();
6467 InitListExpr *To = new (ToCtx) InitListExpr(
6468 ToCtx, Importer.Import(ILE->getLBraceLoc()),
6469 Exprs, Importer.Import(ILE->getLBraceLoc()));
6470 To->setType(T);
6471
6472 if (ILE->hasArrayFiller()) {
6473 Expr *Filler = Importer.Import(ILE->getArrayFiller());
6474 if (!Filler)
6475 return nullptr;
6476 To->setArrayFiller(Filler);
6477 }
6478
6479 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006480 auto *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006481 if (!ToFD)
6482 return nullptr;
6483 To->setInitializedFieldInUnion(ToFD);
6484 }
6485
6486 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006487 auto *ToSyntForm = cast_or_null<InitListExpr>(Importer.Import(SyntForm));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00006488 if (!ToSyntForm)
6489 return nullptr;
6490 To->setSyntacticForm(ToSyntForm);
6491 }
6492
6493 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
6494 To->setValueDependent(ILE->isValueDependent());
6495 To->setInstantiationDependent(ILE->isInstantiationDependent());
6496
6497 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00006498}
6499
Richard Smith30e304e2016-12-14 00:03:17 +00006500Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
6501 QualType ToType = Importer.Import(E->getType());
6502 if (ToType.isNull())
6503 return nullptr;
6504
6505 Expr *ToCommon = Importer.Import(E->getCommonExpr());
6506 if (!ToCommon && E->getCommonExpr())
6507 return nullptr;
6508
6509 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
6510 if (!ToSubExpr && E->getSubExpr())
6511 return nullptr;
6512
6513 return new (Importer.getToContext())
6514 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
6515}
6516
6517Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
6518 QualType ToType = Importer.Import(E->getType());
6519 if (ToType.isNull())
6520 return nullptr;
6521 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
6522}
6523
Sean Callanandd2c1742016-05-16 20:48:03 +00006524Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006525 auto *ToField = dyn_cast_or_null<FieldDecl>(Importer.Import(DIE->getField()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006526 if (!ToField && DIE->getField())
6527 return nullptr;
6528
6529 return CXXDefaultInitExpr::Create(
6530 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
6531}
6532
6533Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
6534 QualType ToType = Importer.Import(E->getType());
6535 if (ToType.isNull() && !E->getType().isNull())
6536 return nullptr;
6537 ExprValueKind VK = E->getValueKind();
6538 CastKind CK = E->getCastKind();
6539 Expr *ToOp = Importer.Import(E->getSubExpr());
6540 if (!ToOp && E->getSubExpr())
6541 return nullptr;
6542 CXXCastPath BasePath;
6543 if (ImportCastPath(E, BasePath))
6544 return nullptr;
6545 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
6546 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
6547 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
6548 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
6549
6550 if (isa<CXXStaticCastExpr>(E)) {
6551 return CXXStaticCastExpr::Create(
6552 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6553 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6554 } else if (isa<CXXDynamicCastExpr>(E)) {
6555 return CXXDynamicCastExpr::Create(
6556 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6557 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6558 } else if (isa<CXXReinterpretCastExpr>(E)) {
6559 return CXXReinterpretCastExpr::Create(
6560 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
6561 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
6562 } else {
6563 return nullptr;
6564 }
6565}
6566
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006567Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
6568 SubstNonTypeTemplateParmExpr *E) {
6569 QualType T = Importer.Import(E->getType());
6570 if (T.isNull())
6571 return nullptr;
6572
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006573 auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006574 Importer.Import(E->getParameter()));
6575 if (!Param)
6576 return nullptr;
6577
6578 Expr *Replacement = Importer.Import(E->getReplacement());
6579 if (!Replacement)
6580 return nullptr;
6581
6582 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
6583 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
6584 Replacement);
6585}
6586
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00006587Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
6588 QualType ToType = Importer.Import(E->getType());
6589 if (ToType.isNull())
6590 return nullptr;
6591
6592 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
6593 if (ImportContainerChecked(E->getArgs(), ToArgs))
6594 return nullptr;
6595
6596 // According to Sema::BuildTypeTrait(), if E is value-dependent,
6597 // Value is always false.
6598 bool ToValue = false;
6599 if (!E->isValueDependent())
6600 ToValue = E->getValue();
6601
6602 return TypeTraitExpr::Create(
6603 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
6604 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
6605}
6606
Gabor Horvathc78d99a2018-01-27 16:11:45 +00006607Expr *ASTNodeImporter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
6608 QualType ToType = Importer.Import(E->getType());
6609 if (ToType.isNull())
6610 return nullptr;
6611
6612 if (E->isTypeOperand()) {
6613 TypeSourceInfo *TSI = Importer.Import(E->getTypeOperandSourceInfo());
6614 if (!TSI)
6615 return nullptr;
6616
6617 return new (Importer.getToContext())
6618 CXXTypeidExpr(ToType, TSI, Importer.Import(E->getSourceRange()));
6619 }
6620
6621 Expr *Op = Importer.Import(E->getExprOperand());
6622 if (!Op)
6623 return nullptr;
6624
6625 return new (Importer.getToContext())
6626 CXXTypeidExpr(ToType, Op, Importer.Import(E->getSourceRange()));
6627}
6628
Lang Hames19e07e12017-06-20 21:06:00 +00006629void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
6630 CXXMethodDecl *FromMethod) {
6631 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
6632 ToMethod->addOverriddenMethod(
6633 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
6634 FromOverriddenMethod))));
6635}
6636
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006637ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00006638 ASTContext &FromContext, FileManager &FromFileManager,
6639 bool MinimalImport)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006640 : ToContext(ToContext), FromContext(FromContext),
6641 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
6642 Minimal(MinimalImport) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00006643 ImportedDecls[FromContext.getTranslationUnitDecl()]
6644 = ToContext.getTranslationUnitDecl();
6645}
6646
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006647ASTImporter::~ASTImporter() = default;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006648
6649QualType ASTImporter::Import(QualType FromT) {
6650 if (FromT.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006651 return {};
John McCall424cec92011-01-19 06:33:43 +00006652
6653 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00006654
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006655 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00006656 llvm::DenseMap<const Type *, const Type *>::iterator Pos
6657 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006658 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00006659 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006660
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006661 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00006662 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00006663 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00006664 if (ToT.isNull())
6665 return ToT;
6666
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006667 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00006668 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00006669
John McCall424cec92011-01-19 06:33:43 +00006670 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00006671}
6672
Douglas Gregor62d311f2010-02-09 19:21:46 +00006673TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006674 if (!FromTSI)
6675 return FromTSI;
6676
6677 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00006678 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006679 QualType T = Import(FromTSI->getType());
6680 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00006681 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00006682
6683 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00006684 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00006685}
6686
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00006687Attr *ASTImporter::Import(const Attr *FromAttr) {
6688 Attr *ToAttr = FromAttr->clone(ToContext);
6689 ToAttr->setRange(Import(FromAttr->getRange()));
6690 return ToAttr;
6691}
6692
Sean Callanan59721b32015-04-28 18:41:46 +00006693Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
6694 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6695 if (Pos != ImportedDecls.end()) {
6696 Decl *ToD = Pos->second;
6697 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6698 return ToD;
6699 } else {
6700 return nullptr;
6701 }
6702}
6703
Douglas Gregor62d311f2010-02-09 19:21:46 +00006704Decl *ASTImporter::Import(Decl *FromD) {
6705 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006706 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006707
Douglas Gregord451ea92011-07-29 23:31:30 +00006708 ASTNodeImporter Importer(*this);
6709
Douglas Gregor62d311f2010-02-09 19:21:46 +00006710 // Check whether we've already imported this declaration.
6711 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006712 if (Pos != ImportedDecls.end()) {
6713 Decl *ToD = Pos->second;
6714 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6715 return ToD;
6716 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006717
6718 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006719 Decl *ToD = Importer.Visit(FromD);
6720 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006721 return nullptr;
6722
Douglas Gregor62d311f2010-02-09 19:21:46 +00006723 // Record the imported declaration.
6724 ImportedDecls[FromD] = ToD;
Peter Szecsib180eeb2018-04-25 17:28:03 +00006725 ToD->IdentifierNamespace = FromD->IdentifierNamespace;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006726 return ToD;
6727}
6728
6729DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6730 if (!FromDC)
6731 return FromDC;
6732
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006733 auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006734 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006735 return nullptr;
6736
Douglas Gregor2e15c842012-02-01 21:00:38 +00006737 // When we're using a record/enum/Objective-C class/protocol as a context, we
6738 // need it to have a definition.
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006739 if (auto *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
6740 auto *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006741 if (ToRecord->isCompleteDefinition()) {
6742 // Do nothing.
6743 } else if (FromRecord->isCompleteDefinition()) {
6744 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6745 ASTNodeImporter::IDK_Basic);
6746 } else {
6747 CompleteDecl(ToRecord);
6748 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006749 } else if (auto *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6750 auto *FromEnum = cast<EnumDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006751 if (ToEnum->isCompleteDefinition()) {
6752 // Do nothing.
6753 } else if (FromEnum->isCompleteDefinition()) {
6754 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6755 ASTNodeImporter::IDK_Basic);
6756 } else {
6757 CompleteDecl(ToEnum);
6758 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006759 } else if (auto *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6760 auto *FromClass = cast<ObjCInterfaceDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006761 if (ToClass->getDefinition()) {
6762 // Do nothing.
6763 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6764 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6765 ASTNodeImporter::IDK_Basic);
6766 } else {
6767 CompleteDecl(ToClass);
6768 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006769 } else if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6770 auto *FromProto = cast<ObjCProtocolDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006771 if (ToProto->getDefinition()) {
6772 // Do nothing.
6773 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6774 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6775 ASTNodeImporter::IDK_Basic);
6776 } else {
6777 CompleteDecl(ToProto);
6778 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006779 }
6780
6781 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006782}
6783
6784Expr *ASTImporter::Import(Expr *FromE) {
6785 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006786 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006787
6788 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6789}
6790
6791Stmt *ASTImporter::Import(Stmt *FromS) {
6792 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006793 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006794
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006795 // Check whether we've already imported this declaration.
6796 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6797 if (Pos != ImportedStmts.end())
6798 return Pos->second;
6799
6800 // Import the type
6801 ASTNodeImporter Importer(*this);
6802 Stmt *ToS = Importer.Visit(FromS);
6803 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006804 return nullptr;
6805
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006806 // Record the imported declaration.
6807 ImportedStmts[FromS] = ToS;
6808 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006809}
6810
6811NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6812 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006813 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006814
Douglas Gregor90ebf252011-04-27 16:48:40 +00006815 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6816
6817 switch (FromNNS->getKind()) {
6818 case NestedNameSpecifier::Identifier:
6819 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6820 return NestedNameSpecifier::Create(ToContext, prefix, II);
6821 }
Craig Topper36250ad2014-05-12 05:36:57 +00006822 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006823
6824 case NestedNameSpecifier::Namespace:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006825 if (auto *NS =
6826 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006827 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6828 }
Craig Topper36250ad2014-05-12 05:36:57 +00006829 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006830
6831 case NestedNameSpecifier::NamespaceAlias:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006832 if (auto *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006833 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006834 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6835 }
Craig Topper36250ad2014-05-12 05:36:57 +00006836 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006837
6838 case NestedNameSpecifier::Global:
6839 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6840
Nikola Smiljanic67860242014-09-26 00:28:20 +00006841 case NestedNameSpecifier::Super:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006842 if (auto *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006843 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006844 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6845 }
6846 return nullptr;
6847
Douglas Gregor90ebf252011-04-27 16:48:40 +00006848 case NestedNameSpecifier::TypeSpec:
6849 case NestedNameSpecifier::TypeSpecWithTemplate: {
6850 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6851 if (!T.isNull()) {
6852 bool bTemplate = FromNNS->getKind() ==
6853 NestedNameSpecifier::TypeSpecWithTemplate;
6854 return NestedNameSpecifier::Create(ToContext, prefix,
6855 bTemplate, T.getTypePtr());
6856 }
6857 }
Craig Topper36250ad2014-05-12 05:36:57 +00006858 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006859 }
6860
6861 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006862}
6863
Douglas Gregor14454802011-02-25 02:25:35 +00006864NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006865 // Copied from NestedNameSpecifier mostly.
6866 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6867 NestedNameSpecifierLoc NNS = FromNNS;
6868
6869 // Push each of the nested-name-specifiers's onto a stack for
6870 // serialization in reverse order.
6871 while (NNS) {
6872 NestedNames.push_back(NNS);
6873 NNS = NNS.getPrefix();
6874 }
6875
6876 NestedNameSpecifierLocBuilder Builder;
6877
6878 while (!NestedNames.empty()) {
6879 NNS = NestedNames.pop_back_val();
6880 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6881 if (!Spec)
6882 return NestedNameSpecifierLoc();
6883
6884 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6885 switch (Kind) {
6886 case NestedNameSpecifier::Identifier:
6887 Builder.Extend(getToContext(),
6888 Spec->getAsIdentifier(),
6889 Import(NNS.getLocalBeginLoc()),
6890 Import(NNS.getLocalEndLoc()));
6891 break;
6892
6893 case NestedNameSpecifier::Namespace:
6894 Builder.Extend(getToContext(),
6895 Spec->getAsNamespace(),
6896 Import(NNS.getLocalBeginLoc()),
6897 Import(NNS.getLocalEndLoc()));
6898 break;
6899
6900 case NestedNameSpecifier::NamespaceAlias:
6901 Builder.Extend(getToContext(),
6902 Spec->getAsNamespaceAlias(),
6903 Import(NNS.getLocalBeginLoc()),
6904 Import(NNS.getLocalEndLoc()));
6905 break;
6906
6907 case NestedNameSpecifier::TypeSpec:
6908 case NestedNameSpecifier::TypeSpecWithTemplate: {
6909 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6910 QualType(Spec->getAsType(), 0));
6911 Builder.Extend(getToContext(),
6912 Import(NNS.getLocalBeginLoc()),
6913 TSI->getTypeLoc(),
6914 Import(NNS.getLocalEndLoc()));
6915 break;
6916 }
6917
6918 case NestedNameSpecifier::Global:
6919 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6920 break;
6921
6922 case NestedNameSpecifier::Super: {
6923 SourceRange ToRange = Import(NNS.getSourceRange());
6924 Builder.MakeSuper(getToContext(),
6925 Spec->getAsRecordDecl(),
6926 ToRange.getBegin(),
6927 ToRange.getEnd());
6928 }
6929 }
6930 }
6931
6932 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006933}
6934
Douglas Gregore2e50d332010-12-01 01:36:18 +00006935TemplateName ASTImporter::Import(TemplateName From) {
6936 switch (From.getKind()) {
6937 case TemplateName::Template:
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006938 if (auto *ToTemplate =
6939 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006940 return TemplateName(ToTemplate);
6941
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006942 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006943
6944 case TemplateName::OverloadedTemplate: {
6945 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6946 UnresolvedSet<2> ToTemplates;
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006947 for (auto *I : *FromStorage) {
6948 if (auto *To = cast_or_null<NamedDecl>(Import(I)))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006949 ToTemplates.addDecl(To);
6950 else
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006951 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006952 }
6953 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6954 ToTemplates.end());
6955 }
6956
6957 case TemplateName::QualifiedTemplate: {
6958 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6959 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6960 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006961 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006962
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006963 if (auto *ToTemplate =
6964 cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
Douglas Gregore2e50d332010-12-01 01:36:18 +00006965 return ToContext.getQualifiedTemplateName(Qualifier,
6966 QTN->hasTemplateKeyword(),
6967 ToTemplate);
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006968
6969 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006970 }
6971
6972 case TemplateName::DependentTemplate: {
6973 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6974 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6975 if (!Qualifier)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006976 return {};
Douglas Gregore2e50d332010-12-01 01:36:18 +00006977
6978 if (DTN->isIdentifier()) {
6979 return ToContext.getDependentTemplateName(Qualifier,
6980 Import(DTN->getIdentifier()));
6981 }
6982
6983 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6984 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006985
6986 case TemplateName::SubstTemplateTemplateParm: {
6987 SubstTemplateTemplateParmStorage *subst
6988 = From.getAsSubstTemplateTemplateParm();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006989 auto *param =
6990 cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
John McCalld9dfe3a2011-06-30 08:33:18 +00006991 if (!param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006992 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006993
6994 TemplateName replacement = Import(subst->getReplacement());
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00006995 if (replacement.isNull())
6996 return {};
John McCalld9dfe3a2011-06-30 08:33:18 +00006997
6998 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6999 }
Douglas Gregor5590be02011-01-15 06:45:20 +00007000
7001 case TemplateName::SubstTemplateTemplateParmPack: {
7002 SubstTemplateTemplateParmPackStorage *SubstPack
7003 = From.getAsSubstTemplateTemplateParmPack();
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007004 auto *Param =
7005 cast_or_null<TemplateTemplateParmDecl>(
7006 Import(SubstPack->getParameterPack()));
Douglas Gregor5590be02011-01-15 06:45:20 +00007007 if (!Param)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007008 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007009
7010 ASTNodeImporter Importer(*this);
7011 TemplateArgument ArgPack
7012 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
7013 if (ArgPack.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007014 return {};
Douglas Gregor5590be02011-01-15 06:45:20 +00007015
7016 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
7017 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00007018 }
7019
7020 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00007021}
7022
Douglas Gregor62d311f2010-02-09 19:21:46 +00007023SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
7024 if (FromLoc.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007025 return {};
Douglas Gregor62d311f2010-02-09 19:21:46 +00007026
Douglas Gregor811663e2010-02-10 00:15:17 +00007027 SourceManager &FromSM = FromContext.getSourceManager();
7028
Sean Callanan24c5fe62016-11-07 20:42:25 +00007029 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00007030 // don't have to import macro expansions.
7031 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00007032 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00007033 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
7034 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00007035 FileID ToFileID = Import(Decomposed.first);
7036 if (ToFileID.isInvalid())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007037 return {};
Sean Callanan59721b32015-04-28 18:41:46 +00007038 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
7039 .getLocWithOffset(Decomposed.second);
7040 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00007041}
7042
7043SourceRange ASTImporter::Import(SourceRange FromRange) {
7044 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
7045}
7046
Douglas Gregor811663e2010-02-10 00:15:17 +00007047FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00007048 llvm::DenseMap<FileID, FileID>::iterator Pos
7049 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00007050 if (Pos != ImportedFileIDs.end())
7051 return Pos->second;
7052
7053 SourceManager &FromSM = FromContext.getSourceManager();
7054 SourceManager &ToSM = ToContext.getSourceManager();
7055 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00007056 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00007057
7058 // Include location of this file.
7059 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
7060
7061 // Map the FileID for to the "to" source manager.
7062 FileID ToID;
7063 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00007064 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00007065 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
7066 // disk again
7067 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
7068 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00007069 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00007070 if (!Entry)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007071 return {};
Douglas Gregor811663e2010-02-10 00:15:17 +00007072 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
7073 FromSLoc.getFile().getFileCharacteristic());
7074 } else {
7075 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007076 const llvm::MemoryBuffer *
7077 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007078 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00007079 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00007080 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00007081 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00007082 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00007083 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007084
Sebastian Redl99219f12010-09-30 01:03:06 +00007085 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00007086 return ToID;
7087}
7088
Sean Callanandd2c1742016-05-16 20:48:03 +00007089CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
7090 Expr *ToExpr = Import(From->getInit());
7091 if (!ToExpr && From->getInit())
7092 return nullptr;
7093
7094 if (From->isBaseInitializer()) {
7095 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7096 if (!ToTInfo && From->getTypeSourceInfo())
7097 return nullptr;
7098
7099 return new (ToContext) CXXCtorInitializer(
7100 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
7101 ToExpr, Import(From->getRParenLoc()),
7102 From->isPackExpansion() ? Import(From->getEllipsisLoc())
7103 : SourceLocation());
7104 } else if (From->isMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007105 auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007106 if (!ToField && From->getMember())
7107 return nullptr;
7108
7109 return new (ToContext) CXXCtorInitializer(
7110 ToContext, ToField, Import(From->getMemberLocation()),
7111 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7112 } else if (From->isIndirectMemberInitializer()) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007113 auto *ToIField = cast_or_null<IndirectFieldDecl>(
Sean Callanandd2c1742016-05-16 20:48:03 +00007114 Import(From->getIndirectMember()));
7115 if (!ToIField && From->getIndirectMember())
7116 return nullptr;
7117
7118 return new (ToContext) CXXCtorInitializer(
7119 ToContext, ToIField, Import(From->getMemberLocation()),
7120 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
7121 } else if (From->isDelegatingInitializer()) {
7122 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
7123 if (!ToTInfo && From->getTypeSourceInfo())
7124 return nullptr;
7125
7126 return new (ToContext)
7127 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
7128 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00007129 } else {
7130 return nullptr;
7131 }
7132}
7133
Aleksei Sidorina693b372016-09-28 10:16:56 +00007134CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
7135 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
7136 if (Pos != ImportedCXXBaseSpecifiers.end())
7137 return Pos->second;
7138
7139 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
7140 Import(BaseSpec->getSourceRange()),
7141 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
7142 BaseSpec->getAccessSpecifierAsWritten(),
7143 Import(BaseSpec->getTypeSourceInfo()),
7144 Import(BaseSpec->getEllipsisLoc()));
7145 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
7146 return Imported;
7147}
7148
Douglas Gregor0a791672011-01-18 03:11:38 +00007149void ASTImporter::ImportDefinition(Decl *From) {
7150 Decl *To = Import(From);
7151 if (!To)
7152 return;
7153
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007154 if (auto *FromDC = cast<DeclContext>(From)) {
Douglas Gregor0a791672011-01-18 03:11:38 +00007155 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007156
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007157 if (auto *ToRecord = dyn_cast<RecordDecl>(To)) {
Sean Callanan53a6bff2011-07-19 22:38:25 +00007158 if (!ToRecord->getDefinition()) {
7159 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00007160 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00007161 return;
7162 }
7163 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007164
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007165 if (auto *ToEnum = dyn_cast<EnumDecl>(To)) {
Douglas Gregord451ea92011-07-29 23:31:30 +00007166 if (!ToEnum->getDefinition()) {
7167 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007168 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00007169 return;
7170 }
7171 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00007172
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007173 if (auto *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007174 if (!ToIFace->getDefinition()) {
7175 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007176 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007177 return;
7178 }
7179 }
Douglas Gregord451ea92011-07-29 23:31:30 +00007180
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007181 if (auto *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00007182 if (!ToProto->getDefinition()) {
7183 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00007184 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00007185 return;
7186 }
7187 }
7188
Douglas Gregor0a791672011-01-18 03:11:38 +00007189 Importer.ImportDeclContext(FromDC, true);
7190 }
7191}
7192
Douglas Gregor96e578d2010-02-05 17:54:41 +00007193DeclarationName ASTImporter::Import(DeclarationName FromName) {
7194 if (!FromName)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007195 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007196
7197 switch (FromName.getNameKind()) {
7198 case DeclarationName::Identifier:
7199 return Import(FromName.getAsIdentifierInfo());
7200
7201 case DeclarationName::ObjCZeroArgSelector:
7202 case DeclarationName::ObjCOneArgSelector:
7203 case DeclarationName::ObjCMultiArgSelector:
7204 return Import(FromName.getObjCSelector());
7205
7206 case DeclarationName::CXXConstructorName: {
7207 QualType T = Import(FromName.getCXXNameType());
7208 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007209 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007210
7211 return ToContext.DeclarationNames.getCXXConstructorName(
7212 ToContext.getCanonicalType(T));
7213 }
7214
7215 case DeclarationName::CXXDestructorName: {
7216 QualType T = Import(FromName.getCXXNameType());
7217 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007218 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007219
7220 return ToContext.DeclarationNames.getCXXDestructorName(
7221 ToContext.getCanonicalType(T));
7222 }
7223
Richard Smith35845152017-02-07 01:37:30 +00007224 case DeclarationName::CXXDeductionGuideName: {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007225 auto *Template = cast_or_null<TemplateDecl>(
Richard Smith35845152017-02-07 01:37:30 +00007226 Import(FromName.getCXXDeductionGuideTemplate()));
7227 if (!Template)
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007228 return {};
Richard Smith35845152017-02-07 01:37:30 +00007229 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
7230 }
7231
Douglas Gregor96e578d2010-02-05 17:54:41 +00007232 case DeclarationName::CXXConversionFunctionName: {
7233 QualType T = Import(FromName.getCXXNameType());
7234 if (T.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007235 return {};
Douglas Gregor96e578d2010-02-05 17:54:41 +00007236
7237 return ToContext.DeclarationNames.getCXXConversionFunctionName(
7238 ToContext.getCanonicalType(T));
7239 }
7240
7241 case DeclarationName::CXXOperatorName:
7242 return ToContext.DeclarationNames.getCXXOperatorName(
7243 FromName.getCXXOverloadedOperator());
7244
7245 case DeclarationName::CXXLiteralOperatorName:
7246 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
7247 Import(FromName.getCXXLiteralIdentifier()));
7248
7249 case DeclarationName::CXXUsingDirective:
7250 // FIXME: STATICS!
7251 return DeclarationName::getUsingDirectiveName();
7252 }
7253
David Blaikiee4d798f2012-01-20 21:50:17 +00007254 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00007255}
7256
Douglas Gregore2e50d332010-12-01 01:36:18 +00007257IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00007258 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00007259 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007260
Sean Callananf94ef1d2016-05-14 06:11:19 +00007261 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
7262
7263 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
7264 ToId->setBuiltinID(FromId->getBuiltinID());
7265
7266 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00007267}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007268
Douglas Gregor43f54792010-02-17 02:12:47 +00007269Selector ASTImporter::Import(Selector FromSel) {
7270 if (FromSel.isNull())
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007271 return {};
Douglas Gregor43f54792010-02-17 02:12:47 +00007272
Chris Lattner0e62c1c2011-07-23 10:55:15 +00007273 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00007274 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
7275 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
7276 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
7277 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
7278}
7279
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007280DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
7281 DeclContext *DC,
7282 unsigned IDNS,
7283 NamedDecl **Decls,
7284 unsigned NumDecls) {
7285 return Name;
7286}
7287
7288DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007289 if (LastDiagFromFrom)
7290 ToContext.getDiagnostics().notePriorDiagnosticFrom(
7291 FromContext.getDiagnostics());
7292 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007293 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007294}
7295
7296DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00007297 if (!LastDiagFromFrom)
7298 FromContext.getDiagnostics().notePriorDiagnosticFrom(
7299 ToContext.getDiagnostics());
7300 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00007301 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00007302}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007303
Douglas Gregor2e15c842012-02-01 21:00:38 +00007304void ASTImporter::CompleteDecl (Decl *D) {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007305 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007306 if (!ID->getDefinition())
7307 ID->startDefinition();
7308 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007309 else if (auto *PD = dyn_cast<ObjCProtocolDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007310 if (!PD->getDefinition())
7311 PD->startDefinition();
7312 }
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007313 else if (auto *TD = dyn_cast<TagDecl>(D)) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00007314 if (!TD->getDefinition() && !TD->isBeingDefined()) {
7315 TD->startDefinition();
7316 TD->setCompleteDefinition(true);
7317 }
7318 }
7319 else {
Eugene Zelenko9a9c8232018-04-09 21:54:38 +00007320 assert(0 && "CompleteDecl called on a Decl that can't be completed");
Douglas Gregor2e15c842012-02-01 21:00:38 +00007321 }
7322}
7323
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007324Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00007325 if (From->hasAttrs()) {
Aleksei Sidorin8f266db2018-05-08 12:45:21 +00007326 for (const auto *FromAttr : From->getAttrs())
7327 To->addAttr(Import(FromAttr));
Sean Callanan8bca9962016-03-28 21:43:01 +00007328 }
7329 if (From->isUsed()) {
7330 To->setIsUsed();
7331 }
Sean Callanandd2c1742016-05-16 20:48:03 +00007332 if (From->isImplicit()) {
7333 To->setImplicit();
7334 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00007335 ImportedDecls[From] = To;
7336 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00007337}
Douglas Gregorb4964f72010-02-15 23:54:17 +00007338
Douglas Gregordd6006f2012-07-17 21:16:27 +00007339bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
7340 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00007341 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00007342 = ImportedTypes.find(From.getTypePtr());
7343 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
7344 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00007345
Douglas Gregordd6006f2012-07-17 21:16:27 +00007346 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
7347 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00007348 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00007349}