blob: c92647770e379049c688545b9ebef7a0a2024a36 [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);
Douglas Gregor96e578d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
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
John McCallc392f372010-06-11 00:33:02 +000080 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000081 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
82 QualType VisitObjCObjectType(const ObjCObjectType *T);
83 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000084
Douglas Gregor95d82832012-01-24 18:36:04 +000085 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000086 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
87 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000088 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000089 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000090 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
91 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000092 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000093
Aleksei Sidorina693b372016-09-28 10:16:56 +000094 bool ImportCastPath(CastExpr *E, CXXCastPath &Path);
95
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000096 typedef DesignatedInitExpr::Designator Designator;
97 Designator ImportDesignator(const Designator &D);
98
Douglas Gregor2e15c842012-02-01 21:00:38 +000099
Douglas Gregor95d82832012-01-24 18:36:04 +0000100 /// \brief What we should import from the definition.
101 enum ImportDefinitionKind {
102 /// \brief Import the default subset of the definition, which might be
103 /// nothing (if minimal import is set) or might be everything (if minimal
104 /// import is not set).
105 IDK_Default,
106 /// \brief Import everything.
107 IDK_Everything,
108 /// \brief Import only the bare bones needed to establish a valid
109 /// DeclContext.
110 IDK_Basic
111 };
112
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
114 return IDK == IDK_Everything ||
115 (IDK == IDK_Default && !Importer.isMinimalImport());
116 }
117
Douglas Gregord451ea92011-07-29 23:31:30 +0000118 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000119 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000120 bool ImportDefinition(VarDecl *From, VarDecl *To,
121 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000122 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000123 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000124 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000125 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000126 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000127 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000128 TemplateParameterList *ImportTemplateParameterList(
129 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000130 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000131 TemplateArgumentLoc ImportTemplateArgumentLoc(
132 const TemplateArgumentLoc &TALoc, bool &Error);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000133 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
134 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000135 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000136 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
137 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000138 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
139 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000140 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000141 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000142 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000143 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000144 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000145 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000146 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000147 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000148 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000149 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000150 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000151 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000152 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000153 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000154 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000155 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000156 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000157 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
158 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
159 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
160 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000161 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000162 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000163 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000164 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000165 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000166 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000167 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000168 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000169 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000170 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000171 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000172 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000173
174 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000175 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000176 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000177 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000178 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000179 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000180 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
181 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
182 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
183 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000184 Decl *VisitClassTemplateSpecializationDecl(
185 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000186 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
187 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
188
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000189 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000190 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
191
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000192 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000193 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000194 Stmt *VisitDeclStmt(DeclStmt *S);
195 Stmt *VisitNullStmt(NullStmt *S);
196 Stmt *VisitCompoundStmt(CompoundStmt *S);
197 Stmt *VisitCaseStmt(CaseStmt *S);
198 Stmt *VisitDefaultStmt(DefaultStmt *S);
199 Stmt *VisitLabelStmt(LabelStmt *S);
200 Stmt *VisitAttributedStmt(AttributedStmt *S);
201 Stmt *VisitIfStmt(IfStmt *S);
202 Stmt *VisitSwitchStmt(SwitchStmt *S);
203 Stmt *VisitWhileStmt(WhileStmt *S);
204 Stmt *VisitDoStmt(DoStmt *S);
205 Stmt *VisitForStmt(ForStmt *S);
206 Stmt *VisitGotoStmt(GotoStmt *S);
207 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
208 Stmt *VisitContinueStmt(ContinueStmt *S);
209 Stmt *VisitBreakStmt(BreakStmt *S);
210 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000211 // FIXME: MSAsmStmt
212 // FIXME: SEHExceptStmt
213 // FIXME: SEHFinallyStmt
214 // FIXME: SEHTryStmt
215 // FIXME: SEHLeaveStmt
216 // FIXME: CapturedStmt
217 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
218 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
219 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
220 // FIXME: MSDependentExistsStmt
221 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
222 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
223 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
224 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
225 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
226 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
227 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000228
229 // Importing expressions
230 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000231 Expr *VisitVAArgExpr(VAArgExpr *E);
232 Expr *VisitGNUNullExpr(GNUNullExpr *E);
233 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000234 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000235 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
236 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
237 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000238 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000239 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000240 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000241 Expr *VisitStringLiteral(StringLiteral *E);
242 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
243 Expr *VisitAtomicExpr(AtomicExpr *E);
244 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000245 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000246 Expr *VisitParenListExpr(ParenListExpr *E);
247 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000248 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000249 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000250 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000251 Expr *VisitConditionalOperator(ConditionalOperator *E);
252 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
253 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000254 Expr *VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E);
255 Expr *VisitExpressionTraitExpr(ExpressionTraitExpr *E);
256 Expr *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000257 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000258 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000259 Expr *VisitExplicitCastExpr(ExplicitCastExpr *E);
260 Expr *VisitOffsetOfExpr(OffsetOfExpr *OE);
261 Expr *VisitCXXThrowExpr(CXXThrowExpr *E);
262 Expr *VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
263 Expr *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
264 Expr *VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
265 Expr *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
266 Expr *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE);
267 Expr *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E);
268 Expr *VisitCXXNewExpr(CXXNewExpr *CE);
269 Expr *VisitCXXDeleteExpr(CXXDeleteExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000270 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000271 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
Aleksei Sidorina693b372016-09-28 10:16:56 +0000272 Expr *VisitExprWithCleanups(ExprWithCleanups *EWC);
Sean Callanan8bca9962016-03-28 21:43:01 +0000273 Expr *VisitCXXThisExpr(CXXThisExpr *E);
274 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000275 Expr *VisitMemberExpr(MemberExpr *E);
276 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000277 Expr *VisitInitListExpr(InitListExpr *E);
Richard Smith30e304e2016-12-14 00:03:17 +0000278 Expr *VisitArrayInitLoopExpr(ArrayInitLoopExpr *E);
279 Expr *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E);
Sean Callanandd2c1742016-05-16 20:48:03 +0000280 Expr *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E);
281 Expr *VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000282 Expr *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E);
283
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000284
285 template<typename IIter, typename OIter>
286 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
287 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
288 ASTImporter &ImporterRef = Importer;
289 std::transform(Ibegin, Iend, Obegin,
290 [&ImporterRef](ItemT From) -> ItemT {
291 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000292 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000293 }
294
295 template<typename IIter, typename OIter>
296 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
297 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
298 ASTImporter &ImporterRef = Importer;
299 bool Failed = false;
300 std::transform(Ibegin, Iend, Obegin,
301 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
Aleksei Sidorina693b372016-09-28 10:16:56 +0000302 ItemT *To = cast_or_null<ItemT>(
303 ImporterRef.Import(From));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000304 if (!To && From)
305 Failed = true;
306 return To;
307 });
308 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000309 }
Aleksei Sidorina693b372016-09-28 10:16:56 +0000310
311 template<typename InContainerTy, typename OutContainerTy>
312 bool ImportContainerChecked(const InContainerTy &InContainer,
313 OutContainerTy &OutContainer) {
314 return ImportArrayChecked(InContainer.begin(), InContainer.end(),
315 OutContainer.begin());
316 }
317
318 template<typename InContainerTy, typename OIter>
319 bool ImportArrayChecked(const InContainerTy &InContainer, OIter Obegin) {
320 return ImportArrayChecked(InContainer.begin(), InContainer.end(), Obegin);
321 }
Lang Hames19e07e12017-06-20 21:06:00 +0000322
323 // Importing overrides.
324 void ImportOverrides(CXXMethodDecl *ToMethod, CXXMethodDecl *FromMethod);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000325 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000326}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000327
Douglas Gregor3996e242010-02-15 22:01:00 +0000328//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000329// Import Types
330//----------------------------------------------------------------------------
331
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000332using namespace clang;
333
John McCall424cec92011-01-19 06:33:43 +0000334QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000335 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
336 << T->getTypeClassName();
337 return QualType();
338}
339
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000340QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
341 QualType UnderlyingType = Importer.Import(T->getValueType());
342 if(UnderlyingType.isNull())
343 return QualType();
344
345 return Importer.getToContext().getAtomicType(UnderlyingType);
346}
347
John McCall424cec92011-01-19 06:33:43 +0000348QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000349 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000350#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
351 case BuiltinType::Id: \
352 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000353#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000354#define SHARED_SINGLETON_TYPE(Expansion)
355#define BUILTIN_TYPE(Id, SingletonId) \
356 case BuiltinType::Id: return Importer.getToContext().SingletonId;
357#include "clang/AST/BuiltinTypes.def"
358
359 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
360 // context supports C++.
361
362 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
363 // context supports ObjC.
364
Douglas Gregor96e578d2010-02-05 17:54:41 +0000365 case BuiltinType::Char_U:
366 // The context we're importing from has an unsigned 'char'. If we're
367 // importing into a context with a signed 'char', translate to
368 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000369 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000370 return Importer.getToContext().UnsignedCharTy;
371
372 return Importer.getToContext().CharTy;
373
Douglas Gregor96e578d2010-02-05 17:54:41 +0000374 case BuiltinType::Char_S:
375 // The context we're importing from has an unsigned 'char'. If we're
376 // importing into a context with a signed 'char', translate to
377 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000378 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000379 return Importer.getToContext().SignedCharTy;
380
381 return Importer.getToContext().CharTy;
382
Chris Lattnerad3467e2010-12-25 23:25:43 +0000383 case BuiltinType::WChar_S:
384 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000385 // FIXME: If not in C++, shall we translate to the C equivalent of
386 // wchar_t?
387 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000388 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000389
390 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000391}
392
Aleksei Sidorina693b372016-09-28 10:16:56 +0000393QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
394 QualType OrigT = Importer.Import(T->getOriginalType());
395 if (OrigT.isNull())
396 return QualType();
397
398 return Importer.getToContext().getDecayedType(OrigT);
399}
400
John McCall424cec92011-01-19 06:33:43 +0000401QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000402 QualType ToElementType = Importer.Import(T->getElementType());
403 if (ToElementType.isNull())
404 return QualType();
405
406 return Importer.getToContext().getComplexType(ToElementType);
407}
408
John McCall424cec92011-01-19 06:33:43 +0000409QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000410 QualType ToPointeeType = Importer.Import(T->getPointeeType());
411 if (ToPointeeType.isNull())
412 return QualType();
413
414 return Importer.getToContext().getPointerType(ToPointeeType);
415}
416
John McCall424cec92011-01-19 06:33:43 +0000417QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000418 // FIXME: Check for blocks support in "to" context.
419 QualType ToPointeeType = Importer.Import(T->getPointeeType());
420 if (ToPointeeType.isNull())
421 return QualType();
422
423 return Importer.getToContext().getBlockPointerType(ToPointeeType);
424}
425
John McCall424cec92011-01-19 06:33:43 +0000426QualType
427ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000428 // FIXME: Check for C++ support in "to" context.
429 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
430 if (ToPointeeType.isNull())
431 return QualType();
432
433 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
434}
435
John McCall424cec92011-01-19 06:33:43 +0000436QualType
437ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000438 // FIXME: Check for C++0x support in "to" context.
439 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
440 if (ToPointeeType.isNull())
441 return QualType();
442
443 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
444}
445
John McCall424cec92011-01-19 06:33:43 +0000446QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000447 // FIXME: Check for C++ support in "to" context.
448 QualType ToPointeeType = Importer.Import(T->getPointeeType());
449 if (ToPointeeType.isNull())
450 return QualType();
451
452 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
453 return Importer.getToContext().getMemberPointerType(ToPointeeType,
454 ClassType.getTypePtr());
455}
456
John McCall424cec92011-01-19 06:33:43 +0000457QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000458 QualType ToElementType = Importer.Import(T->getElementType());
459 if (ToElementType.isNull())
460 return QualType();
461
462 return Importer.getToContext().getConstantArrayType(ToElementType,
463 T->getSize(),
464 T->getSizeModifier(),
465 T->getIndexTypeCVRQualifiers());
466}
467
John McCall424cec92011-01-19 06:33:43 +0000468QualType
469ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *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().getIncompleteArrayType(ToElementType,
475 T->getSizeModifier(),
476 T->getIndexTypeCVRQualifiers());
477}
478
John McCall424cec92011-01-19 06:33:43 +0000479QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000480 QualType ToElementType = Importer.Import(T->getElementType());
481 if (ToElementType.isNull())
482 return QualType();
483
484 Expr *Size = Importer.Import(T->getSizeExpr());
485 if (!Size)
486 return QualType();
487
488 SourceRange Brackets = Importer.Import(T->getBracketsRange());
489 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
490 T->getSizeModifier(),
491 T->getIndexTypeCVRQualifiers(),
492 Brackets);
493}
494
John McCall424cec92011-01-19 06:33:43 +0000495QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000496 QualType ToElementType = Importer.Import(T->getElementType());
497 if (ToElementType.isNull())
498 return QualType();
499
500 return Importer.getToContext().getVectorType(ToElementType,
501 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000502 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000503}
504
John McCall424cec92011-01-19 06:33:43 +0000505QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000506 QualType ToElementType = Importer.Import(T->getElementType());
507 if (ToElementType.isNull())
508 return QualType();
509
510 return Importer.getToContext().getExtVectorType(ToElementType,
511 T->getNumElements());
512}
513
John McCall424cec92011-01-19 06:33:43 +0000514QualType
515ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000516 // FIXME: What happens if we're importing a function without a prototype
517 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000518 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000519 if (ToResultType.isNull())
520 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000521
Douglas Gregor96e578d2010-02-05 17:54:41 +0000522 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000523 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000524}
525
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000526QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000527 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000528 if (ToResultType.isNull())
529 return QualType();
530
531 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000532 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000533 for (const auto &A : T->param_types()) {
534 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000535 if (ArgType.isNull())
536 return QualType();
537 ArgTypes.push_back(ArgType);
538 }
539
540 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000541 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000542 for (const auto &E : T->exceptions()) {
543 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000544 if (ExceptionType.isNull())
545 return QualType();
546 ExceptionTypes.push_back(ExceptionType);
547 }
John McCalldb40c7f2010-12-14 08:05:40 +0000548
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000549 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
550 FunctionProtoType::ExtProtoInfo ToEPI;
551
552 ToEPI.ExtInfo = FromEPI.ExtInfo;
553 ToEPI.Variadic = FromEPI.Variadic;
554 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
555 ToEPI.TypeQuals = FromEPI.TypeQuals;
556 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000557 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
558 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
559 ToEPI.ExceptionSpec.NoexceptExpr =
560 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
561 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
562 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
563 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
564 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000565
Jordan Rose5c382722013-03-08 21:51:21 +0000566 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000567}
568
Sean Callananda6df8a2011-08-11 16:56:07 +0000569QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
570 QualType ToInnerType = Importer.Import(T->getInnerType());
571 if (ToInnerType.isNull())
572 return QualType();
573
574 return Importer.getToContext().getParenType(ToInnerType);
575}
576
John McCall424cec92011-01-19 06:33:43 +0000577QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000578 TypedefNameDecl *ToDecl
579 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000580 if (!ToDecl)
581 return QualType();
582
583 return Importer.getToContext().getTypeDeclType(ToDecl);
584}
585
John McCall424cec92011-01-19 06:33:43 +0000586QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000587 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
588 if (!ToExpr)
589 return QualType();
590
591 return Importer.getToContext().getTypeOfExprType(ToExpr);
592}
593
John McCall424cec92011-01-19 06:33:43 +0000594QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000595 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
596 if (ToUnderlyingType.isNull())
597 return QualType();
598
599 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
600}
601
John McCall424cec92011-01-19 06:33:43 +0000602QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000603 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000604 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
605 if (!ToExpr)
606 return QualType();
607
Douglas Gregor81495f32012-02-12 18:42:33 +0000608 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
609 if (UnderlyingType.isNull())
610 return QualType();
611
612 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000613}
614
Alexis Hunte852b102011-05-24 22:41:36 +0000615QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
616 QualType ToBaseType = Importer.Import(T->getBaseType());
617 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
618 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
619 return QualType();
620
621 return Importer.getToContext().getUnaryTransformType(ToBaseType,
622 ToUnderlyingType,
623 T->getUTTKind());
624}
625
Richard Smith30482bc2011-02-20 03:19:35 +0000626QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000627 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000628 QualType FromDeduced = T->getDeducedType();
629 QualType ToDeduced;
630 if (!FromDeduced.isNull()) {
631 ToDeduced = Importer.Import(FromDeduced);
632 if (ToDeduced.isNull())
633 return QualType();
634 }
635
Richard Smithe301ba22015-11-11 02:02:15 +0000636 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000637 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000638}
639
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000640QualType ASTNodeImporter::VisitInjectedClassNameType(
641 const InjectedClassNameType *T) {
642 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
643 if (!D)
644 return QualType();
645
646 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
647 if (InjType.isNull())
648 return QualType();
649
650 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
651 // See comments in InjectedClassNameType definition for details
652 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
653 enum {
654 TypeAlignmentInBits = 4,
655 TypeAlignment = 1 << TypeAlignmentInBits
656 };
657
658 return QualType(new (Importer.getToContext(), TypeAlignment)
659 InjectedClassNameType(D, InjType), 0);
660}
661
John McCall424cec92011-01-19 06:33:43 +0000662QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000663 RecordDecl *ToDecl
664 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
665 if (!ToDecl)
666 return QualType();
667
668 return Importer.getToContext().getTagDeclType(ToDecl);
669}
670
John McCall424cec92011-01-19 06:33:43 +0000671QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000672 EnumDecl *ToDecl
673 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
674 if (!ToDecl)
675 return QualType();
676
677 return Importer.getToContext().getTagDeclType(ToDecl);
678}
679
Sean Callanan72fe0852015-04-02 23:50:08 +0000680QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
681 QualType FromModifiedType = T->getModifiedType();
682 QualType FromEquivalentType = T->getEquivalentType();
683 QualType ToModifiedType;
684 QualType ToEquivalentType;
685
686 if (!FromModifiedType.isNull()) {
687 ToModifiedType = Importer.Import(FromModifiedType);
688 if (ToModifiedType.isNull())
689 return QualType();
690 }
691 if (!FromEquivalentType.isNull()) {
692 ToEquivalentType = Importer.Import(FromEquivalentType);
693 if (ToEquivalentType.isNull())
694 return QualType();
695 }
696
697 return Importer.getToContext().getAttributedType(T->getAttrKind(),
698 ToModifiedType, ToEquivalentType);
699}
700
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000701
702QualType ASTNodeImporter::VisitTemplateTypeParmType(
703 const TemplateTypeParmType *T) {
704 TemplateTypeParmDecl *ParmDecl =
705 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
706 if (!ParmDecl && T->getDecl())
707 return QualType();
708
709 return Importer.getToContext().getTemplateTypeParmType(
710 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
711}
712
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000713QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
714 const SubstTemplateTypeParmType *T) {
715 const TemplateTypeParmType *Replaced =
716 cast_or_null<TemplateTypeParmType>(Importer.Import(
717 QualType(T->getReplacedParameter(), 0)).getTypePtr());
718 if (!Replaced)
719 return QualType();
720
721 QualType Replacement = Importer.Import(T->getReplacementType());
722 if (Replacement.isNull())
723 return QualType();
724 Replacement = Replacement.getCanonicalType();
725
726 return Importer.getToContext().getSubstTemplateTypeParmType(
727 Replaced, Replacement);
728}
729
Douglas Gregore2e50d332010-12-01 01:36:18 +0000730QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000731 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000732 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
733 if (ToTemplate.isNull())
734 return QualType();
735
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000736 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000737 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
738 return QualType();
739
740 QualType ToCanonType;
741 if (!QualType(T, 0).isCanonical()) {
742 QualType FromCanonType
743 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
744 ToCanonType =Importer.Import(FromCanonType);
745 if (ToCanonType.isNull())
746 return QualType();
747 }
748 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000749 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000750 ToCanonType);
751}
752
John McCall424cec92011-01-19 06:33:43 +0000753QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000754 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000755 // Note: the qualifier in an ElaboratedType is optional.
756 if (T->getQualifier()) {
757 ToQualifier = Importer.Import(T->getQualifier());
758 if (!ToQualifier)
759 return QualType();
760 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000761
762 QualType ToNamedType = Importer.Import(T->getNamedType());
763 if (ToNamedType.isNull())
764 return QualType();
765
Abramo Bagnara6150c882010-05-11 21:36:43 +0000766 return Importer.getToContext().getElaboratedType(T->getKeyword(),
767 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000768}
769
John McCall424cec92011-01-19 06:33:43 +0000770QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000771 ObjCInterfaceDecl *Class
772 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
773 if (!Class)
774 return QualType();
775
John McCall8b07ec22010-05-15 11:32:37 +0000776 return Importer.getToContext().getObjCInterfaceType(Class);
777}
778
John McCall424cec92011-01-19 06:33:43 +0000779QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000780 QualType ToBaseType = Importer.Import(T->getBaseType());
781 if (ToBaseType.isNull())
782 return QualType();
783
Douglas Gregore9d95f12015-07-07 03:57:35 +0000784 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000785 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000786 QualType ImportedTypeArg = Importer.Import(TypeArg);
787 if (ImportedTypeArg.isNull())
788 return QualType();
789
790 TypeArgs.push_back(ImportedTypeArg);
791 }
792
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000793 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000794 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000795 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000796 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000797 if (!Protocol)
798 return QualType();
799 Protocols.push_back(Protocol);
800 }
801
Douglas Gregore9d95f12015-07-07 03:57:35 +0000802 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000803 Protocols,
804 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000805}
806
John McCall424cec92011-01-19 06:33:43 +0000807QualType
808ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000809 QualType ToPointeeType = Importer.Import(T->getPointeeType());
810 if (ToPointeeType.isNull())
811 return QualType();
812
John McCall8b07ec22010-05-15 11:32:37 +0000813 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000814}
815
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000816//----------------------------------------------------------------------------
817// Import Declarations
818//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000819bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
820 DeclContext *&LexicalDC,
821 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000822 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000823 SourceLocation &Loc) {
824 // Import the context of this declaration.
825 DC = Importer.ImportContext(D->getDeclContext());
826 if (!DC)
827 return true;
828
829 LexicalDC = DC;
830 if (D->getDeclContext() != D->getLexicalDeclContext()) {
831 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
832 if (!LexicalDC)
833 return true;
834 }
835
836 // Import the name of this declaration.
837 Name = Importer.Import(D->getDeclName());
838 if (D->getDeclName() && !Name)
839 return true;
840
841 // Import the location of this declaration.
842 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000843 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000844 return false;
845}
846
Douglas Gregord451ea92011-07-29 23:31:30 +0000847void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
848 if (!FromD)
849 return;
850
851 if (!ToD) {
852 ToD = Importer.Import(FromD);
853 if (!ToD)
854 return;
855 }
856
857 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
858 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000859 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000860 ImportDefinition(FromRecord, ToRecord);
861 }
862 }
863 return;
864 }
865
866 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
867 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
868 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
869 ImportDefinition(FromEnum, ToEnum);
870 }
871 }
872 return;
873 }
874}
875
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000876void
877ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
878 DeclarationNameInfo& To) {
879 // NOTE: To.Name and To.Loc are already imported.
880 // We only have to import To.LocInfo.
881 switch (To.getName().getNameKind()) {
882 case DeclarationName::Identifier:
883 case DeclarationName::ObjCZeroArgSelector:
884 case DeclarationName::ObjCOneArgSelector:
885 case DeclarationName::ObjCMultiArgSelector:
886 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000887 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000888 return;
889
890 case DeclarationName::CXXOperatorName: {
891 SourceRange Range = From.getCXXOperatorNameRange();
892 To.setCXXOperatorNameRange(Importer.Import(Range));
893 return;
894 }
895 case DeclarationName::CXXLiteralOperatorName: {
896 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
897 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
898 return;
899 }
900 case DeclarationName::CXXConstructorName:
901 case DeclarationName::CXXDestructorName:
902 case DeclarationName::CXXConversionFunctionName: {
903 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
904 To.setNamedTypeInfo(Importer.Import(FromTInfo));
905 return;
906 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000907 }
Douglas Gregor07216d12011-11-02 20:52:01 +0000908 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000909}
910
Douglas Gregor2e15c842012-02-01 21:00:38 +0000911void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +0000912 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +0000913 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +0000914 return;
915 }
916
Aaron Ballman629afae2014-03-07 19:56:05 +0000917 for (auto *From : FromDC->decls())
918 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +0000919}
920
Douglas Gregord451ea92011-07-29 23:31:30 +0000921bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000922 ImportDefinitionKind Kind) {
923 if (To->getDefinition() || To->isBeingDefined()) {
924 if (Kind == IDK_Everything)
925 ImportDeclContext(From, /*ForceImport=*/true);
926
Douglas Gregore2e50d332010-12-01 01:36:18 +0000927 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +0000928 }
Douglas Gregore2e50d332010-12-01 01:36:18 +0000929
930 To->startDefinition();
931
932 // Add base classes.
933 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
934 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000935
936 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
937 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
938 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000939 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000940 ToData.Aggregate = FromData.Aggregate;
941 ToData.PlainOldData = FromData.PlainOldData;
942 ToData.Empty = FromData.Empty;
943 ToData.Polymorphic = FromData.Polymorphic;
944 ToData.Abstract = FromData.Abstract;
945 ToData.IsStandardLayout = FromData.IsStandardLayout;
946 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
947 ToData.HasPrivateFields = FromData.HasPrivateFields;
948 ToData.HasProtectedFields = FromData.HasProtectedFields;
949 ToData.HasPublicFields = FromData.HasPublicFields;
950 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +0000951 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +0000952 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +0000953 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +0000954 ToData.HasUninitializedReferenceMember
955 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +0000956 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +0000957 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
958 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith96cd6712017-08-16 01:49:53 +0000959 ToData.NeedOverloadResolutionForCopyConstructor
960 = FromData.NeedOverloadResolutionForCopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000961 ToData.NeedOverloadResolutionForMoveConstructor
962 = FromData.NeedOverloadResolutionForMoveConstructor;
963 ToData.NeedOverloadResolutionForMoveAssignment
964 = FromData.NeedOverloadResolutionForMoveAssignment;
965 ToData.NeedOverloadResolutionForDestructor
966 = FromData.NeedOverloadResolutionForDestructor;
Richard Smith96cd6712017-08-16 01:49:53 +0000967 ToData.DefaultedCopyConstructorIsDeleted
968 = FromData.DefaultedCopyConstructorIsDeleted;
Richard Smith6b02d462012-12-08 08:32:28 +0000969 ToData.DefaultedMoveConstructorIsDeleted
970 = FromData.DefaultedMoveConstructorIsDeleted;
971 ToData.DefaultedMoveAssignmentIsDeleted
972 = FromData.DefaultedMoveAssignmentIsDeleted;
973 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +0000974 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
975 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000976 ToData.HasConstexprNonCopyMoveConstructor
977 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +0000978 ToData.HasDefaultedDefaultConstructor
979 = FromData.HasDefaultedDefaultConstructor;
Richard Smith96cd6712017-08-16 01:49:53 +0000980 ToData.CanPassInRegisters = FromData.CanPassInRegisters;
Richard Smith561fb152012-02-25 07:33:38 +0000981 ToData.DefaultedDefaultConstructorIsConstexpr
982 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +0000983 ToData.HasConstexprDefaultConstructor
984 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000985 ToData.HasNonLiteralTypeFieldsOrBases
986 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +0000987 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000988 ToData.UserProvidedDefaultConstructor
989 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000990 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +0000991 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
992 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
993 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
994 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +0000995 ToData.ImplicitCopyAssignmentHasConstParam
996 = FromData.ImplicitCopyAssignmentHasConstParam;
997 ToData.HasDeclaredCopyConstructorWithConstParam
998 = FromData.HasDeclaredCopyConstructorWithConstParam;
999 ToData.HasDeclaredCopyAssignmentWithConstParam
1000 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001001 ToData.IsLambda = FromData.IsLambda;
1002
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001003 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001004 for (const auto &Base1 : FromCXX->bases()) {
1005 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001006 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001007 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001008
1009 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001010 if (Base1.isPackExpansion())
1011 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001012
1013 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001014 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001015
Douglas Gregore2e50d332010-12-01 01:36:18 +00001016 Bases.push_back(
1017 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001018 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1019 Base1.isVirtual(),
1020 Base1.isBaseOfClass(),
1021 Base1.getAccessSpecifierAsWritten(),
1022 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001023 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001024 }
1025 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001026 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001027 }
1028
Douglas Gregor2e15c842012-02-01 21:00:38 +00001029 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001030 ImportDeclContext(From, /*ForceImport=*/true);
1031
Douglas Gregore2e50d332010-12-01 01:36:18 +00001032 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001033 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001034}
1035
Larisse Voufo39a1e502013-08-06 01:03:05 +00001036bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1037 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001038 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001039 return false;
1040
1041 // FIXME: Can we really import any initializer? Alternatively, we could force
1042 // ourselves to import every declaration of a variable and then only use
1043 // getInit() here.
1044 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1045
1046 // FIXME: Other bits to merge?
1047
1048 return false;
1049}
1050
Douglas Gregord451ea92011-07-29 23:31:30 +00001051bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001052 ImportDefinitionKind Kind) {
1053 if (To->getDefinition() || To->isBeingDefined()) {
1054 if (Kind == IDK_Everything)
1055 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001056 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001057 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001058
1059 To->startDefinition();
1060
1061 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1062 if (T.isNull())
1063 return true;
1064
1065 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1066 if (ToPromotionType.isNull())
1067 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001068
1069 if (shouldForceImportDeclContext(Kind))
1070 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001071
1072 // FIXME: we might need to merge the number of positive or negative bits
1073 // if the enumerator lists don't match.
1074 To->completeDefinition(T, ToPromotionType,
1075 From->getNumPositiveBits(),
1076 From->getNumNegativeBits());
1077 return false;
1078}
1079
Douglas Gregora082a492010-11-30 19:14:50 +00001080TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1081 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001082 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1083 if (ImportContainerChecked(*Params, ToParams))
1084 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001085
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001086 Expr *ToRequiresClause;
1087 if (Expr *const R = Params->getRequiresClause()) {
1088 ToRequiresClause = Importer.Import(R);
1089 if (!ToRequiresClause)
1090 return nullptr;
1091 } else {
1092 ToRequiresClause = nullptr;
1093 }
1094
Douglas Gregora082a492010-11-30 19:14:50 +00001095 return TemplateParameterList::Create(Importer.getToContext(),
1096 Importer.Import(Params->getTemplateLoc()),
1097 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001098 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001099 Importer.Import(Params->getRAngleLoc()),
1100 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001101}
1102
Douglas Gregore2e50d332010-12-01 01:36:18 +00001103TemplateArgument
1104ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1105 switch (From.getKind()) {
1106 case TemplateArgument::Null:
1107 return TemplateArgument();
1108
1109 case TemplateArgument::Type: {
1110 QualType ToType = Importer.Import(From.getAsType());
1111 if (ToType.isNull())
1112 return TemplateArgument();
1113 return TemplateArgument(ToType);
1114 }
1115
1116 case TemplateArgument::Integral: {
1117 QualType ToType = Importer.Import(From.getIntegralType());
1118 if (ToType.isNull())
1119 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001120 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001121 }
1122
Eli Friedmanb826a002012-09-26 02:36:12 +00001123 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001124 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1125 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1126 if (!To || ToType.isNull())
1127 return TemplateArgument();
1128 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001129 }
1130
1131 case TemplateArgument::NullPtr: {
1132 QualType ToType = Importer.Import(From.getNullPtrType());
1133 if (ToType.isNull())
1134 return TemplateArgument();
1135 return TemplateArgument(ToType, /*isNullPtr*/true);
1136 }
1137
Douglas Gregore2e50d332010-12-01 01:36:18 +00001138 case TemplateArgument::Template: {
1139 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1140 if (ToTemplate.isNull())
1141 return TemplateArgument();
1142
1143 return TemplateArgument(ToTemplate);
1144 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001145
1146 case TemplateArgument::TemplateExpansion: {
1147 TemplateName ToTemplate
1148 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1149 if (ToTemplate.isNull())
1150 return TemplateArgument();
1151
Douglas Gregore1d60df2011-01-14 23:41:42 +00001152 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001153 }
1154
Douglas Gregore2e50d332010-12-01 01:36:18 +00001155 case TemplateArgument::Expression:
1156 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1157 return TemplateArgument(ToExpr);
1158 return TemplateArgument();
1159
1160 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001161 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001162 ToPack.reserve(From.pack_size());
1163 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1164 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001165
1166 return TemplateArgument(
1167 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001168 }
1169 }
1170
1171 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001172}
1173
Aleksei Sidorina693b372016-09-28 10:16:56 +00001174TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
1175 const TemplateArgumentLoc &TALoc, bool &Error) {
1176 Error = false;
1177 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1178 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1179 TemplateArgumentLocInfo ToInfo;
1180 if (Arg.getKind() == TemplateArgument::Expression) {
1181 Expr *E = Importer.Import(FromInfo.getAsExpr());
1182 ToInfo = TemplateArgumentLocInfo(E);
1183 if (!E)
1184 Error = true;
1185 } else if (Arg.getKind() == TemplateArgument::Type) {
1186 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1187 ToInfo = TemplateArgumentLocInfo(TSI);
1188 else
1189 Error = true;
1190 } else {
1191 ToInfo = TemplateArgumentLocInfo(
1192 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1193 Importer.Import(FromInfo.getTemplateNameLoc()),
1194 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1195 }
1196 return TemplateArgumentLoc(Arg, ToInfo);
1197}
1198
Douglas Gregore2e50d332010-12-01 01:36:18 +00001199bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1200 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001201 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001202 for (unsigned I = 0; I != NumFromArgs; ++I) {
1203 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1204 if (To.isNull() && !FromArgs[I].isNull())
1205 return true;
1206
1207 ToArgs.push_back(To);
1208 }
1209
1210 return false;
1211}
1212
Douglas Gregor5c73e912010-02-11 00:48:18 +00001213bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001214 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001215 // Eliminate a potential failure point where we attempt to re-import
1216 // something we're trying to import while completing ToRecord.
1217 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1218 if (ToOrigin) {
1219 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1220 if (ToOriginRecord)
1221 ToRecord = ToOriginRecord;
1222 }
1223
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001224 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001225 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001226 Importer.getNonEquivalentDecls(),
1227 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001228 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001229}
1230
Larisse Voufo39a1e502013-08-06 01:03:05 +00001231bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1232 bool Complain) {
1233 StructuralEquivalenceContext Ctx(
1234 Importer.getFromContext(), Importer.getToContext(),
1235 Importer.getNonEquivalentDecls(), false, Complain);
1236 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1237}
1238
Douglas Gregor98c10182010-02-12 22:17:39 +00001239bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001240 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001241 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001242 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001243 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001244}
1245
Douglas Gregor91155082012-11-14 22:29:20 +00001246bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1247 EnumConstantDecl *ToEC)
1248{
1249 const llvm::APSInt &FromVal = FromEC->getInitVal();
1250 const llvm::APSInt &ToVal = ToEC->getInitVal();
1251
1252 return FromVal.isSigned() == ToVal.isSigned() &&
1253 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1254 FromVal == ToVal;
1255}
1256
1257bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001258 ClassTemplateDecl *To) {
1259 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1260 Importer.getToContext(),
1261 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001262 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001263}
1264
Larisse Voufo39a1e502013-08-06 01:03:05 +00001265bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1266 VarTemplateDecl *To) {
1267 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1268 Importer.getToContext(),
1269 Importer.getNonEquivalentDecls());
1270 return Ctx.IsStructurallyEquivalent(From, To);
1271}
1272
Douglas Gregore4c83e42010-02-09 22:48:33 +00001273Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001274 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001275 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001276 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001277}
1278
Sean Callanan65198272011-11-17 23:20:56 +00001279Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1280 TranslationUnitDecl *ToD =
1281 Importer.getToContext().getTranslationUnitDecl();
1282
1283 Importer.Imported(D, ToD);
1284
1285 return ToD;
1286}
1287
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001288Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1289
1290 SourceLocation Loc = Importer.Import(D->getLocation());
1291 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1292
1293 // Import the context of this declaration.
1294 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1295 if (!DC)
1296 return nullptr;
1297
1298 AccessSpecDecl *accessSpecDecl
1299 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1300 DC, Loc, ColonLoc);
1301
1302 if (!accessSpecDecl)
1303 return nullptr;
1304
1305 // Lexical DeclContext and Semantic DeclContext
1306 // is always the same for the accessSpec.
1307 accessSpecDecl->setLexicalDeclContext(DC);
1308 DC->addDeclInternal(accessSpecDecl);
1309
1310 return accessSpecDecl;
1311}
1312
Aleksei Sidorina693b372016-09-28 10:16:56 +00001313Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1314 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1315 if (!DC)
1316 return nullptr;
1317
1318 DeclContext *LexicalDC = DC;
1319
1320 // Import the location of this declaration.
1321 SourceLocation Loc = Importer.Import(D->getLocation());
1322
1323 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1324 if (!AssertExpr)
1325 return nullptr;
1326
1327 StringLiteral *FromMsg = D->getMessage();
1328 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1329 if (!ToMsg && FromMsg)
1330 return nullptr;
1331
1332 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1333 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1334 Importer.Import(D->getRParenLoc()), D->isFailed());
1335
1336 ToD->setLexicalDeclContext(LexicalDC);
1337 LexicalDC->addDeclInternal(ToD);
1338 Importer.Imported(D, ToD);
1339 return ToD;
1340}
1341
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001342Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1343 // Import the major distinguishing characteristics of this namespace.
1344 DeclContext *DC, *LexicalDC;
1345 DeclarationName Name;
1346 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001347 NamedDecl *ToD;
1348 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001349 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001350 if (ToD)
1351 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001352
1353 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001354 if (!Name) {
1355 // This is an anonymous namespace. Adopt an existing anonymous
1356 // namespace if we can.
1357 // FIXME: Not testable.
1358 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1359 MergeWithNamespace = TU->getAnonymousNamespace();
1360 else
1361 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1362 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001363 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001364 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001365 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001366 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1367 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001368 continue;
1369
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001370 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001371 MergeWithNamespace = FoundNS;
1372 ConflictingDecls.clear();
1373 break;
1374 }
1375
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001376 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001377 }
1378
1379 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001380 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001381 ConflictingDecls.data(),
1382 ConflictingDecls.size());
1383 }
1384 }
1385
1386 // Create the "to" namespace, if needed.
1387 NamespaceDecl *ToNamespace = MergeWithNamespace;
1388 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001389 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001390 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001391 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001392 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001393 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001394 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001395 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001396
1397 // If this is an anonymous namespace, register it as the anonymous
1398 // namespace within its context.
1399 if (!Name) {
1400 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1401 TU->setAnonymousNamespace(ToNamespace);
1402 else
1403 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1404 }
1405 }
1406 Importer.Imported(D, ToNamespace);
1407
1408 ImportDeclContext(D);
1409
1410 return ToNamespace;
1411}
1412
Richard Smithdda56e42011-04-15 14:24:37 +00001413Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001414 // Import the major distinguishing characteristics of this typedef.
1415 DeclContext *DC, *LexicalDC;
1416 DeclarationName Name;
1417 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001418 NamedDecl *ToD;
1419 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001420 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001421 if (ToD)
1422 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001423
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001424 // If this typedef is not in block scope, determine whether we've
1425 // seen a typedef with the same name (that we can merge with) or any
1426 // other entity by that name (which name lookup could conflict with).
1427 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001428 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001429 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001430 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001431 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001432 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1433 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001434 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001435 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001436 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001437 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1438 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001439 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001440 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001441
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001442 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001443 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001444
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001445 if (!ConflictingDecls.empty()) {
1446 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1447 ConflictingDecls.data(),
1448 ConflictingDecls.size());
1449 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001450 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001451 }
1452 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001453
Douglas Gregorb4964f72010-02-15 23:54:17 +00001454 // Import the underlying type of this typedef;
1455 QualType T = Importer.Import(D->getUnderlyingType());
1456 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001457 return nullptr;
1458
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001459 // Create the new typedef node.
1460 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001461 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001462 TypedefNameDecl *ToTypedef;
1463 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001464 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1465 StartL, Loc,
1466 Name.getAsIdentifierInfo(),
1467 TInfo);
1468 else
Richard Smithdda56e42011-04-15 14:24:37 +00001469 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1470 StartL, Loc,
1471 Name.getAsIdentifierInfo(),
1472 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001473
Douglas Gregordd483172010-02-22 17:42:47 +00001474 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001475 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001476 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001477 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001478
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001479 return ToTypedef;
1480}
1481
Richard Smithdda56e42011-04-15 14:24:37 +00001482Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1483 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1484}
1485
1486Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1487 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1488}
1489
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001490Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1491 // Import the major distinguishing characteristics of this label.
1492 DeclContext *DC, *LexicalDC;
1493 DeclarationName Name;
1494 SourceLocation Loc;
1495 NamedDecl *ToD;
1496 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1497 return nullptr;
1498 if (ToD)
1499 return ToD;
1500
1501 assert(LexicalDC->isFunctionOrMethod());
1502
1503 LabelDecl *ToLabel = D->isGnuLocal()
1504 ? LabelDecl::Create(Importer.getToContext(),
1505 DC, Importer.Import(D->getLocation()),
1506 Name.getAsIdentifierInfo(),
1507 Importer.Import(D->getLocStart()))
1508 : LabelDecl::Create(Importer.getToContext(),
1509 DC, Importer.Import(D->getLocation()),
1510 Name.getAsIdentifierInfo());
1511 Importer.Imported(D, ToLabel);
1512
1513 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1514 if (!Label)
1515 return nullptr;
1516
1517 ToLabel->setStmt(Label);
1518 ToLabel->setLexicalDeclContext(LexicalDC);
1519 LexicalDC->addDeclInternal(ToLabel);
1520 return ToLabel;
1521}
1522
Douglas Gregor98c10182010-02-12 22:17:39 +00001523Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1524 // Import the major distinguishing characteristics of this enum.
1525 DeclContext *DC, *LexicalDC;
1526 DeclarationName Name;
1527 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001528 NamedDecl *ToD;
1529 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001530 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001531 if (ToD)
1532 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001533
Douglas Gregor98c10182010-02-12 22:17:39 +00001534 // Figure out what enum name we're looking for.
1535 unsigned IDNS = Decl::IDNS_Tag;
1536 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001537 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1538 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001539 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001540 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001541 IDNS |= Decl::IDNS_Ordinary;
1542
1543 // We may already have an enum of the same name; try to find and match it.
1544 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001545 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001546 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001547 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001548 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1549 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001550 continue;
1551
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001552 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001553 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001554 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1555 Found = Tag->getDecl();
1556 }
1557
1558 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001559 if (IsStructuralMatch(D, FoundEnum))
1560 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001561 }
1562
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001563 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001564 }
1565
1566 if (!ConflictingDecls.empty()) {
1567 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1568 ConflictingDecls.data(),
1569 ConflictingDecls.size());
1570 }
1571 }
1572
1573 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001574 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1575 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001576 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001577 D->isScoped(), D->isScopedUsingClassTag(),
1578 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001579 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001580 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001581 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001582 D2->setLexicalDeclContext(LexicalDC);
1583 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001584 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001585
1586 // Import the integer type.
1587 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1588 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001589 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001590 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001591
1592 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001593 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001594 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001595
Douglas Gregor3996e242010-02-15 22:01:00 +00001596 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001597}
1598
Douglas Gregor5c73e912010-02-11 00:48:18 +00001599Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1600 // If this record has a definition in the translation unit we're coming from,
1601 // but this particular declaration is not that definition, import the
1602 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001603 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001604 if (Definition && Definition != D) {
1605 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001606 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001607 return nullptr;
1608
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001609 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001610 }
1611
1612 // Import the major distinguishing characteristics of this record.
1613 DeclContext *DC, *LexicalDC;
1614 DeclarationName Name;
1615 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001616 NamedDecl *ToD;
1617 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001618 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001619 if (ToD)
1620 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001621
Douglas Gregor5c73e912010-02-11 00:48:18 +00001622 // Figure out what structure name we're looking for.
1623 unsigned IDNS = Decl::IDNS_Tag;
1624 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001625 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1626 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001627 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001628 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001629 IDNS |= Decl::IDNS_Ordinary;
1630
1631 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001632 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001633 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001634 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001635 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001636 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001637 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001638
1639 if (!FoundDecls.empty()) {
1640 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1641 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1642 D->getASTContext().getExternalSource()->CompleteType(D);
1643 }
1644
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001645 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1646 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001647 continue;
1648
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001649 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001650 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001651 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1652 Found = Tag->getDecl();
1653 }
1654
1655 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001656 if (D->isAnonymousStructOrUnion() &&
1657 FoundRecord->isAnonymousStructOrUnion()) {
1658 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001659 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001660 if (Optional<unsigned> Index1 =
1661 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1662 D)) {
1663 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001664 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001665 if (*Index1 != *Index2)
1666 continue;
1667 }
1668 }
1669 }
1670
Sean Callanan9092d472017-05-13 00:46:33 +00001671 PrevDecl = FoundRecord;
1672
Douglas Gregor25791052010-02-12 00:09:27 +00001673 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001674 if ((SearchName && !D->isCompleteDefinition())
1675 || (D->isCompleteDefinition() &&
1676 D->isAnonymousStructOrUnion()
1677 == FoundDef->isAnonymousStructOrUnion() &&
1678 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001679 // The record types structurally match, or the "from" translation
1680 // unit only had a forward declaration anyway; call it the same
1681 // function.
1682 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001683 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001684 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001685 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001686 // We have a forward declaration of this type, so adopt that forward
1687 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001688
1689 // If one or both can be completed from external storage then try one
1690 // last time to complete and compare them before doing this.
1691
1692 if (FoundRecord->hasExternalLexicalStorage() &&
1693 !FoundRecord->isCompleteDefinition())
1694 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1695 if (D->hasExternalLexicalStorage())
1696 D->getASTContext().getExternalSource()->CompleteType(D);
1697
1698 if (FoundRecord->isCompleteDefinition() &&
1699 D->isCompleteDefinition() &&
1700 !IsStructuralMatch(D, FoundRecord))
1701 continue;
1702
Douglas Gregor25791052010-02-12 00:09:27 +00001703 AdoptDecl = FoundRecord;
1704 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001705 } else if (!SearchName) {
1706 continue;
1707 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001708 }
1709
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001710 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001711 }
1712
Douglas Gregordd6006f2012-07-17 21:16:27 +00001713 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001714 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1715 ConflictingDecls.data(),
1716 ConflictingDecls.size());
1717 }
1718 }
1719
1720 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001721 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001722 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001723 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001724 CXXRecordDecl *D2CXX = nullptr;
1725 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1726 if (DCXX->isLambda()) {
1727 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1728 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1729 DC, TInfo, Loc,
1730 DCXX->isDependentLambda(),
1731 DCXX->isGenericLambda(),
1732 DCXX->getLambdaCaptureDefault());
1733 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1734 if (DCXX->getLambdaContextDecl() && !CDecl)
1735 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001736 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1737 } else if (DCXX->isInjectedClassName()) {
1738 // We have to be careful to do a similar dance to the one in
1739 // Sema::ActOnStartCXXMemberDeclarations
1740 CXXRecordDecl *const PrevDecl = nullptr;
1741 const bool DelayTypeCreation = true;
1742 D2CXX = CXXRecordDecl::Create(
1743 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1744 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1745 Importer.getToContext().getTypeDeclType(
1746 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001747 } else {
1748 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1749 D->getTagKind(),
1750 DC, StartLoc, Loc,
1751 Name.getAsIdentifierInfo());
1752 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001753 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001754 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00001755 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001756 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001757 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001758 }
Douglas Gregor14454802011-02-25 02:25:35 +00001759
1760 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001761 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001762 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00001763 if (D->isAnonymousStructOrUnion())
1764 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00001765 if (PrevDecl) {
1766 // FIXME: do this for all Redeclarables, not just RecordDecls.
1767 D2->setPreviousDecl(PrevDecl);
1768 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001769 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001770
Douglas Gregor3996e242010-02-15 22:01:00 +00001771 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001772
Douglas Gregor95d82832012-01-24 18:36:04 +00001773 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00001774 return nullptr;
1775
Douglas Gregor3996e242010-02-15 22:01:00 +00001776 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001777}
1778
Douglas Gregor98c10182010-02-12 22:17:39 +00001779Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1780 // Import the major distinguishing characteristics of this enumerator.
1781 DeclContext *DC, *LexicalDC;
1782 DeclarationName Name;
1783 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001784 NamedDecl *ToD;
1785 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001786 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001787 if (ToD)
1788 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001789
1790 QualType T = Importer.Import(D->getType());
1791 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001792 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001793
Douglas Gregor98c10182010-02-12 22:17:39 +00001794 // Determine whether there are any other declarations with the same name and
1795 // in the same context.
1796 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001797 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00001798 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001799 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001800 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001801 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1802 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001803 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00001804
1805 if (EnumConstantDecl *FoundEnumConstant
1806 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
1807 if (IsStructuralMatch(D, FoundEnumConstant))
1808 return Importer.Imported(D, FoundEnumConstant);
1809 }
1810
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001811 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001812 }
1813
1814 if (!ConflictingDecls.empty()) {
1815 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1816 ConflictingDecls.data(),
1817 ConflictingDecls.size());
1818 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001819 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001820 }
1821 }
1822
1823 Expr *Init = Importer.Import(D->getInitExpr());
1824 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00001825 return nullptr;
1826
Douglas Gregor98c10182010-02-12 22:17:39 +00001827 EnumConstantDecl *ToEnumerator
1828 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1829 Name.getAsIdentifierInfo(), T,
1830 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00001831 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00001832 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001833 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00001834 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001835 return ToEnumerator;
1836}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001837
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001838Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1839 // Import the major distinguishing characteristics of this function.
1840 DeclContext *DC, *LexicalDC;
1841 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001842 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001843 NamedDecl *ToD;
1844 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001845 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001846 if (ToD)
1847 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001848
Gabor Horvathe350b0a2017-09-22 11:11:01 +00001849 const FunctionDecl *FoundWithoutBody = nullptr;
1850
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001851 // Try to find a function in our own ("to") context with the same name, same
1852 // type, and in the same context as the function we're importing.
1853 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001854 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001855 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001856 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001857 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001858 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1859 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001860 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001861
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001862 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00001863 if (FoundFunction->hasExternalFormalLinkage() &&
1864 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001865 if (Importer.IsStructurallyEquivalent(D->getType(),
1866 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001867 // FIXME: Actually try to merge the body and other attributes.
Gabor Horvathe350b0a2017-09-22 11:11:01 +00001868 const FunctionDecl *FromBodyDecl = nullptr;
1869 D->hasBody(FromBodyDecl);
1870 if (D == FromBodyDecl && !FoundFunction->hasBody()) {
1871 // This function is needed to merge completely.
1872 FoundWithoutBody = FoundFunction;
1873 break;
1874 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001875 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001876 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001877
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001878 // FIXME: Check for overloading more carefully, e.g., by boosting
1879 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001880
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001881 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001882 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001883 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001884
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001885 // Complain about inconsistent function types.
1886 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001887 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001888 Importer.ToDiag(FoundFunction->getLocation(),
1889 diag::note_odr_value_here)
1890 << FoundFunction->getType();
1891 }
1892 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001893
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001894 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001895 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001896
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001897 if (!ConflictingDecls.empty()) {
1898 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1899 ConflictingDecls.data(),
1900 ConflictingDecls.size());
1901 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001902 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001903 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001904 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001905
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001906 DeclarationNameInfo NameInfo(Name, Loc);
1907 // Import additional name location/type info.
1908 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
1909
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001910 QualType FromTy = D->getType();
1911 bool usedDifferentExceptionSpec = false;
1912
1913 if (const FunctionProtoType *
1914 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
1915 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
1916 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
1917 // FunctionDecl that we are importing the FunctionProtoType for.
1918 // To avoid an infinite recursion when importing, create the FunctionDecl
1919 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00001920 if (FromEPI.ExceptionSpec.SourceDecl ||
1921 FromEPI.ExceptionSpec.SourceTemplate ||
1922 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001923 FunctionProtoType::ExtProtoInfo DefaultEPI;
1924 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00001925 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001926 usedDifferentExceptionSpec = true;
1927 }
1928 }
1929
Douglas Gregorb4964f72010-02-15 23:54:17 +00001930 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001931 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001932 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001933 return nullptr;
1934
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001935 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001936 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00001937 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001938 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001939 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00001940 return nullptr;
1941
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001942 Parameters.push_back(ToP);
1943 }
1944
1945 // Create the imported function.
1946 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00001947 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001948 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00001949 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1950 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1951 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001952 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001953 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001954 FromConstructor->isExplicit(),
1955 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001956 D->isImplicit(),
1957 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00001958 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
1959 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
1960 for (CXXCtorInitializer *I : FromConstructor->inits()) {
1961 CXXCtorInitializer *ToI =
1962 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
1963 if (!ToI && I)
1964 return nullptr;
1965 CtorInitializers.push_back(ToI);
1966 }
1967 CXXCtorInitializer **Memory =
1968 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
1969 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
1970 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
1971 ToCtor->setCtorInitializers(Memory);
1972 ToCtor->setNumCtorInitializers(NumInitializers);
1973 }
Douglas Gregor00eace12010-02-21 18:29:16 +00001974 } else if (isa<CXXDestructorDecl>(D)) {
1975 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1976 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001977 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001978 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001979 D->isInlineSpecified(),
1980 D->isImplicit());
1981 } else if (CXXConversionDecl *FromConversion
1982 = dyn_cast<CXXConversionDecl>(D)) {
1983 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1984 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001985 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001986 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001987 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001988 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001989 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001990 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00001991 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
1992 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
1993 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001994 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00001995 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001996 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001997 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001998 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001999 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002000 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002001 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002002 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002003 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002004 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002005 D->hasWrittenPrototype(),
2006 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002007 }
John McCall3e11ebe2010-03-15 10:12:16 +00002008
2009 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002010 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002011 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002012 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002013 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2014 ToFunction->setTrivial(D->isTrivial());
2015 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002016 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002017
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002018 // Set the parameters.
2019 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002020 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002021 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002022 }
David Blaikie9c70e042011-09-21 18:16:56 +00002023 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002024
Gabor Horvathe350b0a2017-09-22 11:11:01 +00002025 if (FoundWithoutBody) {
2026 auto *Recent = const_cast<FunctionDecl *>(
2027 FoundWithoutBody->getMostRecentDecl());
2028 ToFunction->setPreviousDecl(Recent);
2029 }
2030
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002031 if (usedDifferentExceptionSpec) {
2032 // Update FunctionProtoType::ExtProtoInfo.
2033 QualType T = Importer.Import(D->getType());
2034 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002035 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002036 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002037 }
2038
Sean Callanan59721b32015-04-28 18:41:46 +00002039 // Import the body, if any.
2040 if (Stmt *FromBody = D->getBody()) {
2041 if (Stmt *ToBody = Importer.Import(FromBody)) {
2042 ToFunction->setBody(ToBody);
2043 }
2044 }
2045
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002046 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002047
2048 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002049 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002050
Lang Hames19e07e12017-06-20 21:06:00 +00002051 if (auto *FromCXXMethod = dyn_cast<CXXMethodDecl>(D))
2052 ImportOverrides(cast<CXXMethodDecl>(ToFunction), FromCXXMethod);
2053
Douglas Gregor43f54792010-02-17 02:12:47 +00002054 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002055}
2056
Douglas Gregor00eace12010-02-21 18:29:16 +00002057Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2058 return VisitFunctionDecl(D);
2059}
2060
2061Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2062 return VisitCXXMethodDecl(D);
2063}
2064
2065Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2066 return VisitCXXMethodDecl(D);
2067}
2068
2069Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2070 return VisitCXXMethodDecl(D);
2071}
2072
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002073static unsigned getFieldIndex(Decl *F) {
2074 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2075 if (!Owner)
2076 return 0;
2077
2078 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002079 for (const auto *D : Owner->noload_decls()) {
2080 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002081 return Index;
2082
2083 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2084 ++Index;
2085 }
2086
2087 return Index;
2088}
2089
Douglas Gregor5c73e912010-02-11 00:48:18 +00002090Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2091 // Import the major distinguishing characteristics of a variable.
2092 DeclContext *DC, *LexicalDC;
2093 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002094 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002095 NamedDecl *ToD;
2096 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002097 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002098 if (ToD)
2099 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002100
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002101 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002102 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002103 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002104 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2105 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002106 // For anonymous fields, match up by index.
2107 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2108 continue;
2109
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002110 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002111 FoundField->getType())) {
2112 Importer.Imported(D, FoundField);
2113 return FoundField;
2114 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002115
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002116 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2117 << Name << D->getType() << FoundField->getType();
2118 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2119 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002120 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002121 }
2122 }
2123
Douglas Gregorb4964f72010-02-15 23:54:17 +00002124 // Import the type.
2125 QualType T = Importer.Import(D->getType());
2126 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002127 return nullptr;
2128
Douglas Gregor5c73e912010-02-11 00:48:18 +00002129 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2130 Expr *BitWidth = Importer.Import(D->getBitWidth());
2131 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002132 return nullptr;
2133
Abramo Bagnaradff19302011-03-08 08:55:46 +00002134 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2135 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002136 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002137 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002138 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002139 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002140 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002141 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002142 Expr *ToInitializer = Importer.Import(FromInitializer);
2143 if (ToInitializer)
2144 ToField->setInClassInitializer(ToInitializer);
2145 else
2146 return nullptr;
2147 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002148 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002149 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002150 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002151 return ToField;
2152}
2153
Francois Pichet783dd6e2010-11-21 06:08:52 +00002154Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2155 // Import the major distinguishing characteristics of a variable.
2156 DeclContext *DC, *LexicalDC;
2157 DeclarationName Name;
2158 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002159 NamedDecl *ToD;
2160 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002161 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002162 if (ToD)
2163 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002164
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002165 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002166 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002167 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002168 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002169 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002170 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002171 // For anonymous indirect fields, match up by index.
2172 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2173 continue;
2174
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002175 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002176 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002177 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002178 Importer.Imported(D, FoundField);
2179 return FoundField;
2180 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002181
2182 // If there are more anonymous fields to check, continue.
2183 if (!Name && I < N-1)
2184 continue;
2185
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002186 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2187 << Name << D->getType() << FoundField->getType();
2188 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2189 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002190 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002191 }
2192 }
2193
Francois Pichet783dd6e2010-11-21 06:08:52 +00002194 // Import the type.
2195 QualType T = Importer.Import(D->getType());
2196 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002197 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002198
2199 NamedDecl **NamedChain =
2200 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2201
2202 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002203 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002204 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002205 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002206 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002207 NamedChain[i++] = cast<NamedDecl>(D);
2208 }
2209
2210 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002211 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002212 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002213
2214 for (const auto *Attr : D->attrs())
2215 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2216
Francois Pichet783dd6e2010-11-21 06:08:52 +00002217 ToIndirectField->setAccess(D->getAccess());
2218 ToIndirectField->setLexicalDeclContext(LexicalDC);
2219 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002220 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002221 return ToIndirectField;
2222}
2223
Aleksei Sidorina693b372016-09-28 10:16:56 +00002224Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2225 // Import the major distinguishing characteristics of a declaration.
2226 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2227 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2228 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2229 if (!DC || !LexicalDC)
2230 return nullptr;
2231
2232 // Determine whether we've already imported this decl.
2233 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2234 auto *RD = cast<CXXRecordDecl>(DC);
2235 FriendDecl *ImportedFriend = RD->getFirstFriend();
2236 StructuralEquivalenceContext Context(
2237 Importer.getFromContext(), Importer.getToContext(),
2238 Importer.getNonEquivalentDecls(), false, false);
2239
2240 while (ImportedFriend) {
2241 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2242 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2243 ImportedFriend->getFriendDecl()))
2244 return Importer.Imported(D, ImportedFriend);
2245
2246 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2247 if (Importer.IsStructurallyEquivalent(
2248 D->getFriendType()->getType(),
2249 ImportedFriend->getFriendType()->getType(), true))
2250 return Importer.Imported(D, ImportedFriend);
2251 }
2252 ImportedFriend = ImportedFriend->getNextFriend();
2253 }
2254
2255 // Not found. Create it.
2256 FriendDecl::FriendUnion ToFU;
2257 if (NamedDecl *FriendD = D->getFriendDecl())
2258 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2259 else
2260 ToFU = Importer.Import(D->getFriendType());
2261 if (!ToFU)
2262 return nullptr;
2263
2264 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2265 TemplateParameterList **FromTPLists =
2266 D->getTrailingObjects<TemplateParameterList *>();
2267 for (unsigned I = 0; I < D->NumTPLists; I++) {
2268 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2269 if (!List)
2270 return nullptr;
2271 ToTPLists[I] = List;
2272 }
2273
2274 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2275 Importer.Import(D->getLocation()),
2276 ToFU, Importer.Import(D->getFriendLoc()),
2277 ToTPLists);
2278
2279 Importer.Imported(D, FrD);
2280 RD->pushFriendDecl(FrD);
2281
2282 FrD->setAccess(D->getAccess());
2283 FrD->setLexicalDeclContext(LexicalDC);
2284 LexicalDC->addDeclInternal(FrD);
2285 return FrD;
2286}
2287
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002288Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2289 // Import the major distinguishing characteristics of an ivar.
2290 DeclContext *DC, *LexicalDC;
2291 DeclarationName Name;
2292 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002293 NamedDecl *ToD;
2294 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002295 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002296 if (ToD)
2297 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002298
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002299 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002300 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002301 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002302 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2303 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002304 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002305 FoundIvar->getType())) {
2306 Importer.Imported(D, FoundIvar);
2307 return FoundIvar;
2308 }
2309
2310 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2311 << Name << D->getType() << FoundIvar->getType();
2312 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2313 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002314 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002315 }
2316 }
2317
2318 // Import the type.
2319 QualType T = Importer.Import(D->getType());
2320 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002321 return nullptr;
2322
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002323 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2324 Expr *BitWidth = Importer.Import(D->getBitWidth());
2325 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002326 return nullptr;
2327
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002328 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2329 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002330 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002331 Loc, Name.getAsIdentifierInfo(),
2332 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002333 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002334 ToIvar->setLexicalDeclContext(LexicalDC);
2335 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002336 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002337 return ToIvar;
2338
2339}
2340
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002341Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2342 // Import the major distinguishing characteristics of a variable.
2343 DeclContext *DC, *LexicalDC;
2344 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002345 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002346 NamedDecl *ToD;
2347 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002348 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002349 if (ToD)
2350 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002351
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002352 // Try to find a variable in our own ("to") context with the same name and
2353 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002354 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002355 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002356 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002357 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002358 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002359 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002360 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2361 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002362 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002363
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002364 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002365 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002366 if (FoundVar->hasExternalFormalLinkage() &&
2367 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002368 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002369 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002370 MergeWithVar = FoundVar;
2371 break;
2372 }
2373
Douglas Gregor56521c52010-02-12 17:23:39 +00002374 const ArrayType *FoundArray
2375 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2376 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002377 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002378 if (FoundArray && TArray) {
2379 if (isa<IncompleteArrayType>(FoundArray) &&
2380 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002381 // Import the type.
2382 QualType T = Importer.Import(D->getType());
2383 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002384 return nullptr;
2385
Douglas Gregor56521c52010-02-12 17:23:39 +00002386 FoundVar->setType(T);
2387 MergeWithVar = FoundVar;
2388 break;
2389 } else if (isa<IncompleteArrayType>(TArray) &&
2390 isa<ConstantArrayType>(FoundArray)) {
2391 MergeWithVar = FoundVar;
2392 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002393 }
2394 }
2395
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002396 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002397 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002398 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2399 << FoundVar->getType();
2400 }
2401 }
2402
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002403 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002404 }
2405
2406 if (MergeWithVar) {
2407 // An equivalent variable with external linkage has been found. Link
2408 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002409 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002410
2411 if (VarDecl *DDef = D->getDefinition()) {
2412 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2413 Importer.ToDiag(ExistingDef->getLocation(),
2414 diag::err_odr_variable_multiple_def)
2415 << Name;
2416 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2417 } else {
2418 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002419 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002420 if (DDef->isInitKnownICE()) {
2421 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2422 Eval->CheckedICE = true;
2423 Eval->IsICE = DDef->isInitICE();
2424 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002425 }
2426 }
2427
2428 return MergeWithVar;
2429 }
2430
2431 if (!ConflictingDecls.empty()) {
2432 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2433 ConflictingDecls.data(),
2434 ConflictingDecls.size());
2435 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002436 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002437 }
2438 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002439
Douglas Gregorb4964f72010-02-15 23:54:17 +00002440 // Import the type.
2441 QualType T = Importer.Import(D->getType());
2442 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002443 return nullptr;
2444
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002445 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002446 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002447 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2448 Importer.Import(D->getInnerLocStart()),
2449 Loc, Name.getAsIdentifierInfo(),
2450 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002451 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002452 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002453 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002454 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002455 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002456 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002457
Sean Callanan59721b32015-04-28 18:41:46 +00002458 if (!D->isFileVarDecl() &&
2459 D->isUsed())
2460 ToVar->setIsUsed();
2461
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002462 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002463 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002464 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002465
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002466 if (D->isConstexpr())
2467 ToVar->setConstexpr(true);
2468
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002469 return ToVar;
2470}
2471
Douglas Gregor8b228d72010-02-17 21:22:52 +00002472Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2473 // Parameters are created in the translation unit's context, then moved
2474 // into the function declaration's context afterward.
2475 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2476
2477 // Import the name of this declaration.
2478 DeclarationName Name = Importer.Import(D->getDeclName());
2479 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002480 return nullptr;
2481
Douglas Gregor8b228d72010-02-17 21:22:52 +00002482 // Import the location of this declaration.
2483 SourceLocation Loc = Importer.Import(D->getLocation());
2484
2485 // Import the parameter's type.
2486 QualType T = Importer.Import(D->getType());
2487 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002488 return nullptr;
2489
Douglas Gregor8b228d72010-02-17 21:22:52 +00002490 // Create the imported parameter.
Alexey Bataev56223232017-06-09 13:40:18 +00002491 auto *ToParm = ImplicitParamDecl::Create(Importer.getToContext(), DC, Loc,
2492 Name.getAsIdentifierInfo(), T,
2493 D->getParameterKind());
Douglas Gregor8b228d72010-02-17 21:22:52 +00002494 return Importer.Imported(D, ToParm);
2495}
2496
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002497Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2498 // Parameters are created in the translation unit's context, then moved
2499 // into the function declaration's context afterward.
2500 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2501
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002502 // Import the name of this declaration.
2503 DeclarationName Name = Importer.Import(D->getDeclName());
2504 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002505 return nullptr;
2506
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002507 // Import the location of this declaration.
2508 SourceLocation Loc = Importer.Import(D->getLocation());
2509
2510 // Import the parameter's type.
2511 QualType T = Importer.Import(D->getType());
2512 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002513 return nullptr;
2514
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002515 // Create the imported parameter.
2516 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2517 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002518 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002519 Loc, Name.getAsIdentifierInfo(),
2520 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002521 /*DefaultArg*/ nullptr);
2522
2523 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002524 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002525 ToParm->setKNRPromoted(D->isKNRPromoted());
2526
2527 Expr *ToDefArg = nullptr;
2528 Expr *FromDefArg = nullptr;
2529 if (D->hasUninstantiatedDefaultArg()) {
2530 FromDefArg = D->getUninstantiatedDefaultArg();
2531 ToDefArg = Importer.Import(FromDefArg);
2532 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2533 } else if (D->hasUnparsedDefaultArg()) {
2534 ToParm->setUnparsedDefaultArg();
2535 } else if (D->hasDefaultArg()) {
2536 FromDefArg = D->getDefaultArg();
2537 ToDefArg = Importer.Import(FromDefArg);
2538 ToParm->setDefaultArg(ToDefArg);
2539 }
2540 if (FromDefArg && !ToDefArg)
2541 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002542
2543 if (D->isUsed())
2544 ToParm->setIsUsed();
2545
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002546 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002547}
2548
Douglas Gregor43f54792010-02-17 02:12:47 +00002549Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2550 // Import the major distinguishing characteristics of a method.
2551 DeclContext *DC, *LexicalDC;
2552 DeclarationName Name;
2553 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002554 NamedDecl *ToD;
2555 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002556 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002557 if (ToD)
2558 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002559
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002560 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002561 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002562 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2563 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002564 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2565 continue;
2566
2567 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002568 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2569 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002570 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002571 << D->isInstanceMethod() << Name << D->getReturnType()
2572 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002573 Importer.ToDiag(FoundMethod->getLocation(),
2574 diag::note_odr_objc_method_here)
2575 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002576 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002577 }
2578
2579 // Check the number of parameters.
2580 if (D->param_size() != FoundMethod->param_size()) {
2581 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2582 << D->isInstanceMethod() << Name
2583 << D->param_size() << FoundMethod->param_size();
2584 Importer.ToDiag(FoundMethod->getLocation(),
2585 diag::note_odr_objc_method_here)
2586 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002587 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002588 }
2589
2590 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002591 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002592 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2593 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002594 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002595 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002596 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002597 diag::err_odr_objc_method_param_type_inconsistent)
2598 << D->isInstanceMethod() << Name
2599 << (*P)->getType() << (*FoundP)->getType();
2600 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2601 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002602 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002603 }
2604 }
2605
2606 // Check variadic/non-variadic.
2607 // Check the number of parameters.
2608 if (D->isVariadic() != FoundMethod->isVariadic()) {
2609 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2610 << D->isInstanceMethod() << Name;
2611 Importer.ToDiag(FoundMethod->getLocation(),
2612 diag::note_odr_objc_method_here)
2613 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002614 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002615 }
2616
2617 // FIXME: Any other bits we need to merge?
2618 return Importer.Imported(D, FoundMethod);
2619 }
2620 }
2621
2622 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00002623 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002624 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002625 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002626
Alp Toker314cc812014-01-25 16:55:45 +00002627 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00002628
Alp Toker314cc812014-01-25 16:55:45 +00002629 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2630 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2631 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2632 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2633 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002634
2635 // FIXME: When we decide to merge method definitions, we'll need to
2636 // deal with implicit parameters.
2637
2638 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002639 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00002640 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00002641 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00002642 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002643 return nullptr;
2644
Douglas Gregor43f54792010-02-17 02:12:47 +00002645 ToParams.push_back(ToP);
2646 }
2647
2648 // Set the parameters.
2649 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2650 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002651 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00002652 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002653 SmallVector<SourceLocation, 12> SelLocs;
2654 D->getSelectorLocs(SelLocs);
2655 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00002656
2657 ToMethod->setLexicalDeclContext(LexicalDC);
2658 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002659 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00002660 return ToMethod;
2661}
2662
Douglas Gregor85f3f952015-07-07 03:57:15 +00002663Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2664 // Import the major distinguishing characteristics of a category.
2665 DeclContext *DC, *LexicalDC;
2666 DeclarationName Name;
2667 SourceLocation Loc;
2668 NamedDecl *ToD;
2669 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2670 return nullptr;
2671 if (ToD)
2672 return ToD;
2673
2674 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2675 if (!BoundInfo)
2676 return nullptr;
2677
2678 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2679 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00002680 D->getVariance(),
2681 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00002682 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002683 Importer.Import(D->getLocation()),
2684 Name.getAsIdentifierInfo(),
2685 Importer.Import(D->getColonLoc()),
2686 BoundInfo);
2687 Importer.Imported(D, Result);
2688 Result->setLexicalDeclContext(LexicalDC);
2689 return Result;
2690}
2691
Douglas Gregor84c51c32010-02-18 01:47:50 +00002692Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2693 // Import the major distinguishing characteristics of a category.
2694 DeclContext *DC, *LexicalDC;
2695 DeclarationName Name;
2696 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002697 NamedDecl *ToD;
2698 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002699 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002700 if (ToD)
2701 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002702
Douglas Gregor84c51c32010-02-18 01:47:50 +00002703 ObjCInterfaceDecl *ToInterface
2704 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2705 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00002706 return nullptr;
2707
Douglas Gregor84c51c32010-02-18 01:47:50 +00002708 // Determine if we've already encountered this category.
2709 ObjCCategoryDecl *MergeWithCategory
2710 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2711 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2712 if (!ToCategory) {
2713 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002714 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00002715 Loc,
2716 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002717 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002718 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002719 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002720 Importer.Import(D->getIvarLBraceLoc()),
2721 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002722 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002723 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002724 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002725 // Import the type parameter list after calling Imported, to avoid
2726 // loops when bringing in their DeclContext.
2727 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2728 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002729
Douglas Gregor84c51c32010-02-18 01:47:50 +00002730 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002731 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2732 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002733 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2734 = D->protocol_loc_begin();
2735 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2736 FromProtoEnd = D->protocol_end();
2737 FromProto != FromProtoEnd;
2738 ++FromProto, ++FromProtoLoc) {
2739 ObjCProtocolDecl *ToProto
2740 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2741 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00002742 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002743 Protocols.push_back(ToProto);
2744 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2745 }
2746
2747 // FIXME: If we're merging, make sure that the protocol list is the same.
2748 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2749 ProtocolLocs.data(), Importer.getToContext());
2750
2751 } else {
2752 Importer.Imported(D, ToCategory);
2753 }
2754
2755 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002756 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002757
2758 // If we have an implementation, import it as well.
2759 if (D->getImplementation()) {
2760 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002761 = cast_or_null<ObjCCategoryImplDecl>(
2762 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002763 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00002764 return nullptr;
2765
Douglas Gregor84c51c32010-02-18 01:47:50 +00002766 ToCategory->setImplementation(Impl);
2767 }
2768
2769 return ToCategory;
2770}
2771
Douglas Gregor2aa53772012-01-24 17:42:07 +00002772bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2773 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002774 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002775 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00002776 if (shouldForceImportDeclContext(Kind))
2777 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002778 return false;
2779 }
2780
2781 // Start the protocol definition
2782 To->startDefinition();
2783
2784 // Import protocols
2785 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2786 SmallVector<SourceLocation, 4> ProtocolLocs;
2787 ObjCProtocolDecl::protocol_loc_iterator
2788 FromProtoLoc = From->protocol_loc_begin();
2789 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2790 FromProtoEnd = From->protocol_end();
2791 FromProto != FromProtoEnd;
2792 ++FromProto, ++FromProtoLoc) {
2793 ObjCProtocolDecl *ToProto
2794 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2795 if (!ToProto)
2796 return true;
2797 Protocols.push_back(ToProto);
2798 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2799 }
2800
2801 // FIXME: If we're merging, make sure that the protocol list is the same.
2802 To->setProtocolList(Protocols.data(), Protocols.size(),
2803 ProtocolLocs.data(), Importer.getToContext());
2804
Douglas Gregor2e15c842012-02-01 21:00:38 +00002805 if (shouldForceImportDeclContext(Kind)) {
2806 // Import all of the members of this protocol.
2807 ImportDeclContext(From, /*ForceImport=*/true);
2808 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002809 return false;
2810}
2811
Douglas Gregor98d156a2010-02-17 16:12:00 +00002812Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002813 // If this protocol has a definition in the translation unit we're coming
2814 // from, but this particular declaration is not that definition, import the
2815 // definition and map to that.
2816 ObjCProtocolDecl *Definition = D->getDefinition();
2817 if (Definition && Definition != D) {
2818 Decl *ImportedDef = Importer.Import(Definition);
2819 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002820 return nullptr;
2821
Douglas Gregor2aa53772012-01-24 17:42:07 +00002822 return Importer.Imported(D, ImportedDef);
2823 }
2824
Douglas Gregor84c51c32010-02-18 01:47:50 +00002825 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002826 DeclContext *DC, *LexicalDC;
2827 DeclarationName Name;
2828 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002829 NamedDecl *ToD;
2830 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002831 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002832 if (ToD)
2833 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00002834
Craig Topper36250ad2014-05-12 05:36:57 +00002835 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002836 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002837 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002838 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2839 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00002840 continue;
2841
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002842 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00002843 break;
2844 }
2845
2846 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00002847 if (!ToProto) {
2848 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
2849 Name.getAsIdentifierInfo(), Loc,
2850 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00002851 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002852 ToProto->setLexicalDeclContext(LexicalDC);
2853 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002854 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002855
2856 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002857
Douglas Gregor2aa53772012-01-24 17:42:07 +00002858 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00002859 return nullptr;
2860
Douglas Gregor98d156a2010-02-17 16:12:00 +00002861 return ToProto;
2862}
2863
Sean Callanan0aae0412014-12-10 00:00:37 +00002864Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
2865 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2866 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2867
2868 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
2869 SourceLocation LangLoc = Importer.Import(D->getLocation());
2870
2871 bool HasBraces = D->hasBraces();
2872
Sean Callananb12a8552014-12-10 21:22:20 +00002873 LinkageSpecDecl *ToLinkageSpec =
2874 LinkageSpecDecl::Create(Importer.getToContext(),
2875 DC,
2876 ExternLoc,
2877 LangLoc,
2878 D->getLanguage(),
2879 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00002880
2881 if (HasBraces) {
2882 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
2883 ToLinkageSpec->setRBraceLoc(RBraceLoc);
2884 }
2885
2886 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
2887 LexicalDC->addDeclInternal(ToLinkageSpec);
2888
2889 Importer.Imported(D, ToLinkageSpec);
2890
2891 return ToLinkageSpec;
2892}
2893
Douglas Gregor2aa53772012-01-24 17:42:07 +00002894bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
2895 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002896 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002897 if (To->getDefinition()) {
2898 // Check consistency of superclass.
2899 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
2900 if (FromSuper) {
2901 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
2902 if (!FromSuper)
2903 return true;
2904 }
2905
2906 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
2907 if ((bool)FromSuper != (bool)ToSuper ||
2908 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
2909 Importer.ToDiag(To->getLocation(),
2910 diag::err_odr_objc_superclass_inconsistent)
2911 << To->getDeclName();
2912 if (ToSuper)
2913 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
2914 << To->getSuperClass()->getDeclName();
2915 else
2916 Importer.ToDiag(To->getLocation(),
2917 diag::note_odr_objc_missing_superclass);
2918 if (From->getSuperClass())
2919 Importer.FromDiag(From->getSuperClassLoc(),
2920 diag::note_odr_objc_superclass)
2921 << From->getSuperClass()->getDeclName();
2922 else
2923 Importer.FromDiag(From->getLocation(),
2924 diag::note_odr_objc_missing_superclass);
2925 }
2926
Douglas Gregor2e15c842012-02-01 21:00:38 +00002927 if (shouldForceImportDeclContext(Kind))
2928 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002929 return false;
2930 }
2931
2932 // Start the definition.
2933 To->startDefinition();
2934
2935 // If this class has a superclass, import it.
2936 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00002937 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
2938 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00002939 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00002940
2941 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002942 }
2943
2944 // Import protocols
2945 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2946 SmallVector<SourceLocation, 4> ProtocolLocs;
2947 ObjCInterfaceDecl::protocol_loc_iterator
2948 FromProtoLoc = From->protocol_loc_begin();
2949
2950 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
2951 FromProtoEnd = From->protocol_end();
2952 FromProto != FromProtoEnd;
2953 ++FromProto, ++FromProtoLoc) {
2954 ObjCProtocolDecl *ToProto
2955 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2956 if (!ToProto)
2957 return true;
2958 Protocols.push_back(ToProto);
2959 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2960 }
2961
2962 // FIXME: If we're merging, make sure that the protocol list is the same.
2963 To->setProtocolList(Protocols.data(), Protocols.size(),
2964 ProtocolLocs.data(), Importer.getToContext());
2965
2966 // Import categories. When the categories themselves are imported, they'll
2967 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00002968 for (auto *Cat : From->known_categories())
2969 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002970
Douglas Gregor2aa53772012-01-24 17:42:07 +00002971 // If we have an @implementation, import it as well.
2972 if (From->getImplementation()) {
2973 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
2974 Importer.Import(From->getImplementation()));
2975 if (!Impl)
2976 return true;
2977
2978 To->setImplementation(Impl);
2979 }
2980
Douglas Gregor2e15c842012-02-01 21:00:38 +00002981 if (shouldForceImportDeclContext(Kind)) {
2982 // Import all of the members of this class.
2983 ImportDeclContext(From, /*ForceImport=*/true);
2984 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002985 return false;
2986}
2987
Douglas Gregor85f3f952015-07-07 03:57:15 +00002988ObjCTypeParamList *
2989ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
2990 if (!list)
2991 return nullptr;
2992
2993 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
2994 for (auto fromTypeParam : *list) {
2995 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
2996 Importer.Import(fromTypeParam));
2997 if (!toTypeParam)
2998 return nullptr;
2999
3000 toTypeParams.push_back(toTypeParam);
3001 }
3002
3003 return ObjCTypeParamList::create(Importer.getToContext(),
3004 Importer.Import(list->getLAngleLoc()),
3005 toTypeParams,
3006 Importer.Import(list->getRAngleLoc()));
3007}
3008
Douglas Gregor45635322010-02-16 01:20:57 +00003009Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003010 // If this class has a definition in the translation unit we're coming from,
3011 // but this particular declaration is not that definition, import the
3012 // definition and map to that.
3013 ObjCInterfaceDecl *Definition = D->getDefinition();
3014 if (Definition && Definition != D) {
3015 Decl *ImportedDef = Importer.Import(Definition);
3016 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003017 return nullptr;
3018
Douglas Gregor2aa53772012-01-24 17:42:07 +00003019 return Importer.Imported(D, ImportedDef);
3020 }
3021
Douglas Gregor45635322010-02-16 01:20:57 +00003022 // Import the major distinguishing characteristics of an @interface.
3023 DeclContext *DC, *LexicalDC;
3024 DeclarationName Name;
3025 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003026 NamedDecl *ToD;
3027 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003028 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003029 if (ToD)
3030 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003031
Douglas Gregor2aa53772012-01-24 17:42:07 +00003032 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003033 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003034 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003035 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003036 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3037 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003038 continue;
3039
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003040 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003041 break;
3042 }
3043
Douglas Gregor2aa53772012-01-24 17:42:07 +00003044 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003045 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003046 if (!ToIface) {
3047 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3048 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003049 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003050 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003051 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003052 D->isImplicitInterfaceDecl());
3053 ToIface->setLexicalDeclContext(LexicalDC);
3054 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003055 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003056 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003057 // Import the type parameter list after calling Imported, to avoid
3058 // loops when bringing in their DeclContext.
3059 ToIface->setTypeParamList(ImportObjCTypeParamList(
3060 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003061
Douglas Gregor2aa53772012-01-24 17:42:07 +00003062 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003063 return nullptr;
3064
Douglas Gregor98d156a2010-02-17 16:12:00 +00003065 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003066}
3067
Douglas Gregor4da9d682010-12-07 15:32:12 +00003068Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3069 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3070 Importer.Import(D->getCategoryDecl()));
3071 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003072 return nullptr;
3073
Douglas Gregor4da9d682010-12-07 15:32:12 +00003074 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3075 if (!ToImpl) {
3076 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3077 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003078 return nullptr;
3079
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003080 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003081 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003082 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003083 Category->getClassInterface(),
3084 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003085 Importer.Import(D->getAtStartLoc()),
3086 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003087
3088 DeclContext *LexicalDC = DC;
3089 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3090 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3091 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003092 return nullptr;
3093
Douglas Gregor4da9d682010-12-07 15:32:12 +00003094 ToImpl->setLexicalDeclContext(LexicalDC);
3095 }
3096
Sean Callanan95e74be2011-10-21 02:57:43 +00003097 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003098 Category->setImplementation(ToImpl);
3099 }
3100
3101 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003102 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003103 return ToImpl;
3104}
3105
Douglas Gregorda8025c2010-12-07 01:26:03 +00003106Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3107 // Find the corresponding interface.
3108 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3109 Importer.Import(D->getClassInterface()));
3110 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003111 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003112
3113 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003114 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003115 if (D->getSuperClass()) {
3116 Super = cast_or_null<ObjCInterfaceDecl>(
3117 Importer.Import(D->getSuperClass()));
3118 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003119 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003120 }
3121
3122 ObjCImplementationDecl *Impl = Iface->getImplementation();
3123 if (!Impl) {
3124 // We haven't imported an implementation yet. Create a new @implementation
3125 // now.
3126 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3127 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003128 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003129 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003130 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003131 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003132 Importer.Import(D->getIvarLBraceLoc()),
3133 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003134
3135 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3136 DeclContext *LexicalDC
3137 = Importer.ImportContext(D->getLexicalDeclContext());
3138 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003139 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003140 Impl->setLexicalDeclContext(LexicalDC);
3141 }
3142
3143 // Associate the implementation with the class it implements.
3144 Iface->setImplementation(Impl);
3145 Importer.Imported(D, Iface->getImplementation());
3146 } else {
3147 Importer.Imported(D, Iface->getImplementation());
3148
3149 // Verify that the existing @implementation has the same superclass.
3150 if ((Super && !Impl->getSuperClass()) ||
3151 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003152 (Super && Impl->getSuperClass() &&
3153 !declaresSameEntity(Super->getCanonicalDecl(),
3154 Impl->getSuperClass()))) {
3155 Importer.ToDiag(Impl->getLocation(),
3156 diag::err_odr_objc_superclass_inconsistent)
3157 << Iface->getDeclName();
3158 // FIXME: It would be nice to have the location of the superclass
3159 // below.
3160 if (Impl->getSuperClass())
3161 Importer.ToDiag(Impl->getLocation(),
3162 diag::note_odr_objc_superclass)
3163 << Impl->getSuperClass()->getDeclName();
3164 else
3165 Importer.ToDiag(Impl->getLocation(),
3166 diag::note_odr_objc_missing_superclass);
3167 if (D->getSuperClass())
3168 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003169 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003170 << D->getSuperClass()->getDeclName();
3171 else
3172 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003173 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003174 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003175 }
3176 }
3177
3178 // Import all of the members of this @implementation.
3179 ImportDeclContext(D);
3180
3181 return Impl;
3182}
3183
Douglas Gregora11c4582010-02-17 18:02:10 +00003184Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3185 // Import the major distinguishing characteristics of an @property.
3186 DeclContext *DC, *LexicalDC;
3187 DeclarationName Name;
3188 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003189 NamedDecl *ToD;
3190 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003191 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003192 if (ToD)
3193 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003194
3195 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003196 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003197 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003198 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003199 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003200 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003201 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003202 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003203 FoundProp->getType())) {
3204 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3205 << Name << D->getType() << FoundProp->getType();
3206 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3207 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003208 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003209 }
3210
3211 // FIXME: Check property attributes, getters, setters, etc.?
3212
3213 // Consider these properties to be equivalent.
3214 Importer.Imported(D, FoundProp);
3215 return FoundProp;
3216 }
3217 }
3218
3219 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003220 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3221 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003222 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003223
3224 // Create the new property.
3225 ObjCPropertyDecl *ToProperty
3226 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3227 Name.getAsIdentifierInfo(),
3228 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003229 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003230 Importer.Import(D->getType()),
3231 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003232 D->getPropertyImplementation());
3233 Importer.Imported(D, ToProperty);
3234 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003235 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003236
3237 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003238 ToProperty->setPropertyAttributesAsWritten(
3239 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003240 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3241 Importer.Import(D->getGetterNameLoc()));
3242 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3243 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003244 ToProperty->setGetterMethodDecl(
3245 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3246 ToProperty->setSetterMethodDecl(
3247 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3248 ToProperty->setPropertyIvarDecl(
3249 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3250 return ToProperty;
3251}
3252
Douglas Gregor14a49e22010-12-07 18:32:03 +00003253Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3254 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3255 Importer.Import(D->getPropertyDecl()));
3256 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003257 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003258
3259 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3260 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003261 return nullptr;
3262
Douglas Gregor14a49e22010-12-07 18:32:03 +00003263 // Import the lexical declaration context.
3264 DeclContext *LexicalDC = DC;
3265 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3266 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3267 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003268 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003269 }
3270
3271 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3272 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003273 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003274
3275 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003276 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003277 if (D->getPropertyIvarDecl()) {
3278 Ivar = cast_or_null<ObjCIvarDecl>(
3279 Importer.Import(D->getPropertyIvarDecl()));
3280 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003281 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003282 }
3283
3284 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003285 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3286 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003287 if (!ToImpl) {
3288 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3289 Importer.Import(D->getLocStart()),
3290 Importer.Import(D->getLocation()),
3291 Property,
3292 D->getPropertyImplementation(),
3293 Ivar,
3294 Importer.Import(D->getPropertyIvarDeclLoc()));
3295 ToImpl->setLexicalDeclContext(LexicalDC);
3296 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003297 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003298 } else {
3299 // Check that we have the same kind of property implementation (@synthesize
3300 // vs. @dynamic).
3301 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3302 Importer.ToDiag(ToImpl->getLocation(),
3303 diag::err_odr_objc_property_impl_kind_inconsistent)
3304 << Property->getDeclName()
3305 << (ToImpl->getPropertyImplementation()
3306 == ObjCPropertyImplDecl::Dynamic);
3307 Importer.FromDiag(D->getLocation(),
3308 diag::note_odr_objc_property_impl_kind)
3309 << D->getPropertyDecl()->getDeclName()
3310 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003311 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003312 }
3313
3314 // For @synthesize, check that we have the same
3315 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3316 Ivar != ToImpl->getPropertyIvarDecl()) {
3317 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3318 diag::err_odr_objc_synthesize_ivar_inconsistent)
3319 << Property->getDeclName()
3320 << ToImpl->getPropertyIvarDecl()->getDeclName()
3321 << Ivar->getDeclName();
3322 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3323 diag::note_odr_objc_synthesize_ivar_here)
3324 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003325 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003326 }
3327
3328 // Merge the existing implementation with the new implementation.
3329 Importer.Imported(D, ToImpl);
3330 }
3331
3332 return ToImpl;
3333}
3334
Douglas Gregora082a492010-11-30 19:14:50 +00003335Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3336 // For template arguments, we adopt the translation unit as our declaration
3337 // context. This context will be fixed when the actual template declaration
3338 // is created.
3339
3340 // FIXME: Import default argument.
3341 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3342 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003343 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003344 Importer.Import(D->getLocation()),
3345 D->getDepth(),
3346 D->getIndex(),
3347 Importer.Import(D->getIdentifier()),
3348 D->wasDeclaredWithTypename(),
3349 D->isParameterPack());
3350}
3351
3352Decl *
3353ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3354 // Import the name of this declaration.
3355 DeclarationName Name = Importer.Import(D->getDeclName());
3356 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003357 return nullptr;
3358
Douglas Gregora082a492010-11-30 19:14:50 +00003359 // Import the location of this declaration.
3360 SourceLocation Loc = Importer.Import(D->getLocation());
3361
3362 // Import the type of this declaration.
3363 QualType T = Importer.Import(D->getType());
3364 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003365 return nullptr;
3366
Douglas Gregora082a492010-11-30 19:14:50 +00003367 // Import type-source information.
3368 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3369 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003370 return nullptr;
3371
Douglas Gregora082a492010-11-30 19:14:50 +00003372 // FIXME: Import default argument.
3373
3374 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3375 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003376 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003377 Loc, D->getDepth(), D->getPosition(),
3378 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003379 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003380}
3381
3382Decl *
3383ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3384 // Import the name of this declaration.
3385 DeclarationName Name = Importer.Import(D->getDeclName());
3386 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003387 return nullptr;
3388
Douglas Gregora082a492010-11-30 19:14:50 +00003389 // Import the location of this declaration.
3390 SourceLocation Loc = Importer.Import(D->getLocation());
3391
3392 // Import template parameters.
3393 TemplateParameterList *TemplateParams
3394 = ImportTemplateParameterList(D->getTemplateParameters());
3395 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003396 return nullptr;
3397
Douglas Gregora082a492010-11-30 19:14:50 +00003398 // FIXME: Import default argument.
3399
3400 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3401 Importer.getToContext().getTranslationUnitDecl(),
3402 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003403 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003404 Name.getAsIdentifierInfo(),
3405 TemplateParams);
3406}
3407
3408Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3409 // If this record has a definition in the translation unit we're coming from,
3410 // but this particular declaration is not that definition, import the
3411 // definition and map to that.
3412 CXXRecordDecl *Definition
3413 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3414 if (Definition && Definition != D->getTemplatedDecl()) {
3415 Decl *ImportedDef
3416 = Importer.Import(Definition->getDescribedClassTemplate());
3417 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003418 return nullptr;
3419
Douglas Gregora082a492010-11-30 19:14:50 +00003420 return Importer.Imported(D, ImportedDef);
3421 }
3422
3423 // Import the major distinguishing characteristics of this class template.
3424 DeclContext *DC, *LexicalDC;
3425 DeclarationName Name;
3426 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003427 NamedDecl *ToD;
3428 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003429 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003430 if (ToD)
3431 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003432
Douglas Gregora082a492010-11-30 19:14:50 +00003433 // We may already have a template of the same name; try to find and match it.
3434 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003435 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003436 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003437 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003438 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3439 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003440 continue;
3441
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003442 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003443 if (ClassTemplateDecl *FoundTemplate
3444 = dyn_cast<ClassTemplateDecl>(Found)) {
3445 if (IsStructuralMatch(D, FoundTemplate)) {
3446 // The class templates structurally match; call it the same template.
3447 // FIXME: We may be filling in a forward declaration here. Handle
3448 // this case!
3449 Importer.Imported(D->getTemplatedDecl(),
3450 FoundTemplate->getTemplatedDecl());
3451 return Importer.Imported(D, FoundTemplate);
3452 }
3453 }
3454
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003455 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003456 }
3457
3458 if (!ConflictingDecls.empty()) {
3459 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3460 ConflictingDecls.data(),
3461 ConflictingDecls.size());
3462 }
3463
3464 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003465 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00003466 }
3467
3468 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3469
3470 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003471 // Create the declaration that is being templated.
3472 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3473 Importer.Import(DTemplated));
3474 if (!D2Templated)
3475 return nullptr;
3476
3477 // Resolve possible cyclic import.
3478 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3479 return AlreadyImported;
3480
Douglas Gregora082a492010-11-30 19:14:50 +00003481 // Create the class template declaration itself.
3482 TemplateParameterList *TemplateParams
3483 = ImportTemplateParameterList(D->getTemplateParameters());
3484 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003485 return nullptr;
3486
Douglas Gregora082a492010-11-30 19:14:50 +00003487 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3488 Loc, Name, TemplateParams,
Vassil Vassilev352e4412017-01-12 09:16:26 +00003489 D2Templated);
Douglas Gregora082a492010-11-30 19:14:50 +00003490 D2Templated->setDescribedClassTemplate(D2);
3491
3492 D2->setAccess(D->getAccess());
3493 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003494 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003495
3496 // Note the relationship between the class templates.
3497 Importer.Imported(D, D2);
3498 Importer.Imported(DTemplated, D2Templated);
3499
John McCallf937c022011-10-07 06:10:15 +00003500 if (DTemplated->isCompleteDefinition() &&
3501 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003502 // FIXME: Import definition!
3503 }
3504
3505 return D2;
3506}
3507
Douglas Gregore2e50d332010-12-01 01:36:18 +00003508Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3509 ClassTemplateSpecializationDecl *D) {
3510 // If this record has a definition in the translation unit we're coming from,
3511 // but this particular declaration is not that definition, import the
3512 // definition and map to that.
3513 TagDecl *Definition = D->getDefinition();
3514 if (Definition && Definition != D) {
3515 Decl *ImportedDef = Importer.Import(Definition);
3516 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003517 return nullptr;
3518
Douglas Gregore2e50d332010-12-01 01:36:18 +00003519 return Importer.Imported(D, ImportedDef);
3520 }
3521
3522 ClassTemplateDecl *ClassTemplate
3523 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3524 D->getSpecializedTemplate()));
3525 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003526 return nullptr;
3527
Douglas Gregore2e50d332010-12-01 01:36:18 +00003528 // Import the context of this declaration.
3529 DeclContext *DC = ClassTemplate->getDeclContext();
3530 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003531 return nullptr;
3532
Douglas Gregore2e50d332010-12-01 01:36:18 +00003533 DeclContext *LexicalDC = DC;
3534 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3535 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3536 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003537 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003538 }
3539
3540 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003541 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3542 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003543
3544 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003545 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003546 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3547 D->getTemplateArgs().size(),
3548 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003549 return nullptr;
3550
Douglas Gregore2e50d332010-12-01 01:36:18 +00003551 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003552 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003553 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00003554 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003555 if (D2) {
3556 // We already have a class template specialization with these template
3557 // arguments.
3558
3559 // FIXME: Check for specialization vs. instantiation errors.
3560
3561 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003562 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003563 // The record types structurally match, or the "from" translation
3564 // unit only had a forward declaration anyway; call it the same
3565 // function.
3566 return Importer.Imported(D, FoundDef);
3567 }
3568 }
3569 } else {
3570 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003571 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3572 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3573
3574 // Import TemplateArgumentListInfo
3575 TemplateArgumentListInfo ToTAInfo;
3576 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3577 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
3578 bool Error = false;
3579 auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error);
3580 if (Error)
3581 return nullptr;
3582 ToTAInfo.addArgument(ToLoc);
3583 }
3584
3585 QualType CanonInjType = Importer.Import(
3586 PartialSpec->getInjectedSpecializationType());
3587 if (CanonInjType.isNull())
3588 return nullptr;
3589 CanonInjType = CanonInjType.getCanonicalType();
3590
3591 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3592 PartialSpec->getTemplateParameters());
3593 if (!ToTPList && PartialSpec->getTemplateParameters())
3594 return nullptr;
3595
3596 D2 = ClassTemplatePartialSpecializationDecl::Create(
3597 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3598 ToTPList, ClassTemplate,
3599 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3600 ToTAInfo, CanonInjType, nullptr);
3601
3602 } else {
3603 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3604 D->getTagKind(), DC,
3605 StartLoc, IdLoc,
3606 ClassTemplate,
3607 TemplateArgs,
3608 /*PrevDecl=*/nullptr);
3609 }
3610
Douglas Gregore2e50d332010-12-01 01:36:18 +00003611 D2->setSpecializationKind(D->getSpecializationKind());
3612
3613 // Add this specialization to the class template.
3614 ClassTemplate->AddSpecialization(D2, InsertPos);
3615
3616 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003617 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003618
3619 Importer.Imported(D, D2);
3620
3621 if (auto *TSI = D->getTypeAsWritten()) {
3622 TypeSourceInfo *TInfo = Importer.Import(TSI);
3623 if (!TInfo)
3624 return nullptr;
3625 D2->setTypeAsWritten(TInfo);
3626 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
3627 D2->setExternLoc(Importer.Import(D->getExternLoc()));
3628 }
3629
3630 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
3631 if (POI.isValid())
3632 D2->setPointOfInstantiation(POI);
3633 else if (D->getPointOfInstantiation().isValid())
3634 return nullptr;
3635
3636 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
3637
Douglas Gregore2e50d332010-12-01 01:36:18 +00003638 // Add the specialization to this context.
3639 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003640 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003641 }
3642 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00003643 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00003644 return nullptr;
3645
Douglas Gregore2e50d332010-12-01 01:36:18 +00003646 return D2;
3647}
3648
Larisse Voufo39a1e502013-08-06 01:03:05 +00003649Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
3650 // If this variable has a definition in the translation unit we're coming
3651 // from,
3652 // but this particular declaration is not that definition, import the
3653 // definition and map to that.
3654 VarDecl *Definition =
3655 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
3656 if (Definition && Definition != D->getTemplatedDecl()) {
3657 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
3658 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003659 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003660
3661 return Importer.Imported(D, ImportedDef);
3662 }
3663
3664 // Import the major distinguishing characteristics of this variable template.
3665 DeclContext *DC, *LexicalDC;
3666 DeclarationName Name;
3667 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003668 NamedDecl *ToD;
3669 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003670 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003671 if (ToD)
3672 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003673
3674 // We may already have a template of the same name; try to find and match it.
3675 assert(!DC->isFunctionOrMethod() &&
3676 "Variable templates cannot be declared at function scope");
3677 SmallVector<NamedDecl *, 4> ConflictingDecls;
3678 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003679 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003680 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3681 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3682 continue;
3683
3684 Decl *Found = FoundDecls[I];
3685 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
3686 if (IsStructuralMatch(D, FoundTemplate)) {
3687 // The variable templates structurally match; call it the same template.
3688 Importer.Imported(D->getTemplatedDecl(),
3689 FoundTemplate->getTemplatedDecl());
3690 return Importer.Imported(D, FoundTemplate);
3691 }
3692 }
3693
3694 ConflictingDecls.push_back(FoundDecls[I]);
3695 }
3696
3697 if (!ConflictingDecls.empty()) {
3698 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3699 ConflictingDecls.data(),
3700 ConflictingDecls.size());
3701 }
3702
3703 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003704 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003705
3706 VarDecl *DTemplated = D->getTemplatedDecl();
3707
3708 // Import the type.
3709 QualType T = Importer.Import(DTemplated->getType());
3710 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003711 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003712
3713 // Create the declaration that is being templated.
3714 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3715 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3716 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
3717 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
3718 IdLoc, Name.getAsIdentifierInfo(), T,
3719 TInfo, DTemplated->getStorageClass());
3720 D2Templated->setAccess(DTemplated->getAccess());
3721 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3722 D2Templated->setLexicalDeclContext(LexicalDC);
3723
3724 // Importer.Imported(DTemplated, D2Templated);
3725 // LexicalDC->addDeclInternal(D2Templated);
3726
3727 // Merge the initializer.
3728 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00003729 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003730
3731 // Create the variable template declaration itself.
3732 TemplateParameterList *TemplateParams =
3733 ImportTemplateParameterList(D->getTemplateParameters());
3734 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003735 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003736
3737 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00003738 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003739 D2Templated->setDescribedVarTemplate(D2);
3740
3741 D2->setAccess(D->getAccess());
3742 D2->setLexicalDeclContext(LexicalDC);
3743 LexicalDC->addDeclInternal(D2);
3744
3745 // Note the relationship between the variable templates.
3746 Importer.Imported(D, D2);
3747 Importer.Imported(DTemplated, D2Templated);
3748
3749 if (DTemplated->isThisDeclarationADefinition() &&
3750 !D2Templated->isThisDeclarationADefinition()) {
3751 // FIXME: Import definition!
3752 }
3753
3754 return D2;
3755}
3756
3757Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
3758 VarTemplateSpecializationDecl *D) {
3759 // If this record has a definition in the translation unit we're coming from,
3760 // but this particular declaration is not that definition, import the
3761 // definition and map to that.
3762 VarDecl *Definition = D->getDefinition();
3763 if (Definition && Definition != D) {
3764 Decl *ImportedDef = Importer.Import(Definition);
3765 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003766 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003767
3768 return Importer.Imported(D, ImportedDef);
3769 }
3770
3771 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
3772 Importer.Import(D->getSpecializedTemplate()));
3773 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003774 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003775
3776 // Import the context of this declaration.
3777 DeclContext *DC = VarTemplate->getDeclContext();
3778 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003779 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003780
3781 DeclContext *LexicalDC = DC;
3782 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3783 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3784 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003785 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003786 }
3787
3788 // Import the location of this declaration.
3789 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3790 SourceLocation IdLoc = Importer.Import(D->getLocation());
3791
3792 // Import template arguments.
3793 SmallVector<TemplateArgument, 2> TemplateArgs;
3794 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3795 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003796 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003797
3798 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003799 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003800 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00003801 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003802 if (D2) {
3803 // We already have a variable template specialization with these template
3804 // arguments.
3805
3806 // FIXME: Check for specialization vs. instantiation errors.
3807
3808 if (VarDecl *FoundDef = D2->getDefinition()) {
3809 if (!D->isThisDeclarationADefinition() ||
3810 IsStructuralMatch(D, FoundDef)) {
3811 // The record types structurally match, or the "from" translation
3812 // unit only had a forward declaration anyway; call it the same
3813 // variable.
3814 return Importer.Imported(D, FoundDef);
3815 }
3816 }
3817 } else {
3818
3819 // Import the type.
3820 QualType T = Importer.Import(D->getType());
3821 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003822 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003823 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3824
3825 // Create a new specialization.
3826 D2 = VarTemplateSpecializationDecl::Create(
3827 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00003828 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003829 D2->setSpecializationKind(D->getSpecializationKind());
3830 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
3831
3832 // Add this specialization to the class template.
3833 VarTemplate->AddSpecialization(D2, InsertPos);
3834
3835 // Import the qualifier, if any.
3836 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3837
3838 // Add the specialization to this context.
3839 D2->setLexicalDeclContext(LexicalDC);
3840 LexicalDC->addDeclInternal(D2);
3841 }
3842 Importer.Imported(D, D2);
3843
3844 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00003845 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003846
3847 return D2;
3848}
3849
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003850//----------------------------------------------------------------------------
3851// Import Statements
3852//----------------------------------------------------------------------------
3853
Sean Callanan59721b32015-04-28 18:41:46 +00003854DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
3855 if (DG.isNull())
3856 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
3857 size_t NumDecls = DG.end() - DG.begin();
3858 SmallVector<Decl *, 1> ToDecls(NumDecls);
3859 auto &_Importer = this->Importer;
3860 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
3861 [&_Importer](Decl *D) -> Decl * {
3862 return _Importer.Import(D);
3863 });
3864 return DeclGroupRef::Create(Importer.getToContext(),
3865 ToDecls.begin(),
3866 NumDecls);
3867}
3868
3869 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3870 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3871 << S->getStmtClassName();
3872 return nullptr;
3873 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003874
3875
3876Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
3877 SmallVector<IdentifierInfo *, 4> Names;
3878 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3879 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00003880 // ToII is nullptr when no symbolic name is given for output operand
3881 // see ParseStmtAsm::ParseAsmOperandsOpt
3882 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003883 return nullptr;
3884 Names.push_back(ToII);
3885 }
3886 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3887 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00003888 // ToII is nullptr when no symbolic name is given for input operand
3889 // see ParseStmtAsm::ParseAsmOperandsOpt
3890 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003891 return nullptr;
3892 Names.push_back(ToII);
3893 }
3894
3895 SmallVector<StringLiteral *, 4> Clobbers;
3896 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
3897 StringLiteral *Clobber = cast_or_null<StringLiteral>(
3898 Importer.Import(S->getClobberStringLiteral(I)));
3899 if (!Clobber)
3900 return nullptr;
3901 Clobbers.push_back(Clobber);
3902 }
3903
3904 SmallVector<StringLiteral *, 4> Constraints;
3905 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3906 StringLiteral *Output = cast_or_null<StringLiteral>(
3907 Importer.Import(S->getOutputConstraintLiteral(I)));
3908 if (!Output)
3909 return nullptr;
3910 Constraints.push_back(Output);
3911 }
3912
3913 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3914 StringLiteral *Input = cast_or_null<StringLiteral>(
3915 Importer.Import(S->getInputConstraintLiteral(I)));
3916 if (!Input)
3917 return nullptr;
3918 Constraints.push_back(Input);
3919 }
3920
3921 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00003922 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003923 return nullptr;
3924
Aleksei Sidorina693b372016-09-28 10:16:56 +00003925 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003926 return nullptr;
3927
3928 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
3929 Importer.Import(S->getAsmString()));
3930 if (!AsmStr)
3931 return nullptr;
3932
3933 return new (Importer.getToContext()) GCCAsmStmt(
3934 Importer.getToContext(),
3935 Importer.Import(S->getAsmLoc()),
3936 S->isSimple(),
3937 S->isVolatile(),
3938 S->getNumOutputs(),
3939 S->getNumInputs(),
3940 Names.data(),
3941 Constraints.data(),
3942 Exprs.data(),
3943 AsmStr,
3944 S->getNumClobbers(),
3945 Clobbers.data(),
3946 Importer.Import(S->getRParenLoc()));
3947}
3948
Sean Callanan59721b32015-04-28 18:41:46 +00003949Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
3950 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
3951 for (Decl *ToD : ToDG) {
3952 if (!ToD)
3953 return nullptr;
3954 }
3955 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
3956 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
3957 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
3958}
3959
3960Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
3961 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
3962 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
3963 S->hasLeadingEmptyMacro());
3964}
3965
3966Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003967 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00003968
3969 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00003970 return nullptr;
3971
Sean Callanan59721b32015-04-28 18:41:46 +00003972 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
3973 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
3974 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
3975 ToStmts,
3976 ToLBraceLoc, ToRBraceLoc);
3977}
3978
3979Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
3980 Expr *ToLHS = Importer.Import(S->getLHS());
3981 if (!ToLHS)
3982 return nullptr;
3983 Expr *ToRHS = Importer.Import(S->getRHS());
3984 if (!ToRHS && S->getRHS())
3985 return nullptr;
Gabor Horvath480892b2017-10-18 09:25:18 +00003986 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3987 if (!ToSubStmt && S->getSubStmt())
3988 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003989 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
3990 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
3991 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
Gabor Horvath480892b2017-10-18 09:25:18 +00003992 CaseStmt *ToStmt = new (Importer.getToContext())
3993 CaseStmt(ToLHS, ToRHS, ToCaseLoc, ToEllipsisLoc, ToColonLoc);
3994 ToStmt->setSubStmt(ToSubStmt);
3995 return ToStmt;
Sean Callanan59721b32015-04-28 18:41:46 +00003996}
3997
3998Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
3999 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4000 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4001 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4002 if (!ToSubStmt && S->getSubStmt())
4003 return nullptr;
4004 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4005 ToSubStmt);
4006}
4007
4008Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4009 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4010 LabelDecl *ToLabelDecl =
4011 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4012 if (!ToLabelDecl && S->getDecl())
4013 return nullptr;
4014 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4015 if (!ToSubStmt && S->getSubStmt())
4016 return nullptr;
4017 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4018 ToSubStmt);
4019}
4020
4021Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4022 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4023 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4024 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4025 ASTContext &_ToContext = Importer.getToContext();
4026 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4027 [&_ToContext](const Attr *A) -> const Attr * {
4028 return A->clone(_ToContext);
4029 });
4030 for (const Attr *ToA : ToAttrs) {
4031 if (!ToA)
4032 return nullptr;
4033 }
4034 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4035 if (!ToSubStmt && S->getSubStmt())
4036 return nullptr;
4037 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4038 ToAttrs, ToSubStmt);
4039}
4040
4041Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4042 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004043 Stmt *ToInit = Importer.Import(S->getInit());
4044 if (!ToInit && S->getInit())
4045 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004046 VarDecl *ToConditionVariable = nullptr;
4047 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4048 ToConditionVariable =
4049 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4050 if (!ToConditionVariable)
4051 return nullptr;
4052 }
4053 Expr *ToCondition = Importer.Import(S->getCond());
4054 if (!ToCondition && S->getCond())
4055 return nullptr;
4056 Stmt *ToThenStmt = Importer.Import(S->getThen());
4057 if (!ToThenStmt && S->getThen())
4058 return nullptr;
4059 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4060 Stmt *ToElseStmt = Importer.Import(S->getElse());
4061 if (!ToElseStmt && S->getElse())
4062 return nullptr;
4063 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004064 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004065 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004066 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004067 ToCondition, ToThenStmt,
4068 ToElseLoc, ToElseStmt);
4069}
4070
4071Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
Richard Smitha547eb22016-07-14 00:11:03 +00004072 Stmt *ToInit = Importer.Import(S->getInit());
4073 if (!ToInit && S->getInit())
4074 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004075 VarDecl *ToConditionVariable = nullptr;
4076 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4077 ToConditionVariable =
4078 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4079 if (!ToConditionVariable)
4080 return nullptr;
4081 }
4082 Expr *ToCondition = Importer.Import(S->getCond());
4083 if (!ToCondition && S->getCond())
4084 return nullptr;
4085 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004086 Importer.getToContext(), ToInit,
4087 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004088 Stmt *ToBody = Importer.Import(S->getBody());
4089 if (!ToBody && S->getBody())
4090 return nullptr;
4091 ToStmt->setBody(ToBody);
4092 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4093 // Now we have to re-chain the cases.
4094 SwitchCase *LastChainedSwitchCase = nullptr;
4095 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4096 SC = SC->getNextSwitchCase()) {
4097 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4098 if (!ToSC)
4099 return nullptr;
4100 if (LastChainedSwitchCase)
4101 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4102 else
4103 ToStmt->setSwitchCaseList(ToSC);
4104 LastChainedSwitchCase = ToSC;
4105 }
4106 return ToStmt;
4107}
4108
4109Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4110 VarDecl *ToConditionVariable = nullptr;
4111 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4112 ToConditionVariable =
4113 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4114 if (!ToConditionVariable)
4115 return nullptr;
4116 }
4117 Expr *ToCondition = Importer.Import(S->getCond());
4118 if (!ToCondition && S->getCond())
4119 return nullptr;
4120 Stmt *ToBody = Importer.Import(S->getBody());
4121 if (!ToBody && S->getBody())
4122 return nullptr;
4123 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4124 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4125 ToConditionVariable,
4126 ToCondition, ToBody,
4127 ToWhileLoc);
4128}
4129
4130Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4131 Stmt *ToBody = Importer.Import(S->getBody());
4132 if (!ToBody && S->getBody())
4133 return nullptr;
4134 Expr *ToCondition = Importer.Import(S->getCond());
4135 if (!ToCondition && S->getCond())
4136 return nullptr;
4137 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4138 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4139 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4140 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4141 ToDoLoc, ToWhileLoc,
4142 ToRParenLoc);
4143}
4144
4145Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4146 Stmt *ToInit = Importer.Import(S->getInit());
4147 if (!ToInit && S->getInit())
4148 return nullptr;
4149 Expr *ToCondition = Importer.Import(S->getCond());
4150 if (!ToCondition && S->getCond())
4151 return nullptr;
4152 VarDecl *ToConditionVariable = nullptr;
4153 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4154 ToConditionVariable =
4155 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4156 if (!ToConditionVariable)
4157 return nullptr;
4158 }
4159 Expr *ToInc = Importer.Import(S->getInc());
4160 if (!ToInc && S->getInc())
4161 return nullptr;
4162 Stmt *ToBody = Importer.Import(S->getBody());
4163 if (!ToBody && S->getBody())
4164 return nullptr;
4165 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4166 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4167 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4168 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4169 ToInit, ToCondition,
4170 ToConditionVariable,
4171 ToInc, ToBody,
4172 ToForLoc, ToLParenLoc,
4173 ToRParenLoc);
4174}
4175
4176Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4177 LabelDecl *ToLabel = nullptr;
4178 if (LabelDecl *FromLabel = S->getLabel()) {
4179 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4180 if (!ToLabel)
4181 return nullptr;
4182 }
4183 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4184 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4185 return new (Importer.getToContext()) GotoStmt(ToLabel,
4186 ToGotoLoc, ToLabelLoc);
4187}
4188
4189Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4190 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4191 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4192 Expr *ToTarget = Importer.Import(S->getTarget());
4193 if (!ToTarget && S->getTarget())
4194 return nullptr;
4195 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4196 ToTarget);
4197}
4198
4199Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4200 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4201 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4202}
4203
4204Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4205 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4206 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4207}
4208
4209Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4210 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4211 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4212 if (!ToRetExpr && S->getRetValue())
4213 return nullptr;
4214 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4215 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4216 if (!ToNRVOCandidate && NRVOCandidate)
4217 return nullptr;
4218 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4219 ToNRVOCandidate);
4220}
4221
4222Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4223 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4224 VarDecl *ToExceptionDecl = nullptr;
4225 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4226 ToExceptionDecl =
4227 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4228 if (!ToExceptionDecl)
4229 return nullptr;
4230 }
4231 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4232 if (!ToHandlerBlock && S->getHandlerBlock())
4233 return nullptr;
4234 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4235 ToExceptionDecl,
4236 ToHandlerBlock);
4237}
4238
4239Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4240 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4241 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4242 if (!ToTryBlock && S->getTryBlock())
4243 return nullptr;
4244 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4245 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4246 CXXCatchStmt *FromHandler = S->getHandler(HI);
4247 if (Stmt *ToHandler = Importer.Import(FromHandler))
4248 ToHandlers[HI] = ToHandler;
4249 else
4250 return nullptr;
4251 }
4252 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4253 ToHandlers);
4254}
4255
4256Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4257 DeclStmt *ToRange =
4258 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4259 if (!ToRange && S->getRangeStmt())
4260 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004261 DeclStmt *ToBegin =
4262 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4263 if (!ToBegin && S->getBeginStmt())
4264 return nullptr;
4265 DeclStmt *ToEnd =
4266 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4267 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004268 return nullptr;
4269 Expr *ToCond = Importer.Import(S->getCond());
4270 if (!ToCond && S->getCond())
4271 return nullptr;
4272 Expr *ToInc = Importer.Import(S->getInc());
4273 if (!ToInc && S->getInc())
4274 return nullptr;
4275 DeclStmt *ToLoopVar =
4276 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4277 if (!ToLoopVar && S->getLoopVarStmt())
4278 return nullptr;
4279 Stmt *ToBody = Importer.Import(S->getBody());
4280 if (!ToBody && S->getBody())
4281 return nullptr;
4282 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004283 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004284 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4285 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004286 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004287 ToCond, ToInc,
4288 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004289 ToForLoc, ToCoawaitLoc,
4290 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004291}
4292
4293Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4294 Stmt *ToElem = Importer.Import(S->getElement());
4295 if (!ToElem && S->getElement())
4296 return nullptr;
4297 Expr *ToCollect = Importer.Import(S->getCollection());
4298 if (!ToCollect && S->getCollection())
4299 return nullptr;
4300 Stmt *ToBody = Importer.Import(S->getBody());
4301 if (!ToBody && S->getBody())
4302 return nullptr;
4303 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4304 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4305 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4306 ToCollect,
4307 ToBody, ToForLoc,
4308 ToRParenLoc);
4309}
4310
4311Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4312 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4313 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4314 VarDecl *ToExceptionDecl = nullptr;
4315 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4316 ToExceptionDecl =
4317 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4318 if (!ToExceptionDecl)
4319 return nullptr;
4320 }
4321 Stmt *ToBody = Importer.Import(S->getCatchBody());
4322 if (!ToBody && S->getCatchBody())
4323 return nullptr;
4324 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4325 ToRParenLoc,
4326 ToExceptionDecl,
4327 ToBody);
4328}
4329
4330Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4331 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4332 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4333 if (!ToAtFinallyStmt && S->getFinallyBody())
4334 return nullptr;
4335 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4336 ToAtFinallyStmt);
4337}
4338
4339Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4340 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4341 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4342 if (!ToAtTryStmt && S->getTryBody())
4343 return nullptr;
4344 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4345 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4346 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4347 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4348 ToCatchStmts[CI] = ToCatchStmt;
4349 else
4350 return nullptr;
4351 }
4352 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4353 if (!ToAtFinallyStmt && S->getFinallyStmt())
4354 return nullptr;
4355 return ObjCAtTryStmt::Create(Importer.getToContext(),
4356 ToAtTryLoc, ToAtTryStmt,
4357 ToCatchStmts.begin(), ToCatchStmts.size(),
4358 ToAtFinallyStmt);
4359}
4360
4361Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4362 (ObjCAtSynchronizedStmt *S) {
4363 SourceLocation ToAtSynchronizedLoc =
4364 Importer.Import(S->getAtSynchronizedLoc());
4365 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4366 if (!ToSynchExpr && S->getSynchExpr())
4367 return nullptr;
4368 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4369 if (!ToSynchBody && S->getSynchBody())
4370 return nullptr;
4371 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4372 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4373}
4374
4375Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4376 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4377 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4378 if (!ToThrow && S->getThrowExpr())
4379 return nullptr;
4380 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4381}
4382
4383Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4384 (ObjCAutoreleasePoolStmt *S) {
4385 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4386 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4387 if (!ToSubStmt && S->getSubStmt())
4388 return nullptr;
4389 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4390 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004391}
4392
4393//----------------------------------------------------------------------------
4394// Import Expressions
4395//----------------------------------------------------------------------------
4396Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4397 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4398 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004399 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004400}
4401
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004402Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4403 QualType T = Importer.Import(E->getType());
4404 if (T.isNull())
4405 return nullptr;
4406
4407 Expr *SubExpr = Importer.Import(E->getSubExpr());
4408 if (!SubExpr && E->getSubExpr())
4409 return nullptr;
4410
4411 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4412 if (!TInfo)
4413 return nullptr;
4414
4415 return new (Importer.getToContext()) VAArgExpr(
4416 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4417 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4418}
4419
4420
4421Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4422 QualType T = Importer.Import(E->getType());
4423 if (T.isNull())
4424 return nullptr;
4425
4426 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004427 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004428}
4429
4430Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4431 QualType T = Importer.Import(E->getType());
4432 if (T.isNull())
4433 return nullptr;
4434
4435 StringLiteral *SL = cast_or_null<StringLiteral>(
4436 Importer.Import(E->getFunctionName()));
4437 if (!SL && E->getFunctionName())
4438 return nullptr;
4439
4440 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004441 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004442}
4443
Douglas Gregor52f820e2010-02-19 01:17:02 +00004444Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004445 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4446 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004447 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004448
Craig Topper36250ad2014-05-12 05:36:57 +00004449 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004450 if (E->getDecl() != E->getFoundDecl()) {
4451 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4452 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004453 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004454 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004455
4456 QualType T = Importer.Import(E->getType());
4457 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004458 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004459
Aleksei Sidorina693b372016-09-28 10:16:56 +00004460
4461 TemplateArgumentListInfo ToTAInfo;
4462 TemplateArgumentListInfo *ResInfo = nullptr;
4463 if (E->hasExplicitTemplateArgs()) {
4464 for (const auto &FromLoc : E->template_arguments()) {
4465 bool Error = false;
4466 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
4467 if (Error)
4468 return nullptr;
4469 ToTAInfo.addArgument(ToTALoc);
4470 }
4471 ResInfo = &ToTAInfo;
4472 }
4473
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004474 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4475 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004476 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004477 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004478 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004479 Importer.Import(E->getLocation()),
4480 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00004481 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004482 if (E->hadMultipleCandidates())
4483 DRE->setHadMultipleCandidates(true);
4484 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004485}
4486
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004487Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4488 QualType T = Importer.Import(E->getType());
4489 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00004490 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004491
4492 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4493}
4494
4495ASTNodeImporter::Designator
4496ASTNodeImporter::ImportDesignator(const Designator &D) {
4497 if (D.isFieldDesignator()) {
4498 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4499 // Caller checks for import error
4500 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4501 Importer.Import(D.getFieldLoc()));
4502 }
4503 if (D.isArrayDesignator())
4504 return Designator(D.getFirstExprIndex(),
4505 Importer.Import(D.getLBracketLoc()),
4506 Importer.Import(D.getRBracketLoc()));
4507
4508 assert(D.isArrayRangeDesignator());
4509 return Designator(D.getFirstExprIndex(),
4510 Importer.Import(D.getLBracketLoc()),
4511 Importer.Import(D.getEllipsisLoc()),
4512 Importer.Import(D.getRBracketLoc()));
4513}
4514
4515
4516Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4517 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4518 if (!Init)
4519 return nullptr;
4520
4521 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4522 // List elements from the second, the first is Init itself
4523 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4524 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4525 IndexExprs[I - 1] = Arg;
4526 else
4527 return nullptr;
4528 }
4529
4530 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00004531 llvm::transform(DIE->designators(), Designators.begin(),
4532 [this](const Designator &D) -> Designator {
4533 return ImportDesignator(D);
4534 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004535
David Majnemerf7e36092016-06-23 00:15:04 +00004536 for (const Designator &D : DIE->designators())
4537 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004538 return nullptr;
4539
4540 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00004541 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004542 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4543 DIE->usesGNUSyntax(), Init);
4544}
4545
4546Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4547 QualType T = Importer.Import(E->getType());
4548 if (T.isNull())
4549 return nullptr;
4550
4551 return new (Importer.getToContext())
4552 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4553}
4554
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004555Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4556 QualType T = Importer.Import(E->getType());
4557 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004558 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004559
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004560 return IntegerLiteral::Create(Importer.getToContext(),
4561 E->getValue(), T,
4562 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004563}
4564
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004565Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4566 QualType T = Importer.Import(E->getType());
4567 if (T.isNull())
4568 return nullptr;
4569
4570 return FloatingLiteral::Create(Importer.getToContext(),
4571 E->getValue(), E->isExact(), T,
4572 Importer.Import(E->getLocation()));
4573}
4574
Douglas Gregor623421d2010-02-18 02:21:22 +00004575Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4576 QualType T = Importer.Import(E->getType());
4577 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004578 return nullptr;
4579
Douglas Gregorfb65e592011-07-27 05:40:30 +00004580 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4581 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004582 Importer.Import(E->getLocation()));
4583}
4584
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004585Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
4586 QualType T = Importer.Import(E->getType());
4587 if (T.isNull())
4588 return nullptr;
4589
4590 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
4591 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
4592
4593 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
4594 E->getKind(), E->isPascal(), T,
4595 Locations.data(), Locations.size());
4596}
4597
4598Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
4599 QualType T = Importer.Import(E->getType());
4600 if (T.isNull())
4601 return nullptr;
4602
4603 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
4604 if (!TInfo)
4605 return nullptr;
4606
4607 Expr *Init = Importer.Import(E->getInitializer());
4608 if (!Init)
4609 return nullptr;
4610
4611 return new (Importer.getToContext()) CompoundLiteralExpr(
4612 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
4613 Init, E->isFileScope());
4614}
4615
4616Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
4617 QualType T = Importer.Import(E->getType());
4618 if (T.isNull())
4619 return nullptr;
4620
4621 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
4622 if (ImportArrayChecked(
4623 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
4624 Exprs.begin()))
4625 return nullptr;
4626
4627 return new (Importer.getToContext()) AtomicExpr(
4628 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
4629 Importer.Import(E->getRParenLoc()));
4630}
4631
4632Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
4633 QualType T = Importer.Import(E->getType());
4634 if (T.isNull())
4635 return nullptr;
4636
4637 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
4638 if (!ToLabel)
4639 return nullptr;
4640
4641 return new (Importer.getToContext()) AddrLabelExpr(
4642 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
4643 ToLabel, T);
4644}
4645
Douglas Gregorc74247e2010-02-19 01:07:06 +00004646Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4647 Expr *SubExpr = Importer.Import(E->getSubExpr());
4648 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004649 return nullptr;
4650
Douglas Gregorc74247e2010-02-19 01:07:06 +00004651 return new (Importer.getToContext())
4652 ParenExpr(Importer.Import(E->getLParen()),
4653 Importer.Import(E->getRParen()),
4654 SubExpr);
4655}
4656
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004657Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
4658 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004659 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004660 return nullptr;
4661
4662 return new (Importer.getToContext()) ParenListExpr(
4663 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
4664 Exprs, Importer.Import(E->getLParenLoc()));
4665}
4666
4667Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
4668 QualType T = Importer.Import(E->getType());
4669 if (T.isNull())
4670 return nullptr;
4671
4672 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
4673 Importer.Import(E->getSubStmt()));
4674 if (!ToSubStmt && E->getSubStmt())
4675 return nullptr;
4676
4677 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
4678 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
4679}
4680
Douglas Gregorc74247e2010-02-19 01:07:06 +00004681Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4682 QualType T = Importer.Import(E->getType());
4683 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004684 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004685
4686 Expr *SubExpr = Importer.Import(E->getSubExpr());
4687 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004688 return nullptr;
4689
Douglas Gregorc74247e2010-02-19 01:07:06 +00004690 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004691 T, E->getValueKind(),
4692 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004693 Importer.Import(E->getOperatorLoc()));
4694}
4695
Peter Collingbournee190dee2011-03-11 19:24:49 +00004696Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4697 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004698 QualType ResultType = Importer.Import(E->getType());
4699
4700 if (E->isArgumentType()) {
4701 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4702 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004703 return nullptr;
4704
Peter Collingbournee190dee2011-03-11 19:24:49 +00004705 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4706 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004707 Importer.Import(E->getOperatorLoc()),
4708 Importer.Import(E->getRParenLoc()));
4709 }
4710
4711 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4712 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004713 return nullptr;
4714
Peter Collingbournee190dee2011-03-11 19:24:49 +00004715 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4716 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004717 Importer.Import(E->getOperatorLoc()),
4718 Importer.Import(E->getRParenLoc()));
4719}
4720
Douglas Gregorc74247e2010-02-19 01:07:06 +00004721Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4722 QualType T = Importer.Import(E->getType());
4723 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004724 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004725
4726 Expr *LHS = Importer.Import(E->getLHS());
4727 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004728 return nullptr;
4729
Douglas Gregorc74247e2010-02-19 01:07:06 +00004730 Expr *RHS = Importer.Import(E->getRHS());
4731 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004732 return nullptr;
4733
Douglas Gregorc74247e2010-02-19 01:07:06 +00004734 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004735 T, E->getValueKind(),
4736 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004737 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00004738 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004739}
4740
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004741Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
4742 QualType T = Importer.Import(E->getType());
4743 if (T.isNull())
4744 return nullptr;
4745
4746 Expr *ToLHS = Importer.Import(E->getLHS());
4747 if (!ToLHS)
4748 return nullptr;
4749
4750 Expr *ToRHS = Importer.Import(E->getRHS());
4751 if (!ToRHS)
4752 return nullptr;
4753
4754 Expr *ToCond = Importer.Import(E->getCond());
4755 if (!ToCond)
4756 return nullptr;
4757
4758 return new (Importer.getToContext()) ConditionalOperator(
4759 ToCond, Importer.Import(E->getQuestionLoc()),
4760 ToLHS, Importer.Import(E->getColonLoc()),
4761 ToRHS, T, E->getValueKind(), E->getObjectKind());
4762}
4763
4764Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
4765 BinaryConditionalOperator *E) {
4766 QualType T = Importer.Import(E->getType());
4767 if (T.isNull())
4768 return nullptr;
4769
4770 Expr *Common = Importer.Import(E->getCommon());
4771 if (!Common)
4772 return nullptr;
4773
4774 Expr *Cond = Importer.Import(E->getCond());
4775 if (!Cond)
4776 return nullptr;
4777
4778 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
4779 Importer.Import(E->getOpaqueValue()));
4780 if (!OpaqueValue)
4781 return nullptr;
4782
4783 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
4784 if (!TrueExpr)
4785 return nullptr;
4786
4787 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
4788 if (!FalseExpr)
4789 return nullptr;
4790
4791 return new (Importer.getToContext()) BinaryConditionalOperator(
4792 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
4793 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
4794 T, E->getValueKind(), E->getObjectKind());
4795}
4796
Aleksei Sidorina693b372016-09-28 10:16:56 +00004797Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
4798 QualType T = Importer.Import(E->getType());
4799 if (T.isNull())
4800 return nullptr;
4801
4802 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
4803 if (!ToQueried)
4804 return nullptr;
4805
4806 Expr *Dim = Importer.Import(E->getDimensionExpression());
4807 if (!Dim && E->getDimensionExpression())
4808 return nullptr;
4809
4810 return new (Importer.getToContext()) ArrayTypeTraitExpr(
4811 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4812 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
4813}
4814
4815Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
4816 QualType T = Importer.Import(E->getType());
4817 if (T.isNull())
4818 return nullptr;
4819
4820 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
4821 if (!ToQueried)
4822 return nullptr;
4823
4824 return new (Importer.getToContext()) ExpressionTraitExpr(
4825 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4826 E->getValue(), Importer.Import(E->getLocEnd()), T);
4827}
4828
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004829Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
4830 QualType T = Importer.Import(E->getType());
4831 if (T.isNull())
4832 return nullptr;
4833
4834 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
4835 if (!SourceExpr && E->getSourceExpr())
4836 return nullptr;
4837
4838 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004839 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004840 E->getObjectKind(), SourceExpr);
4841}
4842
Aleksei Sidorina693b372016-09-28 10:16:56 +00004843Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
4844 QualType T = Importer.Import(E->getType());
4845 if (T.isNull())
4846 return nullptr;
4847
4848 Expr *ToLHS = Importer.Import(E->getLHS());
4849 if (!ToLHS)
4850 return nullptr;
4851
4852 Expr *ToRHS = Importer.Import(E->getRHS());
4853 if (!ToRHS)
4854 return nullptr;
4855
4856 return new (Importer.getToContext()) ArraySubscriptExpr(
4857 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
4858 Importer.Import(E->getRBracketLoc()));
4859}
4860
Douglas Gregorc74247e2010-02-19 01:07:06 +00004861Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4862 QualType T = Importer.Import(E->getType());
4863 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004864 return nullptr;
4865
Douglas Gregorc74247e2010-02-19 01:07:06 +00004866 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4867 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004868 return nullptr;
4869
Douglas Gregorc74247e2010-02-19 01:07:06 +00004870 QualType CompResultType = Importer.Import(E->getComputationResultType());
4871 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004872 return nullptr;
4873
Douglas Gregorc74247e2010-02-19 01:07:06 +00004874 Expr *LHS = Importer.Import(E->getLHS());
4875 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004876 return nullptr;
4877
Douglas Gregorc74247e2010-02-19 01:07:06 +00004878 Expr *RHS = Importer.Import(E->getRHS());
4879 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004880 return nullptr;
4881
Douglas Gregorc74247e2010-02-19 01:07:06 +00004882 return new (Importer.getToContext())
4883 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004884 T, E->getValueKind(),
4885 E->getObjectKind(),
4886 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004887 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00004888 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004889}
4890
Aleksei Sidorina693b372016-09-28 10:16:56 +00004891bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
4892 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
4893 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
4894 Path.push_back(Spec);
4895 else
4896 return true;
4897 }
4898 return false;
John McCallcf142162010-08-07 06:22:56 +00004899}
4900
Douglas Gregor98c10182010-02-12 22:17:39 +00004901Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4902 QualType T = Importer.Import(E->getType());
4903 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004904 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00004905
4906 Expr *SubExpr = Importer.Import(E->getSubExpr());
4907 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004908 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004909
4910 CXXCastPath BasePath;
4911 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004912 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004913
4914 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004915 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004916}
4917
Aleksei Sidorina693b372016-09-28 10:16:56 +00004918Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00004919 QualType T = Importer.Import(E->getType());
4920 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004921 return nullptr;
4922
Douglas Gregor5481d322010-02-19 01:32:14 +00004923 Expr *SubExpr = Importer.Import(E->getSubExpr());
4924 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004925 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00004926
4927 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4928 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00004929 return nullptr;
4930
John McCallcf142162010-08-07 06:22:56 +00004931 CXXCastPath BasePath;
4932 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004933 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004934
Aleksei Sidorina693b372016-09-28 10:16:56 +00004935 switch (E->getStmtClass()) {
4936 case Stmt::CStyleCastExprClass: {
4937 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
4938 return CStyleCastExpr::Create(Importer.getToContext(), T,
4939 E->getValueKind(), E->getCastKind(),
4940 SubExpr, &BasePath, TInfo,
4941 Importer.Import(CCE->getLParenLoc()),
4942 Importer.Import(CCE->getRParenLoc()));
4943 }
4944
4945 case Stmt::CXXFunctionalCastExprClass: {
4946 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
4947 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
4948 E->getValueKind(), TInfo,
4949 E->getCastKind(), SubExpr, &BasePath,
4950 Importer.Import(FCE->getLParenLoc()),
4951 Importer.Import(FCE->getRParenLoc()));
4952 }
4953
4954 case Stmt::ObjCBridgedCastExprClass: {
4955 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
4956 return new (Importer.getToContext()) ObjCBridgedCastExpr(
4957 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
4958 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
4959 TInfo, SubExpr);
4960 }
4961 default:
4962 break; // just fall through
4963 }
4964
4965 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
4966 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
4967 RParenLoc = Importer.Import(Named->getRParenLoc());
4968 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
4969
4970 switch (E->getStmtClass()) {
4971 case Stmt::CXXStaticCastExprClass:
4972 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
4973 E->getValueKind(), E->getCastKind(),
4974 SubExpr, &BasePath, TInfo,
4975 ExprLoc, RParenLoc, Brackets);
4976
4977 case Stmt::CXXDynamicCastExprClass:
4978 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
4979 E->getValueKind(), E->getCastKind(),
4980 SubExpr, &BasePath, TInfo,
4981 ExprLoc, RParenLoc, Brackets);
4982
4983 case Stmt::CXXReinterpretCastExprClass:
4984 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
4985 E->getValueKind(), E->getCastKind(),
4986 SubExpr, &BasePath, TInfo,
4987 ExprLoc, RParenLoc, Brackets);
4988
4989 case Stmt::CXXConstCastExprClass:
4990 return CXXConstCastExpr::Create(Importer.getToContext(), T,
4991 E->getValueKind(), SubExpr, TInfo, ExprLoc,
4992 RParenLoc, Brackets);
4993 default:
4994 llvm_unreachable("Cast expression of unsupported type!");
4995 return nullptr;
4996 }
4997}
4998
4999Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
5000 QualType T = Importer.Import(OE->getType());
5001 if (T.isNull())
5002 return nullptr;
5003
5004 SmallVector<OffsetOfNode, 4> Nodes;
5005 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
5006 const OffsetOfNode &Node = OE->getComponent(I);
5007
5008 switch (Node.getKind()) {
5009 case OffsetOfNode::Array:
5010 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
5011 Node.getArrayExprIndex(),
5012 Importer.Import(Node.getLocEnd())));
5013 break;
5014
5015 case OffsetOfNode::Base: {
5016 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
5017 if (!BS && Node.getBase())
5018 return nullptr;
5019 Nodes.push_back(OffsetOfNode(BS));
5020 break;
5021 }
5022 case OffsetOfNode::Field: {
5023 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
5024 if (!FD)
5025 return nullptr;
5026 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
5027 Importer.Import(Node.getLocEnd())));
5028 break;
5029 }
5030 case OffsetOfNode::Identifier: {
5031 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5032 if (!ToII)
5033 return nullptr;
5034 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5035 Importer.Import(Node.getLocEnd())));
5036 break;
5037 }
5038 }
5039 }
5040
5041 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5042 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5043 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5044 if (!ToIndexExpr)
5045 return nullptr;
5046 Exprs[I] = ToIndexExpr;
5047 }
5048
5049 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5050 if (!TInfo && OE->getTypeSourceInfo())
5051 return nullptr;
5052
5053 return OffsetOfExpr::Create(Importer.getToContext(), T,
5054 Importer.Import(OE->getOperatorLoc()),
5055 TInfo, Nodes, Exprs,
5056 Importer.Import(OE->getRParenLoc()));
5057}
5058
5059Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5060 QualType T = Importer.Import(E->getType());
5061 if (T.isNull())
5062 return nullptr;
5063
5064 Expr *Operand = Importer.Import(E->getOperand());
5065 if (!Operand)
5066 return nullptr;
5067
5068 CanThrowResult CanThrow;
5069 if (E->isValueDependent())
5070 CanThrow = CT_Dependent;
5071 else
5072 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5073
5074 return new (Importer.getToContext()) CXXNoexceptExpr(
5075 T, Operand, CanThrow,
5076 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5077}
5078
5079Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5080 QualType T = Importer.Import(E->getType());
5081 if (T.isNull())
5082 return nullptr;
5083
5084 Expr *SubExpr = Importer.Import(E->getSubExpr());
5085 if (!SubExpr && E->getSubExpr())
5086 return nullptr;
5087
5088 return new (Importer.getToContext()) CXXThrowExpr(
5089 SubExpr, T, Importer.Import(E->getThrowLoc()),
5090 E->isThrownVariableInScope());
5091}
5092
5093Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5094 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5095 Importer.Import(E->getParam()));
5096 if (!Param)
5097 return nullptr;
5098
5099 return CXXDefaultArgExpr::Create(
5100 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5101}
5102
5103Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5104 QualType T = Importer.Import(E->getType());
5105 if (T.isNull())
5106 return nullptr;
5107
5108 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5109 if (!TypeInfo)
5110 return nullptr;
5111
5112 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5113 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5114}
5115
5116Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5117 Expr *SubExpr = Importer.Import(E->getSubExpr());
5118 if (!SubExpr)
5119 return nullptr;
5120
5121 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5122 Importer.Import(const_cast<CXXDestructorDecl *>(
5123 E->getTemporary()->getDestructor())));
5124 if (!Dtor)
5125 return nullptr;
5126
5127 ASTContext &ToCtx = Importer.getToContext();
5128 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5129 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5130}
5131
5132Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5133 QualType T = Importer.Import(CE->getType());
5134 if (T.isNull())
5135 return nullptr;
5136
5137 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5138 if (ImportContainerChecked(CE->arguments(), Args))
5139 return nullptr;
5140
5141 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5142 Importer.Import(CE->getConstructor()));
5143 if (!Ctor)
5144 return nullptr;
5145
5146 return CXXTemporaryObjectExpr::Create(
5147 Importer.getToContext(), T,
5148 Importer.Import(CE->getLocStart()),
5149 Ctor,
5150 CE->isElidable(),
5151 Args,
5152 CE->hadMultipleCandidates(),
5153 CE->isListInitialization(),
5154 CE->isStdInitListInitialization(),
5155 CE->requiresZeroInitialization(),
5156 CE->getConstructionKind(),
5157 Importer.Import(CE->getParenOrBraceRange()));
5158}
5159
5160Expr *
5161ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5162 QualType T = Importer.Import(E->getType());
5163 if (T.isNull())
5164 return nullptr;
5165
5166 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5167 if (!TempE)
5168 return nullptr;
5169
5170 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5171 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5172 if (!ExtendedBy && E->getExtendingDecl())
5173 return nullptr;
5174
5175 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5176 T, TempE, E->isBoundToLvalueReference());
5177
5178 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5179 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5180 return ToMTE;
5181}
5182
5183Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5184 QualType T = Importer.Import(CE->getType());
5185 if (T.isNull())
5186 return nullptr;
5187
5188 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5189 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5190 return nullptr;
5191
5192 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5193 Importer.Import(CE->getOperatorNew()));
5194 if (!OperatorNewDecl && CE->getOperatorNew())
5195 return nullptr;
5196
5197 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5198 Importer.Import(CE->getOperatorDelete()));
5199 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5200 return nullptr;
5201
5202 Expr *ToInit = Importer.Import(CE->getInitializer());
5203 if (!ToInit && CE->getInitializer())
5204 return nullptr;
5205
5206 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5207 if (!TInfo)
5208 return nullptr;
5209
5210 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5211 if (!ToArrSize && CE->getArraySize())
5212 return nullptr;
5213
5214 return new (Importer.getToContext()) CXXNewExpr(
5215 Importer.getToContext(),
5216 CE->isGlobalNew(),
5217 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005218 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005219 CE->doesUsualArrayDeleteWantSize(),
5220 PlacementArgs,
5221 Importer.Import(CE->getTypeIdParens()),
5222 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5223 Importer.Import(CE->getSourceRange()),
5224 Importer.Import(CE->getDirectInitRange()));
5225}
5226
5227Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5228 QualType T = Importer.Import(E->getType());
5229 if (T.isNull())
5230 return nullptr;
5231
5232 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5233 Importer.Import(E->getOperatorDelete()));
5234 if (!OperatorDeleteDecl && E->getOperatorDelete())
5235 return nullptr;
5236
5237 Expr *ToArg = Importer.Import(E->getArgument());
5238 if (!ToArg && E->getArgument())
5239 return nullptr;
5240
5241 return new (Importer.getToContext()) CXXDeleteExpr(
5242 T, E->isGlobalDelete(),
5243 E->isArrayForm(),
5244 E->isArrayFormAsWritten(),
5245 E->doesUsualArrayDeleteWantSize(),
5246 OperatorDeleteDecl,
5247 ToArg,
5248 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005249}
5250
Sean Callanan59721b32015-04-28 18:41:46 +00005251Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5252 QualType T = Importer.Import(E->getType());
5253 if (T.isNull())
5254 return nullptr;
5255
5256 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005257 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005258 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005259 return nullptr;
5260
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005261 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005262 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005263 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005264
5265 return CXXConstructExpr::Create(Importer.getToContext(), T,
5266 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005267 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005268 ToArgs, E->hadMultipleCandidates(),
5269 E->isListInitialization(),
5270 E->isStdInitListInitialization(),
5271 E->requiresZeroInitialization(),
5272 E->getConstructionKind(),
5273 Importer.Import(E->getParenOrBraceRange()));
5274}
5275
Aleksei Sidorina693b372016-09-28 10:16:56 +00005276Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5277 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5278 if (!SubExpr && EWC->getSubExpr())
5279 return nullptr;
5280
5281 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5282 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5283 if (ExprWithCleanups::CleanupObject Obj =
5284 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5285 Objs[I] = Obj;
5286 else
5287 return nullptr;
5288
5289 return ExprWithCleanups::Create(Importer.getToContext(),
5290 SubExpr, EWC->cleanupsHaveSideEffects(),
5291 Objs);
5292}
5293
Sean Callanan8bca9962016-03-28 21:43:01 +00005294Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5295 QualType T = Importer.Import(E->getType());
5296 if (T.isNull())
5297 return nullptr;
5298
5299 Expr *ToFn = Importer.Import(E->getCallee());
5300 if (!ToFn)
5301 return nullptr;
5302
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005303 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005304 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005305 return nullptr;
5306
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005307 return new (Importer.getToContext()) CXXMemberCallExpr(
5308 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5309 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005310}
5311
5312Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5313 QualType T = Importer.Import(E->getType());
5314 if (T.isNull())
5315 return nullptr;
5316
5317 return new (Importer.getToContext())
5318 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5319}
5320
5321Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5322 QualType T = Importer.Import(E->getType());
5323 if (T.isNull())
5324 return nullptr;
5325
5326 return new (Importer.getToContext())
5327 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5328}
5329
5330
Sean Callanan59721b32015-04-28 18:41:46 +00005331Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5332 QualType T = Importer.Import(E->getType());
5333 if (T.isNull())
5334 return nullptr;
5335
5336 Expr *ToBase = Importer.Import(E->getBase());
5337 if (!ToBase && E->getBase())
5338 return nullptr;
5339
5340 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5341 if (!ToMember && E->getMemberDecl())
5342 return nullptr;
5343
5344 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5345 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5346 E->getFoundDecl().getAccess());
5347
5348 DeclarationNameInfo ToMemberNameInfo(
5349 Importer.Import(E->getMemberNameInfo().getName()),
5350 Importer.Import(E->getMemberNameInfo().getLoc()));
5351
5352 if (E->hasExplicitTemplateArgs()) {
5353 return nullptr; // FIXME: handle template arguments
5354 }
5355
5356 return MemberExpr::Create(Importer.getToContext(), ToBase,
5357 E->isArrow(),
5358 Importer.Import(E->getOperatorLoc()),
5359 Importer.Import(E->getQualifierLoc()),
5360 Importer.Import(E->getTemplateKeywordLoc()),
5361 ToMember, ToFoundDecl, ToMemberNameInfo,
5362 nullptr, T, E->getValueKind(),
5363 E->getObjectKind());
5364}
5365
5366Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5367 QualType T = Importer.Import(E->getType());
5368 if (T.isNull())
5369 return nullptr;
5370
5371 Expr *ToCallee = Importer.Import(E->getCallee());
5372 if (!ToCallee && E->getCallee())
5373 return nullptr;
5374
5375 unsigned NumArgs = E->getNumArgs();
5376
5377 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5378
5379 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5380 Expr *FromArg = E->getArg(ai);
5381 Expr *ToArg = Importer.Import(FromArg);
5382 if (!ToArg)
5383 return nullptr;
5384 ToArgs[ai] = ToArg;
5385 }
5386
5387 Expr **ToArgs_Copied = new (Importer.getToContext())
5388 Expr*[NumArgs];
5389
5390 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5391 ToArgs_Copied[ai] = ToArgs[ai];
5392
5393 return new (Importer.getToContext())
5394 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005395 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005396 Importer.Import(E->getRParenLoc()));
5397}
5398
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005399Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5400 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005401 if (T.isNull())
5402 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005403
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005404 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005405 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005406 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005407
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005408 ASTContext &ToCtx = Importer.getToContext();
5409 InitListExpr *To = new (ToCtx) InitListExpr(
5410 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5411 Exprs, Importer.Import(ILE->getLBraceLoc()));
5412 To->setType(T);
5413
5414 if (ILE->hasArrayFiller()) {
5415 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5416 if (!Filler)
5417 return nullptr;
5418 To->setArrayFiller(Filler);
5419 }
5420
5421 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5422 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5423 if (!ToFD)
5424 return nullptr;
5425 To->setInitializedFieldInUnion(ToFD);
5426 }
5427
5428 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5429 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5430 Importer.Import(SyntForm));
5431 if (!ToSyntForm)
5432 return nullptr;
5433 To->setSyntacticForm(ToSyntForm);
5434 }
5435
5436 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5437 To->setValueDependent(ILE->isValueDependent());
5438 To->setInstantiationDependent(ILE->isInstantiationDependent());
5439
5440 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005441}
5442
Richard Smith30e304e2016-12-14 00:03:17 +00005443Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5444 QualType ToType = Importer.Import(E->getType());
5445 if (ToType.isNull())
5446 return nullptr;
5447
5448 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5449 if (!ToCommon && E->getCommonExpr())
5450 return nullptr;
5451
5452 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5453 if (!ToSubExpr && E->getSubExpr())
5454 return nullptr;
5455
5456 return new (Importer.getToContext())
5457 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5458}
5459
5460Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5461 QualType ToType = Importer.Import(E->getType());
5462 if (ToType.isNull())
5463 return nullptr;
5464 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5465}
5466
Sean Callanandd2c1742016-05-16 20:48:03 +00005467Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5468 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5469 Importer.Import(DIE->getField()));
5470 if (!ToField && DIE->getField())
5471 return nullptr;
5472
5473 return CXXDefaultInitExpr::Create(
5474 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5475}
5476
5477Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5478 QualType ToType = Importer.Import(E->getType());
5479 if (ToType.isNull() && !E->getType().isNull())
5480 return nullptr;
5481 ExprValueKind VK = E->getValueKind();
5482 CastKind CK = E->getCastKind();
5483 Expr *ToOp = Importer.Import(E->getSubExpr());
5484 if (!ToOp && E->getSubExpr())
5485 return nullptr;
5486 CXXCastPath BasePath;
5487 if (ImportCastPath(E, BasePath))
5488 return nullptr;
5489 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5490 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5491 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5492 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5493
5494 if (isa<CXXStaticCastExpr>(E)) {
5495 return CXXStaticCastExpr::Create(
5496 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5497 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5498 } else if (isa<CXXDynamicCastExpr>(E)) {
5499 return CXXDynamicCastExpr::Create(
5500 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5501 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5502 } else if (isa<CXXReinterpretCastExpr>(E)) {
5503 return CXXReinterpretCastExpr::Create(
5504 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5505 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5506 } else {
5507 return nullptr;
5508 }
5509}
5510
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005511
5512Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
5513 SubstNonTypeTemplateParmExpr *E) {
5514 QualType T = Importer.Import(E->getType());
5515 if (T.isNull())
5516 return nullptr;
5517
5518 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
5519 Importer.Import(E->getParameter()));
5520 if (!Param)
5521 return nullptr;
5522
5523 Expr *Replacement = Importer.Import(E->getReplacement());
5524 if (!Replacement)
5525 return nullptr;
5526
5527 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
5528 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
5529 Replacement);
5530}
5531
Lang Hames19e07e12017-06-20 21:06:00 +00005532void ASTNodeImporter::ImportOverrides(CXXMethodDecl *ToMethod,
5533 CXXMethodDecl *FromMethod) {
5534 for (auto *FromOverriddenMethod : FromMethod->overridden_methods())
5535 ToMethod->addOverriddenMethod(
5536 cast<CXXMethodDecl>(Importer.Import(const_cast<CXXMethodDecl*>(
5537 FromOverriddenMethod))));
5538}
5539
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005540ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005541 ASTContext &FromContext, FileManager &FromFileManager,
5542 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005543 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005544 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005545 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005546{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005547 ImportedDecls[FromContext.getTranslationUnitDecl()]
5548 = ToContext.getTranslationUnitDecl();
5549}
5550
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005551ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005552
5553QualType ASTImporter::Import(QualType FromT) {
5554 if (FromT.isNull())
5555 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005556
5557 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005558
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005559 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005560 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5561 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005562 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005563 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005564
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005565 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005566 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005567 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005568 if (ToT.isNull())
5569 return ToT;
5570
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005571 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005572 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005573
John McCall424cec92011-01-19 06:33:43 +00005574 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005575}
5576
Douglas Gregor62d311f2010-02-09 19:21:46 +00005577TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005578 if (!FromTSI)
5579 return FromTSI;
5580
5581 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005582 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005583 QualType T = Import(FromTSI->getType());
5584 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005585 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005586
5587 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005588 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005589}
5590
Sean Callanan59721b32015-04-28 18:41:46 +00005591Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5592 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5593 if (Pos != ImportedDecls.end()) {
5594 Decl *ToD = Pos->second;
5595 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5596 return ToD;
5597 } else {
5598 return nullptr;
5599 }
5600}
5601
Douglas Gregor62d311f2010-02-09 19:21:46 +00005602Decl *ASTImporter::Import(Decl *FromD) {
5603 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005604 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005605
Douglas Gregord451ea92011-07-29 23:31:30 +00005606 ASTNodeImporter Importer(*this);
5607
Douglas Gregor62d311f2010-02-09 19:21:46 +00005608 // Check whether we've already imported this declaration.
5609 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005610 if (Pos != ImportedDecls.end()) {
5611 Decl *ToD = Pos->second;
5612 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5613 return ToD;
5614 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005615
5616 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005617 Decl *ToD = Importer.Visit(FromD);
5618 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005619 return nullptr;
5620
Douglas Gregor62d311f2010-02-09 19:21:46 +00005621 // Record the imported declaration.
5622 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005623
5624 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5625 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005626 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005627 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005628 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005629 // When we've finished transforming a typedef, see whether it was the
5630 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005631 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005632 FromTag = AnonTagsWithPendingTypedefs.begin(),
5633 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5634 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005635 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005636 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5637 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005638 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005639 AnonTagsWithPendingTypedefs.erase(FromTag);
5640 break;
5641 }
5642 }
5643 }
5644 }
5645
Douglas Gregor62d311f2010-02-09 19:21:46 +00005646 return ToD;
5647}
5648
5649DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5650 if (!FromDC)
5651 return FromDC;
5652
Douglas Gregor95d82832012-01-24 18:36:04 +00005653 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005654 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005655 return nullptr;
5656
Douglas Gregor2e15c842012-02-01 21:00:38 +00005657 // When we're using a record/enum/Objective-C class/protocol as a context, we
5658 // need it to have a definition.
5659 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005660 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005661 if (ToRecord->isCompleteDefinition()) {
5662 // Do nothing.
5663 } else if (FromRecord->isCompleteDefinition()) {
5664 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5665 ASTNodeImporter::IDK_Basic);
5666 } else {
5667 CompleteDecl(ToRecord);
5668 }
5669 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5670 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5671 if (ToEnum->isCompleteDefinition()) {
5672 // Do nothing.
5673 } else if (FromEnum->isCompleteDefinition()) {
5674 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5675 ASTNodeImporter::IDK_Basic);
5676 } else {
5677 CompleteDecl(ToEnum);
5678 }
5679 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5680 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5681 if (ToClass->getDefinition()) {
5682 // Do nothing.
5683 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5684 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5685 ASTNodeImporter::IDK_Basic);
5686 } else {
5687 CompleteDecl(ToClass);
5688 }
5689 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5690 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5691 if (ToProto->getDefinition()) {
5692 // Do nothing.
5693 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5694 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5695 ASTNodeImporter::IDK_Basic);
5696 } else {
5697 CompleteDecl(ToProto);
5698 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005699 }
5700
5701 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005702}
5703
5704Expr *ASTImporter::Import(Expr *FromE) {
5705 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005706 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005707
5708 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5709}
5710
5711Stmt *ASTImporter::Import(Stmt *FromS) {
5712 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005713 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005714
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005715 // Check whether we've already imported this declaration.
5716 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5717 if (Pos != ImportedStmts.end())
5718 return Pos->second;
5719
5720 // Import the type
5721 ASTNodeImporter Importer(*this);
5722 Stmt *ToS = Importer.Visit(FromS);
5723 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005724 return nullptr;
5725
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005726 // Record the imported declaration.
5727 ImportedStmts[FromS] = ToS;
5728 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005729}
5730
5731NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5732 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005733 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005734
Douglas Gregor90ebf252011-04-27 16:48:40 +00005735 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5736
5737 switch (FromNNS->getKind()) {
5738 case NestedNameSpecifier::Identifier:
5739 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5740 return NestedNameSpecifier::Create(ToContext, prefix, II);
5741 }
Craig Topper36250ad2014-05-12 05:36:57 +00005742 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005743
5744 case NestedNameSpecifier::Namespace:
5745 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005746 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00005747 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5748 }
Craig Topper36250ad2014-05-12 05:36:57 +00005749 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005750
5751 case NestedNameSpecifier::NamespaceAlias:
5752 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005753 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00005754 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5755 }
Craig Topper36250ad2014-05-12 05:36:57 +00005756 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005757
5758 case NestedNameSpecifier::Global:
5759 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5760
Nikola Smiljanic67860242014-09-26 00:28:20 +00005761 case NestedNameSpecifier::Super:
5762 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005763 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00005764 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5765 }
5766 return nullptr;
5767
Douglas Gregor90ebf252011-04-27 16:48:40 +00005768 case NestedNameSpecifier::TypeSpec:
5769 case NestedNameSpecifier::TypeSpecWithTemplate: {
5770 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5771 if (!T.isNull()) {
5772 bool bTemplate = FromNNS->getKind() ==
5773 NestedNameSpecifier::TypeSpecWithTemplate;
5774 return NestedNameSpecifier::Create(ToContext, prefix,
5775 bTemplate, T.getTypePtr());
5776 }
5777 }
Craig Topper36250ad2014-05-12 05:36:57 +00005778 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005779 }
5780
5781 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005782}
5783
Douglas Gregor14454802011-02-25 02:25:35 +00005784NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005785 // Copied from NestedNameSpecifier mostly.
5786 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5787 NestedNameSpecifierLoc NNS = FromNNS;
5788
5789 // Push each of the nested-name-specifiers's onto a stack for
5790 // serialization in reverse order.
5791 while (NNS) {
5792 NestedNames.push_back(NNS);
5793 NNS = NNS.getPrefix();
5794 }
5795
5796 NestedNameSpecifierLocBuilder Builder;
5797
5798 while (!NestedNames.empty()) {
5799 NNS = NestedNames.pop_back_val();
5800 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
5801 if (!Spec)
5802 return NestedNameSpecifierLoc();
5803
5804 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
5805 switch (Kind) {
5806 case NestedNameSpecifier::Identifier:
5807 Builder.Extend(getToContext(),
5808 Spec->getAsIdentifier(),
5809 Import(NNS.getLocalBeginLoc()),
5810 Import(NNS.getLocalEndLoc()));
5811 break;
5812
5813 case NestedNameSpecifier::Namespace:
5814 Builder.Extend(getToContext(),
5815 Spec->getAsNamespace(),
5816 Import(NNS.getLocalBeginLoc()),
5817 Import(NNS.getLocalEndLoc()));
5818 break;
5819
5820 case NestedNameSpecifier::NamespaceAlias:
5821 Builder.Extend(getToContext(),
5822 Spec->getAsNamespaceAlias(),
5823 Import(NNS.getLocalBeginLoc()),
5824 Import(NNS.getLocalEndLoc()));
5825 break;
5826
5827 case NestedNameSpecifier::TypeSpec:
5828 case NestedNameSpecifier::TypeSpecWithTemplate: {
5829 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
5830 QualType(Spec->getAsType(), 0));
5831 Builder.Extend(getToContext(),
5832 Import(NNS.getLocalBeginLoc()),
5833 TSI->getTypeLoc(),
5834 Import(NNS.getLocalEndLoc()));
5835 break;
5836 }
5837
5838 case NestedNameSpecifier::Global:
5839 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
5840 break;
5841
5842 case NestedNameSpecifier::Super: {
5843 SourceRange ToRange = Import(NNS.getSourceRange());
5844 Builder.MakeSuper(getToContext(),
5845 Spec->getAsRecordDecl(),
5846 ToRange.getBegin(),
5847 ToRange.getEnd());
5848 }
5849 }
5850 }
5851
5852 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00005853}
5854
Douglas Gregore2e50d332010-12-01 01:36:18 +00005855TemplateName ASTImporter::Import(TemplateName From) {
5856 switch (From.getKind()) {
5857 case TemplateName::Template:
5858 if (TemplateDecl *ToTemplate
5859 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5860 return TemplateName(ToTemplate);
5861
5862 return TemplateName();
5863
5864 case TemplateName::OverloadedTemplate: {
5865 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5866 UnresolvedSet<2> ToTemplates;
5867 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5868 E = FromStorage->end();
5869 I != E; ++I) {
5870 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5871 ToTemplates.addDecl(To);
5872 else
5873 return TemplateName();
5874 }
5875 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5876 ToTemplates.end());
5877 }
5878
5879 case TemplateName::QualifiedTemplate: {
5880 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5881 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5882 if (!Qualifier)
5883 return TemplateName();
5884
5885 if (TemplateDecl *ToTemplate
5886 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5887 return ToContext.getQualifiedTemplateName(Qualifier,
5888 QTN->hasTemplateKeyword(),
5889 ToTemplate);
5890
5891 return TemplateName();
5892 }
5893
5894 case TemplateName::DependentTemplate: {
5895 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5896 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5897 if (!Qualifier)
5898 return TemplateName();
5899
5900 if (DTN->isIdentifier()) {
5901 return ToContext.getDependentTemplateName(Qualifier,
5902 Import(DTN->getIdentifier()));
5903 }
5904
5905 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5906 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005907
5908 case TemplateName::SubstTemplateTemplateParm: {
5909 SubstTemplateTemplateParmStorage *subst
5910 = From.getAsSubstTemplateTemplateParm();
5911 TemplateTemplateParmDecl *param
5912 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5913 if (!param)
5914 return TemplateName();
5915
5916 TemplateName replacement = Import(subst->getReplacement());
5917 if (replacement.isNull()) return TemplateName();
5918
5919 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5920 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005921
5922 case TemplateName::SubstTemplateTemplateParmPack: {
5923 SubstTemplateTemplateParmPackStorage *SubstPack
5924 = From.getAsSubstTemplateTemplateParmPack();
5925 TemplateTemplateParmDecl *Param
5926 = cast_or_null<TemplateTemplateParmDecl>(
5927 Import(SubstPack->getParameterPack()));
5928 if (!Param)
5929 return TemplateName();
5930
5931 ASTNodeImporter Importer(*this);
5932 TemplateArgument ArgPack
5933 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5934 if (ArgPack.isNull())
5935 return TemplateName();
5936
5937 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5938 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005939 }
5940
5941 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005942}
5943
Douglas Gregor62d311f2010-02-09 19:21:46 +00005944SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5945 if (FromLoc.isInvalid())
5946 return SourceLocation();
5947
Douglas Gregor811663e2010-02-10 00:15:17 +00005948 SourceManager &FromSM = FromContext.getSourceManager();
5949
Sean Callanan24c5fe62016-11-07 20:42:25 +00005950 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005951 // don't have to import macro expansions.
5952 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00005953 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00005954 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5955 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005956 FileID ToFileID = Import(Decomposed.first);
5957 if (ToFileID.isInvalid())
5958 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005959 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5960 .getLocWithOffset(Decomposed.second);
5961 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005962}
5963
5964SourceRange ASTImporter::Import(SourceRange FromRange) {
5965 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5966}
5967
Douglas Gregor811663e2010-02-10 00:15:17 +00005968FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005969 llvm::DenseMap<FileID, FileID>::iterator Pos
5970 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005971 if (Pos != ImportedFileIDs.end())
5972 return Pos->second;
5973
5974 SourceManager &FromSM = FromContext.getSourceManager();
5975 SourceManager &ToSM = ToContext.getSourceManager();
5976 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005977 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005978
5979 // Include location of this file.
5980 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5981
5982 // Map the FileID for to the "to" source manager.
5983 FileID ToID;
5984 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005985 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005986 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5987 // disk again
5988 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5989 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005990 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005991 if (!Entry)
5992 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005993 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5994 FromSLoc.getFile().getFileCharacteristic());
5995 } else {
5996 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005997 const llvm::MemoryBuffer *
5998 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005999 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006000 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006001 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006002 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006003 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006004 }
6005
6006
Sebastian Redl99219f12010-09-30 01:03:06 +00006007 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006008 return ToID;
6009}
6010
Sean Callanandd2c1742016-05-16 20:48:03 +00006011CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
6012 Expr *ToExpr = Import(From->getInit());
6013 if (!ToExpr && From->getInit())
6014 return nullptr;
6015
6016 if (From->isBaseInitializer()) {
6017 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6018 if (!ToTInfo && From->getTypeSourceInfo())
6019 return nullptr;
6020
6021 return new (ToContext) CXXCtorInitializer(
6022 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
6023 ToExpr, Import(From->getRParenLoc()),
6024 From->isPackExpansion() ? Import(From->getEllipsisLoc())
6025 : SourceLocation());
6026 } else if (From->isMemberInitializer()) {
6027 FieldDecl *ToField =
6028 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
6029 if (!ToField && From->getMember())
6030 return nullptr;
6031
6032 return new (ToContext) CXXCtorInitializer(
6033 ToContext, ToField, Import(From->getMemberLocation()),
6034 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6035 } else if (From->isIndirectMemberInitializer()) {
6036 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6037 Import(From->getIndirectMember()));
6038 if (!ToIField && From->getIndirectMember())
6039 return nullptr;
6040
6041 return new (ToContext) CXXCtorInitializer(
6042 ToContext, ToIField, Import(From->getMemberLocation()),
6043 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6044 } else if (From->isDelegatingInitializer()) {
6045 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6046 if (!ToTInfo && From->getTypeSourceInfo())
6047 return nullptr;
6048
6049 return new (ToContext)
6050 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6051 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006052 } else {
6053 return nullptr;
6054 }
6055}
6056
6057
Aleksei Sidorina693b372016-09-28 10:16:56 +00006058CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6059 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6060 if (Pos != ImportedCXXBaseSpecifiers.end())
6061 return Pos->second;
6062
6063 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6064 Import(BaseSpec->getSourceRange()),
6065 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6066 BaseSpec->getAccessSpecifierAsWritten(),
6067 Import(BaseSpec->getTypeSourceInfo()),
6068 Import(BaseSpec->getEllipsisLoc()));
6069 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6070 return Imported;
6071}
6072
Douglas Gregor0a791672011-01-18 03:11:38 +00006073void ASTImporter::ImportDefinition(Decl *From) {
6074 Decl *To = Import(From);
6075 if (!To)
6076 return;
6077
6078 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6079 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006080
6081 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6082 if (!ToRecord->getDefinition()) {
6083 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006084 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006085 return;
6086 }
6087 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006088
6089 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6090 if (!ToEnum->getDefinition()) {
6091 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006092 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006093 return;
6094 }
6095 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006096
6097 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6098 if (!ToIFace->getDefinition()) {
6099 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006100 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006101 return;
6102 }
6103 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006104
Douglas Gregor2aa53772012-01-24 17:42:07 +00006105 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6106 if (!ToProto->getDefinition()) {
6107 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006108 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006109 return;
6110 }
6111 }
6112
Douglas Gregor0a791672011-01-18 03:11:38 +00006113 Importer.ImportDeclContext(FromDC, true);
6114 }
6115}
6116
Douglas Gregor96e578d2010-02-05 17:54:41 +00006117DeclarationName ASTImporter::Import(DeclarationName FromName) {
6118 if (!FromName)
6119 return DeclarationName();
6120
6121 switch (FromName.getNameKind()) {
6122 case DeclarationName::Identifier:
6123 return Import(FromName.getAsIdentifierInfo());
6124
6125 case DeclarationName::ObjCZeroArgSelector:
6126 case DeclarationName::ObjCOneArgSelector:
6127 case DeclarationName::ObjCMultiArgSelector:
6128 return Import(FromName.getObjCSelector());
6129
6130 case DeclarationName::CXXConstructorName: {
6131 QualType T = Import(FromName.getCXXNameType());
6132 if (T.isNull())
6133 return DeclarationName();
6134
6135 return ToContext.DeclarationNames.getCXXConstructorName(
6136 ToContext.getCanonicalType(T));
6137 }
6138
6139 case DeclarationName::CXXDestructorName: {
6140 QualType T = Import(FromName.getCXXNameType());
6141 if (T.isNull())
6142 return DeclarationName();
6143
6144 return ToContext.DeclarationNames.getCXXDestructorName(
6145 ToContext.getCanonicalType(T));
6146 }
6147
Richard Smith35845152017-02-07 01:37:30 +00006148 case DeclarationName::CXXDeductionGuideName: {
6149 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6150 Import(FromName.getCXXDeductionGuideTemplate()));
6151 if (!Template)
6152 return DeclarationName();
6153 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6154 }
6155
Douglas Gregor96e578d2010-02-05 17:54:41 +00006156 case DeclarationName::CXXConversionFunctionName: {
6157 QualType T = Import(FromName.getCXXNameType());
6158 if (T.isNull())
6159 return DeclarationName();
6160
6161 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6162 ToContext.getCanonicalType(T));
6163 }
6164
6165 case DeclarationName::CXXOperatorName:
6166 return ToContext.DeclarationNames.getCXXOperatorName(
6167 FromName.getCXXOverloadedOperator());
6168
6169 case DeclarationName::CXXLiteralOperatorName:
6170 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6171 Import(FromName.getCXXLiteralIdentifier()));
6172
6173 case DeclarationName::CXXUsingDirective:
6174 // FIXME: STATICS!
6175 return DeclarationName::getUsingDirectiveName();
6176 }
6177
David Blaikiee4d798f2012-01-20 21:50:17 +00006178 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006179}
6180
Douglas Gregore2e50d332010-12-01 01:36:18 +00006181IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006182 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006183 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006184
Sean Callananf94ef1d2016-05-14 06:11:19 +00006185 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6186
6187 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6188 ToId->setBuiltinID(FromId->getBuiltinID());
6189
6190 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006191}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006192
Douglas Gregor43f54792010-02-17 02:12:47 +00006193Selector ASTImporter::Import(Selector FromSel) {
6194 if (FromSel.isNull())
6195 return Selector();
6196
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006197 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006198 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6199 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6200 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6201 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6202}
6203
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006204DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6205 DeclContext *DC,
6206 unsigned IDNS,
6207 NamedDecl **Decls,
6208 unsigned NumDecls) {
6209 return Name;
6210}
6211
6212DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006213 if (LastDiagFromFrom)
6214 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6215 FromContext.getDiagnostics());
6216 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006217 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006218}
6219
6220DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006221 if (!LastDiagFromFrom)
6222 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6223 ToContext.getDiagnostics());
6224 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006225 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006226}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006227
Douglas Gregor2e15c842012-02-01 21:00:38 +00006228void ASTImporter::CompleteDecl (Decl *D) {
6229 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6230 if (!ID->getDefinition())
6231 ID->startDefinition();
6232 }
6233 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6234 if (!PD->getDefinition())
6235 PD->startDefinition();
6236 }
6237 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6238 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6239 TD->startDefinition();
6240 TD->setCompleteDefinition(true);
6241 }
6242 }
6243 else {
6244 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6245 }
6246}
6247
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006248Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006249 if (From->hasAttrs()) {
6250 for (Attr *FromAttr : From->getAttrs())
6251 To->addAttr(FromAttr->clone(To->getASTContext()));
6252 }
6253 if (From->isUsed()) {
6254 To->setIsUsed();
6255 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006256 if (From->isImplicit()) {
6257 To->setImplicit();
6258 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006259 ImportedDecls[From] = To;
6260 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006261}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006262
Douglas Gregordd6006f2012-07-17 21:16:27 +00006263bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6264 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006265 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006266 = ImportedTypes.find(From.getTypePtr());
6267 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6268 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00006269
Douglas Gregordd6006f2012-07-17 21:16:27 +00006270 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6271 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006272 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006273}