blob: 05f39abaa37fc14bcf8c473a5c5e42776a464069 [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
32
33 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
42 QualType VisitBuiltinType(const BuiltinType *T);
43 QualType VisitComplexType(const ComplexType *T);
44 QualType VisitPointerType(const PointerType *T);
45 QualType VisitBlockPointerType(const BlockPointerType *T);
46 QualType VisitLValueReferenceType(const LValueReferenceType *T);
47 QualType VisitRValueReferenceType(const RValueReferenceType *T);
48 QualType VisitMemberPointerType(const MemberPointerType *T);
49 QualType VisitConstantArrayType(const ConstantArrayType *T);
50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
51 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
73 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000076 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
77 QualType VisitObjCObjectType(const ObjCObjectType *T);
78 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
Douglas Gregor95d82832012-01-24 18:36:04 +000080 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000084 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000085 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
86 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000087 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000088
Douglas Gregor95d82832012-01-24 18:36:04 +000089 /// \brief What we should import from the definition.
90 enum ImportDefinitionKind {
91 /// \brief Import the default subset of the definition, which might be
92 /// nothing (if minimal import is set) or might be everything (if minimal
93 /// import is not set).
94 IDK_Default,
95 /// \brief Import everything.
96 IDK_Everything,
97 /// \brief Import only the bare bones needed to establish a valid
98 /// DeclContext.
99 IDK_Basic
100 };
101
Douglas Gregor2e15c842012-02-01 21:00:38 +0000102 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
103 return IDK == IDK_Everything ||
104 (IDK == IDK_Default && !Importer.isMinimalImport());
105 }
106
Douglas Gregord451ea92011-07-29 23:31:30 +0000107 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000108 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000109 bool ImportDefinition(VarDecl *From, VarDecl *To,
110 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000111 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000112 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000113 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000115 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000116 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000117 TemplateParameterList *ImportTemplateParameterList(
118 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000119 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
120 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
121 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000122 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000123 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
124 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000125 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
126 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000127 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000128 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000129 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000130 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000131 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000132 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000133 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000134 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000135 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000136 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000137 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000138 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000139 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000140 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000141 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
142 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
143 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
144 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000145 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000146 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000147 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000148 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000149 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000150 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000151 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000152 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000153 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000154 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000155 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000156 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000157 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000158 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000159 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
160 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
161 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
162 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000163 Decl *VisitClassTemplateSpecializationDecl(
164 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000165 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
166 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
167
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000168 // Importing statements
169 Stmt *VisitStmt(Stmt *S);
170
171 // Importing expressions
172 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000173 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000174 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000175 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000176 Expr *VisitParenExpr(ParenExpr *E);
177 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000178 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000179 Expr *VisitBinaryOperator(BinaryOperator *E);
180 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000181 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000182 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000183 };
184}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000185using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000186
187//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000188// Structural Equivalence
189//----------------------------------------------------------------------------
190
191namespace {
192 struct StructuralEquivalenceContext {
193 /// \brief AST contexts for which we are checking structural equivalence.
194 ASTContext &C1, &C2;
195
Douglas Gregor3996e242010-02-15 22:01:00 +0000196 /// \brief The set of "tentative" equivalences between two canonical
197 /// declarations, mapping from a declaration in the first context to the
198 /// declaration in the second context that we believe to be equivalent.
199 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
200
201 /// \brief Queue of declarations in the first context whose equivalence
202 /// with a declaration in the second context still needs to be verified.
203 std::deque<Decl *> DeclsToCheck;
204
Douglas Gregorb4964f72010-02-15 23:54:17 +0000205 /// \brief Declaration (from, to) pairs that are known not to be equivalent
206 /// (which we have already complained about).
207 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
208
Douglas Gregor3996e242010-02-15 22:01:00 +0000209 /// \brief Whether we're being strict about the spelling of types when
210 /// unifying two types.
211 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000212
213 /// \brief Whether to complain about failures.
214 bool Complain;
215
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000216 /// \brief \c true if the last diagnostic came from C2.
217 bool LastDiagFromC2;
218
Douglas Gregor3996e242010-02-15 22:01:00 +0000219 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000220 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000221 bool StrictTypeSpelling = false,
222 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000223 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000224 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
225 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000226
227 /// \brief Determine whether the two declarations are structurally
228 /// equivalent.
229 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
230
231 /// \brief Determine whether the two types are structurally equivalent.
232 bool IsStructurallyEquivalent(QualType T1, QualType T2);
233
234 private:
235 /// \brief Finish checking all of the structural equivalences.
236 ///
237 /// \returns true if an error occurred, false otherwise.
238 bool Finish();
239
240 public:
241 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000242 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000243 if (LastDiagFromC2)
244 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
245 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000246 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000247 }
248
249 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000250 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000251 if (!LastDiagFromC2)
252 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
253 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000254 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000255 }
256 };
257}
258
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 QualType T1, QualType T2);
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 Decl *D1, Decl *D2);
263
Douglas Gregor3996e242010-02-15 22:01:00 +0000264/// \brief Determine structural equivalence of two expressions.
265static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
266 Expr *E1, Expr *E2) {
267 if (!E1 || !E2)
268 return E1 == E2;
269
270 // FIXME: Actually perform a structural comparison!
271 return true;
272}
273
274/// \brief Determine whether two identifiers are equivalent.
275static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
276 const IdentifierInfo *Name2) {
277 if (!Name1 || !Name2)
278 return Name1 == Name2;
279
280 return Name1->getName() == Name2->getName();
281}
282
283/// \brief Determine whether two nested-name-specifiers are equivalent.
284static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
285 NestedNameSpecifier *NNS1,
286 NestedNameSpecifier *NNS2) {
287 // FIXME: Implement!
288 return true;
289}
290
291/// \brief Determine whether two template arguments are equivalent.
292static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
293 const TemplateArgument &Arg1,
294 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000295 if (Arg1.getKind() != Arg2.getKind())
296 return false;
297
298 switch (Arg1.getKind()) {
299 case TemplateArgument::Null:
300 return true;
301
302 case TemplateArgument::Type:
303 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000304
Douglas Gregore2e50d332010-12-01 01:36:18 +0000305 case TemplateArgument::Integral:
306 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
307 Arg2.getIntegralType()))
308 return false;
309
Eric Christopher6dcc3762012-07-15 00:23:57 +0000310 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311
312 case TemplateArgument::Declaration:
313 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000314
315 case TemplateArgument::NullPtr:
316 return true; // FIXME: Is this correct?
317
Douglas Gregore2e50d332010-12-01 01:36:18 +0000318 case TemplateArgument::Template:
319 return IsStructurallyEquivalent(Context,
320 Arg1.getAsTemplate(),
321 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000322
323 case TemplateArgument::TemplateExpansion:
324 return IsStructurallyEquivalent(Context,
325 Arg1.getAsTemplateOrTemplatePattern(),
326 Arg2.getAsTemplateOrTemplatePattern());
327
Douglas Gregore2e50d332010-12-01 01:36:18 +0000328 case TemplateArgument::Expression:
329 return IsStructurallyEquivalent(Context,
330 Arg1.getAsExpr(), Arg2.getAsExpr());
331
332 case TemplateArgument::Pack:
333 if (Arg1.pack_size() != Arg2.pack_size())
334 return false;
335
336 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
337 if (!IsStructurallyEquivalent(Context,
338 Arg1.pack_begin()[I],
339 Arg2.pack_begin()[I]))
340 return false;
341
342 return true;
343 }
344
345 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000346}
347
348/// \brief Determine structural equivalence for the common part of array
349/// types.
350static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
351 const ArrayType *Array1,
352 const ArrayType *Array2) {
353 if (!IsStructurallyEquivalent(Context,
354 Array1->getElementType(),
355 Array2->getElementType()))
356 return false;
357 if (Array1->getSizeModifier() != Array2->getSizeModifier())
358 return false;
359 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
360 return false;
361
362 return true;
363}
364
365/// \brief Determine structural equivalence of two types.
366static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
367 QualType T1, QualType T2) {
368 if (T1.isNull() || T2.isNull())
369 return T1.isNull() && T2.isNull();
370
371 if (!Context.StrictTypeSpelling) {
372 // We aren't being strict about token-to-token equivalence of types,
373 // so map down to the canonical type.
374 T1 = Context.C1.getCanonicalType(T1);
375 T2 = Context.C2.getCanonicalType(T2);
376 }
377
378 if (T1.getQualifiers() != T2.getQualifiers())
379 return false;
380
Douglas Gregorb4964f72010-02-15 23:54:17 +0000381 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000382
Douglas Gregorb4964f72010-02-15 23:54:17 +0000383 if (T1->getTypeClass() != T2->getTypeClass()) {
384 // Compare function types with prototypes vs. without prototypes as if
385 // both did not have prototypes.
386 if (T1->getTypeClass() == Type::FunctionProto &&
387 T2->getTypeClass() == Type::FunctionNoProto)
388 TC = Type::FunctionNoProto;
389 else if (T1->getTypeClass() == Type::FunctionNoProto &&
390 T2->getTypeClass() == Type::FunctionProto)
391 TC = Type::FunctionNoProto;
392 else
393 return false;
394 }
395
396 switch (TC) {
397 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000398 // FIXME: Deal with Char_S/Char_U.
399 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
400 return false;
401 break;
402
403 case Type::Complex:
404 if (!IsStructurallyEquivalent(Context,
405 cast<ComplexType>(T1)->getElementType(),
406 cast<ComplexType>(T2)->getElementType()))
407 return false;
408 break;
409
Reid Kleckner0503a872013-12-05 01:23:43 +0000410 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000411 case Type::Decayed:
412 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000413 cast<AdjustedType>(T1)->getOriginalType(),
414 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000415 return false;
416 break;
417
Douglas Gregor3996e242010-02-15 22:01:00 +0000418 case Type::Pointer:
419 if (!IsStructurallyEquivalent(Context,
420 cast<PointerType>(T1)->getPointeeType(),
421 cast<PointerType>(T2)->getPointeeType()))
422 return false;
423 break;
424
425 case Type::BlockPointer:
426 if (!IsStructurallyEquivalent(Context,
427 cast<BlockPointerType>(T1)->getPointeeType(),
428 cast<BlockPointerType>(T2)->getPointeeType()))
429 return false;
430 break;
431
432 case Type::LValueReference:
433 case Type::RValueReference: {
434 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
435 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
436 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
437 return false;
438 if (Ref1->isInnerRef() != Ref2->isInnerRef())
439 return false;
440 if (!IsStructurallyEquivalent(Context,
441 Ref1->getPointeeTypeAsWritten(),
442 Ref2->getPointeeTypeAsWritten()))
443 return false;
444 break;
445 }
446
447 case Type::MemberPointer: {
448 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
449 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
450 if (!IsStructurallyEquivalent(Context,
451 MemPtr1->getPointeeType(),
452 MemPtr2->getPointeeType()))
453 return false;
454 if (!IsStructurallyEquivalent(Context,
455 QualType(MemPtr1->getClass(), 0),
456 QualType(MemPtr2->getClass(), 0)))
457 return false;
458 break;
459 }
460
461 case Type::ConstantArray: {
462 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
463 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000464 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000465 return false;
466
467 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
468 return false;
469 break;
470 }
471
472 case Type::IncompleteArray:
473 if (!IsArrayStructurallyEquivalent(Context,
474 cast<ArrayType>(T1),
475 cast<ArrayType>(T2)))
476 return false;
477 break;
478
479 case Type::VariableArray: {
480 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
481 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
482 if (!IsStructurallyEquivalent(Context,
483 Array1->getSizeExpr(), Array2->getSizeExpr()))
484 return false;
485
486 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
487 return false;
488
489 break;
490 }
491
492 case Type::DependentSizedArray: {
493 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
494 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 Array1->getSizeExpr(), Array2->getSizeExpr()))
497 return false;
498
499 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
500 return false;
501
502 break;
503 }
504
505 case Type::DependentSizedExtVector: {
506 const DependentSizedExtVectorType *Vec1
507 = cast<DependentSizedExtVectorType>(T1);
508 const DependentSizedExtVectorType *Vec2
509 = cast<DependentSizedExtVectorType>(T2);
510 if (!IsStructurallyEquivalent(Context,
511 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
512 return false;
513 if (!IsStructurallyEquivalent(Context,
514 Vec1->getElementType(),
515 Vec2->getElementType()))
516 return false;
517 break;
518 }
519
520 case Type::Vector:
521 case Type::ExtVector: {
522 const VectorType *Vec1 = cast<VectorType>(T1);
523 const VectorType *Vec2 = cast<VectorType>(T2);
524 if (!IsStructurallyEquivalent(Context,
525 Vec1->getElementType(),
526 Vec2->getElementType()))
527 return false;
528 if (Vec1->getNumElements() != Vec2->getNumElements())
529 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000530 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000531 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000532 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000533 }
534
535 case Type::FunctionProto: {
536 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
537 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000538 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000540 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
541 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
542 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000543 return false;
544 }
545 if (Proto1->isVariadic() != Proto2->isVariadic())
546 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000547 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000548 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000549 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
550 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
551 return false;
552 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
553 if (!IsStructurallyEquivalent(Context,
554 Proto1->getExceptionType(I),
555 Proto2->getExceptionType(I)))
556 return false;
557 }
558 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000559 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000560 Proto1->getNoexceptExpr(),
561 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000562 return false;
563 }
564 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
565 return false;
566
567 // Fall through to check the bits common with FunctionNoProtoType.
568 }
569
570 case Type::FunctionNoProto: {
571 const FunctionType *Function1 = cast<FunctionType>(T1);
572 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000573 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
574 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000575 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000576 if (Function1->getExtInfo() != Function2->getExtInfo())
577 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000578 break;
579 }
580
581 case Type::UnresolvedUsing:
582 if (!IsStructurallyEquivalent(Context,
583 cast<UnresolvedUsingType>(T1)->getDecl(),
584 cast<UnresolvedUsingType>(T2)->getDecl()))
585 return false;
586
587 break;
John McCall81904512011-01-06 01:58:22 +0000588
589 case Type::Attributed:
590 if (!IsStructurallyEquivalent(Context,
591 cast<AttributedType>(T1)->getModifiedType(),
592 cast<AttributedType>(T2)->getModifiedType()))
593 return false;
594 if (!IsStructurallyEquivalent(Context,
595 cast<AttributedType>(T1)->getEquivalentType(),
596 cast<AttributedType>(T2)->getEquivalentType()))
597 return false;
598 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000599
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000600 case Type::Paren:
601 if (!IsStructurallyEquivalent(Context,
602 cast<ParenType>(T1)->getInnerType(),
603 cast<ParenType>(T2)->getInnerType()))
604 return false;
605 break;
606
Douglas Gregor3996e242010-02-15 22:01:00 +0000607 case Type::Typedef:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypedefType>(T1)->getDecl(),
610 cast<TypedefType>(T2)->getDecl()))
611 return false;
612 break;
613
614 case Type::TypeOfExpr:
615 if (!IsStructurallyEquivalent(Context,
616 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
617 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
618 return false;
619 break;
620
621 case Type::TypeOf:
622 if (!IsStructurallyEquivalent(Context,
623 cast<TypeOfType>(T1)->getUnderlyingType(),
624 cast<TypeOfType>(T2)->getUnderlyingType()))
625 return false;
626 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000627
628 case Type::UnaryTransform:
629 if (!IsStructurallyEquivalent(Context,
630 cast<UnaryTransformType>(T1)->getUnderlyingType(),
631 cast<UnaryTransformType>(T1)->getUnderlyingType()))
632 return false;
633 break;
634
Douglas Gregor3996e242010-02-15 22:01:00 +0000635 case Type::Decltype:
636 if (!IsStructurallyEquivalent(Context,
637 cast<DecltypeType>(T1)->getUnderlyingExpr(),
638 cast<DecltypeType>(T2)->getUnderlyingExpr()))
639 return false;
640 break;
641
Richard Smith30482bc2011-02-20 03:19:35 +0000642 case Type::Auto:
643 if (!IsStructurallyEquivalent(Context,
644 cast<AutoType>(T1)->getDeducedType(),
645 cast<AutoType>(T2)->getDeducedType()))
646 return false;
647 break;
648
Douglas Gregor3996e242010-02-15 22:01:00 +0000649 case Type::Record:
650 case Type::Enum:
651 if (!IsStructurallyEquivalent(Context,
652 cast<TagType>(T1)->getDecl(),
653 cast<TagType>(T2)->getDecl()))
654 return false;
655 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000656
Douglas Gregor3996e242010-02-15 22:01:00 +0000657 case Type::TemplateTypeParm: {
658 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
659 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
660 if (Parm1->getDepth() != Parm2->getDepth())
661 return false;
662 if (Parm1->getIndex() != Parm2->getIndex())
663 return false;
664 if (Parm1->isParameterPack() != Parm2->isParameterPack())
665 return false;
666
667 // Names of template type parameters are never significant.
668 break;
669 }
670
671 case Type::SubstTemplateTypeParm: {
672 const SubstTemplateTypeParmType *Subst1
673 = cast<SubstTemplateTypeParmType>(T1);
674 const SubstTemplateTypeParmType *Subst2
675 = cast<SubstTemplateTypeParmType>(T2);
676 if (!IsStructurallyEquivalent(Context,
677 QualType(Subst1->getReplacedParameter(), 0),
678 QualType(Subst2->getReplacedParameter(), 0)))
679 return false;
680 if (!IsStructurallyEquivalent(Context,
681 Subst1->getReplacementType(),
682 Subst2->getReplacementType()))
683 return false;
684 break;
685 }
686
Douglas Gregorfb322d82011-01-14 05:11:40 +0000687 case Type::SubstTemplateTypeParmPack: {
688 const SubstTemplateTypeParmPackType *Subst1
689 = cast<SubstTemplateTypeParmPackType>(T1);
690 const SubstTemplateTypeParmPackType *Subst2
691 = cast<SubstTemplateTypeParmPackType>(T2);
692 if (!IsStructurallyEquivalent(Context,
693 QualType(Subst1->getReplacedParameter(), 0),
694 QualType(Subst2->getReplacedParameter(), 0)))
695 return false;
696 if (!IsStructurallyEquivalent(Context,
697 Subst1->getArgumentPack(),
698 Subst2->getArgumentPack()))
699 return false;
700 break;
701 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000702 case Type::TemplateSpecialization: {
703 const TemplateSpecializationType *Spec1
704 = cast<TemplateSpecializationType>(T1);
705 const TemplateSpecializationType *Spec2
706 = cast<TemplateSpecializationType>(T2);
707 if (!IsStructurallyEquivalent(Context,
708 Spec1->getTemplateName(),
709 Spec2->getTemplateName()))
710 return false;
711 if (Spec1->getNumArgs() != Spec2->getNumArgs())
712 return false;
713 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
714 if (!IsStructurallyEquivalent(Context,
715 Spec1->getArg(I), Spec2->getArg(I)))
716 return false;
717 }
718 break;
719 }
720
Abramo Bagnara6150c882010-05-11 21:36:43 +0000721 case Type::Elaborated: {
722 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
723 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
724 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
725 if (Elab1->getKeyword() != Elab2->getKeyword())
726 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000727 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000728 Elab1->getQualifier(),
729 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000730 return false;
731 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000732 Elab1->getNamedType(),
733 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000734 return false;
735 break;
736 }
737
John McCalle78aac42010-03-10 03:28:59 +0000738 case Type::InjectedClassName: {
739 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
740 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
741 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000742 Inj1->getInjectedSpecializationType(),
743 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000744 return false;
745 break;
746 }
747
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000748 case Type::DependentName: {
749 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
750 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000751 if (!IsStructurallyEquivalent(Context,
752 Typename1->getQualifier(),
753 Typename2->getQualifier()))
754 return false;
755 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
756 Typename2->getIdentifier()))
757 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000758
759 break;
760 }
761
John McCallc392f372010-06-11 00:33:02 +0000762 case Type::DependentTemplateSpecialization: {
763 const DependentTemplateSpecializationType *Spec1 =
764 cast<DependentTemplateSpecializationType>(T1);
765 const DependentTemplateSpecializationType *Spec2 =
766 cast<DependentTemplateSpecializationType>(T2);
767 if (!IsStructurallyEquivalent(Context,
768 Spec1->getQualifier(),
769 Spec2->getQualifier()))
770 return false;
771 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
772 Spec2->getIdentifier()))
773 return false;
774 if (Spec1->getNumArgs() != Spec2->getNumArgs())
775 return false;
776 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
777 if (!IsStructurallyEquivalent(Context,
778 Spec1->getArg(I), Spec2->getArg(I)))
779 return false;
780 }
781 break;
782 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000783
784 case Type::PackExpansion:
785 if (!IsStructurallyEquivalent(Context,
786 cast<PackExpansionType>(T1)->getPattern(),
787 cast<PackExpansionType>(T2)->getPattern()))
788 return false;
789 break;
790
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 case Type::ObjCInterface: {
792 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
793 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
794 if (!IsStructurallyEquivalent(Context,
795 Iface1->getDecl(), Iface2->getDecl()))
796 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000797 break;
798 }
799
800 case Type::ObjCObject: {
801 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
802 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
803 if (!IsStructurallyEquivalent(Context,
804 Obj1->getBaseType(),
805 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000806 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000807 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
808 return false;
809 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000810 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000811 Obj1->getProtocol(I),
812 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000813 return false;
814 }
815 break;
816 }
817
818 case Type::ObjCObjectPointer: {
819 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
820 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
821 if (!IsStructurallyEquivalent(Context,
822 Ptr1->getPointeeType(),
823 Ptr2->getPointeeType()))
824 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000825 break;
826 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000827
828 case Type::Atomic: {
829 if (!IsStructurallyEquivalent(Context,
830 cast<AtomicType>(T1)->getValueType(),
831 cast<AtomicType>(T2)->getValueType()))
832 return false;
833 break;
834 }
835
Douglas Gregor3996e242010-02-15 22:01:00 +0000836 } // end switch
837
838 return true;
839}
840
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000841/// \brief Determine structural equivalence of two fields.
842static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
843 FieldDecl *Field1, FieldDecl *Field2) {
844 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000845
846 // For anonymous structs/unions, match up the anonymous struct/union type
847 // declarations directly, so that we don't go off searching for anonymous
848 // types
849 if (Field1->isAnonymousStructOrUnion() &&
850 Field2->isAnonymousStructOrUnion()) {
851 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
852 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
853 return IsStructurallyEquivalent(Context, D1, D2);
854 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000855
856 // Check for equivalent field names.
857 IdentifierInfo *Name1 = Field1->getIdentifier();
858 IdentifierInfo *Name2 = Field2->getIdentifier();
859 if (!::IsStructurallyEquivalent(Name1, Name2))
860 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000861
862 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000863 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000864 if (Context.Complain) {
865 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(Owner2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
868 << Field2->getDeclName() << Field2->getType();
869 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
870 << Field1->getDeclName() << Field1->getType();
871 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000872 return false;
873 }
874
875 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000876 if (Context.Complain) {
877 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
878 << Context.C2.getTypeDeclType(Owner2);
879 if (Field1->isBitField()) {
880 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
881 << Field1->getDeclName() << Field1->getType()
882 << Field1->getBitWidthValue(Context.C1);
883 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
884 << Field2->getDeclName();
885 } else {
886 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
887 << Field2->getDeclName() << Field2->getType()
888 << Field2->getBitWidthValue(Context.C2);
889 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
890 << Field1->getDeclName();
891 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000892 }
893 return false;
894 }
895
896 if (Field1->isBitField()) {
897 // Make sure that the bit-fields are the same length.
898 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
899 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
900
901 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000902 if (Context.Complain) {
903 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
904 << Context.C2.getTypeDeclType(Owner2);
905 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
906 << Field2->getDeclName() << Field2->getType() << Bits2;
907 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
908 << Field1->getDeclName() << Field1->getType() << Bits1;
909 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000910 return false;
911 }
912 }
913
914 return true;
915}
916
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000917/// \brief Find the index of the given anonymous struct/union within its
918/// context.
919///
920/// \returns Returns the index of this anonymous struct/union in its context,
921/// including the next assigned index (if none of them match). Returns an
922/// empty option if the context is not a record, i.e.. if the anonymous
923/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000924static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000925 ASTContext &Context = Anon->getASTContext();
926 QualType AnonTy = Context.getRecordType(Anon);
927
928 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
929 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000930 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000931
932 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +0000933 for (const auto *D : Owner->noload_decls()) {
934 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000935 if (!F || !F->isAnonymousStructOrUnion())
936 continue;
937
938 if (Context.hasSameType(F->getType(), AnonTy))
939 break;
940
941 ++Index;
942 }
943
944 return Index;
945}
946
Douglas Gregor3996e242010-02-15 22:01:00 +0000947/// \brief Determine structural equivalence of two records.
948static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
949 RecordDecl *D1, RecordDecl *D2) {
950 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000951 if (Context.Complain) {
952 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
953 << Context.C2.getTypeDeclType(D2);
954 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
955 << D1->getDeclName() << (unsigned)D1->getTagKind();
956 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000957 return false;
958 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000959
960 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
961 // If both anonymous structs/unions are in a record context, make sure
962 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +0000963 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
964 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000965 if (*Index1 != *Index2)
966 return false;
967 }
968 }
969 }
970
Douglas Gregore2e50d332010-12-01 01:36:18 +0000971 // If both declarations are class template specializations, we know
972 // the ODR applies, so check the template and template arguments.
973 ClassTemplateSpecializationDecl *Spec1
974 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
975 ClassTemplateSpecializationDecl *Spec2
976 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
977 if (Spec1 && Spec2) {
978 // Check that the specialized templates are the same.
979 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
980 Spec2->getSpecializedTemplate()))
981 return false;
982
983 // Check that the template arguments are the same.
984 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
985 return false;
986
987 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
988 if (!IsStructurallyEquivalent(Context,
989 Spec1->getTemplateArgs().get(I),
990 Spec2->getTemplateArgs().get(I)))
991 return false;
992 }
993 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000994 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000995 else if (Spec1 || Spec2)
996 return false;
997
Douglas Gregorb4964f72010-02-15 23:54:17 +0000998 // Compare the definitions of these two records. If either or both are
999 // incomplete, we assume that they are equivalent.
1000 D1 = D1->getDefinition();
1001 D2 = D2->getDefinition();
1002 if (!D1 || !D2)
1003 return true;
1004
Douglas Gregor3996e242010-02-15 22:01:00 +00001005 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1006 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1007 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001008 if (Context.Complain) {
1009 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1010 << Context.C2.getTypeDeclType(D2);
1011 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1012 << D2CXX->getNumBases();
1013 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1014 << D1CXX->getNumBases();
1015 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001016 return false;
1017 }
1018
1019 // Check the base classes.
1020 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1021 BaseEnd1 = D1CXX->bases_end(),
1022 Base2 = D2CXX->bases_begin();
1023 Base1 != BaseEnd1;
1024 ++Base1, ++Base2) {
1025 if (!IsStructurallyEquivalent(Context,
1026 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001027 if (Context.Complain) {
1028 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1029 << Context.C2.getTypeDeclType(D2);
1030 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1031 << Base2->getType()
1032 << Base2->getSourceRange();
1033 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1034 << Base1->getType()
1035 << Base1->getSourceRange();
1036 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001037 return false;
1038 }
1039
1040 // Check virtual vs. non-virtual inheritance mismatch.
1041 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001042 if (Context.Complain) {
1043 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1044 << Context.C2.getTypeDeclType(D2);
1045 Context.Diag2(Base2->getLocStart(),
1046 diag::note_odr_virtual_base)
1047 << Base2->isVirtual() << Base2->getSourceRange();
1048 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1049 << Base1->isVirtual()
1050 << Base1->getSourceRange();
1051 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001052 return false;
1053 }
1054 }
1055 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001056 if (Context.Complain) {
1057 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1058 << Context.C2.getTypeDeclType(D2);
1059 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1060 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1061 << Base1->getType()
1062 << Base1->getSourceRange();
1063 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1064 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001065 return false;
1066 }
1067 }
1068
1069 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001070 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001071 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001072 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001073 Field1End = D1->field_end();
1074 Field1 != Field1End;
1075 ++Field1, ++Field2) {
1076 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001077 if (Context.Complain) {
1078 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1079 << Context.C2.getTypeDeclType(D2);
1080 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1081 << Field1->getDeclName() << Field1->getType();
1082 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1083 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001084 return false;
1085 }
1086
David Blaikie40ed2972012-06-06 20:45:41 +00001087 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001088 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001089 }
1090
1091 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001092 if (Context.Complain) {
1093 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1094 << Context.C2.getTypeDeclType(D2);
1095 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1096 << Field2->getDeclName() << Field2->getType();
1097 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1098 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001099 return false;
1100 }
1101
1102 return true;
1103}
1104
1105/// \brief Determine structural equivalence of two enums.
1106static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1107 EnumDecl *D1, EnumDecl *D2) {
1108 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1109 EC2End = D2->enumerator_end();
1110 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1111 EC1End = D1->enumerator_end();
1112 EC1 != EC1End; ++EC1, ++EC2) {
1113 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001114 if (Context.Complain) {
1115 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1116 << Context.C2.getTypeDeclType(D2);
1117 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1118 << EC1->getDeclName()
1119 << EC1->getInitVal().toString(10);
1120 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1121 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001122 return false;
1123 }
1124
1125 llvm::APSInt Val1 = EC1->getInitVal();
1126 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001127 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001128 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001129 if (Context.Complain) {
1130 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1131 << Context.C2.getTypeDeclType(D2);
1132 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1133 << EC2->getDeclName()
1134 << EC2->getInitVal().toString(10);
1135 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1136 << EC1->getDeclName()
1137 << EC1->getInitVal().toString(10);
1138 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001139 return false;
1140 }
1141 }
1142
1143 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001144 if (Context.Complain) {
1145 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1146 << Context.C2.getTypeDeclType(D2);
1147 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1148 << EC2->getDeclName()
1149 << EC2->getInitVal().toString(10);
1150 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1151 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001152 return false;
1153 }
1154
1155 return true;
1156}
Douglas Gregora082a492010-11-30 19:14:50 +00001157
1158static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1159 TemplateParameterList *Params1,
1160 TemplateParameterList *Params2) {
1161 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001162 if (Context.Complain) {
1163 Context.Diag2(Params2->getTemplateLoc(),
1164 diag::err_odr_different_num_template_parameters)
1165 << Params1->size() << Params2->size();
1166 Context.Diag1(Params1->getTemplateLoc(),
1167 diag::note_odr_template_parameter_list);
1168 }
Douglas Gregora082a492010-11-30 19:14:50 +00001169 return false;
1170 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001171
Douglas Gregora082a492010-11-30 19:14:50 +00001172 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1173 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001174 if (Context.Complain) {
1175 Context.Diag2(Params2->getParam(I)->getLocation(),
1176 diag::err_odr_different_template_parameter_kind);
1177 Context.Diag1(Params1->getParam(I)->getLocation(),
1178 diag::note_odr_template_parameter_here);
1179 }
Douglas Gregora082a492010-11-30 19:14:50 +00001180 return false;
1181 }
1182
1183 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1184 Params2->getParam(I))) {
1185
1186 return false;
1187 }
1188 }
1189
1190 return true;
1191}
1192
1193static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1194 TemplateTypeParmDecl *D1,
1195 TemplateTypeParmDecl *D2) {
1196 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001197 if (Context.Complain) {
1198 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1199 << D2->isParameterPack();
1200 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1201 << D1->isParameterPack();
1202 }
Douglas Gregora082a492010-11-30 19:14:50 +00001203 return false;
1204 }
1205
1206 return true;
1207}
1208
1209static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1210 NonTypeTemplateParmDecl *D1,
1211 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001212 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001213 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001214 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1215 << D2->isParameterPack();
1216 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1217 << D1->isParameterPack();
1218 }
Douglas Gregora082a492010-11-30 19:14:50 +00001219 return false;
1220 }
Douglas Gregora082a492010-11-30 19:14:50 +00001221
1222 // Check types.
1223 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001224 if (Context.Complain) {
1225 Context.Diag2(D2->getLocation(),
1226 diag::err_odr_non_type_parameter_type_inconsistent)
1227 << D2->getType() << D1->getType();
1228 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1229 << D1->getType();
1230 }
Douglas Gregora082a492010-11-30 19:14:50 +00001231 return false;
1232 }
1233
1234 return true;
1235}
1236
1237static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1238 TemplateTemplateParmDecl *D1,
1239 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001240 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001241 if (Context.Complain) {
1242 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1243 << D2->isParameterPack();
1244 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1245 << D1->isParameterPack();
1246 }
Douglas Gregora082a492010-11-30 19:14:50 +00001247 return false;
1248 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001249
Douglas Gregora082a492010-11-30 19:14:50 +00001250 // Check template parameter lists.
1251 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1252 D2->getTemplateParameters());
1253}
1254
1255static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1256 ClassTemplateDecl *D1,
1257 ClassTemplateDecl *D2) {
1258 // Check template parameters.
1259 if (!IsStructurallyEquivalent(Context,
1260 D1->getTemplateParameters(),
1261 D2->getTemplateParameters()))
1262 return false;
1263
1264 // Check the templated declaration.
1265 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1266 D2->getTemplatedDecl());
1267}
1268
Douglas Gregor3996e242010-02-15 22:01:00 +00001269/// \brief Determine structural equivalence of two declarations.
1270static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1271 Decl *D1, Decl *D2) {
1272 // FIXME: Check for known structural equivalences via a callback of some sort.
1273
Douglas Gregorb4964f72010-02-15 23:54:17 +00001274 // Check whether we already know that these two declarations are not
1275 // structurally equivalent.
1276 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1277 D2->getCanonicalDecl())))
1278 return false;
1279
Douglas Gregor3996e242010-02-15 22:01:00 +00001280 // Determine whether we've already produced a tentative equivalence for D1.
1281 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1282 if (EquivToD1)
1283 return EquivToD1 == D2->getCanonicalDecl();
1284
1285 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1286 EquivToD1 = D2->getCanonicalDecl();
1287 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1288 return true;
1289}
1290
1291bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1292 Decl *D2) {
1293 if (!::IsStructurallyEquivalent(*this, D1, D2))
1294 return false;
1295
1296 return !Finish();
1297}
1298
1299bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1300 QualType T2) {
1301 if (!::IsStructurallyEquivalent(*this, T1, T2))
1302 return false;
1303
1304 return !Finish();
1305}
1306
1307bool StructuralEquivalenceContext::Finish() {
1308 while (!DeclsToCheck.empty()) {
1309 // Check the next declaration.
1310 Decl *D1 = DeclsToCheck.front();
1311 DeclsToCheck.pop_front();
1312
1313 Decl *D2 = TentativeEquivalences[D1];
1314 assert(D2 && "Unrecorded tentative equivalence?");
1315
Douglas Gregorb4964f72010-02-15 23:54:17 +00001316 bool Equivalent = true;
1317
Douglas Gregor3996e242010-02-15 22:01:00 +00001318 // FIXME: Switch on all declaration kinds. For now, we're just going to
1319 // check the obvious ones.
1320 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1321 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1322 // Check for equivalent structure names.
1323 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001324 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1325 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001326 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001327 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1328 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001329 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1330 !::IsStructurallyEquivalent(*this, Record1, Record2))
1331 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001332 } else {
1333 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001334 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001335 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001336 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001337 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1338 // Check for equivalent enum names.
1339 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001340 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1341 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001342 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001343 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1344 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001345 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1346 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1347 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001348 } else {
1349 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001350 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001351 }
Richard Smithdda56e42011-04-15 14:24:37 +00001352 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1353 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001354 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001355 Typedef2->getIdentifier()) ||
1356 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001357 Typedef1->getUnderlyingType(),
1358 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001359 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001360 } else {
1361 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001362 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001363 }
Douglas Gregora082a492010-11-30 19:14:50 +00001364 } else if (ClassTemplateDecl *ClassTemplate1
1365 = dyn_cast<ClassTemplateDecl>(D1)) {
1366 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1367 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1368 ClassTemplate2->getIdentifier()) ||
1369 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1370 Equivalent = false;
1371 } else {
1372 // Class template/non-class-template mismatch.
1373 Equivalent = false;
1374 }
1375 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1376 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1377 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1378 Equivalent = false;
1379 } else {
1380 // Kind mismatch.
1381 Equivalent = false;
1382 }
1383 } else if (NonTypeTemplateParmDecl *NTTP1
1384 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1385 if (NonTypeTemplateParmDecl *NTTP2
1386 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1387 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1388 Equivalent = false;
1389 } else {
1390 // Kind mismatch.
1391 Equivalent = false;
1392 }
1393 } else if (TemplateTemplateParmDecl *TTP1
1394 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1395 if (TemplateTemplateParmDecl *TTP2
1396 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1397 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1398 Equivalent = false;
1399 } else {
1400 // Kind mismatch.
1401 Equivalent = false;
1402 }
1403 }
1404
Douglas Gregorb4964f72010-02-15 23:54:17 +00001405 if (!Equivalent) {
1406 // Note that these two declarations are not equivalent (and we already
1407 // know about it).
1408 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1409 D2->getCanonicalDecl()));
1410 return true;
1411 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001412 // FIXME: Check other declaration kinds!
1413 }
1414
1415 return false;
1416}
1417
1418//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001419// Import Types
1420//----------------------------------------------------------------------------
1421
John McCall424cec92011-01-19 06:33:43 +00001422QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001423 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1424 << T->getTypeClassName();
1425 return QualType();
1426}
1427
John McCall424cec92011-01-19 06:33:43 +00001428QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001429 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001430#define SHARED_SINGLETON_TYPE(Expansion)
1431#define BUILTIN_TYPE(Id, SingletonId) \
1432 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1433#include "clang/AST/BuiltinTypes.def"
1434
1435 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1436 // context supports C++.
1437
1438 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1439 // context supports ObjC.
1440
Douglas Gregor96e578d2010-02-05 17:54:41 +00001441 case BuiltinType::Char_U:
1442 // The context we're importing from has an unsigned 'char'. If we're
1443 // importing into a context with a signed 'char', translate to
1444 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001445 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001446 return Importer.getToContext().UnsignedCharTy;
1447
1448 return Importer.getToContext().CharTy;
1449
Douglas Gregor96e578d2010-02-05 17:54:41 +00001450 case BuiltinType::Char_S:
1451 // The context we're importing from has an unsigned 'char'. If we're
1452 // importing into a context with a signed 'char', translate to
1453 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001454 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001455 return Importer.getToContext().SignedCharTy;
1456
1457 return Importer.getToContext().CharTy;
1458
Chris Lattnerad3467e2010-12-25 23:25:43 +00001459 case BuiltinType::WChar_S:
1460 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001461 // FIXME: If not in C++, shall we translate to the C equivalent of
1462 // wchar_t?
1463 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001464 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001465
1466 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001467}
1468
John McCall424cec92011-01-19 06:33:43 +00001469QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001470 QualType ToElementType = Importer.Import(T->getElementType());
1471 if (ToElementType.isNull())
1472 return QualType();
1473
1474 return Importer.getToContext().getComplexType(ToElementType);
1475}
1476
John McCall424cec92011-01-19 06:33:43 +00001477QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001478 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1479 if (ToPointeeType.isNull())
1480 return QualType();
1481
1482 return Importer.getToContext().getPointerType(ToPointeeType);
1483}
1484
John McCall424cec92011-01-19 06:33:43 +00001485QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486 // FIXME: Check for blocks support in "to" context.
1487 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1488 if (ToPointeeType.isNull())
1489 return QualType();
1490
1491 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1492}
1493
John McCall424cec92011-01-19 06:33:43 +00001494QualType
1495ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001496 // FIXME: Check for C++ support in "to" context.
1497 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1498 if (ToPointeeType.isNull())
1499 return QualType();
1500
1501 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1502}
1503
John McCall424cec92011-01-19 06:33:43 +00001504QualType
1505ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506 // FIXME: Check for C++0x support in "to" context.
1507 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1508 if (ToPointeeType.isNull())
1509 return QualType();
1510
1511 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1512}
1513
John McCall424cec92011-01-19 06:33:43 +00001514QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 // FIXME: Check for C++ support in "to" context.
1516 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1517 if (ToPointeeType.isNull())
1518 return QualType();
1519
1520 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1521 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1522 ClassType.getTypePtr());
1523}
1524
John McCall424cec92011-01-19 06:33:43 +00001525QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001526 QualType ToElementType = Importer.Import(T->getElementType());
1527 if (ToElementType.isNull())
1528 return QualType();
1529
1530 return Importer.getToContext().getConstantArrayType(ToElementType,
1531 T->getSize(),
1532 T->getSizeModifier(),
1533 T->getIndexTypeCVRQualifiers());
1534}
1535
John McCall424cec92011-01-19 06:33:43 +00001536QualType
1537ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001538 QualType ToElementType = Importer.Import(T->getElementType());
1539 if (ToElementType.isNull())
1540 return QualType();
1541
1542 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1543 T->getSizeModifier(),
1544 T->getIndexTypeCVRQualifiers());
1545}
1546
John McCall424cec92011-01-19 06:33:43 +00001547QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001548 QualType ToElementType = Importer.Import(T->getElementType());
1549 if (ToElementType.isNull())
1550 return QualType();
1551
1552 Expr *Size = Importer.Import(T->getSizeExpr());
1553 if (!Size)
1554 return QualType();
1555
1556 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1557 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1558 T->getSizeModifier(),
1559 T->getIndexTypeCVRQualifiers(),
1560 Brackets);
1561}
1562
John McCall424cec92011-01-19 06:33:43 +00001563QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001564 QualType ToElementType = Importer.Import(T->getElementType());
1565 if (ToElementType.isNull())
1566 return QualType();
1567
1568 return Importer.getToContext().getVectorType(ToElementType,
1569 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001570 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571}
1572
John McCall424cec92011-01-19 06:33:43 +00001573QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001574 QualType ToElementType = Importer.Import(T->getElementType());
1575 if (ToElementType.isNull())
1576 return QualType();
1577
1578 return Importer.getToContext().getExtVectorType(ToElementType,
1579 T->getNumElements());
1580}
1581
John McCall424cec92011-01-19 06:33:43 +00001582QualType
1583ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001584 // FIXME: What happens if we're importing a function without a prototype
1585 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001586 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001587 if (ToResultType.isNull())
1588 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001589
Douglas Gregor96e578d2010-02-05 17:54:41 +00001590 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001591 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592}
1593
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001594QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001595 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001596 if (ToResultType.isNull())
1597 return QualType();
1598
1599 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001600 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001601 for (const auto &A : T->param_types()) {
1602 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001603 if (ArgType.isNull())
1604 return QualType();
1605 ArgTypes.push_back(ArgType);
1606 }
1607
1608 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001609 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001610 for (const auto &E : T->exceptions()) {
1611 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001612 if (ExceptionType.isNull())
1613 return QualType();
1614 ExceptionTypes.push_back(ExceptionType);
1615 }
John McCalldb40c7f2010-12-14 08:05:40 +00001616
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001617 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1618 FunctionProtoType::ExtProtoInfo ToEPI;
1619
1620 ToEPI.ExtInfo = FromEPI.ExtInfo;
1621 ToEPI.Variadic = FromEPI.Variadic;
1622 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1623 ToEPI.TypeQuals = FromEPI.TypeQuals;
1624 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001625 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1626 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1627 ToEPI.ExceptionSpec.NoexceptExpr =
1628 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1629 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1630 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1631 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1632 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001633
Jordan Rose5c382722013-03-08 21:51:21 +00001634 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001635}
1636
Sean Callananda6df8a2011-08-11 16:56:07 +00001637QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1638 QualType ToInnerType = Importer.Import(T->getInnerType());
1639 if (ToInnerType.isNull())
1640 return QualType();
1641
1642 return Importer.getToContext().getParenType(ToInnerType);
1643}
1644
John McCall424cec92011-01-19 06:33:43 +00001645QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001646 TypedefNameDecl *ToDecl
1647 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001648 if (!ToDecl)
1649 return QualType();
1650
1651 return Importer.getToContext().getTypeDeclType(ToDecl);
1652}
1653
John McCall424cec92011-01-19 06:33:43 +00001654QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001655 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1656 if (!ToExpr)
1657 return QualType();
1658
1659 return Importer.getToContext().getTypeOfExprType(ToExpr);
1660}
1661
John McCall424cec92011-01-19 06:33:43 +00001662QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001663 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1664 if (ToUnderlyingType.isNull())
1665 return QualType();
1666
1667 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1668}
1669
John McCall424cec92011-01-19 06:33:43 +00001670QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001671 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001672 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1673 if (!ToExpr)
1674 return QualType();
1675
Douglas Gregor81495f32012-02-12 18:42:33 +00001676 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1677 if (UnderlyingType.isNull())
1678 return QualType();
1679
1680 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001681}
1682
Alexis Hunte852b102011-05-24 22:41:36 +00001683QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1684 QualType ToBaseType = Importer.Import(T->getBaseType());
1685 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1686 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1687 return QualType();
1688
1689 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1690 ToUnderlyingType,
1691 T->getUTTKind());
1692}
1693
Richard Smith30482bc2011-02-20 03:19:35 +00001694QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001695 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001696 QualType FromDeduced = T->getDeducedType();
1697 QualType ToDeduced;
1698 if (!FromDeduced.isNull()) {
1699 ToDeduced = Importer.Import(FromDeduced);
1700 if (ToDeduced.isNull())
1701 return QualType();
1702 }
1703
Faisal Vali2b391ab2013-09-26 19:54:12 +00001704 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1705 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001706}
1707
John McCall424cec92011-01-19 06:33:43 +00001708QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001709 RecordDecl *ToDecl
1710 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1711 if (!ToDecl)
1712 return QualType();
1713
1714 return Importer.getToContext().getTagDeclType(ToDecl);
1715}
1716
John McCall424cec92011-01-19 06:33:43 +00001717QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001718 EnumDecl *ToDecl
1719 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1720 if (!ToDecl)
1721 return QualType();
1722
1723 return Importer.getToContext().getTagDeclType(ToDecl);
1724}
1725
Douglas Gregore2e50d332010-12-01 01:36:18 +00001726QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001727 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001728 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1729 if (ToTemplate.isNull())
1730 return QualType();
1731
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001732 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001733 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1734 return QualType();
1735
1736 QualType ToCanonType;
1737 if (!QualType(T, 0).isCanonical()) {
1738 QualType FromCanonType
1739 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1740 ToCanonType =Importer.Import(FromCanonType);
1741 if (ToCanonType.isNull())
1742 return QualType();
1743 }
1744 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1745 ToTemplateArgs.data(),
1746 ToTemplateArgs.size(),
1747 ToCanonType);
1748}
1749
John McCall424cec92011-01-19 06:33:43 +00001750QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001751 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001752 // Note: the qualifier in an ElaboratedType is optional.
1753 if (T->getQualifier()) {
1754 ToQualifier = Importer.Import(T->getQualifier());
1755 if (!ToQualifier)
1756 return QualType();
1757 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758
1759 QualType ToNamedType = Importer.Import(T->getNamedType());
1760 if (ToNamedType.isNull())
1761 return QualType();
1762
Abramo Bagnara6150c882010-05-11 21:36:43 +00001763 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1764 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765}
1766
John McCall424cec92011-01-19 06:33:43 +00001767QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001768 ObjCInterfaceDecl *Class
1769 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1770 if (!Class)
1771 return QualType();
1772
John McCall8b07ec22010-05-15 11:32:37 +00001773 return Importer.getToContext().getObjCInterfaceType(Class);
1774}
1775
John McCall424cec92011-01-19 06:33:43 +00001776QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001777 QualType ToBaseType = Importer.Import(T->getBaseType());
1778 if (ToBaseType.isNull())
1779 return QualType();
1780
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001781 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001782 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001783 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001784 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001785 if (!Protocol)
1786 return QualType();
1787 Protocols.push_back(Protocol);
1788 }
1789
John McCall8b07ec22010-05-15 11:32:37 +00001790 return Importer.getToContext().getObjCObjectType(ToBaseType,
1791 Protocols.data(),
1792 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001793}
1794
John McCall424cec92011-01-19 06:33:43 +00001795QualType
1796ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001797 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1798 if (ToPointeeType.isNull())
1799 return QualType();
1800
John McCall8b07ec22010-05-15 11:32:37 +00001801 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001802}
1803
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001804//----------------------------------------------------------------------------
1805// Import Declarations
1806//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001807bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1808 DeclContext *&LexicalDC,
1809 DeclarationName &Name,
1810 SourceLocation &Loc) {
1811 // Import the context of this declaration.
1812 DC = Importer.ImportContext(D->getDeclContext());
1813 if (!DC)
1814 return true;
1815
1816 LexicalDC = DC;
1817 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1818 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1819 if (!LexicalDC)
1820 return true;
1821 }
1822
1823 // Import the name of this declaration.
1824 Name = Importer.Import(D->getDeclName());
1825 if (D->getDeclName() && !Name)
1826 return true;
1827
1828 // Import the location of this declaration.
1829 Loc = Importer.Import(D->getLocation());
1830 return false;
1831}
1832
Douglas Gregord451ea92011-07-29 23:31:30 +00001833void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1834 if (!FromD)
1835 return;
1836
1837 if (!ToD) {
1838 ToD = Importer.Import(FromD);
1839 if (!ToD)
1840 return;
1841 }
1842
1843 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1844 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001845 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001846 ImportDefinition(FromRecord, ToRecord);
1847 }
1848 }
1849 return;
1850 }
1851
1852 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1853 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1854 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1855 ImportDefinition(FromEnum, ToEnum);
1856 }
1857 }
1858 return;
1859 }
1860}
1861
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001862void
1863ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1864 DeclarationNameInfo& To) {
1865 // NOTE: To.Name and To.Loc are already imported.
1866 // We only have to import To.LocInfo.
1867 switch (To.getName().getNameKind()) {
1868 case DeclarationName::Identifier:
1869 case DeclarationName::ObjCZeroArgSelector:
1870 case DeclarationName::ObjCOneArgSelector:
1871 case DeclarationName::ObjCMultiArgSelector:
1872 case DeclarationName::CXXUsingDirective:
1873 return;
1874
1875 case DeclarationName::CXXOperatorName: {
1876 SourceRange Range = From.getCXXOperatorNameRange();
1877 To.setCXXOperatorNameRange(Importer.Import(Range));
1878 return;
1879 }
1880 case DeclarationName::CXXLiteralOperatorName: {
1881 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1882 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1883 return;
1884 }
1885 case DeclarationName::CXXConstructorName:
1886 case DeclarationName::CXXDestructorName:
1887 case DeclarationName::CXXConversionFunctionName: {
1888 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1889 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1890 return;
1891 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001892 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001893 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001894}
1895
Douglas Gregor2e15c842012-02-01 21:00:38 +00001896void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001897 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001898 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001899 return;
1900 }
1901
Aaron Ballman629afae2014-03-07 19:56:05 +00001902 for (auto *From : FromDC->decls())
1903 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001904}
1905
Douglas Gregord451ea92011-07-29 23:31:30 +00001906bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001907 ImportDefinitionKind Kind) {
1908 if (To->getDefinition() || To->isBeingDefined()) {
1909 if (Kind == IDK_Everything)
1910 ImportDeclContext(From, /*ForceImport=*/true);
1911
Douglas Gregore2e50d332010-12-01 01:36:18 +00001912 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001913 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001914
1915 To->startDefinition();
1916
1917 // Add base classes.
1918 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1919 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001920
1921 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1922 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1923 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001924 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001925 ToData.Aggregate = FromData.Aggregate;
1926 ToData.PlainOldData = FromData.PlainOldData;
1927 ToData.Empty = FromData.Empty;
1928 ToData.Polymorphic = FromData.Polymorphic;
1929 ToData.Abstract = FromData.Abstract;
1930 ToData.IsStandardLayout = FromData.IsStandardLayout;
1931 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1932 ToData.HasPrivateFields = FromData.HasPrivateFields;
1933 ToData.HasProtectedFields = FromData.HasProtectedFields;
1934 ToData.HasPublicFields = FromData.HasPublicFields;
1935 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001936 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001937 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001938 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001939 ToData.HasUninitializedReferenceMember
1940 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00001941 ToData.NeedOverloadResolutionForMoveConstructor
1942 = FromData.NeedOverloadResolutionForMoveConstructor;
1943 ToData.NeedOverloadResolutionForMoveAssignment
1944 = FromData.NeedOverloadResolutionForMoveAssignment;
1945 ToData.NeedOverloadResolutionForDestructor
1946 = FromData.NeedOverloadResolutionForDestructor;
1947 ToData.DefaultedMoveConstructorIsDeleted
1948 = FromData.DefaultedMoveConstructorIsDeleted;
1949 ToData.DefaultedMoveAssignmentIsDeleted
1950 = FromData.DefaultedMoveAssignmentIsDeleted;
1951 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001952 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1953 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001954 ToData.HasConstexprNonCopyMoveConstructor
1955 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001956 ToData.DefaultedDefaultConstructorIsConstexpr
1957 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001958 ToData.HasConstexprDefaultConstructor
1959 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001960 ToData.HasNonLiteralTypeFieldsOrBases
1961 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001962 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001963 ToData.UserProvidedDefaultConstructor
1964 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001965 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00001966 ToData.ImplicitCopyConstructorHasConstParam
1967 = FromData.ImplicitCopyConstructorHasConstParam;
1968 ToData.ImplicitCopyAssignmentHasConstParam
1969 = FromData.ImplicitCopyAssignmentHasConstParam;
1970 ToData.HasDeclaredCopyConstructorWithConstParam
1971 = FromData.HasDeclaredCopyConstructorWithConstParam;
1972 ToData.HasDeclaredCopyAssignmentWithConstParam
1973 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001974 ToData.IsLambda = FromData.IsLambda;
1975
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001976 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00001977 for (const auto &Base1 : FromCXX->bases()) {
1978 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001979 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001980 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001981
1982 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001983 if (Base1.isPackExpansion())
1984 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001985
1986 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00001987 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00001988
Douglas Gregore2e50d332010-12-01 01:36:18 +00001989 Bases.push_back(
1990 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00001991 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
1992 Base1.isVirtual(),
1993 Base1.isBaseOfClass(),
1994 Base1.getAccessSpecifierAsWritten(),
1995 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00001996 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001997 }
1998 if (!Bases.empty())
1999 ToCXX->setBases(Bases.data(), Bases.size());
2000 }
2001
Douglas Gregor2e15c842012-02-01 21:00:38 +00002002 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002003 ImportDeclContext(From, /*ForceImport=*/true);
2004
Douglas Gregore2e50d332010-12-01 01:36:18 +00002005 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002006 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002007}
2008
Larisse Voufo39a1e502013-08-06 01:03:05 +00002009bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2010 ImportDefinitionKind Kind) {
2011 if (To->getDefinition())
2012 return false;
2013
2014 // FIXME: Can we really import any initializer? Alternatively, we could force
2015 // ourselves to import every declaration of a variable and then only use
2016 // getInit() here.
2017 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2018
2019 // FIXME: Other bits to merge?
2020
2021 return false;
2022}
2023
Douglas Gregord451ea92011-07-29 23:31:30 +00002024bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002025 ImportDefinitionKind Kind) {
2026 if (To->getDefinition() || To->isBeingDefined()) {
2027 if (Kind == IDK_Everything)
2028 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002029 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002030 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002031
2032 To->startDefinition();
2033
2034 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2035 if (T.isNull())
2036 return true;
2037
2038 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2039 if (ToPromotionType.isNull())
2040 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002041
2042 if (shouldForceImportDeclContext(Kind))
2043 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002044
2045 // FIXME: we might need to merge the number of positive or negative bits
2046 // if the enumerator lists don't match.
2047 To->completeDefinition(T, ToPromotionType,
2048 From->getNumPositiveBits(),
2049 From->getNumNegativeBits());
2050 return false;
2051}
2052
Douglas Gregora082a492010-11-30 19:14:50 +00002053TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2054 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002055 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002056 ToParams.reserve(Params->size());
2057 for (TemplateParameterList::iterator P = Params->begin(),
2058 PEnd = Params->end();
2059 P != PEnd; ++P) {
2060 Decl *To = Importer.Import(*P);
2061 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002062 return nullptr;
2063
Douglas Gregora082a492010-11-30 19:14:50 +00002064 ToParams.push_back(cast<NamedDecl>(To));
2065 }
2066
2067 return TemplateParameterList::Create(Importer.getToContext(),
2068 Importer.Import(Params->getTemplateLoc()),
2069 Importer.Import(Params->getLAngleLoc()),
2070 ToParams.data(), ToParams.size(),
2071 Importer.Import(Params->getRAngleLoc()));
2072}
2073
Douglas Gregore2e50d332010-12-01 01:36:18 +00002074TemplateArgument
2075ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2076 switch (From.getKind()) {
2077 case TemplateArgument::Null:
2078 return TemplateArgument();
2079
2080 case TemplateArgument::Type: {
2081 QualType ToType = Importer.Import(From.getAsType());
2082 if (ToType.isNull())
2083 return TemplateArgument();
2084 return TemplateArgument(ToType);
2085 }
2086
2087 case TemplateArgument::Integral: {
2088 QualType ToType = Importer.Import(From.getIntegralType());
2089 if (ToType.isNull())
2090 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002091 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002092 }
2093
Eli Friedmanb826a002012-09-26 02:36:12 +00002094 case TemplateArgument::Declaration: {
2095 ValueDecl *FromD = From.getAsDecl();
2096 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2097 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002098 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002099 }
2100
2101 case TemplateArgument::NullPtr: {
2102 QualType ToType = Importer.Import(From.getNullPtrType());
2103 if (ToType.isNull())
2104 return TemplateArgument();
2105 return TemplateArgument(ToType, /*isNullPtr*/true);
2106 }
2107
Douglas Gregore2e50d332010-12-01 01:36:18 +00002108 case TemplateArgument::Template: {
2109 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2110 if (ToTemplate.isNull())
2111 return TemplateArgument();
2112
2113 return TemplateArgument(ToTemplate);
2114 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002115
2116 case TemplateArgument::TemplateExpansion: {
2117 TemplateName ToTemplate
2118 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2119 if (ToTemplate.isNull())
2120 return TemplateArgument();
2121
Douglas Gregore1d60df2011-01-14 23:41:42 +00002122 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002123 }
2124
Douglas Gregore2e50d332010-12-01 01:36:18 +00002125 case TemplateArgument::Expression:
2126 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2127 return TemplateArgument(ToExpr);
2128 return TemplateArgument();
2129
2130 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002131 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002132 ToPack.reserve(From.pack_size());
2133 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2134 return TemplateArgument();
2135
2136 TemplateArgument *ToArgs
2137 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2138 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2139 return TemplateArgument(ToArgs, ToPack.size());
2140 }
2141 }
2142
2143 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002144}
2145
2146bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2147 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002148 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002149 for (unsigned I = 0; I != NumFromArgs; ++I) {
2150 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2151 if (To.isNull() && !FromArgs[I].isNull())
2152 return true;
2153
2154 ToArgs.push_back(To);
2155 }
2156
2157 return false;
2158}
2159
Douglas Gregor5c73e912010-02-11 00:48:18 +00002160bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002161 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002162 // Eliminate a potential failure point where we attempt to re-import
2163 // something we're trying to import while completing ToRecord.
2164 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2165 if (ToOrigin) {
2166 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2167 if (ToOriginRecord)
2168 ToRecord = ToOriginRecord;
2169 }
2170
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002171 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002172 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002173 Importer.getNonEquivalentDecls(),
2174 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002175 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002176}
2177
Larisse Voufo39a1e502013-08-06 01:03:05 +00002178bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2179 bool Complain) {
2180 StructuralEquivalenceContext Ctx(
2181 Importer.getFromContext(), Importer.getToContext(),
2182 Importer.getNonEquivalentDecls(), false, Complain);
2183 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2184}
2185
Douglas Gregor98c10182010-02-12 22:17:39 +00002186bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002187 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002188 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002189 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002190 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002191}
2192
Douglas Gregor91155082012-11-14 22:29:20 +00002193bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2194 EnumConstantDecl *ToEC)
2195{
2196 const llvm::APSInt &FromVal = FromEC->getInitVal();
2197 const llvm::APSInt &ToVal = ToEC->getInitVal();
2198
2199 return FromVal.isSigned() == ToVal.isSigned() &&
2200 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2201 FromVal == ToVal;
2202}
2203
2204bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002205 ClassTemplateDecl *To) {
2206 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2207 Importer.getToContext(),
2208 Importer.getNonEquivalentDecls());
2209 return Ctx.IsStructurallyEquivalent(From, To);
2210}
2211
Larisse Voufo39a1e502013-08-06 01:03:05 +00002212bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2213 VarTemplateDecl *To) {
2214 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2215 Importer.getToContext(),
2216 Importer.getNonEquivalentDecls());
2217 return Ctx.IsStructurallyEquivalent(From, To);
2218}
2219
Douglas Gregore4c83e42010-02-09 22:48:33 +00002220Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002221 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002222 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002223 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002224}
2225
Sean Callanan65198272011-11-17 23:20:56 +00002226Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2227 TranslationUnitDecl *ToD =
2228 Importer.getToContext().getTranslationUnitDecl();
2229
2230 Importer.Imported(D, ToD);
2231
2232 return ToD;
2233}
2234
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002235Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2236 // Import the major distinguishing characteristics of this namespace.
2237 DeclContext *DC, *LexicalDC;
2238 DeclarationName Name;
2239 SourceLocation Loc;
2240 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002241 return nullptr;
2242
2243 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002244 if (!Name) {
2245 // This is an anonymous namespace. Adopt an existing anonymous
2246 // namespace if we can.
2247 // FIXME: Not testable.
2248 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2249 MergeWithNamespace = TU->getAnonymousNamespace();
2250 else
2251 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2252 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002253 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002254 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002255 DC->localUncachedLookup(Name, FoundDecls);
2256 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2257 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002258 continue;
2259
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002260 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002261 MergeWithNamespace = FoundNS;
2262 ConflictingDecls.clear();
2263 break;
2264 }
2265
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002266 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002267 }
2268
2269 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002270 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002271 ConflictingDecls.data(),
2272 ConflictingDecls.size());
2273 }
2274 }
2275
2276 // Create the "to" namespace, if needed.
2277 NamespaceDecl *ToNamespace = MergeWithNamespace;
2278 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002279 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002280 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002281 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002282 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002283 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002284 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002285 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002286
2287 // If this is an anonymous namespace, register it as the anonymous
2288 // namespace within its context.
2289 if (!Name) {
2290 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2291 TU->setAnonymousNamespace(ToNamespace);
2292 else
2293 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2294 }
2295 }
2296 Importer.Imported(D, ToNamespace);
2297
2298 ImportDeclContext(D);
2299
2300 return ToNamespace;
2301}
2302
Richard Smithdda56e42011-04-15 14:24:37 +00002303Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002304 // Import the major distinguishing characteristics of this typedef.
2305 DeclContext *DC, *LexicalDC;
2306 DeclarationName Name;
2307 SourceLocation Loc;
2308 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002309 return nullptr;
2310
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002311 // If this typedef is not in block scope, determine whether we've
2312 // seen a typedef with the same name (that we can merge with) or any
2313 // other entity by that name (which name lookup could conflict with).
2314 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002315 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002316 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002317 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002318 DC->localUncachedLookup(Name, FoundDecls);
2319 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2320 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002321 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002322 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002323 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002324 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2325 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002326 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002327 }
2328
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002329 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002330 }
2331
2332 if (!ConflictingDecls.empty()) {
2333 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2334 ConflictingDecls.data(),
2335 ConflictingDecls.size());
2336 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002337 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002338 }
2339 }
2340
Douglas Gregorb4964f72010-02-15 23:54:17 +00002341 // Import the underlying type of this typedef;
2342 QualType T = Importer.Import(D->getUnderlyingType());
2343 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002344 return nullptr;
2345
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002346 // Create the new typedef node.
2347 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002348 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002349 TypedefNameDecl *ToTypedef;
2350 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002351 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2352 StartL, Loc,
2353 Name.getAsIdentifierInfo(),
2354 TInfo);
2355 else
Richard Smithdda56e42011-04-15 14:24:37 +00002356 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2357 StartL, Loc,
2358 Name.getAsIdentifierInfo(),
2359 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002360
Douglas Gregordd483172010-02-22 17:42:47 +00002361 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002362 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002363 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002364 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002365
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002366 return ToTypedef;
2367}
2368
Richard Smithdda56e42011-04-15 14:24:37 +00002369Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2370 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2371}
2372
2373Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2374 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2375}
2376
Douglas Gregor98c10182010-02-12 22:17:39 +00002377Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2378 // Import the major distinguishing characteristics of this enum.
2379 DeclContext *DC, *LexicalDC;
2380 DeclarationName Name;
2381 SourceLocation Loc;
2382 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002383 return nullptr;
2384
Douglas Gregor98c10182010-02-12 22:17:39 +00002385 // Figure out what enum name we're looking for.
2386 unsigned IDNS = Decl::IDNS_Tag;
2387 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002388 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2389 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002390 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002391 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002392 IDNS |= Decl::IDNS_Ordinary;
2393
2394 // We may already have an enum of the same name; try to find and match it.
2395 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002396 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002397 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002398 DC->localUncachedLookup(SearchName, FoundDecls);
2399 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2400 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002401 continue;
2402
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002403 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002404 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002405 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2406 Found = Tag->getDecl();
2407 }
2408
2409 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002410 if (IsStructuralMatch(D, FoundEnum))
2411 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002412 }
2413
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002414 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002415 }
2416
2417 if (!ConflictingDecls.empty()) {
2418 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2419 ConflictingDecls.data(),
2420 ConflictingDecls.size());
2421 }
2422 }
2423
2424 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002425 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2426 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002427 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002428 D->isScoped(), D->isScopedUsingClassTag(),
2429 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002430 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002431 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002432 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002433 D2->setLexicalDeclContext(LexicalDC);
2434 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002435 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002436
2437 // Import the integer type.
2438 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2439 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002440 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002441 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002442
2443 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002444 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002445 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002446
Douglas Gregor3996e242010-02-15 22:01:00 +00002447 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002448}
2449
Douglas Gregor5c73e912010-02-11 00:48:18 +00002450Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2451 // If this record has a definition in the translation unit we're coming from,
2452 // but this particular declaration is not that definition, import the
2453 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002454 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002455 if (Definition && Definition != D) {
2456 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002457 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002458 return nullptr;
2459
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002460 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002461 }
2462
2463 // Import the major distinguishing characteristics of this record.
2464 DeclContext *DC, *LexicalDC;
2465 DeclarationName Name;
2466 SourceLocation Loc;
2467 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002468 return nullptr;
2469
Douglas Gregor5c73e912010-02-11 00:48:18 +00002470 // Figure out what structure name we're looking for.
2471 unsigned IDNS = Decl::IDNS_Tag;
2472 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002473 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2474 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002475 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002476 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002477 IDNS |= Decl::IDNS_Ordinary;
2478
2479 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002480 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002481 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002482 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002483 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002484 DC->localUncachedLookup(SearchName, FoundDecls);
2485 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2486 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002487 continue;
2488
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002489 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002490 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002491 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2492 Found = Tag->getDecl();
2493 }
2494
2495 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002496 if (D->isAnonymousStructOrUnion() &&
2497 FoundRecord->isAnonymousStructOrUnion()) {
2498 // If both anonymous structs/unions are in a record context, make sure
2499 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002500 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002501 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002502 if (Optional<unsigned> Index2 =
2503 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002504 if (*Index1 != *Index2)
2505 continue;
2506 }
2507 }
2508 }
2509
Douglas Gregor25791052010-02-12 00:09:27 +00002510 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002511 if ((SearchName && !D->isCompleteDefinition())
2512 || (D->isCompleteDefinition() &&
2513 D->isAnonymousStructOrUnion()
2514 == FoundDef->isAnonymousStructOrUnion() &&
2515 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002516 // The record types structurally match, or the "from" translation
2517 // unit only had a forward declaration anyway; call it the same
2518 // function.
2519 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002520 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002521 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002522 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002523 // We have a forward declaration of this type, so adopt that forward
2524 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002525
2526 // If one or both can be completed from external storage then try one
2527 // last time to complete and compare them before doing this.
2528
2529 if (FoundRecord->hasExternalLexicalStorage() &&
2530 !FoundRecord->isCompleteDefinition())
2531 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2532 if (D->hasExternalLexicalStorage())
2533 D->getASTContext().getExternalSource()->CompleteType(D);
2534
2535 if (FoundRecord->isCompleteDefinition() &&
2536 D->isCompleteDefinition() &&
2537 !IsStructuralMatch(D, FoundRecord))
2538 continue;
2539
Douglas Gregor25791052010-02-12 00:09:27 +00002540 AdoptDecl = FoundRecord;
2541 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002542 } else if (!SearchName) {
2543 continue;
2544 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002545 }
2546
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002547 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002548 }
2549
Douglas Gregordd6006f2012-07-17 21:16:27 +00002550 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002551 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2552 ConflictingDecls.data(),
2553 ConflictingDecls.size());
2554 }
2555 }
2556
2557 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002558 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002559 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002560 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002561 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002562 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002563 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002564 DC, StartLoc, Loc,
2565 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002566 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002567 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002568 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002569 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002570 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002571 }
Douglas Gregor14454802011-02-25 02:25:35 +00002572
2573 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002574 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002575 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002576 if (D->isAnonymousStructOrUnion())
2577 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002578 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002579
Douglas Gregor3996e242010-02-15 22:01:00 +00002580 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002581
Douglas Gregor95d82832012-01-24 18:36:04 +00002582 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002583 return nullptr;
2584
Douglas Gregor3996e242010-02-15 22:01:00 +00002585 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002586}
2587
Douglas Gregor98c10182010-02-12 22:17:39 +00002588Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2589 // Import the major distinguishing characteristics of this enumerator.
2590 DeclContext *DC, *LexicalDC;
2591 DeclarationName Name;
2592 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002593 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002594 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002595
2596 QualType T = Importer.Import(D->getType());
2597 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002598 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002599
Douglas Gregor98c10182010-02-12 22:17:39 +00002600 // Determine whether there are any other declarations with the same name and
2601 // in the same context.
2602 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002603 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002604 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002605 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002606 DC->localUncachedLookup(Name, FoundDecls);
2607 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2608 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002609 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002610
2611 if (EnumConstantDecl *FoundEnumConstant
2612 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2613 if (IsStructuralMatch(D, FoundEnumConstant))
2614 return Importer.Imported(D, FoundEnumConstant);
2615 }
2616
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002617 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002618 }
2619
2620 if (!ConflictingDecls.empty()) {
2621 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2622 ConflictingDecls.data(),
2623 ConflictingDecls.size());
2624 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002625 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002626 }
2627 }
2628
2629 Expr *Init = Importer.Import(D->getInitExpr());
2630 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002631 return nullptr;
2632
Douglas Gregor98c10182010-02-12 22:17:39 +00002633 EnumConstantDecl *ToEnumerator
2634 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2635 Name.getAsIdentifierInfo(), T,
2636 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002637 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002638 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002639 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002640 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002641 return ToEnumerator;
2642}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002643
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002644Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2645 // Import the major distinguishing characteristics of this function.
2646 DeclContext *DC, *LexicalDC;
2647 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002648 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002649 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002650 return nullptr;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002651
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002652 // Try to find a function in our own ("to") context with the same name, same
2653 // type, and in the same context as the function we're importing.
2654 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002655 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002656 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002657 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002658 DC->localUncachedLookup(Name, FoundDecls);
2659 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2660 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002661 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002662
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002663 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002664 if (FoundFunction->hasExternalFormalLinkage() &&
2665 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002666 if (Importer.IsStructurallyEquivalent(D->getType(),
2667 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002668 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002669 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002670 }
2671
2672 // FIXME: Check for overloading more carefully, e.g., by boosting
2673 // Sema::IsOverload out to the AST library.
2674
2675 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002676 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002677 continue;
2678
2679 // Complain about inconsistent function types.
2680 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002681 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002682 Importer.ToDiag(FoundFunction->getLocation(),
2683 diag::note_odr_value_here)
2684 << FoundFunction->getType();
2685 }
2686 }
2687
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002688 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002689 }
2690
2691 if (!ConflictingDecls.empty()) {
2692 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2693 ConflictingDecls.data(),
2694 ConflictingDecls.size());
2695 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002696 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002697 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002698 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002699
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002700 DeclarationNameInfo NameInfo(Name, Loc);
2701 // Import additional name location/type info.
2702 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2703
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002704 QualType FromTy = D->getType();
2705 bool usedDifferentExceptionSpec = false;
2706
2707 if (const FunctionProtoType *
2708 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2709 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2710 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2711 // FunctionDecl that we are importing the FunctionProtoType for.
2712 // To avoid an infinite recursion when importing, create the FunctionDecl
2713 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002714 if (FromEPI.ExceptionSpec.SourceDecl ||
2715 FromEPI.ExceptionSpec.SourceTemplate ||
2716 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002717 FunctionProtoType::ExtProtoInfo DefaultEPI;
2718 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002719 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002720 usedDifferentExceptionSpec = true;
2721 }
2722 }
2723
Douglas Gregorb4964f72010-02-15 23:54:17 +00002724 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002725 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002726 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002727 return nullptr;
2728
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002729 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002730 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002731 for (auto P : D->params()) {
2732 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002733 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002734 return nullptr;
2735
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002736 Parameters.push_back(ToP);
2737 }
2738
2739 // Create the imported function.
2740 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002741 FunctionDecl *ToFunction = nullptr;
Douglas Gregor00eace12010-02-21 18:29:16 +00002742 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2743 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2744 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002745 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002746 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002747 FromConstructor->isExplicit(),
2748 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002749 D->isImplicit(),
2750 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002751 } else if (isa<CXXDestructorDecl>(D)) {
2752 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2753 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002754 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002755 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002756 D->isInlineSpecified(),
2757 D->isImplicit());
2758 } else if (CXXConversionDecl *FromConversion
2759 = dyn_cast<CXXConversionDecl>(D)) {
2760 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2761 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002762 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002763 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002764 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002765 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002766 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002767 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002768 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2769 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2770 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002771 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002772 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002773 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002774 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002775 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002776 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002777 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002778 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002779 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002780 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002781 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002782 D->hasWrittenPrototype(),
2783 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002784 }
John McCall3e11ebe2010-03-15 10:12:16 +00002785
2786 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002787 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002788 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002789 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002790 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2791 ToFunction->setTrivial(D->isTrivial());
2792 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002793 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002794
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002795 // Set the parameters.
2796 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002797 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002798 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002799 }
David Blaikie9c70e042011-09-21 18:16:56 +00002800 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002801
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002802 if (usedDifferentExceptionSpec) {
2803 // Update FunctionProtoType::ExtProtoInfo.
2804 QualType T = Importer.Import(D->getType());
2805 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002806 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002807 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002808 }
2809
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002810 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002811
2812 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002813 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002814
Douglas Gregor43f54792010-02-17 02:12:47 +00002815 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002816}
2817
Douglas Gregor00eace12010-02-21 18:29:16 +00002818Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2819 return VisitFunctionDecl(D);
2820}
2821
2822Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2823 return VisitCXXMethodDecl(D);
2824}
2825
2826Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2827 return VisitCXXMethodDecl(D);
2828}
2829
2830Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2831 return VisitCXXMethodDecl(D);
2832}
2833
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002834static unsigned getFieldIndex(Decl *F) {
2835 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2836 if (!Owner)
2837 return 0;
2838
2839 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002840 for (const auto *D : Owner->noload_decls()) {
2841 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002842 return Index;
2843
2844 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2845 ++Index;
2846 }
2847
2848 return Index;
2849}
2850
Douglas Gregor5c73e912010-02-11 00:48:18 +00002851Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2852 // Import the major distinguishing characteristics of a variable.
2853 DeclContext *DC, *LexicalDC;
2854 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002855 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002856 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002857 return nullptr;
2858
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002859 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002860 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002861 DC->localUncachedLookup(Name, FoundDecls);
2862 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2863 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002864 // For anonymous fields, match up by index.
2865 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2866 continue;
2867
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002868 if (Importer.IsStructurallyEquivalent(D->getType(),
2869 FoundField->getType())) {
2870 Importer.Imported(D, FoundField);
2871 return FoundField;
2872 }
2873
2874 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2875 << Name << D->getType() << FoundField->getType();
2876 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2877 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002878 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002879 }
2880 }
2881
Douglas Gregorb4964f72010-02-15 23:54:17 +00002882 // Import the type.
2883 QualType T = Importer.Import(D->getType());
2884 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002885 return nullptr;
2886
Douglas Gregor5c73e912010-02-11 00:48:18 +00002887 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2888 Expr *BitWidth = Importer.Import(D->getBitWidth());
2889 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00002890 return nullptr;
2891
Abramo Bagnaradff19302011-03-08 08:55:46 +00002892 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2893 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002894 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002895 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002896 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002897 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002898 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002899 if (ToField->hasInClassInitializer())
2900 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002901 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002902 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002903 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002904 return ToField;
2905}
2906
Francois Pichet783dd6e2010-11-21 06:08:52 +00002907Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2908 // Import the major distinguishing characteristics of a variable.
2909 DeclContext *DC, *LexicalDC;
2910 DeclarationName Name;
2911 SourceLocation Loc;
2912 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002913 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002914
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002915 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002916 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002917 DC->localUncachedLookup(Name, FoundDecls);
2918 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002919 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002920 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002921 // For anonymous indirect fields, match up by index.
2922 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2923 continue;
2924
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002925 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002926 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002927 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002928 Importer.Imported(D, FoundField);
2929 return FoundField;
2930 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002931
2932 // If there are more anonymous fields to check, continue.
2933 if (!Name && I < N-1)
2934 continue;
2935
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002936 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2937 << Name << D->getType() << FoundField->getType();
2938 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2939 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002940 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002941 }
2942 }
2943
Francois Pichet783dd6e2010-11-21 06:08:52 +00002944 // Import the type.
2945 QualType T = Importer.Import(D->getType());
2946 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002947 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002948
2949 NamedDecl **NamedChain =
2950 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2951
2952 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00002953 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00002954 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002955 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00002956 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00002957 NamedChain[i++] = cast<NamedDecl>(D);
2958 }
2959
2960 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2961 Importer.getToContext(), DC,
2962 Loc, Name.getAsIdentifierInfo(), T,
2963 NamedChain, D->getChainingSize());
2964 ToIndirectField->setAccess(D->getAccess());
2965 ToIndirectField->setLexicalDeclContext(LexicalDC);
2966 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002967 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002968 return ToIndirectField;
2969}
2970
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002971Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2972 // Import the major distinguishing characteristics of an ivar.
2973 DeclContext *DC, *LexicalDC;
2974 DeclarationName Name;
2975 SourceLocation Loc;
2976 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002977 return nullptr;
2978
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002979 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002980 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002981 DC->localUncachedLookup(Name, FoundDecls);
2982 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2983 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002984 if (Importer.IsStructurallyEquivalent(D->getType(),
2985 FoundIvar->getType())) {
2986 Importer.Imported(D, FoundIvar);
2987 return FoundIvar;
2988 }
2989
2990 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2991 << Name << D->getType() << FoundIvar->getType();
2992 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2993 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00002994 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002995 }
2996 }
2997
2998 // Import the type.
2999 QualType T = Importer.Import(D->getType());
3000 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003001 return nullptr;
3002
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003003 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3004 Expr *BitWidth = Importer.Import(D->getBitWidth());
3005 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003006 return nullptr;
3007
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003008 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3009 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003010 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003011 Loc, Name.getAsIdentifierInfo(),
3012 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003013 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003014 ToIvar->setLexicalDeclContext(LexicalDC);
3015 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003016 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003017 return ToIvar;
3018
3019}
3020
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003021Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3022 // Import the major distinguishing characteristics of a variable.
3023 DeclContext *DC, *LexicalDC;
3024 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003025 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003026 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003027 return nullptr;
3028
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003029 // Try to find a variable in our own ("to") context with the same name and
3030 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003031 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003032 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003033 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003034 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003035 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003036 DC->localUncachedLookup(Name, FoundDecls);
3037 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3038 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003039 continue;
3040
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003041 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003042 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003043 if (FoundVar->hasExternalFormalLinkage() &&
3044 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003045 if (Importer.IsStructurallyEquivalent(D->getType(),
3046 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003047 MergeWithVar = FoundVar;
3048 break;
3049 }
3050
Douglas Gregor56521c52010-02-12 17:23:39 +00003051 const ArrayType *FoundArray
3052 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3053 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003054 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003055 if (FoundArray && TArray) {
3056 if (isa<IncompleteArrayType>(FoundArray) &&
3057 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003058 // Import the type.
3059 QualType T = Importer.Import(D->getType());
3060 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003061 return nullptr;
3062
Douglas Gregor56521c52010-02-12 17:23:39 +00003063 FoundVar->setType(T);
3064 MergeWithVar = FoundVar;
3065 break;
3066 } else if (isa<IncompleteArrayType>(TArray) &&
3067 isa<ConstantArrayType>(FoundArray)) {
3068 MergeWithVar = FoundVar;
3069 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003070 }
3071 }
3072
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003073 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003074 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003075 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3076 << FoundVar->getType();
3077 }
3078 }
3079
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003080 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003081 }
3082
3083 if (MergeWithVar) {
3084 // An equivalent variable with external linkage has been found. Link
3085 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003086 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003087
3088 if (VarDecl *DDef = D->getDefinition()) {
3089 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3090 Importer.ToDiag(ExistingDef->getLocation(),
3091 diag::err_odr_variable_multiple_def)
3092 << Name;
3093 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3094 } else {
3095 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003096 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003097 if (DDef->isInitKnownICE()) {
3098 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3099 Eval->CheckedICE = true;
3100 Eval->IsICE = DDef->isInitICE();
3101 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003102 }
3103 }
3104
3105 return MergeWithVar;
3106 }
3107
3108 if (!ConflictingDecls.empty()) {
3109 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3110 ConflictingDecls.data(),
3111 ConflictingDecls.size());
3112 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003113 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003114 }
3115 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003116
Douglas Gregorb4964f72010-02-15 23:54:17 +00003117 // Import the type.
3118 QualType T = Importer.Import(D->getType());
3119 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003120 return nullptr;
3121
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003122 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003123 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003124 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3125 Importer.Import(D->getInnerLocStart()),
3126 Loc, Name.getAsIdentifierInfo(),
3127 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003128 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003129 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003130 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003131 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003132 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003133 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003134
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003135 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003136 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003137 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003138
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003139 return ToVar;
3140}
3141
Douglas Gregor8b228d72010-02-17 21:22:52 +00003142Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3143 // Parameters are created in the translation unit's context, then moved
3144 // into the function declaration's context afterward.
3145 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3146
3147 // Import the name of this declaration.
3148 DeclarationName Name = Importer.Import(D->getDeclName());
3149 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003150 return nullptr;
3151
Douglas Gregor8b228d72010-02-17 21:22:52 +00003152 // Import the location of this declaration.
3153 SourceLocation Loc = Importer.Import(D->getLocation());
3154
3155 // Import the parameter's type.
3156 QualType T = Importer.Import(D->getType());
3157 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003158 return nullptr;
3159
Douglas Gregor8b228d72010-02-17 21:22:52 +00003160 // Create the imported parameter.
3161 ImplicitParamDecl *ToParm
3162 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3163 Loc, Name.getAsIdentifierInfo(),
3164 T);
3165 return Importer.Imported(D, ToParm);
3166}
3167
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003168Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3169 // Parameters are created in the translation unit's context, then moved
3170 // into the function declaration's context afterward.
3171 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3172
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003173 // Import the name of this declaration.
3174 DeclarationName Name = Importer.Import(D->getDeclName());
3175 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003176 return nullptr;
3177
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003178 // Import the location of this declaration.
3179 SourceLocation Loc = Importer.Import(D->getLocation());
3180
3181 // Import the parameter's type.
3182 QualType T = Importer.Import(D->getType());
3183 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003184 return nullptr;
3185
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003186 // Create the imported parameter.
3187 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3188 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003189 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003190 Loc, Name.getAsIdentifierInfo(),
3191 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003192 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003193 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003194 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003195}
3196
Douglas Gregor43f54792010-02-17 02:12:47 +00003197Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3198 // Import the major distinguishing characteristics of a method.
3199 DeclContext *DC, *LexicalDC;
3200 DeclarationName Name;
3201 SourceLocation Loc;
3202 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003203 return nullptr;
3204
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003205 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003206 DC->localUncachedLookup(Name, FoundDecls);
3207 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3208 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003209 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3210 continue;
3211
3212 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003213 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3214 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003215 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003216 << D->isInstanceMethod() << Name << D->getReturnType()
3217 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003218 Importer.ToDiag(FoundMethod->getLocation(),
3219 diag::note_odr_objc_method_here)
3220 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003221 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003222 }
3223
3224 // Check the number of parameters.
3225 if (D->param_size() != FoundMethod->param_size()) {
3226 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3227 << D->isInstanceMethod() << Name
3228 << D->param_size() << FoundMethod->param_size();
3229 Importer.ToDiag(FoundMethod->getLocation(),
3230 diag::note_odr_objc_method_here)
3231 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003232 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003233 }
3234
3235 // Check parameter types.
3236 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3237 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3238 P != PEnd; ++P, ++FoundP) {
3239 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3240 (*FoundP)->getType())) {
3241 Importer.FromDiag((*P)->getLocation(),
3242 diag::err_odr_objc_method_param_type_inconsistent)
3243 << D->isInstanceMethod() << Name
3244 << (*P)->getType() << (*FoundP)->getType();
3245 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3246 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003247 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003248 }
3249 }
3250
3251 // Check variadic/non-variadic.
3252 // Check the number of parameters.
3253 if (D->isVariadic() != FoundMethod->isVariadic()) {
3254 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3255 << D->isInstanceMethod() << Name;
3256 Importer.ToDiag(FoundMethod->getLocation(),
3257 diag::note_odr_objc_method_here)
3258 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003259 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003260 }
3261
3262 // FIXME: Any other bits we need to merge?
3263 return Importer.Imported(D, FoundMethod);
3264 }
3265 }
3266
3267 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003268 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003269 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003270 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003271
Alp Toker314cc812014-01-25 16:55:45 +00003272 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003273
Alp Toker314cc812014-01-25 16:55:45 +00003274 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3275 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3276 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3277 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3278 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003279
3280 // FIXME: When we decide to merge method definitions, we'll need to
3281 // deal with implicit parameters.
3282
3283 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003284 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003285 for (auto *FromP : D->params()) {
3286 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003287 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003288 return nullptr;
3289
Douglas Gregor43f54792010-02-17 02:12:47 +00003290 ToParams.push_back(ToP);
3291 }
3292
3293 // Set the parameters.
3294 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3295 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003296 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003297 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003298 SmallVector<SourceLocation, 12> SelLocs;
3299 D->getSelectorLocs(SelLocs);
3300 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003301
3302 ToMethod->setLexicalDeclContext(LexicalDC);
3303 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003304 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003305 return ToMethod;
3306}
3307
Douglas Gregor84c51c32010-02-18 01:47:50 +00003308Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3309 // Import the major distinguishing characteristics of a category.
3310 DeclContext *DC, *LexicalDC;
3311 DeclarationName Name;
3312 SourceLocation Loc;
3313 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003314 return nullptr;
3315
Douglas Gregor84c51c32010-02-18 01:47:50 +00003316 ObjCInterfaceDecl *ToInterface
3317 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3318 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003319 return nullptr;
3320
Douglas Gregor84c51c32010-02-18 01:47:50 +00003321 // Determine if we've already encountered this category.
3322 ObjCCategoryDecl *MergeWithCategory
3323 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3324 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3325 if (!ToCategory) {
3326 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003327 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003328 Loc,
3329 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003330 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003331 ToInterface,
3332 Importer.Import(D->getIvarLBraceLoc()),
3333 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003334 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003335 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003336 Importer.Imported(D, ToCategory);
3337
Douglas Gregor84c51c32010-02-18 01:47:50 +00003338 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003339 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3340 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003341 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3342 = D->protocol_loc_begin();
3343 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3344 FromProtoEnd = D->protocol_end();
3345 FromProto != FromProtoEnd;
3346 ++FromProto, ++FromProtoLoc) {
3347 ObjCProtocolDecl *ToProto
3348 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3349 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003350 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003351 Protocols.push_back(ToProto);
3352 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3353 }
3354
3355 // FIXME: If we're merging, make sure that the protocol list is the same.
3356 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3357 ProtocolLocs.data(), Importer.getToContext());
3358
3359 } else {
3360 Importer.Imported(D, ToCategory);
3361 }
3362
3363 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003364 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003365
3366 // If we have an implementation, import it as well.
3367 if (D->getImplementation()) {
3368 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003369 = cast_or_null<ObjCCategoryImplDecl>(
3370 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003371 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003372 return nullptr;
3373
Douglas Gregor84c51c32010-02-18 01:47:50 +00003374 ToCategory->setImplementation(Impl);
3375 }
3376
3377 return ToCategory;
3378}
3379
Douglas Gregor2aa53772012-01-24 17:42:07 +00003380bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3381 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003382 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003383 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003384 if (shouldForceImportDeclContext(Kind))
3385 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003386 return false;
3387 }
3388
3389 // Start the protocol definition
3390 To->startDefinition();
3391
3392 // Import protocols
3393 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3394 SmallVector<SourceLocation, 4> ProtocolLocs;
3395 ObjCProtocolDecl::protocol_loc_iterator
3396 FromProtoLoc = From->protocol_loc_begin();
3397 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3398 FromProtoEnd = From->protocol_end();
3399 FromProto != FromProtoEnd;
3400 ++FromProto, ++FromProtoLoc) {
3401 ObjCProtocolDecl *ToProto
3402 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3403 if (!ToProto)
3404 return true;
3405 Protocols.push_back(ToProto);
3406 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3407 }
3408
3409 // FIXME: If we're merging, make sure that the protocol list is the same.
3410 To->setProtocolList(Protocols.data(), Protocols.size(),
3411 ProtocolLocs.data(), Importer.getToContext());
3412
Douglas Gregor2e15c842012-02-01 21:00:38 +00003413 if (shouldForceImportDeclContext(Kind)) {
3414 // Import all of the members of this protocol.
3415 ImportDeclContext(From, /*ForceImport=*/true);
3416 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003417 return false;
3418}
3419
Douglas Gregor98d156a2010-02-17 16:12:00 +00003420Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003421 // If this protocol has a definition in the translation unit we're coming
3422 // from, but this particular declaration is not that definition, import the
3423 // definition and map to that.
3424 ObjCProtocolDecl *Definition = D->getDefinition();
3425 if (Definition && Definition != D) {
3426 Decl *ImportedDef = Importer.Import(Definition);
3427 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003428 return nullptr;
3429
Douglas Gregor2aa53772012-01-24 17:42:07 +00003430 return Importer.Imported(D, ImportedDef);
3431 }
3432
Douglas Gregor84c51c32010-02-18 01:47:50 +00003433 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003434 DeclContext *DC, *LexicalDC;
3435 DeclarationName Name;
3436 SourceLocation Loc;
3437 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003438 return nullptr;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003439
Craig Topper36250ad2014-05-12 05:36:57 +00003440 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003441 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003442 DC->localUncachedLookup(Name, FoundDecls);
3443 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3444 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003445 continue;
3446
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003447 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003448 break;
3449 }
3450
3451 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003452 if (!ToProto) {
3453 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3454 Name.getAsIdentifierInfo(), Loc,
3455 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003456 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003457 ToProto->setLexicalDeclContext(LexicalDC);
3458 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003459 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003460
3461 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003462
Douglas Gregor2aa53772012-01-24 17:42:07 +00003463 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003464 return nullptr;
3465
Douglas Gregor98d156a2010-02-17 16:12:00 +00003466 return ToProto;
3467}
3468
Douglas Gregor2aa53772012-01-24 17:42:07 +00003469bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3470 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003471 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003472 if (To->getDefinition()) {
3473 // Check consistency of superclass.
3474 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3475 if (FromSuper) {
3476 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3477 if (!FromSuper)
3478 return true;
3479 }
3480
3481 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3482 if ((bool)FromSuper != (bool)ToSuper ||
3483 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3484 Importer.ToDiag(To->getLocation(),
3485 diag::err_odr_objc_superclass_inconsistent)
3486 << To->getDeclName();
3487 if (ToSuper)
3488 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3489 << To->getSuperClass()->getDeclName();
3490 else
3491 Importer.ToDiag(To->getLocation(),
3492 diag::note_odr_objc_missing_superclass);
3493 if (From->getSuperClass())
3494 Importer.FromDiag(From->getSuperClassLoc(),
3495 diag::note_odr_objc_superclass)
3496 << From->getSuperClass()->getDeclName();
3497 else
3498 Importer.FromDiag(From->getLocation(),
3499 diag::note_odr_objc_missing_superclass);
3500 }
3501
Douglas Gregor2e15c842012-02-01 21:00:38 +00003502 if (shouldForceImportDeclContext(Kind))
3503 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003504 return false;
3505 }
3506
3507 // Start the definition.
3508 To->startDefinition();
3509
3510 // If this class has a superclass, import it.
3511 if (From->getSuperClass()) {
3512 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3513 Importer.Import(From->getSuperClass()));
3514 if (!Super)
3515 return true;
3516
3517 To->setSuperClass(Super);
3518 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3519 }
3520
3521 // Import protocols
3522 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3523 SmallVector<SourceLocation, 4> ProtocolLocs;
3524 ObjCInterfaceDecl::protocol_loc_iterator
3525 FromProtoLoc = From->protocol_loc_begin();
3526
3527 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3528 FromProtoEnd = From->protocol_end();
3529 FromProto != FromProtoEnd;
3530 ++FromProto, ++FromProtoLoc) {
3531 ObjCProtocolDecl *ToProto
3532 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3533 if (!ToProto)
3534 return true;
3535 Protocols.push_back(ToProto);
3536 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3537 }
3538
3539 // FIXME: If we're merging, make sure that the protocol list is the same.
3540 To->setProtocolList(Protocols.data(), Protocols.size(),
3541 ProtocolLocs.data(), Importer.getToContext());
3542
3543 // Import categories. When the categories themselves are imported, they'll
3544 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003545 for (auto *Cat : From->known_categories())
3546 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003547
Douglas Gregor2aa53772012-01-24 17:42:07 +00003548 // If we have an @implementation, import it as well.
3549 if (From->getImplementation()) {
3550 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3551 Importer.Import(From->getImplementation()));
3552 if (!Impl)
3553 return true;
3554
3555 To->setImplementation(Impl);
3556 }
3557
Douglas Gregor2e15c842012-02-01 21:00:38 +00003558 if (shouldForceImportDeclContext(Kind)) {
3559 // Import all of the members of this class.
3560 ImportDeclContext(From, /*ForceImport=*/true);
3561 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003562 return false;
3563}
3564
Douglas Gregor45635322010-02-16 01:20:57 +00003565Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003566 // If this class has a definition in the translation unit we're coming from,
3567 // but this particular declaration is not that definition, import the
3568 // definition and map to that.
3569 ObjCInterfaceDecl *Definition = D->getDefinition();
3570 if (Definition && Definition != D) {
3571 Decl *ImportedDef = Importer.Import(Definition);
3572 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003573 return nullptr;
3574
Douglas Gregor2aa53772012-01-24 17:42:07 +00003575 return Importer.Imported(D, ImportedDef);
3576 }
3577
Douglas Gregor45635322010-02-16 01:20:57 +00003578 // Import the major distinguishing characteristics of an @interface.
3579 DeclContext *DC, *LexicalDC;
3580 DeclarationName Name;
3581 SourceLocation Loc;
3582 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003583 return nullptr;
Douglas Gregor45635322010-02-16 01:20:57 +00003584
Douglas Gregor2aa53772012-01-24 17:42:07 +00003585 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003586 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003587 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003588 DC->localUncachedLookup(Name, FoundDecls);
3589 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3590 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003591 continue;
3592
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003593 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003594 break;
3595 }
3596
Douglas Gregor2aa53772012-01-24 17:42:07 +00003597 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003598 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003599 if (!ToIface) {
3600 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3601 Importer.Import(D->getAtStartLoc()),
3602 Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00003603 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003604 D->isImplicitInterfaceDecl());
3605 ToIface->setLexicalDeclContext(LexicalDC);
3606 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003607 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003608 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003609
Douglas Gregor2aa53772012-01-24 17:42:07 +00003610 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003611 return nullptr;
3612
Douglas Gregor98d156a2010-02-17 16:12:00 +00003613 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003614}
3615
Douglas Gregor4da9d682010-12-07 15:32:12 +00003616Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3617 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3618 Importer.Import(D->getCategoryDecl()));
3619 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003620 return nullptr;
3621
Douglas Gregor4da9d682010-12-07 15:32:12 +00003622 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3623 if (!ToImpl) {
3624 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3625 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003626 return nullptr;
3627
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003628 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003629 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003630 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003631 Category->getClassInterface(),
3632 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003633 Importer.Import(D->getAtStartLoc()),
3634 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003635
3636 DeclContext *LexicalDC = DC;
3637 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3638 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3639 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003640 return nullptr;
3641
Douglas Gregor4da9d682010-12-07 15:32:12 +00003642 ToImpl->setLexicalDeclContext(LexicalDC);
3643 }
3644
Sean Callanan95e74be2011-10-21 02:57:43 +00003645 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003646 Category->setImplementation(ToImpl);
3647 }
3648
3649 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003650 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003651 return ToImpl;
3652}
3653
Douglas Gregorda8025c2010-12-07 01:26:03 +00003654Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3655 // Find the corresponding interface.
3656 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3657 Importer.Import(D->getClassInterface()));
3658 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003659 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003660
3661 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003662 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003663 if (D->getSuperClass()) {
3664 Super = cast_or_null<ObjCInterfaceDecl>(
3665 Importer.Import(D->getSuperClass()));
3666 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003667 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003668 }
3669
3670 ObjCImplementationDecl *Impl = Iface->getImplementation();
3671 if (!Impl) {
3672 // We haven't imported an implementation yet. Create a new @implementation
3673 // now.
3674 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3675 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003676 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003677 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003678 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003679 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003680 Importer.Import(D->getIvarLBraceLoc()),
3681 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003682
3683 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3684 DeclContext *LexicalDC
3685 = Importer.ImportContext(D->getLexicalDeclContext());
3686 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003687 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003688 Impl->setLexicalDeclContext(LexicalDC);
3689 }
3690
3691 // Associate the implementation with the class it implements.
3692 Iface->setImplementation(Impl);
3693 Importer.Imported(D, Iface->getImplementation());
3694 } else {
3695 Importer.Imported(D, Iface->getImplementation());
3696
3697 // Verify that the existing @implementation has the same superclass.
3698 if ((Super && !Impl->getSuperClass()) ||
3699 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003700 (Super && Impl->getSuperClass() &&
3701 !declaresSameEntity(Super->getCanonicalDecl(),
3702 Impl->getSuperClass()))) {
3703 Importer.ToDiag(Impl->getLocation(),
3704 diag::err_odr_objc_superclass_inconsistent)
3705 << Iface->getDeclName();
3706 // FIXME: It would be nice to have the location of the superclass
3707 // below.
3708 if (Impl->getSuperClass())
3709 Importer.ToDiag(Impl->getLocation(),
3710 diag::note_odr_objc_superclass)
3711 << Impl->getSuperClass()->getDeclName();
3712 else
3713 Importer.ToDiag(Impl->getLocation(),
3714 diag::note_odr_objc_missing_superclass);
3715 if (D->getSuperClass())
3716 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003717 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003718 << D->getSuperClass()->getDeclName();
3719 else
3720 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003721 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003722 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003723 }
3724 }
3725
3726 // Import all of the members of this @implementation.
3727 ImportDeclContext(D);
3728
3729 return Impl;
3730}
3731
Douglas Gregora11c4582010-02-17 18:02:10 +00003732Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3733 // Import the major distinguishing characteristics of an @property.
3734 DeclContext *DC, *LexicalDC;
3735 DeclarationName Name;
3736 SourceLocation Loc;
3737 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003738 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003739
3740 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003741 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003742 DC->localUncachedLookup(Name, FoundDecls);
3743 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003744 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003745 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003746 // Check property types.
3747 if (!Importer.IsStructurallyEquivalent(D->getType(),
3748 FoundProp->getType())) {
3749 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3750 << Name << D->getType() << FoundProp->getType();
3751 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3752 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003753 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003754 }
3755
3756 // FIXME: Check property attributes, getters, setters, etc.?
3757
3758 // Consider these properties to be equivalent.
3759 Importer.Imported(D, FoundProp);
3760 return FoundProp;
3761 }
3762 }
3763
3764 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003765 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3766 if (!T)
Craig Topper36250ad2014-05-12 05:36:57 +00003767 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00003768
3769 // Create the new property.
3770 ObjCPropertyDecl *ToProperty
3771 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3772 Name.getAsIdentifierInfo(),
3773 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003774 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003775 T,
3776 D->getPropertyImplementation());
3777 Importer.Imported(D, ToProperty);
3778 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003779 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003780
3781 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003782 ToProperty->setPropertyAttributesAsWritten(
3783 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003784 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3785 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3786 ToProperty->setGetterMethodDecl(
3787 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3788 ToProperty->setSetterMethodDecl(
3789 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3790 ToProperty->setPropertyIvarDecl(
3791 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3792 return ToProperty;
3793}
3794
Douglas Gregor14a49e22010-12-07 18:32:03 +00003795Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3796 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3797 Importer.Import(D->getPropertyDecl()));
3798 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00003799 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003800
3801 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3802 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003803 return nullptr;
3804
Douglas Gregor14a49e22010-12-07 18:32:03 +00003805 // Import the lexical declaration context.
3806 DeclContext *LexicalDC = DC;
3807 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3808 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3809 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003810 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003811 }
3812
3813 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3814 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00003815 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003816
3817 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00003818 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003819 if (D->getPropertyIvarDecl()) {
3820 Ivar = cast_or_null<ObjCIvarDecl>(
3821 Importer.Import(D->getPropertyIvarDecl()));
3822 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00003823 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003824 }
3825
3826 ObjCPropertyImplDecl *ToImpl
3827 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3828 if (!ToImpl) {
3829 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3830 Importer.Import(D->getLocStart()),
3831 Importer.Import(D->getLocation()),
3832 Property,
3833 D->getPropertyImplementation(),
3834 Ivar,
3835 Importer.Import(D->getPropertyIvarDeclLoc()));
3836 ToImpl->setLexicalDeclContext(LexicalDC);
3837 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003838 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003839 } else {
3840 // Check that we have the same kind of property implementation (@synthesize
3841 // vs. @dynamic).
3842 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3843 Importer.ToDiag(ToImpl->getLocation(),
3844 diag::err_odr_objc_property_impl_kind_inconsistent)
3845 << Property->getDeclName()
3846 << (ToImpl->getPropertyImplementation()
3847 == ObjCPropertyImplDecl::Dynamic);
3848 Importer.FromDiag(D->getLocation(),
3849 diag::note_odr_objc_property_impl_kind)
3850 << D->getPropertyDecl()->getDeclName()
3851 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00003852 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003853 }
3854
3855 // For @synthesize, check that we have the same
3856 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3857 Ivar != ToImpl->getPropertyIvarDecl()) {
3858 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3859 diag::err_odr_objc_synthesize_ivar_inconsistent)
3860 << Property->getDeclName()
3861 << ToImpl->getPropertyIvarDecl()->getDeclName()
3862 << Ivar->getDeclName();
3863 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3864 diag::note_odr_objc_synthesize_ivar_here)
3865 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00003866 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00003867 }
3868
3869 // Merge the existing implementation with the new implementation.
3870 Importer.Imported(D, ToImpl);
3871 }
3872
3873 return ToImpl;
3874}
3875
Douglas Gregora082a492010-11-30 19:14:50 +00003876Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3877 // For template arguments, we adopt the translation unit as our declaration
3878 // context. This context will be fixed when the actual template declaration
3879 // is created.
3880
3881 // FIXME: Import default argument.
3882 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3883 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003884 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003885 Importer.Import(D->getLocation()),
3886 D->getDepth(),
3887 D->getIndex(),
3888 Importer.Import(D->getIdentifier()),
3889 D->wasDeclaredWithTypename(),
3890 D->isParameterPack());
3891}
3892
3893Decl *
3894ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3895 // Import the name of this declaration.
3896 DeclarationName Name = Importer.Import(D->getDeclName());
3897 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003898 return nullptr;
3899
Douglas Gregora082a492010-11-30 19:14:50 +00003900 // Import the location of this declaration.
3901 SourceLocation Loc = Importer.Import(D->getLocation());
3902
3903 // Import the type of this declaration.
3904 QualType T = Importer.Import(D->getType());
3905 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003906 return nullptr;
3907
Douglas Gregora082a492010-11-30 19:14:50 +00003908 // Import type-source information.
3909 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3910 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00003911 return nullptr;
3912
Douglas Gregora082a492010-11-30 19:14:50 +00003913 // FIXME: Import default argument.
3914
3915 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3916 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003917 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003918 Loc, D->getDepth(), D->getPosition(),
3919 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003920 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003921}
3922
3923Decl *
3924ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3925 // Import the name of this declaration.
3926 DeclarationName Name = Importer.Import(D->getDeclName());
3927 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003928 return nullptr;
3929
Douglas Gregora082a492010-11-30 19:14:50 +00003930 // Import the location of this declaration.
3931 SourceLocation Loc = Importer.Import(D->getLocation());
3932
3933 // Import template parameters.
3934 TemplateParameterList *TemplateParams
3935 = ImportTemplateParameterList(D->getTemplateParameters());
3936 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00003937 return nullptr;
3938
Douglas Gregora082a492010-11-30 19:14:50 +00003939 // FIXME: Import default argument.
3940
3941 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3942 Importer.getToContext().getTranslationUnitDecl(),
3943 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003944 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003945 Name.getAsIdentifierInfo(),
3946 TemplateParams);
3947}
3948
3949Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3950 // If this record has a definition in the translation unit we're coming from,
3951 // but this particular declaration is not that definition, import the
3952 // definition and map to that.
3953 CXXRecordDecl *Definition
3954 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3955 if (Definition && Definition != D->getTemplatedDecl()) {
3956 Decl *ImportedDef
3957 = Importer.Import(Definition->getDescribedClassTemplate());
3958 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003959 return nullptr;
3960
Douglas Gregora082a492010-11-30 19:14:50 +00003961 return Importer.Imported(D, ImportedDef);
3962 }
3963
3964 // Import the major distinguishing characteristics of this class template.
3965 DeclContext *DC, *LexicalDC;
3966 DeclarationName Name;
3967 SourceLocation Loc;
3968 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003969 return nullptr;
3970
Douglas Gregora082a492010-11-30 19:14:50 +00003971 // We may already have a template of the same name; try to find and match it.
3972 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003973 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003974 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003975 DC->localUncachedLookup(Name, FoundDecls);
3976 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3977 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003978 continue;
3979
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003980 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003981 if (ClassTemplateDecl *FoundTemplate
3982 = dyn_cast<ClassTemplateDecl>(Found)) {
3983 if (IsStructuralMatch(D, FoundTemplate)) {
3984 // The class templates structurally match; call it the same template.
3985 // FIXME: We may be filling in a forward declaration here. Handle
3986 // this case!
3987 Importer.Imported(D->getTemplatedDecl(),
3988 FoundTemplate->getTemplatedDecl());
3989 return Importer.Imported(D, FoundTemplate);
3990 }
3991 }
3992
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003993 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003994 }
3995
3996 if (!ConflictingDecls.empty()) {
3997 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3998 ConflictingDecls.data(),
3999 ConflictingDecls.size());
4000 }
4001
4002 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004003 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004004 }
4005
4006 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4007
4008 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004009 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4010 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004011 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4012 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004013 DC, StartLoc, IdLoc,
4014 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004015 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004016 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004017 D2Templated->setLexicalDeclContext(LexicalDC);
4018
4019 // Create the class template declaration itself.
4020 TemplateParameterList *TemplateParams
4021 = ImportTemplateParameterList(D->getTemplateParameters());
4022 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004023 return nullptr;
4024
Douglas Gregora082a492010-11-30 19:14:50 +00004025 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4026 Loc, Name, TemplateParams,
4027 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004028 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004029 D2Templated->setDescribedClassTemplate(D2);
4030
4031 D2->setAccess(D->getAccess());
4032 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004033 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004034
4035 // Note the relationship between the class templates.
4036 Importer.Imported(D, D2);
4037 Importer.Imported(DTemplated, D2Templated);
4038
John McCallf937c022011-10-07 06:10:15 +00004039 if (DTemplated->isCompleteDefinition() &&
4040 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004041 // FIXME: Import definition!
4042 }
4043
4044 return D2;
4045}
4046
Douglas Gregore2e50d332010-12-01 01:36:18 +00004047Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4048 ClassTemplateSpecializationDecl *D) {
4049 // If this record has a definition in the translation unit we're coming from,
4050 // but this particular declaration is not that definition, import the
4051 // definition and map to that.
4052 TagDecl *Definition = D->getDefinition();
4053 if (Definition && Definition != D) {
4054 Decl *ImportedDef = Importer.Import(Definition);
4055 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004056 return nullptr;
4057
Douglas Gregore2e50d332010-12-01 01:36:18 +00004058 return Importer.Imported(D, ImportedDef);
4059 }
4060
4061 ClassTemplateDecl *ClassTemplate
4062 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4063 D->getSpecializedTemplate()));
4064 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004065 return nullptr;
4066
Douglas Gregore2e50d332010-12-01 01:36:18 +00004067 // Import the context of this declaration.
4068 DeclContext *DC = ClassTemplate->getDeclContext();
4069 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004070 return nullptr;
4071
Douglas Gregore2e50d332010-12-01 01:36:18 +00004072 DeclContext *LexicalDC = DC;
4073 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4074 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4075 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004076 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004077 }
4078
4079 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004080 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4081 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004082
4083 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004084 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004085 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4086 D->getTemplateArgs().size(),
4087 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004088 return nullptr;
4089
Douglas Gregore2e50d332010-12-01 01:36:18 +00004090 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004091 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004092 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004093 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004094 if (D2) {
4095 // We already have a class template specialization with these template
4096 // arguments.
4097
4098 // FIXME: Check for specialization vs. instantiation errors.
4099
4100 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004101 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004102 // The record types structurally match, or the "from" translation
4103 // unit only had a forward declaration anyway; call it the same
4104 // function.
4105 return Importer.Imported(D, FoundDef);
4106 }
4107 }
4108 } else {
4109 // Create a new specialization.
4110 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4111 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004112 StartLoc, IdLoc,
4113 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004114 TemplateArgs.data(),
4115 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004116 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004117 D2->setSpecializationKind(D->getSpecializationKind());
4118
4119 // Add this specialization to the class template.
4120 ClassTemplate->AddSpecialization(D2, InsertPos);
4121
4122 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004123 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004124
4125 // Add the specialization to this context.
4126 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004127 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004128 }
4129 Importer.Imported(D, D2);
4130
John McCallf937c022011-10-07 06:10:15 +00004131 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004132 return nullptr;
4133
Douglas Gregore2e50d332010-12-01 01:36:18 +00004134 return D2;
4135}
4136
Larisse Voufo39a1e502013-08-06 01:03:05 +00004137Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4138 // If this variable has a definition in the translation unit we're coming
4139 // from,
4140 // but this particular declaration is not that definition, import the
4141 // definition and map to that.
4142 VarDecl *Definition =
4143 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4144 if (Definition && Definition != D->getTemplatedDecl()) {
4145 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4146 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004147 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004148
4149 return Importer.Imported(D, ImportedDef);
4150 }
4151
4152 // Import the major distinguishing characteristics of this variable template.
4153 DeclContext *DC, *LexicalDC;
4154 DeclarationName Name;
4155 SourceLocation Loc;
4156 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004157 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004158
4159 // We may already have a template of the same name; try to find and match it.
4160 assert(!DC->isFunctionOrMethod() &&
4161 "Variable templates cannot be declared at function scope");
4162 SmallVector<NamedDecl *, 4> ConflictingDecls;
4163 SmallVector<NamedDecl *, 2> FoundDecls;
4164 DC->localUncachedLookup(Name, FoundDecls);
4165 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4166 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4167 continue;
4168
4169 Decl *Found = FoundDecls[I];
4170 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4171 if (IsStructuralMatch(D, FoundTemplate)) {
4172 // The variable templates structurally match; call it the same template.
4173 Importer.Imported(D->getTemplatedDecl(),
4174 FoundTemplate->getTemplatedDecl());
4175 return Importer.Imported(D, FoundTemplate);
4176 }
4177 }
4178
4179 ConflictingDecls.push_back(FoundDecls[I]);
4180 }
4181
4182 if (!ConflictingDecls.empty()) {
4183 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4184 ConflictingDecls.data(),
4185 ConflictingDecls.size());
4186 }
4187
4188 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004189 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004190
4191 VarDecl *DTemplated = D->getTemplatedDecl();
4192
4193 // Import the type.
4194 QualType T = Importer.Import(DTemplated->getType());
4195 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004196 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004197
4198 // Create the declaration that is being templated.
4199 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4200 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4201 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4202 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4203 IdLoc, Name.getAsIdentifierInfo(), T,
4204 TInfo, DTemplated->getStorageClass());
4205 D2Templated->setAccess(DTemplated->getAccess());
4206 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4207 D2Templated->setLexicalDeclContext(LexicalDC);
4208
4209 // Importer.Imported(DTemplated, D2Templated);
4210 // LexicalDC->addDeclInternal(D2Templated);
4211
4212 // Merge the initializer.
4213 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004214 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004215
4216 // Create the variable template declaration itself.
4217 TemplateParameterList *TemplateParams =
4218 ImportTemplateParameterList(D->getTemplateParameters());
4219 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004220 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004221
4222 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004223 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004224 D2Templated->setDescribedVarTemplate(D2);
4225
4226 D2->setAccess(D->getAccess());
4227 D2->setLexicalDeclContext(LexicalDC);
4228 LexicalDC->addDeclInternal(D2);
4229
4230 // Note the relationship between the variable templates.
4231 Importer.Imported(D, D2);
4232 Importer.Imported(DTemplated, D2Templated);
4233
4234 if (DTemplated->isThisDeclarationADefinition() &&
4235 !D2Templated->isThisDeclarationADefinition()) {
4236 // FIXME: Import definition!
4237 }
4238
4239 return D2;
4240}
4241
4242Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4243 VarTemplateSpecializationDecl *D) {
4244 // If this record has a definition in the translation unit we're coming from,
4245 // but this particular declaration is not that definition, import the
4246 // definition and map to that.
4247 VarDecl *Definition = D->getDefinition();
4248 if (Definition && Definition != D) {
4249 Decl *ImportedDef = Importer.Import(Definition);
4250 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004251 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004252
4253 return Importer.Imported(D, ImportedDef);
4254 }
4255
4256 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4257 Importer.Import(D->getSpecializedTemplate()));
4258 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004259 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004260
4261 // Import the context of this declaration.
4262 DeclContext *DC = VarTemplate->getDeclContext();
4263 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004264 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004265
4266 DeclContext *LexicalDC = DC;
4267 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4268 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4269 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004270 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004271 }
4272
4273 // Import the location of this declaration.
4274 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4275 SourceLocation IdLoc = Importer.Import(D->getLocation());
4276
4277 // Import template arguments.
4278 SmallVector<TemplateArgument, 2> TemplateArgs;
4279 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4280 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004281 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004282
4283 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004284 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004285 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004286 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004287 if (D2) {
4288 // We already have a variable template specialization with these template
4289 // arguments.
4290
4291 // FIXME: Check for specialization vs. instantiation errors.
4292
4293 if (VarDecl *FoundDef = D2->getDefinition()) {
4294 if (!D->isThisDeclarationADefinition() ||
4295 IsStructuralMatch(D, FoundDef)) {
4296 // The record types structurally match, or the "from" translation
4297 // unit only had a forward declaration anyway; call it the same
4298 // variable.
4299 return Importer.Imported(D, FoundDef);
4300 }
4301 }
4302 } else {
4303
4304 // Import the type.
4305 QualType T = Importer.Import(D->getType());
4306 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004307 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004308 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4309
4310 // Create a new specialization.
4311 D2 = VarTemplateSpecializationDecl::Create(
4312 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4313 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4314 D2->setSpecializationKind(D->getSpecializationKind());
4315 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4316
4317 // Add this specialization to the class template.
4318 VarTemplate->AddSpecialization(D2, InsertPos);
4319
4320 // Import the qualifier, if any.
4321 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4322
4323 // Add the specialization to this context.
4324 D2->setLexicalDeclContext(LexicalDC);
4325 LexicalDC->addDeclInternal(D2);
4326 }
4327 Importer.Imported(D, D2);
4328
4329 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004330 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004331
4332 return D2;
4333}
4334
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004335//----------------------------------------------------------------------------
4336// Import Statements
4337//----------------------------------------------------------------------------
4338
4339Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4340 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4341 << S->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004342 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004343}
4344
4345//----------------------------------------------------------------------------
4346// Import Expressions
4347//----------------------------------------------------------------------------
4348Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4349 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4350 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00004351 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004352}
4353
Douglas Gregor52f820e2010-02-19 01:17:02 +00004354Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004355 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4356 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004357 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004358
Craig Topper36250ad2014-05-12 05:36:57 +00004359 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004360 if (E->getDecl() != E->getFoundDecl()) {
4361 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4362 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00004363 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004364 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004365
4366 QualType T = Importer.Import(E->getType());
4367 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004368 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004369
4370 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4371 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004372 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004373 ToD,
John McCall113bee02012-03-10 09:33:50 +00004374 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004375 Importer.Import(E->getLocation()),
4376 T, E->getValueKind(),
4377 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00004378 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004379 if (E->hadMultipleCandidates())
4380 DRE->setHadMultipleCandidates(true);
4381 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004382}
4383
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004384Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4385 QualType T = Importer.Import(E->getType());
4386 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004387 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004388
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004389 return IntegerLiteral::Create(Importer.getToContext(),
4390 E->getValue(), T,
4391 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004392}
4393
Douglas Gregor623421d2010-02-18 02:21:22 +00004394Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4395 QualType T = Importer.Import(E->getType());
4396 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004397 return nullptr;
4398
Douglas Gregorfb65e592011-07-27 05:40:30 +00004399 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4400 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004401 Importer.Import(E->getLocation()));
4402}
4403
Douglas Gregorc74247e2010-02-19 01:07:06 +00004404Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4405 Expr *SubExpr = Importer.Import(E->getSubExpr());
4406 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004407 return nullptr;
4408
Douglas Gregorc74247e2010-02-19 01:07:06 +00004409 return new (Importer.getToContext())
4410 ParenExpr(Importer.Import(E->getLParen()),
4411 Importer.Import(E->getRParen()),
4412 SubExpr);
4413}
4414
4415Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4416 QualType T = Importer.Import(E->getType());
4417 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004418 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004419
4420 Expr *SubExpr = Importer.Import(E->getSubExpr());
4421 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004422 return nullptr;
4423
Douglas Gregorc74247e2010-02-19 01:07:06 +00004424 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004425 T, E->getValueKind(),
4426 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004427 Importer.Import(E->getOperatorLoc()));
4428}
4429
Peter Collingbournee190dee2011-03-11 19:24:49 +00004430Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4431 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004432 QualType ResultType = Importer.Import(E->getType());
4433
4434 if (E->isArgumentType()) {
4435 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4436 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004437 return nullptr;
4438
Peter Collingbournee190dee2011-03-11 19:24:49 +00004439 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4440 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004441 Importer.Import(E->getOperatorLoc()),
4442 Importer.Import(E->getRParenLoc()));
4443 }
4444
4445 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4446 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004447 return nullptr;
4448
Peter Collingbournee190dee2011-03-11 19:24:49 +00004449 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4450 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004451 Importer.Import(E->getOperatorLoc()),
4452 Importer.Import(E->getRParenLoc()));
4453}
4454
Douglas Gregorc74247e2010-02-19 01:07:06 +00004455Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4456 QualType T = Importer.Import(E->getType());
4457 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004458 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00004459
4460 Expr *LHS = Importer.Import(E->getLHS());
4461 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004462 return nullptr;
4463
Douglas Gregorc74247e2010-02-19 01:07:06 +00004464 Expr *RHS = Importer.Import(E->getRHS());
4465 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004466 return nullptr;
4467
Douglas Gregorc74247e2010-02-19 01:07:06 +00004468 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004469 T, E->getValueKind(),
4470 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004471 Importer.Import(E->getOperatorLoc()),
4472 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004473}
4474
4475Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4476 QualType T = Importer.Import(E->getType());
4477 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004478 return nullptr;
4479
Douglas Gregorc74247e2010-02-19 01:07:06 +00004480 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4481 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004482 return nullptr;
4483
Douglas Gregorc74247e2010-02-19 01:07:06 +00004484 QualType CompResultType = Importer.Import(E->getComputationResultType());
4485 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004486 return nullptr;
4487
Douglas Gregorc74247e2010-02-19 01:07:06 +00004488 Expr *LHS = Importer.Import(E->getLHS());
4489 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004490 return nullptr;
4491
Douglas Gregorc74247e2010-02-19 01:07:06 +00004492 Expr *RHS = Importer.Import(E->getRHS());
4493 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00004494 return nullptr;
4495
Douglas Gregorc74247e2010-02-19 01:07:06 +00004496 return new (Importer.getToContext())
4497 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004498 T, E->getValueKind(),
4499 E->getObjectKind(),
4500 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004501 Importer.Import(E->getOperatorLoc()),
4502 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004503}
4504
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004505static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004506 if (E->path_empty()) return false;
4507
4508 // TODO: import cast paths
4509 return true;
4510}
4511
Douglas Gregor98c10182010-02-12 22:17:39 +00004512Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4513 QualType T = Importer.Import(E->getType());
4514 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004515 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00004516
4517 Expr *SubExpr = Importer.Import(E->getSubExpr());
4518 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004519 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004520
4521 CXXCastPath BasePath;
4522 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004523 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004524
4525 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004526 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004527}
4528
Douglas Gregor5481d322010-02-19 01:32:14 +00004529Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4530 QualType T = Importer.Import(E->getType());
4531 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004532 return nullptr;
4533
Douglas Gregor5481d322010-02-19 01:32:14 +00004534 Expr *SubExpr = Importer.Import(E->getSubExpr());
4535 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00004536 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00004537
4538 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4539 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00004540 return nullptr;
4541
John McCallcf142162010-08-07 06:22:56 +00004542 CXXCastPath BasePath;
4543 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00004544 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00004545
John McCall7decc9e2010-11-18 06:31:45 +00004546 return CStyleCastExpr::Create(Importer.getToContext(), T,
4547 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004548 SubExpr, &BasePath, TInfo,
4549 Importer.Import(E->getLParenLoc()),
4550 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004551}
4552
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004553ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004554 ASTContext &FromContext, FileManager &FromFileManager,
4555 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004556 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004557 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00004558 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00004559{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004560 ImportedDecls[FromContext.getTranslationUnitDecl()]
4561 = ToContext.getTranslationUnitDecl();
4562}
4563
4564ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004565
4566QualType ASTImporter::Import(QualType FromT) {
4567 if (FromT.isNull())
4568 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004569
4570 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004571
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004572 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004573 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4574 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004575 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004576 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004577
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004578 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004579 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004580 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004581 if (ToT.isNull())
4582 return ToT;
4583
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004584 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004585 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004586
John McCall424cec92011-01-19 06:33:43 +00004587 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004588}
4589
Douglas Gregor62d311f2010-02-09 19:21:46 +00004590TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004591 if (!FromTSI)
4592 return FromTSI;
4593
4594 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004595 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004596 QualType T = Import(FromTSI->getType());
4597 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004598 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004599
4600 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004601 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004602}
4603
4604Decl *ASTImporter::Import(Decl *FromD) {
4605 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00004606 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004607
Douglas Gregord451ea92011-07-29 23:31:30 +00004608 ASTNodeImporter Importer(*this);
4609
Douglas Gregor62d311f2010-02-09 19:21:46 +00004610 // Check whether we've already imported this declaration.
4611 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004612 if (Pos != ImportedDecls.end()) {
4613 Decl *ToD = Pos->second;
4614 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4615 return ToD;
4616 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004617
4618 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004619 Decl *ToD = Importer.Visit(FromD);
4620 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00004621 return nullptr;
4622
Douglas Gregor62d311f2010-02-09 19:21:46 +00004623 // Record the imported declaration.
4624 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004625
4626 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4627 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004628 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004629 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004630 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004631 // When we've finished transforming a typedef, see whether it was the
4632 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00004633 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004634 FromTag = AnonTagsWithPendingTypedefs.begin(),
4635 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4636 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004637 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004638 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4639 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004640 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004641 AnonTagsWithPendingTypedefs.erase(FromTag);
4642 break;
4643 }
4644 }
4645 }
4646 }
4647
Douglas Gregor62d311f2010-02-09 19:21:46 +00004648 return ToD;
4649}
4650
4651DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4652 if (!FromDC)
4653 return FromDC;
4654
Douglas Gregor95d82832012-01-24 18:36:04 +00004655 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004656 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004657 return nullptr;
4658
Douglas Gregor2e15c842012-02-01 21:00:38 +00004659 // When we're using a record/enum/Objective-C class/protocol as a context, we
4660 // need it to have a definition.
4661 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004662 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004663 if (ToRecord->isCompleteDefinition()) {
4664 // Do nothing.
4665 } else if (FromRecord->isCompleteDefinition()) {
4666 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4667 ASTNodeImporter::IDK_Basic);
4668 } else {
4669 CompleteDecl(ToRecord);
4670 }
4671 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4672 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4673 if (ToEnum->isCompleteDefinition()) {
4674 // Do nothing.
4675 } else if (FromEnum->isCompleteDefinition()) {
4676 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4677 ASTNodeImporter::IDK_Basic);
4678 } else {
4679 CompleteDecl(ToEnum);
4680 }
4681 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4682 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4683 if (ToClass->getDefinition()) {
4684 // Do nothing.
4685 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4686 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4687 ASTNodeImporter::IDK_Basic);
4688 } else {
4689 CompleteDecl(ToClass);
4690 }
4691 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4692 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4693 if (ToProto->getDefinition()) {
4694 // Do nothing.
4695 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4696 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4697 ASTNodeImporter::IDK_Basic);
4698 } else {
4699 CompleteDecl(ToProto);
4700 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004701 }
4702
4703 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004704}
4705
4706Expr *ASTImporter::Import(Expr *FromE) {
4707 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00004708 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004709
4710 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4711}
4712
4713Stmt *ASTImporter::Import(Stmt *FromS) {
4714 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00004715 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004716
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004717 // Check whether we've already imported this declaration.
4718 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4719 if (Pos != ImportedStmts.end())
4720 return Pos->second;
4721
4722 // Import the type
4723 ASTNodeImporter Importer(*this);
4724 Stmt *ToS = Importer.Visit(FromS);
4725 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00004726 return nullptr;
4727
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004728 // Record the imported declaration.
4729 ImportedStmts[FromS] = ToS;
4730 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004731}
4732
4733NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4734 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00004735 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004736
Douglas Gregor90ebf252011-04-27 16:48:40 +00004737 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4738
4739 switch (FromNNS->getKind()) {
4740 case NestedNameSpecifier::Identifier:
4741 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4742 return NestedNameSpecifier::Create(ToContext, prefix, II);
4743 }
Craig Topper36250ad2014-05-12 05:36:57 +00004744 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00004745
4746 case NestedNameSpecifier::Namespace:
4747 if (NamespaceDecl *NS =
4748 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4749 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4750 }
Craig Topper36250ad2014-05-12 05:36:57 +00004751 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00004752
4753 case NestedNameSpecifier::NamespaceAlias:
4754 if (NamespaceAliasDecl *NSAD =
4755 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4756 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4757 }
Craig Topper36250ad2014-05-12 05:36:57 +00004758 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00004759
4760 case NestedNameSpecifier::Global:
4761 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4762
4763 case NestedNameSpecifier::TypeSpec:
4764 case NestedNameSpecifier::TypeSpecWithTemplate: {
4765 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4766 if (!T.isNull()) {
4767 bool bTemplate = FromNNS->getKind() ==
4768 NestedNameSpecifier::TypeSpecWithTemplate;
4769 return NestedNameSpecifier::Create(ToContext, prefix,
4770 bTemplate, T.getTypePtr());
4771 }
4772 }
Craig Topper36250ad2014-05-12 05:36:57 +00004773 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00004774 }
4775
4776 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004777}
4778
Douglas Gregor14454802011-02-25 02:25:35 +00004779NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4780 // FIXME: Implement!
4781 return NestedNameSpecifierLoc();
4782}
4783
Douglas Gregore2e50d332010-12-01 01:36:18 +00004784TemplateName ASTImporter::Import(TemplateName From) {
4785 switch (From.getKind()) {
4786 case TemplateName::Template:
4787 if (TemplateDecl *ToTemplate
4788 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4789 return TemplateName(ToTemplate);
4790
4791 return TemplateName();
4792
4793 case TemplateName::OverloadedTemplate: {
4794 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4795 UnresolvedSet<2> ToTemplates;
4796 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4797 E = FromStorage->end();
4798 I != E; ++I) {
4799 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4800 ToTemplates.addDecl(To);
4801 else
4802 return TemplateName();
4803 }
4804 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4805 ToTemplates.end());
4806 }
4807
4808 case TemplateName::QualifiedTemplate: {
4809 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4810 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4811 if (!Qualifier)
4812 return TemplateName();
4813
4814 if (TemplateDecl *ToTemplate
4815 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4816 return ToContext.getQualifiedTemplateName(Qualifier,
4817 QTN->hasTemplateKeyword(),
4818 ToTemplate);
4819
4820 return TemplateName();
4821 }
4822
4823 case TemplateName::DependentTemplate: {
4824 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4825 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4826 if (!Qualifier)
4827 return TemplateName();
4828
4829 if (DTN->isIdentifier()) {
4830 return ToContext.getDependentTemplateName(Qualifier,
4831 Import(DTN->getIdentifier()));
4832 }
4833
4834 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4835 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004836
4837 case TemplateName::SubstTemplateTemplateParm: {
4838 SubstTemplateTemplateParmStorage *subst
4839 = From.getAsSubstTemplateTemplateParm();
4840 TemplateTemplateParmDecl *param
4841 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4842 if (!param)
4843 return TemplateName();
4844
4845 TemplateName replacement = Import(subst->getReplacement());
4846 if (replacement.isNull()) return TemplateName();
4847
4848 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4849 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004850
4851 case TemplateName::SubstTemplateTemplateParmPack: {
4852 SubstTemplateTemplateParmPackStorage *SubstPack
4853 = From.getAsSubstTemplateTemplateParmPack();
4854 TemplateTemplateParmDecl *Param
4855 = cast_or_null<TemplateTemplateParmDecl>(
4856 Import(SubstPack->getParameterPack()));
4857 if (!Param)
4858 return TemplateName();
4859
4860 ASTNodeImporter Importer(*this);
4861 TemplateArgument ArgPack
4862 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4863 if (ArgPack.isNull())
4864 return TemplateName();
4865
4866 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4867 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004868 }
4869
4870 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004871}
4872
Douglas Gregor62d311f2010-02-09 19:21:46 +00004873SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4874 if (FromLoc.isInvalid())
4875 return SourceLocation();
4876
Douglas Gregor811663e2010-02-10 00:15:17 +00004877 SourceManager &FromSM = FromContext.getSourceManager();
4878
4879 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004880 // don't have to import macro expansions.
4881 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004882 FromLoc = FromSM.getSpellingLoc(FromLoc);
4883 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4884 SourceManager &ToSM = ToContext.getSourceManager();
4885 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004886 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004887}
4888
4889SourceRange ASTImporter::Import(SourceRange FromRange) {
4890 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4891}
4892
Douglas Gregor811663e2010-02-10 00:15:17 +00004893FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004894 llvm::DenseMap<FileID, FileID>::iterator Pos
4895 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004896 if (Pos != ImportedFileIDs.end())
4897 return Pos->second;
4898
4899 SourceManager &FromSM = FromContext.getSourceManager();
4900 SourceManager &ToSM = ToContext.getSourceManager();
4901 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004902 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004903
4904 // Include location of this file.
4905 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4906
4907 // Map the FileID for to the "to" source manager.
4908 FileID ToID;
4909 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004910 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004911 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4912 // disk again
4913 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4914 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004915 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004916 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4917 FromSLoc.getFile().getFileCharacteristic());
4918 } else {
4919 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004920 const llvm::MemoryBuffer *
4921 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00004922 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004923 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004924 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00004925 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00004926 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00004927 }
4928
4929
Sebastian Redl99219f12010-09-30 01:03:06 +00004930 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004931 return ToID;
4932}
4933
Douglas Gregor0a791672011-01-18 03:11:38 +00004934void ASTImporter::ImportDefinition(Decl *From) {
4935 Decl *To = Import(From);
4936 if (!To)
4937 return;
4938
4939 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4940 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004941
4942 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4943 if (!ToRecord->getDefinition()) {
4944 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004945 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004946 return;
4947 }
4948 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004949
4950 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4951 if (!ToEnum->getDefinition()) {
4952 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004953 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004954 return;
4955 }
4956 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004957
4958 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4959 if (!ToIFace->getDefinition()) {
4960 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004961 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004962 return;
4963 }
4964 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004965
Douglas Gregor2aa53772012-01-24 17:42:07 +00004966 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4967 if (!ToProto->getDefinition()) {
4968 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004969 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004970 return;
4971 }
4972 }
4973
Douglas Gregor0a791672011-01-18 03:11:38 +00004974 Importer.ImportDeclContext(FromDC, true);
4975 }
4976}
4977
Douglas Gregor96e578d2010-02-05 17:54:41 +00004978DeclarationName ASTImporter::Import(DeclarationName FromName) {
4979 if (!FromName)
4980 return DeclarationName();
4981
4982 switch (FromName.getNameKind()) {
4983 case DeclarationName::Identifier:
4984 return Import(FromName.getAsIdentifierInfo());
4985
4986 case DeclarationName::ObjCZeroArgSelector:
4987 case DeclarationName::ObjCOneArgSelector:
4988 case DeclarationName::ObjCMultiArgSelector:
4989 return Import(FromName.getObjCSelector());
4990
4991 case DeclarationName::CXXConstructorName: {
4992 QualType T = Import(FromName.getCXXNameType());
4993 if (T.isNull())
4994 return DeclarationName();
4995
4996 return ToContext.DeclarationNames.getCXXConstructorName(
4997 ToContext.getCanonicalType(T));
4998 }
4999
5000 case DeclarationName::CXXDestructorName: {
5001 QualType T = Import(FromName.getCXXNameType());
5002 if (T.isNull())
5003 return DeclarationName();
5004
5005 return ToContext.DeclarationNames.getCXXDestructorName(
5006 ToContext.getCanonicalType(T));
5007 }
5008
5009 case DeclarationName::CXXConversionFunctionName: {
5010 QualType T = Import(FromName.getCXXNameType());
5011 if (T.isNull())
5012 return DeclarationName();
5013
5014 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5015 ToContext.getCanonicalType(T));
5016 }
5017
5018 case DeclarationName::CXXOperatorName:
5019 return ToContext.DeclarationNames.getCXXOperatorName(
5020 FromName.getCXXOverloadedOperator());
5021
5022 case DeclarationName::CXXLiteralOperatorName:
5023 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5024 Import(FromName.getCXXLiteralIdentifier()));
5025
5026 case DeclarationName::CXXUsingDirective:
5027 // FIXME: STATICS!
5028 return DeclarationName::getUsingDirectiveName();
5029 }
5030
David Blaikiee4d798f2012-01-20 21:50:17 +00005031 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005032}
5033
Douglas Gregore2e50d332010-12-01 01:36:18 +00005034IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005035 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005036 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005037
5038 return &ToContext.Idents.get(FromId->getName());
5039}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005040
Douglas Gregor43f54792010-02-17 02:12:47 +00005041Selector ASTImporter::Import(Selector FromSel) {
5042 if (FromSel.isNull())
5043 return Selector();
5044
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005045 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005046 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5047 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5048 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5049 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5050}
5051
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005052DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5053 DeclContext *DC,
5054 unsigned IDNS,
5055 NamedDecl **Decls,
5056 unsigned NumDecls) {
5057 return Name;
5058}
5059
5060DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005061 if (LastDiagFromFrom)
5062 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5063 FromContext.getDiagnostics());
5064 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005065 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005066}
5067
5068DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005069 if (!LastDiagFromFrom)
5070 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5071 ToContext.getDiagnostics());
5072 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005073 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005074}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005075
Douglas Gregor2e15c842012-02-01 21:00:38 +00005076void ASTImporter::CompleteDecl (Decl *D) {
5077 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5078 if (!ID->getDefinition())
5079 ID->startDefinition();
5080 }
5081 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5082 if (!PD->getDefinition())
5083 PD->startDefinition();
5084 }
5085 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5086 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5087 TD->startDefinition();
5088 TD->setCompleteDefinition(true);
5089 }
5090 }
5091 else {
5092 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5093 }
5094}
5095
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005096Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5097 ImportedDecls[From] = To;
5098 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005099}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005100
Douglas Gregordd6006f2012-07-17 21:16:27 +00005101bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5102 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005103 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005104 = ImportedTypes.find(From.getTypePtr());
5105 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5106 return true;
5107
Douglas Gregordd6006f2012-07-17 21:16:27 +00005108 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5109 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005110 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005111}