blob: 76e4e119150117c86bd92e6401d272014802e4d5 [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 Gregor84c51c32010-02-18 01:47:50 +0000153 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000154 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000155 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000156 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000157 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000158 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000159 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000160 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000161 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
162 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
163 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
164 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000165 Decl *VisitClassTemplateSpecializationDecl(
166 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000167 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
168 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
169
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000170 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000171 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
172
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000173 Stmt *VisitStmt(Stmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000174 Stmt *VisitDeclStmt(DeclStmt *S);
175 Stmt *VisitNullStmt(NullStmt *S);
176 Stmt *VisitCompoundStmt(CompoundStmt *S);
177 Stmt *VisitCaseStmt(CaseStmt *S);
178 Stmt *VisitDefaultStmt(DefaultStmt *S);
179 Stmt *VisitLabelStmt(LabelStmt *S);
180 Stmt *VisitAttributedStmt(AttributedStmt *S);
181 Stmt *VisitIfStmt(IfStmt *S);
182 Stmt *VisitSwitchStmt(SwitchStmt *S);
183 Stmt *VisitWhileStmt(WhileStmt *S);
184 Stmt *VisitDoStmt(DoStmt *S);
185 Stmt *VisitForStmt(ForStmt *S);
186 Stmt *VisitGotoStmt(GotoStmt *S);
187 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
188 Stmt *VisitContinueStmt(ContinueStmt *S);
189 Stmt *VisitBreakStmt(BreakStmt *S);
190 Stmt *VisitReturnStmt(ReturnStmt *S);
191 // FIXME: GCCAsmStmt
192 // FIXME: MSAsmStmt
193 // FIXME: SEHExceptStmt
194 // FIXME: SEHFinallyStmt
195 // FIXME: SEHTryStmt
196 // FIXME: SEHLeaveStmt
197 // FIXME: CapturedStmt
198 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
199 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
200 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
201 // FIXME: MSDependentExistsStmt
202 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
203 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
204 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
205 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
206 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
207 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
208 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000209
210 // Importing expressions
211 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000212 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000213 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000214 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000215 Expr *VisitParenExpr(ParenExpr *E);
216 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000217 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000218 Expr *VisitBinaryOperator(BinaryOperator *E);
219 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000220 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000221 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000222 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
223 Expr *VisitMemberExpr(MemberExpr *E);
224 Expr *VisitCallExpr(CallExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000225 };
226}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000227using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000228
229//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000230// Structural Equivalence
231//----------------------------------------------------------------------------
232
233namespace {
234 struct StructuralEquivalenceContext {
235 /// \brief AST contexts for which we are checking structural equivalence.
236 ASTContext &C1, &C2;
237
Douglas Gregor3996e242010-02-15 22:01:00 +0000238 /// \brief The set of "tentative" equivalences between two canonical
239 /// declarations, mapping from a declaration in the first context to the
240 /// declaration in the second context that we believe to be equivalent.
241 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
242
243 /// \brief Queue of declarations in the first context whose equivalence
244 /// with a declaration in the second context still needs to be verified.
245 std::deque<Decl *> DeclsToCheck;
246
Douglas Gregorb4964f72010-02-15 23:54:17 +0000247 /// \brief Declaration (from, to) pairs that are known not to be equivalent
248 /// (which we have already complained about).
249 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
250
Douglas Gregor3996e242010-02-15 22:01:00 +0000251 /// \brief Whether we're being strict about the spelling of types when
252 /// unifying two types.
253 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000254
255 /// \brief Whether to complain about failures.
256 bool Complain;
257
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000258 /// \brief \c true if the last diagnostic came from C2.
259 bool LastDiagFromC2;
260
Douglas Gregor3996e242010-02-15 22:01:00 +0000261 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000262 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000263 bool StrictTypeSpelling = false,
264 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000265 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000266 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
267 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000268
269 /// \brief Determine whether the two declarations are structurally
270 /// equivalent.
271 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
272
273 /// \brief Determine whether the two types are structurally equivalent.
274 bool IsStructurallyEquivalent(QualType T1, QualType T2);
275
276 private:
277 /// \brief Finish checking all of the structural equivalences.
278 ///
279 /// \returns true if an error occurred, false otherwise.
280 bool Finish();
281
282 public:
283 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000284 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000285 if (LastDiagFromC2)
286 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
287 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000288 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000289 }
290
291 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000292 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000293 if (!LastDiagFromC2)
294 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
295 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000296 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000297 }
298 };
299}
300
301static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
302 QualType T1, QualType T2);
303static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
304 Decl *D1, Decl *D2);
305
Douglas Gregor3996e242010-02-15 22:01:00 +0000306/// \brief Determine structural equivalence of two expressions.
307static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
308 Expr *E1, Expr *E2) {
309 if (!E1 || !E2)
310 return E1 == E2;
311
312 // FIXME: Actually perform a structural comparison!
313 return true;
314}
315
316/// \brief Determine whether two identifiers are equivalent.
317static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
318 const IdentifierInfo *Name2) {
319 if (!Name1 || !Name2)
320 return Name1 == Name2;
321
322 return Name1->getName() == Name2->getName();
323}
324
325/// \brief Determine whether two nested-name-specifiers are equivalent.
326static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
327 NestedNameSpecifier *NNS1,
328 NestedNameSpecifier *NNS2) {
329 // FIXME: Implement!
330 return true;
331}
332
333/// \brief Determine whether two template arguments are equivalent.
334static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
335 const TemplateArgument &Arg1,
336 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000337 if (Arg1.getKind() != Arg2.getKind())
338 return false;
339
340 switch (Arg1.getKind()) {
341 case TemplateArgument::Null:
342 return true;
343
344 case TemplateArgument::Type:
345 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000346
Douglas Gregore2e50d332010-12-01 01:36:18 +0000347 case TemplateArgument::Integral:
348 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
349 Arg2.getIntegralType()))
350 return false;
351
Eric Christopher6dcc3762012-07-15 00:23:57 +0000352 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000353
354 case TemplateArgument::Declaration:
355 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000356
357 case TemplateArgument::NullPtr:
358 return true; // FIXME: Is this correct?
359
Douglas Gregore2e50d332010-12-01 01:36:18 +0000360 case TemplateArgument::Template:
361 return IsStructurallyEquivalent(Context,
362 Arg1.getAsTemplate(),
363 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000364
365 case TemplateArgument::TemplateExpansion:
366 return IsStructurallyEquivalent(Context,
367 Arg1.getAsTemplateOrTemplatePattern(),
368 Arg2.getAsTemplateOrTemplatePattern());
369
Douglas Gregore2e50d332010-12-01 01:36:18 +0000370 case TemplateArgument::Expression:
371 return IsStructurallyEquivalent(Context,
372 Arg1.getAsExpr(), Arg2.getAsExpr());
373
374 case TemplateArgument::Pack:
375 if (Arg1.pack_size() != Arg2.pack_size())
376 return false;
377
378 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
379 if (!IsStructurallyEquivalent(Context,
380 Arg1.pack_begin()[I],
381 Arg2.pack_begin()[I]))
382 return false;
383
384 return true;
385 }
386
387 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000388}
389
390/// \brief Determine structural equivalence for the common part of array
391/// types.
392static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
393 const ArrayType *Array1,
394 const ArrayType *Array2) {
395 if (!IsStructurallyEquivalent(Context,
396 Array1->getElementType(),
397 Array2->getElementType()))
398 return false;
399 if (Array1->getSizeModifier() != Array2->getSizeModifier())
400 return false;
401 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
402 return false;
403
404 return true;
405}
406
407/// \brief Determine structural equivalence of two types.
408static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
409 QualType T1, QualType T2) {
410 if (T1.isNull() || T2.isNull())
411 return T1.isNull() && T2.isNull();
412
413 if (!Context.StrictTypeSpelling) {
414 // We aren't being strict about token-to-token equivalence of types,
415 // so map down to the canonical type.
416 T1 = Context.C1.getCanonicalType(T1);
417 T2 = Context.C2.getCanonicalType(T2);
418 }
419
420 if (T1.getQualifiers() != T2.getQualifiers())
421 return false;
422
Douglas Gregorb4964f72010-02-15 23:54:17 +0000423 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000424
Douglas Gregorb4964f72010-02-15 23:54:17 +0000425 if (T1->getTypeClass() != T2->getTypeClass()) {
426 // Compare function types with prototypes vs. without prototypes as if
427 // both did not have prototypes.
428 if (T1->getTypeClass() == Type::FunctionProto &&
429 T2->getTypeClass() == Type::FunctionNoProto)
430 TC = Type::FunctionNoProto;
431 else if (T1->getTypeClass() == Type::FunctionNoProto &&
432 T2->getTypeClass() == Type::FunctionProto)
433 TC = Type::FunctionNoProto;
434 else
435 return false;
436 }
437
438 switch (TC) {
439 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000440 // FIXME: Deal with Char_S/Char_U.
441 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
442 return false;
443 break;
444
445 case Type::Complex:
446 if (!IsStructurallyEquivalent(Context,
447 cast<ComplexType>(T1)->getElementType(),
448 cast<ComplexType>(T2)->getElementType()))
449 return false;
450 break;
451
Reid Kleckner0503a872013-12-05 01:23:43 +0000452 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000453 case Type::Decayed:
454 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000455 cast<AdjustedType>(T1)->getOriginalType(),
456 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000457 return false;
458 break;
459
Douglas Gregor3996e242010-02-15 22:01:00 +0000460 case Type::Pointer:
461 if (!IsStructurallyEquivalent(Context,
462 cast<PointerType>(T1)->getPointeeType(),
463 cast<PointerType>(T2)->getPointeeType()))
464 return false;
465 break;
466
467 case Type::BlockPointer:
468 if (!IsStructurallyEquivalent(Context,
469 cast<BlockPointerType>(T1)->getPointeeType(),
470 cast<BlockPointerType>(T2)->getPointeeType()))
471 return false;
472 break;
473
474 case Type::LValueReference:
475 case Type::RValueReference: {
476 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
477 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
478 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
479 return false;
480 if (Ref1->isInnerRef() != Ref2->isInnerRef())
481 return false;
482 if (!IsStructurallyEquivalent(Context,
483 Ref1->getPointeeTypeAsWritten(),
484 Ref2->getPointeeTypeAsWritten()))
485 return false;
486 break;
487 }
488
489 case Type::MemberPointer: {
490 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
491 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
492 if (!IsStructurallyEquivalent(Context,
493 MemPtr1->getPointeeType(),
494 MemPtr2->getPointeeType()))
495 return false;
496 if (!IsStructurallyEquivalent(Context,
497 QualType(MemPtr1->getClass(), 0),
498 QualType(MemPtr2->getClass(), 0)))
499 return false;
500 break;
501 }
502
503 case Type::ConstantArray: {
504 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
505 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000506 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000507 return false;
508
509 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
510 return false;
511 break;
512 }
513
514 case Type::IncompleteArray:
515 if (!IsArrayStructurallyEquivalent(Context,
516 cast<ArrayType>(T1),
517 cast<ArrayType>(T2)))
518 return false;
519 break;
520
521 case Type::VariableArray: {
522 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
523 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
524 if (!IsStructurallyEquivalent(Context,
525 Array1->getSizeExpr(), Array2->getSizeExpr()))
526 return false;
527
528 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
529 return false;
530
531 break;
532 }
533
534 case Type::DependentSizedArray: {
535 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
536 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
537 if (!IsStructurallyEquivalent(Context,
538 Array1->getSizeExpr(), Array2->getSizeExpr()))
539 return false;
540
541 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
542 return false;
543
544 break;
545 }
546
547 case Type::DependentSizedExtVector: {
548 const DependentSizedExtVectorType *Vec1
549 = cast<DependentSizedExtVectorType>(T1);
550 const DependentSizedExtVectorType *Vec2
551 = cast<DependentSizedExtVectorType>(T2);
552 if (!IsStructurallyEquivalent(Context,
553 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
554 return false;
555 if (!IsStructurallyEquivalent(Context,
556 Vec1->getElementType(),
557 Vec2->getElementType()))
558 return false;
559 break;
560 }
561
562 case Type::Vector:
563 case Type::ExtVector: {
564 const VectorType *Vec1 = cast<VectorType>(T1);
565 const VectorType *Vec2 = cast<VectorType>(T2);
566 if (!IsStructurallyEquivalent(Context,
567 Vec1->getElementType(),
568 Vec2->getElementType()))
569 return false;
570 if (Vec1->getNumElements() != Vec2->getNumElements())
571 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000572 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000573 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000574 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000575 }
576
577 case Type::FunctionProto: {
578 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
579 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000580 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000581 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000582 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
583 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
584 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000585 return false;
586 }
587 if (Proto1->isVariadic() != Proto2->isVariadic())
588 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000589 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000590 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000591 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
592 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
593 return false;
594 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
595 if (!IsStructurallyEquivalent(Context,
596 Proto1->getExceptionType(I),
597 Proto2->getExceptionType(I)))
598 return false;
599 }
600 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000601 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000602 Proto1->getNoexceptExpr(),
603 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000604 return false;
605 }
606 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
607 return false;
608
609 // Fall through to check the bits common with FunctionNoProtoType.
610 }
611
612 case Type::FunctionNoProto: {
613 const FunctionType *Function1 = cast<FunctionType>(T1);
614 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000615 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
616 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000617 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000618 if (Function1->getExtInfo() != Function2->getExtInfo())
619 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000620 break;
621 }
622
623 case Type::UnresolvedUsing:
624 if (!IsStructurallyEquivalent(Context,
625 cast<UnresolvedUsingType>(T1)->getDecl(),
626 cast<UnresolvedUsingType>(T2)->getDecl()))
627 return false;
628
629 break;
John McCall81904512011-01-06 01:58:22 +0000630
631 case Type::Attributed:
632 if (!IsStructurallyEquivalent(Context,
633 cast<AttributedType>(T1)->getModifiedType(),
634 cast<AttributedType>(T2)->getModifiedType()))
635 return false;
636 if (!IsStructurallyEquivalent(Context,
637 cast<AttributedType>(T1)->getEquivalentType(),
638 cast<AttributedType>(T2)->getEquivalentType()))
639 return false;
640 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000641
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000642 case Type::Paren:
643 if (!IsStructurallyEquivalent(Context,
644 cast<ParenType>(T1)->getInnerType(),
645 cast<ParenType>(T2)->getInnerType()))
646 return false;
647 break;
648
Douglas Gregor3996e242010-02-15 22:01:00 +0000649 case Type::Typedef:
650 if (!IsStructurallyEquivalent(Context,
651 cast<TypedefType>(T1)->getDecl(),
652 cast<TypedefType>(T2)->getDecl()))
653 return false;
654 break;
655
656 case Type::TypeOfExpr:
657 if (!IsStructurallyEquivalent(Context,
658 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
659 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
660 return false;
661 break;
662
663 case Type::TypeOf:
664 if (!IsStructurallyEquivalent(Context,
665 cast<TypeOfType>(T1)->getUnderlyingType(),
666 cast<TypeOfType>(T2)->getUnderlyingType()))
667 return false;
668 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000669
670 case Type::UnaryTransform:
671 if (!IsStructurallyEquivalent(Context,
672 cast<UnaryTransformType>(T1)->getUnderlyingType(),
673 cast<UnaryTransformType>(T1)->getUnderlyingType()))
674 return false;
675 break;
676
Douglas Gregor3996e242010-02-15 22:01:00 +0000677 case Type::Decltype:
678 if (!IsStructurallyEquivalent(Context,
679 cast<DecltypeType>(T1)->getUnderlyingExpr(),
680 cast<DecltypeType>(T2)->getUnderlyingExpr()))
681 return false;
682 break;
683
Richard Smith30482bc2011-02-20 03:19:35 +0000684 case Type::Auto:
685 if (!IsStructurallyEquivalent(Context,
686 cast<AutoType>(T1)->getDeducedType(),
687 cast<AutoType>(T2)->getDeducedType()))
688 return false;
689 break;
690
Douglas Gregor3996e242010-02-15 22:01:00 +0000691 case Type::Record:
692 case Type::Enum:
693 if (!IsStructurallyEquivalent(Context,
694 cast<TagType>(T1)->getDecl(),
695 cast<TagType>(T2)->getDecl()))
696 return false;
697 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000698
Douglas Gregor3996e242010-02-15 22:01:00 +0000699 case Type::TemplateTypeParm: {
700 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
701 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
702 if (Parm1->getDepth() != Parm2->getDepth())
703 return false;
704 if (Parm1->getIndex() != Parm2->getIndex())
705 return false;
706 if (Parm1->isParameterPack() != Parm2->isParameterPack())
707 return false;
708
709 // Names of template type parameters are never significant.
710 break;
711 }
712
713 case Type::SubstTemplateTypeParm: {
714 const SubstTemplateTypeParmType *Subst1
715 = cast<SubstTemplateTypeParmType>(T1);
716 const SubstTemplateTypeParmType *Subst2
717 = cast<SubstTemplateTypeParmType>(T2);
718 if (!IsStructurallyEquivalent(Context,
719 QualType(Subst1->getReplacedParameter(), 0),
720 QualType(Subst2->getReplacedParameter(), 0)))
721 return false;
722 if (!IsStructurallyEquivalent(Context,
723 Subst1->getReplacementType(),
724 Subst2->getReplacementType()))
725 return false;
726 break;
727 }
728
Douglas Gregorfb322d82011-01-14 05:11:40 +0000729 case Type::SubstTemplateTypeParmPack: {
730 const SubstTemplateTypeParmPackType *Subst1
731 = cast<SubstTemplateTypeParmPackType>(T1);
732 const SubstTemplateTypeParmPackType *Subst2
733 = cast<SubstTemplateTypeParmPackType>(T2);
734 if (!IsStructurallyEquivalent(Context,
735 QualType(Subst1->getReplacedParameter(), 0),
736 QualType(Subst2->getReplacedParameter(), 0)))
737 return false;
738 if (!IsStructurallyEquivalent(Context,
739 Subst1->getArgumentPack(),
740 Subst2->getArgumentPack()))
741 return false;
742 break;
743 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000744 case Type::TemplateSpecialization: {
745 const TemplateSpecializationType *Spec1
746 = cast<TemplateSpecializationType>(T1);
747 const TemplateSpecializationType *Spec2
748 = cast<TemplateSpecializationType>(T2);
749 if (!IsStructurallyEquivalent(Context,
750 Spec1->getTemplateName(),
751 Spec2->getTemplateName()))
752 return false;
753 if (Spec1->getNumArgs() != Spec2->getNumArgs())
754 return false;
755 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
756 if (!IsStructurallyEquivalent(Context,
757 Spec1->getArg(I), Spec2->getArg(I)))
758 return false;
759 }
760 break;
761 }
762
Abramo Bagnara6150c882010-05-11 21:36:43 +0000763 case Type::Elaborated: {
764 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
765 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
766 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
767 if (Elab1->getKeyword() != Elab2->getKeyword())
768 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000769 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000770 Elab1->getQualifier(),
771 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000772 return false;
773 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000774 Elab1->getNamedType(),
775 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 return false;
777 break;
778 }
779
John McCalle78aac42010-03-10 03:28:59 +0000780 case Type::InjectedClassName: {
781 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
782 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
783 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000784 Inj1->getInjectedSpecializationType(),
785 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000786 return false;
787 break;
788 }
789
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000790 case Type::DependentName: {
791 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
792 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000793 if (!IsStructurallyEquivalent(Context,
794 Typename1->getQualifier(),
795 Typename2->getQualifier()))
796 return false;
797 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
798 Typename2->getIdentifier()))
799 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000800
801 break;
802 }
803
John McCallc392f372010-06-11 00:33:02 +0000804 case Type::DependentTemplateSpecialization: {
805 const DependentTemplateSpecializationType *Spec1 =
806 cast<DependentTemplateSpecializationType>(T1);
807 const DependentTemplateSpecializationType *Spec2 =
808 cast<DependentTemplateSpecializationType>(T2);
809 if (!IsStructurallyEquivalent(Context,
810 Spec1->getQualifier(),
811 Spec2->getQualifier()))
812 return false;
813 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
814 Spec2->getIdentifier()))
815 return false;
816 if (Spec1->getNumArgs() != Spec2->getNumArgs())
817 return false;
818 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
819 if (!IsStructurallyEquivalent(Context,
820 Spec1->getArg(I), Spec2->getArg(I)))
821 return false;
822 }
823 break;
824 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000825
826 case Type::PackExpansion:
827 if (!IsStructurallyEquivalent(Context,
828 cast<PackExpansionType>(T1)->getPattern(),
829 cast<PackExpansionType>(T2)->getPattern()))
830 return false;
831 break;
832
Douglas Gregor3996e242010-02-15 22:01:00 +0000833 case Type::ObjCInterface: {
834 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
835 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
836 if (!IsStructurallyEquivalent(Context,
837 Iface1->getDecl(), Iface2->getDecl()))
838 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000839 break;
840 }
841
842 case Type::ObjCObject: {
843 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
844 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
845 if (!IsStructurallyEquivalent(Context,
846 Obj1->getBaseType(),
847 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000848 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000849 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
850 return false;
851 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000852 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000853 Obj1->getProtocol(I),
854 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000855 return false;
856 }
857 break;
858 }
859
860 case Type::ObjCObjectPointer: {
861 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
862 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
863 if (!IsStructurallyEquivalent(Context,
864 Ptr1->getPointeeType(),
865 Ptr2->getPointeeType()))
866 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000867 break;
868 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000869
870 case Type::Atomic: {
871 if (!IsStructurallyEquivalent(Context,
872 cast<AtomicType>(T1)->getValueType(),
873 cast<AtomicType>(T2)->getValueType()))
874 return false;
875 break;
876 }
877
Douglas Gregor3996e242010-02-15 22:01:00 +0000878 } // end switch
879
880 return true;
881}
882
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000883/// \brief Determine structural equivalence of two fields.
884static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
885 FieldDecl *Field1, FieldDecl *Field2) {
886 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000887
888 // For anonymous structs/unions, match up the anonymous struct/union type
889 // declarations directly, so that we don't go off searching for anonymous
890 // types
891 if (Field1->isAnonymousStructOrUnion() &&
892 Field2->isAnonymousStructOrUnion()) {
893 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
894 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
895 return IsStructurallyEquivalent(Context, D1, D2);
896 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000897
898 // Check for equivalent field names.
899 IdentifierInfo *Name1 = Field1->getIdentifier();
900 IdentifierInfo *Name2 = Field2->getIdentifier();
901 if (!::IsStructurallyEquivalent(Name1, Name2))
902 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000903
904 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000905 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000906 if (Context.Complain) {
907 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
908 << Context.C2.getTypeDeclType(Owner2);
909 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
910 << Field2->getDeclName() << Field2->getType();
911 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
912 << Field1->getDeclName() << Field1->getType();
913 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000914 return false;
915 }
916
917 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000918 if (Context.Complain) {
919 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
920 << Context.C2.getTypeDeclType(Owner2);
921 if (Field1->isBitField()) {
922 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
923 << Field1->getDeclName() << Field1->getType()
924 << Field1->getBitWidthValue(Context.C1);
925 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
926 << Field2->getDeclName();
927 } else {
928 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
929 << Field2->getDeclName() << Field2->getType()
930 << Field2->getBitWidthValue(Context.C2);
931 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
932 << Field1->getDeclName();
933 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000934 }
935 return false;
936 }
937
938 if (Field1->isBitField()) {
939 // Make sure that the bit-fields are the same length.
940 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
941 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
942
943 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000944 if (Context.Complain) {
945 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
946 << Context.C2.getTypeDeclType(Owner2);
947 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
948 << Field2->getDeclName() << Field2->getType() << Bits2;
949 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
950 << Field1->getDeclName() << Field1->getType() << Bits1;
951 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000952 return false;
953 }
954 }
955
956 return true;
957}
958
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000959/// \brief Find the index of the given anonymous struct/union within its
960/// context.
961///
962/// \returns Returns the index of this anonymous struct/union in its context,
963/// including the next assigned index (if none of them match). Returns an
964/// empty option if the context is not a record, i.e.. if the anonymous
965/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000966static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000967 ASTContext &Context = Anon->getASTContext();
968 QualType AnonTy = Context.getRecordType(Anon);
969
970 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
971 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000972 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000973
974 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +0000975 for (const auto *D : Owner->noload_decls()) {
976 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000977 if (!F || !F->isAnonymousStructOrUnion())
978 continue;
979
980 if (Context.hasSameType(F->getType(), AnonTy))
981 break;
982
983 ++Index;
984 }
985
986 return Index;
987}
988
Douglas Gregor3996e242010-02-15 22:01:00 +0000989/// \brief Determine structural equivalence of two records.
990static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
991 RecordDecl *D1, RecordDecl *D2) {
992 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000993 if (Context.Complain) {
994 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
995 << Context.C2.getTypeDeclType(D2);
996 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
997 << D1->getDeclName() << (unsigned)D1->getTagKind();
998 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000999 return false;
1000 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001001
1002 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1003 // If both anonymous structs/unions are in a record context, make sure
1004 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001005 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1006 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001007 if (*Index1 != *Index2)
1008 return false;
1009 }
1010 }
1011 }
1012
Douglas Gregore2e50d332010-12-01 01:36:18 +00001013 // If both declarations are class template specializations, we know
1014 // the ODR applies, so check the template and template arguments.
1015 ClassTemplateSpecializationDecl *Spec1
1016 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1017 ClassTemplateSpecializationDecl *Spec2
1018 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1019 if (Spec1 && Spec2) {
1020 // Check that the specialized templates are the same.
1021 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1022 Spec2->getSpecializedTemplate()))
1023 return false;
1024
1025 // Check that the template arguments are the same.
1026 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1027 return false;
1028
1029 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1030 if (!IsStructurallyEquivalent(Context,
1031 Spec1->getTemplateArgs().get(I),
1032 Spec2->getTemplateArgs().get(I)))
1033 return false;
1034 }
1035 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001036 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001037 else if (Spec1 || Spec2)
1038 return false;
1039
Douglas Gregorb4964f72010-02-15 23:54:17 +00001040 // Compare the definitions of these two records. If either or both are
1041 // incomplete, we assume that they are equivalent.
1042 D1 = D1->getDefinition();
1043 D2 = D2->getDefinition();
1044 if (!D1 || !D2)
1045 return true;
1046
Douglas Gregor3996e242010-02-15 22:01:00 +00001047 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1048 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1049 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001050 if (Context.Complain) {
1051 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1052 << Context.C2.getTypeDeclType(D2);
1053 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1054 << D2CXX->getNumBases();
1055 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1056 << D1CXX->getNumBases();
1057 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001058 return false;
1059 }
1060
1061 // Check the base classes.
1062 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1063 BaseEnd1 = D1CXX->bases_end(),
1064 Base2 = D2CXX->bases_begin();
1065 Base1 != BaseEnd1;
1066 ++Base1, ++Base2) {
1067 if (!IsStructurallyEquivalent(Context,
1068 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001069 if (Context.Complain) {
1070 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1071 << Context.C2.getTypeDeclType(D2);
1072 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1073 << Base2->getType()
1074 << Base2->getSourceRange();
1075 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1076 << Base1->getType()
1077 << Base1->getSourceRange();
1078 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001079 return false;
1080 }
1081
1082 // Check virtual vs. non-virtual inheritance mismatch.
1083 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001084 if (Context.Complain) {
1085 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1086 << Context.C2.getTypeDeclType(D2);
1087 Context.Diag2(Base2->getLocStart(),
1088 diag::note_odr_virtual_base)
1089 << Base2->isVirtual() << Base2->getSourceRange();
1090 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1091 << Base1->isVirtual()
1092 << Base1->getSourceRange();
1093 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001094 return false;
1095 }
1096 }
1097 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001098 if (Context.Complain) {
1099 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1100 << Context.C2.getTypeDeclType(D2);
1101 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1102 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1103 << Base1->getType()
1104 << Base1->getSourceRange();
1105 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1106 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001107 return false;
1108 }
1109 }
1110
1111 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001112 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001113 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001114 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001115 Field1End = D1->field_end();
1116 Field1 != Field1End;
1117 ++Field1, ++Field2) {
1118 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001119 if (Context.Complain) {
1120 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1121 << Context.C2.getTypeDeclType(D2);
1122 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1123 << Field1->getDeclName() << Field1->getType();
1124 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1125 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001126 return false;
1127 }
1128
David Blaikie40ed2972012-06-06 20:45:41 +00001129 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001130 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001131 }
1132
1133 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001134 if (Context.Complain) {
1135 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1136 << Context.C2.getTypeDeclType(D2);
1137 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1138 << Field2->getDeclName() << Field2->getType();
1139 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1140 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001141 return false;
1142 }
1143
1144 return true;
1145}
1146
1147/// \brief Determine structural equivalence of two enums.
1148static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1149 EnumDecl *D1, EnumDecl *D2) {
1150 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1151 EC2End = D2->enumerator_end();
1152 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1153 EC1End = D1->enumerator_end();
1154 EC1 != EC1End; ++EC1, ++EC2) {
1155 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001156 if (Context.Complain) {
1157 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1158 << Context.C2.getTypeDeclType(D2);
1159 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1160 << EC1->getDeclName()
1161 << EC1->getInitVal().toString(10);
1162 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1163 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001164 return false;
1165 }
1166
1167 llvm::APSInt Val1 = EC1->getInitVal();
1168 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001169 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001170 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001171 if (Context.Complain) {
1172 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1173 << Context.C2.getTypeDeclType(D2);
1174 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1175 << EC2->getDeclName()
1176 << EC2->getInitVal().toString(10);
1177 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1178 << EC1->getDeclName()
1179 << EC1->getInitVal().toString(10);
1180 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001181 return false;
1182 }
1183 }
1184
1185 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001186 if (Context.Complain) {
1187 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1188 << Context.C2.getTypeDeclType(D2);
1189 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1190 << EC2->getDeclName()
1191 << EC2->getInitVal().toString(10);
1192 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1193 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001194 return false;
1195 }
1196
1197 return true;
1198}
Douglas Gregora082a492010-11-30 19:14:50 +00001199
1200static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1201 TemplateParameterList *Params1,
1202 TemplateParameterList *Params2) {
1203 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001204 if (Context.Complain) {
1205 Context.Diag2(Params2->getTemplateLoc(),
1206 diag::err_odr_different_num_template_parameters)
1207 << Params1->size() << Params2->size();
1208 Context.Diag1(Params1->getTemplateLoc(),
1209 diag::note_odr_template_parameter_list);
1210 }
Douglas Gregora082a492010-11-30 19:14:50 +00001211 return false;
1212 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001213
Douglas Gregora082a492010-11-30 19:14:50 +00001214 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1215 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001216 if (Context.Complain) {
1217 Context.Diag2(Params2->getParam(I)->getLocation(),
1218 diag::err_odr_different_template_parameter_kind);
1219 Context.Diag1(Params1->getParam(I)->getLocation(),
1220 diag::note_odr_template_parameter_here);
1221 }
Douglas Gregora082a492010-11-30 19:14:50 +00001222 return false;
1223 }
1224
1225 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1226 Params2->getParam(I))) {
1227
1228 return false;
1229 }
1230 }
1231
1232 return true;
1233}
1234
1235static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1236 TemplateTypeParmDecl *D1,
1237 TemplateTypeParmDecl *D2) {
1238 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001239 if (Context.Complain) {
1240 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1241 << D2->isParameterPack();
1242 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1243 << D1->isParameterPack();
1244 }
Douglas Gregora082a492010-11-30 19:14:50 +00001245 return false;
1246 }
1247
1248 return true;
1249}
1250
1251static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1252 NonTypeTemplateParmDecl *D1,
1253 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001254 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001255 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001256 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1257 << D2->isParameterPack();
1258 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1259 << D1->isParameterPack();
1260 }
Douglas Gregora082a492010-11-30 19:14:50 +00001261 return false;
1262 }
Douglas Gregora082a492010-11-30 19:14:50 +00001263
1264 // Check types.
1265 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001266 if (Context.Complain) {
1267 Context.Diag2(D2->getLocation(),
1268 diag::err_odr_non_type_parameter_type_inconsistent)
1269 << D2->getType() << D1->getType();
1270 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1271 << D1->getType();
1272 }
Douglas Gregora082a492010-11-30 19:14:50 +00001273 return false;
1274 }
1275
1276 return true;
1277}
1278
1279static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1280 TemplateTemplateParmDecl *D1,
1281 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001282 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001283 if (Context.Complain) {
1284 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1285 << D2->isParameterPack();
1286 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1287 << D1->isParameterPack();
1288 }
Douglas Gregora082a492010-11-30 19:14:50 +00001289 return false;
1290 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001291
Douglas Gregora082a492010-11-30 19:14:50 +00001292 // Check template parameter lists.
1293 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1294 D2->getTemplateParameters());
1295}
1296
1297static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1298 ClassTemplateDecl *D1,
1299 ClassTemplateDecl *D2) {
1300 // Check template parameters.
1301 if (!IsStructurallyEquivalent(Context,
1302 D1->getTemplateParameters(),
1303 D2->getTemplateParameters()))
1304 return false;
1305
1306 // Check the templated declaration.
1307 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1308 D2->getTemplatedDecl());
1309}
1310
Douglas Gregor3996e242010-02-15 22:01:00 +00001311/// \brief Determine structural equivalence of two declarations.
1312static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1313 Decl *D1, Decl *D2) {
1314 // FIXME: Check for known structural equivalences via a callback of some sort.
1315
Douglas Gregorb4964f72010-02-15 23:54:17 +00001316 // Check whether we already know that these two declarations are not
1317 // structurally equivalent.
1318 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1319 D2->getCanonicalDecl())))
1320 return false;
1321
Douglas Gregor3996e242010-02-15 22:01:00 +00001322 // Determine whether we've already produced a tentative equivalence for D1.
1323 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1324 if (EquivToD1)
1325 return EquivToD1 == D2->getCanonicalDecl();
1326
1327 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1328 EquivToD1 = D2->getCanonicalDecl();
1329 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1330 return true;
1331}
1332
1333bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1334 Decl *D2) {
1335 if (!::IsStructurallyEquivalent(*this, D1, D2))
1336 return false;
1337
1338 return !Finish();
1339}
1340
1341bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1342 QualType T2) {
1343 if (!::IsStructurallyEquivalent(*this, T1, T2))
1344 return false;
1345
1346 return !Finish();
1347}
1348
1349bool StructuralEquivalenceContext::Finish() {
1350 while (!DeclsToCheck.empty()) {
1351 // Check the next declaration.
1352 Decl *D1 = DeclsToCheck.front();
1353 DeclsToCheck.pop_front();
1354
1355 Decl *D2 = TentativeEquivalences[D1];
1356 assert(D2 && "Unrecorded tentative equivalence?");
1357
Douglas Gregorb4964f72010-02-15 23:54:17 +00001358 bool Equivalent = true;
1359
Douglas Gregor3996e242010-02-15 22:01:00 +00001360 // FIXME: Switch on all declaration kinds. For now, we're just going to
1361 // check the obvious ones.
1362 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1363 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1364 // Check for equivalent structure names.
1365 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001366 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1367 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001368 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001369 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1370 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001371 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1372 !::IsStructurallyEquivalent(*this, Record1, Record2))
1373 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001374 } else {
1375 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001376 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001377 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001378 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001379 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1380 // Check for equivalent enum names.
1381 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001382 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1383 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001384 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001385 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1386 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001387 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1388 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1389 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001390 } else {
1391 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001392 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001393 }
Richard Smithdda56e42011-04-15 14:24:37 +00001394 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1395 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001396 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001397 Typedef2->getIdentifier()) ||
1398 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001399 Typedef1->getUnderlyingType(),
1400 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001401 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001402 } else {
1403 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001404 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001405 }
Douglas Gregora082a492010-11-30 19:14:50 +00001406 } else if (ClassTemplateDecl *ClassTemplate1
1407 = dyn_cast<ClassTemplateDecl>(D1)) {
1408 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1409 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1410 ClassTemplate2->getIdentifier()) ||
1411 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1412 Equivalent = false;
1413 } else {
1414 // Class template/non-class-template mismatch.
1415 Equivalent = false;
1416 }
1417 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1418 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1419 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1420 Equivalent = false;
1421 } else {
1422 // Kind mismatch.
1423 Equivalent = false;
1424 }
1425 } else if (NonTypeTemplateParmDecl *NTTP1
1426 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1427 if (NonTypeTemplateParmDecl *NTTP2
1428 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1429 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1430 Equivalent = false;
1431 } else {
1432 // Kind mismatch.
1433 Equivalent = false;
1434 }
1435 } else if (TemplateTemplateParmDecl *TTP1
1436 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1437 if (TemplateTemplateParmDecl *TTP2
1438 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1439 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1440 Equivalent = false;
1441 } else {
1442 // Kind mismatch.
1443 Equivalent = false;
1444 }
1445 }
1446
Douglas Gregorb4964f72010-02-15 23:54:17 +00001447 if (!Equivalent) {
1448 // Note that these two declarations are not equivalent (and we already
1449 // know about it).
1450 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1451 D2->getCanonicalDecl()));
1452 return true;
1453 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001454 // FIXME: Check other declaration kinds!
1455 }
1456
1457 return false;
1458}
1459
1460//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001461// Import Types
1462//----------------------------------------------------------------------------
1463
John McCall424cec92011-01-19 06:33:43 +00001464QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001465 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1466 << T->getTypeClassName();
1467 return QualType();
1468}
1469
John McCall424cec92011-01-19 06:33:43 +00001470QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001471 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001472#define SHARED_SINGLETON_TYPE(Expansion)
1473#define BUILTIN_TYPE(Id, SingletonId) \
1474 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1475#include "clang/AST/BuiltinTypes.def"
1476
1477 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1478 // context supports C++.
1479
1480 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1481 // context supports ObjC.
1482
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 case BuiltinType::Char_U:
1484 // The context we're importing from has an unsigned 'char'. If we're
1485 // importing into a context with a signed 'char', translate to
1486 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001487 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001488 return Importer.getToContext().UnsignedCharTy;
1489
1490 return Importer.getToContext().CharTy;
1491
Douglas Gregor96e578d2010-02-05 17:54:41 +00001492 case BuiltinType::Char_S:
1493 // The context we're importing from has an unsigned 'char'. If we're
1494 // importing into a context with a signed 'char', translate to
1495 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001496 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001497 return Importer.getToContext().SignedCharTy;
1498
1499 return Importer.getToContext().CharTy;
1500
Chris Lattnerad3467e2010-12-25 23:25:43 +00001501 case BuiltinType::WChar_S:
1502 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 // FIXME: If not in C++, shall we translate to the C equivalent of
1504 // wchar_t?
1505 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001507
1508 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001509}
1510
John McCall424cec92011-01-19 06:33:43 +00001511QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001512 QualType ToElementType = Importer.Import(T->getElementType());
1513 if (ToElementType.isNull())
1514 return QualType();
1515
1516 return Importer.getToContext().getComplexType(ToElementType);
1517}
1518
John McCall424cec92011-01-19 06:33:43 +00001519QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001520 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1521 if (ToPointeeType.isNull())
1522 return QualType();
1523
1524 return Importer.getToContext().getPointerType(ToPointeeType);
1525}
1526
John McCall424cec92011-01-19 06:33:43 +00001527QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001528 // FIXME: Check for blocks support in "to" context.
1529 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1530 if (ToPointeeType.isNull())
1531 return QualType();
1532
1533 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1534}
1535
John McCall424cec92011-01-19 06:33:43 +00001536QualType
1537ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001538 // FIXME: Check for C++ support in "to" context.
1539 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1540 if (ToPointeeType.isNull())
1541 return QualType();
1542
1543 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1544}
1545
John McCall424cec92011-01-19 06:33:43 +00001546QualType
1547ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001548 // FIXME: Check for C++0x support in "to" context.
1549 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1550 if (ToPointeeType.isNull())
1551 return QualType();
1552
1553 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1554}
1555
John McCall424cec92011-01-19 06:33:43 +00001556QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001557 // FIXME: Check for C++ support in "to" context.
1558 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1559 if (ToPointeeType.isNull())
1560 return QualType();
1561
1562 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1563 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1564 ClassType.getTypePtr());
1565}
1566
John McCall424cec92011-01-19 06:33:43 +00001567QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568 QualType ToElementType = Importer.Import(T->getElementType());
1569 if (ToElementType.isNull())
1570 return QualType();
1571
1572 return Importer.getToContext().getConstantArrayType(ToElementType,
1573 T->getSize(),
1574 T->getSizeModifier(),
1575 T->getIndexTypeCVRQualifiers());
1576}
1577
John McCall424cec92011-01-19 06:33:43 +00001578QualType
1579ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001580 QualType ToElementType = Importer.Import(T->getElementType());
1581 if (ToElementType.isNull())
1582 return QualType();
1583
1584 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1585 T->getSizeModifier(),
1586 T->getIndexTypeCVRQualifiers());
1587}
1588
John McCall424cec92011-01-19 06:33:43 +00001589QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001590 QualType ToElementType = Importer.Import(T->getElementType());
1591 if (ToElementType.isNull())
1592 return QualType();
1593
1594 Expr *Size = Importer.Import(T->getSizeExpr());
1595 if (!Size)
1596 return QualType();
1597
1598 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1599 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1600 T->getSizeModifier(),
1601 T->getIndexTypeCVRQualifiers(),
1602 Brackets);
1603}
1604
John McCall424cec92011-01-19 06:33:43 +00001605QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606 QualType ToElementType = Importer.Import(T->getElementType());
1607 if (ToElementType.isNull())
1608 return QualType();
1609
1610 return Importer.getToContext().getVectorType(ToElementType,
1611 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001612 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001613}
1614
John McCall424cec92011-01-19 06:33:43 +00001615QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001616 QualType ToElementType = Importer.Import(T->getElementType());
1617 if (ToElementType.isNull())
1618 return QualType();
1619
1620 return Importer.getToContext().getExtVectorType(ToElementType,
1621 T->getNumElements());
1622}
1623
John McCall424cec92011-01-19 06:33:43 +00001624QualType
1625ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001626 // FIXME: What happens if we're importing a function without a prototype
1627 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001628 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001629 if (ToResultType.isNull())
1630 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001631
Douglas Gregor96e578d2010-02-05 17:54:41 +00001632 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001633 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001634}
1635
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001636QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001637 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 if (ToResultType.isNull())
1639 return QualType();
1640
1641 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001642 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001643 for (const auto &A : T->param_types()) {
1644 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001645 if (ArgType.isNull())
1646 return QualType();
1647 ArgTypes.push_back(ArgType);
1648 }
1649
1650 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001651 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001652 for (const auto &E : T->exceptions()) {
1653 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 if (ExceptionType.isNull())
1655 return QualType();
1656 ExceptionTypes.push_back(ExceptionType);
1657 }
John McCalldb40c7f2010-12-14 08:05:40 +00001658
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001659 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1660 FunctionProtoType::ExtProtoInfo ToEPI;
1661
1662 ToEPI.ExtInfo = FromEPI.ExtInfo;
1663 ToEPI.Variadic = FromEPI.Variadic;
1664 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1665 ToEPI.TypeQuals = FromEPI.TypeQuals;
1666 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001667 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1668 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1669 ToEPI.ExceptionSpec.NoexceptExpr =
1670 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1671 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1672 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1673 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1674 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001675
Jordan Rose5c382722013-03-08 21:51:21 +00001676 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001677}
1678
Sean Callananda6df8a2011-08-11 16:56:07 +00001679QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1680 QualType ToInnerType = Importer.Import(T->getInnerType());
1681 if (ToInnerType.isNull())
1682 return QualType();
1683
1684 return Importer.getToContext().getParenType(ToInnerType);
1685}
1686
John McCall424cec92011-01-19 06:33:43 +00001687QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001688 TypedefNameDecl *ToDecl
1689 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001690 if (!ToDecl)
1691 return QualType();
1692
1693 return Importer.getToContext().getTypeDeclType(ToDecl);
1694}
1695
John McCall424cec92011-01-19 06:33:43 +00001696QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001697 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1698 if (!ToExpr)
1699 return QualType();
1700
1701 return Importer.getToContext().getTypeOfExprType(ToExpr);
1702}
1703
John McCall424cec92011-01-19 06:33:43 +00001704QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001705 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1706 if (ToUnderlyingType.isNull())
1707 return QualType();
1708
1709 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1710}
1711
John McCall424cec92011-01-19 06:33:43 +00001712QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001713 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001714 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1715 if (!ToExpr)
1716 return QualType();
1717
Douglas Gregor81495f32012-02-12 18:42:33 +00001718 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1719 if (UnderlyingType.isNull())
1720 return QualType();
1721
1722 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001723}
1724
Alexis Hunte852b102011-05-24 22:41:36 +00001725QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1726 QualType ToBaseType = Importer.Import(T->getBaseType());
1727 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1728 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1729 return QualType();
1730
1731 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1732 ToUnderlyingType,
1733 T->getUTTKind());
1734}
1735
Richard Smith30482bc2011-02-20 03:19:35 +00001736QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001737 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001738 QualType FromDeduced = T->getDeducedType();
1739 QualType ToDeduced;
1740 if (!FromDeduced.isNull()) {
1741 ToDeduced = Importer.Import(FromDeduced);
1742 if (ToDeduced.isNull())
1743 return QualType();
1744 }
1745
Faisal Vali2b391ab2013-09-26 19:54:12 +00001746 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1747 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001748}
1749
John McCall424cec92011-01-19 06:33:43 +00001750QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001751 RecordDecl *ToDecl
1752 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1753 if (!ToDecl)
1754 return QualType();
1755
1756 return Importer.getToContext().getTagDeclType(ToDecl);
1757}
1758
John McCall424cec92011-01-19 06:33:43 +00001759QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001760 EnumDecl *ToDecl
1761 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1762 if (!ToDecl)
1763 return QualType();
1764
1765 return Importer.getToContext().getTagDeclType(ToDecl);
1766}
1767
Sean Callanan72fe0852015-04-02 23:50:08 +00001768QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1769 QualType FromModifiedType = T->getModifiedType();
1770 QualType FromEquivalentType = T->getEquivalentType();
1771 QualType ToModifiedType;
1772 QualType ToEquivalentType;
1773
1774 if (!FromModifiedType.isNull()) {
1775 ToModifiedType = Importer.Import(FromModifiedType);
1776 if (ToModifiedType.isNull())
1777 return QualType();
1778 }
1779 if (!FromEquivalentType.isNull()) {
1780 ToEquivalentType = Importer.Import(FromEquivalentType);
1781 if (ToEquivalentType.isNull())
1782 return QualType();
1783 }
1784
1785 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1786 ToModifiedType, ToEquivalentType);
1787}
1788
Douglas Gregore2e50d332010-12-01 01:36:18 +00001789QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001790 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001791 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1792 if (ToTemplate.isNull())
1793 return QualType();
1794
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001795 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001796 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1797 return QualType();
1798
1799 QualType ToCanonType;
1800 if (!QualType(T, 0).isCanonical()) {
1801 QualType FromCanonType
1802 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1803 ToCanonType =Importer.Import(FromCanonType);
1804 if (ToCanonType.isNull())
1805 return QualType();
1806 }
1807 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1808 ToTemplateArgs.data(),
1809 ToTemplateArgs.size(),
1810 ToCanonType);
1811}
1812
John McCall424cec92011-01-19 06:33:43 +00001813QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001814 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001815 // Note: the qualifier in an ElaboratedType is optional.
1816 if (T->getQualifier()) {
1817 ToQualifier = Importer.Import(T->getQualifier());
1818 if (!ToQualifier)
1819 return QualType();
1820 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001821
1822 QualType ToNamedType = Importer.Import(T->getNamedType());
1823 if (ToNamedType.isNull())
1824 return QualType();
1825
Abramo Bagnara6150c882010-05-11 21:36:43 +00001826 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1827 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001828}
1829
John McCall424cec92011-01-19 06:33:43 +00001830QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001831 ObjCInterfaceDecl *Class
1832 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1833 if (!Class)
1834 return QualType();
1835
John McCall8b07ec22010-05-15 11:32:37 +00001836 return Importer.getToContext().getObjCInterfaceType(Class);
1837}
1838
John McCall424cec92011-01-19 06:33:43 +00001839QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001840 QualType ToBaseType = Importer.Import(T->getBaseType());
1841 if (ToBaseType.isNull())
1842 return QualType();
1843
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001844 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001845 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001846 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001847 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001848 if (!Protocol)
1849 return QualType();
1850 Protocols.push_back(Protocol);
1851 }
1852
John McCall8b07ec22010-05-15 11:32:37 +00001853 return Importer.getToContext().getObjCObjectType(ToBaseType,
1854 Protocols.data(),
1855 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001856}
1857
John McCall424cec92011-01-19 06:33:43 +00001858QualType
1859ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001860 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1861 if (ToPointeeType.isNull())
1862 return QualType();
1863
John McCall8b07ec22010-05-15 11:32:37 +00001864 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001865}
1866
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001867//----------------------------------------------------------------------------
1868// Import Declarations
1869//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001870bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1871 DeclContext *&LexicalDC,
1872 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001873 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001874 SourceLocation &Loc) {
1875 // Import the context of this declaration.
1876 DC = Importer.ImportContext(D->getDeclContext());
1877 if (!DC)
1878 return true;
1879
1880 LexicalDC = DC;
1881 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1882 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1883 if (!LexicalDC)
1884 return true;
1885 }
1886
1887 // Import the name of this declaration.
1888 Name = Importer.Import(D->getDeclName());
1889 if (D->getDeclName() && !Name)
1890 return true;
1891
1892 // Import the location of this declaration.
1893 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001894 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001895 return false;
1896}
1897
Douglas Gregord451ea92011-07-29 23:31:30 +00001898void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1899 if (!FromD)
1900 return;
1901
1902 if (!ToD) {
1903 ToD = Importer.Import(FromD);
1904 if (!ToD)
1905 return;
1906 }
1907
1908 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1909 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001910 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001911 ImportDefinition(FromRecord, ToRecord);
1912 }
1913 }
1914 return;
1915 }
1916
1917 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1918 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1919 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1920 ImportDefinition(FromEnum, ToEnum);
1921 }
1922 }
1923 return;
1924 }
1925}
1926
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001927void
1928ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1929 DeclarationNameInfo& To) {
1930 // NOTE: To.Name and To.Loc are already imported.
1931 // We only have to import To.LocInfo.
1932 switch (To.getName().getNameKind()) {
1933 case DeclarationName::Identifier:
1934 case DeclarationName::ObjCZeroArgSelector:
1935 case DeclarationName::ObjCOneArgSelector:
1936 case DeclarationName::ObjCMultiArgSelector:
1937 case DeclarationName::CXXUsingDirective:
1938 return;
1939
1940 case DeclarationName::CXXOperatorName: {
1941 SourceRange Range = From.getCXXOperatorNameRange();
1942 To.setCXXOperatorNameRange(Importer.Import(Range));
1943 return;
1944 }
1945 case DeclarationName::CXXLiteralOperatorName: {
1946 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1947 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1948 return;
1949 }
1950 case DeclarationName::CXXConstructorName:
1951 case DeclarationName::CXXDestructorName:
1952 case DeclarationName::CXXConversionFunctionName: {
1953 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1954 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1955 return;
1956 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001957 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001958 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001959}
1960
Douglas Gregor2e15c842012-02-01 21:00:38 +00001961void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001962 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001963 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001964 return;
1965 }
1966
Aaron Ballman629afae2014-03-07 19:56:05 +00001967 for (auto *From : FromDC->decls())
1968 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001969}
1970
Douglas Gregord451ea92011-07-29 23:31:30 +00001971bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001972 ImportDefinitionKind Kind) {
1973 if (To->getDefinition() || To->isBeingDefined()) {
1974 if (Kind == IDK_Everything)
1975 ImportDeclContext(From, /*ForceImport=*/true);
1976
Douglas Gregore2e50d332010-12-01 01:36:18 +00001977 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001978 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001979
1980 To->startDefinition();
1981
1982 // Add base classes.
1983 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1984 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001985
1986 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1987 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1988 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001989 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001990 ToData.Aggregate = FromData.Aggregate;
1991 ToData.PlainOldData = FromData.PlainOldData;
1992 ToData.Empty = FromData.Empty;
1993 ToData.Polymorphic = FromData.Polymorphic;
1994 ToData.Abstract = FromData.Abstract;
1995 ToData.IsStandardLayout = FromData.IsStandardLayout;
1996 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1997 ToData.HasPrivateFields = FromData.HasPrivateFields;
1998 ToData.HasProtectedFields = FromData.HasProtectedFields;
1999 ToData.HasPublicFields = FromData.HasPublicFields;
2000 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002001 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002002 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002003 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002004 ToData.HasUninitializedReferenceMember
2005 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00002006 ToData.NeedOverloadResolutionForMoveConstructor
2007 = FromData.NeedOverloadResolutionForMoveConstructor;
2008 ToData.NeedOverloadResolutionForMoveAssignment
2009 = FromData.NeedOverloadResolutionForMoveAssignment;
2010 ToData.NeedOverloadResolutionForDestructor
2011 = FromData.NeedOverloadResolutionForDestructor;
2012 ToData.DefaultedMoveConstructorIsDeleted
2013 = FromData.DefaultedMoveConstructorIsDeleted;
2014 ToData.DefaultedMoveAssignmentIsDeleted
2015 = FromData.DefaultedMoveAssignmentIsDeleted;
2016 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002017 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2018 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002019 ToData.HasConstexprNonCopyMoveConstructor
2020 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002021 ToData.DefaultedDefaultConstructorIsConstexpr
2022 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002023 ToData.HasConstexprDefaultConstructor
2024 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002025 ToData.HasNonLiteralTypeFieldsOrBases
2026 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002027 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002028 ToData.UserProvidedDefaultConstructor
2029 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002030 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002031 ToData.ImplicitCopyConstructorHasConstParam
2032 = FromData.ImplicitCopyConstructorHasConstParam;
2033 ToData.ImplicitCopyAssignmentHasConstParam
2034 = FromData.ImplicitCopyAssignmentHasConstParam;
2035 ToData.HasDeclaredCopyConstructorWithConstParam
2036 = FromData.HasDeclaredCopyConstructorWithConstParam;
2037 ToData.HasDeclaredCopyAssignmentWithConstParam
2038 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002039 ToData.IsLambda = FromData.IsLambda;
2040
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002041 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002042 for (const auto &Base1 : FromCXX->bases()) {
2043 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002044 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002045 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002046
2047 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002048 if (Base1.isPackExpansion())
2049 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002050
2051 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002052 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002053
Douglas Gregore2e50d332010-12-01 01:36:18 +00002054 Bases.push_back(
2055 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002056 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2057 Base1.isVirtual(),
2058 Base1.isBaseOfClass(),
2059 Base1.getAccessSpecifierAsWritten(),
2060 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002061 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002062 }
2063 if (!Bases.empty())
2064 ToCXX->setBases(Bases.data(), Bases.size());
2065 }
2066
Douglas Gregor2e15c842012-02-01 21:00:38 +00002067 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002068 ImportDeclContext(From, /*ForceImport=*/true);
2069
Douglas Gregore2e50d332010-12-01 01:36:18 +00002070 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002071 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002072}
2073
Larisse Voufo39a1e502013-08-06 01:03:05 +00002074bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2075 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002076 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002077 return false;
2078
2079 // FIXME: Can we really import any initializer? Alternatively, we could force
2080 // ourselves to import every declaration of a variable and then only use
2081 // getInit() here.
2082 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2083
2084 // FIXME: Other bits to merge?
2085
2086 return false;
2087}
2088
Douglas Gregord451ea92011-07-29 23:31:30 +00002089bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002090 ImportDefinitionKind Kind) {
2091 if (To->getDefinition() || To->isBeingDefined()) {
2092 if (Kind == IDK_Everything)
2093 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002094 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002095 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002096
2097 To->startDefinition();
2098
2099 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2100 if (T.isNull())
2101 return true;
2102
2103 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2104 if (ToPromotionType.isNull())
2105 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002106
2107 if (shouldForceImportDeclContext(Kind))
2108 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002109
2110 // FIXME: we might need to merge the number of positive or negative bits
2111 // if the enumerator lists don't match.
2112 To->completeDefinition(T, ToPromotionType,
2113 From->getNumPositiveBits(),
2114 From->getNumNegativeBits());
2115 return false;
2116}
2117
Douglas Gregora082a492010-11-30 19:14:50 +00002118TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2119 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002120 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002121 ToParams.reserve(Params->size());
2122 for (TemplateParameterList::iterator P = Params->begin(),
2123 PEnd = Params->end();
2124 P != PEnd; ++P) {
2125 Decl *To = Importer.Import(*P);
2126 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002127 return nullptr;
2128
Douglas Gregora082a492010-11-30 19:14:50 +00002129 ToParams.push_back(cast<NamedDecl>(To));
2130 }
2131
2132 return TemplateParameterList::Create(Importer.getToContext(),
2133 Importer.Import(Params->getTemplateLoc()),
2134 Importer.Import(Params->getLAngleLoc()),
2135 ToParams.data(), ToParams.size(),
2136 Importer.Import(Params->getRAngleLoc()));
2137}
2138
Douglas Gregore2e50d332010-12-01 01:36:18 +00002139TemplateArgument
2140ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2141 switch (From.getKind()) {
2142 case TemplateArgument::Null:
2143 return TemplateArgument();
2144
2145 case TemplateArgument::Type: {
2146 QualType ToType = Importer.Import(From.getAsType());
2147 if (ToType.isNull())
2148 return TemplateArgument();
2149 return TemplateArgument(ToType);
2150 }
2151
2152 case TemplateArgument::Integral: {
2153 QualType ToType = Importer.Import(From.getIntegralType());
2154 if (ToType.isNull())
2155 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002156 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002157 }
2158
Eli Friedmanb826a002012-09-26 02:36:12 +00002159 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002160 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2161 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2162 if (!To || ToType.isNull())
2163 return TemplateArgument();
2164 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002165 }
2166
2167 case TemplateArgument::NullPtr: {
2168 QualType ToType = Importer.Import(From.getNullPtrType());
2169 if (ToType.isNull())
2170 return TemplateArgument();
2171 return TemplateArgument(ToType, /*isNullPtr*/true);
2172 }
2173
Douglas Gregore2e50d332010-12-01 01:36:18 +00002174 case TemplateArgument::Template: {
2175 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2176 if (ToTemplate.isNull())
2177 return TemplateArgument();
2178
2179 return TemplateArgument(ToTemplate);
2180 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002181
2182 case TemplateArgument::TemplateExpansion: {
2183 TemplateName ToTemplate
2184 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2185 if (ToTemplate.isNull())
2186 return TemplateArgument();
2187
Douglas Gregore1d60df2011-01-14 23:41:42 +00002188 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002189 }
2190
Douglas Gregore2e50d332010-12-01 01:36:18 +00002191 case TemplateArgument::Expression:
2192 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2193 return TemplateArgument(ToExpr);
2194 return TemplateArgument();
2195
2196 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002197 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002198 ToPack.reserve(From.pack_size());
2199 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2200 return TemplateArgument();
2201
2202 TemplateArgument *ToArgs
2203 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2204 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2205 return TemplateArgument(ToArgs, ToPack.size());
2206 }
2207 }
2208
2209 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002210}
2211
2212bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2213 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002214 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002215 for (unsigned I = 0; I != NumFromArgs; ++I) {
2216 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2217 if (To.isNull() && !FromArgs[I].isNull())
2218 return true;
2219
2220 ToArgs.push_back(To);
2221 }
2222
2223 return false;
2224}
2225
Douglas Gregor5c73e912010-02-11 00:48:18 +00002226bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002227 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002228 // Eliminate a potential failure point where we attempt to re-import
2229 // something we're trying to import while completing ToRecord.
2230 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2231 if (ToOrigin) {
2232 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2233 if (ToOriginRecord)
2234 ToRecord = ToOriginRecord;
2235 }
2236
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002237 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002238 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002239 Importer.getNonEquivalentDecls(),
2240 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002241 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002242}
2243
Larisse Voufo39a1e502013-08-06 01:03:05 +00002244bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2245 bool Complain) {
2246 StructuralEquivalenceContext Ctx(
2247 Importer.getFromContext(), Importer.getToContext(),
2248 Importer.getNonEquivalentDecls(), false, Complain);
2249 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2250}
2251
Douglas Gregor98c10182010-02-12 22:17:39 +00002252bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002253 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002254 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002255 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002256 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002257}
2258
Douglas Gregor91155082012-11-14 22:29:20 +00002259bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2260 EnumConstantDecl *ToEC)
2261{
2262 const llvm::APSInt &FromVal = FromEC->getInitVal();
2263 const llvm::APSInt &ToVal = ToEC->getInitVal();
2264
2265 return FromVal.isSigned() == ToVal.isSigned() &&
2266 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2267 FromVal == ToVal;
2268}
2269
2270bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002271 ClassTemplateDecl *To) {
2272 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2273 Importer.getToContext(),
2274 Importer.getNonEquivalentDecls());
2275 return Ctx.IsStructurallyEquivalent(From, To);
2276}
2277
Larisse Voufo39a1e502013-08-06 01:03:05 +00002278bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2279 VarTemplateDecl *To) {
2280 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2281 Importer.getToContext(),
2282 Importer.getNonEquivalentDecls());
2283 return Ctx.IsStructurallyEquivalent(From, To);
2284}
2285
Douglas Gregore4c83e42010-02-09 22:48:33 +00002286Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002287 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002288 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002289 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002290}
2291
Sean Callanan65198272011-11-17 23:20:56 +00002292Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2293 TranslationUnitDecl *ToD =
2294 Importer.getToContext().getTranslationUnitDecl();
2295
2296 Importer.Imported(D, ToD);
2297
2298 return ToD;
2299}
2300
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002301Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2302 // Import the major distinguishing characteristics of this namespace.
2303 DeclContext *DC, *LexicalDC;
2304 DeclarationName Name;
2305 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002306 NamedDecl *ToD;
2307 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002308 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002309 if (ToD)
2310 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002311
2312 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002313 if (!Name) {
2314 // This is an anonymous namespace. Adopt an existing anonymous
2315 // namespace if we can.
2316 // FIXME: Not testable.
2317 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2318 MergeWithNamespace = TU->getAnonymousNamespace();
2319 else
2320 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2321 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002322 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002323 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002324 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002325 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2326 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002327 continue;
2328
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002329 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002330 MergeWithNamespace = FoundNS;
2331 ConflictingDecls.clear();
2332 break;
2333 }
2334
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002335 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002336 }
2337
2338 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002339 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002340 ConflictingDecls.data(),
2341 ConflictingDecls.size());
2342 }
2343 }
2344
2345 // Create the "to" namespace, if needed.
2346 NamespaceDecl *ToNamespace = MergeWithNamespace;
2347 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002348 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002349 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002350 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002351 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002352 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002353 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002354 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002355
2356 // If this is an anonymous namespace, register it as the anonymous
2357 // namespace within its context.
2358 if (!Name) {
2359 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2360 TU->setAnonymousNamespace(ToNamespace);
2361 else
2362 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2363 }
2364 }
2365 Importer.Imported(D, ToNamespace);
2366
2367 ImportDeclContext(D);
2368
2369 return ToNamespace;
2370}
2371
Richard Smithdda56e42011-04-15 14:24:37 +00002372Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002373 // Import the major distinguishing characteristics of this typedef.
2374 DeclContext *DC, *LexicalDC;
2375 DeclarationName Name;
2376 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002377 NamedDecl *ToD;
2378 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002379 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002380 if (ToD)
2381 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002382
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002383 // If this typedef is not in block scope, determine whether we've
2384 // seen a typedef with the same name (that we can merge with) or any
2385 // other entity by that name (which name lookup could conflict with).
2386 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002387 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002388 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002389 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002390 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002391 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2392 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002393 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002394 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002395 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002396 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2397 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002398 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002399 }
2400
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002401 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002402 }
2403
2404 if (!ConflictingDecls.empty()) {
2405 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2406 ConflictingDecls.data(),
2407 ConflictingDecls.size());
2408 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002409 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002410 }
2411 }
2412
Douglas Gregorb4964f72010-02-15 23:54:17 +00002413 // Import the underlying type of this typedef;
2414 QualType T = Importer.Import(D->getUnderlyingType());
2415 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002416 return nullptr;
2417
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002418 // Create the new typedef node.
2419 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002420 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002421 TypedefNameDecl *ToTypedef;
2422 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002423 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2424 StartL, Loc,
2425 Name.getAsIdentifierInfo(),
2426 TInfo);
2427 else
Richard Smithdda56e42011-04-15 14:24:37 +00002428 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2429 StartL, Loc,
2430 Name.getAsIdentifierInfo(),
2431 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002432
Douglas Gregordd483172010-02-22 17:42:47 +00002433 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002434 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002435 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002436 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002437
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002438 return ToTypedef;
2439}
2440
Richard Smithdda56e42011-04-15 14:24:37 +00002441Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2442 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2443}
2444
2445Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2446 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2447}
2448
Douglas Gregor98c10182010-02-12 22:17:39 +00002449Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2450 // Import the major distinguishing characteristics of this enum.
2451 DeclContext *DC, *LexicalDC;
2452 DeclarationName Name;
2453 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002454 NamedDecl *ToD;
2455 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002456 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002457 if (ToD)
2458 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002459
Douglas Gregor98c10182010-02-12 22:17:39 +00002460 // Figure out what enum name we're looking for.
2461 unsigned IDNS = Decl::IDNS_Tag;
2462 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002463 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2464 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002465 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002466 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002467 IDNS |= Decl::IDNS_Ordinary;
2468
2469 // We may already have an enum of the same name; try to find and match it.
2470 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002471 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002472 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002473 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002474 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2475 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002476 continue;
2477
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002478 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002479 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002480 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2481 Found = Tag->getDecl();
2482 }
2483
2484 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002485 if (IsStructuralMatch(D, FoundEnum))
2486 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002487 }
2488
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002489 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002490 }
2491
2492 if (!ConflictingDecls.empty()) {
2493 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2494 ConflictingDecls.data(),
2495 ConflictingDecls.size());
2496 }
2497 }
2498
2499 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002500 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2501 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002502 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002503 D->isScoped(), D->isScopedUsingClassTag(),
2504 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002505 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002506 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002507 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002508 D2->setLexicalDeclContext(LexicalDC);
2509 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002510 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002511
2512 // Import the integer type.
2513 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2514 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002515 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002516 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002517
2518 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002519 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002520 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002521
Douglas Gregor3996e242010-02-15 22:01:00 +00002522 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002523}
2524
Douglas Gregor5c73e912010-02-11 00:48:18 +00002525Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2526 // If this record has a definition in the translation unit we're coming from,
2527 // but this particular declaration is not that definition, import the
2528 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002529 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002530 if (Definition && Definition != D) {
2531 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002532 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002533 return nullptr;
2534
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002535 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002536 }
2537
2538 // Import the major distinguishing characteristics of this record.
2539 DeclContext *DC, *LexicalDC;
2540 DeclarationName Name;
2541 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002542 NamedDecl *ToD;
2543 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002544 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002545 if (ToD)
2546 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002547
Douglas Gregor5c73e912010-02-11 00:48:18 +00002548 // Figure out what structure name we're looking for.
2549 unsigned IDNS = Decl::IDNS_Tag;
2550 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002551 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2552 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002553 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002554 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002555 IDNS |= Decl::IDNS_Ordinary;
2556
2557 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002558 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002559 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002560 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002561 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002562 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002563 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2564 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002565 continue;
2566
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002567 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002568 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002569 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2570 Found = Tag->getDecl();
2571 }
2572
2573 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002574 if (D->isAnonymousStructOrUnion() &&
2575 FoundRecord->isAnonymousStructOrUnion()) {
2576 // If both anonymous structs/unions are in a record context, make sure
2577 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002578 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002579 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002580 if (Optional<unsigned> Index2 =
2581 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002582 if (*Index1 != *Index2)
2583 continue;
2584 }
2585 }
2586 }
2587
Douglas Gregor25791052010-02-12 00:09:27 +00002588 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002589 if ((SearchName && !D->isCompleteDefinition())
2590 || (D->isCompleteDefinition() &&
2591 D->isAnonymousStructOrUnion()
2592 == FoundDef->isAnonymousStructOrUnion() &&
2593 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002594 // The record types structurally match, or the "from" translation
2595 // unit only had a forward declaration anyway; call it the same
2596 // function.
2597 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002598 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002599 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002600 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002601 // We have a forward declaration of this type, so adopt that forward
2602 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002603
2604 // If one or both can be completed from external storage then try one
2605 // last time to complete and compare them before doing this.
2606
2607 if (FoundRecord->hasExternalLexicalStorage() &&
2608 !FoundRecord->isCompleteDefinition())
2609 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2610 if (D->hasExternalLexicalStorage())
2611 D->getASTContext().getExternalSource()->CompleteType(D);
2612
2613 if (FoundRecord->isCompleteDefinition() &&
2614 D->isCompleteDefinition() &&
2615 !IsStructuralMatch(D, FoundRecord))
2616 continue;
2617
Douglas Gregor25791052010-02-12 00:09:27 +00002618 AdoptDecl = FoundRecord;
2619 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002620 } else if (!SearchName) {
2621 continue;
2622 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002623 }
2624
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002625 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002626 }
2627
Douglas Gregordd6006f2012-07-17 21:16:27 +00002628 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002629 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2630 ConflictingDecls.data(),
2631 ConflictingDecls.size());
2632 }
2633 }
2634
2635 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002636 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002637 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002638 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002639 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002640 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002641 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002642 DC, StartLoc, Loc,
2643 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002644 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002645 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002646 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002647 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002648 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002649 }
Douglas Gregor14454802011-02-25 02:25:35 +00002650
2651 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002652 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002653 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002654 if (D->isAnonymousStructOrUnion())
2655 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002656 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002657
Douglas Gregor3996e242010-02-15 22:01:00 +00002658 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002659
Douglas Gregor95d82832012-01-24 18:36:04 +00002660 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002661 return nullptr;
2662
Douglas Gregor3996e242010-02-15 22:01:00 +00002663 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002664}
2665
Douglas Gregor98c10182010-02-12 22:17:39 +00002666Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2667 // Import the major distinguishing characteristics of this enumerator.
2668 DeclContext *DC, *LexicalDC;
2669 DeclarationName Name;
2670 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002671 NamedDecl *ToD;
2672 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002673 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002674 if (ToD)
2675 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002676
2677 QualType T = Importer.Import(D->getType());
2678 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002679 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002680
Douglas Gregor98c10182010-02-12 22:17:39 +00002681 // Determine whether there are any other declarations with the same name and
2682 // in the same context.
2683 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002684 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002685 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002686 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002687 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002688 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2689 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002690 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002691
2692 if (EnumConstantDecl *FoundEnumConstant
2693 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2694 if (IsStructuralMatch(D, FoundEnumConstant))
2695 return Importer.Imported(D, FoundEnumConstant);
2696 }
2697
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002698 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002699 }
2700
2701 if (!ConflictingDecls.empty()) {
2702 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2703 ConflictingDecls.data(),
2704 ConflictingDecls.size());
2705 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002706 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002707 }
2708 }
2709
2710 Expr *Init = Importer.Import(D->getInitExpr());
2711 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002712 return nullptr;
2713
Douglas Gregor98c10182010-02-12 22:17:39 +00002714 EnumConstantDecl *ToEnumerator
2715 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2716 Name.getAsIdentifierInfo(), T,
2717 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002718 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002719 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002720 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002721 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002722 return ToEnumerator;
2723}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002724
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002725Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2726 // Import the major distinguishing characteristics of this function.
2727 DeclContext *DC, *LexicalDC;
2728 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002729 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002730 NamedDecl *ToD;
2731 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002732 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002733 if (ToD)
2734 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002735
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002736 // Try to find a function in our own ("to") context with the same name, same
2737 // type, and in the same context as the function we're importing.
2738 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002739 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002740 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002741 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002742 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002743 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2744 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002745 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002746
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002747 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002748 if (FoundFunction->hasExternalFormalLinkage() &&
2749 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002750 if (Importer.IsStructurallyEquivalent(D->getType(),
2751 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002752 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002753 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002754 }
2755
2756 // FIXME: Check for overloading more carefully, e.g., by boosting
2757 // Sema::IsOverload out to the AST library.
2758
2759 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002760 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002761 continue;
2762
2763 // Complain about inconsistent function types.
2764 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002765 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002766 Importer.ToDiag(FoundFunction->getLocation(),
2767 diag::note_odr_value_here)
2768 << FoundFunction->getType();
2769 }
2770 }
2771
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002772 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002773 }
2774
2775 if (!ConflictingDecls.empty()) {
2776 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2777 ConflictingDecls.data(),
2778 ConflictingDecls.size());
2779 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002780 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002781 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002782 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002783
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002784 DeclarationNameInfo NameInfo(Name, Loc);
2785 // Import additional name location/type info.
2786 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2787
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002788 QualType FromTy = D->getType();
2789 bool usedDifferentExceptionSpec = false;
2790
2791 if (const FunctionProtoType *
2792 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2793 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2794 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2795 // FunctionDecl that we are importing the FunctionProtoType for.
2796 // To avoid an infinite recursion when importing, create the FunctionDecl
2797 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002798 if (FromEPI.ExceptionSpec.SourceDecl ||
2799 FromEPI.ExceptionSpec.SourceTemplate ||
2800 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002801 FunctionProtoType::ExtProtoInfo DefaultEPI;
2802 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002803 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002804 usedDifferentExceptionSpec = true;
2805 }
2806 }
2807
Douglas Gregorb4964f72010-02-15 23:54:17 +00002808 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002809 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002810 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002811 return nullptr;
2812
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002813 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002814 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002815 for (auto P : D->params()) {
2816 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002817 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002818 return nullptr;
2819
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002820 Parameters.push_back(ToP);
2821 }
2822
2823 // Create the imported function.
2824 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002825 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002826 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002827 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2828 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2829 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002830 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002831 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002832 FromConstructor->isExplicit(),
2833 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002834 D->isImplicit(),
2835 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002836 } else if (isa<CXXDestructorDecl>(D)) {
2837 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2838 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002839 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002840 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002841 D->isInlineSpecified(),
2842 D->isImplicit());
2843 } else if (CXXConversionDecl *FromConversion
2844 = dyn_cast<CXXConversionDecl>(D)) {
2845 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2846 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002847 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002848 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002849 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002850 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002851 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002852 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002853 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2854 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2855 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002856 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002857 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002858 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002859 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002860 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002861 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002862 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002863 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002864 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002865 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002866 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002867 D->hasWrittenPrototype(),
2868 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002869 }
John McCall3e11ebe2010-03-15 10:12:16 +00002870
2871 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002872 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002873 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002874 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002875 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2876 ToFunction->setTrivial(D->isTrivial());
2877 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002878 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002879
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002880 // Set the parameters.
2881 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002882 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002883 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002884 }
David Blaikie9c70e042011-09-21 18:16:56 +00002885 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002886
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002887 if (usedDifferentExceptionSpec) {
2888 // Update FunctionProtoType::ExtProtoInfo.
2889 QualType T = Importer.Import(D->getType());
2890 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002891 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002892 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002893 }
2894
Sean Callanan59721b32015-04-28 18:41:46 +00002895 // Import the body, if any.
2896 if (Stmt *FromBody = D->getBody()) {
2897 if (Stmt *ToBody = Importer.Import(FromBody)) {
2898 ToFunction->setBody(ToBody);
2899 }
2900 }
2901
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002902 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002903
2904 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002905 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002906
Douglas Gregor43f54792010-02-17 02:12:47 +00002907 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002908}
2909
Douglas Gregor00eace12010-02-21 18:29:16 +00002910Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2911 return VisitFunctionDecl(D);
2912}
2913
2914Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2915 return VisitCXXMethodDecl(D);
2916}
2917
2918Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2919 return VisitCXXMethodDecl(D);
2920}
2921
2922Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2923 return VisitCXXMethodDecl(D);
2924}
2925
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002926static unsigned getFieldIndex(Decl *F) {
2927 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2928 if (!Owner)
2929 return 0;
2930
2931 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002932 for (const auto *D : Owner->noload_decls()) {
2933 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002934 return Index;
2935
2936 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2937 ++Index;
2938 }
2939
2940 return Index;
2941}
2942
Douglas Gregor5c73e912010-02-11 00:48:18 +00002943Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2944 // Import the major distinguishing characteristics of a variable.
2945 DeclContext *DC, *LexicalDC;
2946 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002947 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002948 NamedDecl *ToD;
2949 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002950 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002951 if (ToD)
2952 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002953
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002954 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002955 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002956 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002957 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2958 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002959 // For anonymous fields, match up by index.
2960 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2961 continue;
2962
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002963 if (Importer.IsStructurallyEquivalent(D->getType(),
2964 FoundField->getType())) {
2965 Importer.Imported(D, FoundField);
2966 return FoundField;
2967 }
2968
2969 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2970 << Name << D->getType() << FoundField->getType();
2971 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2972 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002973 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002974 }
2975 }
2976
Douglas Gregorb4964f72010-02-15 23:54:17 +00002977 // Import the type.
2978 QualType T = Importer.Import(D->getType());
2979 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002980 return nullptr;
2981
Douglas Gregor5c73e912010-02-11 00:48:18 +00002982 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2983 Expr *BitWidth = Importer.Import(D->getBitWidth());
2984 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002985 return nullptr;
2986
Abramo Bagnaradff19302011-03-08 08:55:46 +00002987 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2988 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002989 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002990 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002991 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002992 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002993 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002994 if (ToField->hasInClassInitializer())
2995 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002996 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002997 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002998 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002999 return ToField;
3000}
3001
Francois Pichet783dd6e2010-11-21 06:08:52 +00003002Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3003 // Import the major distinguishing characteristics of a variable.
3004 DeclContext *DC, *LexicalDC;
3005 DeclarationName Name;
3006 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003007 NamedDecl *ToD;
3008 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003009 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003010 if (ToD)
3011 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003012
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003013 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003014 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003015 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003016 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003017 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003018 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003019 // For anonymous indirect fields, match up by index.
3020 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3021 continue;
3022
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003023 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003024 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003025 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003026 Importer.Imported(D, FoundField);
3027 return FoundField;
3028 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003029
3030 // If there are more anonymous fields to check, continue.
3031 if (!Name && I < N-1)
3032 continue;
3033
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003034 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3035 << Name << D->getType() << FoundField->getType();
3036 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3037 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003038 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003039 }
3040 }
3041
Francois Pichet783dd6e2010-11-21 06:08:52 +00003042 // Import the type.
3043 QualType T = Importer.Import(D->getType());
3044 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003045 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003046
3047 NamedDecl **NamedChain =
3048 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3049
3050 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003051 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003052 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003053 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003054 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003055 NamedChain[i++] = cast<NamedDecl>(D);
3056 }
3057
3058 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003059 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3060 NamedChain, D->getChainingSize());
3061
3062 for (const auto *Attr : D->attrs())
3063 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3064
Francois Pichet783dd6e2010-11-21 06:08:52 +00003065 ToIndirectField->setAccess(D->getAccess());
3066 ToIndirectField->setLexicalDeclContext(LexicalDC);
3067 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003068 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003069 return ToIndirectField;
3070}
3071
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003072Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3073 // Import the major distinguishing characteristics of an ivar.
3074 DeclContext *DC, *LexicalDC;
3075 DeclarationName Name;
3076 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003077 NamedDecl *ToD;
3078 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003079 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003080 if (ToD)
3081 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003082
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003083 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003084 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003085 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003086 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3087 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003088 if (Importer.IsStructurallyEquivalent(D->getType(),
3089 FoundIvar->getType())) {
3090 Importer.Imported(D, FoundIvar);
3091 return FoundIvar;
3092 }
3093
3094 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3095 << Name << D->getType() << FoundIvar->getType();
3096 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3097 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003098 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003099 }
3100 }
3101
3102 // Import the type.
3103 QualType T = Importer.Import(D->getType());
3104 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003105 return nullptr;
3106
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003107 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3108 Expr *BitWidth = Importer.Import(D->getBitWidth());
3109 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003110 return nullptr;
3111
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003112 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3113 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003114 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003115 Loc, Name.getAsIdentifierInfo(),
3116 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003117 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003118 ToIvar->setLexicalDeclContext(LexicalDC);
3119 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003120 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003121 return ToIvar;
3122
3123}
3124
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003125Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3126 // Import the major distinguishing characteristics of a variable.
3127 DeclContext *DC, *LexicalDC;
3128 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003129 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003130 NamedDecl *ToD;
3131 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003132 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003133 if (ToD)
3134 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003135
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003136 // Try to find a variable in our own ("to") context with the same name and
3137 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003138 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003139 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003140 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003141 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003142 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003143 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003144 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3145 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003146 continue;
3147
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003148 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003149 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003150 if (FoundVar->hasExternalFormalLinkage() &&
3151 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003152 if (Importer.IsStructurallyEquivalent(D->getType(),
3153 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003154 MergeWithVar = FoundVar;
3155 break;
3156 }
3157
Douglas Gregor56521c52010-02-12 17:23:39 +00003158 const ArrayType *FoundArray
3159 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3160 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003161 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003162 if (FoundArray && TArray) {
3163 if (isa<IncompleteArrayType>(FoundArray) &&
3164 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003165 // Import the type.
3166 QualType T = Importer.Import(D->getType());
3167 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003168 return nullptr;
3169
Douglas Gregor56521c52010-02-12 17:23:39 +00003170 FoundVar->setType(T);
3171 MergeWithVar = FoundVar;
3172 break;
3173 } else if (isa<IncompleteArrayType>(TArray) &&
3174 isa<ConstantArrayType>(FoundArray)) {
3175 MergeWithVar = FoundVar;
3176 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003177 }
3178 }
3179
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003180 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003181 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003182 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3183 << FoundVar->getType();
3184 }
3185 }
3186
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003187 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003188 }
3189
3190 if (MergeWithVar) {
3191 // An equivalent variable with external linkage has been found. Link
3192 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003193 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003194
3195 if (VarDecl *DDef = D->getDefinition()) {
3196 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3197 Importer.ToDiag(ExistingDef->getLocation(),
3198 diag::err_odr_variable_multiple_def)
3199 << Name;
3200 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3201 } else {
3202 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003203 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003204 if (DDef->isInitKnownICE()) {
3205 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3206 Eval->CheckedICE = true;
3207 Eval->IsICE = DDef->isInitICE();
3208 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003209 }
3210 }
3211
3212 return MergeWithVar;
3213 }
3214
3215 if (!ConflictingDecls.empty()) {
3216 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3217 ConflictingDecls.data(),
3218 ConflictingDecls.size());
3219 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003220 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003221 }
3222 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003223
Douglas Gregorb4964f72010-02-15 23:54:17 +00003224 // Import the type.
3225 QualType T = Importer.Import(D->getType());
3226 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003227 return nullptr;
3228
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003229 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003230 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003231 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3232 Importer.Import(D->getInnerLocStart()),
3233 Loc, Name.getAsIdentifierInfo(),
3234 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003235 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003236 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003237 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003238 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003239 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003240 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003241
Sean Callanan59721b32015-04-28 18:41:46 +00003242 if (!D->isFileVarDecl() &&
3243 D->isUsed())
3244 ToVar->setIsUsed();
3245
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003246 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003247 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003248 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003249
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003250 return ToVar;
3251}
3252
Douglas Gregor8b228d72010-02-17 21:22:52 +00003253Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3254 // Parameters are created in the translation unit's context, then moved
3255 // into the function declaration's context afterward.
3256 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3257
3258 // Import the name of this declaration.
3259 DeclarationName Name = Importer.Import(D->getDeclName());
3260 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003261 return nullptr;
3262
Douglas Gregor8b228d72010-02-17 21:22:52 +00003263 // Import the location of this declaration.
3264 SourceLocation Loc = Importer.Import(D->getLocation());
3265
3266 // Import the parameter's type.
3267 QualType T = Importer.Import(D->getType());
3268 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003269 return nullptr;
3270
Douglas Gregor8b228d72010-02-17 21:22:52 +00003271 // Create the imported parameter.
3272 ImplicitParamDecl *ToParm
3273 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3274 Loc, Name.getAsIdentifierInfo(),
3275 T);
3276 return Importer.Imported(D, ToParm);
3277}
3278
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003279Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3280 // Parameters are created in the translation unit's context, then moved
3281 // into the function declaration's context afterward.
3282 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3283
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003284 // Import the name of this declaration.
3285 DeclarationName Name = Importer.Import(D->getDeclName());
3286 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003287 return nullptr;
3288
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003289 // Import the location of this declaration.
3290 SourceLocation Loc = Importer.Import(D->getLocation());
3291
3292 // Import the parameter's type.
3293 QualType T = Importer.Import(D->getType());
3294 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003295 return nullptr;
3296
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003297 // Create the imported parameter.
3298 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3299 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003300 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003301 Loc, Name.getAsIdentifierInfo(),
3302 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003303 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003304 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003305
3306 if (D->isUsed())
3307 ToParm->setIsUsed();
3308
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003309 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003310}
3311
Douglas Gregor43f54792010-02-17 02:12:47 +00003312Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3313 // Import the major distinguishing characteristics of a method.
3314 DeclContext *DC, *LexicalDC;
3315 DeclarationName Name;
3316 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003317 NamedDecl *ToD;
3318 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003319 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003320 if (ToD)
3321 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003322
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003323 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003324 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003325 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3326 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003327 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3328 continue;
3329
3330 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003331 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3332 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003333 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003334 << D->isInstanceMethod() << Name << D->getReturnType()
3335 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003336 Importer.ToDiag(FoundMethod->getLocation(),
3337 diag::note_odr_objc_method_here)
3338 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003339 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003340 }
3341
3342 // Check the number of parameters.
3343 if (D->param_size() != FoundMethod->param_size()) {
3344 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3345 << D->isInstanceMethod() << Name
3346 << D->param_size() << FoundMethod->param_size();
3347 Importer.ToDiag(FoundMethod->getLocation(),
3348 diag::note_odr_objc_method_here)
3349 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003351 }
3352
3353 // Check parameter types.
3354 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3355 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3356 P != PEnd; ++P, ++FoundP) {
3357 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3358 (*FoundP)->getType())) {
3359 Importer.FromDiag((*P)->getLocation(),
3360 diag::err_odr_objc_method_param_type_inconsistent)
3361 << D->isInstanceMethod() << Name
3362 << (*P)->getType() << (*FoundP)->getType();
3363 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3364 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003365 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003366 }
3367 }
3368
3369 // Check variadic/non-variadic.
3370 // Check the number of parameters.
3371 if (D->isVariadic() != FoundMethod->isVariadic()) {
3372 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3373 << D->isInstanceMethod() << Name;
3374 Importer.ToDiag(FoundMethod->getLocation(),
3375 diag::note_odr_objc_method_here)
3376 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003377 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003378 }
3379
3380 // FIXME: Any other bits we need to merge?
3381 return Importer.Imported(D, FoundMethod);
3382 }
3383 }
3384
3385 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003386 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003387 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003388 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003389
Alp Toker314cc812014-01-25 16:55:45 +00003390 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003391
Alp Toker314cc812014-01-25 16:55:45 +00003392 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3393 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3394 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3395 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3396 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003397
3398 // FIXME: When we decide to merge method definitions, we'll need to
3399 // deal with implicit parameters.
3400
3401 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003402 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003403 for (auto *FromP : D->params()) {
3404 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003405 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003406 return nullptr;
3407
Douglas Gregor43f54792010-02-17 02:12:47 +00003408 ToParams.push_back(ToP);
3409 }
3410
3411 // Set the parameters.
3412 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3413 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003414 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003415 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003416 SmallVector<SourceLocation, 12> SelLocs;
3417 D->getSelectorLocs(SelLocs);
3418 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003419
3420 ToMethod->setLexicalDeclContext(LexicalDC);
3421 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003422 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003423 return ToMethod;
3424}
3425
Douglas Gregor84c51c32010-02-18 01:47:50 +00003426Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3427 // Import the major distinguishing characteristics of a category.
3428 DeclContext *DC, *LexicalDC;
3429 DeclarationName Name;
3430 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003431 NamedDecl *ToD;
3432 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003433 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003434 if (ToD)
3435 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003436
Douglas Gregor84c51c32010-02-18 01:47:50 +00003437 ObjCInterfaceDecl *ToInterface
3438 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3439 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003440 return nullptr;
3441
Douglas Gregor84c51c32010-02-18 01:47:50 +00003442 // Determine if we've already encountered this category.
3443 ObjCCategoryDecl *MergeWithCategory
3444 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3445 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3446 if (!ToCategory) {
3447 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003448 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003449 Loc,
3450 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003451 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003452 ToInterface,
3453 Importer.Import(D->getIvarLBraceLoc()),
3454 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003455 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003456 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003457 Importer.Imported(D, ToCategory);
3458
Douglas Gregor84c51c32010-02-18 01:47:50 +00003459 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003460 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3461 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003462 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3463 = D->protocol_loc_begin();
3464 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3465 FromProtoEnd = D->protocol_end();
3466 FromProto != FromProtoEnd;
3467 ++FromProto, ++FromProtoLoc) {
3468 ObjCProtocolDecl *ToProto
3469 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3470 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003471 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003472 Protocols.push_back(ToProto);
3473 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3474 }
3475
3476 // FIXME: If we're merging, make sure that the protocol list is the same.
3477 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3478 ProtocolLocs.data(), Importer.getToContext());
3479
3480 } else {
3481 Importer.Imported(D, ToCategory);
3482 }
3483
3484 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003485 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003486
3487 // If we have an implementation, import it as well.
3488 if (D->getImplementation()) {
3489 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003490 = cast_or_null<ObjCCategoryImplDecl>(
3491 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003492 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003493 return nullptr;
3494
Douglas Gregor84c51c32010-02-18 01:47:50 +00003495 ToCategory->setImplementation(Impl);
3496 }
3497
3498 return ToCategory;
3499}
3500
Douglas Gregor2aa53772012-01-24 17:42:07 +00003501bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3502 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003503 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003504 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003505 if (shouldForceImportDeclContext(Kind))
3506 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003507 return false;
3508 }
3509
3510 // Start the protocol definition
3511 To->startDefinition();
3512
3513 // Import protocols
3514 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3515 SmallVector<SourceLocation, 4> ProtocolLocs;
3516 ObjCProtocolDecl::protocol_loc_iterator
3517 FromProtoLoc = From->protocol_loc_begin();
3518 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3519 FromProtoEnd = From->protocol_end();
3520 FromProto != FromProtoEnd;
3521 ++FromProto, ++FromProtoLoc) {
3522 ObjCProtocolDecl *ToProto
3523 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3524 if (!ToProto)
3525 return true;
3526 Protocols.push_back(ToProto);
3527 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3528 }
3529
3530 // FIXME: If we're merging, make sure that the protocol list is the same.
3531 To->setProtocolList(Protocols.data(), Protocols.size(),
3532 ProtocolLocs.data(), Importer.getToContext());
3533
Douglas Gregor2e15c842012-02-01 21:00:38 +00003534 if (shouldForceImportDeclContext(Kind)) {
3535 // Import all of the members of this protocol.
3536 ImportDeclContext(From, /*ForceImport=*/true);
3537 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003538 return false;
3539}
3540
Douglas Gregor98d156a2010-02-17 16:12:00 +00003541Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003542 // If this protocol has a definition in the translation unit we're coming
3543 // from, but this particular declaration is not that definition, import the
3544 // definition and map to that.
3545 ObjCProtocolDecl *Definition = D->getDefinition();
3546 if (Definition && Definition != D) {
3547 Decl *ImportedDef = Importer.Import(Definition);
3548 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003549 return nullptr;
3550
Douglas Gregor2aa53772012-01-24 17:42:07 +00003551 return Importer.Imported(D, ImportedDef);
3552 }
3553
Douglas Gregor84c51c32010-02-18 01:47:50 +00003554 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003555 DeclContext *DC, *LexicalDC;
3556 DeclarationName Name;
3557 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003558 NamedDecl *ToD;
3559 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003560 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003561 if (ToD)
3562 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003563
Craig Topper36250ad2014-05-12 05:36:57 +00003564 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003565 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003566 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003567 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3568 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003569 continue;
3570
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003571 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003572 break;
3573 }
3574
3575 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003576 if (!ToProto) {
3577 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3578 Name.getAsIdentifierInfo(), Loc,
3579 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003580 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003581 ToProto->setLexicalDeclContext(LexicalDC);
3582 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003583 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003584
3585 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003586
Douglas Gregor2aa53772012-01-24 17:42:07 +00003587 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003588 return nullptr;
3589
Douglas Gregor98d156a2010-02-17 16:12:00 +00003590 return ToProto;
3591}
3592
Sean Callanan0aae0412014-12-10 00:00:37 +00003593Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3594 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3595 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3596
3597 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3598 SourceLocation LangLoc = Importer.Import(D->getLocation());
3599
3600 bool HasBraces = D->hasBraces();
3601
Sean Callananb12a8552014-12-10 21:22:20 +00003602 LinkageSpecDecl *ToLinkageSpec =
3603 LinkageSpecDecl::Create(Importer.getToContext(),
3604 DC,
3605 ExternLoc,
3606 LangLoc,
3607 D->getLanguage(),
3608 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003609
3610 if (HasBraces) {
3611 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3612 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3613 }
3614
3615 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3616 LexicalDC->addDeclInternal(ToLinkageSpec);
3617
3618 Importer.Imported(D, ToLinkageSpec);
3619
3620 return ToLinkageSpec;
3621}
3622
Douglas Gregor2aa53772012-01-24 17:42:07 +00003623bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3624 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003625 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003626 if (To->getDefinition()) {
3627 // Check consistency of superclass.
3628 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3629 if (FromSuper) {
3630 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3631 if (!FromSuper)
3632 return true;
3633 }
3634
3635 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3636 if ((bool)FromSuper != (bool)ToSuper ||
3637 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3638 Importer.ToDiag(To->getLocation(),
3639 diag::err_odr_objc_superclass_inconsistent)
3640 << To->getDeclName();
3641 if (ToSuper)
3642 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3643 << To->getSuperClass()->getDeclName();
3644 else
3645 Importer.ToDiag(To->getLocation(),
3646 diag::note_odr_objc_missing_superclass);
3647 if (From->getSuperClass())
3648 Importer.FromDiag(From->getSuperClassLoc(),
3649 diag::note_odr_objc_superclass)
3650 << From->getSuperClass()->getDeclName();
3651 else
3652 Importer.FromDiag(From->getLocation(),
3653 diag::note_odr_objc_missing_superclass);
3654 }
3655
Douglas Gregor2e15c842012-02-01 21:00:38 +00003656 if (shouldForceImportDeclContext(Kind))
3657 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003658 return false;
3659 }
3660
3661 // Start the definition.
3662 To->startDefinition();
3663
3664 // If this class has a superclass, import it.
3665 if (From->getSuperClass()) {
3666 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3667 Importer.Import(From->getSuperClass()));
3668 if (!Super)
3669 return true;
3670
3671 To->setSuperClass(Super);
3672 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3673 }
3674
3675 // Import protocols
3676 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3677 SmallVector<SourceLocation, 4> ProtocolLocs;
3678 ObjCInterfaceDecl::protocol_loc_iterator
3679 FromProtoLoc = From->protocol_loc_begin();
3680
3681 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3682 FromProtoEnd = From->protocol_end();
3683 FromProto != FromProtoEnd;
3684 ++FromProto, ++FromProtoLoc) {
3685 ObjCProtocolDecl *ToProto
3686 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3687 if (!ToProto)
3688 return true;
3689 Protocols.push_back(ToProto);
3690 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3691 }
3692
3693 // FIXME: If we're merging, make sure that the protocol list is the same.
3694 To->setProtocolList(Protocols.data(), Protocols.size(),
3695 ProtocolLocs.data(), Importer.getToContext());
3696
3697 // Import categories. When the categories themselves are imported, they'll
3698 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003699 for (auto *Cat : From->known_categories())
3700 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003701
Douglas Gregor2aa53772012-01-24 17:42:07 +00003702 // If we have an @implementation, import it as well.
3703 if (From->getImplementation()) {
3704 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3705 Importer.Import(From->getImplementation()));
3706 if (!Impl)
3707 return true;
3708
3709 To->setImplementation(Impl);
3710 }
3711
Douglas Gregor2e15c842012-02-01 21:00:38 +00003712 if (shouldForceImportDeclContext(Kind)) {
3713 // Import all of the members of this class.
3714 ImportDeclContext(From, /*ForceImport=*/true);
3715 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003716 return false;
3717}
3718
Douglas Gregor45635322010-02-16 01:20:57 +00003719Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003720 // If this class has a definition in the translation unit we're coming from,
3721 // but this particular declaration is not that definition, import the
3722 // definition and map to that.
3723 ObjCInterfaceDecl *Definition = D->getDefinition();
3724 if (Definition && Definition != D) {
3725 Decl *ImportedDef = Importer.Import(Definition);
3726 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003727 return nullptr;
3728
Douglas Gregor2aa53772012-01-24 17:42:07 +00003729 return Importer.Imported(D, ImportedDef);
3730 }
3731
Douglas Gregor45635322010-02-16 01:20:57 +00003732 // Import the major distinguishing characteristics of an @interface.
3733 DeclContext *DC, *LexicalDC;
3734 DeclarationName Name;
3735 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003736 NamedDecl *ToD;
3737 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003739 if (ToD)
3740 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003741
Douglas Gregor2aa53772012-01-24 17:42:07 +00003742 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003743 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003744 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003745 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003746 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3747 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003748 continue;
3749
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003750 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003751 break;
3752 }
3753
Douglas Gregor2aa53772012-01-24 17:42:07 +00003754 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003755 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003756 if (!ToIface) {
3757 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3758 Importer.Import(D->getAtStartLoc()),
3759 Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00003760 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003761 D->isImplicitInterfaceDecl());
3762 ToIface->setLexicalDeclContext(LexicalDC);
3763 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003764 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003765 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003766
Douglas Gregor2aa53772012-01-24 17:42:07 +00003767 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003768 return nullptr;
3769
Douglas Gregor98d156a2010-02-17 16:12:00 +00003770 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003771}
3772
Douglas Gregor4da9d682010-12-07 15:32:12 +00003773Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3774 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3775 Importer.Import(D->getCategoryDecl()));
3776 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003777 return nullptr;
3778
Douglas Gregor4da9d682010-12-07 15:32:12 +00003779 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3780 if (!ToImpl) {
3781 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3782 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003783 return nullptr;
3784
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003785 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003786 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003787 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003788 Category->getClassInterface(),
3789 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003790 Importer.Import(D->getAtStartLoc()),
3791 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003792
3793 DeclContext *LexicalDC = DC;
3794 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3795 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3796 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003797 return nullptr;
3798
Douglas Gregor4da9d682010-12-07 15:32:12 +00003799 ToImpl->setLexicalDeclContext(LexicalDC);
3800 }
3801
Sean Callanan95e74be2011-10-21 02:57:43 +00003802 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003803 Category->setImplementation(ToImpl);
3804 }
3805
3806 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003807 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003808 return ToImpl;
3809}
3810
Douglas Gregorda8025c2010-12-07 01:26:03 +00003811Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3812 // Find the corresponding interface.
3813 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3814 Importer.Import(D->getClassInterface()));
3815 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003816 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003817
3818 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003819 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003820 if (D->getSuperClass()) {
3821 Super = cast_or_null<ObjCInterfaceDecl>(
3822 Importer.Import(D->getSuperClass()));
3823 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003824 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003825 }
3826
3827 ObjCImplementationDecl *Impl = Iface->getImplementation();
3828 if (!Impl) {
3829 // We haven't imported an implementation yet. Create a new @implementation
3830 // now.
3831 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3832 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003833 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003834 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003835 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003836 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003837 Importer.Import(D->getIvarLBraceLoc()),
3838 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003839
3840 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3841 DeclContext *LexicalDC
3842 = Importer.ImportContext(D->getLexicalDeclContext());
3843 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003844 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003845 Impl->setLexicalDeclContext(LexicalDC);
3846 }
3847
3848 // Associate the implementation with the class it implements.
3849 Iface->setImplementation(Impl);
3850 Importer.Imported(D, Iface->getImplementation());
3851 } else {
3852 Importer.Imported(D, Iface->getImplementation());
3853
3854 // Verify that the existing @implementation has the same superclass.
3855 if ((Super && !Impl->getSuperClass()) ||
3856 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003857 (Super && Impl->getSuperClass() &&
3858 !declaresSameEntity(Super->getCanonicalDecl(),
3859 Impl->getSuperClass()))) {
3860 Importer.ToDiag(Impl->getLocation(),
3861 diag::err_odr_objc_superclass_inconsistent)
3862 << Iface->getDeclName();
3863 // FIXME: It would be nice to have the location of the superclass
3864 // below.
3865 if (Impl->getSuperClass())
3866 Importer.ToDiag(Impl->getLocation(),
3867 diag::note_odr_objc_superclass)
3868 << Impl->getSuperClass()->getDeclName();
3869 else
3870 Importer.ToDiag(Impl->getLocation(),
3871 diag::note_odr_objc_missing_superclass);
3872 if (D->getSuperClass())
3873 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003874 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003875 << D->getSuperClass()->getDeclName();
3876 else
3877 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003878 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003879 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003880 }
3881 }
3882
3883 // Import all of the members of this @implementation.
3884 ImportDeclContext(D);
3885
3886 return Impl;
3887}
3888
Douglas Gregora11c4582010-02-17 18:02:10 +00003889Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3890 // Import the major distinguishing characteristics of an @property.
3891 DeclContext *DC, *LexicalDC;
3892 DeclarationName Name;
3893 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003894 NamedDecl *ToD;
3895 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003896 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003897 if (ToD)
3898 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00003899
3900 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003901 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003902 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003903 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003904 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003905 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003906 // Check property types.
3907 if (!Importer.IsStructurallyEquivalent(D->getType(),
3908 FoundProp->getType())) {
3909 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3910 << Name << D->getType() << FoundProp->getType();
3911 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3912 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003913 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003914 }
3915
3916 // FIXME: Check property attributes, getters, setters, etc.?
3917
3918 // Consider these properties to be equivalent.
3919 Importer.Imported(D, FoundProp);
3920 return FoundProp;
3921 }
3922 }
3923
3924 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00003925 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
3926 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00003927 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003928
3929 // Create the new property.
3930 ObjCPropertyDecl *ToProperty
3931 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3932 Name.getAsIdentifierInfo(),
3933 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003934 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00003935 Importer.Import(D->getType()),
3936 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00003937 D->getPropertyImplementation());
3938 Importer.Imported(D, ToProperty);
3939 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003940 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003941
3942 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003943 ToProperty->setPropertyAttributesAsWritten(
3944 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003945 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3946 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3947 ToProperty->setGetterMethodDecl(
3948 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3949 ToProperty->setSetterMethodDecl(
3950 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3951 ToProperty->setPropertyIvarDecl(
3952 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3953 return ToProperty;
3954}
3955
Douglas Gregor14a49e22010-12-07 18:32:03 +00003956Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3957 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3958 Importer.Import(D->getPropertyDecl()));
3959 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003960 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003961
3962 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3963 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003964 return nullptr;
3965
Douglas Gregor14a49e22010-12-07 18:32:03 +00003966 // Import the lexical declaration context.
3967 DeclContext *LexicalDC = DC;
3968 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3969 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3970 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003971 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003972 }
3973
3974 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3975 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003976 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003977
3978 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003979 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003980 if (D->getPropertyIvarDecl()) {
3981 Ivar = cast_or_null<ObjCIvarDecl>(
3982 Importer.Import(D->getPropertyIvarDecl()));
3983 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003984 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003985 }
3986
3987 ObjCPropertyImplDecl *ToImpl
3988 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3989 if (!ToImpl) {
3990 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3991 Importer.Import(D->getLocStart()),
3992 Importer.Import(D->getLocation()),
3993 Property,
3994 D->getPropertyImplementation(),
3995 Ivar,
3996 Importer.Import(D->getPropertyIvarDeclLoc()));
3997 ToImpl->setLexicalDeclContext(LexicalDC);
3998 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003999 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004000 } else {
4001 // Check that we have the same kind of property implementation (@synthesize
4002 // vs. @dynamic).
4003 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4004 Importer.ToDiag(ToImpl->getLocation(),
4005 diag::err_odr_objc_property_impl_kind_inconsistent)
4006 << Property->getDeclName()
4007 << (ToImpl->getPropertyImplementation()
4008 == ObjCPropertyImplDecl::Dynamic);
4009 Importer.FromDiag(D->getLocation(),
4010 diag::note_odr_objc_property_impl_kind)
4011 << D->getPropertyDecl()->getDeclName()
4012 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004013 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004014 }
4015
4016 // For @synthesize, check that we have the same
4017 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4018 Ivar != ToImpl->getPropertyIvarDecl()) {
4019 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4020 diag::err_odr_objc_synthesize_ivar_inconsistent)
4021 << Property->getDeclName()
4022 << ToImpl->getPropertyIvarDecl()->getDeclName()
4023 << Ivar->getDeclName();
4024 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4025 diag::note_odr_objc_synthesize_ivar_here)
4026 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004027 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004028 }
4029
4030 // Merge the existing implementation with the new implementation.
4031 Importer.Imported(D, ToImpl);
4032 }
4033
4034 return ToImpl;
4035}
4036
Douglas Gregora082a492010-11-30 19:14:50 +00004037Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4038 // For template arguments, we adopt the translation unit as our declaration
4039 // context. This context will be fixed when the actual template declaration
4040 // is created.
4041
4042 // FIXME: Import default argument.
4043 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4044 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004045 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004046 Importer.Import(D->getLocation()),
4047 D->getDepth(),
4048 D->getIndex(),
4049 Importer.Import(D->getIdentifier()),
4050 D->wasDeclaredWithTypename(),
4051 D->isParameterPack());
4052}
4053
4054Decl *
4055ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4056 // Import the name of this declaration.
4057 DeclarationName Name = Importer.Import(D->getDeclName());
4058 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004059 return nullptr;
4060
Douglas Gregora082a492010-11-30 19:14:50 +00004061 // Import the location of this declaration.
4062 SourceLocation Loc = Importer.Import(D->getLocation());
4063
4064 // Import the type of this declaration.
4065 QualType T = Importer.Import(D->getType());
4066 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004067 return nullptr;
4068
Douglas Gregora082a492010-11-30 19:14:50 +00004069 // Import type-source information.
4070 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4071 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004072 return nullptr;
4073
Douglas Gregora082a492010-11-30 19:14:50 +00004074 // FIXME: Import default argument.
4075
4076 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4077 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004078 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004079 Loc, D->getDepth(), D->getPosition(),
4080 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004081 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004082}
4083
4084Decl *
4085ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4086 // Import the name of this declaration.
4087 DeclarationName Name = Importer.Import(D->getDeclName());
4088 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004089 return nullptr;
4090
Douglas Gregora082a492010-11-30 19:14:50 +00004091 // Import the location of this declaration.
4092 SourceLocation Loc = Importer.Import(D->getLocation());
4093
4094 // Import template parameters.
4095 TemplateParameterList *TemplateParams
4096 = ImportTemplateParameterList(D->getTemplateParameters());
4097 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004098 return nullptr;
4099
Douglas Gregora082a492010-11-30 19:14:50 +00004100 // FIXME: Import default argument.
4101
4102 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4103 Importer.getToContext().getTranslationUnitDecl(),
4104 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004105 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004106 Name.getAsIdentifierInfo(),
4107 TemplateParams);
4108}
4109
4110Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4111 // If this record has a definition in the translation unit we're coming from,
4112 // but this particular declaration is not that definition, import the
4113 // definition and map to that.
4114 CXXRecordDecl *Definition
4115 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4116 if (Definition && Definition != D->getTemplatedDecl()) {
4117 Decl *ImportedDef
4118 = Importer.Import(Definition->getDescribedClassTemplate());
4119 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004120 return nullptr;
4121
Douglas Gregora082a492010-11-30 19:14:50 +00004122 return Importer.Imported(D, ImportedDef);
4123 }
4124
4125 // Import the major distinguishing characteristics of this class template.
4126 DeclContext *DC, *LexicalDC;
4127 DeclarationName Name;
4128 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004129 NamedDecl *ToD;
4130 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004131 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004132 if (ToD)
4133 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004134
Douglas Gregora082a492010-11-30 19:14:50 +00004135 // We may already have a template of the same name; try to find and match it.
4136 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004137 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004138 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004139 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004140 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4141 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004142 continue;
4143
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004144 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004145 if (ClassTemplateDecl *FoundTemplate
4146 = dyn_cast<ClassTemplateDecl>(Found)) {
4147 if (IsStructuralMatch(D, FoundTemplate)) {
4148 // The class templates structurally match; call it the same template.
4149 // FIXME: We may be filling in a forward declaration here. Handle
4150 // this case!
4151 Importer.Imported(D->getTemplatedDecl(),
4152 FoundTemplate->getTemplatedDecl());
4153 return Importer.Imported(D, FoundTemplate);
4154 }
4155 }
4156
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004157 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004158 }
4159
4160 if (!ConflictingDecls.empty()) {
4161 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4162 ConflictingDecls.data(),
4163 ConflictingDecls.size());
4164 }
4165
4166 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004167 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004168 }
4169
4170 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4171
4172 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004173 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4174 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004175 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4176 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004177 DC, StartLoc, IdLoc,
4178 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004179 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004180 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004181 D2Templated->setLexicalDeclContext(LexicalDC);
4182
4183 // Create the class template declaration itself.
4184 TemplateParameterList *TemplateParams
4185 = ImportTemplateParameterList(D->getTemplateParameters());
4186 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004187 return nullptr;
4188
Douglas Gregora082a492010-11-30 19:14:50 +00004189 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4190 Loc, Name, TemplateParams,
4191 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004192 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004193 D2Templated->setDescribedClassTemplate(D2);
4194
4195 D2->setAccess(D->getAccess());
4196 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004197 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004198
4199 // Note the relationship between the class templates.
4200 Importer.Imported(D, D2);
4201 Importer.Imported(DTemplated, D2Templated);
4202
John McCallf937c022011-10-07 06:10:15 +00004203 if (DTemplated->isCompleteDefinition() &&
4204 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004205 // FIXME: Import definition!
4206 }
4207
4208 return D2;
4209}
4210
Douglas Gregore2e50d332010-12-01 01:36:18 +00004211Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4212 ClassTemplateSpecializationDecl *D) {
4213 // If this record has a definition in the translation unit we're coming from,
4214 // but this particular declaration is not that definition, import the
4215 // definition and map to that.
4216 TagDecl *Definition = D->getDefinition();
4217 if (Definition && Definition != D) {
4218 Decl *ImportedDef = Importer.Import(Definition);
4219 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004220 return nullptr;
4221
Douglas Gregore2e50d332010-12-01 01:36:18 +00004222 return Importer.Imported(D, ImportedDef);
4223 }
4224
4225 ClassTemplateDecl *ClassTemplate
4226 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4227 D->getSpecializedTemplate()));
4228 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004229 return nullptr;
4230
Douglas Gregore2e50d332010-12-01 01:36:18 +00004231 // Import the context of this declaration.
4232 DeclContext *DC = ClassTemplate->getDeclContext();
4233 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004234 return nullptr;
4235
Douglas Gregore2e50d332010-12-01 01:36:18 +00004236 DeclContext *LexicalDC = DC;
4237 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4238 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4239 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004240 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004241 }
4242
4243 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004244 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4245 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004246
4247 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004248 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004249 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4250 D->getTemplateArgs().size(),
4251 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004252 return nullptr;
4253
Douglas Gregore2e50d332010-12-01 01:36:18 +00004254 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004255 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004256 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004257 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004258 if (D2) {
4259 // We already have a class template specialization with these template
4260 // arguments.
4261
4262 // FIXME: Check for specialization vs. instantiation errors.
4263
4264 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004265 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004266 // The record types structurally match, or the "from" translation
4267 // unit only had a forward declaration anyway; call it the same
4268 // function.
4269 return Importer.Imported(D, FoundDef);
4270 }
4271 }
4272 } else {
4273 // Create a new specialization.
4274 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4275 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004276 StartLoc, IdLoc,
4277 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004278 TemplateArgs.data(),
4279 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004280 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004281 D2->setSpecializationKind(D->getSpecializationKind());
4282
4283 // Add this specialization to the class template.
4284 ClassTemplate->AddSpecialization(D2, InsertPos);
4285
4286 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004287 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004288
4289 // Add the specialization to this context.
4290 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004291 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004292 }
4293 Importer.Imported(D, D2);
4294
John McCallf937c022011-10-07 06:10:15 +00004295 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004296 return nullptr;
4297
Douglas Gregore2e50d332010-12-01 01:36:18 +00004298 return D2;
4299}
4300
Larisse Voufo39a1e502013-08-06 01:03:05 +00004301Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4302 // If this variable has a definition in the translation unit we're coming
4303 // from,
4304 // but this particular declaration is not that definition, import the
4305 // definition and map to that.
4306 VarDecl *Definition =
4307 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4308 if (Definition && Definition != D->getTemplatedDecl()) {
4309 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4310 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004311 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004312
4313 return Importer.Imported(D, ImportedDef);
4314 }
4315
4316 // Import the major distinguishing characteristics of this variable template.
4317 DeclContext *DC, *LexicalDC;
4318 DeclarationName Name;
4319 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004320 NamedDecl *ToD;
4321 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004322 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004323 if (ToD)
4324 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004325
4326 // We may already have a template of the same name; try to find and match it.
4327 assert(!DC->isFunctionOrMethod() &&
4328 "Variable templates cannot be declared at function scope");
4329 SmallVector<NamedDecl *, 4> ConflictingDecls;
4330 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004331 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004332 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4333 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4334 continue;
4335
4336 Decl *Found = FoundDecls[I];
4337 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4338 if (IsStructuralMatch(D, FoundTemplate)) {
4339 // The variable templates structurally match; call it the same template.
4340 Importer.Imported(D->getTemplatedDecl(),
4341 FoundTemplate->getTemplatedDecl());
4342 return Importer.Imported(D, FoundTemplate);
4343 }
4344 }
4345
4346 ConflictingDecls.push_back(FoundDecls[I]);
4347 }
4348
4349 if (!ConflictingDecls.empty()) {
4350 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4351 ConflictingDecls.data(),
4352 ConflictingDecls.size());
4353 }
4354
4355 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004356 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004357
4358 VarDecl *DTemplated = D->getTemplatedDecl();
4359
4360 // Import the type.
4361 QualType T = Importer.Import(DTemplated->getType());
4362 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004363 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004364
4365 // Create the declaration that is being templated.
4366 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4367 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4368 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4369 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4370 IdLoc, Name.getAsIdentifierInfo(), T,
4371 TInfo, DTemplated->getStorageClass());
4372 D2Templated->setAccess(DTemplated->getAccess());
4373 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4374 D2Templated->setLexicalDeclContext(LexicalDC);
4375
4376 // Importer.Imported(DTemplated, D2Templated);
4377 // LexicalDC->addDeclInternal(D2Templated);
4378
4379 // Merge the initializer.
4380 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004381 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004382
4383 // Create the variable template declaration itself.
4384 TemplateParameterList *TemplateParams =
4385 ImportTemplateParameterList(D->getTemplateParameters());
4386 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004387 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004388
4389 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004390 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004391 D2Templated->setDescribedVarTemplate(D2);
4392
4393 D2->setAccess(D->getAccess());
4394 D2->setLexicalDeclContext(LexicalDC);
4395 LexicalDC->addDeclInternal(D2);
4396
4397 // Note the relationship between the variable templates.
4398 Importer.Imported(D, D2);
4399 Importer.Imported(DTemplated, D2Templated);
4400
4401 if (DTemplated->isThisDeclarationADefinition() &&
4402 !D2Templated->isThisDeclarationADefinition()) {
4403 // FIXME: Import definition!
4404 }
4405
4406 return D2;
4407}
4408
4409Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4410 VarTemplateSpecializationDecl *D) {
4411 // If this record has a definition in the translation unit we're coming from,
4412 // but this particular declaration is not that definition, import the
4413 // definition and map to that.
4414 VarDecl *Definition = D->getDefinition();
4415 if (Definition && Definition != D) {
4416 Decl *ImportedDef = Importer.Import(Definition);
4417 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004418 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004419
4420 return Importer.Imported(D, ImportedDef);
4421 }
4422
4423 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4424 Importer.Import(D->getSpecializedTemplate()));
4425 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004426 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004427
4428 // Import the context of this declaration.
4429 DeclContext *DC = VarTemplate->getDeclContext();
4430 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004431 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004432
4433 DeclContext *LexicalDC = DC;
4434 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4435 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4436 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004437 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004438 }
4439
4440 // Import the location of this declaration.
4441 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4442 SourceLocation IdLoc = Importer.Import(D->getLocation());
4443
4444 // Import template arguments.
4445 SmallVector<TemplateArgument, 2> TemplateArgs;
4446 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4447 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004448 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004449
4450 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004451 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004452 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004453 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004454 if (D2) {
4455 // We already have a variable template specialization with these template
4456 // arguments.
4457
4458 // FIXME: Check for specialization vs. instantiation errors.
4459
4460 if (VarDecl *FoundDef = D2->getDefinition()) {
4461 if (!D->isThisDeclarationADefinition() ||
4462 IsStructuralMatch(D, FoundDef)) {
4463 // The record types structurally match, or the "from" translation
4464 // unit only had a forward declaration anyway; call it the same
4465 // variable.
4466 return Importer.Imported(D, FoundDef);
4467 }
4468 }
4469 } else {
4470
4471 // Import the type.
4472 QualType T = Importer.Import(D->getType());
4473 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004474 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004475 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4476
4477 // Create a new specialization.
4478 D2 = VarTemplateSpecializationDecl::Create(
4479 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4480 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4481 D2->setSpecializationKind(D->getSpecializationKind());
4482 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4483
4484 // Add this specialization to the class template.
4485 VarTemplate->AddSpecialization(D2, InsertPos);
4486
4487 // Import the qualifier, if any.
4488 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4489
4490 // Add the specialization to this context.
4491 D2->setLexicalDeclContext(LexicalDC);
4492 LexicalDC->addDeclInternal(D2);
4493 }
4494 Importer.Imported(D, D2);
4495
4496 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004497 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004498
4499 return D2;
4500}
4501
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004502//----------------------------------------------------------------------------
4503// Import Statements
4504//----------------------------------------------------------------------------
4505
Sean Callanan59721b32015-04-28 18:41:46 +00004506DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4507 if (DG.isNull())
4508 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4509 size_t NumDecls = DG.end() - DG.begin();
4510 SmallVector<Decl *, 1> ToDecls(NumDecls);
4511 auto &_Importer = this->Importer;
4512 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4513 [&_Importer](Decl *D) -> Decl * {
4514 return _Importer.Import(D);
4515 });
4516 return DeclGroupRef::Create(Importer.getToContext(),
4517 ToDecls.begin(),
4518 NumDecls);
4519}
4520
4521 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4522 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4523 << S->getStmtClassName();
4524 return nullptr;
4525 }
4526
4527Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4528 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4529 for (Decl *ToD : ToDG) {
4530 if (!ToD)
4531 return nullptr;
4532 }
4533 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4534 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4535 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4536}
4537
4538Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4539 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4540 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4541 S->hasLeadingEmptyMacro());
4542}
4543
4544Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4545 SmallVector<Stmt *, 4> ToStmts(S->size());
4546 auto &_Importer = this->Importer;
4547 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4548 [&_Importer](Stmt *CS) -> Stmt * {
4549 return _Importer.Import(CS);
4550 });
4551 for (Stmt *ToS : ToStmts) {
4552 if (!ToS)
4553 return nullptr;
4554 }
4555 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4556 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4557 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4558 ToStmts,
4559 ToLBraceLoc, ToRBraceLoc);
4560}
4561
4562Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4563 Expr *ToLHS = Importer.Import(S->getLHS());
4564 if (!ToLHS)
4565 return nullptr;
4566 Expr *ToRHS = Importer.Import(S->getRHS());
4567 if (!ToRHS && S->getRHS())
4568 return nullptr;
4569 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4570 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4571 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4572 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4573 ToCaseLoc, ToEllipsisLoc,
4574 ToColonLoc);
4575}
4576
4577Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4578 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4579 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4580 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4581 if (!ToSubStmt && S->getSubStmt())
4582 return nullptr;
4583 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4584 ToSubStmt);
4585}
4586
4587Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4588 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4589 LabelDecl *ToLabelDecl =
4590 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4591 if (!ToLabelDecl && S->getDecl())
4592 return nullptr;
4593 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4594 if (!ToSubStmt && S->getSubStmt())
4595 return nullptr;
4596 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4597 ToSubStmt);
4598}
4599
4600Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4601 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4602 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4603 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4604 ASTContext &_ToContext = Importer.getToContext();
4605 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4606 [&_ToContext](const Attr *A) -> const Attr * {
4607 return A->clone(_ToContext);
4608 });
4609 for (const Attr *ToA : ToAttrs) {
4610 if (!ToA)
4611 return nullptr;
4612 }
4613 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4614 if (!ToSubStmt && S->getSubStmt())
4615 return nullptr;
4616 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4617 ToAttrs, ToSubStmt);
4618}
4619
4620Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4621 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4622 VarDecl *ToConditionVariable = nullptr;
4623 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4624 ToConditionVariable =
4625 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4626 if (!ToConditionVariable)
4627 return nullptr;
4628 }
4629 Expr *ToCondition = Importer.Import(S->getCond());
4630 if (!ToCondition && S->getCond())
4631 return nullptr;
4632 Stmt *ToThenStmt = Importer.Import(S->getThen());
4633 if (!ToThenStmt && S->getThen())
4634 return nullptr;
4635 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4636 Stmt *ToElseStmt = Importer.Import(S->getElse());
4637 if (!ToElseStmt && S->getElse())
4638 return nullptr;
4639 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4640 ToIfLoc, ToConditionVariable,
4641 ToCondition, ToThenStmt,
4642 ToElseLoc, ToElseStmt);
4643}
4644
4645Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4646 VarDecl *ToConditionVariable = nullptr;
4647 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4648 ToConditionVariable =
4649 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4650 if (!ToConditionVariable)
4651 return nullptr;
4652 }
4653 Expr *ToCondition = Importer.Import(S->getCond());
4654 if (!ToCondition && S->getCond())
4655 return nullptr;
4656 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4657 Importer.getToContext(), ToConditionVariable,
4658 ToCondition);
4659 Stmt *ToBody = Importer.Import(S->getBody());
4660 if (!ToBody && S->getBody())
4661 return nullptr;
4662 ToStmt->setBody(ToBody);
4663 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4664 // Now we have to re-chain the cases.
4665 SwitchCase *LastChainedSwitchCase = nullptr;
4666 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4667 SC = SC->getNextSwitchCase()) {
4668 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4669 if (!ToSC)
4670 return nullptr;
4671 if (LastChainedSwitchCase)
4672 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4673 else
4674 ToStmt->setSwitchCaseList(ToSC);
4675 LastChainedSwitchCase = ToSC;
4676 }
4677 return ToStmt;
4678}
4679
4680Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4681 VarDecl *ToConditionVariable = nullptr;
4682 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4683 ToConditionVariable =
4684 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4685 if (!ToConditionVariable)
4686 return nullptr;
4687 }
4688 Expr *ToCondition = Importer.Import(S->getCond());
4689 if (!ToCondition && S->getCond())
4690 return nullptr;
4691 Stmt *ToBody = Importer.Import(S->getBody());
4692 if (!ToBody && S->getBody())
4693 return nullptr;
4694 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4695 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4696 ToConditionVariable,
4697 ToCondition, ToBody,
4698 ToWhileLoc);
4699}
4700
4701Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4702 Stmt *ToBody = Importer.Import(S->getBody());
4703 if (!ToBody && S->getBody())
4704 return nullptr;
4705 Expr *ToCondition = Importer.Import(S->getCond());
4706 if (!ToCondition && S->getCond())
4707 return nullptr;
4708 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4709 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4710 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4711 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4712 ToDoLoc, ToWhileLoc,
4713 ToRParenLoc);
4714}
4715
4716Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4717 Stmt *ToInit = Importer.Import(S->getInit());
4718 if (!ToInit && S->getInit())
4719 return nullptr;
4720 Expr *ToCondition = Importer.Import(S->getCond());
4721 if (!ToCondition && S->getCond())
4722 return nullptr;
4723 VarDecl *ToConditionVariable = nullptr;
4724 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4725 ToConditionVariable =
4726 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4727 if (!ToConditionVariable)
4728 return nullptr;
4729 }
4730 Expr *ToInc = Importer.Import(S->getInc());
4731 if (!ToInc && S->getInc())
4732 return nullptr;
4733 Stmt *ToBody = Importer.Import(S->getBody());
4734 if (!ToBody && S->getBody())
4735 return nullptr;
4736 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4737 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4738 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4739 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4740 ToInit, ToCondition,
4741 ToConditionVariable,
4742 ToInc, ToBody,
4743 ToForLoc, ToLParenLoc,
4744 ToRParenLoc);
4745}
4746
4747Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4748 LabelDecl *ToLabel = nullptr;
4749 if (LabelDecl *FromLabel = S->getLabel()) {
4750 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4751 if (!ToLabel)
4752 return nullptr;
4753 }
4754 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4755 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4756 return new (Importer.getToContext()) GotoStmt(ToLabel,
4757 ToGotoLoc, ToLabelLoc);
4758}
4759
4760Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4761 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4762 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4763 Expr *ToTarget = Importer.Import(S->getTarget());
4764 if (!ToTarget && S->getTarget())
4765 return nullptr;
4766 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4767 ToTarget);
4768}
4769
4770Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4771 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4772 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4773}
4774
4775Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4776 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4777 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4778}
4779
4780Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4781 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4782 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4783 if (!ToRetExpr && S->getRetValue())
4784 return nullptr;
4785 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4786 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4787 if (!ToNRVOCandidate && NRVOCandidate)
4788 return nullptr;
4789 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4790 ToNRVOCandidate);
4791}
4792
4793Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4794 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4795 VarDecl *ToExceptionDecl = nullptr;
4796 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4797 ToExceptionDecl =
4798 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4799 if (!ToExceptionDecl)
4800 return nullptr;
4801 }
4802 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4803 if (!ToHandlerBlock && S->getHandlerBlock())
4804 return nullptr;
4805 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4806 ToExceptionDecl,
4807 ToHandlerBlock);
4808}
4809
4810Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4811 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4812 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4813 if (!ToTryBlock && S->getTryBlock())
4814 return nullptr;
4815 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4816 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4817 CXXCatchStmt *FromHandler = S->getHandler(HI);
4818 if (Stmt *ToHandler = Importer.Import(FromHandler))
4819 ToHandlers[HI] = ToHandler;
4820 else
4821 return nullptr;
4822 }
4823 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4824 ToHandlers);
4825}
4826
4827Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4828 DeclStmt *ToRange =
4829 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4830 if (!ToRange && S->getRangeStmt())
4831 return nullptr;
4832 DeclStmt *ToBeginEnd =
4833 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4834 if (!ToBeginEnd && S->getBeginEndStmt())
4835 return nullptr;
4836 Expr *ToCond = Importer.Import(S->getCond());
4837 if (!ToCond && S->getCond())
4838 return nullptr;
4839 Expr *ToInc = Importer.Import(S->getInc());
4840 if (!ToInc && S->getInc())
4841 return nullptr;
4842 DeclStmt *ToLoopVar =
4843 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4844 if (!ToLoopVar && S->getLoopVarStmt())
4845 return nullptr;
4846 Stmt *ToBody = Importer.Import(S->getBody());
4847 if (!ToBody && S->getBody())
4848 return nullptr;
4849 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4850 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4851 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4852 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4853 ToCond, ToInc,
4854 ToLoopVar, ToBody,
4855 ToForLoc, ToColonLoc,
4856 ToRParenLoc);
4857}
4858
4859Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4860 Stmt *ToElem = Importer.Import(S->getElement());
4861 if (!ToElem && S->getElement())
4862 return nullptr;
4863 Expr *ToCollect = Importer.Import(S->getCollection());
4864 if (!ToCollect && S->getCollection())
4865 return nullptr;
4866 Stmt *ToBody = Importer.Import(S->getBody());
4867 if (!ToBody && S->getBody())
4868 return nullptr;
4869 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4870 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4871 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4872 ToCollect,
4873 ToBody, ToForLoc,
4874 ToRParenLoc);
4875}
4876
4877Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4878 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4879 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4880 VarDecl *ToExceptionDecl = nullptr;
4881 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4882 ToExceptionDecl =
4883 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4884 if (!ToExceptionDecl)
4885 return nullptr;
4886 }
4887 Stmt *ToBody = Importer.Import(S->getCatchBody());
4888 if (!ToBody && S->getCatchBody())
4889 return nullptr;
4890 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4891 ToRParenLoc,
4892 ToExceptionDecl,
4893 ToBody);
4894}
4895
4896Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4897 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4898 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4899 if (!ToAtFinallyStmt && S->getFinallyBody())
4900 return nullptr;
4901 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4902 ToAtFinallyStmt);
4903}
4904
4905Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4906 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4907 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4908 if (!ToAtTryStmt && S->getTryBody())
4909 return nullptr;
4910 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4911 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4912 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4913 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4914 ToCatchStmts[CI] = ToCatchStmt;
4915 else
4916 return nullptr;
4917 }
4918 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4919 if (!ToAtFinallyStmt && S->getFinallyStmt())
4920 return nullptr;
4921 return ObjCAtTryStmt::Create(Importer.getToContext(),
4922 ToAtTryLoc, ToAtTryStmt,
4923 ToCatchStmts.begin(), ToCatchStmts.size(),
4924 ToAtFinallyStmt);
4925}
4926
4927Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4928 (ObjCAtSynchronizedStmt *S) {
4929 SourceLocation ToAtSynchronizedLoc =
4930 Importer.Import(S->getAtSynchronizedLoc());
4931 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4932 if (!ToSynchExpr && S->getSynchExpr())
4933 return nullptr;
4934 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4935 if (!ToSynchBody && S->getSynchBody())
4936 return nullptr;
4937 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4938 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4939}
4940
4941Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4942 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4943 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4944 if (!ToThrow && S->getThrowExpr())
4945 return nullptr;
4946 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4947}
4948
4949Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4950 (ObjCAutoreleasePoolStmt *S) {
4951 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4952 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4953 if (!ToSubStmt && S->getSubStmt())
4954 return nullptr;
4955 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4956 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004957}
4958
4959//----------------------------------------------------------------------------
4960// Import Expressions
4961//----------------------------------------------------------------------------
4962Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4963 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4964 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004965 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004966}
4967
Douglas Gregor52f820e2010-02-19 01:17:02 +00004968Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004969 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4970 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004971 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004972
Craig Topper36250ad2014-05-12 05:36:57 +00004973 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004974 if (E->getDecl() != E->getFoundDecl()) {
4975 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4976 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004977 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004978 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004979
4980 QualType T = Importer.Import(E->getType());
4981 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004982 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004983
4984 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4985 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004986 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004987 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004988 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004989 Importer.Import(E->getLocation()),
4990 T, E->getValueKind(),
4991 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00004992 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004993 if (E->hadMultipleCandidates())
4994 DRE->setHadMultipleCandidates(true);
4995 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004996}
4997
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004998Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4999 QualType T = Importer.Import(E->getType());
5000 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005001 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005002
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005003 return IntegerLiteral::Create(Importer.getToContext(),
5004 E->getValue(), T,
5005 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005006}
5007
Douglas Gregor623421d2010-02-18 02:21:22 +00005008Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5009 QualType T = Importer.Import(E->getType());
5010 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005011 return nullptr;
5012
Douglas Gregorfb65e592011-07-27 05:40:30 +00005013 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5014 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005015 Importer.Import(E->getLocation()));
5016}
5017
Douglas Gregorc74247e2010-02-19 01:07:06 +00005018Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5019 Expr *SubExpr = Importer.Import(E->getSubExpr());
5020 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005021 return nullptr;
5022
Douglas Gregorc74247e2010-02-19 01:07:06 +00005023 return new (Importer.getToContext())
5024 ParenExpr(Importer.Import(E->getLParen()),
5025 Importer.Import(E->getRParen()),
5026 SubExpr);
5027}
5028
5029Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5030 QualType T = Importer.Import(E->getType());
5031 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005032 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005033
5034 Expr *SubExpr = Importer.Import(E->getSubExpr());
5035 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005036 return nullptr;
5037
Douglas Gregorc74247e2010-02-19 01:07:06 +00005038 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005039 T, E->getValueKind(),
5040 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005041 Importer.Import(E->getOperatorLoc()));
5042}
5043
Peter Collingbournee190dee2011-03-11 19:24:49 +00005044Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5045 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005046 QualType ResultType = Importer.Import(E->getType());
5047
5048 if (E->isArgumentType()) {
5049 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5050 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005051 return nullptr;
5052
Peter Collingbournee190dee2011-03-11 19:24:49 +00005053 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5054 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005055 Importer.Import(E->getOperatorLoc()),
5056 Importer.Import(E->getRParenLoc()));
5057 }
5058
5059 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5060 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005061 return nullptr;
5062
Peter Collingbournee190dee2011-03-11 19:24:49 +00005063 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5064 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005065 Importer.Import(E->getOperatorLoc()),
5066 Importer.Import(E->getRParenLoc()));
5067}
5068
Douglas Gregorc74247e2010-02-19 01:07:06 +00005069Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5070 QualType T = Importer.Import(E->getType());
5071 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005072 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005073
5074 Expr *LHS = Importer.Import(E->getLHS());
5075 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005076 return nullptr;
5077
Douglas Gregorc74247e2010-02-19 01:07:06 +00005078 Expr *RHS = Importer.Import(E->getRHS());
5079 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005080 return nullptr;
5081
Douglas Gregorc74247e2010-02-19 01:07:06 +00005082 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005083 T, E->getValueKind(),
5084 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005085 Importer.Import(E->getOperatorLoc()),
5086 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005087}
5088
5089Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5090 QualType T = Importer.Import(E->getType());
5091 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005092 return nullptr;
5093
Douglas Gregorc74247e2010-02-19 01:07:06 +00005094 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5095 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005096 return nullptr;
5097
Douglas Gregorc74247e2010-02-19 01:07:06 +00005098 QualType CompResultType = Importer.Import(E->getComputationResultType());
5099 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005100 return nullptr;
5101
Douglas Gregorc74247e2010-02-19 01:07:06 +00005102 Expr *LHS = Importer.Import(E->getLHS());
5103 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005104 return nullptr;
5105
Douglas Gregorc74247e2010-02-19 01:07:06 +00005106 Expr *RHS = Importer.Import(E->getRHS());
5107 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005108 return nullptr;
5109
Douglas Gregorc74247e2010-02-19 01:07:06 +00005110 return new (Importer.getToContext())
5111 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005112 T, E->getValueKind(),
5113 E->getObjectKind(),
5114 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005115 Importer.Import(E->getOperatorLoc()),
5116 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005117}
5118
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005119static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005120 if (E->path_empty()) return false;
5121
5122 // TODO: import cast paths
5123 return true;
5124}
5125
Douglas Gregor98c10182010-02-12 22:17:39 +00005126Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5127 QualType T = Importer.Import(E->getType());
5128 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005129 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005130
5131 Expr *SubExpr = Importer.Import(E->getSubExpr());
5132 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005133 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005134
5135 CXXCastPath BasePath;
5136 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005137 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005138
5139 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005140 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005141}
5142
Douglas Gregor5481d322010-02-19 01:32:14 +00005143Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5144 QualType T = Importer.Import(E->getType());
5145 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005146 return nullptr;
5147
Douglas Gregor5481d322010-02-19 01:32:14 +00005148 Expr *SubExpr = Importer.Import(E->getSubExpr());
5149 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005150 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005151
5152 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5153 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005154 return nullptr;
5155
John McCallcf142162010-08-07 06:22:56 +00005156 CXXCastPath BasePath;
5157 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005158 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005159
John McCall7decc9e2010-11-18 06:31:45 +00005160 return CStyleCastExpr::Create(Importer.getToContext(), T,
5161 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005162 SubExpr, &BasePath, TInfo,
5163 Importer.Import(E->getLParenLoc()),
5164 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005165}
5166
Sean Callanan59721b32015-04-28 18:41:46 +00005167Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5168 QualType T = Importer.Import(E->getType());
5169 if (T.isNull())
5170 return nullptr;
5171
5172 CXXConstructorDecl *ToCCD =
5173 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5174 if (!ToCCD && E->getConstructor())
5175 return nullptr;
5176
5177 size_t NumArgs = E->getNumArgs();
5178 SmallVector<Expr *, 1> ToArgs(NumArgs);
5179 ASTImporter &_Importer = Importer;
5180 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5181 [&_Importer](Expr *AE) -> Expr * {
5182 return _Importer.Import(AE);
5183 });
5184 for (Expr *ToA : ToArgs) {
5185 if (!ToA)
5186 return nullptr;
5187 }
5188
5189 return CXXConstructExpr::Create(Importer.getToContext(), T,
5190 Importer.Import(E->getLocation()),
5191 ToCCD, E->isElidable(),
5192 ToArgs, E->hadMultipleCandidates(),
5193 E->isListInitialization(),
5194 E->isStdInitListInitialization(),
5195 E->requiresZeroInitialization(),
5196 E->getConstructionKind(),
5197 Importer.Import(E->getParenOrBraceRange()));
5198}
5199
5200Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5201 QualType T = Importer.Import(E->getType());
5202 if (T.isNull())
5203 return nullptr;
5204
5205 Expr *ToBase = Importer.Import(E->getBase());
5206 if (!ToBase && E->getBase())
5207 return nullptr;
5208
5209 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5210 if (!ToMember && E->getMemberDecl())
5211 return nullptr;
5212
5213 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5214 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5215 E->getFoundDecl().getAccess());
5216
5217 DeclarationNameInfo ToMemberNameInfo(
5218 Importer.Import(E->getMemberNameInfo().getName()),
5219 Importer.Import(E->getMemberNameInfo().getLoc()));
5220
5221 if (E->hasExplicitTemplateArgs()) {
5222 return nullptr; // FIXME: handle template arguments
5223 }
5224
5225 return MemberExpr::Create(Importer.getToContext(), ToBase,
5226 E->isArrow(),
5227 Importer.Import(E->getOperatorLoc()),
5228 Importer.Import(E->getQualifierLoc()),
5229 Importer.Import(E->getTemplateKeywordLoc()),
5230 ToMember, ToFoundDecl, ToMemberNameInfo,
5231 nullptr, T, E->getValueKind(),
5232 E->getObjectKind());
5233}
5234
5235Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5236 QualType T = Importer.Import(E->getType());
5237 if (T.isNull())
5238 return nullptr;
5239
5240 Expr *ToCallee = Importer.Import(E->getCallee());
5241 if (!ToCallee && E->getCallee())
5242 return nullptr;
5243
5244 unsigned NumArgs = E->getNumArgs();
5245
5246 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5247
5248 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5249 Expr *FromArg = E->getArg(ai);
5250 Expr *ToArg = Importer.Import(FromArg);
5251 if (!ToArg)
5252 return nullptr;
5253 ToArgs[ai] = ToArg;
5254 }
5255
5256 Expr **ToArgs_Copied = new (Importer.getToContext())
5257 Expr*[NumArgs];
5258
5259 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5260 ToArgs_Copied[ai] = ToArgs[ai];
5261
5262 return new (Importer.getToContext())
5263 CallExpr(Importer.getToContext(), ToCallee,
5264 ArrayRef<Expr*>(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5265 Importer.Import(E->getRParenLoc()));
5266}
5267
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005268ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005269 ASTContext &FromContext, FileManager &FromFileManager,
5270 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005271 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005272 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005273 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005274{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005275 ImportedDecls[FromContext.getTranslationUnitDecl()]
5276 = ToContext.getTranslationUnitDecl();
5277}
5278
5279ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005280
5281QualType ASTImporter::Import(QualType FromT) {
5282 if (FromT.isNull())
5283 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005284
5285 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005286
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005287 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005288 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5289 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005290 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005291 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005292
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005293 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005294 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005295 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005296 if (ToT.isNull())
5297 return ToT;
5298
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005299 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005300 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005301
John McCall424cec92011-01-19 06:33:43 +00005302 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005303}
5304
Douglas Gregor62d311f2010-02-09 19:21:46 +00005305TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005306 if (!FromTSI)
5307 return FromTSI;
5308
5309 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005310 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005311 QualType T = Import(FromTSI->getType());
5312 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005313 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005314
5315 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00005316 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00005317}
5318
Sean Callanan59721b32015-04-28 18:41:46 +00005319Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5320 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5321 if (Pos != ImportedDecls.end()) {
5322 Decl *ToD = Pos->second;
5323 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5324 return ToD;
5325 } else {
5326 return nullptr;
5327 }
5328}
5329
Douglas Gregor62d311f2010-02-09 19:21:46 +00005330Decl *ASTImporter::Import(Decl *FromD) {
5331 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005332 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005333
Douglas Gregord451ea92011-07-29 23:31:30 +00005334 ASTNodeImporter Importer(*this);
5335
Douglas Gregor62d311f2010-02-09 19:21:46 +00005336 // Check whether we've already imported this declaration.
5337 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005338 if (Pos != ImportedDecls.end()) {
5339 Decl *ToD = Pos->second;
5340 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5341 return ToD;
5342 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005343
5344 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005345 Decl *ToD = Importer.Visit(FromD);
5346 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005347 return nullptr;
5348
Douglas Gregor62d311f2010-02-09 19:21:46 +00005349 // Record the imported declaration.
5350 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005351
5352 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5353 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005354 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005355 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005356 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005357 // When we've finished transforming a typedef, see whether it was the
5358 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005359 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005360 FromTag = AnonTagsWithPendingTypedefs.begin(),
5361 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5362 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005363 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005364 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5365 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005366 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005367 AnonTagsWithPendingTypedefs.erase(FromTag);
5368 break;
5369 }
5370 }
5371 }
5372 }
5373
Douglas Gregor62d311f2010-02-09 19:21:46 +00005374 return ToD;
5375}
5376
5377DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5378 if (!FromDC)
5379 return FromDC;
5380
Douglas Gregor95d82832012-01-24 18:36:04 +00005381 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005382 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005383 return nullptr;
5384
Douglas Gregor2e15c842012-02-01 21:00:38 +00005385 // When we're using a record/enum/Objective-C class/protocol as a context, we
5386 // need it to have a definition.
5387 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005388 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005389 if (ToRecord->isCompleteDefinition()) {
5390 // Do nothing.
5391 } else if (FromRecord->isCompleteDefinition()) {
5392 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5393 ASTNodeImporter::IDK_Basic);
5394 } else {
5395 CompleteDecl(ToRecord);
5396 }
5397 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5398 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5399 if (ToEnum->isCompleteDefinition()) {
5400 // Do nothing.
5401 } else if (FromEnum->isCompleteDefinition()) {
5402 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5403 ASTNodeImporter::IDK_Basic);
5404 } else {
5405 CompleteDecl(ToEnum);
5406 }
5407 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5408 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5409 if (ToClass->getDefinition()) {
5410 // Do nothing.
5411 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5412 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5413 ASTNodeImporter::IDK_Basic);
5414 } else {
5415 CompleteDecl(ToClass);
5416 }
5417 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5418 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5419 if (ToProto->getDefinition()) {
5420 // Do nothing.
5421 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5422 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5423 ASTNodeImporter::IDK_Basic);
5424 } else {
5425 CompleteDecl(ToProto);
5426 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005427 }
5428
5429 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005430}
5431
5432Expr *ASTImporter::Import(Expr *FromE) {
5433 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005434 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005435
5436 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5437}
5438
5439Stmt *ASTImporter::Import(Stmt *FromS) {
5440 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005441 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005442
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005443 // Check whether we've already imported this declaration.
5444 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5445 if (Pos != ImportedStmts.end())
5446 return Pos->second;
5447
5448 // Import the type
5449 ASTNodeImporter Importer(*this);
5450 Stmt *ToS = Importer.Visit(FromS);
5451 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005452 return nullptr;
5453
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005454 // Record the imported declaration.
5455 ImportedStmts[FromS] = ToS;
5456 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005457}
5458
5459NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5460 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005461 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005462
Douglas Gregor90ebf252011-04-27 16:48:40 +00005463 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5464
5465 switch (FromNNS->getKind()) {
5466 case NestedNameSpecifier::Identifier:
5467 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5468 return NestedNameSpecifier::Create(ToContext, prefix, II);
5469 }
Craig Topper36250ad2014-05-12 05:36:57 +00005470 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005471
5472 case NestedNameSpecifier::Namespace:
5473 if (NamespaceDecl *NS =
5474 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5475 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5476 }
Craig Topper36250ad2014-05-12 05:36:57 +00005477 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005478
5479 case NestedNameSpecifier::NamespaceAlias:
5480 if (NamespaceAliasDecl *NSAD =
5481 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5482 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5483 }
Craig Topper36250ad2014-05-12 05:36:57 +00005484 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005485
5486 case NestedNameSpecifier::Global:
5487 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5488
Nikola Smiljanic67860242014-09-26 00:28:20 +00005489 case NestedNameSpecifier::Super:
5490 if (CXXRecordDecl *RD =
5491 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5492 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5493 }
5494 return nullptr;
5495
Douglas Gregor90ebf252011-04-27 16:48:40 +00005496 case NestedNameSpecifier::TypeSpec:
5497 case NestedNameSpecifier::TypeSpecWithTemplate: {
5498 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5499 if (!T.isNull()) {
5500 bool bTemplate = FromNNS->getKind() ==
5501 NestedNameSpecifier::TypeSpecWithTemplate;
5502 return NestedNameSpecifier::Create(ToContext, prefix,
5503 bTemplate, T.getTypePtr());
5504 }
5505 }
Craig Topper36250ad2014-05-12 05:36:57 +00005506 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005507 }
5508
5509 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005510}
5511
Douglas Gregor14454802011-02-25 02:25:35 +00005512NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5513 // FIXME: Implement!
5514 return NestedNameSpecifierLoc();
5515}
5516
Douglas Gregore2e50d332010-12-01 01:36:18 +00005517TemplateName ASTImporter::Import(TemplateName From) {
5518 switch (From.getKind()) {
5519 case TemplateName::Template:
5520 if (TemplateDecl *ToTemplate
5521 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5522 return TemplateName(ToTemplate);
5523
5524 return TemplateName();
5525
5526 case TemplateName::OverloadedTemplate: {
5527 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5528 UnresolvedSet<2> ToTemplates;
5529 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5530 E = FromStorage->end();
5531 I != E; ++I) {
5532 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5533 ToTemplates.addDecl(To);
5534 else
5535 return TemplateName();
5536 }
5537 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5538 ToTemplates.end());
5539 }
5540
5541 case TemplateName::QualifiedTemplate: {
5542 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5543 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5544 if (!Qualifier)
5545 return TemplateName();
5546
5547 if (TemplateDecl *ToTemplate
5548 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5549 return ToContext.getQualifiedTemplateName(Qualifier,
5550 QTN->hasTemplateKeyword(),
5551 ToTemplate);
5552
5553 return TemplateName();
5554 }
5555
5556 case TemplateName::DependentTemplate: {
5557 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5558 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5559 if (!Qualifier)
5560 return TemplateName();
5561
5562 if (DTN->isIdentifier()) {
5563 return ToContext.getDependentTemplateName(Qualifier,
5564 Import(DTN->getIdentifier()));
5565 }
5566
5567 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5568 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005569
5570 case TemplateName::SubstTemplateTemplateParm: {
5571 SubstTemplateTemplateParmStorage *subst
5572 = From.getAsSubstTemplateTemplateParm();
5573 TemplateTemplateParmDecl *param
5574 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5575 if (!param)
5576 return TemplateName();
5577
5578 TemplateName replacement = Import(subst->getReplacement());
5579 if (replacement.isNull()) return TemplateName();
5580
5581 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5582 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005583
5584 case TemplateName::SubstTemplateTemplateParmPack: {
5585 SubstTemplateTemplateParmPackStorage *SubstPack
5586 = From.getAsSubstTemplateTemplateParmPack();
5587 TemplateTemplateParmDecl *Param
5588 = cast_or_null<TemplateTemplateParmDecl>(
5589 Import(SubstPack->getParameterPack()));
5590 if (!Param)
5591 return TemplateName();
5592
5593 ASTNodeImporter Importer(*this);
5594 TemplateArgument ArgPack
5595 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5596 if (ArgPack.isNull())
5597 return TemplateName();
5598
5599 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5600 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005601 }
5602
5603 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005604}
5605
Douglas Gregor62d311f2010-02-09 19:21:46 +00005606SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5607 if (FromLoc.isInvalid())
5608 return SourceLocation();
5609
Douglas Gregor811663e2010-02-10 00:15:17 +00005610 SourceManager &FromSM = FromContext.getSourceManager();
5611
5612 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005613 // don't have to import macro expansions.
5614 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005615 FromLoc = FromSM.getSpellingLoc(FromLoc);
5616 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5617 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005618 FileID ToFileID = Import(Decomposed.first);
5619 if (ToFileID.isInvalid())
5620 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005621 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5622 .getLocWithOffset(Decomposed.second);
5623 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005624}
5625
5626SourceRange ASTImporter::Import(SourceRange FromRange) {
5627 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5628}
5629
Douglas Gregor811663e2010-02-10 00:15:17 +00005630FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005631 llvm::DenseMap<FileID, FileID>::iterator Pos
5632 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005633 if (Pos != ImportedFileIDs.end())
5634 return Pos->second;
5635
5636 SourceManager &FromSM = FromContext.getSourceManager();
5637 SourceManager &ToSM = ToContext.getSourceManager();
5638 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005639 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005640
5641 // Include location of this file.
5642 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5643
5644 // Map the FileID for to the "to" source manager.
5645 FileID ToID;
5646 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005647 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005648 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5649 // disk again
5650 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5651 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005652 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005653 if (!Entry)
5654 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005655 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5656 FromSLoc.getFile().getFileCharacteristic());
5657 } else {
5658 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005659 const llvm::MemoryBuffer *
5660 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005661 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005662 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005663 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005664 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005665 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005666 }
5667
5668
Sebastian Redl99219f12010-09-30 01:03:06 +00005669 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005670 return ToID;
5671}
5672
Douglas Gregor0a791672011-01-18 03:11:38 +00005673void ASTImporter::ImportDefinition(Decl *From) {
5674 Decl *To = Import(From);
5675 if (!To)
5676 return;
5677
5678 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5679 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005680
5681 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5682 if (!ToRecord->getDefinition()) {
5683 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005684 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005685 return;
5686 }
5687 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005688
5689 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5690 if (!ToEnum->getDefinition()) {
5691 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005692 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005693 return;
5694 }
5695 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005696
5697 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5698 if (!ToIFace->getDefinition()) {
5699 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005700 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005701 return;
5702 }
5703 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005704
Douglas Gregor2aa53772012-01-24 17:42:07 +00005705 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5706 if (!ToProto->getDefinition()) {
5707 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005708 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005709 return;
5710 }
5711 }
5712
Douglas Gregor0a791672011-01-18 03:11:38 +00005713 Importer.ImportDeclContext(FromDC, true);
5714 }
5715}
5716
Douglas Gregor96e578d2010-02-05 17:54:41 +00005717DeclarationName ASTImporter::Import(DeclarationName FromName) {
5718 if (!FromName)
5719 return DeclarationName();
5720
5721 switch (FromName.getNameKind()) {
5722 case DeclarationName::Identifier:
5723 return Import(FromName.getAsIdentifierInfo());
5724
5725 case DeclarationName::ObjCZeroArgSelector:
5726 case DeclarationName::ObjCOneArgSelector:
5727 case DeclarationName::ObjCMultiArgSelector:
5728 return Import(FromName.getObjCSelector());
5729
5730 case DeclarationName::CXXConstructorName: {
5731 QualType T = Import(FromName.getCXXNameType());
5732 if (T.isNull())
5733 return DeclarationName();
5734
5735 return ToContext.DeclarationNames.getCXXConstructorName(
5736 ToContext.getCanonicalType(T));
5737 }
5738
5739 case DeclarationName::CXXDestructorName: {
5740 QualType T = Import(FromName.getCXXNameType());
5741 if (T.isNull())
5742 return DeclarationName();
5743
5744 return ToContext.DeclarationNames.getCXXDestructorName(
5745 ToContext.getCanonicalType(T));
5746 }
5747
5748 case DeclarationName::CXXConversionFunctionName: {
5749 QualType T = Import(FromName.getCXXNameType());
5750 if (T.isNull())
5751 return DeclarationName();
5752
5753 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5754 ToContext.getCanonicalType(T));
5755 }
5756
5757 case DeclarationName::CXXOperatorName:
5758 return ToContext.DeclarationNames.getCXXOperatorName(
5759 FromName.getCXXOverloadedOperator());
5760
5761 case DeclarationName::CXXLiteralOperatorName:
5762 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5763 Import(FromName.getCXXLiteralIdentifier()));
5764
5765 case DeclarationName::CXXUsingDirective:
5766 // FIXME: STATICS!
5767 return DeclarationName::getUsingDirectiveName();
5768 }
5769
David Blaikiee4d798f2012-01-20 21:50:17 +00005770 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005771}
5772
Douglas Gregore2e50d332010-12-01 01:36:18 +00005773IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005774 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005775 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005776
5777 return &ToContext.Idents.get(FromId->getName());
5778}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005779
Douglas Gregor43f54792010-02-17 02:12:47 +00005780Selector ASTImporter::Import(Selector FromSel) {
5781 if (FromSel.isNull())
5782 return Selector();
5783
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005784 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005785 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5786 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5787 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5788 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5789}
5790
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005791DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5792 DeclContext *DC,
5793 unsigned IDNS,
5794 NamedDecl **Decls,
5795 unsigned NumDecls) {
5796 return Name;
5797}
5798
5799DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005800 if (LastDiagFromFrom)
5801 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5802 FromContext.getDiagnostics());
5803 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005804 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005805}
5806
5807DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005808 if (!LastDiagFromFrom)
5809 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5810 ToContext.getDiagnostics());
5811 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005812 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005813}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005814
Douglas Gregor2e15c842012-02-01 21:00:38 +00005815void ASTImporter::CompleteDecl (Decl *D) {
5816 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5817 if (!ID->getDefinition())
5818 ID->startDefinition();
5819 }
5820 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5821 if (!PD->getDefinition())
5822 PD->startDefinition();
5823 }
5824 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5825 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5826 TD->startDefinition();
5827 TD->setCompleteDefinition(true);
5828 }
5829 }
5830 else {
5831 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5832 }
5833}
5834
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005835Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5836 ImportedDecls[From] = To;
5837 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005838}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005839
Douglas Gregordd6006f2012-07-17 21:16:27 +00005840bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5841 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005842 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005843 = ImportedTypes.find(From.getTypePtr());
5844 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5845 return true;
5846
Douglas Gregordd6006f2012-07-17 21:16:27 +00005847 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5848 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005849 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005850}