blob: 847638b7bbeb88598b25b388e8b053e60abb35a1 [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 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000322 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000323}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000324
Douglas Gregor3996e242010-02-15 22:01:00 +0000325//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000326// Import Types
327//----------------------------------------------------------------------------
328
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +0000329using namespace clang;
330
John McCall424cec92011-01-19 06:33:43 +0000331QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +0000332 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
333 << T->getTypeClassName();
334 return QualType();
335}
336
Gabor Horvath0866c2f2016-11-23 15:24:23 +0000337QualType ASTNodeImporter::VisitAtomicType(const AtomicType *T){
338 QualType UnderlyingType = Importer.Import(T->getValueType());
339 if(UnderlyingType.isNull())
340 return QualType();
341
342 return Importer.getToContext().getAtomicType(UnderlyingType);
343}
344
John McCall424cec92011-01-19 06:33:43 +0000345QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000346 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +0000347#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
348 case BuiltinType::Id: \
349 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +0000350#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +0000351#define SHARED_SINGLETON_TYPE(Expansion)
352#define BUILTIN_TYPE(Id, SingletonId) \
353 case BuiltinType::Id: return Importer.getToContext().SingletonId;
354#include "clang/AST/BuiltinTypes.def"
355
356 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
357 // context supports C++.
358
359 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
360 // context supports ObjC.
361
Douglas Gregor96e578d2010-02-05 17:54:41 +0000362 case BuiltinType::Char_U:
363 // The context we're importing from has an unsigned 'char'. If we're
364 // importing into a context with a signed 'char', translate to
365 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000366 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000367 return Importer.getToContext().UnsignedCharTy;
368
369 return Importer.getToContext().CharTy;
370
Douglas Gregor96e578d2010-02-05 17:54:41 +0000371 case BuiltinType::Char_S:
372 // The context we're importing from has an unsigned 'char'. If we're
373 // importing into a context with a signed 'char', translate to
374 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000375 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +0000376 return Importer.getToContext().SignedCharTy;
377
378 return Importer.getToContext().CharTy;
379
Chris Lattnerad3467e2010-12-25 23:25:43 +0000380 case BuiltinType::WChar_S:
381 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +0000382 // FIXME: If not in C++, shall we translate to the C equivalent of
383 // wchar_t?
384 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000385 }
David Blaikiee4d798f2012-01-20 21:50:17 +0000386
387 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +0000388}
389
Aleksei Sidorina693b372016-09-28 10:16:56 +0000390QualType ASTNodeImporter::VisitDecayedType(const DecayedType *T) {
391 QualType OrigT = Importer.Import(T->getOriginalType());
392 if (OrigT.isNull())
393 return QualType();
394
395 return Importer.getToContext().getDecayedType(OrigT);
396}
397
John McCall424cec92011-01-19 06:33:43 +0000398QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000399 QualType ToElementType = Importer.Import(T->getElementType());
400 if (ToElementType.isNull())
401 return QualType();
402
403 return Importer.getToContext().getComplexType(ToElementType);
404}
405
John McCall424cec92011-01-19 06:33:43 +0000406QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000407 QualType ToPointeeType = Importer.Import(T->getPointeeType());
408 if (ToPointeeType.isNull())
409 return QualType();
410
411 return Importer.getToContext().getPointerType(ToPointeeType);
412}
413
John McCall424cec92011-01-19 06:33:43 +0000414QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000415 // FIXME: Check for blocks support in "to" context.
416 QualType ToPointeeType = Importer.Import(T->getPointeeType());
417 if (ToPointeeType.isNull())
418 return QualType();
419
420 return Importer.getToContext().getBlockPointerType(ToPointeeType);
421}
422
John McCall424cec92011-01-19 06:33:43 +0000423QualType
424ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000425 // FIXME: Check for C++ support in "to" context.
426 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
427 if (ToPointeeType.isNull())
428 return QualType();
429
430 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
431}
432
John McCall424cec92011-01-19 06:33:43 +0000433QualType
434ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000435 // FIXME: Check for C++0x support in "to" context.
436 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
437 if (ToPointeeType.isNull())
438 return QualType();
439
440 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
441}
442
John McCall424cec92011-01-19 06:33:43 +0000443QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000444 // FIXME: Check for C++ support in "to" context.
445 QualType ToPointeeType = Importer.Import(T->getPointeeType());
446 if (ToPointeeType.isNull())
447 return QualType();
448
449 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
450 return Importer.getToContext().getMemberPointerType(ToPointeeType,
451 ClassType.getTypePtr());
452}
453
John McCall424cec92011-01-19 06:33:43 +0000454QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000455 QualType ToElementType = Importer.Import(T->getElementType());
456 if (ToElementType.isNull())
457 return QualType();
458
459 return Importer.getToContext().getConstantArrayType(ToElementType,
460 T->getSize(),
461 T->getSizeModifier(),
462 T->getIndexTypeCVRQualifiers());
463}
464
John McCall424cec92011-01-19 06:33:43 +0000465QualType
466ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000467 QualType ToElementType = Importer.Import(T->getElementType());
468 if (ToElementType.isNull())
469 return QualType();
470
471 return Importer.getToContext().getIncompleteArrayType(ToElementType,
472 T->getSizeModifier(),
473 T->getIndexTypeCVRQualifiers());
474}
475
John McCall424cec92011-01-19 06:33:43 +0000476QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000477 QualType ToElementType = Importer.Import(T->getElementType());
478 if (ToElementType.isNull())
479 return QualType();
480
481 Expr *Size = Importer.Import(T->getSizeExpr());
482 if (!Size)
483 return QualType();
484
485 SourceRange Brackets = Importer.Import(T->getBracketsRange());
486 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
487 T->getSizeModifier(),
488 T->getIndexTypeCVRQualifiers(),
489 Brackets);
490}
491
John McCall424cec92011-01-19 06:33:43 +0000492QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000493 QualType ToElementType = Importer.Import(T->getElementType());
494 if (ToElementType.isNull())
495 return QualType();
496
497 return Importer.getToContext().getVectorType(ToElementType,
498 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +0000499 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000500}
501
John McCall424cec92011-01-19 06:33:43 +0000502QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000503 QualType ToElementType = Importer.Import(T->getElementType());
504 if (ToElementType.isNull())
505 return QualType();
506
507 return Importer.getToContext().getExtVectorType(ToElementType,
508 T->getNumElements());
509}
510
John McCall424cec92011-01-19 06:33:43 +0000511QualType
512ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000513 // FIXME: What happens if we're importing a function without a prototype
514 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +0000515 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000516 if (ToResultType.isNull())
517 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000518
Douglas Gregor96e578d2010-02-05 17:54:41 +0000519 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000520 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000521}
522
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +0000523QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000524 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000525 if (ToResultType.isNull())
526 return QualType();
527
528 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000529 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +0000530 for (const auto &A : T->param_types()) {
531 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000532 if (ArgType.isNull())
533 return QualType();
534 ArgTypes.push_back(ArgType);
535 }
536
537 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000538 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +0000539 for (const auto &E : T->exceptions()) {
540 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000541 if (ExceptionType.isNull())
542 return QualType();
543 ExceptionTypes.push_back(ExceptionType);
544 }
John McCalldb40c7f2010-12-14 08:05:40 +0000545
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000546 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
547 FunctionProtoType::ExtProtoInfo ToEPI;
548
549 ToEPI.ExtInfo = FromEPI.ExtInfo;
550 ToEPI.Variadic = FromEPI.Variadic;
551 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
552 ToEPI.TypeQuals = FromEPI.TypeQuals;
553 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +0000554 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
555 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
556 ToEPI.ExceptionSpec.NoexceptExpr =
557 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
558 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
559 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
560 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
561 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000562
Jordan Rose5c382722013-03-08 21:51:21 +0000563 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +0000564}
565
Sean Callananda6df8a2011-08-11 16:56:07 +0000566QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
567 QualType ToInnerType = Importer.Import(T->getInnerType());
568 if (ToInnerType.isNull())
569 return QualType();
570
571 return Importer.getToContext().getParenType(ToInnerType);
572}
573
John McCall424cec92011-01-19 06:33:43 +0000574QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +0000575 TypedefNameDecl *ToDecl
576 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000577 if (!ToDecl)
578 return QualType();
579
580 return Importer.getToContext().getTypeDeclType(ToDecl);
581}
582
John McCall424cec92011-01-19 06:33:43 +0000583QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000584 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
585 if (!ToExpr)
586 return QualType();
587
588 return Importer.getToContext().getTypeOfExprType(ToExpr);
589}
590
John McCall424cec92011-01-19 06:33:43 +0000591QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000592 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
593 if (ToUnderlyingType.isNull())
594 return QualType();
595
596 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
597}
598
John McCall424cec92011-01-19 06:33:43 +0000599QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +0000600 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +0000601 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
602 if (!ToExpr)
603 return QualType();
604
Douglas Gregor81495f32012-02-12 18:42:33 +0000605 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
606 if (UnderlyingType.isNull())
607 return QualType();
608
609 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000610}
611
Alexis Hunte852b102011-05-24 22:41:36 +0000612QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
613 QualType ToBaseType = Importer.Import(T->getBaseType());
614 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
615 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
616 return QualType();
617
618 return Importer.getToContext().getUnaryTransformType(ToBaseType,
619 ToUnderlyingType,
620 T->getUTTKind());
621}
622
Richard Smith30482bc2011-02-20 03:19:35 +0000623QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +0000624 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +0000625 QualType FromDeduced = T->getDeducedType();
626 QualType ToDeduced;
627 if (!FromDeduced.isNull()) {
628 ToDeduced = Importer.Import(FromDeduced);
629 if (ToDeduced.isNull())
630 return QualType();
631 }
632
Richard Smithe301ba22015-11-11 02:02:15 +0000633 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +0000634 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +0000635}
636
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000637QualType ASTNodeImporter::VisitInjectedClassNameType(
638 const InjectedClassNameType *T) {
639 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
640 if (!D)
641 return QualType();
642
643 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
644 if (InjType.isNull())
645 return QualType();
646
647 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
648 // See comments in InjectedClassNameType definition for details
649 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
650 enum {
651 TypeAlignmentInBits = 4,
652 TypeAlignment = 1 << TypeAlignmentInBits
653 };
654
655 return QualType(new (Importer.getToContext(), TypeAlignment)
656 InjectedClassNameType(D, InjType), 0);
657}
658
John McCall424cec92011-01-19 06:33:43 +0000659QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000660 RecordDecl *ToDecl
661 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
662 if (!ToDecl)
663 return QualType();
664
665 return Importer.getToContext().getTagDeclType(ToDecl);
666}
667
John McCall424cec92011-01-19 06:33:43 +0000668QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000669 EnumDecl *ToDecl
670 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
671 if (!ToDecl)
672 return QualType();
673
674 return Importer.getToContext().getTagDeclType(ToDecl);
675}
676
Sean Callanan72fe0852015-04-02 23:50:08 +0000677QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
678 QualType FromModifiedType = T->getModifiedType();
679 QualType FromEquivalentType = T->getEquivalentType();
680 QualType ToModifiedType;
681 QualType ToEquivalentType;
682
683 if (!FromModifiedType.isNull()) {
684 ToModifiedType = Importer.Import(FromModifiedType);
685 if (ToModifiedType.isNull())
686 return QualType();
687 }
688 if (!FromEquivalentType.isNull()) {
689 ToEquivalentType = Importer.Import(FromEquivalentType);
690 if (ToEquivalentType.isNull())
691 return QualType();
692 }
693
694 return Importer.getToContext().getAttributedType(T->getAttrKind(),
695 ToModifiedType, ToEquivalentType);
696}
697
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000698
699QualType ASTNodeImporter::VisitTemplateTypeParmType(
700 const TemplateTypeParmType *T) {
701 TemplateTypeParmDecl *ParmDecl =
702 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
703 if (!ParmDecl && T->getDecl())
704 return QualType();
705
706 return Importer.getToContext().getTemplateTypeParmType(
707 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
708}
709
Aleksei Sidorin855086d2017-01-23 09:30:36 +0000710QualType ASTNodeImporter::VisitSubstTemplateTypeParmType(
711 const SubstTemplateTypeParmType *T) {
712 const TemplateTypeParmType *Replaced =
713 cast_or_null<TemplateTypeParmType>(Importer.Import(
714 QualType(T->getReplacedParameter(), 0)).getTypePtr());
715 if (!Replaced)
716 return QualType();
717
718 QualType Replacement = Importer.Import(T->getReplacementType());
719 if (Replacement.isNull())
720 return QualType();
721 Replacement = Replacement.getCanonicalType();
722
723 return Importer.getToContext().getSubstTemplateTypeParmType(
724 Replaced, Replacement);
725}
726
Douglas Gregore2e50d332010-12-01 01:36:18 +0000727QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +0000728 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000729 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
730 if (ToTemplate.isNull())
731 return QualType();
732
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000733 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +0000734 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
735 return QualType();
736
737 QualType ToCanonType;
738 if (!QualType(T, 0).isCanonical()) {
739 QualType FromCanonType
740 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
741 ToCanonType =Importer.Import(FromCanonType);
742 if (ToCanonType.isNull())
743 return QualType();
744 }
745 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
David Majnemer6fbeee32016-07-07 04:43:07 +0000746 ToTemplateArgs,
Douglas Gregore2e50d332010-12-01 01:36:18 +0000747 ToCanonType);
748}
749
John McCall424cec92011-01-19 06:33:43 +0000750QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +0000751 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000752 // Note: the qualifier in an ElaboratedType is optional.
753 if (T->getQualifier()) {
754 ToQualifier = Importer.Import(T->getQualifier());
755 if (!ToQualifier)
756 return QualType();
757 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000758
759 QualType ToNamedType = Importer.Import(T->getNamedType());
760 if (ToNamedType.isNull())
761 return QualType();
762
Abramo Bagnara6150c882010-05-11 21:36:43 +0000763 return Importer.getToContext().getElaboratedType(T->getKeyword(),
764 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000765}
766
John McCall424cec92011-01-19 06:33:43 +0000767QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000768 ObjCInterfaceDecl *Class
769 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
770 if (!Class)
771 return QualType();
772
John McCall8b07ec22010-05-15 11:32:37 +0000773 return Importer.getToContext().getObjCInterfaceType(Class);
774}
775
John McCall424cec92011-01-19 06:33:43 +0000776QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000777 QualType ToBaseType = Importer.Import(T->getBaseType());
778 if (ToBaseType.isNull())
779 return QualType();
780
Douglas Gregore9d95f12015-07-07 03:57:35 +0000781 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +0000782 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +0000783 QualType ImportedTypeArg = Importer.Import(TypeArg);
784 if (ImportedTypeArg.isNull())
785 return QualType();
786
787 TypeArgs.push_back(ImportedTypeArg);
788 }
789
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000790 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000791 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000792 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000793 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +0000794 if (!Protocol)
795 return QualType();
796 Protocols.push_back(Protocol);
797 }
798
Douglas Gregore9d95f12015-07-07 03:57:35 +0000799 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +0000800 Protocols,
801 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +0000802}
803
John McCall424cec92011-01-19 06:33:43 +0000804QualType
805ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +0000806 QualType ToPointeeType = Importer.Import(T->getPointeeType());
807 if (ToPointeeType.isNull())
808 return QualType();
809
John McCall8b07ec22010-05-15 11:32:37 +0000810 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000811}
812
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000813//----------------------------------------------------------------------------
814// Import Declarations
815//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000816bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
817 DeclContext *&LexicalDC,
818 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +0000819 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000820 SourceLocation &Loc) {
821 // Import the context of this declaration.
822 DC = Importer.ImportContext(D->getDeclContext());
823 if (!DC)
824 return true;
825
826 LexicalDC = DC;
827 if (D->getDeclContext() != D->getLexicalDeclContext()) {
828 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
829 if (!LexicalDC)
830 return true;
831 }
832
833 // Import the name of this declaration.
834 Name = Importer.Import(D->getDeclName());
835 if (D->getDeclName() && !Name)
836 return true;
837
838 // Import the location of this declaration.
839 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +0000840 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000841 return false;
842}
843
Douglas Gregord451ea92011-07-29 23:31:30 +0000844void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
845 if (!FromD)
846 return;
847
848 if (!ToD) {
849 ToD = Importer.Import(FromD);
850 if (!ToD)
851 return;
852 }
853
854 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
855 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +0000856 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +0000857 ImportDefinition(FromRecord, ToRecord);
858 }
859 }
860 return;
861 }
862
863 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
864 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
865 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
866 ImportDefinition(FromEnum, ToEnum);
867 }
868 }
869 return;
870 }
871}
872
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000873void
874ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
875 DeclarationNameInfo& To) {
876 // NOTE: To.Name and To.Loc are already imported.
877 // We only have to import To.LocInfo.
878 switch (To.getName().getNameKind()) {
879 case DeclarationName::Identifier:
880 case DeclarationName::ObjCZeroArgSelector:
881 case DeclarationName::ObjCOneArgSelector:
882 case DeclarationName::ObjCMultiArgSelector:
883 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +0000884 case DeclarationName::CXXDeductionGuideName:
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000885 return;
886
887 case DeclarationName::CXXOperatorName: {
888 SourceRange Range = From.getCXXOperatorNameRange();
889 To.setCXXOperatorNameRange(Importer.Import(Range));
890 return;
891 }
892 case DeclarationName::CXXLiteralOperatorName: {
893 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
894 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
895 return;
896 }
897 case DeclarationName::CXXConstructorName:
898 case DeclarationName::CXXDestructorName:
899 case DeclarationName::CXXConversionFunctionName: {
900 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
901 To.setNamedTypeInfo(Importer.Import(FromTInfo));
902 return;
903 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000904 }
Douglas Gregor07216d12011-11-02 20:52:01 +0000905 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000906}
907
Douglas Gregor2e15c842012-02-01 21:00:38 +0000908void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +0000909 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +0000910 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +0000911 return;
912 }
913
Aaron Ballman629afae2014-03-07 19:56:05 +0000914 for (auto *From : FromDC->decls())
915 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +0000916}
917
Douglas Gregord451ea92011-07-29 23:31:30 +0000918bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000919 ImportDefinitionKind Kind) {
920 if (To->getDefinition() || To->isBeingDefined()) {
921 if (Kind == IDK_Everything)
922 ImportDeclContext(From, /*ForceImport=*/true);
923
Douglas Gregore2e50d332010-12-01 01:36:18 +0000924 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +0000925 }
Douglas Gregore2e50d332010-12-01 01:36:18 +0000926
927 To->startDefinition();
928
929 // Add base classes.
930 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
931 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000932
933 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
934 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
935 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000936 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000937 ToData.Aggregate = FromData.Aggregate;
938 ToData.PlainOldData = FromData.PlainOldData;
939 ToData.Empty = FromData.Empty;
940 ToData.Polymorphic = FromData.Polymorphic;
941 ToData.Abstract = FromData.Abstract;
942 ToData.IsStandardLayout = FromData.IsStandardLayout;
943 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
944 ToData.HasPrivateFields = FromData.HasPrivateFields;
945 ToData.HasProtectedFields = FromData.HasProtectedFields;
946 ToData.HasPublicFields = FromData.HasPublicFields;
947 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +0000948 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +0000949 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +0000950 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +0000951 ToData.HasUninitializedReferenceMember
952 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +0000953 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith12e79312016-05-13 06:47:56 +0000954 ToData.HasInheritedConstructor = FromData.HasInheritedConstructor;
955 ToData.HasInheritedAssignment = FromData.HasInheritedAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000956 ToData.NeedOverloadResolutionForMoveConstructor
957 = FromData.NeedOverloadResolutionForMoveConstructor;
958 ToData.NeedOverloadResolutionForMoveAssignment
959 = FromData.NeedOverloadResolutionForMoveAssignment;
960 ToData.NeedOverloadResolutionForDestructor
961 = FromData.NeedOverloadResolutionForDestructor;
962 ToData.DefaultedMoveConstructorIsDeleted
963 = FromData.DefaultedMoveConstructorIsDeleted;
964 ToData.DefaultedMoveAssignmentIsDeleted
965 = FromData.DefaultedMoveAssignmentIsDeleted;
966 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +0000967 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
968 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000969 ToData.HasConstexprNonCopyMoveConstructor
970 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +0000971 ToData.HasDefaultedDefaultConstructor
972 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +0000973 ToData.DefaultedDefaultConstructorIsConstexpr
974 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +0000975 ToData.HasConstexprDefaultConstructor
976 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000977 ToData.HasNonLiteralTypeFieldsOrBases
978 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +0000979 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000980 ToData.UserProvidedDefaultConstructor
981 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +0000982 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithdf054d32017-02-25 23:53:05 +0000983 ToData.ImplicitCopyConstructorCanHaveConstParamForVBase
984 = FromData.ImplicitCopyConstructorCanHaveConstParamForVBase;
985 ToData.ImplicitCopyConstructorCanHaveConstParamForNonVBase
986 = FromData.ImplicitCopyConstructorCanHaveConstParamForNonVBase;
Richard Smith1c33fe82012-11-28 06:23:12 +0000987 ToData.ImplicitCopyAssignmentHasConstParam
988 = FromData.ImplicitCopyAssignmentHasConstParam;
989 ToData.HasDeclaredCopyConstructorWithConstParam
990 = FromData.HasDeclaredCopyConstructorWithConstParam;
991 ToData.HasDeclaredCopyAssignmentWithConstParam
992 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +0000993 ToData.IsLambda = FromData.IsLambda;
994
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000995 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +0000996 for (const auto &Base1 : FromCXX->bases()) {
997 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000998 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +0000999 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001000
1001 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001002 if (Base1.isPackExpansion())
1003 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001004
1005 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001006 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001007
Douglas Gregore2e50d332010-12-01 01:36:18 +00001008 Bases.push_back(
1009 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001010 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1011 Base1.isVirtual(),
1012 Base1.isBaseOfClass(),
1013 Base1.getAccessSpecifierAsWritten(),
1014 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001015 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001016 }
1017 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00001018 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001019 }
1020
Douglas Gregor2e15c842012-02-01 21:00:38 +00001021 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001022 ImportDeclContext(From, /*ForceImport=*/true);
1023
Douglas Gregore2e50d332010-12-01 01:36:18 +00001024 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001025 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001026}
1027
Larisse Voufo39a1e502013-08-06 01:03:05 +00001028bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
1029 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00001030 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00001031 return false;
1032
1033 // FIXME: Can we really import any initializer? Alternatively, we could force
1034 // ourselves to import every declaration of a variable and then only use
1035 // getInit() here.
1036 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
1037
1038 // FIXME: Other bits to merge?
1039
1040 return false;
1041}
1042
Douglas Gregord451ea92011-07-29 23:31:30 +00001043bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001044 ImportDefinitionKind Kind) {
1045 if (To->getDefinition() || To->isBeingDefined()) {
1046 if (Kind == IDK_Everything)
1047 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001048 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001049 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001050
1051 To->startDefinition();
1052
1053 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1054 if (T.isNull())
1055 return true;
1056
1057 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1058 if (ToPromotionType.isNull())
1059 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001060
1061 if (shouldForceImportDeclContext(Kind))
1062 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001063
1064 // FIXME: we might need to merge the number of positive or negative bits
1065 // if the enumerator lists don't match.
1066 To->completeDefinition(T, ToPromotionType,
1067 From->getNumPositiveBits(),
1068 From->getNumNegativeBits());
1069 return false;
1070}
1071
Douglas Gregora082a492010-11-30 19:14:50 +00001072TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1073 TemplateParameterList *Params) {
Aleksei Sidorina693b372016-09-28 10:16:56 +00001074 SmallVector<NamedDecl *, 4> ToParams(Params->size());
1075 if (ImportContainerChecked(*Params, ToParams))
1076 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00001077
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001078 Expr *ToRequiresClause;
1079 if (Expr *const R = Params->getRequiresClause()) {
1080 ToRequiresClause = Importer.Import(R);
1081 if (!ToRequiresClause)
1082 return nullptr;
1083 } else {
1084 ToRequiresClause = nullptr;
1085 }
1086
Douglas Gregora082a492010-11-30 19:14:50 +00001087 return TemplateParameterList::Create(Importer.getToContext(),
1088 Importer.Import(Params->getTemplateLoc()),
1089 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00001090 ToParams,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001091 Importer.Import(Params->getRAngleLoc()),
1092 ToRequiresClause);
Douglas Gregora082a492010-11-30 19:14:50 +00001093}
1094
Douglas Gregore2e50d332010-12-01 01:36:18 +00001095TemplateArgument
1096ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1097 switch (From.getKind()) {
1098 case TemplateArgument::Null:
1099 return TemplateArgument();
1100
1101 case TemplateArgument::Type: {
1102 QualType ToType = Importer.Import(From.getAsType());
1103 if (ToType.isNull())
1104 return TemplateArgument();
1105 return TemplateArgument(ToType);
1106 }
1107
1108 case TemplateArgument::Integral: {
1109 QualType ToType = Importer.Import(From.getIntegralType());
1110 if (ToType.isNull())
1111 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001112 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001113 }
1114
Eli Friedmanb826a002012-09-26 02:36:12 +00001115 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00001116 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
1117 QualType ToType = Importer.Import(From.getParamTypeForDecl());
1118 if (!To || ToType.isNull())
1119 return TemplateArgument();
1120 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00001121 }
1122
1123 case TemplateArgument::NullPtr: {
1124 QualType ToType = Importer.Import(From.getNullPtrType());
1125 if (ToType.isNull())
1126 return TemplateArgument();
1127 return TemplateArgument(ToType, /*isNullPtr*/true);
1128 }
1129
Douglas Gregore2e50d332010-12-01 01:36:18 +00001130 case TemplateArgument::Template: {
1131 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1132 if (ToTemplate.isNull())
1133 return TemplateArgument();
1134
1135 return TemplateArgument(ToTemplate);
1136 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001137
1138 case TemplateArgument::TemplateExpansion: {
1139 TemplateName ToTemplate
1140 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1141 if (ToTemplate.isNull())
1142 return TemplateArgument();
1143
Douglas Gregore1d60df2011-01-14 23:41:42 +00001144 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001145 }
1146
Douglas Gregore2e50d332010-12-01 01:36:18 +00001147 case TemplateArgument::Expression:
1148 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1149 return TemplateArgument(ToExpr);
1150 return TemplateArgument();
1151
1152 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001153 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001154 ToPack.reserve(From.pack_size());
1155 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1156 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00001157
1158 return TemplateArgument(
1159 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001160 }
1161 }
1162
1163 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001164}
1165
Aleksei Sidorina693b372016-09-28 10:16:56 +00001166TemplateArgumentLoc ASTNodeImporter::ImportTemplateArgumentLoc(
1167 const TemplateArgumentLoc &TALoc, bool &Error) {
1168 Error = false;
1169 TemplateArgument Arg = ImportTemplateArgument(TALoc.getArgument());
1170 TemplateArgumentLocInfo FromInfo = TALoc.getLocInfo();
1171 TemplateArgumentLocInfo ToInfo;
1172 if (Arg.getKind() == TemplateArgument::Expression) {
1173 Expr *E = Importer.Import(FromInfo.getAsExpr());
1174 ToInfo = TemplateArgumentLocInfo(E);
1175 if (!E)
1176 Error = true;
1177 } else if (Arg.getKind() == TemplateArgument::Type) {
1178 if (TypeSourceInfo *TSI = Importer.Import(FromInfo.getAsTypeSourceInfo()))
1179 ToInfo = TemplateArgumentLocInfo(TSI);
1180 else
1181 Error = true;
1182 } else {
1183 ToInfo = TemplateArgumentLocInfo(
1184 Importer.Import(FromInfo.getTemplateQualifierLoc()),
1185 Importer.Import(FromInfo.getTemplateNameLoc()),
1186 Importer.Import(FromInfo.getTemplateEllipsisLoc()));
1187 }
1188 return TemplateArgumentLoc(Arg, ToInfo);
1189}
1190
Douglas Gregore2e50d332010-12-01 01:36:18 +00001191bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1192 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001193 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001194 for (unsigned I = 0; I != NumFromArgs; ++I) {
1195 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1196 if (To.isNull() && !FromArgs[I].isNull())
1197 return true;
1198
1199 ToArgs.push_back(To);
1200 }
1201
1202 return false;
1203}
1204
Douglas Gregor5c73e912010-02-11 00:48:18 +00001205bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00001206 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00001207 // Eliminate a potential failure point where we attempt to re-import
1208 // something we're trying to import while completing ToRecord.
1209 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
1210 if (ToOrigin) {
1211 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
1212 if (ToOriginRecord)
1213 ToRecord = ToOriginRecord;
1214 }
1215
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001216 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00001217 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00001218 Importer.getNonEquivalentDecls(),
1219 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001220 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001221}
1222
Larisse Voufo39a1e502013-08-06 01:03:05 +00001223bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
1224 bool Complain) {
1225 StructuralEquivalenceContext Ctx(
1226 Importer.getFromContext(), Importer.getToContext(),
1227 Importer.getNonEquivalentDecls(), false, Complain);
1228 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
1229}
1230
Douglas Gregor98c10182010-02-12 22:17:39 +00001231bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001232 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001233 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001234 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001235 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001236}
1237
Douglas Gregor91155082012-11-14 22:29:20 +00001238bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
1239 EnumConstantDecl *ToEC)
1240{
1241 const llvm::APSInt &FromVal = FromEC->getInitVal();
1242 const llvm::APSInt &ToVal = ToEC->getInitVal();
1243
1244 return FromVal.isSigned() == ToVal.isSigned() &&
1245 FromVal.getBitWidth() == ToVal.getBitWidth() &&
1246 FromVal == ToVal;
1247}
1248
1249bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00001250 ClassTemplateDecl *To) {
1251 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1252 Importer.getToContext(),
1253 Importer.getNonEquivalentDecls());
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001254 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregora082a492010-11-30 19:14:50 +00001255}
1256
Larisse Voufo39a1e502013-08-06 01:03:05 +00001257bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
1258 VarTemplateDecl *To) {
1259 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1260 Importer.getToContext(),
1261 Importer.getNonEquivalentDecls());
1262 return Ctx.IsStructurallyEquivalent(From, To);
1263}
1264
Douglas Gregore4c83e42010-02-09 22:48:33 +00001265Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001266 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001267 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00001268 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00001269}
1270
Sean Callanan65198272011-11-17 23:20:56 +00001271Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
1272 TranslationUnitDecl *ToD =
1273 Importer.getToContext().getTranslationUnitDecl();
1274
1275 Importer.Imported(D, ToD);
1276
1277 return ToD;
1278}
1279
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00001280Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1281
1282 SourceLocation Loc = Importer.Import(D->getLocation());
1283 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
1284
1285 // Import the context of this declaration.
1286 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1287 if (!DC)
1288 return nullptr;
1289
1290 AccessSpecDecl *accessSpecDecl
1291 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
1292 DC, Loc, ColonLoc);
1293
1294 if (!accessSpecDecl)
1295 return nullptr;
1296
1297 // Lexical DeclContext and Semantic DeclContext
1298 // is always the same for the accessSpec.
1299 accessSpecDecl->setLexicalDeclContext(DC);
1300 DC->addDeclInternal(accessSpecDecl);
1301
1302 return accessSpecDecl;
1303}
1304
Aleksei Sidorina693b372016-09-28 10:16:56 +00001305Decl *ASTNodeImporter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1306 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
1307 if (!DC)
1308 return nullptr;
1309
1310 DeclContext *LexicalDC = DC;
1311
1312 // Import the location of this declaration.
1313 SourceLocation Loc = Importer.Import(D->getLocation());
1314
1315 Expr *AssertExpr = Importer.Import(D->getAssertExpr());
1316 if (!AssertExpr)
1317 return nullptr;
1318
1319 StringLiteral *FromMsg = D->getMessage();
1320 StringLiteral *ToMsg = cast_or_null<StringLiteral>(Importer.Import(FromMsg));
1321 if (!ToMsg && FromMsg)
1322 return nullptr;
1323
1324 StaticAssertDecl *ToD = StaticAssertDecl::Create(
1325 Importer.getToContext(), DC, Loc, AssertExpr, ToMsg,
1326 Importer.Import(D->getRParenLoc()), D->isFailed());
1327
1328 ToD->setLexicalDeclContext(LexicalDC);
1329 LexicalDC->addDeclInternal(ToD);
1330 Importer.Imported(D, ToD);
1331 return ToD;
1332}
1333
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001334Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1335 // Import the major distinguishing characteristics of this namespace.
1336 DeclContext *DC, *LexicalDC;
1337 DeclarationName Name;
1338 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001339 NamedDecl *ToD;
1340 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001341 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001342 if (ToD)
1343 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001344
1345 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001346 if (!Name) {
1347 // This is an anonymous namespace. Adopt an existing anonymous
1348 // namespace if we can.
1349 // FIXME: Not testable.
1350 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1351 MergeWithNamespace = TU->getAnonymousNamespace();
1352 else
1353 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1354 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001355 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001356 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001357 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001358 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1359 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001360 continue;
1361
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001362 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001363 MergeWithNamespace = FoundNS;
1364 ConflictingDecls.clear();
1365 break;
1366 }
1367
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001368 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001369 }
1370
1371 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001372 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001373 ConflictingDecls.data(),
1374 ConflictingDecls.size());
1375 }
1376 }
1377
1378 // Create the "to" namespace, if needed.
1379 NamespaceDecl *ToNamespace = MergeWithNamespace;
1380 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001381 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001382 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001383 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00001384 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00001385 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001386 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001387 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001388
1389 // If this is an anonymous namespace, register it as the anonymous
1390 // namespace within its context.
1391 if (!Name) {
1392 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1393 TU->setAnonymousNamespace(ToNamespace);
1394 else
1395 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1396 }
1397 }
1398 Importer.Imported(D, ToNamespace);
1399
1400 ImportDeclContext(D);
1401
1402 return ToNamespace;
1403}
1404
Richard Smithdda56e42011-04-15 14:24:37 +00001405Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001406 // Import the major distinguishing characteristics of this typedef.
1407 DeclContext *DC, *LexicalDC;
1408 DeclarationName Name;
1409 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001410 NamedDecl *ToD;
1411 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001412 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001413 if (ToD)
1414 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001415
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001416 // If this typedef is not in block scope, determine whether we've
1417 // seen a typedef with the same name (that we can merge with) or any
1418 // other entity by that name (which name lookup could conflict with).
1419 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001420 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001421 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001422 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001423 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001424 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1425 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001426 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00001427 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001428 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001429 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1430 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001431 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001432 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001433
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001434 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001435 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001436
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001437 if (!ConflictingDecls.empty()) {
1438 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1439 ConflictingDecls.data(),
1440 ConflictingDecls.size());
1441 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001442 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001443 }
1444 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001445
Douglas Gregorb4964f72010-02-15 23:54:17 +00001446 // Import the underlying type of this typedef;
1447 QualType T = Importer.Import(D->getUnderlyingType());
1448 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001449 return nullptr;
1450
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001451 // Create the new typedef node.
1452 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00001453 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00001454 TypedefNameDecl *ToTypedef;
1455 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001456 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
1457 StartL, Loc,
1458 Name.getAsIdentifierInfo(),
1459 TInfo);
1460 else
Richard Smithdda56e42011-04-15 14:24:37 +00001461 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1462 StartL, Loc,
1463 Name.getAsIdentifierInfo(),
1464 TInfo);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001465
Douglas Gregordd483172010-02-22 17:42:47 +00001466 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001467 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001468 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00001469 LexicalDC->addDeclInternal(ToTypedef);
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001470
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001471 return ToTypedef;
1472}
1473
Richard Smithdda56e42011-04-15 14:24:37 +00001474Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1475 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
1476}
1477
1478Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
1479 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
1480}
1481
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001482Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
1483 // Import the major distinguishing characteristics of this label.
1484 DeclContext *DC, *LexicalDC;
1485 DeclarationName Name;
1486 SourceLocation Loc;
1487 NamedDecl *ToD;
1488 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
1489 return nullptr;
1490 if (ToD)
1491 return ToD;
1492
1493 assert(LexicalDC->isFunctionOrMethod());
1494
1495 LabelDecl *ToLabel = D->isGnuLocal()
1496 ? LabelDecl::Create(Importer.getToContext(),
1497 DC, Importer.Import(D->getLocation()),
1498 Name.getAsIdentifierInfo(),
1499 Importer.Import(D->getLocStart()))
1500 : LabelDecl::Create(Importer.getToContext(),
1501 DC, Importer.Import(D->getLocation()),
1502 Name.getAsIdentifierInfo());
1503 Importer.Imported(D, ToLabel);
1504
1505 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
1506 if (!Label)
1507 return nullptr;
1508
1509 ToLabel->setStmt(Label);
1510 ToLabel->setLexicalDeclContext(LexicalDC);
1511 LexicalDC->addDeclInternal(ToLabel);
1512 return ToLabel;
1513}
1514
Douglas Gregor98c10182010-02-12 22:17:39 +00001515Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1516 // Import the major distinguishing characteristics of this enum.
1517 DeclContext *DC, *LexicalDC;
1518 DeclarationName Name;
1519 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001520 NamedDecl *ToD;
1521 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001522 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001523 if (ToD)
1524 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001525
Douglas Gregor98c10182010-02-12 22:17:39 +00001526 // Figure out what enum name we're looking for.
1527 unsigned IDNS = Decl::IDNS_Tag;
1528 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001529 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1530 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00001531 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001532 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00001533 IDNS |= Decl::IDNS_Ordinary;
1534
1535 // We may already have an enum of the same name; try to find and match it.
1536 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001537 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001538 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001539 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001540 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1541 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001542 continue;
1543
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001544 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001545 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001546 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1547 Found = Tag->getDecl();
1548 }
1549
1550 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001551 if (IsStructuralMatch(D, FoundEnum))
1552 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001553 }
1554
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001555 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001556 }
1557
1558 if (!ConflictingDecls.empty()) {
1559 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1560 ConflictingDecls.data(),
1561 ConflictingDecls.size());
1562 }
1563 }
1564
1565 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001566 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
1567 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00001568 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001569 D->isScoped(), D->isScopedUsingClassTag(),
1570 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001571 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001572 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001573 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001574 D2->setLexicalDeclContext(LexicalDC);
1575 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00001576 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001577
1578 // Import the integer type.
1579 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1580 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001581 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00001582 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001583
1584 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00001585 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00001586 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001587
Douglas Gregor3996e242010-02-15 22:01:00 +00001588 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001589}
1590
Douglas Gregor5c73e912010-02-11 00:48:18 +00001591Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1592 // If this record has a definition in the translation unit we're coming from,
1593 // but this particular declaration is not that definition, import the
1594 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001595 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001596 if (Definition && Definition != D) {
1597 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001598 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00001599 return nullptr;
1600
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001601 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001602 }
1603
1604 // Import the major distinguishing characteristics of this record.
1605 DeclContext *DC, *LexicalDC;
1606 DeclarationName Name;
1607 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001608 NamedDecl *ToD;
1609 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001610 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001611 if (ToD)
1612 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00001613
Douglas Gregor5c73e912010-02-11 00:48:18 +00001614 // Figure out what structure name we're looking for.
1615 unsigned IDNS = Decl::IDNS_Tag;
1616 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00001617 if (!SearchName && D->getTypedefNameForAnonDecl()) {
1618 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001619 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001620 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00001621 IDNS |= Decl::IDNS_Ordinary;
1622
1623 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00001624 RecordDecl *AdoptDecl = nullptr;
Sean Callanan9092d472017-05-13 00:46:33 +00001625 RecordDecl *PrevDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001626 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001627 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001628 SmallVector<NamedDecl *, 2> FoundDecls;
Gabor Horvath5558ba22017-04-03 09:30:20 +00001629 DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls);
Sean Callanan9092d472017-05-13 00:46:33 +00001630
1631 if (!FoundDecls.empty()) {
1632 // We're going to have to compare D against potentially conflicting Decls, so complete it.
1633 if (D->hasExternalLexicalStorage() && !D->isCompleteDefinition())
1634 D->getASTContext().getExternalSource()->CompleteType(D);
1635 }
1636
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001637 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1638 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00001639 continue;
1640
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001641 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00001642 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001643 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1644 Found = Tag->getDecl();
1645 }
1646
1647 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Gabor Horvath3b392bb2017-04-03 21:06:45 +00001648 if (D->isAnonymousStructOrUnion() &&
1649 FoundRecord->isAnonymousStructOrUnion()) {
1650 // If both anonymous structs/unions are in a record context, make sure
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001651 // they occur in the same location in the context records.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001652 if (Optional<unsigned> Index1 =
1653 StructuralEquivalenceContext::findUntaggedStructOrUnionIndex(
1654 D)) {
1655 if (Optional<unsigned> Index2 = StructuralEquivalenceContext::
Sean Callanan488f8612016-07-14 19:53:44 +00001656 findUntaggedStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001657 if (*Index1 != *Index2)
1658 continue;
1659 }
1660 }
1661 }
1662
Sean Callanan9092d472017-05-13 00:46:33 +00001663 PrevDecl = FoundRecord;
1664
Douglas Gregor25791052010-02-12 00:09:27 +00001665 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001666 if ((SearchName && !D->isCompleteDefinition())
1667 || (D->isCompleteDefinition() &&
1668 D->isAnonymousStructOrUnion()
1669 == FoundDef->isAnonymousStructOrUnion() &&
1670 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00001671 // The record types structurally match, or the "from" translation
1672 // unit only had a forward declaration anyway; call it the same
1673 // function.
1674 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001675 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001676 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001677 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00001678 // We have a forward declaration of this type, so adopt that forward
1679 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00001680
1681 // If one or both can be completed from external storage then try one
1682 // last time to complete and compare them before doing this.
1683
1684 if (FoundRecord->hasExternalLexicalStorage() &&
1685 !FoundRecord->isCompleteDefinition())
1686 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
1687 if (D->hasExternalLexicalStorage())
1688 D->getASTContext().getExternalSource()->CompleteType(D);
1689
1690 if (FoundRecord->isCompleteDefinition() &&
1691 D->isCompleteDefinition() &&
1692 !IsStructuralMatch(D, FoundRecord))
1693 continue;
1694
Douglas Gregor25791052010-02-12 00:09:27 +00001695 AdoptDecl = FoundRecord;
1696 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00001697 } else if (!SearchName) {
1698 continue;
1699 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001700 }
1701
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001702 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001703 }
1704
Douglas Gregordd6006f2012-07-17 21:16:27 +00001705 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00001706 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1707 ConflictingDecls.data(),
1708 ConflictingDecls.size());
1709 }
1710 }
1711
1712 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001713 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001714 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00001715 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00001716 CXXRecordDecl *D2CXX = nullptr;
1717 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
1718 if (DCXX->isLambda()) {
1719 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
1720 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
1721 DC, TInfo, Loc,
1722 DCXX->isDependentLambda(),
1723 DCXX->isGenericLambda(),
1724 DCXX->getLambdaCaptureDefault());
1725 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
1726 if (DCXX->getLambdaContextDecl() && !CDecl)
1727 return nullptr;
Sean Callanan041cceb2016-05-14 05:43:57 +00001728 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(), CDecl);
1729 } else if (DCXX->isInjectedClassName()) {
1730 // We have to be careful to do a similar dance to the one in
1731 // Sema::ActOnStartCXXMemberDeclarations
1732 CXXRecordDecl *const PrevDecl = nullptr;
1733 const bool DelayTypeCreation = true;
1734 D2CXX = CXXRecordDecl::Create(
1735 Importer.getToContext(), D->getTagKind(), DC, StartLoc, Loc,
1736 Name.getAsIdentifierInfo(), PrevDecl, DelayTypeCreation);
1737 Importer.getToContext().getTypeDeclType(
1738 D2CXX, llvm::dyn_cast<CXXRecordDecl>(DC));
Sean Callanan8bca9962016-03-28 21:43:01 +00001739 } else {
1740 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
1741 D->getTagKind(),
1742 DC, StartLoc, Loc,
1743 Name.getAsIdentifierInfo());
1744 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001745 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001746 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00001747 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001748 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00001749 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001750 }
Douglas Gregor14454802011-02-25 02:25:35 +00001751
1752 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001753 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001754 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00001755 if (D->isAnonymousStructOrUnion())
1756 D2->setAnonymousStructOrUnion(true);
Sean Callanan9092d472017-05-13 00:46:33 +00001757 if (PrevDecl) {
1758 // FIXME: do this for all Redeclarables, not just RecordDecls.
1759 D2->setPreviousDecl(PrevDecl);
1760 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001761 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001762
Douglas Gregor3996e242010-02-15 22:01:00 +00001763 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001764
Douglas Gregor95d82832012-01-24 18:36:04 +00001765 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00001766 return nullptr;
1767
Douglas Gregor3996e242010-02-15 22:01:00 +00001768 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001769}
1770
Douglas Gregor98c10182010-02-12 22:17:39 +00001771Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1772 // Import the major distinguishing characteristics of this enumerator.
1773 DeclContext *DC, *LexicalDC;
1774 DeclarationName Name;
1775 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001776 NamedDecl *ToD;
1777 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001778 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001779 if (ToD)
1780 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001781
1782 QualType T = Importer.Import(D->getType());
1783 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001784 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001785
Douglas Gregor98c10182010-02-12 22:17:39 +00001786 // Determine whether there are any other declarations with the same name and
1787 // in the same context.
1788 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001789 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00001790 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001791 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001792 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001793 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1794 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00001795 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00001796
1797 if (EnumConstantDecl *FoundEnumConstant
1798 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
1799 if (IsStructuralMatch(D, FoundEnumConstant))
1800 return Importer.Imported(D, FoundEnumConstant);
1801 }
1802
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001803 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00001804 }
1805
1806 if (!ConflictingDecls.empty()) {
1807 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1808 ConflictingDecls.data(),
1809 ConflictingDecls.size());
1810 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001811 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00001812 }
1813 }
1814
1815 Expr *Init = Importer.Import(D->getInitExpr());
1816 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00001817 return nullptr;
1818
Douglas Gregor98c10182010-02-12 22:17:39 +00001819 EnumConstantDecl *ToEnumerator
1820 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1821 Name.getAsIdentifierInfo(), T,
1822 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00001823 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00001824 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001825 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00001826 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001827 return ToEnumerator;
1828}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001829
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001830Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1831 // Import the major distinguishing characteristics of this function.
1832 DeclContext *DC, *LexicalDC;
1833 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001834 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00001835 NamedDecl *ToD;
1836 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00001837 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001838 if (ToD)
1839 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001840
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001841 // Try to find a function in our own ("to") context with the same name, same
1842 // type, and in the same context as the function we're importing.
1843 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001844 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001845 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001846 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00001847 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001848 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
1849 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001850 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001851
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001852 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00001853 if (FoundFunction->hasExternalFormalLinkage() &&
1854 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001855 if (Importer.IsStructurallyEquivalent(D->getType(),
1856 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001857 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001858 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001859 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001860
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001861 // FIXME: Check for overloading more carefully, e.g., by boosting
1862 // Sema::IsOverload out to the AST library.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001863
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001864 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001865 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001866 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001867
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001868 // Complain about inconsistent function types.
1869 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001870 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001871 Importer.ToDiag(FoundFunction->getLocation(),
1872 diag::note_odr_value_here)
1873 << FoundFunction->getType();
1874 }
1875 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001876
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001877 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001878 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00001879
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001880 if (!ConflictingDecls.empty()) {
1881 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1882 ConflictingDecls.data(),
1883 ConflictingDecls.size());
1884 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00001885 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001886 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001887 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001888
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001889 DeclarationNameInfo NameInfo(Name, Loc);
1890 // Import additional name location/type info.
1891 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
1892
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001893 QualType FromTy = D->getType();
1894 bool usedDifferentExceptionSpec = false;
1895
1896 if (const FunctionProtoType *
1897 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
1898 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
1899 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
1900 // FunctionDecl that we are importing the FunctionProtoType for.
1901 // To avoid an infinite recursion when importing, create the FunctionDecl
1902 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00001903 if (FromEPI.ExceptionSpec.SourceDecl ||
1904 FromEPI.ExceptionSpec.SourceTemplate ||
1905 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001906 FunctionProtoType::ExtProtoInfo DefaultEPI;
1907 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00001908 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001909 usedDifferentExceptionSpec = true;
1910 }
1911 }
1912
Douglas Gregorb4964f72010-02-15 23:54:17 +00001913 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001914 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001915 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00001916 return nullptr;
1917
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001918 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001919 SmallVector<ParmVarDecl *, 8> Parameters;
David Majnemer59f77922016-06-24 04:05:48 +00001920 for (auto P : D->parameters()) {
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001921 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001922 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00001923 return nullptr;
1924
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001925 Parameters.push_back(ToP);
1926 }
1927
1928 // Create the imported function.
1929 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00001930 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00001931 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00001932 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1933 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1934 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001935 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001936 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001937 FromConstructor->isExplicit(),
1938 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001939 D->isImplicit(),
1940 D->isConstexpr());
Sean Callanandd2c1742016-05-16 20:48:03 +00001941 if (unsigned NumInitializers = FromConstructor->getNumCtorInitializers()) {
1942 SmallVector<CXXCtorInitializer *, 4> CtorInitializers;
1943 for (CXXCtorInitializer *I : FromConstructor->inits()) {
1944 CXXCtorInitializer *ToI =
1945 cast_or_null<CXXCtorInitializer>(Importer.Import(I));
1946 if (!ToI && I)
1947 return nullptr;
1948 CtorInitializers.push_back(ToI);
1949 }
1950 CXXCtorInitializer **Memory =
1951 new (Importer.getToContext()) CXXCtorInitializer *[NumInitializers];
1952 std::copy(CtorInitializers.begin(), CtorInitializers.end(), Memory);
1953 CXXConstructorDecl *ToCtor = llvm::cast<CXXConstructorDecl>(ToFunction);
1954 ToCtor->setCtorInitializers(Memory);
1955 ToCtor->setNumCtorInitializers(NumInitializers);
1956 }
Douglas Gregor00eace12010-02-21 18:29:16 +00001957 } else if (isa<CXXDestructorDecl>(D)) {
1958 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1959 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001960 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001961 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001962 D->isInlineSpecified(),
1963 D->isImplicit());
1964 } else if (CXXConversionDecl *FromConversion
1965 = dyn_cast<CXXConversionDecl>(D)) {
1966 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1967 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001968 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001969 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001970 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001971 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001972 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001973 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00001974 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
1975 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
1976 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00001977 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00001978 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001979 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001980 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001981 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00001982 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00001983 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001984 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00001985 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001986 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00001987 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00001988 D->hasWrittenPrototype(),
1989 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00001990 }
John McCall3e11ebe2010-03-15 10:12:16 +00001991
1992 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00001993 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00001994 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00001995 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00001996 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
1997 ToFunction->setTrivial(D->isTrivial());
1998 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00001999 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002000
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002001 // Set the parameters.
2002 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002003 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002004 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002005 }
David Blaikie9c70e042011-09-21 18:16:56 +00002006 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002007
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002008 if (usedDifferentExceptionSpec) {
2009 // Update FunctionProtoType::ExtProtoInfo.
2010 QualType T = Importer.Import(D->getType());
2011 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002012 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002013 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002014 }
2015
Sean Callanan59721b32015-04-28 18:41:46 +00002016 // Import the body, if any.
2017 if (Stmt *FromBody = D->getBody()) {
2018 if (Stmt *ToBody = Importer.Import(FromBody)) {
2019 ToFunction->setBody(ToBody);
2020 }
2021 }
2022
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002023 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002024
2025 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002026 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002027
Douglas Gregor43f54792010-02-17 02:12:47 +00002028 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002029}
2030
Douglas Gregor00eace12010-02-21 18:29:16 +00002031Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2032 return VisitFunctionDecl(D);
2033}
2034
2035Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2036 return VisitCXXMethodDecl(D);
2037}
2038
2039Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2040 return VisitCXXMethodDecl(D);
2041}
2042
2043Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2044 return VisitCXXMethodDecl(D);
2045}
2046
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002047static unsigned getFieldIndex(Decl *F) {
2048 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2049 if (!Owner)
2050 return 0;
2051
2052 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002053 for (const auto *D : Owner->noload_decls()) {
2054 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002055 return Index;
2056
2057 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2058 ++Index;
2059 }
2060
2061 return Index;
2062}
2063
Douglas Gregor5c73e912010-02-11 00:48:18 +00002064Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2065 // Import the major distinguishing characteristics of a variable.
2066 DeclContext *DC, *LexicalDC;
2067 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002068 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002069 NamedDecl *ToD;
2070 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002071 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002072 if (ToD)
2073 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002074
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002075 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002076 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002077 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002078 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2079 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002080 // For anonymous fields, match up by index.
2081 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2082 continue;
2083
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002084 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002085 FoundField->getType())) {
2086 Importer.Imported(D, FoundField);
2087 return FoundField;
2088 }
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002089
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002090 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2091 << Name << D->getType() << FoundField->getType();
2092 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2093 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002094 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002095 }
2096 }
2097
Douglas Gregorb4964f72010-02-15 23:54:17 +00002098 // Import the type.
2099 QualType T = Importer.Import(D->getType());
2100 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002101 return nullptr;
2102
Douglas Gregor5c73e912010-02-11 00:48:18 +00002103 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2104 Expr *BitWidth = Importer.Import(D->getBitWidth());
2105 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002106 return nullptr;
2107
Abramo Bagnaradff19302011-03-08 08:55:46 +00002108 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2109 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002110 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002111 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002112 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002113 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002114 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00002115 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00002116 Expr *ToInitializer = Importer.Import(FromInitializer);
2117 if (ToInitializer)
2118 ToField->setInClassInitializer(ToInitializer);
2119 else
2120 return nullptr;
2121 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002122 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002123 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002124 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002125 return ToField;
2126}
2127
Francois Pichet783dd6e2010-11-21 06:08:52 +00002128Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2129 // Import the major distinguishing characteristics of a variable.
2130 DeclContext *DC, *LexicalDC;
2131 DeclarationName Name;
2132 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002133 NamedDecl *ToD;
2134 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002135 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002136 if (ToD)
2137 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002138
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002139 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002140 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002141 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002142 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002143 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002144 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002145 // For anonymous indirect fields, match up by index.
2146 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2147 continue;
2148
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002149 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002150 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002151 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002152 Importer.Imported(D, FoundField);
2153 return FoundField;
2154 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002155
2156 // If there are more anonymous fields to check, continue.
2157 if (!Name && I < N-1)
2158 continue;
2159
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002160 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2161 << Name << D->getType() << FoundField->getType();
2162 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2163 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002164 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002165 }
2166 }
2167
Francois Pichet783dd6e2010-11-21 06:08:52 +00002168 // Import the type.
2169 QualType T = Importer.Import(D->getType());
2170 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002171 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002172
2173 NamedDecl **NamedChain =
2174 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2175
2176 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002177 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002178 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002179 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002180 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002181 NamedChain[i++] = cast<NamedDecl>(D);
2182 }
2183
2184 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00002185 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
David Majnemer59f77922016-06-24 04:05:48 +00002186 {NamedChain, D->getChainingSize()});
Aaron Ballman260995b2014-10-15 16:58:18 +00002187
2188 for (const auto *Attr : D->attrs())
2189 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2190
Francois Pichet783dd6e2010-11-21 06:08:52 +00002191 ToIndirectField->setAccess(D->getAccess());
2192 ToIndirectField->setLexicalDeclContext(LexicalDC);
2193 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002194 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002195 return ToIndirectField;
2196}
2197
Aleksei Sidorina693b372016-09-28 10:16:56 +00002198Decl *ASTNodeImporter::VisitFriendDecl(FriendDecl *D) {
2199 // Import the major distinguishing characteristics of a declaration.
2200 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2201 DeclContext *LexicalDC = D->getDeclContext() == D->getLexicalDeclContext()
2202 ? DC : Importer.ImportContext(D->getLexicalDeclContext());
2203 if (!DC || !LexicalDC)
2204 return nullptr;
2205
2206 // Determine whether we've already imported this decl.
2207 // FriendDecl is not a NamedDecl so we cannot use localUncachedLookup.
2208 auto *RD = cast<CXXRecordDecl>(DC);
2209 FriendDecl *ImportedFriend = RD->getFirstFriend();
2210 StructuralEquivalenceContext Context(
2211 Importer.getFromContext(), Importer.getToContext(),
2212 Importer.getNonEquivalentDecls(), false, false);
2213
2214 while (ImportedFriend) {
2215 if (D->getFriendDecl() && ImportedFriend->getFriendDecl()) {
2216 if (Context.IsStructurallyEquivalent(D->getFriendDecl(),
2217 ImportedFriend->getFriendDecl()))
2218 return Importer.Imported(D, ImportedFriend);
2219
2220 } else if (D->getFriendType() && ImportedFriend->getFriendType()) {
2221 if (Importer.IsStructurallyEquivalent(
2222 D->getFriendType()->getType(),
2223 ImportedFriend->getFriendType()->getType(), true))
2224 return Importer.Imported(D, ImportedFriend);
2225 }
2226 ImportedFriend = ImportedFriend->getNextFriend();
2227 }
2228
2229 // Not found. Create it.
2230 FriendDecl::FriendUnion ToFU;
2231 if (NamedDecl *FriendD = D->getFriendDecl())
2232 ToFU = cast_or_null<NamedDecl>(Importer.Import(FriendD));
2233 else
2234 ToFU = Importer.Import(D->getFriendType());
2235 if (!ToFU)
2236 return nullptr;
2237
2238 SmallVector<TemplateParameterList *, 1> ToTPLists(D->NumTPLists);
2239 TemplateParameterList **FromTPLists =
2240 D->getTrailingObjects<TemplateParameterList *>();
2241 for (unsigned I = 0; I < D->NumTPLists; I++) {
2242 TemplateParameterList *List = ImportTemplateParameterList(FromTPLists[I]);
2243 if (!List)
2244 return nullptr;
2245 ToTPLists[I] = List;
2246 }
2247
2248 FriendDecl *FrD = FriendDecl::Create(Importer.getToContext(), DC,
2249 Importer.Import(D->getLocation()),
2250 ToFU, Importer.Import(D->getFriendLoc()),
2251 ToTPLists);
2252
2253 Importer.Imported(D, FrD);
2254 RD->pushFriendDecl(FrD);
2255
2256 FrD->setAccess(D->getAccess());
2257 FrD->setLexicalDeclContext(LexicalDC);
2258 LexicalDC->addDeclInternal(FrD);
2259 return FrD;
2260}
2261
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002262Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2263 // Import the major distinguishing characteristics of an ivar.
2264 DeclContext *DC, *LexicalDC;
2265 DeclarationName Name;
2266 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002267 NamedDecl *ToD;
2268 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002269 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002270 if (ToD)
2271 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002272
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002273 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002274 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002275 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002276 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2277 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002278 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002279 FoundIvar->getType())) {
2280 Importer.Imported(D, FoundIvar);
2281 return FoundIvar;
2282 }
2283
2284 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2285 << Name << D->getType() << FoundIvar->getType();
2286 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2287 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002288 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002289 }
2290 }
2291
2292 // Import the type.
2293 QualType T = Importer.Import(D->getType());
2294 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002295 return nullptr;
2296
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002297 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2298 Expr *BitWidth = Importer.Import(D->getBitWidth());
2299 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002300 return nullptr;
2301
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002302 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2303 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002304 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002305 Loc, Name.getAsIdentifierInfo(),
2306 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002307 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002308 ToIvar->setLexicalDeclContext(LexicalDC);
2309 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002310 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002311 return ToIvar;
2312
2313}
2314
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002315Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2316 // Import the major distinguishing characteristics of a variable.
2317 DeclContext *DC, *LexicalDC;
2318 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002319 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002320 NamedDecl *ToD;
2321 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002322 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002323 if (ToD)
2324 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002325
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002326 // Try to find a variable in our own ("to") context with the same name and
2327 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002328 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00002329 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002330 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002331 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002332 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002333 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002334 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2335 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002336 continue;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002337
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002338 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002339 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002340 if (FoundVar->hasExternalFormalLinkage() &&
2341 D->hasExternalFormalLinkage()) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002342 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002343 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002344 MergeWithVar = FoundVar;
2345 break;
2346 }
2347
Douglas Gregor56521c52010-02-12 17:23:39 +00002348 const ArrayType *FoundArray
2349 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2350 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002351 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002352 if (FoundArray && TArray) {
2353 if (isa<IncompleteArrayType>(FoundArray) &&
2354 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002355 // Import the type.
2356 QualType T = Importer.Import(D->getType());
2357 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002358 return nullptr;
2359
Douglas Gregor56521c52010-02-12 17:23:39 +00002360 FoundVar->setType(T);
2361 MergeWithVar = FoundVar;
2362 break;
2363 } else if (isa<IncompleteArrayType>(TArray) &&
2364 isa<ConstantArrayType>(FoundArray)) {
2365 MergeWithVar = FoundVar;
2366 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002367 }
2368 }
2369
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002370 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002371 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002372 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2373 << FoundVar->getType();
2374 }
2375 }
2376
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002377 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002378 }
2379
2380 if (MergeWithVar) {
2381 // An equivalent variable with external linkage has been found. Link
2382 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002383 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002384
2385 if (VarDecl *DDef = D->getDefinition()) {
2386 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2387 Importer.ToDiag(ExistingDef->getLocation(),
2388 diag::err_odr_variable_multiple_def)
2389 << Name;
2390 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2391 } else {
2392 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002393 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002394 if (DDef->isInitKnownICE()) {
2395 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2396 Eval->CheckedICE = true;
2397 Eval->IsICE = DDef->isInitICE();
2398 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002399 }
2400 }
2401
2402 return MergeWithVar;
2403 }
2404
2405 if (!ConflictingDecls.empty()) {
2406 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2407 ConflictingDecls.data(),
2408 ConflictingDecls.size());
2409 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002410 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002411 }
2412 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002413
Douglas Gregorb4964f72010-02-15 23:54:17 +00002414 // Import the type.
2415 QualType T = Importer.Import(D->getType());
2416 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002417 return nullptr;
2418
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002419 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002420 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002421 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2422 Importer.Import(D->getInnerLocStart()),
2423 Loc, Name.getAsIdentifierInfo(),
2424 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002425 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00002426 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002427 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002428 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002429 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002430 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002431
Sean Callanan59721b32015-04-28 18:41:46 +00002432 if (!D->isFileVarDecl() &&
2433 D->isUsed())
2434 ToVar->setIsUsed();
2435
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002436 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002437 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00002438 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002439
Aleksei Sidorin855086d2017-01-23 09:30:36 +00002440 if (D->isConstexpr())
2441 ToVar->setConstexpr(true);
2442
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002443 return ToVar;
2444}
2445
Douglas Gregor8b228d72010-02-17 21:22:52 +00002446Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2447 // Parameters are created in the translation unit's context, then moved
2448 // into the function declaration's context afterward.
2449 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2450
2451 // Import the name of this declaration.
2452 DeclarationName Name = Importer.Import(D->getDeclName());
2453 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002454 return nullptr;
2455
Douglas Gregor8b228d72010-02-17 21:22:52 +00002456 // Import the location of this declaration.
2457 SourceLocation Loc = Importer.Import(D->getLocation());
2458
2459 // Import the parameter's type.
2460 QualType T = Importer.Import(D->getType());
2461 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002462 return nullptr;
2463
Douglas Gregor8b228d72010-02-17 21:22:52 +00002464 // Create the imported parameter.
2465 ImplicitParamDecl *ToParm
2466 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2467 Loc, Name.getAsIdentifierInfo(),
2468 T);
2469 return Importer.Imported(D, ToParm);
2470}
2471
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002472Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *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
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002477 // 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 Gregorbb7930c2010-02-10 19:54:31 +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 Gregorbb7930c2010-02-10 19:54:31 +00002490 // Create the imported parameter.
2491 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2492 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002493 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002494 Loc, Name.getAsIdentifierInfo(),
2495 T, TInfo, D->getStorageClass(),
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002496 /*DefaultArg*/ nullptr);
2497
2498 // Set the default argument.
John McCallf3cd6652010-03-12 18:31:32 +00002499 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Aleksei Sidorin55a63502017-02-20 11:57:12 +00002500 ToParm->setKNRPromoted(D->isKNRPromoted());
2501
2502 Expr *ToDefArg = nullptr;
2503 Expr *FromDefArg = nullptr;
2504 if (D->hasUninstantiatedDefaultArg()) {
2505 FromDefArg = D->getUninstantiatedDefaultArg();
2506 ToDefArg = Importer.Import(FromDefArg);
2507 ToParm->setUninstantiatedDefaultArg(ToDefArg);
2508 } else if (D->hasUnparsedDefaultArg()) {
2509 ToParm->setUnparsedDefaultArg();
2510 } else if (D->hasDefaultArg()) {
2511 FromDefArg = D->getDefaultArg();
2512 ToDefArg = Importer.Import(FromDefArg);
2513 ToParm->setDefaultArg(ToDefArg);
2514 }
2515 if (FromDefArg && !ToDefArg)
2516 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002517
2518 if (D->isUsed())
2519 ToParm->setIsUsed();
2520
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002521 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002522}
2523
Douglas Gregor43f54792010-02-17 02:12:47 +00002524Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2525 // Import the major distinguishing characteristics of a method.
2526 DeclContext *DC, *LexicalDC;
2527 DeclarationName Name;
2528 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002529 NamedDecl *ToD;
2530 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002531 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002532 if (ToD)
2533 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002534
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002535 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002536 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002537 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2538 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002539 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2540 continue;
2541
2542 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00002543 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
2544 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002545 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00002546 << D->isInstanceMethod() << Name << D->getReturnType()
2547 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00002548 Importer.ToDiag(FoundMethod->getLocation(),
2549 diag::note_odr_objc_method_here)
2550 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002551 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002552 }
2553
2554 // Check the number of parameters.
2555 if (D->param_size() != FoundMethod->param_size()) {
2556 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2557 << D->isInstanceMethod() << Name
2558 << D->param_size() << FoundMethod->param_size();
2559 Importer.ToDiag(FoundMethod->getLocation(),
2560 diag::note_odr_objc_method_here)
2561 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002562 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002563 }
2564
2565 // Check parameter types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002566 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002567 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2568 P != PEnd; ++P, ++FoundP) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002569 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002570 (*FoundP)->getType())) {
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00002571 Importer.FromDiag((*P)->getLocation(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002572 diag::err_odr_objc_method_param_type_inconsistent)
2573 << D->isInstanceMethod() << Name
2574 << (*P)->getType() << (*FoundP)->getType();
2575 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2576 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002577 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002578 }
2579 }
2580
2581 // Check variadic/non-variadic.
2582 // Check the number of parameters.
2583 if (D->isVariadic() != FoundMethod->isVariadic()) {
2584 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2585 << D->isInstanceMethod() << Name;
2586 Importer.ToDiag(FoundMethod->getLocation(),
2587 diag::note_odr_objc_method_here)
2588 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00002589 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002590 }
2591
2592 // FIXME: Any other bits we need to merge?
2593 return Importer.Imported(D, FoundMethod);
2594 }
2595 }
2596
2597 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00002598 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002599 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002600 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00002601
Alp Toker314cc812014-01-25 16:55:45 +00002602 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00002603
Alp Toker314cc812014-01-25 16:55:45 +00002604 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
2605 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
2606 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
2607 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
2608 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00002609
2610 // FIXME: When we decide to merge method definitions, we'll need to
2611 // deal with implicit parameters.
2612
2613 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002614 SmallVector<ParmVarDecl *, 5> ToParams;
David Majnemer59f77922016-06-24 04:05:48 +00002615 for (auto *FromP : D->parameters()) {
Aaron Ballman43b68be2014-03-07 17:50:17 +00002616 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00002617 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002618 return nullptr;
2619
Douglas Gregor43f54792010-02-17 02:12:47 +00002620 ToParams.push_back(ToP);
2621 }
2622
2623 // Set the parameters.
2624 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2625 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002626 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00002627 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002628 SmallVector<SourceLocation, 12> SelLocs;
2629 D->getSelectorLocs(SelLocs);
2630 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00002631
2632 ToMethod->setLexicalDeclContext(LexicalDC);
2633 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00002634 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00002635 return ToMethod;
2636}
2637
Douglas Gregor85f3f952015-07-07 03:57:15 +00002638Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
2639 // Import the major distinguishing characteristics of a category.
2640 DeclContext *DC, *LexicalDC;
2641 DeclarationName Name;
2642 SourceLocation Loc;
2643 NamedDecl *ToD;
2644 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2645 return nullptr;
2646 if (ToD)
2647 return ToD;
2648
2649 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
2650 if (!BoundInfo)
2651 return nullptr;
2652
2653 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
2654 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00002655 D->getVariance(),
2656 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00002657 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00002658 Importer.Import(D->getLocation()),
2659 Name.getAsIdentifierInfo(),
2660 Importer.Import(D->getColonLoc()),
2661 BoundInfo);
2662 Importer.Imported(D, Result);
2663 Result->setLexicalDeclContext(LexicalDC);
2664 return Result;
2665}
2666
Douglas Gregor84c51c32010-02-18 01:47:50 +00002667Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2668 // Import the major distinguishing characteristics of a category.
2669 DeclContext *DC, *LexicalDC;
2670 DeclarationName Name;
2671 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002672 NamedDecl *ToD;
2673 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002674 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002675 if (ToD)
2676 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002677
Douglas Gregor84c51c32010-02-18 01:47:50 +00002678 ObjCInterfaceDecl *ToInterface
2679 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2680 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00002681 return nullptr;
2682
Douglas Gregor84c51c32010-02-18 01:47:50 +00002683 // Determine if we've already encountered this category.
2684 ObjCCategoryDecl *MergeWithCategory
2685 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2686 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2687 if (!ToCategory) {
2688 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00002689 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00002690 Loc,
2691 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00002692 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002693 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002694 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00002695 Importer.Import(D->getIvarLBraceLoc()),
2696 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002697 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002698 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002699 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00002700 // Import the type parameter list after calling Imported, to avoid
2701 // loops when bringing in their DeclContext.
2702 ToCategory->setTypeParamList(ImportObjCTypeParamList(
2703 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002704
Douglas Gregor84c51c32010-02-18 01:47:50 +00002705 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002706 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2707 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002708 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2709 = D->protocol_loc_begin();
2710 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2711 FromProtoEnd = D->protocol_end();
2712 FromProto != FromProtoEnd;
2713 ++FromProto, ++FromProtoLoc) {
2714 ObjCProtocolDecl *ToProto
2715 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2716 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00002717 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00002718 Protocols.push_back(ToProto);
2719 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2720 }
2721
2722 // FIXME: If we're merging, make sure that the protocol list is the same.
2723 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2724 ProtocolLocs.data(), Importer.getToContext());
2725
2726 } else {
2727 Importer.Imported(D, ToCategory);
2728 }
2729
2730 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002731 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002732
2733 // If we have an implementation, import it as well.
2734 if (D->getImplementation()) {
2735 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002736 = cast_or_null<ObjCCategoryImplDecl>(
2737 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002738 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00002739 return nullptr;
2740
Douglas Gregor84c51c32010-02-18 01:47:50 +00002741 ToCategory->setImplementation(Impl);
2742 }
2743
2744 return ToCategory;
2745}
2746
Douglas Gregor2aa53772012-01-24 17:42:07 +00002747bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
2748 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002749 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002750 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00002751 if (shouldForceImportDeclContext(Kind))
2752 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002753 return false;
2754 }
2755
2756 // Start the protocol definition
2757 To->startDefinition();
2758
2759 // Import protocols
2760 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2761 SmallVector<SourceLocation, 4> ProtocolLocs;
2762 ObjCProtocolDecl::protocol_loc_iterator
2763 FromProtoLoc = From->protocol_loc_begin();
2764 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
2765 FromProtoEnd = From->protocol_end();
2766 FromProto != FromProtoEnd;
2767 ++FromProto, ++FromProtoLoc) {
2768 ObjCProtocolDecl *ToProto
2769 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2770 if (!ToProto)
2771 return true;
2772 Protocols.push_back(ToProto);
2773 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2774 }
2775
2776 // FIXME: If we're merging, make sure that the protocol list is the same.
2777 To->setProtocolList(Protocols.data(), Protocols.size(),
2778 ProtocolLocs.data(), Importer.getToContext());
2779
Douglas Gregor2e15c842012-02-01 21:00:38 +00002780 if (shouldForceImportDeclContext(Kind)) {
2781 // Import all of the members of this protocol.
2782 ImportDeclContext(From, /*ForceImport=*/true);
2783 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002784 return false;
2785}
2786
Douglas Gregor98d156a2010-02-17 16:12:00 +00002787Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002788 // If this protocol has a definition in the translation unit we're coming
2789 // from, but this particular declaration is not that definition, import the
2790 // definition and map to that.
2791 ObjCProtocolDecl *Definition = D->getDefinition();
2792 if (Definition && Definition != D) {
2793 Decl *ImportedDef = Importer.Import(Definition);
2794 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002795 return nullptr;
2796
Douglas Gregor2aa53772012-01-24 17:42:07 +00002797 return Importer.Imported(D, ImportedDef);
2798 }
2799
Douglas Gregor84c51c32010-02-18 01:47:50 +00002800 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002801 DeclContext *DC, *LexicalDC;
2802 DeclarationName Name;
2803 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002804 NamedDecl *ToD;
2805 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002806 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002807 if (ToD)
2808 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00002809
Craig Topper36250ad2014-05-12 05:36:57 +00002810 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002811 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002812 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002813 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2814 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00002815 continue;
2816
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002817 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00002818 break;
2819 }
2820
2821 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00002822 if (!ToProto) {
2823 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
2824 Name.getAsIdentifierInfo(), Loc,
2825 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00002826 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002827 ToProto->setLexicalDeclContext(LexicalDC);
2828 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002829 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002830
2831 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002832
Douglas Gregor2aa53772012-01-24 17:42:07 +00002833 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00002834 return nullptr;
2835
Douglas Gregor98d156a2010-02-17 16:12:00 +00002836 return ToProto;
2837}
2838
Sean Callanan0aae0412014-12-10 00:00:37 +00002839Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
2840 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2841 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2842
2843 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
2844 SourceLocation LangLoc = Importer.Import(D->getLocation());
2845
2846 bool HasBraces = D->hasBraces();
2847
Sean Callananb12a8552014-12-10 21:22:20 +00002848 LinkageSpecDecl *ToLinkageSpec =
2849 LinkageSpecDecl::Create(Importer.getToContext(),
2850 DC,
2851 ExternLoc,
2852 LangLoc,
2853 D->getLanguage(),
2854 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00002855
2856 if (HasBraces) {
2857 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
2858 ToLinkageSpec->setRBraceLoc(RBraceLoc);
2859 }
2860
2861 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
2862 LexicalDC->addDeclInternal(ToLinkageSpec);
2863
2864 Importer.Imported(D, ToLinkageSpec);
2865
2866 return ToLinkageSpec;
2867}
2868
Douglas Gregor2aa53772012-01-24 17:42:07 +00002869bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
2870 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002871 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002872 if (To->getDefinition()) {
2873 // Check consistency of superclass.
2874 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
2875 if (FromSuper) {
2876 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
2877 if (!FromSuper)
2878 return true;
2879 }
2880
2881 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
2882 if ((bool)FromSuper != (bool)ToSuper ||
2883 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
2884 Importer.ToDiag(To->getLocation(),
2885 diag::err_odr_objc_superclass_inconsistent)
2886 << To->getDeclName();
2887 if (ToSuper)
2888 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
2889 << To->getSuperClass()->getDeclName();
2890 else
2891 Importer.ToDiag(To->getLocation(),
2892 diag::note_odr_objc_missing_superclass);
2893 if (From->getSuperClass())
2894 Importer.FromDiag(From->getSuperClassLoc(),
2895 diag::note_odr_objc_superclass)
2896 << From->getSuperClass()->getDeclName();
2897 else
2898 Importer.FromDiag(From->getLocation(),
2899 diag::note_odr_objc_missing_superclass);
2900 }
2901
Douglas Gregor2e15c842012-02-01 21:00:38 +00002902 if (shouldForceImportDeclContext(Kind))
2903 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002904 return false;
2905 }
2906
2907 // Start the definition.
2908 To->startDefinition();
2909
2910 // If this class has a superclass, import it.
2911 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00002912 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
2913 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00002914 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00002915
2916 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00002917 }
2918
2919 // Import protocols
2920 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2921 SmallVector<SourceLocation, 4> ProtocolLocs;
2922 ObjCInterfaceDecl::protocol_loc_iterator
2923 FromProtoLoc = From->protocol_loc_begin();
2924
2925 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
2926 FromProtoEnd = From->protocol_end();
2927 FromProto != FromProtoEnd;
2928 ++FromProto, ++FromProtoLoc) {
2929 ObjCProtocolDecl *ToProto
2930 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2931 if (!ToProto)
2932 return true;
2933 Protocols.push_back(ToProto);
2934 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2935 }
2936
2937 // FIXME: If we're merging, make sure that the protocol list is the same.
2938 To->setProtocolList(Protocols.data(), Protocols.size(),
2939 ProtocolLocs.data(), Importer.getToContext());
2940
2941 // Import categories. When the categories themselves are imported, they'll
2942 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00002943 for (auto *Cat : From->known_categories())
2944 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002945
Douglas Gregor2aa53772012-01-24 17:42:07 +00002946 // If we have an @implementation, import it as well.
2947 if (From->getImplementation()) {
2948 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
2949 Importer.Import(From->getImplementation()));
2950 if (!Impl)
2951 return true;
2952
2953 To->setImplementation(Impl);
2954 }
2955
Douglas Gregor2e15c842012-02-01 21:00:38 +00002956 if (shouldForceImportDeclContext(Kind)) {
2957 // Import all of the members of this class.
2958 ImportDeclContext(From, /*ForceImport=*/true);
2959 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00002960 return false;
2961}
2962
Douglas Gregor85f3f952015-07-07 03:57:15 +00002963ObjCTypeParamList *
2964ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
2965 if (!list)
2966 return nullptr;
2967
2968 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
2969 for (auto fromTypeParam : *list) {
2970 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
2971 Importer.Import(fromTypeParam));
2972 if (!toTypeParam)
2973 return nullptr;
2974
2975 toTypeParams.push_back(toTypeParam);
2976 }
2977
2978 return ObjCTypeParamList::create(Importer.getToContext(),
2979 Importer.Import(list->getLAngleLoc()),
2980 toTypeParams,
2981 Importer.Import(list->getRAngleLoc()));
2982}
2983
Douglas Gregor45635322010-02-16 01:20:57 +00002984Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00002985 // If this class has a definition in the translation unit we're coming from,
2986 // but this particular declaration is not that definition, import the
2987 // definition and map to that.
2988 ObjCInterfaceDecl *Definition = D->getDefinition();
2989 if (Definition && Definition != D) {
2990 Decl *ImportedDef = Importer.Import(Definition);
2991 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002992 return nullptr;
2993
Douglas Gregor2aa53772012-01-24 17:42:07 +00002994 return Importer.Imported(D, ImportedDef);
2995 }
2996
Douglas Gregor45635322010-02-16 01:20:57 +00002997 // Import the major distinguishing characteristics of an @interface.
2998 DeclContext *DC, *LexicalDC;
2999 DeclarationName Name;
3000 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003001 NamedDecl *ToD;
3002 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003003 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003004 if (ToD)
3005 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003006
Douglas Gregor2aa53772012-01-24 17:42:07 +00003007 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003008 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003009 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003010 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003011 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3012 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003013 continue;
3014
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003015 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003016 break;
3017 }
3018
Douglas Gregor2aa53772012-01-24 17:42:07 +00003019 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003020 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003021 if (!ToIface) {
3022 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3023 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003024 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003025 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003026 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003027 D->isImplicitInterfaceDecl());
3028 ToIface->setLexicalDeclContext(LexicalDC);
3029 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003030 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003031 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003032 // Import the type parameter list after calling Imported, to avoid
3033 // loops when bringing in their DeclContext.
3034 ToIface->setTypeParamList(ImportObjCTypeParamList(
3035 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003036
Douglas Gregor2aa53772012-01-24 17:42:07 +00003037 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003038 return nullptr;
3039
Douglas Gregor98d156a2010-02-17 16:12:00 +00003040 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003041}
3042
Douglas Gregor4da9d682010-12-07 15:32:12 +00003043Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3044 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3045 Importer.Import(D->getCategoryDecl()));
3046 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003047 return nullptr;
3048
Douglas Gregor4da9d682010-12-07 15:32:12 +00003049 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3050 if (!ToImpl) {
3051 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3052 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003053 return nullptr;
3054
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003055 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003056 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003057 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003058 Category->getClassInterface(),
3059 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003060 Importer.Import(D->getAtStartLoc()),
3061 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003062
3063 DeclContext *LexicalDC = DC;
3064 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3065 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3066 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003067 return nullptr;
3068
Douglas Gregor4da9d682010-12-07 15:32:12 +00003069 ToImpl->setLexicalDeclContext(LexicalDC);
3070 }
3071
Sean Callanan95e74be2011-10-21 02:57:43 +00003072 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003073 Category->setImplementation(ToImpl);
3074 }
3075
3076 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003077 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003078 return ToImpl;
3079}
3080
Douglas Gregorda8025c2010-12-07 01:26:03 +00003081Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3082 // Find the corresponding interface.
3083 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3084 Importer.Import(D->getClassInterface()));
3085 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003086 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003087
3088 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003089 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003090 if (D->getSuperClass()) {
3091 Super = cast_or_null<ObjCInterfaceDecl>(
3092 Importer.Import(D->getSuperClass()));
3093 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003094 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003095 }
3096
3097 ObjCImplementationDecl *Impl = Iface->getImplementation();
3098 if (!Impl) {
3099 // We haven't imported an implementation yet. Create a new @implementation
3100 // now.
3101 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3102 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003103 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003104 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003105 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003106 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003107 Importer.Import(D->getIvarLBraceLoc()),
3108 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003109
3110 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3111 DeclContext *LexicalDC
3112 = Importer.ImportContext(D->getLexicalDeclContext());
3113 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003114 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003115 Impl->setLexicalDeclContext(LexicalDC);
3116 }
3117
3118 // Associate the implementation with the class it implements.
3119 Iface->setImplementation(Impl);
3120 Importer.Imported(D, Iface->getImplementation());
3121 } else {
3122 Importer.Imported(D, Iface->getImplementation());
3123
3124 // Verify that the existing @implementation has the same superclass.
3125 if ((Super && !Impl->getSuperClass()) ||
3126 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003127 (Super && Impl->getSuperClass() &&
3128 !declaresSameEntity(Super->getCanonicalDecl(),
3129 Impl->getSuperClass()))) {
3130 Importer.ToDiag(Impl->getLocation(),
3131 diag::err_odr_objc_superclass_inconsistent)
3132 << Iface->getDeclName();
3133 // FIXME: It would be nice to have the location of the superclass
3134 // below.
3135 if (Impl->getSuperClass())
3136 Importer.ToDiag(Impl->getLocation(),
3137 diag::note_odr_objc_superclass)
3138 << Impl->getSuperClass()->getDeclName();
3139 else
3140 Importer.ToDiag(Impl->getLocation(),
3141 diag::note_odr_objc_missing_superclass);
3142 if (D->getSuperClass())
3143 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003144 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003145 << D->getSuperClass()->getDeclName();
3146 else
3147 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003148 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003149 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003150 }
3151 }
3152
3153 // Import all of the members of this @implementation.
3154 ImportDeclContext(D);
3155
3156 return Impl;
3157}
3158
Douglas Gregora11c4582010-02-17 18:02:10 +00003159Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3160 // Import the major distinguishing characteristics of an @property.
3161 DeclContext *DC, *LexicalDC;
3162 DeclarationName Name;
3163 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003164 NamedDecl *ToD;
3165 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003166 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003167 if (ToD)
3168 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003169
3170 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003171 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003172 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003173 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003174 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003175 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003176 // Check property types.
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00003177 if (!Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregora11c4582010-02-17 18:02:10 +00003178 FoundProp->getType())) {
3179 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3180 << Name << D->getType() << FoundProp->getType();
3181 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3182 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003183 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003184 }
3185
3186 // FIXME: Check property attributes, getters, setters, etc.?
3187
3188 // Consider these properties to be equivalent.
3189 Importer.Imported(D, FoundProp);
3190 return FoundProp;
3191 }
3192 }
3193
3194 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003195 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3196 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003197 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003198
3199 // Create the new property.
3200 ObjCPropertyDecl *ToProperty
3201 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3202 Name.getAsIdentifierInfo(),
3203 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003204 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003205 Importer.Import(D->getType()),
3206 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003207 D->getPropertyImplementation());
3208 Importer.Imported(D, ToProperty);
3209 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003210 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003211
3212 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003213 ToProperty->setPropertyAttributesAsWritten(
3214 D->getPropertyAttributesAsWritten());
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +00003215 ToProperty->setGetterName(Importer.Import(D->getGetterName()),
3216 Importer.Import(D->getGetterNameLoc()));
3217 ToProperty->setSetterName(Importer.Import(D->getSetterName()),
3218 Importer.Import(D->getSetterNameLoc()));
Douglas Gregora11c4582010-02-17 18:02:10 +00003219 ToProperty->setGetterMethodDecl(
3220 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3221 ToProperty->setSetterMethodDecl(
3222 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3223 ToProperty->setPropertyIvarDecl(
3224 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3225 return ToProperty;
3226}
3227
Douglas Gregor14a49e22010-12-07 18:32:03 +00003228Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3229 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3230 Importer.Import(D->getPropertyDecl()));
3231 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003232 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003233
3234 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3235 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003236 return nullptr;
3237
Douglas Gregor14a49e22010-12-07 18:32:03 +00003238 // Import the lexical declaration context.
3239 DeclContext *LexicalDC = DC;
3240 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3241 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3242 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003243 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003244 }
3245
3246 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3247 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003248 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003249
3250 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003251 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003252 if (D->getPropertyIvarDecl()) {
3253 Ivar = cast_or_null<ObjCIvarDecl>(
3254 Importer.Import(D->getPropertyIvarDecl()));
3255 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003256 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003257 }
3258
3259 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00003260 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
3261 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00003262 if (!ToImpl) {
3263 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3264 Importer.Import(D->getLocStart()),
3265 Importer.Import(D->getLocation()),
3266 Property,
3267 D->getPropertyImplementation(),
3268 Ivar,
3269 Importer.Import(D->getPropertyIvarDeclLoc()));
3270 ToImpl->setLexicalDeclContext(LexicalDC);
3271 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003272 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003273 } else {
3274 // Check that we have the same kind of property implementation (@synthesize
3275 // vs. @dynamic).
3276 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3277 Importer.ToDiag(ToImpl->getLocation(),
3278 diag::err_odr_objc_property_impl_kind_inconsistent)
3279 << Property->getDeclName()
3280 << (ToImpl->getPropertyImplementation()
3281 == ObjCPropertyImplDecl::Dynamic);
3282 Importer.FromDiag(D->getLocation(),
3283 diag::note_odr_objc_property_impl_kind)
3284 << D->getPropertyDecl()->getDeclName()
3285 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003286 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003287 }
3288
3289 // For @synthesize, check that we have the same
3290 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3291 Ivar != ToImpl->getPropertyIvarDecl()) {
3292 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3293 diag::err_odr_objc_synthesize_ivar_inconsistent)
3294 << Property->getDeclName()
3295 << ToImpl->getPropertyIvarDecl()->getDeclName()
3296 << Ivar->getDeclName();
3297 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3298 diag::note_odr_objc_synthesize_ivar_here)
3299 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003300 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003301 }
3302
3303 // Merge the existing implementation with the new implementation.
3304 Importer.Imported(D, ToImpl);
3305 }
3306
3307 return ToImpl;
3308}
3309
Douglas Gregora082a492010-11-30 19:14:50 +00003310Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3311 // For template arguments, we adopt the translation unit as our declaration
3312 // context. This context will be fixed when the actual template declaration
3313 // is created.
3314
3315 // FIXME: Import default argument.
3316 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3317 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003318 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003319 Importer.Import(D->getLocation()),
3320 D->getDepth(),
3321 D->getIndex(),
3322 Importer.Import(D->getIdentifier()),
3323 D->wasDeclaredWithTypename(),
3324 D->isParameterPack());
3325}
3326
3327Decl *
3328ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3329 // Import the name of this declaration.
3330 DeclarationName Name = Importer.Import(D->getDeclName());
3331 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003332 return nullptr;
3333
Douglas Gregora082a492010-11-30 19:14:50 +00003334 // Import the location of this declaration.
3335 SourceLocation Loc = Importer.Import(D->getLocation());
3336
3337 // Import the type of this declaration.
3338 QualType T = Importer.Import(D->getType());
3339 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003340 return nullptr;
3341
Douglas Gregora082a492010-11-30 19:14:50 +00003342 // Import type-source information.
3343 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3344 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003345 return nullptr;
3346
Douglas Gregora082a492010-11-30 19:14:50 +00003347 // FIXME: Import default argument.
3348
3349 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3350 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003351 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003352 Loc, D->getDepth(), D->getPosition(),
3353 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003354 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003355}
3356
3357Decl *
3358ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3359 // Import the name of this declaration.
3360 DeclarationName Name = Importer.Import(D->getDeclName());
3361 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003362 return nullptr;
3363
Douglas Gregora082a492010-11-30 19:14:50 +00003364 // Import the location of this declaration.
3365 SourceLocation Loc = Importer.Import(D->getLocation());
3366
3367 // Import template parameters.
3368 TemplateParameterList *TemplateParams
3369 = ImportTemplateParameterList(D->getTemplateParameters());
3370 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003371 return nullptr;
3372
Douglas Gregora082a492010-11-30 19:14:50 +00003373 // FIXME: Import default argument.
3374
3375 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3376 Importer.getToContext().getTranslationUnitDecl(),
3377 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003378 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003379 Name.getAsIdentifierInfo(),
3380 TemplateParams);
3381}
3382
3383Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3384 // If this record has a definition in the translation unit we're coming from,
3385 // but this particular declaration is not that definition, import the
3386 // definition and map to that.
3387 CXXRecordDecl *Definition
3388 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3389 if (Definition && Definition != D->getTemplatedDecl()) {
3390 Decl *ImportedDef
3391 = Importer.Import(Definition->getDescribedClassTemplate());
3392 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003393 return nullptr;
3394
Douglas Gregora082a492010-11-30 19:14:50 +00003395 return Importer.Imported(D, ImportedDef);
3396 }
3397
3398 // Import the major distinguishing characteristics of this class template.
3399 DeclContext *DC, *LexicalDC;
3400 DeclarationName Name;
3401 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003402 NamedDecl *ToD;
3403 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003404 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003405 if (ToD)
3406 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003407
Douglas Gregora082a492010-11-30 19:14:50 +00003408 // We may already have a template of the same name; try to find and match it.
3409 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003410 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003411 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003412 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003413 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3414 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003415 continue;
3416
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003417 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003418 if (ClassTemplateDecl *FoundTemplate
3419 = dyn_cast<ClassTemplateDecl>(Found)) {
3420 if (IsStructuralMatch(D, FoundTemplate)) {
3421 // The class templates structurally match; call it the same template.
3422 // FIXME: We may be filling in a forward declaration here. Handle
3423 // this case!
3424 Importer.Imported(D->getTemplatedDecl(),
3425 FoundTemplate->getTemplatedDecl());
3426 return Importer.Imported(D, FoundTemplate);
3427 }
3428 }
3429
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003430 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003431 }
3432
3433 if (!ConflictingDecls.empty()) {
3434 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3435 ConflictingDecls.data(),
3436 ConflictingDecls.size());
3437 }
3438
3439 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003440 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00003441 }
3442
3443 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3444
3445 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003446 // Create the declaration that is being templated.
3447 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
3448 Importer.Import(DTemplated));
3449 if (!D2Templated)
3450 return nullptr;
3451
3452 // Resolve possible cyclic import.
3453 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
3454 return AlreadyImported;
3455
Douglas Gregora082a492010-11-30 19:14:50 +00003456 // Create the class template declaration itself.
3457 TemplateParameterList *TemplateParams
3458 = ImportTemplateParameterList(D->getTemplateParameters());
3459 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003460 return nullptr;
3461
Douglas Gregora082a492010-11-30 19:14:50 +00003462 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3463 Loc, Name, TemplateParams,
Vassil Vassilev352e4412017-01-12 09:16:26 +00003464 D2Templated);
Douglas Gregora082a492010-11-30 19:14:50 +00003465 D2Templated->setDescribedClassTemplate(D2);
3466
3467 D2->setAccess(D->getAccess());
3468 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003469 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003470
3471 // Note the relationship between the class templates.
3472 Importer.Imported(D, D2);
3473 Importer.Imported(DTemplated, D2Templated);
3474
John McCallf937c022011-10-07 06:10:15 +00003475 if (DTemplated->isCompleteDefinition() &&
3476 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003477 // FIXME: Import definition!
3478 }
3479
3480 return D2;
3481}
3482
Douglas Gregore2e50d332010-12-01 01:36:18 +00003483Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3484 ClassTemplateSpecializationDecl *D) {
3485 // If this record has a definition in the translation unit we're coming from,
3486 // but this particular declaration is not that definition, import the
3487 // definition and map to that.
3488 TagDecl *Definition = D->getDefinition();
3489 if (Definition && Definition != D) {
3490 Decl *ImportedDef = Importer.Import(Definition);
3491 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003492 return nullptr;
3493
Douglas Gregore2e50d332010-12-01 01:36:18 +00003494 return Importer.Imported(D, ImportedDef);
3495 }
3496
3497 ClassTemplateDecl *ClassTemplate
3498 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3499 D->getSpecializedTemplate()));
3500 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003501 return nullptr;
3502
Douglas Gregore2e50d332010-12-01 01:36:18 +00003503 // Import the context of this declaration.
3504 DeclContext *DC = ClassTemplate->getDeclContext();
3505 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003506 return nullptr;
3507
Douglas Gregore2e50d332010-12-01 01:36:18 +00003508 DeclContext *LexicalDC = DC;
3509 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3510 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3511 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003512 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003513 }
3514
3515 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003516 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3517 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003518
3519 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003520 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003521 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3522 D->getTemplateArgs().size(),
3523 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003524 return nullptr;
3525
Douglas Gregore2e50d332010-12-01 01:36:18 +00003526 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003527 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003528 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00003529 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003530 if (D2) {
3531 // We already have a class template specialization with these template
3532 // arguments.
3533
3534 // FIXME: Check for specialization vs. instantiation errors.
3535
3536 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003537 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003538 // The record types structurally match, or the "from" translation
3539 // unit only had a forward declaration anyway; call it the same
3540 // function.
3541 return Importer.Imported(D, FoundDef);
3542 }
3543 }
3544 } else {
3545 // Create a new specialization.
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003546 if (ClassTemplatePartialSpecializationDecl *PartialSpec =
3547 dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
3548
3549 // Import TemplateArgumentListInfo
3550 TemplateArgumentListInfo ToTAInfo;
3551 auto &ASTTemplateArgs = *PartialSpec->getTemplateArgsAsWritten();
3552 for (unsigned I = 0, E = ASTTemplateArgs.NumTemplateArgs; I < E; ++I) {
3553 bool Error = false;
3554 auto ToLoc = ImportTemplateArgumentLoc(ASTTemplateArgs[I], Error);
3555 if (Error)
3556 return nullptr;
3557 ToTAInfo.addArgument(ToLoc);
3558 }
3559
3560 QualType CanonInjType = Importer.Import(
3561 PartialSpec->getInjectedSpecializationType());
3562 if (CanonInjType.isNull())
3563 return nullptr;
3564 CanonInjType = CanonInjType.getCanonicalType();
3565
3566 TemplateParameterList *ToTPList = ImportTemplateParameterList(
3567 PartialSpec->getTemplateParameters());
3568 if (!ToTPList && PartialSpec->getTemplateParameters())
3569 return nullptr;
3570
3571 D2 = ClassTemplatePartialSpecializationDecl::Create(
3572 Importer.getToContext(), D->getTagKind(), DC, StartLoc, IdLoc,
3573 ToTPList, ClassTemplate,
3574 llvm::makeArrayRef(TemplateArgs.data(), TemplateArgs.size()),
3575 ToTAInfo, CanonInjType, nullptr);
3576
3577 } else {
3578 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3579 D->getTagKind(), DC,
3580 StartLoc, IdLoc,
3581 ClassTemplate,
3582 TemplateArgs,
3583 /*PrevDecl=*/nullptr);
3584 }
3585
Douglas Gregore2e50d332010-12-01 01:36:18 +00003586 D2->setSpecializationKind(D->getSpecializationKind());
3587
3588 // Add this specialization to the class template.
3589 ClassTemplate->AddSpecialization(D2, InsertPos);
3590
3591 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003592 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Aleksei Sidorin855086d2017-01-23 09:30:36 +00003593
3594 Importer.Imported(D, D2);
3595
3596 if (auto *TSI = D->getTypeAsWritten()) {
3597 TypeSourceInfo *TInfo = Importer.Import(TSI);
3598 if (!TInfo)
3599 return nullptr;
3600 D2->setTypeAsWritten(TInfo);
3601 D2->setTemplateKeywordLoc(Importer.Import(D->getTemplateKeywordLoc()));
3602 D2->setExternLoc(Importer.Import(D->getExternLoc()));
3603 }
3604
3605 SourceLocation POI = Importer.Import(D->getPointOfInstantiation());
3606 if (POI.isValid())
3607 D2->setPointOfInstantiation(POI);
3608 else if (D->getPointOfInstantiation().isValid())
3609 return nullptr;
3610
3611 D2->setTemplateSpecializationKind(D->getTemplateSpecializationKind());
3612
Douglas Gregore2e50d332010-12-01 01:36:18 +00003613 // Add the specialization to this context.
3614 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003615 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003616 }
3617 Importer.Imported(D, D2);
John McCallf937c022011-10-07 06:10:15 +00003618 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00003619 return nullptr;
3620
Douglas Gregore2e50d332010-12-01 01:36:18 +00003621 return D2;
3622}
3623
Larisse Voufo39a1e502013-08-06 01:03:05 +00003624Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
3625 // If this variable has a definition in the translation unit we're coming
3626 // from,
3627 // but this particular declaration is not that definition, import the
3628 // definition and map to that.
3629 VarDecl *Definition =
3630 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
3631 if (Definition && Definition != D->getTemplatedDecl()) {
3632 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
3633 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003634 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003635
3636 return Importer.Imported(D, ImportedDef);
3637 }
3638
3639 // Import the major distinguishing characteristics of this variable template.
3640 DeclContext *DC, *LexicalDC;
3641 DeclarationName Name;
3642 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003643 NamedDecl *ToD;
3644 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003645 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003646 if (ToD)
3647 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003648
3649 // We may already have a template of the same name; try to find and match it.
3650 assert(!DC->isFunctionOrMethod() &&
3651 "Variable templates cannot be declared at function scope");
3652 SmallVector<NamedDecl *, 4> ConflictingDecls;
3653 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003654 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003655 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3656 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3657 continue;
3658
3659 Decl *Found = FoundDecls[I];
3660 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
3661 if (IsStructuralMatch(D, FoundTemplate)) {
3662 // The variable templates structurally match; call it the same template.
3663 Importer.Imported(D->getTemplatedDecl(),
3664 FoundTemplate->getTemplatedDecl());
3665 return Importer.Imported(D, FoundTemplate);
3666 }
3667 }
3668
3669 ConflictingDecls.push_back(FoundDecls[I]);
3670 }
3671
3672 if (!ConflictingDecls.empty()) {
3673 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3674 ConflictingDecls.data(),
3675 ConflictingDecls.size());
3676 }
3677
3678 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003679 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003680
3681 VarDecl *DTemplated = D->getTemplatedDecl();
3682
3683 // Import the type.
3684 QualType T = Importer.Import(DTemplated->getType());
3685 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003686 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003687
3688 // Create the declaration that is being templated.
3689 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3690 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
3691 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
3692 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
3693 IdLoc, Name.getAsIdentifierInfo(), T,
3694 TInfo, DTemplated->getStorageClass());
3695 D2Templated->setAccess(DTemplated->getAccess());
3696 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
3697 D2Templated->setLexicalDeclContext(LexicalDC);
3698
3699 // Importer.Imported(DTemplated, D2Templated);
3700 // LexicalDC->addDeclInternal(D2Templated);
3701
3702 // Merge the initializer.
3703 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00003704 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003705
3706 // Create the variable template declaration itself.
3707 TemplateParameterList *TemplateParams =
3708 ImportTemplateParameterList(D->getTemplateParameters());
3709 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003710 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003711
3712 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00003713 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003714 D2Templated->setDescribedVarTemplate(D2);
3715
3716 D2->setAccess(D->getAccess());
3717 D2->setLexicalDeclContext(LexicalDC);
3718 LexicalDC->addDeclInternal(D2);
3719
3720 // Note the relationship between the variable templates.
3721 Importer.Imported(D, D2);
3722 Importer.Imported(DTemplated, D2Templated);
3723
3724 if (DTemplated->isThisDeclarationADefinition() &&
3725 !D2Templated->isThisDeclarationADefinition()) {
3726 // FIXME: Import definition!
3727 }
3728
3729 return D2;
3730}
3731
3732Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
3733 VarTemplateSpecializationDecl *D) {
3734 // If this record has a definition in the translation unit we're coming from,
3735 // but this particular declaration is not that definition, import the
3736 // definition and map to that.
3737 VarDecl *Definition = D->getDefinition();
3738 if (Definition && Definition != D) {
3739 Decl *ImportedDef = Importer.Import(Definition);
3740 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003741 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003742
3743 return Importer.Imported(D, ImportedDef);
3744 }
3745
3746 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
3747 Importer.Import(D->getSpecializedTemplate()));
3748 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00003749 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003750
3751 // Import the context of this declaration.
3752 DeclContext *DC = VarTemplate->getDeclContext();
3753 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003754 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003755
3756 DeclContext *LexicalDC = DC;
3757 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3758 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3759 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003760 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003761 }
3762
3763 // Import the location of this declaration.
3764 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3765 SourceLocation IdLoc = Importer.Import(D->getLocation());
3766
3767 // Import template arguments.
3768 SmallVector<TemplateArgument, 2> TemplateArgs;
3769 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3770 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00003771 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003772
3773 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00003774 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003775 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00003776 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003777 if (D2) {
3778 // We already have a variable template specialization with these template
3779 // arguments.
3780
3781 // FIXME: Check for specialization vs. instantiation errors.
3782
3783 if (VarDecl *FoundDef = D2->getDefinition()) {
3784 if (!D->isThisDeclarationADefinition() ||
3785 IsStructuralMatch(D, FoundDef)) {
3786 // The record types structurally match, or the "from" translation
3787 // unit only had a forward declaration anyway; call it the same
3788 // variable.
3789 return Importer.Imported(D, FoundDef);
3790 }
3791 }
3792 } else {
3793
3794 // Import the type.
3795 QualType T = Importer.Import(D->getType());
3796 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003797 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003798 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3799
3800 // Create a new specialization.
3801 D2 = VarTemplateSpecializationDecl::Create(
3802 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00003803 D->getStorageClass(), TemplateArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00003804 D2->setSpecializationKind(D->getSpecializationKind());
3805 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
3806
3807 // Add this specialization to the class template.
3808 VarTemplate->AddSpecialization(D2, InsertPos);
3809
3810 // Import the qualifier, if any.
3811 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
3812
3813 // Add the specialization to this context.
3814 D2->setLexicalDeclContext(LexicalDC);
3815 LexicalDC->addDeclInternal(D2);
3816 }
3817 Importer.Imported(D, D2);
3818
3819 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00003820 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00003821
3822 return D2;
3823}
3824
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003825//----------------------------------------------------------------------------
3826// Import Statements
3827//----------------------------------------------------------------------------
3828
Sean Callanan59721b32015-04-28 18:41:46 +00003829DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
3830 if (DG.isNull())
3831 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
3832 size_t NumDecls = DG.end() - DG.begin();
3833 SmallVector<Decl *, 1> ToDecls(NumDecls);
3834 auto &_Importer = this->Importer;
3835 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
3836 [&_Importer](Decl *D) -> Decl * {
3837 return _Importer.Import(D);
3838 });
3839 return DeclGroupRef::Create(Importer.getToContext(),
3840 ToDecls.begin(),
3841 NumDecls);
3842}
3843
3844 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3845 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3846 << S->getStmtClassName();
3847 return nullptr;
3848 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003849
3850
3851Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
3852 SmallVector<IdentifierInfo *, 4> Names;
3853 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3854 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00003855 // ToII is nullptr when no symbolic name is given for output operand
3856 // see ParseStmtAsm::ParseAsmOperandsOpt
3857 if (!ToII && S->getOutputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003858 return nullptr;
3859 Names.push_back(ToII);
3860 }
3861 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3862 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
Gabor Horvath27f5ff62017-03-13 15:32:24 +00003863 // ToII is nullptr when no symbolic name is given for input operand
3864 // see ParseStmtAsm::ParseAsmOperandsOpt
3865 if (!ToII && S->getInputIdentifier(I))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003866 return nullptr;
3867 Names.push_back(ToII);
3868 }
3869
3870 SmallVector<StringLiteral *, 4> Clobbers;
3871 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
3872 StringLiteral *Clobber = cast_or_null<StringLiteral>(
3873 Importer.Import(S->getClobberStringLiteral(I)));
3874 if (!Clobber)
3875 return nullptr;
3876 Clobbers.push_back(Clobber);
3877 }
3878
3879 SmallVector<StringLiteral *, 4> Constraints;
3880 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
3881 StringLiteral *Output = cast_or_null<StringLiteral>(
3882 Importer.Import(S->getOutputConstraintLiteral(I)));
3883 if (!Output)
3884 return nullptr;
3885 Constraints.push_back(Output);
3886 }
3887
3888 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
3889 StringLiteral *Input = cast_or_null<StringLiteral>(
3890 Importer.Import(S->getInputConstraintLiteral(I)));
3891 if (!Input)
3892 return nullptr;
3893 Constraints.push_back(Input);
3894 }
3895
3896 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00003897 if (ImportContainerChecked(S->outputs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003898 return nullptr;
3899
Aleksei Sidorina693b372016-09-28 10:16:56 +00003900 if (ImportArrayChecked(S->inputs(), Exprs.begin() + S->getNumOutputs()))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003901 return nullptr;
3902
3903 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
3904 Importer.Import(S->getAsmString()));
3905 if (!AsmStr)
3906 return nullptr;
3907
3908 return new (Importer.getToContext()) GCCAsmStmt(
3909 Importer.getToContext(),
3910 Importer.Import(S->getAsmLoc()),
3911 S->isSimple(),
3912 S->isVolatile(),
3913 S->getNumOutputs(),
3914 S->getNumInputs(),
3915 Names.data(),
3916 Constraints.data(),
3917 Exprs.data(),
3918 AsmStr,
3919 S->getNumClobbers(),
3920 Clobbers.data(),
3921 Importer.Import(S->getRParenLoc()));
3922}
3923
Sean Callanan59721b32015-04-28 18:41:46 +00003924Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
3925 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
3926 for (Decl *ToD : ToDG) {
3927 if (!ToD)
3928 return nullptr;
3929 }
3930 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
3931 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
3932 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
3933}
3934
3935Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
3936 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
3937 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
3938 S->hasLeadingEmptyMacro());
3939}
3940
3941Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00003942 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Aleksei Sidorina693b372016-09-28 10:16:56 +00003943
3944 if (ImportContainerChecked(S->body(), ToStmts))
Sean Callanan8bca9962016-03-28 21:43:01 +00003945 return nullptr;
3946
Sean Callanan59721b32015-04-28 18:41:46 +00003947 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
3948 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
3949 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
3950 ToStmts,
3951 ToLBraceLoc, ToRBraceLoc);
3952}
3953
3954Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
3955 Expr *ToLHS = Importer.Import(S->getLHS());
3956 if (!ToLHS)
3957 return nullptr;
3958 Expr *ToRHS = Importer.Import(S->getRHS());
3959 if (!ToRHS && S->getRHS())
3960 return nullptr;
3961 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
3962 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
3963 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3964 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
3965 ToCaseLoc, ToEllipsisLoc,
3966 ToColonLoc);
3967}
3968
3969Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
3970 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
3971 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
3972 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3973 if (!ToSubStmt && S->getSubStmt())
3974 return nullptr;
3975 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
3976 ToSubStmt);
3977}
3978
3979Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
3980 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
3981 LabelDecl *ToLabelDecl =
3982 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
3983 if (!ToLabelDecl && S->getDecl())
3984 return nullptr;
3985 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
3986 if (!ToSubStmt && S->getSubStmt())
3987 return nullptr;
3988 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
3989 ToSubStmt);
3990}
3991
3992Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
3993 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
3994 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
3995 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
3996 ASTContext &_ToContext = Importer.getToContext();
3997 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
3998 [&_ToContext](const Attr *A) -> const Attr * {
3999 return A->clone(_ToContext);
4000 });
4001 for (const Attr *ToA : ToAttrs) {
4002 if (!ToA)
4003 return nullptr;
4004 }
4005 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4006 if (!ToSubStmt && S->getSubStmt())
4007 return nullptr;
4008 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4009 ToAttrs, ToSubStmt);
4010}
4011
4012Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4013 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
Richard Smitha547eb22016-07-14 00:11:03 +00004014 Stmt *ToInit = Importer.Import(S->getInit());
4015 if (!ToInit && S->getInit())
4016 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004017 VarDecl *ToConditionVariable = nullptr;
4018 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4019 ToConditionVariable =
4020 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4021 if (!ToConditionVariable)
4022 return nullptr;
4023 }
4024 Expr *ToCondition = Importer.Import(S->getCond());
4025 if (!ToCondition && S->getCond())
4026 return nullptr;
4027 Stmt *ToThenStmt = Importer.Import(S->getThen());
4028 if (!ToThenStmt && S->getThen())
4029 return nullptr;
4030 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4031 Stmt *ToElseStmt = Importer.Import(S->getElse());
4032 if (!ToElseStmt && S->getElse())
4033 return nullptr;
4034 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
Richard Smithb130fe72016-06-23 19:16:49 +00004035 ToIfLoc, S->isConstexpr(),
Richard Smitha547eb22016-07-14 00:11:03 +00004036 ToInit,
Richard Smithb130fe72016-06-23 19:16:49 +00004037 ToConditionVariable,
Sean Callanan59721b32015-04-28 18:41:46 +00004038 ToCondition, ToThenStmt,
4039 ToElseLoc, ToElseStmt);
4040}
4041
4042Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
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 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
Richard Smitha547eb22016-07-14 00:11:03 +00004057 Importer.getToContext(), ToInit,
4058 ToConditionVariable, ToCondition);
Sean Callanan59721b32015-04-28 18:41:46 +00004059 Stmt *ToBody = Importer.Import(S->getBody());
4060 if (!ToBody && S->getBody())
4061 return nullptr;
4062 ToStmt->setBody(ToBody);
4063 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4064 // Now we have to re-chain the cases.
4065 SwitchCase *LastChainedSwitchCase = nullptr;
4066 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4067 SC = SC->getNextSwitchCase()) {
4068 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4069 if (!ToSC)
4070 return nullptr;
4071 if (LastChainedSwitchCase)
4072 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4073 else
4074 ToStmt->setSwitchCaseList(ToSC);
4075 LastChainedSwitchCase = ToSC;
4076 }
4077 return ToStmt;
4078}
4079
4080Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4081 VarDecl *ToConditionVariable = nullptr;
4082 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4083 ToConditionVariable =
4084 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4085 if (!ToConditionVariable)
4086 return nullptr;
4087 }
4088 Expr *ToCondition = Importer.Import(S->getCond());
4089 if (!ToCondition && S->getCond())
4090 return nullptr;
4091 Stmt *ToBody = Importer.Import(S->getBody());
4092 if (!ToBody && S->getBody())
4093 return nullptr;
4094 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4095 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4096 ToConditionVariable,
4097 ToCondition, ToBody,
4098 ToWhileLoc);
4099}
4100
4101Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4102 Stmt *ToBody = Importer.Import(S->getBody());
4103 if (!ToBody && S->getBody())
4104 return nullptr;
4105 Expr *ToCondition = Importer.Import(S->getCond());
4106 if (!ToCondition && S->getCond())
4107 return nullptr;
4108 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4109 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4110 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4111 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4112 ToDoLoc, ToWhileLoc,
4113 ToRParenLoc);
4114}
4115
4116Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4117 Stmt *ToInit = Importer.Import(S->getInit());
4118 if (!ToInit && S->getInit())
4119 return nullptr;
4120 Expr *ToCondition = Importer.Import(S->getCond());
4121 if (!ToCondition && S->getCond())
4122 return nullptr;
4123 VarDecl *ToConditionVariable = nullptr;
4124 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4125 ToConditionVariable =
4126 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4127 if (!ToConditionVariable)
4128 return nullptr;
4129 }
4130 Expr *ToInc = Importer.Import(S->getInc());
4131 if (!ToInc && S->getInc())
4132 return nullptr;
4133 Stmt *ToBody = Importer.Import(S->getBody());
4134 if (!ToBody && S->getBody())
4135 return nullptr;
4136 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4137 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4138 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4139 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4140 ToInit, ToCondition,
4141 ToConditionVariable,
4142 ToInc, ToBody,
4143 ToForLoc, ToLParenLoc,
4144 ToRParenLoc);
4145}
4146
4147Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4148 LabelDecl *ToLabel = nullptr;
4149 if (LabelDecl *FromLabel = S->getLabel()) {
4150 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4151 if (!ToLabel)
4152 return nullptr;
4153 }
4154 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4155 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4156 return new (Importer.getToContext()) GotoStmt(ToLabel,
4157 ToGotoLoc, ToLabelLoc);
4158}
4159
4160Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4161 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4162 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4163 Expr *ToTarget = Importer.Import(S->getTarget());
4164 if (!ToTarget && S->getTarget())
4165 return nullptr;
4166 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4167 ToTarget);
4168}
4169
4170Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4171 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4172 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4173}
4174
4175Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4176 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4177 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4178}
4179
4180Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4181 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4182 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4183 if (!ToRetExpr && S->getRetValue())
4184 return nullptr;
4185 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4186 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4187 if (!ToNRVOCandidate && NRVOCandidate)
4188 return nullptr;
4189 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4190 ToNRVOCandidate);
4191}
4192
4193Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4194 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4195 VarDecl *ToExceptionDecl = nullptr;
4196 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4197 ToExceptionDecl =
4198 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4199 if (!ToExceptionDecl)
4200 return nullptr;
4201 }
4202 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4203 if (!ToHandlerBlock && S->getHandlerBlock())
4204 return nullptr;
4205 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4206 ToExceptionDecl,
4207 ToHandlerBlock);
4208}
4209
4210Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4211 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4212 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4213 if (!ToTryBlock && S->getTryBlock())
4214 return nullptr;
4215 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4216 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4217 CXXCatchStmt *FromHandler = S->getHandler(HI);
4218 if (Stmt *ToHandler = Importer.Import(FromHandler))
4219 ToHandlers[HI] = ToHandler;
4220 else
4221 return nullptr;
4222 }
4223 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4224 ToHandlers);
4225}
4226
4227Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4228 DeclStmt *ToRange =
4229 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4230 if (!ToRange && S->getRangeStmt())
4231 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004232 DeclStmt *ToBegin =
4233 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4234 if (!ToBegin && S->getBeginStmt())
4235 return nullptr;
4236 DeclStmt *ToEnd =
4237 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4238 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004239 return nullptr;
4240 Expr *ToCond = Importer.Import(S->getCond());
4241 if (!ToCond && S->getCond())
4242 return nullptr;
4243 Expr *ToInc = Importer.Import(S->getInc());
4244 if (!ToInc && S->getInc())
4245 return nullptr;
4246 DeclStmt *ToLoopVar =
4247 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4248 if (!ToLoopVar && S->getLoopVarStmt())
4249 return nullptr;
4250 Stmt *ToBody = Importer.Import(S->getBody());
4251 if (!ToBody && S->getBody())
4252 return nullptr;
4253 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004254 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004255 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4256 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00004257 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00004258 ToCond, ToInc,
4259 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004260 ToForLoc, ToCoawaitLoc,
4261 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004262}
4263
4264Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4265 Stmt *ToElem = Importer.Import(S->getElement());
4266 if (!ToElem && S->getElement())
4267 return nullptr;
4268 Expr *ToCollect = Importer.Import(S->getCollection());
4269 if (!ToCollect && S->getCollection())
4270 return nullptr;
4271 Stmt *ToBody = Importer.Import(S->getBody());
4272 if (!ToBody && S->getBody())
4273 return nullptr;
4274 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4275 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4276 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4277 ToCollect,
4278 ToBody, ToForLoc,
4279 ToRParenLoc);
4280}
4281
4282Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4283 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4284 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4285 VarDecl *ToExceptionDecl = nullptr;
4286 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4287 ToExceptionDecl =
4288 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4289 if (!ToExceptionDecl)
4290 return nullptr;
4291 }
4292 Stmt *ToBody = Importer.Import(S->getCatchBody());
4293 if (!ToBody && S->getCatchBody())
4294 return nullptr;
4295 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4296 ToRParenLoc,
4297 ToExceptionDecl,
4298 ToBody);
4299}
4300
4301Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4302 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4303 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4304 if (!ToAtFinallyStmt && S->getFinallyBody())
4305 return nullptr;
4306 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4307 ToAtFinallyStmt);
4308}
4309
4310Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4311 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4312 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4313 if (!ToAtTryStmt && S->getTryBody())
4314 return nullptr;
4315 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4316 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4317 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4318 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4319 ToCatchStmts[CI] = ToCatchStmt;
4320 else
4321 return nullptr;
4322 }
4323 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4324 if (!ToAtFinallyStmt && S->getFinallyStmt())
4325 return nullptr;
4326 return ObjCAtTryStmt::Create(Importer.getToContext(),
4327 ToAtTryLoc, ToAtTryStmt,
4328 ToCatchStmts.begin(), ToCatchStmts.size(),
4329 ToAtFinallyStmt);
4330}
4331
4332Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4333 (ObjCAtSynchronizedStmt *S) {
4334 SourceLocation ToAtSynchronizedLoc =
4335 Importer.Import(S->getAtSynchronizedLoc());
4336 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4337 if (!ToSynchExpr && S->getSynchExpr())
4338 return nullptr;
4339 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4340 if (!ToSynchBody && S->getSynchBody())
4341 return nullptr;
4342 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4343 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4344}
4345
4346Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4347 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4348 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4349 if (!ToThrow && S->getThrowExpr())
4350 return nullptr;
4351 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4352}
4353
4354Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4355 (ObjCAutoreleasePoolStmt *S) {
4356 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4357 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4358 if (!ToSubStmt && S->getSubStmt())
4359 return nullptr;
4360 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4361 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004362}
4363
4364//----------------------------------------------------------------------------
4365// Import Expressions
4366//----------------------------------------------------------------------------
4367Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4368 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4369 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004370 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004371}
4372
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004373Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
4374 QualType T = Importer.Import(E->getType());
4375 if (T.isNull())
4376 return nullptr;
4377
4378 Expr *SubExpr = Importer.Import(E->getSubExpr());
4379 if (!SubExpr && E->getSubExpr())
4380 return nullptr;
4381
4382 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
4383 if (!TInfo)
4384 return nullptr;
4385
4386 return new (Importer.getToContext()) VAArgExpr(
4387 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
4388 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
4389}
4390
4391
4392Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
4393 QualType T = Importer.Import(E->getType());
4394 if (T.isNull())
4395 return nullptr;
4396
4397 return new (Importer.getToContext()) GNUNullExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004398 T, Importer.Import(E->getLocStart()));
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004399}
4400
4401Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
4402 QualType T = Importer.Import(E->getType());
4403 if (T.isNull())
4404 return nullptr;
4405
4406 StringLiteral *SL = cast_or_null<StringLiteral>(
4407 Importer.Import(E->getFunctionName()));
4408 if (!SL && E->getFunctionName())
4409 return nullptr;
4410
4411 return new (Importer.getToContext()) PredefinedExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004412 Importer.Import(E->getLocStart()), T, E->getIdentType(), SL);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004413}
4414
Douglas Gregor52f820e2010-02-19 01:17:02 +00004415Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004416 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4417 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004418 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004419
Craig Topper36250ad2014-05-12 05:36:57 +00004420 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004421 if (E->getDecl() != E->getFoundDecl()) {
4422 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4423 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004424 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004425 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004426
4427 QualType T = Importer.Import(E->getType());
4428 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004429 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004430
Aleksei Sidorina693b372016-09-28 10:16:56 +00004431
4432 TemplateArgumentListInfo ToTAInfo;
4433 TemplateArgumentListInfo *ResInfo = nullptr;
4434 if (E->hasExplicitTemplateArgs()) {
4435 for (const auto &FromLoc : E->template_arguments()) {
4436 bool Error = false;
4437 TemplateArgumentLoc ToTALoc = ImportTemplateArgumentLoc(FromLoc, Error);
4438 if (Error)
4439 return nullptr;
4440 ToTAInfo.addArgument(ToTALoc);
4441 }
4442 ResInfo = &ToTAInfo;
4443 }
4444
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004445 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4446 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004447 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004448 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004449 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004450 Importer.Import(E->getLocation()),
4451 T, E->getValueKind(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00004452 FoundD, ResInfo);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004453 if (E->hadMultipleCandidates())
4454 DRE->setHadMultipleCandidates(true);
4455 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004456}
4457
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004458Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
4459 QualType T = Importer.Import(E->getType());
4460 if (T.isNull())
Aleksei Sidorina693b372016-09-28 10:16:56 +00004461 return nullptr;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004462
4463 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
4464}
4465
4466ASTNodeImporter::Designator
4467ASTNodeImporter::ImportDesignator(const Designator &D) {
4468 if (D.isFieldDesignator()) {
4469 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
4470 // Caller checks for import error
4471 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
4472 Importer.Import(D.getFieldLoc()));
4473 }
4474 if (D.isArrayDesignator())
4475 return Designator(D.getFirstExprIndex(),
4476 Importer.Import(D.getLBracketLoc()),
4477 Importer.Import(D.getRBracketLoc()));
4478
4479 assert(D.isArrayRangeDesignator());
4480 return Designator(D.getFirstExprIndex(),
4481 Importer.Import(D.getLBracketLoc()),
4482 Importer.Import(D.getEllipsisLoc()),
4483 Importer.Import(D.getRBracketLoc()));
4484}
4485
4486
4487Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
4488 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
4489 if (!Init)
4490 return nullptr;
4491
4492 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
4493 // List elements from the second, the first is Init itself
4494 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
4495 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
4496 IndexExprs[I - 1] = Arg;
4497 else
4498 return nullptr;
4499 }
4500
4501 SmallVector<Designator, 4> Designators(DIE->size());
David Majnemerf7e36092016-06-23 00:15:04 +00004502 llvm::transform(DIE->designators(), Designators.begin(),
4503 [this](const Designator &D) -> Designator {
4504 return ImportDesignator(D);
4505 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004506
David Majnemerf7e36092016-06-23 00:15:04 +00004507 for (const Designator &D : DIE->designators())
4508 if (D.isFieldDesignator() && !D.getFieldName())
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004509 return nullptr;
4510
4511 return DesignatedInitExpr::Create(
David Majnemerf7e36092016-06-23 00:15:04 +00004512 Importer.getToContext(), Designators,
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004513 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
4514 DIE->usesGNUSyntax(), Init);
4515}
4516
4517Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
4518 QualType T = Importer.Import(E->getType());
4519 if (T.isNull())
4520 return nullptr;
4521
4522 return new (Importer.getToContext())
4523 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
4524}
4525
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004526Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4527 QualType T = Importer.Import(E->getType());
4528 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004529 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004530
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004531 return IntegerLiteral::Create(Importer.getToContext(),
4532 E->getValue(), T,
4533 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004534}
4535
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004536Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
4537 QualType T = Importer.Import(E->getType());
4538 if (T.isNull())
4539 return nullptr;
4540
4541 return FloatingLiteral::Create(Importer.getToContext(),
4542 E->getValue(), E->isExact(), T,
4543 Importer.Import(E->getLocation()));
4544}
4545
Douglas Gregor623421d2010-02-18 02:21:22 +00004546Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4547 QualType T = Importer.Import(E->getType());
4548 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004549 return nullptr;
4550
Douglas Gregorfb65e592011-07-27 05:40:30 +00004551 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4552 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004553 Importer.Import(E->getLocation()));
4554}
4555
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004556Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
4557 QualType T = Importer.Import(E->getType());
4558 if (T.isNull())
4559 return nullptr;
4560
4561 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
4562 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
4563
4564 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
4565 E->getKind(), E->isPascal(), T,
4566 Locations.data(), Locations.size());
4567}
4568
4569Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
4570 QualType T = Importer.Import(E->getType());
4571 if (T.isNull())
4572 return nullptr;
4573
4574 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
4575 if (!TInfo)
4576 return nullptr;
4577
4578 Expr *Init = Importer.Import(E->getInitializer());
4579 if (!Init)
4580 return nullptr;
4581
4582 return new (Importer.getToContext()) CompoundLiteralExpr(
4583 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
4584 Init, E->isFileScope());
4585}
4586
4587Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
4588 QualType T = Importer.Import(E->getType());
4589 if (T.isNull())
4590 return nullptr;
4591
4592 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
4593 if (ImportArrayChecked(
4594 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
4595 Exprs.begin()))
4596 return nullptr;
4597
4598 return new (Importer.getToContext()) AtomicExpr(
4599 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
4600 Importer.Import(E->getRParenLoc()));
4601}
4602
4603Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
4604 QualType T = Importer.Import(E->getType());
4605 if (T.isNull())
4606 return nullptr;
4607
4608 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
4609 if (!ToLabel)
4610 return nullptr;
4611
4612 return new (Importer.getToContext()) AddrLabelExpr(
4613 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
4614 ToLabel, T);
4615}
4616
Douglas Gregorc74247e2010-02-19 01:07:06 +00004617Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4618 Expr *SubExpr = Importer.Import(E->getSubExpr());
4619 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004620 return nullptr;
4621
Douglas Gregorc74247e2010-02-19 01:07:06 +00004622 return new (Importer.getToContext())
4623 ParenExpr(Importer.Import(E->getLParen()),
4624 Importer.Import(E->getRParen()),
4625 SubExpr);
4626}
4627
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004628Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
4629 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00004630 if (ImportContainerChecked(E->exprs(), Exprs))
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004631 return nullptr;
4632
4633 return new (Importer.getToContext()) ParenListExpr(
4634 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
4635 Exprs, Importer.Import(E->getLParenLoc()));
4636}
4637
4638Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
4639 QualType T = Importer.Import(E->getType());
4640 if (T.isNull())
4641 return nullptr;
4642
4643 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
4644 Importer.Import(E->getSubStmt()));
4645 if (!ToSubStmt && E->getSubStmt())
4646 return nullptr;
4647
4648 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
4649 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
4650}
4651
Douglas Gregorc74247e2010-02-19 01:07:06 +00004652Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4653 QualType T = Importer.Import(E->getType());
4654 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004655 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004656
4657 Expr *SubExpr = Importer.Import(E->getSubExpr());
4658 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004659 return nullptr;
4660
Douglas Gregorc74247e2010-02-19 01:07:06 +00004661 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004662 T, E->getValueKind(),
4663 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004664 Importer.Import(E->getOperatorLoc()));
4665}
4666
Peter Collingbournee190dee2011-03-11 19:24:49 +00004667Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4668 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004669 QualType ResultType = Importer.Import(E->getType());
4670
4671 if (E->isArgumentType()) {
4672 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4673 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004674 return nullptr;
4675
Peter Collingbournee190dee2011-03-11 19:24:49 +00004676 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4677 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004678 Importer.Import(E->getOperatorLoc()),
4679 Importer.Import(E->getRParenLoc()));
4680 }
4681
4682 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4683 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004684 return nullptr;
4685
Peter Collingbournee190dee2011-03-11 19:24:49 +00004686 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4687 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004688 Importer.Import(E->getOperatorLoc()),
4689 Importer.Import(E->getRParenLoc()));
4690}
4691
Douglas Gregorc74247e2010-02-19 01:07:06 +00004692Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4693 QualType T = Importer.Import(E->getType());
4694 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004695 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004696
4697 Expr *LHS = Importer.Import(E->getLHS());
4698 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004699 return nullptr;
4700
Douglas Gregorc74247e2010-02-19 01:07:06 +00004701 Expr *RHS = Importer.Import(E->getRHS());
4702 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004703 return nullptr;
4704
Douglas Gregorc74247e2010-02-19 01:07:06 +00004705 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004706 T, E->getValueKind(),
4707 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004708 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00004709 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004710}
4711
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004712Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
4713 QualType T = Importer.Import(E->getType());
4714 if (T.isNull())
4715 return nullptr;
4716
4717 Expr *ToLHS = Importer.Import(E->getLHS());
4718 if (!ToLHS)
4719 return nullptr;
4720
4721 Expr *ToRHS = Importer.Import(E->getRHS());
4722 if (!ToRHS)
4723 return nullptr;
4724
4725 Expr *ToCond = Importer.Import(E->getCond());
4726 if (!ToCond)
4727 return nullptr;
4728
4729 return new (Importer.getToContext()) ConditionalOperator(
4730 ToCond, Importer.Import(E->getQuestionLoc()),
4731 ToLHS, Importer.Import(E->getColonLoc()),
4732 ToRHS, T, E->getValueKind(), E->getObjectKind());
4733}
4734
4735Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
4736 BinaryConditionalOperator *E) {
4737 QualType T = Importer.Import(E->getType());
4738 if (T.isNull())
4739 return nullptr;
4740
4741 Expr *Common = Importer.Import(E->getCommon());
4742 if (!Common)
4743 return nullptr;
4744
4745 Expr *Cond = Importer.Import(E->getCond());
4746 if (!Cond)
4747 return nullptr;
4748
4749 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
4750 Importer.Import(E->getOpaqueValue()));
4751 if (!OpaqueValue)
4752 return nullptr;
4753
4754 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
4755 if (!TrueExpr)
4756 return nullptr;
4757
4758 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
4759 if (!FalseExpr)
4760 return nullptr;
4761
4762 return new (Importer.getToContext()) BinaryConditionalOperator(
4763 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
4764 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
4765 T, E->getValueKind(), E->getObjectKind());
4766}
4767
Aleksei Sidorina693b372016-09-28 10:16:56 +00004768Expr *ASTNodeImporter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
4769 QualType T = Importer.Import(E->getType());
4770 if (T.isNull())
4771 return nullptr;
4772
4773 TypeSourceInfo *ToQueried = Importer.Import(E->getQueriedTypeSourceInfo());
4774 if (!ToQueried)
4775 return nullptr;
4776
4777 Expr *Dim = Importer.Import(E->getDimensionExpression());
4778 if (!Dim && E->getDimensionExpression())
4779 return nullptr;
4780
4781 return new (Importer.getToContext()) ArrayTypeTraitExpr(
4782 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4783 E->getValue(), Dim, Importer.Import(E->getLocEnd()), T);
4784}
4785
4786Expr *ASTNodeImporter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
4787 QualType T = Importer.Import(E->getType());
4788 if (T.isNull())
4789 return nullptr;
4790
4791 Expr *ToQueried = Importer.Import(E->getQueriedExpression());
4792 if (!ToQueried)
4793 return nullptr;
4794
4795 return new (Importer.getToContext()) ExpressionTraitExpr(
4796 Importer.Import(E->getLocStart()), E->getTrait(), ToQueried,
4797 E->getValue(), Importer.Import(E->getLocEnd()), T);
4798}
4799
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004800Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
4801 QualType T = Importer.Import(E->getType());
4802 if (T.isNull())
4803 return nullptr;
4804
4805 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
4806 if (!SourceExpr && E->getSourceExpr())
4807 return nullptr;
4808
4809 return new (Importer.getToContext()) OpaqueValueExpr(
Aleksei Sidorina693b372016-09-28 10:16:56 +00004810 Importer.Import(E->getLocation()), T, E->getValueKind(),
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004811 E->getObjectKind(), SourceExpr);
4812}
4813
Aleksei Sidorina693b372016-09-28 10:16:56 +00004814Expr *ASTNodeImporter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
4815 QualType T = Importer.Import(E->getType());
4816 if (T.isNull())
4817 return nullptr;
4818
4819 Expr *ToLHS = Importer.Import(E->getLHS());
4820 if (!ToLHS)
4821 return nullptr;
4822
4823 Expr *ToRHS = Importer.Import(E->getRHS());
4824 if (!ToRHS)
4825 return nullptr;
4826
4827 return new (Importer.getToContext()) ArraySubscriptExpr(
4828 ToLHS, ToRHS, T, E->getValueKind(), E->getObjectKind(),
4829 Importer.Import(E->getRBracketLoc()));
4830}
4831
Douglas Gregorc74247e2010-02-19 01:07:06 +00004832Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4833 QualType T = Importer.Import(E->getType());
4834 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004835 return nullptr;
4836
Douglas Gregorc74247e2010-02-19 01:07:06 +00004837 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4838 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004839 return nullptr;
4840
Douglas Gregorc74247e2010-02-19 01:07:06 +00004841 QualType CompResultType = Importer.Import(E->getComputationResultType());
4842 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004843 return nullptr;
4844
Douglas Gregorc74247e2010-02-19 01:07:06 +00004845 Expr *LHS = Importer.Import(E->getLHS());
4846 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004847 return nullptr;
4848
Douglas Gregorc74247e2010-02-19 01:07:06 +00004849 Expr *RHS = Importer.Import(E->getRHS());
4850 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004851 return nullptr;
4852
Douglas Gregorc74247e2010-02-19 01:07:06 +00004853 return new (Importer.getToContext())
4854 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004855 T, E->getValueKind(),
4856 E->getObjectKind(),
4857 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004858 Importer.Import(E->getOperatorLoc()),
Adam Nemet484aa452017-03-27 19:17:25 +00004859 E->getFPFeatures());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004860}
4861
Aleksei Sidorina693b372016-09-28 10:16:56 +00004862bool ASTNodeImporter::ImportCastPath(CastExpr *CE, CXXCastPath &Path) {
4863 for (auto I = CE->path_begin(), E = CE->path_end(); I != E; ++I) {
4864 if (CXXBaseSpecifier *Spec = Importer.Import(*I))
4865 Path.push_back(Spec);
4866 else
4867 return true;
4868 }
4869 return false;
John McCallcf142162010-08-07 06:22:56 +00004870}
4871
Douglas Gregor98c10182010-02-12 22:17:39 +00004872Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4873 QualType T = Importer.Import(E->getType());
4874 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004875 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00004876
4877 Expr *SubExpr = Importer.Import(E->getSubExpr());
4878 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004879 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004880
4881 CXXCastPath BasePath;
4882 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004883 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004884
4885 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004886 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004887}
4888
Aleksei Sidorina693b372016-09-28 10:16:56 +00004889Expr *ASTNodeImporter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
Douglas Gregor5481d322010-02-19 01:32:14 +00004890 QualType T = Importer.Import(E->getType());
4891 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004892 return nullptr;
4893
Douglas Gregor5481d322010-02-19 01:32:14 +00004894 Expr *SubExpr = Importer.Import(E->getSubExpr());
4895 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004896 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00004897
4898 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4899 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00004900 return nullptr;
4901
John McCallcf142162010-08-07 06:22:56 +00004902 CXXCastPath BasePath;
4903 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004904 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004905
Aleksei Sidorina693b372016-09-28 10:16:56 +00004906 switch (E->getStmtClass()) {
4907 case Stmt::CStyleCastExprClass: {
4908 CStyleCastExpr *CCE = cast<CStyleCastExpr>(E);
4909 return CStyleCastExpr::Create(Importer.getToContext(), T,
4910 E->getValueKind(), E->getCastKind(),
4911 SubExpr, &BasePath, TInfo,
4912 Importer.Import(CCE->getLParenLoc()),
4913 Importer.Import(CCE->getRParenLoc()));
4914 }
4915
4916 case Stmt::CXXFunctionalCastExprClass: {
4917 CXXFunctionalCastExpr *FCE = cast<CXXFunctionalCastExpr>(E);
4918 return CXXFunctionalCastExpr::Create(Importer.getToContext(), T,
4919 E->getValueKind(), TInfo,
4920 E->getCastKind(), SubExpr, &BasePath,
4921 Importer.Import(FCE->getLParenLoc()),
4922 Importer.Import(FCE->getRParenLoc()));
4923 }
4924
4925 case Stmt::ObjCBridgedCastExprClass: {
4926 ObjCBridgedCastExpr *OCE = cast<ObjCBridgedCastExpr>(E);
4927 return new (Importer.getToContext()) ObjCBridgedCastExpr(
4928 Importer.Import(OCE->getLParenLoc()), OCE->getBridgeKind(),
4929 E->getCastKind(), Importer.Import(OCE->getBridgeKeywordLoc()),
4930 TInfo, SubExpr);
4931 }
4932 default:
4933 break; // just fall through
4934 }
4935
4936 CXXNamedCastExpr *Named = cast<CXXNamedCastExpr>(E);
4937 SourceLocation ExprLoc = Importer.Import(Named->getOperatorLoc()),
4938 RParenLoc = Importer.Import(Named->getRParenLoc());
4939 SourceRange Brackets = Importer.Import(Named->getAngleBrackets());
4940
4941 switch (E->getStmtClass()) {
4942 case Stmt::CXXStaticCastExprClass:
4943 return CXXStaticCastExpr::Create(Importer.getToContext(), T,
4944 E->getValueKind(), E->getCastKind(),
4945 SubExpr, &BasePath, TInfo,
4946 ExprLoc, RParenLoc, Brackets);
4947
4948 case Stmt::CXXDynamicCastExprClass:
4949 return CXXDynamicCastExpr::Create(Importer.getToContext(), T,
4950 E->getValueKind(), E->getCastKind(),
4951 SubExpr, &BasePath, TInfo,
4952 ExprLoc, RParenLoc, Brackets);
4953
4954 case Stmt::CXXReinterpretCastExprClass:
4955 return CXXReinterpretCastExpr::Create(Importer.getToContext(), T,
4956 E->getValueKind(), E->getCastKind(),
4957 SubExpr, &BasePath, TInfo,
4958 ExprLoc, RParenLoc, Brackets);
4959
4960 case Stmt::CXXConstCastExprClass:
4961 return CXXConstCastExpr::Create(Importer.getToContext(), T,
4962 E->getValueKind(), SubExpr, TInfo, ExprLoc,
4963 RParenLoc, Brackets);
4964 default:
4965 llvm_unreachable("Cast expression of unsupported type!");
4966 return nullptr;
4967 }
4968}
4969
4970Expr *ASTNodeImporter::VisitOffsetOfExpr(OffsetOfExpr *OE) {
4971 QualType T = Importer.Import(OE->getType());
4972 if (T.isNull())
4973 return nullptr;
4974
4975 SmallVector<OffsetOfNode, 4> Nodes;
4976 for (int I = 0, E = OE->getNumComponents(); I < E; ++I) {
4977 const OffsetOfNode &Node = OE->getComponent(I);
4978
4979 switch (Node.getKind()) {
4980 case OffsetOfNode::Array:
4981 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()),
4982 Node.getArrayExprIndex(),
4983 Importer.Import(Node.getLocEnd())));
4984 break;
4985
4986 case OffsetOfNode::Base: {
4987 CXXBaseSpecifier *BS = Importer.Import(Node.getBase());
4988 if (!BS && Node.getBase())
4989 return nullptr;
4990 Nodes.push_back(OffsetOfNode(BS));
4991 break;
4992 }
4993 case OffsetOfNode::Field: {
4994 FieldDecl *FD = cast_or_null<FieldDecl>(Importer.Import(Node.getField()));
4995 if (!FD)
4996 return nullptr;
4997 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), FD,
4998 Importer.Import(Node.getLocEnd())));
4999 break;
5000 }
5001 case OffsetOfNode::Identifier: {
5002 IdentifierInfo *ToII = Importer.Import(Node.getFieldName());
5003 if (!ToII)
5004 return nullptr;
5005 Nodes.push_back(OffsetOfNode(Importer.Import(Node.getLocStart()), ToII,
5006 Importer.Import(Node.getLocEnd())));
5007 break;
5008 }
5009 }
5010 }
5011
5012 SmallVector<Expr *, 4> Exprs(OE->getNumExpressions());
5013 for (int I = 0, E = OE->getNumExpressions(); I < E; ++I) {
5014 Expr *ToIndexExpr = Importer.Import(OE->getIndexExpr(I));
5015 if (!ToIndexExpr)
5016 return nullptr;
5017 Exprs[I] = ToIndexExpr;
5018 }
5019
5020 TypeSourceInfo *TInfo = Importer.Import(OE->getTypeSourceInfo());
5021 if (!TInfo && OE->getTypeSourceInfo())
5022 return nullptr;
5023
5024 return OffsetOfExpr::Create(Importer.getToContext(), T,
5025 Importer.Import(OE->getOperatorLoc()),
5026 TInfo, Nodes, Exprs,
5027 Importer.Import(OE->getRParenLoc()));
5028}
5029
5030Expr *ASTNodeImporter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
5031 QualType T = Importer.Import(E->getType());
5032 if (T.isNull())
5033 return nullptr;
5034
5035 Expr *Operand = Importer.Import(E->getOperand());
5036 if (!Operand)
5037 return nullptr;
5038
5039 CanThrowResult CanThrow;
5040 if (E->isValueDependent())
5041 CanThrow = CT_Dependent;
5042 else
5043 CanThrow = E->getValue() ? CT_Can : CT_Cannot;
5044
5045 return new (Importer.getToContext()) CXXNoexceptExpr(
5046 T, Operand, CanThrow,
5047 Importer.Import(E->getLocStart()), Importer.Import(E->getLocEnd()));
5048}
5049
5050Expr *ASTNodeImporter::VisitCXXThrowExpr(CXXThrowExpr *E) {
5051 QualType T = Importer.Import(E->getType());
5052 if (T.isNull())
5053 return nullptr;
5054
5055 Expr *SubExpr = Importer.Import(E->getSubExpr());
5056 if (!SubExpr && E->getSubExpr())
5057 return nullptr;
5058
5059 return new (Importer.getToContext()) CXXThrowExpr(
5060 SubExpr, T, Importer.Import(E->getThrowLoc()),
5061 E->isThrownVariableInScope());
5062}
5063
5064Expr *ASTNodeImporter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
5065 ParmVarDecl *Param = cast_or_null<ParmVarDecl>(
5066 Importer.Import(E->getParam()));
5067 if (!Param)
5068 return nullptr;
5069
5070 return CXXDefaultArgExpr::Create(
5071 Importer.getToContext(), Importer.Import(E->getUsedLocation()), Param);
5072}
5073
5074Expr *ASTNodeImporter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
5075 QualType T = Importer.Import(E->getType());
5076 if (T.isNull())
5077 return nullptr;
5078
5079 TypeSourceInfo *TypeInfo = Importer.Import(E->getTypeSourceInfo());
5080 if (!TypeInfo)
5081 return nullptr;
5082
5083 return new (Importer.getToContext()) CXXScalarValueInitExpr(
5084 T, TypeInfo, Importer.Import(E->getRParenLoc()));
5085}
5086
5087Expr *ASTNodeImporter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
5088 Expr *SubExpr = Importer.Import(E->getSubExpr());
5089 if (!SubExpr)
5090 return nullptr;
5091
5092 auto *Dtor = cast_or_null<CXXDestructorDecl>(
5093 Importer.Import(const_cast<CXXDestructorDecl *>(
5094 E->getTemporary()->getDestructor())));
5095 if (!Dtor)
5096 return nullptr;
5097
5098 ASTContext &ToCtx = Importer.getToContext();
5099 CXXTemporary *Temp = CXXTemporary::Create(ToCtx, Dtor);
5100 return CXXBindTemporaryExpr::Create(ToCtx, Temp, SubExpr);
5101}
5102
5103Expr *ASTNodeImporter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *CE) {
5104 QualType T = Importer.Import(CE->getType());
5105 if (T.isNull())
5106 return nullptr;
5107
5108 SmallVector<Expr *, 8> Args(CE->getNumArgs());
5109 if (ImportContainerChecked(CE->arguments(), Args))
5110 return nullptr;
5111
5112 auto *Ctor = cast_or_null<CXXConstructorDecl>(
5113 Importer.Import(CE->getConstructor()));
5114 if (!Ctor)
5115 return nullptr;
5116
5117 return CXXTemporaryObjectExpr::Create(
5118 Importer.getToContext(), T,
5119 Importer.Import(CE->getLocStart()),
5120 Ctor,
5121 CE->isElidable(),
5122 Args,
5123 CE->hadMultipleCandidates(),
5124 CE->isListInitialization(),
5125 CE->isStdInitListInitialization(),
5126 CE->requiresZeroInitialization(),
5127 CE->getConstructionKind(),
5128 Importer.Import(CE->getParenOrBraceRange()));
5129}
5130
5131Expr *
5132ASTNodeImporter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
5133 QualType T = Importer.Import(E->getType());
5134 if (T.isNull())
5135 return nullptr;
5136
5137 Expr *TempE = Importer.Import(E->GetTemporaryExpr());
5138 if (!TempE)
5139 return nullptr;
5140
5141 ValueDecl *ExtendedBy = cast_or_null<ValueDecl>(
5142 Importer.Import(const_cast<ValueDecl *>(E->getExtendingDecl())));
5143 if (!ExtendedBy && E->getExtendingDecl())
5144 return nullptr;
5145
5146 auto *ToMTE = new (Importer.getToContext()) MaterializeTemporaryExpr(
5147 T, TempE, E->isBoundToLvalueReference());
5148
5149 // FIXME: Should ManglingNumber get numbers associated with 'to' context?
5150 ToMTE->setExtendingDecl(ExtendedBy, E->getManglingNumber());
5151 return ToMTE;
5152}
5153
5154Expr *ASTNodeImporter::VisitCXXNewExpr(CXXNewExpr *CE) {
5155 QualType T = Importer.Import(CE->getType());
5156 if (T.isNull())
5157 return nullptr;
5158
5159 SmallVector<Expr *, 4> PlacementArgs(CE->getNumPlacementArgs());
5160 if (ImportContainerChecked(CE->placement_arguments(), PlacementArgs))
5161 return nullptr;
5162
5163 FunctionDecl *OperatorNewDecl = cast_or_null<FunctionDecl>(
5164 Importer.Import(CE->getOperatorNew()));
5165 if (!OperatorNewDecl && CE->getOperatorNew())
5166 return nullptr;
5167
5168 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5169 Importer.Import(CE->getOperatorDelete()));
5170 if (!OperatorDeleteDecl && CE->getOperatorDelete())
5171 return nullptr;
5172
5173 Expr *ToInit = Importer.Import(CE->getInitializer());
5174 if (!ToInit && CE->getInitializer())
5175 return nullptr;
5176
5177 TypeSourceInfo *TInfo = Importer.Import(CE->getAllocatedTypeSourceInfo());
5178 if (!TInfo)
5179 return nullptr;
5180
5181 Expr *ToArrSize = Importer.Import(CE->getArraySize());
5182 if (!ToArrSize && CE->getArraySize())
5183 return nullptr;
5184
5185 return new (Importer.getToContext()) CXXNewExpr(
5186 Importer.getToContext(),
5187 CE->isGlobalNew(),
5188 OperatorNewDecl, OperatorDeleteDecl,
Richard Smithb2f0f052016-10-10 18:54:32 +00005189 CE->passAlignment(),
Aleksei Sidorina693b372016-09-28 10:16:56 +00005190 CE->doesUsualArrayDeleteWantSize(),
5191 PlacementArgs,
5192 Importer.Import(CE->getTypeIdParens()),
5193 ToArrSize, CE->getInitializationStyle(), ToInit, T, TInfo,
5194 Importer.Import(CE->getSourceRange()),
5195 Importer.Import(CE->getDirectInitRange()));
5196}
5197
5198Expr *ASTNodeImporter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
5199 QualType T = Importer.Import(E->getType());
5200 if (T.isNull())
5201 return nullptr;
5202
5203 FunctionDecl *OperatorDeleteDecl = cast_or_null<FunctionDecl>(
5204 Importer.Import(E->getOperatorDelete()));
5205 if (!OperatorDeleteDecl && E->getOperatorDelete())
5206 return nullptr;
5207
5208 Expr *ToArg = Importer.Import(E->getArgument());
5209 if (!ToArg && E->getArgument())
5210 return nullptr;
5211
5212 return new (Importer.getToContext()) CXXDeleteExpr(
5213 T, E->isGlobalDelete(),
5214 E->isArrayForm(),
5215 E->isArrayFormAsWritten(),
5216 E->doesUsualArrayDeleteWantSize(),
5217 OperatorDeleteDecl,
5218 ToArg,
5219 Importer.Import(E->getLocStart()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005220}
5221
Sean Callanan59721b32015-04-28 18:41:46 +00005222Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5223 QualType T = Importer.Import(E->getType());
5224 if (T.isNull())
5225 return nullptr;
5226
5227 CXXConstructorDecl *ToCCD =
Sean Callanandd2c1742016-05-16 20:48:03 +00005228 dyn_cast_or_null<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005229 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005230 return nullptr;
5231
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005232 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005233 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005234 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005235
5236 return CXXConstructExpr::Create(Importer.getToContext(), T,
5237 Importer.Import(E->getLocation()),
Richard Smithc83bf822016-06-10 00:58:19 +00005238 ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005239 ToArgs, E->hadMultipleCandidates(),
5240 E->isListInitialization(),
5241 E->isStdInitListInitialization(),
5242 E->requiresZeroInitialization(),
5243 E->getConstructionKind(),
5244 Importer.Import(E->getParenOrBraceRange()));
5245}
5246
Aleksei Sidorina693b372016-09-28 10:16:56 +00005247Expr *ASTNodeImporter::VisitExprWithCleanups(ExprWithCleanups *EWC) {
5248 Expr *SubExpr = Importer.Import(EWC->getSubExpr());
5249 if (!SubExpr && EWC->getSubExpr())
5250 return nullptr;
5251
5252 SmallVector<ExprWithCleanups::CleanupObject, 8> Objs(EWC->getNumObjects());
5253 for (unsigned I = 0, E = EWC->getNumObjects(); I < E; I++)
5254 if (ExprWithCleanups::CleanupObject Obj =
5255 cast_or_null<BlockDecl>(Importer.Import(EWC->getObject(I))))
5256 Objs[I] = Obj;
5257 else
5258 return nullptr;
5259
5260 return ExprWithCleanups::Create(Importer.getToContext(),
5261 SubExpr, EWC->cleanupsHaveSideEffects(),
5262 Objs);
5263}
5264
Sean Callanan8bca9962016-03-28 21:43:01 +00005265Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5266 QualType T = Importer.Import(E->getType());
5267 if (T.isNull())
5268 return nullptr;
5269
5270 Expr *ToFn = Importer.Import(E->getCallee());
5271 if (!ToFn)
5272 return nullptr;
5273
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005274 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005275 if (ImportContainerChecked(E->arguments(), ToArgs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005276 return nullptr;
5277
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005278 return new (Importer.getToContext()) CXXMemberCallExpr(
5279 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5280 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005281}
5282
5283Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5284 QualType T = Importer.Import(E->getType());
5285 if (T.isNull())
5286 return nullptr;
5287
5288 return new (Importer.getToContext())
5289 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5290}
5291
5292Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5293 QualType T = Importer.Import(E->getType());
5294 if (T.isNull())
5295 return nullptr;
5296
5297 return new (Importer.getToContext())
5298 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5299}
5300
5301
Sean Callanan59721b32015-04-28 18:41:46 +00005302Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5303 QualType T = Importer.Import(E->getType());
5304 if (T.isNull())
5305 return nullptr;
5306
5307 Expr *ToBase = Importer.Import(E->getBase());
5308 if (!ToBase && E->getBase())
5309 return nullptr;
5310
5311 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5312 if (!ToMember && E->getMemberDecl())
5313 return nullptr;
5314
5315 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5316 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5317 E->getFoundDecl().getAccess());
5318
5319 DeclarationNameInfo ToMemberNameInfo(
5320 Importer.Import(E->getMemberNameInfo().getName()),
5321 Importer.Import(E->getMemberNameInfo().getLoc()));
5322
5323 if (E->hasExplicitTemplateArgs()) {
5324 return nullptr; // FIXME: handle template arguments
5325 }
5326
5327 return MemberExpr::Create(Importer.getToContext(), ToBase,
5328 E->isArrow(),
5329 Importer.Import(E->getOperatorLoc()),
5330 Importer.Import(E->getQualifierLoc()),
5331 Importer.Import(E->getTemplateKeywordLoc()),
5332 ToMember, ToFoundDecl, ToMemberNameInfo,
5333 nullptr, T, E->getValueKind(),
5334 E->getObjectKind());
5335}
5336
5337Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5338 QualType T = Importer.Import(E->getType());
5339 if (T.isNull())
5340 return nullptr;
5341
5342 Expr *ToCallee = Importer.Import(E->getCallee());
5343 if (!ToCallee && E->getCallee())
5344 return nullptr;
5345
5346 unsigned NumArgs = E->getNumArgs();
5347
5348 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5349
5350 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5351 Expr *FromArg = E->getArg(ai);
5352 Expr *ToArg = Importer.Import(FromArg);
5353 if (!ToArg)
5354 return nullptr;
5355 ToArgs[ai] = ToArg;
5356 }
5357
5358 Expr **ToArgs_Copied = new (Importer.getToContext())
5359 Expr*[NumArgs];
5360
5361 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5362 ToArgs_Copied[ai] = ToArgs[ai];
5363
5364 return new (Importer.getToContext())
5365 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005366 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005367 Importer.Import(E->getRParenLoc()));
5368}
5369
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005370Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5371 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005372 if (T.isNull())
5373 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005374
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005375 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
Aleksei Sidorina693b372016-09-28 10:16:56 +00005376 if (ImportContainerChecked(ILE->inits(), Exprs))
Sean Callanan8bca9962016-03-28 21:43:01 +00005377 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005378
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005379 ASTContext &ToCtx = Importer.getToContext();
5380 InitListExpr *To = new (ToCtx) InitListExpr(
5381 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5382 Exprs, Importer.Import(ILE->getLBraceLoc()));
5383 To->setType(T);
5384
5385 if (ILE->hasArrayFiller()) {
5386 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5387 if (!Filler)
5388 return nullptr;
5389 To->setArrayFiller(Filler);
5390 }
5391
5392 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5393 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5394 if (!ToFD)
5395 return nullptr;
5396 To->setInitializedFieldInUnion(ToFD);
5397 }
5398
5399 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5400 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5401 Importer.Import(SyntForm));
5402 if (!ToSyntForm)
5403 return nullptr;
5404 To->setSyntacticForm(ToSyntForm);
5405 }
5406
5407 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5408 To->setValueDependent(ILE->isValueDependent());
5409 To->setInstantiationDependent(ILE->isInstantiationDependent());
5410
5411 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005412}
5413
Richard Smith30e304e2016-12-14 00:03:17 +00005414Expr *ASTNodeImporter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
5415 QualType ToType = Importer.Import(E->getType());
5416 if (ToType.isNull())
5417 return nullptr;
5418
5419 Expr *ToCommon = Importer.Import(E->getCommonExpr());
5420 if (!ToCommon && E->getCommonExpr())
5421 return nullptr;
5422
5423 Expr *ToSubExpr = Importer.Import(E->getSubExpr());
5424 if (!ToSubExpr && E->getSubExpr())
5425 return nullptr;
5426
5427 return new (Importer.getToContext())
5428 ArrayInitLoopExpr(ToType, ToCommon, ToSubExpr);
5429}
5430
5431Expr *ASTNodeImporter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
5432 QualType ToType = Importer.Import(E->getType());
5433 if (ToType.isNull())
5434 return nullptr;
5435 return new (Importer.getToContext()) ArrayInitIndexExpr(ToType);
5436}
5437
Sean Callanandd2c1742016-05-16 20:48:03 +00005438Expr *ASTNodeImporter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
5439 FieldDecl *ToField = llvm::dyn_cast_or_null<FieldDecl>(
5440 Importer.Import(DIE->getField()));
5441 if (!ToField && DIE->getField())
5442 return nullptr;
5443
5444 return CXXDefaultInitExpr::Create(
5445 Importer.getToContext(), Importer.Import(DIE->getLocStart()), ToField);
5446}
5447
5448Expr *ASTNodeImporter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
5449 QualType ToType = Importer.Import(E->getType());
5450 if (ToType.isNull() && !E->getType().isNull())
5451 return nullptr;
5452 ExprValueKind VK = E->getValueKind();
5453 CastKind CK = E->getCastKind();
5454 Expr *ToOp = Importer.Import(E->getSubExpr());
5455 if (!ToOp && E->getSubExpr())
5456 return nullptr;
5457 CXXCastPath BasePath;
5458 if (ImportCastPath(E, BasePath))
5459 return nullptr;
5460 TypeSourceInfo *ToWritten = Importer.Import(E->getTypeInfoAsWritten());
5461 SourceLocation ToOperatorLoc = Importer.Import(E->getOperatorLoc());
5462 SourceLocation ToRParenLoc = Importer.Import(E->getRParenLoc());
5463 SourceRange ToAngleBrackets = Importer.Import(E->getAngleBrackets());
5464
5465 if (isa<CXXStaticCastExpr>(E)) {
5466 return CXXStaticCastExpr::Create(
5467 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5468 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5469 } else if (isa<CXXDynamicCastExpr>(E)) {
5470 return CXXDynamicCastExpr::Create(
5471 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5472 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5473 } else if (isa<CXXReinterpretCastExpr>(E)) {
5474 return CXXReinterpretCastExpr::Create(
5475 Importer.getToContext(), ToType, VK, CK, ToOp, &BasePath,
5476 ToWritten, ToOperatorLoc, ToRParenLoc, ToAngleBrackets);
5477 } else {
5478 return nullptr;
5479 }
5480}
5481
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005482
5483Expr *ASTNodeImporter::VisitSubstNonTypeTemplateParmExpr(
5484 SubstNonTypeTemplateParmExpr *E) {
5485 QualType T = Importer.Import(E->getType());
5486 if (T.isNull())
5487 return nullptr;
5488
5489 NonTypeTemplateParmDecl *Param = cast_or_null<NonTypeTemplateParmDecl>(
5490 Importer.Import(E->getParameter()));
5491 if (!Param)
5492 return nullptr;
5493
5494 Expr *Replacement = Importer.Import(E->getReplacement());
5495 if (!Replacement)
5496 return nullptr;
5497
5498 return new (Importer.getToContext()) SubstNonTypeTemplateParmExpr(
5499 T, E->getValueKind(), Importer.Import(E->getExprLoc()), Param,
5500 Replacement);
5501}
5502
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005503ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005504 ASTContext &FromContext, FileManager &FromFileManager,
5505 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005506 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005507 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005508 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005509{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005510 ImportedDecls[FromContext.getTranslationUnitDecl()]
5511 = ToContext.getTranslationUnitDecl();
5512}
5513
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005514ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005515
5516QualType ASTImporter::Import(QualType FromT) {
5517 if (FromT.isNull())
5518 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005519
5520 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005521
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005522 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005523 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5524 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005525 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005526 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005527
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005528 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005529 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005530 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005531 if (ToT.isNull())
5532 return ToT;
5533
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005534 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005535 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005536
John McCall424cec92011-01-19 06:33:43 +00005537 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005538}
5539
Douglas Gregor62d311f2010-02-09 19:21:46 +00005540TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005541 if (!FromTSI)
5542 return FromTSI;
5543
5544 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005545 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005546 QualType T = Import(FromTSI->getType());
5547 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005548 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005549
5550 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005551 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005552}
5553
Sean Callanan59721b32015-04-28 18:41:46 +00005554Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5555 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5556 if (Pos != ImportedDecls.end()) {
5557 Decl *ToD = Pos->second;
5558 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5559 return ToD;
5560 } else {
5561 return nullptr;
5562 }
5563}
5564
Douglas Gregor62d311f2010-02-09 19:21:46 +00005565Decl *ASTImporter::Import(Decl *FromD) {
5566 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005567 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005568
Douglas Gregord451ea92011-07-29 23:31:30 +00005569 ASTNodeImporter Importer(*this);
5570
Douglas Gregor62d311f2010-02-09 19:21:46 +00005571 // Check whether we've already imported this declaration.
5572 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005573 if (Pos != ImportedDecls.end()) {
5574 Decl *ToD = Pos->second;
5575 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5576 return ToD;
5577 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005578
5579 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005580 Decl *ToD = Importer.Visit(FromD);
5581 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005582 return nullptr;
5583
Douglas Gregor62d311f2010-02-09 19:21:46 +00005584 // Record the imported declaration.
5585 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005586
5587 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5588 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005589 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005590 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005591 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005592 // When we've finished transforming a typedef, see whether it was the
5593 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005594 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005595 FromTag = AnonTagsWithPendingTypedefs.begin(),
5596 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5597 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005598 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005599 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5600 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005601 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005602 AnonTagsWithPendingTypedefs.erase(FromTag);
5603 break;
5604 }
5605 }
5606 }
5607 }
5608
Douglas Gregor62d311f2010-02-09 19:21:46 +00005609 return ToD;
5610}
5611
5612DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5613 if (!FromDC)
5614 return FromDC;
5615
Douglas Gregor95d82832012-01-24 18:36:04 +00005616 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005617 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005618 return nullptr;
5619
Douglas Gregor2e15c842012-02-01 21:00:38 +00005620 // When we're using a record/enum/Objective-C class/protocol as a context, we
5621 // need it to have a definition.
5622 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005623 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005624 if (ToRecord->isCompleteDefinition()) {
5625 // Do nothing.
5626 } else if (FromRecord->isCompleteDefinition()) {
5627 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5628 ASTNodeImporter::IDK_Basic);
5629 } else {
5630 CompleteDecl(ToRecord);
5631 }
5632 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5633 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5634 if (ToEnum->isCompleteDefinition()) {
5635 // Do nothing.
5636 } else if (FromEnum->isCompleteDefinition()) {
5637 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5638 ASTNodeImporter::IDK_Basic);
5639 } else {
5640 CompleteDecl(ToEnum);
5641 }
5642 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5643 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5644 if (ToClass->getDefinition()) {
5645 // Do nothing.
5646 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5647 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5648 ASTNodeImporter::IDK_Basic);
5649 } else {
5650 CompleteDecl(ToClass);
5651 }
5652 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5653 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5654 if (ToProto->getDefinition()) {
5655 // Do nothing.
5656 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5657 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5658 ASTNodeImporter::IDK_Basic);
5659 } else {
5660 CompleteDecl(ToProto);
5661 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005662 }
5663
5664 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005665}
5666
5667Expr *ASTImporter::Import(Expr *FromE) {
5668 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005669 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005670
5671 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5672}
5673
5674Stmt *ASTImporter::Import(Stmt *FromS) {
5675 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005676 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005677
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005678 // Check whether we've already imported this declaration.
5679 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5680 if (Pos != ImportedStmts.end())
5681 return Pos->second;
5682
5683 // Import the type
5684 ASTNodeImporter Importer(*this);
5685 Stmt *ToS = Importer.Visit(FromS);
5686 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005687 return nullptr;
5688
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005689 // Record the imported declaration.
5690 ImportedStmts[FromS] = ToS;
5691 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005692}
5693
5694NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5695 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005696 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005697
Douglas Gregor90ebf252011-04-27 16:48:40 +00005698 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5699
5700 switch (FromNNS->getKind()) {
5701 case NestedNameSpecifier::Identifier:
5702 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5703 return NestedNameSpecifier::Create(ToContext, prefix, II);
5704 }
Craig Topper36250ad2014-05-12 05:36:57 +00005705 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005706
5707 case NestedNameSpecifier::Namespace:
5708 if (NamespaceDecl *NS =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005709 cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00005710 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5711 }
Craig Topper36250ad2014-05-12 05:36:57 +00005712 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005713
5714 case NestedNameSpecifier::NamespaceAlias:
5715 if (NamespaceAliasDecl *NSAD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005716 cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
Douglas Gregor90ebf252011-04-27 16:48:40 +00005717 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5718 }
Craig Topper36250ad2014-05-12 05:36:57 +00005719 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005720
5721 case NestedNameSpecifier::Global:
5722 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5723
Nikola Smiljanic67860242014-09-26 00:28:20 +00005724 case NestedNameSpecifier::Super:
5725 if (CXXRecordDecl *RD =
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005726 cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
Nikola Smiljanic67860242014-09-26 00:28:20 +00005727 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5728 }
5729 return nullptr;
5730
Douglas Gregor90ebf252011-04-27 16:48:40 +00005731 case NestedNameSpecifier::TypeSpec:
5732 case NestedNameSpecifier::TypeSpecWithTemplate: {
5733 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5734 if (!T.isNull()) {
5735 bool bTemplate = FromNNS->getKind() ==
5736 NestedNameSpecifier::TypeSpecWithTemplate;
5737 return NestedNameSpecifier::Create(ToContext, prefix,
5738 bTemplate, T.getTypePtr());
5739 }
5740 }
Craig Topper36250ad2014-05-12 05:36:57 +00005741 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005742 }
5743
5744 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005745}
5746
Douglas Gregor14454802011-02-25 02:25:35 +00005747NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
Aleksei Sidorin855086d2017-01-23 09:30:36 +00005748 // Copied from NestedNameSpecifier mostly.
5749 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5750 NestedNameSpecifierLoc NNS = FromNNS;
5751
5752 // Push each of the nested-name-specifiers's onto a stack for
5753 // serialization in reverse order.
5754 while (NNS) {
5755 NestedNames.push_back(NNS);
5756 NNS = NNS.getPrefix();
5757 }
5758
5759 NestedNameSpecifierLocBuilder Builder;
5760
5761 while (!NestedNames.empty()) {
5762 NNS = NestedNames.pop_back_val();
5763 NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier());
5764 if (!Spec)
5765 return NestedNameSpecifierLoc();
5766
5767 NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
5768 switch (Kind) {
5769 case NestedNameSpecifier::Identifier:
5770 Builder.Extend(getToContext(),
5771 Spec->getAsIdentifier(),
5772 Import(NNS.getLocalBeginLoc()),
5773 Import(NNS.getLocalEndLoc()));
5774 break;
5775
5776 case NestedNameSpecifier::Namespace:
5777 Builder.Extend(getToContext(),
5778 Spec->getAsNamespace(),
5779 Import(NNS.getLocalBeginLoc()),
5780 Import(NNS.getLocalEndLoc()));
5781 break;
5782
5783 case NestedNameSpecifier::NamespaceAlias:
5784 Builder.Extend(getToContext(),
5785 Spec->getAsNamespaceAlias(),
5786 Import(NNS.getLocalBeginLoc()),
5787 Import(NNS.getLocalEndLoc()));
5788 break;
5789
5790 case NestedNameSpecifier::TypeSpec:
5791 case NestedNameSpecifier::TypeSpecWithTemplate: {
5792 TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
5793 QualType(Spec->getAsType(), 0));
5794 Builder.Extend(getToContext(),
5795 Import(NNS.getLocalBeginLoc()),
5796 TSI->getTypeLoc(),
5797 Import(NNS.getLocalEndLoc()));
5798 break;
5799 }
5800
5801 case NestedNameSpecifier::Global:
5802 Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc()));
5803 break;
5804
5805 case NestedNameSpecifier::Super: {
5806 SourceRange ToRange = Import(NNS.getSourceRange());
5807 Builder.MakeSuper(getToContext(),
5808 Spec->getAsRecordDecl(),
5809 ToRange.getBegin(),
5810 ToRange.getEnd());
5811 }
5812 }
5813 }
5814
5815 return Builder.getWithLocInContext(getToContext());
Douglas Gregor14454802011-02-25 02:25:35 +00005816}
5817
Douglas Gregore2e50d332010-12-01 01:36:18 +00005818TemplateName ASTImporter::Import(TemplateName From) {
5819 switch (From.getKind()) {
5820 case TemplateName::Template:
5821 if (TemplateDecl *ToTemplate
5822 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5823 return TemplateName(ToTemplate);
5824
5825 return TemplateName();
5826
5827 case TemplateName::OverloadedTemplate: {
5828 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5829 UnresolvedSet<2> ToTemplates;
5830 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5831 E = FromStorage->end();
5832 I != E; ++I) {
5833 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5834 ToTemplates.addDecl(To);
5835 else
5836 return TemplateName();
5837 }
5838 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5839 ToTemplates.end());
5840 }
5841
5842 case TemplateName::QualifiedTemplate: {
5843 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5844 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5845 if (!Qualifier)
5846 return TemplateName();
5847
5848 if (TemplateDecl *ToTemplate
5849 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5850 return ToContext.getQualifiedTemplateName(Qualifier,
5851 QTN->hasTemplateKeyword(),
5852 ToTemplate);
5853
5854 return TemplateName();
5855 }
5856
5857 case TemplateName::DependentTemplate: {
5858 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5859 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5860 if (!Qualifier)
5861 return TemplateName();
5862
5863 if (DTN->isIdentifier()) {
5864 return ToContext.getDependentTemplateName(Qualifier,
5865 Import(DTN->getIdentifier()));
5866 }
5867
5868 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5869 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005870
5871 case TemplateName::SubstTemplateTemplateParm: {
5872 SubstTemplateTemplateParmStorage *subst
5873 = From.getAsSubstTemplateTemplateParm();
5874 TemplateTemplateParmDecl *param
5875 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5876 if (!param)
5877 return TemplateName();
5878
5879 TemplateName replacement = Import(subst->getReplacement());
5880 if (replacement.isNull()) return TemplateName();
5881
5882 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5883 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005884
5885 case TemplateName::SubstTemplateTemplateParmPack: {
5886 SubstTemplateTemplateParmPackStorage *SubstPack
5887 = From.getAsSubstTemplateTemplateParmPack();
5888 TemplateTemplateParmDecl *Param
5889 = cast_or_null<TemplateTemplateParmDecl>(
5890 Import(SubstPack->getParameterPack()));
5891 if (!Param)
5892 return TemplateName();
5893
5894 ASTNodeImporter Importer(*this);
5895 TemplateArgument ArgPack
5896 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5897 if (ArgPack.isNull())
5898 return TemplateName();
5899
5900 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5901 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005902 }
5903
5904 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005905}
5906
Douglas Gregor62d311f2010-02-09 19:21:46 +00005907SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5908 if (FromLoc.isInvalid())
5909 return SourceLocation();
5910
Douglas Gregor811663e2010-02-10 00:15:17 +00005911 SourceManager &FromSM = FromContext.getSourceManager();
5912
Sean Callanan24c5fe62016-11-07 20:42:25 +00005913 // For now, map everything down to its file location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005914 // don't have to import macro expansions.
5915 // FIXME: Import macro expansions!
Sean Callanan24c5fe62016-11-07 20:42:25 +00005916 FromLoc = FromSM.getFileLoc(FromLoc);
Douglas Gregor811663e2010-02-10 00:15:17 +00005917 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5918 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005919 FileID ToFileID = Import(Decomposed.first);
5920 if (ToFileID.isInvalid())
5921 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005922 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5923 .getLocWithOffset(Decomposed.second);
5924 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005925}
5926
5927SourceRange ASTImporter::Import(SourceRange FromRange) {
5928 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5929}
5930
Douglas Gregor811663e2010-02-10 00:15:17 +00005931FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005932 llvm::DenseMap<FileID, FileID>::iterator Pos
5933 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005934 if (Pos != ImportedFileIDs.end())
5935 return Pos->second;
5936
5937 SourceManager &FromSM = FromContext.getSourceManager();
5938 SourceManager &ToSM = ToContext.getSourceManager();
5939 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005940 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005941
5942 // Include location of this file.
5943 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5944
5945 // Map the FileID for to the "to" source manager.
5946 FileID ToID;
5947 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005948 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005949 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5950 // disk again
5951 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5952 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005953 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005954 if (!Entry)
5955 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005956 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5957 FromSLoc.getFile().getFileCharacteristic());
5958 } else {
5959 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005960 const llvm::MemoryBuffer *
5961 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005962 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005963 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005964 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005965 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005966 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005967 }
5968
5969
Sebastian Redl99219f12010-09-30 01:03:06 +00005970 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005971 return ToID;
5972}
5973
Sean Callanandd2c1742016-05-16 20:48:03 +00005974CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
5975 Expr *ToExpr = Import(From->getInit());
5976 if (!ToExpr && From->getInit())
5977 return nullptr;
5978
5979 if (From->isBaseInitializer()) {
5980 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
5981 if (!ToTInfo && From->getTypeSourceInfo())
5982 return nullptr;
5983
5984 return new (ToContext) CXXCtorInitializer(
5985 ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()),
5986 ToExpr, Import(From->getRParenLoc()),
5987 From->isPackExpansion() ? Import(From->getEllipsisLoc())
5988 : SourceLocation());
5989 } else if (From->isMemberInitializer()) {
5990 FieldDecl *ToField =
5991 llvm::cast_or_null<FieldDecl>(Import(From->getMember()));
5992 if (!ToField && From->getMember())
5993 return nullptr;
5994
5995 return new (ToContext) CXXCtorInitializer(
5996 ToContext, ToField, Import(From->getMemberLocation()),
5997 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
5998 } else if (From->isIndirectMemberInitializer()) {
5999 IndirectFieldDecl *ToIField = llvm::cast_or_null<IndirectFieldDecl>(
6000 Import(From->getIndirectMember()));
6001 if (!ToIField && From->getIndirectMember())
6002 return nullptr;
6003
6004 return new (ToContext) CXXCtorInitializer(
6005 ToContext, ToIField, Import(From->getMemberLocation()),
6006 Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc()));
6007 } else if (From->isDelegatingInitializer()) {
6008 TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo());
6009 if (!ToTInfo && From->getTypeSourceInfo())
6010 return nullptr;
6011
6012 return new (ToContext)
6013 CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()),
6014 ToExpr, Import(From->getRParenLoc()));
Sean Callanandd2c1742016-05-16 20:48:03 +00006015 } else {
6016 return nullptr;
6017 }
6018}
6019
6020
Aleksei Sidorina693b372016-09-28 10:16:56 +00006021CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
6022 auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
6023 if (Pos != ImportedCXXBaseSpecifiers.end())
6024 return Pos->second;
6025
6026 CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
6027 Import(BaseSpec->getSourceRange()),
6028 BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
6029 BaseSpec->getAccessSpecifierAsWritten(),
6030 Import(BaseSpec->getTypeSourceInfo()),
6031 Import(BaseSpec->getEllipsisLoc()));
6032 ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
6033 return Imported;
6034}
6035
Douglas Gregor0a791672011-01-18 03:11:38 +00006036void ASTImporter::ImportDefinition(Decl *From) {
6037 Decl *To = Import(From);
6038 if (!To)
6039 return;
6040
6041 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6042 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006043
6044 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6045 if (!ToRecord->getDefinition()) {
6046 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006047 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006048 return;
6049 }
6050 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006051
6052 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6053 if (!ToEnum->getDefinition()) {
6054 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006055 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006056 return;
6057 }
6058 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006059
6060 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6061 if (!ToIFace->getDefinition()) {
6062 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006063 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006064 return;
6065 }
6066 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006067
Douglas Gregor2aa53772012-01-24 17:42:07 +00006068 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6069 if (!ToProto->getDefinition()) {
6070 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006071 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006072 return;
6073 }
6074 }
6075
Douglas Gregor0a791672011-01-18 03:11:38 +00006076 Importer.ImportDeclContext(FromDC, true);
6077 }
6078}
6079
Douglas Gregor96e578d2010-02-05 17:54:41 +00006080DeclarationName ASTImporter::Import(DeclarationName FromName) {
6081 if (!FromName)
6082 return DeclarationName();
6083
6084 switch (FromName.getNameKind()) {
6085 case DeclarationName::Identifier:
6086 return Import(FromName.getAsIdentifierInfo());
6087
6088 case DeclarationName::ObjCZeroArgSelector:
6089 case DeclarationName::ObjCOneArgSelector:
6090 case DeclarationName::ObjCMultiArgSelector:
6091 return Import(FromName.getObjCSelector());
6092
6093 case DeclarationName::CXXConstructorName: {
6094 QualType T = Import(FromName.getCXXNameType());
6095 if (T.isNull())
6096 return DeclarationName();
6097
6098 return ToContext.DeclarationNames.getCXXConstructorName(
6099 ToContext.getCanonicalType(T));
6100 }
6101
6102 case DeclarationName::CXXDestructorName: {
6103 QualType T = Import(FromName.getCXXNameType());
6104 if (T.isNull())
6105 return DeclarationName();
6106
6107 return ToContext.DeclarationNames.getCXXDestructorName(
6108 ToContext.getCanonicalType(T));
6109 }
6110
Richard Smith35845152017-02-07 01:37:30 +00006111 case DeclarationName::CXXDeductionGuideName: {
6112 TemplateDecl *Template = cast_or_null<TemplateDecl>(
6113 Import(FromName.getCXXDeductionGuideTemplate()));
6114 if (!Template)
6115 return DeclarationName();
6116 return ToContext.DeclarationNames.getCXXDeductionGuideName(Template);
6117 }
6118
Douglas Gregor96e578d2010-02-05 17:54:41 +00006119 case DeclarationName::CXXConversionFunctionName: {
6120 QualType T = Import(FromName.getCXXNameType());
6121 if (T.isNull())
6122 return DeclarationName();
6123
6124 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6125 ToContext.getCanonicalType(T));
6126 }
6127
6128 case DeclarationName::CXXOperatorName:
6129 return ToContext.DeclarationNames.getCXXOperatorName(
6130 FromName.getCXXOverloadedOperator());
6131
6132 case DeclarationName::CXXLiteralOperatorName:
6133 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6134 Import(FromName.getCXXLiteralIdentifier()));
6135
6136 case DeclarationName::CXXUsingDirective:
6137 // FIXME: STATICS!
6138 return DeclarationName::getUsingDirectiveName();
6139 }
6140
David Blaikiee4d798f2012-01-20 21:50:17 +00006141 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006142}
6143
Douglas Gregore2e50d332010-12-01 01:36:18 +00006144IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006145 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006146 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006147
Sean Callananf94ef1d2016-05-14 06:11:19 +00006148 IdentifierInfo *ToId = &ToContext.Idents.get(FromId->getName());
6149
6150 if (!ToId->getBuiltinID() && FromId->getBuiltinID())
6151 ToId->setBuiltinID(FromId->getBuiltinID());
6152
6153 return ToId;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006154}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006155
Douglas Gregor43f54792010-02-17 02:12:47 +00006156Selector ASTImporter::Import(Selector FromSel) {
6157 if (FromSel.isNull())
6158 return Selector();
6159
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006160 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006161 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6162 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6163 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6164 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6165}
6166
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006167DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6168 DeclContext *DC,
6169 unsigned IDNS,
6170 NamedDecl **Decls,
6171 unsigned NumDecls) {
6172 return Name;
6173}
6174
6175DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006176 if (LastDiagFromFrom)
6177 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6178 FromContext.getDiagnostics());
6179 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006180 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006181}
6182
6183DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006184 if (!LastDiagFromFrom)
6185 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6186 ToContext.getDiagnostics());
6187 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006188 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006189}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006190
Douglas Gregor2e15c842012-02-01 21:00:38 +00006191void ASTImporter::CompleteDecl (Decl *D) {
6192 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6193 if (!ID->getDefinition())
6194 ID->startDefinition();
6195 }
6196 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6197 if (!PD->getDefinition())
6198 PD->startDefinition();
6199 }
6200 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6201 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6202 TD->startDefinition();
6203 TD->setCompleteDefinition(true);
6204 }
6205 }
6206 else {
6207 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6208 }
6209}
6210
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006211Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006212 if (From->hasAttrs()) {
6213 for (Attr *FromAttr : From->getAttrs())
6214 To->addAttr(FromAttr->clone(To->getASTContext()));
6215 }
6216 if (From->isUsed()) {
6217 To->setIsUsed();
6218 }
Sean Callanandd2c1742016-05-16 20:48:03 +00006219 if (From->isImplicit()) {
6220 To->setImplicit();
6221 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006222 ImportedDecls[From] = To;
6223 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006224}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006225
Douglas Gregordd6006f2012-07-17 21:16:27 +00006226bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6227 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006228 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006229 = ImportedTypes.find(From.getTypePtr());
6230 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6231 return true;
Bruno Cardoso Lopes95ff11b2017-04-28 00:31:30 +00006232
Douglas Gregordd6006f2012-07-17 21:16:27 +00006233 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6234 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006235 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006236}