blob: 8460030b4b2002bb67ad7af9bc8486c3d61905c6 [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);
Sean Callanan65198272011-11-17 23:20:56 +0000133 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000134 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000135 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000136 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000137 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000138 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000139 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000140 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000141 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000142 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
143 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
144 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
145 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000146 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000147 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000148 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000149 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000150 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000151 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000152 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000153 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000154 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000155 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000156 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000157
158 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000159 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000160 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000161 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000162 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000163 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000164 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
165 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
166 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
167 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000168 Decl *VisitClassTemplateSpecializationDecl(
169 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000170 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
171 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
172
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000173 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000174 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
175
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000176 Stmt *VisitStmt(Stmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000177 Stmt *VisitDeclStmt(DeclStmt *S);
178 Stmt *VisitNullStmt(NullStmt *S);
179 Stmt *VisitCompoundStmt(CompoundStmt *S);
180 Stmt *VisitCaseStmt(CaseStmt *S);
181 Stmt *VisitDefaultStmt(DefaultStmt *S);
182 Stmt *VisitLabelStmt(LabelStmt *S);
183 Stmt *VisitAttributedStmt(AttributedStmt *S);
184 Stmt *VisitIfStmt(IfStmt *S);
185 Stmt *VisitSwitchStmt(SwitchStmt *S);
186 Stmt *VisitWhileStmt(WhileStmt *S);
187 Stmt *VisitDoStmt(DoStmt *S);
188 Stmt *VisitForStmt(ForStmt *S);
189 Stmt *VisitGotoStmt(GotoStmt *S);
190 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
191 Stmt *VisitContinueStmt(ContinueStmt *S);
192 Stmt *VisitBreakStmt(BreakStmt *S);
193 Stmt *VisitReturnStmt(ReturnStmt *S);
194 // FIXME: GCCAsmStmt
195 // FIXME: MSAsmStmt
196 // FIXME: SEHExceptStmt
197 // FIXME: SEHFinallyStmt
198 // FIXME: SEHTryStmt
199 // FIXME: SEHLeaveStmt
200 // FIXME: CapturedStmt
201 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
202 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
203 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
204 // FIXME: MSDependentExistsStmt
205 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
206 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
207 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
208 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
209 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
210 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
211 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000212
213 // Importing expressions
214 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000215 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000216 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000217 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000218 Expr *VisitParenExpr(ParenExpr *E);
219 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000220 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000221 Expr *VisitBinaryOperator(BinaryOperator *E);
222 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000223 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000224 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000225 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
226 Expr *VisitMemberExpr(MemberExpr *E);
227 Expr *VisitCallExpr(CallExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000228 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000229}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000230using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000231
232//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000233// Structural Equivalence
234//----------------------------------------------------------------------------
235
236namespace {
237 struct StructuralEquivalenceContext {
238 /// \brief AST contexts for which we are checking structural equivalence.
239 ASTContext &C1, &C2;
240
Douglas Gregor3996e242010-02-15 22:01:00 +0000241 /// \brief The set of "tentative" equivalences between two canonical
242 /// declarations, mapping from a declaration in the first context to the
243 /// declaration in the second context that we believe to be equivalent.
244 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
245
246 /// \brief Queue of declarations in the first context whose equivalence
247 /// with a declaration in the second context still needs to be verified.
248 std::deque<Decl *> DeclsToCheck;
249
Douglas Gregorb4964f72010-02-15 23:54:17 +0000250 /// \brief Declaration (from, to) pairs that are known not to be equivalent
251 /// (which we have already complained about).
252 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
253
Douglas Gregor3996e242010-02-15 22:01:00 +0000254 /// \brief Whether we're being strict about the spelling of types when
255 /// unifying two types.
256 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000257
258 /// \brief Whether to complain about failures.
259 bool Complain;
260
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000261 /// \brief \c true if the last diagnostic came from C2.
262 bool LastDiagFromC2;
263
Douglas Gregor3996e242010-02-15 22:01:00 +0000264 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000265 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000266 bool StrictTypeSpelling = false,
267 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000268 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000269 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
270 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000271
272 /// \brief Determine whether the two declarations are structurally
273 /// equivalent.
274 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
275
276 /// \brief Determine whether the two types are structurally equivalent.
277 bool IsStructurallyEquivalent(QualType T1, QualType T2);
278
279 private:
280 /// \brief Finish checking all of the structural equivalences.
281 ///
282 /// \returns true if an error occurred, false otherwise.
283 bool Finish();
284
285 public:
286 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000287 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000288 if (LastDiagFromC2)
289 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
290 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000291 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000292 }
293
294 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000295 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000296 if (!LastDiagFromC2)
297 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
298 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000299 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000300 }
301 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000302}
Douglas Gregor3996e242010-02-15 22:01:00 +0000303
304static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
305 QualType T1, QualType T2);
306static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
307 Decl *D1, Decl *D2);
308
Douglas Gregor3996e242010-02-15 22:01:00 +0000309/// \brief Determine structural equivalence of two expressions.
310static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
311 Expr *E1, Expr *E2) {
312 if (!E1 || !E2)
313 return E1 == E2;
314
315 // FIXME: Actually perform a structural comparison!
316 return true;
317}
318
319/// \brief Determine whether two identifiers are equivalent.
320static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
321 const IdentifierInfo *Name2) {
322 if (!Name1 || !Name2)
323 return Name1 == Name2;
324
325 return Name1->getName() == Name2->getName();
326}
327
328/// \brief Determine whether two nested-name-specifiers are equivalent.
329static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
330 NestedNameSpecifier *NNS1,
331 NestedNameSpecifier *NNS2) {
332 // FIXME: Implement!
333 return true;
334}
335
336/// \brief Determine whether two template arguments are equivalent.
337static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
338 const TemplateArgument &Arg1,
339 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000340 if (Arg1.getKind() != Arg2.getKind())
341 return false;
342
343 switch (Arg1.getKind()) {
344 case TemplateArgument::Null:
345 return true;
346
347 case TemplateArgument::Type:
348 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000349
Douglas Gregore2e50d332010-12-01 01:36:18 +0000350 case TemplateArgument::Integral:
351 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
352 Arg2.getIntegralType()))
353 return false;
354
Eric Christopher6dcc3762012-07-15 00:23:57 +0000355 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000356
357 case TemplateArgument::Declaration:
358 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000359
360 case TemplateArgument::NullPtr:
361 return true; // FIXME: Is this correct?
362
Douglas Gregore2e50d332010-12-01 01:36:18 +0000363 case TemplateArgument::Template:
364 return IsStructurallyEquivalent(Context,
365 Arg1.getAsTemplate(),
366 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000367
368 case TemplateArgument::TemplateExpansion:
369 return IsStructurallyEquivalent(Context,
370 Arg1.getAsTemplateOrTemplatePattern(),
371 Arg2.getAsTemplateOrTemplatePattern());
372
Douglas Gregore2e50d332010-12-01 01:36:18 +0000373 case TemplateArgument::Expression:
374 return IsStructurallyEquivalent(Context,
375 Arg1.getAsExpr(), Arg2.getAsExpr());
376
377 case TemplateArgument::Pack:
378 if (Arg1.pack_size() != Arg2.pack_size())
379 return false;
380
381 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
382 if (!IsStructurallyEquivalent(Context,
383 Arg1.pack_begin()[I],
384 Arg2.pack_begin()[I]))
385 return false;
386
387 return true;
388 }
389
390 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000391}
392
393/// \brief Determine structural equivalence for the common part of array
394/// types.
395static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
396 const ArrayType *Array1,
397 const ArrayType *Array2) {
398 if (!IsStructurallyEquivalent(Context,
399 Array1->getElementType(),
400 Array2->getElementType()))
401 return false;
402 if (Array1->getSizeModifier() != Array2->getSizeModifier())
403 return false;
404 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
405 return false;
406
407 return true;
408}
409
410/// \brief Determine structural equivalence of two types.
411static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
412 QualType T1, QualType T2) {
413 if (T1.isNull() || T2.isNull())
414 return T1.isNull() && T2.isNull();
415
416 if (!Context.StrictTypeSpelling) {
417 // We aren't being strict about token-to-token equivalence of types,
418 // so map down to the canonical type.
419 T1 = Context.C1.getCanonicalType(T1);
420 T2 = Context.C2.getCanonicalType(T2);
421 }
422
423 if (T1.getQualifiers() != T2.getQualifiers())
424 return false;
425
Douglas Gregorb4964f72010-02-15 23:54:17 +0000426 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000427
Douglas Gregorb4964f72010-02-15 23:54:17 +0000428 if (T1->getTypeClass() != T2->getTypeClass()) {
429 // Compare function types with prototypes vs. without prototypes as if
430 // both did not have prototypes.
431 if (T1->getTypeClass() == Type::FunctionProto &&
432 T2->getTypeClass() == Type::FunctionNoProto)
433 TC = Type::FunctionNoProto;
434 else if (T1->getTypeClass() == Type::FunctionNoProto &&
435 T2->getTypeClass() == Type::FunctionProto)
436 TC = Type::FunctionNoProto;
437 else
438 return false;
439 }
440
441 switch (TC) {
442 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000443 // FIXME: Deal with Char_S/Char_U.
444 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
445 return false;
446 break;
447
448 case Type::Complex:
449 if (!IsStructurallyEquivalent(Context,
450 cast<ComplexType>(T1)->getElementType(),
451 cast<ComplexType>(T2)->getElementType()))
452 return false;
453 break;
454
Reid Kleckner0503a872013-12-05 01:23:43 +0000455 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000456 case Type::Decayed:
457 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000458 cast<AdjustedType>(T1)->getOriginalType(),
459 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000460 return false;
461 break;
462
Douglas Gregor3996e242010-02-15 22:01:00 +0000463 case Type::Pointer:
464 if (!IsStructurallyEquivalent(Context,
465 cast<PointerType>(T1)->getPointeeType(),
466 cast<PointerType>(T2)->getPointeeType()))
467 return false;
468 break;
469
470 case Type::BlockPointer:
471 if (!IsStructurallyEquivalent(Context,
472 cast<BlockPointerType>(T1)->getPointeeType(),
473 cast<BlockPointerType>(T2)->getPointeeType()))
474 return false;
475 break;
476
477 case Type::LValueReference:
478 case Type::RValueReference: {
479 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
480 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
481 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
482 return false;
483 if (Ref1->isInnerRef() != Ref2->isInnerRef())
484 return false;
485 if (!IsStructurallyEquivalent(Context,
486 Ref1->getPointeeTypeAsWritten(),
487 Ref2->getPointeeTypeAsWritten()))
488 return false;
489 break;
490 }
491
492 case Type::MemberPointer: {
493 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
494 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 MemPtr1->getPointeeType(),
497 MemPtr2->getPointeeType()))
498 return false;
499 if (!IsStructurallyEquivalent(Context,
500 QualType(MemPtr1->getClass(), 0),
501 QualType(MemPtr2->getClass(), 0)))
502 return false;
503 break;
504 }
505
506 case Type::ConstantArray: {
507 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
508 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000509 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000510 return false;
511
512 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
513 return false;
514 break;
515 }
516
517 case Type::IncompleteArray:
518 if (!IsArrayStructurallyEquivalent(Context,
519 cast<ArrayType>(T1),
520 cast<ArrayType>(T2)))
521 return false;
522 break;
523
524 case Type::VariableArray: {
525 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
526 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
527 if (!IsStructurallyEquivalent(Context,
528 Array1->getSizeExpr(), Array2->getSizeExpr()))
529 return false;
530
531 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
532 return false;
533
534 break;
535 }
536
537 case Type::DependentSizedArray: {
538 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
539 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
540 if (!IsStructurallyEquivalent(Context,
541 Array1->getSizeExpr(), Array2->getSizeExpr()))
542 return false;
543
544 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
545 return false;
546
547 break;
548 }
549
550 case Type::DependentSizedExtVector: {
551 const DependentSizedExtVectorType *Vec1
552 = cast<DependentSizedExtVectorType>(T1);
553 const DependentSizedExtVectorType *Vec2
554 = cast<DependentSizedExtVectorType>(T2);
555 if (!IsStructurallyEquivalent(Context,
556 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
557 return false;
558 if (!IsStructurallyEquivalent(Context,
559 Vec1->getElementType(),
560 Vec2->getElementType()))
561 return false;
562 break;
563 }
564
565 case Type::Vector:
566 case Type::ExtVector: {
567 const VectorType *Vec1 = cast<VectorType>(T1);
568 const VectorType *Vec2 = cast<VectorType>(T2);
569 if (!IsStructurallyEquivalent(Context,
570 Vec1->getElementType(),
571 Vec2->getElementType()))
572 return false;
573 if (Vec1->getNumElements() != Vec2->getNumElements())
574 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000575 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000576 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000577 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000578 }
579
580 case Type::FunctionProto: {
581 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
582 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000583 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000584 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000585 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
586 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
587 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000588 return false;
589 }
590 if (Proto1->isVariadic() != Proto2->isVariadic())
591 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000592 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000593 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000594 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
595 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
596 return false;
597 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
598 if (!IsStructurallyEquivalent(Context,
599 Proto1->getExceptionType(I),
600 Proto2->getExceptionType(I)))
601 return false;
602 }
603 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000604 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000605 Proto1->getNoexceptExpr(),
606 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000607 return false;
608 }
609 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
610 return false;
611
612 // Fall through to check the bits common with FunctionNoProtoType.
613 }
614
615 case Type::FunctionNoProto: {
616 const FunctionType *Function1 = cast<FunctionType>(T1);
617 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000618 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
619 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000620 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000621 if (Function1->getExtInfo() != Function2->getExtInfo())
622 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000623 break;
624 }
625
626 case Type::UnresolvedUsing:
627 if (!IsStructurallyEquivalent(Context,
628 cast<UnresolvedUsingType>(T1)->getDecl(),
629 cast<UnresolvedUsingType>(T2)->getDecl()))
630 return false;
631
632 break;
John McCall81904512011-01-06 01:58:22 +0000633
634 case Type::Attributed:
635 if (!IsStructurallyEquivalent(Context,
636 cast<AttributedType>(T1)->getModifiedType(),
637 cast<AttributedType>(T2)->getModifiedType()))
638 return false;
639 if (!IsStructurallyEquivalent(Context,
640 cast<AttributedType>(T1)->getEquivalentType(),
641 cast<AttributedType>(T2)->getEquivalentType()))
642 return false;
643 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000644
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000645 case Type::Paren:
646 if (!IsStructurallyEquivalent(Context,
647 cast<ParenType>(T1)->getInnerType(),
648 cast<ParenType>(T2)->getInnerType()))
649 return false;
650 break;
651
Douglas Gregor3996e242010-02-15 22:01:00 +0000652 case Type::Typedef:
653 if (!IsStructurallyEquivalent(Context,
654 cast<TypedefType>(T1)->getDecl(),
655 cast<TypedefType>(T2)->getDecl()))
656 return false;
657 break;
658
659 case Type::TypeOfExpr:
660 if (!IsStructurallyEquivalent(Context,
661 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
662 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
663 return false;
664 break;
665
666 case Type::TypeOf:
667 if (!IsStructurallyEquivalent(Context,
668 cast<TypeOfType>(T1)->getUnderlyingType(),
669 cast<TypeOfType>(T2)->getUnderlyingType()))
670 return false;
671 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000672
673 case Type::UnaryTransform:
674 if (!IsStructurallyEquivalent(Context,
675 cast<UnaryTransformType>(T1)->getUnderlyingType(),
676 cast<UnaryTransformType>(T1)->getUnderlyingType()))
677 return false;
678 break;
679
Douglas Gregor3996e242010-02-15 22:01:00 +0000680 case Type::Decltype:
681 if (!IsStructurallyEquivalent(Context,
682 cast<DecltypeType>(T1)->getUnderlyingExpr(),
683 cast<DecltypeType>(T2)->getUnderlyingExpr()))
684 return false;
685 break;
686
Richard Smith30482bc2011-02-20 03:19:35 +0000687 case Type::Auto:
688 if (!IsStructurallyEquivalent(Context,
689 cast<AutoType>(T1)->getDeducedType(),
690 cast<AutoType>(T2)->getDeducedType()))
691 return false;
692 break;
693
Douglas Gregor3996e242010-02-15 22:01:00 +0000694 case Type::Record:
695 case Type::Enum:
696 if (!IsStructurallyEquivalent(Context,
697 cast<TagType>(T1)->getDecl(),
698 cast<TagType>(T2)->getDecl()))
699 return false;
700 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000701
Douglas Gregor3996e242010-02-15 22:01:00 +0000702 case Type::TemplateTypeParm: {
703 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
704 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
705 if (Parm1->getDepth() != Parm2->getDepth())
706 return false;
707 if (Parm1->getIndex() != Parm2->getIndex())
708 return false;
709 if (Parm1->isParameterPack() != Parm2->isParameterPack())
710 return false;
711
712 // Names of template type parameters are never significant.
713 break;
714 }
715
716 case Type::SubstTemplateTypeParm: {
717 const SubstTemplateTypeParmType *Subst1
718 = cast<SubstTemplateTypeParmType>(T1);
719 const SubstTemplateTypeParmType *Subst2
720 = cast<SubstTemplateTypeParmType>(T2);
721 if (!IsStructurallyEquivalent(Context,
722 QualType(Subst1->getReplacedParameter(), 0),
723 QualType(Subst2->getReplacedParameter(), 0)))
724 return false;
725 if (!IsStructurallyEquivalent(Context,
726 Subst1->getReplacementType(),
727 Subst2->getReplacementType()))
728 return false;
729 break;
730 }
731
Douglas Gregorfb322d82011-01-14 05:11:40 +0000732 case Type::SubstTemplateTypeParmPack: {
733 const SubstTemplateTypeParmPackType *Subst1
734 = cast<SubstTemplateTypeParmPackType>(T1);
735 const SubstTemplateTypeParmPackType *Subst2
736 = cast<SubstTemplateTypeParmPackType>(T2);
737 if (!IsStructurallyEquivalent(Context,
738 QualType(Subst1->getReplacedParameter(), 0),
739 QualType(Subst2->getReplacedParameter(), 0)))
740 return false;
741 if (!IsStructurallyEquivalent(Context,
742 Subst1->getArgumentPack(),
743 Subst2->getArgumentPack()))
744 return false;
745 break;
746 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000747 case Type::TemplateSpecialization: {
748 const TemplateSpecializationType *Spec1
749 = cast<TemplateSpecializationType>(T1);
750 const TemplateSpecializationType *Spec2
751 = cast<TemplateSpecializationType>(T2);
752 if (!IsStructurallyEquivalent(Context,
753 Spec1->getTemplateName(),
754 Spec2->getTemplateName()))
755 return false;
756 if (Spec1->getNumArgs() != Spec2->getNumArgs())
757 return false;
758 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
759 if (!IsStructurallyEquivalent(Context,
760 Spec1->getArg(I), Spec2->getArg(I)))
761 return false;
762 }
763 break;
764 }
765
Abramo Bagnara6150c882010-05-11 21:36:43 +0000766 case Type::Elaborated: {
767 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
768 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
769 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
770 if (Elab1->getKeyword() != Elab2->getKeyword())
771 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000772 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000773 Elab1->getQualifier(),
774 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000775 return false;
776 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000777 Elab1->getNamedType(),
778 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000779 return false;
780 break;
781 }
782
John McCalle78aac42010-03-10 03:28:59 +0000783 case Type::InjectedClassName: {
784 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
785 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
786 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000787 Inj1->getInjectedSpecializationType(),
788 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000789 return false;
790 break;
791 }
792
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000793 case Type::DependentName: {
794 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
795 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
797 Typename1->getQualifier(),
798 Typename2->getQualifier()))
799 return false;
800 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
801 Typename2->getIdentifier()))
802 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000803
804 break;
805 }
806
John McCallc392f372010-06-11 00:33:02 +0000807 case Type::DependentTemplateSpecialization: {
808 const DependentTemplateSpecializationType *Spec1 =
809 cast<DependentTemplateSpecializationType>(T1);
810 const DependentTemplateSpecializationType *Spec2 =
811 cast<DependentTemplateSpecializationType>(T2);
812 if (!IsStructurallyEquivalent(Context,
813 Spec1->getQualifier(),
814 Spec2->getQualifier()))
815 return false;
816 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
817 Spec2->getIdentifier()))
818 return false;
819 if (Spec1->getNumArgs() != Spec2->getNumArgs())
820 return false;
821 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
822 if (!IsStructurallyEquivalent(Context,
823 Spec1->getArg(I), Spec2->getArg(I)))
824 return false;
825 }
826 break;
827 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000828
829 case Type::PackExpansion:
830 if (!IsStructurallyEquivalent(Context,
831 cast<PackExpansionType>(T1)->getPattern(),
832 cast<PackExpansionType>(T2)->getPattern()))
833 return false;
834 break;
835
Douglas Gregor3996e242010-02-15 22:01:00 +0000836 case Type::ObjCInterface: {
837 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
838 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
839 if (!IsStructurallyEquivalent(Context,
840 Iface1->getDecl(), Iface2->getDecl()))
841 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000842 break;
843 }
844
845 case Type::ObjCObject: {
846 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
847 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
848 if (!IsStructurallyEquivalent(Context,
849 Obj1->getBaseType(),
850 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000851 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000852 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
853 return false;
854 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000855 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000856 Obj1->getProtocol(I),
857 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000858 return false;
859 }
860 break;
861 }
862
863 case Type::ObjCObjectPointer: {
864 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
865 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
866 if (!IsStructurallyEquivalent(Context,
867 Ptr1->getPointeeType(),
868 Ptr2->getPointeeType()))
869 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000870 break;
871 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000872
873 case Type::Atomic: {
874 if (!IsStructurallyEquivalent(Context,
875 cast<AtomicType>(T1)->getValueType(),
876 cast<AtomicType>(T2)->getValueType()))
877 return false;
878 break;
879 }
880
Douglas Gregor3996e242010-02-15 22:01:00 +0000881 } // end switch
882
883 return true;
884}
885
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000886/// \brief Determine structural equivalence of two fields.
887static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
888 FieldDecl *Field1, FieldDecl *Field2) {
889 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000890
891 // For anonymous structs/unions, match up the anonymous struct/union type
892 // declarations directly, so that we don't go off searching for anonymous
893 // types
894 if (Field1->isAnonymousStructOrUnion() &&
895 Field2->isAnonymousStructOrUnion()) {
896 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
897 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
898 return IsStructurallyEquivalent(Context, D1, D2);
899 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000900
901 // Check for equivalent field names.
902 IdentifierInfo *Name1 = Field1->getIdentifier();
903 IdentifierInfo *Name2 = Field2->getIdentifier();
904 if (!::IsStructurallyEquivalent(Name1, Name2))
905 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000906
907 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000908 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000909 if (Context.Complain) {
910 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
911 << Context.C2.getTypeDeclType(Owner2);
912 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
913 << Field2->getDeclName() << Field2->getType();
914 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
915 << Field1->getDeclName() << Field1->getType();
916 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000917 return false;
918 }
919
920 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000921 if (Context.Complain) {
922 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
923 << Context.C2.getTypeDeclType(Owner2);
924 if (Field1->isBitField()) {
925 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
926 << Field1->getDeclName() << Field1->getType()
927 << Field1->getBitWidthValue(Context.C1);
928 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
929 << Field2->getDeclName();
930 } else {
931 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
932 << Field2->getDeclName() << Field2->getType()
933 << Field2->getBitWidthValue(Context.C2);
934 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
935 << Field1->getDeclName();
936 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000937 }
938 return false;
939 }
940
941 if (Field1->isBitField()) {
942 // Make sure that the bit-fields are the same length.
943 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
944 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
945
946 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000947 if (Context.Complain) {
948 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
949 << Context.C2.getTypeDeclType(Owner2);
950 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
951 << Field2->getDeclName() << Field2->getType() << Bits2;
952 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
953 << Field1->getDeclName() << Field1->getType() << Bits1;
954 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000955 return false;
956 }
957 }
958
959 return true;
960}
961
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000962/// \brief Find the index of the given anonymous struct/union within its
963/// context.
964///
965/// \returns Returns the index of this anonymous struct/union in its context,
966/// including the next assigned index (if none of them match). Returns an
967/// empty option if the context is not a record, i.e.. if the anonymous
968/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000969static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000970 ASTContext &Context = Anon->getASTContext();
971 QualType AnonTy = Context.getRecordType(Anon);
972
973 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
974 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000975 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000976
977 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +0000978 for (const auto *D : Owner->noload_decls()) {
979 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000980 if (!F || !F->isAnonymousStructOrUnion())
981 continue;
982
983 if (Context.hasSameType(F->getType(), AnonTy))
984 break;
985
986 ++Index;
987 }
988
989 return Index;
990}
991
Douglas Gregor3996e242010-02-15 22:01:00 +0000992/// \brief Determine structural equivalence of two records.
993static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
994 RecordDecl *D1, RecordDecl *D2) {
995 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000996 if (Context.Complain) {
997 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
998 << Context.C2.getTypeDeclType(D2);
999 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1000 << D1->getDeclName() << (unsigned)D1->getTagKind();
1001 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001002 return false;
1003 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001004
1005 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1006 // If both anonymous structs/unions are in a record context, make sure
1007 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001008 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1009 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001010 if (*Index1 != *Index2)
1011 return false;
1012 }
1013 }
1014 }
1015
Douglas Gregore2e50d332010-12-01 01:36:18 +00001016 // If both declarations are class template specializations, we know
1017 // the ODR applies, so check the template and template arguments.
1018 ClassTemplateSpecializationDecl *Spec1
1019 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1020 ClassTemplateSpecializationDecl *Spec2
1021 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1022 if (Spec1 && Spec2) {
1023 // Check that the specialized templates are the same.
1024 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1025 Spec2->getSpecializedTemplate()))
1026 return false;
1027
1028 // Check that the template arguments are the same.
1029 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1030 return false;
1031
1032 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1033 if (!IsStructurallyEquivalent(Context,
1034 Spec1->getTemplateArgs().get(I),
1035 Spec2->getTemplateArgs().get(I)))
1036 return false;
1037 }
1038 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001039 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001040 else if (Spec1 || Spec2)
1041 return false;
1042
Douglas Gregorb4964f72010-02-15 23:54:17 +00001043 // Compare the definitions of these two records. If either or both are
1044 // incomplete, we assume that they are equivalent.
1045 D1 = D1->getDefinition();
1046 D2 = D2->getDefinition();
1047 if (!D1 || !D2)
1048 return true;
1049
Douglas Gregor3996e242010-02-15 22:01:00 +00001050 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1051 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1052 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001053 if (Context.Complain) {
1054 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1055 << Context.C2.getTypeDeclType(D2);
1056 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1057 << D2CXX->getNumBases();
1058 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1059 << D1CXX->getNumBases();
1060 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001061 return false;
1062 }
1063
1064 // Check the base classes.
1065 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1066 BaseEnd1 = D1CXX->bases_end(),
1067 Base2 = D2CXX->bases_begin();
1068 Base1 != BaseEnd1;
1069 ++Base1, ++Base2) {
1070 if (!IsStructurallyEquivalent(Context,
1071 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001072 if (Context.Complain) {
1073 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1074 << Context.C2.getTypeDeclType(D2);
1075 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1076 << Base2->getType()
1077 << Base2->getSourceRange();
1078 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1079 << Base1->getType()
1080 << Base1->getSourceRange();
1081 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001082 return false;
1083 }
1084
1085 // Check virtual vs. non-virtual inheritance mismatch.
1086 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001087 if (Context.Complain) {
1088 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1089 << Context.C2.getTypeDeclType(D2);
1090 Context.Diag2(Base2->getLocStart(),
1091 diag::note_odr_virtual_base)
1092 << Base2->isVirtual() << Base2->getSourceRange();
1093 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1094 << Base1->isVirtual()
1095 << Base1->getSourceRange();
1096 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001097 return false;
1098 }
1099 }
1100 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001101 if (Context.Complain) {
1102 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1103 << Context.C2.getTypeDeclType(D2);
1104 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1105 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1106 << Base1->getType()
1107 << Base1->getSourceRange();
1108 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1109 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001110 return false;
1111 }
1112 }
1113
1114 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001115 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001116 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001117 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001118 Field1End = D1->field_end();
1119 Field1 != Field1End;
1120 ++Field1, ++Field2) {
1121 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001122 if (Context.Complain) {
1123 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1124 << Context.C2.getTypeDeclType(D2);
1125 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1126 << Field1->getDeclName() << Field1->getType();
1127 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1128 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001129 return false;
1130 }
1131
David Blaikie40ed2972012-06-06 20:45:41 +00001132 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001133 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001134 }
1135
1136 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001137 if (Context.Complain) {
1138 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1139 << Context.C2.getTypeDeclType(D2);
1140 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1141 << Field2->getDeclName() << Field2->getType();
1142 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1143 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001144 return false;
1145 }
1146
1147 return true;
1148}
1149
1150/// \brief Determine structural equivalence of two enums.
1151static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1152 EnumDecl *D1, EnumDecl *D2) {
1153 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1154 EC2End = D2->enumerator_end();
1155 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1156 EC1End = D1->enumerator_end();
1157 EC1 != EC1End; ++EC1, ++EC2) {
1158 if (EC2 == EC2End) {
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(EC1->getLocation(), diag::note_odr_enumerator)
1163 << EC1->getDeclName()
1164 << EC1->getInitVal().toString(10);
1165 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1166 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001167 return false;
1168 }
1169
1170 llvm::APSInt Val1 = EC1->getInitVal();
1171 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001172 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001173 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
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(EC2->getLocation(), diag::note_odr_enumerator)
1178 << EC2->getDeclName()
1179 << EC2->getInitVal().toString(10);
1180 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1181 << EC1->getDeclName()
1182 << EC1->getInitVal().toString(10);
1183 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001184 return false;
1185 }
1186 }
1187
1188 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001189 if (Context.Complain) {
1190 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1191 << Context.C2.getTypeDeclType(D2);
1192 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1193 << EC2->getDeclName()
1194 << EC2->getInitVal().toString(10);
1195 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1196 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001197 return false;
1198 }
1199
1200 return true;
1201}
Douglas Gregora082a492010-11-30 19:14:50 +00001202
1203static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1204 TemplateParameterList *Params1,
1205 TemplateParameterList *Params2) {
1206 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001207 if (Context.Complain) {
1208 Context.Diag2(Params2->getTemplateLoc(),
1209 diag::err_odr_different_num_template_parameters)
1210 << Params1->size() << Params2->size();
1211 Context.Diag1(Params1->getTemplateLoc(),
1212 diag::note_odr_template_parameter_list);
1213 }
Douglas Gregora082a492010-11-30 19:14:50 +00001214 return false;
1215 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001216
Douglas Gregora082a492010-11-30 19:14:50 +00001217 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1218 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001219 if (Context.Complain) {
1220 Context.Diag2(Params2->getParam(I)->getLocation(),
1221 diag::err_odr_different_template_parameter_kind);
1222 Context.Diag1(Params1->getParam(I)->getLocation(),
1223 diag::note_odr_template_parameter_here);
1224 }
Douglas Gregora082a492010-11-30 19:14:50 +00001225 return false;
1226 }
1227
1228 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1229 Params2->getParam(I))) {
1230
1231 return false;
1232 }
1233 }
1234
1235 return true;
1236}
1237
1238static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1239 TemplateTypeParmDecl *D1,
1240 TemplateTypeParmDecl *D2) {
1241 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001242 if (Context.Complain) {
1243 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1244 << D2->isParameterPack();
1245 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1246 << D1->isParameterPack();
1247 }
Douglas Gregora082a492010-11-30 19:14:50 +00001248 return false;
1249 }
1250
1251 return true;
1252}
1253
1254static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1255 NonTypeTemplateParmDecl *D1,
1256 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001257 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001258 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001259 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1260 << D2->isParameterPack();
1261 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1262 << D1->isParameterPack();
1263 }
Douglas Gregora082a492010-11-30 19:14:50 +00001264 return false;
1265 }
Douglas Gregora082a492010-11-30 19:14:50 +00001266
1267 // Check types.
1268 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001269 if (Context.Complain) {
1270 Context.Diag2(D2->getLocation(),
1271 diag::err_odr_non_type_parameter_type_inconsistent)
1272 << D2->getType() << D1->getType();
1273 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1274 << D1->getType();
1275 }
Douglas Gregora082a492010-11-30 19:14:50 +00001276 return false;
1277 }
1278
1279 return true;
1280}
1281
1282static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1283 TemplateTemplateParmDecl *D1,
1284 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001285 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001286 if (Context.Complain) {
1287 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1288 << D2->isParameterPack();
1289 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1290 << D1->isParameterPack();
1291 }
Douglas Gregora082a492010-11-30 19:14:50 +00001292 return false;
1293 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001294
Douglas Gregora082a492010-11-30 19:14:50 +00001295 // Check template parameter lists.
1296 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1297 D2->getTemplateParameters());
1298}
1299
1300static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1301 ClassTemplateDecl *D1,
1302 ClassTemplateDecl *D2) {
1303 // Check template parameters.
1304 if (!IsStructurallyEquivalent(Context,
1305 D1->getTemplateParameters(),
1306 D2->getTemplateParameters()))
1307 return false;
1308
1309 // Check the templated declaration.
1310 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1311 D2->getTemplatedDecl());
1312}
1313
Douglas Gregor3996e242010-02-15 22:01:00 +00001314/// \brief Determine structural equivalence of two declarations.
1315static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1316 Decl *D1, Decl *D2) {
1317 // FIXME: Check for known structural equivalences via a callback of some sort.
1318
Douglas Gregorb4964f72010-02-15 23:54:17 +00001319 // Check whether we already know that these two declarations are not
1320 // structurally equivalent.
1321 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1322 D2->getCanonicalDecl())))
1323 return false;
1324
Douglas Gregor3996e242010-02-15 22:01:00 +00001325 // Determine whether we've already produced a tentative equivalence for D1.
1326 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1327 if (EquivToD1)
1328 return EquivToD1 == D2->getCanonicalDecl();
1329
1330 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1331 EquivToD1 = D2->getCanonicalDecl();
1332 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1333 return true;
1334}
1335
1336bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1337 Decl *D2) {
1338 if (!::IsStructurallyEquivalent(*this, D1, D2))
1339 return false;
1340
1341 return !Finish();
1342}
1343
1344bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1345 QualType T2) {
1346 if (!::IsStructurallyEquivalent(*this, T1, T2))
1347 return false;
1348
1349 return !Finish();
1350}
1351
1352bool StructuralEquivalenceContext::Finish() {
1353 while (!DeclsToCheck.empty()) {
1354 // Check the next declaration.
1355 Decl *D1 = DeclsToCheck.front();
1356 DeclsToCheck.pop_front();
1357
1358 Decl *D2 = TentativeEquivalences[D1];
1359 assert(D2 && "Unrecorded tentative equivalence?");
1360
Douglas Gregorb4964f72010-02-15 23:54:17 +00001361 bool Equivalent = true;
1362
Douglas Gregor3996e242010-02-15 22:01:00 +00001363 // FIXME: Switch on all declaration kinds. For now, we're just going to
1364 // check the obvious ones.
1365 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1366 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1367 // Check for equivalent structure names.
1368 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001369 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1370 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001371 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001372 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1373 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001374 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1375 !::IsStructurallyEquivalent(*this, Record1, Record2))
1376 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001377 } else {
1378 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001379 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001380 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001381 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001382 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1383 // Check for equivalent enum names.
1384 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001385 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1386 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001387 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001388 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1389 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001390 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1391 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1392 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001393 } else {
1394 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001395 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001396 }
Richard Smithdda56e42011-04-15 14:24:37 +00001397 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1398 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001399 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001400 Typedef2->getIdentifier()) ||
1401 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001402 Typedef1->getUnderlyingType(),
1403 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001404 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001405 } else {
1406 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001407 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001408 }
Douglas Gregora082a492010-11-30 19:14:50 +00001409 } else if (ClassTemplateDecl *ClassTemplate1
1410 = dyn_cast<ClassTemplateDecl>(D1)) {
1411 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1412 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1413 ClassTemplate2->getIdentifier()) ||
1414 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1415 Equivalent = false;
1416 } else {
1417 // Class template/non-class-template mismatch.
1418 Equivalent = false;
1419 }
1420 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1421 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1422 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1423 Equivalent = false;
1424 } else {
1425 // Kind mismatch.
1426 Equivalent = false;
1427 }
1428 } else if (NonTypeTemplateParmDecl *NTTP1
1429 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1430 if (NonTypeTemplateParmDecl *NTTP2
1431 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1432 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1433 Equivalent = false;
1434 } else {
1435 // Kind mismatch.
1436 Equivalent = false;
1437 }
1438 } else if (TemplateTemplateParmDecl *TTP1
1439 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1440 if (TemplateTemplateParmDecl *TTP2
1441 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1442 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1443 Equivalent = false;
1444 } else {
1445 // Kind mismatch.
1446 Equivalent = false;
1447 }
1448 }
1449
Douglas Gregorb4964f72010-02-15 23:54:17 +00001450 if (!Equivalent) {
1451 // Note that these two declarations are not equivalent (and we already
1452 // know about it).
1453 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1454 D2->getCanonicalDecl()));
1455 return true;
1456 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001457 // FIXME: Check other declaration kinds!
1458 }
1459
1460 return false;
1461}
1462
1463//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001464// Import Types
1465//----------------------------------------------------------------------------
1466
John McCall424cec92011-01-19 06:33:43 +00001467QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001468 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1469 << T->getTypeClassName();
1470 return QualType();
1471}
1472
John McCall424cec92011-01-19 06:33:43 +00001473QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001474 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001475#define SHARED_SINGLETON_TYPE(Expansion)
1476#define BUILTIN_TYPE(Id, SingletonId) \
1477 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1478#include "clang/AST/BuiltinTypes.def"
1479
1480 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1481 // context supports C++.
1482
1483 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1484 // context supports ObjC.
1485
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486 case BuiltinType::Char_U:
1487 // The context we're importing from has an unsigned 'char'. If we're
1488 // importing into a context with a signed 'char', translate to
1489 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001490 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001491 return Importer.getToContext().UnsignedCharTy;
1492
1493 return Importer.getToContext().CharTy;
1494
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495 case BuiltinType::Char_S:
1496 // The context we're importing from has an unsigned 'char'. If we're
1497 // importing into a context with a signed 'char', translate to
1498 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001499 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500 return Importer.getToContext().SignedCharTy;
1501
1502 return Importer.getToContext().CharTy;
1503
Chris Lattnerad3467e2010-12-25 23:25:43 +00001504 case BuiltinType::WChar_S:
1505 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506 // FIXME: If not in C++, shall we translate to the C equivalent of
1507 // wchar_t?
1508 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001509 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001510
1511 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001512}
1513
John McCall424cec92011-01-19 06:33:43 +00001514QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 QualType ToElementType = Importer.Import(T->getElementType());
1516 if (ToElementType.isNull())
1517 return QualType();
1518
1519 return Importer.getToContext().getComplexType(ToElementType);
1520}
1521
John McCall424cec92011-01-19 06:33:43 +00001522QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001523 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1524 if (ToPointeeType.isNull())
1525 return QualType();
1526
1527 return Importer.getToContext().getPointerType(ToPointeeType);
1528}
1529
John McCall424cec92011-01-19 06:33:43 +00001530QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001531 // FIXME: Check for blocks support in "to" context.
1532 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1533 if (ToPointeeType.isNull())
1534 return QualType();
1535
1536 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType
1540ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001541 // FIXME: Check for C++ support in "to" context.
1542 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1543 if (ToPointeeType.isNull())
1544 return QualType();
1545
1546 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType
1550ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001551 // FIXME: Check for C++0x support in "to" context.
1552 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1553 if (ToPointeeType.isNull())
1554 return QualType();
1555
1556 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1557}
1558
John McCall424cec92011-01-19 06:33:43 +00001559QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 // FIXME: Check for C++ support in "to" context.
1561 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1562 if (ToPointeeType.isNull())
1563 return QualType();
1564
1565 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1566 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1567 ClassType.getTypePtr());
1568}
1569
John McCall424cec92011-01-19 06:33:43 +00001570QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571 QualType ToElementType = Importer.Import(T->getElementType());
1572 if (ToElementType.isNull())
1573 return QualType();
1574
1575 return Importer.getToContext().getConstantArrayType(ToElementType,
1576 T->getSize(),
1577 T->getSizeModifier(),
1578 T->getIndexTypeCVRQualifiers());
1579}
1580
John McCall424cec92011-01-19 06:33:43 +00001581QualType
1582ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001583 QualType ToElementType = Importer.Import(T->getElementType());
1584 if (ToElementType.isNull())
1585 return QualType();
1586
1587 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1588 T->getSizeModifier(),
1589 T->getIndexTypeCVRQualifiers());
1590}
1591
John McCall424cec92011-01-19 06:33:43 +00001592QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001593 QualType ToElementType = Importer.Import(T->getElementType());
1594 if (ToElementType.isNull())
1595 return QualType();
1596
1597 Expr *Size = Importer.Import(T->getSizeExpr());
1598 if (!Size)
1599 return QualType();
1600
1601 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1602 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1603 T->getSizeModifier(),
1604 T->getIndexTypeCVRQualifiers(),
1605 Brackets);
1606}
1607
John McCall424cec92011-01-19 06:33:43 +00001608QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001609 QualType ToElementType = Importer.Import(T->getElementType());
1610 if (ToElementType.isNull())
1611 return QualType();
1612
1613 return Importer.getToContext().getVectorType(ToElementType,
1614 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001615 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001616}
1617
John McCall424cec92011-01-19 06:33:43 +00001618QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001619 QualType ToElementType = Importer.Import(T->getElementType());
1620 if (ToElementType.isNull())
1621 return QualType();
1622
1623 return Importer.getToContext().getExtVectorType(ToElementType,
1624 T->getNumElements());
1625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType
1628ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001629 // FIXME: What happens if we're importing a function without a prototype
1630 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001631 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001632 if (ToResultType.isNull())
1633 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001634
Douglas Gregor96e578d2010-02-05 17:54:41 +00001635 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001636 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001637}
1638
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001639QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001640 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001641 if (ToResultType.isNull())
1642 return QualType();
1643
1644 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001645 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001646 for (const auto &A : T->param_types()) {
1647 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001648 if (ArgType.isNull())
1649 return QualType();
1650 ArgTypes.push_back(ArgType);
1651 }
1652
1653 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001654 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001655 for (const auto &E : T->exceptions()) {
1656 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657 if (ExceptionType.isNull())
1658 return QualType();
1659 ExceptionTypes.push_back(ExceptionType);
1660 }
John McCalldb40c7f2010-12-14 08:05:40 +00001661
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001662 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1663 FunctionProtoType::ExtProtoInfo ToEPI;
1664
1665 ToEPI.ExtInfo = FromEPI.ExtInfo;
1666 ToEPI.Variadic = FromEPI.Variadic;
1667 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1668 ToEPI.TypeQuals = FromEPI.TypeQuals;
1669 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001670 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1671 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1672 ToEPI.ExceptionSpec.NoexceptExpr =
1673 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1674 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1675 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1676 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1677 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001678
Jordan Rose5c382722013-03-08 21:51:21 +00001679 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001680}
1681
Sean Callananda6df8a2011-08-11 16:56:07 +00001682QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1683 QualType ToInnerType = Importer.Import(T->getInnerType());
1684 if (ToInnerType.isNull())
1685 return QualType();
1686
1687 return Importer.getToContext().getParenType(ToInnerType);
1688}
1689
John McCall424cec92011-01-19 06:33:43 +00001690QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001691 TypedefNameDecl *ToDecl
1692 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001693 if (!ToDecl)
1694 return QualType();
1695
1696 return Importer.getToContext().getTypeDeclType(ToDecl);
1697}
1698
John McCall424cec92011-01-19 06:33:43 +00001699QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001700 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1701 if (!ToExpr)
1702 return QualType();
1703
1704 return Importer.getToContext().getTypeOfExprType(ToExpr);
1705}
1706
John McCall424cec92011-01-19 06:33:43 +00001707QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001708 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1709 if (ToUnderlyingType.isNull())
1710 return QualType();
1711
1712 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1713}
1714
John McCall424cec92011-01-19 06:33:43 +00001715QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001716 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001717 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1718 if (!ToExpr)
1719 return QualType();
1720
Douglas Gregor81495f32012-02-12 18:42:33 +00001721 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1722 if (UnderlyingType.isNull())
1723 return QualType();
1724
1725 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001726}
1727
Alexis Hunte852b102011-05-24 22:41:36 +00001728QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1729 QualType ToBaseType = Importer.Import(T->getBaseType());
1730 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1731 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1732 return QualType();
1733
1734 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1735 ToUnderlyingType,
1736 T->getUTTKind());
1737}
1738
Richard Smith30482bc2011-02-20 03:19:35 +00001739QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001740 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001741 QualType FromDeduced = T->getDeducedType();
1742 QualType ToDeduced;
1743 if (!FromDeduced.isNull()) {
1744 ToDeduced = Importer.Import(FromDeduced);
1745 if (ToDeduced.isNull())
1746 return QualType();
1747 }
1748
Faisal Vali2b391ab2013-09-26 19:54:12 +00001749 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1750 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001751}
1752
John McCall424cec92011-01-19 06:33:43 +00001753QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001754 RecordDecl *ToDecl
1755 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1756 if (!ToDecl)
1757 return QualType();
1758
1759 return Importer.getToContext().getTagDeclType(ToDecl);
1760}
1761
John McCall424cec92011-01-19 06:33:43 +00001762QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001763 EnumDecl *ToDecl
1764 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1765 if (!ToDecl)
1766 return QualType();
1767
1768 return Importer.getToContext().getTagDeclType(ToDecl);
1769}
1770
Sean Callanan72fe0852015-04-02 23:50:08 +00001771QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1772 QualType FromModifiedType = T->getModifiedType();
1773 QualType FromEquivalentType = T->getEquivalentType();
1774 QualType ToModifiedType;
1775 QualType ToEquivalentType;
1776
1777 if (!FromModifiedType.isNull()) {
1778 ToModifiedType = Importer.Import(FromModifiedType);
1779 if (ToModifiedType.isNull())
1780 return QualType();
1781 }
1782 if (!FromEquivalentType.isNull()) {
1783 ToEquivalentType = Importer.Import(FromEquivalentType);
1784 if (ToEquivalentType.isNull())
1785 return QualType();
1786 }
1787
1788 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1789 ToModifiedType, ToEquivalentType);
1790}
1791
Douglas Gregore2e50d332010-12-01 01:36:18 +00001792QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001793 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001794 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1795 if (ToTemplate.isNull())
1796 return QualType();
1797
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001798 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001799 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1800 return QualType();
1801
1802 QualType ToCanonType;
1803 if (!QualType(T, 0).isCanonical()) {
1804 QualType FromCanonType
1805 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1806 ToCanonType =Importer.Import(FromCanonType);
1807 if (ToCanonType.isNull())
1808 return QualType();
1809 }
1810 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1811 ToTemplateArgs.data(),
1812 ToTemplateArgs.size(),
1813 ToCanonType);
1814}
1815
John McCall424cec92011-01-19 06:33:43 +00001816QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001817 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001818 // Note: the qualifier in an ElaboratedType is optional.
1819 if (T->getQualifier()) {
1820 ToQualifier = Importer.Import(T->getQualifier());
1821 if (!ToQualifier)
1822 return QualType();
1823 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001824
1825 QualType ToNamedType = Importer.Import(T->getNamedType());
1826 if (ToNamedType.isNull())
1827 return QualType();
1828
Abramo Bagnara6150c882010-05-11 21:36:43 +00001829 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1830 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001831}
1832
John McCall424cec92011-01-19 06:33:43 +00001833QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001834 ObjCInterfaceDecl *Class
1835 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1836 if (!Class)
1837 return QualType();
1838
John McCall8b07ec22010-05-15 11:32:37 +00001839 return Importer.getToContext().getObjCInterfaceType(Class);
1840}
1841
John McCall424cec92011-01-19 06:33:43 +00001842QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001843 QualType ToBaseType = Importer.Import(T->getBaseType());
1844 if (ToBaseType.isNull())
1845 return QualType();
1846
Douglas Gregore9d95f12015-07-07 03:57:35 +00001847 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001848 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001849 QualType ImportedTypeArg = Importer.Import(TypeArg);
1850 if (ImportedTypeArg.isNull())
1851 return QualType();
1852
1853 TypeArgs.push_back(ImportedTypeArg);
1854 }
1855
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001856 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001857 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001858 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001859 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001860 if (!Protocol)
1861 return QualType();
1862 Protocols.push_back(Protocol);
1863 }
1864
Douglas Gregore9d95f12015-07-07 03:57:35 +00001865 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001866 Protocols,
1867 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001868}
1869
John McCall424cec92011-01-19 06:33:43 +00001870QualType
1871ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001872 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1873 if (ToPointeeType.isNull())
1874 return QualType();
1875
John McCall8b07ec22010-05-15 11:32:37 +00001876 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001877}
1878
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001879//----------------------------------------------------------------------------
1880// Import Declarations
1881//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001882bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1883 DeclContext *&LexicalDC,
1884 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001885 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001886 SourceLocation &Loc) {
1887 // Import the context of this declaration.
1888 DC = Importer.ImportContext(D->getDeclContext());
1889 if (!DC)
1890 return true;
1891
1892 LexicalDC = DC;
1893 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1894 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1895 if (!LexicalDC)
1896 return true;
1897 }
1898
1899 // Import the name of this declaration.
1900 Name = Importer.Import(D->getDeclName());
1901 if (D->getDeclName() && !Name)
1902 return true;
1903
1904 // Import the location of this declaration.
1905 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001906 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001907 return false;
1908}
1909
Douglas Gregord451ea92011-07-29 23:31:30 +00001910void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1911 if (!FromD)
1912 return;
1913
1914 if (!ToD) {
1915 ToD = Importer.Import(FromD);
1916 if (!ToD)
1917 return;
1918 }
1919
1920 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1921 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001922 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001923 ImportDefinition(FromRecord, ToRecord);
1924 }
1925 }
1926 return;
1927 }
1928
1929 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1930 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1931 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1932 ImportDefinition(FromEnum, ToEnum);
1933 }
1934 }
1935 return;
1936 }
1937}
1938
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001939void
1940ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1941 DeclarationNameInfo& To) {
1942 // NOTE: To.Name and To.Loc are already imported.
1943 // We only have to import To.LocInfo.
1944 switch (To.getName().getNameKind()) {
1945 case DeclarationName::Identifier:
1946 case DeclarationName::ObjCZeroArgSelector:
1947 case DeclarationName::ObjCOneArgSelector:
1948 case DeclarationName::ObjCMultiArgSelector:
1949 case DeclarationName::CXXUsingDirective:
1950 return;
1951
1952 case DeclarationName::CXXOperatorName: {
1953 SourceRange Range = From.getCXXOperatorNameRange();
1954 To.setCXXOperatorNameRange(Importer.Import(Range));
1955 return;
1956 }
1957 case DeclarationName::CXXLiteralOperatorName: {
1958 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1959 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1960 return;
1961 }
1962 case DeclarationName::CXXConstructorName:
1963 case DeclarationName::CXXDestructorName:
1964 case DeclarationName::CXXConversionFunctionName: {
1965 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1966 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1967 return;
1968 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001969 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001970 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001971}
1972
Douglas Gregor2e15c842012-02-01 21:00:38 +00001973void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001974 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001975 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001976 return;
1977 }
1978
Aaron Ballman629afae2014-03-07 19:56:05 +00001979 for (auto *From : FromDC->decls())
1980 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001981}
1982
Douglas Gregord451ea92011-07-29 23:31:30 +00001983bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001984 ImportDefinitionKind Kind) {
1985 if (To->getDefinition() || To->isBeingDefined()) {
1986 if (Kind == IDK_Everything)
1987 ImportDeclContext(From, /*ForceImport=*/true);
1988
Douglas Gregore2e50d332010-12-01 01:36:18 +00001989 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001990 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001991
1992 To->startDefinition();
1993
1994 // Add base classes.
1995 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1996 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001997
1998 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1999 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2000 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002001 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002002 ToData.Aggregate = FromData.Aggregate;
2003 ToData.PlainOldData = FromData.PlainOldData;
2004 ToData.Empty = FromData.Empty;
2005 ToData.Polymorphic = FromData.Polymorphic;
2006 ToData.Abstract = FromData.Abstract;
2007 ToData.IsStandardLayout = FromData.IsStandardLayout;
2008 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2009 ToData.HasPrivateFields = FromData.HasPrivateFields;
2010 ToData.HasProtectedFields = FromData.HasProtectedFields;
2011 ToData.HasPublicFields = FromData.HasPublicFields;
2012 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002013 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002014 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002015 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002016 ToData.HasUninitializedReferenceMember
2017 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00002018 ToData.NeedOverloadResolutionForMoveConstructor
2019 = FromData.NeedOverloadResolutionForMoveConstructor;
2020 ToData.NeedOverloadResolutionForMoveAssignment
2021 = FromData.NeedOverloadResolutionForMoveAssignment;
2022 ToData.NeedOverloadResolutionForDestructor
2023 = FromData.NeedOverloadResolutionForDestructor;
2024 ToData.DefaultedMoveConstructorIsDeleted
2025 = FromData.DefaultedMoveConstructorIsDeleted;
2026 ToData.DefaultedMoveAssignmentIsDeleted
2027 = FromData.DefaultedMoveAssignmentIsDeleted;
2028 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002029 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2030 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002031 ToData.HasConstexprNonCopyMoveConstructor
2032 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002033 ToData.DefaultedDefaultConstructorIsConstexpr
2034 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002035 ToData.HasConstexprDefaultConstructor
2036 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002037 ToData.HasNonLiteralTypeFieldsOrBases
2038 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002039 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002040 ToData.UserProvidedDefaultConstructor
2041 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002042 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002043 ToData.ImplicitCopyConstructorHasConstParam
2044 = FromData.ImplicitCopyConstructorHasConstParam;
2045 ToData.ImplicitCopyAssignmentHasConstParam
2046 = FromData.ImplicitCopyAssignmentHasConstParam;
2047 ToData.HasDeclaredCopyConstructorWithConstParam
2048 = FromData.HasDeclaredCopyConstructorWithConstParam;
2049 ToData.HasDeclaredCopyAssignmentWithConstParam
2050 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002051 ToData.IsLambda = FromData.IsLambda;
2052
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002053 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002054 for (const auto &Base1 : FromCXX->bases()) {
2055 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002056 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002057 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002058
2059 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002060 if (Base1.isPackExpansion())
2061 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002062
2063 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002064 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002065
Douglas Gregore2e50d332010-12-01 01:36:18 +00002066 Bases.push_back(
2067 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002068 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2069 Base1.isVirtual(),
2070 Base1.isBaseOfClass(),
2071 Base1.getAccessSpecifierAsWritten(),
2072 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002073 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002074 }
2075 if (!Bases.empty())
2076 ToCXX->setBases(Bases.data(), Bases.size());
2077 }
2078
Douglas Gregor2e15c842012-02-01 21:00:38 +00002079 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002080 ImportDeclContext(From, /*ForceImport=*/true);
2081
Douglas Gregore2e50d332010-12-01 01:36:18 +00002082 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002083 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002084}
2085
Larisse Voufo39a1e502013-08-06 01:03:05 +00002086bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2087 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002088 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002089 return false;
2090
2091 // FIXME: Can we really import any initializer? Alternatively, we could force
2092 // ourselves to import every declaration of a variable and then only use
2093 // getInit() here.
2094 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2095
2096 // FIXME: Other bits to merge?
2097
2098 return false;
2099}
2100
Douglas Gregord451ea92011-07-29 23:31:30 +00002101bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002102 ImportDefinitionKind Kind) {
2103 if (To->getDefinition() || To->isBeingDefined()) {
2104 if (Kind == IDK_Everything)
2105 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002106 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002107 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002108
2109 To->startDefinition();
2110
2111 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2112 if (T.isNull())
2113 return true;
2114
2115 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2116 if (ToPromotionType.isNull())
2117 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002118
2119 if (shouldForceImportDeclContext(Kind))
2120 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002121
2122 // FIXME: we might need to merge the number of positive or negative bits
2123 // if the enumerator lists don't match.
2124 To->completeDefinition(T, ToPromotionType,
2125 From->getNumPositiveBits(),
2126 From->getNumNegativeBits());
2127 return false;
2128}
2129
Douglas Gregora082a492010-11-30 19:14:50 +00002130TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2131 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002132 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002133 ToParams.reserve(Params->size());
2134 for (TemplateParameterList::iterator P = Params->begin(),
2135 PEnd = Params->end();
2136 P != PEnd; ++P) {
2137 Decl *To = Importer.Import(*P);
2138 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002139 return nullptr;
2140
Douglas Gregora082a492010-11-30 19:14:50 +00002141 ToParams.push_back(cast<NamedDecl>(To));
2142 }
2143
2144 return TemplateParameterList::Create(Importer.getToContext(),
2145 Importer.Import(Params->getTemplateLoc()),
2146 Importer.Import(Params->getLAngleLoc()),
2147 ToParams.data(), ToParams.size(),
2148 Importer.Import(Params->getRAngleLoc()));
2149}
2150
Douglas Gregore2e50d332010-12-01 01:36:18 +00002151TemplateArgument
2152ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2153 switch (From.getKind()) {
2154 case TemplateArgument::Null:
2155 return TemplateArgument();
2156
2157 case TemplateArgument::Type: {
2158 QualType ToType = Importer.Import(From.getAsType());
2159 if (ToType.isNull())
2160 return TemplateArgument();
2161 return TemplateArgument(ToType);
2162 }
2163
2164 case TemplateArgument::Integral: {
2165 QualType ToType = Importer.Import(From.getIntegralType());
2166 if (ToType.isNull())
2167 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002168 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002169 }
2170
Eli Friedmanb826a002012-09-26 02:36:12 +00002171 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002172 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2173 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2174 if (!To || ToType.isNull())
2175 return TemplateArgument();
2176 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002177 }
2178
2179 case TemplateArgument::NullPtr: {
2180 QualType ToType = Importer.Import(From.getNullPtrType());
2181 if (ToType.isNull())
2182 return TemplateArgument();
2183 return TemplateArgument(ToType, /*isNullPtr*/true);
2184 }
2185
Douglas Gregore2e50d332010-12-01 01:36:18 +00002186 case TemplateArgument::Template: {
2187 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2188 if (ToTemplate.isNull())
2189 return TemplateArgument();
2190
2191 return TemplateArgument(ToTemplate);
2192 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002193
2194 case TemplateArgument::TemplateExpansion: {
2195 TemplateName ToTemplate
2196 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2197 if (ToTemplate.isNull())
2198 return TemplateArgument();
2199
Douglas Gregore1d60df2011-01-14 23:41:42 +00002200 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002201 }
2202
Douglas Gregore2e50d332010-12-01 01:36:18 +00002203 case TemplateArgument::Expression:
2204 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2205 return TemplateArgument(ToExpr);
2206 return TemplateArgument();
2207
2208 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002209 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002210 ToPack.reserve(From.pack_size());
2211 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2212 return TemplateArgument();
2213
2214 TemplateArgument *ToArgs
2215 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2216 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2217 return TemplateArgument(ToArgs, ToPack.size());
2218 }
2219 }
2220
2221 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002222}
2223
2224bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2225 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002226 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002227 for (unsigned I = 0; I != NumFromArgs; ++I) {
2228 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2229 if (To.isNull() && !FromArgs[I].isNull())
2230 return true;
2231
2232 ToArgs.push_back(To);
2233 }
2234
2235 return false;
2236}
2237
Douglas Gregor5c73e912010-02-11 00:48:18 +00002238bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002239 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002240 // Eliminate a potential failure point where we attempt to re-import
2241 // something we're trying to import while completing ToRecord.
2242 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2243 if (ToOrigin) {
2244 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2245 if (ToOriginRecord)
2246 ToRecord = ToOriginRecord;
2247 }
2248
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002249 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002250 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002251 Importer.getNonEquivalentDecls(),
2252 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002253 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002254}
2255
Larisse Voufo39a1e502013-08-06 01:03:05 +00002256bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2257 bool Complain) {
2258 StructuralEquivalenceContext Ctx(
2259 Importer.getFromContext(), Importer.getToContext(),
2260 Importer.getNonEquivalentDecls(), false, Complain);
2261 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2262}
2263
Douglas Gregor98c10182010-02-12 22:17:39 +00002264bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002265 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002266 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002267 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002268 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002269}
2270
Douglas Gregor91155082012-11-14 22:29:20 +00002271bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2272 EnumConstantDecl *ToEC)
2273{
2274 const llvm::APSInt &FromVal = FromEC->getInitVal();
2275 const llvm::APSInt &ToVal = ToEC->getInitVal();
2276
2277 return FromVal.isSigned() == ToVal.isSigned() &&
2278 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2279 FromVal == ToVal;
2280}
2281
2282bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002283 ClassTemplateDecl *To) {
2284 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2285 Importer.getToContext(),
2286 Importer.getNonEquivalentDecls());
2287 return Ctx.IsStructurallyEquivalent(From, To);
2288}
2289
Larisse Voufo39a1e502013-08-06 01:03:05 +00002290bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2291 VarTemplateDecl *To) {
2292 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2293 Importer.getToContext(),
2294 Importer.getNonEquivalentDecls());
2295 return Ctx.IsStructurallyEquivalent(From, To);
2296}
2297
Douglas Gregore4c83e42010-02-09 22:48:33 +00002298Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002299 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002300 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002301 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002302}
2303
Sean Callanan65198272011-11-17 23:20:56 +00002304Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2305 TranslationUnitDecl *ToD =
2306 Importer.getToContext().getTranslationUnitDecl();
2307
2308 Importer.Imported(D, ToD);
2309
2310 return ToD;
2311}
2312
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002313Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2314 // Import the major distinguishing characteristics of this namespace.
2315 DeclContext *DC, *LexicalDC;
2316 DeclarationName Name;
2317 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002318 NamedDecl *ToD;
2319 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002320 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002321 if (ToD)
2322 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002323
2324 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002325 if (!Name) {
2326 // This is an anonymous namespace. Adopt an existing anonymous
2327 // namespace if we can.
2328 // FIXME: Not testable.
2329 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2330 MergeWithNamespace = TU->getAnonymousNamespace();
2331 else
2332 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2333 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002334 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002335 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002336 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002337 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2338 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002339 continue;
2340
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002341 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002342 MergeWithNamespace = FoundNS;
2343 ConflictingDecls.clear();
2344 break;
2345 }
2346
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002347 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002348 }
2349
2350 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002351 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002352 ConflictingDecls.data(),
2353 ConflictingDecls.size());
2354 }
2355 }
2356
2357 // Create the "to" namespace, if needed.
2358 NamespaceDecl *ToNamespace = MergeWithNamespace;
2359 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002360 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002361 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002362 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002363 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002364 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002365 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002366 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002367
2368 // If this is an anonymous namespace, register it as the anonymous
2369 // namespace within its context.
2370 if (!Name) {
2371 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2372 TU->setAnonymousNamespace(ToNamespace);
2373 else
2374 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2375 }
2376 }
2377 Importer.Imported(D, ToNamespace);
2378
2379 ImportDeclContext(D);
2380
2381 return ToNamespace;
2382}
2383
Richard Smithdda56e42011-04-15 14:24:37 +00002384Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002385 // Import the major distinguishing characteristics of this typedef.
2386 DeclContext *DC, *LexicalDC;
2387 DeclarationName Name;
2388 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002389 NamedDecl *ToD;
2390 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002391 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002392 if (ToD)
2393 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002394
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002395 // If this typedef is not in block scope, determine whether we've
2396 // seen a typedef with the same name (that we can merge with) or any
2397 // other entity by that name (which name lookup could conflict with).
2398 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002399 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002400 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002401 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002402 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002403 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2404 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002405 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002406 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002407 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002408 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2409 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002410 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002411 }
2412
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002413 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002414 }
2415
2416 if (!ConflictingDecls.empty()) {
2417 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2418 ConflictingDecls.data(),
2419 ConflictingDecls.size());
2420 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002421 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002422 }
2423 }
2424
Douglas Gregorb4964f72010-02-15 23:54:17 +00002425 // Import the underlying type of this typedef;
2426 QualType T = Importer.Import(D->getUnderlyingType());
2427 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002428 return nullptr;
2429
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002430 // Create the new typedef node.
2431 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002432 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002433 TypedefNameDecl *ToTypedef;
2434 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002435 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2436 StartL, Loc,
2437 Name.getAsIdentifierInfo(),
2438 TInfo);
2439 else
Richard Smithdda56e42011-04-15 14:24:37 +00002440 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2441 StartL, Loc,
2442 Name.getAsIdentifierInfo(),
2443 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002444
Douglas Gregordd483172010-02-22 17:42:47 +00002445 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002446 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002447 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002448 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002449
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002450 return ToTypedef;
2451}
2452
Richard Smithdda56e42011-04-15 14:24:37 +00002453Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2454 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2455}
2456
2457Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2458 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2459}
2460
Douglas Gregor98c10182010-02-12 22:17:39 +00002461Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2462 // Import the major distinguishing characteristics of this enum.
2463 DeclContext *DC, *LexicalDC;
2464 DeclarationName Name;
2465 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002466 NamedDecl *ToD;
2467 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002468 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002469 if (ToD)
2470 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002471
Douglas Gregor98c10182010-02-12 22:17:39 +00002472 // Figure out what enum name we're looking for.
2473 unsigned IDNS = Decl::IDNS_Tag;
2474 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002475 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2476 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002477 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002478 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002479 IDNS |= Decl::IDNS_Ordinary;
2480
2481 // We may already have an enum of the same name; try to find and match it.
2482 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002483 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002484 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002485 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002486 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2487 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002488 continue;
2489
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002490 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002491 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002492 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2493 Found = Tag->getDecl();
2494 }
2495
2496 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002497 if (IsStructuralMatch(D, FoundEnum))
2498 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002499 }
2500
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002501 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002502 }
2503
2504 if (!ConflictingDecls.empty()) {
2505 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2506 ConflictingDecls.data(),
2507 ConflictingDecls.size());
2508 }
2509 }
2510
2511 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002512 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2513 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002514 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002515 D->isScoped(), D->isScopedUsingClassTag(),
2516 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002517 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002518 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002519 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002520 D2->setLexicalDeclContext(LexicalDC);
2521 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002522 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002523
2524 // Import the integer type.
2525 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2526 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002527 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002528 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002529
2530 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002531 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002532 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002533
Douglas Gregor3996e242010-02-15 22:01:00 +00002534 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002535}
2536
Douglas Gregor5c73e912010-02-11 00:48:18 +00002537Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2538 // If this record has a definition in the translation unit we're coming from,
2539 // but this particular declaration is not that definition, import the
2540 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002541 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002542 if (Definition && Definition != D) {
2543 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002544 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002545 return nullptr;
2546
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002547 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002548 }
2549
2550 // Import the major distinguishing characteristics of this record.
2551 DeclContext *DC, *LexicalDC;
2552 DeclarationName Name;
2553 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002554 NamedDecl *ToD;
2555 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002556 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002557 if (ToD)
2558 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002559
Douglas Gregor5c73e912010-02-11 00:48:18 +00002560 // Figure out what structure name we're looking for.
2561 unsigned IDNS = Decl::IDNS_Tag;
2562 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002563 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2564 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002565 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002566 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002567 IDNS |= Decl::IDNS_Ordinary;
2568
2569 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002570 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002571 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002572 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002573 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002574 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002575 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2576 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002577 continue;
2578
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002579 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002580 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002581 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2582 Found = Tag->getDecl();
2583 }
2584
2585 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002586 if (D->isAnonymousStructOrUnion() &&
2587 FoundRecord->isAnonymousStructOrUnion()) {
2588 // If both anonymous structs/unions are in a record context, make sure
2589 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002590 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002591 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002592 if (Optional<unsigned> Index2 =
2593 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002594 if (*Index1 != *Index2)
2595 continue;
2596 }
2597 }
2598 }
2599
Douglas Gregor25791052010-02-12 00:09:27 +00002600 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002601 if ((SearchName && !D->isCompleteDefinition())
2602 || (D->isCompleteDefinition() &&
2603 D->isAnonymousStructOrUnion()
2604 == FoundDef->isAnonymousStructOrUnion() &&
2605 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002606 // The record types structurally match, or the "from" translation
2607 // unit only had a forward declaration anyway; call it the same
2608 // function.
2609 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002610 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002611 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002612 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002613 // We have a forward declaration of this type, so adopt that forward
2614 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002615
2616 // If one or both can be completed from external storage then try one
2617 // last time to complete and compare them before doing this.
2618
2619 if (FoundRecord->hasExternalLexicalStorage() &&
2620 !FoundRecord->isCompleteDefinition())
2621 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2622 if (D->hasExternalLexicalStorage())
2623 D->getASTContext().getExternalSource()->CompleteType(D);
2624
2625 if (FoundRecord->isCompleteDefinition() &&
2626 D->isCompleteDefinition() &&
2627 !IsStructuralMatch(D, FoundRecord))
2628 continue;
2629
Douglas Gregor25791052010-02-12 00:09:27 +00002630 AdoptDecl = FoundRecord;
2631 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002632 } else if (!SearchName) {
2633 continue;
2634 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002635 }
2636
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002637 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002638 }
2639
Douglas Gregordd6006f2012-07-17 21:16:27 +00002640 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002641 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2642 ConflictingDecls.data(),
2643 ConflictingDecls.size());
2644 }
2645 }
2646
2647 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002648 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002649 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002650 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002651 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002652 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002653 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002654 DC, StartLoc, Loc,
2655 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002656 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002657 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002658 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002659 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002660 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002661 }
Douglas Gregor14454802011-02-25 02:25:35 +00002662
2663 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002664 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002665 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002666 if (D->isAnonymousStructOrUnion())
2667 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002668 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002669
Douglas Gregor3996e242010-02-15 22:01:00 +00002670 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002671
Douglas Gregor95d82832012-01-24 18:36:04 +00002672 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002673 return nullptr;
2674
Douglas Gregor3996e242010-02-15 22:01:00 +00002675 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002676}
2677
Douglas Gregor98c10182010-02-12 22:17:39 +00002678Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2679 // Import the major distinguishing characteristics of this enumerator.
2680 DeclContext *DC, *LexicalDC;
2681 DeclarationName Name;
2682 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002683 NamedDecl *ToD;
2684 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002685 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002686 if (ToD)
2687 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002688
2689 QualType T = Importer.Import(D->getType());
2690 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002691 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002692
Douglas Gregor98c10182010-02-12 22:17:39 +00002693 // Determine whether there are any other declarations with the same name and
2694 // in the same context.
2695 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002696 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002697 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002698 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002699 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002700 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2701 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002702 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002703
2704 if (EnumConstantDecl *FoundEnumConstant
2705 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2706 if (IsStructuralMatch(D, FoundEnumConstant))
2707 return Importer.Imported(D, FoundEnumConstant);
2708 }
2709
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002710 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002711 }
2712
2713 if (!ConflictingDecls.empty()) {
2714 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2715 ConflictingDecls.data(),
2716 ConflictingDecls.size());
2717 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002718 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002719 }
2720 }
2721
2722 Expr *Init = Importer.Import(D->getInitExpr());
2723 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002724 return nullptr;
2725
Douglas Gregor98c10182010-02-12 22:17:39 +00002726 EnumConstantDecl *ToEnumerator
2727 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2728 Name.getAsIdentifierInfo(), T,
2729 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002730 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002731 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002732 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002733 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002734 return ToEnumerator;
2735}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002736
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002737Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2738 // Import the major distinguishing characteristics of this function.
2739 DeclContext *DC, *LexicalDC;
2740 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002741 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002742 NamedDecl *ToD;
2743 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002744 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002745 if (ToD)
2746 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002747
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002748 // Try to find a function in our own ("to") context with the same name, same
2749 // type, and in the same context as the function we're importing.
2750 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002751 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002752 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002753 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002754 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002755 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2756 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002757 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002758
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002759 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002760 if (FoundFunction->hasExternalFormalLinkage() &&
2761 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002762 if (Importer.IsStructurallyEquivalent(D->getType(),
2763 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002764 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002765 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002766 }
2767
2768 // FIXME: Check for overloading more carefully, e.g., by boosting
2769 // Sema::IsOverload out to the AST library.
2770
2771 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002772 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002773 continue;
2774
2775 // Complain about inconsistent function types.
2776 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002777 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002778 Importer.ToDiag(FoundFunction->getLocation(),
2779 diag::note_odr_value_here)
2780 << FoundFunction->getType();
2781 }
2782 }
2783
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002784 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002785 }
2786
2787 if (!ConflictingDecls.empty()) {
2788 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2789 ConflictingDecls.data(),
2790 ConflictingDecls.size());
2791 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002792 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002793 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002794 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002795
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002796 DeclarationNameInfo NameInfo(Name, Loc);
2797 // Import additional name location/type info.
2798 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2799
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002800 QualType FromTy = D->getType();
2801 bool usedDifferentExceptionSpec = false;
2802
2803 if (const FunctionProtoType *
2804 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2805 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2806 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2807 // FunctionDecl that we are importing the FunctionProtoType for.
2808 // To avoid an infinite recursion when importing, create the FunctionDecl
2809 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002810 if (FromEPI.ExceptionSpec.SourceDecl ||
2811 FromEPI.ExceptionSpec.SourceTemplate ||
2812 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002813 FunctionProtoType::ExtProtoInfo DefaultEPI;
2814 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002815 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002816 usedDifferentExceptionSpec = true;
2817 }
2818 }
2819
Douglas Gregorb4964f72010-02-15 23:54:17 +00002820 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002821 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002822 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002823 return nullptr;
2824
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002825 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002826 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002827 for (auto P : D->params()) {
2828 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002829 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002830 return nullptr;
2831
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002832 Parameters.push_back(ToP);
2833 }
2834
2835 // Create the imported function.
2836 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002837 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002838 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002839 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2840 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2841 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002842 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002843 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002844 FromConstructor->isExplicit(),
2845 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002846 D->isImplicit(),
2847 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002848 } else if (isa<CXXDestructorDecl>(D)) {
2849 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2850 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002851 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002852 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002853 D->isInlineSpecified(),
2854 D->isImplicit());
2855 } else if (CXXConversionDecl *FromConversion
2856 = dyn_cast<CXXConversionDecl>(D)) {
2857 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2858 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002859 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002860 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002861 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002862 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002863 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002864 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002865 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2866 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2867 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002868 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002869 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002870 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002871 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002872 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002873 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002874 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002875 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002876 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002877 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002878 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002879 D->hasWrittenPrototype(),
2880 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002881 }
John McCall3e11ebe2010-03-15 10:12:16 +00002882
2883 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002884 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002885 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002886 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002887 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2888 ToFunction->setTrivial(D->isTrivial());
2889 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002890 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002891
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002892 // Set the parameters.
2893 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002894 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002895 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002896 }
David Blaikie9c70e042011-09-21 18:16:56 +00002897 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002898
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002899 if (usedDifferentExceptionSpec) {
2900 // Update FunctionProtoType::ExtProtoInfo.
2901 QualType T = Importer.Import(D->getType());
2902 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002903 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002904 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002905 }
2906
Sean Callanan59721b32015-04-28 18:41:46 +00002907 // Import the body, if any.
2908 if (Stmt *FromBody = D->getBody()) {
2909 if (Stmt *ToBody = Importer.Import(FromBody)) {
2910 ToFunction->setBody(ToBody);
2911 }
2912 }
2913
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002914 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002915
2916 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002917 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002918
Douglas Gregor43f54792010-02-17 02:12:47 +00002919 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002920}
2921
Douglas Gregor00eace12010-02-21 18:29:16 +00002922Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2923 return VisitFunctionDecl(D);
2924}
2925
2926Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2927 return VisitCXXMethodDecl(D);
2928}
2929
2930Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2931 return VisitCXXMethodDecl(D);
2932}
2933
2934Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2935 return VisitCXXMethodDecl(D);
2936}
2937
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002938static unsigned getFieldIndex(Decl *F) {
2939 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2940 if (!Owner)
2941 return 0;
2942
2943 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002944 for (const auto *D : Owner->noload_decls()) {
2945 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002946 return Index;
2947
2948 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2949 ++Index;
2950 }
2951
2952 return Index;
2953}
2954
Douglas Gregor5c73e912010-02-11 00:48:18 +00002955Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2956 // Import the major distinguishing characteristics of a variable.
2957 DeclContext *DC, *LexicalDC;
2958 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002959 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002960 NamedDecl *ToD;
2961 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002962 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002963 if (ToD)
2964 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002965
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002966 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002967 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002968 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002969 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2970 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002971 // For anonymous fields, match up by index.
2972 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2973 continue;
2974
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002975 if (Importer.IsStructurallyEquivalent(D->getType(),
2976 FoundField->getType())) {
2977 Importer.Imported(D, FoundField);
2978 return FoundField;
2979 }
2980
2981 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2982 << Name << D->getType() << FoundField->getType();
2983 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2984 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002985 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002986 }
2987 }
2988
Douglas Gregorb4964f72010-02-15 23:54:17 +00002989 // Import the type.
2990 QualType T = Importer.Import(D->getType());
2991 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002992 return nullptr;
2993
Douglas Gregor5c73e912010-02-11 00:48:18 +00002994 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2995 Expr *BitWidth = Importer.Import(D->getBitWidth());
2996 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002997 return nullptr;
2998
Abramo Bagnaradff19302011-03-08 08:55:46 +00002999 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3000 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003001 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003002 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003003 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003004 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003005 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00003006 if (ToField->hasInClassInitializer())
3007 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003008 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003009 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003010 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003011 return ToField;
3012}
3013
Francois Pichet783dd6e2010-11-21 06:08:52 +00003014Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3015 // Import the major distinguishing characteristics of a variable.
3016 DeclContext *DC, *LexicalDC;
3017 DeclarationName Name;
3018 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003019 NamedDecl *ToD;
3020 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003021 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003022 if (ToD)
3023 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003024
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003025 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003026 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003027 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003028 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003029 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003030 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003031 // For anonymous indirect fields, match up by index.
3032 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3033 continue;
3034
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003035 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003036 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003037 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003038 Importer.Imported(D, FoundField);
3039 return FoundField;
3040 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003041
3042 // If there are more anonymous fields to check, continue.
3043 if (!Name && I < N-1)
3044 continue;
3045
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003046 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3047 << Name << D->getType() << FoundField->getType();
3048 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3049 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003050 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003051 }
3052 }
3053
Francois Pichet783dd6e2010-11-21 06:08:52 +00003054 // Import the type.
3055 QualType T = Importer.Import(D->getType());
3056 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003057 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003058
3059 NamedDecl **NamedChain =
3060 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3061
3062 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003063 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003064 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003065 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003066 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003067 NamedChain[i++] = cast<NamedDecl>(D);
3068 }
3069
3070 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003071 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3072 NamedChain, D->getChainingSize());
3073
3074 for (const auto *Attr : D->attrs())
3075 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3076
Francois Pichet783dd6e2010-11-21 06:08:52 +00003077 ToIndirectField->setAccess(D->getAccess());
3078 ToIndirectField->setLexicalDeclContext(LexicalDC);
3079 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003080 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003081 return ToIndirectField;
3082}
3083
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003084Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3085 // Import the major distinguishing characteristics of an ivar.
3086 DeclContext *DC, *LexicalDC;
3087 DeclarationName Name;
3088 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003089 NamedDecl *ToD;
3090 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003091 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003092 if (ToD)
3093 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003094
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003095 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003096 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003097 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003098 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3099 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003100 if (Importer.IsStructurallyEquivalent(D->getType(),
3101 FoundIvar->getType())) {
3102 Importer.Imported(D, FoundIvar);
3103 return FoundIvar;
3104 }
3105
3106 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3107 << Name << D->getType() << FoundIvar->getType();
3108 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3109 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003110 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003111 }
3112 }
3113
3114 // Import the type.
3115 QualType T = Importer.Import(D->getType());
3116 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003117 return nullptr;
3118
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003119 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3120 Expr *BitWidth = Importer.Import(D->getBitWidth());
3121 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003122 return nullptr;
3123
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003124 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3125 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003126 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003127 Loc, Name.getAsIdentifierInfo(),
3128 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003129 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003130 ToIvar->setLexicalDeclContext(LexicalDC);
3131 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003132 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003133 return ToIvar;
3134
3135}
3136
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003137Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3138 // Import the major distinguishing characteristics of a variable.
3139 DeclContext *DC, *LexicalDC;
3140 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003141 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003142 NamedDecl *ToD;
3143 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003144 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003145 if (ToD)
3146 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003147
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003148 // Try to find a variable in our own ("to") context with the same name and
3149 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003150 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003151 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003152 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003153 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003154 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003155 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003156 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3157 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003158 continue;
3159
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003160 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003161 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003162 if (FoundVar->hasExternalFormalLinkage() &&
3163 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003164 if (Importer.IsStructurallyEquivalent(D->getType(),
3165 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003166 MergeWithVar = FoundVar;
3167 break;
3168 }
3169
Douglas Gregor56521c52010-02-12 17:23:39 +00003170 const ArrayType *FoundArray
3171 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3172 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003173 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003174 if (FoundArray && TArray) {
3175 if (isa<IncompleteArrayType>(FoundArray) &&
3176 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003177 // Import the type.
3178 QualType T = Importer.Import(D->getType());
3179 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003180 return nullptr;
3181
Douglas Gregor56521c52010-02-12 17:23:39 +00003182 FoundVar->setType(T);
3183 MergeWithVar = FoundVar;
3184 break;
3185 } else if (isa<IncompleteArrayType>(TArray) &&
3186 isa<ConstantArrayType>(FoundArray)) {
3187 MergeWithVar = FoundVar;
3188 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003189 }
3190 }
3191
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003192 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003193 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003194 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3195 << FoundVar->getType();
3196 }
3197 }
3198
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003199 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003200 }
3201
3202 if (MergeWithVar) {
3203 // An equivalent variable with external linkage has been found. Link
3204 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003205 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003206
3207 if (VarDecl *DDef = D->getDefinition()) {
3208 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3209 Importer.ToDiag(ExistingDef->getLocation(),
3210 diag::err_odr_variable_multiple_def)
3211 << Name;
3212 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3213 } else {
3214 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003215 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003216 if (DDef->isInitKnownICE()) {
3217 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3218 Eval->CheckedICE = true;
3219 Eval->IsICE = DDef->isInitICE();
3220 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003221 }
3222 }
3223
3224 return MergeWithVar;
3225 }
3226
3227 if (!ConflictingDecls.empty()) {
3228 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3229 ConflictingDecls.data(),
3230 ConflictingDecls.size());
3231 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003232 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003233 }
3234 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003235
Douglas Gregorb4964f72010-02-15 23:54:17 +00003236 // Import the type.
3237 QualType T = Importer.Import(D->getType());
3238 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003239 return nullptr;
3240
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003241 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003242 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003243 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3244 Importer.Import(D->getInnerLocStart()),
3245 Loc, Name.getAsIdentifierInfo(),
3246 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003247 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003248 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003249 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003250 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003251 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003252 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003253
Sean Callanan59721b32015-04-28 18:41:46 +00003254 if (!D->isFileVarDecl() &&
3255 D->isUsed())
3256 ToVar->setIsUsed();
3257
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003258 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003259 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003260 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003261
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003262 return ToVar;
3263}
3264
Douglas Gregor8b228d72010-02-17 21:22:52 +00003265Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3266 // Parameters are created in the translation unit's context, then moved
3267 // into the function declaration's context afterward.
3268 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3269
3270 // Import the name of this declaration.
3271 DeclarationName Name = Importer.Import(D->getDeclName());
3272 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003273 return nullptr;
3274
Douglas Gregor8b228d72010-02-17 21:22:52 +00003275 // Import the location of this declaration.
3276 SourceLocation Loc = Importer.Import(D->getLocation());
3277
3278 // Import the parameter's type.
3279 QualType T = Importer.Import(D->getType());
3280 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003281 return nullptr;
3282
Douglas Gregor8b228d72010-02-17 21:22:52 +00003283 // Create the imported parameter.
3284 ImplicitParamDecl *ToParm
3285 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3286 Loc, Name.getAsIdentifierInfo(),
3287 T);
3288 return Importer.Imported(D, ToParm);
3289}
3290
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003291Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3292 // Parameters are created in the translation unit's context, then moved
3293 // into the function declaration's context afterward.
3294 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3295
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003296 // Import the name of this declaration.
3297 DeclarationName Name = Importer.Import(D->getDeclName());
3298 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003299 return nullptr;
3300
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003301 // Import the location of this declaration.
3302 SourceLocation Loc = Importer.Import(D->getLocation());
3303
3304 // Import the parameter's type.
3305 QualType T = Importer.Import(D->getType());
3306 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003307 return nullptr;
3308
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003309 // Create the imported parameter.
3310 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3311 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003312 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003313 Loc, Name.getAsIdentifierInfo(),
3314 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003315 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003316 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003317
3318 if (D->isUsed())
3319 ToParm->setIsUsed();
3320
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003321 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003322}
3323
Douglas Gregor43f54792010-02-17 02:12:47 +00003324Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3325 // Import the major distinguishing characteristics of a method.
3326 DeclContext *DC, *LexicalDC;
3327 DeclarationName Name;
3328 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003329 NamedDecl *ToD;
3330 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003331 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003332 if (ToD)
3333 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003334
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003335 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003336 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003337 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3338 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003339 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3340 continue;
3341
3342 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003343 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3344 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003345 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003346 << D->isInstanceMethod() << Name << D->getReturnType()
3347 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003348 Importer.ToDiag(FoundMethod->getLocation(),
3349 diag::note_odr_objc_method_here)
3350 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003351 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003352 }
3353
3354 // Check the number of parameters.
3355 if (D->param_size() != FoundMethod->param_size()) {
3356 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3357 << D->isInstanceMethod() << Name
3358 << D->param_size() << FoundMethod->param_size();
3359 Importer.ToDiag(FoundMethod->getLocation(),
3360 diag::note_odr_objc_method_here)
3361 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003362 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003363 }
3364
3365 // Check parameter types.
3366 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3367 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3368 P != PEnd; ++P, ++FoundP) {
3369 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3370 (*FoundP)->getType())) {
3371 Importer.FromDiag((*P)->getLocation(),
3372 diag::err_odr_objc_method_param_type_inconsistent)
3373 << D->isInstanceMethod() << Name
3374 << (*P)->getType() << (*FoundP)->getType();
3375 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3376 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003377 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003378 }
3379 }
3380
3381 // Check variadic/non-variadic.
3382 // Check the number of parameters.
3383 if (D->isVariadic() != FoundMethod->isVariadic()) {
3384 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3385 << D->isInstanceMethod() << Name;
3386 Importer.ToDiag(FoundMethod->getLocation(),
3387 diag::note_odr_objc_method_here)
3388 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003389 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003390 }
3391
3392 // FIXME: Any other bits we need to merge?
3393 return Importer.Imported(D, FoundMethod);
3394 }
3395 }
3396
3397 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003398 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003399 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003400 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003401
Alp Toker314cc812014-01-25 16:55:45 +00003402 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003403
Alp Toker314cc812014-01-25 16:55:45 +00003404 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3405 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3406 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3407 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3408 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003409
3410 // FIXME: When we decide to merge method definitions, we'll need to
3411 // deal with implicit parameters.
3412
3413 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003414 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003415 for (auto *FromP : D->params()) {
3416 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003417 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003418 return nullptr;
3419
Douglas Gregor43f54792010-02-17 02:12:47 +00003420 ToParams.push_back(ToP);
3421 }
3422
3423 // Set the parameters.
3424 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3425 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003426 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003427 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003428 SmallVector<SourceLocation, 12> SelLocs;
3429 D->getSelectorLocs(SelLocs);
3430 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003431
3432 ToMethod->setLexicalDeclContext(LexicalDC);
3433 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003434 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003435 return ToMethod;
3436}
3437
Douglas Gregor85f3f952015-07-07 03:57:15 +00003438Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3439 // Import the major distinguishing characteristics of a category.
3440 DeclContext *DC, *LexicalDC;
3441 DeclarationName Name;
3442 SourceLocation Loc;
3443 NamedDecl *ToD;
3444 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3445 return nullptr;
3446 if (ToD)
3447 return ToD;
3448
3449 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3450 if (!BoundInfo)
3451 return nullptr;
3452
3453 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3454 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003455 D->getVariance(),
3456 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003457 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003458 Importer.Import(D->getLocation()),
3459 Name.getAsIdentifierInfo(),
3460 Importer.Import(D->getColonLoc()),
3461 BoundInfo);
3462 Importer.Imported(D, Result);
3463 Result->setLexicalDeclContext(LexicalDC);
3464 return Result;
3465}
3466
Douglas Gregor84c51c32010-02-18 01:47:50 +00003467Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3468 // Import the major distinguishing characteristics of a category.
3469 DeclContext *DC, *LexicalDC;
3470 DeclarationName Name;
3471 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003472 NamedDecl *ToD;
3473 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003474 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003475 if (ToD)
3476 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003477
Douglas Gregor84c51c32010-02-18 01:47:50 +00003478 ObjCInterfaceDecl *ToInterface
3479 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3480 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003481 return nullptr;
3482
Douglas Gregor84c51c32010-02-18 01:47:50 +00003483 // Determine if we've already encountered this category.
3484 ObjCCategoryDecl *MergeWithCategory
3485 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3486 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3487 if (!ToCategory) {
3488 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003489 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003490 Loc,
3491 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003492 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003493 ToInterface,
Douglas Gregor85f3f952015-07-07 03:57:15 +00003494 ImportObjCTypeParamList(
3495 D->getTypeParamList()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003496 Importer.Import(D->getIvarLBraceLoc()),
3497 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003498 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003499 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003500 Importer.Imported(D, ToCategory);
3501
Douglas Gregor84c51c32010-02-18 01:47:50 +00003502 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003503 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3504 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003505 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3506 = D->protocol_loc_begin();
3507 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3508 FromProtoEnd = D->protocol_end();
3509 FromProto != FromProtoEnd;
3510 ++FromProto, ++FromProtoLoc) {
3511 ObjCProtocolDecl *ToProto
3512 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3513 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003514 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003515 Protocols.push_back(ToProto);
3516 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3517 }
3518
3519 // FIXME: If we're merging, make sure that the protocol list is the same.
3520 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3521 ProtocolLocs.data(), Importer.getToContext());
3522
3523 } else {
3524 Importer.Imported(D, ToCategory);
3525 }
3526
3527 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003528 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003529
3530 // If we have an implementation, import it as well.
3531 if (D->getImplementation()) {
3532 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003533 = cast_or_null<ObjCCategoryImplDecl>(
3534 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003535 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003536 return nullptr;
3537
Douglas Gregor84c51c32010-02-18 01:47:50 +00003538 ToCategory->setImplementation(Impl);
3539 }
3540
3541 return ToCategory;
3542}
3543
Douglas Gregor2aa53772012-01-24 17:42:07 +00003544bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3545 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003546 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003547 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003548 if (shouldForceImportDeclContext(Kind))
3549 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003550 return false;
3551 }
3552
3553 // Start the protocol definition
3554 To->startDefinition();
3555
3556 // Import protocols
3557 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3558 SmallVector<SourceLocation, 4> ProtocolLocs;
3559 ObjCProtocolDecl::protocol_loc_iterator
3560 FromProtoLoc = From->protocol_loc_begin();
3561 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3562 FromProtoEnd = From->protocol_end();
3563 FromProto != FromProtoEnd;
3564 ++FromProto, ++FromProtoLoc) {
3565 ObjCProtocolDecl *ToProto
3566 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3567 if (!ToProto)
3568 return true;
3569 Protocols.push_back(ToProto);
3570 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3571 }
3572
3573 // FIXME: If we're merging, make sure that the protocol list is the same.
3574 To->setProtocolList(Protocols.data(), Protocols.size(),
3575 ProtocolLocs.data(), Importer.getToContext());
3576
Douglas Gregor2e15c842012-02-01 21:00:38 +00003577 if (shouldForceImportDeclContext(Kind)) {
3578 // Import all of the members of this protocol.
3579 ImportDeclContext(From, /*ForceImport=*/true);
3580 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003581 return false;
3582}
3583
Douglas Gregor98d156a2010-02-17 16:12:00 +00003584Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003585 // If this protocol has a definition in the translation unit we're coming
3586 // from, but this particular declaration is not that definition, import the
3587 // definition and map to that.
3588 ObjCProtocolDecl *Definition = D->getDefinition();
3589 if (Definition && Definition != D) {
3590 Decl *ImportedDef = Importer.Import(Definition);
3591 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003592 return nullptr;
3593
Douglas Gregor2aa53772012-01-24 17:42:07 +00003594 return Importer.Imported(D, ImportedDef);
3595 }
3596
Douglas Gregor84c51c32010-02-18 01:47:50 +00003597 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003598 DeclContext *DC, *LexicalDC;
3599 DeclarationName Name;
3600 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003601 NamedDecl *ToD;
3602 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003603 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003604 if (ToD)
3605 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003606
Craig Topper36250ad2014-05-12 05:36:57 +00003607 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003608 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003609 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003610 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3611 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003612 continue;
3613
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003614 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003615 break;
3616 }
3617
3618 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003619 if (!ToProto) {
3620 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3621 Name.getAsIdentifierInfo(), Loc,
3622 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003623 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003624 ToProto->setLexicalDeclContext(LexicalDC);
3625 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003626 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003627
3628 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003629
Douglas Gregor2aa53772012-01-24 17:42:07 +00003630 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003631 return nullptr;
3632
Douglas Gregor98d156a2010-02-17 16:12:00 +00003633 return ToProto;
3634}
3635
Sean Callanan0aae0412014-12-10 00:00:37 +00003636Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3637 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3638 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3639
3640 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3641 SourceLocation LangLoc = Importer.Import(D->getLocation());
3642
3643 bool HasBraces = D->hasBraces();
3644
Sean Callananb12a8552014-12-10 21:22:20 +00003645 LinkageSpecDecl *ToLinkageSpec =
3646 LinkageSpecDecl::Create(Importer.getToContext(),
3647 DC,
3648 ExternLoc,
3649 LangLoc,
3650 D->getLanguage(),
3651 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003652
3653 if (HasBraces) {
3654 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3655 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3656 }
3657
3658 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3659 LexicalDC->addDeclInternal(ToLinkageSpec);
3660
3661 Importer.Imported(D, ToLinkageSpec);
3662
3663 return ToLinkageSpec;
3664}
3665
Douglas Gregor2aa53772012-01-24 17:42:07 +00003666bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3667 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003668 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003669 if (To->getDefinition()) {
3670 // Check consistency of superclass.
3671 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3672 if (FromSuper) {
3673 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3674 if (!FromSuper)
3675 return true;
3676 }
3677
3678 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3679 if ((bool)FromSuper != (bool)ToSuper ||
3680 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3681 Importer.ToDiag(To->getLocation(),
3682 diag::err_odr_objc_superclass_inconsistent)
3683 << To->getDeclName();
3684 if (ToSuper)
3685 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3686 << To->getSuperClass()->getDeclName();
3687 else
3688 Importer.ToDiag(To->getLocation(),
3689 diag::note_odr_objc_missing_superclass);
3690 if (From->getSuperClass())
3691 Importer.FromDiag(From->getSuperClassLoc(),
3692 diag::note_odr_objc_superclass)
3693 << From->getSuperClass()->getDeclName();
3694 else
3695 Importer.FromDiag(From->getLocation(),
3696 diag::note_odr_objc_missing_superclass);
3697 }
3698
Douglas Gregor2e15c842012-02-01 21:00:38 +00003699 if (shouldForceImportDeclContext(Kind))
3700 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003701 return false;
3702 }
3703
3704 // Start the definition.
3705 To->startDefinition();
3706
3707 // If this class has a superclass, import it.
3708 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003709 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3710 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003711 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003712
3713 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003714 }
3715
3716 // Import protocols
3717 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3718 SmallVector<SourceLocation, 4> ProtocolLocs;
3719 ObjCInterfaceDecl::protocol_loc_iterator
3720 FromProtoLoc = From->protocol_loc_begin();
3721
3722 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3723 FromProtoEnd = From->protocol_end();
3724 FromProto != FromProtoEnd;
3725 ++FromProto, ++FromProtoLoc) {
3726 ObjCProtocolDecl *ToProto
3727 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3728 if (!ToProto)
3729 return true;
3730 Protocols.push_back(ToProto);
3731 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3732 }
3733
3734 // FIXME: If we're merging, make sure that the protocol list is the same.
3735 To->setProtocolList(Protocols.data(), Protocols.size(),
3736 ProtocolLocs.data(), Importer.getToContext());
3737
3738 // Import categories. When the categories themselves are imported, they'll
3739 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003740 for (auto *Cat : From->known_categories())
3741 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003742
Douglas Gregor2aa53772012-01-24 17:42:07 +00003743 // If we have an @implementation, import it as well.
3744 if (From->getImplementation()) {
3745 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3746 Importer.Import(From->getImplementation()));
3747 if (!Impl)
3748 return true;
3749
3750 To->setImplementation(Impl);
3751 }
3752
Douglas Gregor2e15c842012-02-01 21:00:38 +00003753 if (shouldForceImportDeclContext(Kind)) {
3754 // Import all of the members of this class.
3755 ImportDeclContext(From, /*ForceImport=*/true);
3756 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003757 return false;
3758}
3759
Douglas Gregor85f3f952015-07-07 03:57:15 +00003760ObjCTypeParamList *
3761ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3762 if (!list)
3763 return nullptr;
3764
3765 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3766 for (auto fromTypeParam : *list) {
3767 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3768 Importer.Import(fromTypeParam));
3769 if (!toTypeParam)
3770 return nullptr;
3771
3772 toTypeParams.push_back(toTypeParam);
3773 }
3774
3775 return ObjCTypeParamList::create(Importer.getToContext(),
3776 Importer.Import(list->getLAngleLoc()),
3777 toTypeParams,
3778 Importer.Import(list->getRAngleLoc()));
3779}
3780
Douglas Gregor45635322010-02-16 01:20:57 +00003781Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003782 // If this class has a definition in the translation unit we're coming from,
3783 // but this particular declaration is not that definition, import the
3784 // definition and map to that.
3785 ObjCInterfaceDecl *Definition = D->getDefinition();
3786 if (Definition && Definition != D) {
3787 Decl *ImportedDef = Importer.Import(Definition);
3788 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003789 return nullptr;
3790
Douglas Gregor2aa53772012-01-24 17:42:07 +00003791 return Importer.Imported(D, ImportedDef);
3792 }
3793
Douglas Gregor45635322010-02-16 01:20:57 +00003794 // Import the major distinguishing characteristics of an @interface.
3795 DeclContext *DC, *LexicalDC;
3796 DeclarationName Name;
3797 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003798 NamedDecl *ToD;
3799 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003800 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003801 if (ToD)
3802 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003803
Douglas Gregor2aa53772012-01-24 17:42:07 +00003804 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003805 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003806 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003807 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003808 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3809 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003810 continue;
3811
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003812 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003813 break;
3814 }
3815
Douglas Gregor2aa53772012-01-24 17:42:07 +00003816 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003817 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003818 if (!ToIface) {
3819 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3820 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003821 Name.getAsIdentifierInfo(),
3822 ImportObjCTypeParamList(
3823 D->getTypeParamListAsWritten()),
Craig Topper36250ad2014-05-12 05:36:57 +00003824 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003825 D->isImplicitInterfaceDecl());
3826 ToIface->setLexicalDeclContext(LexicalDC);
3827 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003828 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003829 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003830
Douglas Gregor2aa53772012-01-24 17:42:07 +00003831 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003832 return nullptr;
3833
Douglas Gregor98d156a2010-02-17 16:12:00 +00003834 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003835}
3836
Douglas Gregor4da9d682010-12-07 15:32:12 +00003837Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3838 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3839 Importer.Import(D->getCategoryDecl()));
3840 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003841 return nullptr;
3842
Douglas Gregor4da9d682010-12-07 15:32:12 +00003843 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3844 if (!ToImpl) {
3845 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3846 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003847 return nullptr;
3848
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003849 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003850 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003851 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003852 Category->getClassInterface(),
3853 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003854 Importer.Import(D->getAtStartLoc()),
3855 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003856
3857 DeclContext *LexicalDC = DC;
3858 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3859 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3860 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003861 return nullptr;
3862
Douglas Gregor4da9d682010-12-07 15:32:12 +00003863 ToImpl->setLexicalDeclContext(LexicalDC);
3864 }
3865
Sean Callanan95e74be2011-10-21 02:57:43 +00003866 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003867 Category->setImplementation(ToImpl);
3868 }
3869
3870 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003871 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003872 return ToImpl;
3873}
3874
Douglas Gregorda8025c2010-12-07 01:26:03 +00003875Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3876 // Find the corresponding interface.
3877 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3878 Importer.Import(D->getClassInterface()));
3879 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003880 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003881
3882 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003883 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003884 if (D->getSuperClass()) {
3885 Super = cast_or_null<ObjCInterfaceDecl>(
3886 Importer.Import(D->getSuperClass()));
3887 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003888 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003889 }
3890
3891 ObjCImplementationDecl *Impl = Iface->getImplementation();
3892 if (!Impl) {
3893 // We haven't imported an implementation yet. Create a new @implementation
3894 // now.
3895 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3896 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003897 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003898 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003899 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003900 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003901 Importer.Import(D->getIvarLBraceLoc()),
3902 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003903
3904 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3905 DeclContext *LexicalDC
3906 = Importer.ImportContext(D->getLexicalDeclContext());
3907 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003908 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003909 Impl->setLexicalDeclContext(LexicalDC);
3910 }
3911
3912 // Associate the implementation with the class it implements.
3913 Iface->setImplementation(Impl);
3914 Importer.Imported(D, Iface->getImplementation());
3915 } else {
3916 Importer.Imported(D, Iface->getImplementation());
3917
3918 // Verify that the existing @implementation has the same superclass.
3919 if ((Super && !Impl->getSuperClass()) ||
3920 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003921 (Super && Impl->getSuperClass() &&
3922 !declaresSameEntity(Super->getCanonicalDecl(),
3923 Impl->getSuperClass()))) {
3924 Importer.ToDiag(Impl->getLocation(),
3925 diag::err_odr_objc_superclass_inconsistent)
3926 << Iface->getDeclName();
3927 // FIXME: It would be nice to have the location of the superclass
3928 // below.
3929 if (Impl->getSuperClass())
3930 Importer.ToDiag(Impl->getLocation(),
3931 diag::note_odr_objc_superclass)
3932 << Impl->getSuperClass()->getDeclName();
3933 else
3934 Importer.ToDiag(Impl->getLocation(),
3935 diag::note_odr_objc_missing_superclass);
3936 if (D->getSuperClass())
3937 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003938 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003939 << D->getSuperClass()->getDeclName();
3940 else
3941 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003942 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003943 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003944 }
3945 }
3946
3947 // Import all of the members of this @implementation.
3948 ImportDeclContext(D);
3949
3950 return Impl;
3951}
3952
Douglas Gregora11c4582010-02-17 18:02:10 +00003953Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3954 // Import the major distinguishing characteristics of an @property.
3955 DeclContext *DC, *LexicalDC;
3956 DeclarationName Name;
3957 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003958 NamedDecl *ToD;
3959 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003960 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003961 if (ToD)
3962 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003963
3964 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003965 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003966 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003967 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003968 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003969 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003970 // Check property types.
3971 if (!Importer.IsStructurallyEquivalent(D->getType(),
3972 FoundProp->getType())) {
3973 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3974 << Name << D->getType() << FoundProp->getType();
3975 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3976 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003977 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003978 }
3979
3980 // FIXME: Check property attributes, getters, setters, etc.?
3981
3982 // Consider these properties to be equivalent.
3983 Importer.Imported(D, FoundProp);
3984 return FoundProp;
3985 }
3986 }
3987
3988 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003989 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3990 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003991 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003992
3993 // Create the new property.
3994 ObjCPropertyDecl *ToProperty
3995 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3996 Name.getAsIdentifierInfo(),
3997 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003998 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003999 Importer.Import(D->getType()),
4000 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004001 D->getPropertyImplementation());
4002 Importer.Imported(D, ToProperty);
4003 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004004 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004005
4006 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004007 ToProperty->setPropertyAttributesAsWritten(
4008 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004009 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4010 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4011 ToProperty->setGetterMethodDecl(
4012 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4013 ToProperty->setSetterMethodDecl(
4014 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4015 ToProperty->setPropertyIvarDecl(
4016 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4017 return ToProperty;
4018}
4019
Douglas Gregor14a49e22010-12-07 18:32:03 +00004020Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4021 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4022 Importer.Import(D->getPropertyDecl()));
4023 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004024 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004025
4026 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4027 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004028 return nullptr;
4029
Douglas Gregor14a49e22010-12-07 18:32:03 +00004030 // Import the lexical declaration context.
4031 DeclContext *LexicalDC = DC;
4032 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4033 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4034 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004035 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004036 }
4037
4038 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4039 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004040 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004041
4042 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004043 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004044 if (D->getPropertyIvarDecl()) {
4045 Ivar = cast_or_null<ObjCIvarDecl>(
4046 Importer.Import(D->getPropertyIvarDecl()));
4047 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004048 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004049 }
4050
4051 ObjCPropertyImplDecl *ToImpl
4052 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
4053 if (!ToImpl) {
4054 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4055 Importer.Import(D->getLocStart()),
4056 Importer.Import(D->getLocation()),
4057 Property,
4058 D->getPropertyImplementation(),
4059 Ivar,
4060 Importer.Import(D->getPropertyIvarDeclLoc()));
4061 ToImpl->setLexicalDeclContext(LexicalDC);
4062 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004063 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004064 } else {
4065 // Check that we have the same kind of property implementation (@synthesize
4066 // vs. @dynamic).
4067 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4068 Importer.ToDiag(ToImpl->getLocation(),
4069 diag::err_odr_objc_property_impl_kind_inconsistent)
4070 << Property->getDeclName()
4071 << (ToImpl->getPropertyImplementation()
4072 == ObjCPropertyImplDecl::Dynamic);
4073 Importer.FromDiag(D->getLocation(),
4074 diag::note_odr_objc_property_impl_kind)
4075 << D->getPropertyDecl()->getDeclName()
4076 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004077 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004078 }
4079
4080 // For @synthesize, check that we have the same
4081 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4082 Ivar != ToImpl->getPropertyIvarDecl()) {
4083 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4084 diag::err_odr_objc_synthesize_ivar_inconsistent)
4085 << Property->getDeclName()
4086 << ToImpl->getPropertyIvarDecl()->getDeclName()
4087 << Ivar->getDeclName();
4088 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4089 diag::note_odr_objc_synthesize_ivar_here)
4090 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004091 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004092 }
4093
4094 // Merge the existing implementation with the new implementation.
4095 Importer.Imported(D, ToImpl);
4096 }
4097
4098 return ToImpl;
4099}
4100
Douglas Gregora082a492010-11-30 19:14:50 +00004101Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4102 // For template arguments, we adopt the translation unit as our declaration
4103 // context. This context will be fixed when the actual template declaration
4104 // is created.
4105
4106 // FIXME: Import default argument.
4107 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4108 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004109 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004110 Importer.Import(D->getLocation()),
4111 D->getDepth(),
4112 D->getIndex(),
4113 Importer.Import(D->getIdentifier()),
4114 D->wasDeclaredWithTypename(),
4115 D->isParameterPack());
4116}
4117
4118Decl *
4119ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4120 // Import the name of this declaration.
4121 DeclarationName Name = Importer.Import(D->getDeclName());
4122 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004123 return nullptr;
4124
Douglas Gregora082a492010-11-30 19:14:50 +00004125 // Import the location of this declaration.
4126 SourceLocation Loc = Importer.Import(D->getLocation());
4127
4128 // Import the type of this declaration.
4129 QualType T = Importer.Import(D->getType());
4130 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004131 return nullptr;
4132
Douglas Gregora082a492010-11-30 19:14:50 +00004133 // Import type-source information.
4134 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4135 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004136 return nullptr;
4137
Douglas Gregora082a492010-11-30 19:14:50 +00004138 // FIXME: Import default argument.
4139
4140 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4141 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004142 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004143 Loc, D->getDepth(), D->getPosition(),
4144 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004145 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004146}
4147
4148Decl *
4149ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4150 // Import the name of this declaration.
4151 DeclarationName Name = Importer.Import(D->getDeclName());
4152 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004153 return nullptr;
4154
Douglas Gregora082a492010-11-30 19:14:50 +00004155 // Import the location of this declaration.
4156 SourceLocation Loc = Importer.Import(D->getLocation());
4157
4158 // Import template parameters.
4159 TemplateParameterList *TemplateParams
4160 = ImportTemplateParameterList(D->getTemplateParameters());
4161 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
4163
Douglas Gregora082a492010-11-30 19:14:50 +00004164 // FIXME: Import default argument.
4165
4166 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4167 Importer.getToContext().getTranslationUnitDecl(),
4168 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004169 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004170 Name.getAsIdentifierInfo(),
4171 TemplateParams);
4172}
4173
4174Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4175 // If this record has a definition in the translation unit we're coming from,
4176 // but this particular declaration is not that definition, import the
4177 // definition and map to that.
4178 CXXRecordDecl *Definition
4179 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4180 if (Definition && Definition != D->getTemplatedDecl()) {
4181 Decl *ImportedDef
4182 = Importer.Import(Definition->getDescribedClassTemplate());
4183 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004184 return nullptr;
4185
Douglas Gregora082a492010-11-30 19:14:50 +00004186 return Importer.Imported(D, ImportedDef);
4187 }
4188
4189 // Import the major distinguishing characteristics of this class template.
4190 DeclContext *DC, *LexicalDC;
4191 DeclarationName Name;
4192 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004193 NamedDecl *ToD;
4194 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004195 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004196 if (ToD)
4197 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004198
Douglas Gregora082a492010-11-30 19:14:50 +00004199 // We may already have a template of the same name; try to find and match it.
4200 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004201 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004202 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004203 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004204 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4205 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004206 continue;
4207
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004208 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004209 if (ClassTemplateDecl *FoundTemplate
4210 = dyn_cast<ClassTemplateDecl>(Found)) {
4211 if (IsStructuralMatch(D, FoundTemplate)) {
4212 // The class templates structurally match; call it the same template.
4213 // FIXME: We may be filling in a forward declaration here. Handle
4214 // this case!
4215 Importer.Imported(D->getTemplatedDecl(),
4216 FoundTemplate->getTemplatedDecl());
4217 return Importer.Imported(D, FoundTemplate);
4218 }
4219 }
4220
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004221 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004222 }
4223
4224 if (!ConflictingDecls.empty()) {
4225 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4226 ConflictingDecls.data(),
4227 ConflictingDecls.size());
4228 }
4229
4230 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004231 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004232 }
4233
4234 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4235
4236 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004237 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4238 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004239 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4240 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004241 DC, StartLoc, IdLoc,
4242 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004243 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004244 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004245 D2Templated->setLexicalDeclContext(LexicalDC);
4246
4247 // Create the class template declaration itself.
4248 TemplateParameterList *TemplateParams
4249 = ImportTemplateParameterList(D->getTemplateParameters());
4250 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004251 return nullptr;
4252
Douglas Gregora082a492010-11-30 19:14:50 +00004253 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4254 Loc, Name, TemplateParams,
4255 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004256 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004257 D2Templated->setDescribedClassTemplate(D2);
4258
4259 D2->setAccess(D->getAccess());
4260 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004261 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004262
4263 // Note the relationship between the class templates.
4264 Importer.Imported(D, D2);
4265 Importer.Imported(DTemplated, D2Templated);
4266
John McCallf937c022011-10-07 06:10:15 +00004267 if (DTemplated->isCompleteDefinition() &&
4268 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004269 // FIXME: Import definition!
4270 }
4271
4272 return D2;
4273}
4274
Douglas Gregore2e50d332010-12-01 01:36:18 +00004275Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4276 ClassTemplateSpecializationDecl *D) {
4277 // If this record has a definition in the translation unit we're coming from,
4278 // but this particular declaration is not that definition, import the
4279 // definition and map to that.
4280 TagDecl *Definition = D->getDefinition();
4281 if (Definition && Definition != D) {
4282 Decl *ImportedDef = Importer.Import(Definition);
4283 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004284 return nullptr;
4285
Douglas Gregore2e50d332010-12-01 01:36:18 +00004286 return Importer.Imported(D, ImportedDef);
4287 }
4288
4289 ClassTemplateDecl *ClassTemplate
4290 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4291 D->getSpecializedTemplate()));
4292 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004293 return nullptr;
4294
Douglas Gregore2e50d332010-12-01 01:36:18 +00004295 // Import the context of this declaration.
4296 DeclContext *DC = ClassTemplate->getDeclContext();
4297 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004298 return nullptr;
4299
Douglas Gregore2e50d332010-12-01 01:36:18 +00004300 DeclContext *LexicalDC = DC;
4301 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4302 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4303 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004304 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004305 }
4306
4307 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004308 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4309 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004310
4311 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004312 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004313 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4314 D->getTemplateArgs().size(),
4315 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004316 return nullptr;
4317
Douglas Gregore2e50d332010-12-01 01:36:18 +00004318 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004319 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004320 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004321 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004322 if (D2) {
4323 // We already have a class template specialization with these template
4324 // arguments.
4325
4326 // FIXME: Check for specialization vs. instantiation errors.
4327
4328 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004329 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004330 // The record types structurally match, or the "from" translation
4331 // unit only had a forward declaration anyway; call it the same
4332 // function.
4333 return Importer.Imported(D, FoundDef);
4334 }
4335 }
4336 } else {
4337 // Create a new specialization.
4338 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4339 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004340 StartLoc, IdLoc,
4341 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004342 TemplateArgs.data(),
4343 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004344 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004345 D2->setSpecializationKind(D->getSpecializationKind());
4346
4347 // Add this specialization to the class template.
4348 ClassTemplate->AddSpecialization(D2, InsertPos);
4349
4350 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004351 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004352
4353 // Add the specialization to this context.
4354 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004355 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004356 }
4357 Importer.Imported(D, D2);
4358
John McCallf937c022011-10-07 06:10:15 +00004359 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004360 return nullptr;
4361
Douglas Gregore2e50d332010-12-01 01:36:18 +00004362 return D2;
4363}
4364
Larisse Voufo39a1e502013-08-06 01:03:05 +00004365Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4366 // If this variable has a definition in the translation unit we're coming
4367 // from,
4368 // but this particular declaration is not that definition, import the
4369 // definition and map to that.
4370 VarDecl *Definition =
4371 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4372 if (Definition && Definition != D->getTemplatedDecl()) {
4373 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4374 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004375 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004376
4377 return Importer.Imported(D, ImportedDef);
4378 }
4379
4380 // Import the major distinguishing characteristics of this variable template.
4381 DeclContext *DC, *LexicalDC;
4382 DeclarationName Name;
4383 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004384 NamedDecl *ToD;
4385 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004386 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004387 if (ToD)
4388 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004389
4390 // We may already have a template of the same name; try to find and match it.
4391 assert(!DC->isFunctionOrMethod() &&
4392 "Variable templates cannot be declared at function scope");
4393 SmallVector<NamedDecl *, 4> ConflictingDecls;
4394 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004395 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004396 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4397 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4398 continue;
4399
4400 Decl *Found = FoundDecls[I];
4401 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4402 if (IsStructuralMatch(D, FoundTemplate)) {
4403 // The variable templates structurally match; call it the same template.
4404 Importer.Imported(D->getTemplatedDecl(),
4405 FoundTemplate->getTemplatedDecl());
4406 return Importer.Imported(D, FoundTemplate);
4407 }
4408 }
4409
4410 ConflictingDecls.push_back(FoundDecls[I]);
4411 }
4412
4413 if (!ConflictingDecls.empty()) {
4414 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4415 ConflictingDecls.data(),
4416 ConflictingDecls.size());
4417 }
4418
4419 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004420 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004421
4422 VarDecl *DTemplated = D->getTemplatedDecl();
4423
4424 // Import the type.
4425 QualType T = Importer.Import(DTemplated->getType());
4426 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004427 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004428
4429 // Create the declaration that is being templated.
4430 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4431 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4432 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4433 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4434 IdLoc, Name.getAsIdentifierInfo(), T,
4435 TInfo, DTemplated->getStorageClass());
4436 D2Templated->setAccess(DTemplated->getAccess());
4437 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4438 D2Templated->setLexicalDeclContext(LexicalDC);
4439
4440 // Importer.Imported(DTemplated, D2Templated);
4441 // LexicalDC->addDeclInternal(D2Templated);
4442
4443 // Merge the initializer.
4444 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004445 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004446
4447 // Create the variable template declaration itself.
4448 TemplateParameterList *TemplateParams =
4449 ImportTemplateParameterList(D->getTemplateParameters());
4450 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004451 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004452
4453 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004454 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004455 D2Templated->setDescribedVarTemplate(D2);
4456
4457 D2->setAccess(D->getAccess());
4458 D2->setLexicalDeclContext(LexicalDC);
4459 LexicalDC->addDeclInternal(D2);
4460
4461 // Note the relationship between the variable templates.
4462 Importer.Imported(D, D2);
4463 Importer.Imported(DTemplated, D2Templated);
4464
4465 if (DTemplated->isThisDeclarationADefinition() &&
4466 !D2Templated->isThisDeclarationADefinition()) {
4467 // FIXME: Import definition!
4468 }
4469
4470 return D2;
4471}
4472
4473Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4474 VarTemplateSpecializationDecl *D) {
4475 // If this record has a definition in the translation unit we're coming from,
4476 // but this particular declaration is not that definition, import the
4477 // definition and map to that.
4478 VarDecl *Definition = D->getDefinition();
4479 if (Definition && Definition != D) {
4480 Decl *ImportedDef = Importer.Import(Definition);
4481 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004482 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004483
4484 return Importer.Imported(D, ImportedDef);
4485 }
4486
4487 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4488 Importer.Import(D->getSpecializedTemplate()));
4489 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004490 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004491
4492 // Import the context of this declaration.
4493 DeclContext *DC = VarTemplate->getDeclContext();
4494 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004495 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004496
4497 DeclContext *LexicalDC = DC;
4498 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4499 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4500 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004501 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004502 }
4503
4504 // Import the location of this declaration.
4505 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4506 SourceLocation IdLoc = Importer.Import(D->getLocation());
4507
4508 // Import template arguments.
4509 SmallVector<TemplateArgument, 2> TemplateArgs;
4510 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4511 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004512 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004513
4514 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004515 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004516 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004517 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004518 if (D2) {
4519 // We already have a variable template specialization with these template
4520 // arguments.
4521
4522 // FIXME: Check for specialization vs. instantiation errors.
4523
4524 if (VarDecl *FoundDef = D2->getDefinition()) {
4525 if (!D->isThisDeclarationADefinition() ||
4526 IsStructuralMatch(D, FoundDef)) {
4527 // The record types structurally match, or the "from" translation
4528 // unit only had a forward declaration anyway; call it the same
4529 // variable.
4530 return Importer.Imported(D, FoundDef);
4531 }
4532 }
4533 } else {
4534
4535 // Import the type.
4536 QualType T = Importer.Import(D->getType());
4537 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004538 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004539 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4540
4541 // Create a new specialization.
4542 D2 = VarTemplateSpecializationDecl::Create(
4543 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4544 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4545 D2->setSpecializationKind(D->getSpecializationKind());
4546 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4547
4548 // Add this specialization to the class template.
4549 VarTemplate->AddSpecialization(D2, InsertPos);
4550
4551 // Import the qualifier, if any.
4552 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4553
4554 // Add the specialization to this context.
4555 D2->setLexicalDeclContext(LexicalDC);
4556 LexicalDC->addDeclInternal(D2);
4557 }
4558 Importer.Imported(D, D2);
4559
4560 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004561 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004562
4563 return D2;
4564}
4565
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004566//----------------------------------------------------------------------------
4567// Import Statements
4568//----------------------------------------------------------------------------
4569
Sean Callanan59721b32015-04-28 18:41:46 +00004570DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4571 if (DG.isNull())
4572 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4573 size_t NumDecls = DG.end() - DG.begin();
4574 SmallVector<Decl *, 1> ToDecls(NumDecls);
4575 auto &_Importer = this->Importer;
4576 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4577 [&_Importer](Decl *D) -> Decl * {
4578 return _Importer.Import(D);
4579 });
4580 return DeclGroupRef::Create(Importer.getToContext(),
4581 ToDecls.begin(),
4582 NumDecls);
4583}
4584
4585 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4586 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4587 << S->getStmtClassName();
4588 return nullptr;
4589 }
4590
4591Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4592 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4593 for (Decl *ToD : ToDG) {
4594 if (!ToD)
4595 return nullptr;
4596 }
4597 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4598 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4599 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4600}
4601
4602Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4603 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4604 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4605 S->hasLeadingEmptyMacro());
4606}
4607
4608Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4609 SmallVector<Stmt *, 4> ToStmts(S->size());
4610 auto &_Importer = this->Importer;
4611 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4612 [&_Importer](Stmt *CS) -> Stmt * {
4613 return _Importer.Import(CS);
4614 });
4615 for (Stmt *ToS : ToStmts) {
4616 if (!ToS)
4617 return nullptr;
4618 }
4619 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4620 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4621 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4622 ToStmts,
4623 ToLBraceLoc, ToRBraceLoc);
4624}
4625
4626Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4627 Expr *ToLHS = Importer.Import(S->getLHS());
4628 if (!ToLHS)
4629 return nullptr;
4630 Expr *ToRHS = Importer.Import(S->getRHS());
4631 if (!ToRHS && S->getRHS())
4632 return nullptr;
4633 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4634 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4635 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4636 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4637 ToCaseLoc, ToEllipsisLoc,
4638 ToColonLoc);
4639}
4640
4641Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4642 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4643 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4644 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4645 if (!ToSubStmt && S->getSubStmt())
4646 return nullptr;
4647 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4648 ToSubStmt);
4649}
4650
4651Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4652 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4653 LabelDecl *ToLabelDecl =
4654 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4655 if (!ToLabelDecl && S->getDecl())
4656 return nullptr;
4657 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4658 if (!ToSubStmt && S->getSubStmt())
4659 return nullptr;
4660 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4661 ToSubStmt);
4662}
4663
4664Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4665 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4666 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4667 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4668 ASTContext &_ToContext = Importer.getToContext();
4669 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4670 [&_ToContext](const Attr *A) -> const Attr * {
4671 return A->clone(_ToContext);
4672 });
4673 for (const Attr *ToA : ToAttrs) {
4674 if (!ToA)
4675 return nullptr;
4676 }
4677 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4678 if (!ToSubStmt && S->getSubStmt())
4679 return nullptr;
4680 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4681 ToAttrs, ToSubStmt);
4682}
4683
4684Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4685 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4686 VarDecl *ToConditionVariable = nullptr;
4687 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4688 ToConditionVariable =
4689 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4690 if (!ToConditionVariable)
4691 return nullptr;
4692 }
4693 Expr *ToCondition = Importer.Import(S->getCond());
4694 if (!ToCondition && S->getCond())
4695 return nullptr;
4696 Stmt *ToThenStmt = Importer.Import(S->getThen());
4697 if (!ToThenStmt && S->getThen())
4698 return nullptr;
4699 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4700 Stmt *ToElseStmt = Importer.Import(S->getElse());
4701 if (!ToElseStmt && S->getElse())
4702 return nullptr;
4703 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4704 ToIfLoc, ToConditionVariable,
4705 ToCondition, ToThenStmt,
4706 ToElseLoc, ToElseStmt);
4707}
4708
4709Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4710 VarDecl *ToConditionVariable = nullptr;
4711 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4712 ToConditionVariable =
4713 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4714 if (!ToConditionVariable)
4715 return nullptr;
4716 }
4717 Expr *ToCondition = Importer.Import(S->getCond());
4718 if (!ToCondition && S->getCond())
4719 return nullptr;
4720 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4721 Importer.getToContext(), ToConditionVariable,
4722 ToCondition);
4723 Stmt *ToBody = Importer.Import(S->getBody());
4724 if (!ToBody && S->getBody())
4725 return nullptr;
4726 ToStmt->setBody(ToBody);
4727 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4728 // Now we have to re-chain the cases.
4729 SwitchCase *LastChainedSwitchCase = nullptr;
4730 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4731 SC = SC->getNextSwitchCase()) {
4732 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4733 if (!ToSC)
4734 return nullptr;
4735 if (LastChainedSwitchCase)
4736 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4737 else
4738 ToStmt->setSwitchCaseList(ToSC);
4739 LastChainedSwitchCase = ToSC;
4740 }
4741 return ToStmt;
4742}
4743
4744Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4745 VarDecl *ToConditionVariable = nullptr;
4746 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4747 ToConditionVariable =
4748 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4749 if (!ToConditionVariable)
4750 return nullptr;
4751 }
4752 Expr *ToCondition = Importer.Import(S->getCond());
4753 if (!ToCondition && S->getCond())
4754 return nullptr;
4755 Stmt *ToBody = Importer.Import(S->getBody());
4756 if (!ToBody && S->getBody())
4757 return nullptr;
4758 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4759 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4760 ToConditionVariable,
4761 ToCondition, ToBody,
4762 ToWhileLoc);
4763}
4764
4765Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4766 Stmt *ToBody = Importer.Import(S->getBody());
4767 if (!ToBody && S->getBody())
4768 return nullptr;
4769 Expr *ToCondition = Importer.Import(S->getCond());
4770 if (!ToCondition && S->getCond())
4771 return nullptr;
4772 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4773 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4774 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4775 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4776 ToDoLoc, ToWhileLoc,
4777 ToRParenLoc);
4778}
4779
4780Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4781 Stmt *ToInit = Importer.Import(S->getInit());
4782 if (!ToInit && S->getInit())
4783 return nullptr;
4784 Expr *ToCondition = Importer.Import(S->getCond());
4785 if (!ToCondition && S->getCond())
4786 return nullptr;
4787 VarDecl *ToConditionVariable = nullptr;
4788 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4789 ToConditionVariable =
4790 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4791 if (!ToConditionVariable)
4792 return nullptr;
4793 }
4794 Expr *ToInc = Importer.Import(S->getInc());
4795 if (!ToInc && S->getInc())
4796 return nullptr;
4797 Stmt *ToBody = Importer.Import(S->getBody());
4798 if (!ToBody && S->getBody())
4799 return nullptr;
4800 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4801 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4802 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4803 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4804 ToInit, ToCondition,
4805 ToConditionVariable,
4806 ToInc, ToBody,
4807 ToForLoc, ToLParenLoc,
4808 ToRParenLoc);
4809}
4810
4811Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4812 LabelDecl *ToLabel = nullptr;
4813 if (LabelDecl *FromLabel = S->getLabel()) {
4814 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4815 if (!ToLabel)
4816 return nullptr;
4817 }
4818 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4819 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4820 return new (Importer.getToContext()) GotoStmt(ToLabel,
4821 ToGotoLoc, ToLabelLoc);
4822}
4823
4824Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4825 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4826 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4827 Expr *ToTarget = Importer.Import(S->getTarget());
4828 if (!ToTarget && S->getTarget())
4829 return nullptr;
4830 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4831 ToTarget);
4832}
4833
4834Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4835 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4836 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4837}
4838
4839Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4840 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4841 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4842}
4843
4844Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4845 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4846 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4847 if (!ToRetExpr && S->getRetValue())
4848 return nullptr;
4849 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4850 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4851 if (!ToNRVOCandidate && NRVOCandidate)
4852 return nullptr;
4853 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4854 ToNRVOCandidate);
4855}
4856
4857Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4858 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4859 VarDecl *ToExceptionDecl = nullptr;
4860 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4861 ToExceptionDecl =
4862 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4863 if (!ToExceptionDecl)
4864 return nullptr;
4865 }
4866 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4867 if (!ToHandlerBlock && S->getHandlerBlock())
4868 return nullptr;
4869 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4870 ToExceptionDecl,
4871 ToHandlerBlock);
4872}
4873
4874Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4875 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4876 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4877 if (!ToTryBlock && S->getTryBlock())
4878 return nullptr;
4879 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4880 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4881 CXXCatchStmt *FromHandler = S->getHandler(HI);
4882 if (Stmt *ToHandler = Importer.Import(FromHandler))
4883 ToHandlers[HI] = ToHandler;
4884 else
4885 return nullptr;
4886 }
4887 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4888 ToHandlers);
4889}
4890
4891Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4892 DeclStmt *ToRange =
4893 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4894 if (!ToRange && S->getRangeStmt())
4895 return nullptr;
4896 DeclStmt *ToBeginEnd =
4897 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4898 if (!ToBeginEnd && S->getBeginEndStmt())
4899 return nullptr;
4900 Expr *ToCond = Importer.Import(S->getCond());
4901 if (!ToCond && S->getCond())
4902 return nullptr;
4903 Expr *ToInc = Importer.Import(S->getInc());
4904 if (!ToInc && S->getInc())
4905 return nullptr;
4906 DeclStmt *ToLoopVar =
4907 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4908 if (!ToLoopVar && S->getLoopVarStmt())
4909 return nullptr;
4910 Stmt *ToBody = Importer.Import(S->getBody());
4911 if (!ToBody && S->getBody())
4912 return nullptr;
4913 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4914 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4915 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4916 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4917 ToCond, ToInc,
4918 ToLoopVar, ToBody,
4919 ToForLoc, ToColonLoc,
4920 ToRParenLoc);
4921}
4922
4923Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4924 Stmt *ToElem = Importer.Import(S->getElement());
4925 if (!ToElem && S->getElement())
4926 return nullptr;
4927 Expr *ToCollect = Importer.Import(S->getCollection());
4928 if (!ToCollect && S->getCollection())
4929 return nullptr;
4930 Stmt *ToBody = Importer.Import(S->getBody());
4931 if (!ToBody && S->getBody())
4932 return nullptr;
4933 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4934 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4935 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4936 ToCollect,
4937 ToBody, ToForLoc,
4938 ToRParenLoc);
4939}
4940
4941Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4942 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4943 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4944 VarDecl *ToExceptionDecl = nullptr;
4945 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4946 ToExceptionDecl =
4947 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4948 if (!ToExceptionDecl)
4949 return nullptr;
4950 }
4951 Stmt *ToBody = Importer.Import(S->getCatchBody());
4952 if (!ToBody && S->getCatchBody())
4953 return nullptr;
4954 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4955 ToRParenLoc,
4956 ToExceptionDecl,
4957 ToBody);
4958}
4959
4960Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4961 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4962 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4963 if (!ToAtFinallyStmt && S->getFinallyBody())
4964 return nullptr;
4965 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4966 ToAtFinallyStmt);
4967}
4968
4969Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4970 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4971 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4972 if (!ToAtTryStmt && S->getTryBody())
4973 return nullptr;
4974 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4975 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4976 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4977 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4978 ToCatchStmts[CI] = ToCatchStmt;
4979 else
4980 return nullptr;
4981 }
4982 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4983 if (!ToAtFinallyStmt && S->getFinallyStmt())
4984 return nullptr;
4985 return ObjCAtTryStmt::Create(Importer.getToContext(),
4986 ToAtTryLoc, ToAtTryStmt,
4987 ToCatchStmts.begin(), ToCatchStmts.size(),
4988 ToAtFinallyStmt);
4989}
4990
4991Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4992 (ObjCAtSynchronizedStmt *S) {
4993 SourceLocation ToAtSynchronizedLoc =
4994 Importer.Import(S->getAtSynchronizedLoc());
4995 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4996 if (!ToSynchExpr && S->getSynchExpr())
4997 return nullptr;
4998 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4999 if (!ToSynchBody && S->getSynchBody())
5000 return nullptr;
5001 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5002 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5003}
5004
5005Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5006 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5007 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5008 if (!ToThrow && S->getThrowExpr())
5009 return nullptr;
5010 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5011}
5012
5013Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5014 (ObjCAutoreleasePoolStmt *S) {
5015 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5016 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5017 if (!ToSubStmt && S->getSubStmt())
5018 return nullptr;
5019 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5020 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005021}
5022
5023//----------------------------------------------------------------------------
5024// Import Expressions
5025//----------------------------------------------------------------------------
5026Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5027 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5028 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005029 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005030}
5031
Douglas Gregor52f820e2010-02-19 01:17:02 +00005032Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005033 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5034 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005035 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005036
Craig Topper36250ad2014-05-12 05:36:57 +00005037 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005038 if (E->getDecl() != E->getFoundDecl()) {
5039 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5040 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005041 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005042 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005043
5044 QualType T = Importer.Import(E->getType());
5045 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005046 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005047
5048 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5049 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005050 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005051 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005052 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005053 Importer.Import(E->getLocation()),
5054 T, E->getValueKind(),
5055 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005056 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005057 if (E->hadMultipleCandidates())
5058 DRE->setHadMultipleCandidates(true);
5059 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005060}
5061
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005062Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5063 QualType T = Importer.Import(E->getType());
5064 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005065 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005066
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005067 return IntegerLiteral::Create(Importer.getToContext(),
5068 E->getValue(), T,
5069 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005070}
5071
Douglas Gregor623421d2010-02-18 02:21:22 +00005072Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5073 QualType T = Importer.Import(E->getType());
5074 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005075 return nullptr;
5076
Douglas Gregorfb65e592011-07-27 05:40:30 +00005077 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5078 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005079 Importer.Import(E->getLocation()));
5080}
5081
Douglas Gregorc74247e2010-02-19 01:07:06 +00005082Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5083 Expr *SubExpr = Importer.Import(E->getSubExpr());
5084 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005085 return nullptr;
5086
Douglas Gregorc74247e2010-02-19 01:07:06 +00005087 return new (Importer.getToContext())
5088 ParenExpr(Importer.Import(E->getLParen()),
5089 Importer.Import(E->getRParen()),
5090 SubExpr);
5091}
5092
5093Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5094 QualType T = Importer.Import(E->getType());
5095 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005096 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005097
5098 Expr *SubExpr = Importer.Import(E->getSubExpr());
5099 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005100 return nullptr;
5101
Douglas Gregorc74247e2010-02-19 01:07:06 +00005102 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005103 T, E->getValueKind(),
5104 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005105 Importer.Import(E->getOperatorLoc()));
5106}
5107
Peter Collingbournee190dee2011-03-11 19:24:49 +00005108Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5109 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005110 QualType ResultType = Importer.Import(E->getType());
5111
5112 if (E->isArgumentType()) {
5113 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5114 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005115 return nullptr;
5116
Peter Collingbournee190dee2011-03-11 19:24:49 +00005117 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5118 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005119 Importer.Import(E->getOperatorLoc()),
5120 Importer.Import(E->getRParenLoc()));
5121 }
5122
5123 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5124 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005125 return nullptr;
5126
Peter Collingbournee190dee2011-03-11 19:24:49 +00005127 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5128 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005129 Importer.Import(E->getOperatorLoc()),
5130 Importer.Import(E->getRParenLoc()));
5131}
5132
Douglas Gregorc74247e2010-02-19 01:07:06 +00005133Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5134 QualType T = Importer.Import(E->getType());
5135 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005136 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005137
5138 Expr *LHS = Importer.Import(E->getLHS());
5139 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005140 return nullptr;
5141
Douglas Gregorc74247e2010-02-19 01:07:06 +00005142 Expr *RHS = Importer.Import(E->getRHS());
5143 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005144 return nullptr;
5145
Douglas Gregorc74247e2010-02-19 01:07:06 +00005146 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005147 T, E->getValueKind(),
5148 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005149 Importer.Import(E->getOperatorLoc()),
5150 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005151}
5152
5153Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5154 QualType T = Importer.Import(E->getType());
5155 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005156 return nullptr;
5157
Douglas Gregorc74247e2010-02-19 01:07:06 +00005158 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5159 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005160 return nullptr;
5161
Douglas Gregorc74247e2010-02-19 01:07:06 +00005162 QualType CompResultType = Importer.Import(E->getComputationResultType());
5163 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005164 return nullptr;
5165
Douglas Gregorc74247e2010-02-19 01:07:06 +00005166 Expr *LHS = Importer.Import(E->getLHS());
5167 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005168 return nullptr;
5169
Douglas Gregorc74247e2010-02-19 01:07:06 +00005170 Expr *RHS = Importer.Import(E->getRHS());
5171 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005172 return nullptr;
5173
Douglas Gregorc74247e2010-02-19 01:07:06 +00005174 return new (Importer.getToContext())
5175 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005176 T, E->getValueKind(),
5177 E->getObjectKind(),
5178 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005179 Importer.Import(E->getOperatorLoc()),
5180 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005181}
5182
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005183static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005184 if (E->path_empty()) return false;
5185
5186 // TODO: import cast paths
5187 return true;
5188}
5189
Douglas Gregor98c10182010-02-12 22:17:39 +00005190Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5191 QualType T = Importer.Import(E->getType());
5192 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005193 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005194
5195 Expr *SubExpr = Importer.Import(E->getSubExpr());
5196 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005197 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005198
5199 CXXCastPath BasePath;
5200 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005201 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005202
5203 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005204 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005205}
5206
Douglas Gregor5481d322010-02-19 01:32:14 +00005207Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5208 QualType T = Importer.Import(E->getType());
5209 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005210 return nullptr;
5211
Douglas Gregor5481d322010-02-19 01:32:14 +00005212 Expr *SubExpr = Importer.Import(E->getSubExpr());
5213 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005214 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005215
5216 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5217 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005218 return nullptr;
5219
John McCallcf142162010-08-07 06:22:56 +00005220 CXXCastPath BasePath;
5221 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005222 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005223
John McCall7decc9e2010-11-18 06:31:45 +00005224 return CStyleCastExpr::Create(Importer.getToContext(), T,
5225 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005226 SubExpr, &BasePath, TInfo,
5227 Importer.Import(E->getLParenLoc()),
5228 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005229}
5230
Sean Callanan59721b32015-04-28 18:41:46 +00005231Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5232 QualType T = Importer.Import(E->getType());
5233 if (T.isNull())
5234 return nullptr;
5235
5236 CXXConstructorDecl *ToCCD =
5237 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5238 if (!ToCCD && E->getConstructor())
5239 return nullptr;
5240
5241 size_t NumArgs = E->getNumArgs();
5242 SmallVector<Expr *, 1> ToArgs(NumArgs);
5243 ASTImporter &_Importer = Importer;
5244 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5245 [&_Importer](Expr *AE) -> Expr * {
5246 return _Importer.Import(AE);
5247 });
5248 for (Expr *ToA : ToArgs) {
5249 if (!ToA)
5250 return nullptr;
5251 }
5252
5253 return CXXConstructExpr::Create(Importer.getToContext(), T,
5254 Importer.Import(E->getLocation()),
5255 ToCCD, E->isElidable(),
5256 ToArgs, E->hadMultipleCandidates(),
5257 E->isListInitialization(),
5258 E->isStdInitListInitialization(),
5259 E->requiresZeroInitialization(),
5260 E->getConstructionKind(),
5261 Importer.Import(E->getParenOrBraceRange()));
5262}
5263
5264Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5265 QualType T = Importer.Import(E->getType());
5266 if (T.isNull())
5267 return nullptr;
5268
5269 Expr *ToBase = Importer.Import(E->getBase());
5270 if (!ToBase && E->getBase())
5271 return nullptr;
5272
5273 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5274 if (!ToMember && E->getMemberDecl())
5275 return nullptr;
5276
5277 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5278 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5279 E->getFoundDecl().getAccess());
5280
5281 DeclarationNameInfo ToMemberNameInfo(
5282 Importer.Import(E->getMemberNameInfo().getName()),
5283 Importer.Import(E->getMemberNameInfo().getLoc()));
5284
5285 if (E->hasExplicitTemplateArgs()) {
5286 return nullptr; // FIXME: handle template arguments
5287 }
5288
5289 return MemberExpr::Create(Importer.getToContext(), ToBase,
5290 E->isArrow(),
5291 Importer.Import(E->getOperatorLoc()),
5292 Importer.Import(E->getQualifierLoc()),
5293 Importer.Import(E->getTemplateKeywordLoc()),
5294 ToMember, ToFoundDecl, ToMemberNameInfo,
5295 nullptr, T, E->getValueKind(),
5296 E->getObjectKind());
5297}
5298
5299Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5300 QualType T = Importer.Import(E->getType());
5301 if (T.isNull())
5302 return nullptr;
5303
5304 Expr *ToCallee = Importer.Import(E->getCallee());
5305 if (!ToCallee && E->getCallee())
5306 return nullptr;
5307
5308 unsigned NumArgs = E->getNumArgs();
5309
5310 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5311
5312 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5313 Expr *FromArg = E->getArg(ai);
5314 Expr *ToArg = Importer.Import(FromArg);
5315 if (!ToArg)
5316 return nullptr;
5317 ToArgs[ai] = ToArg;
5318 }
5319
5320 Expr **ToArgs_Copied = new (Importer.getToContext())
5321 Expr*[NumArgs];
5322
5323 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5324 ToArgs_Copied[ai] = ToArgs[ai];
5325
5326 return new (Importer.getToContext())
5327 CallExpr(Importer.getToContext(), ToCallee,
5328 ArrayRef<Expr*>(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5329 Importer.Import(E->getRParenLoc()));
5330}
5331
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005332ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005333 ASTContext &FromContext, FileManager &FromFileManager,
5334 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005335 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005336 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005337 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005338{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005339 ImportedDecls[FromContext.getTranslationUnitDecl()]
5340 = ToContext.getTranslationUnitDecl();
5341}
5342
5343ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005344
5345QualType ASTImporter::Import(QualType FromT) {
5346 if (FromT.isNull())
5347 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005348
5349 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005350
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005351 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005352 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5353 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005354 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005355 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005356
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005357 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005358 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005359 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005360 if (ToT.isNull())
5361 return ToT;
5362
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005363 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005364 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005365
John McCall424cec92011-01-19 06:33:43 +00005366 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005367}
5368
Douglas Gregor62d311f2010-02-09 19:21:46 +00005369TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005370 if (!FromTSI)
5371 return FromTSI;
5372
5373 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005374 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005375 QualType T = Import(FromTSI->getType());
5376 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005377 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005378
5379 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005380 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005381}
5382
Sean Callanan59721b32015-04-28 18:41:46 +00005383Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5384 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5385 if (Pos != ImportedDecls.end()) {
5386 Decl *ToD = Pos->second;
5387 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5388 return ToD;
5389 } else {
5390 return nullptr;
5391 }
5392}
5393
Douglas Gregor62d311f2010-02-09 19:21:46 +00005394Decl *ASTImporter::Import(Decl *FromD) {
5395 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005396 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005397
Douglas Gregord451ea92011-07-29 23:31:30 +00005398 ASTNodeImporter Importer(*this);
5399
Douglas Gregor62d311f2010-02-09 19:21:46 +00005400 // Check whether we've already imported this declaration.
5401 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005402 if (Pos != ImportedDecls.end()) {
5403 Decl *ToD = Pos->second;
5404 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5405 return ToD;
5406 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005407
5408 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005409 Decl *ToD = Importer.Visit(FromD);
5410 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005411 return nullptr;
5412
Douglas Gregor62d311f2010-02-09 19:21:46 +00005413 // Record the imported declaration.
5414 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005415
5416 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5417 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005418 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005419 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005420 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005421 // When we've finished transforming a typedef, see whether it was the
5422 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005423 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005424 FromTag = AnonTagsWithPendingTypedefs.begin(),
5425 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5426 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005427 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005428 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5429 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005430 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005431 AnonTagsWithPendingTypedefs.erase(FromTag);
5432 break;
5433 }
5434 }
5435 }
5436 }
5437
Douglas Gregor62d311f2010-02-09 19:21:46 +00005438 return ToD;
5439}
5440
5441DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5442 if (!FromDC)
5443 return FromDC;
5444
Douglas Gregor95d82832012-01-24 18:36:04 +00005445 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005446 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005447 return nullptr;
5448
Douglas Gregor2e15c842012-02-01 21:00:38 +00005449 // When we're using a record/enum/Objective-C class/protocol as a context, we
5450 // need it to have a definition.
5451 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005452 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005453 if (ToRecord->isCompleteDefinition()) {
5454 // Do nothing.
5455 } else if (FromRecord->isCompleteDefinition()) {
5456 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5457 ASTNodeImporter::IDK_Basic);
5458 } else {
5459 CompleteDecl(ToRecord);
5460 }
5461 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5462 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5463 if (ToEnum->isCompleteDefinition()) {
5464 // Do nothing.
5465 } else if (FromEnum->isCompleteDefinition()) {
5466 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5467 ASTNodeImporter::IDK_Basic);
5468 } else {
5469 CompleteDecl(ToEnum);
5470 }
5471 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5472 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5473 if (ToClass->getDefinition()) {
5474 // Do nothing.
5475 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5476 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5477 ASTNodeImporter::IDK_Basic);
5478 } else {
5479 CompleteDecl(ToClass);
5480 }
5481 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5482 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5483 if (ToProto->getDefinition()) {
5484 // Do nothing.
5485 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5486 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5487 ASTNodeImporter::IDK_Basic);
5488 } else {
5489 CompleteDecl(ToProto);
5490 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005491 }
5492
5493 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005494}
5495
5496Expr *ASTImporter::Import(Expr *FromE) {
5497 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005498 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005499
5500 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5501}
5502
5503Stmt *ASTImporter::Import(Stmt *FromS) {
5504 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005505 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005506
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005507 // Check whether we've already imported this declaration.
5508 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5509 if (Pos != ImportedStmts.end())
5510 return Pos->second;
5511
5512 // Import the type
5513 ASTNodeImporter Importer(*this);
5514 Stmt *ToS = Importer.Visit(FromS);
5515 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005516 return nullptr;
5517
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005518 // Record the imported declaration.
5519 ImportedStmts[FromS] = ToS;
5520 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005521}
5522
5523NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5524 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005525 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005526
Douglas Gregor90ebf252011-04-27 16:48:40 +00005527 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5528
5529 switch (FromNNS->getKind()) {
5530 case NestedNameSpecifier::Identifier:
5531 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5532 return NestedNameSpecifier::Create(ToContext, prefix, II);
5533 }
Craig Topper36250ad2014-05-12 05:36:57 +00005534 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005535
5536 case NestedNameSpecifier::Namespace:
5537 if (NamespaceDecl *NS =
5538 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5539 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5540 }
Craig Topper36250ad2014-05-12 05:36:57 +00005541 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005542
5543 case NestedNameSpecifier::NamespaceAlias:
5544 if (NamespaceAliasDecl *NSAD =
5545 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5546 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5547 }
Craig Topper36250ad2014-05-12 05:36:57 +00005548 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005549
5550 case NestedNameSpecifier::Global:
5551 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5552
Nikola Smiljanic67860242014-09-26 00:28:20 +00005553 case NestedNameSpecifier::Super:
5554 if (CXXRecordDecl *RD =
5555 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5556 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5557 }
5558 return nullptr;
5559
Douglas Gregor90ebf252011-04-27 16:48:40 +00005560 case NestedNameSpecifier::TypeSpec:
5561 case NestedNameSpecifier::TypeSpecWithTemplate: {
5562 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5563 if (!T.isNull()) {
5564 bool bTemplate = FromNNS->getKind() ==
5565 NestedNameSpecifier::TypeSpecWithTemplate;
5566 return NestedNameSpecifier::Create(ToContext, prefix,
5567 bTemplate, T.getTypePtr());
5568 }
5569 }
Craig Topper36250ad2014-05-12 05:36:57 +00005570 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005571 }
5572
5573 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005574}
5575
Douglas Gregor14454802011-02-25 02:25:35 +00005576NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5577 // FIXME: Implement!
5578 return NestedNameSpecifierLoc();
5579}
5580
Douglas Gregore2e50d332010-12-01 01:36:18 +00005581TemplateName ASTImporter::Import(TemplateName From) {
5582 switch (From.getKind()) {
5583 case TemplateName::Template:
5584 if (TemplateDecl *ToTemplate
5585 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5586 return TemplateName(ToTemplate);
5587
5588 return TemplateName();
5589
5590 case TemplateName::OverloadedTemplate: {
5591 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5592 UnresolvedSet<2> ToTemplates;
5593 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5594 E = FromStorage->end();
5595 I != E; ++I) {
5596 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5597 ToTemplates.addDecl(To);
5598 else
5599 return TemplateName();
5600 }
5601 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5602 ToTemplates.end());
5603 }
5604
5605 case TemplateName::QualifiedTemplate: {
5606 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5607 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5608 if (!Qualifier)
5609 return TemplateName();
5610
5611 if (TemplateDecl *ToTemplate
5612 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5613 return ToContext.getQualifiedTemplateName(Qualifier,
5614 QTN->hasTemplateKeyword(),
5615 ToTemplate);
5616
5617 return TemplateName();
5618 }
5619
5620 case TemplateName::DependentTemplate: {
5621 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5622 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5623 if (!Qualifier)
5624 return TemplateName();
5625
5626 if (DTN->isIdentifier()) {
5627 return ToContext.getDependentTemplateName(Qualifier,
5628 Import(DTN->getIdentifier()));
5629 }
5630
5631 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5632 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005633
5634 case TemplateName::SubstTemplateTemplateParm: {
5635 SubstTemplateTemplateParmStorage *subst
5636 = From.getAsSubstTemplateTemplateParm();
5637 TemplateTemplateParmDecl *param
5638 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5639 if (!param)
5640 return TemplateName();
5641
5642 TemplateName replacement = Import(subst->getReplacement());
5643 if (replacement.isNull()) return TemplateName();
5644
5645 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5646 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005647
5648 case TemplateName::SubstTemplateTemplateParmPack: {
5649 SubstTemplateTemplateParmPackStorage *SubstPack
5650 = From.getAsSubstTemplateTemplateParmPack();
5651 TemplateTemplateParmDecl *Param
5652 = cast_or_null<TemplateTemplateParmDecl>(
5653 Import(SubstPack->getParameterPack()));
5654 if (!Param)
5655 return TemplateName();
5656
5657 ASTNodeImporter Importer(*this);
5658 TemplateArgument ArgPack
5659 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5660 if (ArgPack.isNull())
5661 return TemplateName();
5662
5663 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5664 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005665 }
5666
5667 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005668}
5669
Douglas Gregor62d311f2010-02-09 19:21:46 +00005670SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5671 if (FromLoc.isInvalid())
5672 return SourceLocation();
5673
Douglas Gregor811663e2010-02-10 00:15:17 +00005674 SourceManager &FromSM = FromContext.getSourceManager();
5675
5676 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005677 // don't have to import macro expansions.
5678 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005679 FromLoc = FromSM.getSpellingLoc(FromLoc);
5680 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5681 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005682 FileID ToFileID = Import(Decomposed.first);
5683 if (ToFileID.isInvalid())
5684 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005685 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5686 .getLocWithOffset(Decomposed.second);
5687 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005688}
5689
5690SourceRange ASTImporter::Import(SourceRange FromRange) {
5691 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5692}
5693
Douglas Gregor811663e2010-02-10 00:15:17 +00005694FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005695 llvm::DenseMap<FileID, FileID>::iterator Pos
5696 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005697 if (Pos != ImportedFileIDs.end())
5698 return Pos->second;
5699
5700 SourceManager &FromSM = FromContext.getSourceManager();
5701 SourceManager &ToSM = ToContext.getSourceManager();
5702 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005703 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005704
5705 // Include location of this file.
5706 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5707
5708 // Map the FileID for to the "to" source manager.
5709 FileID ToID;
5710 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005711 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005712 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5713 // disk again
5714 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5715 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005716 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005717 if (!Entry)
5718 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005719 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5720 FromSLoc.getFile().getFileCharacteristic());
5721 } else {
5722 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005723 const llvm::MemoryBuffer *
5724 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005725 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005726 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005727 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005728 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005729 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005730 }
5731
5732
Sebastian Redl99219f12010-09-30 01:03:06 +00005733 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005734 return ToID;
5735}
5736
Douglas Gregor0a791672011-01-18 03:11:38 +00005737void ASTImporter::ImportDefinition(Decl *From) {
5738 Decl *To = Import(From);
5739 if (!To)
5740 return;
5741
5742 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5743 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005744
5745 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5746 if (!ToRecord->getDefinition()) {
5747 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005748 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005749 return;
5750 }
5751 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005752
5753 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5754 if (!ToEnum->getDefinition()) {
5755 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005756 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005757 return;
5758 }
5759 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005760
5761 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5762 if (!ToIFace->getDefinition()) {
5763 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005764 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005765 return;
5766 }
5767 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005768
Douglas Gregor2aa53772012-01-24 17:42:07 +00005769 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5770 if (!ToProto->getDefinition()) {
5771 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005772 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005773 return;
5774 }
5775 }
5776
Douglas Gregor0a791672011-01-18 03:11:38 +00005777 Importer.ImportDeclContext(FromDC, true);
5778 }
5779}
5780
Douglas Gregor96e578d2010-02-05 17:54:41 +00005781DeclarationName ASTImporter::Import(DeclarationName FromName) {
5782 if (!FromName)
5783 return DeclarationName();
5784
5785 switch (FromName.getNameKind()) {
5786 case DeclarationName::Identifier:
5787 return Import(FromName.getAsIdentifierInfo());
5788
5789 case DeclarationName::ObjCZeroArgSelector:
5790 case DeclarationName::ObjCOneArgSelector:
5791 case DeclarationName::ObjCMultiArgSelector:
5792 return Import(FromName.getObjCSelector());
5793
5794 case DeclarationName::CXXConstructorName: {
5795 QualType T = Import(FromName.getCXXNameType());
5796 if (T.isNull())
5797 return DeclarationName();
5798
5799 return ToContext.DeclarationNames.getCXXConstructorName(
5800 ToContext.getCanonicalType(T));
5801 }
5802
5803 case DeclarationName::CXXDestructorName: {
5804 QualType T = Import(FromName.getCXXNameType());
5805 if (T.isNull())
5806 return DeclarationName();
5807
5808 return ToContext.DeclarationNames.getCXXDestructorName(
5809 ToContext.getCanonicalType(T));
5810 }
5811
5812 case DeclarationName::CXXConversionFunctionName: {
5813 QualType T = Import(FromName.getCXXNameType());
5814 if (T.isNull())
5815 return DeclarationName();
5816
5817 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5818 ToContext.getCanonicalType(T));
5819 }
5820
5821 case DeclarationName::CXXOperatorName:
5822 return ToContext.DeclarationNames.getCXXOperatorName(
5823 FromName.getCXXOverloadedOperator());
5824
5825 case DeclarationName::CXXLiteralOperatorName:
5826 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5827 Import(FromName.getCXXLiteralIdentifier()));
5828
5829 case DeclarationName::CXXUsingDirective:
5830 // FIXME: STATICS!
5831 return DeclarationName::getUsingDirectiveName();
5832 }
5833
David Blaikiee4d798f2012-01-20 21:50:17 +00005834 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005835}
5836
Douglas Gregore2e50d332010-12-01 01:36:18 +00005837IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005838 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005839 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005840
5841 return &ToContext.Idents.get(FromId->getName());
5842}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005843
Douglas Gregor43f54792010-02-17 02:12:47 +00005844Selector ASTImporter::Import(Selector FromSel) {
5845 if (FromSel.isNull())
5846 return Selector();
5847
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005848 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005849 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5850 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5851 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5852 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5853}
5854
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005855DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5856 DeclContext *DC,
5857 unsigned IDNS,
5858 NamedDecl **Decls,
5859 unsigned NumDecls) {
5860 return Name;
5861}
5862
5863DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005864 if (LastDiagFromFrom)
5865 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5866 FromContext.getDiagnostics());
5867 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005868 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005869}
5870
5871DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005872 if (!LastDiagFromFrom)
5873 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5874 ToContext.getDiagnostics());
5875 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005876 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005877}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005878
Douglas Gregor2e15c842012-02-01 21:00:38 +00005879void ASTImporter::CompleteDecl (Decl *D) {
5880 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5881 if (!ID->getDefinition())
5882 ID->startDefinition();
5883 }
5884 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5885 if (!PD->getDefinition())
5886 PD->startDefinition();
5887 }
5888 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5889 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5890 TD->startDefinition();
5891 TD->setCompleteDefinition(true);
5892 }
5893 }
5894 else {
5895 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5896 }
5897}
5898
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005899Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5900 ImportedDecls[From] = To;
5901 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005902}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005903
Douglas Gregordd6006f2012-07-17 21:16:27 +00005904bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5905 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005906 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005907 = ImportedTypes.find(From.getTypePtr());
5908 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5909 return true;
5910
Douglas Gregordd6006f2012-07-17 21:16:27 +00005911 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5912 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005913 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005914}