blob: eb9b00ad842879709469cdebd136480e929dcb5f [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor1b2949d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor88523732010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000026
Douglas Gregor27c72d82011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor089459a2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000039
40 // Importing types
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -070070 QualType VisitAttributedType(const AttributedType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
Douglas Gregorcd0d56a2012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnara25777432010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregorac32ff92012-02-01 21:00:38 +000089
Douglas Gregorcd0d56a2012-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 Gregorac32ff92012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000110 bool ImportDefinition(VarDecl *From, VarDecl *To,
111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000112 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000117 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor040afae2010-11-30 19:14:50 +0000118 TemplateParameterList *ImportTemplateParameterList(
119 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
121 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
122 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor93ed7cf2012-07-17 21:16:27 +0000124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
125 bool Complain = true);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
127 bool Complain = true);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor1cef4592012-11-14 22:29:20 +0000129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregor040afae2010-11-30 19:14:50 +0000130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000132 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000133 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000134 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000135 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000136 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000137 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000138 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000139 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000140 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000141 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-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 Gregor96a01b42010-02-11 00:48:18 +0000146 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000147 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000148 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000149 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000150 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000151 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000152 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000153 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000154 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700155 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000156 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000157 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000158 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000159 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000160 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor040afae2010-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 Gregord5dc83a2010-12-01 01:36:18 +0000165 Decl *VisitClassTemplateSpecializationDecl(
166 ClassTemplateSpecializationDecl *D);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000167 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
168 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
169
Douglas Gregor4800d952010-02-11 19:21:55 +0000170 // Importing statements
171 Stmt *VisitStmt(Stmt *S);
172
173 // Importing expressions
174 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000175 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000176 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000177 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000178 Expr *VisitParenExpr(ParenExpr *E);
179 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000180 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000181 Expr *VisitBinaryOperator(BinaryOperator *E);
182 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000183 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000184 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000185 };
186}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000187using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000188
189//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000190// Structural Equivalence
191//----------------------------------------------------------------------------
192
193namespace {
194 struct StructuralEquivalenceContext {
195 /// \brief AST contexts for which we are checking structural equivalence.
196 ASTContext &C1, &C2;
197
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000198 /// \brief The set of "tentative" equivalences between two canonical
199 /// declarations, mapping from a declaration in the first context to the
200 /// declaration in the second context that we believe to be equivalent.
201 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
202
203 /// \brief Queue of declarations in the first context whose equivalence
204 /// with a declaration in the second context still needs to be verified.
205 std::deque<Decl *> DeclsToCheck;
206
Douglas Gregorea35d112010-02-15 23:54:17 +0000207 /// \brief Declaration (from, to) pairs that are known not to be equivalent
208 /// (which we have already complained about).
209 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
210
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000211 /// \brief Whether we're being strict about the spelling of types when
212 /// unifying two types.
213 bool StrictTypeSpelling;
Douglas Gregor93ed7cf2012-07-17 21:16:27 +0000214
215 /// \brief Whether to complain about failures.
216 bool Complain;
217
Richard Smith5b9268f2012-12-20 02:22:15 +0000218 /// \brief \c true if the last diagnostic came from C2.
219 bool LastDiagFromC2;
220
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000221 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000222 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor93ed7cf2012-07-17 21:16:27 +0000223 bool StrictTypeSpelling = false,
224 bool Complain = true)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000225 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5b9268f2012-12-20 02:22:15 +0000226 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
227 LastDiagFromC2(false) {}
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000228
229 /// \brief Determine whether the two declarations are structurally
230 /// equivalent.
231 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
232
233 /// \brief Determine whether the two types are structurally equivalent.
234 bool IsStructurallyEquivalent(QualType T1, QualType T2);
235
236 private:
237 /// \brief Finish checking all of the structural equivalences.
238 ///
239 /// \returns true if an error occurred, false otherwise.
240 bool Finish();
241
242 public:
243 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000244 assert(Complain && "Not allowed to complain");
Richard Smith5b9268f2012-12-20 02:22:15 +0000245 if (LastDiagFromC2)
246 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
247 LastDiagFromC2 = false;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000248 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000249 }
250
251 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000252 assert(Complain && "Not allowed to complain");
Richard Smith5b9268f2012-12-20 02:22:15 +0000253 if (!LastDiagFromC2)
254 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
255 LastDiagFromC2 = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000256 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000257 }
258 };
259}
260
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 QualType T1, QualType T2);
263static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
264 Decl *D1, Decl *D2);
265
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000266/// \brief Determine structural equivalence of two expressions.
267static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
268 Expr *E1, Expr *E2) {
269 if (!E1 || !E2)
270 return E1 == E2;
271
272 // FIXME: Actually perform a structural comparison!
273 return true;
274}
275
276/// \brief Determine whether two identifiers are equivalent.
277static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
278 const IdentifierInfo *Name2) {
279 if (!Name1 || !Name2)
280 return Name1 == Name2;
281
282 return Name1->getName() == Name2->getName();
283}
284
285/// \brief Determine whether two nested-name-specifiers are equivalent.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 NestedNameSpecifier *NNS1,
288 NestedNameSpecifier *NNS2) {
289 // FIXME: Implement!
290 return true;
291}
292
293/// \brief Determine whether two template arguments are equivalent.
294static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
295 const TemplateArgument &Arg1,
296 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000297 if (Arg1.getKind() != Arg2.getKind())
298 return false;
299
300 switch (Arg1.getKind()) {
301 case TemplateArgument::Null:
302 return true;
303
304 case TemplateArgument::Type:
305 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmand7a6b162012-09-26 02:36:12 +0000306
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000307 case TemplateArgument::Integral:
308 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
309 Arg2.getIntegralType()))
310 return false;
311
Eric Christopher81695fa2012-07-15 00:23:57 +0000312 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000313
314 case TemplateArgument::Declaration:
315 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmand7a6b162012-09-26 02:36:12 +0000316
317 case TemplateArgument::NullPtr:
318 return true; // FIXME: Is this correct?
319
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000320 case TemplateArgument::Template:
321 return IsStructurallyEquivalent(Context,
322 Arg1.getAsTemplate(),
323 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000324
325 case TemplateArgument::TemplateExpansion:
326 return IsStructurallyEquivalent(Context,
327 Arg1.getAsTemplateOrTemplatePattern(),
328 Arg2.getAsTemplateOrTemplatePattern());
329
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000330 case TemplateArgument::Expression:
331 return IsStructurallyEquivalent(Context,
332 Arg1.getAsExpr(), Arg2.getAsExpr());
333
334 case TemplateArgument::Pack:
335 if (Arg1.pack_size() != Arg2.pack_size())
336 return false;
337
338 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
339 if (!IsStructurallyEquivalent(Context,
340 Arg1.pack_begin()[I],
341 Arg2.pack_begin()[I]))
342 return false;
343
344 return true;
345 }
346
347 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000348}
349
350/// \brief Determine structural equivalence for the common part of array
351/// types.
352static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
353 const ArrayType *Array1,
354 const ArrayType *Array2) {
355 if (!IsStructurallyEquivalent(Context,
356 Array1->getElementType(),
357 Array2->getElementType()))
358 return false;
359 if (Array1->getSizeModifier() != Array2->getSizeModifier())
360 return false;
361 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
362 return false;
363
364 return true;
365}
366
367/// \brief Determine structural equivalence of two types.
368static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
369 QualType T1, QualType T2) {
370 if (T1.isNull() || T2.isNull())
371 return T1.isNull() && T2.isNull();
372
373 if (!Context.StrictTypeSpelling) {
374 // We aren't being strict about token-to-token equivalence of types,
375 // so map down to the canonical type.
376 T1 = Context.C1.getCanonicalType(T1);
377 T2 = Context.C2.getCanonicalType(T2);
378 }
379
380 if (T1.getQualifiers() != T2.getQualifiers())
381 return false;
382
Douglas Gregorea35d112010-02-15 23:54:17 +0000383 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000384
Douglas Gregorea35d112010-02-15 23:54:17 +0000385 if (T1->getTypeClass() != T2->getTypeClass()) {
386 // Compare function types with prototypes vs. without prototypes as if
387 // both did not have prototypes.
388 if (T1->getTypeClass() == Type::FunctionProto &&
389 T2->getTypeClass() == Type::FunctionNoProto)
390 TC = Type::FunctionNoProto;
391 else if (T1->getTypeClass() == Type::FunctionNoProto &&
392 T2->getTypeClass() == Type::FunctionProto)
393 TC = Type::FunctionNoProto;
394 else
395 return false;
396 }
397
398 switch (TC) {
399 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000400 // FIXME: Deal with Char_S/Char_U.
401 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
402 return false;
403 break;
404
405 case Type::Complex:
406 if (!IsStructurallyEquivalent(Context,
407 cast<ComplexType>(T1)->getElementType(),
408 cast<ComplexType>(T2)->getElementType()))
409 return false;
410 break;
411
Stephen Hines651f13c2014-04-23 16:59:28 -0700412 case Type::Adjusted:
Reid Kleckner12df2462013-06-24 17:51:48 +0000413 case Type::Decayed:
414 if (!IsStructurallyEquivalent(Context,
Stephen Hines651f13c2014-04-23 16:59:28 -0700415 cast<AdjustedType>(T1)->getOriginalType(),
416 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner12df2462013-06-24 17:51:48 +0000417 return false;
418 break;
419
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000420 case Type::Pointer:
421 if (!IsStructurallyEquivalent(Context,
422 cast<PointerType>(T1)->getPointeeType(),
423 cast<PointerType>(T2)->getPointeeType()))
424 return false;
425 break;
426
427 case Type::BlockPointer:
428 if (!IsStructurallyEquivalent(Context,
429 cast<BlockPointerType>(T1)->getPointeeType(),
430 cast<BlockPointerType>(T2)->getPointeeType()))
431 return false;
432 break;
433
434 case Type::LValueReference:
435 case Type::RValueReference: {
436 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
437 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
438 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
439 return false;
440 if (Ref1->isInnerRef() != Ref2->isInnerRef())
441 return false;
442 if (!IsStructurallyEquivalent(Context,
443 Ref1->getPointeeTypeAsWritten(),
444 Ref2->getPointeeTypeAsWritten()))
445 return false;
446 break;
447 }
448
449 case Type::MemberPointer: {
450 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
451 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
452 if (!IsStructurallyEquivalent(Context,
453 MemPtr1->getPointeeType(),
454 MemPtr2->getPointeeType()))
455 return false;
456 if (!IsStructurallyEquivalent(Context,
457 QualType(MemPtr1->getClass(), 0),
458 QualType(MemPtr2->getClass(), 0)))
459 return false;
460 break;
461 }
462
463 case Type::ConstantArray: {
464 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
465 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher81695fa2012-07-15 00:23:57 +0000466 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000467 return false;
468
469 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
470 return false;
471 break;
472 }
473
474 case Type::IncompleteArray:
475 if (!IsArrayStructurallyEquivalent(Context,
476 cast<ArrayType>(T1),
477 cast<ArrayType>(T2)))
478 return false;
479 break;
480
481 case Type::VariableArray: {
482 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
483 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
484 if (!IsStructurallyEquivalent(Context,
485 Array1->getSizeExpr(), Array2->getSizeExpr()))
486 return false;
487
488 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
489 return false;
490
491 break;
492 }
493
494 case Type::DependentSizedArray: {
495 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
496 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
497 if (!IsStructurallyEquivalent(Context,
498 Array1->getSizeExpr(), Array2->getSizeExpr()))
499 return false;
500
501 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
502 return false;
503
504 break;
505 }
506
507 case Type::DependentSizedExtVector: {
508 const DependentSizedExtVectorType *Vec1
509 = cast<DependentSizedExtVectorType>(T1);
510 const DependentSizedExtVectorType *Vec2
511 = cast<DependentSizedExtVectorType>(T2);
512 if (!IsStructurallyEquivalent(Context,
513 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
514 return false;
515 if (!IsStructurallyEquivalent(Context,
516 Vec1->getElementType(),
517 Vec2->getElementType()))
518 return false;
519 break;
520 }
521
522 case Type::Vector:
523 case Type::ExtVector: {
524 const VectorType *Vec1 = cast<VectorType>(T1);
525 const VectorType *Vec2 = cast<VectorType>(T2);
526 if (!IsStructurallyEquivalent(Context,
527 Vec1->getElementType(),
528 Vec2->getElementType()))
529 return false;
530 if (Vec1->getNumElements() != Vec2->getNumElements())
531 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000532 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000533 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000534 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000535 }
536
537 case Type::FunctionProto: {
538 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
539 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Stephen Hines651f13c2014-04-23 16:59:28 -0700540 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000541 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700542 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
543 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
544 Proto2->getParamType(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 return false;
546 }
547 if (Proto1->isVariadic() != Proto2->isVariadic())
548 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000549 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000550 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000551 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
552 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
553 return false;
554 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
555 if (!IsStructurallyEquivalent(Context,
556 Proto1->getExceptionType(I),
557 Proto2->getExceptionType(I)))
558 return false;
559 }
560 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000561 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000562 Proto1->getNoexceptExpr(),
563 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 return false;
565 }
566 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
567 return false;
568
569 // Fall through to check the bits common with FunctionNoProtoType.
570 }
571
572 case Type::FunctionNoProto: {
573 const FunctionType *Function1 = cast<FunctionType>(T1);
574 const FunctionType *Function2 = cast<FunctionType>(T2);
Stephen Hines651f13c2014-04-23 16:59:28 -0700575 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
576 Function2->getReturnType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000577 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000578 if (Function1->getExtInfo() != Function2->getExtInfo())
579 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000580 break;
581 }
582
583 case Type::UnresolvedUsing:
584 if (!IsStructurallyEquivalent(Context,
585 cast<UnresolvedUsingType>(T1)->getDecl(),
586 cast<UnresolvedUsingType>(T2)->getDecl()))
587 return false;
588
589 break;
John McCall9d156a72011-01-06 01:58:22 +0000590
591 case Type::Attributed:
592 if (!IsStructurallyEquivalent(Context,
593 cast<AttributedType>(T1)->getModifiedType(),
594 cast<AttributedType>(T2)->getModifiedType()))
595 return false;
596 if (!IsStructurallyEquivalent(Context,
597 cast<AttributedType>(T1)->getEquivalentType(),
598 cast<AttributedType>(T2)->getEquivalentType()))
599 return false;
600 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000601
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000602 case Type::Paren:
603 if (!IsStructurallyEquivalent(Context,
604 cast<ParenType>(T1)->getInnerType(),
605 cast<ParenType>(T2)->getInnerType()))
606 return false;
607 break;
608
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000609 case Type::Typedef:
610 if (!IsStructurallyEquivalent(Context,
611 cast<TypedefType>(T1)->getDecl(),
612 cast<TypedefType>(T2)->getDecl()))
613 return false;
614 break;
615
616 case Type::TypeOfExpr:
617 if (!IsStructurallyEquivalent(Context,
618 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
619 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
620 return false;
621 break;
622
623 case Type::TypeOf:
624 if (!IsStructurallyEquivalent(Context,
625 cast<TypeOfType>(T1)->getUnderlyingType(),
626 cast<TypeOfType>(T2)->getUnderlyingType()))
627 return false;
628 break;
Sean Huntca63c202011-05-24 22:41:36 +0000629
630 case Type::UnaryTransform:
631 if (!IsStructurallyEquivalent(Context,
632 cast<UnaryTransformType>(T1)->getUnderlyingType(),
633 cast<UnaryTransformType>(T1)->getUnderlyingType()))
634 return false;
635 break;
636
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000637 case Type::Decltype:
638 if (!IsStructurallyEquivalent(Context,
639 cast<DecltypeType>(T1)->getUnderlyingExpr(),
640 cast<DecltypeType>(T2)->getUnderlyingExpr()))
641 return false;
642 break;
643
Richard Smith34b41d92011-02-20 03:19:35 +0000644 case Type::Auto:
645 if (!IsStructurallyEquivalent(Context,
646 cast<AutoType>(T1)->getDeducedType(),
647 cast<AutoType>(T2)->getDeducedType()))
648 return false;
649 break;
650
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000651 case Type::Record:
652 case Type::Enum:
653 if (!IsStructurallyEquivalent(Context,
654 cast<TagType>(T1)->getDecl(),
655 cast<TagType>(T2)->getDecl()))
656 return false;
657 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000658
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000659 case Type::TemplateTypeParm: {
660 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
661 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
662 if (Parm1->getDepth() != Parm2->getDepth())
663 return false;
664 if (Parm1->getIndex() != Parm2->getIndex())
665 return false;
666 if (Parm1->isParameterPack() != Parm2->isParameterPack())
667 return false;
668
669 // Names of template type parameters are never significant.
670 break;
671 }
672
673 case Type::SubstTemplateTypeParm: {
674 const SubstTemplateTypeParmType *Subst1
675 = cast<SubstTemplateTypeParmType>(T1);
676 const SubstTemplateTypeParmType *Subst2
677 = cast<SubstTemplateTypeParmType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 QualType(Subst1->getReplacedParameter(), 0),
680 QualType(Subst2->getReplacedParameter(), 0)))
681 return false;
682 if (!IsStructurallyEquivalent(Context,
683 Subst1->getReplacementType(),
684 Subst2->getReplacementType()))
685 return false;
686 break;
687 }
688
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000689 case Type::SubstTemplateTypeParmPack: {
690 const SubstTemplateTypeParmPackType *Subst1
691 = cast<SubstTemplateTypeParmPackType>(T1);
692 const SubstTemplateTypeParmPackType *Subst2
693 = cast<SubstTemplateTypeParmPackType>(T2);
694 if (!IsStructurallyEquivalent(Context,
695 QualType(Subst1->getReplacedParameter(), 0),
696 QualType(Subst2->getReplacedParameter(), 0)))
697 return false;
698 if (!IsStructurallyEquivalent(Context,
699 Subst1->getArgumentPack(),
700 Subst2->getArgumentPack()))
701 return false;
702 break;
703 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000704 case Type::TemplateSpecialization: {
705 const TemplateSpecializationType *Spec1
706 = cast<TemplateSpecializationType>(T1);
707 const TemplateSpecializationType *Spec2
708 = cast<TemplateSpecializationType>(T2);
709 if (!IsStructurallyEquivalent(Context,
710 Spec1->getTemplateName(),
711 Spec2->getTemplateName()))
712 return false;
713 if (Spec1->getNumArgs() != Spec2->getNumArgs())
714 return false;
715 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
716 if (!IsStructurallyEquivalent(Context,
717 Spec1->getArg(I), Spec2->getArg(I)))
718 return false;
719 }
720 break;
721 }
722
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000723 case Type::Elaborated: {
724 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
725 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
726 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
727 if (Elab1->getKeyword() != Elab2->getKeyword())
728 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000729 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000730 Elab1->getQualifier(),
731 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000732 return false;
733 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000734 Elab1->getNamedType(),
735 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000736 return false;
737 break;
738 }
739
John McCall3cb0ebd2010-03-10 03:28:59 +0000740 case Type::InjectedClassName: {
741 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
742 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
743 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000744 Inj1->getInjectedSpecializationType(),
745 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000746 return false;
747 break;
748 }
749
Douglas Gregor4714c122010-03-31 17:34:00 +0000750 case Type::DependentName: {
751 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
752 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000753 if (!IsStructurallyEquivalent(Context,
754 Typename1->getQualifier(),
755 Typename2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
758 Typename2->getIdentifier()))
759 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000760
761 break;
762 }
763
John McCall33500952010-06-11 00:33:02 +0000764 case Type::DependentTemplateSpecialization: {
765 const DependentTemplateSpecializationType *Spec1 =
766 cast<DependentTemplateSpecializationType>(T1);
767 const DependentTemplateSpecializationType *Spec2 =
768 cast<DependentTemplateSpecializationType>(T2);
769 if (!IsStructurallyEquivalent(Context,
770 Spec1->getQualifier(),
771 Spec2->getQualifier()))
772 return false;
773 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
774 Spec2->getIdentifier()))
775 return false;
776 if (Spec1->getNumArgs() != Spec2->getNumArgs())
777 return false;
778 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
779 if (!IsStructurallyEquivalent(Context,
780 Spec1->getArg(I), Spec2->getArg(I)))
781 return false;
782 }
783 break;
784 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000785
786 case Type::PackExpansion:
787 if (!IsStructurallyEquivalent(Context,
788 cast<PackExpansionType>(T1)->getPattern(),
789 cast<PackExpansionType>(T2)->getPattern()))
790 return false;
791 break;
792
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000793 case Type::ObjCInterface: {
794 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
795 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
796 if (!IsStructurallyEquivalent(Context,
797 Iface1->getDecl(), Iface2->getDecl()))
798 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000799 break;
800 }
801
802 case Type::ObjCObject: {
803 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
804 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
805 if (!IsStructurallyEquivalent(Context,
806 Obj1->getBaseType(),
807 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000808 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000809 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
810 return false;
811 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000812 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000813 Obj1->getProtocol(I),
814 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000815 return false;
816 }
817 break;
818 }
819
820 case Type::ObjCObjectPointer: {
821 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
822 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
823 if (!IsStructurallyEquivalent(Context,
824 Ptr1->getPointeeType(),
825 Ptr2->getPointeeType()))
826 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000827 break;
828 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000829
830 case Type::Atomic: {
831 if (!IsStructurallyEquivalent(Context,
832 cast<AtomicType>(T1)->getValueType(),
833 cast<AtomicType>(T2)->getValueType()))
834 return false;
835 break;
836 }
837
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000838 } // end switch
839
840 return true;
841}
842
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000843/// \brief Determine structural equivalence of two fields.
844static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
845 FieldDecl *Field1, FieldDecl *Field2) {
846 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000847
848 // For anonymous structs/unions, match up the anonymous struct/union type
849 // declarations directly, so that we don't go off searching for anonymous
850 // types
851 if (Field1->isAnonymousStructOrUnion() &&
852 Field2->isAnonymousStructOrUnion()) {
853 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
854 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
855 return IsStructurallyEquivalent(Context, D1, D2);
856 }
Sean Callananfff418b2013-04-26 22:49:25 +0000857
858 // Check for equivalent field names.
859 IdentifierInfo *Name1 = Field1->getIdentifier();
860 IdentifierInfo *Name2 = Field2->getIdentifier();
861 if (!::IsStructurallyEquivalent(Name1, Name2))
862 return false;
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000863
864 if (!IsStructurallyEquivalent(Context,
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000865 Field1->getType(), Field2->getType())) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000866 if (Context.Complain) {
867 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
868 << Context.C2.getTypeDeclType(Owner2);
869 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
870 << Field2->getDeclName() << Field2->getType();
871 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
872 << Field1->getDeclName() << Field1->getType();
873 }
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000874 return false;
875 }
876
877 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000878 if (Context.Complain) {
879 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
880 << Context.C2.getTypeDeclType(Owner2);
881 if (Field1->isBitField()) {
882 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
883 << Field1->getDeclName() << Field1->getType()
884 << Field1->getBitWidthValue(Context.C1);
885 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
886 << Field2->getDeclName();
887 } else {
888 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
889 << Field2->getDeclName() << Field2->getType()
890 << Field2->getBitWidthValue(Context.C2);
891 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
892 << Field1->getDeclName();
893 }
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000894 }
895 return false;
896 }
897
898 if (Field1->isBitField()) {
899 // Make sure that the bit-fields are the same length.
900 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
901 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
902
903 if (Bits1 != Bits2) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000904 if (Context.Complain) {
905 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
906 << Context.C2.getTypeDeclType(Owner2);
907 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
908 << Field2->getDeclName() << Field2->getType() << Bits2;
909 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
910 << Field1->getDeclName() << Field1->getType() << Bits1;
911 }
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000912 return false;
913 }
914 }
915
916 return true;
917}
918
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000919/// \brief Find the index of the given anonymous struct/union within its
920/// context.
921///
922/// \returns Returns the index of this anonymous struct/union in its context,
923/// including the next assigned index (if none of them match). Returns an
924/// empty option if the context is not a record, i.e.. if the anonymous
925/// struct/union is at namespace or block scope.
David Blaikiedc84cd52013-02-20 22:23:23 +0000926static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000927 ASTContext &Context = Anon->getASTContext();
928 QualType AnonTy = Context.getRecordType(Anon);
929
930 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
931 if (!Owner)
David Blaikie66874fb2013-02-21 01:47:18 +0000932 return None;
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000933
934 unsigned Index = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -0700935 for (const auto *D : Owner->noload_decls()) {
936 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000937 if (!F || !F->isAnonymousStructOrUnion())
938 continue;
939
940 if (Context.hasSameType(F->getType(), AnonTy))
941 break;
942
943 ++Index;
944 }
945
946 return Index;
947}
948
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000949/// \brief Determine structural equivalence of two records.
950static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
951 RecordDecl *D1, RecordDecl *D2) {
952 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +0000953 if (Context.Complain) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
956 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
957 << D1->getDeclName() << (unsigned)D1->getTagKind();
958 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000959 return false;
960 }
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000961
962 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
963 // If both anonymous structs/unions are in a record context, make sure
964 // they occur in the same location in the context records.
David Blaikiedc84cd52013-02-20 22:23:23 +0000965 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
966 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +0000967 if (*Index1 != *Index2)
968 return false;
969 }
970 }
971 }
972
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000973 // If both declarations are class template specializations, we know
974 // the ODR applies, so check the template and template arguments.
975 ClassTemplateSpecializationDecl *Spec1
976 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
977 ClassTemplateSpecializationDecl *Spec2
978 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
979 if (Spec1 && Spec2) {
980 // Check that the specialized templates are the same.
981 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
982 Spec2->getSpecializedTemplate()))
983 return false;
984
985 // Check that the template arguments are the same.
986 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
987 return false;
988
989 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
990 if (!IsStructurallyEquivalent(Context,
991 Spec1->getTemplateArgs().get(I),
992 Spec2->getTemplateArgs().get(I)))
993 return false;
994 }
995 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000996 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000997 else if (Spec1 || Spec2)
998 return false;
999
Douglas Gregorea35d112010-02-15 23:54:17 +00001000 // Compare the definitions of these two records. If either or both are
1001 // incomplete, we assume that they are equivalent.
1002 D1 = D1->getDefinition();
1003 D2 = D2->getDefinition();
1004 if (!D1 || !D2)
1005 return true;
1006
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001007 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1008 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1009 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001010 if (Context.Complain) {
1011 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1012 << Context.C2.getTypeDeclType(D2);
1013 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1014 << D2CXX->getNumBases();
1015 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1016 << D1CXX->getNumBases();
1017 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001018 return false;
1019 }
1020
1021 // Check the base classes.
1022 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1023 BaseEnd1 = D1CXX->bases_end(),
1024 Base2 = D2CXX->bases_begin();
1025 Base1 != BaseEnd1;
1026 ++Base1, ++Base2) {
1027 if (!IsStructurallyEquivalent(Context,
1028 Base1->getType(), Base2->getType())) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001029 if (Context.Complain) {
1030 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1031 << Context.C2.getTypeDeclType(D2);
1032 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1033 << Base2->getType()
1034 << Base2->getSourceRange();
1035 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1036 << Base1->getType()
1037 << Base1->getSourceRange();
1038 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001039 return false;
1040 }
1041
1042 // Check virtual vs. non-virtual inheritance mismatch.
1043 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001044 if (Context.Complain) {
1045 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1046 << Context.C2.getTypeDeclType(D2);
1047 Context.Diag2(Base2->getLocStart(),
1048 diag::note_odr_virtual_base)
1049 << Base2->isVirtual() << Base2->getSourceRange();
1050 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1051 << Base1->isVirtual()
1052 << Base1->getSourceRange();
1053 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001054 return false;
1055 }
1056 }
1057 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001058 if (Context.Complain) {
1059 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1060 << Context.C2.getTypeDeclType(D2);
1061 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1062 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1063 << Base1->getType()
1064 << Base1->getSourceRange();
1065 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1066 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001067 return false;
1068 }
1069 }
1070
1071 // Check the fields for consistency.
Dmitri Gribenko2bd4b602012-05-19 17:17:26 +00001072 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001073 Field2End = D2->field_end();
Dmitri Gribenko2bd4b602012-05-19 17:17:26 +00001074 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001075 Field1End = D1->field_end();
1076 Field1 != Field1End;
1077 ++Field1, ++Field2) {
1078 if (Field2 == Field2End) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001079 if (Context.Complain) {
1080 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1081 << Context.C2.getTypeDeclType(D2);
1082 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1083 << Field1->getDeclName() << Field1->getType();
1084 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1085 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001086 return false;
1087 }
1088
David Blaikie581deb32012-06-06 20:45:41 +00001089 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor7c9412c2011-10-14 21:54:42 +00001090 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001091 }
1092
1093 if (Field2 != Field2End) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001094 if (Context.Complain) {
1095 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1096 << Context.C2.getTypeDeclType(D2);
1097 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1098 << Field2->getDeclName() << Field2->getType();
1099 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1100 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001101 return false;
1102 }
1103
1104 return true;
1105}
1106
1107/// \brief Determine structural equivalence of two enums.
1108static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1109 EnumDecl *D1, EnumDecl *D2) {
1110 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1111 EC2End = D2->enumerator_end();
1112 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1113 EC1End = D1->enumerator_end();
1114 EC1 != EC1End; ++EC1, ++EC2) {
1115 if (EC2 == EC2End) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001116 if (Context.Complain) {
1117 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1118 << Context.C2.getTypeDeclType(D2);
1119 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1120 << EC1->getDeclName()
1121 << EC1->getInitVal().toString(10);
1122 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1123 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001124 return false;
1125 }
1126
1127 llvm::APSInt Val1 = EC1->getInitVal();
1128 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher81695fa2012-07-15 00:23:57 +00001129 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001130 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001131 if (Context.Complain) {
1132 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1133 << Context.C2.getTypeDeclType(D2);
1134 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1135 << EC2->getDeclName()
1136 << EC2->getInitVal().toString(10);
1137 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1138 << EC1->getDeclName()
1139 << EC1->getInitVal().toString(10);
1140 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001141 return false;
1142 }
1143 }
1144
1145 if (EC2 != EC2End) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001146 if (Context.Complain) {
1147 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1148 << Context.C2.getTypeDeclType(D2);
1149 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1150 << EC2->getDeclName()
1151 << EC2->getInitVal().toString(10);
1152 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1153 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001154 return false;
1155 }
1156
1157 return true;
1158}
Douglas Gregor040afae2010-11-30 19:14:50 +00001159
1160static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1161 TemplateParameterList *Params1,
1162 TemplateParameterList *Params2) {
1163 if (Params1->size() != Params2->size()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001164 if (Context.Complain) {
1165 Context.Diag2(Params2->getTemplateLoc(),
1166 diag::err_odr_different_num_template_parameters)
1167 << Params1->size() << Params2->size();
1168 Context.Diag1(Params1->getTemplateLoc(),
1169 diag::note_odr_template_parameter_list);
1170 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001171 return false;
1172 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001173
Douglas Gregor040afae2010-11-30 19:14:50 +00001174 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1175 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001176 if (Context.Complain) {
1177 Context.Diag2(Params2->getParam(I)->getLocation(),
1178 diag::err_odr_different_template_parameter_kind);
1179 Context.Diag1(Params1->getParam(I)->getLocation(),
1180 diag::note_odr_template_parameter_here);
1181 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001182 return false;
1183 }
1184
1185 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1186 Params2->getParam(I))) {
1187
1188 return false;
1189 }
1190 }
1191
1192 return true;
1193}
1194
1195static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1196 TemplateTypeParmDecl *D1,
1197 TemplateTypeParmDecl *D2) {
1198 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001199 if (Context.Complain) {
1200 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1201 << D2->isParameterPack();
1202 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1203 << D1->isParameterPack();
1204 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001205 return false;
1206 }
1207
1208 return true;
1209}
1210
1211static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1212 NonTypeTemplateParmDecl *D1,
1213 NonTypeTemplateParmDecl *D2) {
Douglas Gregor040afae2010-11-30 19:14:50 +00001214 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor47395d92012-10-26 15:36:15 +00001215 if (Context.Complain) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001216 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1217 << D2->isParameterPack();
1218 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1219 << D1->isParameterPack();
1220 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001221 return false;
1222 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001223
1224 // Check types.
1225 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001226 if (Context.Complain) {
1227 Context.Diag2(D2->getLocation(),
1228 diag::err_odr_non_type_parameter_type_inconsistent)
1229 << D2->getType() << D1->getType();
1230 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1231 << D1->getType();
1232 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001233 return false;
1234 }
1235
1236 return true;
1237}
1238
1239static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1240 TemplateTemplateParmDecl *D1,
1241 TemplateTemplateParmDecl *D2) {
Douglas Gregor040afae2010-11-30 19:14:50 +00001242 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor347f64f2012-10-26 15:34:11 +00001243 if (Context.Complain) {
1244 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1245 << D2->isParameterPack();
1246 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1247 << D1->isParameterPack();
1248 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001249 return false;
1250 }
Douglas Gregor47395d92012-10-26 15:36:15 +00001251
Douglas Gregor040afae2010-11-30 19:14:50 +00001252 // Check template parameter lists.
1253 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1254 D2->getTemplateParameters());
1255}
1256
1257static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1258 ClassTemplateDecl *D1,
1259 ClassTemplateDecl *D2) {
1260 // Check template parameters.
1261 if (!IsStructurallyEquivalent(Context,
1262 D1->getTemplateParameters(),
1263 D2->getTemplateParameters()))
1264 return false;
1265
1266 // Check the templated declaration.
1267 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1268 D2->getTemplatedDecl());
1269}
1270
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001271/// \brief Determine structural equivalence of two declarations.
1272static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1273 Decl *D1, Decl *D2) {
1274 // FIXME: Check for known structural equivalences via a callback of some sort.
1275
Douglas Gregorea35d112010-02-15 23:54:17 +00001276 // Check whether we already know that these two declarations are not
1277 // structurally equivalent.
1278 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1279 D2->getCanonicalDecl())))
1280 return false;
1281
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001282 // Determine whether we've already produced a tentative equivalence for D1.
1283 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1284 if (EquivToD1)
1285 return EquivToD1 == D2->getCanonicalDecl();
1286
1287 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1288 EquivToD1 = D2->getCanonicalDecl();
1289 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1290 return true;
1291}
1292
1293bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1294 Decl *D2) {
1295 if (!::IsStructurallyEquivalent(*this, D1, D2))
1296 return false;
1297
1298 return !Finish();
1299}
1300
1301bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1302 QualType T2) {
1303 if (!::IsStructurallyEquivalent(*this, T1, T2))
1304 return false;
1305
1306 return !Finish();
1307}
1308
1309bool StructuralEquivalenceContext::Finish() {
1310 while (!DeclsToCheck.empty()) {
1311 // Check the next declaration.
1312 Decl *D1 = DeclsToCheck.front();
1313 DeclsToCheck.pop_front();
1314
1315 Decl *D2 = TentativeEquivalences[D1];
1316 assert(D2 && "Unrecorded tentative equivalence?");
1317
Douglas Gregorea35d112010-02-15 23:54:17 +00001318 bool Equivalent = true;
1319
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001320 // FIXME: Switch on all declaration kinds. For now, we're just going to
1321 // check the obvious ones.
1322 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1323 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1324 // Check for equivalent structure names.
1325 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001326 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1327 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001328 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001329 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1330 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001331 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1332 !::IsStructurallyEquivalent(*this, Record1, Record2))
1333 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001334 } else {
1335 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001336 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001337 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001338 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001339 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1340 // Check for equivalent enum names.
1341 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001342 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1343 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001344 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001345 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1346 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001347 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1348 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1349 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001350 } else {
1351 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001352 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001353 }
Richard Smith162e1c12011-04-15 14:24:37 +00001354 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1355 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001356 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001357 Typedef2->getIdentifier()) ||
1358 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001359 Typedef1->getUnderlyingType(),
1360 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001361 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001362 } else {
1363 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001364 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001365 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001366 } else if (ClassTemplateDecl *ClassTemplate1
1367 = dyn_cast<ClassTemplateDecl>(D1)) {
1368 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1369 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1370 ClassTemplate2->getIdentifier()) ||
1371 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1372 Equivalent = false;
1373 } else {
1374 // Class template/non-class-template mismatch.
1375 Equivalent = false;
1376 }
1377 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1378 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1379 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1380 Equivalent = false;
1381 } else {
1382 // Kind mismatch.
1383 Equivalent = false;
1384 }
1385 } else if (NonTypeTemplateParmDecl *NTTP1
1386 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1387 if (NonTypeTemplateParmDecl *NTTP2
1388 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1389 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1390 Equivalent = false;
1391 } else {
1392 // Kind mismatch.
1393 Equivalent = false;
1394 }
1395 } else if (TemplateTemplateParmDecl *TTP1
1396 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1397 if (TemplateTemplateParmDecl *TTP2
1398 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1399 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1400 Equivalent = false;
1401 } else {
1402 // Kind mismatch.
1403 Equivalent = false;
1404 }
1405 }
1406
Douglas Gregorea35d112010-02-15 23:54:17 +00001407 if (!Equivalent) {
1408 // Note that these two declarations are not equivalent (and we already
1409 // know about it).
1410 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1411 D2->getCanonicalDecl()));
1412 return true;
1413 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001414 // FIXME: Check other declaration kinds!
1415 }
1416
1417 return false;
1418}
1419
1420//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421// Import Types
1422//----------------------------------------------------------------------------
1423
John McCallf4c73712011-01-19 06:33:43 +00001424QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001425 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1426 << T->getTypeClassName();
1427 return QualType();
1428}
1429
John McCallf4c73712011-01-19 06:33:43 +00001430QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001431 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001432#define SHARED_SINGLETON_TYPE(Expansion)
1433#define BUILTIN_TYPE(Id, SingletonId) \
1434 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1435#include "clang/AST/BuiltinTypes.def"
1436
1437 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1438 // context supports C++.
1439
1440 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1441 // context supports ObjC.
1442
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001443 case BuiltinType::Char_U:
1444 // The context we're importing from has an unsigned 'char'. If we're
1445 // importing into a context with a signed 'char', translate to
1446 // 'unsigned char' instead.
David Blaikie4e4d0842012-03-11 07:00:24 +00001447 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001448 return Importer.getToContext().UnsignedCharTy;
1449
1450 return Importer.getToContext().CharTy;
1451
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001452 case BuiltinType::Char_S:
1453 // The context we're importing from has an unsigned 'char'. If we're
1454 // importing into a context with a signed 'char', translate to
1455 // 'unsigned char' instead.
David Blaikie4e4d0842012-03-11 07:00:24 +00001456 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001457 return Importer.getToContext().SignedCharTy;
1458
1459 return Importer.getToContext().CharTy;
1460
Chris Lattner3f59c972010-12-25 23:25:43 +00001461 case BuiltinType::WChar_S:
1462 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001463 // FIXME: If not in C++, shall we translate to the C equivalent of
1464 // wchar_t?
1465 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001466 }
David Blaikie30263482012-01-20 21:50:17 +00001467
1468 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001469}
1470
John McCallf4c73712011-01-19 06:33:43 +00001471QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001472 QualType ToElementType = Importer.Import(T->getElementType());
1473 if (ToElementType.isNull())
1474 return QualType();
1475
1476 return Importer.getToContext().getComplexType(ToElementType);
1477}
1478
John McCallf4c73712011-01-19 06:33:43 +00001479QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001480 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1481 if (ToPointeeType.isNull())
1482 return QualType();
1483
1484 return Importer.getToContext().getPointerType(ToPointeeType);
1485}
1486
John McCallf4c73712011-01-19 06:33:43 +00001487QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001488 // FIXME: Check for blocks support in "to" context.
1489 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1490 if (ToPointeeType.isNull())
1491 return QualType();
1492
1493 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1494}
1495
John McCallf4c73712011-01-19 06:33:43 +00001496QualType
1497ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001498 // FIXME: Check for C++ support in "to" context.
1499 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1500 if (ToPointeeType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1504}
1505
John McCallf4c73712011-01-19 06:33:43 +00001506QualType
1507ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001508 // FIXME: Check for C++0x support in "to" context.
1509 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1510 if (ToPointeeType.isNull())
1511 return QualType();
1512
1513 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1514}
1515
John McCallf4c73712011-01-19 06:33:43 +00001516QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001517 // FIXME: Check for C++ support in "to" context.
1518 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1519 if (ToPointeeType.isNull())
1520 return QualType();
1521
1522 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1523 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1524 ClassType.getTypePtr());
1525}
1526
John McCallf4c73712011-01-19 06:33:43 +00001527QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001528 QualType ToElementType = Importer.Import(T->getElementType());
1529 if (ToElementType.isNull())
1530 return QualType();
1531
1532 return Importer.getToContext().getConstantArrayType(ToElementType,
1533 T->getSize(),
1534 T->getSizeModifier(),
1535 T->getIndexTypeCVRQualifiers());
1536}
1537
John McCallf4c73712011-01-19 06:33:43 +00001538QualType
1539ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001540 QualType ToElementType = Importer.Import(T->getElementType());
1541 if (ToElementType.isNull())
1542 return QualType();
1543
1544 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1545 T->getSizeModifier(),
1546 T->getIndexTypeCVRQualifiers());
1547}
1548
John McCallf4c73712011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001550 QualType ToElementType = Importer.Import(T->getElementType());
1551 if (ToElementType.isNull())
1552 return QualType();
1553
1554 Expr *Size = Importer.Import(T->getSizeExpr());
1555 if (!Size)
1556 return QualType();
1557
1558 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1559 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1560 T->getSizeModifier(),
1561 T->getIndexTypeCVRQualifiers(),
1562 Brackets);
1563}
1564
John McCallf4c73712011-01-19 06:33:43 +00001565QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001566 QualType ToElementType = Importer.Import(T->getElementType());
1567 if (ToElementType.isNull())
1568 return QualType();
1569
1570 return Importer.getToContext().getVectorType(ToElementType,
1571 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001572 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001573}
1574
John McCallf4c73712011-01-19 06:33:43 +00001575QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001576 QualType ToElementType = Importer.Import(T->getElementType());
1577 if (ToElementType.isNull())
1578 return QualType();
1579
1580 return Importer.getToContext().getExtVectorType(ToElementType,
1581 T->getNumElements());
1582}
1583
John McCallf4c73712011-01-19 06:33:43 +00001584QualType
1585ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001586 // FIXME: What happens if we're importing a function without a prototype
1587 // into C++? Should we make it variadic?
Stephen Hines651f13c2014-04-23 16:59:28 -07001588 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 if (ToResultType.isNull())
1590 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001591
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001592 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001593 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001594}
1595
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00001596QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001597 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001598 if (ToResultType.isNull())
1599 return QualType();
1600
1601 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001602 SmallVector<QualType, 4> ArgTypes;
Stephen Hines651f13c2014-04-23 16:59:28 -07001603 for (const auto &A : T->param_types()) {
1604 QualType ArgType = Importer.Import(A);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001605 if (ArgType.isNull())
1606 return QualType();
1607 ArgTypes.push_back(ArgType);
1608 }
1609
1610 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001611 SmallVector<QualType, 4> ExceptionTypes;
Stephen Hines651f13c2014-04-23 16:59:28 -07001612 for (const auto &E : T->exceptions()) {
1613 QualType ExceptionType = Importer.Import(E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001614 if (ExceptionType.isNull())
1615 return QualType();
1616 ExceptionTypes.push_back(ExceptionType);
1617 }
John McCalle23cf432010-12-14 08:05:40 +00001618
Argyrios Kyrtzidisf0fdefc2012-09-22 01:58:06 +00001619 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1620 FunctionProtoType::ExtProtoInfo ToEPI;
1621
1622 ToEPI.ExtInfo = FromEPI.ExtInfo;
1623 ToEPI.Variadic = FromEPI.Variadic;
1624 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1625 ToEPI.TypeQuals = FromEPI.TypeQuals;
1626 ToEPI.RefQualifier = FromEPI.RefQualifier;
Stephen Hines176edba2014-12-01 14:53:08 -08001627 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1628 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1629 ToEPI.ExceptionSpec.NoexceptExpr =
1630 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1631 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1632 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1633 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1634 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisf0fdefc2012-09-22 01:58:06 +00001635
Jordan Rosebea522f2013-03-08 21:51:21 +00001636 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisf0fdefc2012-09-22 01:58:06 +00001637}
1638
Sean Callanan0aeb2892011-08-11 16:56:07 +00001639QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1640 QualType ToInnerType = Importer.Import(T->getInnerType());
1641 if (ToInnerType.isNull())
1642 return QualType();
1643
1644 return Importer.getToContext().getParenType(ToInnerType);
1645}
1646
John McCallf4c73712011-01-19 06:33:43 +00001647QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001648 TypedefNameDecl *ToDecl
1649 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001650 if (!ToDecl)
1651 return QualType();
1652
1653 return Importer.getToContext().getTypeDeclType(ToDecl);
1654}
1655
John McCallf4c73712011-01-19 06:33:43 +00001656QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001657 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1658 if (!ToExpr)
1659 return QualType();
1660
1661 return Importer.getToContext().getTypeOfExprType(ToExpr);
1662}
1663
John McCallf4c73712011-01-19 06:33:43 +00001664QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001665 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1666 if (ToUnderlyingType.isNull())
1667 return QualType();
1668
1669 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1670}
1671
John McCallf4c73712011-01-19 06:33:43 +00001672QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001673 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001674 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1675 if (!ToExpr)
1676 return QualType();
1677
Douglas Gregorf8af9822012-02-12 18:42:33 +00001678 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1679 if (UnderlyingType.isNull())
1680 return QualType();
1681
1682 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001683}
1684
Sean Huntca63c202011-05-24 22:41:36 +00001685QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1686 QualType ToBaseType = Importer.Import(T->getBaseType());
1687 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1688 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1689 return QualType();
1690
1691 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1692 ToUnderlyingType,
1693 T->getUTTKind());
1694}
1695
Richard Smith34b41d92011-02-20 03:19:35 +00001696QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smitha2c36462013-04-26 16:15:35 +00001697 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith34b41d92011-02-20 03:19:35 +00001698 QualType FromDeduced = T->getDeducedType();
1699 QualType ToDeduced;
1700 if (!FromDeduced.isNull()) {
1701 ToDeduced = Importer.Import(FromDeduced);
1702 if (ToDeduced.isNull())
1703 return QualType();
1704 }
1705
Faisal Valifad9e132013-09-26 19:54:12 +00001706 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1707 /*IsDependent*/false);
Richard Smith34b41d92011-02-20 03:19:35 +00001708}
1709
John McCallf4c73712011-01-19 06:33:43 +00001710QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001711 RecordDecl *ToDecl
1712 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1713 if (!ToDecl)
1714 return QualType();
1715
1716 return Importer.getToContext().getTagDeclType(ToDecl);
1717}
1718
John McCallf4c73712011-01-19 06:33:43 +00001719QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001720 EnumDecl *ToDecl
1721 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1722 if (!ToDecl)
1723 return QualType();
1724
1725 return Importer.getToContext().getTagDeclType(ToDecl);
1726}
1727
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -07001728QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1729 QualType FromModifiedType = T->getModifiedType();
1730 QualType FromEquivalentType = T->getEquivalentType();
1731 QualType ToModifiedType;
1732 QualType ToEquivalentType;
1733
1734 if (!FromModifiedType.isNull()) {
1735 ToModifiedType = Importer.Import(FromModifiedType);
1736 if (ToModifiedType.isNull())
1737 return QualType();
1738 }
1739 if (!FromEquivalentType.isNull()) {
1740 ToEquivalentType = Importer.Import(FromEquivalentType);
1741 if (ToEquivalentType.isNull())
1742 return QualType();
1743 }
1744
1745 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1746 ToModifiedType, ToEquivalentType);
1747}
1748
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001749QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001750 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001751 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1752 if (ToTemplate.isNull())
1753 return QualType();
1754
Chris Lattner5f9e2722011-07-23 10:55:15 +00001755 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001756 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1757 return QualType();
1758
1759 QualType ToCanonType;
1760 if (!QualType(T, 0).isCanonical()) {
1761 QualType FromCanonType
1762 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1763 ToCanonType =Importer.Import(FromCanonType);
1764 if (ToCanonType.isNull())
1765 return QualType();
1766 }
1767 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1768 ToTemplateArgs.data(),
1769 ToTemplateArgs.size(),
1770 ToCanonType);
1771}
1772
John McCallf4c73712011-01-19 06:33:43 +00001773QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001774 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001775 // Note: the qualifier in an ElaboratedType is optional.
1776 if (T->getQualifier()) {
1777 ToQualifier = Importer.Import(T->getQualifier());
1778 if (!ToQualifier)
1779 return QualType();
1780 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001781
1782 QualType ToNamedType = Importer.Import(T->getNamedType());
1783 if (ToNamedType.isNull())
1784 return QualType();
1785
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001786 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1787 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001788}
1789
John McCallf4c73712011-01-19 06:33:43 +00001790QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001791 ObjCInterfaceDecl *Class
1792 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1793 if (!Class)
1794 return QualType();
1795
John McCallc12c5bb2010-05-15 11:32:37 +00001796 return Importer.getToContext().getObjCInterfaceType(Class);
1797}
1798
John McCallf4c73712011-01-19 06:33:43 +00001799QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001800 QualType ToBaseType = Importer.Import(T->getBaseType());
1801 if (ToBaseType.isNull())
1802 return QualType();
1803
Chris Lattner5f9e2722011-07-23 10:55:15 +00001804 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Stephen Hines651f13c2014-04-23 16:59:28 -07001805 for (auto *P : T->quals()) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001806 ObjCProtocolDecl *Protocol
Stephen Hines651f13c2014-04-23 16:59:28 -07001807 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001808 if (!Protocol)
1809 return QualType();
1810 Protocols.push_back(Protocol);
1811 }
1812
John McCallc12c5bb2010-05-15 11:32:37 +00001813 return Importer.getToContext().getObjCObjectType(ToBaseType,
1814 Protocols.data(),
1815 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001816}
1817
John McCallf4c73712011-01-19 06:33:43 +00001818QualType
1819ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001820 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1821 if (ToPointeeType.isNull())
1822 return QualType();
1823
John McCallc12c5bb2010-05-15 11:32:37 +00001824 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001825}
1826
Douglas Gregor089459a2010-02-08 21:09:39 +00001827//----------------------------------------------------------------------------
1828// Import Declarations
1829//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001830bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1831 DeclContext *&LexicalDC,
1832 DeclarationName &Name,
1833 SourceLocation &Loc) {
1834 // Import the context of this declaration.
1835 DC = Importer.ImportContext(D->getDeclContext());
1836 if (!DC)
1837 return true;
1838
1839 LexicalDC = DC;
1840 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1841 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1842 if (!LexicalDC)
1843 return true;
1844 }
1845
1846 // Import the name of this declaration.
1847 Name = Importer.Import(D->getDeclName());
1848 if (D->getDeclName() && !Name)
1849 return true;
1850
1851 // Import the location of this declaration.
1852 Loc = Importer.Import(D->getLocation());
1853 return false;
1854}
1855
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001856void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1857 if (!FromD)
1858 return;
1859
1860 if (!ToD) {
1861 ToD = Importer.Import(FromD);
1862 if (!ToD)
1863 return;
1864 }
1865
1866 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1867 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan22468232013-01-11 23:17:47 +00001868 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001869 ImportDefinition(FromRecord, ToRecord);
1870 }
1871 }
1872 return;
1873 }
1874
1875 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1876 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1877 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1878 ImportDefinition(FromEnum, ToEnum);
1879 }
1880 }
1881 return;
1882 }
1883}
1884
Abramo Bagnara25777432010-08-11 22:01:17 +00001885void
1886ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1887 DeclarationNameInfo& To) {
1888 // NOTE: To.Name and To.Loc are already imported.
1889 // We only have to import To.LocInfo.
1890 switch (To.getName().getNameKind()) {
1891 case DeclarationName::Identifier:
1892 case DeclarationName::ObjCZeroArgSelector:
1893 case DeclarationName::ObjCOneArgSelector:
1894 case DeclarationName::ObjCMultiArgSelector:
1895 case DeclarationName::CXXUsingDirective:
1896 return;
1897
1898 case DeclarationName::CXXOperatorName: {
1899 SourceRange Range = From.getCXXOperatorNameRange();
1900 To.setCXXOperatorNameRange(Importer.Import(Range));
1901 return;
1902 }
1903 case DeclarationName::CXXLiteralOperatorName: {
1904 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1905 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1906 return;
1907 }
1908 case DeclarationName::CXXConstructorName:
1909 case DeclarationName::CXXDestructorName:
1910 case DeclarationName::CXXConversionFunctionName: {
1911 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1912 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1913 return;
1914 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001915 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001916 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001917}
1918
Douglas Gregorac32ff92012-02-01 21:00:38 +00001919void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregord8868a62011-01-18 03:11:38 +00001920 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001921 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001922 return;
1923 }
1924
Stephen Hines651f13c2014-04-23 16:59:28 -07001925 for (auto *From : FromDC->decls())
1926 Importer.Import(From);
Douglas Gregor083a8212010-02-21 18:24:45 +00001927}
1928
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001929bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001930 ImportDefinitionKind Kind) {
1931 if (To->getDefinition() || To->isBeingDefined()) {
1932 if (Kind == IDK_Everything)
1933 ImportDeclContext(From, /*ForceImport=*/true);
1934
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001935 return false;
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001936 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001937
1938 To->startDefinition();
1939
1940 // Add base classes.
1941 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1942 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001943
1944 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1945 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1946 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001947 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001948 ToData.Aggregate = FromData.Aggregate;
1949 ToData.PlainOldData = FromData.PlainOldData;
1950 ToData.Empty = FromData.Empty;
1951 ToData.Polymorphic = FromData.Polymorphic;
1952 ToData.Abstract = FromData.Abstract;
1953 ToData.IsStandardLayout = FromData.IsStandardLayout;
1954 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1955 ToData.HasPrivateFields = FromData.HasPrivateFields;
1956 ToData.HasProtectedFields = FromData.HasProtectedFields;
1957 ToData.HasPublicFields = FromData.HasPublicFields;
1958 ToData.HasMutableFields = FromData.HasMutableFields;
Stephen Hines651f13c2014-04-23 16:59:28 -07001959 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smithdfefb842012-02-25 07:33:38 +00001960 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithd079abf2012-05-07 01:07:30 +00001961 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smithd5bc8672012-12-08 02:01:17 +00001962 ToData.HasUninitializedReferenceMember
1963 = FromData.HasUninitializedReferenceMember;
Richard Smithbc2a35d2012-12-08 08:32:28 +00001964 ToData.NeedOverloadResolutionForMoveConstructor
1965 = FromData.NeedOverloadResolutionForMoveConstructor;
1966 ToData.NeedOverloadResolutionForMoveAssignment
1967 = FromData.NeedOverloadResolutionForMoveAssignment;
1968 ToData.NeedOverloadResolutionForDestructor
1969 = FromData.NeedOverloadResolutionForDestructor;
1970 ToData.DefaultedMoveConstructorIsDeleted
1971 = FromData.DefaultedMoveConstructorIsDeleted;
1972 ToData.DefaultedMoveAssignmentIsDeleted
1973 = FromData.DefaultedMoveAssignmentIsDeleted;
1974 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001975 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1976 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001977 ToData.HasConstexprNonCopyMoveConstructor
1978 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smithdfefb842012-02-25 07:33:38 +00001979 ToData.DefaultedDefaultConstructorIsConstexpr
1980 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smithdfefb842012-02-25 07:33:38 +00001981 ToData.HasConstexprDefaultConstructor
1982 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001983 ToData.HasNonLiteralTypeFieldsOrBases
1984 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smithdfefb842012-02-25 07:33:38 +00001985 // ComputedVisibleConversions not imported.
Douglas Gregor27c72d82011-11-03 18:07:07 +00001986 ToData.UserProvidedDefaultConstructor
1987 = FromData.UserProvidedDefaultConstructor;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001988 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smithacf796b2012-11-28 06:23:12 +00001989 ToData.ImplicitCopyConstructorHasConstParam
1990 = FromData.ImplicitCopyConstructorHasConstParam;
1991 ToData.ImplicitCopyAssignmentHasConstParam
1992 = FromData.ImplicitCopyAssignmentHasConstParam;
1993 ToData.HasDeclaredCopyConstructorWithConstParam
1994 = FromData.HasDeclaredCopyConstructorWithConstParam;
1995 ToData.HasDeclaredCopyAssignmentWithConstParam
1996 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smithdfefb842012-02-25 07:33:38 +00001997 ToData.IsLambda = FromData.IsLambda;
1998
Chris Lattner5f9e2722011-07-23 10:55:15 +00001999 SmallVector<CXXBaseSpecifier *, 4> Bases;
Stephen Hines651f13c2014-04-23 16:59:28 -07002000 for (const auto &Base1 : FromCXX->bases()) {
2001 QualType T = Importer.Import(Base1.getType());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002002 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00002003 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002004
2005 SourceLocation EllipsisLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07002006 if (Base1.isPackExpansion())
2007 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002008
2009 // Ensure that we have a definition for the base.
Stephen Hines651f13c2014-04-23 16:59:28 -07002010 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002011
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002012 Bases.push_back(
2013 new (Importer.getToContext())
Stephen Hines651f13c2014-04-23 16:59:28 -07002014 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2015 Base1.isVirtual(),
2016 Base1.isBaseOfClass(),
2017 Base1.getAccessSpecifierAsWritten(),
2018 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00002019 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002020 }
2021 if (!Bases.empty())
2022 ToCXX->setBases(Bases.data(), Bases.size());
2023 }
2024
Douglas Gregorac32ff92012-02-01 21:00:38 +00002025 if (shouldForceImportDeclContext(Kind))
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002026 ImportDeclContext(From, /*ForceImport=*/true);
2027
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002028 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00002029 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002030}
2031
Larisse Voufoef4579c2013-08-06 01:03:05 +00002032bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2033 ImportDefinitionKind Kind) {
2034 if (To->getDefinition())
2035 return false;
2036
2037 // FIXME: Can we really import any initializer? Alternatively, we could force
2038 // ourselves to import every declaration of a variable and then only use
2039 // getInit() here.
2040 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2041
2042 // FIXME: Other bits to merge?
2043
2044 return false;
2045}
2046
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002047bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00002048 ImportDefinitionKind Kind) {
2049 if (To->getDefinition() || To->isBeingDefined()) {
2050 if (Kind == IDK_Everything)
2051 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002052 return false;
Douglas Gregorac32ff92012-02-01 21:00:38 +00002053 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002054
2055 To->startDefinition();
2056
2057 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2058 if (T.isNull())
2059 return true;
2060
2061 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2062 if (ToPromotionType.isNull())
2063 return true;
Douglas Gregorac32ff92012-02-01 21:00:38 +00002064
2065 if (shouldForceImportDeclContext(Kind))
2066 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002067
2068 // FIXME: we might need to merge the number of positive or negative bits
2069 // if the enumerator lists don't match.
2070 To->completeDefinition(T, ToPromotionType,
2071 From->getNumPositiveBits(),
2072 From->getNumNegativeBits());
2073 return false;
2074}
2075
Douglas Gregor040afae2010-11-30 19:14:50 +00002076TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2077 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002078 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00002079 ToParams.reserve(Params->size());
2080 for (TemplateParameterList::iterator P = Params->begin(),
2081 PEnd = Params->end();
2082 P != PEnd; ++P) {
2083 Decl *To = Importer.Import(*P);
2084 if (!To)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002085 return nullptr;
2086
Douglas Gregor040afae2010-11-30 19:14:50 +00002087 ToParams.push_back(cast<NamedDecl>(To));
2088 }
2089
2090 return TemplateParameterList::Create(Importer.getToContext(),
2091 Importer.Import(Params->getTemplateLoc()),
2092 Importer.Import(Params->getLAngleLoc()),
2093 ToParams.data(), ToParams.size(),
2094 Importer.Import(Params->getRAngleLoc()));
2095}
2096
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002097TemplateArgument
2098ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2099 switch (From.getKind()) {
2100 case TemplateArgument::Null:
2101 return TemplateArgument();
2102
2103 case TemplateArgument::Type: {
2104 QualType ToType = Importer.Import(From.getAsType());
2105 if (ToType.isNull())
2106 return TemplateArgument();
2107 return TemplateArgument(ToType);
2108 }
2109
2110 case TemplateArgument::Integral: {
2111 QualType ToType = Importer.Import(From.getIntegralType());
2112 if (ToType.isNull())
2113 return TemplateArgument();
Benjamin Kramer85524372012-06-07 15:09:51 +00002114 return TemplateArgument(From, ToType);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002115 }
2116
Eli Friedmand7a6b162012-09-26 02:36:12 +00002117 case TemplateArgument::Declaration: {
Stephen Hines176edba2014-12-01 14:53:08 -08002118 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2119 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2120 if (!To || ToType.isNull())
2121 return TemplateArgument();
2122 return TemplateArgument(To, ToType);
Eli Friedmand7a6b162012-09-26 02:36:12 +00002123 }
2124
2125 case TemplateArgument::NullPtr: {
2126 QualType ToType = Importer.Import(From.getNullPtrType());
2127 if (ToType.isNull())
2128 return TemplateArgument();
2129 return TemplateArgument(ToType, /*isNullPtr*/true);
2130 }
2131
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002132 case TemplateArgument::Template: {
2133 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2134 if (ToTemplate.isNull())
2135 return TemplateArgument();
2136
2137 return TemplateArgument(ToTemplate);
2138 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002139
2140 case TemplateArgument::TemplateExpansion: {
2141 TemplateName ToTemplate
2142 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2143 if (ToTemplate.isNull())
2144 return TemplateArgument();
2145
Douglas Gregor2be29f42011-01-14 23:41:42 +00002146 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00002147 }
2148
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002149 case TemplateArgument::Expression:
2150 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2151 return TemplateArgument(ToExpr);
2152 return TemplateArgument();
2153
2154 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002155 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002156 ToPack.reserve(From.pack_size());
2157 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2158 return TemplateArgument();
2159
2160 TemplateArgument *ToArgs
2161 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2162 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2163 return TemplateArgument(ToArgs, ToPack.size());
2164 }
2165 }
2166
2167 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002168}
2169
2170bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2171 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002172 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002173 for (unsigned I = 0; I != NumFromArgs; ++I) {
2174 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2175 if (To.isNull() && !FromArgs[I].isNull())
2176 return true;
2177
2178 ToArgs.push_back(To);
2179 }
2180
2181 return false;
2182}
2183
Douglas Gregor96a01b42010-02-11 00:48:18 +00002184bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002185 RecordDecl *ToRecord, bool Complain) {
Sean Callanan8f2cb422013-10-09 21:45:11 +00002186 // Eliminate a potential failure point where we attempt to re-import
2187 // something we're trying to import while completing ToRecord.
2188 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2189 if (ToOrigin) {
2190 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2191 if (ToOriginRecord)
2192 ToRecord = ToOriginRecord;
2193 }
2194
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002195 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callanan8f2cb422013-10-09 21:45:11 +00002196 ToRecord->getASTContext(),
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002197 Importer.getNonEquivalentDecls(),
2198 false, Complain);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002199 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002200}
2201
Larisse Voufoef4579c2013-08-06 01:03:05 +00002202bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2203 bool Complain) {
2204 StructuralEquivalenceContext Ctx(
2205 Importer.getFromContext(), Importer.getToContext(),
2206 Importer.getNonEquivalentDecls(), false, Complain);
2207 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2208}
2209
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002210bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002211 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002212 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002213 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002214 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002215}
2216
Douglas Gregor1cef4592012-11-14 22:29:20 +00002217bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2218 EnumConstantDecl *ToEC)
2219{
2220 const llvm::APSInt &FromVal = FromEC->getInitVal();
2221 const llvm::APSInt &ToVal = ToEC->getInitVal();
2222
2223 return FromVal.isSigned() == ToVal.isSigned() &&
2224 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2225 FromVal == ToVal;
2226}
2227
2228bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregor040afae2010-11-30 19:14:50 +00002229 ClassTemplateDecl *To) {
2230 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2231 Importer.getToContext(),
2232 Importer.getNonEquivalentDecls());
2233 return Ctx.IsStructurallyEquivalent(From, To);
2234}
2235
Larisse Voufoef4579c2013-08-06 01:03:05 +00002236bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2237 VarTemplateDecl *To) {
2238 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2239 Importer.getToContext(),
2240 Importer.getNonEquivalentDecls());
2241 return Ctx.IsStructurallyEquivalent(From, To);
2242}
2243
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002244Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002245 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002246 << D->getDeclKindName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002247 return nullptr;
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002248}
2249
Sean Callananf1b69462011-11-17 23:20:56 +00002250Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2251 TranslationUnitDecl *ToD =
2252 Importer.getToContext().getTranslationUnitDecl();
2253
2254 Importer.Imported(D, ToD);
2255
2256 return ToD;
2257}
2258
Douglas Gregor788c62d2010-02-21 18:26:36 +00002259Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2260 // Import the major distinguishing characteristics of this namespace.
2261 DeclContext *DC, *LexicalDC;
2262 DeclarationName Name;
2263 SourceLocation Loc;
2264 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002265 return nullptr;
2266
2267 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002268 if (!Name) {
2269 // This is an anonymous namespace. Adopt an existing anonymous
2270 // namespace if we can.
2271 // FIXME: Not testable.
2272 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2273 MergeWithNamespace = TU->getAnonymousNamespace();
2274 else
2275 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2276 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002277 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002278 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002279 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002280 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2281 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002282 continue;
2283
Douglas Gregorb75a3452011-10-15 00:10:27 +00002284 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002285 MergeWithNamespace = FoundNS;
2286 ConflictingDecls.clear();
2287 break;
2288 }
2289
Douglas Gregorb75a3452011-10-15 00:10:27 +00002290 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002291 }
2292
2293 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002294 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002295 ConflictingDecls.data(),
2296 ConflictingDecls.size());
2297 }
2298 }
2299
2300 // Create the "to" namespace, if needed.
2301 NamespaceDecl *ToNamespace = MergeWithNamespace;
2302 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002303 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002304 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002305 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002306 Loc, Name.getAsIdentifierInfo(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002307 /*PrevDecl=*/nullptr);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002308 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002309 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002310
2311 // If this is an anonymous namespace, register it as the anonymous
2312 // namespace within its context.
2313 if (!Name) {
2314 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2315 TU->setAnonymousNamespace(ToNamespace);
2316 else
2317 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2318 }
2319 }
2320 Importer.Imported(D, ToNamespace);
2321
2322 ImportDeclContext(D);
2323
2324 return ToNamespace;
2325}
2326
Richard Smith162e1c12011-04-15 14:24:37 +00002327Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002328 // Import the major distinguishing characteristics of this typedef.
2329 DeclContext *DC, *LexicalDC;
2330 DeclarationName Name;
2331 SourceLocation Loc;
2332 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002333 return nullptr;
2334
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002335 // If this typedef is not in block scope, determine whether we've
2336 // seen a typedef with the same name (that we can merge with) or any
2337 // other entity by that name (which name lookup could conflict with).
2338 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002339 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002340 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002341 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002342 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002343 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2344 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002345 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002346 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002347 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002348 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2349 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002350 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002351 }
2352
Douglas Gregorb75a3452011-10-15 00:10:27 +00002353 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002354 }
2355
2356 if (!ConflictingDecls.empty()) {
2357 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2358 ConflictingDecls.data(),
2359 ConflictingDecls.size());
2360 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002361 return nullptr;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002362 }
2363 }
2364
Douglas Gregorea35d112010-02-15 23:54:17 +00002365 // Import the underlying type of this typedef;
2366 QualType T = Importer.Import(D->getUnderlyingType());
2367 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002368 return nullptr;
2369
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002370 // Create the new typedef node.
2371 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002372 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002373 TypedefNameDecl *ToTypedef;
2374 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002375 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2376 StartL, Loc,
2377 Name.getAsIdentifierInfo(),
2378 TInfo);
2379 else
Richard Smith162e1c12011-04-15 14:24:37 +00002380 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2381 StartL, Loc,
2382 Name.getAsIdentifierInfo(),
2383 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002384
Douglas Gregor325bf172010-02-22 17:42:47 +00002385 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002386 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002387 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002388 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002389
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002390 return ToTypedef;
2391}
2392
Richard Smith162e1c12011-04-15 14:24:37 +00002393Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2394 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2395}
2396
2397Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2398 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2399}
2400
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002401Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2402 // Import the major distinguishing characteristics of this enum.
2403 DeclContext *DC, *LexicalDC;
2404 DeclarationName Name;
2405 SourceLocation Loc;
2406 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002407 return nullptr;
2408
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002409 // Figure out what enum name we're looking for.
2410 unsigned IDNS = Decl::IDNS_Tag;
2411 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002412 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2413 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002414 IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00002415 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002416 IDNS |= Decl::IDNS_Ordinary;
2417
2418 // We may already have an enum of the same name; try to find and match it.
2419 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002420 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002421 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002422 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002423 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2424 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002425 continue;
2426
Douglas Gregorb75a3452011-10-15 00:10:27 +00002427 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002428 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002429 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2430 Found = Tag->getDecl();
2431 }
2432
2433 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002434 if (IsStructuralMatch(D, FoundEnum))
2435 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002436 }
2437
Douglas Gregorb75a3452011-10-15 00:10:27 +00002438 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002439 }
2440
2441 if (!ConflictingDecls.empty()) {
2442 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2443 ConflictingDecls.data(),
2444 ConflictingDecls.size());
2445 }
2446 }
2447
2448 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002449 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2450 Importer.Import(D->getLocStart()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002451 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002452 D->isScoped(), D->isScopedUsingClassTag(),
2453 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002454 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002455 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002456 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002457 D2->setLexicalDeclContext(LexicalDC);
2458 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002459 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002460
2461 // Import the integer type.
2462 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2463 if (ToIntegerType.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002464 return nullptr;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002465 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002466
2467 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002468 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002469 return nullptr;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002470
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002471 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002472}
2473
Douglas Gregor96a01b42010-02-11 00:48:18 +00002474Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2475 // If this record has a definition in the translation unit we're coming from,
2476 // but this particular declaration is not that definition, import the
2477 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002478 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002479 if (Definition && Definition != D) {
2480 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002481 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002482 return nullptr;
2483
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002484 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002485 }
2486
2487 // Import the major distinguishing characteristics of this record.
2488 DeclContext *DC, *LexicalDC;
2489 DeclarationName Name;
2490 SourceLocation Loc;
2491 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002492 return nullptr;
2493
Douglas Gregor96a01b42010-02-11 00:48:18 +00002494 // Figure out what structure name we're looking for.
2495 unsigned IDNS = Decl::IDNS_Tag;
2496 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002497 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2498 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002499 IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00002500 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor96a01b42010-02-11 00:48:18 +00002501 IDNS |= Decl::IDNS_Ordinary;
2502
2503 // We may already have a record of the same name; try to find and match it.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002504 RecordDecl *AdoptDecl = nullptr;
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002505 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002506 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002507 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002508 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002509 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2510 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002511 continue;
2512
Douglas Gregorb75a3452011-10-15 00:10:27 +00002513 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002514 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002515 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2516 Found = Tag->getDecl();
2517 }
2518
2519 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002520 if (D->isAnonymousStructOrUnion() &&
2521 FoundRecord->isAnonymousStructOrUnion()) {
2522 // If both anonymous structs/unions are in a record context, make sure
2523 // they occur in the same location in the context records.
David Blaikiedc84cd52013-02-20 22:23:23 +00002524 if (Optional<unsigned> Index1
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002525 = findAnonymousStructOrUnionIndex(D)) {
David Blaikiedc84cd52013-02-20 22:23:23 +00002526 if (Optional<unsigned> Index2 =
2527 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002528 if (*Index1 != *Index2)
2529 continue;
2530 }
2531 }
2532 }
2533
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002534 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002535 if ((SearchName && !D->isCompleteDefinition())
2536 || (D->isCompleteDefinition() &&
2537 D->isAnonymousStructOrUnion()
2538 == FoundDef->isAnonymousStructOrUnion() &&
2539 IsStructuralMatch(D, FoundDef))) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002540 // The record types structurally match, or the "from" translation
2541 // unit only had a forward declaration anyway; call it the same
2542 // function.
2543 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002544 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002545 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002546 } else if (!D->isCompleteDefinition()) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002547 // We have a forward declaration of this type, so adopt that forward
2548 // declaration rather than building a new one.
Stephen Hines651f13c2014-04-23 16:59:28 -07002549
2550 // If one or both can be completed from external storage then try one
2551 // last time to complete and compare them before doing this.
2552
2553 if (FoundRecord->hasExternalLexicalStorage() &&
2554 !FoundRecord->isCompleteDefinition())
2555 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2556 if (D->hasExternalLexicalStorage())
2557 D->getASTContext().getExternalSource()->CompleteType(D);
2558
2559 if (FoundRecord->isCompleteDefinition() &&
2560 D->isCompleteDefinition() &&
2561 !IsStructuralMatch(D, FoundRecord))
2562 continue;
2563
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002564 AdoptDecl = FoundRecord;
2565 continue;
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002566 } else if (!SearchName) {
2567 continue;
2568 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002569 }
2570
Douglas Gregorb75a3452011-10-15 00:10:27 +00002571 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002572 }
2573
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002574 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002575 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2576 ConflictingDecls.data(),
2577 ConflictingDecls.size());
2578 }
2579 }
2580
2581 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002582 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002583 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002584 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002585 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002586 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002587 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002588 DC, StartLoc, Loc,
2589 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002590 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002591 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002592 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002593 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002594 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002595 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002596
2597 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002598 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002599 LexicalDC->addDeclInternal(D2);
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002600 if (D->isAnonymousStructOrUnion())
2601 D2->setAnonymousStructOrUnion(true);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002602 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002603
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002604 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002605
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002606 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002607 return nullptr;
2608
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002609 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002610}
2611
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002612Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2613 // Import the major distinguishing characteristics of this enumerator.
2614 DeclContext *DC, *LexicalDC;
2615 DeclarationName Name;
2616 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002617 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002618 return nullptr;
Douglas Gregorea35d112010-02-15 23:54:17 +00002619
2620 QualType T = Importer.Import(D->getType());
2621 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002622 return nullptr;
Douglas Gregorea35d112010-02-15 23:54:17 +00002623
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002624 // Determine whether there are any other declarations with the same name and
2625 // in the same context.
2626 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002627 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002628 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002629 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002630 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002631 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2632 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002633 continue;
Douglas Gregor1cef4592012-11-14 22:29:20 +00002634
2635 if (EnumConstantDecl *FoundEnumConstant
2636 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2637 if (IsStructuralMatch(D, FoundEnumConstant))
2638 return Importer.Imported(D, FoundEnumConstant);
2639 }
2640
Douglas Gregorb75a3452011-10-15 00:10:27 +00002641 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002642 }
2643
2644 if (!ConflictingDecls.empty()) {
2645 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2646 ConflictingDecls.data(),
2647 ConflictingDecls.size());
2648 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002649 return nullptr;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002650 }
2651 }
2652
2653 Expr *Init = Importer.Import(D->getInitExpr());
2654 if (D->getInitExpr() && !Init)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002655 return nullptr;
2656
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002657 EnumConstantDecl *ToEnumerator
2658 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2659 Name.getAsIdentifierInfo(), T,
2660 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002661 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002662 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002663 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002664 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002665 return ToEnumerator;
2666}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002667
Douglas Gregora404ea62010-02-10 19:54:31 +00002668Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2669 // Import the major distinguishing characteristics of this function.
2670 DeclContext *DC, *LexicalDC;
2671 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002672 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002673 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002674 return nullptr;
Abramo Bagnara25777432010-08-11 22:01:17 +00002675
Douglas Gregora404ea62010-02-10 19:54:31 +00002676 // Try to find a function in our own ("to") context with the same name, same
2677 // type, and in the same context as the function we're importing.
2678 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002679 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002680 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002681 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002682 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002683 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2684 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002685 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002686
Douglas Gregorb75a3452011-10-15 00:10:27 +00002687 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola181e3ec2013-05-13 00:12:11 +00002688 if (FoundFunction->hasExternalFormalLinkage() &&
2689 D->hasExternalFormalLinkage()) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002690 if (Importer.IsStructurallyEquivalent(D->getType(),
2691 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002692 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002693 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002694 }
2695
2696 // FIXME: Check for overloading more carefully, e.g., by boosting
2697 // Sema::IsOverload out to the AST library.
2698
2699 // Function overloading is okay in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002700 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregora404ea62010-02-10 19:54:31 +00002701 continue;
2702
2703 // Complain about inconsistent function types.
2704 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002705 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002706 Importer.ToDiag(FoundFunction->getLocation(),
2707 diag::note_odr_value_here)
2708 << FoundFunction->getType();
2709 }
2710 }
2711
Douglas Gregorb75a3452011-10-15 00:10:27 +00002712 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002713 }
2714
2715 if (!ConflictingDecls.empty()) {
2716 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2717 ConflictingDecls.data(),
2718 ConflictingDecls.size());
2719 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002720 return nullptr;
Douglas Gregora404ea62010-02-10 19:54:31 +00002721 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002722 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002723
Abramo Bagnara25777432010-08-11 22:01:17 +00002724 DeclarationNameInfo NameInfo(Name, Loc);
2725 // Import additional name location/type info.
2726 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2727
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002728 QualType FromTy = D->getType();
2729 bool usedDifferentExceptionSpec = false;
2730
2731 if (const FunctionProtoType *
2732 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2733 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2734 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2735 // FunctionDecl that we are importing the FunctionProtoType for.
2736 // To avoid an infinite recursion when importing, create the FunctionDecl
2737 // with a simplified function type and update it afterwards.
Stephen Hines176edba2014-12-01 14:53:08 -08002738 if (FromEPI.ExceptionSpec.SourceDecl ||
2739 FromEPI.ExceptionSpec.SourceTemplate ||
2740 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002741 FunctionProtoType::ExtProtoInfo DefaultEPI;
2742 FromTy = Importer.getFromContext().getFunctionType(
Stephen Hines651f13c2014-04-23 16:59:28 -07002743 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002744 usedDifferentExceptionSpec = true;
2745 }
2746 }
2747
Douglas Gregorea35d112010-02-15 23:54:17 +00002748 // Import the type.
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002749 QualType T = Importer.Import(FromTy);
Douglas Gregorea35d112010-02-15 23:54:17 +00002750 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002751 return nullptr;
2752
Douglas Gregora404ea62010-02-10 19:54:31 +00002753 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002754 SmallVector<ParmVarDecl *, 8> Parameters;
Stephen Hines651f13c2014-04-23 16:59:28 -07002755 for (auto P : D->params()) {
2756 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregora404ea62010-02-10 19:54:31 +00002757 if (!ToP)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002758 return nullptr;
2759
Douglas Gregora404ea62010-02-10 19:54:31 +00002760 Parameters.push_back(ToP);
2761 }
2762
2763 // Create the imported function.
2764 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002765 FunctionDecl *ToFunction = nullptr;
Douglas Gregorc144f352010-02-21 18:29:16 +00002766 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2767 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2768 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002769 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002770 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002771 FromConstructor->isExplicit(),
2772 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002773 D->isImplicit(),
2774 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002775 } else if (isa<CXXDestructorDecl>(D)) {
2776 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2777 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002778 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002779 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002780 D->isInlineSpecified(),
2781 D->isImplicit());
2782 } else if (CXXConversionDecl *FromConversion
2783 = dyn_cast<CXXConversionDecl>(D)) {
2784 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2785 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002786 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002787 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002788 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002789 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002790 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002791 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002792 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2793 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2794 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002795 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002796 NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002797 Method->getStorageClass(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002798 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002799 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002800 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002801 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002802 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002803 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002804 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002805 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002806 D->hasWrittenPrototype(),
2807 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002808 }
John McCallb6217662010-03-15 10:12:16 +00002809
2810 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002811 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002812 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002813 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002814 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2815 ToFunction->setTrivial(D->isTrivial());
2816 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002817 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002818
Douglas Gregora404ea62010-02-10 19:54:31 +00002819 // Set the parameters.
2820 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002821 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002822 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002823 }
David Blaikie4278c652011-09-21 18:16:56 +00002824 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002825
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002826 if (usedDifferentExceptionSpec) {
2827 // Update FunctionProtoType::ExtProtoInfo.
2828 QualType T = Importer.Import(D->getType());
2829 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002830 return nullptr;
Argyrios Kyrtzidisd498f382012-09-25 19:26:39 +00002831 ToFunction->setType(T);
Argyrios Kyrtzidisf0fdefc2012-09-22 01:58:06 +00002832 }
2833
Douglas Gregora404ea62010-02-10 19:54:31 +00002834 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002835
2836 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002837 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002838
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002839 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002840}
2841
Douglas Gregorc144f352010-02-21 18:29:16 +00002842Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2843 return VisitFunctionDecl(D);
2844}
2845
2846Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2847 return VisitCXXMethodDecl(D);
2848}
2849
2850Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2851 return VisitCXXMethodDecl(D);
2852}
2853
2854Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2855 return VisitCXXMethodDecl(D);
2856}
2857
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002858static unsigned getFieldIndex(Decl *F) {
2859 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2860 if (!Owner)
2861 return 0;
2862
2863 unsigned Index = 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07002864 for (const auto *D : Owner->noload_decls()) {
2865 if (D == F)
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002866 return Index;
2867
2868 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2869 ++Index;
2870 }
2871
2872 return Index;
2873}
2874
Douglas Gregor96a01b42010-02-11 00:48:18 +00002875Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2876 // Import the major distinguishing characteristics of a variable.
2877 DeclContext *DC, *LexicalDC;
2878 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002879 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002880 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002881 return nullptr;
2882
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002883 // Determine whether we've already imported this field.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002884 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002885 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002886 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2887 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002888 // For anonymous fields, match up by index.
2889 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2890 continue;
2891
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002892 if (Importer.IsStructurallyEquivalent(D->getType(),
2893 FoundField->getType())) {
2894 Importer.Imported(D, FoundField);
2895 return FoundField;
2896 }
2897
2898 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2899 << Name << D->getType() << FoundField->getType();
2900 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2901 << FoundField->getType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002902 return nullptr;
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002903 }
2904 }
2905
Douglas Gregorea35d112010-02-15 23:54:17 +00002906 // Import the type.
2907 QualType T = Importer.Import(D->getType());
2908 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002909 return nullptr;
2910
Douglas Gregor96a01b42010-02-11 00:48:18 +00002911 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2912 Expr *BitWidth = Importer.Import(D->getBitWidth());
2913 if (!BitWidth && D->getBitWidth())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002914 return nullptr;
2915
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002916 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2917 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002918 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002919 T, TInfo, BitWidth, D->isMutable(),
Richard Smithca523302012-06-10 03:12:00 +00002920 D->getInClassInitStyle());
Douglas Gregor325bf172010-02-22 17:42:47 +00002921 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002922 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002923 if (ToField->hasInClassInitializer())
2924 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002925 ToField->setImplicit(D->isImplicit());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002926 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002927 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002928 return ToField;
2929}
2930
Francois Pichet87c2e122010-11-21 06:08:52 +00002931Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2932 // Import the major distinguishing characteristics of a variable.
2933 DeclContext *DC, *LexicalDC;
2934 DeclarationName Name;
2935 SourceLocation Loc;
2936 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002937 return nullptr;
Francois Pichet87c2e122010-11-21 06:08:52 +00002938
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002939 // Determine whether we've already imported this field.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002940 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002941 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00002942 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002943 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002944 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor11ce5fe2012-10-26 16:45:11 +00002945 // For anonymous indirect fields, match up by index.
2946 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2947 continue;
2948
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002949 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002950 FoundField->getType(),
David Blaikie7247c882013-05-15 07:37:26 +00002951 !Name.isEmpty())) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002952 Importer.Imported(D, FoundField);
2953 return FoundField;
2954 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00002955
2956 // If there are more anonymous fields to check, continue.
2957 if (!Name && I < N-1)
2958 continue;
2959
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002960 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2961 << Name << D->getType() << FoundField->getType();
2962 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2963 << FoundField->getType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002964 return nullptr;
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002965 }
2966 }
2967
Francois Pichet87c2e122010-11-21 06:08:52 +00002968 // Import the type.
2969 QualType T = Importer.Import(D->getType());
2970 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002971 return nullptr;
Francois Pichet87c2e122010-11-21 06:08:52 +00002972
2973 NamedDecl **NamedChain =
2974 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2975
2976 unsigned i = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002977 for (auto *PI : D->chain()) {
2978 Decl *D = Importer.Import(PI);
Francois Pichet87c2e122010-11-21 06:08:52 +00002979 if (!D)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002980 return nullptr;
Francois Pichet87c2e122010-11-21 06:08:52 +00002981 NamedChain[i++] = cast<NamedDecl>(D);
2982 }
2983
2984 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Stephen Hines176edba2014-12-01 14:53:08 -08002985 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
2986 NamedChain, D->getChainingSize());
2987
2988 for (const auto *Attr : D->attrs())
2989 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
2990
Francois Pichet87c2e122010-11-21 06:08:52 +00002991 ToIndirectField->setAccess(D->getAccess());
2992 ToIndirectField->setLexicalDeclContext(LexicalDC);
2993 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002994 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002995 return ToIndirectField;
2996}
2997
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002998Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2999 // Import the major distinguishing characteristics of an ivar.
3000 DeclContext *DC, *LexicalDC;
3001 DeclarationName Name;
3002 SourceLocation Loc;
3003 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003004 return nullptr;
3005
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003006 // Determine whether we've already imported this ivar
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003007 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003008 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003009 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3010 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003011 if (Importer.IsStructurallyEquivalent(D->getType(),
3012 FoundIvar->getType())) {
3013 Importer.Imported(D, FoundIvar);
3014 return FoundIvar;
3015 }
3016
3017 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3018 << Name << D->getType() << FoundIvar->getType();
3019 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3020 << FoundIvar->getType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003021 return nullptr;
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003022 }
3023 }
3024
3025 // Import the type.
3026 QualType T = Importer.Import(D->getType());
3027 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003028 return nullptr;
3029
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003030 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3031 Expr *BitWidth = Importer.Import(D->getBitWidth());
3032 if (!BitWidth && D->getBitWidth())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003033 return nullptr;
3034
Daniel Dunbara0654922010-04-02 20:10:03 +00003035 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3036 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003037 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003038 Loc, Name.getAsIdentifierInfo(),
3039 T, TInfo, D->getAccessControl(),
Stephen Hines651f13c2014-04-23 16:59:28 -07003040 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003041 ToIvar->setLexicalDeclContext(LexicalDC);
3042 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00003043 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003044 return ToIvar;
3045
3046}
3047
Douglas Gregora404ea62010-02-10 19:54:31 +00003048Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3049 // Import the major distinguishing characteristics of a variable.
3050 DeclContext *DC, *LexicalDC;
3051 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00003052 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00003053 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003054 return nullptr;
3055
Douglas Gregor089459a2010-02-08 21:09:39 +00003056 // Try to find a variable in our own ("to") context with the same name and
3057 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00003058 if (D->isFileVarDecl()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003059 VarDecl *MergeWithVar = nullptr;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003060 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00003061 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003062 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003063 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003064 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3065 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00003066 continue;
3067
Douglas Gregorb75a3452011-10-15 00:10:27 +00003068 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00003069 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola181e3ec2013-05-13 00:12:11 +00003070 if (FoundVar->hasExternalFormalLinkage() &&
3071 D->hasExternalFormalLinkage()) {
Douglas Gregorea35d112010-02-15 23:54:17 +00003072 if (Importer.IsStructurallyEquivalent(D->getType(),
3073 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00003074 MergeWithVar = FoundVar;
3075 break;
3076 }
3077
Douglas Gregord0145422010-02-12 17:23:39 +00003078 const ArrayType *FoundArray
3079 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3080 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00003081 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00003082 if (FoundArray && TArray) {
3083 if (isa<IncompleteArrayType>(FoundArray) &&
3084 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00003085 // Import the type.
3086 QualType T = Importer.Import(D->getType());
3087 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003088 return nullptr;
3089
Douglas Gregord0145422010-02-12 17:23:39 +00003090 FoundVar->setType(T);
3091 MergeWithVar = FoundVar;
3092 break;
3093 } else if (isa<IncompleteArrayType>(TArray) &&
3094 isa<ConstantArrayType>(FoundArray)) {
3095 MergeWithVar = FoundVar;
3096 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00003097 }
3098 }
3099
Douglas Gregor089459a2010-02-08 21:09:39 +00003100 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00003101 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00003102 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3103 << FoundVar->getType();
3104 }
3105 }
3106
Douglas Gregorb75a3452011-10-15 00:10:27 +00003107 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00003108 }
3109
3110 if (MergeWithVar) {
3111 // An equivalent variable with external linkage has been found. Link
3112 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003113 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00003114
3115 if (VarDecl *DDef = D->getDefinition()) {
3116 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3117 Importer.ToDiag(ExistingDef->getLocation(),
3118 diag::err_odr_variable_multiple_def)
3119 << Name;
3120 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3121 } else {
3122 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00003123 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00003124 if (DDef->isInitKnownICE()) {
3125 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3126 Eval->CheckedICE = true;
3127 Eval->IsICE = DDef->isInitICE();
3128 }
Douglas Gregor089459a2010-02-08 21:09:39 +00003129 }
3130 }
3131
3132 return MergeWithVar;
3133 }
3134
3135 if (!ConflictingDecls.empty()) {
3136 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3137 ConflictingDecls.data(),
3138 ConflictingDecls.size());
3139 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003140 return nullptr;
Douglas Gregor089459a2010-02-08 21:09:39 +00003141 }
3142 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003143
Douglas Gregorea35d112010-02-15 23:54:17 +00003144 // Import the type.
3145 QualType T = Importer.Import(D->getType());
3146 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003147 return nullptr;
3148
Douglas Gregor089459a2010-02-08 21:09:39 +00003149 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003150 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003151 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3152 Importer.Import(D->getInnerLocStart()),
3153 Loc, Name.getAsIdentifierInfo(),
3154 T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003155 D->getStorageClass());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003156 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00003157 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003158 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003159 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00003160 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00003161
Douglas Gregor089459a2010-02-08 21:09:39 +00003162 // Merge the initializer.
Larisse Voufoef4579c2013-08-06 01:03:05 +00003163 if (ImportDefinition(D, ToVar))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003164 return nullptr;
Douglas Gregor089459a2010-02-08 21:09:39 +00003165
Douglas Gregor089459a2010-02-08 21:09:39 +00003166 return ToVar;
3167}
3168
Douglas Gregor2cd00932010-02-17 21:22:52 +00003169Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3170 // Parameters are created in the translation unit's context, then moved
3171 // into the function declaration's context afterward.
3172 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3173
3174 // Import the name of this declaration.
3175 DeclarationName Name = Importer.Import(D->getDeclName());
3176 if (D->getDeclName() && !Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003177 return nullptr;
3178
Douglas Gregor2cd00932010-02-17 21:22:52 +00003179 // Import the location of this declaration.
3180 SourceLocation Loc = Importer.Import(D->getLocation());
3181
3182 // Import the parameter's type.
3183 QualType T = Importer.Import(D->getType());
3184 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003185 return nullptr;
3186
Douglas Gregor2cd00932010-02-17 21:22:52 +00003187 // Create the imported parameter.
3188 ImplicitParamDecl *ToParm
3189 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3190 Loc, Name.getAsIdentifierInfo(),
3191 T);
3192 return Importer.Imported(D, ToParm);
3193}
3194
Douglas Gregora404ea62010-02-10 19:54:31 +00003195Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3196 // Parameters are created in the translation unit's context, then moved
3197 // into the function declaration's context afterward.
3198 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3199
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003200 // Import the name of this declaration.
3201 DeclarationName Name = Importer.Import(D->getDeclName());
3202 if (D->getDeclName() && !Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003203 return nullptr;
3204
Douglas Gregora404ea62010-02-10 19:54:31 +00003205 // Import the location of this declaration.
3206 SourceLocation Loc = Importer.Import(D->getLocation());
3207
3208 // Import the parameter's type.
3209 QualType T = Importer.Import(D->getType());
3210 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003211 return nullptr;
3212
Douglas Gregora404ea62010-02-10 19:54:31 +00003213 // Create the imported parameter.
3214 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3215 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003216 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00003217 Loc, Name.getAsIdentifierInfo(),
3218 T, TInfo, D->getStorageClass(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003219 /*FIXME: Default argument*/nullptr);
John McCallbf73b352010-03-12 18:31:32 +00003220 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003221 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00003222}
3223
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003224Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3225 // Import the major distinguishing characteristics of a method.
3226 DeclContext *DC, *LexicalDC;
3227 DeclarationName Name;
3228 SourceLocation Loc;
3229 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003230 return nullptr;
3231
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003232 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003233 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003234 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3235 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003236 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3237 continue;
3238
3239 // Check return types.
Stephen Hines651f13c2014-04-23 16:59:28 -07003240 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3241 FoundMethod->getReturnType())) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003242 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Stephen Hines651f13c2014-04-23 16:59:28 -07003243 << D->isInstanceMethod() << Name << D->getReturnType()
3244 << FoundMethod->getReturnType();
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003245 Importer.ToDiag(FoundMethod->getLocation(),
3246 diag::note_odr_objc_method_here)
3247 << D->isInstanceMethod() << Name;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003248 return nullptr;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003249 }
3250
3251 // Check the number of parameters.
3252 if (D->param_size() != FoundMethod->param_size()) {
3253 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3254 << D->isInstanceMethod() << Name
3255 << D->param_size() << FoundMethod->param_size();
3256 Importer.ToDiag(FoundMethod->getLocation(),
3257 diag::note_odr_objc_method_here)
3258 << D->isInstanceMethod() << Name;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003259 return nullptr;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003260 }
3261
3262 // Check parameter types.
3263 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3264 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3265 P != PEnd; ++P, ++FoundP) {
3266 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3267 (*FoundP)->getType())) {
3268 Importer.FromDiag((*P)->getLocation(),
3269 diag::err_odr_objc_method_param_type_inconsistent)
3270 << D->isInstanceMethod() << Name
3271 << (*P)->getType() << (*FoundP)->getType();
3272 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3273 << (*FoundP)->getType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003274 return nullptr;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003275 }
3276 }
3277
3278 // Check variadic/non-variadic.
3279 // Check the number of parameters.
3280 if (D->isVariadic() != FoundMethod->isVariadic()) {
3281 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3282 << D->isInstanceMethod() << Name;
3283 Importer.ToDiag(FoundMethod->getLocation(),
3284 diag::note_odr_objc_method_here)
3285 << D->isInstanceMethod() << Name;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003286 return nullptr;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003287 }
3288
3289 // FIXME: Any other bits we need to merge?
3290 return Importer.Imported(D, FoundMethod);
3291 }
3292 }
3293
3294 // Import the result type.
Stephen Hines651f13c2014-04-23 16:59:28 -07003295 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003296 if (ResultTy.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003297 return nullptr;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003298
Stephen Hines651f13c2014-04-23 16:59:28 -07003299 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003300
Stephen Hines651f13c2014-04-23 16:59:28 -07003301 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3302 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3303 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3304 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3305 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003306
3307 // FIXME: When we decide to merge method definitions, we'll need to
3308 // deal with implicit parameters.
3309
3310 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003311 SmallVector<ParmVarDecl *, 5> ToParams;
Stephen Hines651f13c2014-04-23 16:59:28 -07003312 for (auto *FromP : D->params()) {
3313 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003314 if (!ToP)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003315 return nullptr;
3316
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003317 ToParams.push_back(ToP);
3318 }
3319
3320 // Set the parameters.
3321 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3322 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003323 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003324 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003325 SmallVector<SourceLocation, 12> SelLocs;
3326 D->getSelectorLocs(SelLocs);
3327 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003328
3329 ToMethod->setLexicalDeclContext(LexicalDC);
3330 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003331 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003332 return ToMethod;
3333}
3334
Douglas Gregorb4677b62010-02-18 01:47:50 +00003335Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3336 // Import the major distinguishing characteristics of a category.
3337 DeclContext *DC, *LexicalDC;
3338 DeclarationName Name;
3339 SourceLocation Loc;
3340 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003341 return nullptr;
3342
Douglas Gregorb4677b62010-02-18 01:47:50 +00003343 ObjCInterfaceDecl *ToInterface
3344 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3345 if (!ToInterface)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003346 return nullptr;
3347
Douglas Gregorb4677b62010-02-18 01:47:50 +00003348 // Determine if we've already encountered this category.
3349 ObjCCategoryDecl *MergeWithCategory
3350 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3351 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3352 if (!ToCategory) {
3353 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003354 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003355 Loc,
3356 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003357 Name.getAsIdentifierInfo(),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003358 ToInterface,
3359 Importer.Import(D->getIvarLBraceLoc()),
3360 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003361 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003362 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003363 Importer.Imported(D, ToCategory);
3364
Douglas Gregorb4677b62010-02-18 01:47:50 +00003365 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003366 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3367 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003368 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3369 = D->protocol_loc_begin();
3370 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3371 FromProtoEnd = D->protocol_end();
3372 FromProto != FromProtoEnd;
3373 ++FromProto, ++FromProtoLoc) {
3374 ObjCProtocolDecl *ToProto
3375 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3376 if (!ToProto)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003377 return nullptr;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003378 Protocols.push_back(ToProto);
3379 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3380 }
3381
3382 // FIXME: If we're merging, make sure that the protocol list is the same.
3383 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3384 ProtocolLocs.data(), Importer.getToContext());
3385
3386 } else {
3387 Importer.Imported(D, ToCategory);
3388 }
3389
3390 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003391 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003392
3393 // If we have an implementation, import it as well.
3394 if (D->getImplementation()) {
3395 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003396 = cast_or_null<ObjCCategoryImplDecl>(
3397 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003398 if (!Impl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003399 return nullptr;
3400
Douglas Gregorb4677b62010-02-18 01:47:50 +00003401 ToCategory->setImplementation(Impl);
3402 }
3403
3404 return ToCategory;
3405}
3406
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003407bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3408 ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003409 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003410 if (To->getDefinition()) {
Douglas Gregorac32ff92012-02-01 21:00:38 +00003411 if (shouldForceImportDeclContext(Kind))
3412 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003413 return false;
3414 }
3415
3416 // Start the protocol definition
3417 To->startDefinition();
3418
3419 // Import protocols
3420 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3421 SmallVector<SourceLocation, 4> ProtocolLocs;
3422 ObjCProtocolDecl::protocol_loc_iterator
3423 FromProtoLoc = From->protocol_loc_begin();
3424 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3425 FromProtoEnd = From->protocol_end();
3426 FromProto != FromProtoEnd;
3427 ++FromProto, ++FromProtoLoc) {
3428 ObjCProtocolDecl *ToProto
3429 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3430 if (!ToProto)
3431 return true;
3432 Protocols.push_back(ToProto);
3433 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3434 }
3435
3436 // FIXME: If we're merging, make sure that the protocol list is the same.
3437 To->setProtocolList(Protocols.data(), Protocols.size(),
3438 ProtocolLocs.data(), Importer.getToContext());
3439
Douglas Gregorac32ff92012-02-01 21:00:38 +00003440 if (shouldForceImportDeclContext(Kind)) {
3441 // Import all of the members of this protocol.
3442 ImportDeclContext(From, /*ForceImport=*/true);
3443 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003444 return false;
3445}
3446
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003447Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003448 // If this protocol has a definition in the translation unit we're coming
3449 // from, but this particular declaration is not that definition, import the
3450 // definition and map to that.
3451 ObjCProtocolDecl *Definition = D->getDefinition();
3452 if (Definition && Definition != D) {
3453 Decl *ImportedDef = Importer.Import(Definition);
3454 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003455 return nullptr;
3456
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003457 return Importer.Imported(D, ImportedDef);
3458 }
3459
Douglas Gregorb4677b62010-02-18 01:47:50 +00003460 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003461 DeclContext *DC, *LexicalDC;
3462 DeclarationName Name;
3463 SourceLocation Loc;
3464 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003465 return nullptr;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003466
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003467 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003468 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003469 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003470 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3471 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003472 continue;
3473
Douglas Gregorb75a3452011-10-15 00:10:27 +00003474 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003475 break;
3476 }
3477
3478 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003479 if (!ToProto) {
3480 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3481 Name.getAsIdentifierInfo(), Loc,
3482 Importer.Import(D->getAtStartLoc()),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003483 /*PrevDecl=*/nullptr);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003484 ToProto->setLexicalDeclContext(LexicalDC);
3485 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003486 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003487
3488 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003489
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003490 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003491 return nullptr;
3492
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003493 return ToProto;
3494}
3495
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003496Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3497 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3498 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3499
3500 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3501 SourceLocation LangLoc = Importer.Import(D->getLocation());
3502
3503 bool HasBraces = D->hasBraces();
3504
3505 LinkageSpecDecl *ToLinkageSpec =
3506 LinkageSpecDecl::Create(Importer.getToContext(),
3507 DC,
3508 ExternLoc,
3509 LangLoc,
3510 D->getLanguage(),
3511 HasBraces);
3512
3513 if (HasBraces) {
3514 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3515 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3516 }
3517
3518 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3519 LexicalDC->addDeclInternal(ToLinkageSpec);
3520
3521 Importer.Imported(D, ToLinkageSpec);
3522
3523 return ToLinkageSpec;
3524}
3525
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003526bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3527 ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003528 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003529 if (To->getDefinition()) {
3530 // Check consistency of superclass.
3531 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3532 if (FromSuper) {
3533 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3534 if (!FromSuper)
3535 return true;
3536 }
3537
3538 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3539 if ((bool)FromSuper != (bool)ToSuper ||
3540 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3541 Importer.ToDiag(To->getLocation(),
3542 diag::err_odr_objc_superclass_inconsistent)
3543 << To->getDeclName();
3544 if (ToSuper)
3545 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3546 << To->getSuperClass()->getDeclName();
3547 else
3548 Importer.ToDiag(To->getLocation(),
3549 diag::note_odr_objc_missing_superclass);
3550 if (From->getSuperClass())
3551 Importer.FromDiag(From->getSuperClassLoc(),
3552 diag::note_odr_objc_superclass)
3553 << From->getSuperClass()->getDeclName();
3554 else
3555 Importer.FromDiag(From->getLocation(),
3556 diag::note_odr_objc_missing_superclass);
3557 }
3558
Douglas Gregorac32ff92012-02-01 21:00:38 +00003559 if (shouldForceImportDeclContext(Kind))
3560 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003561 return false;
3562 }
3563
3564 // Start the definition.
3565 To->startDefinition();
3566
3567 // If this class has a superclass, import it.
3568 if (From->getSuperClass()) {
3569 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3570 Importer.Import(From->getSuperClass()));
3571 if (!Super)
3572 return true;
3573
3574 To->setSuperClass(Super);
3575 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3576 }
3577
3578 // Import protocols
3579 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3580 SmallVector<SourceLocation, 4> ProtocolLocs;
3581 ObjCInterfaceDecl::protocol_loc_iterator
3582 FromProtoLoc = From->protocol_loc_begin();
3583
3584 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3585 FromProtoEnd = From->protocol_end();
3586 FromProto != FromProtoEnd;
3587 ++FromProto, ++FromProtoLoc) {
3588 ObjCProtocolDecl *ToProto
3589 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3590 if (!ToProto)
3591 return true;
3592 Protocols.push_back(ToProto);
3593 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3594 }
3595
3596 // FIXME: If we're merging, make sure that the protocol list is the same.
3597 To->setProtocolList(Protocols.data(), Protocols.size(),
3598 ProtocolLocs.data(), Importer.getToContext());
3599
3600 // Import categories. When the categories themselves are imported, they'll
3601 // hook themselves into this interface.
Stephen Hines651f13c2014-04-23 16:59:28 -07003602 for (auto *Cat : From->known_categories())
3603 Importer.Import(Cat);
Douglas Gregord3297242013-01-16 23:00:23 +00003604
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003605 // If we have an @implementation, import it as well.
3606 if (From->getImplementation()) {
3607 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3608 Importer.Import(From->getImplementation()));
3609 if (!Impl)
3610 return true;
3611
3612 To->setImplementation(Impl);
3613 }
3614
Douglas Gregorac32ff92012-02-01 21:00:38 +00003615 if (shouldForceImportDeclContext(Kind)) {
3616 // Import all of the members of this class.
3617 ImportDeclContext(From, /*ForceImport=*/true);
3618 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003619 return false;
3620}
3621
Douglas Gregora12d2942010-02-16 01:20:57 +00003622Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003623 // If this class has a definition in the translation unit we're coming from,
3624 // but this particular declaration is not that definition, import the
3625 // definition and map to that.
3626 ObjCInterfaceDecl *Definition = D->getDefinition();
3627 if (Definition && Definition != D) {
3628 Decl *ImportedDef = Importer.Import(Definition);
3629 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003630 return nullptr;
3631
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003632 return Importer.Imported(D, ImportedDef);
3633 }
3634
Douglas Gregora12d2942010-02-16 01:20:57 +00003635 // Import the major distinguishing characteristics of an @interface.
3636 DeclContext *DC, *LexicalDC;
3637 DeclarationName Name;
3638 SourceLocation Loc;
3639 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003640 return nullptr;
Douglas Gregora12d2942010-02-16 01:20:57 +00003641
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003642 // Look for an existing interface with the same name.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003643 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003644 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003645 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003646 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3647 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003648 continue;
3649
Douglas Gregorb75a3452011-10-15 00:10:27 +00003650 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003651 break;
3652 }
3653
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003654 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003655 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003656 if (!ToIface) {
3657 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3658 Importer.Import(D->getAtStartLoc()),
3659 Name.getAsIdentifierInfo(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003660 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003661 D->isImplicitInterfaceDecl());
3662 ToIface->setLexicalDeclContext(LexicalDC);
3663 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003664 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003665 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003666
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003667 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003668 return nullptr;
3669
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003670 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003671}
3672
Douglas Gregor3daef292010-12-07 15:32:12 +00003673Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3674 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3675 Importer.Import(D->getCategoryDecl()));
3676 if (!Category)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003677 return nullptr;
3678
Douglas Gregor3daef292010-12-07 15:32:12 +00003679 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3680 if (!ToImpl) {
3681 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3682 if (!DC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003683 return nullptr;
3684
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003685 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003686 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003687 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003688 Category->getClassInterface(),
3689 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003690 Importer.Import(D->getAtStartLoc()),
3691 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003692
3693 DeclContext *LexicalDC = DC;
3694 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3695 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3696 if (!LexicalDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003697 return nullptr;
3698
Douglas Gregor3daef292010-12-07 15:32:12 +00003699 ToImpl->setLexicalDeclContext(LexicalDC);
3700 }
3701
Sean Callanan9faf8102011-10-21 02:57:43 +00003702 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003703 Category->setImplementation(ToImpl);
3704 }
3705
3706 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003707 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003708 return ToImpl;
3709}
3710
Douglas Gregordd182ff2010-12-07 01:26:03 +00003711Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3712 // Find the corresponding interface.
3713 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3714 Importer.Import(D->getClassInterface()));
3715 if (!Iface)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003716 return nullptr;
Douglas Gregordd182ff2010-12-07 01:26:03 +00003717
3718 // Import the superclass, if any.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003719 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregordd182ff2010-12-07 01:26:03 +00003720 if (D->getSuperClass()) {
3721 Super = cast_or_null<ObjCInterfaceDecl>(
3722 Importer.Import(D->getSuperClass()));
3723 if (!Super)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003724 return nullptr;
Douglas Gregordd182ff2010-12-07 01:26:03 +00003725 }
3726
3727 ObjCImplementationDecl *Impl = Iface->getImplementation();
3728 if (!Impl) {
3729 // We haven't imported an implementation yet. Create a new @implementation
3730 // now.
3731 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3732 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003733 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003734 Importer.Import(D->getLocation()),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003735 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis2f729002013-05-03 22:31:26 +00003736 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003737 Importer.Import(D->getIvarLBraceLoc()),
3738 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003739
3740 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3741 DeclContext *LexicalDC
3742 = Importer.ImportContext(D->getLexicalDeclContext());
3743 if (!LexicalDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003744 return nullptr;
Douglas Gregordd182ff2010-12-07 01:26:03 +00003745 Impl->setLexicalDeclContext(LexicalDC);
3746 }
3747
3748 // Associate the implementation with the class it implements.
3749 Iface->setImplementation(Impl);
3750 Importer.Imported(D, Iface->getImplementation());
3751 } else {
3752 Importer.Imported(D, Iface->getImplementation());
3753
3754 // Verify that the existing @implementation has the same superclass.
3755 if ((Super && !Impl->getSuperClass()) ||
3756 (!Super && Impl->getSuperClass()) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003757 (Super && Impl->getSuperClass() &&
3758 !declaresSameEntity(Super->getCanonicalDecl(),
3759 Impl->getSuperClass()))) {
3760 Importer.ToDiag(Impl->getLocation(),
3761 diag::err_odr_objc_superclass_inconsistent)
3762 << Iface->getDeclName();
3763 // FIXME: It would be nice to have the location of the superclass
3764 // below.
3765 if (Impl->getSuperClass())
3766 Importer.ToDiag(Impl->getLocation(),
3767 diag::note_odr_objc_superclass)
3768 << Impl->getSuperClass()->getDeclName();
3769 else
3770 Importer.ToDiag(Impl->getLocation(),
3771 diag::note_odr_objc_missing_superclass);
3772 if (D->getSuperClass())
3773 Importer.FromDiag(D->getLocation(),
Douglas Gregordd182ff2010-12-07 01:26:03 +00003774 diag::note_odr_objc_superclass)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003775 << D->getSuperClass()->getDeclName();
3776 else
3777 Importer.FromDiag(D->getLocation(),
Douglas Gregordd182ff2010-12-07 01:26:03 +00003778 diag::note_odr_objc_missing_superclass);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003779 return nullptr;
Douglas Gregordd182ff2010-12-07 01:26:03 +00003780 }
3781 }
3782
3783 // Import all of the members of this @implementation.
3784 ImportDeclContext(D);
3785
3786 return Impl;
3787}
3788
Douglas Gregore3261622010-02-17 18:02:10 +00003789Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3790 // Import the major distinguishing characteristics of an @property.
3791 DeclContext *DC, *LexicalDC;
3792 DeclarationName Name;
3793 SourceLocation Loc;
3794 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003795 return nullptr;
Douglas Gregore3261622010-02-17 18:02:10 +00003796
3797 // Check whether we have already imported this property.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003798 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003799 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00003800 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003801 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003802 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003803 // Check property types.
3804 if (!Importer.IsStructurallyEquivalent(D->getType(),
3805 FoundProp->getType())) {
3806 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3807 << Name << D->getType() << FoundProp->getType();
3808 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3809 << FoundProp->getType();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003810 return nullptr;
Douglas Gregore3261622010-02-17 18:02:10 +00003811 }
3812
3813 // FIXME: Check property attributes, getters, setters, etc.?
3814
3815 // Consider these properties to be equivalent.
3816 Importer.Imported(D, FoundProp);
3817 return FoundProp;
3818 }
3819 }
3820
3821 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003822 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3823 if (!T)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003824 return nullptr;
Douglas Gregore3261622010-02-17 18:02:10 +00003825
3826 // Create the new property.
3827 ObjCPropertyDecl *ToProperty
3828 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3829 Name.getAsIdentifierInfo(),
3830 Importer.Import(D->getAtLoc()),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00003831 Importer.Import(D->getLParenLoc()),
Douglas Gregore3261622010-02-17 18:02:10 +00003832 T,
3833 D->getPropertyImplementation());
3834 Importer.Imported(D, ToProperty);
3835 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003836 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003837
3838 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003839 ToProperty->setPropertyAttributesAsWritten(
3840 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003841 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3842 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3843 ToProperty->setGetterMethodDecl(
3844 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3845 ToProperty->setSetterMethodDecl(
3846 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3847 ToProperty->setPropertyIvarDecl(
3848 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3849 return ToProperty;
3850}
3851
Douglas Gregor954e0c72010-12-07 18:32:03 +00003852Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3853 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3854 Importer.Import(D->getPropertyDecl()));
3855 if (!Property)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003856 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003857
3858 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3859 if (!DC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003860 return nullptr;
3861
Douglas Gregor954e0c72010-12-07 18:32:03 +00003862 // Import the lexical declaration context.
3863 DeclContext *LexicalDC = DC;
3864 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3865 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3866 if (!LexicalDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003867 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003868 }
3869
3870 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3871 if (!InImpl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003872 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003873
3874 // Import the ivar (for an @synthesize).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003875 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003876 if (D->getPropertyIvarDecl()) {
3877 Ivar = cast_or_null<ObjCIvarDecl>(
3878 Importer.Import(D->getPropertyIvarDecl()));
3879 if (!Ivar)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003880 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003881 }
3882
3883 ObjCPropertyImplDecl *ToImpl
3884 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3885 if (!ToImpl) {
3886 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3887 Importer.Import(D->getLocStart()),
3888 Importer.Import(D->getLocation()),
3889 Property,
3890 D->getPropertyImplementation(),
3891 Ivar,
3892 Importer.Import(D->getPropertyIvarDeclLoc()));
3893 ToImpl->setLexicalDeclContext(LexicalDC);
3894 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003895 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003896 } else {
3897 // Check that we have the same kind of property implementation (@synthesize
3898 // vs. @dynamic).
3899 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3900 Importer.ToDiag(ToImpl->getLocation(),
3901 diag::err_odr_objc_property_impl_kind_inconsistent)
3902 << Property->getDeclName()
3903 << (ToImpl->getPropertyImplementation()
3904 == ObjCPropertyImplDecl::Dynamic);
3905 Importer.FromDiag(D->getLocation(),
3906 diag::note_odr_objc_property_impl_kind)
3907 << D->getPropertyDecl()->getDeclName()
3908 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003909 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003910 }
3911
3912 // For @synthesize, check that we have the same
3913 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3914 Ivar != ToImpl->getPropertyIvarDecl()) {
3915 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3916 diag::err_odr_objc_synthesize_ivar_inconsistent)
3917 << Property->getDeclName()
3918 << ToImpl->getPropertyIvarDecl()->getDeclName()
3919 << Ivar->getDeclName();
3920 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3921 diag::note_odr_objc_synthesize_ivar_here)
3922 << D->getPropertyIvarDecl()->getDeclName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003923 return nullptr;
Douglas Gregor954e0c72010-12-07 18:32:03 +00003924 }
3925
3926 // Merge the existing implementation with the new implementation.
3927 Importer.Imported(D, ToImpl);
3928 }
3929
3930 return ToImpl;
3931}
3932
Douglas Gregor040afae2010-11-30 19:14:50 +00003933Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3934 // For template arguments, we adopt the translation unit as our declaration
3935 // context. This context will be fixed when the actual template declaration
3936 // is created.
3937
3938 // FIXME: Import default argument.
3939 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3940 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003941 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003942 Importer.Import(D->getLocation()),
3943 D->getDepth(),
3944 D->getIndex(),
3945 Importer.Import(D->getIdentifier()),
3946 D->wasDeclaredWithTypename(),
3947 D->isParameterPack());
3948}
3949
3950Decl *
3951ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3952 // Import the name of this declaration.
3953 DeclarationName Name = Importer.Import(D->getDeclName());
3954 if (D->getDeclName() && !Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003955 return nullptr;
3956
Douglas Gregor040afae2010-11-30 19:14:50 +00003957 // Import the location of this declaration.
3958 SourceLocation Loc = Importer.Import(D->getLocation());
3959
3960 // Import the type of this declaration.
3961 QualType T = Importer.Import(D->getType());
3962 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003963 return nullptr;
3964
Douglas Gregor040afae2010-11-30 19:14:50 +00003965 // Import type-source information.
3966 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3967 if (D->getTypeSourceInfo() && !TInfo)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003968 return nullptr;
3969
Douglas Gregor040afae2010-11-30 19:14:50 +00003970 // FIXME: Import default argument.
3971
3972 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3973 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003974 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003975 Loc, D->getDepth(), D->getPosition(),
3976 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003977 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003978}
3979
3980Decl *
3981ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3982 // Import the name of this declaration.
3983 DeclarationName Name = Importer.Import(D->getDeclName());
3984 if (D->getDeclName() && !Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003985 return nullptr;
3986
Douglas Gregor040afae2010-11-30 19:14:50 +00003987 // Import the location of this declaration.
3988 SourceLocation Loc = Importer.Import(D->getLocation());
3989
3990 // Import template parameters.
3991 TemplateParameterList *TemplateParams
3992 = ImportTemplateParameterList(D->getTemplateParameters());
3993 if (!TemplateParams)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003994 return nullptr;
3995
Douglas Gregor040afae2010-11-30 19:14:50 +00003996 // FIXME: Import default argument.
3997
3998 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3999 Importer.getToContext().getTranslationUnitDecl(),
4000 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00004001 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00004002 Name.getAsIdentifierInfo(),
4003 TemplateParams);
4004}
4005
4006Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4007 // If this record has a definition in the translation unit we're coming from,
4008 // but this particular declaration is not that definition, import the
4009 // definition and map to that.
4010 CXXRecordDecl *Definition
4011 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4012 if (Definition && Definition != D->getTemplatedDecl()) {
4013 Decl *ImportedDef
4014 = Importer.Import(Definition->getDescribedClassTemplate());
4015 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004016 return nullptr;
4017
Douglas Gregor040afae2010-11-30 19:14:50 +00004018 return Importer.Imported(D, ImportedDef);
4019 }
4020
4021 // Import the major distinguishing characteristics of this class template.
4022 DeclContext *DC, *LexicalDC;
4023 DeclarationName Name;
4024 SourceLocation Loc;
4025 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004026 return nullptr;
4027
Douglas Gregor040afae2010-11-30 19:14:50 +00004028 // We may already have a template of the same name; try to find and match it.
4029 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00004030 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004031 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004032 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregorb75a3452011-10-15 00:10:27 +00004033 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4034 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00004035 continue;
4036
Douglas Gregorb75a3452011-10-15 00:10:27 +00004037 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00004038 if (ClassTemplateDecl *FoundTemplate
4039 = dyn_cast<ClassTemplateDecl>(Found)) {
4040 if (IsStructuralMatch(D, FoundTemplate)) {
4041 // The class templates structurally match; call it the same template.
4042 // FIXME: We may be filling in a forward declaration here. Handle
4043 // this case!
4044 Importer.Imported(D->getTemplatedDecl(),
4045 FoundTemplate->getTemplatedDecl());
4046 return Importer.Imported(D, FoundTemplate);
4047 }
4048 }
4049
Douglas Gregorb75a3452011-10-15 00:10:27 +00004050 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00004051 }
4052
4053 if (!ConflictingDecls.empty()) {
4054 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4055 ConflictingDecls.data(),
4056 ConflictingDecls.size());
4057 }
4058
4059 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004060 return nullptr;
Douglas Gregor040afae2010-11-30 19:14:50 +00004061 }
4062
4063 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4064
4065 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004066 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4067 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00004068 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4069 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004070 DC, StartLoc, IdLoc,
4071 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00004072 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004073 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00004074 D2Templated->setLexicalDeclContext(LexicalDC);
4075
4076 // Create the class template declaration itself.
4077 TemplateParameterList *TemplateParams
4078 = ImportTemplateParameterList(D->getTemplateParameters());
4079 if (!TemplateParams)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004080 return nullptr;
4081
Douglas Gregor040afae2010-11-30 19:14:50 +00004082 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4083 Loc, Name, TemplateParams,
4084 D2Templated,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004085 /*PrevDecl=*/nullptr);
Douglas Gregor040afae2010-11-30 19:14:50 +00004086 D2Templated->setDescribedClassTemplate(D2);
4087
4088 D2->setAccess(D->getAccess());
4089 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00004090 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00004091
4092 // Note the relationship between the class templates.
4093 Importer.Imported(D, D2);
4094 Importer.Imported(DTemplated, D2Templated);
4095
John McCall5e1cdac2011-10-07 06:10:15 +00004096 if (DTemplated->isCompleteDefinition() &&
4097 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00004098 // FIXME: Import definition!
4099 }
4100
4101 return D2;
4102}
4103
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004104Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4105 ClassTemplateSpecializationDecl *D) {
4106 // If this record has a definition in the translation unit we're coming from,
4107 // but this particular declaration is not that definition, import the
4108 // definition and map to that.
4109 TagDecl *Definition = D->getDefinition();
4110 if (Definition && Definition != D) {
4111 Decl *ImportedDef = Importer.Import(Definition);
4112 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004113 return nullptr;
4114
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004115 return Importer.Imported(D, ImportedDef);
4116 }
4117
4118 ClassTemplateDecl *ClassTemplate
4119 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4120 D->getSpecializedTemplate()));
4121 if (!ClassTemplate)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004122 return nullptr;
4123
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004124 // Import the context of this declaration.
4125 DeclContext *DC = ClassTemplate->getDeclContext();
4126 if (!DC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004127 return nullptr;
4128
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004129 DeclContext *LexicalDC = DC;
4130 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4131 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4132 if (!LexicalDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004133 return nullptr;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004134 }
4135
4136 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004137 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4138 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004139
4140 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004141 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004142 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4143 D->getTemplateArgs().size(),
4144 TemplateArgs))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004145 return nullptr;
4146
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004147 // Try to find an existing specialization with these template arguments.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004148 void *InsertPos = nullptr;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004149 ClassTemplateSpecializationDecl *D2
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004150 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004151 if (D2) {
4152 // We already have a class template specialization with these template
4153 // arguments.
4154
4155 // FIXME: Check for specialization vs. instantiation errors.
4156
4157 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00004158 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004159 // The record types structurally match, or the "from" translation
4160 // unit only had a forward declaration anyway; call it the same
4161 // function.
4162 return Importer.Imported(D, FoundDef);
4163 }
4164 }
4165 } else {
4166 // Create a new specialization.
4167 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4168 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004169 StartLoc, IdLoc,
4170 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004171 TemplateArgs.data(),
4172 TemplateArgs.size(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004173 /*PrevDecl=*/nullptr);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004174 D2->setSpecializationKind(D->getSpecializationKind());
4175
4176 // Add this specialization to the class template.
4177 ClassTemplate->AddSpecialization(D2, InsertPos);
4178
4179 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004180 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004181
4182 // Add the specialization to this context.
4183 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00004184 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004185 }
4186 Importer.Imported(D, D2);
4187
John McCall5e1cdac2011-10-07 06:10:15 +00004188 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004189 return nullptr;
4190
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004191 return D2;
4192}
4193
Larisse Voufoef4579c2013-08-06 01:03:05 +00004194Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4195 // If this variable has a definition in the translation unit we're coming
4196 // from,
4197 // but this particular declaration is not that definition, import the
4198 // definition and map to that.
4199 VarDecl *Definition =
4200 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4201 if (Definition && Definition != D->getTemplatedDecl()) {
4202 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4203 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004204 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004205
4206 return Importer.Imported(D, ImportedDef);
4207 }
4208
4209 // Import the major distinguishing characteristics of this variable template.
4210 DeclContext *DC, *LexicalDC;
4211 DeclarationName Name;
4212 SourceLocation Loc;
4213 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004214 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004215
4216 // We may already have a template of the same name; try to find and match it.
4217 assert(!DC->isFunctionOrMethod() &&
4218 "Variable templates cannot be declared at function scope");
4219 SmallVector<NamedDecl *, 4> ConflictingDecls;
4220 SmallVector<NamedDecl *, 2> FoundDecls;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004221 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufoef4579c2013-08-06 01:03:05 +00004222 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4223 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4224 continue;
4225
4226 Decl *Found = FoundDecls[I];
4227 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4228 if (IsStructuralMatch(D, FoundTemplate)) {
4229 // The variable templates structurally match; call it the same template.
4230 Importer.Imported(D->getTemplatedDecl(),
4231 FoundTemplate->getTemplatedDecl());
4232 return Importer.Imported(D, FoundTemplate);
4233 }
4234 }
4235
4236 ConflictingDecls.push_back(FoundDecls[I]);
4237 }
4238
4239 if (!ConflictingDecls.empty()) {
4240 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4241 ConflictingDecls.data(),
4242 ConflictingDecls.size());
4243 }
4244
4245 if (!Name)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004246 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004247
4248 VarDecl *DTemplated = D->getTemplatedDecl();
4249
4250 // Import the type.
4251 QualType T = Importer.Import(DTemplated->getType());
4252 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004253 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004254
4255 // Create the declaration that is being templated.
4256 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4257 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4258 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4259 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4260 IdLoc, Name.getAsIdentifierInfo(), T,
4261 TInfo, DTemplated->getStorageClass());
4262 D2Templated->setAccess(DTemplated->getAccess());
4263 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4264 D2Templated->setLexicalDeclContext(LexicalDC);
4265
4266 // Importer.Imported(DTemplated, D2Templated);
4267 // LexicalDC->addDeclInternal(D2Templated);
4268
4269 // Merge the initializer.
4270 if (ImportDefinition(DTemplated, D2Templated))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004271 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004272
4273 // Create the variable template declaration itself.
4274 TemplateParameterList *TemplateParams =
4275 ImportTemplateParameterList(D->getTemplateParameters());
4276 if (!TemplateParams)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004277 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004278
4279 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Stephen Hines651f13c2014-04-23 16:59:28 -07004280 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufoef4579c2013-08-06 01:03:05 +00004281 D2Templated->setDescribedVarTemplate(D2);
4282
4283 D2->setAccess(D->getAccess());
4284 D2->setLexicalDeclContext(LexicalDC);
4285 LexicalDC->addDeclInternal(D2);
4286
4287 // Note the relationship between the variable templates.
4288 Importer.Imported(D, D2);
4289 Importer.Imported(DTemplated, D2Templated);
4290
4291 if (DTemplated->isThisDeclarationADefinition() &&
4292 !D2Templated->isThisDeclarationADefinition()) {
4293 // FIXME: Import definition!
4294 }
4295
4296 return D2;
4297}
4298
4299Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4300 VarTemplateSpecializationDecl *D) {
4301 // If this record has a definition in the translation unit we're coming from,
4302 // but this particular declaration is not that definition, import the
4303 // definition and map to that.
4304 VarDecl *Definition = D->getDefinition();
4305 if (Definition && Definition != D) {
4306 Decl *ImportedDef = Importer.Import(Definition);
4307 if (!ImportedDef)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004308 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004309
4310 return Importer.Imported(D, ImportedDef);
4311 }
4312
4313 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4314 Importer.Import(D->getSpecializedTemplate()));
4315 if (!VarTemplate)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004316 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004317
4318 // Import the context of this declaration.
4319 DeclContext *DC = VarTemplate->getDeclContext();
4320 if (!DC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004321 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004322
4323 DeclContext *LexicalDC = DC;
4324 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4325 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4326 if (!LexicalDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004327 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004328 }
4329
4330 // Import the location of this declaration.
4331 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4332 SourceLocation IdLoc = Importer.Import(D->getLocation());
4333
4334 // Import template arguments.
4335 SmallVector<TemplateArgument, 2> TemplateArgs;
4336 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4337 D->getTemplateArgs().size(), TemplateArgs))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004338 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004339
4340 // Try to find an existing specialization with these template arguments.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004341 void *InsertPos = nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004342 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Stephen Hinesc568f1e2014-07-21 00:47:37 -07004343 TemplateArgs, InsertPos);
Larisse Voufoef4579c2013-08-06 01:03:05 +00004344 if (D2) {
4345 // We already have a variable template specialization with these template
4346 // arguments.
4347
4348 // FIXME: Check for specialization vs. instantiation errors.
4349
4350 if (VarDecl *FoundDef = D2->getDefinition()) {
4351 if (!D->isThisDeclarationADefinition() ||
4352 IsStructuralMatch(D, FoundDef)) {
4353 // The record types structurally match, or the "from" translation
4354 // unit only had a forward declaration anyway; call it the same
4355 // variable.
4356 return Importer.Imported(D, FoundDef);
4357 }
4358 }
4359 } else {
4360
4361 // Import the type.
4362 QualType T = Importer.Import(D->getType());
4363 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004364 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004365 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4366
4367 // Create a new specialization.
4368 D2 = VarTemplateSpecializationDecl::Create(
4369 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4370 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4371 D2->setSpecializationKind(D->getSpecializationKind());
4372 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4373
4374 // Add this specialization to the class template.
4375 VarTemplate->AddSpecialization(D2, InsertPos);
4376
4377 // Import the qualifier, if any.
4378 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4379
4380 // Add the specialization to this context.
4381 D2->setLexicalDeclContext(LexicalDC);
4382 LexicalDC->addDeclInternal(D2);
4383 }
4384 Importer.Imported(D, D2);
4385
4386 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004387 return nullptr;
Larisse Voufoef4579c2013-08-06 01:03:05 +00004388
4389 return D2;
4390}
4391
Douglas Gregor4800d952010-02-11 19:21:55 +00004392//----------------------------------------------------------------------------
4393// Import Statements
4394//----------------------------------------------------------------------------
4395
4396Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4397 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4398 << S->getStmtClassName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004399 return nullptr;
Douglas Gregor4800d952010-02-11 19:21:55 +00004400}
4401
4402//----------------------------------------------------------------------------
4403// Import Expressions
4404//----------------------------------------------------------------------------
4405Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4406 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4407 << E->getStmtClassName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004408 return nullptr;
Douglas Gregor4800d952010-02-11 19:21:55 +00004409}
4410
Douglas Gregor44080632010-02-19 01:17:02 +00004411Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00004412 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4413 if (!ToD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004414 return nullptr;
Chandler Carruth3aa81402011-05-01 23:48:14 +00004415
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004416 NamedDecl *FoundD = nullptr;
Chandler Carruth3aa81402011-05-01 23:48:14 +00004417 if (E->getDecl() != E->getFoundDecl()) {
4418 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4419 if (!FoundD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004420 return nullptr;
Chandler Carruth3aa81402011-05-01 23:48:14 +00004421 }
Douglas Gregor44080632010-02-19 01:17:02 +00004422
4423 QualType T = Importer.Import(E->getType());
4424 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004425 return nullptr;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00004426
4427 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4428 Importer.Import(E->getQualifierLoc()),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004429 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00004430 ToD,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004431 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00004432 Importer.Import(E->getLocation()),
4433 T, E->getValueKind(),
4434 FoundD,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004435 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00004436 if (E->hadMultipleCandidates())
4437 DRE->setHadMultipleCandidates(true);
4438 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00004439}
4440
Douglas Gregor4800d952010-02-11 19:21:55 +00004441Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4442 QualType T = Importer.Import(E->getType());
4443 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004444 return nullptr;
Douglas Gregor4800d952010-02-11 19:21:55 +00004445
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00004446 return IntegerLiteral::Create(Importer.getToContext(),
4447 E->getValue(), T,
4448 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00004449}
4450
Douglas Gregorb2e400a2010-02-18 02:21:22 +00004451Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4452 QualType T = Importer.Import(E->getType());
4453 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004454 return nullptr;
4455
Douglas Gregor5cee1192011-07-27 05:40:30 +00004456 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4457 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00004458 Importer.Import(E->getLocation()));
4459}
4460
Douglas Gregorf638f952010-02-19 01:07:06 +00004461Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4462 Expr *SubExpr = Importer.Import(E->getSubExpr());
4463 if (!SubExpr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004464 return nullptr;
4465
Douglas Gregorf638f952010-02-19 01:07:06 +00004466 return new (Importer.getToContext())
4467 ParenExpr(Importer.Import(E->getLParen()),
4468 Importer.Import(E->getRParen()),
4469 SubExpr);
4470}
4471
4472Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4473 QualType T = Importer.Import(E->getType());
4474 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004475 return nullptr;
Douglas Gregorf638f952010-02-19 01:07:06 +00004476
4477 Expr *SubExpr = Importer.Import(E->getSubExpr());
4478 if (!SubExpr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004479 return nullptr;
4480
Douglas Gregorf638f952010-02-19 01:07:06 +00004481 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004482 T, E->getValueKind(),
4483 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004484 Importer.Import(E->getOperatorLoc()));
4485}
4486
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004487Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4488 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00004489 QualType ResultType = Importer.Import(E->getType());
4490
4491 if (E->isArgumentType()) {
4492 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4493 if (!TInfo)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004494 return nullptr;
4495
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004496 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4497 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004498 Importer.Import(E->getOperatorLoc()),
4499 Importer.Import(E->getRParenLoc()));
4500 }
4501
4502 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4503 if (!SubExpr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004504 return nullptr;
4505
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004506 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4507 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004508 Importer.Import(E->getOperatorLoc()),
4509 Importer.Import(E->getRParenLoc()));
4510}
4511
Douglas Gregorf638f952010-02-19 01:07:06 +00004512Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4513 QualType T = Importer.Import(E->getType());
4514 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004515 return nullptr;
Douglas Gregorf638f952010-02-19 01:07:06 +00004516
4517 Expr *LHS = Importer.Import(E->getLHS());
4518 if (!LHS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004519 return nullptr;
4520
Douglas Gregorf638f952010-02-19 01:07:06 +00004521 Expr *RHS = Importer.Import(E->getRHS());
4522 if (!RHS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004523 return nullptr;
4524
Douglas Gregorf638f952010-02-19 01:07:06 +00004525 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004526 T, E->getValueKind(),
4527 E->getObjectKind(),
Lang Hamesbe9af122012-10-02 04:45:10 +00004528 Importer.Import(E->getOperatorLoc()),
4529 E->isFPContractable());
Douglas Gregorf638f952010-02-19 01:07:06 +00004530}
4531
4532Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4533 QualType T = Importer.Import(E->getType());
4534 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004535 return nullptr;
4536
Douglas Gregorf638f952010-02-19 01:07:06 +00004537 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4538 if (CompLHSType.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004539 return nullptr;
4540
Douglas Gregorf638f952010-02-19 01:07:06 +00004541 QualType CompResultType = Importer.Import(E->getComputationResultType());
4542 if (CompResultType.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004543 return nullptr;
4544
Douglas Gregorf638f952010-02-19 01:07:06 +00004545 Expr *LHS = Importer.Import(E->getLHS());
4546 if (!LHS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004547 return nullptr;
4548
Douglas Gregorf638f952010-02-19 01:07:06 +00004549 Expr *RHS = Importer.Import(E->getRHS());
4550 if (!RHS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004551 return nullptr;
4552
Douglas Gregorf638f952010-02-19 01:07:06 +00004553 return new (Importer.getToContext())
4554 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004555 T, E->getValueKind(),
4556 E->getObjectKind(),
4557 CompLHSType, CompResultType,
Lang Hamesbe9af122012-10-02 04:45:10 +00004558 Importer.Import(E->getOperatorLoc()),
4559 E->isFPContractable());
Douglas Gregorf638f952010-02-19 01:07:06 +00004560}
4561
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004562static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004563 if (E->path_empty()) return false;
4564
4565 // TODO: import cast paths
4566 return true;
4567}
4568
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004569Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4570 QualType T = Importer.Import(E->getType());
4571 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004572 return nullptr;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004573
4574 Expr *SubExpr = Importer.Import(E->getSubExpr());
4575 if (!SubExpr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004576 return nullptr;
John McCallf871d0c2010-08-07 06:22:56 +00004577
4578 CXXCastPath BasePath;
4579 if (ImportCastPath(E, BasePath))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004580 return nullptr;
John McCallf871d0c2010-08-07 06:22:56 +00004581
4582 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004583 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004584}
4585
Douglas Gregor008847a2010-02-19 01:32:14 +00004586Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4587 QualType T = Importer.Import(E->getType());
4588 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004589 return nullptr;
4590
Douglas Gregor008847a2010-02-19 01:32:14 +00004591 Expr *SubExpr = Importer.Import(E->getSubExpr());
4592 if (!SubExpr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004593 return nullptr;
Douglas Gregor008847a2010-02-19 01:32:14 +00004594
4595 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4596 if (!TInfo && E->getTypeInfoAsWritten())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004597 return nullptr;
4598
John McCallf871d0c2010-08-07 06:22:56 +00004599 CXXCastPath BasePath;
4600 if (ImportCastPath(E, BasePath))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004601 return nullptr;
John McCallf871d0c2010-08-07 06:22:56 +00004602
John McCallf89e55a2010-11-18 06:31:45 +00004603 return CStyleCastExpr::Create(Importer.getToContext(), T,
4604 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004605 SubExpr, &BasePath, TInfo,
4606 Importer.Import(E->getLParenLoc()),
4607 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004608}
4609
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004610ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004611 ASTContext &FromContext, FileManager &FromFileManager,
4612 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004613 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004614 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5b9268f2012-12-20 02:22:15 +00004615 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregord8868a62011-01-18 03:11:38 +00004616{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004617 ImportedDecls[FromContext.getTranslationUnitDecl()]
4618 = ToContext.getTranslationUnitDecl();
4619}
4620
4621ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004622
4623QualType ASTImporter::Import(QualType FromT) {
4624 if (FromT.isNull())
4625 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004626
4627 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004628
Douglas Gregor169fba52010-02-08 15:18:58 +00004629 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004630 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4631 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004632 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004633 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004634
Douglas Gregor169fba52010-02-08 15:18:58 +00004635 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004636 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004637 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004638 if (ToT.isNull())
4639 return ToT;
4640
Douglas Gregor169fba52010-02-08 15:18:58 +00004641 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004642 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004643
John McCallf4c73712011-01-19 06:33:43 +00004644 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004645}
4646
Douglas Gregor9bed8792010-02-09 19:21:46 +00004647TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004648 if (!FromTSI)
4649 return FromTSI;
4650
4651 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004652 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004653 QualType T = Import(FromTSI->getType());
4654 if (T.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004655 return nullptr;
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004656
4657 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar96a00142012-03-09 18:35:03 +00004658 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004659}
4660
4661Decl *ASTImporter::Import(Decl *FromD) {
4662 if (!FromD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004663 return nullptr;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004664
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004665 ASTNodeImporter Importer(*this);
4666
Douglas Gregor9bed8792010-02-09 19:21:46 +00004667 // Check whether we've already imported this declaration.
4668 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004669 if (Pos != ImportedDecls.end()) {
4670 Decl *ToD = Pos->second;
4671 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4672 return ToD;
4673 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004674
4675 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004676 Decl *ToD = Importer.Visit(FromD);
4677 if (!ToD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004678 return nullptr;
4679
Douglas Gregor9bed8792010-02-09 19:21:46 +00004680 // Record the imported declaration.
4681 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004682
4683 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4684 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004685 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004686 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004687 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004688 // When we've finished transforming a typedef, see whether it was the
4689 // typedef for an anonymous tag.
Craig Topper09d19ef2013-07-04 03:08:24 +00004690 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004691 FromTag = AnonTagsWithPendingTypedefs.begin(),
4692 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4693 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004694 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004695 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4696 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004697 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004698 AnonTagsWithPendingTypedefs.erase(FromTag);
4699 break;
4700 }
4701 }
4702 }
4703 }
4704
Douglas Gregor9bed8792010-02-09 19:21:46 +00004705 return ToD;
4706}
4707
4708DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4709 if (!FromDC)
4710 return FromDC;
4711
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004712 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregorac32ff92012-02-01 21:00:38 +00004713 if (!ToDC)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004714 return nullptr;
4715
Douglas Gregorac32ff92012-02-01 21:00:38 +00004716 // When we're using a record/enum/Objective-C class/protocol as a context, we
4717 // need it to have a definition.
4718 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor568991b2012-01-25 01:13:20 +00004719 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregorac32ff92012-02-01 21:00:38 +00004720 if (ToRecord->isCompleteDefinition()) {
4721 // Do nothing.
4722 } else if (FromRecord->isCompleteDefinition()) {
4723 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4724 ASTNodeImporter::IDK_Basic);
4725 } else {
4726 CompleteDecl(ToRecord);
4727 }
4728 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4729 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4730 if (ToEnum->isCompleteDefinition()) {
4731 // Do nothing.
4732 } else if (FromEnum->isCompleteDefinition()) {
4733 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4734 ASTNodeImporter::IDK_Basic);
4735 } else {
4736 CompleteDecl(ToEnum);
4737 }
4738 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4739 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4740 if (ToClass->getDefinition()) {
4741 // Do nothing.
4742 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4743 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4744 ASTNodeImporter::IDK_Basic);
4745 } else {
4746 CompleteDecl(ToClass);
4747 }
4748 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4749 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4750 if (ToProto->getDefinition()) {
4751 // Do nothing.
4752 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4753 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4754 ASTNodeImporter::IDK_Basic);
4755 } else {
4756 CompleteDecl(ToProto);
4757 }
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004758 }
4759
4760 return ToDC;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004761}
4762
4763Expr *ASTImporter::Import(Expr *FromE) {
4764 if (!FromE)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004765 return nullptr;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004766
4767 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4768}
4769
4770Stmt *ASTImporter::Import(Stmt *FromS) {
4771 if (!FromS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004772 return nullptr;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004773
Douglas Gregor4800d952010-02-11 19:21:55 +00004774 // Check whether we've already imported this declaration.
4775 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4776 if (Pos != ImportedStmts.end())
4777 return Pos->second;
4778
4779 // Import the type
4780 ASTNodeImporter Importer(*this);
4781 Stmt *ToS = Importer.Visit(FromS);
4782 if (!ToS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004783 return nullptr;
4784
Douglas Gregor4800d952010-02-11 19:21:55 +00004785 // Record the imported declaration.
4786 ImportedStmts[FromS] = ToS;
4787 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004788}
4789
4790NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4791 if (!FromNNS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004792 return nullptr;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004793
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004794 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4795
4796 switch (FromNNS->getKind()) {
4797 case NestedNameSpecifier::Identifier:
4798 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4799 return NestedNameSpecifier::Create(ToContext, prefix, II);
4800 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004801 return nullptr;
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004802
4803 case NestedNameSpecifier::Namespace:
4804 if (NamespaceDecl *NS =
4805 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4806 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4807 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004808 return nullptr;
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004809
4810 case NestedNameSpecifier::NamespaceAlias:
4811 if (NamespaceAliasDecl *NSAD =
4812 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4813 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4814 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004815 return nullptr;
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004816
4817 case NestedNameSpecifier::Global:
4818 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4819
Stephen Hines176edba2014-12-01 14:53:08 -08004820 case NestedNameSpecifier::Super:
4821 if (CXXRecordDecl *RD =
4822 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
4823 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
4824 }
4825 return nullptr;
4826
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004827 case NestedNameSpecifier::TypeSpec:
4828 case NestedNameSpecifier::TypeSpecWithTemplate: {
4829 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4830 if (!T.isNull()) {
4831 bool bTemplate = FromNNS->getKind() ==
4832 NestedNameSpecifier::TypeSpecWithTemplate;
4833 return NestedNameSpecifier::Create(ToContext, prefix,
4834 bTemplate, T.getTypePtr());
4835 }
4836 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004837 return nullptr;
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004838 }
4839
4840 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004841}
4842
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004843NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4844 // FIXME: Implement!
4845 return NestedNameSpecifierLoc();
4846}
4847
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004848TemplateName ASTImporter::Import(TemplateName From) {
4849 switch (From.getKind()) {
4850 case TemplateName::Template:
4851 if (TemplateDecl *ToTemplate
4852 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4853 return TemplateName(ToTemplate);
4854
4855 return TemplateName();
4856
4857 case TemplateName::OverloadedTemplate: {
4858 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4859 UnresolvedSet<2> ToTemplates;
4860 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4861 E = FromStorage->end();
4862 I != E; ++I) {
4863 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4864 ToTemplates.addDecl(To);
4865 else
4866 return TemplateName();
4867 }
4868 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4869 ToTemplates.end());
4870 }
4871
4872 case TemplateName::QualifiedTemplate: {
4873 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4874 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4875 if (!Qualifier)
4876 return TemplateName();
4877
4878 if (TemplateDecl *ToTemplate
4879 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4880 return ToContext.getQualifiedTemplateName(Qualifier,
4881 QTN->hasTemplateKeyword(),
4882 ToTemplate);
4883
4884 return TemplateName();
4885 }
4886
4887 case TemplateName::DependentTemplate: {
4888 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4889 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4890 if (!Qualifier)
4891 return TemplateName();
4892
4893 if (DTN->isIdentifier()) {
4894 return ToContext.getDependentTemplateName(Qualifier,
4895 Import(DTN->getIdentifier()));
4896 }
4897
4898 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4899 }
John McCall14606042011-06-30 08:33:18 +00004900
4901 case TemplateName::SubstTemplateTemplateParm: {
4902 SubstTemplateTemplateParmStorage *subst
4903 = From.getAsSubstTemplateTemplateParm();
4904 TemplateTemplateParmDecl *param
4905 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4906 if (!param)
4907 return TemplateName();
4908
4909 TemplateName replacement = Import(subst->getReplacement());
4910 if (replacement.isNull()) return TemplateName();
4911
4912 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4913 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004914
4915 case TemplateName::SubstTemplateTemplateParmPack: {
4916 SubstTemplateTemplateParmPackStorage *SubstPack
4917 = From.getAsSubstTemplateTemplateParmPack();
4918 TemplateTemplateParmDecl *Param
4919 = cast_or_null<TemplateTemplateParmDecl>(
4920 Import(SubstPack->getParameterPack()));
4921 if (!Param)
4922 return TemplateName();
4923
4924 ASTNodeImporter Importer(*this);
4925 TemplateArgument ArgPack
4926 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4927 if (ArgPack.isNull())
4928 return TemplateName();
4929
4930 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4931 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004932 }
4933
4934 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004935}
4936
Douglas Gregor9bed8792010-02-09 19:21:46 +00004937SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4938 if (FromLoc.isInvalid())
4939 return SourceLocation();
4940
Douglas Gregor88523732010-02-10 00:15:17 +00004941 SourceManager &FromSM = FromContext.getSourceManager();
4942
4943 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004944 // don't have to import macro expansions.
4945 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004946 FromLoc = FromSM.getSpellingLoc(FromLoc);
4947 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4948 SourceManager &ToSM = ToContext.getSourceManager();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004949 FileID ToFileID = Import(Decomposed.first);
4950 if (ToFileID.isInvalid())
4951 return SourceLocation();
4952 return ToSM.getLocForStartOfFile(ToFileID)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004953 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004954}
4955
4956SourceRange ASTImporter::Import(SourceRange FromRange) {
4957 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4958}
4959
Douglas Gregor88523732010-02-10 00:15:17 +00004960FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004961 llvm::DenseMap<FileID, FileID>::iterator Pos
4962 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004963 if (Pos != ImportedFileIDs.end())
4964 return Pos->second;
4965
4966 SourceManager &FromSM = FromContext.getSourceManager();
4967 SourceManager &ToSM = ToContext.getSourceManager();
4968 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004969 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004970
4971 // Include location of this file.
4972 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4973
4974 // Map the FileID for to the "to" source manager.
4975 FileID ToID;
4976 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004977 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004978 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4979 // disk again
4980 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4981 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004982 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004983 if (!Entry)
4984 return FileID();
Douglas Gregor88523732010-02-10 00:15:17 +00004985 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4986 FromSLoc.getFile().getFileCharacteristic());
4987 } else {
4988 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004989 const llvm::MemoryBuffer *
4990 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Stephen Hines176edba2014-12-01 14:53:08 -08004991 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004992 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004993 FromBuf->getBufferIdentifier());
Stephen Hines176edba2014-12-01 14:53:08 -08004994 ToID = ToSM.createFileID(std::move(ToBuf),
4995 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor88523732010-02-10 00:15:17 +00004996 }
4997
4998
Sebastian Redl535a3e22010-09-30 01:03:06 +00004999 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00005000 return ToID;
5001}
5002
Douglas Gregord8868a62011-01-18 03:11:38 +00005003void ASTImporter::ImportDefinition(Decl *From) {
5004 Decl *To = Import(From);
5005 if (!To)
5006 return;
5007
5008 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5009 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00005010
5011 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5012 if (!ToRecord->getDefinition()) {
5013 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00005014 ASTNodeImporter::IDK_Everything);
Sean Callanan673e7752011-07-19 22:38:25 +00005015 return;
5016 }
5017 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00005018
5019 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5020 if (!ToEnum->getDefinition()) {
5021 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregorac32ff92012-02-01 21:00:38 +00005022 ASTNodeImporter::IDK_Everything);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00005023 return;
5024 }
5025 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00005026
5027 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5028 if (!ToIFace->getDefinition()) {
5029 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregorac32ff92012-02-01 21:00:38 +00005030 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00005031 return;
5032 }
5033 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00005034
Douglas Gregor5602f7e2012-01-24 17:42:07 +00005035 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5036 if (!ToProto->getDefinition()) {
5037 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregorac32ff92012-02-01 21:00:38 +00005038 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00005039 return;
5040 }
5041 }
5042
Douglas Gregord8868a62011-01-18 03:11:38 +00005043 Importer.ImportDeclContext(FromDC, true);
5044 }
5045}
5046
Douglas Gregor1b2949d2010-02-05 17:54:41 +00005047DeclarationName ASTImporter::Import(DeclarationName FromName) {
5048 if (!FromName)
5049 return DeclarationName();
5050
5051 switch (FromName.getNameKind()) {
5052 case DeclarationName::Identifier:
5053 return Import(FromName.getAsIdentifierInfo());
5054
5055 case DeclarationName::ObjCZeroArgSelector:
5056 case DeclarationName::ObjCOneArgSelector:
5057 case DeclarationName::ObjCMultiArgSelector:
5058 return Import(FromName.getObjCSelector());
5059
5060 case DeclarationName::CXXConstructorName: {
5061 QualType T = Import(FromName.getCXXNameType());
5062 if (T.isNull())
5063 return DeclarationName();
5064
5065 return ToContext.DeclarationNames.getCXXConstructorName(
5066 ToContext.getCanonicalType(T));
5067 }
5068
5069 case DeclarationName::CXXDestructorName: {
5070 QualType T = Import(FromName.getCXXNameType());
5071 if (T.isNull())
5072 return DeclarationName();
5073
5074 return ToContext.DeclarationNames.getCXXDestructorName(
5075 ToContext.getCanonicalType(T));
5076 }
5077
5078 case DeclarationName::CXXConversionFunctionName: {
5079 QualType T = Import(FromName.getCXXNameType());
5080 if (T.isNull())
5081 return DeclarationName();
5082
5083 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5084 ToContext.getCanonicalType(T));
5085 }
5086
5087 case DeclarationName::CXXOperatorName:
5088 return ToContext.DeclarationNames.getCXXOperatorName(
5089 FromName.getCXXOverloadedOperator());
5090
5091 case DeclarationName::CXXLiteralOperatorName:
5092 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5093 Import(FromName.getCXXLiteralIdentifier()));
5094
5095 case DeclarationName::CXXUsingDirective:
5096 // FIXME: STATICS!
5097 return DeclarationName::getUsingDirectiveName();
5098 }
5099
David Blaikie30263482012-01-20 21:50:17 +00005100 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00005101}
5102
Douglas Gregord5dc83a2010-12-01 01:36:18 +00005103IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00005104 if (!FromId)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005105 return nullptr;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00005106
5107 return &ToContext.Idents.get(FromId->getName());
5108}
Douglas Gregor089459a2010-02-08 21:09:39 +00005109
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00005110Selector ASTImporter::Import(Selector FromSel) {
5111 if (FromSel.isNull())
5112 return Selector();
5113
Chris Lattner5f9e2722011-07-23 10:55:15 +00005114 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00005115 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5116 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5117 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5118 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5119}
5120
Douglas Gregor089459a2010-02-08 21:09:39 +00005121DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5122 DeclContext *DC,
5123 unsigned IDNS,
5124 NamedDecl **Decls,
5125 unsigned NumDecls) {
5126 return Name;
5127}
5128
5129DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5b9268f2012-12-20 02:22:15 +00005130 if (LastDiagFromFrom)
5131 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5132 FromContext.getDiagnostics());
5133 LastDiagFromFrom = false;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005134 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00005135}
5136
5137DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5b9268f2012-12-20 02:22:15 +00005138 if (!LastDiagFromFrom)
5139 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5140 ToContext.getDiagnostics());
5141 LastDiagFromFrom = true;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005142 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00005143}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00005144
Douglas Gregorac32ff92012-02-01 21:00:38 +00005145void ASTImporter::CompleteDecl (Decl *D) {
5146 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5147 if (!ID->getDefinition())
5148 ID->startDefinition();
5149 }
5150 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5151 if (!PD->getDefinition())
5152 PD->startDefinition();
5153 }
5154 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5155 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5156 TD->startDefinition();
5157 TD->setCompleteDefinition(true);
5158 }
5159 }
5160 else {
5161 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5162 }
5163}
5164
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00005165Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5166 ImportedDecls[From] = To;
5167 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00005168}
Douglas Gregorea35d112010-02-15 23:54:17 +00005169
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00005170bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5171 bool Complain) {
John McCallf4c73712011-01-19 06:33:43 +00005172 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00005173 = ImportedTypes.find(From.getTypePtr());
5174 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5175 return true;
5176
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00005177 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5178 false, Complain);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00005179 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00005180}