blob: 8bfc9030a606b91f8e931eefb0bdd421f6703336 [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
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//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +000017#include "clang/AST/ASTStructuralEquivalence.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
Douglas Gregor3c2404b2011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000033
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000040
41 // Importing types
John McCall424cec92011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
Gabor Horvath0866c2f2016-11-23 15:24:23 +000043 QualType VisitAtomicType(const AtomicType *T);
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitBuiltinType(const BuiltinType *T);
Aleksei Sidorina693b372016-09-28 10:16:56 +000045 QualType VisitDecayedType(const DecayedType *T);
John McCall424cec92011-01-19 06:33:43 +000046 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +000061 QualType VisitUnresolvedUsingType(const UnresolvedUsingType *T);
Sean Callananda6df8a2011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000070 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitRecordType(const RecordType *T);
73 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000074 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000075 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Aleksei Sidorin855086d2017-01-23 09:30:36 +000076 QualType VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T);
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
78 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000079 // FIXME: DependentNameType
Gabor Horvath7a91c082017-11-14 11:30:38 +000080 QualType VisitPackExpansionType(const PackExpansionType *T);
John McCallc392f372010-06-11 00:33:02 +000081 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000082 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
83 QualType VisitObjCObjectType(const ObjCObjectType *T);
84 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000085
Douglas Gregor95d82832012-01-24 18:36:04 +000086 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000087 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
88 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000089 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000090 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000091 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
92 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000093 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000094
Aleksei Sidorina693b372016-09-28 10:16:56 +000095 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
96
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000097 typedef DesignatedInitExpr::Designator Designator;
98 Designator ImportDesignator(const Designator &D);
99
Douglas Gregor2e15c842012-02-01 21:00:38 +0000100
Douglas Gregor95d82832012-01-24 18:36:04 +0000101 /// \brief What we should import from the definition.
102 enum ImportDefinitionKind {
103 /// \brief Import the default subset of the definition, which might be
104 /// nothing (if minimal import is set) or might be everything (if minimal
105 /// import is not set).
106 IDK_Default,
107 /// \brief Import everything.
108 IDK_Everything,
109 /// \brief Import only the bare bones needed to establish a valid
110 /// DeclContext.
111 IDK_Basic
112 };
113
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
115 return IDK == IDK_Everything ||
116 (IDK == IDK_Default && !Importer.isMinimalImport());
117 }
118
Douglas Gregord451ea92011-07-29 23:31:30 +0000119 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000120 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000121 bool ImportDefinition(VarDecl *From, VarDecl *To,
122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000123 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000124 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000125 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000126 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000127 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000128 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000129 TemplateParameterList *ImportTemplateParameterList(
130 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000131 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000132 Optional<TemplateArgumentLoc> ImportTemplateArgumentLoc(
133 const TemplateArgumentLoc &TALoc);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000134 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
135 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000136 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000137 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
138 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000139 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
140 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000141 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000142 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000143 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000144 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000145 Decl *VisitDecl(Decl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000146 Decl *VisitEmptyDecl(EmptyDecl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000147 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000148 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000149 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000150 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000151 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000152 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000153 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000154 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000155 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000156 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000157 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000158 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000159 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000160 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000161 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
162 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
163 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
164 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000165 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000166 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000167 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000168 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000169 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000170 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000171 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000172 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000173 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000174 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000175 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000176 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000177 Decl *VisitUsingDecl(UsingDecl *D);
178 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
179 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
180 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
181 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
182
Douglas Gregor85f3f952015-07-07 03:57:15 +0000183
184 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000185 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000186 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000187 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000188 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000189 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000190 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
191 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
192 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
193 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000194 Decl *VisitClassTemplateSpecializationDecl(
195 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000196 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
197 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
198
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000199 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000200 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
201
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000202 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000203 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000204 Stmt *VisitDeclStmt(DeclStmt *S);
205 Stmt *VisitNullStmt(NullStmt *S);
206 Stmt *VisitCompoundStmt(CompoundStmt *S);
207 Stmt *VisitCaseStmt(CaseStmt *S);
208 Stmt *VisitDefaultStmt(DefaultStmt *S);
209 Stmt *VisitLabelStmt(LabelStmt *S);
210 Stmt *VisitAttributedStmt(AttributedStmt *S);
211 Stmt *VisitIfStmt(IfStmt *S);
212 Stmt *VisitSwitchStmt(SwitchStmt *S);
213 Stmt *VisitWhileStmt(WhileStmt *S);
214 Stmt *VisitDoStmt(DoStmt *S);
215 Stmt *VisitForStmt(ForStmt *S);
216 Stmt *VisitGotoStmt(GotoStmt *S);
217 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
218 Stmt *VisitContinueStmt(ContinueStmt *S);
219 Stmt *VisitBreakStmt(BreakStmt *S);
220 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000221 // FIXME: MSAsmStmt
222 // FIXME: SEHExceptStmt
223 // FIXME: SEHFinallyStmt
224 // FIXME: SEHTryStmt
225 // FIXME: SEHLeaveStmt
226 // FIXME: CapturedStmt
227 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
228 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
229 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
230 // FIXME: MSDependentExistsStmt
231 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
232 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
233 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
234 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
235 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
236 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
237 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000238
239 // Importing expressions
240 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000241 Expr *VisitVAArgExpr(VAArgExpr *E);
242 Expr *VisitGNUNullExpr(GNUNullExpr *E);
243 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000244 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000245 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
246 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
247 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000248 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000249 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000250 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000251 Expr *VisitStringLiteral(StringLiteral *E);
252 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
253 Expr *VisitAtomicExpr(AtomicExpr *E);
254 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000255 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000256 Expr *VisitParenListExpr(ParenListExpr *E);
257 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000258 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000259 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000260 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000261 Expr *VisitConditionalOperator(ConditionalOperator *E);
262 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
263 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000264 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
265 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
266 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000267 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000268 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000269 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
270 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
271 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
272 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
273 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
274 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
275 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
276 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
277 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
Gabor Horvath7a91c082017-11-14 11:30:38 +0000278 Expr *VisitPackExpansionExpr(PackExpansionExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000279 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
280 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000281 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000282 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000283 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000284 Expr *VisitCXXThisExpr(CXXThisExpr *E);
285 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000286 Expr *VisitMemberExpr(MemberExpr *E);
287 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000288 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000289 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
290 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000291 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
292 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000293 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +0000294 Expr *VisitTypeTraitExpr(TypeTraitExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000295
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000296
297 template<typename IIter, typename OIter>
298 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
299 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
300 ASTImporter &ImporterRef = Importer;
301 std::transform(Ibegin, Iend, Obegin,
302 [&ImporterRef](ItemT From) -> ItemT {
303 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000304 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000305 }
306
307 template<typename IIter, typename OIter>
308 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
309 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
310 ASTImporter &ImporterRef = Importer;
311 bool Failed = false;
312 std::transform(Ibegin, Iend, Obegin,
313 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000314 ItemT *To = cast_or_null<ItemT>(
315 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000316 if (!To && From)
317 Failed = true;
318 return To;
319 });
320 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000321 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000322
323 template<typename InContainerTy, typename OutContainerTy>
324 bool ImportContainerChecked(const InContainerTy &InContainer,
325 OutContainerTy &OutContainer) {
326 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
327 OutContainer.begin());
328 }
329
330 template<typename InContainerTy, typename OIter>
331 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
332 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
333 }
Lang Hames19e07e12017-06-20 21:06:00 +0000334
335 // Importing overrides.
336 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000337 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000338}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000339
Douglas Gregor3996e242010-02-15 22:01:00 +0000340//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000341// Import Types
342//----------------------------------------------------------------------------
343
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000344using namespace clang;
345
John McCall424cec92011-01-19 06:33:43 +0000346QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000347 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
348 << T->getTypeClassName();
349 return QualType();
350}
351
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000352QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
353 QualType UnderlyingType = Importer.Import(T->getValueType());
354 if(UnderlyingType.isNull())
355 return QualType();
356
357 return Importer.getToContext().getAtomicType(UnderlyingType);
358}
359
John McCall424cec92011-01-19 06:33:43 +0000360QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000361 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000362#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
363 case BuiltinType::Id: \
364 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000365#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000366#define SHARED_SINGLETON_TYPE(Expansion)
367#define BUILTIN_TYPE(Id, SingletonId) \
368 case BuiltinType::Id: return Importer.getToContext().SingletonId;
369#include "clang/AST/BuiltinTypes.def"
370
371 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
372 // context supports C++.
373
374 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
375 // context supports ObjC.
376
Douglas Gregor96e578d2010-02-05 17:54:41 +0000377 case BuiltinType::Char_U:
378 // The context we're importing from has an unsigned 'char'. If we're
379 // importing into a context with a signed 'char', translate to
380 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000381 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000382 return Importer.getToContext().UnsignedCharTy;
383
384 return Importer.getToContext().CharTy;
385
Douglas Gregor96e578d2010-02-05 17:54:41 +0000386 case BuiltinType::Char_S:
387 // The context we're importing from has an unsigned 'char'. If we're
388 // importing into a context with a signed 'char', translate to
389 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000390 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000391 return Importer.getToContext().SignedCharTy;
392
393 return Importer.getToContext().CharTy;
394
Chris Lattnerad3467e2010-12-25 23:25:43 +0000395 case BuiltinType::WChar_S:
396 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000397 // FIXME: If not in C++, shall we translate to the C equivalent of
398 // wchar_t?
399 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000400 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000401
402 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000403}
404
Aleksei Sidorina693b372016-09-28 10:16:56 +0000405QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
406 QualType OrigT = Importer.Import(T->getOriginalType());
407 if (OrigT.isNull())
408 return QualType();
409
410 return Importer.getToContext().getDecayedType(OrigT);
411}
412
John McCall424cec92011-01-19 06:33:43 +0000413QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000414 QualType ToElementType = Importer.Import(T->getElementType());
415 if (ToElementType.isNull())
416 return QualType();
417
418 return Importer.getToContext().getComplexType(ToElementType);
419}
420
John McCall424cec92011-01-19 06:33:43 +0000421QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000422 QualType ToPointeeType = Importer.Import(T->getPointeeType());
423 if (ToPointeeType.isNull())
424 return QualType();
425
426 return Importer.getToContext().getPointerType(ToPointeeType);
427}
428
John McCall424cec92011-01-19 06:33:43 +0000429QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000430 // FIXME: Check for blocks support in "to" context.
431 QualType ToPointeeType = Importer.Import(T->getPointeeType());
432 if (ToPointeeType.isNull())
433 return QualType();
434
435 return Importer.getToContext().getBlockPointerType(ToPointeeType);
436}
437
John McCall424cec92011-01-19 06:33:43 +0000438QualType
439ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000440 // FIXME: Check for C++ support in "to" context.
441 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
442 if (ToPointeeType.isNull())
443 return QualType();
444
445 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
446}
447
John McCall424cec92011-01-19 06:33:43 +0000448QualType
449ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000450 // FIXME: Check for C++0x support in "to" context.
451 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
452 if (ToPointeeType.isNull())
453 return QualType();
454
455 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
456}
457
John McCall424cec92011-01-19 06:33:43 +0000458QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000459 // FIXME: Check for C++ support in "to" context.
460 QualType ToPointeeType = Importer.Import(T->getPointeeType());
461 if (ToPointeeType.isNull())
462 return QualType();
463
464 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
465 return Importer.getToContext().getMemberPointerType(ToPointeeType,
466 ClassType.getTypePtr());
467}
468
John McCall424cec92011-01-19 06:33:43 +0000469QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000470 QualType ToElementType = Importer.Import(T->getElementType());
471 if (ToElementType.isNull())
472 return QualType();
473
474 return Importer.getToContext().getConstantArrayType(ToElementType,
475 T->getSize(),
476 T->getSizeModifier(),
477 T->getIndexTypeCVRQualifiers());
478}
479
John McCall424cec92011-01-19 06:33:43 +0000480QualType
481ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000482 QualType ToElementType = Importer.Import(T->getElementType());
483 if (ToElementType.isNull())
484 return QualType();
485
486 return Importer.getToContext().getIncompleteArrayType(ToElementType,
487 T->getSizeModifier(),
488 T->getIndexTypeCVRQualifiers());
489}
490
John McCall424cec92011-01-19 06:33:43 +0000491QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000492 QualType ToElementType = Importer.Import(T->getElementType());
493 if (ToElementType.isNull())
494 return QualType();
495
496 Expr *Size = Importer.Import(T->getSizeExpr());
497 if (!Size)
498 return QualType();
499
500 SourceRange Brackets = Importer.Import(T->getBracketsRange());
501 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
502 T->getSizeModifier(),
503 T->getIndexTypeCVRQualifiers(),
504 Brackets);
505}
506
John McCall424cec92011-01-19 06:33:43 +0000507QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000508 QualType ToElementType = Importer.Import(T->getElementType());
509 if (ToElementType.isNull())
510 return QualType();
511
512 return Importer.getToContext().getVectorType(ToElementType,
513 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000514 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000515}
516
John McCall424cec92011-01-19 06:33:43 +0000517QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000518 QualType ToElementType = Importer.Import(T->getElementType());
519 if (ToElementType.isNull())
520 return QualType();
521
522 return Importer.getToContext().getExtVectorType(ToElementType,
523 T->getNumElements());
524}
525
John McCall424cec92011-01-19 06:33:43 +0000526QualType
527ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000528 // FIXME: What happens if we're importing a function without a prototype
529 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000530 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000531 if (ToResultType.isNull())
532 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000533
Douglas Gregor96e578d2010-02-05 17:54:41 +0000534 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000535 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000536}
537
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000538QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000539 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000540 if (ToResultType.isNull())
541 return QualType();
542
543 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000544 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000545 for (const auto &A : T->param_types()) {
546 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000547 if (ArgType.isNull())
548 return QualType();
549 ArgTypes.push_back(ArgType);
550 }
551
552 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000553 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000554 for (const auto &E : T->exceptions()) {
555 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000556 if (ExceptionType.isNull())
557 return QualType();
558 ExceptionTypes.push_back(ExceptionType);
559 }
John McCalldb40c7f2010-12-14 08:05:40 +0000560
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000561 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
562 FunctionProtoType::ExtProtoInfo ToEPI;
563
564 ToEPI.ExtInfo = FromEPI.ExtInfo;
565 ToEPI.Variadic = FromEPI.Variadic;
566 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
567 ToEPI.TypeQuals = FromEPI.TypeQuals;
568 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000569 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
570 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
571 ToEPI.ExceptionSpec.NoexceptExpr =
572 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
573 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
574 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
575 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
576 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000577
Jordan Rose5c382722013-03-08 21:51:21 +0000578 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000579}
580
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +0000581QualType ASTNodeImporter::VisitUnresolvedUsingType(
582 const UnresolvedUsingType *T) {
583 UnresolvedUsingTypenameDecl *ToD = cast_or_null<UnresolvedUsingTypenameDecl>(
584 Importer.Import(T->getDecl()));
585 if (!ToD)
586 return QualType();
587
588 UnresolvedUsingTypenameDecl *ToPrevD =
589 cast_or_null<UnresolvedUsingTypenameDecl>(
590 Importer.Import(T->getDecl()->getPreviousDecl()));
591 if (!ToPrevD && T->getDecl()->getPreviousDecl())
592 return QualType();
593
594 return Importer.getToContext().getTypeDeclType(ToD, ToPrevD);
595}
596
Sean Callananda6df8a2011-08-11 16:56:07 +0000597QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
598 QualType ToInnerType = Importer.Import(T->getInnerType());
599 if (ToInnerType.isNull())
600 return QualType();
601
602 return Importer.getToContext().getParenType(ToInnerType);
603}
604
John McCall424cec92011-01-19 06:33:43 +0000605QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000606 TypedefNameDecl *ToDecl
607 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000608 if (!ToDecl)
609 return QualType();
610
611 return Importer.getToContext().getTypeDeclType(ToDecl);
612}
613
John McCall424cec92011-01-19 06:33:43 +0000614QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000615 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
616 if (!ToExpr)
617 return QualType();
618
619 return Importer.getToContext().getTypeOfExprType(ToExpr);
620}
621
John McCall424cec92011-01-19 06:33:43 +0000622QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000623 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
624 if (ToUnderlyingType.isNull())
625 return QualType();
626
627 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
628}
629
John McCall424cec92011-01-19 06:33:43 +0000630QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000631 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000632 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
633 if (!ToExpr)
634 return QualType();
635
Douglas Gregor81495f32012-02-12 18:42:33 +0000636 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
637 if (UnderlyingType.isNull())
638 return QualType();
639
640 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000641}
642
Alexis Hunte852b102011-05-24 22:41:36 +0000643QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
644 QualType ToBaseType = Importer.Import(T->getBaseType());
645 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
646 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
647 return QualType();
648
649 return Importer.getToContext().getUnaryTransformType(ToBaseType,
650 ToUnderlyingType,
651 T->getUTTKind());
652}
653
Richard Smith30482bc2011-02-20 03:19:35 +0000654QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000655 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000656 QualType FromDeduced = T->getDeducedType();
657 QualType ToDeduced;
658 if (!FromDeduced.isNull()) {
659 ToDeduced = Importer.Import(FromDeduced);
660 if (ToDeduced.isNull())
661 return QualType();
662 }
663
Richard Smithe301ba22015-11-11 02:02:15 +0000664 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000665 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000666}
667
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000668QualType ASTNodeImporter::VisitInjectedClassNameType(
669 const InjectedClassNameType *T) {
670 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
671 if (!D)
672 return QualType();
673
674 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
675 if (InjType.isNull())
676 return QualType();
677
678 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
679 // See comments in InjectedClassNameType definition for details
680 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
681 enum {
682 TypeAlignmentInBits = 4,
683 TypeAlignment = 1 << TypeAlignmentInBits
684 };
685
686 return QualType(new (Importer.getToContext(), TypeAlignment)
687 InjectedClassNameType(D, InjType), 0);
688}
689
John McCall424cec92011-01-19 06:33:43 +0000690QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000691 RecordDecl *ToDecl
692 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
693 if (!ToDecl)
694 return QualType();
695
696 return Importer.getToContext().getTagDeclType(ToDecl);
697}
698
John McCall424cec92011-01-19 06:33:43 +0000699QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000700 EnumDecl *ToDecl
701 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
702 if (!ToDecl)
703 return QualType();
704
705 return Importer.getToContext().getTagDeclType(ToDecl);
706}
707
Sean Callanan72fe0852015-04-02 23:50:08 +0000708QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
709 QualType FromModifiedType = T->getModifiedType();
710 QualType FromEquivalentType = T->getEquivalentType();
711 QualType ToModifiedType;
712 QualType ToEquivalentType;
713
714 if (!FromModifiedType.isNull()) {
715 ToModifiedType = Importer.Import(FromModifiedType);
716 if (ToModifiedType.isNull())
717 return QualType();
718 }
719 if (!FromEquivalentType.isNull()) {
720 ToEquivalentType = Importer.Import(FromEquivalentType);
721 if (ToEquivalentType.isNull())
722 return QualType();
723 }
724
725 return Importer.getToContext().getAttributedType(T->getAttrKind(),
726 ToModifiedType, ToEquivalentType);
727}
728
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000729
730QualType ASTNodeImporter::VisitTemplateTypeParmType(
731 const TemplateTypeParmType *T) {
732 TemplateTypeParmDecl *ParmDecl =
733 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
734 if (!ParmDecl && T->getDecl())
735 return QualType();
736
737 return Importer.getToContext().getTemplateTypeParmType(
738 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
739}
740
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000741QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
742 const SubstTemplateTypeParmType *T) {
743 const TemplateTypeParmType *Replaced =
744 cast_or_null<TemplateTypeParmType>(Importer.Import(
745 QualType(T->getReplacedParameter(), 0)).getTypePtr());
746 if (!Replaced)
747 return QualType();
748
749 QualType Replacement = Importer.Import(T->getReplacementType());
750 if (Replacement.isNull())
751 return QualType();
752 Replacement = Replacement.getCanonicalType();
753
754 return Importer.getToContext().getSubstTemplateTypeParmType(
755 Replaced, Replacement);
756}
757
Douglas Gregore2e50d332010-12-01 01:36:18 +0000758QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000759 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000760 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
761 if (ToTemplate.isNull())
762 return QualType();
763
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000764 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000765 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
766 return QualType();
767
768 QualType ToCanonType;
769 if (!QualType(T, 0).isCanonical()) {
770 QualType FromCanonType
771 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
772 ToCanonType =Importer.Import(FromCanonType);
773 if (ToCanonType.isNull())
774 return QualType();
775 }
776 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000777 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000778 ToCanonType);
779}
780
John McCall424cec92011-01-19 06:33:43 +0000781QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000782 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000783 // Note: the qualifier in an ElaboratedType is optional.
784 if (T->getQualifier()) {
785 ToQualifier = Importer.Import(T->getQualifier());
786 if (!ToQualifier)
787 return QualType();
788 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000789
790 QualType ToNamedType = Importer.Import(T->getNamedType());
791 if (ToNamedType.isNull())
792 return QualType();
793
Abramo Bagnara6150c882010-05-11 21:36:43 +0000794 return Importer.getToContext().getElaboratedType(T->getKeyword(),
795 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000796}
797
Gabor Horvath7a91c082017-11-14 11:30:38 +0000798QualType ASTNodeImporter::VisitPackExpansionType(const PackExpansionType *T) {
799 QualType Pattern = Importer.Import(T->getPattern());
800 if (Pattern.isNull())
801 return QualType();
802
803 return Importer.getToContext().getPackExpansionType(Pattern,
804 T->getNumExpansions());
805}
806
John McCall424cec92011-01-19 06:33:43 +0000807QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000808 ObjCInterfaceDecl *Class
809 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
810 if (!Class)
811 return QualType();
812
John McCall8b07ec22010-05-15 11:32:37 +0000813 return Importer.getToContext().getObjCInterfaceType(Class);
814}
815
John McCall424cec92011-01-19 06:33:43 +0000816QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000817 QualType ToBaseType = Importer.Import(T->getBaseType());
818 if (ToBaseType.isNull())
819 return QualType();
820
Douglas Gregore9d95f12015-07-07 03:57:35 +0000821 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000822 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000823 QualType ImportedTypeArg = Importer.Import(TypeArg);
824 if (ImportedTypeArg.isNull())
825 return QualType();
826
827 TypeArgs.push_back(ImportedTypeArg);
828 }
829
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000830 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000831 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000832 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000833 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000834 if (!Protocol)
835 return QualType();
836 Protocols.push_back(Protocol);
837 }
838
Douglas Gregore9d95f12015-07-07 03:57:35 +0000839 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000840 Protocols,
841 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000842}
843
John McCall424cec92011-01-19 06:33:43 +0000844QualType
845ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000846 QualType ToPointeeType = Importer.Import(T->getPointeeType());
847 if (ToPointeeType.isNull())
848 return QualType();
849
John McCall8b07ec22010-05-15 11:32:37 +0000850 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000851}
852
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000853//----------------------------------------------------------------------------
854// Import Declarations
855//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000856bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
857 DeclContext *&LexicalDC,
858 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000859 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000860 SourceLocation &Loc) {
861 // Import the context of this declaration.
862 DC = Importer.ImportContext(D->getDeclContext());
863 if (!DC)
864 return true;
865
866 LexicalDC = DC;
867 if (D->getDeclContext() != D->getLexicalDeclContext()) {
868 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
869 if (!LexicalDC)
870 return true;
871 }
872
873 // Import the name of this declaration.
874 Name = Importer.Import(D->getDeclName());
875 if (D->getDeclName() && !Name)
876 return true;
877
878 // Import the location of this declaration.
879 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000880 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000881 return false;
882}
883
Douglas Gregord451ea92011-07-29 23:31:30 +0000884void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
885 if (!FromD)
886 return;
887
888 if (!ToD) {
889 ToD = Importer.Import(FromD);
890 if (!ToD)
891 return;
892 }
893
894 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
895 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000896 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000897 ImportDefinition(FromRecord, ToRecord);
898 }
899 }
900 return;
901 }
902
903 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
904 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
905 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
906 ImportDefinition(FromEnum, ToEnum);
907 }
908 }
909 return;
910 }
911}
912
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000913void
914ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
915 DeclarationNameInfo& To) {
916 // NOTE: To.Name and To.Loc are already imported.
917 // We only have to import To.LocInfo.
918 switch (To.getName().getNameKind()) {
919 case DeclarationName::Identifier:
920 case DeclarationName::ObjCZeroArgSelector:
921 case DeclarationName::ObjCOneArgSelector:
922 case DeclarationName::ObjCMultiArgSelector:
923 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000924 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000925 return;
926
927 case DeclarationName::CXXOperatorName: {
928 SourceRange Range = From.getCXXOperatorNameRange();
929 To.setCXXOperatorNameRange(Importer.Import(Range));
930 return;
931 }
932 case DeclarationName::CXXLiteralOperatorName: {
933 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
934 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
935 return;
936 }
937 case DeclarationName::CXXConstructorName:
938 case DeclarationName::CXXDestructorName:
939 case DeclarationName::CXXConversionFunctionName: {
940 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
941 To.setNamedTypeInfo(Importer.Import(FromTInfo));
942 return;
943 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000944 }
Douglas Gregor07216d12011-11-02 20:52:01 +0000945 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000946}
947
Douglas Gregor2e15c842012-02-01 21:00:38 +0000948void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +0000949 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +0000950 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +0000951 return;
952 }
953
Aaron Ballman629afae2014-03-07 19:56:05 +0000954 for (auto *From : FromDC->decls())
955 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +0000956}
957
Douglas Gregord451ea92011-07-29 23:31:30 +0000958bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000959 ImportDefinitionKind Kind) {
960 if (To->getDefinition() || To->isBeingDefined()) {
961 if (Kind == IDK_Everything)
962 ImportDeclContext(From, /*ForceImport=*/true);
963
Douglas Gregore2e50d332010-12-01 01:36:18 +0000964 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +0000965 }
Douglas Gregore2e50d332010-12-01 01:36:18 +0000966
967 To->startDefinition();
968
969 // Add base classes.
970 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
971 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000972
973 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
974 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
975 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000976 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000977 ToData.Aggregate = FromData.Aggregate;
978 ToData.PlainOldData = FromData.PlainOldData;
979 ToData.Empty = FromData.Empty;
980 ToData.Polymorphic = FromData.Polymorphic;
981 ToData.Abstract = FromData.Abstract;
982 ToData.IsStandardLayout = FromData.IsStandardLayout;
983 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
984 ToData.HasPrivateFields = FromData.HasPrivateFields;
985 ToData.HasProtectedFields = FromData.HasProtectedFields;
986 ToData.HasPublicFields = FromData.HasPublicFields;
987 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +0000988 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +0000989 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +0000990 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +0000991 ToData.HasUninitializedReferenceMember
992 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +0000993 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +0000994 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
995 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +0000996 ToData.NeedOverloadResolutionForCopyConstructor
997 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000998 ToData.NeedOverloadResolutionForMoveConstructor
999 = FromData.NeedOverloadResolutionForMoveConstructor;
1000 ToData.NeedOverloadResolutionForMoveAssignment
1001 = FromData.NeedOverloadResolutionForMoveAssignment;
1002 ToData.NeedOverloadResolutionForDestructor
1003 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001004 ToData.DefaultedCopyConstructorIsDeleted
1005 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +00001006 ToData.DefaultedMoveConstructorIsDeleted
1007 = FromData.DefaultedMoveConstructorIsDeleted;
1008 ToData.DefaultedMoveAssignmentIsDeleted
1009 = FromData.DefaultedMoveAssignmentIsDeleted;
1010 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001011 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1012 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001013 ToData.HasConstexprNonCopyMoveConstructor
1014 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00001015 ToData.HasDefaultedDefaultConstructor
1016 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +00001017 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +00001018 ToData.DefaultedDefaultConstructorIsConstexpr
1019 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001020 ToData.HasConstexprDefaultConstructor
1021 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001022 ToData.HasNonLiteralTypeFieldsOrBases
1023 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001024 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001025 ToData.UserProvidedDefaultConstructor
1026 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001027 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +00001028 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
1029 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
1030 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
1031 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +00001032 ToData.ImplicitCopyAssignmentHasConstParam
1033 = FromData.ImplicitCopyAssignmentHasConstParam;
1034 ToData.HasDeclaredCopyConstructorWithConstParam
1035 = FromData.HasDeclaredCopyConstructorWithConstParam;
1036 ToData.HasDeclaredCopyAssignmentWithConstParam
1037 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001038 ToData.IsLambda = FromData.IsLambda;
1039
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001040 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001041 for (const auto &Base1 : FromCXX->bases()) {
1042 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001043 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001044 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001045
1046 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001047 if (Base1.isPackExpansion())
1048 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001049
1050 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001051 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001052
Douglas Gregore2e50d332010-12-01 01:36:18 +00001053 Bases.push_back(
1054 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001055 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1056 Base1.isVirtual(),
1057 Base1.isBaseOfClass(),
1058 Base1.getAccessSpecifierAsWritten(),
1059 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001060 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001061 }
1062 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001063 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001064 }
1065
Douglas Gregor2e15c842012-02-01 21:00:38 +00001066 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001067 ImportDeclContext(From, /*ForceImport=*/true);
1068
Douglas Gregore2e50d332010-12-01 01:36:18 +00001069 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001070 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001071}
1072
Larisse Voufo39a1e502013-08-06 01:03:05 +00001073bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1074 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001075 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001076 return false;
1077
1078 // FIXME: Can we really import any initializer? Alternatively, we could force
1079 // ourselves to import every declaration of a variable and then only use
1080 // getInit() here.
1081 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1082
1083 // FIXME: Other bits to merge?
1084
1085 return false;
1086}
1087
Douglas Gregord451ea92011-07-29 23:31:30 +00001088bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001089 ImportDefinitionKind Kind) {
1090 if (To->getDefinition() || To->isBeingDefined()) {
1091 if (Kind == IDK_Everything)
1092 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001093 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001094 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001095
1096 To->startDefinition();
1097
1098 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1099 if (T.isNull())
1100 return true;
1101
1102 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1103 if (ToPromotionType.isNull())
1104 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001105
1106 if (shouldForceImportDeclContext(Kind))
1107 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001108
1109 // FIXME: we might need to merge the number of positive or negative bits
1110 // if the enumerator lists don't match.
1111 To->completeDefinition(T, ToPromotionType,
1112 From->getNumPositiveBits(),
1113 From->getNumNegativeBits());
1114 return false;
1115}
1116
Douglas Gregora082a492010-11-30 19:14:50 +00001117TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1118 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001119 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1120 if (ImportContainerChecked(*Params, ToParams))
1121 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001122
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001123 Expr *ToRequiresClause;
1124 if (Expr *const R = Params->getRequiresClause()) {
1125 ToRequiresClause = Importer.Import(R);
1126 if (!ToRequiresClause)
1127 return nullptr;
1128 } else {
1129 ToRequiresClause = nullptr;
1130 }
1131
Douglas Gregora082a492010-11-30 19:14:50 +00001132 return TemplateParameterList::Create(Importer.getToContext(),
1133 Importer.Import(Params->getTemplateLoc()),
1134 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001135 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001136 Importer.Import(Params->getRAngleLoc()),
1137 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001138}
1139
Douglas Gregore2e50d332010-12-01 01:36:18 +00001140TemplateArgument
1141ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1142 switch (From.getKind()) {
1143 case TemplateArgument::Null:
1144 return TemplateArgument();
1145
1146 case TemplateArgument::Type: {
1147 QualType ToType = Importer.Import(From.getAsType());
1148 if (ToType.isNull())
1149 return TemplateArgument();
1150 return TemplateArgument(ToType);
1151 }
1152
1153 case TemplateArgument::Integral: {
1154 QualType ToType = Importer.Import(From.getIntegralType());
1155 if (ToType.isNull())
1156 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001157 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001158 }
1159
Eli Friedmanb826a002012-09-26 02:36:12 +00001160 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001161 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1162 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1163 if (!To || ToType.isNull())
1164 return TemplateArgument();
1165 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001166 }
1167
1168 case TemplateArgument::NullPtr: {
1169 QualType ToType = Importer.Import(From.getNullPtrType());
1170 if (ToType.isNull())
1171 return TemplateArgument();
1172 return TemplateArgument(ToType, /*isNullPtr*/true);
1173 }
1174
Douglas Gregore2e50d332010-12-01 01:36:18 +00001175 case TemplateArgument::Template: {
1176 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1177 if (ToTemplate.isNull())
1178 return TemplateArgument();
1179
1180 return TemplateArgument(ToTemplate);
1181 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001182
1183 case TemplateArgument::TemplateExpansion: {
1184 TemplateName ToTemplate
1185 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1186 if (ToTemplate.isNull())
1187 return TemplateArgument();
1188
Douglas Gregore1d60df2011-01-14 23:41:42 +00001189 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001190 }
1191
Douglas Gregore2e50d332010-12-01 01:36:18 +00001192 case TemplateArgument::Expression:
1193 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1194 return TemplateArgument(ToExpr);
1195 return TemplateArgument();
1196
1197 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001199 ToPack.reserve(From.pack_size());
1200 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1201 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001202
1203 return TemplateArgument(
1204 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001205 }
1206 }
1207
1208 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001209}
1210
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001211Optional<TemplateArgumentLoc>
1212ASTNodeImporter::ImportTemplateArgumentLoc(const TemplateArgumentLoc &TALoc) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001213 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1214 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1215 TemplateArgumentLocInfo ToInfo;
1216 if (Arg.getKind() == TemplateArgument::Expression) {
1217 Expr *E = Importer.Import(FromInfo.getAsExpr());
1218 ToInfo = TemplateArgumentLocInfo(E);
1219 if (!E)
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001220 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001221 } else if (Arg.getKind() == TemplateArgument::Type) {
1222 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1223 ToInfo = TemplateArgumentLocInfo(TSI);
1224 else
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001225 return None;
Aleksei Sidorina693b372016-09-28 10:16:56 +00001226 } else {
1227 ToInfo = TemplateArgumentLocInfo(
1228 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1229 Importer.Import(FromInfo.getTemplateNameLoc()),
1230 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1231 }
1232 return TemplateArgumentLoc(Arg, ToInfo);
1233}
1234
Douglas Gregore2e50d332010-12-01 01:36:18 +00001235bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1236 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001237 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001238 for (unsigned I = 0; I != NumFromArgs; ++I) {
1239 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1240 if (To.isNull() && !FromArgs[I].isNull())
1241 return true;
1242
1243 ToArgs.push_back(To);
1244 }
1245
1246 return false;
1247}
1248
Douglas Gregor5c73e912010-02-11 00:48:18 +00001249bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001250 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001251 // Eliminate a potential failure point where we attempt to re-import
1252 // something we're trying to import while completing ToRecord.
1253 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1254 if (ToOrigin) {
1255 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1256 if (ToOriginRecord)
1257 ToRecord = ToOriginRecord;
1258 }
1259
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001260 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001261 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001262 Importer.getNonEquivalentDecls(),
1263 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001264 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001265}
1266
Larisse Voufo39a1e502013-08-06 01:03:05 +00001267bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1268 bool Complain) {
1269 StructuralEquivalenceContext Ctx(
1270 Importer.getFromContext(), Importer.getToContext(),
1271 Importer.getNonEquivalentDecls(), false, Complain);
1272 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1273}
1274
Douglas Gregor98c10182010-02-12 22:17:39 +00001275bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001276 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001277 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001278 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001279 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001280}
1281
Douglas Gregor91155082012-11-14 22:29:20 +00001282bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1283 EnumConstantDecl *ToEC)
1284{
1285 const llvm::APSInt &FromVal = FromEC->getInitVal();
1286 const llvm::APSInt &ToVal = ToEC->getInitVal();
1287
1288 return FromVal.isSigned() == ToVal.isSigned() &&
1289 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1290 FromVal == ToVal;
1291}
1292
1293bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001294 ClassTemplateDecl *To) {
1295 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1296 Importer.getToContext(),
1297 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001298 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001299}
1300
Larisse Voufo39a1e502013-08-06 01:03:05 +00001301bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1302 VarTemplateDecl *To) {
1303 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1304 Importer.getToContext(),
1305 Importer.getNonEquivalentDecls());
1306 return Ctx.IsStructurallyEquivalent(From, To);
1307}
1308
Douglas Gregore4c83e42010-02-09 22:48:33 +00001309Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001310 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001311 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001312 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001313}
1314
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001315Decl *ASTNodeImporter::VisitEmptyDecl(EmptyDecl *D) {
1316 // Import the context of this declaration.
1317 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1318 if (!DC)
1319 return nullptr;
1320
1321 DeclContext *LexicalDC = DC;
1322 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1323 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1324 if (!LexicalDC)
1325 return nullptr;
1326 }
1327
1328 // Import the location of this declaration.
1329 SourceLocation Loc = Importer.Import(D->getLocation());
1330
1331 EmptyDecl *ToD = EmptyDecl::Create(Importer.getToContext(), DC, Loc);
1332 ToD->setLexicalDeclContext(LexicalDC);
1333 Importer.Imported(D, ToD);
1334 LexicalDC->addDeclInternal(ToD);
1335 return ToD;
1336}
1337
Sean Callanan65198272011-11-17 23:20:56 +00001338Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1339 TranslationUnitDecl *ToD =
1340 Importer.getToContext().getTranslationUnitDecl();
1341
1342 Importer.Imported(D, ToD);
1343
1344 return ToD;
1345}
1346
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001347Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1348
1349 SourceLocation Loc = Importer.Import(D->getLocation());
1350 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1351
1352 // Import the context of this declaration.
1353 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1354 if (!DC)
1355 return nullptr;
1356
1357 AccessSpecDecl *accessSpecDecl
1358 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1359 DC, Loc, ColonLoc);
1360
1361 if (!accessSpecDecl)
1362 return nullptr;
1363
1364 // Lexical DeclContext and Semantic DeclContext
1365 // is always the same for the accessSpec.
1366 accessSpecDecl->setLexicalDeclContext(DC);
1367 DC->addDeclInternal(accessSpecDecl);
1368
1369 return accessSpecDecl;
1370}
1371
Aleksei Sidorina693b372016-09-28 10:16:56 +00001372Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1373 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1374 if (!DC)
1375 return nullptr;
1376
1377 DeclContext *LexicalDC = DC;
1378
1379 // Import the location of this declaration.
1380 SourceLocation Loc = Importer.Import(D->getLocation());
1381
1382 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1383 if (!AssertExpr)
1384 return nullptr;
1385
1386 StringLiteral *FromMsg = D->getMessage();
1387 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1388 if (!ToMsg && FromMsg)
1389 return nullptr;
1390
1391 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1392 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1393 Importer.Import(D->getRParenLoc()), D->isFailed());
1394
1395 ToD->setLexicalDeclContext(LexicalDC);
1396 LexicalDC->addDeclInternal(ToD);
1397 Importer.Imported(D, ToD);
1398 return ToD;
1399}
1400
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001401Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1402 // Import the major distinguishing characteristics of this namespace.
1403 DeclContext *DC, *LexicalDC;
1404 DeclarationName Name;
1405 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001406 NamedDecl *ToD;
1407 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001408 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001409 if (ToD)
1410 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001411
1412 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001413 if (!Name) {
1414 // This is an anonymous namespace. Adopt an existing anonymous
1415 // namespace if we can.
1416 // FIXME: Not testable.
1417 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1418 MergeWithNamespace = TU->getAnonymousNamespace();
1419 else
1420 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1421 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001422 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001423 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001424 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001425 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1426 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001427 continue;
1428
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001429 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001430 MergeWithNamespace = FoundNS;
1431 ConflictingDecls.clear();
1432 break;
1433 }
1434
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001435 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001436 }
1437
1438 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001439 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001440 ConflictingDecls.data(),
1441 ConflictingDecls.size());
1442 }
1443 }
1444
1445 // Create the "to" namespace, if needed.
1446 NamespaceDecl *ToNamespace = MergeWithNamespace;
1447 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001448 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001449 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001450 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001451 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001452 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001453 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001454 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001455
1456 // If this is an anonymous namespace, register it as the anonymous
1457 // namespace within its context.
1458 if (!Name) {
1459 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1460 TU->setAnonymousNamespace(ToNamespace);
1461 else
1462 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1463 }
1464 }
1465 Importer.Imported(D, ToNamespace);
1466
1467 ImportDeclContext(D);
1468
1469 return ToNamespace;
1470}
1471
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001472Decl *ASTNodeImporter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1473 // Import the major distinguishing characteristics of this namespace.
1474 DeclContext *DC, *LexicalDC;
1475 DeclarationName Name;
1476 SourceLocation Loc;
1477 NamedDecl *LookupD;
1478 if (ImportDeclParts(D, DC, LexicalDC, Name, LookupD, Loc))
1479 return nullptr;
1480 if (LookupD)
1481 return LookupD;
1482
1483 // NOTE: No conflict resolution is done for namespace aliases now.
1484
1485 NamespaceDecl *TargetDecl = cast_or_null<NamespaceDecl>(
1486 Importer.Import(D->getNamespace()));
1487 if (!TargetDecl)
1488 return nullptr;
1489
1490 IdentifierInfo *ToII = Importer.Import(D->getIdentifier());
1491 if (!ToII)
1492 return nullptr;
1493
1494 NestedNameSpecifierLoc ToQLoc = Importer.Import(D->getQualifierLoc());
1495 if (D->getQualifierLoc() && !ToQLoc)
1496 return nullptr;
1497
1498 NamespaceAliasDecl *ToD = NamespaceAliasDecl::Create(
1499 Importer.getToContext(), DC, Importer.Import(D->getNamespaceLoc()),
1500 Importer.Import(D->getAliasLoc()), ToII, ToQLoc,
1501 Importer.Import(D->getTargetNameLoc()), TargetDecl);
1502
1503 ToD->setLexicalDeclContext(LexicalDC);
1504 Importer.Imported(D, ToD);
1505 LexicalDC->addDeclInternal(ToD);
1506
1507 return ToD;
1508}
1509
Richard Smithdda56e42011-04-15 14:24:37 +00001510Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001511 // Import the major distinguishing characteristics of this typedef.
1512 DeclContext *DC, *LexicalDC;
1513 DeclarationName Name;
1514 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001515 NamedDecl *ToD;
1516 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001517 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001518 if (ToD)
1519 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001520
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001521 // If this typedef is not in block scope, determine whether we've
1522 // seen a typedef with the same name (that we can merge with) or any
1523 // other entity by that name (which name lookup could conflict with).
1524 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001525 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001526 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001527 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001528 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001529 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1530 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001531 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001532 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001533 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001534 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1535 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001536 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001537 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001538
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001539 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001540 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001541
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001542 if (!ConflictingDecls.empty()) {
1543 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1544 ConflictingDecls.data(),
1545 ConflictingDecls.size());
1546 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001547 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001548 }
1549 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001550
Douglas Gregorb4964f72010-02-15 23:54:17 +00001551 // Import the underlying type of this typedef;
1552 QualType T = Importer.Import(D->getUnderlyingType());
1553 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001554 return nullptr;
1555
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001556 // Create the new typedef node.
1557 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001558 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001559 TypedefNameDecl *ToTypedef;
1560 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001561 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1562 StartL, Loc,
1563 Name.getAsIdentifierInfo(),
1564 TInfo);
1565 else
Richard Smithdda56e42011-04-15 14:24:37 +00001566 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1567 StartL, Loc,
1568 Name.getAsIdentifierInfo(),
1569 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001570
Douglas Gregordd483172010-02-22 17:42:47 +00001571 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001572 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001573 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001574 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001575
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001576 return ToTypedef;
1577}
1578
Richard Smithdda56e42011-04-15 14:24:37 +00001579Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1580 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1581}
1582
1583Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1584 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1585}
1586
Gabor Horvath7a91c082017-11-14 11:30:38 +00001587Decl *ASTNodeImporter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1588 // Import the major distinguishing characteristics of this typedef.
1589 DeclContext *DC, *LexicalDC;
1590 DeclarationName Name;
1591 SourceLocation Loc;
1592 NamedDecl *ToD;
1593 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1594 return nullptr;
1595 if (ToD)
1596 return ToD;
1597
1598 // If this typedef is not in block scope, determine whether we've
1599 // seen a typedef with the same name (that we can merge with) or any
1600 // other entity by that name (which name lookup could conflict with).
1601 if (!DC->isFunctionOrMethod()) {
1602 SmallVector<NamedDecl *, 4> ConflictingDecls;
1603 unsigned IDNS = Decl::IDNS_Ordinary;
1604 SmallVector<NamedDecl *, 2> FoundDecls;
1605 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
1606 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1607 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
1608 continue;
1609 if (auto *FoundAlias =
1610 dyn_cast<TypeAliasTemplateDecl>(FoundDecls[I]))
1611 return Importer.Imported(D, FoundAlias);
1612 ConflictingDecls.push_back(FoundDecls[I]);
1613 }
1614
1615 if (!ConflictingDecls.empty()) {
1616 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1617 ConflictingDecls.data(),
1618 ConflictingDecls.size());
1619 if (!Name)
1620 return nullptr;
1621 }
1622 }
1623
1624 TemplateParameterList *Params = ImportTemplateParameterList(
1625 D->getTemplateParameters());
1626 if (!Params)
1627 return nullptr;
1628
1629 NamedDecl *TemplDecl = cast_or_null<NamedDecl>(
1630 Importer.Import(D->getTemplatedDecl()));
1631 if (!TemplDecl)
1632 return nullptr;
1633
1634 TypeAliasTemplateDecl *ToAlias = TypeAliasTemplateDecl::Create(
1635 Importer.getToContext(), DC, Loc, Name, Params, TemplDecl);
1636
1637 ToAlias->setAccess(D->getAccess());
1638 ToAlias->setLexicalDeclContext(LexicalDC);
1639 Importer.Imported(D, ToAlias);
1640 LexicalDC->addDeclInternal(ToAlias);
1641 return ToD;
1642}
1643
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001644Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1645 // Import the major distinguishing characteristics of this label.
1646 DeclContext *DC, *LexicalDC;
1647 DeclarationName Name;
1648 SourceLocation Loc;
1649 NamedDecl *ToD;
1650 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1651 return nullptr;
1652 if (ToD)
1653 return ToD;
1654
1655 assert(LexicalDC->isFunctionOrMethod());
1656
1657 LabelDecl *ToLabel = D->isGnuLocal()
1658 ? LabelDecl::Create(Importer.getToContext(),
1659 DC, Importer.Import(D->getLocation()),
1660 Name.getAsIdentifierInfo(),
1661 Importer.Import(D->getLocStart()))
1662 : LabelDecl::Create(Importer.getToContext(),
1663 DC, Importer.Import(D->getLocation()),
1664 Name.getAsIdentifierInfo());
1665 Importer.Imported(D, ToLabel);
1666
1667 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1668 if (!Label)
1669 return nullptr;
1670
1671 ToLabel->setStmt(Label);
1672 ToLabel->setLexicalDeclContext(LexicalDC);
1673 LexicalDC->addDeclInternal(ToLabel);
1674 return ToLabel;
1675}
1676
Douglas Gregor98c10182010-02-12 22:17:39 +00001677Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1678 // Import the major distinguishing characteristics of this enum.
1679 DeclContext *DC, *LexicalDC;
1680 DeclarationName Name;
1681 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001682 NamedDecl *ToD;
1683 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001684 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001685 if (ToD)
1686 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001687
Douglas Gregor98c10182010-02-12 22:17:39 +00001688 // Figure out what enum name we're looking for.
1689 unsigned IDNS = Decl::IDNS_Tag;
1690 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001691 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1692 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001693 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001694 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001695 IDNS |= Decl::IDNS_Ordinary;
1696
1697 // We may already have an enum of the same name; try to find and match it.
1698 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001699 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001700 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001701 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001702 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1703 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001704 continue;
1705
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001706 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001707 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001708 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1709 Found = Tag->getDecl();
1710 }
1711
1712 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001713 if (IsStructuralMatch(D, FoundEnum))
1714 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001715 }
1716
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001717 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001718 }
1719
1720 if (!ConflictingDecls.empty()) {
1721 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1722 ConflictingDecls.data(),
1723 ConflictingDecls.size());
1724 }
1725 }
1726
1727 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001728 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1729 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001730 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001731 D->isScoped(), D->isScopedUsingClassTag(),
1732 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001733 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001734 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001735 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001736 D2->setLexicalDeclContext(LexicalDC);
1737 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001738 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001739
1740 // Import the integer type.
1741 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1742 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001743 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001744 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001745
1746 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001747 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001748 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001749
Douglas Gregor3996e242010-02-15 22:01:00 +00001750 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001751}
1752
Douglas Gregor5c73e912010-02-11 00:48:18 +00001753Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1754 // If this record has a definition in the translation unit we're coming from,
1755 // but this particular declaration is not that definition, import the
1756 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001757 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001758 if (Definition && Definition != D) {
1759 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001760 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001761 return nullptr;
1762
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001763 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001764 }
1765
1766 // Import the major distinguishing characteristics of this record.
1767 DeclContext *DC, *LexicalDC;
1768 DeclarationName Name;
1769 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001770 NamedDecl *ToD;
1771 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001772 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001773 if (ToD)
1774 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001775
Douglas Gregor5c73e912010-02-11 00:48:18 +00001776 // Figure out what structure name we're looking for.
1777 unsigned IDNS = Decl::IDNS_Tag;
1778 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001779 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1780 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001781 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001782 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001783 IDNS |= Decl::IDNS_Ordinary;
1784
1785 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001786 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001787 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001788 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001789 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001790 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001791 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001792
1793 if (!FoundDecls.empty()) {
1794 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1795 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1796 D->getASTContext().getExternalSource()->CompleteType(D);
1797 }
1798
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001799 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1800 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001801 continue;
1802
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001803 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001804 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001805 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1806 Found = Tag->getDecl();
1807 }
1808
1809 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001810 if (D->isAnonymousStructOrUnion() &&
1811 FoundRecord->isAnonymousStructOrUnion()) {
1812 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001813 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001814 if (Optional<unsigned> Index1 =
1815 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1816 D)) {
1817 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001818 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001819 if (*Index1 != *Index2)
1820 continue;
1821 }
1822 }
1823 }
1824
Sean Callanan9092d472017-05-13 00:46:33 +00001825 PrevDecl = FoundRecord;
1826
Douglas Gregor25791052010-02-12 00:09:27 +00001827 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001828 if ((SearchName && !D->isCompleteDefinition())
1829 || (D->isCompleteDefinition() &&
1830 D->isAnonymousStructOrUnion()
1831 == FoundDef->isAnonymousStructOrUnion() &&
1832 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001833 // The record types structurally match, or the "from" translation
1834 // unit only had a forward declaration anyway; call it the same
1835 // function.
1836 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001837 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001838 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001839 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001840 // We have a forward declaration of this type, so adopt that forward
1841 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001842
1843 // If one or both can be completed from external storage then try one
1844 // last time to complete and compare them before doing this.
1845
1846 if (FoundRecord->hasExternalLexicalStorage() &&
1847 !FoundRecord->isCompleteDefinition())
1848 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1849 if (D->hasExternalLexicalStorage())
1850 D->getASTContext().getExternalSource()->CompleteType(D);
1851
1852 if (FoundRecord->isCompleteDefinition() &&
1853 D->isCompleteDefinition() &&
1854 !IsStructuralMatch(D, FoundRecord))
1855 continue;
1856
Douglas Gregor25791052010-02-12 00:09:27 +00001857 AdoptDecl = FoundRecord;
1858 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001859 } else if (!SearchName) {
1860 continue;
1861 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001862 }
1863
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001864 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001865 }
1866
Douglas Gregordd6006f2012-07-17 21:16:27 +00001867 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001868 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1869 ConflictingDecls.data(),
1870 ConflictingDecls.size());
1871 }
1872 }
1873
1874 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001875 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001876 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001877 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001878 CXXRecordDecl *D2CXX = nullptr;
1879 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1880 if (DCXX->isLambda()) {
1881 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1882 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1883 DC, TInfo, Loc,
1884 DCXX->isDependentLambda(),
1885 DCXX->isGenericLambda(),
1886 DCXX->getLambdaCaptureDefault());
1887 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1888 if (DCXX->getLambdaContextDecl() && !CDecl)
1889 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001890 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1891 } else if (DCXX->isInjectedClassName()) {
1892 // We have to be careful to do a similar dance to the one in
1893 // Sema::ActOnStartCXXMemberDeclarations
1894 CXXRecordDecl *const PrevDecl = nullptr;
1895 const bool DelayTypeCreation = true;
1896 D2CXX = CXXRecordDecl::Create(
1897 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1898 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1899 Importer.getToContext().getTypeDeclType(
1900 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001901 } else {
1902 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1903 D->getTagKind(),
1904 DC, StartLoc, Loc,
1905 Name.getAsIdentifierInfo());
1906 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001907 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001908 D2->setAccess(D->getAccess());
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00001909
1910 Importer.Imported(D, D2);
1911
1912 if (ClassTemplateDecl *FromDescribed =
1913 DCXX->getDescribedClassTemplate()) {
1914 ClassTemplateDecl *ToDescribed = cast_or_null<ClassTemplateDecl>(
1915 Importer.Import(FromDescribed));
1916 if (!ToDescribed)
1917 return nullptr;
1918 D2CXX->setDescribedClassTemplate(ToDescribed);
1919
1920 } else if (MemberSpecializationInfo *MemberInfo =
1921 DCXX->getMemberSpecializationInfo()) {
1922 TemplateSpecializationKind SK =
1923 MemberInfo->getTemplateSpecializationKind();
1924 CXXRecordDecl *FromInst = DCXX->getInstantiatedFromMemberClass();
1925 CXXRecordDecl *ToInst =
1926 cast_or_null<CXXRecordDecl>(Importer.Import(FromInst));
1927 if (FromInst && !ToInst)
1928 return nullptr;
1929 D2CXX->setInstantiationOfMemberClass(ToInst, SK);
1930 D2CXX->getMemberSpecializationInfo()->setPointOfInstantiation(
1931 Importer.Import(MemberInfo->getPointOfInstantiation()));
1932 }
1933
Douglas Gregor25791052010-02-12 00:09:27 +00001934 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001935 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001936 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001937 }
Douglas Gregor14454802011-02-25 02:25:35 +00001938
1939 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001940 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001941 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00001942 if (D->isAnonymousStructOrUnion())
1943 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00001944 if (PrevDecl) {
1945 // FIXME: do this for all Redeclarables, not just RecordDecls.
1946 D2->setPreviousDecl(PrevDecl);
1947 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001948 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001949
Douglas Gregor3996e242010-02-15 22:01:00 +00001950 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001951
Douglas Gregor95d82832012-01-24 18:36:04 +00001952 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00001953 return nullptr;
1954
Douglas Gregor3996e242010-02-15 22:01:00 +00001955 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001956}
1957
Douglas Gregor98c10182010-02-12 22:17:39 +00001958Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1959 // Import the major distinguishing characteristics of this enumerator.
1960 DeclContext *DC, *LexicalDC;
1961 DeclarationName Name;
1962 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001963 NamedDecl *ToD;
1964 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001965 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001966 if (ToD)
1967 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001968
1969 QualType T = Importer.Import(D->getType());
1970 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001971 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001972
Douglas Gregor98c10182010-02-12 22:17:39 +00001973 // Determine whether there are any other declarations with the same name and
1974 // in the same context.
1975 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001976 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00001977 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001978 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001979 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001980 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1981 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001982 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00001983
1984 if (EnumConstantDecl *FoundEnumConstant
1985 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
1986 if (IsStructuralMatch(D, FoundEnumConstant))
1987 return Importer.Imported(D, FoundEnumConstant);
1988 }
1989
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001990 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001991 }
1992
1993 if (!ConflictingDecls.empty()) {
1994 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1995 ConflictingDecls.data(),
1996 ConflictingDecls.size());
1997 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001998 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001999 }
2000 }
2001
2002 Expr *Init = Importer.Import(D->getInitExpr());
2003 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002004 return nullptr;
2005
Douglas Gregor98c10182010-02-12 22:17:39 +00002006 EnumConstantDecl *ToEnumerator
2007 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2008 Name.getAsIdentifierInfo(), T,
2009 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002010 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002011 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002012 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002013 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002014 return ToEnumerator;
2015}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002016
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002017Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2018 // Import the major distinguishing characteristics of this function.
2019 DeclContext *DC, *LexicalDC;
2020 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002021 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002022 NamedDecl *ToD;
2023 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002024 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002025 if (ToD)
2026 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002027
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002028 const FunctionDecl *FoundWithoutBody = nullptr;
2029
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002030 // Try to find a function in our own ("to") context with the same name, same
2031 // type, and in the same context as the function we're importing.
2032 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002033 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002034 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002035 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002036 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002037 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2038 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002039 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002040
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002041 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002042 if (FoundFunction->hasExternalFormalLinkage() &&
2043 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002044 if (Importer.IsStructurallyEquivalent(D->getType(),
2045 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002046 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002047 const FunctionDecl *FromBodyDecl = nullptr;
2048 D->hasBody(FromBodyDecl);
2049 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
2050 // This function is needed to merge completely.
2051 FoundWithoutBody = FoundFunction;
2052 break;
2053 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002054 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002055 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002056
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002057 // FIXME: Check for overloading more carefully, e.g., by boosting
2058 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002059
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002060 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002061 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002062 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002063
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002064 // Complain about inconsistent function types.
2065 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002066 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002067 Importer.ToDiag(FoundFunction->getLocation(),
2068 diag::note_odr_value_here)
2069 << FoundFunction->getType();
2070 }
2071 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002072
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002073 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002074 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002075
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002076 if (!ConflictingDecls.empty()) {
2077 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2078 ConflictingDecls.data(),
2079 ConflictingDecls.size());
2080 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002081 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002082 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002083 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002084
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002085 DeclarationNameInfo NameInfo(Name, Loc);
2086 // Import additional name location/type info.
2087 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2088
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002089 QualType FromTy = D->getType();
2090 bool usedDifferentExceptionSpec = false;
2091
2092 if (const FunctionProtoType *
2093 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2094 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2095 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2096 // FunctionDecl that we are importing the FunctionProtoType for.
2097 // To avoid an infinite recursion when importing, create the FunctionDecl
2098 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002099 if (FromEPI.ExceptionSpec.SourceDecl ||
2100 FromEPI.ExceptionSpec.SourceTemplate ||
2101 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002102 FunctionProtoType::ExtProtoInfo DefaultEPI;
2103 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002104 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002105 usedDifferentExceptionSpec = true;
2106 }
2107 }
2108
Douglas Gregorb4964f72010-02-15 23:54:17 +00002109 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002110 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002111 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002112 return nullptr;
2113
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002114 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002115 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00002116 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002117 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002118 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002119 return nullptr;
2120
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002121 Parameters.push_back(ToP);
2122 }
2123
2124 // Create the imported function.
2125 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002126 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002127 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002128 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2129 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2130 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002131 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002132 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002133 FromConstructor->isExplicit(),
2134 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002135 D->isImplicit(),
2136 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00002137 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
2138 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
2139 for (CXXCtorInitializer *I : FromConstructor->inits()) {
2140 CXXCtorInitializer *ToI =
2141 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
2142 if (!ToI && I)
2143 return nullptr;
2144 CtorInitializers.push_back(ToI);
2145 }
2146 CXXCtorInitializer **Memory =
2147 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
2148 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
2149 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
2150 ToCtor->setCtorInitializers(Memory);
2151 ToCtor->setNumCtorInitializers(NumInitializers);
2152 }
Douglas Gregor00eace12010-02-21 18:29:16 +00002153 } else if (isa<CXXDestructorDecl>(D)) {
2154 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2155 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002156 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002157 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002158 D->isInlineSpecified(),
2159 D->isImplicit());
2160 } else if (CXXConversionDecl *FromConversion
2161 = dyn_cast<CXXConversionDecl>(D)) {
2162 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2163 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002164 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002165 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002166 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002167 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002168 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002169 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002170 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2171 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2172 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002173 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002174 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002175 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002176 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002177 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002178 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002179 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002180 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002181 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002182 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002183 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002184 D->hasWrittenPrototype(),
2185 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002186 }
John McCall3e11ebe2010-03-15 10:12:16 +00002187
2188 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002189 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002190 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002191 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002192 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2193 ToFunction->setTrivial(D->isTrivial());
2194 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002195 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002196
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002197 // Set the parameters.
2198 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002199 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002200 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002201 }
David Blaikie9c70e042011-09-21 18:16:56 +00002202 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002203
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002204 if (FoundWithoutBody) {
2205 auto *Recent = const_cast<FunctionDecl *>(
2206 FoundWithoutBody->getMostRecentDecl());
2207 ToFunction->setPreviousDecl(Recent);
2208 }
2209
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002210 if (usedDifferentExceptionSpec) {
2211 // Update FunctionProtoType::ExtProtoInfo.
2212 QualType T = Importer.Import(D->getType());
2213 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002214 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002215 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002216 }
2217
Sean Callanan59721b32015-04-28 18:41:46 +00002218 // Import the body, if any.
2219 if (Stmt *FromBody = D->getBody()) {
2220 if (Stmt *ToBody = Importer.Import(FromBody)) {
2221 ToFunction->setBody(ToBody);
2222 }
2223 }
2224
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002225 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002226
2227 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002228 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002229
Lang Hames19e07e12017-06-20 21:06:00 +00002230 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2231 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2232
Douglas Gregor43f54792010-02-17 02:12:47 +00002233 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002234}
2235
Douglas Gregor00eace12010-02-21 18:29:16 +00002236Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2237 return VisitFunctionDecl(D);
2238}
2239
2240Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2241 return VisitCXXMethodDecl(D);
2242}
2243
2244Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2245 return VisitCXXMethodDecl(D);
2246}
2247
2248Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2249 return VisitCXXMethodDecl(D);
2250}
2251
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002252static unsigned getFieldIndex(Decl *F) {
2253 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2254 if (!Owner)
2255 return 0;
2256
2257 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002258 for (const auto *D : Owner->noload_decls()) {
2259 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002260 return Index;
2261
2262 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2263 ++Index;
2264 }
2265
2266 return Index;
2267}
2268
Douglas Gregor5c73e912010-02-11 00:48:18 +00002269Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2270 // Import the major distinguishing characteristics of a variable.
2271 DeclContext *DC, *LexicalDC;
2272 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002273 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002274 NamedDecl *ToD;
2275 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002276 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002277 if (ToD)
2278 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002279
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002280 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002281 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002282 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002283 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2284 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002285 // For anonymous fields, match up by index.
2286 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2287 continue;
2288
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002289 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002290 FoundField->getType())) {
2291 Importer.Imported(D, FoundField);
2292 return FoundField;
2293 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002294
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002295 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2296 << Name << D->getType() << FoundField->getType();
2297 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2298 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002299 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002300 }
2301 }
2302
Douglas Gregorb4964f72010-02-15 23:54:17 +00002303 // Import the type.
2304 QualType T = Importer.Import(D->getType());
2305 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002306 return nullptr;
2307
Douglas Gregor5c73e912010-02-11 00:48:18 +00002308 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2309 Expr *BitWidth = Importer.Import(D->getBitWidth());
2310 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002311 return nullptr;
2312
Abramo Bagnaradff19302011-03-08 08:55:46 +00002313 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2314 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002315 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002316 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002317 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002318 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002319 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002320 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002321 Expr *ToInitializer = Importer.Import(FromInitializer);
2322 if (ToInitializer)
2323 ToField->setInClassInitializer(ToInitializer);
2324 else
2325 return nullptr;
2326 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002327 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002328 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002329 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002330 return ToField;
2331}
2332
Francois Pichet783dd6e2010-11-21 06:08:52 +00002333Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2334 // Import the major distinguishing characteristics of a variable.
2335 DeclContext *DC, *LexicalDC;
2336 DeclarationName Name;
2337 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002338 NamedDecl *ToD;
2339 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002340 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002341 if (ToD)
2342 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002343
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002344 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002345 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002346 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002347 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002348 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002349 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002350 // For anonymous indirect fields, match up by index.
2351 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2352 continue;
2353
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002354 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002355 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002356 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002357 Importer.Imported(D, FoundField);
2358 return FoundField;
2359 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002360
2361 // If there are more anonymous fields to check, continue.
2362 if (!Name && I < N-1)
2363 continue;
2364
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002365 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2366 << Name << D->getType() << FoundField->getType();
2367 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2368 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002369 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002370 }
2371 }
2372
Francois Pichet783dd6e2010-11-21 06:08:52 +00002373 // Import the type.
2374 QualType T = Importer.Import(D->getType());
2375 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002376 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002377
2378 NamedDecl **NamedChain =
2379 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2380
2381 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002382 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002383 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002384 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002385 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002386 NamedChain[i++] = cast<NamedDecl>(D);
2387 }
2388
2389 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002390 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002391 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002392
2393 for (const auto *Attr : D->attrs())
2394 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2395
Francois Pichet783dd6e2010-11-21 06:08:52 +00002396 ToIndirectField->setAccess(D->getAccess());
2397 ToIndirectField->setLexicalDeclContext(LexicalDC);
2398 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002399 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002400 return ToIndirectField;
2401}
2402
Aleksei Sidorina693b372016-09-28 10:16:56 +00002403Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2404 // Import the major distinguishing characteristics of a declaration.
2405 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2406 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2407 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2408 if (!DC || !LexicalDC)
2409 return nullptr;
2410
2411 // Determine whether we've already imported this decl.
2412 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2413 auto *RD = cast<CXXRecordDecl>(DC);
2414 FriendDecl *ImportedFriend = RD->getFirstFriend();
2415 StructuralEquivalenceContext Context(
2416 Importer.getFromContext(), Importer.getToContext(),
2417 Importer.getNonEquivalentDecls(), false, false);
2418
2419 while (ImportedFriend) {
2420 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2421 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2422 ImportedFriend->getFriendDecl()))
2423 return Importer.Imported(D, ImportedFriend);
2424
2425 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2426 if (Importer.IsStructurallyEquivalent(
2427 D->getFriendType()->getType(),
2428 ImportedFriend->getFriendType()->getType(), true))
2429 return Importer.Imported(D, ImportedFriend);
2430 }
2431 ImportedFriend = ImportedFriend->getNextFriend();
2432 }
2433
2434 // Not found. Create it.
2435 FriendDecl::FriendUnion ToFU;
2436 if (NamedDecl *FriendD = D->getFriendDecl())
2437 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2438 else
2439 ToFU = Importer.Import(D->getFriendType());
2440 if (!ToFU)
2441 return nullptr;
2442
2443 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2444 TemplateParameterList **FromTPLists =
2445 D->getTrailingObjects<TemplateParameterList *>();
2446 for (unsigned I = 0; I < D->NumTPLists; I++) {
2447 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2448 if (!List)
2449 return nullptr;
2450 ToTPLists[I] = List;
2451 }
2452
2453 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2454 Importer.Import(D->getLocation()),
2455 ToFU, Importer.Import(D->getFriendLoc()),
2456 ToTPLists);
2457
2458 Importer.Imported(D, FrD);
2459 RD->pushFriendDecl(FrD);
2460
2461 FrD->setAccess(D->getAccess());
2462 FrD->setLexicalDeclContext(LexicalDC);
2463 LexicalDC->addDeclInternal(FrD);
2464 return FrD;
2465}
2466
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002467Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2468 // Import the major distinguishing characteristics of an ivar.
2469 DeclContext *DC, *LexicalDC;
2470 DeclarationName Name;
2471 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002472 NamedDecl *ToD;
2473 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002474 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002475 if (ToD)
2476 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002477
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002478 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002479 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002480 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002481 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2482 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002483 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002484 FoundIvar->getType())) {
2485 Importer.Imported(D, FoundIvar);
2486 return FoundIvar;
2487 }
2488
2489 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2490 << Name << D->getType() << FoundIvar->getType();
2491 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2492 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002493 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002494 }
2495 }
2496
2497 // Import the type.
2498 QualType T = Importer.Import(D->getType());
2499 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002500 return nullptr;
2501
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002502 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2503 Expr *BitWidth = Importer.Import(D->getBitWidth());
2504 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002505 return nullptr;
2506
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002507 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2508 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002509 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002510 Loc, Name.getAsIdentifierInfo(),
2511 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002512 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002513 ToIvar->setLexicalDeclContext(LexicalDC);
2514 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002515 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002516 return ToIvar;
2517
2518}
2519
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002520Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2521 // Import the major distinguishing characteristics of a variable.
2522 DeclContext *DC, *LexicalDC;
2523 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002524 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002525 NamedDecl *ToD;
2526 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002527 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002528 if (ToD)
2529 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002530
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002531 // Try to find a variable in our own ("to") context with the same name and
2532 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002533 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002534 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002535 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002536 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002537 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002538 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002539 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2540 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002541 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002542
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002543 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002544 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002545 if (FoundVar->hasExternalFormalLinkage() &&
2546 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002547 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002548 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002549 MergeWithVar = FoundVar;
2550 break;
2551 }
2552
Douglas Gregor56521c52010-02-12 17:23:39 +00002553 const ArrayType *FoundArray
2554 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2555 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002556 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002557 if (FoundArray && TArray) {
2558 if (isa<IncompleteArrayType>(FoundArray) &&
2559 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002560 // Import the type.
2561 QualType T = Importer.Import(D->getType());
2562 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002563 return nullptr;
2564
Douglas Gregor56521c52010-02-12 17:23:39 +00002565 FoundVar->setType(T);
2566 MergeWithVar = FoundVar;
2567 break;
2568 } else if (isa<IncompleteArrayType>(TArray) &&
2569 isa<ConstantArrayType>(FoundArray)) {
2570 MergeWithVar = FoundVar;
2571 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002572 }
2573 }
2574
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002575 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002576 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002577 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2578 << FoundVar->getType();
2579 }
2580 }
2581
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002582 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002583 }
2584
2585 if (MergeWithVar) {
2586 // An equivalent variable with external linkage has been found. Link
2587 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002588 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002589
2590 if (VarDecl *DDef = D->getDefinition()) {
2591 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2592 Importer.ToDiag(ExistingDef->getLocation(),
2593 diag::err_odr_variable_multiple_def)
2594 << Name;
2595 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2596 } else {
2597 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002598 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002599 if (DDef->isInitKnownICE()) {
2600 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2601 Eval->CheckedICE = true;
2602 Eval->IsICE = DDef->isInitICE();
2603 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002604 }
2605 }
2606
2607 return MergeWithVar;
2608 }
2609
2610 if (!ConflictingDecls.empty()) {
2611 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2612 ConflictingDecls.data(),
2613 ConflictingDecls.size());
2614 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002615 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002616 }
2617 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002618
Douglas Gregorb4964f72010-02-15 23:54:17 +00002619 // Import the type.
2620 QualType T = Importer.Import(D->getType());
2621 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002622 return nullptr;
2623
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002624 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002625 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002626 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2627 Importer.Import(D->getInnerLocStart()),
2628 Loc, Name.getAsIdentifierInfo(),
2629 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002630 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002631 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002632 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002633 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002634 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002635 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002636
Sean Callanan59721b32015-04-28 18:41:46 +00002637 if (!D->isFileVarDecl() &&
2638 D->isUsed())
2639 ToVar->setIsUsed();
2640
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002641 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002642 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002643 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002644
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002645 if (D->isConstexpr())
2646 ToVar->setConstexpr(true);
2647
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002648 return ToVar;
2649}
2650
Douglas Gregor8b228d72010-02-17 21:22:52 +00002651Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2652 // Parameters are created in the translation unit's context, then moved
2653 // into the function declaration's context afterward.
2654 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2655
2656 // Import the name of this declaration.
2657 DeclarationName Name = Importer.Import(D->getDeclName());
2658 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002659 return nullptr;
2660
Douglas Gregor8b228d72010-02-17 21:22:52 +00002661 // Import the location of this declaration.
2662 SourceLocation Loc = Importer.Import(D->getLocation());
2663
2664 // Import the parameter's type.
2665 QualType T = Importer.Import(D->getType());
2666 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002667 return nullptr;
2668
Douglas Gregor8b228d72010-02-17 21:22:52 +00002669 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002670 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2671 Name.getAsIdentifierInfo(), T,
2672 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002673 return Importer.Imported(D, ToParm);
2674}
2675
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002676Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2677 // Parameters are created in the translation unit's context, then moved
2678 // into the function declaration's context afterward.
2679 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2680
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002681 // Import the name of this declaration.
2682 DeclarationName Name = Importer.Import(D->getDeclName());
2683 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002684 return nullptr;
2685
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002686 // Import the location of this declaration.
2687 SourceLocation Loc = Importer.Import(D->getLocation());
2688
2689 // Import the parameter's type.
2690 QualType T = Importer.Import(D->getType());
2691 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002692 return nullptr;
2693
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002694 // Create the imported parameter.
2695 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2696 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002697 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002698 Loc, Name.getAsIdentifierInfo(),
2699 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002700 /*DefaultArg*/ nullptr);
2701
2702 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002703 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002704 ToParm->setKNRPromoted(D->isKNRPromoted());
2705
2706 Expr *ToDefArg = nullptr;
2707 Expr *FromDefArg = nullptr;
2708 if (D->hasUninstantiatedDefaultArg()) {
2709 FromDefArg = D->getUninstantiatedDefaultArg();
2710 ToDefArg = Importer.Import(FromDefArg);
2711 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2712 } else if (D->hasUnparsedDefaultArg()) {
2713 ToParm->setUnparsedDefaultArg();
2714 } else if (D->hasDefaultArg()) {
2715 FromDefArg = D->getDefaultArg();
2716 ToDefArg = Importer.Import(FromDefArg);
2717 ToParm->setDefaultArg(ToDefArg);
2718 }
2719 if (FromDefArg && !ToDefArg)
2720 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002721
2722 if (D->isUsed())
2723 ToParm->setIsUsed();
2724
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002725 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002726}
2727
Douglas Gregor43f54792010-02-17 02:12:47 +00002728Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2729 // Import the major distinguishing characteristics of a method.
2730 DeclContext *DC, *LexicalDC;
2731 DeclarationName Name;
2732 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002733 NamedDecl *ToD;
2734 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002735 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002736 if (ToD)
2737 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002738
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002739 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002740 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002741 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2742 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002743 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2744 continue;
2745
2746 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002747 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2748 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002749 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002750 << D->isInstanceMethod() << Name << D->getReturnType()
2751 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002752 Importer.ToDiag(FoundMethod->getLocation(),
2753 diag::note_odr_objc_method_here)
2754 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002755 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002756 }
2757
2758 // Check the number of parameters.
2759 if (D->param_size() != FoundMethod->param_size()) {
2760 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2761 << D->isInstanceMethod() << Name
2762 << D->param_size() << FoundMethod->param_size();
2763 Importer.ToDiag(FoundMethod->getLocation(),
2764 diag::note_odr_objc_method_here)
2765 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002766 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002767 }
2768
2769 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002770 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002771 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2772 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002773 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002774 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002775 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002776 diag::err_odr_objc_method_param_type_inconsistent)
2777 << D->isInstanceMethod() << Name
2778 << (*P)->getType() << (*FoundP)->getType();
2779 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2780 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002781 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002782 }
2783 }
2784
2785 // Check variadic/non-variadic.
2786 // Check the number of parameters.
2787 if (D->isVariadic() != FoundMethod->isVariadic()) {
2788 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2789 << D->isInstanceMethod() << Name;
2790 Importer.ToDiag(FoundMethod->getLocation(),
2791 diag::note_odr_objc_method_here)
2792 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002793 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002794 }
2795
2796 // FIXME: Any other bits we need to merge?
2797 return Importer.Imported(D, FoundMethod);
2798 }
2799 }
2800
2801 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00002802 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002803 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002804 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002805
Alp Toker314cc812014-01-25 16:55:45 +00002806 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00002807
Alp Toker314cc812014-01-25 16:55:45 +00002808 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2809 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2810 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2811 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2812 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002813
2814 // FIXME: When we decide to merge method definitions, we'll need to
2815 // deal with implicit parameters.
2816
2817 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002818 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00002819 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00002820 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00002821 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002822 return nullptr;
2823
Douglas Gregor43f54792010-02-17 02:12:47 +00002824 ToParams.push_back(ToP);
2825 }
2826
2827 // Set the parameters.
2828 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2829 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002830 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00002831 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002832 SmallVector<SourceLocation, 12> SelLocs;
2833 D->getSelectorLocs(SelLocs);
2834 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00002835
2836 ToMethod->setLexicalDeclContext(LexicalDC);
2837 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002838 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00002839 return ToMethod;
2840}
2841
Douglas Gregor85f3f952015-07-07 03:57:15 +00002842Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2843 // Import the major distinguishing characteristics of a category.
2844 DeclContext *DC, *LexicalDC;
2845 DeclarationName Name;
2846 SourceLocation Loc;
2847 NamedDecl *ToD;
2848 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2849 return nullptr;
2850 if (ToD)
2851 return ToD;
2852
2853 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2854 if (!BoundInfo)
2855 return nullptr;
2856
2857 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2858 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00002859 D->getVariance(),
2860 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00002861 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002862 Importer.Import(D->getLocation()),
2863 Name.getAsIdentifierInfo(),
2864 Importer.Import(D->getColonLoc()),
2865 BoundInfo);
2866 Importer.Imported(D, Result);
2867 Result->setLexicalDeclContext(LexicalDC);
2868 return Result;
2869}
2870
Douglas Gregor84c51c32010-02-18 01:47:50 +00002871Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2872 // Import the major distinguishing characteristics of a category.
2873 DeclContext *DC, *LexicalDC;
2874 DeclarationName Name;
2875 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002876 NamedDecl *ToD;
2877 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002878 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002879 if (ToD)
2880 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002881
Douglas Gregor84c51c32010-02-18 01:47:50 +00002882 ObjCInterfaceDecl *ToInterface
2883 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2884 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00002885 return nullptr;
2886
Douglas Gregor84c51c32010-02-18 01:47:50 +00002887 // Determine if we've already encountered this category.
2888 ObjCCategoryDecl *MergeWithCategory
2889 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2890 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2891 if (!ToCategory) {
2892 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002893 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00002894 Loc,
2895 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002896 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002897 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002898 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002899 Importer.Import(D->getIvarLBraceLoc()),
2900 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002901 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002902 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002903 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002904 // Import the type parameter list after calling Imported, to avoid
2905 // loops when bringing in their DeclContext.
2906 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2907 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002908
Douglas Gregor84c51c32010-02-18 01:47:50 +00002909 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002910 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2911 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002912 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2913 = D->protocol_loc_begin();
2914 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2915 FromProtoEnd = D->protocol_end();
2916 FromProto != FromProtoEnd;
2917 ++FromProto, ++FromProtoLoc) {
2918 ObjCProtocolDecl *ToProto
2919 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2920 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00002921 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002922 Protocols.push_back(ToProto);
2923 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2924 }
2925
2926 // FIXME: If we're merging, make sure that the protocol list is the same.
2927 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2928 ProtocolLocs.data(), Importer.getToContext());
2929
2930 } else {
2931 Importer.Imported(D, ToCategory);
2932 }
2933
2934 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002935 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002936
2937 // If we have an implementation, import it as well.
2938 if (D->getImplementation()) {
2939 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002940 = cast_or_null<ObjCCategoryImplDecl>(
2941 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002942 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00002943 return nullptr;
2944
Douglas Gregor84c51c32010-02-18 01:47:50 +00002945 ToCategory->setImplementation(Impl);
2946 }
2947
2948 return ToCategory;
2949}
2950
Douglas Gregor2aa53772012-01-24 17:42:07 +00002951bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2952 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002953 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002954 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00002955 if (shouldForceImportDeclContext(Kind))
2956 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002957 return false;
2958 }
2959
2960 // Start the protocol definition
2961 To->startDefinition();
2962
2963 // Import protocols
2964 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2965 SmallVector<SourceLocation, 4> ProtocolLocs;
2966 ObjCProtocolDecl::protocol_loc_iterator
2967 FromProtoLoc = From->protocol_loc_begin();
2968 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2969 FromProtoEnd = From->protocol_end();
2970 FromProto != FromProtoEnd;
2971 ++FromProto, ++FromProtoLoc) {
2972 ObjCProtocolDecl *ToProto
2973 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2974 if (!ToProto)
2975 return true;
2976 Protocols.push_back(ToProto);
2977 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2978 }
2979
2980 // FIXME: If we're merging, make sure that the protocol list is the same.
2981 To->setProtocolList(Protocols.data(), Protocols.size(),
2982 ProtocolLocs.data(), Importer.getToContext());
2983
Douglas Gregor2e15c842012-02-01 21:00:38 +00002984 if (shouldForceImportDeclContext(Kind)) {
2985 // Import all of the members of this protocol.
2986 ImportDeclContext(From, /*ForceImport=*/true);
2987 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002988 return false;
2989}
2990
Douglas Gregor98d156a2010-02-17 16:12:00 +00002991Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002992 // If this protocol has a definition in the translation unit we're coming
2993 // from, but this particular declaration is not that definition, import the
2994 // definition and map to that.
2995 ObjCProtocolDecl *Definition = D->getDefinition();
2996 if (Definition && Definition != D) {
2997 Decl *ImportedDef = Importer.Import(Definition);
2998 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002999 return nullptr;
3000
Douglas Gregor2aa53772012-01-24 17:42:07 +00003001 return Importer.Imported(D, ImportedDef);
3002 }
3003
Douglas Gregor84c51c32010-02-18 01:47:50 +00003004 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003005 DeclContext *DC, *LexicalDC;
3006 DeclarationName Name;
3007 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003008 NamedDecl *ToD;
3009 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003010 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003011 if (ToD)
3012 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003013
Craig Topper36250ad2014-05-12 05:36:57 +00003014 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003015 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003016 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003017 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3018 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003019 continue;
3020
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003021 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003022 break;
3023 }
3024
3025 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003026 if (!ToProto) {
3027 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3028 Name.getAsIdentifierInfo(), Loc,
3029 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003030 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003031 ToProto->setLexicalDeclContext(LexicalDC);
3032 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003033 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003034
3035 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003036
Douglas Gregor2aa53772012-01-24 17:42:07 +00003037 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003038 return nullptr;
3039
Douglas Gregor98d156a2010-02-17 16:12:00 +00003040 return ToProto;
3041}
3042
Sean Callanan0aae0412014-12-10 00:00:37 +00003043Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3044 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3045 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3046
3047 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3048 SourceLocation LangLoc = Importer.Import(D->getLocation());
3049
3050 bool HasBraces = D->hasBraces();
3051
Sean Callananb12a8552014-12-10 21:22:20 +00003052 LinkageSpecDecl *ToLinkageSpec =
3053 LinkageSpecDecl::Create(Importer.getToContext(),
3054 DC,
3055 ExternLoc,
3056 LangLoc,
3057 D->getLanguage(),
3058 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003059
3060 if (HasBraces) {
3061 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3062 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3063 }
3064
3065 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3066 LexicalDC->addDeclInternal(ToLinkageSpec);
3067
3068 Importer.Imported(D, ToLinkageSpec);
3069
3070 return ToLinkageSpec;
3071}
3072
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003073Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) {
3074 DeclContext *DC, *LexicalDC;
3075 DeclarationName Name;
3076 SourceLocation Loc;
3077 NamedDecl *ToD = nullptr;
3078 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3079 return nullptr;
3080 if (ToD)
3081 return ToD;
3082
3083 DeclarationNameInfo NameInfo(Name,
3084 Importer.Import(D->getNameInfo().getLoc()));
3085 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3086
3087 UsingDecl *ToUsing = UsingDecl::Create(Importer.getToContext(), DC,
3088 Importer.Import(D->getUsingLoc()),
3089 Importer.Import(D->getQualifierLoc()),
3090 NameInfo, D->hasTypename());
3091 ToUsing->setLexicalDeclContext(LexicalDC);
3092 LexicalDC->addDeclInternal(ToUsing);
3093 Importer.Imported(D, ToUsing);
3094
3095 if (NamedDecl *FromPattern =
3096 Importer.getFromContext().getInstantiatedFromUsingDecl(D)) {
3097 if (NamedDecl *ToPattern =
3098 dyn_cast_or_null<NamedDecl>(Importer.Import(FromPattern)))
3099 Importer.getToContext().setInstantiatedFromUsingDecl(ToUsing, ToPattern);
3100 else
3101 return nullptr;
3102 }
3103
3104 for (UsingShadowDecl *FromShadow : D->shadows()) {
3105 if (UsingShadowDecl *ToShadow =
3106 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromShadow)))
3107 ToUsing->addShadowDecl(ToShadow);
3108 else
3109 // FIXME: We return a nullptr here but the definition is already created
3110 // and available with lookups. How to fix this?..
3111 return nullptr;
3112 }
3113 return ToUsing;
3114}
3115
3116Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) {
3117 DeclContext *DC, *LexicalDC;
3118 DeclarationName Name;
3119 SourceLocation Loc;
3120 NamedDecl *ToD = nullptr;
3121 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3122 return nullptr;
3123 if (ToD)
3124 return ToD;
3125
3126 UsingDecl *ToUsing = dyn_cast_or_null<UsingDecl>(
3127 Importer.Import(D->getUsingDecl()));
3128 if (!ToUsing)
3129 return nullptr;
3130
3131 NamedDecl *ToTarget = dyn_cast_or_null<NamedDecl>(
3132 Importer.Import(D->getTargetDecl()));
3133 if (!ToTarget)
3134 return nullptr;
3135
3136 UsingShadowDecl *ToShadow = UsingShadowDecl::Create(
3137 Importer.getToContext(), DC, Loc, ToUsing, ToTarget);
3138
3139 ToShadow->setLexicalDeclContext(LexicalDC);
3140 ToShadow->setAccess(D->getAccess());
3141 Importer.Imported(D, ToShadow);
3142
3143 if (UsingShadowDecl *FromPattern =
3144 Importer.getFromContext().getInstantiatedFromUsingShadowDecl(D)) {
3145 if (UsingShadowDecl *ToPattern =
3146 dyn_cast_or_null<UsingShadowDecl>(Importer.Import(FromPattern)))
3147 Importer.getToContext().setInstantiatedFromUsingShadowDecl(ToShadow,
3148 ToPattern);
3149 else
3150 // FIXME: We return a nullptr here but the definition is already created
3151 // and available with lookups. How to fix this?..
3152 return nullptr;
3153 }
3154
3155 LexicalDC->addDeclInternal(ToShadow);
3156
3157 return ToShadow;
3158}
3159
3160
3161Decl *ASTNodeImporter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
3162 DeclContext *DC, *LexicalDC;
3163 DeclarationName Name;
3164 SourceLocation Loc;
3165 NamedDecl *ToD = nullptr;
3166 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3167 return nullptr;
3168 if (ToD)
3169 return ToD;
3170
3171 DeclContext *ToComAncestor = Importer.ImportContext(D->getCommonAncestor());
3172 if (!ToComAncestor)
3173 return nullptr;
3174
3175 NamespaceDecl *ToNominated = cast_or_null<NamespaceDecl>(
3176 Importer.Import(D->getNominatedNamespace()));
3177 if (!ToNominated)
3178 return nullptr;
3179
3180 UsingDirectiveDecl *ToUsingDir = UsingDirectiveDecl::Create(
3181 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3182 Importer.Import(D->getNamespaceKeyLocation()),
3183 Importer.Import(D->getQualifierLoc()),
3184 Importer.Import(D->getIdentLocation()), ToNominated, ToComAncestor);
3185 ToUsingDir->setLexicalDeclContext(LexicalDC);
3186 LexicalDC->addDeclInternal(ToUsingDir);
3187 Importer.Imported(D, ToUsingDir);
3188
3189 return ToUsingDir;
3190}
3191
3192Decl *ASTNodeImporter::VisitUnresolvedUsingValueDecl(
3193 UnresolvedUsingValueDecl *D) {
3194 DeclContext *DC, *LexicalDC;
3195 DeclarationName Name;
3196 SourceLocation Loc;
3197 NamedDecl *ToD = nullptr;
3198 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3199 return nullptr;
3200 if (ToD)
3201 return ToD;
3202
3203 DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc()));
3204 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
3205
3206 UnresolvedUsingValueDecl *ToUsingValue = UnresolvedUsingValueDecl::Create(
3207 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3208 Importer.Import(D->getQualifierLoc()), NameInfo,
3209 Importer.Import(D->getEllipsisLoc()));
3210
3211 Importer.Imported(D, ToUsingValue);
3212 ToUsingValue->setAccess(D->getAccess());
3213 ToUsingValue->setLexicalDeclContext(LexicalDC);
3214 LexicalDC->addDeclInternal(ToUsingValue);
3215
3216 return ToUsingValue;
3217}
3218
3219Decl *ASTNodeImporter::VisitUnresolvedUsingTypenameDecl(
3220 UnresolvedUsingTypenameDecl *D) {
3221 DeclContext *DC, *LexicalDC;
3222 DeclarationName Name;
3223 SourceLocation Loc;
3224 NamedDecl *ToD = nullptr;
3225 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3226 return nullptr;
3227 if (ToD)
3228 return ToD;
3229
3230 UnresolvedUsingTypenameDecl *ToUsing = UnresolvedUsingTypenameDecl::Create(
3231 Importer.getToContext(), DC, Importer.Import(D->getUsingLoc()),
3232 Importer.Import(D->getTypenameLoc()),
3233 Importer.Import(D->getQualifierLoc()), Loc, Name,
3234 Importer.Import(D->getEllipsisLoc()));
3235
3236 Importer.Imported(D, ToUsing);
3237 ToUsing->setAccess(D->getAccess());
3238 ToUsing->setLexicalDeclContext(LexicalDC);
3239 LexicalDC->addDeclInternal(ToUsing);
3240
3241 return ToUsing;
3242}
3243
3244
Douglas Gregor2aa53772012-01-24 17:42:07 +00003245bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3246 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003247 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003248 if (To->getDefinition()) {
3249 // Check consistency of superclass.
3250 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3251 if (FromSuper) {
3252 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3253 if (!FromSuper)
3254 return true;
3255 }
3256
3257 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3258 if ((bool)FromSuper != (bool)ToSuper ||
3259 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3260 Importer.ToDiag(To->getLocation(),
3261 diag::err_odr_objc_superclass_inconsistent)
3262 << To->getDeclName();
3263 if (ToSuper)
3264 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3265 << To->getSuperClass()->getDeclName();
3266 else
3267 Importer.ToDiag(To->getLocation(),
3268 diag::note_odr_objc_missing_superclass);
3269 if (From->getSuperClass())
3270 Importer.FromDiag(From->getSuperClassLoc(),
3271 diag::note_odr_objc_superclass)
3272 << From->getSuperClass()->getDeclName();
3273 else
3274 Importer.FromDiag(From->getLocation(),
3275 diag::note_odr_objc_missing_superclass);
3276 }
3277
Douglas Gregor2e15c842012-02-01 21:00:38 +00003278 if (shouldForceImportDeclContext(Kind))
3279 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003280 return false;
3281 }
3282
3283 // Start the definition.
3284 To->startDefinition();
3285
3286 // If this class has a superclass, import it.
3287 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003288 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3289 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003290 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003291
3292 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003293 }
3294
3295 // Import protocols
3296 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3297 SmallVector<SourceLocation, 4> ProtocolLocs;
3298 ObjCInterfaceDecl::protocol_loc_iterator
3299 FromProtoLoc = From->protocol_loc_begin();
3300
3301 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3302 FromProtoEnd = From->protocol_end();
3303 FromProto != FromProtoEnd;
3304 ++FromProto, ++FromProtoLoc) {
3305 ObjCProtocolDecl *ToProto
3306 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3307 if (!ToProto)
3308 return true;
3309 Protocols.push_back(ToProto);
3310 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3311 }
3312
3313 // FIXME: If we're merging, make sure that the protocol list is the same.
3314 To->setProtocolList(Protocols.data(), Protocols.size(),
3315 ProtocolLocs.data(), Importer.getToContext());
3316
3317 // Import categories. When the categories themselves are imported, they'll
3318 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003319 for (auto *Cat : From->known_categories())
3320 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003321
Douglas Gregor2aa53772012-01-24 17:42:07 +00003322 // If we have an @implementation, import it as well.
3323 if (From->getImplementation()) {
3324 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3325 Importer.Import(From->getImplementation()));
3326 if (!Impl)
3327 return true;
3328
3329 To->setImplementation(Impl);
3330 }
3331
Douglas Gregor2e15c842012-02-01 21:00:38 +00003332 if (shouldForceImportDeclContext(Kind)) {
3333 // Import all of the members of this class.
3334 ImportDeclContext(From, /*ForceImport=*/true);
3335 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003336 return false;
3337}
3338
Douglas Gregor85f3f952015-07-07 03:57:15 +00003339ObjCTypeParamList *
3340ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3341 if (!list)
3342 return nullptr;
3343
3344 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3345 for (auto fromTypeParam : *list) {
3346 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3347 Importer.Import(fromTypeParam));
3348 if (!toTypeParam)
3349 return nullptr;
3350
3351 toTypeParams.push_back(toTypeParam);
3352 }
3353
3354 return ObjCTypeParamList::create(Importer.getToContext(),
3355 Importer.Import(list->getLAngleLoc()),
3356 toTypeParams,
3357 Importer.Import(list->getRAngleLoc()));
3358}
3359
Douglas Gregor45635322010-02-16 01:20:57 +00003360Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003361 // If this class has a definition in the translation unit we're coming from,
3362 // but this particular declaration is not that definition, import the
3363 // definition and map to that.
3364 ObjCInterfaceDecl *Definition = D->getDefinition();
3365 if (Definition && Definition != D) {
3366 Decl *ImportedDef = Importer.Import(Definition);
3367 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003368 return nullptr;
3369
Douglas Gregor2aa53772012-01-24 17:42:07 +00003370 return Importer.Imported(D, ImportedDef);
3371 }
3372
Douglas Gregor45635322010-02-16 01:20:57 +00003373 // Import the major distinguishing characteristics of an @interface.
3374 DeclContext *DC, *LexicalDC;
3375 DeclarationName Name;
3376 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003377 NamedDecl *ToD;
3378 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003379 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003380 if (ToD)
3381 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003382
Douglas Gregor2aa53772012-01-24 17:42:07 +00003383 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003384 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003385 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003386 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003387 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3388 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003389 continue;
3390
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003391 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003392 break;
3393 }
3394
Douglas Gregor2aa53772012-01-24 17:42:07 +00003395 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003396 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003397 if (!ToIface) {
3398 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3399 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003400 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003401 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003402 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003403 D->isImplicitInterfaceDecl());
3404 ToIface->setLexicalDeclContext(LexicalDC);
3405 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003406 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003407 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003408 // Import the type parameter list after calling Imported, to avoid
3409 // loops when bringing in their DeclContext.
3410 ToIface->setTypeParamList(ImportObjCTypeParamList(
3411 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003412
Douglas Gregor2aa53772012-01-24 17:42:07 +00003413 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003414 return nullptr;
3415
Douglas Gregor98d156a2010-02-17 16:12:00 +00003416 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003417}
3418
Douglas Gregor4da9d682010-12-07 15:32:12 +00003419Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3420 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3421 Importer.Import(D->getCategoryDecl()));
3422 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003423 return nullptr;
3424
Douglas Gregor4da9d682010-12-07 15:32:12 +00003425 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3426 if (!ToImpl) {
3427 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3428 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003429 return nullptr;
3430
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003431 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003432 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003433 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003434 Category->getClassInterface(),
3435 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003436 Importer.Import(D->getAtStartLoc()),
3437 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003438
3439 DeclContext *LexicalDC = DC;
3440 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3441 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3442 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003443 return nullptr;
3444
Douglas Gregor4da9d682010-12-07 15:32:12 +00003445 ToImpl->setLexicalDeclContext(LexicalDC);
3446 }
3447
Sean Callanan95e74be2011-10-21 02:57:43 +00003448 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003449 Category->setImplementation(ToImpl);
3450 }
3451
3452 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003453 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003454 return ToImpl;
3455}
3456
Douglas Gregorda8025c2010-12-07 01:26:03 +00003457Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3458 // Find the corresponding interface.
3459 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3460 Importer.Import(D->getClassInterface()));
3461 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003462 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003463
3464 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003465 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003466 if (D->getSuperClass()) {
3467 Super = cast_or_null<ObjCInterfaceDecl>(
3468 Importer.Import(D->getSuperClass()));
3469 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003470 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003471 }
3472
3473 ObjCImplementationDecl *Impl = Iface->getImplementation();
3474 if (!Impl) {
3475 // We haven't imported an implementation yet. Create a new @implementation
3476 // now.
3477 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3478 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003479 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003480 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003481 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003482 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003483 Importer.Import(D->getIvarLBraceLoc()),
3484 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003485
3486 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3487 DeclContext *LexicalDC
3488 = Importer.ImportContext(D->getLexicalDeclContext());
3489 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003490 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003491 Impl->setLexicalDeclContext(LexicalDC);
3492 }
3493
3494 // Associate the implementation with the class it implements.
3495 Iface->setImplementation(Impl);
3496 Importer.Imported(D, Iface->getImplementation());
3497 } else {
3498 Importer.Imported(D, Iface->getImplementation());
3499
3500 // Verify that the existing @implementation has the same superclass.
3501 if ((Super && !Impl->getSuperClass()) ||
3502 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003503 (Super && Impl->getSuperClass() &&
3504 !declaresSameEntity(Super->getCanonicalDecl(),
3505 Impl->getSuperClass()))) {
3506 Importer.ToDiag(Impl->getLocation(),
3507 diag::err_odr_objc_superclass_inconsistent)
3508 << Iface->getDeclName();
3509 // FIXME: It would be nice to have the location of the superclass
3510 // below.
3511 if (Impl->getSuperClass())
3512 Importer.ToDiag(Impl->getLocation(),
3513 diag::note_odr_objc_superclass)
3514 << Impl->getSuperClass()->getDeclName();
3515 else
3516 Importer.ToDiag(Impl->getLocation(),
3517 diag::note_odr_objc_missing_superclass);
3518 if (D->getSuperClass())
3519 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003520 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003521 << D->getSuperClass()->getDeclName();
3522 else
3523 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003524 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003525 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003526 }
3527 }
3528
3529 // Import all of the members of this @implementation.
3530 ImportDeclContext(D);
3531
3532 return Impl;
3533}
3534
Douglas Gregora11c4582010-02-17 18:02:10 +00003535Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3536 // Import the major distinguishing characteristics of an @property.
3537 DeclContext *DC, *LexicalDC;
3538 DeclarationName Name;
3539 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003540 NamedDecl *ToD;
3541 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003542 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003543 if (ToD)
3544 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003545
3546 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003547 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003548 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003549 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003550 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003551 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003552 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003553 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003554 FoundProp->getType())) {
3555 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3556 << Name << D->getType() << FoundProp->getType();
3557 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3558 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003559 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003560 }
3561
3562 // FIXME: Check property attributes, getters, setters, etc.?
3563
3564 // Consider these properties to be equivalent.
3565 Importer.Imported(D, FoundProp);
3566 return FoundProp;
3567 }
3568 }
3569
3570 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003571 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3572 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003573 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003574
3575 // Create the new property.
3576 ObjCPropertyDecl *ToProperty
3577 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3578 Name.getAsIdentifierInfo(),
3579 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003580 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003581 Importer.Import(D->getType()),
3582 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003583 D->getPropertyImplementation());
3584 Importer.Imported(D, ToProperty);
3585 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003586 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003587
3588 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003589 ToProperty->setPropertyAttributesAsWritten(
3590 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003591 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3592 Importer.Import(D->getGetterNameLoc()));
3593 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3594 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003595 ToProperty->setGetterMethodDecl(
3596 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3597 ToProperty->setSetterMethodDecl(
3598 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3599 ToProperty->setPropertyIvarDecl(
3600 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3601 return ToProperty;
3602}
3603
Douglas Gregor14a49e22010-12-07 18:32:03 +00003604Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3605 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3606 Importer.Import(D->getPropertyDecl()));
3607 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003608 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003609
3610 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3611 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003612 return nullptr;
3613
Douglas Gregor14a49e22010-12-07 18:32:03 +00003614 // Import the lexical declaration context.
3615 DeclContext *LexicalDC = DC;
3616 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3617 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3618 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003619 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003620 }
3621
3622 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3623 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003624 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003625
3626 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003627 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003628 if (D->getPropertyIvarDecl()) {
3629 Ivar = cast_or_null<ObjCIvarDecl>(
3630 Importer.Import(D->getPropertyIvarDecl()));
3631 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003632 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003633 }
3634
3635 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003636 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3637 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003638 if (!ToImpl) {
3639 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3640 Importer.Import(D->getLocStart()),
3641 Importer.Import(D->getLocation()),
3642 Property,
3643 D->getPropertyImplementation(),
3644 Ivar,
3645 Importer.Import(D->getPropertyIvarDeclLoc()));
3646 ToImpl->setLexicalDeclContext(LexicalDC);
3647 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003648 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003649 } else {
3650 // Check that we have the same kind of property implementation (@synthesize
3651 // vs. @dynamic).
3652 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3653 Importer.ToDiag(ToImpl->getLocation(),
3654 diag::err_odr_objc_property_impl_kind_inconsistent)
3655 << Property->getDeclName()
3656 << (ToImpl->getPropertyImplementation()
3657 == ObjCPropertyImplDecl::Dynamic);
3658 Importer.FromDiag(D->getLocation(),
3659 diag::note_odr_objc_property_impl_kind)
3660 << D->getPropertyDecl()->getDeclName()
3661 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003662 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003663 }
3664
3665 // For @synthesize, check that we have the same
3666 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3667 Ivar != ToImpl->getPropertyIvarDecl()) {
3668 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3669 diag::err_odr_objc_synthesize_ivar_inconsistent)
3670 << Property->getDeclName()
3671 << ToImpl->getPropertyIvarDecl()->getDeclName()
3672 << Ivar->getDeclName();
3673 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3674 diag::note_odr_objc_synthesize_ivar_here)
3675 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003676 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003677 }
3678
3679 // Merge the existing implementation with the new implementation.
3680 Importer.Imported(D, ToImpl);
3681 }
3682
3683 return ToImpl;
3684}
3685
Douglas Gregora082a492010-11-30 19:14:50 +00003686Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3687 // For template arguments, we adopt the translation unit as our declaration
3688 // context. This context will be fixed when the actual template declaration
3689 // is created.
3690
3691 // FIXME: Import default argument.
3692 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3693 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003694 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003695 Importer.Import(D->getLocation()),
3696 D->getDepth(),
3697 D->getIndex(),
3698 Importer.Import(D->getIdentifier()),
3699 D->wasDeclaredWithTypename(),
3700 D->isParameterPack());
3701}
3702
3703Decl *
3704ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3705 // Import the name of this declaration.
3706 DeclarationName Name = Importer.Import(D->getDeclName());
3707 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003708 return nullptr;
3709
Douglas Gregora082a492010-11-30 19:14:50 +00003710 // Import the location of this declaration.
3711 SourceLocation Loc = Importer.Import(D->getLocation());
3712
3713 // Import the type of this declaration.
3714 QualType T = Importer.Import(D->getType());
3715 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003716 return nullptr;
3717
Douglas Gregora082a492010-11-30 19:14:50 +00003718 // Import type-source information.
3719 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3720 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003721 return nullptr;
3722
Douglas Gregora082a492010-11-30 19:14:50 +00003723 // FIXME: Import default argument.
3724
3725 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3726 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003727 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003728 Loc, D->getDepth(), D->getPosition(),
3729 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003730 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003731}
3732
3733Decl *
3734ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3735 // Import the name of this declaration.
3736 DeclarationName Name = Importer.Import(D->getDeclName());
3737 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
3739
Douglas Gregora082a492010-11-30 19:14:50 +00003740 // Import the location of this declaration.
3741 SourceLocation Loc = Importer.Import(D->getLocation());
3742
3743 // Import template parameters.
3744 TemplateParameterList *TemplateParams
3745 = ImportTemplateParameterList(D->getTemplateParameters());
3746 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003747 return nullptr;
3748
Douglas Gregora082a492010-11-30 19:14:50 +00003749 // FIXME: Import default argument.
3750
3751 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3752 Importer.getToContext().getTranslationUnitDecl(),
3753 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003754 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003755 Name.getAsIdentifierInfo(),
3756 TemplateParams);
3757}
3758
3759Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3760 // If this record has a definition in the translation unit we're coming from,
3761 // but this particular declaration is not that definition, import the
3762 // definition and map to that.
3763 CXXRecordDecl *Definition
3764 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3765 if (Definition && Definition != D->getTemplatedDecl()) {
3766 Decl *ImportedDef
3767 = Importer.Import(Definition->getDescribedClassTemplate());
3768 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003769 return nullptr;
3770
Douglas Gregora082a492010-11-30 19:14:50 +00003771 return Importer.Imported(D, ImportedDef);
3772 }
3773
3774 // Import the major distinguishing characteristics of this class template.
3775 DeclContext *DC, *LexicalDC;
3776 DeclarationName Name;
3777 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003778 NamedDecl *ToD;
3779 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003780 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003781 if (ToD)
3782 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003783
Douglas Gregora082a492010-11-30 19:14:50 +00003784 // We may already have a template of the same name; try to find and match it.
3785 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003786 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003787 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003788 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003789 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3790 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003791 continue;
3792
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003793 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003794 if (ClassTemplateDecl *FoundTemplate
3795 = dyn_cast<ClassTemplateDecl>(Found)) {
3796 if (IsStructuralMatch(D, FoundTemplate)) {
3797 // The class templates structurally match; call it the same template.
3798 // FIXME: We may be filling in a forward declaration here. Handle
3799 // this case!
3800 Importer.Imported(D->getTemplatedDecl(),
3801 FoundTemplate->getTemplatedDecl());
3802 return Importer.Imported(D, FoundTemplate);
3803 }
3804 }
3805
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003806 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003807 }
3808
3809 if (!ConflictingDecls.empty()) {
3810 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3811 ConflictingDecls.data(),
3812 ConflictingDecls.size());
3813 }
3814
3815 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003816 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00003817 }
3818
3819 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3820
3821 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003822 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3823 Importer.Import(DTemplated));
3824 if (!D2Templated)
3825 return nullptr;
3826
3827 // Resolve possible cyclic import.
3828 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3829 return AlreadyImported;
3830
Douglas Gregora082a492010-11-30 19:14:50 +00003831 // Create the class template declaration itself.
3832 TemplateParameterList *TemplateParams
3833 = ImportTemplateParameterList(D->getTemplateParameters());
3834 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003835 return nullptr;
3836
Douglas Gregora082a492010-11-30 19:14:50 +00003837 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3838 Loc, Name, TemplateParams,
Vassil Vassilev352e4412017-01-12 09:16:26 +00003839 D2Templated);
Douglas Gregora082a492010-11-30 19:14:50 +00003840 D2Templated->setDescribedClassTemplate(D2);
3841
3842 D2->setAccess(D->getAccess());
3843 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003844 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003845
3846 // Note the relationship between the class templates.
3847 Importer.Imported(D, D2);
3848 Importer.Imported(DTemplated, D2Templated);
3849
John McCallf937c022011-10-07 06:10:15 +00003850 if (DTemplated->isCompleteDefinition() &&
3851 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003852 // FIXME: Import definition!
3853 }
3854
3855 return D2;
3856}
3857
Douglas Gregore2e50d332010-12-01 01:36:18 +00003858Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3859 ClassTemplateSpecializationDecl *D) {
3860 // If this record has a definition in the translation unit we're coming from,
3861 // but this particular declaration is not that definition, import the
3862 // definition and map to that.
3863 TagDecl *Definition = D->getDefinition();
3864 if (Definition && Definition != D) {
3865 Decl *ImportedDef = Importer.Import(Definition);
3866 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003867 return nullptr;
3868
Douglas Gregore2e50d332010-12-01 01:36:18 +00003869 return Importer.Imported(D, ImportedDef);
3870 }
3871
3872 ClassTemplateDecl *ClassTemplate
3873 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3874 D->getSpecializedTemplate()));
3875 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003876 return nullptr;
3877
Douglas Gregore2e50d332010-12-01 01:36:18 +00003878 // Import the context of this declaration.
3879 DeclContext *DC = ClassTemplate->getDeclContext();
3880 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003881 return nullptr;
3882
Douglas Gregore2e50d332010-12-01 01:36:18 +00003883 DeclContext *LexicalDC = DC;
3884 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3885 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3886 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003887 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003888 }
3889
3890 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003891 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3892 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003893
3894 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003895 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003896 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3897 D->getTemplateArgs().size(),
3898 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003899 return nullptr;
3900
Douglas Gregore2e50d332010-12-01 01:36:18 +00003901 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003902 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003903 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00003904 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003905 if (D2) {
3906 // We already have a class template specialization with these template
3907 // arguments.
3908
3909 // FIXME: Check for specialization vs. instantiation errors.
3910
3911 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003912 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003913 // The record types structurally match, or the "from" translation
3914 // unit only had a forward declaration anyway; call it the same
3915 // function.
3916 return Importer.Imported(D, FoundDef);
3917 }
3918 }
3919 } else {
3920 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003921 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3922 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3923
3924 // Import TemplateArgumentListInfo
3925 TemplateArgumentListInfo ToTAInfo;
3926 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3927 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00003928 if (auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I]))
3929 ToTAInfo.addArgument(*ToLoc);
3930 else
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003931 return nullptr;
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003932 }
3933
3934 QualType CanonInjType = Importer.Import(
3935 PartialSpec->getInjectedSpecializationType());
3936 if (CanonInjType.isNull())
3937 return nullptr;
3938 CanonInjType = CanonInjType.getCanonicalType();
3939
3940 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3941 PartialSpec->getTemplateParameters());
3942 if (!ToTPList && PartialSpec->getTemplateParameters())
3943 return nullptr;
3944
3945 D2 = ClassTemplatePartialSpecializationDecl::Create(
3946 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3947 ToTPList, ClassTemplate,
3948 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3949 ToTAInfo, CanonInjType, nullptr);
3950
3951 } else {
3952 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3953 D->getTagKind(), DC,
3954 StartLoc, IdLoc,
3955 ClassTemplate,
3956 TemplateArgs,
3957 /*PrevDecl=*/nullptr);
3958 }
3959
Douglas Gregore2e50d332010-12-01 01:36:18 +00003960 D2->setSpecializationKind(D->getSpecializationKind());
3961
3962 // Add this specialization to the class template.
3963 ClassTemplate->AddSpecialization(D2, InsertPos);
3964
3965 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003966 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003967
3968 Importer.Imported(D, D2);
3969
3970 if (auto *TSI = D->getTypeAsWritten()) {
3971 TypeSourceInfo *TInfo = Importer.Import(TSI);
3972 if (!TInfo)
3973 return nullptr;
3974 D2->setTypeAsWritten(TInfo);
3975 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
3976 D2->setExternLoc(Importer.Import(D->getExternLoc()));
3977 }
3978
3979 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
3980 if (POI.isValid())
3981 D2->setPointOfInstantiation(POI);
3982 else if (D->getPointOfInstantiation().isValid())
3983 return nullptr;
3984
3985 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
3986
Douglas Gregore2e50d332010-12-01 01:36:18 +00003987 // Add the specialization to this context.
3988 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003989 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003990 }
3991 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00003992 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00003993 return nullptr;
3994
Douglas Gregore2e50d332010-12-01 01:36:18 +00003995 return D2;
3996}
3997
Larisse Voufo39a1e502013-08-06 01:03:05 +00003998Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
3999 // If this variable has a definition in the translation unit we're coming
4000 // from,
4001 // but this particular declaration is not that definition, import the
4002 // definition and map to that.
4003 VarDecl *Definition =
4004 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4005 if (Definition && Definition != D->getTemplatedDecl()) {
4006 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4007 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004008 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004009
4010 return Importer.Imported(D, ImportedDef);
4011 }
4012
4013 // Import the major distinguishing characteristics of this variable template.
4014 DeclContext *DC, *LexicalDC;
4015 DeclarationName Name;
4016 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004017 NamedDecl *ToD;
4018 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004019 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004020 if (ToD)
4021 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004022
4023 // We may already have a template of the same name; try to find and match it.
4024 assert(!DC->isFunctionOrMethod() &&
4025 "Variable templates cannot be declared at function scope");
4026 SmallVector<NamedDecl *, 4> ConflictingDecls;
4027 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004028 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004029 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4030 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4031 continue;
4032
4033 Decl *Found = FoundDecls[I];
4034 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4035 if (IsStructuralMatch(D, FoundTemplate)) {
4036 // The variable templates structurally match; call it the same template.
4037 Importer.Imported(D->getTemplatedDecl(),
4038 FoundTemplate->getTemplatedDecl());
4039 return Importer.Imported(D, FoundTemplate);
4040 }
4041 }
4042
4043 ConflictingDecls.push_back(FoundDecls[I]);
4044 }
4045
4046 if (!ConflictingDecls.empty()) {
4047 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4048 ConflictingDecls.data(),
4049 ConflictingDecls.size());
4050 }
4051
4052 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004054
4055 VarDecl *DTemplated = D->getTemplatedDecl();
4056
4057 // Import the type.
4058 QualType T = Importer.Import(DTemplated->getType());
4059 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004060 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004061
4062 // Create the declaration that is being templated.
4063 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4064 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4065 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4066 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4067 IdLoc, Name.getAsIdentifierInfo(), T,
4068 TInfo, DTemplated->getStorageClass());
4069 D2Templated->setAccess(DTemplated->getAccess());
4070 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4071 D2Templated->setLexicalDeclContext(LexicalDC);
4072
4073 // Importer.Imported(DTemplated, D2Templated);
4074 // LexicalDC->addDeclInternal(D2Templated);
4075
4076 // Merge the initializer.
4077 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004078 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004079
4080 // Create the variable template declaration itself.
4081 TemplateParameterList *TemplateParams =
4082 ImportTemplateParameterList(D->getTemplateParameters());
4083 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004084 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004085
4086 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004087 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004088 D2Templated->setDescribedVarTemplate(D2);
4089
4090 D2->setAccess(D->getAccess());
4091 D2->setLexicalDeclContext(LexicalDC);
4092 LexicalDC->addDeclInternal(D2);
4093
4094 // Note the relationship between the variable templates.
4095 Importer.Imported(D, D2);
4096 Importer.Imported(DTemplated, D2Templated);
4097
4098 if (DTemplated->isThisDeclarationADefinition() &&
4099 !D2Templated->isThisDeclarationADefinition()) {
4100 // FIXME: Import definition!
4101 }
4102
4103 return D2;
4104}
4105
4106Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4107 VarTemplateSpecializationDecl *D) {
4108 // If this record has a definition in the translation unit we're coming from,
4109 // but this particular declaration is not that definition, import the
4110 // definition and map to that.
4111 VarDecl *Definition = D->getDefinition();
4112 if (Definition && Definition != D) {
4113 Decl *ImportedDef = Importer.Import(Definition);
4114 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004115 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004116
4117 return Importer.Imported(D, ImportedDef);
4118 }
4119
4120 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4121 Importer.Import(D->getSpecializedTemplate()));
4122 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004123 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004124
4125 // Import the context of this declaration.
4126 DeclContext *DC = VarTemplate->getDeclContext();
4127 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004128 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004129
4130 DeclContext *LexicalDC = DC;
4131 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4132 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4133 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004134 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004135 }
4136
4137 // Import the location of this declaration.
4138 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4139 SourceLocation IdLoc = Importer.Import(D->getLocation());
4140
4141 // Import template arguments.
4142 SmallVector<TemplateArgument, 2> TemplateArgs;
4143 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4144 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004145 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004146
4147 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004148 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004149 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004150 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004151 if (D2) {
4152 // We already have a variable template specialization with these template
4153 // arguments.
4154
4155 // FIXME: Check for specialization vs. instantiation errors.
4156
4157 if (VarDecl *FoundDef = D2->getDefinition()) {
4158 if (!D->isThisDeclarationADefinition() ||
4159 IsStructuralMatch(D, FoundDef)) {
4160 // The record types structurally match, or the "from" translation
4161 // unit only had a forward declaration anyway; call it the same
4162 // variable.
4163 return Importer.Imported(D, FoundDef);
4164 }
4165 }
4166 } else {
4167
4168 // Import the type.
4169 QualType T = Importer.Import(D->getType());
4170 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004171 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004172 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4173
4174 // Create a new specialization.
4175 D2 = VarTemplateSpecializationDecl::Create(
4176 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00004177 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004178 D2->setSpecializationKind(D->getSpecializationKind());
4179 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4180
4181 // Add this specialization to the class template.
4182 VarTemplate->AddSpecialization(D2, InsertPos);
4183
4184 // Import the qualifier, if any.
4185 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4186
4187 // Add the specialization to this context.
4188 D2->setLexicalDeclContext(LexicalDC);
4189 LexicalDC->addDeclInternal(D2);
4190 }
4191 Importer.Imported(D, D2);
4192
4193 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004194 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004195
4196 return D2;
4197}
4198
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004199//----------------------------------------------------------------------------
4200// Import Statements
4201//----------------------------------------------------------------------------
4202
Sean Callanan59721b32015-04-28 18:41:46 +00004203DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4204 if (DG.isNull())
4205 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4206 size_t NumDecls = DG.end() - DG.begin();
4207 SmallVector<Decl *, 1> ToDecls(NumDecls);
4208 auto &_Importer = this->Importer;
4209 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4210 [&_Importer](Decl *D) -> Decl * {
4211 return _Importer.Import(D);
4212 });
4213 return DeclGroupRef::Create(Importer.getToContext(),
4214 ToDecls.begin(),
4215 NumDecls);
4216}
4217
4218 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4219 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4220 << S->getStmtClassName();
4221 return nullptr;
4222 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004223
4224
4225Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4226 SmallVector<IdentifierInfo *, 4> Names;
4227 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4228 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004229 // ToII is nullptr when no symbolic name is given for output operand
4230 // see ParseStmtAsm::ParseAsmOperandsOpt
4231 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004232 return nullptr;
4233 Names.push_back(ToII);
4234 }
4235 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4236 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00004237 // ToII is nullptr when no symbolic name is given for input operand
4238 // see ParseStmtAsm::ParseAsmOperandsOpt
4239 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004240 return nullptr;
4241 Names.push_back(ToII);
4242 }
4243
4244 SmallVector<StringLiteral *, 4> Clobbers;
4245 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4246 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4247 Importer.Import(S->getClobberStringLiteral(I)));
4248 if (!Clobber)
4249 return nullptr;
4250 Clobbers.push_back(Clobber);
4251 }
4252
4253 SmallVector<StringLiteral *, 4> Constraints;
4254 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4255 StringLiteral *Output = cast_or_null<StringLiteral>(
4256 Importer.Import(S->getOutputConstraintLiteral(I)));
4257 if (!Output)
4258 return nullptr;
4259 Constraints.push_back(Output);
4260 }
4261
4262 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4263 StringLiteral *Input = cast_or_null<StringLiteral>(
4264 Importer.Import(S->getInputConstraintLiteral(I)));
4265 if (!Input)
4266 return nullptr;
4267 Constraints.push_back(Input);
4268 }
4269
4270 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004271 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004272 return nullptr;
4273
Aleksei Sidorina693b372016-09-28 10:16:56 +00004274 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004275 return nullptr;
4276
4277 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4278 Importer.Import(S->getAsmString()));
4279 if (!AsmStr)
4280 return nullptr;
4281
4282 return new (Importer.getToContext()) GCCAsmStmt(
4283 Importer.getToContext(),
4284 Importer.Import(S->getAsmLoc()),
4285 S->isSimple(),
4286 S->isVolatile(),
4287 S->getNumOutputs(),
4288 S->getNumInputs(),
4289 Names.data(),
4290 Constraints.data(),
4291 Exprs.data(),
4292 AsmStr,
4293 S->getNumClobbers(),
4294 Clobbers.data(),
4295 Importer.Import(S->getRParenLoc()));
4296}
4297
Sean Callanan59721b32015-04-28 18:41:46 +00004298Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4299 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4300 for (Decl *ToD : ToDG) {
4301 if (!ToD)
4302 return nullptr;
4303 }
4304 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4305 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4306 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4307}
4308
4309Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4310 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4311 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4312 S->hasLeadingEmptyMacro());
4313}
4314
4315Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004316 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004317
4318 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00004319 return nullptr;
4320
Sean Callanan59721b32015-04-28 18:41:46 +00004321 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4322 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4323 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4324 ToStmts,
4325 ToLBraceLoc, ToRBraceLoc);
4326}
4327
4328Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4329 Expr *ToLHS = Importer.Import(S->getLHS());
4330 if (!ToLHS)
4331 return nullptr;
4332 Expr *ToRHS = Importer.Import(S->getRHS());
4333 if (!ToRHS && S->getRHS())
4334 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00004335 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4336 if (!ToSubStmt && S->getSubStmt())
4337 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004338 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4339 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4340 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00004341 CaseStmt *ToStmt = new (Importer.getToContext())
4342 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
4343 ToStmt->setSubStmt(ToSubStmt);
4344 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00004345}
4346
4347Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4348 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4349 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4350 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4351 if (!ToSubStmt && S->getSubStmt())
4352 return nullptr;
4353 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4354 ToSubStmt);
4355}
4356
4357Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4358 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4359 LabelDecl *ToLabelDecl =
4360 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4361 if (!ToLabelDecl && S->getDecl())
4362 return nullptr;
4363 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4364 if (!ToSubStmt && S->getSubStmt())
4365 return nullptr;
4366 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4367 ToSubStmt);
4368}
4369
4370Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4371 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4372 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4373 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4374 ASTContext &_ToContext = Importer.getToContext();
4375 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4376 [&_ToContext](const Attr *A) -> const Attr * {
4377 return A->clone(_ToContext);
4378 });
4379 for (const Attr *ToA : ToAttrs) {
4380 if (!ToA)
4381 return nullptr;
4382 }
4383 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4384 if (!ToSubStmt && S->getSubStmt())
4385 return nullptr;
4386 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4387 ToAttrs, ToSubStmt);
4388}
4389
4390Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4391 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004392 Stmt *ToInit = Importer.Import(S->getInit());
4393 if (!ToInit && S->getInit())
4394 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004395 VarDecl *ToConditionVariable = nullptr;
4396 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4397 ToConditionVariable =
4398 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4399 if (!ToConditionVariable)
4400 return nullptr;
4401 }
4402 Expr *ToCondition = Importer.Import(S->getCond());
4403 if (!ToCondition && S->getCond())
4404 return nullptr;
4405 Stmt *ToThenStmt = Importer.Import(S->getThen());
4406 if (!ToThenStmt && S->getThen())
4407 return nullptr;
4408 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4409 Stmt *ToElseStmt = Importer.Import(S->getElse());
4410 if (!ToElseStmt && S->getElse())
4411 return nullptr;
4412 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004413 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004414 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004415 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004416 ToCondition, ToThenStmt,
4417 ToElseLoc, ToElseStmt);
4418}
4419
4420Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004421 Stmt *ToInit = Importer.Import(S->getInit());
4422 if (!ToInit && S->getInit())
4423 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004424 VarDecl *ToConditionVariable = nullptr;
4425 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4426 ToConditionVariable =
4427 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4428 if (!ToConditionVariable)
4429 return nullptr;
4430 }
4431 Expr *ToCondition = Importer.Import(S->getCond());
4432 if (!ToCondition && S->getCond())
4433 return nullptr;
4434 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004435 Importer.getToContext(), ToInit,
4436 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004437 Stmt *ToBody = Importer.Import(S->getBody());
4438 if (!ToBody && S->getBody())
4439 return nullptr;
4440 ToStmt->setBody(ToBody);
4441 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4442 // Now we have to re-chain the cases.
4443 SwitchCase *LastChainedSwitchCase = nullptr;
4444 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4445 SC = SC->getNextSwitchCase()) {
4446 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4447 if (!ToSC)
4448 return nullptr;
4449 if (LastChainedSwitchCase)
4450 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4451 else
4452 ToStmt->setSwitchCaseList(ToSC);
4453 LastChainedSwitchCase = ToSC;
4454 }
4455 return ToStmt;
4456}
4457
4458Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4459 VarDecl *ToConditionVariable = nullptr;
4460 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4461 ToConditionVariable =
4462 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4463 if (!ToConditionVariable)
4464 return nullptr;
4465 }
4466 Expr *ToCondition = Importer.Import(S->getCond());
4467 if (!ToCondition && S->getCond())
4468 return nullptr;
4469 Stmt *ToBody = Importer.Import(S->getBody());
4470 if (!ToBody && S->getBody())
4471 return nullptr;
4472 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4473 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4474 ToConditionVariable,
4475 ToCondition, ToBody,
4476 ToWhileLoc);
4477}
4478
4479Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4480 Stmt *ToBody = Importer.Import(S->getBody());
4481 if (!ToBody && S->getBody())
4482 return nullptr;
4483 Expr *ToCondition = Importer.Import(S->getCond());
4484 if (!ToCondition && S->getCond())
4485 return nullptr;
4486 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4487 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4488 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4489 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4490 ToDoLoc, ToWhileLoc,
4491 ToRParenLoc);
4492}
4493
4494Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4495 Stmt *ToInit = Importer.Import(S->getInit());
4496 if (!ToInit && S->getInit())
4497 return nullptr;
4498 Expr *ToCondition = Importer.Import(S->getCond());
4499 if (!ToCondition && S->getCond())
4500 return nullptr;
4501 VarDecl *ToConditionVariable = nullptr;
4502 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4503 ToConditionVariable =
4504 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4505 if (!ToConditionVariable)
4506 return nullptr;
4507 }
4508 Expr *ToInc = Importer.Import(S->getInc());
4509 if (!ToInc && S->getInc())
4510 return nullptr;
4511 Stmt *ToBody = Importer.Import(S->getBody());
4512 if (!ToBody && S->getBody())
4513 return nullptr;
4514 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4515 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4516 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4517 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4518 ToInit, ToCondition,
4519 ToConditionVariable,
4520 ToInc, ToBody,
4521 ToForLoc, ToLParenLoc,
4522 ToRParenLoc);
4523}
4524
4525Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4526 LabelDecl *ToLabel = nullptr;
4527 if (LabelDecl *FromLabel = S->getLabel()) {
4528 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4529 if (!ToLabel)
4530 return nullptr;
4531 }
4532 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4533 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4534 return new (Importer.getToContext()) GotoStmt(ToLabel,
4535 ToGotoLoc, ToLabelLoc);
4536}
4537
4538Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4539 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4540 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4541 Expr *ToTarget = Importer.Import(S->getTarget());
4542 if (!ToTarget && S->getTarget())
4543 return nullptr;
4544 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4545 ToTarget);
4546}
4547
4548Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4549 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4550 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4551}
4552
4553Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4554 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4555 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4556}
4557
4558Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4559 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4560 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4561 if (!ToRetExpr && S->getRetValue())
4562 return nullptr;
4563 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4564 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4565 if (!ToNRVOCandidate && NRVOCandidate)
4566 return nullptr;
4567 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4568 ToNRVOCandidate);
4569}
4570
4571Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4572 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4573 VarDecl *ToExceptionDecl = nullptr;
4574 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4575 ToExceptionDecl =
4576 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4577 if (!ToExceptionDecl)
4578 return nullptr;
4579 }
4580 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4581 if (!ToHandlerBlock && S->getHandlerBlock())
4582 return nullptr;
4583 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4584 ToExceptionDecl,
4585 ToHandlerBlock);
4586}
4587
4588Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4589 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4590 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4591 if (!ToTryBlock && S->getTryBlock())
4592 return nullptr;
4593 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4594 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4595 CXXCatchStmt *FromHandler = S->getHandler(HI);
4596 if (Stmt *ToHandler = Importer.Import(FromHandler))
4597 ToHandlers[HI] = ToHandler;
4598 else
4599 return nullptr;
4600 }
4601 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4602 ToHandlers);
4603}
4604
4605Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4606 DeclStmt *ToRange =
4607 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4608 if (!ToRange && S->getRangeStmt())
4609 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004610 DeclStmt *ToBegin =
4611 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4612 if (!ToBegin && S->getBeginStmt())
4613 return nullptr;
4614 DeclStmt *ToEnd =
4615 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4616 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004617 return nullptr;
4618 Expr *ToCond = Importer.Import(S->getCond());
4619 if (!ToCond && S->getCond())
4620 return nullptr;
4621 Expr *ToInc = Importer.Import(S->getInc());
4622 if (!ToInc && S->getInc())
4623 return nullptr;
4624 DeclStmt *ToLoopVar =
4625 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4626 if (!ToLoopVar && S->getLoopVarStmt())
4627 return nullptr;
4628 Stmt *ToBody = Importer.Import(S->getBody());
4629 if (!ToBody && S->getBody())
4630 return nullptr;
4631 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004632 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004633 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4634 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004635 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004636 ToCond, ToInc,
4637 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004638 ToForLoc, ToCoawaitLoc,
4639 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004640}
4641
4642Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4643 Stmt *ToElem = Importer.Import(S->getElement());
4644 if (!ToElem && S->getElement())
4645 return nullptr;
4646 Expr *ToCollect = Importer.Import(S->getCollection());
4647 if (!ToCollect && S->getCollection())
4648 return nullptr;
4649 Stmt *ToBody = Importer.Import(S->getBody());
4650 if (!ToBody && S->getBody())
4651 return nullptr;
4652 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4653 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4654 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4655 ToCollect,
4656 ToBody, ToForLoc,
4657 ToRParenLoc);
4658}
4659
4660Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4661 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4662 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4663 VarDecl *ToExceptionDecl = nullptr;
4664 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4665 ToExceptionDecl =
4666 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4667 if (!ToExceptionDecl)
4668 return nullptr;
4669 }
4670 Stmt *ToBody = Importer.Import(S->getCatchBody());
4671 if (!ToBody && S->getCatchBody())
4672 return nullptr;
4673 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4674 ToRParenLoc,
4675 ToExceptionDecl,
4676 ToBody);
4677}
4678
4679Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4680 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4681 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4682 if (!ToAtFinallyStmt && S->getFinallyBody())
4683 return nullptr;
4684 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4685 ToAtFinallyStmt);
4686}
4687
4688Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4689 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4690 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4691 if (!ToAtTryStmt && S->getTryBody())
4692 return nullptr;
4693 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4694 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4695 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4696 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4697 ToCatchStmts[CI] = ToCatchStmt;
4698 else
4699 return nullptr;
4700 }
4701 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4702 if (!ToAtFinallyStmt && S->getFinallyStmt())
4703 return nullptr;
4704 return ObjCAtTryStmt::Create(Importer.getToContext(),
4705 ToAtTryLoc, ToAtTryStmt,
4706 ToCatchStmts.begin(), ToCatchStmts.size(),
4707 ToAtFinallyStmt);
4708}
4709
4710Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4711 (ObjCAtSynchronizedStmt *S) {
4712 SourceLocation ToAtSynchronizedLoc =
4713 Importer.Import(S->getAtSynchronizedLoc());
4714 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4715 if (!ToSynchExpr && S->getSynchExpr())
4716 return nullptr;
4717 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4718 if (!ToSynchBody && S->getSynchBody())
4719 return nullptr;
4720 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4721 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4722}
4723
4724Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4725 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4726 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4727 if (!ToThrow && S->getThrowExpr())
4728 return nullptr;
4729 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4730}
4731
4732Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4733 (ObjCAutoreleasePoolStmt *S) {
4734 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4735 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4736 if (!ToSubStmt && S->getSubStmt())
4737 return nullptr;
4738 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4739 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004740}
4741
4742//----------------------------------------------------------------------------
4743// Import Expressions
4744//----------------------------------------------------------------------------
4745Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4746 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4747 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004748 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004749}
4750
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004751Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4752 QualType T = Importer.Import(E->getType());
4753 if (T.isNull())
4754 return nullptr;
4755
4756 Expr *SubExpr = Importer.Import(E->getSubExpr());
4757 if (!SubExpr && E->getSubExpr())
4758 return nullptr;
4759
4760 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4761 if (!TInfo)
4762 return nullptr;
4763
4764 return new (Importer.getToContext()) VAArgExpr(
4765 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4766 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4767}
4768
4769
4770Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4771 QualType T = Importer.Import(E->getType());
4772 if (T.isNull())
4773 return nullptr;
4774
4775 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004776 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004777}
4778
4779Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4780 QualType T = Importer.Import(E->getType());
4781 if (T.isNull())
4782 return nullptr;
4783
4784 StringLiteral *SL = cast_or_null<StringLiteral>(
4785 Importer.Import(E->getFunctionName()));
4786 if (!SL && E->getFunctionName())
4787 return nullptr;
4788
4789 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004790 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004791}
4792
Douglas Gregor52f820e2010-02-19 01:17:02 +00004793Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004794 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4795 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004796 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004797
Craig Topper36250ad2014-05-12 05:36:57 +00004798 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004799 if (E->getDecl() != E->getFoundDecl()) {
4800 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4801 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004802 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004803 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004804
4805 QualType T = Importer.Import(E->getType());
4806 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004807 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004808
Aleksei Sidorina693b372016-09-28 10:16:56 +00004809
4810 TemplateArgumentListInfo ToTAInfo;
4811 TemplateArgumentListInfo *ResInfo = nullptr;
4812 if (E->hasExplicitTemplateArgs()) {
4813 for (const auto &FromLoc : E->template_arguments()) {
Aleksei Sidorin2697f8e2017-11-21 16:08:41 +00004814 if (auto ToTALoc = ImportTemplateArgumentLoc(FromLoc))
4815 ToTAInfo.addArgument(*ToTALoc);
4816 else
Aleksei Sidorina693b372016-09-28 10:16:56 +00004817 return nullptr;
Aleksei Sidorina693b372016-09-28 10:16:56 +00004818 }
4819 ResInfo = &ToTAInfo;
4820 }
4821
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004822 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4823 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004824 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004825 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004826 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004827 Importer.Import(E->getLocation()),
4828 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00004829 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004830 if (E->hadMultipleCandidates())
4831 DRE->setHadMultipleCandidates(true);
4832 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004833}
4834
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004835Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4836 QualType T = Importer.Import(E->getType());
4837 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00004838 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004839
4840 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4841}
4842
4843ASTNodeImporter::Designator
4844ASTNodeImporter::ImportDesignator(const Designator &D) {
4845 if (D.isFieldDesignator()) {
4846 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4847 // Caller checks for import error
4848 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4849 Importer.Import(D.getFieldLoc()));
4850 }
4851 if (D.isArrayDesignator())
4852 return Designator(D.getFirstExprIndex(),
4853 Importer.Import(D.getLBracketLoc()),
4854 Importer.Import(D.getRBracketLoc()));
4855
4856 assert(D.isArrayRangeDesignator());
4857 return Designator(D.getFirstExprIndex(),
4858 Importer.Import(D.getLBracketLoc()),
4859 Importer.Import(D.getEllipsisLoc()),
4860 Importer.Import(D.getRBracketLoc()));
4861}
4862
4863
4864Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4865 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4866 if (!Init)
4867 return nullptr;
4868
4869 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4870 // List elements from the second, the first is Init itself
4871 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4872 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4873 IndexExprs[I - 1] = Arg;
4874 else
4875 return nullptr;
4876 }
4877
4878 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00004879 llvm::transform(DIE->designators(), Designators.begin(),
4880 [this](const Designator &D) -> Designator {
4881 return ImportDesignator(D);
4882 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004883
David Majnemerf7e36092016-06-23 00:15:04 +00004884 for (const Designator &D : DIE->designators())
4885 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004886 return nullptr;
4887
4888 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00004889 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004890 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4891 DIE->usesGNUSyntax(), Init);
4892}
4893
4894Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4895 QualType T = Importer.Import(E->getType());
4896 if (T.isNull())
4897 return nullptr;
4898
4899 return new (Importer.getToContext())
4900 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4901}
4902
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004903Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4904 QualType T = Importer.Import(E->getType());
4905 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004906 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004907
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004908 return IntegerLiteral::Create(Importer.getToContext(),
4909 E->getValue(), T,
4910 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004911}
4912
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004913Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4914 QualType T = Importer.Import(E->getType());
4915 if (T.isNull())
4916 return nullptr;
4917
4918 return FloatingLiteral::Create(Importer.getToContext(),
4919 E->getValue(), E->isExact(), T,
4920 Importer.Import(E->getLocation()));
4921}
4922
Douglas Gregor623421d2010-02-18 02:21:22 +00004923Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4924 QualType T = Importer.Import(E->getType());
4925 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004926 return nullptr;
4927
Douglas Gregorfb65e592011-07-27 05:40:30 +00004928 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4929 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004930 Importer.Import(E->getLocation()));
4931}
4932
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004933Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
4934 QualType T = Importer.Import(E->getType());
4935 if (T.isNull())
4936 return nullptr;
4937
4938 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
4939 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
4940
4941 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
4942 E->getKind(), E->isPascal(), T,
4943 Locations.data(), Locations.size());
4944}
4945
4946Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
4947 QualType T = Importer.Import(E->getType());
4948 if (T.isNull())
4949 return nullptr;
4950
4951 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
4952 if (!TInfo)
4953 return nullptr;
4954
4955 Expr *Init = Importer.Import(E->getInitializer());
4956 if (!Init)
4957 return nullptr;
4958
4959 return new (Importer.getToContext()) CompoundLiteralExpr(
4960 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
4961 Init, E->isFileScope());
4962}
4963
4964Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
4965 QualType T = Importer.Import(E->getType());
4966 if (T.isNull())
4967 return nullptr;
4968
4969 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
4970 if (ImportArrayChecked(
4971 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
4972 Exprs.begin()))
4973 return nullptr;
4974
4975 return new (Importer.getToContext()) AtomicExpr(
4976 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
4977 Importer.Import(E->getRParenLoc()));
4978}
4979
4980Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
4981 QualType T = Importer.Import(E->getType());
4982 if (T.isNull())
4983 return nullptr;
4984
4985 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
4986 if (!ToLabel)
4987 return nullptr;
4988
4989 return new (Importer.getToContext()) AddrLabelExpr(
4990 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
4991 ToLabel, T);
4992}
4993
Douglas Gregorc74247e2010-02-19 01:07:06 +00004994Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4995 Expr *SubExpr = Importer.Import(E->getSubExpr());
4996 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004997 return nullptr;
4998
Douglas Gregorc74247e2010-02-19 01:07:06 +00004999 return new (Importer.getToContext())
5000 ParenExpr(Importer.Import(E->getLParen()),
5001 Importer.Import(E->getRParen()),
5002 SubExpr);
5003}
5004
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005005Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5006 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005007 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005008 return nullptr;
5009
5010 return new (Importer.getToContext()) ParenListExpr(
5011 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5012 Exprs, Importer.Import(E->getLParenLoc()));
5013}
5014
5015Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5016 QualType T = Importer.Import(E->getType());
5017 if (T.isNull())
5018 return nullptr;
5019
5020 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5021 Importer.Import(E->getSubStmt()));
5022 if (!ToSubStmt && E->getSubStmt())
5023 return nullptr;
5024
5025 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5026 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5027}
5028
Douglas Gregorc74247e2010-02-19 01:07:06 +00005029Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5030 QualType T = Importer.Import(E->getType());
5031 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005032 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005033
5034 Expr *SubExpr = Importer.Import(E->getSubExpr());
5035 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005036 return nullptr;
5037
Douglas Gregorc74247e2010-02-19 01:07:06 +00005038 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005039 T, E->getValueKind(),
5040 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005041 Importer.Import(E->getOperatorLoc()));
5042}
5043
Peter Collingbournee190dee2011-03-11 19:24:49 +00005044Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5045 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005046 QualType ResultType = Importer.Import(E->getType());
5047
5048 if (E->isArgumentType()) {
5049 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5050 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005051 return nullptr;
5052
Peter Collingbournee190dee2011-03-11 19:24:49 +00005053 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5054 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005055 Importer.Import(E->getOperatorLoc()),
5056 Importer.Import(E->getRParenLoc()));
5057 }
5058
5059 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5060 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005061 return nullptr;
5062
Peter Collingbournee190dee2011-03-11 19:24:49 +00005063 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5064 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005065 Importer.Import(E->getOperatorLoc()),
5066 Importer.Import(E->getRParenLoc()));
5067}
5068
Douglas Gregorc74247e2010-02-19 01:07:06 +00005069Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5070 QualType T = Importer.Import(E->getType());
5071 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005072 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005073
5074 Expr *LHS = Importer.Import(E->getLHS());
5075 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005076 return nullptr;
5077
Douglas Gregorc74247e2010-02-19 01:07:06 +00005078 Expr *RHS = Importer.Import(E->getRHS());
5079 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005080 return nullptr;
5081
Douglas Gregorc74247e2010-02-19 01:07:06 +00005082 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005083 T, E->getValueKind(),
5084 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005085 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005086 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005087}
5088
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005089Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5090 QualType T = Importer.Import(E->getType());
5091 if (T.isNull())
5092 return nullptr;
5093
5094 Expr *ToLHS = Importer.Import(E->getLHS());
5095 if (!ToLHS)
5096 return nullptr;
5097
5098 Expr *ToRHS = Importer.Import(E->getRHS());
5099 if (!ToRHS)
5100 return nullptr;
5101
5102 Expr *ToCond = Importer.Import(E->getCond());
5103 if (!ToCond)
5104 return nullptr;
5105
5106 return new (Importer.getToContext()) ConditionalOperator(
5107 ToCond, Importer.Import(E->getQuestionLoc()),
5108 ToLHS, Importer.Import(E->getColonLoc()),
5109 ToRHS, T, E->getValueKind(), E->getObjectKind());
5110}
5111
5112Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5113 BinaryConditionalOperator *E) {
5114 QualType T = Importer.Import(E->getType());
5115 if (T.isNull())
5116 return nullptr;
5117
5118 Expr *Common = Importer.Import(E->getCommon());
5119 if (!Common)
5120 return nullptr;
5121
5122 Expr *Cond = Importer.Import(E->getCond());
5123 if (!Cond)
5124 return nullptr;
5125
5126 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5127 Importer.Import(E->getOpaqueValue()));
5128 if (!OpaqueValue)
5129 return nullptr;
5130
5131 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5132 if (!TrueExpr)
5133 return nullptr;
5134
5135 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5136 if (!FalseExpr)
5137 return nullptr;
5138
5139 return new (Importer.getToContext()) BinaryConditionalOperator(
5140 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5141 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5142 T, E->getValueKind(), E->getObjectKind());
5143}
5144
Aleksei Sidorina693b372016-09-28 10:16:56 +00005145Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
5146 QualType T = Importer.Import(E->getType());
5147 if (T.isNull())
5148 return nullptr;
5149
5150 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
5151 if (!ToQueried)
5152 return nullptr;
5153
5154 Expr *Dim = Importer.Import(E->getDimensionExpression());
5155 if (!Dim && E->getDimensionExpression())
5156 return nullptr;
5157
5158 return new (Importer.getToContext()) ArrayTypeTraitExpr(
5159 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5160 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
5161}
5162
5163Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
5164 QualType T = Importer.Import(E->getType());
5165 if (T.isNull())
5166 return nullptr;
5167
5168 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
5169 if (!ToQueried)
5170 return nullptr;
5171
5172 return new (Importer.getToContext()) ExpressionTraitExpr(
5173 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
5174 E->getValue(), Importer.Import(E->getLocEnd()), T);
5175}
5176
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005177Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5178 QualType T = Importer.Import(E->getType());
5179 if (T.isNull())
5180 return nullptr;
5181
5182 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5183 if (!SourceExpr && E->getSourceExpr())
5184 return nullptr;
5185
5186 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00005187 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005188 E->getObjectKind(), SourceExpr);
5189}
5190
Aleksei Sidorina693b372016-09-28 10:16:56 +00005191Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
5192 QualType T = Importer.Import(E->getType());
5193 if (T.isNull())
5194 return nullptr;
5195
5196 Expr *ToLHS = Importer.Import(E->getLHS());
5197 if (!ToLHS)
5198 return nullptr;
5199
5200 Expr *ToRHS = Importer.Import(E->getRHS());
5201 if (!ToRHS)
5202 return nullptr;
5203
5204 return new (Importer.getToContext()) ArraySubscriptExpr(
5205 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
5206 Importer.Import(E->getRBracketLoc()));
5207}
5208
Douglas Gregorc74247e2010-02-19 01:07:06 +00005209Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5210 QualType T = Importer.Import(E->getType());
5211 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005212 return nullptr;
5213
Douglas Gregorc74247e2010-02-19 01:07:06 +00005214 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5215 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005216 return nullptr;
5217
Douglas Gregorc74247e2010-02-19 01:07:06 +00005218 QualType CompResultType = Importer.Import(E->getComputationResultType());
5219 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005220 return nullptr;
5221
Douglas Gregorc74247e2010-02-19 01:07:06 +00005222 Expr *LHS = Importer.Import(E->getLHS());
5223 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005224 return nullptr;
5225
Douglas Gregorc74247e2010-02-19 01:07:06 +00005226 Expr *RHS = Importer.Import(E->getRHS());
5227 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005228 return nullptr;
5229
Douglas Gregorc74247e2010-02-19 01:07:06 +00005230 return new (Importer.getToContext())
5231 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005232 T, E->getValueKind(),
5233 E->getObjectKind(),
5234 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005235 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00005236 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005237}
5238
Aleksei Sidorina693b372016-09-28 10:16:56 +00005239bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
5240 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
5241 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
5242 Path.push_back(Spec);
5243 else
5244 return true;
5245 }
5246 return false;
John McCallcf142162010-08-07 06:22:56 +00005247}
5248
Douglas Gregor98c10182010-02-12 22:17:39 +00005249Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5250 QualType T = Importer.Import(E->getType());
5251 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005252 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005253
5254 Expr *SubExpr = Importer.Import(E->getSubExpr());
5255 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005256 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005257
5258 CXXCastPath BasePath;
5259 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005260 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005261
5262 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005263 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005264}
5265
Aleksei Sidorina693b372016-09-28 10:16:56 +00005266Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00005267 QualType T = Importer.Import(E->getType());
5268 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005269 return nullptr;
5270
Douglas Gregor5481d322010-02-19 01:32:14 +00005271 Expr *SubExpr = Importer.Import(E->getSubExpr());
5272 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005273 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005274
5275 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5276 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005277 return nullptr;
5278
John McCallcf142162010-08-07 06:22:56 +00005279 CXXCastPath BasePath;
5280 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005281 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005282
Aleksei Sidorina693b372016-09-28 10:16:56 +00005283 switch (E->getStmtClass()) {
5284 case Stmt::CStyleCastExprClass: {
5285 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
5286 return CStyleCastExpr::Create(Importer.getToContext(), T,
5287 E->getValueKind(), E->getCastKind(),
5288 SubExpr, &BasePath, TInfo,
5289 Importer.Import(CCE->getLParenLoc()),
5290 Importer.Import(CCE->getRParenLoc()));
5291 }
5292
5293 case Stmt::CXXFunctionalCastExprClass: {
5294 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
5295 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
5296 E->getValueKind(), TInfo,
5297 E->getCastKind(), SubExpr, &BasePath,
5298 Importer.Import(FCE->getLParenLoc()),
5299 Importer.Import(FCE->getRParenLoc()));
5300 }
5301
5302 case Stmt::ObjCBridgedCastExprClass: {
5303 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
5304 return new (Importer.getToContext()) ObjCBridgedCastExpr(
5305 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
5306 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
5307 TInfo, SubExpr);
5308 }
5309 default:
5310 break; // just fall through
5311 }
5312
5313 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
5314 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
5315 RParenLoc = Importer.Import(Named->getRParenLoc());
5316 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
5317
5318 switch (E->getStmtClass()) {
5319 case Stmt::CXXStaticCastExprClass:
5320 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
5321 E->getValueKind(), E->getCastKind(),
5322 SubExpr, &BasePath, TInfo,
5323 ExprLoc, RParenLoc, Brackets);
5324
5325 case Stmt::CXXDynamicCastExprClass:
5326 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
5327 E->getValueKind(), E->getCastKind(),
5328 SubExpr, &BasePath, TInfo,
5329 ExprLoc, RParenLoc, Brackets);
5330
5331 case Stmt::CXXReinterpretCastExprClass:
5332 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
5333 E->getValueKind(), E->getCastKind(),
5334 SubExpr, &BasePath, TInfo,
5335 ExprLoc, RParenLoc, Brackets);
5336
5337 case Stmt::CXXConstCastExprClass:
5338 return CXXConstCastExpr::Create(Importer.getToContext(), T,
5339 E->getValueKind(), SubExpr, TInfo, ExprLoc,
5340 RParenLoc, Brackets);
5341 default:
5342 llvm_unreachable("Cast expression of unsupported type!");
5343 return nullptr;
5344 }
5345}
5346
5347Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5348 QualType T = Importer.Import(OE->getType());
5349 if (T.isNull())
5350 return nullptr;
5351
5352 SmallVector<OffsetOfNode, 4> Nodes;
5353 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5354 const OffsetOfNode &Node = OE->getComponent(I);
5355
5356 switch (Node.getKind()) {
5357 case OffsetOfNode::Array:
5358 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5359 Node.getArrayExprIndex(),
5360 Importer.Import(Node.getLocEnd())));
5361 break;
5362
5363 case OffsetOfNode::Base: {
5364 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5365 if (!BS && Node.getBase())
5366 return nullptr;
5367 Nodes.push_back(OffsetOfNode(BS));
5368 break;
5369 }
5370 case OffsetOfNode::Field: {
5371 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5372 if (!FD)
5373 return nullptr;
5374 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5375 Importer.Import(Node.getLocEnd())));
5376 break;
5377 }
5378 case OffsetOfNode::Identifier: {
5379 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5380 if (!ToII)
5381 return nullptr;
5382 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5383 Importer.Import(Node.getLocEnd())));
5384 break;
5385 }
5386 }
5387 }
5388
5389 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5390 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5391 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5392 if (!ToIndexExpr)
5393 return nullptr;
5394 Exprs[I] = ToIndexExpr;
5395 }
5396
5397 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5398 if (!TInfo && OE->getTypeSourceInfo())
5399 return nullptr;
5400
5401 return OffsetOfExpr::Create(Importer.getToContext(), T,
5402 Importer.Import(OE->getOperatorLoc()),
5403 TInfo, Nodes, Exprs,
5404 Importer.Import(OE->getRParenLoc()));
5405}
5406
5407Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5408 QualType T = Importer.Import(E->getType());
5409 if (T.isNull())
5410 return nullptr;
5411
5412 Expr *Operand = Importer.Import(E->getOperand());
5413 if (!Operand)
5414 return nullptr;
5415
5416 CanThrowResult CanThrow;
5417 if (E->isValueDependent())
5418 CanThrow = CT_Dependent;
5419 else
5420 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5421
5422 return new (Importer.getToContext()) CXXNoexceptExpr(
5423 T, Operand, CanThrow,
5424 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5425}
5426
5427Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5428 QualType T = Importer.Import(E->getType());
5429 if (T.isNull())
5430 return nullptr;
5431
5432 Expr *SubExpr = Importer.Import(E->getSubExpr());
5433 if (!SubExpr && E->getSubExpr())
5434 return nullptr;
5435
5436 return new (Importer.getToContext()) CXXThrowExpr(
5437 SubExpr, T, Importer.Import(E->getThrowLoc()),
5438 E->isThrownVariableInScope());
5439}
5440
5441Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5442 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5443 Importer.Import(E->getParam()));
5444 if (!Param)
5445 return nullptr;
5446
5447 return CXXDefaultArgExpr::Create(
5448 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5449}
5450
5451Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5452 QualType T = Importer.Import(E->getType());
5453 if (T.isNull())
5454 return nullptr;
5455
5456 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5457 if (!TypeInfo)
5458 return nullptr;
5459
5460 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5461 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5462}
5463
5464Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5465 Expr *SubExpr = Importer.Import(E->getSubExpr());
5466 if (!SubExpr)
5467 return nullptr;
5468
5469 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5470 Importer.Import(const_cast<CXXDestructorDecl *>(
5471 E->getTemporary()->getDestructor())));
5472 if (!Dtor)
5473 return nullptr;
5474
5475 ASTContext &ToCtx = Importer.getToContext();
5476 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5477 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5478}
5479
5480Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5481 QualType T = Importer.Import(CE->getType());
5482 if (T.isNull())
5483 return nullptr;
5484
5485 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5486 if (ImportContainerChecked(CE->arguments(), Args))
5487 return nullptr;
5488
5489 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5490 Importer.Import(CE->getConstructor()));
5491 if (!Ctor)
5492 return nullptr;
5493
5494 return CXXTemporaryObjectExpr::Create(
5495 Importer.getToContext(), T,
5496 Importer.Import(CE->getLocStart()),
5497 Ctor,
5498 CE->isElidable(),
5499 Args,
5500 CE->hadMultipleCandidates(),
5501 CE->isListInitialization(),
5502 CE->isStdInitListInitialization(),
5503 CE->requiresZeroInitialization(),
5504 CE->getConstructionKind(),
5505 Importer.Import(CE->getParenOrBraceRange()));
5506}
5507
5508Expr *
5509ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5510 QualType T = Importer.Import(E->getType());
5511 if (T.isNull())
5512 return nullptr;
5513
5514 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5515 if (!TempE)
5516 return nullptr;
5517
5518 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5519 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5520 if (!ExtendedBy && E->getExtendingDecl())
5521 return nullptr;
5522
5523 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5524 T, TempE, E->isBoundToLvalueReference());
5525
5526 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5527 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5528 return ToMTE;
5529}
5530
Gabor Horvath7a91c082017-11-14 11:30:38 +00005531Expr *ASTNodeImporter::VisitPackExpansionExpr(PackExpansionExpr *E) {
5532 QualType T = Importer.Import(E->getType());
5533 if (T.isNull())
5534 return nullptr;
5535
5536 Expr *Pattern = Importer.Import(E->getPattern());
5537 if (!Pattern)
5538 return nullptr;
5539
5540 return new (Importer.getToContext()) PackExpansionExpr(
5541 T, Pattern, Importer.Import(E->getEllipsisLoc()),
5542 E->getNumExpansions());
5543}
5544
Aleksei Sidorina693b372016-09-28 10:16:56 +00005545Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5546 QualType T = Importer.Import(CE->getType());
5547 if (T.isNull())
5548 return nullptr;
5549
5550 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5551 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5552 return nullptr;
5553
5554 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5555 Importer.Import(CE->getOperatorNew()));
5556 if (!OperatorNewDecl && CE->getOperatorNew())
5557 return nullptr;
5558
5559 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5560 Importer.Import(CE->getOperatorDelete()));
5561 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5562 return nullptr;
5563
5564 Expr *ToInit = Importer.Import(CE->getInitializer());
5565 if (!ToInit && CE->getInitializer())
5566 return nullptr;
5567
5568 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5569 if (!TInfo)
5570 return nullptr;
5571
5572 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5573 if (!ToArrSize && CE->getArraySize())
5574 return nullptr;
5575
5576 return new (Importer.getToContext()) CXXNewExpr(
5577 Importer.getToContext(),
5578 CE->isGlobalNew(),
5579 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005580 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005581 CE->doesUsualArrayDeleteWantSize(),
5582 PlacementArgs,
5583 Importer.Import(CE->getTypeIdParens()),
5584 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5585 Importer.Import(CE->getSourceRange()),
5586 Importer.Import(CE->getDirectInitRange()));
5587}
5588
5589Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5590 QualType T = Importer.Import(E->getType());
5591 if (T.isNull())
5592 return nullptr;
5593
5594 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5595 Importer.Import(E->getOperatorDelete()));
5596 if (!OperatorDeleteDecl && E->getOperatorDelete())
5597 return nullptr;
5598
5599 Expr *ToArg = Importer.Import(E->getArgument());
5600 if (!ToArg && E->getArgument())
5601 return nullptr;
5602
5603 return new (Importer.getToContext()) CXXDeleteExpr(
5604 T, E->isGlobalDelete(),
5605 E->isArrayForm(),
5606 E->isArrayFormAsWritten(),
5607 E->doesUsualArrayDeleteWantSize(),
5608 OperatorDeleteDecl,
5609 ToArg,
5610 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005611}
5612
Sean Callanan59721b32015-04-28 18:41:46 +00005613Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5614 QualType T = Importer.Import(E->getType());
5615 if (T.isNull())
5616 return nullptr;
5617
5618 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005619 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005620 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005621 return nullptr;
5622
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005623 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005624 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005625 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005626
5627 return CXXConstructExpr::Create(Importer.getToContext(), T,
5628 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005629 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005630 ToArgs, E->hadMultipleCandidates(),
5631 E->isListInitialization(),
5632 E->isStdInitListInitialization(),
5633 E->requiresZeroInitialization(),
5634 E->getConstructionKind(),
5635 Importer.Import(E->getParenOrBraceRange()));
5636}
5637
Aleksei Sidorina693b372016-09-28 10:16:56 +00005638Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5639 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5640 if (!SubExpr && EWC->getSubExpr())
5641 return nullptr;
5642
5643 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5644 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5645 if (ExprWithCleanups::CleanupObject Obj =
5646 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5647 Objs[I] = Obj;
5648 else
5649 return nullptr;
5650
5651 return ExprWithCleanups::Create(Importer.getToContext(),
5652 SubExpr, EWC->cleanupsHaveSideEffects(),
5653 Objs);
5654}
5655
Sean Callanan8bca9962016-03-28 21:43:01 +00005656Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5657 QualType T = Importer.Import(E->getType());
5658 if (T.isNull())
5659 return nullptr;
5660
5661 Expr *ToFn = Importer.Import(E->getCallee());
5662 if (!ToFn)
5663 return nullptr;
5664
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005665 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005666 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005667 return nullptr;
5668
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005669 return new (Importer.getToContext()) CXXMemberCallExpr(
5670 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5671 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005672}
5673
5674Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5675 QualType T = Importer.Import(E->getType());
5676 if (T.isNull())
5677 return nullptr;
5678
5679 return new (Importer.getToContext())
5680 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5681}
5682
5683Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5684 QualType T = Importer.Import(E->getType());
5685 if (T.isNull())
5686 return nullptr;
5687
5688 return new (Importer.getToContext())
5689 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5690}
5691
5692
Sean Callanan59721b32015-04-28 18:41:46 +00005693Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5694 QualType T = Importer.Import(E->getType());
5695 if (T.isNull())
5696 return nullptr;
5697
5698 Expr *ToBase = Importer.Import(E->getBase());
5699 if (!ToBase && E->getBase())
5700 return nullptr;
5701
5702 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5703 if (!ToMember && E->getMemberDecl())
5704 return nullptr;
5705
5706 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5707 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5708 E->getFoundDecl().getAccess());
5709
5710 DeclarationNameInfo ToMemberNameInfo(
5711 Importer.Import(E->getMemberNameInfo().getName()),
5712 Importer.Import(E->getMemberNameInfo().getLoc()));
5713
5714 if (E->hasExplicitTemplateArgs()) {
5715 return nullptr; // FIXME: handle template arguments
5716 }
5717
5718 return MemberExpr::Create(Importer.getToContext(), ToBase,
5719 E->isArrow(),
5720 Importer.Import(E->getOperatorLoc()),
5721 Importer.Import(E->getQualifierLoc()),
5722 Importer.Import(E->getTemplateKeywordLoc()),
5723 ToMember, ToFoundDecl, ToMemberNameInfo,
5724 nullptr, T, E->getValueKind(),
5725 E->getObjectKind());
5726}
5727
5728Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5729 QualType T = Importer.Import(E->getType());
5730 if (T.isNull())
5731 return nullptr;
5732
5733 Expr *ToCallee = Importer.Import(E->getCallee());
5734 if (!ToCallee && E->getCallee())
5735 return nullptr;
5736
5737 unsigned NumArgs = E->getNumArgs();
5738
5739 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5740
5741 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5742 Expr *FromArg = E->getArg(ai);
5743 Expr *ToArg = Importer.Import(FromArg);
5744 if (!ToArg)
5745 return nullptr;
5746 ToArgs[ai] = ToArg;
5747 }
5748
5749 Expr **ToArgs_Copied = new (Importer.getToContext())
5750 Expr*[NumArgs];
5751
5752 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5753 ToArgs_Copied[ai] = ToArgs[ai];
5754
5755 return new (Importer.getToContext())
5756 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005757 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005758 Importer.Import(E->getRParenLoc()));
5759}
5760
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005761Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5762 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005763 if (T.isNull())
5764 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005765
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005766 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005767 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005768 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005769
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005770 ASTContext &ToCtx = Importer.getToContext();
5771 InitListExpr *To = new (ToCtx) InitListExpr(
5772 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5773 Exprs, Importer.Import(ILE->getLBraceLoc()));
5774 To->setType(T);
5775
5776 if (ILE->hasArrayFiller()) {
5777 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5778 if (!Filler)
5779 return nullptr;
5780 To->setArrayFiller(Filler);
5781 }
5782
5783 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5784 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5785 if (!ToFD)
5786 return nullptr;
5787 To->setInitializedFieldInUnion(ToFD);
5788 }
5789
5790 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5791 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5792 Importer.Import(SyntForm));
5793 if (!ToSyntForm)
5794 return nullptr;
5795 To->setSyntacticForm(ToSyntForm);
5796 }
5797
5798 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5799 To->setValueDependent(ILE->isValueDependent());
5800 To->setInstantiationDependent(ILE->isInstantiationDependent());
5801
5802 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005803}
5804
Richard Smith30e304e2016-12-14 00:03:17 +00005805Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5806 QualType ToType = Importer.Import(E->getType());
5807 if (ToType.isNull())
5808 return nullptr;
5809
5810 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5811 if (!ToCommon && E->getCommonExpr())
5812 return nullptr;
5813
5814 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5815 if (!ToSubExpr && E->getSubExpr())
5816 return nullptr;
5817
5818 return new (Importer.getToContext())
5819 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5820}
5821
5822Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5823 QualType ToType = Importer.Import(E->getType());
5824 if (ToType.isNull())
5825 return nullptr;
5826 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5827}
5828
Sean Callanandd2c1742016-05-16 20:48:03 +00005829Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5830 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5831 Importer.Import(DIE->getField()));
5832 if (!ToField && DIE->getField())
5833 return nullptr;
5834
5835 return CXXDefaultInitExpr::Create(
5836 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5837}
5838
5839Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5840 QualType ToType = Importer.Import(E->getType());
5841 if (ToType.isNull() && !E->getType().isNull())
5842 return nullptr;
5843 ExprValueKind VK = E->getValueKind();
5844 CastKind CK = E->getCastKind();
5845 Expr *ToOp = Importer.Import(E->getSubExpr());
5846 if (!ToOp && E->getSubExpr())
5847 return nullptr;
5848 CXXCastPath BasePath;
5849 if (ImportCastPath(E, BasePath))
5850 return nullptr;
5851 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5852 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5853 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5854 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5855
5856 if (isa<CXXStaticCastExpr>(E)) {
5857 return CXXStaticCastExpr::Create(
5858 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5859 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5860 } else if (isa<CXXDynamicCastExpr>(E)) {
5861 return CXXDynamicCastExpr::Create(
5862 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5863 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5864 } else if (isa<CXXReinterpretCastExpr>(E)) {
5865 return CXXReinterpretCastExpr::Create(
5866 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5867 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5868 } else {
5869 return nullptr;
5870 }
5871}
5872
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005873
5874Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
5875 SubstNonTypeTemplateParmExpr *E) {
5876 QualType T = Importer.Import(E->getType());
5877 if (T.isNull())
5878 return nullptr;
5879
5880 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
5881 Importer.Import(E->getParameter()));
5882 if (!Param)
5883 return nullptr;
5884
5885 Expr *Replacement = Importer.Import(E->getReplacement());
5886 if (!Replacement)
5887 return nullptr;
5888
5889 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
5890 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
5891 Replacement);
5892}
5893
Aleksei Sidorinb05f37a2017-11-26 17:04:06 +00005894Expr *ASTNodeImporter::VisitTypeTraitExpr(TypeTraitExpr *E) {
5895 QualType ToType = Importer.Import(E->getType());
5896 if (ToType.isNull())
5897 return nullptr;
5898
5899 SmallVector<TypeSourceInfo *, 4> ToArgs(E->getNumArgs());
5900 if (ImportContainerChecked(E->getArgs(), ToArgs))
5901 return nullptr;
5902
5903 // According to Sema::BuildTypeTrait(), if E is value-dependent,
5904 // Value is always false.
5905 bool ToValue = false;
5906 if (!E->isValueDependent())
5907 ToValue = E->getValue();
5908
5909 return TypeTraitExpr::Create(
5910 Importer.getToContext(), ToType, Importer.Import(E->getLocStart()),
5911 E->getTrait(), ToArgs, Importer.Import(E->getLocEnd()), ToValue);
5912}
5913
Lang Hames19e07e12017-06-20 21:06:00 +00005914void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
5915 CXXMethodDecl *FromMethod) {
5916 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
5917 ToMethod->addOverriddenMethod(
5918 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
5919 FromOverriddenMethod))));
5920}
5921
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005922ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005923 ASTContext &FromContext, FileManager &FromFileManager,
5924 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005925 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005926 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005927 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005928{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005929 ImportedDecls[FromContext.getTranslationUnitDecl()]
5930 = ToContext.getTranslationUnitDecl();
5931}
5932
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005933ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005934
5935QualType ASTImporter::Import(QualType FromT) {
5936 if (FromT.isNull())
5937 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005938
5939 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005940
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005941 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005942 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5943 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005944 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005945 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005946
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005947 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005948 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005949 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005950 if (ToT.isNull())
5951 return ToT;
5952
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005953 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005954 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005955
John McCall424cec92011-01-19 06:33:43 +00005956 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005957}
5958
Douglas Gregor62d311f2010-02-09 19:21:46 +00005959TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005960 if (!FromTSI)
5961 return FromTSI;
5962
5963 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005964 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005965 QualType T = Import(FromTSI->getType());
5966 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005967 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005968
5969 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005970 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005971}
5972
Sean Callanan59721b32015-04-28 18:41:46 +00005973Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5974 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5975 if (Pos != ImportedDecls.end()) {
5976 Decl *ToD = Pos->second;
5977 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5978 return ToD;
5979 } else {
5980 return nullptr;
5981 }
5982}
5983
Douglas Gregor62d311f2010-02-09 19:21:46 +00005984Decl *ASTImporter::Import(Decl *FromD) {
5985 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005986 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005987
Douglas Gregord451ea92011-07-29 23:31:30 +00005988 ASTNodeImporter Importer(*this);
5989
Douglas Gregor62d311f2010-02-09 19:21:46 +00005990 // Check whether we've already imported this declaration.
5991 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005992 if (Pos != ImportedDecls.end()) {
5993 Decl *ToD = Pos->second;
5994 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5995 return ToD;
5996 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005997
5998 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005999 Decl *ToD = Importer.Visit(FromD);
6000 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006001 return nullptr;
6002
Douglas Gregor62d311f2010-02-09 19:21:46 +00006003 // Record the imported declaration.
6004 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006005
6006 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6007 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006008 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006009 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006010 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006011 // When we've finished transforming a typedef, see whether it was the
6012 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006013 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006014 FromTag = AnonTagsWithPendingTypedefs.begin(),
6015 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6016 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006017 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006018 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6019 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006020 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006021 AnonTagsWithPendingTypedefs.erase(FromTag);
6022 break;
6023 }
6024 }
6025 }
6026 }
6027
Douglas Gregor62d311f2010-02-09 19:21:46 +00006028 return ToD;
6029}
6030
6031DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6032 if (!FromDC)
6033 return FromDC;
6034
Douglas Gregor95d82832012-01-24 18:36:04 +00006035 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006036 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006037 return nullptr;
6038
Douglas Gregor2e15c842012-02-01 21:00:38 +00006039 // When we're using a record/enum/Objective-C class/protocol as a context, we
6040 // need it to have a definition.
6041 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006042 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006043 if (ToRecord->isCompleteDefinition()) {
6044 // Do nothing.
6045 } else if (FromRecord->isCompleteDefinition()) {
6046 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6047 ASTNodeImporter::IDK_Basic);
6048 } else {
6049 CompleteDecl(ToRecord);
6050 }
6051 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6052 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6053 if (ToEnum->isCompleteDefinition()) {
6054 // Do nothing.
6055 } else if (FromEnum->isCompleteDefinition()) {
6056 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6057 ASTNodeImporter::IDK_Basic);
6058 } else {
6059 CompleteDecl(ToEnum);
6060 }
6061 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6062 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6063 if (ToClass->getDefinition()) {
6064 // Do nothing.
6065 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6066 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6067 ASTNodeImporter::IDK_Basic);
6068 } else {
6069 CompleteDecl(ToClass);
6070 }
6071 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6072 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6073 if (ToProto->getDefinition()) {
6074 // Do nothing.
6075 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6076 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6077 ASTNodeImporter::IDK_Basic);
6078 } else {
6079 CompleteDecl(ToProto);
6080 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006081 }
6082
6083 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006084}
6085
6086Expr *ASTImporter::Import(Expr *FromE) {
6087 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006088 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006089
6090 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6091}
6092
6093Stmt *ASTImporter::Import(Stmt *FromS) {
6094 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006095 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006096
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006097 // Check whether we've already imported this declaration.
6098 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6099 if (Pos != ImportedStmts.end())
6100 return Pos->second;
6101
6102 // Import the type
6103 ASTNodeImporter Importer(*this);
6104 Stmt *ToS = Importer.Visit(FromS);
6105 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006106 return nullptr;
6107
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006108 // Record the imported declaration.
6109 ImportedStmts[FromS] = ToS;
6110 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006111}
6112
6113NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6114 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006115 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006116
Douglas Gregor90ebf252011-04-27 16:48:40 +00006117 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6118
6119 switch (FromNNS->getKind()) {
6120 case NestedNameSpecifier::Identifier:
6121 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6122 return NestedNameSpecifier::Create(ToContext, prefix, II);
6123 }
Craig Topper36250ad2014-05-12 05:36:57 +00006124 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006125
6126 case NestedNameSpecifier::Namespace:
6127 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006128 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006129 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6130 }
Craig Topper36250ad2014-05-12 05:36:57 +00006131 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006132
6133 case NestedNameSpecifier::NamespaceAlias:
6134 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006135 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00006136 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6137 }
Craig Topper36250ad2014-05-12 05:36:57 +00006138 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006139
6140 case NestedNameSpecifier::Global:
6141 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6142
Nikola Smiljanic67860242014-09-26 00:28:20 +00006143 case NestedNameSpecifier::Super:
6144 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006145 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00006146 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6147 }
6148 return nullptr;
6149
Douglas Gregor90ebf252011-04-27 16:48:40 +00006150 case NestedNameSpecifier::TypeSpec:
6151 case NestedNameSpecifier::TypeSpecWithTemplate: {
6152 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6153 if (!T.isNull()) {
6154 bool bTemplate = FromNNS->getKind() ==
6155 NestedNameSpecifier::TypeSpecWithTemplate;
6156 return NestedNameSpecifier::Create(ToContext, prefix,
6157 bTemplate, T.getTypePtr());
6158 }
6159 }
Craig Topper36250ad2014-05-12 05:36:57 +00006160 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006161 }
6162
6163 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006164}
6165
Douglas Gregor14454802011-02-25 02:25:35 +00006166NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00006167 // Copied from NestedNameSpecifier mostly.
6168 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
6169 NestedNameSpecifierLoc NNS = FromNNS;
6170
6171 // Push each of the nested-name-specifiers's onto a stack for
6172 // serialization in reverse order.
6173 while (NNS) {
6174 NestedNames.push_back(NNS);
6175 NNS = NNS.getPrefix();
6176 }
6177
6178 NestedNameSpecifierLocBuilder Builder;
6179
6180 while (!NestedNames.empty()) {
6181 NNS = NestedNames.pop_back_val();
6182 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
6183 if (!Spec)
6184 return NestedNameSpecifierLoc();
6185
6186 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
6187 switch (Kind) {
6188 case NestedNameSpecifier::Identifier:
6189 Builder.Extend(getToContext(),
6190 Spec->getAsIdentifier(),
6191 Import(NNS.getLocalBeginLoc()),
6192 Import(NNS.getLocalEndLoc()));
6193 break;
6194
6195 case NestedNameSpecifier::Namespace:
6196 Builder.Extend(getToContext(),
6197 Spec->getAsNamespace(),
6198 Import(NNS.getLocalBeginLoc()),
6199 Import(NNS.getLocalEndLoc()));
6200 break;
6201
6202 case NestedNameSpecifier::NamespaceAlias:
6203 Builder.Extend(getToContext(),
6204 Spec->getAsNamespaceAlias(),
6205 Import(NNS.getLocalBeginLoc()),
6206 Import(NNS.getLocalEndLoc()));
6207 break;
6208
6209 case NestedNameSpecifier::TypeSpec:
6210 case NestedNameSpecifier::TypeSpecWithTemplate: {
6211 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
6212 QualType(Spec->getAsType(), 0));
6213 Builder.Extend(getToContext(),
6214 Import(NNS.getLocalBeginLoc()),
6215 TSI->getTypeLoc(),
6216 Import(NNS.getLocalEndLoc()));
6217 break;
6218 }
6219
6220 case NestedNameSpecifier::Global:
6221 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
6222 break;
6223
6224 case NestedNameSpecifier::Super: {
6225 SourceRange ToRange = Import(NNS.getSourceRange());
6226 Builder.MakeSuper(getToContext(),
6227 Spec->getAsRecordDecl(),
6228 ToRange.getBegin(),
6229 ToRange.getEnd());
6230 }
6231 }
6232 }
6233
6234 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00006235}
6236
Douglas Gregore2e50d332010-12-01 01:36:18 +00006237TemplateName ASTImporter::Import(TemplateName From) {
6238 switch (From.getKind()) {
6239 case TemplateName::Template:
6240 if (TemplateDecl *ToTemplate
6241 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6242 return TemplateName(ToTemplate);
6243
6244 return TemplateName();
6245
6246 case TemplateName::OverloadedTemplate: {
6247 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6248 UnresolvedSet<2> ToTemplates;
6249 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6250 E = FromStorage->end();
6251 I != E; ++I) {
6252 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6253 ToTemplates.addDecl(To);
6254 else
6255 return TemplateName();
6256 }
6257 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6258 ToTemplates.end());
6259 }
6260
6261 case TemplateName::QualifiedTemplate: {
6262 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6263 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6264 if (!Qualifier)
6265 return TemplateName();
6266
6267 if (TemplateDecl *ToTemplate
6268 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6269 return ToContext.getQualifiedTemplateName(Qualifier,
6270 QTN->hasTemplateKeyword(),
6271 ToTemplate);
6272
6273 return TemplateName();
6274 }
6275
6276 case TemplateName::DependentTemplate: {
6277 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6278 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6279 if (!Qualifier)
6280 return TemplateName();
6281
6282 if (DTN->isIdentifier()) {
6283 return ToContext.getDependentTemplateName(Qualifier,
6284 Import(DTN->getIdentifier()));
6285 }
6286
6287 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6288 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006289
6290 case TemplateName::SubstTemplateTemplateParm: {
6291 SubstTemplateTemplateParmStorage *subst
6292 = From.getAsSubstTemplateTemplateParm();
6293 TemplateTemplateParmDecl *param
6294 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6295 if (!param)
6296 return TemplateName();
6297
6298 TemplateName replacement = Import(subst->getReplacement());
6299 if (replacement.isNull()) return TemplateName();
6300
6301 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6302 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006303
6304 case TemplateName::SubstTemplateTemplateParmPack: {
6305 SubstTemplateTemplateParmPackStorage *SubstPack
6306 = From.getAsSubstTemplateTemplateParmPack();
6307 TemplateTemplateParmDecl *Param
6308 = cast_or_null<TemplateTemplateParmDecl>(
6309 Import(SubstPack->getParameterPack()));
6310 if (!Param)
6311 return TemplateName();
6312
6313 ASTNodeImporter Importer(*this);
6314 TemplateArgument ArgPack
6315 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6316 if (ArgPack.isNull())
6317 return TemplateName();
6318
6319 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6320 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006321 }
6322
6323 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006324}
6325
Douglas Gregor62d311f2010-02-09 19:21:46 +00006326SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6327 if (FromLoc.isInvalid())
6328 return SourceLocation();
6329
Douglas Gregor811663e2010-02-10 00:15:17 +00006330 SourceManager &FromSM = FromContext.getSourceManager();
6331
Sean Callanan24c5fe62016-11-07 20:42:25 +00006332 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006333 // don't have to import macro expansions.
6334 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00006335 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00006336 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6337 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006338 FileID ToFileID = Import(Decomposed.first);
6339 if (ToFileID.isInvalid())
6340 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006341 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6342 .getLocWithOffset(Decomposed.second);
6343 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006344}
6345
6346SourceRange ASTImporter::Import(SourceRange FromRange) {
6347 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6348}
6349
Douglas Gregor811663e2010-02-10 00:15:17 +00006350FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006351 llvm::DenseMap<FileID, FileID>::iterator Pos
6352 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006353 if (Pos != ImportedFileIDs.end())
6354 return Pos->second;
6355
6356 SourceManager &FromSM = FromContext.getSourceManager();
6357 SourceManager &ToSM = ToContext.getSourceManager();
6358 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006359 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006360
6361 // Include location of this file.
6362 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6363
6364 // Map the FileID for to the "to" source manager.
6365 FileID ToID;
6366 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006367 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006368 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6369 // disk again
6370 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6371 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006372 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006373 if (!Entry)
6374 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006375 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6376 FromSLoc.getFile().getFileCharacteristic());
6377 } else {
6378 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006379 const llvm::MemoryBuffer *
6380 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006381 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006382 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006383 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006384 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006385 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006386 }
6387
6388
Sebastian Redl99219f12010-09-30 01:03:06 +00006389 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006390 return ToID;
6391}
6392
Sean Callanandd2c1742016-05-16 20:48:03 +00006393CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6394 Expr *ToExpr = Import(From->getInit());
6395 if (!ToExpr && From->getInit())
6396 return nullptr;
6397
6398 if (From->isBaseInitializer()) {
6399 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6400 if (!ToTInfo && From->getTypeSourceInfo())
6401 return nullptr;
6402
6403 return new (ToContext) CXXCtorInitializer(
6404 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6405 ToExpr, Import(From->getRParenLoc()),
6406 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6407 : SourceLocation());
6408 } else if (From->isMemberInitializer()) {
6409 FieldDecl *ToField =
6410 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6411 if (!ToField && From->getMember())
6412 return nullptr;
6413
6414 return new (ToContext) CXXCtorInitializer(
6415 ToContext, ToField, Import(From->getMemberLocation()),
6416 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6417 } else if (From->isIndirectMemberInitializer()) {
6418 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6419 Import(From->getIndirectMember()));
6420 if (!ToIField && From->getIndirectMember())
6421 return nullptr;
6422
6423 return new (ToContext) CXXCtorInitializer(
6424 ToContext, ToIField, Import(From->getMemberLocation()),
6425 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6426 } else if (From->isDelegatingInitializer()) {
6427 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6428 if (!ToTInfo && From->getTypeSourceInfo())
6429 return nullptr;
6430
6431 return new (ToContext)
6432 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6433 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006434 } else {
6435 return nullptr;
6436 }
6437}
6438
6439
Aleksei Sidorina693b372016-09-28 10:16:56 +00006440CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6441 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6442 if (Pos != ImportedCXXBaseSpecifiers.end())
6443 return Pos->second;
6444
6445 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6446 Import(BaseSpec->getSourceRange()),
6447 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6448 BaseSpec->getAccessSpecifierAsWritten(),
6449 Import(BaseSpec->getTypeSourceInfo()),
6450 Import(BaseSpec->getEllipsisLoc()));
6451 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6452 return Imported;
6453}
6454
Douglas Gregor0a791672011-01-18 03:11:38 +00006455void ASTImporter::ImportDefinition(Decl *From) {
6456 Decl *To = Import(From);
6457 if (!To)
6458 return;
6459
6460 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6461 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006462
6463 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6464 if (!ToRecord->getDefinition()) {
6465 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006466 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006467 return;
6468 }
6469 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006470
6471 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6472 if (!ToEnum->getDefinition()) {
6473 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006474 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006475 return;
6476 }
6477 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006478
6479 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6480 if (!ToIFace->getDefinition()) {
6481 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006482 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006483 return;
6484 }
6485 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006486
Douglas Gregor2aa53772012-01-24 17:42:07 +00006487 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6488 if (!ToProto->getDefinition()) {
6489 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006490 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006491 return;
6492 }
6493 }
6494
Douglas Gregor0a791672011-01-18 03:11:38 +00006495 Importer.ImportDeclContext(FromDC, true);
6496 }
6497}
6498
Douglas Gregor96e578d2010-02-05 17:54:41 +00006499DeclarationName ASTImporter::Import(DeclarationName FromName) {
6500 if (!FromName)
6501 return DeclarationName();
6502
6503 switch (FromName.getNameKind()) {
6504 case DeclarationName::Identifier:
6505 return Import(FromName.getAsIdentifierInfo());
6506
6507 case DeclarationName::ObjCZeroArgSelector:
6508 case DeclarationName::ObjCOneArgSelector:
6509 case DeclarationName::ObjCMultiArgSelector:
6510 return Import(FromName.getObjCSelector());
6511
6512 case DeclarationName::CXXConstructorName: {
6513 QualType T = Import(FromName.getCXXNameType());
6514 if (T.isNull())
6515 return DeclarationName();
6516
6517 return ToContext.DeclarationNames.getCXXConstructorName(
6518 ToContext.getCanonicalType(T));
6519 }
6520
6521 case DeclarationName::CXXDestructorName: {
6522 QualType T = Import(FromName.getCXXNameType());
6523 if (T.isNull())
6524 return DeclarationName();
6525
6526 return ToContext.DeclarationNames.getCXXDestructorName(
6527 ToContext.getCanonicalType(T));
6528 }
6529
Richard Smith35845152017-02-07 01:37:30 +00006530 case DeclarationName::CXXDeductionGuideName: {
6531 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6532 Import(FromName.getCXXDeductionGuideTemplate()));
6533 if (!Template)
6534 return DeclarationName();
6535 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6536 }
6537
Douglas Gregor96e578d2010-02-05 17:54:41 +00006538 case DeclarationName::CXXConversionFunctionName: {
6539 QualType T = Import(FromName.getCXXNameType());
6540 if (T.isNull())
6541 return DeclarationName();
6542
6543 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6544 ToContext.getCanonicalType(T));
6545 }
6546
6547 case DeclarationName::CXXOperatorName:
6548 return ToContext.DeclarationNames.getCXXOperatorName(
6549 FromName.getCXXOverloadedOperator());
6550
6551 case DeclarationName::CXXLiteralOperatorName:
6552 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6553 Import(FromName.getCXXLiteralIdentifier()));
6554
6555 case DeclarationName::CXXUsingDirective:
6556 // FIXME: STATICS!
6557 return DeclarationName::getUsingDirectiveName();
6558 }
6559
David Blaikiee4d798f2012-01-20 21:50:17 +00006560 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006561}
6562
Douglas Gregore2e50d332010-12-01 01:36:18 +00006563IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006564 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006565 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006566
Sean Callananf94ef1d2016-05-14 06:11:19 +00006567 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6568
6569 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6570 ToId->setBuiltinID(FromId->getBuiltinID());
6571
6572 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006573}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006574
Douglas Gregor43f54792010-02-17 02:12:47 +00006575Selector ASTImporter::Import(Selector FromSel) {
6576 if (FromSel.isNull())
6577 return Selector();
6578
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006579 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006580 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6581 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6582 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6583 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6584}
6585
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006586DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6587 DeclContext *DC,
6588 unsigned IDNS,
6589 NamedDecl **Decls,
6590 unsigned NumDecls) {
6591 return Name;
6592}
6593
6594DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006595 if (LastDiagFromFrom)
6596 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6597 FromContext.getDiagnostics());
6598 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006599 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006600}
6601
6602DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006603 if (!LastDiagFromFrom)
6604 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6605 ToContext.getDiagnostics());
6606 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006607 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006608}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006609
Douglas Gregor2e15c842012-02-01 21:00:38 +00006610void ASTImporter::CompleteDecl (Decl *D) {
6611 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6612 if (!ID->getDefinition())
6613 ID->startDefinition();
6614 }
6615 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6616 if (!PD->getDefinition())
6617 PD->startDefinition();
6618 }
6619 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6620 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6621 TD->startDefinition();
6622 TD->setCompleteDefinition(true);
6623 }
6624 }
6625 else {
6626 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6627 }
6628}
6629
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006630Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006631 if (From->hasAttrs()) {
6632 for (Attr *FromAttr : From->getAttrs())
6633 To->addAttr(FromAttr->clone(To->getASTContext()));
6634 }
6635 if (From->isUsed()) {
6636 To->setIsUsed();
6637 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006638 if (From->isImplicit()) {
6639 To->setImplicit();
6640 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006641 ImportedDecls[From] = To;
6642 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006643}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006644
Douglas Gregordd6006f2012-07-17 21:16:27 +00006645bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6646 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006647 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006648 = ImportedTypes.find(From.getTypePtr());
6649 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6650 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00006651
Douglas Gregordd6006f2012-07-17 21:16:27 +00006652 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6653 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006654 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006655}