blob: 6816dd11d9b20447150dbbea95c231486322ae9d [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();
Benjamin Kramercce63472015-08-05 09:40:22 +00002213
2214 return TemplateArgument(
2215 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002216 }
2217 }
2218
2219 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002220}
2221
2222bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2223 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002224 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002225 for (unsigned I = 0; I != NumFromArgs; ++I) {
2226 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2227 if (To.isNull() && !FromArgs[I].isNull())
2228 return true;
2229
2230 ToArgs.push_back(To);
2231 }
2232
2233 return false;
2234}
2235
Douglas Gregor5c73e912010-02-11 00:48:18 +00002236bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002237 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002238 // Eliminate a potential failure point where we attempt to re-import
2239 // something we're trying to import while completing ToRecord.
2240 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2241 if (ToOrigin) {
2242 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2243 if (ToOriginRecord)
2244 ToRecord = ToOriginRecord;
2245 }
2246
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002247 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002248 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002249 Importer.getNonEquivalentDecls(),
2250 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002251 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002252}
2253
Larisse Voufo39a1e502013-08-06 01:03:05 +00002254bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2255 bool Complain) {
2256 StructuralEquivalenceContext Ctx(
2257 Importer.getFromContext(), Importer.getToContext(),
2258 Importer.getNonEquivalentDecls(), false, Complain);
2259 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2260}
2261
Douglas Gregor98c10182010-02-12 22:17:39 +00002262bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002263 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002264 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002265 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002266 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002267}
2268
Douglas Gregor91155082012-11-14 22:29:20 +00002269bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2270 EnumConstantDecl *ToEC)
2271{
2272 const llvm::APSInt &FromVal = FromEC->getInitVal();
2273 const llvm::APSInt &ToVal = ToEC->getInitVal();
2274
2275 return FromVal.isSigned() == ToVal.isSigned() &&
2276 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2277 FromVal == ToVal;
2278}
2279
2280bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002281 ClassTemplateDecl *To) {
2282 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2283 Importer.getToContext(),
2284 Importer.getNonEquivalentDecls());
2285 return Ctx.IsStructurallyEquivalent(From, To);
2286}
2287
Larisse Voufo39a1e502013-08-06 01:03:05 +00002288bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2289 VarTemplateDecl *To) {
2290 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2291 Importer.getToContext(),
2292 Importer.getNonEquivalentDecls());
2293 return Ctx.IsStructurallyEquivalent(From, To);
2294}
2295
Douglas Gregore4c83e42010-02-09 22:48:33 +00002296Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002297 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002298 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002299 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002300}
2301
Sean Callanan65198272011-11-17 23:20:56 +00002302Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2303 TranslationUnitDecl *ToD =
2304 Importer.getToContext().getTranslationUnitDecl();
2305
2306 Importer.Imported(D, ToD);
2307
2308 return ToD;
2309}
2310
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002311Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2312 // Import the major distinguishing characteristics of this namespace.
2313 DeclContext *DC, *LexicalDC;
2314 DeclarationName Name;
2315 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002316 NamedDecl *ToD;
2317 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002318 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002319 if (ToD)
2320 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002321
2322 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002323 if (!Name) {
2324 // This is an anonymous namespace. Adopt an existing anonymous
2325 // namespace if we can.
2326 // FIXME: Not testable.
2327 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2328 MergeWithNamespace = TU->getAnonymousNamespace();
2329 else
2330 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2331 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002332 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002333 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002334 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002335 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2336 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002337 continue;
2338
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002339 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002340 MergeWithNamespace = FoundNS;
2341 ConflictingDecls.clear();
2342 break;
2343 }
2344
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002345 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002346 }
2347
2348 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002349 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002350 ConflictingDecls.data(),
2351 ConflictingDecls.size());
2352 }
2353 }
2354
2355 // Create the "to" namespace, if needed.
2356 NamespaceDecl *ToNamespace = MergeWithNamespace;
2357 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002358 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002359 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002360 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002361 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002362 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002363 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002364 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002365
2366 // If this is an anonymous namespace, register it as the anonymous
2367 // namespace within its context.
2368 if (!Name) {
2369 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2370 TU->setAnonymousNamespace(ToNamespace);
2371 else
2372 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2373 }
2374 }
2375 Importer.Imported(D, ToNamespace);
2376
2377 ImportDeclContext(D);
2378
2379 return ToNamespace;
2380}
2381
Richard Smithdda56e42011-04-15 14:24:37 +00002382Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002383 // Import the major distinguishing characteristics of this typedef.
2384 DeclContext *DC, *LexicalDC;
2385 DeclarationName Name;
2386 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002387 NamedDecl *ToD;
2388 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002389 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002390 if (ToD)
2391 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002392
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002393 // If this typedef is not in block scope, determine whether we've
2394 // seen a typedef with the same name (that we can merge with) or any
2395 // other entity by that name (which name lookup could conflict with).
2396 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002397 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002398 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002399 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002400 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002401 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2402 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002403 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002404 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002405 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002406 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2407 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002408 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002409 }
2410
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002411 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002412 }
2413
2414 if (!ConflictingDecls.empty()) {
2415 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2416 ConflictingDecls.data(),
2417 ConflictingDecls.size());
2418 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002419 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002420 }
2421 }
2422
Douglas Gregorb4964f72010-02-15 23:54:17 +00002423 // Import the underlying type of this typedef;
2424 QualType T = Importer.Import(D->getUnderlyingType());
2425 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002426 return nullptr;
2427
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002428 // Create the new typedef node.
2429 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002430 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002431 TypedefNameDecl *ToTypedef;
2432 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002433 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2434 StartL, Loc,
2435 Name.getAsIdentifierInfo(),
2436 TInfo);
2437 else
Richard Smithdda56e42011-04-15 14:24:37 +00002438 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2439 StartL, Loc,
2440 Name.getAsIdentifierInfo(),
2441 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002442
Douglas Gregordd483172010-02-22 17:42:47 +00002443 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002444 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002445 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002446 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002447
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002448 return ToTypedef;
2449}
2450
Richard Smithdda56e42011-04-15 14:24:37 +00002451Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2452 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2453}
2454
2455Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2456 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2457}
2458
Douglas Gregor98c10182010-02-12 22:17:39 +00002459Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2460 // Import the major distinguishing characteristics of this enum.
2461 DeclContext *DC, *LexicalDC;
2462 DeclarationName Name;
2463 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002464 NamedDecl *ToD;
2465 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002466 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002467 if (ToD)
2468 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002469
Douglas Gregor98c10182010-02-12 22:17:39 +00002470 // Figure out what enum name we're looking for.
2471 unsigned IDNS = Decl::IDNS_Tag;
2472 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002473 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2474 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002475 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002476 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002477 IDNS |= Decl::IDNS_Ordinary;
2478
2479 // We may already have an enum of the same name; try to find and match it.
2480 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002481 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002482 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002483 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002484 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2485 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002486 continue;
2487
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002488 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002489 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002490 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2491 Found = Tag->getDecl();
2492 }
2493
2494 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002495 if (IsStructuralMatch(D, FoundEnum))
2496 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002497 }
2498
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002499 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002500 }
2501
2502 if (!ConflictingDecls.empty()) {
2503 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2504 ConflictingDecls.data(),
2505 ConflictingDecls.size());
2506 }
2507 }
2508
2509 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002510 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2511 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002512 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002513 D->isScoped(), D->isScopedUsingClassTag(),
2514 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002515 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002516 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002517 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002518 D2->setLexicalDeclContext(LexicalDC);
2519 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002520 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002521
2522 // Import the integer type.
2523 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2524 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002525 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002526 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002527
2528 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002529 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002530 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002531
Douglas Gregor3996e242010-02-15 22:01:00 +00002532 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002533}
2534
Douglas Gregor5c73e912010-02-11 00:48:18 +00002535Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2536 // If this record has a definition in the translation unit we're coming from,
2537 // but this particular declaration is not that definition, import the
2538 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002539 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002540 if (Definition && Definition != D) {
2541 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002542 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002543 return nullptr;
2544
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002545 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002546 }
2547
2548 // Import the major distinguishing characteristics of this record.
2549 DeclContext *DC, *LexicalDC;
2550 DeclarationName Name;
2551 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002552 NamedDecl *ToD;
2553 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002554 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002555 if (ToD)
2556 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002557
Douglas Gregor5c73e912010-02-11 00:48:18 +00002558 // Figure out what structure name we're looking for.
2559 unsigned IDNS = Decl::IDNS_Tag;
2560 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002561 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2562 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002563 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002564 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002565 IDNS |= Decl::IDNS_Ordinary;
2566
2567 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002568 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002569 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002570 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002571 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002572 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002573 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2574 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002575 continue;
2576
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002577 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002578 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2580 Found = Tag->getDecl();
2581 }
2582
2583 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002584 if (D->isAnonymousStructOrUnion() &&
2585 FoundRecord->isAnonymousStructOrUnion()) {
2586 // If both anonymous structs/unions are in a record context, make sure
2587 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002588 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002589 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002590 if (Optional<unsigned> Index2 =
2591 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002592 if (*Index1 != *Index2)
2593 continue;
2594 }
2595 }
2596 }
2597
Douglas Gregor25791052010-02-12 00:09:27 +00002598 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002599 if ((SearchName && !D->isCompleteDefinition())
2600 || (D->isCompleteDefinition() &&
2601 D->isAnonymousStructOrUnion()
2602 == FoundDef->isAnonymousStructOrUnion() &&
2603 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002604 // The record types structurally match, or the "from" translation
2605 // unit only had a forward declaration anyway; call it the same
2606 // function.
2607 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002608 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002609 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002610 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002611 // We have a forward declaration of this type, so adopt that forward
2612 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002613
2614 // If one or both can be completed from external storage then try one
2615 // last time to complete and compare them before doing this.
2616
2617 if (FoundRecord->hasExternalLexicalStorage() &&
2618 !FoundRecord->isCompleteDefinition())
2619 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2620 if (D->hasExternalLexicalStorage())
2621 D->getASTContext().getExternalSource()->CompleteType(D);
2622
2623 if (FoundRecord->isCompleteDefinition() &&
2624 D->isCompleteDefinition() &&
2625 !IsStructuralMatch(D, FoundRecord))
2626 continue;
2627
Douglas Gregor25791052010-02-12 00:09:27 +00002628 AdoptDecl = FoundRecord;
2629 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002630 } else if (!SearchName) {
2631 continue;
2632 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002633 }
2634
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002635 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002636 }
2637
Douglas Gregordd6006f2012-07-17 21:16:27 +00002638 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002639 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2640 ConflictingDecls.data(),
2641 ConflictingDecls.size());
2642 }
2643 }
2644
2645 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002646 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002647 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002648 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002649 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002650 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002651 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002652 DC, StartLoc, Loc,
2653 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002654 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002655 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002656 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002657 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002658 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002659 }
Douglas Gregor14454802011-02-25 02:25:35 +00002660
2661 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002662 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002663 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002664 if (D->isAnonymousStructOrUnion())
2665 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002666 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002667
Douglas Gregor3996e242010-02-15 22:01:00 +00002668 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002669
Douglas Gregor95d82832012-01-24 18:36:04 +00002670 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002671 return nullptr;
2672
Douglas Gregor3996e242010-02-15 22:01:00 +00002673 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002674}
2675
Douglas Gregor98c10182010-02-12 22:17:39 +00002676Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2677 // Import the major distinguishing characteristics of this enumerator.
2678 DeclContext *DC, *LexicalDC;
2679 DeclarationName Name;
2680 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002681 NamedDecl *ToD;
2682 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002683 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002684 if (ToD)
2685 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002686
2687 QualType T = Importer.Import(D->getType());
2688 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002689 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002690
Douglas Gregor98c10182010-02-12 22:17:39 +00002691 // Determine whether there are any other declarations with the same name and
2692 // in the same context.
2693 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002694 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002695 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002696 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002697 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002698 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2699 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002700 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002701
2702 if (EnumConstantDecl *FoundEnumConstant
2703 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2704 if (IsStructuralMatch(D, FoundEnumConstant))
2705 return Importer.Imported(D, FoundEnumConstant);
2706 }
2707
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002708 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002709 }
2710
2711 if (!ConflictingDecls.empty()) {
2712 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2713 ConflictingDecls.data(),
2714 ConflictingDecls.size());
2715 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002716 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002717 }
2718 }
2719
2720 Expr *Init = Importer.Import(D->getInitExpr());
2721 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002722 return nullptr;
2723
Douglas Gregor98c10182010-02-12 22:17:39 +00002724 EnumConstantDecl *ToEnumerator
2725 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2726 Name.getAsIdentifierInfo(), T,
2727 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002728 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002729 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002730 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002731 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002732 return ToEnumerator;
2733}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002734
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002735Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2736 // Import the major distinguishing characteristics of this function.
2737 DeclContext *DC, *LexicalDC;
2738 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002739 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002740 NamedDecl *ToD;
2741 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002742 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002743 if (ToD)
2744 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002745
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002746 // Try to find a function in our own ("to") context with the same name, same
2747 // type, and in the same context as the function we're importing.
2748 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002749 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002750 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002751 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002752 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002753 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2754 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002755 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002756
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002757 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002758 if (FoundFunction->hasExternalFormalLinkage() &&
2759 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002760 if (Importer.IsStructurallyEquivalent(D->getType(),
2761 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002762 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002763 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002764 }
2765
2766 // FIXME: Check for overloading more carefully, e.g., by boosting
2767 // Sema::IsOverload out to the AST library.
2768
2769 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002770 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002771 continue;
2772
2773 // Complain about inconsistent function types.
2774 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002775 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002776 Importer.ToDiag(FoundFunction->getLocation(),
2777 diag::note_odr_value_here)
2778 << FoundFunction->getType();
2779 }
2780 }
2781
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002782 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002783 }
2784
2785 if (!ConflictingDecls.empty()) {
2786 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2787 ConflictingDecls.data(),
2788 ConflictingDecls.size());
2789 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002790 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002791 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002792 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002793
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002794 DeclarationNameInfo NameInfo(Name, Loc);
2795 // Import additional name location/type info.
2796 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2797
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002798 QualType FromTy = D->getType();
2799 bool usedDifferentExceptionSpec = false;
2800
2801 if (const FunctionProtoType *
2802 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2803 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2804 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2805 // FunctionDecl that we are importing the FunctionProtoType for.
2806 // To avoid an infinite recursion when importing, create the FunctionDecl
2807 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002808 if (FromEPI.ExceptionSpec.SourceDecl ||
2809 FromEPI.ExceptionSpec.SourceTemplate ||
2810 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002811 FunctionProtoType::ExtProtoInfo DefaultEPI;
2812 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002813 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002814 usedDifferentExceptionSpec = true;
2815 }
2816 }
2817
Douglas Gregorb4964f72010-02-15 23:54:17 +00002818 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002819 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002820 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002821 return nullptr;
2822
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002823 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002824 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002825 for (auto P : D->params()) {
2826 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002827 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002828 return nullptr;
2829
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002830 Parameters.push_back(ToP);
2831 }
2832
2833 // Create the imported function.
2834 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002835 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002836 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002837 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2838 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2839 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002840 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002841 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002842 FromConstructor->isExplicit(),
2843 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002844 D->isImplicit(),
2845 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002846 } else if (isa<CXXDestructorDecl>(D)) {
2847 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2848 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002849 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002850 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002851 D->isInlineSpecified(),
2852 D->isImplicit());
2853 } else if (CXXConversionDecl *FromConversion
2854 = dyn_cast<CXXConversionDecl>(D)) {
2855 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2856 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002857 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002858 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002859 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002860 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002861 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002862 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002863 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2864 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2865 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002866 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002867 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002868 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002869 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002870 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002871 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002872 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002873 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002874 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002875 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002876 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002877 D->hasWrittenPrototype(),
2878 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002879 }
John McCall3e11ebe2010-03-15 10:12:16 +00002880
2881 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002882 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002883 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002884 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002885 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2886 ToFunction->setTrivial(D->isTrivial());
2887 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002888 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002889
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002890 // Set the parameters.
2891 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002892 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002893 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002894 }
David Blaikie9c70e042011-09-21 18:16:56 +00002895 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002896
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002897 if (usedDifferentExceptionSpec) {
2898 // Update FunctionProtoType::ExtProtoInfo.
2899 QualType T = Importer.Import(D->getType());
2900 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002901 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002902 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002903 }
2904
Sean Callanan59721b32015-04-28 18:41:46 +00002905 // Import the body, if any.
2906 if (Stmt *FromBody = D->getBody()) {
2907 if (Stmt *ToBody = Importer.Import(FromBody)) {
2908 ToFunction->setBody(ToBody);
2909 }
2910 }
2911
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002912 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002913
2914 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002915 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002916
Douglas Gregor43f54792010-02-17 02:12:47 +00002917 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002918}
2919
Douglas Gregor00eace12010-02-21 18:29:16 +00002920Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2921 return VisitFunctionDecl(D);
2922}
2923
2924Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2925 return VisitCXXMethodDecl(D);
2926}
2927
2928Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2929 return VisitCXXMethodDecl(D);
2930}
2931
2932Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2933 return VisitCXXMethodDecl(D);
2934}
2935
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002936static unsigned getFieldIndex(Decl *F) {
2937 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2938 if (!Owner)
2939 return 0;
2940
2941 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002942 for (const auto *D : Owner->noload_decls()) {
2943 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002944 return Index;
2945
2946 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2947 ++Index;
2948 }
2949
2950 return Index;
2951}
2952
Douglas Gregor5c73e912010-02-11 00:48:18 +00002953Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2954 // Import the major distinguishing characteristics of a variable.
2955 DeclContext *DC, *LexicalDC;
2956 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002957 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002958 NamedDecl *ToD;
2959 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002960 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002961 if (ToD)
2962 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002963
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002964 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002965 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002966 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002967 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2968 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002969 // For anonymous fields, match up by index.
2970 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2971 continue;
2972
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002973 if (Importer.IsStructurallyEquivalent(D->getType(),
2974 FoundField->getType())) {
2975 Importer.Imported(D, FoundField);
2976 return FoundField;
2977 }
2978
2979 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2980 << Name << D->getType() << FoundField->getType();
2981 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2982 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002983 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002984 }
2985 }
2986
Douglas Gregorb4964f72010-02-15 23:54:17 +00002987 // Import the type.
2988 QualType T = Importer.Import(D->getType());
2989 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002990 return nullptr;
2991
Douglas Gregor5c73e912010-02-11 00:48:18 +00002992 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2993 Expr *BitWidth = Importer.Import(D->getBitWidth());
2994 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002995 return nullptr;
2996
Abramo Bagnaradff19302011-03-08 08:55:46 +00002997 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2998 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002999 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003000 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003001 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003002 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003003 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00003004 if (ToField->hasInClassInitializer())
3005 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003006 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003007 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003008 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003009 return ToField;
3010}
3011
Francois Pichet783dd6e2010-11-21 06:08:52 +00003012Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3013 // Import the major distinguishing characteristics of a variable.
3014 DeclContext *DC, *LexicalDC;
3015 DeclarationName Name;
3016 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003017 NamedDecl *ToD;
3018 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003019 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003020 if (ToD)
3021 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003022
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003023 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003024 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003025 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003026 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003027 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003028 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003029 // For anonymous indirect fields, match up by index.
3030 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3031 continue;
3032
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003033 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003034 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003035 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003036 Importer.Imported(D, FoundField);
3037 return FoundField;
3038 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003039
3040 // If there are more anonymous fields to check, continue.
3041 if (!Name && I < N-1)
3042 continue;
3043
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003044 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3045 << Name << D->getType() << FoundField->getType();
3046 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3047 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003048 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003049 }
3050 }
3051
Francois Pichet783dd6e2010-11-21 06:08:52 +00003052 // Import the type.
3053 QualType T = Importer.Import(D->getType());
3054 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003055 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003056
3057 NamedDecl **NamedChain =
3058 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3059
3060 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003061 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003062 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003063 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003064 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003065 NamedChain[i++] = cast<NamedDecl>(D);
3066 }
3067
3068 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003069 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3070 NamedChain, D->getChainingSize());
3071
3072 for (const auto *Attr : D->attrs())
3073 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3074
Francois Pichet783dd6e2010-11-21 06:08:52 +00003075 ToIndirectField->setAccess(D->getAccess());
3076 ToIndirectField->setLexicalDeclContext(LexicalDC);
3077 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003078 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003079 return ToIndirectField;
3080}
3081
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003082Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3083 // Import the major distinguishing characteristics of an ivar.
3084 DeclContext *DC, *LexicalDC;
3085 DeclarationName Name;
3086 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003087 NamedDecl *ToD;
3088 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003089 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003090 if (ToD)
3091 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003092
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003093 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003094 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003095 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003096 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3097 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003098 if (Importer.IsStructurallyEquivalent(D->getType(),
3099 FoundIvar->getType())) {
3100 Importer.Imported(D, FoundIvar);
3101 return FoundIvar;
3102 }
3103
3104 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3105 << Name << D->getType() << FoundIvar->getType();
3106 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3107 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003108 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003109 }
3110 }
3111
3112 // Import the type.
3113 QualType T = Importer.Import(D->getType());
3114 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003115 return nullptr;
3116
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003117 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3118 Expr *BitWidth = Importer.Import(D->getBitWidth());
3119 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003120 return nullptr;
3121
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003122 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3123 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003124 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003125 Loc, Name.getAsIdentifierInfo(),
3126 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003127 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003128 ToIvar->setLexicalDeclContext(LexicalDC);
3129 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003130 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003131 return ToIvar;
3132
3133}
3134
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003135Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3136 // Import the major distinguishing characteristics of a variable.
3137 DeclContext *DC, *LexicalDC;
3138 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003139 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003140 NamedDecl *ToD;
3141 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003142 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003143 if (ToD)
3144 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003145
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003146 // Try to find a variable in our own ("to") context with the same name and
3147 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003148 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003149 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003150 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003151 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003152 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003153 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003154 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3155 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003156 continue;
3157
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003158 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003159 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003160 if (FoundVar->hasExternalFormalLinkage() &&
3161 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003162 if (Importer.IsStructurallyEquivalent(D->getType(),
3163 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003164 MergeWithVar = FoundVar;
3165 break;
3166 }
3167
Douglas Gregor56521c52010-02-12 17:23:39 +00003168 const ArrayType *FoundArray
3169 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3170 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003171 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003172 if (FoundArray && TArray) {
3173 if (isa<IncompleteArrayType>(FoundArray) &&
3174 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003175 // Import the type.
3176 QualType T = Importer.Import(D->getType());
3177 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003178 return nullptr;
3179
Douglas Gregor56521c52010-02-12 17:23:39 +00003180 FoundVar->setType(T);
3181 MergeWithVar = FoundVar;
3182 break;
3183 } else if (isa<IncompleteArrayType>(TArray) &&
3184 isa<ConstantArrayType>(FoundArray)) {
3185 MergeWithVar = FoundVar;
3186 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003187 }
3188 }
3189
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003190 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003191 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003192 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3193 << FoundVar->getType();
3194 }
3195 }
3196
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003197 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003198 }
3199
3200 if (MergeWithVar) {
3201 // An equivalent variable with external linkage has been found. Link
3202 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003203 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003204
3205 if (VarDecl *DDef = D->getDefinition()) {
3206 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3207 Importer.ToDiag(ExistingDef->getLocation(),
3208 diag::err_odr_variable_multiple_def)
3209 << Name;
3210 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3211 } else {
3212 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003213 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003214 if (DDef->isInitKnownICE()) {
3215 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3216 Eval->CheckedICE = true;
3217 Eval->IsICE = DDef->isInitICE();
3218 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003219 }
3220 }
3221
3222 return MergeWithVar;
3223 }
3224
3225 if (!ConflictingDecls.empty()) {
3226 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3227 ConflictingDecls.data(),
3228 ConflictingDecls.size());
3229 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003230 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003231 }
3232 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003233
Douglas Gregorb4964f72010-02-15 23:54:17 +00003234 // Import the type.
3235 QualType T = Importer.Import(D->getType());
3236 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003237 return nullptr;
3238
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003239 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003240 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003241 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3242 Importer.Import(D->getInnerLocStart()),
3243 Loc, Name.getAsIdentifierInfo(),
3244 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003245 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003246 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003247 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003248 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003249 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003250 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003251
Sean Callanan59721b32015-04-28 18:41:46 +00003252 if (!D->isFileVarDecl() &&
3253 D->isUsed())
3254 ToVar->setIsUsed();
3255
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003256 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003257 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003258 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003259
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003260 return ToVar;
3261}
3262
Douglas Gregor8b228d72010-02-17 21:22:52 +00003263Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3264 // Parameters are created in the translation unit's context, then moved
3265 // into the function declaration's context afterward.
3266 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3267
3268 // Import the name of this declaration.
3269 DeclarationName Name = Importer.Import(D->getDeclName());
3270 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003271 return nullptr;
3272
Douglas Gregor8b228d72010-02-17 21:22:52 +00003273 // Import the location of this declaration.
3274 SourceLocation Loc = Importer.Import(D->getLocation());
3275
3276 // Import the parameter's type.
3277 QualType T = Importer.Import(D->getType());
3278 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003279 return nullptr;
3280
Douglas Gregor8b228d72010-02-17 21:22:52 +00003281 // Create the imported parameter.
3282 ImplicitParamDecl *ToParm
3283 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3284 Loc, Name.getAsIdentifierInfo(),
3285 T);
3286 return Importer.Imported(D, ToParm);
3287}
3288
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003289Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3290 // Parameters are created in the translation unit's context, then moved
3291 // into the function declaration's context afterward.
3292 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3293
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003294 // Import the name of this declaration.
3295 DeclarationName Name = Importer.Import(D->getDeclName());
3296 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003297 return nullptr;
3298
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003299 // Import the location of this declaration.
3300 SourceLocation Loc = Importer.Import(D->getLocation());
3301
3302 // Import the parameter's type.
3303 QualType T = Importer.Import(D->getType());
3304 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003305 return nullptr;
3306
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003307 // Create the imported parameter.
3308 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3309 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003310 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003311 Loc, Name.getAsIdentifierInfo(),
3312 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003313 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003314 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003315
3316 if (D->isUsed())
3317 ToParm->setIsUsed();
3318
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003319 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003320}
3321
Douglas Gregor43f54792010-02-17 02:12:47 +00003322Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3323 // Import the major distinguishing characteristics of a method.
3324 DeclContext *DC, *LexicalDC;
3325 DeclarationName Name;
3326 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003327 NamedDecl *ToD;
3328 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003329 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003330 if (ToD)
3331 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003332
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003333 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003334 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003335 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3336 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003337 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3338 continue;
3339
3340 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003341 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3342 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003343 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003344 << D->isInstanceMethod() << Name << D->getReturnType()
3345 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003346 Importer.ToDiag(FoundMethod->getLocation(),
3347 diag::note_odr_objc_method_here)
3348 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003349 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003350 }
3351
3352 // Check the number of parameters.
3353 if (D->param_size() != FoundMethod->param_size()) {
3354 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3355 << D->isInstanceMethod() << Name
3356 << D->param_size() << FoundMethod->param_size();
3357 Importer.ToDiag(FoundMethod->getLocation(),
3358 diag::note_odr_objc_method_here)
3359 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003360 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003361 }
3362
3363 // Check parameter types.
3364 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3365 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3366 P != PEnd; ++P, ++FoundP) {
3367 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3368 (*FoundP)->getType())) {
3369 Importer.FromDiag((*P)->getLocation(),
3370 diag::err_odr_objc_method_param_type_inconsistent)
3371 << D->isInstanceMethod() << Name
3372 << (*P)->getType() << (*FoundP)->getType();
3373 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3374 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003375 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003376 }
3377 }
3378
3379 // Check variadic/non-variadic.
3380 // Check the number of parameters.
3381 if (D->isVariadic() != FoundMethod->isVariadic()) {
3382 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3383 << D->isInstanceMethod() << Name;
3384 Importer.ToDiag(FoundMethod->getLocation(),
3385 diag::note_odr_objc_method_here)
3386 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003387 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003388 }
3389
3390 // FIXME: Any other bits we need to merge?
3391 return Importer.Imported(D, FoundMethod);
3392 }
3393 }
3394
3395 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003396 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003397 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003398 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003399
Alp Toker314cc812014-01-25 16:55:45 +00003400 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003401
Alp Toker314cc812014-01-25 16:55:45 +00003402 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3403 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3404 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3405 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3406 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003407
3408 // FIXME: When we decide to merge method definitions, we'll need to
3409 // deal with implicit parameters.
3410
3411 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003412 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003413 for (auto *FromP : D->params()) {
3414 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003415 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003416 return nullptr;
3417
Douglas Gregor43f54792010-02-17 02:12:47 +00003418 ToParams.push_back(ToP);
3419 }
3420
3421 // Set the parameters.
3422 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3423 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003424 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003425 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003426 SmallVector<SourceLocation, 12> SelLocs;
3427 D->getSelectorLocs(SelLocs);
3428 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003429
3430 ToMethod->setLexicalDeclContext(LexicalDC);
3431 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003432 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003433 return ToMethod;
3434}
3435
Douglas Gregor85f3f952015-07-07 03:57:15 +00003436Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3437 // Import the major distinguishing characteristics of a category.
3438 DeclContext *DC, *LexicalDC;
3439 DeclarationName Name;
3440 SourceLocation Loc;
3441 NamedDecl *ToD;
3442 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3443 return nullptr;
3444 if (ToD)
3445 return ToD;
3446
3447 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3448 if (!BoundInfo)
3449 return nullptr;
3450
3451 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3452 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003453 D->getVariance(),
3454 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003455 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003456 Importer.Import(D->getLocation()),
3457 Name.getAsIdentifierInfo(),
3458 Importer.Import(D->getColonLoc()),
3459 BoundInfo);
3460 Importer.Imported(D, Result);
3461 Result->setLexicalDeclContext(LexicalDC);
3462 return Result;
3463}
3464
Douglas Gregor84c51c32010-02-18 01:47:50 +00003465Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3466 // Import the major distinguishing characteristics of a category.
3467 DeclContext *DC, *LexicalDC;
3468 DeclarationName Name;
3469 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003470 NamedDecl *ToD;
3471 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003472 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003473 if (ToD)
3474 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003475
Douglas Gregor84c51c32010-02-18 01:47:50 +00003476 ObjCInterfaceDecl *ToInterface
3477 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3478 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003479 return nullptr;
3480
Douglas Gregor84c51c32010-02-18 01:47:50 +00003481 // Determine if we've already encountered this category.
3482 ObjCCategoryDecl *MergeWithCategory
3483 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3484 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3485 if (!ToCategory) {
3486 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003487 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003488 Loc,
3489 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003490 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003491 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003492 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003493 Importer.Import(D->getIvarLBraceLoc()),
3494 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003495 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003496 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003497 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003498 // Import the type parameter list after calling Imported, to avoid
3499 // loops when bringing in their DeclContext.
3500 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3501 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003502
Douglas Gregor84c51c32010-02-18 01:47:50 +00003503 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003504 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3505 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003506 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3507 = D->protocol_loc_begin();
3508 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3509 FromProtoEnd = D->protocol_end();
3510 FromProto != FromProtoEnd;
3511 ++FromProto, ++FromProtoLoc) {
3512 ObjCProtocolDecl *ToProto
3513 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3514 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003515 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003516 Protocols.push_back(ToProto);
3517 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3518 }
3519
3520 // FIXME: If we're merging, make sure that the protocol list is the same.
3521 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3522 ProtocolLocs.data(), Importer.getToContext());
3523
3524 } else {
3525 Importer.Imported(D, ToCategory);
3526 }
3527
3528 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003529 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003530
3531 // If we have an implementation, import it as well.
3532 if (D->getImplementation()) {
3533 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003534 = cast_or_null<ObjCCategoryImplDecl>(
3535 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003536 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003537 return nullptr;
3538
Douglas Gregor84c51c32010-02-18 01:47:50 +00003539 ToCategory->setImplementation(Impl);
3540 }
3541
3542 return ToCategory;
3543}
3544
Douglas Gregor2aa53772012-01-24 17:42:07 +00003545bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3546 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003547 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003548 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003549 if (shouldForceImportDeclContext(Kind))
3550 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003551 return false;
3552 }
3553
3554 // Start the protocol definition
3555 To->startDefinition();
3556
3557 // Import protocols
3558 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3559 SmallVector<SourceLocation, 4> ProtocolLocs;
3560 ObjCProtocolDecl::protocol_loc_iterator
3561 FromProtoLoc = From->protocol_loc_begin();
3562 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3563 FromProtoEnd = From->protocol_end();
3564 FromProto != FromProtoEnd;
3565 ++FromProto, ++FromProtoLoc) {
3566 ObjCProtocolDecl *ToProto
3567 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3568 if (!ToProto)
3569 return true;
3570 Protocols.push_back(ToProto);
3571 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3572 }
3573
3574 // FIXME: If we're merging, make sure that the protocol list is the same.
3575 To->setProtocolList(Protocols.data(), Protocols.size(),
3576 ProtocolLocs.data(), Importer.getToContext());
3577
Douglas Gregor2e15c842012-02-01 21:00:38 +00003578 if (shouldForceImportDeclContext(Kind)) {
3579 // Import all of the members of this protocol.
3580 ImportDeclContext(From, /*ForceImport=*/true);
3581 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003582 return false;
3583}
3584
Douglas Gregor98d156a2010-02-17 16:12:00 +00003585Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003586 // If this protocol has a definition in the translation unit we're coming
3587 // from, but this particular declaration is not that definition, import the
3588 // definition and map to that.
3589 ObjCProtocolDecl *Definition = D->getDefinition();
3590 if (Definition && Definition != D) {
3591 Decl *ImportedDef = Importer.Import(Definition);
3592 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003593 return nullptr;
3594
Douglas Gregor2aa53772012-01-24 17:42:07 +00003595 return Importer.Imported(D, ImportedDef);
3596 }
3597
Douglas Gregor84c51c32010-02-18 01:47:50 +00003598 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003599 DeclContext *DC, *LexicalDC;
3600 DeclarationName Name;
3601 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003602 NamedDecl *ToD;
3603 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003604 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003605 if (ToD)
3606 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003607
Craig Topper36250ad2014-05-12 05:36:57 +00003608 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003609 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003610 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003611 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3612 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003613 continue;
3614
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003615 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003616 break;
3617 }
3618
3619 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003620 if (!ToProto) {
3621 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3622 Name.getAsIdentifierInfo(), Loc,
3623 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003624 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003625 ToProto->setLexicalDeclContext(LexicalDC);
3626 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003627 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003628
3629 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003630
Douglas Gregor2aa53772012-01-24 17:42:07 +00003631 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003632 return nullptr;
3633
Douglas Gregor98d156a2010-02-17 16:12:00 +00003634 return ToProto;
3635}
3636
Sean Callanan0aae0412014-12-10 00:00:37 +00003637Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3638 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3639 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3640
3641 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3642 SourceLocation LangLoc = Importer.Import(D->getLocation());
3643
3644 bool HasBraces = D->hasBraces();
3645
Sean Callananb12a8552014-12-10 21:22:20 +00003646 LinkageSpecDecl *ToLinkageSpec =
3647 LinkageSpecDecl::Create(Importer.getToContext(),
3648 DC,
3649 ExternLoc,
3650 LangLoc,
3651 D->getLanguage(),
3652 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003653
3654 if (HasBraces) {
3655 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3656 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3657 }
3658
3659 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3660 LexicalDC->addDeclInternal(ToLinkageSpec);
3661
3662 Importer.Imported(D, ToLinkageSpec);
3663
3664 return ToLinkageSpec;
3665}
3666
Douglas Gregor2aa53772012-01-24 17:42:07 +00003667bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3668 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003669 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003670 if (To->getDefinition()) {
3671 // Check consistency of superclass.
3672 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3673 if (FromSuper) {
3674 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3675 if (!FromSuper)
3676 return true;
3677 }
3678
3679 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3680 if ((bool)FromSuper != (bool)ToSuper ||
3681 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3682 Importer.ToDiag(To->getLocation(),
3683 diag::err_odr_objc_superclass_inconsistent)
3684 << To->getDeclName();
3685 if (ToSuper)
3686 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3687 << To->getSuperClass()->getDeclName();
3688 else
3689 Importer.ToDiag(To->getLocation(),
3690 diag::note_odr_objc_missing_superclass);
3691 if (From->getSuperClass())
3692 Importer.FromDiag(From->getSuperClassLoc(),
3693 diag::note_odr_objc_superclass)
3694 << From->getSuperClass()->getDeclName();
3695 else
3696 Importer.FromDiag(From->getLocation(),
3697 diag::note_odr_objc_missing_superclass);
3698 }
3699
Douglas Gregor2e15c842012-02-01 21:00:38 +00003700 if (shouldForceImportDeclContext(Kind))
3701 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003702 return false;
3703 }
3704
3705 // Start the definition.
3706 To->startDefinition();
3707
3708 // If this class has a superclass, import it.
3709 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003710 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3711 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003712 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003713
3714 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003715 }
3716
3717 // Import protocols
3718 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3719 SmallVector<SourceLocation, 4> ProtocolLocs;
3720 ObjCInterfaceDecl::protocol_loc_iterator
3721 FromProtoLoc = From->protocol_loc_begin();
3722
3723 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3724 FromProtoEnd = From->protocol_end();
3725 FromProto != FromProtoEnd;
3726 ++FromProto, ++FromProtoLoc) {
3727 ObjCProtocolDecl *ToProto
3728 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3729 if (!ToProto)
3730 return true;
3731 Protocols.push_back(ToProto);
3732 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3733 }
3734
3735 // FIXME: If we're merging, make sure that the protocol list is the same.
3736 To->setProtocolList(Protocols.data(), Protocols.size(),
3737 ProtocolLocs.data(), Importer.getToContext());
3738
3739 // Import categories. When the categories themselves are imported, they'll
3740 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003741 for (auto *Cat : From->known_categories())
3742 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003743
Douglas Gregor2aa53772012-01-24 17:42:07 +00003744 // If we have an @implementation, import it as well.
3745 if (From->getImplementation()) {
3746 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3747 Importer.Import(From->getImplementation()));
3748 if (!Impl)
3749 return true;
3750
3751 To->setImplementation(Impl);
3752 }
3753
Douglas Gregor2e15c842012-02-01 21:00:38 +00003754 if (shouldForceImportDeclContext(Kind)) {
3755 // Import all of the members of this class.
3756 ImportDeclContext(From, /*ForceImport=*/true);
3757 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003758 return false;
3759}
3760
Douglas Gregor85f3f952015-07-07 03:57:15 +00003761ObjCTypeParamList *
3762ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3763 if (!list)
3764 return nullptr;
3765
3766 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3767 for (auto fromTypeParam : *list) {
3768 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3769 Importer.Import(fromTypeParam));
3770 if (!toTypeParam)
3771 return nullptr;
3772
3773 toTypeParams.push_back(toTypeParam);
3774 }
3775
3776 return ObjCTypeParamList::create(Importer.getToContext(),
3777 Importer.Import(list->getLAngleLoc()),
3778 toTypeParams,
3779 Importer.Import(list->getRAngleLoc()));
3780}
3781
Douglas Gregor45635322010-02-16 01:20:57 +00003782Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003783 // If this class has a definition in the translation unit we're coming from,
3784 // but this particular declaration is not that definition, import the
3785 // definition and map to that.
3786 ObjCInterfaceDecl *Definition = D->getDefinition();
3787 if (Definition && Definition != D) {
3788 Decl *ImportedDef = Importer.Import(Definition);
3789 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003790 return nullptr;
3791
Douglas Gregor2aa53772012-01-24 17:42:07 +00003792 return Importer.Imported(D, ImportedDef);
3793 }
3794
Douglas Gregor45635322010-02-16 01:20:57 +00003795 // Import the major distinguishing characteristics of an @interface.
3796 DeclContext *DC, *LexicalDC;
3797 DeclarationName Name;
3798 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003799 NamedDecl *ToD;
3800 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003801 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003802 if (ToD)
3803 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003804
Douglas Gregor2aa53772012-01-24 17:42:07 +00003805 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003806 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003807 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003808 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003809 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3810 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003811 continue;
3812
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003813 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003814 break;
3815 }
3816
Douglas Gregor2aa53772012-01-24 17:42:07 +00003817 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003818 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003819 if (!ToIface) {
3820 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3821 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003822 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003823 /*TypeParamList=*/nullptr,
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 Gregorab7f0b32015-07-07 06:20:12 +00003830 // Import the type parameter list after calling Imported, to avoid
3831 // loops when bringing in their DeclContext.
3832 ToIface->setTypeParamList(ImportObjCTypeParamList(
3833 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003834
Douglas Gregor2aa53772012-01-24 17:42:07 +00003835 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003836 return nullptr;
3837
Douglas Gregor98d156a2010-02-17 16:12:00 +00003838 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003839}
3840
Douglas Gregor4da9d682010-12-07 15:32:12 +00003841Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3842 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3843 Importer.Import(D->getCategoryDecl()));
3844 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003845 return nullptr;
3846
Douglas Gregor4da9d682010-12-07 15:32:12 +00003847 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3848 if (!ToImpl) {
3849 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3850 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003851 return nullptr;
3852
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003853 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003854 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003855 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003856 Category->getClassInterface(),
3857 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003858 Importer.Import(D->getAtStartLoc()),
3859 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003860
3861 DeclContext *LexicalDC = DC;
3862 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3863 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3864 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003865 return nullptr;
3866
Douglas Gregor4da9d682010-12-07 15:32:12 +00003867 ToImpl->setLexicalDeclContext(LexicalDC);
3868 }
3869
Sean Callanan95e74be2011-10-21 02:57:43 +00003870 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003871 Category->setImplementation(ToImpl);
3872 }
3873
3874 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003875 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003876 return ToImpl;
3877}
3878
Douglas Gregorda8025c2010-12-07 01:26:03 +00003879Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3880 // Find the corresponding interface.
3881 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3882 Importer.Import(D->getClassInterface()));
3883 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003884 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003885
3886 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003887 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003888 if (D->getSuperClass()) {
3889 Super = cast_or_null<ObjCInterfaceDecl>(
3890 Importer.Import(D->getSuperClass()));
3891 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003892 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003893 }
3894
3895 ObjCImplementationDecl *Impl = Iface->getImplementation();
3896 if (!Impl) {
3897 // We haven't imported an implementation yet. Create a new @implementation
3898 // now.
3899 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3900 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003901 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003902 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003903 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003904 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003905 Importer.Import(D->getIvarLBraceLoc()),
3906 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003907
3908 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3909 DeclContext *LexicalDC
3910 = Importer.ImportContext(D->getLexicalDeclContext());
3911 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003912 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003913 Impl->setLexicalDeclContext(LexicalDC);
3914 }
3915
3916 // Associate the implementation with the class it implements.
3917 Iface->setImplementation(Impl);
3918 Importer.Imported(D, Iface->getImplementation());
3919 } else {
3920 Importer.Imported(D, Iface->getImplementation());
3921
3922 // Verify that the existing @implementation has the same superclass.
3923 if ((Super && !Impl->getSuperClass()) ||
3924 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003925 (Super && Impl->getSuperClass() &&
3926 !declaresSameEntity(Super->getCanonicalDecl(),
3927 Impl->getSuperClass()))) {
3928 Importer.ToDiag(Impl->getLocation(),
3929 diag::err_odr_objc_superclass_inconsistent)
3930 << Iface->getDeclName();
3931 // FIXME: It would be nice to have the location of the superclass
3932 // below.
3933 if (Impl->getSuperClass())
3934 Importer.ToDiag(Impl->getLocation(),
3935 diag::note_odr_objc_superclass)
3936 << Impl->getSuperClass()->getDeclName();
3937 else
3938 Importer.ToDiag(Impl->getLocation(),
3939 diag::note_odr_objc_missing_superclass);
3940 if (D->getSuperClass())
3941 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003942 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003943 << D->getSuperClass()->getDeclName();
3944 else
3945 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003946 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003947 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003948 }
3949 }
3950
3951 // Import all of the members of this @implementation.
3952 ImportDeclContext(D);
3953
3954 return Impl;
3955}
3956
Douglas Gregora11c4582010-02-17 18:02:10 +00003957Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3958 // Import the major distinguishing characteristics of an @property.
3959 DeclContext *DC, *LexicalDC;
3960 DeclarationName Name;
3961 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003962 NamedDecl *ToD;
3963 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003964 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003965 if (ToD)
3966 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003967
3968 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003969 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003970 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003971 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003972 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003973 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003974 // Check property types.
3975 if (!Importer.IsStructurallyEquivalent(D->getType(),
3976 FoundProp->getType())) {
3977 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3978 << Name << D->getType() << FoundProp->getType();
3979 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3980 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003981 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003982 }
3983
3984 // FIXME: Check property attributes, getters, setters, etc.?
3985
3986 // Consider these properties to be equivalent.
3987 Importer.Imported(D, FoundProp);
3988 return FoundProp;
3989 }
3990 }
3991
3992 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003993 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3994 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003995 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003996
3997 // Create the new property.
3998 ObjCPropertyDecl *ToProperty
3999 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4000 Name.getAsIdentifierInfo(),
4001 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004002 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004003 Importer.Import(D->getType()),
4004 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004005 D->getPropertyImplementation());
4006 Importer.Imported(D, ToProperty);
4007 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004008 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004009
4010 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004011 ToProperty->setPropertyAttributesAsWritten(
4012 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004013 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4014 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4015 ToProperty->setGetterMethodDecl(
4016 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4017 ToProperty->setSetterMethodDecl(
4018 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4019 ToProperty->setPropertyIvarDecl(
4020 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4021 return ToProperty;
4022}
4023
Douglas Gregor14a49e22010-12-07 18:32:03 +00004024Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4025 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4026 Importer.Import(D->getPropertyDecl()));
4027 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004028 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004029
4030 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4031 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004032 return nullptr;
4033
Douglas Gregor14a49e22010-12-07 18:32:03 +00004034 // Import the lexical declaration context.
4035 DeclContext *LexicalDC = DC;
4036 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4037 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4038 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004039 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004040 }
4041
4042 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4043 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004044 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004045
4046 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004047 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004048 if (D->getPropertyIvarDecl()) {
4049 Ivar = cast_or_null<ObjCIvarDecl>(
4050 Importer.Import(D->getPropertyIvarDecl()));
4051 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004052 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004053 }
4054
4055 ObjCPropertyImplDecl *ToImpl
4056 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
4057 if (!ToImpl) {
4058 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4059 Importer.Import(D->getLocStart()),
4060 Importer.Import(D->getLocation()),
4061 Property,
4062 D->getPropertyImplementation(),
4063 Ivar,
4064 Importer.Import(D->getPropertyIvarDeclLoc()));
4065 ToImpl->setLexicalDeclContext(LexicalDC);
4066 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004067 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004068 } else {
4069 // Check that we have the same kind of property implementation (@synthesize
4070 // vs. @dynamic).
4071 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4072 Importer.ToDiag(ToImpl->getLocation(),
4073 diag::err_odr_objc_property_impl_kind_inconsistent)
4074 << Property->getDeclName()
4075 << (ToImpl->getPropertyImplementation()
4076 == ObjCPropertyImplDecl::Dynamic);
4077 Importer.FromDiag(D->getLocation(),
4078 diag::note_odr_objc_property_impl_kind)
4079 << D->getPropertyDecl()->getDeclName()
4080 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004081 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004082 }
4083
4084 // For @synthesize, check that we have the same
4085 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4086 Ivar != ToImpl->getPropertyIvarDecl()) {
4087 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4088 diag::err_odr_objc_synthesize_ivar_inconsistent)
4089 << Property->getDeclName()
4090 << ToImpl->getPropertyIvarDecl()->getDeclName()
4091 << Ivar->getDeclName();
4092 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4093 diag::note_odr_objc_synthesize_ivar_here)
4094 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004095 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004096 }
4097
4098 // Merge the existing implementation with the new implementation.
4099 Importer.Imported(D, ToImpl);
4100 }
4101
4102 return ToImpl;
4103}
4104
Douglas Gregora082a492010-11-30 19:14:50 +00004105Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4106 // For template arguments, we adopt the translation unit as our declaration
4107 // context. This context will be fixed when the actual template declaration
4108 // is created.
4109
4110 // FIXME: Import default argument.
4111 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4112 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004113 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004114 Importer.Import(D->getLocation()),
4115 D->getDepth(),
4116 D->getIndex(),
4117 Importer.Import(D->getIdentifier()),
4118 D->wasDeclaredWithTypename(),
4119 D->isParameterPack());
4120}
4121
4122Decl *
4123ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4124 // Import the name of this declaration.
4125 DeclarationName Name = Importer.Import(D->getDeclName());
4126 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004127 return nullptr;
4128
Douglas Gregora082a492010-11-30 19:14:50 +00004129 // Import the location of this declaration.
4130 SourceLocation Loc = Importer.Import(D->getLocation());
4131
4132 // Import the type of this declaration.
4133 QualType T = Importer.Import(D->getType());
4134 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004135 return nullptr;
4136
Douglas Gregora082a492010-11-30 19:14:50 +00004137 // Import type-source information.
4138 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4139 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004140 return nullptr;
4141
Douglas Gregora082a492010-11-30 19:14:50 +00004142 // FIXME: Import default argument.
4143
4144 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4145 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004146 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004147 Loc, D->getDepth(), D->getPosition(),
4148 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004149 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004150}
4151
4152Decl *
4153ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4154 // Import the name of this declaration.
4155 DeclarationName Name = Importer.Import(D->getDeclName());
4156 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004157 return nullptr;
4158
Douglas Gregora082a492010-11-30 19:14:50 +00004159 // Import the location of this declaration.
4160 SourceLocation Loc = Importer.Import(D->getLocation());
4161
4162 // Import template parameters.
4163 TemplateParameterList *TemplateParams
4164 = ImportTemplateParameterList(D->getTemplateParameters());
4165 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004166 return nullptr;
4167
Douglas Gregora082a492010-11-30 19:14:50 +00004168 // FIXME: Import default argument.
4169
4170 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4171 Importer.getToContext().getTranslationUnitDecl(),
4172 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004173 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004174 Name.getAsIdentifierInfo(),
4175 TemplateParams);
4176}
4177
4178Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4179 // If this record has a definition in the translation unit we're coming from,
4180 // but this particular declaration is not that definition, import the
4181 // definition and map to that.
4182 CXXRecordDecl *Definition
4183 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4184 if (Definition && Definition != D->getTemplatedDecl()) {
4185 Decl *ImportedDef
4186 = Importer.Import(Definition->getDescribedClassTemplate());
4187 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004188 return nullptr;
4189
Douglas Gregora082a492010-11-30 19:14:50 +00004190 return Importer.Imported(D, ImportedDef);
4191 }
4192
4193 // Import the major distinguishing characteristics of this class template.
4194 DeclContext *DC, *LexicalDC;
4195 DeclarationName Name;
4196 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004197 NamedDecl *ToD;
4198 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004199 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004200 if (ToD)
4201 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004202
Douglas Gregora082a492010-11-30 19:14:50 +00004203 // We may already have a template of the same name; try to find and match it.
4204 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004205 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004206 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004207 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004208 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4209 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004210 continue;
4211
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004212 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004213 if (ClassTemplateDecl *FoundTemplate
4214 = dyn_cast<ClassTemplateDecl>(Found)) {
4215 if (IsStructuralMatch(D, FoundTemplate)) {
4216 // The class templates structurally match; call it the same template.
4217 // FIXME: We may be filling in a forward declaration here. Handle
4218 // this case!
4219 Importer.Imported(D->getTemplatedDecl(),
4220 FoundTemplate->getTemplatedDecl());
4221 return Importer.Imported(D, FoundTemplate);
4222 }
4223 }
4224
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004225 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004226 }
4227
4228 if (!ConflictingDecls.empty()) {
4229 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4230 ConflictingDecls.data(),
4231 ConflictingDecls.size());
4232 }
4233
4234 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004235 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004236 }
4237
4238 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4239
4240 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004241 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4242 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004243 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4244 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004245 DC, StartLoc, IdLoc,
4246 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004247 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004248 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004249 D2Templated->setLexicalDeclContext(LexicalDC);
4250
4251 // Create the class template declaration itself.
4252 TemplateParameterList *TemplateParams
4253 = ImportTemplateParameterList(D->getTemplateParameters());
4254 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004255 return nullptr;
4256
Douglas Gregora082a492010-11-30 19:14:50 +00004257 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4258 Loc, Name, TemplateParams,
4259 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004260 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004261 D2Templated->setDescribedClassTemplate(D2);
4262
4263 D2->setAccess(D->getAccess());
4264 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004265 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004266
4267 // Note the relationship between the class templates.
4268 Importer.Imported(D, D2);
4269 Importer.Imported(DTemplated, D2Templated);
4270
John McCallf937c022011-10-07 06:10:15 +00004271 if (DTemplated->isCompleteDefinition() &&
4272 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004273 // FIXME: Import definition!
4274 }
4275
4276 return D2;
4277}
4278
Douglas Gregore2e50d332010-12-01 01:36:18 +00004279Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4280 ClassTemplateSpecializationDecl *D) {
4281 // If this record has a definition in the translation unit we're coming from,
4282 // but this particular declaration is not that definition, import the
4283 // definition and map to that.
4284 TagDecl *Definition = D->getDefinition();
4285 if (Definition && Definition != D) {
4286 Decl *ImportedDef = Importer.Import(Definition);
4287 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004288 return nullptr;
4289
Douglas Gregore2e50d332010-12-01 01:36:18 +00004290 return Importer.Imported(D, ImportedDef);
4291 }
4292
4293 ClassTemplateDecl *ClassTemplate
4294 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4295 D->getSpecializedTemplate()));
4296 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004297 return nullptr;
4298
Douglas Gregore2e50d332010-12-01 01:36:18 +00004299 // Import the context of this declaration.
4300 DeclContext *DC = ClassTemplate->getDeclContext();
4301 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004302 return nullptr;
4303
Douglas Gregore2e50d332010-12-01 01:36:18 +00004304 DeclContext *LexicalDC = DC;
4305 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4306 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4307 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004308 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004309 }
4310
4311 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004312 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4313 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004314
4315 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004316 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004317 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4318 D->getTemplateArgs().size(),
4319 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004320 return nullptr;
4321
Douglas Gregore2e50d332010-12-01 01:36:18 +00004322 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004323 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004324 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004325 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004326 if (D2) {
4327 // We already have a class template specialization with these template
4328 // arguments.
4329
4330 // FIXME: Check for specialization vs. instantiation errors.
4331
4332 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004333 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004334 // The record types structurally match, or the "from" translation
4335 // unit only had a forward declaration anyway; call it the same
4336 // function.
4337 return Importer.Imported(D, FoundDef);
4338 }
4339 }
4340 } else {
4341 // Create a new specialization.
4342 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4343 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004344 StartLoc, IdLoc,
4345 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004346 TemplateArgs.data(),
4347 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004348 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004349 D2->setSpecializationKind(D->getSpecializationKind());
4350
4351 // Add this specialization to the class template.
4352 ClassTemplate->AddSpecialization(D2, InsertPos);
4353
4354 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004355 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004356
4357 // Add the specialization to this context.
4358 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004359 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004360 }
4361 Importer.Imported(D, D2);
4362
John McCallf937c022011-10-07 06:10:15 +00004363 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004364 return nullptr;
4365
Douglas Gregore2e50d332010-12-01 01:36:18 +00004366 return D2;
4367}
4368
Larisse Voufo39a1e502013-08-06 01:03:05 +00004369Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4370 // If this variable has a definition in the translation unit we're coming
4371 // from,
4372 // but this particular declaration is not that definition, import the
4373 // definition and map to that.
4374 VarDecl *Definition =
4375 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4376 if (Definition && Definition != D->getTemplatedDecl()) {
4377 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4378 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004379 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004380
4381 return Importer.Imported(D, ImportedDef);
4382 }
4383
4384 // Import the major distinguishing characteristics of this variable template.
4385 DeclContext *DC, *LexicalDC;
4386 DeclarationName Name;
4387 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004388 NamedDecl *ToD;
4389 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004390 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004391 if (ToD)
4392 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004393
4394 // We may already have a template of the same name; try to find and match it.
4395 assert(!DC->isFunctionOrMethod() &&
4396 "Variable templates cannot be declared at function scope");
4397 SmallVector<NamedDecl *, 4> ConflictingDecls;
4398 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004399 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004400 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4401 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4402 continue;
4403
4404 Decl *Found = FoundDecls[I];
4405 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4406 if (IsStructuralMatch(D, FoundTemplate)) {
4407 // The variable templates structurally match; call it the same template.
4408 Importer.Imported(D->getTemplatedDecl(),
4409 FoundTemplate->getTemplatedDecl());
4410 return Importer.Imported(D, FoundTemplate);
4411 }
4412 }
4413
4414 ConflictingDecls.push_back(FoundDecls[I]);
4415 }
4416
4417 if (!ConflictingDecls.empty()) {
4418 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4419 ConflictingDecls.data(),
4420 ConflictingDecls.size());
4421 }
4422
4423 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004424 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004425
4426 VarDecl *DTemplated = D->getTemplatedDecl();
4427
4428 // Import the type.
4429 QualType T = Importer.Import(DTemplated->getType());
4430 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004431 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004432
4433 // Create the declaration that is being templated.
4434 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4435 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4436 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4437 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4438 IdLoc, Name.getAsIdentifierInfo(), T,
4439 TInfo, DTemplated->getStorageClass());
4440 D2Templated->setAccess(DTemplated->getAccess());
4441 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4442 D2Templated->setLexicalDeclContext(LexicalDC);
4443
4444 // Importer.Imported(DTemplated, D2Templated);
4445 // LexicalDC->addDeclInternal(D2Templated);
4446
4447 // Merge the initializer.
4448 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004449 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004450
4451 // Create the variable template declaration itself.
4452 TemplateParameterList *TemplateParams =
4453 ImportTemplateParameterList(D->getTemplateParameters());
4454 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004455 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004456
4457 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004458 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004459 D2Templated->setDescribedVarTemplate(D2);
4460
4461 D2->setAccess(D->getAccess());
4462 D2->setLexicalDeclContext(LexicalDC);
4463 LexicalDC->addDeclInternal(D2);
4464
4465 // Note the relationship between the variable templates.
4466 Importer.Imported(D, D2);
4467 Importer.Imported(DTemplated, D2Templated);
4468
4469 if (DTemplated->isThisDeclarationADefinition() &&
4470 !D2Templated->isThisDeclarationADefinition()) {
4471 // FIXME: Import definition!
4472 }
4473
4474 return D2;
4475}
4476
4477Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4478 VarTemplateSpecializationDecl *D) {
4479 // If this record has a definition in the translation unit we're coming from,
4480 // but this particular declaration is not that definition, import the
4481 // definition and map to that.
4482 VarDecl *Definition = D->getDefinition();
4483 if (Definition && Definition != D) {
4484 Decl *ImportedDef = Importer.Import(Definition);
4485 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004486 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004487
4488 return Importer.Imported(D, ImportedDef);
4489 }
4490
4491 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4492 Importer.Import(D->getSpecializedTemplate()));
4493 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004494 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004495
4496 // Import the context of this declaration.
4497 DeclContext *DC = VarTemplate->getDeclContext();
4498 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004499 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004500
4501 DeclContext *LexicalDC = DC;
4502 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4503 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4504 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004505 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004506 }
4507
4508 // Import the location of this declaration.
4509 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4510 SourceLocation IdLoc = Importer.Import(D->getLocation());
4511
4512 // Import template arguments.
4513 SmallVector<TemplateArgument, 2> TemplateArgs;
4514 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4515 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004516 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004517
4518 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004519 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004520 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004521 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004522 if (D2) {
4523 // We already have a variable template specialization with these template
4524 // arguments.
4525
4526 // FIXME: Check for specialization vs. instantiation errors.
4527
4528 if (VarDecl *FoundDef = D2->getDefinition()) {
4529 if (!D->isThisDeclarationADefinition() ||
4530 IsStructuralMatch(D, FoundDef)) {
4531 // The record types structurally match, or the "from" translation
4532 // unit only had a forward declaration anyway; call it the same
4533 // variable.
4534 return Importer.Imported(D, FoundDef);
4535 }
4536 }
4537 } else {
4538
4539 // Import the type.
4540 QualType T = Importer.Import(D->getType());
4541 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004542 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004543 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4544
4545 // Create a new specialization.
4546 D2 = VarTemplateSpecializationDecl::Create(
4547 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4548 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4549 D2->setSpecializationKind(D->getSpecializationKind());
4550 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4551
4552 // Add this specialization to the class template.
4553 VarTemplate->AddSpecialization(D2, InsertPos);
4554
4555 // Import the qualifier, if any.
4556 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4557
4558 // Add the specialization to this context.
4559 D2->setLexicalDeclContext(LexicalDC);
4560 LexicalDC->addDeclInternal(D2);
4561 }
4562 Importer.Imported(D, D2);
4563
4564 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004565 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004566
4567 return D2;
4568}
4569
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004570//----------------------------------------------------------------------------
4571// Import Statements
4572//----------------------------------------------------------------------------
4573
Sean Callanan59721b32015-04-28 18:41:46 +00004574DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4575 if (DG.isNull())
4576 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4577 size_t NumDecls = DG.end() - DG.begin();
4578 SmallVector<Decl *, 1> ToDecls(NumDecls);
4579 auto &_Importer = this->Importer;
4580 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4581 [&_Importer](Decl *D) -> Decl * {
4582 return _Importer.Import(D);
4583 });
4584 return DeclGroupRef::Create(Importer.getToContext(),
4585 ToDecls.begin(),
4586 NumDecls);
4587}
4588
4589 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4590 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4591 << S->getStmtClassName();
4592 return nullptr;
4593 }
4594
4595Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4596 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4597 for (Decl *ToD : ToDG) {
4598 if (!ToD)
4599 return nullptr;
4600 }
4601 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4602 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4603 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4604}
4605
4606Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4607 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4608 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4609 S->hasLeadingEmptyMacro());
4610}
4611
4612Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4613 SmallVector<Stmt *, 4> ToStmts(S->size());
4614 auto &_Importer = this->Importer;
4615 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4616 [&_Importer](Stmt *CS) -> Stmt * {
4617 return _Importer.Import(CS);
4618 });
4619 for (Stmt *ToS : ToStmts) {
4620 if (!ToS)
4621 return nullptr;
4622 }
4623 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4624 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4625 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4626 ToStmts,
4627 ToLBraceLoc, ToRBraceLoc);
4628}
4629
4630Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4631 Expr *ToLHS = Importer.Import(S->getLHS());
4632 if (!ToLHS)
4633 return nullptr;
4634 Expr *ToRHS = Importer.Import(S->getRHS());
4635 if (!ToRHS && S->getRHS())
4636 return nullptr;
4637 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4638 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4639 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4640 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4641 ToCaseLoc, ToEllipsisLoc,
4642 ToColonLoc);
4643}
4644
4645Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4646 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4647 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4648 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4649 if (!ToSubStmt && S->getSubStmt())
4650 return nullptr;
4651 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4652 ToSubStmt);
4653}
4654
4655Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4656 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4657 LabelDecl *ToLabelDecl =
4658 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4659 if (!ToLabelDecl && S->getDecl())
4660 return nullptr;
4661 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4662 if (!ToSubStmt && S->getSubStmt())
4663 return nullptr;
4664 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4665 ToSubStmt);
4666}
4667
4668Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4669 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4670 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4671 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4672 ASTContext &_ToContext = Importer.getToContext();
4673 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4674 [&_ToContext](const Attr *A) -> const Attr * {
4675 return A->clone(_ToContext);
4676 });
4677 for (const Attr *ToA : ToAttrs) {
4678 if (!ToA)
4679 return nullptr;
4680 }
4681 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4682 if (!ToSubStmt && S->getSubStmt())
4683 return nullptr;
4684 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4685 ToAttrs, ToSubStmt);
4686}
4687
4688Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4689 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4690 VarDecl *ToConditionVariable = nullptr;
4691 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4692 ToConditionVariable =
4693 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4694 if (!ToConditionVariable)
4695 return nullptr;
4696 }
4697 Expr *ToCondition = Importer.Import(S->getCond());
4698 if (!ToCondition && S->getCond())
4699 return nullptr;
4700 Stmt *ToThenStmt = Importer.Import(S->getThen());
4701 if (!ToThenStmt && S->getThen())
4702 return nullptr;
4703 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4704 Stmt *ToElseStmt = Importer.Import(S->getElse());
4705 if (!ToElseStmt && S->getElse())
4706 return nullptr;
4707 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4708 ToIfLoc, ToConditionVariable,
4709 ToCondition, ToThenStmt,
4710 ToElseLoc, ToElseStmt);
4711}
4712
4713Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4714 VarDecl *ToConditionVariable = nullptr;
4715 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4716 ToConditionVariable =
4717 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4718 if (!ToConditionVariable)
4719 return nullptr;
4720 }
4721 Expr *ToCondition = Importer.Import(S->getCond());
4722 if (!ToCondition && S->getCond())
4723 return nullptr;
4724 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4725 Importer.getToContext(), ToConditionVariable,
4726 ToCondition);
4727 Stmt *ToBody = Importer.Import(S->getBody());
4728 if (!ToBody && S->getBody())
4729 return nullptr;
4730 ToStmt->setBody(ToBody);
4731 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4732 // Now we have to re-chain the cases.
4733 SwitchCase *LastChainedSwitchCase = nullptr;
4734 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4735 SC = SC->getNextSwitchCase()) {
4736 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4737 if (!ToSC)
4738 return nullptr;
4739 if (LastChainedSwitchCase)
4740 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4741 else
4742 ToStmt->setSwitchCaseList(ToSC);
4743 LastChainedSwitchCase = ToSC;
4744 }
4745 return ToStmt;
4746}
4747
4748Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4749 VarDecl *ToConditionVariable = nullptr;
4750 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4751 ToConditionVariable =
4752 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4753 if (!ToConditionVariable)
4754 return nullptr;
4755 }
4756 Expr *ToCondition = Importer.Import(S->getCond());
4757 if (!ToCondition && S->getCond())
4758 return nullptr;
4759 Stmt *ToBody = Importer.Import(S->getBody());
4760 if (!ToBody && S->getBody())
4761 return nullptr;
4762 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4763 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4764 ToConditionVariable,
4765 ToCondition, ToBody,
4766 ToWhileLoc);
4767}
4768
4769Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4770 Stmt *ToBody = Importer.Import(S->getBody());
4771 if (!ToBody && S->getBody())
4772 return nullptr;
4773 Expr *ToCondition = Importer.Import(S->getCond());
4774 if (!ToCondition && S->getCond())
4775 return nullptr;
4776 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4777 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4778 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4779 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4780 ToDoLoc, ToWhileLoc,
4781 ToRParenLoc);
4782}
4783
4784Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4785 Stmt *ToInit = Importer.Import(S->getInit());
4786 if (!ToInit && S->getInit())
4787 return nullptr;
4788 Expr *ToCondition = Importer.Import(S->getCond());
4789 if (!ToCondition && S->getCond())
4790 return nullptr;
4791 VarDecl *ToConditionVariable = nullptr;
4792 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4793 ToConditionVariable =
4794 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4795 if (!ToConditionVariable)
4796 return nullptr;
4797 }
4798 Expr *ToInc = Importer.Import(S->getInc());
4799 if (!ToInc && S->getInc())
4800 return nullptr;
4801 Stmt *ToBody = Importer.Import(S->getBody());
4802 if (!ToBody && S->getBody())
4803 return nullptr;
4804 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4805 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4806 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4807 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4808 ToInit, ToCondition,
4809 ToConditionVariable,
4810 ToInc, ToBody,
4811 ToForLoc, ToLParenLoc,
4812 ToRParenLoc);
4813}
4814
4815Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4816 LabelDecl *ToLabel = nullptr;
4817 if (LabelDecl *FromLabel = S->getLabel()) {
4818 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4819 if (!ToLabel)
4820 return nullptr;
4821 }
4822 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4823 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4824 return new (Importer.getToContext()) GotoStmt(ToLabel,
4825 ToGotoLoc, ToLabelLoc);
4826}
4827
4828Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4829 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4830 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4831 Expr *ToTarget = Importer.Import(S->getTarget());
4832 if (!ToTarget && S->getTarget())
4833 return nullptr;
4834 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4835 ToTarget);
4836}
4837
4838Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4839 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4840 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4841}
4842
4843Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4844 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4845 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4846}
4847
4848Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4849 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4850 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4851 if (!ToRetExpr && S->getRetValue())
4852 return nullptr;
4853 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4854 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4855 if (!ToNRVOCandidate && NRVOCandidate)
4856 return nullptr;
4857 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4858 ToNRVOCandidate);
4859}
4860
4861Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4862 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4863 VarDecl *ToExceptionDecl = nullptr;
4864 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4865 ToExceptionDecl =
4866 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4867 if (!ToExceptionDecl)
4868 return nullptr;
4869 }
4870 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4871 if (!ToHandlerBlock && S->getHandlerBlock())
4872 return nullptr;
4873 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4874 ToExceptionDecl,
4875 ToHandlerBlock);
4876}
4877
4878Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4879 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4880 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4881 if (!ToTryBlock && S->getTryBlock())
4882 return nullptr;
4883 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4884 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4885 CXXCatchStmt *FromHandler = S->getHandler(HI);
4886 if (Stmt *ToHandler = Importer.Import(FromHandler))
4887 ToHandlers[HI] = ToHandler;
4888 else
4889 return nullptr;
4890 }
4891 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4892 ToHandlers);
4893}
4894
4895Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4896 DeclStmt *ToRange =
4897 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4898 if (!ToRange && S->getRangeStmt())
4899 return nullptr;
4900 DeclStmt *ToBeginEnd =
4901 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4902 if (!ToBeginEnd && S->getBeginEndStmt())
4903 return nullptr;
4904 Expr *ToCond = Importer.Import(S->getCond());
4905 if (!ToCond && S->getCond())
4906 return nullptr;
4907 Expr *ToInc = Importer.Import(S->getInc());
4908 if (!ToInc && S->getInc())
4909 return nullptr;
4910 DeclStmt *ToLoopVar =
4911 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4912 if (!ToLoopVar && S->getLoopVarStmt())
4913 return nullptr;
4914 Stmt *ToBody = Importer.Import(S->getBody());
4915 if (!ToBody && S->getBody())
4916 return nullptr;
4917 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4918 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4919 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4920 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4921 ToCond, ToInc,
4922 ToLoopVar, ToBody,
4923 ToForLoc, ToColonLoc,
4924 ToRParenLoc);
4925}
4926
4927Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4928 Stmt *ToElem = Importer.Import(S->getElement());
4929 if (!ToElem && S->getElement())
4930 return nullptr;
4931 Expr *ToCollect = Importer.Import(S->getCollection());
4932 if (!ToCollect && S->getCollection())
4933 return nullptr;
4934 Stmt *ToBody = Importer.Import(S->getBody());
4935 if (!ToBody && S->getBody())
4936 return nullptr;
4937 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4938 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4939 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4940 ToCollect,
4941 ToBody, ToForLoc,
4942 ToRParenLoc);
4943}
4944
4945Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4946 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4947 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4948 VarDecl *ToExceptionDecl = nullptr;
4949 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4950 ToExceptionDecl =
4951 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4952 if (!ToExceptionDecl)
4953 return nullptr;
4954 }
4955 Stmt *ToBody = Importer.Import(S->getCatchBody());
4956 if (!ToBody && S->getCatchBody())
4957 return nullptr;
4958 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4959 ToRParenLoc,
4960 ToExceptionDecl,
4961 ToBody);
4962}
4963
4964Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4965 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4966 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4967 if (!ToAtFinallyStmt && S->getFinallyBody())
4968 return nullptr;
4969 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4970 ToAtFinallyStmt);
4971}
4972
4973Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4974 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4975 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4976 if (!ToAtTryStmt && S->getTryBody())
4977 return nullptr;
4978 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4979 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4980 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4981 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4982 ToCatchStmts[CI] = ToCatchStmt;
4983 else
4984 return nullptr;
4985 }
4986 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4987 if (!ToAtFinallyStmt && S->getFinallyStmt())
4988 return nullptr;
4989 return ObjCAtTryStmt::Create(Importer.getToContext(),
4990 ToAtTryLoc, ToAtTryStmt,
4991 ToCatchStmts.begin(), ToCatchStmts.size(),
4992 ToAtFinallyStmt);
4993}
4994
4995Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4996 (ObjCAtSynchronizedStmt *S) {
4997 SourceLocation ToAtSynchronizedLoc =
4998 Importer.Import(S->getAtSynchronizedLoc());
4999 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5000 if (!ToSynchExpr && S->getSynchExpr())
5001 return nullptr;
5002 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5003 if (!ToSynchBody && S->getSynchBody())
5004 return nullptr;
5005 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5006 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5007}
5008
5009Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5010 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5011 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5012 if (!ToThrow && S->getThrowExpr())
5013 return nullptr;
5014 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5015}
5016
5017Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5018 (ObjCAutoreleasePoolStmt *S) {
5019 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5020 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5021 if (!ToSubStmt && S->getSubStmt())
5022 return nullptr;
5023 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5024 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005025}
5026
5027//----------------------------------------------------------------------------
5028// Import Expressions
5029//----------------------------------------------------------------------------
5030Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5031 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5032 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005033 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005034}
5035
Douglas Gregor52f820e2010-02-19 01:17:02 +00005036Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005037 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5038 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005039 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005040
Craig Topper36250ad2014-05-12 05:36:57 +00005041 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005042 if (E->getDecl() != E->getFoundDecl()) {
5043 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5044 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005045 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005046 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005047
5048 QualType T = Importer.Import(E->getType());
5049 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005050 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005051
5052 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5053 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005054 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005055 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005056 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005057 Importer.Import(E->getLocation()),
5058 T, E->getValueKind(),
5059 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005060 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005061 if (E->hadMultipleCandidates())
5062 DRE->setHadMultipleCandidates(true);
5063 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005064}
5065
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005066Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5067 QualType T = Importer.Import(E->getType());
5068 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005069 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005070
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005071 return IntegerLiteral::Create(Importer.getToContext(),
5072 E->getValue(), T,
5073 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005074}
5075
Douglas Gregor623421d2010-02-18 02:21:22 +00005076Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5077 QualType T = Importer.Import(E->getType());
5078 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005079 return nullptr;
5080
Douglas Gregorfb65e592011-07-27 05:40:30 +00005081 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5082 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005083 Importer.Import(E->getLocation()));
5084}
5085
Douglas Gregorc74247e2010-02-19 01:07:06 +00005086Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5087 Expr *SubExpr = Importer.Import(E->getSubExpr());
5088 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005089 return nullptr;
5090
Douglas Gregorc74247e2010-02-19 01:07:06 +00005091 return new (Importer.getToContext())
5092 ParenExpr(Importer.Import(E->getLParen()),
5093 Importer.Import(E->getRParen()),
5094 SubExpr);
5095}
5096
5097Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5098 QualType T = Importer.Import(E->getType());
5099 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005100 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005101
5102 Expr *SubExpr = Importer.Import(E->getSubExpr());
5103 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005104 return nullptr;
5105
Douglas Gregorc74247e2010-02-19 01:07:06 +00005106 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005107 T, E->getValueKind(),
5108 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005109 Importer.Import(E->getOperatorLoc()));
5110}
5111
Peter Collingbournee190dee2011-03-11 19:24:49 +00005112Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5113 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005114 QualType ResultType = Importer.Import(E->getType());
5115
5116 if (E->isArgumentType()) {
5117 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5118 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005119 return nullptr;
5120
Peter Collingbournee190dee2011-03-11 19:24:49 +00005121 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5122 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005123 Importer.Import(E->getOperatorLoc()),
5124 Importer.Import(E->getRParenLoc()));
5125 }
5126
5127 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5128 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005129 return nullptr;
5130
Peter Collingbournee190dee2011-03-11 19:24:49 +00005131 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5132 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005133 Importer.Import(E->getOperatorLoc()),
5134 Importer.Import(E->getRParenLoc()));
5135}
5136
Douglas Gregorc74247e2010-02-19 01:07:06 +00005137Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5138 QualType T = Importer.Import(E->getType());
5139 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005140 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005141
5142 Expr *LHS = Importer.Import(E->getLHS());
5143 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005144 return nullptr;
5145
Douglas Gregorc74247e2010-02-19 01:07:06 +00005146 Expr *RHS = Importer.Import(E->getRHS());
5147 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005148 return nullptr;
5149
Douglas Gregorc74247e2010-02-19 01:07:06 +00005150 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005151 T, E->getValueKind(),
5152 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005153 Importer.Import(E->getOperatorLoc()),
5154 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005155}
5156
5157Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5158 QualType T = Importer.Import(E->getType());
5159 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005160 return nullptr;
5161
Douglas Gregorc74247e2010-02-19 01:07:06 +00005162 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5163 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005164 return nullptr;
5165
Douglas Gregorc74247e2010-02-19 01:07:06 +00005166 QualType CompResultType = Importer.Import(E->getComputationResultType());
5167 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005168 return nullptr;
5169
Douglas Gregorc74247e2010-02-19 01:07:06 +00005170 Expr *LHS = Importer.Import(E->getLHS());
5171 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005172 return nullptr;
5173
Douglas Gregorc74247e2010-02-19 01:07:06 +00005174 Expr *RHS = Importer.Import(E->getRHS());
5175 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005176 return nullptr;
5177
Douglas Gregorc74247e2010-02-19 01:07:06 +00005178 return new (Importer.getToContext())
5179 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005180 T, E->getValueKind(),
5181 E->getObjectKind(),
5182 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005183 Importer.Import(E->getOperatorLoc()),
5184 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005185}
5186
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005187static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005188 if (E->path_empty()) return false;
5189
5190 // TODO: import cast paths
5191 return true;
5192}
5193
Douglas Gregor98c10182010-02-12 22:17:39 +00005194Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5195 QualType T = Importer.Import(E->getType());
5196 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005197 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005198
5199 Expr *SubExpr = Importer.Import(E->getSubExpr());
5200 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005201 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005202
5203 CXXCastPath BasePath;
5204 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005205 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005206
5207 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005208 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005209}
5210
Douglas Gregor5481d322010-02-19 01:32:14 +00005211Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5212 QualType T = Importer.Import(E->getType());
5213 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005214 return nullptr;
5215
Douglas Gregor5481d322010-02-19 01:32:14 +00005216 Expr *SubExpr = Importer.Import(E->getSubExpr());
5217 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005218 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005219
5220 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5221 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005222 return nullptr;
5223
John McCallcf142162010-08-07 06:22:56 +00005224 CXXCastPath BasePath;
5225 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005226 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005227
John McCall7decc9e2010-11-18 06:31:45 +00005228 return CStyleCastExpr::Create(Importer.getToContext(), T,
5229 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005230 SubExpr, &BasePath, TInfo,
5231 Importer.Import(E->getLParenLoc()),
5232 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005233}
5234
Sean Callanan59721b32015-04-28 18:41:46 +00005235Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5236 QualType T = Importer.Import(E->getType());
5237 if (T.isNull())
5238 return nullptr;
5239
5240 CXXConstructorDecl *ToCCD =
5241 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5242 if (!ToCCD && E->getConstructor())
5243 return nullptr;
5244
5245 size_t NumArgs = E->getNumArgs();
5246 SmallVector<Expr *, 1> ToArgs(NumArgs);
5247 ASTImporter &_Importer = Importer;
5248 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5249 [&_Importer](Expr *AE) -> Expr * {
5250 return _Importer.Import(AE);
5251 });
5252 for (Expr *ToA : ToArgs) {
5253 if (!ToA)
5254 return nullptr;
5255 }
5256
5257 return CXXConstructExpr::Create(Importer.getToContext(), T,
5258 Importer.Import(E->getLocation()),
5259 ToCCD, E->isElidable(),
5260 ToArgs, E->hadMultipleCandidates(),
5261 E->isListInitialization(),
5262 E->isStdInitListInitialization(),
5263 E->requiresZeroInitialization(),
5264 E->getConstructionKind(),
5265 Importer.Import(E->getParenOrBraceRange()));
5266}
5267
5268Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5269 QualType T = Importer.Import(E->getType());
5270 if (T.isNull())
5271 return nullptr;
5272
5273 Expr *ToBase = Importer.Import(E->getBase());
5274 if (!ToBase && E->getBase())
5275 return nullptr;
5276
5277 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5278 if (!ToMember && E->getMemberDecl())
5279 return nullptr;
5280
5281 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5282 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5283 E->getFoundDecl().getAccess());
5284
5285 DeclarationNameInfo ToMemberNameInfo(
5286 Importer.Import(E->getMemberNameInfo().getName()),
5287 Importer.Import(E->getMemberNameInfo().getLoc()));
5288
5289 if (E->hasExplicitTemplateArgs()) {
5290 return nullptr; // FIXME: handle template arguments
5291 }
5292
5293 return MemberExpr::Create(Importer.getToContext(), ToBase,
5294 E->isArrow(),
5295 Importer.Import(E->getOperatorLoc()),
5296 Importer.Import(E->getQualifierLoc()),
5297 Importer.Import(E->getTemplateKeywordLoc()),
5298 ToMember, ToFoundDecl, ToMemberNameInfo,
5299 nullptr, T, E->getValueKind(),
5300 E->getObjectKind());
5301}
5302
5303Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5304 QualType T = Importer.Import(E->getType());
5305 if (T.isNull())
5306 return nullptr;
5307
5308 Expr *ToCallee = Importer.Import(E->getCallee());
5309 if (!ToCallee && E->getCallee())
5310 return nullptr;
5311
5312 unsigned NumArgs = E->getNumArgs();
5313
5314 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5315
5316 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5317 Expr *FromArg = E->getArg(ai);
5318 Expr *ToArg = Importer.Import(FromArg);
5319 if (!ToArg)
5320 return nullptr;
5321 ToArgs[ai] = ToArg;
5322 }
5323
5324 Expr **ToArgs_Copied = new (Importer.getToContext())
5325 Expr*[NumArgs];
5326
5327 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5328 ToArgs_Copied[ai] = ToArgs[ai];
5329
5330 return new (Importer.getToContext())
5331 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005332 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005333 Importer.Import(E->getRParenLoc()));
5334}
5335
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005336ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005337 ASTContext &FromContext, FileManager &FromFileManager,
5338 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005339 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005340 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005341 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005342{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005343 ImportedDecls[FromContext.getTranslationUnitDecl()]
5344 = ToContext.getTranslationUnitDecl();
5345}
5346
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005347ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005348
5349QualType ASTImporter::Import(QualType FromT) {
5350 if (FromT.isNull())
5351 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005352
5353 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005354
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005355 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005356 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5357 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005358 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005359 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005360
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005361 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005362 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005363 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005364 if (ToT.isNull())
5365 return ToT;
5366
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005367 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005368 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005369
John McCall424cec92011-01-19 06:33:43 +00005370 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005371}
5372
Douglas Gregor62d311f2010-02-09 19:21:46 +00005373TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005374 if (!FromTSI)
5375 return FromTSI;
5376
5377 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005378 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005379 QualType T = Import(FromTSI->getType());
5380 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005381 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005382
5383 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005384 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005385}
5386
Sean Callanan59721b32015-04-28 18:41:46 +00005387Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5388 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5389 if (Pos != ImportedDecls.end()) {
5390 Decl *ToD = Pos->second;
5391 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5392 return ToD;
5393 } else {
5394 return nullptr;
5395 }
5396}
5397
Douglas Gregor62d311f2010-02-09 19:21:46 +00005398Decl *ASTImporter::Import(Decl *FromD) {
5399 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005400 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005401
Douglas Gregord451ea92011-07-29 23:31:30 +00005402 ASTNodeImporter Importer(*this);
5403
Douglas Gregor62d311f2010-02-09 19:21:46 +00005404 // Check whether we've already imported this declaration.
5405 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005406 if (Pos != ImportedDecls.end()) {
5407 Decl *ToD = Pos->second;
5408 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5409 return ToD;
5410 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005411
5412 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005413 Decl *ToD = Importer.Visit(FromD);
5414 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005415 return nullptr;
5416
Douglas Gregor62d311f2010-02-09 19:21:46 +00005417 // Record the imported declaration.
5418 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005419
5420 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5421 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005422 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005423 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005424 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005425 // When we've finished transforming a typedef, see whether it was the
5426 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005427 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005428 FromTag = AnonTagsWithPendingTypedefs.begin(),
5429 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5430 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005431 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005432 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5433 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005434 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005435 AnonTagsWithPendingTypedefs.erase(FromTag);
5436 break;
5437 }
5438 }
5439 }
5440 }
5441
Douglas Gregor62d311f2010-02-09 19:21:46 +00005442 return ToD;
5443}
5444
5445DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5446 if (!FromDC)
5447 return FromDC;
5448
Douglas Gregor95d82832012-01-24 18:36:04 +00005449 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005450 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
5452
Douglas Gregor2e15c842012-02-01 21:00:38 +00005453 // When we're using a record/enum/Objective-C class/protocol as a context, we
5454 // need it to have a definition.
5455 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005456 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005457 if (ToRecord->isCompleteDefinition()) {
5458 // Do nothing.
5459 } else if (FromRecord->isCompleteDefinition()) {
5460 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5461 ASTNodeImporter::IDK_Basic);
5462 } else {
5463 CompleteDecl(ToRecord);
5464 }
5465 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5466 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5467 if (ToEnum->isCompleteDefinition()) {
5468 // Do nothing.
5469 } else if (FromEnum->isCompleteDefinition()) {
5470 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5471 ASTNodeImporter::IDK_Basic);
5472 } else {
5473 CompleteDecl(ToEnum);
5474 }
5475 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5476 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5477 if (ToClass->getDefinition()) {
5478 // Do nothing.
5479 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5480 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5481 ASTNodeImporter::IDK_Basic);
5482 } else {
5483 CompleteDecl(ToClass);
5484 }
5485 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5486 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5487 if (ToProto->getDefinition()) {
5488 // Do nothing.
5489 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5490 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5491 ASTNodeImporter::IDK_Basic);
5492 } else {
5493 CompleteDecl(ToProto);
5494 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005495 }
5496
5497 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005498}
5499
5500Expr *ASTImporter::Import(Expr *FromE) {
5501 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005502 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005503
5504 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5505}
5506
5507Stmt *ASTImporter::Import(Stmt *FromS) {
5508 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005509 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005510
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005511 // Check whether we've already imported this declaration.
5512 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5513 if (Pos != ImportedStmts.end())
5514 return Pos->second;
5515
5516 // Import the type
5517 ASTNodeImporter Importer(*this);
5518 Stmt *ToS = Importer.Visit(FromS);
5519 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005520 return nullptr;
5521
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005522 // Record the imported declaration.
5523 ImportedStmts[FromS] = ToS;
5524 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005525}
5526
5527NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5528 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005529 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005530
Douglas Gregor90ebf252011-04-27 16:48:40 +00005531 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5532
5533 switch (FromNNS->getKind()) {
5534 case NestedNameSpecifier::Identifier:
5535 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5536 return NestedNameSpecifier::Create(ToContext, prefix, II);
5537 }
Craig Topper36250ad2014-05-12 05:36:57 +00005538 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005539
5540 case NestedNameSpecifier::Namespace:
5541 if (NamespaceDecl *NS =
5542 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5543 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5544 }
Craig Topper36250ad2014-05-12 05:36:57 +00005545 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005546
5547 case NestedNameSpecifier::NamespaceAlias:
5548 if (NamespaceAliasDecl *NSAD =
5549 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5550 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5551 }
Craig Topper36250ad2014-05-12 05:36:57 +00005552 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005553
5554 case NestedNameSpecifier::Global:
5555 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5556
Nikola Smiljanic67860242014-09-26 00:28:20 +00005557 case NestedNameSpecifier::Super:
5558 if (CXXRecordDecl *RD =
5559 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5560 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5561 }
5562 return nullptr;
5563
Douglas Gregor90ebf252011-04-27 16:48:40 +00005564 case NestedNameSpecifier::TypeSpec:
5565 case NestedNameSpecifier::TypeSpecWithTemplate: {
5566 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5567 if (!T.isNull()) {
5568 bool bTemplate = FromNNS->getKind() ==
5569 NestedNameSpecifier::TypeSpecWithTemplate;
5570 return NestedNameSpecifier::Create(ToContext, prefix,
5571 bTemplate, T.getTypePtr());
5572 }
5573 }
Craig Topper36250ad2014-05-12 05:36:57 +00005574 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005575 }
5576
5577 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005578}
5579
Douglas Gregor14454802011-02-25 02:25:35 +00005580NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5581 // FIXME: Implement!
5582 return NestedNameSpecifierLoc();
5583}
5584
Douglas Gregore2e50d332010-12-01 01:36:18 +00005585TemplateName ASTImporter::Import(TemplateName From) {
5586 switch (From.getKind()) {
5587 case TemplateName::Template:
5588 if (TemplateDecl *ToTemplate
5589 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5590 return TemplateName(ToTemplate);
5591
5592 return TemplateName();
5593
5594 case TemplateName::OverloadedTemplate: {
5595 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5596 UnresolvedSet<2> ToTemplates;
5597 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5598 E = FromStorage->end();
5599 I != E; ++I) {
5600 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5601 ToTemplates.addDecl(To);
5602 else
5603 return TemplateName();
5604 }
5605 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5606 ToTemplates.end());
5607 }
5608
5609 case TemplateName::QualifiedTemplate: {
5610 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5611 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5612 if (!Qualifier)
5613 return TemplateName();
5614
5615 if (TemplateDecl *ToTemplate
5616 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5617 return ToContext.getQualifiedTemplateName(Qualifier,
5618 QTN->hasTemplateKeyword(),
5619 ToTemplate);
5620
5621 return TemplateName();
5622 }
5623
5624 case TemplateName::DependentTemplate: {
5625 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5626 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5627 if (!Qualifier)
5628 return TemplateName();
5629
5630 if (DTN->isIdentifier()) {
5631 return ToContext.getDependentTemplateName(Qualifier,
5632 Import(DTN->getIdentifier()));
5633 }
5634
5635 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5636 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005637
5638 case TemplateName::SubstTemplateTemplateParm: {
5639 SubstTemplateTemplateParmStorage *subst
5640 = From.getAsSubstTemplateTemplateParm();
5641 TemplateTemplateParmDecl *param
5642 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5643 if (!param)
5644 return TemplateName();
5645
5646 TemplateName replacement = Import(subst->getReplacement());
5647 if (replacement.isNull()) return TemplateName();
5648
5649 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5650 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005651
5652 case TemplateName::SubstTemplateTemplateParmPack: {
5653 SubstTemplateTemplateParmPackStorage *SubstPack
5654 = From.getAsSubstTemplateTemplateParmPack();
5655 TemplateTemplateParmDecl *Param
5656 = cast_or_null<TemplateTemplateParmDecl>(
5657 Import(SubstPack->getParameterPack()));
5658 if (!Param)
5659 return TemplateName();
5660
5661 ASTNodeImporter Importer(*this);
5662 TemplateArgument ArgPack
5663 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5664 if (ArgPack.isNull())
5665 return TemplateName();
5666
5667 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5668 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005669 }
5670
5671 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005672}
5673
Douglas Gregor62d311f2010-02-09 19:21:46 +00005674SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5675 if (FromLoc.isInvalid())
5676 return SourceLocation();
5677
Douglas Gregor811663e2010-02-10 00:15:17 +00005678 SourceManager &FromSM = FromContext.getSourceManager();
5679
5680 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005681 // don't have to import macro expansions.
5682 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005683 FromLoc = FromSM.getSpellingLoc(FromLoc);
5684 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5685 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005686 FileID ToFileID = Import(Decomposed.first);
5687 if (ToFileID.isInvalid())
5688 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005689 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5690 .getLocWithOffset(Decomposed.second);
5691 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005692}
5693
5694SourceRange ASTImporter::Import(SourceRange FromRange) {
5695 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5696}
5697
Douglas Gregor811663e2010-02-10 00:15:17 +00005698FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005699 llvm::DenseMap<FileID, FileID>::iterator Pos
5700 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005701 if (Pos != ImportedFileIDs.end())
5702 return Pos->second;
5703
5704 SourceManager &FromSM = FromContext.getSourceManager();
5705 SourceManager &ToSM = ToContext.getSourceManager();
5706 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005707 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005708
5709 // Include location of this file.
5710 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5711
5712 // Map the FileID for to the "to" source manager.
5713 FileID ToID;
5714 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005715 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005716 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5717 // disk again
5718 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5719 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005720 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005721 if (!Entry)
5722 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005723 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5724 FromSLoc.getFile().getFileCharacteristic());
5725 } else {
5726 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005727 const llvm::MemoryBuffer *
5728 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005729 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005730 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005731 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005732 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005733 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005734 }
5735
5736
Sebastian Redl99219f12010-09-30 01:03:06 +00005737 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005738 return ToID;
5739}
5740
Douglas Gregor0a791672011-01-18 03:11:38 +00005741void ASTImporter::ImportDefinition(Decl *From) {
5742 Decl *To = Import(From);
5743 if (!To)
5744 return;
5745
5746 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5747 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005748
5749 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5750 if (!ToRecord->getDefinition()) {
5751 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005752 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005753 return;
5754 }
5755 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005756
5757 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5758 if (!ToEnum->getDefinition()) {
5759 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005760 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005761 return;
5762 }
5763 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005764
5765 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5766 if (!ToIFace->getDefinition()) {
5767 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005768 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005769 return;
5770 }
5771 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005772
Douglas Gregor2aa53772012-01-24 17:42:07 +00005773 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5774 if (!ToProto->getDefinition()) {
5775 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005776 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005777 return;
5778 }
5779 }
5780
Douglas Gregor0a791672011-01-18 03:11:38 +00005781 Importer.ImportDeclContext(FromDC, true);
5782 }
5783}
5784
Douglas Gregor96e578d2010-02-05 17:54:41 +00005785DeclarationName ASTImporter::Import(DeclarationName FromName) {
5786 if (!FromName)
5787 return DeclarationName();
5788
5789 switch (FromName.getNameKind()) {
5790 case DeclarationName::Identifier:
5791 return Import(FromName.getAsIdentifierInfo());
5792
5793 case DeclarationName::ObjCZeroArgSelector:
5794 case DeclarationName::ObjCOneArgSelector:
5795 case DeclarationName::ObjCMultiArgSelector:
5796 return Import(FromName.getObjCSelector());
5797
5798 case DeclarationName::CXXConstructorName: {
5799 QualType T = Import(FromName.getCXXNameType());
5800 if (T.isNull())
5801 return DeclarationName();
5802
5803 return ToContext.DeclarationNames.getCXXConstructorName(
5804 ToContext.getCanonicalType(T));
5805 }
5806
5807 case DeclarationName::CXXDestructorName: {
5808 QualType T = Import(FromName.getCXXNameType());
5809 if (T.isNull())
5810 return DeclarationName();
5811
5812 return ToContext.DeclarationNames.getCXXDestructorName(
5813 ToContext.getCanonicalType(T));
5814 }
5815
5816 case DeclarationName::CXXConversionFunctionName: {
5817 QualType T = Import(FromName.getCXXNameType());
5818 if (T.isNull())
5819 return DeclarationName();
5820
5821 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5822 ToContext.getCanonicalType(T));
5823 }
5824
5825 case DeclarationName::CXXOperatorName:
5826 return ToContext.DeclarationNames.getCXXOperatorName(
5827 FromName.getCXXOverloadedOperator());
5828
5829 case DeclarationName::CXXLiteralOperatorName:
5830 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5831 Import(FromName.getCXXLiteralIdentifier()));
5832
5833 case DeclarationName::CXXUsingDirective:
5834 // FIXME: STATICS!
5835 return DeclarationName::getUsingDirectiveName();
5836 }
5837
David Blaikiee4d798f2012-01-20 21:50:17 +00005838 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005839}
5840
Douglas Gregore2e50d332010-12-01 01:36:18 +00005841IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005842 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005843 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005844
5845 return &ToContext.Idents.get(FromId->getName());
5846}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005847
Douglas Gregor43f54792010-02-17 02:12:47 +00005848Selector ASTImporter::Import(Selector FromSel) {
5849 if (FromSel.isNull())
5850 return Selector();
5851
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005852 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005853 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5854 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5855 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5856 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5857}
5858
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005859DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5860 DeclContext *DC,
5861 unsigned IDNS,
5862 NamedDecl **Decls,
5863 unsigned NumDecls) {
5864 return Name;
5865}
5866
5867DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005868 if (LastDiagFromFrom)
5869 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5870 FromContext.getDiagnostics());
5871 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005872 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005873}
5874
5875DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005876 if (!LastDiagFromFrom)
5877 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5878 ToContext.getDiagnostics());
5879 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005880 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005881}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005882
Douglas Gregor2e15c842012-02-01 21:00:38 +00005883void ASTImporter::CompleteDecl (Decl *D) {
5884 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5885 if (!ID->getDefinition())
5886 ID->startDefinition();
5887 }
5888 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5889 if (!PD->getDefinition())
5890 PD->startDefinition();
5891 }
5892 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5893 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5894 TD->startDefinition();
5895 TD->setCompleteDefinition(true);
5896 }
5897 }
5898 else {
5899 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5900 }
5901}
5902
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005903Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5904 ImportedDecls[From] = To;
5905 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005906}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005907
Douglas Gregordd6006f2012-07-17 21:16:27 +00005908bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5909 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005910 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005911 = ImportedTypes.find(From.getTypePtr());
5912 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5913 return true;
5914
Douglas Gregordd6006f2012-07-17 21:16:27 +00005915 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5916 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005917 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005918}