blob: c75eba696cc978e8cd464d8c5bb7f4a9ec40f2aa [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.
John McCall339bb662010-06-04 20:50:08 +00003925 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3926 if (!T)
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 Gregora11c4582010-02-17 18:02:10 +00003935 T,
3936 D->getPropertyImplementation());
3937 Importer.Imported(D, ToProperty);
3938 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003939 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003940
3941 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003942 ToProperty->setPropertyAttributesAsWritten(
3943 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003944 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3945 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3946 ToProperty->setGetterMethodDecl(
3947 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3948 ToProperty->setSetterMethodDecl(
3949 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3950 ToProperty->setPropertyIvarDecl(
3951 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3952 return ToProperty;
3953}
3954
Douglas Gregor14a49e22010-12-07 18:32:03 +00003955Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3956 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3957 Importer.Import(D->getPropertyDecl()));
3958 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003959 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003960
3961 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3962 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003963 return nullptr;
3964
Douglas Gregor14a49e22010-12-07 18:32:03 +00003965 // Import the lexical declaration context.
3966 DeclContext *LexicalDC = DC;
3967 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3968 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3969 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003970 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003971 }
3972
3973 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3974 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003975 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003976
3977 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003978 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003979 if (D->getPropertyIvarDecl()) {
3980 Ivar = cast_or_null<ObjCIvarDecl>(
3981 Importer.Import(D->getPropertyIvarDecl()));
3982 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003983 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003984 }
3985
3986 ObjCPropertyImplDecl *ToImpl
3987 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3988 if (!ToImpl) {
3989 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3990 Importer.Import(D->getLocStart()),
3991 Importer.Import(D->getLocation()),
3992 Property,
3993 D->getPropertyImplementation(),
3994 Ivar,
3995 Importer.Import(D->getPropertyIvarDeclLoc()));
3996 ToImpl->setLexicalDeclContext(LexicalDC);
3997 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003998 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003999 } else {
4000 // Check that we have the same kind of property implementation (@synthesize
4001 // vs. @dynamic).
4002 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4003 Importer.ToDiag(ToImpl->getLocation(),
4004 diag::err_odr_objc_property_impl_kind_inconsistent)
4005 << Property->getDeclName()
4006 << (ToImpl->getPropertyImplementation()
4007 == ObjCPropertyImplDecl::Dynamic);
4008 Importer.FromDiag(D->getLocation(),
4009 diag::note_odr_objc_property_impl_kind)
4010 << D->getPropertyDecl()->getDeclName()
4011 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004012 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004013 }
4014
4015 // For @synthesize, check that we have the same
4016 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4017 Ivar != ToImpl->getPropertyIvarDecl()) {
4018 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4019 diag::err_odr_objc_synthesize_ivar_inconsistent)
4020 << Property->getDeclName()
4021 << ToImpl->getPropertyIvarDecl()->getDeclName()
4022 << Ivar->getDeclName();
4023 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4024 diag::note_odr_objc_synthesize_ivar_here)
4025 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004026 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004027 }
4028
4029 // Merge the existing implementation with the new implementation.
4030 Importer.Imported(D, ToImpl);
4031 }
4032
4033 return ToImpl;
4034}
4035
Douglas Gregora082a492010-11-30 19:14:50 +00004036Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4037 // For template arguments, we adopt the translation unit as our declaration
4038 // context. This context will be fixed when the actual template declaration
4039 // is created.
4040
4041 // FIXME: Import default argument.
4042 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4043 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004044 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004045 Importer.Import(D->getLocation()),
4046 D->getDepth(),
4047 D->getIndex(),
4048 Importer.Import(D->getIdentifier()),
4049 D->wasDeclaredWithTypename(),
4050 D->isParameterPack());
4051}
4052
4053Decl *
4054ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4055 // Import the name of this declaration.
4056 DeclarationName Name = Importer.Import(D->getDeclName());
4057 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004058 return nullptr;
4059
Douglas Gregora082a492010-11-30 19:14:50 +00004060 // Import the location of this declaration.
4061 SourceLocation Loc = Importer.Import(D->getLocation());
4062
4063 // Import the type of this declaration.
4064 QualType T = Importer.Import(D->getType());
4065 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004066 return nullptr;
4067
Douglas Gregora082a492010-11-30 19:14:50 +00004068 // Import type-source information.
4069 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4070 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004071 return nullptr;
4072
Douglas Gregora082a492010-11-30 19:14:50 +00004073 // FIXME: Import default argument.
4074
4075 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4076 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004077 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004078 Loc, D->getDepth(), D->getPosition(),
4079 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004080 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004081}
4082
4083Decl *
4084ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4085 // Import the name of this declaration.
4086 DeclarationName Name = Importer.Import(D->getDeclName());
4087 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004088 return nullptr;
4089
Douglas Gregora082a492010-11-30 19:14:50 +00004090 // Import the location of this declaration.
4091 SourceLocation Loc = Importer.Import(D->getLocation());
4092
4093 // Import template parameters.
4094 TemplateParameterList *TemplateParams
4095 = ImportTemplateParameterList(D->getTemplateParameters());
4096 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004097 return nullptr;
4098
Douglas Gregora082a492010-11-30 19:14:50 +00004099 // FIXME: Import default argument.
4100
4101 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4102 Importer.getToContext().getTranslationUnitDecl(),
4103 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004104 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004105 Name.getAsIdentifierInfo(),
4106 TemplateParams);
4107}
4108
4109Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4110 // If this record has a definition in the translation unit we're coming from,
4111 // but this particular declaration is not that definition, import the
4112 // definition and map to that.
4113 CXXRecordDecl *Definition
4114 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4115 if (Definition && Definition != D->getTemplatedDecl()) {
4116 Decl *ImportedDef
4117 = Importer.Import(Definition->getDescribedClassTemplate());
4118 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004119 return nullptr;
4120
Douglas Gregora082a492010-11-30 19:14:50 +00004121 return Importer.Imported(D, ImportedDef);
4122 }
4123
4124 // Import the major distinguishing characteristics of this class template.
4125 DeclContext *DC, *LexicalDC;
4126 DeclarationName Name;
4127 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004128 NamedDecl *ToD;
4129 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004130 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004131 if (ToD)
4132 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004133
Douglas Gregora082a492010-11-30 19:14:50 +00004134 // We may already have a template of the same name; try to find and match it.
4135 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004136 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004137 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004138 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004139 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4140 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004141 continue;
4142
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004143 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004144 if (ClassTemplateDecl *FoundTemplate
4145 = dyn_cast<ClassTemplateDecl>(Found)) {
4146 if (IsStructuralMatch(D, FoundTemplate)) {
4147 // The class templates structurally match; call it the same template.
4148 // FIXME: We may be filling in a forward declaration here. Handle
4149 // this case!
4150 Importer.Imported(D->getTemplatedDecl(),
4151 FoundTemplate->getTemplatedDecl());
4152 return Importer.Imported(D, FoundTemplate);
4153 }
4154 }
4155
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004156 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004157 }
4158
4159 if (!ConflictingDecls.empty()) {
4160 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4161 ConflictingDecls.data(),
4162 ConflictingDecls.size());
4163 }
4164
4165 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004166 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004167 }
4168
4169 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4170
4171 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004172 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4173 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004174 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4175 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004176 DC, StartLoc, IdLoc,
4177 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004178 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004179 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004180 D2Templated->setLexicalDeclContext(LexicalDC);
4181
4182 // Create the class template declaration itself.
4183 TemplateParameterList *TemplateParams
4184 = ImportTemplateParameterList(D->getTemplateParameters());
4185 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004186 return nullptr;
4187
Douglas Gregora082a492010-11-30 19:14:50 +00004188 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4189 Loc, Name, TemplateParams,
4190 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004191 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004192 D2Templated->setDescribedClassTemplate(D2);
4193
4194 D2->setAccess(D->getAccess());
4195 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004196 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004197
4198 // Note the relationship between the class templates.
4199 Importer.Imported(D, D2);
4200 Importer.Imported(DTemplated, D2Templated);
4201
John McCallf937c022011-10-07 06:10:15 +00004202 if (DTemplated->isCompleteDefinition() &&
4203 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004204 // FIXME: Import definition!
4205 }
4206
4207 return D2;
4208}
4209
Douglas Gregore2e50d332010-12-01 01:36:18 +00004210Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4211 ClassTemplateSpecializationDecl *D) {
4212 // If this record has a definition in the translation unit we're coming from,
4213 // but this particular declaration is not that definition, import the
4214 // definition and map to that.
4215 TagDecl *Definition = D->getDefinition();
4216 if (Definition && Definition != D) {
4217 Decl *ImportedDef = Importer.Import(Definition);
4218 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004219 return nullptr;
4220
Douglas Gregore2e50d332010-12-01 01:36:18 +00004221 return Importer.Imported(D, ImportedDef);
4222 }
4223
4224 ClassTemplateDecl *ClassTemplate
4225 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4226 D->getSpecializedTemplate()));
4227 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004228 return nullptr;
4229
Douglas Gregore2e50d332010-12-01 01:36:18 +00004230 // Import the context of this declaration.
4231 DeclContext *DC = ClassTemplate->getDeclContext();
4232 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004233 return nullptr;
4234
Douglas Gregore2e50d332010-12-01 01:36:18 +00004235 DeclContext *LexicalDC = DC;
4236 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4237 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4238 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004239 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004240 }
4241
4242 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004243 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4244 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004245
4246 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004247 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004248 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4249 D->getTemplateArgs().size(),
4250 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004251 return nullptr;
4252
Douglas Gregore2e50d332010-12-01 01:36:18 +00004253 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004254 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004255 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004256 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004257 if (D2) {
4258 // We already have a class template specialization with these template
4259 // arguments.
4260
4261 // FIXME: Check for specialization vs. instantiation errors.
4262
4263 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004264 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004265 // The record types structurally match, or the "from" translation
4266 // unit only had a forward declaration anyway; call it the same
4267 // function.
4268 return Importer.Imported(D, FoundDef);
4269 }
4270 }
4271 } else {
4272 // Create a new specialization.
4273 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4274 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004275 StartLoc, IdLoc,
4276 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004277 TemplateArgs.data(),
4278 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004279 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004280 D2->setSpecializationKind(D->getSpecializationKind());
4281
4282 // Add this specialization to the class template.
4283 ClassTemplate->AddSpecialization(D2, InsertPos);
4284
4285 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004286 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004287
4288 // Add the specialization to this context.
4289 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004290 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004291 }
4292 Importer.Imported(D, D2);
4293
John McCallf937c022011-10-07 06:10:15 +00004294 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004295 return nullptr;
4296
Douglas Gregore2e50d332010-12-01 01:36:18 +00004297 return D2;
4298}
4299
Larisse Voufo39a1e502013-08-06 01:03:05 +00004300Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4301 // If this variable has a definition in the translation unit we're coming
4302 // from,
4303 // but this particular declaration is not that definition, import the
4304 // definition and map to that.
4305 VarDecl *Definition =
4306 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4307 if (Definition && Definition != D->getTemplatedDecl()) {
4308 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4309 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004310 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004311
4312 return Importer.Imported(D, ImportedDef);
4313 }
4314
4315 // Import the major distinguishing characteristics of this variable template.
4316 DeclContext *DC, *LexicalDC;
4317 DeclarationName Name;
4318 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004319 NamedDecl *ToD;
4320 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004321 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004322 if (ToD)
4323 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004324
4325 // We may already have a template of the same name; try to find and match it.
4326 assert(!DC->isFunctionOrMethod() &&
4327 "Variable templates cannot be declared at function scope");
4328 SmallVector<NamedDecl *, 4> ConflictingDecls;
4329 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004330 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004331 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4332 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4333 continue;
4334
4335 Decl *Found = FoundDecls[I];
4336 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4337 if (IsStructuralMatch(D, FoundTemplate)) {
4338 // The variable templates structurally match; call it the same template.
4339 Importer.Imported(D->getTemplatedDecl(),
4340 FoundTemplate->getTemplatedDecl());
4341 return Importer.Imported(D, FoundTemplate);
4342 }
4343 }
4344
4345 ConflictingDecls.push_back(FoundDecls[I]);
4346 }
4347
4348 if (!ConflictingDecls.empty()) {
4349 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4350 ConflictingDecls.data(),
4351 ConflictingDecls.size());
4352 }
4353
4354 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004355 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004356
4357 VarDecl *DTemplated = D->getTemplatedDecl();
4358
4359 // Import the type.
4360 QualType T = Importer.Import(DTemplated->getType());
4361 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004362 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004363
4364 // Create the declaration that is being templated.
4365 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4366 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4367 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4368 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4369 IdLoc, Name.getAsIdentifierInfo(), T,
4370 TInfo, DTemplated->getStorageClass());
4371 D2Templated->setAccess(DTemplated->getAccess());
4372 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4373 D2Templated->setLexicalDeclContext(LexicalDC);
4374
4375 // Importer.Imported(DTemplated, D2Templated);
4376 // LexicalDC->addDeclInternal(D2Templated);
4377
4378 // Merge the initializer.
4379 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004380 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004381
4382 // Create the variable template declaration itself.
4383 TemplateParameterList *TemplateParams =
4384 ImportTemplateParameterList(D->getTemplateParameters());
4385 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004386 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004387
4388 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004389 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004390 D2Templated->setDescribedVarTemplate(D2);
4391
4392 D2->setAccess(D->getAccess());
4393 D2->setLexicalDeclContext(LexicalDC);
4394 LexicalDC->addDeclInternal(D2);
4395
4396 // Note the relationship between the variable templates.
4397 Importer.Imported(D, D2);
4398 Importer.Imported(DTemplated, D2Templated);
4399
4400 if (DTemplated->isThisDeclarationADefinition() &&
4401 !D2Templated->isThisDeclarationADefinition()) {
4402 // FIXME: Import definition!
4403 }
4404
4405 return D2;
4406}
4407
4408Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4409 VarTemplateSpecializationDecl *D) {
4410 // If this record has a definition in the translation unit we're coming from,
4411 // but this particular declaration is not that definition, import the
4412 // definition and map to that.
4413 VarDecl *Definition = D->getDefinition();
4414 if (Definition && Definition != D) {
4415 Decl *ImportedDef = Importer.Import(Definition);
4416 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004417 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004418
4419 return Importer.Imported(D, ImportedDef);
4420 }
4421
4422 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4423 Importer.Import(D->getSpecializedTemplate()));
4424 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004425 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004426
4427 // Import the context of this declaration.
4428 DeclContext *DC = VarTemplate->getDeclContext();
4429 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004430 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004431
4432 DeclContext *LexicalDC = DC;
4433 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4434 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4435 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004436 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004437 }
4438
4439 // Import the location of this declaration.
4440 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4441 SourceLocation IdLoc = Importer.Import(D->getLocation());
4442
4443 // Import template arguments.
4444 SmallVector<TemplateArgument, 2> TemplateArgs;
4445 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4446 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004447 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004448
4449 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004450 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004451 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004452 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004453 if (D2) {
4454 // We already have a variable template specialization with these template
4455 // arguments.
4456
4457 // FIXME: Check for specialization vs. instantiation errors.
4458
4459 if (VarDecl *FoundDef = D2->getDefinition()) {
4460 if (!D->isThisDeclarationADefinition() ||
4461 IsStructuralMatch(D, FoundDef)) {
4462 // The record types structurally match, or the "from" translation
4463 // unit only had a forward declaration anyway; call it the same
4464 // variable.
4465 return Importer.Imported(D, FoundDef);
4466 }
4467 }
4468 } else {
4469
4470 // Import the type.
4471 QualType T = Importer.Import(D->getType());
4472 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004473 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004474 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4475
4476 // Create a new specialization.
4477 D2 = VarTemplateSpecializationDecl::Create(
4478 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4479 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4480 D2->setSpecializationKind(D->getSpecializationKind());
4481 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4482
4483 // Add this specialization to the class template.
4484 VarTemplate->AddSpecialization(D2, InsertPos);
4485
4486 // Import the qualifier, if any.
4487 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4488
4489 // Add the specialization to this context.
4490 D2->setLexicalDeclContext(LexicalDC);
4491 LexicalDC->addDeclInternal(D2);
4492 }
4493 Importer.Imported(D, D2);
4494
4495 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004496 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004497
4498 return D2;
4499}
4500
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004501//----------------------------------------------------------------------------
4502// Import Statements
4503//----------------------------------------------------------------------------
4504
Sean Callanan59721b32015-04-28 18:41:46 +00004505DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4506 if (DG.isNull())
4507 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4508 size_t NumDecls = DG.end() - DG.begin();
4509 SmallVector<Decl *, 1> ToDecls(NumDecls);
4510 auto &_Importer = this->Importer;
4511 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4512 [&_Importer](Decl *D) -> Decl * {
4513 return _Importer.Import(D);
4514 });
4515 return DeclGroupRef::Create(Importer.getToContext(),
4516 ToDecls.begin(),
4517 NumDecls);
4518}
4519
4520 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4521 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4522 << S->getStmtClassName();
4523 return nullptr;
4524 }
4525
4526Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4527 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4528 for (Decl *ToD : ToDG) {
4529 if (!ToD)
4530 return nullptr;
4531 }
4532 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4533 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4534 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4535}
4536
4537Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4538 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4539 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4540 S->hasLeadingEmptyMacro());
4541}
4542
4543Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4544 SmallVector<Stmt *, 4> ToStmts(S->size());
4545 auto &_Importer = this->Importer;
4546 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4547 [&_Importer](Stmt *CS) -> Stmt * {
4548 return _Importer.Import(CS);
4549 });
4550 for (Stmt *ToS : ToStmts) {
4551 if (!ToS)
4552 return nullptr;
4553 }
4554 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4555 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4556 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4557 ToStmts,
4558 ToLBraceLoc, ToRBraceLoc);
4559}
4560
4561Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4562 Expr *ToLHS = Importer.Import(S->getLHS());
4563 if (!ToLHS)
4564 return nullptr;
4565 Expr *ToRHS = Importer.Import(S->getRHS());
4566 if (!ToRHS && S->getRHS())
4567 return nullptr;
4568 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4569 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4570 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4571 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4572 ToCaseLoc, ToEllipsisLoc,
4573 ToColonLoc);
4574}
4575
4576Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4577 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4578 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4579 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4580 if (!ToSubStmt && S->getSubStmt())
4581 return nullptr;
4582 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4583 ToSubStmt);
4584}
4585
4586Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4587 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4588 LabelDecl *ToLabelDecl =
4589 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4590 if (!ToLabelDecl && S->getDecl())
4591 return nullptr;
4592 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4593 if (!ToSubStmt && S->getSubStmt())
4594 return nullptr;
4595 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4596 ToSubStmt);
4597}
4598
4599Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4600 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4601 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4602 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4603 ASTContext &_ToContext = Importer.getToContext();
4604 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4605 [&_ToContext](const Attr *A) -> const Attr * {
4606 return A->clone(_ToContext);
4607 });
4608 for (const Attr *ToA : ToAttrs) {
4609 if (!ToA)
4610 return nullptr;
4611 }
4612 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4613 if (!ToSubStmt && S->getSubStmt())
4614 return nullptr;
4615 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4616 ToAttrs, ToSubStmt);
4617}
4618
4619Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4620 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4621 VarDecl *ToConditionVariable = nullptr;
4622 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4623 ToConditionVariable =
4624 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4625 if (!ToConditionVariable)
4626 return nullptr;
4627 }
4628 Expr *ToCondition = Importer.Import(S->getCond());
4629 if (!ToCondition && S->getCond())
4630 return nullptr;
4631 Stmt *ToThenStmt = Importer.Import(S->getThen());
4632 if (!ToThenStmt && S->getThen())
4633 return nullptr;
4634 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4635 Stmt *ToElseStmt = Importer.Import(S->getElse());
4636 if (!ToElseStmt && S->getElse())
4637 return nullptr;
4638 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4639 ToIfLoc, ToConditionVariable,
4640 ToCondition, ToThenStmt,
4641 ToElseLoc, ToElseStmt);
4642}
4643
4644Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4645 VarDecl *ToConditionVariable = nullptr;
4646 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4647 ToConditionVariable =
4648 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4649 if (!ToConditionVariable)
4650 return nullptr;
4651 }
4652 Expr *ToCondition = Importer.Import(S->getCond());
4653 if (!ToCondition && S->getCond())
4654 return nullptr;
4655 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4656 Importer.getToContext(), ToConditionVariable,
4657 ToCondition);
4658 Stmt *ToBody = Importer.Import(S->getBody());
4659 if (!ToBody && S->getBody())
4660 return nullptr;
4661 ToStmt->setBody(ToBody);
4662 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4663 // Now we have to re-chain the cases.
4664 SwitchCase *LastChainedSwitchCase = nullptr;
4665 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4666 SC = SC->getNextSwitchCase()) {
4667 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4668 if (!ToSC)
4669 return nullptr;
4670 if (LastChainedSwitchCase)
4671 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4672 else
4673 ToStmt->setSwitchCaseList(ToSC);
4674 LastChainedSwitchCase = ToSC;
4675 }
4676 return ToStmt;
4677}
4678
4679Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4680 VarDecl *ToConditionVariable = nullptr;
4681 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4682 ToConditionVariable =
4683 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4684 if (!ToConditionVariable)
4685 return nullptr;
4686 }
4687 Expr *ToCondition = Importer.Import(S->getCond());
4688 if (!ToCondition && S->getCond())
4689 return nullptr;
4690 Stmt *ToBody = Importer.Import(S->getBody());
4691 if (!ToBody && S->getBody())
4692 return nullptr;
4693 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4694 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4695 ToConditionVariable,
4696 ToCondition, ToBody,
4697 ToWhileLoc);
4698}
4699
4700Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4701 Stmt *ToBody = Importer.Import(S->getBody());
4702 if (!ToBody && S->getBody())
4703 return nullptr;
4704 Expr *ToCondition = Importer.Import(S->getCond());
4705 if (!ToCondition && S->getCond())
4706 return nullptr;
4707 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4708 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4709 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4710 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4711 ToDoLoc, ToWhileLoc,
4712 ToRParenLoc);
4713}
4714
4715Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4716 Stmt *ToInit = Importer.Import(S->getInit());
4717 if (!ToInit && S->getInit())
4718 return nullptr;
4719 Expr *ToCondition = Importer.Import(S->getCond());
4720 if (!ToCondition && S->getCond())
4721 return nullptr;
4722 VarDecl *ToConditionVariable = nullptr;
4723 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4724 ToConditionVariable =
4725 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4726 if (!ToConditionVariable)
4727 return nullptr;
4728 }
4729 Expr *ToInc = Importer.Import(S->getInc());
4730 if (!ToInc && S->getInc())
4731 return nullptr;
4732 Stmt *ToBody = Importer.Import(S->getBody());
4733 if (!ToBody && S->getBody())
4734 return nullptr;
4735 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4736 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4737 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4738 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4739 ToInit, ToCondition,
4740 ToConditionVariable,
4741 ToInc, ToBody,
4742 ToForLoc, ToLParenLoc,
4743 ToRParenLoc);
4744}
4745
4746Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4747 LabelDecl *ToLabel = nullptr;
4748 if (LabelDecl *FromLabel = S->getLabel()) {
4749 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4750 if (!ToLabel)
4751 return nullptr;
4752 }
4753 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4754 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4755 return new (Importer.getToContext()) GotoStmt(ToLabel,
4756 ToGotoLoc, ToLabelLoc);
4757}
4758
4759Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4760 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4761 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4762 Expr *ToTarget = Importer.Import(S->getTarget());
4763 if (!ToTarget && S->getTarget())
4764 return nullptr;
4765 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4766 ToTarget);
4767}
4768
4769Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4770 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4771 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4772}
4773
4774Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4775 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4776 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4777}
4778
4779Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4780 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4781 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4782 if (!ToRetExpr && S->getRetValue())
4783 return nullptr;
4784 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4785 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4786 if (!ToNRVOCandidate && NRVOCandidate)
4787 return nullptr;
4788 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4789 ToNRVOCandidate);
4790}
4791
4792Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4793 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4794 VarDecl *ToExceptionDecl = nullptr;
4795 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4796 ToExceptionDecl =
4797 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4798 if (!ToExceptionDecl)
4799 return nullptr;
4800 }
4801 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4802 if (!ToHandlerBlock && S->getHandlerBlock())
4803 return nullptr;
4804 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4805 ToExceptionDecl,
4806 ToHandlerBlock);
4807}
4808
4809Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4810 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4811 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4812 if (!ToTryBlock && S->getTryBlock())
4813 return nullptr;
4814 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4815 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4816 CXXCatchStmt *FromHandler = S->getHandler(HI);
4817 if (Stmt *ToHandler = Importer.Import(FromHandler))
4818 ToHandlers[HI] = ToHandler;
4819 else
4820 return nullptr;
4821 }
4822 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4823 ToHandlers);
4824}
4825
4826Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4827 DeclStmt *ToRange =
4828 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4829 if (!ToRange && S->getRangeStmt())
4830 return nullptr;
4831 DeclStmt *ToBeginEnd =
4832 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4833 if (!ToBeginEnd && S->getBeginEndStmt())
4834 return nullptr;
4835 Expr *ToCond = Importer.Import(S->getCond());
4836 if (!ToCond && S->getCond())
4837 return nullptr;
4838 Expr *ToInc = Importer.Import(S->getInc());
4839 if (!ToInc && S->getInc())
4840 return nullptr;
4841 DeclStmt *ToLoopVar =
4842 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4843 if (!ToLoopVar && S->getLoopVarStmt())
4844 return nullptr;
4845 Stmt *ToBody = Importer.Import(S->getBody());
4846 if (!ToBody && S->getBody())
4847 return nullptr;
4848 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4849 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4850 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4851 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4852 ToCond, ToInc,
4853 ToLoopVar, ToBody,
4854 ToForLoc, ToColonLoc,
4855 ToRParenLoc);
4856}
4857
4858Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4859 Stmt *ToElem = Importer.Import(S->getElement());
4860 if (!ToElem && S->getElement())
4861 return nullptr;
4862 Expr *ToCollect = Importer.Import(S->getCollection());
4863 if (!ToCollect && S->getCollection())
4864 return nullptr;
4865 Stmt *ToBody = Importer.Import(S->getBody());
4866 if (!ToBody && S->getBody())
4867 return nullptr;
4868 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4869 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4870 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4871 ToCollect,
4872 ToBody, ToForLoc,
4873 ToRParenLoc);
4874}
4875
4876Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4877 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4878 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4879 VarDecl *ToExceptionDecl = nullptr;
4880 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4881 ToExceptionDecl =
4882 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4883 if (!ToExceptionDecl)
4884 return nullptr;
4885 }
4886 Stmt *ToBody = Importer.Import(S->getCatchBody());
4887 if (!ToBody && S->getCatchBody())
4888 return nullptr;
4889 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4890 ToRParenLoc,
4891 ToExceptionDecl,
4892 ToBody);
4893}
4894
4895Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
4896 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
4897 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
4898 if (!ToAtFinallyStmt && S->getFinallyBody())
4899 return nullptr;
4900 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
4901 ToAtFinallyStmt);
4902}
4903
4904Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
4905 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
4906 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
4907 if (!ToAtTryStmt && S->getTryBody())
4908 return nullptr;
4909 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
4910 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
4911 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
4912 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
4913 ToCatchStmts[CI] = ToCatchStmt;
4914 else
4915 return nullptr;
4916 }
4917 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
4918 if (!ToAtFinallyStmt && S->getFinallyStmt())
4919 return nullptr;
4920 return ObjCAtTryStmt::Create(Importer.getToContext(),
4921 ToAtTryLoc, ToAtTryStmt,
4922 ToCatchStmts.begin(), ToCatchStmts.size(),
4923 ToAtFinallyStmt);
4924}
4925
4926Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
4927 (ObjCAtSynchronizedStmt *S) {
4928 SourceLocation ToAtSynchronizedLoc =
4929 Importer.Import(S->getAtSynchronizedLoc());
4930 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
4931 if (!ToSynchExpr && S->getSynchExpr())
4932 return nullptr;
4933 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
4934 if (!ToSynchBody && S->getSynchBody())
4935 return nullptr;
4936 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
4937 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
4938}
4939
4940Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
4941 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
4942 Expr *ToThrow = Importer.Import(S->getThrowExpr());
4943 if (!ToThrow && S->getThrowExpr())
4944 return nullptr;
4945 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
4946}
4947
4948Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
4949 (ObjCAutoreleasePoolStmt *S) {
4950 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
4951 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4952 if (!ToSubStmt && S->getSubStmt())
4953 return nullptr;
4954 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
4955 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004956}
4957
4958//----------------------------------------------------------------------------
4959// Import Expressions
4960//----------------------------------------------------------------------------
4961Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4962 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4963 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004964 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004965}
4966
Douglas Gregor52f820e2010-02-19 01:17:02 +00004967Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004968 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4969 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004970 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004971
Craig Topper36250ad2014-05-12 05:36:57 +00004972 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004973 if (E->getDecl() != E->getFoundDecl()) {
4974 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4975 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004976 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004977 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004978
4979 QualType T = Importer.Import(E->getType());
4980 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004981 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004982
4983 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4984 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004985 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004986 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004987 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004988 Importer.Import(E->getLocation()),
4989 T, E->getValueKind(),
4990 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00004991 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004992 if (E->hadMultipleCandidates())
4993 DRE->setHadMultipleCandidates(true);
4994 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004995}
4996
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004997Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4998 QualType T = Importer.Import(E->getType());
4999 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005000 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005001
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005002 return IntegerLiteral::Create(Importer.getToContext(),
5003 E->getValue(), T,
5004 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005005}
5006
Douglas Gregor623421d2010-02-18 02:21:22 +00005007Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5008 QualType T = Importer.Import(E->getType());
5009 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005010 return nullptr;
5011
Douglas Gregorfb65e592011-07-27 05:40:30 +00005012 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5013 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005014 Importer.Import(E->getLocation()));
5015}
5016
Douglas Gregorc74247e2010-02-19 01:07:06 +00005017Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5018 Expr *SubExpr = Importer.Import(E->getSubExpr());
5019 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005020 return nullptr;
5021
Douglas Gregorc74247e2010-02-19 01:07:06 +00005022 return new (Importer.getToContext())
5023 ParenExpr(Importer.Import(E->getLParen()),
5024 Importer.Import(E->getRParen()),
5025 SubExpr);
5026}
5027
5028Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5029 QualType T = Importer.Import(E->getType());
5030 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005031 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005032
5033 Expr *SubExpr = Importer.Import(E->getSubExpr());
5034 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005035 return nullptr;
5036
Douglas Gregorc74247e2010-02-19 01:07:06 +00005037 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005038 T, E->getValueKind(),
5039 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005040 Importer.Import(E->getOperatorLoc()));
5041}
5042
Peter Collingbournee190dee2011-03-11 19:24:49 +00005043Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5044 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005045 QualType ResultType = Importer.Import(E->getType());
5046
5047 if (E->isArgumentType()) {
5048 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5049 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005050 return nullptr;
5051
Peter Collingbournee190dee2011-03-11 19:24:49 +00005052 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5053 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005054 Importer.Import(E->getOperatorLoc()),
5055 Importer.Import(E->getRParenLoc()));
5056 }
5057
5058 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5059 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005060 return nullptr;
5061
Peter Collingbournee190dee2011-03-11 19:24:49 +00005062 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5063 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005064 Importer.Import(E->getOperatorLoc()),
5065 Importer.Import(E->getRParenLoc()));
5066}
5067
Douglas Gregorc74247e2010-02-19 01:07:06 +00005068Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5069 QualType T = Importer.Import(E->getType());
5070 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005071 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005072
5073 Expr *LHS = Importer.Import(E->getLHS());
5074 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005075 return nullptr;
5076
Douglas Gregorc74247e2010-02-19 01:07:06 +00005077 Expr *RHS = Importer.Import(E->getRHS());
5078 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005079 return nullptr;
5080
Douglas Gregorc74247e2010-02-19 01:07:06 +00005081 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005082 T, E->getValueKind(),
5083 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005084 Importer.Import(E->getOperatorLoc()),
5085 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005086}
5087
5088Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5089 QualType T = Importer.Import(E->getType());
5090 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005091 return nullptr;
5092
Douglas Gregorc74247e2010-02-19 01:07:06 +00005093 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5094 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005095 return nullptr;
5096
Douglas Gregorc74247e2010-02-19 01:07:06 +00005097 QualType CompResultType = Importer.Import(E->getComputationResultType());
5098 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005099 return nullptr;
5100
Douglas Gregorc74247e2010-02-19 01:07:06 +00005101 Expr *LHS = Importer.Import(E->getLHS());
5102 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005103 return nullptr;
5104
Douglas Gregorc74247e2010-02-19 01:07:06 +00005105 Expr *RHS = Importer.Import(E->getRHS());
5106 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005107 return nullptr;
5108
Douglas Gregorc74247e2010-02-19 01:07:06 +00005109 return new (Importer.getToContext())
5110 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005111 T, E->getValueKind(),
5112 E->getObjectKind(),
5113 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005114 Importer.Import(E->getOperatorLoc()),
5115 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005116}
5117
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005118static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005119 if (E->path_empty()) return false;
5120
5121 // TODO: import cast paths
5122 return true;
5123}
5124
Douglas Gregor98c10182010-02-12 22:17:39 +00005125Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5126 QualType T = Importer.Import(E->getType());
5127 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005128 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005129
5130 Expr *SubExpr = Importer.Import(E->getSubExpr());
5131 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005132 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005133
5134 CXXCastPath BasePath;
5135 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005136 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005137
5138 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005139 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005140}
5141
Douglas Gregor5481d322010-02-19 01:32:14 +00005142Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5143 QualType T = Importer.Import(E->getType());
5144 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005145 return nullptr;
5146
Douglas Gregor5481d322010-02-19 01:32:14 +00005147 Expr *SubExpr = Importer.Import(E->getSubExpr());
5148 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005149 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005150
5151 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5152 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005153 return nullptr;
5154
John McCallcf142162010-08-07 06:22:56 +00005155 CXXCastPath BasePath;
5156 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005157 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005158
John McCall7decc9e2010-11-18 06:31:45 +00005159 return CStyleCastExpr::Create(Importer.getToContext(), T,
5160 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005161 SubExpr, &BasePath, TInfo,
5162 Importer.Import(E->getLParenLoc()),
5163 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005164}
5165
Sean Callanan59721b32015-04-28 18:41:46 +00005166Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5167 QualType T = Importer.Import(E->getType());
5168 if (T.isNull())
5169 return nullptr;
5170
5171 CXXConstructorDecl *ToCCD =
5172 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5173 if (!ToCCD && E->getConstructor())
5174 return nullptr;
5175
5176 size_t NumArgs = E->getNumArgs();
5177 SmallVector<Expr *, 1> ToArgs(NumArgs);
5178 ASTImporter &_Importer = Importer;
5179 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5180 [&_Importer](Expr *AE) -> Expr * {
5181 return _Importer.Import(AE);
5182 });
5183 for (Expr *ToA : ToArgs) {
5184 if (!ToA)
5185 return nullptr;
5186 }
5187
5188 return CXXConstructExpr::Create(Importer.getToContext(), T,
5189 Importer.Import(E->getLocation()),
5190 ToCCD, E->isElidable(),
5191 ToArgs, E->hadMultipleCandidates(),
5192 E->isListInitialization(),
5193 E->isStdInitListInitialization(),
5194 E->requiresZeroInitialization(),
5195 E->getConstructionKind(),
5196 Importer.Import(E->getParenOrBraceRange()));
5197}
5198
5199Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5200 QualType T = Importer.Import(E->getType());
5201 if (T.isNull())
5202 return nullptr;
5203
5204 Expr *ToBase = Importer.Import(E->getBase());
5205 if (!ToBase && E->getBase())
5206 return nullptr;
5207
5208 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5209 if (!ToMember && E->getMemberDecl())
5210 return nullptr;
5211
5212 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5213 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5214 E->getFoundDecl().getAccess());
5215
5216 DeclarationNameInfo ToMemberNameInfo(
5217 Importer.Import(E->getMemberNameInfo().getName()),
5218 Importer.Import(E->getMemberNameInfo().getLoc()));
5219
5220 if (E->hasExplicitTemplateArgs()) {
5221 return nullptr; // FIXME: handle template arguments
5222 }
5223
5224 return MemberExpr::Create(Importer.getToContext(), ToBase,
5225 E->isArrow(),
5226 Importer.Import(E->getOperatorLoc()),
5227 Importer.Import(E->getQualifierLoc()),
5228 Importer.Import(E->getTemplateKeywordLoc()),
5229 ToMember, ToFoundDecl, ToMemberNameInfo,
5230 nullptr, T, E->getValueKind(),
5231 E->getObjectKind());
5232}
5233
5234Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5235 QualType T = Importer.Import(E->getType());
5236 if (T.isNull())
5237 return nullptr;
5238
5239 Expr *ToCallee = Importer.Import(E->getCallee());
5240 if (!ToCallee && E->getCallee())
5241 return nullptr;
5242
5243 unsigned NumArgs = E->getNumArgs();
5244
5245 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5246
5247 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5248 Expr *FromArg = E->getArg(ai);
5249 Expr *ToArg = Importer.Import(FromArg);
5250 if (!ToArg)
5251 return nullptr;
5252 ToArgs[ai] = ToArg;
5253 }
5254
5255 Expr **ToArgs_Copied = new (Importer.getToContext())
5256 Expr*[NumArgs];
5257
5258 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5259 ToArgs_Copied[ai] = ToArgs[ai];
5260
5261 return new (Importer.getToContext())
5262 CallExpr(Importer.getToContext(), ToCallee,
5263 ArrayRef<Expr*>(ToArgs_Copied, NumArgs), T, E->getValueKind(),
5264 Importer.Import(E->getRParenLoc()));
5265}
5266
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005267ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005268 ASTContext &FromContext, FileManager &FromFileManager,
5269 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005270 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005271 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005272 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005273{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005274 ImportedDecls[FromContext.getTranslationUnitDecl()]
5275 = ToContext.getTranslationUnitDecl();
5276}
5277
5278ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005279
5280QualType ASTImporter::Import(QualType FromT) {
5281 if (FromT.isNull())
5282 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005283
5284 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005285
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005286 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005287 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5288 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005289 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005290 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005291
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005292 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005293 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005294 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005295 if (ToT.isNull())
5296 return ToT;
5297
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005298 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005299 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005300
John McCall424cec92011-01-19 06:33:43 +00005301 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005302}
5303
Douglas Gregor62d311f2010-02-09 19:21:46 +00005304TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005305 if (!FromTSI)
5306 return FromTSI;
5307
5308 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005309 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005310 QualType T = Import(FromTSI->getType());
5311 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005312 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005313
5314 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00005315 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00005316}
5317
Sean Callanan59721b32015-04-28 18:41:46 +00005318Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5319 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5320 if (Pos != ImportedDecls.end()) {
5321 Decl *ToD = Pos->second;
5322 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5323 return ToD;
5324 } else {
5325 return nullptr;
5326 }
5327}
5328
Douglas Gregor62d311f2010-02-09 19:21:46 +00005329Decl *ASTImporter::Import(Decl *FromD) {
5330 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005331 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005332
Douglas Gregord451ea92011-07-29 23:31:30 +00005333 ASTNodeImporter Importer(*this);
5334
Douglas Gregor62d311f2010-02-09 19:21:46 +00005335 // Check whether we've already imported this declaration.
5336 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005337 if (Pos != ImportedDecls.end()) {
5338 Decl *ToD = Pos->second;
5339 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5340 return ToD;
5341 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005342
5343 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005344 Decl *ToD = Importer.Visit(FromD);
5345 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005346 return nullptr;
5347
Douglas Gregor62d311f2010-02-09 19:21:46 +00005348 // Record the imported declaration.
5349 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005350
5351 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5352 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005353 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005354 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005355 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005356 // When we've finished transforming a typedef, see whether it was the
5357 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005358 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005359 FromTag = AnonTagsWithPendingTypedefs.begin(),
5360 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5361 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005362 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005363 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5364 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005365 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005366 AnonTagsWithPendingTypedefs.erase(FromTag);
5367 break;
5368 }
5369 }
5370 }
5371 }
5372
Douglas Gregor62d311f2010-02-09 19:21:46 +00005373 return ToD;
5374}
5375
5376DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5377 if (!FromDC)
5378 return FromDC;
5379
Douglas Gregor95d82832012-01-24 18:36:04 +00005380 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005381 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005382 return nullptr;
5383
Douglas Gregor2e15c842012-02-01 21:00:38 +00005384 // When we're using a record/enum/Objective-C class/protocol as a context, we
5385 // need it to have a definition.
5386 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005387 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005388 if (ToRecord->isCompleteDefinition()) {
5389 // Do nothing.
5390 } else if (FromRecord->isCompleteDefinition()) {
5391 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5392 ASTNodeImporter::IDK_Basic);
5393 } else {
5394 CompleteDecl(ToRecord);
5395 }
5396 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5397 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5398 if (ToEnum->isCompleteDefinition()) {
5399 // Do nothing.
5400 } else if (FromEnum->isCompleteDefinition()) {
5401 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5402 ASTNodeImporter::IDK_Basic);
5403 } else {
5404 CompleteDecl(ToEnum);
5405 }
5406 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5407 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5408 if (ToClass->getDefinition()) {
5409 // Do nothing.
5410 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5411 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5412 ASTNodeImporter::IDK_Basic);
5413 } else {
5414 CompleteDecl(ToClass);
5415 }
5416 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5417 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5418 if (ToProto->getDefinition()) {
5419 // Do nothing.
5420 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5421 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5422 ASTNodeImporter::IDK_Basic);
5423 } else {
5424 CompleteDecl(ToProto);
5425 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005426 }
5427
5428 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005429}
5430
5431Expr *ASTImporter::Import(Expr *FromE) {
5432 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005433 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005434
5435 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5436}
5437
5438Stmt *ASTImporter::Import(Stmt *FromS) {
5439 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005440 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005441
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005442 // Check whether we've already imported this declaration.
5443 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5444 if (Pos != ImportedStmts.end())
5445 return Pos->second;
5446
5447 // Import the type
5448 ASTNodeImporter Importer(*this);
5449 Stmt *ToS = Importer.Visit(FromS);
5450 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
5452
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005453 // Record the imported declaration.
5454 ImportedStmts[FromS] = ToS;
5455 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005456}
5457
5458NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5459 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005460 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005461
Douglas Gregor90ebf252011-04-27 16:48:40 +00005462 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5463
5464 switch (FromNNS->getKind()) {
5465 case NestedNameSpecifier::Identifier:
5466 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5467 return NestedNameSpecifier::Create(ToContext, prefix, II);
5468 }
Craig Topper36250ad2014-05-12 05:36:57 +00005469 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005470
5471 case NestedNameSpecifier::Namespace:
5472 if (NamespaceDecl *NS =
5473 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5474 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5475 }
Craig Topper36250ad2014-05-12 05:36:57 +00005476 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005477
5478 case NestedNameSpecifier::NamespaceAlias:
5479 if (NamespaceAliasDecl *NSAD =
5480 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5481 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5482 }
Craig Topper36250ad2014-05-12 05:36:57 +00005483 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005484
5485 case NestedNameSpecifier::Global:
5486 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5487
Nikola Smiljanic67860242014-09-26 00:28:20 +00005488 case NestedNameSpecifier::Super:
5489 if (CXXRecordDecl *RD =
5490 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5491 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5492 }
5493 return nullptr;
5494
Douglas Gregor90ebf252011-04-27 16:48:40 +00005495 case NestedNameSpecifier::TypeSpec:
5496 case NestedNameSpecifier::TypeSpecWithTemplate: {
5497 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5498 if (!T.isNull()) {
5499 bool bTemplate = FromNNS->getKind() ==
5500 NestedNameSpecifier::TypeSpecWithTemplate;
5501 return NestedNameSpecifier::Create(ToContext, prefix,
5502 bTemplate, T.getTypePtr());
5503 }
5504 }
Craig Topper36250ad2014-05-12 05:36:57 +00005505 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005506 }
5507
5508 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005509}
5510
Douglas Gregor14454802011-02-25 02:25:35 +00005511NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5512 // FIXME: Implement!
5513 return NestedNameSpecifierLoc();
5514}
5515
Douglas Gregore2e50d332010-12-01 01:36:18 +00005516TemplateName ASTImporter::Import(TemplateName From) {
5517 switch (From.getKind()) {
5518 case TemplateName::Template:
5519 if (TemplateDecl *ToTemplate
5520 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5521 return TemplateName(ToTemplate);
5522
5523 return TemplateName();
5524
5525 case TemplateName::OverloadedTemplate: {
5526 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5527 UnresolvedSet<2> ToTemplates;
5528 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5529 E = FromStorage->end();
5530 I != E; ++I) {
5531 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5532 ToTemplates.addDecl(To);
5533 else
5534 return TemplateName();
5535 }
5536 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5537 ToTemplates.end());
5538 }
5539
5540 case TemplateName::QualifiedTemplate: {
5541 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5542 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5543 if (!Qualifier)
5544 return TemplateName();
5545
5546 if (TemplateDecl *ToTemplate
5547 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5548 return ToContext.getQualifiedTemplateName(Qualifier,
5549 QTN->hasTemplateKeyword(),
5550 ToTemplate);
5551
5552 return TemplateName();
5553 }
5554
5555 case TemplateName::DependentTemplate: {
5556 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5557 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5558 if (!Qualifier)
5559 return TemplateName();
5560
5561 if (DTN->isIdentifier()) {
5562 return ToContext.getDependentTemplateName(Qualifier,
5563 Import(DTN->getIdentifier()));
5564 }
5565
5566 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5567 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005568
5569 case TemplateName::SubstTemplateTemplateParm: {
5570 SubstTemplateTemplateParmStorage *subst
5571 = From.getAsSubstTemplateTemplateParm();
5572 TemplateTemplateParmDecl *param
5573 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5574 if (!param)
5575 return TemplateName();
5576
5577 TemplateName replacement = Import(subst->getReplacement());
5578 if (replacement.isNull()) return TemplateName();
5579
5580 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5581 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005582
5583 case TemplateName::SubstTemplateTemplateParmPack: {
5584 SubstTemplateTemplateParmPackStorage *SubstPack
5585 = From.getAsSubstTemplateTemplateParmPack();
5586 TemplateTemplateParmDecl *Param
5587 = cast_or_null<TemplateTemplateParmDecl>(
5588 Import(SubstPack->getParameterPack()));
5589 if (!Param)
5590 return TemplateName();
5591
5592 ASTNodeImporter Importer(*this);
5593 TemplateArgument ArgPack
5594 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5595 if (ArgPack.isNull())
5596 return TemplateName();
5597
5598 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5599 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005600 }
5601
5602 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005603}
5604
Douglas Gregor62d311f2010-02-09 19:21:46 +00005605SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5606 if (FromLoc.isInvalid())
5607 return SourceLocation();
5608
Douglas Gregor811663e2010-02-10 00:15:17 +00005609 SourceManager &FromSM = FromContext.getSourceManager();
5610
5611 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005612 // don't have to import macro expansions.
5613 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005614 FromLoc = FromSM.getSpellingLoc(FromLoc);
5615 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5616 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005617 FileID ToFileID = Import(Decomposed.first);
5618 if (ToFileID.isInvalid())
5619 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005620 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5621 .getLocWithOffset(Decomposed.second);
5622 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005623}
5624
5625SourceRange ASTImporter::Import(SourceRange FromRange) {
5626 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5627}
5628
Douglas Gregor811663e2010-02-10 00:15:17 +00005629FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005630 llvm::DenseMap<FileID, FileID>::iterator Pos
5631 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005632 if (Pos != ImportedFileIDs.end())
5633 return Pos->second;
5634
5635 SourceManager &FromSM = FromContext.getSourceManager();
5636 SourceManager &ToSM = ToContext.getSourceManager();
5637 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005638 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005639
5640 // Include location of this file.
5641 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5642
5643 // Map the FileID for to the "to" source manager.
5644 FileID ToID;
5645 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan59721b32015-04-28 18:41:46 +00005646 if (Cache->OrigEntry &&
5647 Cache->OrigEntry->getUniqueID() != llvm::sys::fs::UniqueID()) {
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}