blob: 484a4459395f8f60e603168124e7b95bf618f1a8 [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"
Douglas Gregor5c73e912010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
32
33 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
42 QualType VisitBuiltinType(const BuiltinType *T);
43 QualType VisitComplexType(const ComplexType *T);
44 QualType VisitPointerType(const PointerType *T);
45 QualType VisitBlockPointerType(const BlockPointerType *T);
46 QualType VisitLValueReferenceType(const LValueReferenceType *T);
47 QualType VisitRValueReferenceType(const RValueReferenceType *T);
48 QualType VisitMemberPointerType(const MemberPointerType *T);
49 QualType VisitConstantArrayType(const ConstantArrayType *T);
50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
51 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000070 QualType VisitAttributedType(const AttributedType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
Douglas Gregor95d82832012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000084 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000089
Douglas Gregor95d82832012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregord451ea92011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000110 bool ImportDefinition(VarDecl *From, VarDecl *To,
111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000112 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000117 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000118 TemplateParameterList *ImportTemplateParameterList(
119 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
121 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
122 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
125 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
127 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000132 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000133 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000134 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000135 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000136 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000137 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000138 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000139 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000141 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000142 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000143 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
144 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
145 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
146 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000147 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000148 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000149 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000150 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000151 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000152 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000153 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000154 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000155 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000156 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000157 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000158
159 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000160 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000161 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000162 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000163 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000164 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000165 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
166 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
167 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
168 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000169 Decl *VisitClassTemplateSpecializationDecl(
170 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000171 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
172 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
173
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000174 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000175 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
176
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000177 Stmt *VisitStmt(Stmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000178 Stmt *VisitDeclStmt(DeclStmt *S);
179 Stmt *VisitNullStmt(NullStmt *S);
180 Stmt *VisitCompoundStmt(CompoundStmt *S);
181 Stmt *VisitCaseStmt(CaseStmt *S);
182 Stmt *VisitDefaultStmt(DefaultStmt *S);
183 Stmt *VisitLabelStmt(LabelStmt *S);
184 Stmt *VisitAttributedStmt(AttributedStmt *S);
185 Stmt *VisitIfStmt(IfStmt *S);
186 Stmt *VisitSwitchStmt(SwitchStmt *S);
187 Stmt *VisitWhileStmt(WhileStmt *S);
188 Stmt *VisitDoStmt(DoStmt *S);
189 Stmt *VisitForStmt(ForStmt *S);
190 Stmt *VisitGotoStmt(GotoStmt *S);
191 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
192 Stmt *VisitContinueStmt(ContinueStmt *S);
193 Stmt *VisitBreakStmt(BreakStmt *S);
194 Stmt *VisitReturnStmt(ReturnStmt *S);
195 // FIXME: GCCAsmStmt
196 // FIXME: MSAsmStmt
197 // FIXME: SEHExceptStmt
198 // FIXME: SEHFinallyStmt
199 // FIXME: SEHTryStmt
200 // FIXME: SEHLeaveStmt
201 // FIXME: CapturedStmt
202 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
203 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
204 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
205 // FIXME: MSDependentExistsStmt
206 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
207 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
208 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
209 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
210 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
211 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
212 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000213
214 // Importing expressions
215 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000216 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000217 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000218 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000219 Expr *VisitParenExpr(ParenExpr *E);
220 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000221 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000222 Expr *VisitBinaryOperator(BinaryOperator *E);
223 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000224 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000225 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000226 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000227 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
228 Expr *VisitCXXThisExpr(CXXThisExpr *E);
229 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000230 Expr *VisitMemberExpr(MemberExpr *E);
231 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000232 Expr *VisitInitListExpr(InitListExpr *E);
233
234 template <typename T, typename Iter> bool ImportArray(Iter B, Iter E, llvm::ArrayRef<T*> &ToArray) {
235 size_t NumElements = E - B;
236 SmallVector<T *, 1> ImportedElements(NumElements);
237 ASTImporter &_Importer = Importer;
238
239 bool Failed = false;
240 std::transform(B, E, ImportedElements.begin(),
241 [&_Importer, &Failed](T *Element) -> T* {
242 T *ToElement = _Importer.Import(Element);
243 if (Element && !ToElement)
244 Failed = true;
245 return ToElement;
246 });
247
248 if (Failed)
249 return false;
250
251 T **CopiedElements = new (Importer.getToContext()) T*[NumElements];
252 std::copy(ImportedElements.begin(), ImportedElements.end(), &CopiedElements[0]);
253 ToArray = llvm::ArrayRef<T*>(CopiedElements, NumElements);
254
255 return true;
256 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000257 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000258}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000259using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000260
261//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000262// Structural Equivalence
263//----------------------------------------------------------------------------
264
265namespace {
266 struct StructuralEquivalenceContext {
267 /// \brief AST contexts for which we are checking structural equivalence.
268 ASTContext &C1, &C2;
269
Douglas Gregor3996e242010-02-15 22:01:00 +0000270 /// \brief The set of "tentative" equivalences between two canonical
271 /// declarations, mapping from a declaration in the first context to the
272 /// declaration in the second context that we believe to be equivalent.
273 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
274
275 /// \brief Queue of declarations in the first context whose equivalence
276 /// with a declaration in the second context still needs to be verified.
277 std::deque<Decl *> DeclsToCheck;
278
Douglas Gregorb4964f72010-02-15 23:54:17 +0000279 /// \brief Declaration (from, to) pairs that are known not to be equivalent
280 /// (which we have already complained about).
281 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
282
Douglas Gregor3996e242010-02-15 22:01:00 +0000283 /// \brief Whether we're being strict about the spelling of types when
284 /// unifying two types.
285 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000286
287 /// \brief Whether to complain about failures.
288 bool Complain;
289
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000290 /// \brief \c true if the last diagnostic came from C2.
291 bool LastDiagFromC2;
292
Douglas Gregor3996e242010-02-15 22:01:00 +0000293 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000294 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000295 bool StrictTypeSpelling = false,
296 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000297 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000298 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
299 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000300
301 /// \brief Determine whether the two declarations are structurally
302 /// equivalent.
303 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
304
305 /// \brief Determine whether the two types are structurally equivalent.
306 bool IsStructurallyEquivalent(QualType T1, QualType T2);
307
308 private:
309 /// \brief Finish checking all of the structural equivalences.
310 ///
311 /// \returns true if an error occurred, false otherwise.
312 bool Finish();
313
314 public:
315 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000316 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000317 if (LastDiagFromC2)
318 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
319 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000320 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000321 }
322
323 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000324 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000325 if (!LastDiagFromC2)
326 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
327 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000328 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000329 }
330 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000331}
Douglas Gregor3996e242010-02-15 22:01:00 +0000332
333static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
334 QualType T1, QualType T2);
335static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
336 Decl *D1, Decl *D2);
337
Douglas Gregor3996e242010-02-15 22:01:00 +0000338/// \brief Determine structural equivalence of two expressions.
339static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
340 Expr *E1, Expr *E2) {
341 if (!E1 || !E2)
342 return E1 == E2;
343
344 // FIXME: Actually perform a structural comparison!
345 return true;
346}
347
348/// \brief Determine whether two identifiers are equivalent.
349static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
350 const IdentifierInfo *Name2) {
351 if (!Name1 || !Name2)
352 return Name1 == Name2;
353
354 return Name1->getName() == Name2->getName();
355}
356
357/// \brief Determine whether two nested-name-specifiers are equivalent.
358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
359 NestedNameSpecifier *NNS1,
360 NestedNameSpecifier *NNS2) {
361 // FIXME: Implement!
362 return true;
363}
364
365/// \brief Determine whether two template arguments are equivalent.
366static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
367 const TemplateArgument &Arg1,
368 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000369 if (Arg1.getKind() != Arg2.getKind())
370 return false;
371
372 switch (Arg1.getKind()) {
373 case TemplateArgument::Null:
374 return true;
375
376 case TemplateArgument::Type:
377 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000378
Douglas Gregore2e50d332010-12-01 01:36:18 +0000379 case TemplateArgument::Integral:
380 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
381 Arg2.getIntegralType()))
382 return false;
383
Eric Christopher6dcc3762012-07-15 00:23:57 +0000384 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000385
386 case TemplateArgument::Declaration:
387 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000388
389 case TemplateArgument::NullPtr:
390 return true; // FIXME: Is this correct?
391
Douglas Gregore2e50d332010-12-01 01:36:18 +0000392 case TemplateArgument::Template:
393 return IsStructurallyEquivalent(Context,
394 Arg1.getAsTemplate(),
395 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000396
397 case TemplateArgument::TemplateExpansion:
398 return IsStructurallyEquivalent(Context,
399 Arg1.getAsTemplateOrTemplatePattern(),
400 Arg2.getAsTemplateOrTemplatePattern());
401
Douglas Gregore2e50d332010-12-01 01:36:18 +0000402 case TemplateArgument::Expression:
403 return IsStructurallyEquivalent(Context,
404 Arg1.getAsExpr(), Arg2.getAsExpr());
405
406 case TemplateArgument::Pack:
407 if (Arg1.pack_size() != Arg2.pack_size())
408 return false;
409
410 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
411 if (!IsStructurallyEquivalent(Context,
412 Arg1.pack_begin()[I],
413 Arg2.pack_begin()[I]))
414 return false;
415
416 return true;
417 }
418
419 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000420}
421
422/// \brief Determine structural equivalence for the common part of array
423/// types.
424static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
425 const ArrayType *Array1,
426 const ArrayType *Array2) {
427 if (!IsStructurallyEquivalent(Context,
428 Array1->getElementType(),
429 Array2->getElementType()))
430 return false;
431 if (Array1->getSizeModifier() != Array2->getSizeModifier())
432 return false;
433 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
434 return false;
435
436 return true;
437}
438
439/// \brief Determine structural equivalence of two types.
440static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
441 QualType T1, QualType T2) {
442 if (T1.isNull() || T2.isNull())
443 return T1.isNull() && T2.isNull();
444
445 if (!Context.StrictTypeSpelling) {
446 // We aren't being strict about token-to-token equivalence of types,
447 // so map down to the canonical type.
448 T1 = Context.C1.getCanonicalType(T1);
449 T2 = Context.C2.getCanonicalType(T2);
450 }
451
452 if (T1.getQualifiers() != T2.getQualifiers())
453 return false;
454
Douglas Gregorb4964f72010-02-15 23:54:17 +0000455 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000456
Douglas Gregorb4964f72010-02-15 23:54:17 +0000457 if (T1->getTypeClass() != T2->getTypeClass()) {
458 // Compare function types with prototypes vs. without prototypes as if
459 // both did not have prototypes.
460 if (T1->getTypeClass() == Type::FunctionProto &&
461 T2->getTypeClass() == Type::FunctionNoProto)
462 TC = Type::FunctionNoProto;
463 else if (T1->getTypeClass() == Type::FunctionNoProto &&
464 T2->getTypeClass() == Type::FunctionProto)
465 TC = Type::FunctionNoProto;
466 else
467 return false;
468 }
469
470 switch (TC) {
471 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000472 // FIXME: Deal with Char_S/Char_U.
473 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
474 return false;
475 break;
476
477 case Type::Complex:
478 if (!IsStructurallyEquivalent(Context,
479 cast<ComplexType>(T1)->getElementType(),
480 cast<ComplexType>(T2)->getElementType()))
481 return false;
482 break;
483
Reid Kleckner0503a872013-12-05 01:23:43 +0000484 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000485 case Type::Decayed:
486 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000487 cast<AdjustedType>(T1)->getOriginalType(),
488 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000489 return false;
490 break;
491
Douglas Gregor3996e242010-02-15 22:01:00 +0000492 case Type::Pointer:
493 if (!IsStructurallyEquivalent(Context,
494 cast<PointerType>(T1)->getPointeeType(),
495 cast<PointerType>(T2)->getPointeeType()))
496 return false;
497 break;
498
499 case Type::BlockPointer:
500 if (!IsStructurallyEquivalent(Context,
501 cast<BlockPointerType>(T1)->getPointeeType(),
502 cast<BlockPointerType>(T2)->getPointeeType()))
503 return false;
504 break;
505
506 case Type::LValueReference:
507 case Type::RValueReference: {
508 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
509 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
510 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
511 return false;
512 if (Ref1->isInnerRef() != Ref2->isInnerRef())
513 return false;
514 if (!IsStructurallyEquivalent(Context,
515 Ref1->getPointeeTypeAsWritten(),
516 Ref2->getPointeeTypeAsWritten()))
517 return false;
518 break;
519 }
520
521 case Type::MemberPointer: {
522 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
523 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
524 if (!IsStructurallyEquivalent(Context,
525 MemPtr1->getPointeeType(),
526 MemPtr2->getPointeeType()))
527 return false;
528 if (!IsStructurallyEquivalent(Context,
529 QualType(MemPtr1->getClass(), 0),
530 QualType(MemPtr2->getClass(), 0)))
531 return false;
532 break;
533 }
534
535 case Type::ConstantArray: {
536 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
537 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000538 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 return false;
540
541 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
542 return false;
543 break;
544 }
545
546 case Type::IncompleteArray:
547 if (!IsArrayStructurallyEquivalent(Context,
548 cast<ArrayType>(T1),
549 cast<ArrayType>(T2)))
550 return false;
551 break;
552
553 case Type::VariableArray: {
554 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
555 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 Array1->getSizeExpr(), Array2->getSizeExpr()))
558 return false;
559
560 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
561 return false;
562
563 break;
564 }
565
566 case Type::DependentSizedArray: {
567 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
568 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
569 if (!IsStructurallyEquivalent(Context,
570 Array1->getSizeExpr(), Array2->getSizeExpr()))
571 return false;
572
573 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
574 return false;
575
576 break;
577 }
578
579 case Type::DependentSizedExtVector: {
580 const DependentSizedExtVectorType *Vec1
581 = cast<DependentSizedExtVectorType>(T1);
582 const DependentSizedExtVectorType *Vec2
583 = cast<DependentSizedExtVectorType>(T2);
584 if (!IsStructurallyEquivalent(Context,
585 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
586 return false;
587 if (!IsStructurallyEquivalent(Context,
588 Vec1->getElementType(),
589 Vec2->getElementType()))
590 return false;
591 break;
592 }
593
594 case Type::Vector:
595 case Type::ExtVector: {
596 const VectorType *Vec1 = cast<VectorType>(T1);
597 const VectorType *Vec2 = cast<VectorType>(T2);
598 if (!IsStructurallyEquivalent(Context,
599 Vec1->getElementType(),
600 Vec2->getElementType()))
601 return false;
602 if (Vec1->getNumElements() != Vec2->getNumElements())
603 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000604 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000605 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000606 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000607 }
608
609 case Type::FunctionProto: {
610 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
611 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000612 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000613 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000614 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
615 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
616 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000617 return false;
618 }
619 if (Proto1->isVariadic() != Proto2->isVariadic())
620 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000621 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000622 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000623 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
624 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
625 return false;
626 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
627 if (!IsStructurallyEquivalent(Context,
628 Proto1->getExceptionType(I),
629 Proto2->getExceptionType(I)))
630 return false;
631 }
632 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000633 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000634 Proto1->getNoexceptExpr(),
635 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000636 return false;
637 }
638 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
639 return false;
640
641 // Fall through to check the bits common with FunctionNoProtoType.
642 }
643
644 case Type::FunctionNoProto: {
645 const FunctionType *Function1 = cast<FunctionType>(T1);
646 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000647 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
648 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000649 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000650 if (Function1->getExtInfo() != Function2->getExtInfo())
651 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000652 break;
653 }
654
655 case Type::UnresolvedUsing:
656 if (!IsStructurallyEquivalent(Context,
657 cast<UnresolvedUsingType>(T1)->getDecl(),
658 cast<UnresolvedUsingType>(T2)->getDecl()))
659 return false;
660
661 break;
John McCall81904512011-01-06 01:58:22 +0000662
663 case Type::Attributed:
664 if (!IsStructurallyEquivalent(Context,
665 cast<AttributedType>(T1)->getModifiedType(),
666 cast<AttributedType>(T2)->getModifiedType()))
667 return false;
668 if (!IsStructurallyEquivalent(Context,
669 cast<AttributedType>(T1)->getEquivalentType(),
670 cast<AttributedType>(T2)->getEquivalentType()))
671 return false;
672 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000673
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000674 case Type::Paren:
675 if (!IsStructurallyEquivalent(Context,
676 cast<ParenType>(T1)->getInnerType(),
677 cast<ParenType>(T2)->getInnerType()))
678 return false;
679 break;
680
Douglas Gregor3996e242010-02-15 22:01:00 +0000681 case Type::Typedef:
682 if (!IsStructurallyEquivalent(Context,
683 cast<TypedefType>(T1)->getDecl(),
684 cast<TypedefType>(T2)->getDecl()))
685 return false;
686 break;
687
688 case Type::TypeOfExpr:
689 if (!IsStructurallyEquivalent(Context,
690 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
691 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
692 return false;
693 break;
694
695 case Type::TypeOf:
696 if (!IsStructurallyEquivalent(Context,
697 cast<TypeOfType>(T1)->getUnderlyingType(),
698 cast<TypeOfType>(T2)->getUnderlyingType()))
699 return false;
700 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000701
702 case Type::UnaryTransform:
703 if (!IsStructurallyEquivalent(Context,
704 cast<UnaryTransformType>(T1)->getUnderlyingType(),
705 cast<UnaryTransformType>(T1)->getUnderlyingType()))
706 return false;
707 break;
708
Douglas Gregor3996e242010-02-15 22:01:00 +0000709 case Type::Decltype:
710 if (!IsStructurallyEquivalent(Context,
711 cast<DecltypeType>(T1)->getUnderlyingExpr(),
712 cast<DecltypeType>(T2)->getUnderlyingExpr()))
713 return false;
714 break;
715
Richard Smith30482bc2011-02-20 03:19:35 +0000716 case Type::Auto:
717 if (!IsStructurallyEquivalent(Context,
718 cast<AutoType>(T1)->getDeducedType(),
719 cast<AutoType>(T2)->getDeducedType()))
720 return false;
721 break;
722
Douglas Gregor3996e242010-02-15 22:01:00 +0000723 case Type::Record:
724 case Type::Enum:
725 if (!IsStructurallyEquivalent(Context,
726 cast<TagType>(T1)->getDecl(),
727 cast<TagType>(T2)->getDecl()))
728 return false;
729 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000730
Douglas Gregor3996e242010-02-15 22:01:00 +0000731 case Type::TemplateTypeParm: {
732 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
733 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
734 if (Parm1->getDepth() != Parm2->getDepth())
735 return false;
736 if (Parm1->getIndex() != Parm2->getIndex())
737 return false;
738 if (Parm1->isParameterPack() != Parm2->isParameterPack())
739 return false;
740
741 // Names of template type parameters are never significant.
742 break;
743 }
744
745 case Type::SubstTemplateTypeParm: {
746 const SubstTemplateTypeParmType *Subst1
747 = cast<SubstTemplateTypeParmType>(T1);
748 const SubstTemplateTypeParmType *Subst2
749 = cast<SubstTemplateTypeParmType>(T2);
750 if (!IsStructurallyEquivalent(Context,
751 QualType(Subst1->getReplacedParameter(), 0),
752 QualType(Subst2->getReplacedParameter(), 0)))
753 return false;
754 if (!IsStructurallyEquivalent(Context,
755 Subst1->getReplacementType(),
756 Subst2->getReplacementType()))
757 return false;
758 break;
759 }
760
Douglas Gregorfb322d82011-01-14 05:11:40 +0000761 case Type::SubstTemplateTypeParmPack: {
762 const SubstTemplateTypeParmPackType *Subst1
763 = cast<SubstTemplateTypeParmPackType>(T1);
764 const SubstTemplateTypeParmPackType *Subst2
765 = cast<SubstTemplateTypeParmPackType>(T2);
766 if (!IsStructurallyEquivalent(Context,
767 QualType(Subst1->getReplacedParameter(), 0),
768 QualType(Subst2->getReplacedParameter(), 0)))
769 return false;
770 if (!IsStructurallyEquivalent(Context,
771 Subst1->getArgumentPack(),
772 Subst2->getArgumentPack()))
773 return false;
774 break;
775 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 case Type::TemplateSpecialization: {
777 const TemplateSpecializationType *Spec1
778 = cast<TemplateSpecializationType>(T1);
779 const TemplateSpecializationType *Spec2
780 = cast<TemplateSpecializationType>(T2);
781 if (!IsStructurallyEquivalent(Context,
782 Spec1->getTemplateName(),
783 Spec2->getTemplateName()))
784 return false;
785 if (Spec1->getNumArgs() != Spec2->getNumArgs())
786 return false;
787 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
788 if (!IsStructurallyEquivalent(Context,
789 Spec1->getArg(I), Spec2->getArg(I)))
790 return false;
791 }
792 break;
793 }
794
Abramo Bagnara6150c882010-05-11 21:36:43 +0000795 case Type::Elaborated: {
796 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
797 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
798 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
799 if (Elab1->getKeyword() != Elab2->getKeyword())
800 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000801 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000802 Elab1->getQualifier(),
803 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000804 return false;
805 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000806 Elab1->getNamedType(),
807 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000808 return false;
809 break;
810 }
811
John McCalle78aac42010-03-10 03:28:59 +0000812 case Type::InjectedClassName: {
813 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
814 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
815 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000816 Inj1->getInjectedSpecializationType(),
817 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000818 return false;
819 break;
820 }
821
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000822 case Type::DependentName: {
823 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
824 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000825 if (!IsStructurallyEquivalent(Context,
826 Typename1->getQualifier(),
827 Typename2->getQualifier()))
828 return false;
829 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
830 Typename2->getIdentifier()))
831 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000832
833 break;
834 }
835
John McCallc392f372010-06-11 00:33:02 +0000836 case Type::DependentTemplateSpecialization: {
837 const DependentTemplateSpecializationType *Spec1 =
838 cast<DependentTemplateSpecializationType>(T1);
839 const DependentTemplateSpecializationType *Spec2 =
840 cast<DependentTemplateSpecializationType>(T2);
841 if (!IsStructurallyEquivalent(Context,
842 Spec1->getQualifier(),
843 Spec2->getQualifier()))
844 return false;
845 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
846 Spec2->getIdentifier()))
847 return false;
848 if (Spec1->getNumArgs() != Spec2->getNumArgs())
849 return false;
850 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
851 if (!IsStructurallyEquivalent(Context,
852 Spec1->getArg(I), Spec2->getArg(I)))
853 return false;
854 }
855 break;
856 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000857
858 case Type::PackExpansion:
859 if (!IsStructurallyEquivalent(Context,
860 cast<PackExpansionType>(T1)->getPattern(),
861 cast<PackExpansionType>(T2)->getPattern()))
862 return false;
863 break;
864
Douglas Gregor3996e242010-02-15 22:01:00 +0000865 case Type::ObjCInterface: {
866 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
867 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
868 if (!IsStructurallyEquivalent(Context,
869 Iface1->getDecl(), Iface2->getDecl()))
870 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000871 break;
872 }
873
874 case Type::ObjCObject: {
875 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
876 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
877 if (!IsStructurallyEquivalent(Context,
878 Obj1->getBaseType(),
879 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000880 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000881 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
882 return false;
883 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000884 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000885 Obj1->getProtocol(I),
886 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000887 return false;
888 }
889 break;
890 }
891
892 case Type::ObjCObjectPointer: {
893 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
894 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
895 if (!IsStructurallyEquivalent(Context,
896 Ptr1->getPointeeType(),
897 Ptr2->getPointeeType()))
898 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000899 break;
900 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000901
902 case Type::Atomic: {
903 if (!IsStructurallyEquivalent(Context,
904 cast<AtomicType>(T1)->getValueType(),
905 cast<AtomicType>(T2)->getValueType()))
906 return false;
907 break;
908 }
909
Xiuli Pan9c14e282016-01-09 12:53:17 +0000910 case Type::Pipe: {
911 if (!IsStructurallyEquivalent(Context,
912 cast<PipeType>(T1)->getElementType(),
913 cast<PipeType>(T2)->getElementType()))
914 return false;
915 break;
916 }
917
Douglas Gregor3996e242010-02-15 22:01:00 +0000918 } // end switch
919
920 return true;
921}
922
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000923/// \brief Determine structural equivalence of two fields.
924static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
925 FieldDecl *Field1, FieldDecl *Field2) {
926 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000927
928 // For anonymous structs/unions, match up the anonymous struct/union type
929 // declarations directly, so that we don't go off searching for anonymous
930 // types
931 if (Field1->isAnonymousStructOrUnion() &&
932 Field2->isAnonymousStructOrUnion()) {
933 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
934 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
935 return IsStructurallyEquivalent(Context, D1, D2);
936 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000937
938 // Check for equivalent field names.
939 IdentifierInfo *Name1 = Field1->getIdentifier();
940 IdentifierInfo *Name2 = Field2->getIdentifier();
941 if (!::IsStructurallyEquivalent(Name1, Name2))
942 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000943
944 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000945 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000946 if (Context.Complain) {
947 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
948 << Context.C2.getTypeDeclType(Owner2);
949 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
950 << Field2->getDeclName() << Field2->getType();
951 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
952 << Field1->getDeclName() << Field1->getType();
953 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000954 return false;
955 }
956
957 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000958 if (Context.Complain) {
959 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
960 << Context.C2.getTypeDeclType(Owner2);
961 if (Field1->isBitField()) {
962 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
963 << Field1->getDeclName() << Field1->getType()
964 << Field1->getBitWidthValue(Context.C1);
965 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
966 << Field2->getDeclName();
967 } else {
968 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
969 << Field2->getDeclName() << Field2->getType()
970 << Field2->getBitWidthValue(Context.C2);
971 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
972 << Field1->getDeclName();
973 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000974 }
975 return false;
976 }
977
978 if (Field1->isBitField()) {
979 // Make sure that the bit-fields are the same length.
980 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
981 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
982
983 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000984 if (Context.Complain) {
985 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(Owner2);
987 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
988 << Field2->getDeclName() << Field2->getType() << Bits2;
989 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
990 << Field1->getDeclName() << Field1->getType() << Bits1;
991 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000992 return false;
993 }
994 }
995
996 return true;
997}
998
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000999/// \brief Find the index of the given anonymous struct/union within its
1000/// context.
1001///
1002/// \returns Returns the index of this anonymous struct/union in its context,
1003/// including the next assigned index (if none of them match). Returns an
1004/// empty option if the context is not a record, i.e.. if the anonymous
1005/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +00001006static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001007 ASTContext &Context = Anon->getASTContext();
1008 QualType AnonTy = Context.getRecordType(Anon);
1009
1010 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1011 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001012 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001013
1014 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001015 for (const auto *D : Owner->noload_decls()) {
1016 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001017 if (!F || !F->isAnonymousStructOrUnion())
1018 continue;
1019
1020 if (Context.hasSameType(F->getType(), AnonTy))
1021 break;
1022
1023 ++Index;
1024 }
1025
1026 return Index;
1027}
1028
Douglas Gregor3996e242010-02-15 22:01:00 +00001029/// \brief Determine structural equivalence of two records.
1030static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1031 RecordDecl *D1, RecordDecl *D2) {
1032 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001033 if (Context.Complain) {
1034 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1035 << Context.C2.getTypeDeclType(D2);
1036 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1037 << D1->getDeclName() << (unsigned)D1->getTagKind();
1038 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001039 return false;
1040 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001041
1042 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1043 // If both anonymous structs/unions are in a record context, make sure
1044 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001045 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1046 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001047 if (*Index1 != *Index2)
1048 return false;
1049 }
1050 }
1051 }
1052
Douglas Gregore2e50d332010-12-01 01:36:18 +00001053 // If both declarations are class template specializations, we know
1054 // the ODR applies, so check the template and template arguments.
1055 ClassTemplateSpecializationDecl *Spec1
1056 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1057 ClassTemplateSpecializationDecl *Spec2
1058 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1059 if (Spec1 && Spec2) {
1060 // Check that the specialized templates are the same.
1061 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1062 Spec2->getSpecializedTemplate()))
1063 return false;
1064
1065 // Check that the template arguments are the same.
1066 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1067 return false;
1068
1069 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1070 if (!IsStructurallyEquivalent(Context,
1071 Spec1->getTemplateArgs().get(I),
1072 Spec2->getTemplateArgs().get(I)))
1073 return false;
1074 }
1075 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001076 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001077 else if (Spec1 || Spec2)
1078 return false;
1079
Douglas Gregorb4964f72010-02-15 23:54:17 +00001080 // Compare the definitions of these two records. If either or both are
1081 // incomplete, we assume that they are equivalent.
1082 D1 = D1->getDefinition();
1083 D2 = D2->getDefinition();
1084 if (!D1 || !D2)
1085 return true;
1086
Douglas Gregor3996e242010-02-15 22:01:00 +00001087 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1088 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1089 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001090 if (Context.Complain) {
1091 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1092 << Context.C2.getTypeDeclType(D2);
1093 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1094 << D2CXX->getNumBases();
1095 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1096 << D1CXX->getNumBases();
1097 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001098 return false;
1099 }
1100
1101 // Check the base classes.
1102 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1103 BaseEnd1 = D1CXX->bases_end(),
1104 Base2 = D2CXX->bases_begin();
1105 Base1 != BaseEnd1;
1106 ++Base1, ++Base2) {
1107 if (!IsStructurallyEquivalent(Context,
1108 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001109 if (Context.Complain) {
1110 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1111 << Context.C2.getTypeDeclType(D2);
1112 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1113 << Base2->getType()
1114 << Base2->getSourceRange();
1115 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1116 << Base1->getType()
1117 << Base1->getSourceRange();
1118 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001119 return false;
1120 }
1121
1122 // Check virtual vs. non-virtual inheritance mismatch.
1123 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001124 if (Context.Complain) {
1125 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1126 << Context.C2.getTypeDeclType(D2);
1127 Context.Diag2(Base2->getLocStart(),
1128 diag::note_odr_virtual_base)
1129 << Base2->isVirtual() << Base2->getSourceRange();
1130 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1131 << Base1->isVirtual()
1132 << Base1->getSourceRange();
1133 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001134 return false;
1135 }
1136 }
1137 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001138 if (Context.Complain) {
1139 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1140 << Context.C2.getTypeDeclType(D2);
1141 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1142 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1143 << Base1->getType()
1144 << Base1->getSourceRange();
1145 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1146 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001147 return false;
1148 }
1149 }
1150
1151 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001152 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001153 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001154 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001155 Field1End = D1->field_end();
1156 Field1 != Field1End;
1157 ++Field1, ++Field2) {
1158 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001159 if (Context.Complain) {
1160 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1161 << Context.C2.getTypeDeclType(D2);
1162 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1163 << Field1->getDeclName() << Field1->getType();
1164 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1165 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001166 return false;
1167 }
1168
David Blaikie40ed2972012-06-06 20:45:41 +00001169 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001170 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 }
1172
1173 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001174 if (Context.Complain) {
1175 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1176 << Context.C2.getTypeDeclType(D2);
1177 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1178 << Field2->getDeclName() << Field2->getType();
1179 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1180 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001181 return false;
1182 }
1183
1184 return true;
1185}
1186
1187/// \brief Determine structural equivalence of two enums.
1188static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1189 EnumDecl *D1, EnumDecl *D2) {
1190 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1191 EC2End = D2->enumerator_end();
1192 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1193 EC1End = D1->enumerator_end();
1194 EC1 != EC1End; ++EC1, ++EC2) {
1195 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001196 if (Context.Complain) {
1197 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1198 << Context.C2.getTypeDeclType(D2);
1199 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1200 << EC1->getDeclName()
1201 << EC1->getInitVal().toString(10);
1202 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1203 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001204 return false;
1205 }
1206
1207 llvm::APSInt Val1 = EC1->getInitVal();
1208 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001209 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001210 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001211 if (Context.Complain) {
1212 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1213 << Context.C2.getTypeDeclType(D2);
1214 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1215 << EC2->getDeclName()
1216 << EC2->getInitVal().toString(10);
1217 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1218 << EC1->getDeclName()
1219 << EC1->getInitVal().toString(10);
1220 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001221 return false;
1222 }
1223 }
1224
1225 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001226 if (Context.Complain) {
1227 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1228 << Context.C2.getTypeDeclType(D2);
1229 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1230 << EC2->getDeclName()
1231 << EC2->getInitVal().toString(10);
1232 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1233 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001234 return false;
1235 }
1236
1237 return true;
1238}
Douglas Gregora082a492010-11-30 19:14:50 +00001239
1240static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1241 TemplateParameterList *Params1,
1242 TemplateParameterList *Params2) {
1243 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001244 if (Context.Complain) {
1245 Context.Diag2(Params2->getTemplateLoc(),
1246 diag::err_odr_different_num_template_parameters)
1247 << Params1->size() << Params2->size();
1248 Context.Diag1(Params1->getTemplateLoc(),
1249 diag::note_odr_template_parameter_list);
1250 }
Douglas Gregora082a492010-11-30 19:14:50 +00001251 return false;
1252 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001253
Douglas Gregora082a492010-11-30 19:14:50 +00001254 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1255 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001256 if (Context.Complain) {
1257 Context.Diag2(Params2->getParam(I)->getLocation(),
1258 diag::err_odr_different_template_parameter_kind);
1259 Context.Diag1(Params1->getParam(I)->getLocation(),
1260 diag::note_odr_template_parameter_here);
1261 }
Douglas Gregora082a492010-11-30 19:14:50 +00001262 return false;
1263 }
1264
1265 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1266 Params2->getParam(I))) {
1267
1268 return false;
1269 }
1270 }
1271
1272 return true;
1273}
1274
1275static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1276 TemplateTypeParmDecl *D1,
1277 TemplateTypeParmDecl *D2) {
1278 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001279 if (Context.Complain) {
1280 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1281 << D2->isParameterPack();
1282 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1283 << D1->isParameterPack();
1284 }
Douglas Gregora082a492010-11-30 19:14:50 +00001285 return false;
1286 }
1287
1288 return true;
1289}
1290
1291static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1292 NonTypeTemplateParmDecl *D1,
1293 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001294 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001295 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001296 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1297 << D2->isParameterPack();
1298 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1299 << D1->isParameterPack();
1300 }
Douglas Gregora082a492010-11-30 19:14:50 +00001301 return false;
1302 }
Douglas Gregora082a492010-11-30 19:14:50 +00001303
1304 // Check types.
1305 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001306 if (Context.Complain) {
1307 Context.Diag2(D2->getLocation(),
1308 diag::err_odr_non_type_parameter_type_inconsistent)
1309 << D2->getType() << D1->getType();
1310 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1311 << D1->getType();
1312 }
Douglas Gregora082a492010-11-30 19:14:50 +00001313 return false;
1314 }
1315
1316 return true;
1317}
1318
1319static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1320 TemplateTemplateParmDecl *D1,
1321 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001322 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001323 if (Context.Complain) {
1324 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1325 << D2->isParameterPack();
1326 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1327 << D1->isParameterPack();
1328 }
Douglas Gregora082a492010-11-30 19:14:50 +00001329 return false;
1330 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001331
Douglas Gregora082a492010-11-30 19:14:50 +00001332 // Check template parameter lists.
1333 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1334 D2->getTemplateParameters());
1335}
1336
1337static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1338 ClassTemplateDecl *D1,
1339 ClassTemplateDecl *D2) {
1340 // Check template parameters.
1341 if (!IsStructurallyEquivalent(Context,
1342 D1->getTemplateParameters(),
1343 D2->getTemplateParameters()))
1344 return false;
1345
1346 // Check the templated declaration.
1347 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1348 D2->getTemplatedDecl());
1349}
1350
Douglas Gregor3996e242010-02-15 22:01:00 +00001351/// \brief Determine structural equivalence of two declarations.
1352static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1353 Decl *D1, Decl *D2) {
1354 // FIXME: Check for known structural equivalences via a callback of some sort.
1355
Douglas Gregorb4964f72010-02-15 23:54:17 +00001356 // Check whether we already know that these two declarations are not
1357 // structurally equivalent.
1358 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1359 D2->getCanonicalDecl())))
1360 return false;
1361
Douglas Gregor3996e242010-02-15 22:01:00 +00001362 // Determine whether we've already produced a tentative equivalence for D1.
1363 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1364 if (EquivToD1)
1365 return EquivToD1 == D2->getCanonicalDecl();
1366
1367 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1368 EquivToD1 = D2->getCanonicalDecl();
1369 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1370 return true;
1371}
1372
1373bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1374 Decl *D2) {
1375 if (!::IsStructurallyEquivalent(*this, D1, D2))
1376 return false;
1377
1378 return !Finish();
1379}
1380
1381bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1382 QualType T2) {
1383 if (!::IsStructurallyEquivalent(*this, T1, T2))
1384 return false;
1385
1386 return !Finish();
1387}
1388
1389bool StructuralEquivalenceContext::Finish() {
1390 while (!DeclsToCheck.empty()) {
1391 // Check the next declaration.
1392 Decl *D1 = DeclsToCheck.front();
1393 DeclsToCheck.pop_front();
1394
1395 Decl *D2 = TentativeEquivalences[D1];
1396 assert(D2 && "Unrecorded tentative equivalence?");
1397
Douglas Gregorb4964f72010-02-15 23:54:17 +00001398 bool Equivalent = true;
1399
Douglas Gregor3996e242010-02-15 22:01:00 +00001400 // FIXME: Switch on all declaration kinds. For now, we're just going to
1401 // check the obvious ones.
1402 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1403 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1404 // Check for equivalent structure names.
1405 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001406 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1407 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001408 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001409 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1410 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001411 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1412 !::IsStructurallyEquivalent(*this, Record1, Record2))
1413 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001414 } else {
1415 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001416 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001417 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001418 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001419 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1420 // Check for equivalent enum names.
1421 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001422 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1423 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001424 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001425 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1426 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001427 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1428 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1429 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001430 } else {
1431 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001432 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001433 }
Richard Smithdda56e42011-04-15 14:24:37 +00001434 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1435 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001436 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001437 Typedef2->getIdentifier()) ||
1438 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001439 Typedef1->getUnderlyingType(),
1440 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001441 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001442 } else {
1443 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001444 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001445 }
Douglas Gregora082a492010-11-30 19:14:50 +00001446 } else if (ClassTemplateDecl *ClassTemplate1
1447 = dyn_cast<ClassTemplateDecl>(D1)) {
1448 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1449 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1450 ClassTemplate2->getIdentifier()) ||
1451 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1452 Equivalent = false;
1453 } else {
1454 // Class template/non-class-template mismatch.
1455 Equivalent = false;
1456 }
1457 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1458 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1459 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1460 Equivalent = false;
1461 } else {
1462 // Kind mismatch.
1463 Equivalent = false;
1464 }
1465 } else if (NonTypeTemplateParmDecl *NTTP1
1466 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1467 if (NonTypeTemplateParmDecl *NTTP2
1468 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1469 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1470 Equivalent = false;
1471 } else {
1472 // Kind mismatch.
1473 Equivalent = false;
1474 }
1475 } else if (TemplateTemplateParmDecl *TTP1
1476 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1477 if (TemplateTemplateParmDecl *TTP2
1478 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1479 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1480 Equivalent = false;
1481 } else {
1482 // Kind mismatch.
1483 Equivalent = false;
1484 }
1485 }
1486
Douglas Gregorb4964f72010-02-15 23:54:17 +00001487 if (!Equivalent) {
1488 // Note that these two declarations are not equivalent (and we already
1489 // know about it).
1490 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1491 D2->getCanonicalDecl()));
1492 return true;
1493 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001494 // FIXME: Check other declaration kinds!
1495 }
1496
1497 return false;
1498}
1499
1500//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001501// Import Types
1502//----------------------------------------------------------------------------
1503
John McCall424cec92011-01-19 06:33:43 +00001504QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001505 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1506 << T->getTypeClassName();
1507 return QualType();
1508}
1509
John McCall424cec92011-01-19 06:33:43 +00001510QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001511 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001512#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1513 case BuiltinType::Id: \
1514 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001515#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001516#define SHARED_SINGLETON_TYPE(Expansion)
1517#define BUILTIN_TYPE(Id, SingletonId) \
1518 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1519#include "clang/AST/BuiltinTypes.def"
1520
1521 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1522 // context supports C++.
1523
1524 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1525 // context supports ObjC.
1526
Douglas Gregor96e578d2010-02-05 17:54:41 +00001527 case BuiltinType::Char_U:
1528 // The context we're importing from has an unsigned 'char'. If we're
1529 // importing into a context with a signed 'char', translate to
1530 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001531 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001532 return Importer.getToContext().UnsignedCharTy;
1533
1534 return Importer.getToContext().CharTy;
1535
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536 case BuiltinType::Char_S:
1537 // The context we're importing from has an unsigned 'char'. If we're
1538 // importing into a context with a signed 'char', translate to
1539 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001540 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001541 return Importer.getToContext().SignedCharTy;
1542
1543 return Importer.getToContext().CharTy;
1544
Chris Lattnerad3467e2010-12-25 23:25:43 +00001545 case BuiltinType::WChar_S:
1546 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001547 // FIXME: If not in C++, shall we translate to the C equivalent of
1548 // wchar_t?
1549 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001551
1552 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553}
1554
John McCall424cec92011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001556 QualType ToElementType = Importer.Import(T->getElementType());
1557 if (ToElementType.isNull())
1558 return QualType();
1559
1560 return Importer.getToContext().getComplexType(ToElementType);
1561}
1562
John McCall424cec92011-01-19 06:33:43 +00001563QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001564 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1565 if (ToPointeeType.isNull())
1566 return QualType();
1567
1568 return Importer.getToContext().getPointerType(ToPointeeType);
1569}
1570
John McCall424cec92011-01-19 06:33:43 +00001571QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001572 // FIXME: Check for blocks support in "to" context.
1573 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1574 if (ToPointeeType.isNull())
1575 return QualType();
1576
1577 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1578}
1579
John McCall424cec92011-01-19 06:33:43 +00001580QualType
1581ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001582 // FIXME: Check for C++ support in "to" context.
1583 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1584 if (ToPointeeType.isNull())
1585 return QualType();
1586
1587 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1588}
1589
John McCall424cec92011-01-19 06:33:43 +00001590QualType
1591ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592 // FIXME: Check for C++0x support in "to" context.
1593 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1594 if (ToPointeeType.isNull())
1595 return QualType();
1596
1597 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1598}
1599
John McCall424cec92011-01-19 06:33:43 +00001600QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001601 // FIXME: Check for C++ support in "to" context.
1602 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1603 if (ToPointeeType.isNull())
1604 return QualType();
1605
1606 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1607 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1608 ClassType.getTypePtr());
1609}
1610
John McCall424cec92011-01-19 06:33:43 +00001611QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001612 QualType ToElementType = Importer.Import(T->getElementType());
1613 if (ToElementType.isNull())
1614 return QualType();
1615
1616 return Importer.getToContext().getConstantArrayType(ToElementType,
1617 T->getSize(),
1618 T->getSizeModifier(),
1619 T->getIndexTypeCVRQualifiers());
1620}
1621
John McCall424cec92011-01-19 06:33:43 +00001622QualType
1623ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001624 QualType ToElementType = Importer.Import(T->getElementType());
1625 if (ToElementType.isNull())
1626 return QualType();
1627
1628 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1629 T->getSizeModifier(),
1630 T->getIndexTypeCVRQualifiers());
1631}
1632
John McCall424cec92011-01-19 06:33:43 +00001633QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001634 QualType ToElementType = Importer.Import(T->getElementType());
1635 if (ToElementType.isNull())
1636 return QualType();
1637
1638 Expr *Size = Importer.Import(T->getSizeExpr());
1639 if (!Size)
1640 return QualType();
1641
1642 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1643 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1644 T->getSizeModifier(),
1645 T->getIndexTypeCVRQualifiers(),
1646 Brackets);
1647}
1648
John McCall424cec92011-01-19 06:33:43 +00001649QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001650 QualType ToElementType = Importer.Import(T->getElementType());
1651 if (ToElementType.isNull())
1652 return QualType();
1653
1654 return Importer.getToContext().getVectorType(ToElementType,
1655 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001656 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657}
1658
John McCall424cec92011-01-19 06:33:43 +00001659QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001660 QualType ToElementType = Importer.Import(T->getElementType());
1661 if (ToElementType.isNull())
1662 return QualType();
1663
1664 return Importer.getToContext().getExtVectorType(ToElementType,
1665 T->getNumElements());
1666}
1667
John McCall424cec92011-01-19 06:33:43 +00001668QualType
1669ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001670 // FIXME: What happens if we're importing a function without a prototype
1671 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001672 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001673 if (ToResultType.isNull())
1674 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001675
Douglas Gregor96e578d2010-02-05 17:54:41 +00001676 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001677 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001678}
1679
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001680QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001681 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001682 if (ToResultType.isNull())
1683 return QualType();
1684
1685 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001686 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001687 for (const auto &A : T->param_types()) {
1688 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001689 if (ArgType.isNull())
1690 return QualType();
1691 ArgTypes.push_back(ArgType);
1692 }
1693
1694 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001695 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001696 for (const auto &E : T->exceptions()) {
1697 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001698 if (ExceptionType.isNull())
1699 return QualType();
1700 ExceptionTypes.push_back(ExceptionType);
1701 }
John McCalldb40c7f2010-12-14 08:05:40 +00001702
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001703 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1704 FunctionProtoType::ExtProtoInfo ToEPI;
1705
1706 ToEPI.ExtInfo = FromEPI.ExtInfo;
1707 ToEPI.Variadic = FromEPI.Variadic;
1708 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1709 ToEPI.TypeQuals = FromEPI.TypeQuals;
1710 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001711 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1712 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1713 ToEPI.ExceptionSpec.NoexceptExpr =
1714 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1715 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1716 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1717 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1718 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001719
Jordan Rose5c382722013-03-08 21:51:21 +00001720 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001721}
1722
Sean Callananda6df8a2011-08-11 16:56:07 +00001723QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1724 QualType ToInnerType = Importer.Import(T->getInnerType());
1725 if (ToInnerType.isNull())
1726 return QualType();
1727
1728 return Importer.getToContext().getParenType(ToInnerType);
1729}
1730
John McCall424cec92011-01-19 06:33:43 +00001731QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001732 TypedefNameDecl *ToDecl
1733 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001734 if (!ToDecl)
1735 return QualType();
1736
1737 return Importer.getToContext().getTypeDeclType(ToDecl);
1738}
1739
John McCall424cec92011-01-19 06:33:43 +00001740QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001741 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1742 if (!ToExpr)
1743 return QualType();
1744
1745 return Importer.getToContext().getTypeOfExprType(ToExpr);
1746}
1747
John McCall424cec92011-01-19 06:33:43 +00001748QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001749 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1750 if (ToUnderlyingType.isNull())
1751 return QualType();
1752
1753 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1754}
1755
John McCall424cec92011-01-19 06:33:43 +00001756QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001757 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1759 if (!ToExpr)
1760 return QualType();
1761
Douglas Gregor81495f32012-02-12 18:42:33 +00001762 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1763 if (UnderlyingType.isNull())
1764 return QualType();
1765
1766 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001767}
1768
Alexis Hunte852b102011-05-24 22:41:36 +00001769QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1770 QualType ToBaseType = Importer.Import(T->getBaseType());
1771 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1772 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1773 return QualType();
1774
1775 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1776 ToUnderlyingType,
1777 T->getUTTKind());
1778}
1779
Richard Smith30482bc2011-02-20 03:19:35 +00001780QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001781 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001782 QualType FromDeduced = T->getDeducedType();
1783 QualType ToDeduced;
1784 if (!FromDeduced.isNull()) {
1785 ToDeduced = Importer.Import(FromDeduced);
1786 if (ToDeduced.isNull())
1787 return QualType();
1788 }
1789
Richard Smithe301ba22015-11-11 02:02:15 +00001790 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001791 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001792}
1793
John McCall424cec92011-01-19 06:33:43 +00001794QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001795 RecordDecl *ToDecl
1796 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1797 if (!ToDecl)
1798 return QualType();
1799
1800 return Importer.getToContext().getTagDeclType(ToDecl);
1801}
1802
John McCall424cec92011-01-19 06:33:43 +00001803QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001804 EnumDecl *ToDecl
1805 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1806 if (!ToDecl)
1807 return QualType();
1808
1809 return Importer.getToContext().getTagDeclType(ToDecl);
1810}
1811
Sean Callanan72fe0852015-04-02 23:50:08 +00001812QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1813 QualType FromModifiedType = T->getModifiedType();
1814 QualType FromEquivalentType = T->getEquivalentType();
1815 QualType ToModifiedType;
1816 QualType ToEquivalentType;
1817
1818 if (!FromModifiedType.isNull()) {
1819 ToModifiedType = Importer.Import(FromModifiedType);
1820 if (ToModifiedType.isNull())
1821 return QualType();
1822 }
1823 if (!FromEquivalentType.isNull()) {
1824 ToEquivalentType = Importer.Import(FromEquivalentType);
1825 if (ToEquivalentType.isNull())
1826 return QualType();
1827 }
1828
1829 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1830 ToModifiedType, ToEquivalentType);
1831}
1832
Douglas Gregore2e50d332010-12-01 01:36:18 +00001833QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001834 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001835 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1836 if (ToTemplate.isNull())
1837 return QualType();
1838
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001839 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001840 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1841 return QualType();
1842
1843 QualType ToCanonType;
1844 if (!QualType(T, 0).isCanonical()) {
1845 QualType FromCanonType
1846 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1847 ToCanonType =Importer.Import(FromCanonType);
1848 if (ToCanonType.isNull())
1849 return QualType();
1850 }
1851 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1852 ToTemplateArgs.data(),
1853 ToTemplateArgs.size(),
1854 ToCanonType);
1855}
1856
John McCall424cec92011-01-19 06:33:43 +00001857QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001858 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001859 // Note: the qualifier in an ElaboratedType is optional.
1860 if (T->getQualifier()) {
1861 ToQualifier = Importer.Import(T->getQualifier());
1862 if (!ToQualifier)
1863 return QualType();
1864 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001865
1866 QualType ToNamedType = Importer.Import(T->getNamedType());
1867 if (ToNamedType.isNull())
1868 return QualType();
1869
Abramo Bagnara6150c882010-05-11 21:36:43 +00001870 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1871 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001872}
1873
John McCall424cec92011-01-19 06:33:43 +00001874QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001875 ObjCInterfaceDecl *Class
1876 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1877 if (!Class)
1878 return QualType();
1879
John McCall8b07ec22010-05-15 11:32:37 +00001880 return Importer.getToContext().getObjCInterfaceType(Class);
1881}
1882
John McCall424cec92011-01-19 06:33:43 +00001883QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001884 QualType ToBaseType = Importer.Import(T->getBaseType());
1885 if (ToBaseType.isNull())
1886 return QualType();
1887
Douglas Gregore9d95f12015-07-07 03:57:35 +00001888 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001889 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001890 QualType ImportedTypeArg = Importer.Import(TypeArg);
1891 if (ImportedTypeArg.isNull())
1892 return QualType();
1893
1894 TypeArgs.push_back(ImportedTypeArg);
1895 }
1896
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001898 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001899 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001900 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001901 if (!Protocol)
1902 return QualType();
1903 Protocols.push_back(Protocol);
1904 }
1905
Douglas Gregore9d95f12015-07-07 03:57:35 +00001906 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001907 Protocols,
1908 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001909}
1910
John McCall424cec92011-01-19 06:33:43 +00001911QualType
1912ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001913 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1914 if (ToPointeeType.isNull())
1915 return QualType();
1916
John McCall8b07ec22010-05-15 11:32:37 +00001917 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001918}
1919
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001920//----------------------------------------------------------------------------
1921// Import Declarations
1922//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001923bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1924 DeclContext *&LexicalDC,
1925 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001926 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001927 SourceLocation &Loc) {
1928 // Import the context of this declaration.
1929 DC = Importer.ImportContext(D->getDeclContext());
1930 if (!DC)
1931 return true;
1932
1933 LexicalDC = DC;
1934 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1935 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1936 if (!LexicalDC)
1937 return true;
1938 }
1939
1940 // Import the name of this declaration.
1941 Name = Importer.Import(D->getDeclName());
1942 if (D->getDeclName() && !Name)
1943 return true;
1944
1945 // Import the location of this declaration.
1946 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001947 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001948 return false;
1949}
1950
Douglas Gregord451ea92011-07-29 23:31:30 +00001951void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1952 if (!FromD)
1953 return;
1954
1955 if (!ToD) {
1956 ToD = Importer.Import(FromD);
1957 if (!ToD)
1958 return;
1959 }
1960
1961 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1962 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001963 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001964 ImportDefinition(FromRecord, ToRecord);
1965 }
1966 }
1967 return;
1968 }
1969
1970 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1971 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1972 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1973 ImportDefinition(FromEnum, ToEnum);
1974 }
1975 }
1976 return;
1977 }
1978}
1979
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001980void
1981ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1982 DeclarationNameInfo& To) {
1983 // NOTE: To.Name and To.Loc are already imported.
1984 // We only have to import To.LocInfo.
1985 switch (To.getName().getNameKind()) {
1986 case DeclarationName::Identifier:
1987 case DeclarationName::ObjCZeroArgSelector:
1988 case DeclarationName::ObjCOneArgSelector:
1989 case DeclarationName::ObjCMultiArgSelector:
1990 case DeclarationName::CXXUsingDirective:
1991 return;
1992
1993 case DeclarationName::CXXOperatorName: {
1994 SourceRange Range = From.getCXXOperatorNameRange();
1995 To.setCXXOperatorNameRange(Importer.Import(Range));
1996 return;
1997 }
1998 case DeclarationName::CXXLiteralOperatorName: {
1999 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2000 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2001 return;
2002 }
2003 case DeclarationName::CXXConstructorName:
2004 case DeclarationName::CXXDestructorName:
2005 case DeclarationName::CXXConversionFunctionName: {
2006 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2007 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2008 return;
2009 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002010 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002011 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002012}
2013
Douglas Gregor2e15c842012-02-01 21:00:38 +00002014void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002015 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002016 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002017 return;
2018 }
2019
Aaron Ballman629afae2014-03-07 19:56:05 +00002020 for (auto *From : FromDC->decls())
2021 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002022}
2023
Douglas Gregord451ea92011-07-29 23:31:30 +00002024bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002025 ImportDefinitionKind Kind) {
2026 if (To->getDefinition() || To->isBeingDefined()) {
2027 if (Kind == IDK_Everything)
2028 ImportDeclContext(From, /*ForceImport=*/true);
2029
Douglas Gregore2e50d332010-12-01 01:36:18 +00002030 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002031 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002032
2033 To->startDefinition();
2034
2035 // Add base classes.
2036 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2037 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002038
2039 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2040 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2041 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002042 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002043 ToData.Aggregate = FromData.Aggregate;
2044 ToData.PlainOldData = FromData.PlainOldData;
2045 ToData.Empty = FromData.Empty;
2046 ToData.Polymorphic = FromData.Polymorphic;
2047 ToData.Abstract = FromData.Abstract;
2048 ToData.IsStandardLayout = FromData.IsStandardLayout;
2049 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2050 ToData.HasPrivateFields = FromData.HasPrivateFields;
2051 ToData.HasProtectedFields = FromData.HasProtectedFields;
2052 ToData.HasPublicFields = FromData.HasPublicFields;
2053 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002054 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002055 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002056 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002057 ToData.HasUninitializedReferenceMember
2058 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002059 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith6b02d462012-12-08 08:32:28 +00002060 ToData.NeedOverloadResolutionForMoveConstructor
2061 = FromData.NeedOverloadResolutionForMoveConstructor;
2062 ToData.NeedOverloadResolutionForMoveAssignment
2063 = FromData.NeedOverloadResolutionForMoveAssignment;
2064 ToData.NeedOverloadResolutionForDestructor
2065 = FromData.NeedOverloadResolutionForDestructor;
2066 ToData.DefaultedMoveConstructorIsDeleted
2067 = FromData.DefaultedMoveConstructorIsDeleted;
2068 ToData.DefaultedMoveAssignmentIsDeleted
2069 = FromData.DefaultedMoveAssignmentIsDeleted;
2070 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002071 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2072 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002073 ToData.HasConstexprNonCopyMoveConstructor
2074 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002075 ToData.HasDefaultedDefaultConstructor
2076 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002077 ToData.DefaultedDefaultConstructorIsConstexpr
2078 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002079 ToData.HasConstexprDefaultConstructor
2080 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002081 ToData.HasNonLiteralTypeFieldsOrBases
2082 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002083 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002084 ToData.UserProvidedDefaultConstructor
2085 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002086 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002087 ToData.ImplicitCopyConstructorHasConstParam
2088 = FromData.ImplicitCopyConstructorHasConstParam;
2089 ToData.ImplicitCopyAssignmentHasConstParam
2090 = FromData.ImplicitCopyAssignmentHasConstParam;
2091 ToData.HasDeclaredCopyConstructorWithConstParam
2092 = FromData.HasDeclaredCopyConstructorWithConstParam;
2093 ToData.HasDeclaredCopyAssignmentWithConstParam
2094 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002095 ToData.IsLambda = FromData.IsLambda;
2096
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002097 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002098 for (const auto &Base1 : FromCXX->bases()) {
2099 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002100 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002101 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002102
2103 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002104 if (Base1.isPackExpansion())
2105 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002106
2107 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002108 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002109
Douglas Gregore2e50d332010-12-01 01:36:18 +00002110 Bases.push_back(
2111 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002112 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2113 Base1.isVirtual(),
2114 Base1.isBaseOfClass(),
2115 Base1.getAccessSpecifierAsWritten(),
2116 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002117 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002118 }
2119 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002120 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002121 }
2122
Douglas Gregor2e15c842012-02-01 21:00:38 +00002123 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002124 ImportDeclContext(From, /*ForceImport=*/true);
2125
Douglas Gregore2e50d332010-12-01 01:36:18 +00002126 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002127 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002128}
2129
Larisse Voufo39a1e502013-08-06 01:03:05 +00002130bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2131 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002132 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002133 return false;
2134
2135 // FIXME: Can we really import any initializer? Alternatively, we could force
2136 // ourselves to import every declaration of a variable and then only use
2137 // getInit() here.
2138 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2139
2140 // FIXME: Other bits to merge?
2141
2142 return false;
2143}
2144
Douglas Gregord451ea92011-07-29 23:31:30 +00002145bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002146 ImportDefinitionKind Kind) {
2147 if (To->getDefinition() || To->isBeingDefined()) {
2148 if (Kind == IDK_Everything)
2149 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002150 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002151 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002152
2153 To->startDefinition();
2154
2155 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2156 if (T.isNull())
2157 return true;
2158
2159 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2160 if (ToPromotionType.isNull())
2161 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002162
2163 if (shouldForceImportDeclContext(Kind))
2164 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002165
2166 // FIXME: we might need to merge the number of positive or negative bits
2167 // if the enumerator lists don't match.
2168 To->completeDefinition(T, ToPromotionType,
2169 From->getNumPositiveBits(),
2170 From->getNumNegativeBits());
2171 return false;
2172}
2173
Douglas Gregora082a492010-11-30 19:14:50 +00002174TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2175 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002176 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002177 ToParams.reserve(Params->size());
2178 for (TemplateParameterList::iterator P = Params->begin(),
2179 PEnd = Params->end();
2180 P != PEnd; ++P) {
2181 Decl *To = Importer.Import(*P);
2182 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002183 return nullptr;
2184
Douglas Gregora082a492010-11-30 19:14:50 +00002185 ToParams.push_back(cast<NamedDecl>(To));
2186 }
2187
2188 return TemplateParameterList::Create(Importer.getToContext(),
2189 Importer.Import(Params->getTemplateLoc()),
2190 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002191 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002192 Importer.Import(Params->getRAngleLoc()));
2193}
2194
Douglas Gregore2e50d332010-12-01 01:36:18 +00002195TemplateArgument
2196ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2197 switch (From.getKind()) {
2198 case TemplateArgument::Null:
2199 return TemplateArgument();
2200
2201 case TemplateArgument::Type: {
2202 QualType ToType = Importer.Import(From.getAsType());
2203 if (ToType.isNull())
2204 return TemplateArgument();
2205 return TemplateArgument(ToType);
2206 }
2207
2208 case TemplateArgument::Integral: {
2209 QualType ToType = Importer.Import(From.getIntegralType());
2210 if (ToType.isNull())
2211 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002212 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002213 }
2214
Eli Friedmanb826a002012-09-26 02:36:12 +00002215 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002216 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2217 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2218 if (!To || ToType.isNull())
2219 return TemplateArgument();
2220 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002221 }
2222
2223 case TemplateArgument::NullPtr: {
2224 QualType ToType = Importer.Import(From.getNullPtrType());
2225 if (ToType.isNull())
2226 return TemplateArgument();
2227 return TemplateArgument(ToType, /*isNullPtr*/true);
2228 }
2229
Douglas Gregore2e50d332010-12-01 01:36:18 +00002230 case TemplateArgument::Template: {
2231 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2232 if (ToTemplate.isNull())
2233 return TemplateArgument();
2234
2235 return TemplateArgument(ToTemplate);
2236 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002237
2238 case TemplateArgument::TemplateExpansion: {
2239 TemplateName ToTemplate
2240 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2241 if (ToTemplate.isNull())
2242 return TemplateArgument();
2243
Douglas Gregore1d60df2011-01-14 23:41:42 +00002244 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002245 }
2246
Douglas Gregore2e50d332010-12-01 01:36:18 +00002247 case TemplateArgument::Expression:
2248 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2249 return TemplateArgument(ToExpr);
2250 return TemplateArgument();
2251
2252 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002253 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002254 ToPack.reserve(From.pack_size());
2255 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2256 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002257
2258 return TemplateArgument(
2259 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002260 }
2261 }
2262
2263 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002264}
2265
2266bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2267 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002268 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002269 for (unsigned I = 0; I != NumFromArgs; ++I) {
2270 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2271 if (To.isNull() && !FromArgs[I].isNull())
2272 return true;
2273
2274 ToArgs.push_back(To);
2275 }
2276
2277 return false;
2278}
2279
Douglas Gregor5c73e912010-02-11 00:48:18 +00002280bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002281 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002282 // Eliminate a potential failure point where we attempt to re-import
2283 // something we're trying to import while completing ToRecord.
2284 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2285 if (ToOrigin) {
2286 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2287 if (ToOriginRecord)
2288 ToRecord = ToOriginRecord;
2289 }
2290
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002291 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002292 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002293 Importer.getNonEquivalentDecls(),
2294 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002295 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002296}
2297
Larisse Voufo39a1e502013-08-06 01:03:05 +00002298bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2299 bool Complain) {
2300 StructuralEquivalenceContext Ctx(
2301 Importer.getFromContext(), Importer.getToContext(),
2302 Importer.getNonEquivalentDecls(), false, Complain);
2303 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2304}
2305
Douglas Gregor98c10182010-02-12 22:17:39 +00002306bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002307 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002308 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002309 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002310 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002311}
2312
Douglas Gregor91155082012-11-14 22:29:20 +00002313bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2314 EnumConstantDecl *ToEC)
2315{
2316 const llvm::APSInt &FromVal = FromEC->getInitVal();
2317 const llvm::APSInt &ToVal = ToEC->getInitVal();
2318
2319 return FromVal.isSigned() == ToVal.isSigned() &&
2320 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2321 FromVal == ToVal;
2322}
2323
2324bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002325 ClassTemplateDecl *To) {
2326 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2327 Importer.getToContext(),
2328 Importer.getNonEquivalentDecls());
2329 return Ctx.IsStructurallyEquivalent(From, To);
2330}
2331
Larisse Voufo39a1e502013-08-06 01:03:05 +00002332bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2333 VarTemplateDecl *To) {
2334 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2335 Importer.getToContext(),
2336 Importer.getNonEquivalentDecls());
2337 return Ctx.IsStructurallyEquivalent(From, To);
2338}
2339
Douglas Gregore4c83e42010-02-09 22:48:33 +00002340Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002341 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002342 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002343 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002344}
2345
Sean Callanan65198272011-11-17 23:20:56 +00002346Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2347 TranslationUnitDecl *ToD =
2348 Importer.getToContext().getTranslationUnitDecl();
2349
2350 Importer.Imported(D, ToD);
2351
2352 return ToD;
2353}
2354
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002355Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2356
2357 SourceLocation Loc = Importer.Import(D->getLocation());
2358 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2359
2360 // Import the context of this declaration.
2361 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2362 if (!DC)
2363 return nullptr;
2364
2365 AccessSpecDecl *accessSpecDecl
2366 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2367 DC, Loc, ColonLoc);
2368
2369 if (!accessSpecDecl)
2370 return nullptr;
2371
2372 // Lexical DeclContext and Semantic DeclContext
2373 // is always the same for the accessSpec.
2374 accessSpecDecl->setLexicalDeclContext(DC);
2375 DC->addDeclInternal(accessSpecDecl);
2376
2377 return accessSpecDecl;
2378}
2379
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002380Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2381 // Import the major distinguishing characteristics of this namespace.
2382 DeclContext *DC, *LexicalDC;
2383 DeclarationName Name;
2384 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002385 NamedDecl *ToD;
2386 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002387 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002388 if (ToD)
2389 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002390
2391 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002392 if (!Name) {
2393 // This is an anonymous namespace. Adopt an existing anonymous
2394 // namespace if we can.
2395 // FIXME: Not testable.
2396 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2397 MergeWithNamespace = TU->getAnonymousNamespace();
2398 else
2399 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2400 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002401 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002402 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002403 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002404 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2405 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002406 continue;
2407
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002408 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002409 MergeWithNamespace = FoundNS;
2410 ConflictingDecls.clear();
2411 break;
2412 }
2413
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002414 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002415 }
2416
2417 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002418 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002419 ConflictingDecls.data(),
2420 ConflictingDecls.size());
2421 }
2422 }
2423
2424 // Create the "to" namespace, if needed.
2425 NamespaceDecl *ToNamespace = MergeWithNamespace;
2426 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002427 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002428 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002429 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002430 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002431 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002432 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002433 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002434
2435 // If this is an anonymous namespace, register it as the anonymous
2436 // namespace within its context.
2437 if (!Name) {
2438 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2439 TU->setAnonymousNamespace(ToNamespace);
2440 else
2441 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2442 }
2443 }
2444 Importer.Imported(D, ToNamespace);
2445
2446 ImportDeclContext(D);
2447
2448 return ToNamespace;
2449}
2450
Richard Smithdda56e42011-04-15 14:24:37 +00002451Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002452 // Import the major distinguishing characteristics of this typedef.
2453 DeclContext *DC, *LexicalDC;
2454 DeclarationName Name;
2455 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002456 NamedDecl *ToD;
2457 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002458 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002459 if (ToD)
2460 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002461
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002462 // If this typedef is not in block scope, determine whether we've
2463 // seen a typedef with the same name (that we can merge with) or any
2464 // other entity by that name (which name lookup could conflict with).
2465 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002466 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002467 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002468 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002469 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002470 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2471 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002472 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002473 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002474 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002475 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2476 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002477 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002478 }
2479
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002480 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002481 }
2482
2483 if (!ConflictingDecls.empty()) {
2484 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2485 ConflictingDecls.data(),
2486 ConflictingDecls.size());
2487 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002488 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002489 }
2490 }
2491
Douglas Gregorb4964f72010-02-15 23:54:17 +00002492 // Import the underlying type of this typedef;
2493 QualType T = Importer.Import(D->getUnderlyingType());
2494 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002495 return nullptr;
2496
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002497 // Create the new typedef node.
2498 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002499 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002500 TypedefNameDecl *ToTypedef;
2501 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002502 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2503 StartL, Loc,
2504 Name.getAsIdentifierInfo(),
2505 TInfo);
2506 else
Richard Smithdda56e42011-04-15 14:24:37 +00002507 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2508 StartL, Loc,
2509 Name.getAsIdentifierInfo(),
2510 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002511
Douglas Gregordd483172010-02-22 17:42:47 +00002512 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002513 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002514 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002515 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002516
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002517 return ToTypedef;
2518}
2519
Richard Smithdda56e42011-04-15 14:24:37 +00002520Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2521 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2522}
2523
2524Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2525 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2526}
2527
Douglas Gregor98c10182010-02-12 22:17:39 +00002528Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2529 // Import the major distinguishing characteristics of this enum.
2530 DeclContext *DC, *LexicalDC;
2531 DeclarationName Name;
2532 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002533 NamedDecl *ToD;
2534 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002535 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002536 if (ToD)
2537 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002538
Douglas Gregor98c10182010-02-12 22:17:39 +00002539 // Figure out what enum name we're looking for.
2540 unsigned IDNS = Decl::IDNS_Tag;
2541 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002542 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2543 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002544 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002545 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002546 IDNS |= Decl::IDNS_Ordinary;
2547
2548 // We may already have an enum of the same name; try to find and match it.
2549 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002550 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002551 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002552 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002553 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2554 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002555 continue;
2556
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002557 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002558 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002559 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2560 Found = Tag->getDecl();
2561 }
2562
2563 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002564 if (IsStructuralMatch(D, FoundEnum))
2565 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002566 }
2567
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002568 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002569 }
2570
2571 if (!ConflictingDecls.empty()) {
2572 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2573 ConflictingDecls.data(),
2574 ConflictingDecls.size());
2575 }
2576 }
2577
2578 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002579 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2580 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002581 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002582 D->isScoped(), D->isScopedUsingClassTag(),
2583 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002584 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002585 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002586 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002587 D2->setLexicalDeclContext(LexicalDC);
2588 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002589 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002590
2591 // Import the integer type.
2592 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2593 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002594 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002595 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002596
2597 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002598 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002599 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002600
Douglas Gregor3996e242010-02-15 22:01:00 +00002601 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002602}
2603
Douglas Gregor5c73e912010-02-11 00:48:18 +00002604Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2605 // If this record has a definition in the translation unit we're coming from,
2606 // but this particular declaration is not that definition, import the
2607 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002608 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002609 if (Definition && Definition != D) {
2610 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002611 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002612 return nullptr;
2613
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002614 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002615 }
2616
2617 // Import the major distinguishing characteristics of this record.
2618 DeclContext *DC, *LexicalDC;
2619 DeclarationName Name;
2620 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002621 NamedDecl *ToD;
2622 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002623 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002624 if (ToD)
2625 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002626
Douglas Gregor5c73e912010-02-11 00:48:18 +00002627 // Figure out what structure name we're looking for.
2628 unsigned IDNS = Decl::IDNS_Tag;
2629 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002630 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2631 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002632 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002633 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002634 IDNS |= Decl::IDNS_Ordinary;
2635
2636 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002637 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002638 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002639 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002640 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002641 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002642 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2643 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002644 continue;
2645
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002646 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002647 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002648 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2649 Found = Tag->getDecl();
2650 }
2651
2652 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002653 if (D->isAnonymousStructOrUnion() &&
2654 FoundRecord->isAnonymousStructOrUnion()) {
2655 // If both anonymous structs/unions are in a record context, make sure
2656 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002657 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002658 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002659 if (Optional<unsigned> Index2 =
2660 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002661 if (*Index1 != *Index2)
2662 continue;
2663 }
2664 }
2665 }
2666
Douglas Gregor25791052010-02-12 00:09:27 +00002667 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002668 if ((SearchName && !D->isCompleteDefinition())
2669 || (D->isCompleteDefinition() &&
2670 D->isAnonymousStructOrUnion()
2671 == FoundDef->isAnonymousStructOrUnion() &&
2672 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002673 // The record types structurally match, or the "from" translation
2674 // unit only had a forward declaration anyway; call it the same
2675 // function.
2676 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002677 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002678 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002679 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002680 // We have a forward declaration of this type, so adopt that forward
2681 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002682
2683 // If one or both can be completed from external storage then try one
2684 // last time to complete and compare them before doing this.
2685
2686 if (FoundRecord->hasExternalLexicalStorage() &&
2687 !FoundRecord->isCompleteDefinition())
2688 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2689 if (D->hasExternalLexicalStorage())
2690 D->getASTContext().getExternalSource()->CompleteType(D);
2691
2692 if (FoundRecord->isCompleteDefinition() &&
2693 D->isCompleteDefinition() &&
2694 !IsStructuralMatch(D, FoundRecord))
2695 continue;
2696
Douglas Gregor25791052010-02-12 00:09:27 +00002697 AdoptDecl = FoundRecord;
2698 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002699 } else if (!SearchName) {
2700 continue;
2701 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002702 }
2703
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002704 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002705 }
2706
Douglas Gregordd6006f2012-07-17 21:16:27 +00002707 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002708 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2709 ConflictingDecls.data(),
2710 ConflictingDecls.size());
2711 }
2712 }
2713
2714 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002715 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002716 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002717 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002718 CXXRecordDecl *D2CXX = nullptr;
2719 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2720 if (DCXX->isLambda()) {
2721 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2722 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2723 DC, TInfo, Loc,
2724 DCXX->isDependentLambda(),
2725 DCXX->isGenericLambda(),
2726 DCXX->getLambdaCaptureDefault());
2727 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2728 if (DCXX->getLambdaContextDecl() && !CDecl)
2729 return nullptr;
2730 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(),
2731 CDecl);
2732 } else {
2733 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2734 D->getTagKind(),
2735 DC, StartLoc, Loc,
2736 Name.getAsIdentifierInfo());
2737 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002738 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002739 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002740 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002741 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002742 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002743 }
Douglas Gregor14454802011-02-25 02:25:35 +00002744
2745 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002746 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002747 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002748 if (D->isAnonymousStructOrUnion())
2749 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002750 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002751
Douglas Gregor3996e242010-02-15 22:01:00 +00002752 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002753
Douglas Gregor95d82832012-01-24 18:36:04 +00002754 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002755 return nullptr;
2756
Douglas Gregor3996e242010-02-15 22:01:00 +00002757 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002758}
2759
Douglas Gregor98c10182010-02-12 22:17:39 +00002760Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2761 // Import the major distinguishing characteristics of this enumerator.
2762 DeclContext *DC, *LexicalDC;
2763 DeclarationName Name;
2764 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002765 NamedDecl *ToD;
2766 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002767 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002768 if (ToD)
2769 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002770
2771 QualType T = Importer.Import(D->getType());
2772 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002773 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002774
Douglas Gregor98c10182010-02-12 22:17:39 +00002775 // Determine whether there are any other declarations with the same name and
2776 // in the same context.
2777 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002778 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002779 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002780 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002781 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002782 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2783 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002784 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002785
2786 if (EnumConstantDecl *FoundEnumConstant
2787 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2788 if (IsStructuralMatch(D, FoundEnumConstant))
2789 return Importer.Imported(D, FoundEnumConstant);
2790 }
2791
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002792 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002793 }
2794
2795 if (!ConflictingDecls.empty()) {
2796 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2797 ConflictingDecls.data(),
2798 ConflictingDecls.size());
2799 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002800 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002801 }
2802 }
2803
2804 Expr *Init = Importer.Import(D->getInitExpr());
2805 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002806 return nullptr;
2807
Douglas Gregor98c10182010-02-12 22:17:39 +00002808 EnumConstantDecl *ToEnumerator
2809 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2810 Name.getAsIdentifierInfo(), T,
2811 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002812 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002813 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002814 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002815 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002816 return ToEnumerator;
2817}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002818
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002819Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2820 // Import the major distinguishing characteristics of this function.
2821 DeclContext *DC, *LexicalDC;
2822 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002823 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002824 NamedDecl *ToD;
2825 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002826 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002827 if (ToD)
2828 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002829
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002830 // Try to find a function in our own ("to") context with the same name, same
2831 // type, and in the same context as the function we're importing.
2832 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002833 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002834 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002835 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002836 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002837 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2838 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002839 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002840
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002841 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002842 if (FoundFunction->hasExternalFormalLinkage() &&
2843 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002844 if (Importer.IsStructurallyEquivalent(D->getType(),
2845 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002846 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002847 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002848 }
2849
2850 // FIXME: Check for overloading more carefully, e.g., by boosting
2851 // Sema::IsOverload out to the AST library.
2852
2853 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002854 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002855 continue;
2856
2857 // Complain about inconsistent function types.
2858 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002859 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002860 Importer.ToDiag(FoundFunction->getLocation(),
2861 diag::note_odr_value_here)
2862 << FoundFunction->getType();
2863 }
2864 }
2865
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002866 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002867 }
2868
2869 if (!ConflictingDecls.empty()) {
2870 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2871 ConflictingDecls.data(),
2872 ConflictingDecls.size());
2873 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002874 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002875 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002876 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002877
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002878 DeclarationNameInfo NameInfo(Name, Loc);
2879 // Import additional name location/type info.
2880 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2881
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002882 QualType FromTy = D->getType();
2883 bool usedDifferentExceptionSpec = false;
2884
2885 if (const FunctionProtoType *
2886 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2887 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2888 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2889 // FunctionDecl that we are importing the FunctionProtoType for.
2890 // To avoid an infinite recursion when importing, create the FunctionDecl
2891 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002892 if (FromEPI.ExceptionSpec.SourceDecl ||
2893 FromEPI.ExceptionSpec.SourceTemplate ||
2894 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002895 FunctionProtoType::ExtProtoInfo DefaultEPI;
2896 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002897 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002898 usedDifferentExceptionSpec = true;
2899 }
2900 }
2901
Douglas Gregorb4964f72010-02-15 23:54:17 +00002902 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002903 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002904 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002905 return nullptr;
2906
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002907 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002908 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002909 for (auto P : D->params()) {
2910 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002911 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002912 return nullptr;
2913
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002914 Parameters.push_back(ToP);
2915 }
2916
2917 // Create the imported function.
2918 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002919 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002920 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002921 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2922 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2923 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002924 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002925 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002926 FromConstructor->isExplicit(),
2927 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002928 D->isImplicit(),
2929 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002930 } else if (isa<CXXDestructorDecl>(D)) {
2931 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2932 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002933 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002934 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002935 D->isInlineSpecified(),
2936 D->isImplicit());
2937 } else if (CXXConversionDecl *FromConversion
2938 = dyn_cast<CXXConversionDecl>(D)) {
2939 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2940 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002941 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002942 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002943 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002944 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002945 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002946 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002947 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2948 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2949 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002950 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002951 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002952 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002953 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002954 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002955 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002956 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002957 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002958 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002959 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002960 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002961 D->hasWrittenPrototype(),
2962 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002963 }
John McCall3e11ebe2010-03-15 10:12:16 +00002964
2965 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002966 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002967 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002968 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002969 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2970 ToFunction->setTrivial(D->isTrivial());
2971 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002972 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002973
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002974 // Set the parameters.
2975 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002976 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002977 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978 }
David Blaikie9c70e042011-09-21 18:16:56 +00002979 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002980
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002981 if (usedDifferentExceptionSpec) {
2982 // Update FunctionProtoType::ExtProtoInfo.
2983 QualType T = Importer.Import(D->getType());
2984 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002985 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002986 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002987 }
2988
Sean Callanan59721b32015-04-28 18:41:46 +00002989 // Import the body, if any.
2990 if (Stmt *FromBody = D->getBody()) {
2991 if (Stmt *ToBody = Importer.Import(FromBody)) {
2992 ToFunction->setBody(ToBody);
2993 }
2994 }
2995
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002996 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002997
2998 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002999 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003000
Douglas Gregor43f54792010-02-17 02:12:47 +00003001 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002}
3003
Douglas Gregor00eace12010-02-21 18:29:16 +00003004Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3005 return VisitFunctionDecl(D);
3006}
3007
3008Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3009 return VisitCXXMethodDecl(D);
3010}
3011
3012Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3013 return VisitCXXMethodDecl(D);
3014}
3015
3016Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3017 return VisitCXXMethodDecl(D);
3018}
3019
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003020static unsigned getFieldIndex(Decl *F) {
3021 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3022 if (!Owner)
3023 return 0;
3024
3025 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003026 for (const auto *D : Owner->noload_decls()) {
3027 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003028 return Index;
3029
3030 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3031 ++Index;
3032 }
3033
3034 return Index;
3035}
3036
Douglas Gregor5c73e912010-02-11 00:48:18 +00003037Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3038 // Import the major distinguishing characteristics of a variable.
3039 DeclContext *DC, *LexicalDC;
3040 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003041 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003042 NamedDecl *ToD;
3043 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003044 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003045 if (ToD)
3046 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003047
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003048 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003049 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003050 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003051 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3052 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003053 // For anonymous fields, match up by index.
3054 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3055 continue;
3056
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003057 if (Importer.IsStructurallyEquivalent(D->getType(),
3058 FoundField->getType())) {
3059 Importer.Imported(D, FoundField);
3060 return FoundField;
3061 }
3062
3063 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3064 << Name << D->getType() << FoundField->getType();
3065 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3066 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003067 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003068 }
3069 }
3070
Douglas Gregorb4964f72010-02-15 23:54:17 +00003071 // Import the type.
3072 QualType T = Importer.Import(D->getType());
3073 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003074 return nullptr;
3075
Douglas Gregor5c73e912010-02-11 00:48:18 +00003076 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3077 Expr *BitWidth = Importer.Import(D->getBitWidth());
3078 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003079 return nullptr;
3080
Abramo Bagnaradff19302011-03-08 08:55:46 +00003081 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3082 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003083 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003084 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003085 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003086 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003087 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003088 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003089 Expr *ToInitializer = Importer.Import(FromInitializer);
3090 if (ToInitializer)
3091 ToField->setInClassInitializer(ToInitializer);
3092 else
3093 return nullptr;
3094 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003095 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003096 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003097 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003098 return ToField;
3099}
3100
Francois Pichet783dd6e2010-11-21 06:08:52 +00003101Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3102 // Import the major distinguishing characteristics of a variable.
3103 DeclContext *DC, *LexicalDC;
3104 DeclarationName Name;
3105 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003106 NamedDecl *ToD;
3107 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003108 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003109 if (ToD)
3110 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003111
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003112 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003113 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003114 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003115 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003116 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003117 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003118 // For anonymous indirect fields, match up by index.
3119 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3120 continue;
3121
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003122 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003123 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003124 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003125 Importer.Imported(D, FoundField);
3126 return FoundField;
3127 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003128
3129 // If there are more anonymous fields to check, continue.
3130 if (!Name && I < N-1)
3131 continue;
3132
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003133 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3134 << Name << D->getType() << FoundField->getType();
3135 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3136 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003137 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003138 }
3139 }
3140
Francois Pichet783dd6e2010-11-21 06:08:52 +00003141 // Import the type.
3142 QualType T = Importer.Import(D->getType());
3143 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003144 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003145
3146 NamedDecl **NamedChain =
3147 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3148
3149 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003150 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003151 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003152 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003153 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003154 NamedChain[i++] = cast<NamedDecl>(D);
3155 }
3156
3157 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003158 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3159 NamedChain, D->getChainingSize());
3160
3161 for (const auto *Attr : D->attrs())
3162 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3163
Francois Pichet783dd6e2010-11-21 06:08:52 +00003164 ToIndirectField->setAccess(D->getAccess());
3165 ToIndirectField->setLexicalDeclContext(LexicalDC);
3166 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003167 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003168 return ToIndirectField;
3169}
3170
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003171Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3172 // Import the major distinguishing characteristics of an ivar.
3173 DeclContext *DC, *LexicalDC;
3174 DeclarationName Name;
3175 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003176 NamedDecl *ToD;
3177 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003179 if (ToD)
3180 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003181
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003182 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003183 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003184 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003185 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3186 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003187 if (Importer.IsStructurallyEquivalent(D->getType(),
3188 FoundIvar->getType())) {
3189 Importer.Imported(D, FoundIvar);
3190 return FoundIvar;
3191 }
3192
3193 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3194 << Name << D->getType() << FoundIvar->getType();
3195 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3196 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003197 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003198 }
3199 }
3200
3201 // Import the type.
3202 QualType T = Importer.Import(D->getType());
3203 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003204 return nullptr;
3205
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003206 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3207 Expr *BitWidth = Importer.Import(D->getBitWidth());
3208 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003209 return nullptr;
3210
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003211 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3212 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003213 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003214 Loc, Name.getAsIdentifierInfo(),
3215 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003216 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003217 ToIvar->setLexicalDeclContext(LexicalDC);
3218 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003219 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003220 return ToIvar;
3221
3222}
3223
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003224Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3225 // Import the major distinguishing characteristics of a variable.
3226 DeclContext *DC, *LexicalDC;
3227 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003228 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003229 NamedDecl *ToD;
3230 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003231 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003232 if (ToD)
3233 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003234
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003235 // Try to find a variable in our own ("to") context with the same name and
3236 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003237 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003238 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003239 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003240 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003241 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003242 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003243 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3244 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003245 continue;
3246
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003247 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003248 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003249 if (FoundVar->hasExternalFormalLinkage() &&
3250 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003251 if (Importer.IsStructurallyEquivalent(D->getType(),
3252 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003253 MergeWithVar = FoundVar;
3254 break;
3255 }
3256
Douglas Gregor56521c52010-02-12 17:23:39 +00003257 const ArrayType *FoundArray
3258 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3259 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003260 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003261 if (FoundArray && TArray) {
3262 if (isa<IncompleteArrayType>(FoundArray) &&
3263 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003264 // Import the type.
3265 QualType T = Importer.Import(D->getType());
3266 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003267 return nullptr;
3268
Douglas Gregor56521c52010-02-12 17:23:39 +00003269 FoundVar->setType(T);
3270 MergeWithVar = FoundVar;
3271 break;
3272 } else if (isa<IncompleteArrayType>(TArray) &&
3273 isa<ConstantArrayType>(FoundArray)) {
3274 MergeWithVar = FoundVar;
3275 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003276 }
3277 }
3278
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003279 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003280 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003281 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3282 << FoundVar->getType();
3283 }
3284 }
3285
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003286 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003287 }
3288
3289 if (MergeWithVar) {
3290 // An equivalent variable with external linkage has been found. Link
3291 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003292 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003293
3294 if (VarDecl *DDef = D->getDefinition()) {
3295 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3296 Importer.ToDiag(ExistingDef->getLocation(),
3297 diag::err_odr_variable_multiple_def)
3298 << Name;
3299 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3300 } else {
3301 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003302 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003303 if (DDef->isInitKnownICE()) {
3304 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3305 Eval->CheckedICE = true;
3306 Eval->IsICE = DDef->isInitICE();
3307 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003308 }
3309 }
3310
3311 return MergeWithVar;
3312 }
3313
3314 if (!ConflictingDecls.empty()) {
3315 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3316 ConflictingDecls.data(),
3317 ConflictingDecls.size());
3318 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003319 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003320 }
3321 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003322
Douglas Gregorb4964f72010-02-15 23:54:17 +00003323 // Import the type.
3324 QualType T = Importer.Import(D->getType());
3325 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003326 return nullptr;
3327
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003328 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003329 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003330 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3331 Importer.Import(D->getInnerLocStart()),
3332 Loc, Name.getAsIdentifierInfo(),
3333 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003334 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003335 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003336 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003337 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003338 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003339 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003340
Sean Callanan59721b32015-04-28 18:41:46 +00003341 if (!D->isFileVarDecl() &&
3342 D->isUsed())
3343 ToVar->setIsUsed();
3344
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003345 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003346 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003347 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003348
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003349 return ToVar;
3350}
3351
Douglas Gregor8b228d72010-02-17 21:22:52 +00003352Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3353 // Parameters are created in the translation unit's context, then moved
3354 // into the function declaration's context afterward.
3355 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3356
3357 // Import the name of this declaration.
3358 DeclarationName Name = Importer.Import(D->getDeclName());
3359 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003360 return nullptr;
3361
Douglas Gregor8b228d72010-02-17 21:22:52 +00003362 // Import the location of this declaration.
3363 SourceLocation Loc = Importer.Import(D->getLocation());
3364
3365 // Import the parameter's type.
3366 QualType T = Importer.Import(D->getType());
3367 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003368 return nullptr;
3369
Douglas Gregor8b228d72010-02-17 21:22:52 +00003370 // Create the imported parameter.
3371 ImplicitParamDecl *ToParm
3372 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3373 Loc, Name.getAsIdentifierInfo(),
3374 T);
3375 return Importer.Imported(D, ToParm);
3376}
3377
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003378Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3379 // Parameters are created in the translation unit's context, then moved
3380 // into the function declaration's context afterward.
3381 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3382
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003383 // Import the name of this declaration.
3384 DeclarationName Name = Importer.Import(D->getDeclName());
3385 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003386 return nullptr;
3387
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003388 // Import the location of this declaration.
3389 SourceLocation Loc = Importer.Import(D->getLocation());
3390
3391 // Import the parameter's type.
3392 QualType T = Importer.Import(D->getType());
3393 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003394 return nullptr;
3395
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003396 // Create the imported parameter.
3397 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3398 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003399 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003400 Loc, Name.getAsIdentifierInfo(),
3401 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003402 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003403 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003404
3405 if (D->isUsed())
3406 ToParm->setIsUsed();
3407
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003408 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003409}
3410
Douglas Gregor43f54792010-02-17 02:12:47 +00003411Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3412 // Import the major distinguishing characteristics of a method.
3413 DeclContext *DC, *LexicalDC;
3414 DeclarationName Name;
3415 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003416 NamedDecl *ToD;
3417 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003418 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003419 if (ToD)
3420 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003421
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003422 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003423 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003424 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3425 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003426 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3427 continue;
3428
3429 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003430 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3431 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003432 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003433 << D->isInstanceMethod() << Name << D->getReturnType()
3434 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003435 Importer.ToDiag(FoundMethod->getLocation(),
3436 diag::note_odr_objc_method_here)
3437 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003438 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003439 }
3440
3441 // Check the number of parameters.
3442 if (D->param_size() != FoundMethod->param_size()) {
3443 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3444 << D->isInstanceMethod() << Name
3445 << D->param_size() << FoundMethod->param_size();
3446 Importer.ToDiag(FoundMethod->getLocation(),
3447 diag::note_odr_objc_method_here)
3448 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003449 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003450 }
3451
3452 // Check parameter types.
3453 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3454 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3455 P != PEnd; ++P, ++FoundP) {
3456 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3457 (*FoundP)->getType())) {
3458 Importer.FromDiag((*P)->getLocation(),
3459 diag::err_odr_objc_method_param_type_inconsistent)
3460 << D->isInstanceMethod() << Name
3461 << (*P)->getType() << (*FoundP)->getType();
3462 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3463 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003464 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003465 }
3466 }
3467
3468 // Check variadic/non-variadic.
3469 // Check the number of parameters.
3470 if (D->isVariadic() != FoundMethod->isVariadic()) {
3471 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3472 << D->isInstanceMethod() << Name;
3473 Importer.ToDiag(FoundMethod->getLocation(),
3474 diag::note_odr_objc_method_here)
3475 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003476 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003477 }
3478
3479 // FIXME: Any other bits we need to merge?
3480 return Importer.Imported(D, FoundMethod);
3481 }
3482 }
3483
3484 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003485 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003486 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003487 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003488
Alp Toker314cc812014-01-25 16:55:45 +00003489 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003490
Alp Toker314cc812014-01-25 16:55:45 +00003491 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3492 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3493 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3494 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3495 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003496
3497 // FIXME: When we decide to merge method definitions, we'll need to
3498 // deal with implicit parameters.
3499
3500 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003501 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003502 for (auto *FromP : D->params()) {
3503 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003504 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003505 return nullptr;
3506
Douglas Gregor43f54792010-02-17 02:12:47 +00003507 ToParams.push_back(ToP);
3508 }
3509
3510 // Set the parameters.
3511 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3512 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003513 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003514 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003515 SmallVector<SourceLocation, 12> SelLocs;
3516 D->getSelectorLocs(SelLocs);
3517 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003518
3519 ToMethod->setLexicalDeclContext(LexicalDC);
3520 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003521 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003522 return ToMethod;
3523}
3524
Douglas Gregor85f3f952015-07-07 03:57:15 +00003525Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3526 // Import the major distinguishing characteristics of a category.
3527 DeclContext *DC, *LexicalDC;
3528 DeclarationName Name;
3529 SourceLocation Loc;
3530 NamedDecl *ToD;
3531 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3532 return nullptr;
3533 if (ToD)
3534 return ToD;
3535
3536 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3537 if (!BoundInfo)
3538 return nullptr;
3539
3540 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3541 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003542 D->getVariance(),
3543 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003544 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003545 Importer.Import(D->getLocation()),
3546 Name.getAsIdentifierInfo(),
3547 Importer.Import(D->getColonLoc()),
3548 BoundInfo);
3549 Importer.Imported(D, Result);
3550 Result->setLexicalDeclContext(LexicalDC);
3551 return Result;
3552}
3553
Douglas Gregor84c51c32010-02-18 01:47:50 +00003554Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3555 // Import the major distinguishing characteristics of a category.
3556 DeclContext *DC, *LexicalDC;
3557 DeclarationName Name;
3558 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003559 NamedDecl *ToD;
3560 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003561 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003562 if (ToD)
3563 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003564
Douglas Gregor84c51c32010-02-18 01:47:50 +00003565 ObjCInterfaceDecl *ToInterface
3566 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3567 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003568 return nullptr;
3569
Douglas Gregor84c51c32010-02-18 01:47:50 +00003570 // Determine if we've already encountered this category.
3571 ObjCCategoryDecl *MergeWithCategory
3572 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3573 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3574 if (!ToCategory) {
3575 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003576 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003577 Loc,
3578 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003579 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003580 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003581 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003582 Importer.Import(D->getIvarLBraceLoc()),
3583 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003584 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003585 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003586 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003587 // Import the type parameter list after calling Imported, to avoid
3588 // loops when bringing in their DeclContext.
3589 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3590 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003591
Douglas Gregor84c51c32010-02-18 01:47:50 +00003592 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003593 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3594 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003595 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3596 = D->protocol_loc_begin();
3597 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3598 FromProtoEnd = D->protocol_end();
3599 FromProto != FromProtoEnd;
3600 ++FromProto, ++FromProtoLoc) {
3601 ObjCProtocolDecl *ToProto
3602 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3603 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003604 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003605 Protocols.push_back(ToProto);
3606 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3607 }
3608
3609 // FIXME: If we're merging, make sure that the protocol list is the same.
3610 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3611 ProtocolLocs.data(), Importer.getToContext());
3612
3613 } else {
3614 Importer.Imported(D, ToCategory);
3615 }
3616
3617 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003618 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003619
3620 // If we have an implementation, import it as well.
3621 if (D->getImplementation()) {
3622 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003623 = cast_or_null<ObjCCategoryImplDecl>(
3624 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003625 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003626 return nullptr;
3627
Douglas Gregor84c51c32010-02-18 01:47:50 +00003628 ToCategory->setImplementation(Impl);
3629 }
3630
3631 return ToCategory;
3632}
3633
Douglas Gregor2aa53772012-01-24 17:42:07 +00003634bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3635 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003636 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003637 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003638 if (shouldForceImportDeclContext(Kind))
3639 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003640 return false;
3641 }
3642
3643 // Start the protocol definition
3644 To->startDefinition();
3645
3646 // Import protocols
3647 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3648 SmallVector<SourceLocation, 4> ProtocolLocs;
3649 ObjCProtocolDecl::protocol_loc_iterator
3650 FromProtoLoc = From->protocol_loc_begin();
3651 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3652 FromProtoEnd = From->protocol_end();
3653 FromProto != FromProtoEnd;
3654 ++FromProto, ++FromProtoLoc) {
3655 ObjCProtocolDecl *ToProto
3656 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3657 if (!ToProto)
3658 return true;
3659 Protocols.push_back(ToProto);
3660 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3661 }
3662
3663 // FIXME: If we're merging, make sure that the protocol list is the same.
3664 To->setProtocolList(Protocols.data(), Protocols.size(),
3665 ProtocolLocs.data(), Importer.getToContext());
3666
Douglas Gregor2e15c842012-02-01 21:00:38 +00003667 if (shouldForceImportDeclContext(Kind)) {
3668 // Import all of the members of this protocol.
3669 ImportDeclContext(From, /*ForceImport=*/true);
3670 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003671 return false;
3672}
3673
Douglas Gregor98d156a2010-02-17 16:12:00 +00003674Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003675 // If this protocol has a definition in the translation unit we're coming
3676 // from, but this particular declaration is not that definition, import the
3677 // definition and map to that.
3678 ObjCProtocolDecl *Definition = D->getDefinition();
3679 if (Definition && Definition != D) {
3680 Decl *ImportedDef = Importer.Import(Definition);
3681 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003682 return nullptr;
3683
Douglas Gregor2aa53772012-01-24 17:42:07 +00003684 return Importer.Imported(D, ImportedDef);
3685 }
3686
Douglas Gregor84c51c32010-02-18 01:47:50 +00003687 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003688 DeclContext *DC, *LexicalDC;
3689 DeclarationName Name;
3690 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003691 NamedDecl *ToD;
3692 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003693 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003694 if (ToD)
3695 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003696
Craig Topper36250ad2014-05-12 05:36:57 +00003697 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003698 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003699 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003700 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3701 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003702 continue;
3703
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003704 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003705 break;
3706 }
3707
3708 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003709 if (!ToProto) {
3710 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3711 Name.getAsIdentifierInfo(), Loc,
3712 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003713 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003714 ToProto->setLexicalDeclContext(LexicalDC);
3715 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003716 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003717
3718 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003719
Douglas Gregor2aa53772012-01-24 17:42:07 +00003720 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003721 return nullptr;
3722
Douglas Gregor98d156a2010-02-17 16:12:00 +00003723 return ToProto;
3724}
3725
Sean Callanan0aae0412014-12-10 00:00:37 +00003726Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3727 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3728 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3729
3730 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3731 SourceLocation LangLoc = Importer.Import(D->getLocation());
3732
3733 bool HasBraces = D->hasBraces();
3734
Sean Callananb12a8552014-12-10 21:22:20 +00003735 LinkageSpecDecl *ToLinkageSpec =
3736 LinkageSpecDecl::Create(Importer.getToContext(),
3737 DC,
3738 ExternLoc,
3739 LangLoc,
3740 D->getLanguage(),
3741 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003742
3743 if (HasBraces) {
3744 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3745 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3746 }
3747
3748 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3749 LexicalDC->addDeclInternal(ToLinkageSpec);
3750
3751 Importer.Imported(D, ToLinkageSpec);
3752
3753 return ToLinkageSpec;
3754}
3755
Douglas Gregor2aa53772012-01-24 17:42:07 +00003756bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3757 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003758 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003759 if (To->getDefinition()) {
3760 // Check consistency of superclass.
3761 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3762 if (FromSuper) {
3763 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3764 if (!FromSuper)
3765 return true;
3766 }
3767
3768 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3769 if ((bool)FromSuper != (bool)ToSuper ||
3770 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3771 Importer.ToDiag(To->getLocation(),
3772 diag::err_odr_objc_superclass_inconsistent)
3773 << To->getDeclName();
3774 if (ToSuper)
3775 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3776 << To->getSuperClass()->getDeclName();
3777 else
3778 Importer.ToDiag(To->getLocation(),
3779 diag::note_odr_objc_missing_superclass);
3780 if (From->getSuperClass())
3781 Importer.FromDiag(From->getSuperClassLoc(),
3782 diag::note_odr_objc_superclass)
3783 << From->getSuperClass()->getDeclName();
3784 else
3785 Importer.FromDiag(From->getLocation(),
3786 diag::note_odr_objc_missing_superclass);
3787 }
3788
Douglas Gregor2e15c842012-02-01 21:00:38 +00003789 if (shouldForceImportDeclContext(Kind))
3790 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003791 return false;
3792 }
3793
3794 // Start the definition.
3795 To->startDefinition();
3796
3797 // If this class has a superclass, import it.
3798 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003799 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3800 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003801 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003802
3803 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003804 }
3805
3806 // Import protocols
3807 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3808 SmallVector<SourceLocation, 4> ProtocolLocs;
3809 ObjCInterfaceDecl::protocol_loc_iterator
3810 FromProtoLoc = From->protocol_loc_begin();
3811
3812 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3813 FromProtoEnd = From->protocol_end();
3814 FromProto != FromProtoEnd;
3815 ++FromProto, ++FromProtoLoc) {
3816 ObjCProtocolDecl *ToProto
3817 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3818 if (!ToProto)
3819 return true;
3820 Protocols.push_back(ToProto);
3821 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3822 }
3823
3824 // FIXME: If we're merging, make sure that the protocol list is the same.
3825 To->setProtocolList(Protocols.data(), Protocols.size(),
3826 ProtocolLocs.data(), Importer.getToContext());
3827
3828 // Import categories. When the categories themselves are imported, they'll
3829 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003830 for (auto *Cat : From->known_categories())
3831 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003832
Douglas Gregor2aa53772012-01-24 17:42:07 +00003833 // If we have an @implementation, import it as well.
3834 if (From->getImplementation()) {
3835 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3836 Importer.Import(From->getImplementation()));
3837 if (!Impl)
3838 return true;
3839
3840 To->setImplementation(Impl);
3841 }
3842
Douglas Gregor2e15c842012-02-01 21:00:38 +00003843 if (shouldForceImportDeclContext(Kind)) {
3844 // Import all of the members of this class.
3845 ImportDeclContext(From, /*ForceImport=*/true);
3846 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003847 return false;
3848}
3849
Douglas Gregor85f3f952015-07-07 03:57:15 +00003850ObjCTypeParamList *
3851ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3852 if (!list)
3853 return nullptr;
3854
3855 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3856 for (auto fromTypeParam : *list) {
3857 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3858 Importer.Import(fromTypeParam));
3859 if (!toTypeParam)
3860 return nullptr;
3861
3862 toTypeParams.push_back(toTypeParam);
3863 }
3864
3865 return ObjCTypeParamList::create(Importer.getToContext(),
3866 Importer.Import(list->getLAngleLoc()),
3867 toTypeParams,
3868 Importer.Import(list->getRAngleLoc()));
3869}
3870
Douglas Gregor45635322010-02-16 01:20:57 +00003871Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003872 // If this class has a definition in the translation unit we're coming from,
3873 // but this particular declaration is not that definition, import the
3874 // definition and map to that.
3875 ObjCInterfaceDecl *Definition = D->getDefinition();
3876 if (Definition && Definition != D) {
3877 Decl *ImportedDef = Importer.Import(Definition);
3878 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003879 return nullptr;
3880
Douglas Gregor2aa53772012-01-24 17:42:07 +00003881 return Importer.Imported(D, ImportedDef);
3882 }
3883
Douglas Gregor45635322010-02-16 01:20:57 +00003884 // Import the major distinguishing characteristics of an @interface.
3885 DeclContext *DC, *LexicalDC;
3886 DeclarationName Name;
3887 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003888 NamedDecl *ToD;
3889 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003890 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003891 if (ToD)
3892 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003893
Douglas Gregor2aa53772012-01-24 17:42:07 +00003894 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003895 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003896 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003897 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003898 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3899 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003900 continue;
3901
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003902 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003903 break;
3904 }
3905
Douglas Gregor2aa53772012-01-24 17:42:07 +00003906 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003907 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003908 if (!ToIface) {
3909 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3910 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003911 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003912 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003913 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003914 D->isImplicitInterfaceDecl());
3915 ToIface->setLexicalDeclContext(LexicalDC);
3916 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003917 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003918 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003919 // Import the type parameter list after calling Imported, to avoid
3920 // loops when bringing in their DeclContext.
3921 ToIface->setTypeParamList(ImportObjCTypeParamList(
3922 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003923
Douglas Gregor2aa53772012-01-24 17:42:07 +00003924 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003925 return nullptr;
3926
Douglas Gregor98d156a2010-02-17 16:12:00 +00003927 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003928}
3929
Douglas Gregor4da9d682010-12-07 15:32:12 +00003930Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3931 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3932 Importer.Import(D->getCategoryDecl()));
3933 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003934 return nullptr;
3935
Douglas Gregor4da9d682010-12-07 15:32:12 +00003936 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3937 if (!ToImpl) {
3938 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3939 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003940 return nullptr;
3941
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003942 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003943 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003944 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003945 Category->getClassInterface(),
3946 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003947 Importer.Import(D->getAtStartLoc()),
3948 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003949
3950 DeclContext *LexicalDC = DC;
3951 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3952 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3953 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003954 return nullptr;
3955
Douglas Gregor4da9d682010-12-07 15:32:12 +00003956 ToImpl->setLexicalDeclContext(LexicalDC);
3957 }
3958
Sean Callanan95e74be2011-10-21 02:57:43 +00003959 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003960 Category->setImplementation(ToImpl);
3961 }
3962
3963 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003964 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003965 return ToImpl;
3966}
3967
Douglas Gregorda8025c2010-12-07 01:26:03 +00003968Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3969 // Find the corresponding interface.
3970 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3971 Importer.Import(D->getClassInterface()));
3972 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003973 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003974
3975 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003976 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003977 if (D->getSuperClass()) {
3978 Super = cast_or_null<ObjCInterfaceDecl>(
3979 Importer.Import(D->getSuperClass()));
3980 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003981 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003982 }
3983
3984 ObjCImplementationDecl *Impl = Iface->getImplementation();
3985 if (!Impl) {
3986 // We haven't imported an implementation yet. Create a new @implementation
3987 // now.
3988 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3989 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003990 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003991 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003992 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003993 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003994 Importer.Import(D->getIvarLBraceLoc()),
3995 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003996
3997 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3998 DeclContext *LexicalDC
3999 = Importer.ImportContext(D->getLexicalDeclContext());
4000 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004001 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004002 Impl->setLexicalDeclContext(LexicalDC);
4003 }
4004
4005 // Associate the implementation with the class it implements.
4006 Iface->setImplementation(Impl);
4007 Importer.Imported(D, Iface->getImplementation());
4008 } else {
4009 Importer.Imported(D, Iface->getImplementation());
4010
4011 // Verify that the existing @implementation has the same superclass.
4012 if ((Super && !Impl->getSuperClass()) ||
4013 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004014 (Super && Impl->getSuperClass() &&
4015 !declaresSameEntity(Super->getCanonicalDecl(),
4016 Impl->getSuperClass()))) {
4017 Importer.ToDiag(Impl->getLocation(),
4018 diag::err_odr_objc_superclass_inconsistent)
4019 << Iface->getDeclName();
4020 // FIXME: It would be nice to have the location of the superclass
4021 // below.
4022 if (Impl->getSuperClass())
4023 Importer.ToDiag(Impl->getLocation(),
4024 diag::note_odr_objc_superclass)
4025 << Impl->getSuperClass()->getDeclName();
4026 else
4027 Importer.ToDiag(Impl->getLocation(),
4028 diag::note_odr_objc_missing_superclass);
4029 if (D->getSuperClass())
4030 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004031 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004032 << D->getSuperClass()->getDeclName();
4033 else
4034 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004035 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004036 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004037 }
4038 }
4039
4040 // Import all of the members of this @implementation.
4041 ImportDeclContext(D);
4042
4043 return Impl;
4044}
4045
Douglas Gregora11c4582010-02-17 18:02:10 +00004046Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4047 // Import the major distinguishing characteristics of an @property.
4048 DeclContext *DC, *LexicalDC;
4049 DeclarationName Name;
4050 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004051 NamedDecl *ToD;
4052 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004053 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004054 if (ToD)
4055 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004056
4057 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004058 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004059 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004060 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004061 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004062 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004063 // Check property types.
4064 if (!Importer.IsStructurallyEquivalent(D->getType(),
4065 FoundProp->getType())) {
4066 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4067 << Name << D->getType() << FoundProp->getType();
4068 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4069 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004070 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004071 }
4072
4073 // FIXME: Check property attributes, getters, setters, etc.?
4074
4075 // Consider these properties to be equivalent.
4076 Importer.Imported(D, FoundProp);
4077 return FoundProp;
4078 }
4079 }
4080
4081 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004082 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4083 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004084 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004085
4086 // Create the new property.
4087 ObjCPropertyDecl *ToProperty
4088 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4089 Name.getAsIdentifierInfo(),
4090 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004091 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004092 Importer.Import(D->getType()),
4093 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004094 D->getPropertyImplementation());
4095 Importer.Imported(D, ToProperty);
4096 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004097 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004098
4099 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004100 ToProperty->setPropertyAttributesAsWritten(
4101 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004102 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4103 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4104 ToProperty->setGetterMethodDecl(
4105 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4106 ToProperty->setSetterMethodDecl(
4107 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4108 ToProperty->setPropertyIvarDecl(
4109 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4110 return ToProperty;
4111}
4112
Douglas Gregor14a49e22010-12-07 18:32:03 +00004113Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4114 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4115 Importer.Import(D->getPropertyDecl()));
4116 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004117 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004118
4119 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4120 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004121 return nullptr;
4122
Douglas Gregor14a49e22010-12-07 18:32:03 +00004123 // Import the lexical declaration context.
4124 DeclContext *LexicalDC = DC;
4125 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4126 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4127 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004128 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004129 }
4130
4131 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4132 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004133 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004134
4135 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004136 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004137 if (D->getPropertyIvarDecl()) {
4138 Ivar = cast_or_null<ObjCIvarDecl>(
4139 Importer.Import(D->getPropertyIvarDecl()));
4140 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004141 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004142 }
4143
4144 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004145 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4146 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004147 if (!ToImpl) {
4148 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4149 Importer.Import(D->getLocStart()),
4150 Importer.Import(D->getLocation()),
4151 Property,
4152 D->getPropertyImplementation(),
4153 Ivar,
4154 Importer.Import(D->getPropertyIvarDeclLoc()));
4155 ToImpl->setLexicalDeclContext(LexicalDC);
4156 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004157 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004158 } else {
4159 // Check that we have the same kind of property implementation (@synthesize
4160 // vs. @dynamic).
4161 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4162 Importer.ToDiag(ToImpl->getLocation(),
4163 diag::err_odr_objc_property_impl_kind_inconsistent)
4164 << Property->getDeclName()
4165 << (ToImpl->getPropertyImplementation()
4166 == ObjCPropertyImplDecl::Dynamic);
4167 Importer.FromDiag(D->getLocation(),
4168 diag::note_odr_objc_property_impl_kind)
4169 << D->getPropertyDecl()->getDeclName()
4170 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004171 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004172 }
4173
4174 // For @synthesize, check that we have the same
4175 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4176 Ivar != ToImpl->getPropertyIvarDecl()) {
4177 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4178 diag::err_odr_objc_synthesize_ivar_inconsistent)
4179 << Property->getDeclName()
4180 << ToImpl->getPropertyIvarDecl()->getDeclName()
4181 << Ivar->getDeclName();
4182 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4183 diag::note_odr_objc_synthesize_ivar_here)
4184 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004185 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004186 }
4187
4188 // Merge the existing implementation with the new implementation.
4189 Importer.Imported(D, ToImpl);
4190 }
4191
4192 return ToImpl;
4193}
4194
Douglas Gregora082a492010-11-30 19:14:50 +00004195Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4196 // For template arguments, we adopt the translation unit as our declaration
4197 // context. This context will be fixed when the actual template declaration
4198 // is created.
4199
4200 // FIXME: Import default argument.
4201 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4202 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004203 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004204 Importer.Import(D->getLocation()),
4205 D->getDepth(),
4206 D->getIndex(),
4207 Importer.Import(D->getIdentifier()),
4208 D->wasDeclaredWithTypename(),
4209 D->isParameterPack());
4210}
4211
4212Decl *
4213ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4214 // Import the name of this declaration.
4215 DeclarationName Name = Importer.Import(D->getDeclName());
4216 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004217 return nullptr;
4218
Douglas Gregora082a492010-11-30 19:14:50 +00004219 // Import the location of this declaration.
4220 SourceLocation Loc = Importer.Import(D->getLocation());
4221
4222 // Import the type of this declaration.
4223 QualType T = Importer.Import(D->getType());
4224 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004225 return nullptr;
4226
Douglas Gregora082a492010-11-30 19:14:50 +00004227 // Import type-source information.
4228 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4229 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004230 return nullptr;
4231
Douglas Gregora082a492010-11-30 19:14:50 +00004232 // FIXME: Import default argument.
4233
4234 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4235 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004236 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004237 Loc, D->getDepth(), D->getPosition(),
4238 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004239 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004240}
4241
4242Decl *
4243ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4244 // Import the name of this declaration.
4245 DeclarationName Name = Importer.Import(D->getDeclName());
4246 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004247 return nullptr;
4248
Douglas Gregora082a492010-11-30 19:14:50 +00004249 // Import the location of this declaration.
4250 SourceLocation Loc = Importer.Import(D->getLocation());
4251
4252 // Import template parameters.
4253 TemplateParameterList *TemplateParams
4254 = ImportTemplateParameterList(D->getTemplateParameters());
4255 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004256 return nullptr;
4257
Douglas Gregora082a492010-11-30 19:14:50 +00004258 // FIXME: Import default argument.
4259
4260 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4261 Importer.getToContext().getTranslationUnitDecl(),
4262 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004263 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004264 Name.getAsIdentifierInfo(),
4265 TemplateParams);
4266}
4267
4268Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4269 // If this record has a definition in the translation unit we're coming from,
4270 // but this particular declaration is not that definition, import the
4271 // definition and map to that.
4272 CXXRecordDecl *Definition
4273 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4274 if (Definition && Definition != D->getTemplatedDecl()) {
4275 Decl *ImportedDef
4276 = Importer.Import(Definition->getDescribedClassTemplate());
4277 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004278 return nullptr;
4279
Douglas Gregora082a492010-11-30 19:14:50 +00004280 return Importer.Imported(D, ImportedDef);
4281 }
4282
4283 // Import the major distinguishing characteristics of this class template.
4284 DeclContext *DC, *LexicalDC;
4285 DeclarationName Name;
4286 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004287 NamedDecl *ToD;
4288 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004289 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004290 if (ToD)
4291 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004292
Douglas Gregora082a492010-11-30 19:14:50 +00004293 // We may already have a template of the same name; try to find and match it.
4294 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004295 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004296 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004297 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004298 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4299 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004300 continue;
4301
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004302 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004303 if (ClassTemplateDecl *FoundTemplate
4304 = dyn_cast<ClassTemplateDecl>(Found)) {
4305 if (IsStructuralMatch(D, FoundTemplate)) {
4306 // The class templates structurally match; call it the same template.
4307 // FIXME: We may be filling in a forward declaration here. Handle
4308 // this case!
4309 Importer.Imported(D->getTemplatedDecl(),
4310 FoundTemplate->getTemplatedDecl());
4311 return Importer.Imported(D, FoundTemplate);
4312 }
4313 }
4314
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004315 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004316 }
4317
4318 if (!ConflictingDecls.empty()) {
4319 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4320 ConflictingDecls.data(),
4321 ConflictingDecls.size());
4322 }
4323
4324 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004325 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004326 }
4327
4328 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4329
4330 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004331 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4332 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004333 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4334 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004335 DC, StartLoc, IdLoc,
4336 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004337 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004338 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004339 D2Templated->setLexicalDeclContext(LexicalDC);
4340
4341 // Create the class template declaration itself.
4342 TemplateParameterList *TemplateParams
4343 = ImportTemplateParameterList(D->getTemplateParameters());
4344 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004345 return nullptr;
4346
Douglas Gregora082a492010-11-30 19:14:50 +00004347 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4348 Loc, Name, TemplateParams,
4349 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004350 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004351 D2Templated->setDescribedClassTemplate(D2);
4352
4353 D2->setAccess(D->getAccess());
4354 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004355 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004356
4357 // Note the relationship between the class templates.
4358 Importer.Imported(D, D2);
4359 Importer.Imported(DTemplated, D2Templated);
4360
John McCallf937c022011-10-07 06:10:15 +00004361 if (DTemplated->isCompleteDefinition() &&
4362 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004363 // FIXME: Import definition!
4364 }
4365
4366 return D2;
4367}
4368
Douglas Gregore2e50d332010-12-01 01:36:18 +00004369Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4370 ClassTemplateSpecializationDecl *D) {
4371 // If this record has a definition in the translation unit we're coming from,
4372 // but this particular declaration is not that definition, import the
4373 // definition and map to that.
4374 TagDecl *Definition = D->getDefinition();
4375 if (Definition && Definition != D) {
4376 Decl *ImportedDef = Importer.Import(Definition);
4377 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004378 return nullptr;
4379
Douglas Gregore2e50d332010-12-01 01:36:18 +00004380 return Importer.Imported(D, ImportedDef);
4381 }
4382
4383 ClassTemplateDecl *ClassTemplate
4384 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4385 D->getSpecializedTemplate()));
4386 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004387 return nullptr;
4388
Douglas Gregore2e50d332010-12-01 01:36:18 +00004389 // Import the context of this declaration.
4390 DeclContext *DC = ClassTemplate->getDeclContext();
4391 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004392 return nullptr;
4393
Douglas Gregore2e50d332010-12-01 01:36:18 +00004394 DeclContext *LexicalDC = DC;
4395 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4396 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4397 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004398 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004399 }
4400
4401 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004402 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4403 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004404
4405 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004406 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004407 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4408 D->getTemplateArgs().size(),
4409 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004410 return nullptr;
4411
Douglas Gregore2e50d332010-12-01 01:36:18 +00004412 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004413 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004414 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004415 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004416 if (D2) {
4417 // We already have a class template specialization with these template
4418 // arguments.
4419
4420 // FIXME: Check for specialization vs. instantiation errors.
4421
4422 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004423 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004424 // The record types structurally match, or the "from" translation
4425 // unit only had a forward declaration anyway; call it the same
4426 // function.
4427 return Importer.Imported(D, FoundDef);
4428 }
4429 }
4430 } else {
4431 // Create a new specialization.
4432 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4433 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004434 StartLoc, IdLoc,
4435 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004436 TemplateArgs.data(),
4437 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004438 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004439 D2->setSpecializationKind(D->getSpecializationKind());
4440
4441 // Add this specialization to the class template.
4442 ClassTemplate->AddSpecialization(D2, InsertPos);
4443
4444 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004445 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004446
4447 // Add the specialization to this context.
4448 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004449 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004450 }
4451 Importer.Imported(D, D2);
4452
John McCallf937c022011-10-07 06:10:15 +00004453 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004454 return nullptr;
4455
Douglas Gregore2e50d332010-12-01 01:36:18 +00004456 return D2;
4457}
4458
Larisse Voufo39a1e502013-08-06 01:03:05 +00004459Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4460 // If this variable has a definition in the translation unit we're coming
4461 // from,
4462 // but this particular declaration is not that definition, import the
4463 // definition and map to that.
4464 VarDecl *Definition =
4465 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4466 if (Definition && Definition != D->getTemplatedDecl()) {
4467 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4468 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004469 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004470
4471 return Importer.Imported(D, ImportedDef);
4472 }
4473
4474 // Import the major distinguishing characteristics of this variable template.
4475 DeclContext *DC, *LexicalDC;
4476 DeclarationName Name;
4477 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004478 NamedDecl *ToD;
4479 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004480 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004481 if (ToD)
4482 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004483
4484 // We may already have a template of the same name; try to find and match it.
4485 assert(!DC->isFunctionOrMethod() &&
4486 "Variable templates cannot be declared at function scope");
4487 SmallVector<NamedDecl *, 4> ConflictingDecls;
4488 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004489 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004490 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4491 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4492 continue;
4493
4494 Decl *Found = FoundDecls[I];
4495 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4496 if (IsStructuralMatch(D, FoundTemplate)) {
4497 // The variable templates structurally match; call it the same template.
4498 Importer.Imported(D->getTemplatedDecl(),
4499 FoundTemplate->getTemplatedDecl());
4500 return Importer.Imported(D, FoundTemplate);
4501 }
4502 }
4503
4504 ConflictingDecls.push_back(FoundDecls[I]);
4505 }
4506
4507 if (!ConflictingDecls.empty()) {
4508 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4509 ConflictingDecls.data(),
4510 ConflictingDecls.size());
4511 }
4512
4513 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004514 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004515
4516 VarDecl *DTemplated = D->getTemplatedDecl();
4517
4518 // Import the type.
4519 QualType T = Importer.Import(DTemplated->getType());
4520 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004521 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004522
4523 // Create the declaration that is being templated.
4524 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4525 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4526 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4527 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4528 IdLoc, Name.getAsIdentifierInfo(), T,
4529 TInfo, DTemplated->getStorageClass());
4530 D2Templated->setAccess(DTemplated->getAccess());
4531 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4532 D2Templated->setLexicalDeclContext(LexicalDC);
4533
4534 // Importer.Imported(DTemplated, D2Templated);
4535 // LexicalDC->addDeclInternal(D2Templated);
4536
4537 // Merge the initializer.
4538 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004539 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004540
4541 // Create the variable template declaration itself.
4542 TemplateParameterList *TemplateParams =
4543 ImportTemplateParameterList(D->getTemplateParameters());
4544 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004545 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004546
4547 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004548 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004549 D2Templated->setDescribedVarTemplate(D2);
4550
4551 D2->setAccess(D->getAccess());
4552 D2->setLexicalDeclContext(LexicalDC);
4553 LexicalDC->addDeclInternal(D2);
4554
4555 // Note the relationship between the variable templates.
4556 Importer.Imported(D, D2);
4557 Importer.Imported(DTemplated, D2Templated);
4558
4559 if (DTemplated->isThisDeclarationADefinition() &&
4560 !D2Templated->isThisDeclarationADefinition()) {
4561 // FIXME: Import definition!
4562 }
4563
4564 return D2;
4565}
4566
4567Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4568 VarTemplateSpecializationDecl *D) {
4569 // If this record has a definition in the translation unit we're coming from,
4570 // but this particular declaration is not that definition, import the
4571 // definition and map to that.
4572 VarDecl *Definition = D->getDefinition();
4573 if (Definition && Definition != D) {
4574 Decl *ImportedDef = Importer.Import(Definition);
4575 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004576 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004577
4578 return Importer.Imported(D, ImportedDef);
4579 }
4580
4581 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4582 Importer.Import(D->getSpecializedTemplate()));
4583 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004584 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004585
4586 // Import the context of this declaration.
4587 DeclContext *DC = VarTemplate->getDeclContext();
4588 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004589 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004590
4591 DeclContext *LexicalDC = DC;
4592 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4593 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4594 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004595 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004596 }
4597
4598 // Import the location of this declaration.
4599 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4600 SourceLocation IdLoc = Importer.Import(D->getLocation());
4601
4602 // Import template arguments.
4603 SmallVector<TemplateArgument, 2> TemplateArgs;
4604 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4605 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004606 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004607
4608 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004609 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004610 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004611 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004612 if (D2) {
4613 // We already have a variable template specialization with these template
4614 // arguments.
4615
4616 // FIXME: Check for specialization vs. instantiation errors.
4617
4618 if (VarDecl *FoundDef = D2->getDefinition()) {
4619 if (!D->isThisDeclarationADefinition() ||
4620 IsStructuralMatch(D, FoundDef)) {
4621 // The record types structurally match, or the "from" translation
4622 // unit only had a forward declaration anyway; call it the same
4623 // variable.
4624 return Importer.Imported(D, FoundDef);
4625 }
4626 }
4627 } else {
4628
4629 // Import the type.
4630 QualType T = Importer.Import(D->getType());
4631 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004632 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004633 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4634
4635 // Create a new specialization.
4636 D2 = VarTemplateSpecializationDecl::Create(
4637 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4638 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4639 D2->setSpecializationKind(D->getSpecializationKind());
4640 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4641
4642 // Add this specialization to the class template.
4643 VarTemplate->AddSpecialization(D2, InsertPos);
4644
4645 // Import the qualifier, if any.
4646 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4647
4648 // Add the specialization to this context.
4649 D2->setLexicalDeclContext(LexicalDC);
4650 LexicalDC->addDeclInternal(D2);
4651 }
4652 Importer.Imported(D, D2);
4653
4654 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004655 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004656
4657 return D2;
4658}
4659
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004660//----------------------------------------------------------------------------
4661// Import Statements
4662//----------------------------------------------------------------------------
4663
Sean Callanan59721b32015-04-28 18:41:46 +00004664DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4665 if (DG.isNull())
4666 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4667 size_t NumDecls = DG.end() - DG.begin();
4668 SmallVector<Decl *, 1> ToDecls(NumDecls);
4669 auto &_Importer = this->Importer;
4670 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4671 [&_Importer](Decl *D) -> Decl * {
4672 return _Importer.Import(D);
4673 });
4674 return DeclGroupRef::Create(Importer.getToContext(),
4675 ToDecls.begin(),
4676 NumDecls);
4677}
4678
4679 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4680 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4681 << S->getStmtClassName();
4682 return nullptr;
4683 }
4684
4685Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4686 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4687 for (Decl *ToD : ToDG) {
4688 if (!ToD)
4689 return nullptr;
4690 }
4691 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4692 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4693 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4694}
4695
4696Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4697 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4698 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4699 S->hasLeadingEmptyMacro());
4700}
4701
4702Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Sean Callanan8bca9962016-03-28 21:43:01 +00004703 llvm::ArrayRef<Stmt *> ToStmts;
4704
4705 if (!ImportArray(S->body_begin(), S->body_end(), ToStmts))
4706 return nullptr;
4707
Sean Callanan59721b32015-04-28 18:41:46 +00004708 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4709 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4710 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4711 ToStmts,
4712 ToLBraceLoc, ToRBraceLoc);
4713}
4714
4715Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4716 Expr *ToLHS = Importer.Import(S->getLHS());
4717 if (!ToLHS)
4718 return nullptr;
4719 Expr *ToRHS = Importer.Import(S->getRHS());
4720 if (!ToRHS && S->getRHS())
4721 return nullptr;
4722 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4723 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4724 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4725 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4726 ToCaseLoc, ToEllipsisLoc,
4727 ToColonLoc);
4728}
4729
4730Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4731 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4732 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4733 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4734 if (!ToSubStmt && S->getSubStmt())
4735 return nullptr;
4736 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4737 ToSubStmt);
4738}
4739
4740Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4741 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4742 LabelDecl *ToLabelDecl =
4743 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4744 if (!ToLabelDecl && S->getDecl())
4745 return nullptr;
4746 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4747 if (!ToSubStmt && S->getSubStmt())
4748 return nullptr;
4749 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4750 ToSubStmt);
4751}
4752
4753Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4754 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4755 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4756 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4757 ASTContext &_ToContext = Importer.getToContext();
4758 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4759 [&_ToContext](const Attr *A) -> const Attr * {
4760 return A->clone(_ToContext);
4761 });
4762 for (const Attr *ToA : ToAttrs) {
4763 if (!ToA)
4764 return nullptr;
4765 }
4766 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4767 if (!ToSubStmt && S->getSubStmt())
4768 return nullptr;
4769 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4770 ToAttrs, ToSubStmt);
4771}
4772
4773Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4774 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4775 VarDecl *ToConditionVariable = nullptr;
4776 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4777 ToConditionVariable =
4778 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4779 if (!ToConditionVariable)
4780 return nullptr;
4781 }
4782 Expr *ToCondition = Importer.Import(S->getCond());
4783 if (!ToCondition && S->getCond())
4784 return nullptr;
4785 Stmt *ToThenStmt = Importer.Import(S->getThen());
4786 if (!ToThenStmt && S->getThen())
4787 return nullptr;
4788 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4789 Stmt *ToElseStmt = Importer.Import(S->getElse());
4790 if (!ToElseStmt && S->getElse())
4791 return nullptr;
4792 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4793 ToIfLoc, ToConditionVariable,
4794 ToCondition, ToThenStmt,
4795 ToElseLoc, ToElseStmt);
4796}
4797
4798Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4799 VarDecl *ToConditionVariable = nullptr;
4800 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4801 ToConditionVariable =
4802 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4803 if (!ToConditionVariable)
4804 return nullptr;
4805 }
4806 Expr *ToCondition = Importer.Import(S->getCond());
4807 if (!ToCondition && S->getCond())
4808 return nullptr;
4809 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4810 Importer.getToContext(), ToConditionVariable,
4811 ToCondition);
4812 Stmt *ToBody = Importer.Import(S->getBody());
4813 if (!ToBody && S->getBody())
4814 return nullptr;
4815 ToStmt->setBody(ToBody);
4816 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4817 // Now we have to re-chain the cases.
4818 SwitchCase *LastChainedSwitchCase = nullptr;
4819 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4820 SC = SC->getNextSwitchCase()) {
4821 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4822 if (!ToSC)
4823 return nullptr;
4824 if (LastChainedSwitchCase)
4825 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4826 else
4827 ToStmt->setSwitchCaseList(ToSC);
4828 LastChainedSwitchCase = ToSC;
4829 }
4830 return ToStmt;
4831}
4832
4833Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4834 VarDecl *ToConditionVariable = nullptr;
4835 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4836 ToConditionVariable =
4837 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4838 if (!ToConditionVariable)
4839 return nullptr;
4840 }
4841 Expr *ToCondition = Importer.Import(S->getCond());
4842 if (!ToCondition && S->getCond())
4843 return nullptr;
4844 Stmt *ToBody = Importer.Import(S->getBody());
4845 if (!ToBody && S->getBody())
4846 return nullptr;
4847 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4848 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4849 ToConditionVariable,
4850 ToCondition, ToBody,
4851 ToWhileLoc);
4852}
4853
4854Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4855 Stmt *ToBody = Importer.Import(S->getBody());
4856 if (!ToBody && S->getBody())
4857 return nullptr;
4858 Expr *ToCondition = Importer.Import(S->getCond());
4859 if (!ToCondition && S->getCond())
4860 return nullptr;
4861 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4862 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4863 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4864 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4865 ToDoLoc, ToWhileLoc,
4866 ToRParenLoc);
4867}
4868
4869Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4870 Stmt *ToInit = Importer.Import(S->getInit());
4871 if (!ToInit && S->getInit())
4872 return nullptr;
4873 Expr *ToCondition = Importer.Import(S->getCond());
4874 if (!ToCondition && S->getCond())
4875 return nullptr;
4876 VarDecl *ToConditionVariable = nullptr;
4877 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4878 ToConditionVariable =
4879 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4880 if (!ToConditionVariable)
4881 return nullptr;
4882 }
4883 Expr *ToInc = Importer.Import(S->getInc());
4884 if (!ToInc && S->getInc())
4885 return nullptr;
4886 Stmt *ToBody = Importer.Import(S->getBody());
4887 if (!ToBody && S->getBody())
4888 return nullptr;
4889 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4890 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4891 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4892 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4893 ToInit, ToCondition,
4894 ToConditionVariable,
4895 ToInc, ToBody,
4896 ToForLoc, ToLParenLoc,
4897 ToRParenLoc);
4898}
4899
4900Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4901 LabelDecl *ToLabel = nullptr;
4902 if (LabelDecl *FromLabel = S->getLabel()) {
4903 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4904 if (!ToLabel)
4905 return nullptr;
4906 }
4907 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4908 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4909 return new (Importer.getToContext()) GotoStmt(ToLabel,
4910 ToGotoLoc, ToLabelLoc);
4911}
4912
4913Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4914 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4915 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4916 Expr *ToTarget = Importer.Import(S->getTarget());
4917 if (!ToTarget && S->getTarget())
4918 return nullptr;
4919 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4920 ToTarget);
4921}
4922
4923Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4924 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4925 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4926}
4927
4928Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4929 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4930 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4931}
4932
4933Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4934 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4935 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4936 if (!ToRetExpr && S->getRetValue())
4937 return nullptr;
4938 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4939 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4940 if (!ToNRVOCandidate && NRVOCandidate)
4941 return nullptr;
4942 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4943 ToNRVOCandidate);
4944}
4945
4946Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4947 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4948 VarDecl *ToExceptionDecl = nullptr;
4949 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4950 ToExceptionDecl =
4951 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4952 if (!ToExceptionDecl)
4953 return nullptr;
4954 }
4955 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4956 if (!ToHandlerBlock && S->getHandlerBlock())
4957 return nullptr;
4958 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4959 ToExceptionDecl,
4960 ToHandlerBlock);
4961}
4962
4963Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4964 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4965 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4966 if (!ToTryBlock && S->getTryBlock())
4967 return nullptr;
4968 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4969 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4970 CXXCatchStmt *FromHandler = S->getHandler(HI);
4971 if (Stmt *ToHandler = Importer.Import(FromHandler))
4972 ToHandlers[HI] = ToHandler;
4973 else
4974 return nullptr;
4975 }
4976 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4977 ToHandlers);
4978}
4979
4980Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4981 DeclStmt *ToRange =
4982 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4983 if (!ToRange && S->getRangeStmt())
4984 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00004985 DeclStmt *ToBegin =
4986 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
4987 if (!ToBegin && S->getBeginStmt())
4988 return nullptr;
4989 DeclStmt *ToEnd =
4990 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
4991 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00004992 return nullptr;
4993 Expr *ToCond = Importer.Import(S->getCond());
4994 if (!ToCond && S->getCond())
4995 return nullptr;
4996 Expr *ToInc = Importer.Import(S->getInc());
4997 if (!ToInc && S->getInc())
4998 return nullptr;
4999 DeclStmt *ToLoopVar =
5000 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5001 if (!ToLoopVar && S->getLoopVarStmt())
5002 return nullptr;
5003 Stmt *ToBody = Importer.Import(S->getBody());
5004 if (!ToBody && S->getBody())
5005 return nullptr;
5006 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005007 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005008 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5009 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005010 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005011 ToCond, ToInc,
5012 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005013 ToForLoc, ToCoawaitLoc,
5014 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005015}
5016
5017Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5018 Stmt *ToElem = Importer.Import(S->getElement());
5019 if (!ToElem && S->getElement())
5020 return nullptr;
5021 Expr *ToCollect = Importer.Import(S->getCollection());
5022 if (!ToCollect && S->getCollection())
5023 return nullptr;
5024 Stmt *ToBody = Importer.Import(S->getBody());
5025 if (!ToBody && S->getBody())
5026 return nullptr;
5027 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5028 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5029 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5030 ToCollect,
5031 ToBody, ToForLoc,
5032 ToRParenLoc);
5033}
5034
5035Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5036 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5037 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5038 VarDecl *ToExceptionDecl = nullptr;
5039 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5040 ToExceptionDecl =
5041 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5042 if (!ToExceptionDecl)
5043 return nullptr;
5044 }
5045 Stmt *ToBody = Importer.Import(S->getCatchBody());
5046 if (!ToBody && S->getCatchBody())
5047 return nullptr;
5048 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5049 ToRParenLoc,
5050 ToExceptionDecl,
5051 ToBody);
5052}
5053
5054Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5055 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5056 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5057 if (!ToAtFinallyStmt && S->getFinallyBody())
5058 return nullptr;
5059 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5060 ToAtFinallyStmt);
5061}
5062
5063Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5064 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5065 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5066 if (!ToAtTryStmt && S->getTryBody())
5067 return nullptr;
5068 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5069 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5070 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5071 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5072 ToCatchStmts[CI] = ToCatchStmt;
5073 else
5074 return nullptr;
5075 }
5076 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5077 if (!ToAtFinallyStmt && S->getFinallyStmt())
5078 return nullptr;
5079 return ObjCAtTryStmt::Create(Importer.getToContext(),
5080 ToAtTryLoc, ToAtTryStmt,
5081 ToCatchStmts.begin(), ToCatchStmts.size(),
5082 ToAtFinallyStmt);
5083}
5084
5085Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5086 (ObjCAtSynchronizedStmt *S) {
5087 SourceLocation ToAtSynchronizedLoc =
5088 Importer.Import(S->getAtSynchronizedLoc());
5089 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5090 if (!ToSynchExpr && S->getSynchExpr())
5091 return nullptr;
5092 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5093 if (!ToSynchBody && S->getSynchBody())
5094 return nullptr;
5095 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5096 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5097}
5098
5099Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5100 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5101 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5102 if (!ToThrow && S->getThrowExpr())
5103 return nullptr;
5104 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5105}
5106
5107Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5108 (ObjCAutoreleasePoolStmt *S) {
5109 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5110 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5111 if (!ToSubStmt && S->getSubStmt())
5112 return nullptr;
5113 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5114 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005115}
5116
5117//----------------------------------------------------------------------------
5118// Import Expressions
5119//----------------------------------------------------------------------------
5120Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5121 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5122 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005123 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005124}
5125
Douglas Gregor52f820e2010-02-19 01:17:02 +00005126Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005127 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5128 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005129 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005130
Craig Topper36250ad2014-05-12 05:36:57 +00005131 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005132 if (E->getDecl() != E->getFoundDecl()) {
5133 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5134 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005135 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005136 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005137
5138 QualType T = Importer.Import(E->getType());
5139 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005140 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005141
5142 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5143 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005144 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005145 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005146 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005147 Importer.Import(E->getLocation()),
5148 T, E->getValueKind(),
5149 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005150 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005151 if (E->hadMultipleCandidates())
5152 DRE->setHadMultipleCandidates(true);
5153 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005154}
5155
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005156Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5157 QualType T = Importer.Import(E->getType());
5158 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005159 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005160
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005161 return IntegerLiteral::Create(Importer.getToContext(),
5162 E->getValue(), T,
5163 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005164}
5165
Douglas Gregor623421d2010-02-18 02:21:22 +00005166Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5167 QualType T = Importer.Import(E->getType());
5168 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005169 return nullptr;
5170
Douglas Gregorfb65e592011-07-27 05:40:30 +00005171 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5172 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005173 Importer.Import(E->getLocation()));
5174}
5175
Douglas Gregorc74247e2010-02-19 01:07:06 +00005176Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5177 Expr *SubExpr = Importer.Import(E->getSubExpr());
5178 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005179 return nullptr;
5180
Douglas Gregorc74247e2010-02-19 01:07:06 +00005181 return new (Importer.getToContext())
5182 ParenExpr(Importer.Import(E->getLParen()),
5183 Importer.Import(E->getRParen()),
5184 SubExpr);
5185}
5186
5187Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5188 QualType T = Importer.Import(E->getType());
5189 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005190 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005191
5192 Expr *SubExpr = Importer.Import(E->getSubExpr());
5193 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005194 return nullptr;
5195
Douglas Gregorc74247e2010-02-19 01:07:06 +00005196 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005197 T, E->getValueKind(),
5198 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005199 Importer.Import(E->getOperatorLoc()));
5200}
5201
Peter Collingbournee190dee2011-03-11 19:24:49 +00005202Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5203 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005204 QualType ResultType = Importer.Import(E->getType());
5205
5206 if (E->isArgumentType()) {
5207 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5208 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005209 return nullptr;
5210
Peter Collingbournee190dee2011-03-11 19:24:49 +00005211 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5212 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005213 Importer.Import(E->getOperatorLoc()),
5214 Importer.Import(E->getRParenLoc()));
5215 }
5216
5217 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5218 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005219 return nullptr;
5220
Peter Collingbournee190dee2011-03-11 19:24:49 +00005221 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5222 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005223 Importer.Import(E->getOperatorLoc()),
5224 Importer.Import(E->getRParenLoc()));
5225}
5226
Douglas Gregorc74247e2010-02-19 01:07:06 +00005227Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5228 QualType T = Importer.Import(E->getType());
5229 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005230 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005231
5232 Expr *LHS = Importer.Import(E->getLHS());
5233 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005234 return nullptr;
5235
Douglas Gregorc74247e2010-02-19 01:07:06 +00005236 Expr *RHS = Importer.Import(E->getRHS());
5237 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005238 return nullptr;
5239
Douglas Gregorc74247e2010-02-19 01:07:06 +00005240 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005241 T, E->getValueKind(),
5242 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005243 Importer.Import(E->getOperatorLoc()),
5244 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005245}
5246
5247Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5248 QualType T = Importer.Import(E->getType());
5249 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005250 return nullptr;
5251
Douglas Gregorc74247e2010-02-19 01:07:06 +00005252 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5253 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005254 return nullptr;
5255
Douglas Gregorc74247e2010-02-19 01:07:06 +00005256 QualType CompResultType = Importer.Import(E->getComputationResultType());
5257 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005258 return nullptr;
5259
Douglas Gregorc74247e2010-02-19 01:07:06 +00005260 Expr *LHS = Importer.Import(E->getLHS());
5261 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005262 return nullptr;
5263
Douglas Gregorc74247e2010-02-19 01:07:06 +00005264 Expr *RHS = Importer.Import(E->getRHS());
5265 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005266 return nullptr;
5267
Douglas Gregorc74247e2010-02-19 01:07:06 +00005268 return new (Importer.getToContext())
5269 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005270 T, E->getValueKind(),
5271 E->getObjectKind(),
5272 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005273 Importer.Import(E->getOperatorLoc()),
5274 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005275}
5276
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005277static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005278 if (E->path_empty()) return false;
5279
5280 // TODO: import cast paths
5281 return true;
5282}
5283
Douglas Gregor98c10182010-02-12 22:17:39 +00005284Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5285 QualType T = Importer.Import(E->getType());
5286 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005287 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005288
5289 Expr *SubExpr = Importer.Import(E->getSubExpr());
5290 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005291 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005292
5293 CXXCastPath BasePath;
5294 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005295 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005296
5297 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005298 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005299}
5300
Douglas Gregor5481d322010-02-19 01:32:14 +00005301Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5302 QualType T = Importer.Import(E->getType());
5303 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005304 return nullptr;
5305
Douglas Gregor5481d322010-02-19 01:32:14 +00005306 Expr *SubExpr = Importer.Import(E->getSubExpr());
5307 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005308 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005309
5310 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5311 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005312 return nullptr;
5313
John McCallcf142162010-08-07 06:22:56 +00005314 CXXCastPath BasePath;
5315 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005316 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005317
John McCall7decc9e2010-11-18 06:31:45 +00005318 return CStyleCastExpr::Create(Importer.getToContext(), T,
5319 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005320 SubExpr, &BasePath, TInfo,
5321 Importer.Import(E->getLParenLoc()),
5322 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005323}
5324
Sean Callanan59721b32015-04-28 18:41:46 +00005325Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5326 QualType T = Importer.Import(E->getType());
5327 if (T.isNull())
5328 return nullptr;
5329
5330 CXXConstructorDecl *ToCCD =
5331 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5332 if (!ToCCD && E->getConstructor())
5333 return nullptr;
5334
Sean Callanan8bca9962016-03-28 21:43:01 +00005335 ArrayRef<Expr *> ToArgs;
5336
5337 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs))
5338 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005339
5340 return CXXConstructExpr::Create(Importer.getToContext(), T,
5341 Importer.Import(E->getLocation()),
5342 ToCCD, E->isElidable(),
5343 ToArgs, E->hadMultipleCandidates(),
5344 E->isListInitialization(),
5345 E->isStdInitListInitialization(),
5346 E->requiresZeroInitialization(),
5347 E->getConstructionKind(),
5348 Importer.Import(E->getParenOrBraceRange()));
5349}
5350
Sean Callanan8bca9962016-03-28 21:43:01 +00005351Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5352 QualType T = Importer.Import(E->getType());
5353 if (T.isNull())
5354 return nullptr;
5355
5356 Expr *ToFn = Importer.Import(E->getCallee());
5357 if (!ToFn)
5358 return nullptr;
5359
5360 ArrayRef<Expr *> ToArgs;
5361
5362 if (!ImportArray(E->arg_begin(), E->arg_end(), ToArgs))
5363 return nullptr;
5364
5365 return new (Importer.getToContext()) CXXMemberCallExpr(Importer.getToContext(), ToFn,
5366 ToArgs, T, E->getValueKind(),
5367 Importer.Import(E->getRParenLoc()));
5368}
5369
5370Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5371 QualType T = Importer.Import(E->getType());
5372 if (T.isNull())
5373 return nullptr;
5374
5375 return new (Importer.getToContext())
5376 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5377}
5378
5379Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5380 QualType T = Importer.Import(E->getType());
5381 if (T.isNull())
5382 return nullptr;
5383
5384 return new (Importer.getToContext())
5385 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5386}
5387
5388
Sean Callanan59721b32015-04-28 18:41:46 +00005389Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5390 QualType T = Importer.Import(E->getType());
5391 if (T.isNull())
5392 return nullptr;
5393
5394 Expr *ToBase = Importer.Import(E->getBase());
5395 if (!ToBase && E->getBase())
5396 return nullptr;
5397
5398 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5399 if (!ToMember && E->getMemberDecl())
5400 return nullptr;
5401
5402 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5403 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5404 E->getFoundDecl().getAccess());
5405
5406 DeclarationNameInfo ToMemberNameInfo(
5407 Importer.Import(E->getMemberNameInfo().getName()),
5408 Importer.Import(E->getMemberNameInfo().getLoc()));
5409
5410 if (E->hasExplicitTemplateArgs()) {
5411 return nullptr; // FIXME: handle template arguments
5412 }
5413
5414 return MemberExpr::Create(Importer.getToContext(), ToBase,
5415 E->isArrow(),
5416 Importer.Import(E->getOperatorLoc()),
5417 Importer.Import(E->getQualifierLoc()),
5418 Importer.Import(E->getTemplateKeywordLoc()),
5419 ToMember, ToFoundDecl, ToMemberNameInfo,
5420 nullptr, T, E->getValueKind(),
5421 E->getObjectKind());
5422}
5423
5424Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5425 QualType T = Importer.Import(E->getType());
5426 if (T.isNull())
5427 return nullptr;
5428
5429 Expr *ToCallee = Importer.Import(E->getCallee());
5430 if (!ToCallee && E->getCallee())
5431 return nullptr;
5432
5433 unsigned NumArgs = E->getNumArgs();
5434
5435 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5436
5437 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5438 Expr *FromArg = E->getArg(ai);
5439 Expr *ToArg = Importer.Import(FromArg);
5440 if (!ToArg)
5441 return nullptr;
5442 ToArgs[ai] = ToArg;
5443 }
5444
5445 Expr **ToArgs_Copied = new (Importer.getToContext())
5446 Expr*[NumArgs];
5447
5448 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5449 ToArgs_Copied[ai] = ToArgs[ai];
5450
5451 return new (Importer.getToContext())
5452 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005453 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005454 Importer.Import(E->getRParenLoc()));
5455}
5456
Sean Callanan8bca9962016-03-28 21:43:01 +00005457Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *E) {
5458 QualType T = Importer.Import(E->getType());
5459 if (T.isNull())
5460 return nullptr;
5461
5462 ArrayRef<Expr *> ToInits;
5463
5464 if (!ImportArray(E->inits().begin(), E->inits().end(), ToInits))
5465 return nullptr;
5466
5467 InitListExpr *ToE = new (Importer.getToContext())
5468 InitListExpr(Importer.getToContext(),
5469 Importer.Import(E->getLBraceLoc()),
5470 ToInits,
5471 Importer.Import(E->getRBraceLoc()));
5472
5473 if (ToE)
5474 ToE->setType(T);
5475
5476 return ToE;
5477}
5478
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005479ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005480 ASTContext &FromContext, FileManager &FromFileManager,
5481 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005482 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005483 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005484 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005485{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005486 ImportedDecls[FromContext.getTranslationUnitDecl()]
5487 = ToContext.getTranslationUnitDecl();
5488}
5489
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005490ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005491
5492QualType ASTImporter::Import(QualType FromT) {
5493 if (FromT.isNull())
5494 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005495
5496 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005497
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005498 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005499 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5500 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005501 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005502 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005503
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005504 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005505 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005506 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005507 if (ToT.isNull())
5508 return ToT;
5509
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005510 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005511 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005512
John McCall424cec92011-01-19 06:33:43 +00005513 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005514}
5515
Douglas Gregor62d311f2010-02-09 19:21:46 +00005516TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005517 if (!FromTSI)
5518 return FromTSI;
5519
5520 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005521 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005522 QualType T = Import(FromTSI->getType());
5523 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005524 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005525
5526 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005527 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005528}
5529
Sean Callanan59721b32015-04-28 18:41:46 +00005530Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5531 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5532 if (Pos != ImportedDecls.end()) {
5533 Decl *ToD = Pos->second;
5534 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5535 return ToD;
5536 } else {
5537 return nullptr;
5538 }
5539}
5540
Douglas Gregor62d311f2010-02-09 19:21:46 +00005541Decl *ASTImporter::Import(Decl *FromD) {
5542 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005543 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005544
Douglas Gregord451ea92011-07-29 23:31:30 +00005545 ASTNodeImporter Importer(*this);
5546
Douglas Gregor62d311f2010-02-09 19:21:46 +00005547 // Check whether we've already imported this declaration.
5548 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005549 if (Pos != ImportedDecls.end()) {
5550 Decl *ToD = Pos->second;
5551 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5552 return ToD;
5553 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005554
5555 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005556 Decl *ToD = Importer.Visit(FromD);
5557 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005558 return nullptr;
5559
Douglas Gregor62d311f2010-02-09 19:21:46 +00005560 // Record the imported declaration.
5561 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005562
5563 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5564 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005565 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005566 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005567 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005568 // When we've finished transforming a typedef, see whether it was the
5569 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005570 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005571 FromTag = AnonTagsWithPendingTypedefs.begin(),
5572 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5573 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005574 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005575 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5576 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005577 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005578 AnonTagsWithPendingTypedefs.erase(FromTag);
5579 break;
5580 }
5581 }
5582 }
5583 }
5584
Douglas Gregor62d311f2010-02-09 19:21:46 +00005585 return ToD;
5586}
5587
5588DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5589 if (!FromDC)
5590 return FromDC;
5591
Douglas Gregor95d82832012-01-24 18:36:04 +00005592 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005593 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005594 return nullptr;
5595
Douglas Gregor2e15c842012-02-01 21:00:38 +00005596 // When we're using a record/enum/Objective-C class/protocol as a context, we
5597 // need it to have a definition.
5598 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005599 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005600 if (ToRecord->isCompleteDefinition()) {
5601 // Do nothing.
5602 } else if (FromRecord->isCompleteDefinition()) {
5603 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5604 ASTNodeImporter::IDK_Basic);
5605 } else {
5606 CompleteDecl(ToRecord);
5607 }
5608 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5609 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5610 if (ToEnum->isCompleteDefinition()) {
5611 // Do nothing.
5612 } else if (FromEnum->isCompleteDefinition()) {
5613 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5614 ASTNodeImporter::IDK_Basic);
5615 } else {
5616 CompleteDecl(ToEnum);
5617 }
5618 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5619 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5620 if (ToClass->getDefinition()) {
5621 // Do nothing.
5622 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5623 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5624 ASTNodeImporter::IDK_Basic);
5625 } else {
5626 CompleteDecl(ToClass);
5627 }
5628 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5629 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5630 if (ToProto->getDefinition()) {
5631 // Do nothing.
5632 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5633 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5634 ASTNodeImporter::IDK_Basic);
5635 } else {
5636 CompleteDecl(ToProto);
5637 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005638 }
5639
5640 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005641}
5642
5643Expr *ASTImporter::Import(Expr *FromE) {
5644 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005645 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005646
5647 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5648}
5649
5650Stmt *ASTImporter::Import(Stmt *FromS) {
5651 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005652 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005653
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005654 // Check whether we've already imported this declaration.
5655 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5656 if (Pos != ImportedStmts.end())
5657 return Pos->second;
5658
5659 // Import the type
5660 ASTNodeImporter Importer(*this);
5661 Stmt *ToS = Importer.Visit(FromS);
5662 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005663 return nullptr;
5664
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005665 // Record the imported declaration.
5666 ImportedStmts[FromS] = ToS;
5667 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005668}
5669
5670NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5671 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005672 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005673
Douglas Gregor90ebf252011-04-27 16:48:40 +00005674 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5675
5676 switch (FromNNS->getKind()) {
5677 case NestedNameSpecifier::Identifier:
5678 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5679 return NestedNameSpecifier::Create(ToContext, prefix, II);
5680 }
Craig Topper36250ad2014-05-12 05:36:57 +00005681 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005682
5683 case NestedNameSpecifier::Namespace:
5684 if (NamespaceDecl *NS =
5685 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5686 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5687 }
Craig Topper36250ad2014-05-12 05:36:57 +00005688 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005689
5690 case NestedNameSpecifier::NamespaceAlias:
5691 if (NamespaceAliasDecl *NSAD =
5692 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5693 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5694 }
Craig Topper36250ad2014-05-12 05:36:57 +00005695 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005696
5697 case NestedNameSpecifier::Global:
5698 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5699
Nikola Smiljanic67860242014-09-26 00:28:20 +00005700 case NestedNameSpecifier::Super:
5701 if (CXXRecordDecl *RD =
5702 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5703 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5704 }
5705 return nullptr;
5706
Douglas Gregor90ebf252011-04-27 16:48:40 +00005707 case NestedNameSpecifier::TypeSpec:
5708 case NestedNameSpecifier::TypeSpecWithTemplate: {
5709 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5710 if (!T.isNull()) {
5711 bool bTemplate = FromNNS->getKind() ==
5712 NestedNameSpecifier::TypeSpecWithTemplate;
5713 return NestedNameSpecifier::Create(ToContext, prefix,
5714 bTemplate, T.getTypePtr());
5715 }
5716 }
Craig Topper36250ad2014-05-12 05:36:57 +00005717 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005718 }
5719
5720 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005721}
5722
Douglas Gregor14454802011-02-25 02:25:35 +00005723NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5724 // FIXME: Implement!
5725 return NestedNameSpecifierLoc();
5726}
5727
Douglas Gregore2e50d332010-12-01 01:36:18 +00005728TemplateName ASTImporter::Import(TemplateName From) {
5729 switch (From.getKind()) {
5730 case TemplateName::Template:
5731 if (TemplateDecl *ToTemplate
5732 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5733 return TemplateName(ToTemplate);
5734
5735 return TemplateName();
5736
5737 case TemplateName::OverloadedTemplate: {
5738 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5739 UnresolvedSet<2> ToTemplates;
5740 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5741 E = FromStorage->end();
5742 I != E; ++I) {
5743 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5744 ToTemplates.addDecl(To);
5745 else
5746 return TemplateName();
5747 }
5748 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5749 ToTemplates.end());
5750 }
5751
5752 case TemplateName::QualifiedTemplate: {
5753 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5754 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5755 if (!Qualifier)
5756 return TemplateName();
5757
5758 if (TemplateDecl *ToTemplate
5759 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5760 return ToContext.getQualifiedTemplateName(Qualifier,
5761 QTN->hasTemplateKeyword(),
5762 ToTemplate);
5763
5764 return TemplateName();
5765 }
5766
5767 case TemplateName::DependentTemplate: {
5768 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5769 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5770 if (!Qualifier)
5771 return TemplateName();
5772
5773 if (DTN->isIdentifier()) {
5774 return ToContext.getDependentTemplateName(Qualifier,
5775 Import(DTN->getIdentifier()));
5776 }
5777
5778 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5779 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005780
5781 case TemplateName::SubstTemplateTemplateParm: {
5782 SubstTemplateTemplateParmStorage *subst
5783 = From.getAsSubstTemplateTemplateParm();
5784 TemplateTemplateParmDecl *param
5785 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5786 if (!param)
5787 return TemplateName();
5788
5789 TemplateName replacement = Import(subst->getReplacement());
5790 if (replacement.isNull()) return TemplateName();
5791
5792 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5793 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005794
5795 case TemplateName::SubstTemplateTemplateParmPack: {
5796 SubstTemplateTemplateParmPackStorage *SubstPack
5797 = From.getAsSubstTemplateTemplateParmPack();
5798 TemplateTemplateParmDecl *Param
5799 = cast_or_null<TemplateTemplateParmDecl>(
5800 Import(SubstPack->getParameterPack()));
5801 if (!Param)
5802 return TemplateName();
5803
5804 ASTNodeImporter Importer(*this);
5805 TemplateArgument ArgPack
5806 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5807 if (ArgPack.isNull())
5808 return TemplateName();
5809
5810 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5811 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005812 }
5813
5814 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005815}
5816
Douglas Gregor62d311f2010-02-09 19:21:46 +00005817SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5818 if (FromLoc.isInvalid())
5819 return SourceLocation();
5820
Douglas Gregor811663e2010-02-10 00:15:17 +00005821 SourceManager &FromSM = FromContext.getSourceManager();
5822
5823 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005824 // don't have to import macro expansions.
5825 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005826 FromLoc = FromSM.getSpellingLoc(FromLoc);
5827 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5828 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005829 FileID ToFileID = Import(Decomposed.first);
5830 if (ToFileID.isInvalid())
5831 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005832 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5833 .getLocWithOffset(Decomposed.second);
5834 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005835}
5836
5837SourceRange ASTImporter::Import(SourceRange FromRange) {
5838 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5839}
5840
Douglas Gregor811663e2010-02-10 00:15:17 +00005841FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005842 llvm::DenseMap<FileID, FileID>::iterator Pos
5843 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005844 if (Pos != ImportedFileIDs.end())
5845 return Pos->second;
5846
5847 SourceManager &FromSM = FromContext.getSourceManager();
5848 SourceManager &ToSM = ToContext.getSourceManager();
5849 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005850 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005851
5852 // Include location of this file.
5853 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5854
5855 // Map the FileID for to the "to" source manager.
5856 FileID ToID;
5857 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005858 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005859 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5860 // disk again
5861 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5862 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005863 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005864 if (!Entry)
5865 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005866 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5867 FromSLoc.getFile().getFileCharacteristic());
5868 } else {
5869 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005870 const llvm::MemoryBuffer *
5871 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005872 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005873 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005874 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005875 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005876 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005877 }
5878
5879
Sebastian Redl99219f12010-09-30 01:03:06 +00005880 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005881 return ToID;
5882}
5883
Douglas Gregor0a791672011-01-18 03:11:38 +00005884void ASTImporter::ImportDefinition(Decl *From) {
5885 Decl *To = Import(From);
5886 if (!To)
5887 return;
5888
5889 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5890 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005891
5892 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5893 if (!ToRecord->getDefinition()) {
5894 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005895 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005896 return;
5897 }
5898 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005899
5900 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5901 if (!ToEnum->getDefinition()) {
5902 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005903 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005904 return;
5905 }
5906 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005907
5908 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5909 if (!ToIFace->getDefinition()) {
5910 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005911 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005912 return;
5913 }
5914 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005915
Douglas Gregor2aa53772012-01-24 17:42:07 +00005916 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5917 if (!ToProto->getDefinition()) {
5918 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005919 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005920 return;
5921 }
5922 }
5923
Douglas Gregor0a791672011-01-18 03:11:38 +00005924 Importer.ImportDeclContext(FromDC, true);
5925 }
5926}
5927
Douglas Gregor96e578d2010-02-05 17:54:41 +00005928DeclarationName ASTImporter::Import(DeclarationName FromName) {
5929 if (!FromName)
5930 return DeclarationName();
5931
5932 switch (FromName.getNameKind()) {
5933 case DeclarationName::Identifier:
5934 return Import(FromName.getAsIdentifierInfo());
5935
5936 case DeclarationName::ObjCZeroArgSelector:
5937 case DeclarationName::ObjCOneArgSelector:
5938 case DeclarationName::ObjCMultiArgSelector:
5939 return Import(FromName.getObjCSelector());
5940
5941 case DeclarationName::CXXConstructorName: {
5942 QualType T = Import(FromName.getCXXNameType());
5943 if (T.isNull())
5944 return DeclarationName();
5945
5946 return ToContext.DeclarationNames.getCXXConstructorName(
5947 ToContext.getCanonicalType(T));
5948 }
5949
5950 case DeclarationName::CXXDestructorName: {
5951 QualType T = Import(FromName.getCXXNameType());
5952 if (T.isNull())
5953 return DeclarationName();
5954
5955 return ToContext.DeclarationNames.getCXXDestructorName(
5956 ToContext.getCanonicalType(T));
5957 }
5958
5959 case DeclarationName::CXXConversionFunctionName: {
5960 QualType T = Import(FromName.getCXXNameType());
5961 if (T.isNull())
5962 return DeclarationName();
5963
5964 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5965 ToContext.getCanonicalType(T));
5966 }
5967
5968 case DeclarationName::CXXOperatorName:
5969 return ToContext.DeclarationNames.getCXXOperatorName(
5970 FromName.getCXXOverloadedOperator());
5971
5972 case DeclarationName::CXXLiteralOperatorName:
5973 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5974 Import(FromName.getCXXLiteralIdentifier()));
5975
5976 case DeclarationName::CXXUsingDirective:
5977 // FIXME: STATICS!
5978 return DeclarationName::getUsingDirectiveName();
5979 }
5980
David Blaikiee4d798f2012-01-20 21:50:17 +00005981 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005982}
5983
Douglas Gregore2e50d332010-12-01 01:36:18 +00005984IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005985 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005986 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005987
5988 return &ToContext.Idents.get(FromId->getName());
5989}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005990
Douglas Gregor43f54792010-02-17 02:12:47 +00005991Selector ASTImporter::Import(Selector FromSel) {
5992 if (FromSel.isNull())
5993 return Selector();
5994
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005995 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005996 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5997 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5998 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5999 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6000}
6001
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006002DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6003 DeclContext *DC,
6004 unsigned IDNS,
6005 NamedDecl **Decls,
6006 unsigned NumDecls) {
6007 return Name;
6008}
6009
6010DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006011 if (LastDiagFromFrom)
6012 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6013 FromContext.getDiagnostics());
6014 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006015 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006016}
6017
6018DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006019 if (!LastDiagFromFrom)
6020 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6021 ToContext.getDiagnostics());
6022 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006023 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006024}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006025
Douglas Gregor2e15c842012-02-01 21:00:38 +00006026void ASTImporter::CompleteDecl (Decl *D) {
6027 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6028 if (!ID->getDefinition())
6029 ID->startDefinition();
6030 }
6031 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6032 if (!PD->getDefinition())
6033 PD->startDefinition();
6034 }
6035 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6036 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6037 TD->startDefinition();
6038 TD->setCompleteDefinition(true);
6039 }
6040 }
6041 else {
6042 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6043 }
6044}
6045
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006046Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006047 if (From->hasAttrs()) {
6048 for (Attr *FromAttr : From->getAttrs())
6049 To->addAttr(FromAttr->clone(To->getASTContext()));
6050 }
6051 if (From->isUsed()) {
6052 To->setIsUsed();
6053 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006054 ImportedDecls[From] = To;
6055 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006056}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006057
Douglas Gregordd6006f2012-07-17 21:16:27 +00006058bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6059 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006060 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006061 = ImportedTypes.find(From.getTypePtr());
6062 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6063 return true;
6064
Douglas Gregordd6006f2012-07-17 21:16:27 +00006065 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6066 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006067 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006068}