blob: f288953df615bfd369f607318f3cd19435c58814 [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);
Douglas Gregord451ea92011-07-29 23:31:30 +000084 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
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);
Douglas Gregord451ea92011-07-29 23:31:30 +0000109 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000110 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000111 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000112 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000113 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000115 TemplateParameterList *ImportTemplateParameterList(
116 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000117 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
118 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
119 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000121 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
122 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000123 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000124 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000125 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000126 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000127 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000128 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000129 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000130 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000131 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000132 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000133 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000134 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000135 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000136 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
137 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
138 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
139 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000141 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000142 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000143 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000144 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000145 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000146 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000147 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000148 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000149 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000150 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000151 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000152 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000153 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000154 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
155 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
156 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
157 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000158 Decl *VisitClassTemplateSpecializationDecl(
159 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000160
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000161 // Importing statements
162 Stmt *VisitStmt(Stmt *S);
163
164 // Importing expressions
165 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000166 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000167 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000168 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000169 Expr *VisitParenExpr(ParenExpr *E);
170 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000171 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000172 Expr *VisitBinaryOperator(BinaryOperator *E);
173 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000174 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000175 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176 };
177}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000178using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179
180//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000181// Structural Equivalence
182//----------------------------------------------------------------------------
183
184namespace {
185 struct StructuralEquivalenceContext {
186 /// \brief AST contexts for which we are checking structural equivalence.
187 ASTContext &C1, &C2;
188
Douglas Gregor3996e242010-02-15 22:01:00 +0000189 /// \brief The set of "tentative" equivalences between two canonical
190 /// declarations, mapping from a declaration in the first context to the
191 /// declaration in the second context that we believe to be equivalent.
192 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
193
194 /// \brief Queue of declarations in the first context whose equivalence
195 /// with a declaration in the second context still needs to be verified.
196 std::deque<Decl *> DeclsToCheck;
197
Douglas Gregorb4964f72010-02-15 23:54:17 +0000198 /// \brief Declaration (from, to) pairs that are known not to be equivalent
199 /// (which we have already complained about).
200 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
201
Douglas Gregor3996e242010-02-15 22:01:00 +0000202 /// \brief Whether we're being strict about the spelling of types when
203 /// unifying two types.
204 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000205
206 /// \brief Whether to complain about failures.
207 bool Complain;
208
Douglas Gregor3996e242010-02-15 22:01:00 +0000209 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000210 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000211 bool StrictTypeSpelling = false,
212 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000213 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregordd6006f2012-07-17 21:16:27 +0000214 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000215
216 /// \brief Determine whether the two declarations are structurally
217 /// equivalent.
218 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
219
220 /// \brief Determine whether the two types are structurally equivalent.
221 bool IsStructurallyEquivalent(QualType T1, QualType T2);
222
223 private:
224 /// \brief Finish checking all of the structural equivalences.
225 ///
226 /// \returns true if an error occurred, false otherwise.
227 bool Finish();
228
229 public:
230 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000231 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000232 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000233 }
234
235 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000236 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000237 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000238 }
239 };
240}
241
242static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
243 QualType T1, QualType T2);
244static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
245 Decl *D1, Decl *D2);
246
Douglas Gregor3996e242010-02-15 22:01:00 +0000247/// \brief Determine structural equivalence of two expressions.
248static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
249 Expr *E1, Expr *E2) {
250 if (!E1 || !E2)
251 return E1 == E2;
252
253 // FIXME: Actually perform a structural comparison!
254 return true;
255}
256
257/// \brief Determine whether two identifiers are equivalent.
258static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
259 const IdentifierInfo *Name2) {
260 if (!Name1 || !Name2)
261 return Name1 == Name2;
262
263 return Name1->getName() == Name2->getName();
264}
265
266/// \brief Determine whether two nested-name-specifiers are equivalent.
267static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
268 NestedNameSpecifier *NNS1,
269 NestedNameSpecifier *NNS2) {
270 // FIXME: Implement!
271 return true;
272}
273
274/// \brief Determine whether two template arguments are equivalent.
275static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
276 const TemplateArgument &Arg1,
277 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000278 if (Arg1.getKind() != Arg2.getKind())
279 return false;
280
281 switch (Arg1.getKind()) {
282 case TemplateArgument::Null:
283 return true;
284
285 case TemplateArgument::Type:
286 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000287
Douglas Gregore2e50d332010-12-01 01:36:18 +0000288 case TemplateArgument::Integral:
289 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
290 Arg2.getIntegralType()))
291 return false;
292
Eric Christopher6dcc3762012-07-15 00:23:57 +0000293 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000294
295 case TemplateArgument::Declaration:
296 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000297
298 case TemplateArgument::NullPtr:
299 return true; // FIXME: Is this correct?
300
Douglas Gregore2e50d332010-12-01 01:36:18 +0000301 case TemplateArgument::Template:
302 return IsStructurallyEquivalent(Context,
303 Arg1.getAsTemplate(),
304 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000305
306 case TemplateArgument::TemplateExpansion:
307 return IsStructurallyEquivalent(Context,
308 Arg1.getAsTemplateOrTemplatePattern(),
309 Arg2.getAsTemplateOrTemplatePattern());
310
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311 case TemplateArgument::Expression:
312 return IsStructurallyEquivalent(Context,
313 Arg1.getAsExpr(), Arg2.getAsExpr());
314
315 case TemplateArgument::Pack:
316 if (Arg1.pack_size() != Arg2.pack_size())
317 return false;
318
319 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
320 if (!IsStructurallyEquivalent(Context,
321 Arg1.pack_begin()[I],
322 Arg2.pack_begin()[I]))
323 return false;
324
325 return true;
326 }
327
328 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000329}
330
331/// \brief Determine structural equivalence for the common part of array
332/// types.
333static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
334 const ArrayType *Array1,
335 const ArrayType *Array2) {
336 if (!IsStructurallyEquivalent(Context,
337 Array1->getElementType(),
338 Array2->getElementType()))
339 return false;
340 if (Array1->getSizeModifier() != Array2->getSizeModifier())
341 return false;
342 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
343 return false;
344
345 return true;
346}
347
348/// \brief Determine structural equivalence of two types.
349static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
350 QualType T1, QualType T2) {
351 if (T1.isNull() || T2.isNull())
352 return T1.isNull() && T2.isNull();
353
354 if (!Context.StrictTypeSpelling) {
355 // We aren't being strict about token-to-token equivalence of types,
356 // so map down to the canonical type.
357 T1 = Context.C1.getCanonicalType(T1);
358 T2 = Context.C2.getCanonicalType(T2);
359 }
360
361 if (T1.getQualifiers() != T2.getQualifiers())
362 return false;
363
Douglas Gregorb4964f72010-02-15 23:54:17 +0000364 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000365
Douglas Gregorb4964f72010-02-15 23:54:17 +0000366 if (T1->getTypeClass() != T2->getTypeClass()) {
367 // Compare function types with prototypes vs. without prototypes as if
368 // both did not have prototypes.
369 if (T1->getTypeClass() == Type::FunctionProto &&
370 T2->getTypeClass() == Type::FunctionNoProto)
371 TC = Type::FunctionNoProto;
372 else if (T1->getTypeClass() == Type::FunctionNoProto &&
373 T2->getTypeClass() == Type::FunctionProto)
374 TC = Type::FunctionNoProto;
375 else
376 return false;
377 }
378
379 switch (TC) {
380 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000381 // FIXME: Deal with Char_S/Char_U.
382 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
383 return false;
384 break;
385
386 case Type::Complex:
387 if (!IsStructurallyEquivalent(Context,
388 cast<ComplexType>(T1)->getElementType(),
389 cast<ComplexType>(T2)->getElementType()))
390 return false;
391 break;
392
393 case Type::Pointer:
394 if (!IsStructurallyEquivalent(Context,
395 cast<PointerType>(T1)->getPointeeType(),
396 cast<PointerType>(T2)->getPointeeType()))
397 return false;
398 break;
399
400 case Type::BlockPointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<BlockPointerType>(T1)->getPointeeType(),
403 cast<BlockPointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::LValueReference:
408 case Type::RValueReference: {
409 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
410 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
411 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
412 return false;
413 if (Ref1->isInnerRef() != Ref2->isInnerRef())
414 return false;
415 if (!IsStructurallyEquivalent(Context,
416 Ref1->getPointeeTypeAsWritten(),
417 Ref2->getPointeeTypeAsWritten()))
418 return false;
419 break;
420 }
421
422 case Type::MemberPointer: {
423 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
424 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
425 if (!IsStructurallyEquivalent(Context,
426 MemPtr1->getPointeeType(),
427 MemPtr2->getPointeeType()))
428 return false;
429 if (!IsStructurallyEquivalent(Context,
430 QualType(MemPtr1->getClass(), 0),
431 QualType(MemPtr2->getClass(), 0)))
432 return false;
433 break;
434 }
435
436 case Type::ConstantArray: {
437 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
438 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000439 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000440 return false;
441
442 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
443 return false;
444 break;
445 }
446
447 case Type::IncompleteArray:
448 if (!IsArrayStructurallyEquivalent(Context,
449 cast<ArrayType>(T1),
450 cast<ArrayType>(T2)))
451 return false;
452 break;
453
454 case Type::VariableArray: {
455 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
456 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
457 if (!IsStructurallyEquivalent(Context,
458 Array1->getSizeExpr(), Array2->getSizeExpr()))
459 return false;
460
461 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
462 return false;
463
464 break;
465 }
466
467 case Type::DependentSizedArray: {
468 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
469 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
470 if (!IsStructurallyEquivalent(Context,
471 Array1->getSizeExpr(), Array2->getSizeExpr()))
472 return false;
473
474 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
475 return false;
476
477 break;
478 }
479
480 case Type::DependentSizedExtVector: {
481 const DependentSizedExtVectorType *Vec1
482 = cast<DependentSizedExtVectorType>(T1);
483 const DependentSizedExtVectorType *Vec2
484 = cast<DependentSizedExtVectorType>(T2);
485 if (!IsStructurallyEquivalent(Context,
486 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
487 return false;
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getElementType(),
490 Vec2->getElementType()))
491 return false;
492 break;
493 }
494
495 case Type::Vector:
496 case Type::ExtVector: {
497 const VectorType *Vec1 = cast<VectorType>(T1);
498 const VectorType *Vec2 = cast<VectorType>(T2);
499 if (!IsStructurallyEquivalent(Context,
500 Vec1->getElementType(),
501 Vec2->getElementType()))
502 return false;
503 if (Vec1->getNumElements() != Vec2->getNumElements())
504 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000505 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000506 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000507 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000508 }
509
510 case Type::FunctionProto: {
511 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
512 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
513 if (Proto1->getNumArgs() != Proto2->getNumArgs())
514 return false;
515 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
516 if (!IsStructurallyEquivalent(Context,
517 Proto1->getArgType(I),
518 Proto2->getArgType(I)))
519 return false;
520 }
521 if (Proto1->isVariadic() != Proto2->isVariadic())
522 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000523 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000524 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000525 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
526 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
527 return false;
528 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
529 if (!IsStructurallyEquivalent(Context,
530 Proto1->getExceptionType(I),
531 Proto2->getExceptionType(I)))
532 return false;
533 }
534 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000535 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000536 Proto1->getNoexceptExpr(),
537 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000538 return false;
539 }
540 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
541 return false;
542
543 // Fall through to check the bits common with FunctionNoProtoType.
544 }
545
546 case Type::FunctionNoProto: {
547 const FunctionType *Function1 = cast<FunctionType>(T1);
548 const FunctionType *Function2 = cast<FunctionType>(T2);
549 if (!IsStructurallyEquivalent(Context,
550 Function1->getResultType(),
551 Function2->getResultType()))
552 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000553 if (Function1->getExtInfo() != Function2->getExtInfo())
554 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000555 break;
556 }
557
558 case Type::UnresolvedUsing:
559 if (!IsStructurallyEquivalent(Context,
560 cast<UnresolvedUsingType>(T1)->getDecl(),
561 cast<UnresolvedUsingType>(T2)->getDecl()))
562 return false;
563
564 break;
John McCall81904512011-01-06 01:58:22 +0000565
566 case Type::Attributed:
567 if (!IsStructurallyEquivalent(Context,
568 cast<AttributedType>(T1)->getModifiedType(),
569 cast<AttributedType>(T2)->getModifiedType()))
570 return false;
571 if (!IsStructurallyEquivalent(Context,
572 cast<AttributedType>(T1)->getEquivalentType(),
573 cast<AttributedType>(T2)->getEquivalentType()))
574 return false;
575 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000576
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000577 case Type::Paren:
578 if (!IsStructurallyEquivalent(Context,
579 cast<ParenType>(T1)->getInnerType(),
580 cast<ParenType>(T2)->getInnerType()))
581 return false;
582 break;
583
Douglas Gregor3996e242010-02-15 22:01:00 +0000584 case Type::Typedef:
585 if (!IsStructurallyEquivalent(Context,
586 cast<TypedefType>(T1)->getDecl(),
587 cast<TypedefType>(T2)->getDecl()))
588 return false;
589 break;
590
591 case Type::TypeOfExpr:
592 if (!IsStructurallyEquivalent(Context,
593 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
594 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
595 return false;
596 break;
597
598 case Type::TypeOf:
599 if (!IsStructurallyEquivalent(Context,
600 cast<TypeOfType>(T1)->getUnderlyingType(),
601 cast<TypeOfType>(T2)->getUnderlyingType()))
602 return false;
603 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000604
605 case Type::UnaryTransform:
606 if (!IsStructurallyEquivalent(Context,
607 cast<UnaryTransformType>(T1)->getUnderlyingType(),
608 cast<UnaryTransformType>(T1)->getUnderlyingType()))
609 return false;
610 break;
611
Douglas Gregor3996e242010-02-15 22:01:00 +0000612 case Type::Decltype:
613 if (!IsStructurallyEquivalent(Context,
614 cast<DecltypeType>(T1)->getUnderlyingExpr(),
615 cast<DecltypeType>(T2)->getUnderlyingExpr()))
616 return false;
617 break;
618
Richard Smith30482bc2011-02-20 03:19:35 +0000619 case Type::Auto:
620 if (!IsStructurallyEquivalent(Context,
621 cast<AutoType>(T1)->getDeducedType(),
622 cast<AutoType>(T2)->getDeducedType()))
623 return false;
624 break;
625
Douglas Gregor3996e242010-02-15 22:01:00 +0000626 case Type::Record:
627 case Type::Enum:
628 if (!IsStructurallyEquivalent(Context,
629 cast<TagType>(T1)->getDecl(),
630 cast<TagType>(T2)->getDecl()))
631 return false;
632 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000633
Douglas Gregor3996e242010-02-15 22:01:00 +0000634 case Type::TemplateTypeParm: {
635 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
636 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
637 if (Parm1->getDepth() != Parm2->getDepth())
638 return false;
639 if (Parm1->getIndex() != Parm2->getIndex())
640 return false;
641 if (Parm1->isParameterPack() != Parm2->isParameterPack())
642 return false;
643
644 // Names of template type parameters are never significant.
645 break;
646 }
647
648 case Type::SubstTemplateTypeParm: {
649 const SubstTemplateTypeParmType *Subst1
650 = cast<SubstTemplateTypeParmType>(T1);
651 const SubstTemplateTypeParmType *Subst2
652 = cast<SubstTemplateTypeParmType>(T2);
653 if (!IsStructurallyEquivalent(Context,
654 QualType(Subst1->getReplacedParameter(), 0),
655 QualType(Subst2->getReplacedParameter(), 0)))
656 return false;
657 if (!IsStructurallyEquivalent(Context,
658 Subst1->getReplacementType(),
659 Subst2->getReplacementType()))
660 return false;
661 break;
662 }
663
Douglas Gregorfb322d82011-01-14 05:11:40 +0000664 case Type::SubstTemplateTypeParmPack: {
665 const SubstTemplateTypeParmPackType *Subst1
666 = cast<SubstTemplateTypeParmPackType>(T1);
667 const SubstTemplateTypeParmPackType *Subst2
668 = cast<SubstTemplateTypeParmPackType>(T2);
669 if (!IsStructurallyEquivalent(Context,
670 QualType(Subst1->getReplacedParameter(), 0),
671 QualType(Subst2->getReplacedParameter(), 0)))
672 return false;
673 if (!IsStructurallyEquivalent(Context,
674 Subst1->getArgumentPack(),
675 Subst2->getArgumentPack()))
676 return false;
677 break;
678 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000679 case Type::TemplateSpecialization: {
680 const TemplateSpecializationType *Spec1
681 = cast<TemplateSpecializationType>(T1);
682 const TemplateSpecializationType *Spec2
683 = cast<TemplateSpecializationType>(T2);
684 if (!IsStructurallyEquivalent(Context,
685 Spec1->getTemplateName(),
686 Spec2->getTemplateName()))
687 return false;
688 if (Spec1->getNumArgs() != Spec2->getNumArgs())
689 return false;
690 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
691 if (!IsStructurallyEquivalent(Context,
692 Spec1->getArg(I), Spec2->getArg(I)))
693 return false;
694 }
695 break;
696 }
697
Abramo Bagnara6150c882010-05-11 21:36:43 +0000698 case Type::Elaborated: {
699 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
700 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
701 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
702 if (Elab1->getKeyword() != Elab2->getKeyword())
703 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000704 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000705 Elab1->getQualifier(),
706 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 return false;
708 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000709 Elab1->getNamedType(),
710 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000711 return false;
712 break;
713 }
714
John McCalle78aac42010-03-10 03:28:59 +0000715 case Type::InjectedClassName: {
716 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
717 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
718 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000719 Inj1->getInjectedSpecializationType(),
720 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000721 return false;
722 break;
723 }
724
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000725 case Type::DependentName: {
726 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
727 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000728 if (!IsStructurallyEquivalent(Context,
729 Typename1->getQualifier(),
730 Typename2->getQualifier()))
731 return false;
732 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
733 Typename2->getIdentifier()))
734 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000735
736 break;
737 }
738
John McCallc392f372010-06-11 00:33:02 +0000739 case Type::DependentTemplateSpecialization: {
740 const DependentTemplateSpecializationType *Spec1 =
741 cast<DependentTemplateSpecializationType>(T1);
742 const DependentTemplateSpecializationType *Spec2 =
743 cast<DependentTemplateSpecializationType>(T2);
744 if (!IsStructurallyEquivalent(Context,
745 Spec1->getQualifier(),
746 Spec2->getQualifier()))
747 return false;
748 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
749 Spec2->getIdentifier()))
750 return false;
751 if (Spec1->getNumArgs() != Spec2->getNumArgs())
752 return false;
753 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
754 if (!IsStructurallyEquivalent(Context,
755 Spec1->getArg(I), Spec2->getArg(I)))
756 return false;
757 }
758 break;
759 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000760
761 case Type::PackExpansion:
762 if (!IsStructurallyEquivalent(Context,
763 cast<PackExpansionType>(T1)->getPattern(),
764 cast<PackExpansionType>(T2)->getPattern()))
765 return false;
766 break;
767
Douglas Gregor3996e242010-02-15 22:01:00 +0000768 case Type::ObjCInterface: {
769 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
770 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
771 if (!IsStructurallyEquivalent(Context,
772 Iface1->getDecl(), Iface2->getDecl()))
773 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000774 break;
775 }
776
777 case Type::ObjCObject: {
778 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
779 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Obj1->getBaseType(),
782 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000783 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000784 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
785 return false;
786 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000787 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000788 Obj1->getProtocol(I),
789 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000790 return false;
791 }
792 break;
793 }
794
795 case Type::ObjCObjectPointer: {
796 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
797 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
798 if (!IsStructurallyEquivalent(Context,
799 Ptr1->getPointeeType(),
800 Ptr2->getPointeeType()))
801 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000802 break;
803 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000804
805 case Type::Atomic: {
806 if (!IsStructurallyEquivalent(Context,
807 cast<AtomicType>(T1)->getValueType(),
808 cast<AtomicType>(T2)->getValueType()))
809 return false;
810 break;
811 }
812
Douglas Gregor3996e242010-02-15 22:01:00 +0000813 } // end switch
814
815 return true;
816}
817
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000818/// \brief Determine structural equivalence of two fields.
819static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
820 FieldDecl *Field1, FieldDecl *Field2) {
821 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000822
823 // For anonymous structs/unions, match up the anonymous struct/union type
824 // declarations directly, so that we don't go off searching for anonymous
825 // types
826 if (Field1->isAnonymousStructOrUnion() &&
827 Field2->isAnonymousStructOrUnion()) {
828 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
829 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
830 return IsStructurallyEquivalent(Context, D1, D2);
831 }
832
833 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000834 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000835 if (Context.Complain) {
836 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
837 << Context.C2.getTypeDeclType(Owner2);
838 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
839 << Field2->getDeclName() << Field2->getType();
840 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
841 << Field1->getDeclName() << Field1->getType();
842 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000843 return false;
844 }
845
846 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000847 if (Context.Complain) {
848 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
849 << Context.C2.getTypeDeclType(Owner2);
850 if (Field1->isBitField()) {
851 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
852 << Field1->getDeclName() << Field1->getType()
853 << Field1->getBitWidthValue(Context.C1);
854 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
855 << Field2->getDeclName();
856 } else {
857 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
858 << Field2->getDeclName() << Field2->getType()
859 << Field2->getBitWidthValue(Context.C2);
860 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
861 << Field1->getDeclName();
862 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000863 }
864 return false;
865 }
866
867 if (Field1->isBitField()) {
868 // Make sure that the bit-fields are the same length.
869 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
870 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
871
872 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000873 if (Context.Complain) {
874 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
875 << Context.C2.getTypeDeclType(Owner2);
876 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
877 << Field2->getDeclName() << Field2->getType() << Bits2;
878 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
879 << Field1->getDeclName() << Field1->getType() << Bits1;
880 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000881 return false;
882 }
883 }
884
885 return true;
886}
887
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000888/// \brief Find the index of the given anonymous struct/union within its
889/// context.
890///
891/// \returns Returns the index of this anonymous struct/union in its context,
892/// including the next assigned index (if none of them match). Returns an
893/// empty option if the context is not a record, i.e.. if the anonymous
894/// struct/union is at namespace or block scope.
895static llvm::Optional<unsigned>
896findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
897 ASTContext &Context = Anon->getASTContext();
898 QualType AnonTy = Context.getRecordType(Anon);
899
900 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
901 if (!Owner)
902 return llvm::Optional<unsigned>();
903
904 unsigned Index = 0;
905 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
906 DEnd = Owner->noload_decls_end();
907 D != DEnd; ++D) {
908 FieldDecl *F = dyn_cast<FieldDecl>(*D);
909 if (!F || !F->isAnonymousStructOrUnion())
910 continue;
911
912 if (Context.hasSameType(F->getType(), AnonTy))
913 break;
914
915 ++Index;
916 }
917
918 return Index;
919}
920
Douglas Gregor3996e242010-02-15 22:01:00 +0000921/// \brief Determine structural equivalence of two records.
922static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
923 RecordDecl *D1, RecordDecl *D2) {
924 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000925 if (Context.Complain) {
926 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
927 << Context.C2.getTypeDeclType(D2);
928 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
929 << D1->getDeclName() << (unsigned)D1->getTagKind();
930 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000931 return false;
932 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000933
934 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
935 // If both anonymous structs/unions are in a record context, make sure
936 // they occur in the same location in the context records.
937 if (llvm::Optional<unsigned> Index1
938 = findAnonymousStructOrUnionIndex(D1)) {
939 if (llvm::Optional<unsigned> Index2
940 = findAnonymousStructOrUnionIndex(D2)) {
941 if (*Index1 != *Index2)
942 return false;
943 }
944 }
945 }
946
Douglas Gregore2e50d332010-12-01 01:36:18 +0000947 // If both declarations are class template specializations, we know
948 // the ODR applies, so check the template and template arguments.
949 ClassTemplateSpecializationDecl *Spec1
950 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
951 ClassTemplateSpecializationDecl *Spec2
952 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
953 if (Spec1 && Spec2) {
954 // Check that the specialized templates are the same.
955 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
956 Spec2->getSpecializedTemplate()))
957 return false;
958
959 // Check that the template arguments are the same.
960 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
961 return false;
962
963 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
964 if (!IsStructurallyEquivalent(Context,
965 Spec1->getTemplateArgs().get(I),
966 Spec2->getTemplateArgs().get(I)))
967 return false;
968 }
969 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000970 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000971 else if (Spec1 || Spec2)
972 return false;
973
Douglas Gregorb4964f72010-02-15 23:54:17 +0000974 // Compare the definitions of these two records. If either or both are
975 // incomplete, we assume that they are equivalent.
976 D1 = D1->getDefinition();
977 D2 = D2->getDefinition();
978 if (!D1 || !D2)
979 return true;
980
Douglas Gregor3996e242010-02-15 22:01:00 +0000981 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
982 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
983 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000984 if (Context.Complain) {
985 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(D2);
987 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
988 << D2CXX->getNumBases();
989 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
990 << D1CXX->getNumBases();
991 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000992 return false;
993 }
994
995 // Check the base classes.
996 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
997 BaseEnd1 = D1CXX->bases_end(),
998 Base2 = D2CXX->bases_begin();
999 Base1 != BaseEnd1;
1000 ++Base1, ++Base2) {
1001 if (!IsStructurallyEquivalent(Context,
1002 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001003 if (Context.Complain) {
1004 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1005 << Context.C2.getTypeDeclType(D2);
1006 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1007 << Base2->getType()
1008 << Base2->getSourceRange();
1009 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1010 << Base1->getType()
1011 << Base1->getSourceRange();
1012 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001013 return false;
1014 }
1015
1016 // Check virtual vs. non-virtual inheritance mismatch.
1017 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001018 if (Context.Complain) {
1019 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1020 << Context.C2.getTypeDeclType(D2);
1021 Context.Diag2(Base2->getLocStart(),
1022 diag::note_odr_virtual_base)
1023 << Base2->isVirtual() << Base2->getSourceRange();
1024 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1025 << Base1->isVirtual()
1026 << Base1->getSourceRange();
1027 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001028 return false;
1029 }
1030 }
1031 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001032 if (Context.Complain) {
1033 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1034 << Context.C2.getTypeDeclType(D2);
1035 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1036 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1037 << Base1->getType()
1038 << Base1->getSourceRange();
1039 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1040 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001041 return false;
1042 }
1043 }
1044
1045 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001046 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001047 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001048 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001049 Field1End = D1->field_end();
1050 Field1 != Field1End;
1051 ++Field1, ++Field2) {
1052 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001053 if (Context.Complain) {
1054 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1055 << Context.C2.getTypeDeclType(D2);
1056 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1057 << Field1->getDeclName() << Field1->getType();
1058 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1059 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001060 return false;
1061 }
1062
David Blaikie40ed2972012-06-06 20:45:41 +00001063 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001064 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001065 }
1066
1067 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001068 if (Context.Complain) {
1069 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1070 << Context.C2.getTypeDeclType(D2);
1071 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1072 << Field2->getDeclName() << Field2->getType();
1073 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1074 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001075 return false;
1076 }
1077
1078 return true;
1079}
1080
1081/// \brief Determine structural equivalence of two enums.
1082static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083 EnumDecl *D1, EnumDecl *D2) {
1084 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1085 EC2End = D2->enumerator_end();
1086 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1087 EC1End = D1->enumerator_end();
1088 EC1 != EC1End; ++EC1, ++EC2) {
1089 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001090 if (Context.Complain) {
1091 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1092 << Context.C2.getTypeDeclType(D2);
1093 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1094 << EC1->getDeclName()
1095 << EC1->getInitVal().toString(10);
1096 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1097 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001098 return false;
1099 }
1100
1101 llvm::APSInt Val1 = EC1->getInitVal();
1102 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001103 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001104 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001105 if (Context.Complain) {
1106 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1107 << Context.C2.getTypeDeclType(D2);
1108 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1109 << EC2->getDeclName()
1110 << EC2->getInitVal().toString(10);
1111 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1112 << EC1->getDeclName()
1113 << EC1->getInitVal().toString(10);
1114 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001115 return false;
1116 }
1117 }
1118
1119 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001120 if (Context.Complain) {
1121 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1122 << Context.C2.getTypeDeclType(D2);
1123 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1124 << EC2->getDeclName()
1125 << EC2->getInitVal().toString(10);
1126 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1127 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001128 return false;
1129 }
1130
1131 return true;
1132}
Douglas Gregora082a492010-11-30 19:14:50 +00001133
1134static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1135 TemplateParameterList *Params1,
1136 TemplateParameterList *Params2) {
1137 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001138 if (Context.Complain) {
1139 Context.Diag2(Params2->getTemplateLoc(),
1140 diag::err_odr_different_num_template_parameters)
1141 << Params1->size() << Params2->size();
1142 Context.Diag1(Params1->getTemplateLoc(),
1143 diag::note_odr_template_parameter_list);
1144 }
Douglas Gregora082a492010-11-30 19:14:50 +00001145 return false;
1146 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001147
Douglas Gregora082a492010-11-30 19:14:50 +00001148 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1149 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001150 if (Context.Complain) {
1151 Context.Diag2(Params2->getParam(I)->getLocation(),
1152 diag::err_odr_different_template_parameter_kind);
1153 Context.Diag1(Params1->getParam(I)->getLocation(),
1154 diag::note_odr_template_parameter_here);
1155 }
Douglas Gregora082a492010-11-30 19:14:50 +00001156 return false;
1157 }
1158
1159 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1160 Params2->getParam(I))) {
1161
1162 return false;
1163 }
1164 }
1165
1166 return true;
1167}
1168
1169static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1170 TemplateTypeParmDecl *D1,
1171 TemplateTypeParmDecl *D2) {
1172 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001173 if (Context.Complain) {
1174 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1175 << D2->isParameterPack();
1176 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1177 << D1->isParameterPack();
1178 }
Douglas Gregora082a492010-11-30 19:14:50 +00001179 return false;
1180 }
1181
1182 return true;
1183}
1184
1185static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1186 NonTypeTemplateParmDecl *D1,
1187 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001188 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001189 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001190 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1191 << D2->isParameterPack();
1192 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1193 << D1->isParameterPack();
1194 }
Douglas Gregora082a492010-11-30 19:14:50 +00001195 return false;
1196 }
Douglas Gregora082a492010-11-30 19:14:50 +00001197
1198 // Check types.
1199 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001200 if (Context.Complain) {
1201 Context.Diag2(D2->getLocation(),
1202 diag::err_odr_non_type_parameter_type_inconsistent)
1203 << D2->getType() << D1->getType();
1204 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1205 << D1->getType();
1206 }
Douglas Gregora082a492010-11-30 19:14:50 +00001207 return false;
1208 }
1209
1210 return true;
1211}
1212
1213static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1214 TemplateTemplateParmDecl *D1,
1215 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001216 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001217 if (Context.Complain) {
1218 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1219 << D2->isParameterPack();
1220 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1221 << D1->isParameterPack();
1222 }
Douglas Gregora082a492010-11-30 19:14:50 +00001223 return false;
1224 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001225
Douglas Gregora082a492010-11-30 19:14:50 +00001226 // Check template parameter lists.
1227 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1228 D2->getTemplateParameters());
1229}
1230
1231static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1232 ClassTemplateDecl *D1,
1233 ClassTemplateDecl *D2) {
1234 // Check template parameters.
1235 if (!IsStructurallyEquivalent(Context,
1236 D1->getTemplateParameters(),
1237 D2->getTemplateParameters()))
1238 return false;
1239
1240 // Check the templated declaration.
1241 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1242 D2->getTemplatedDecl());
1243}
1244
Douglas Gregor3996e242010-02-15 22:01:00 +00001245/// \brief Determine structural equivalence of two declarations.
1246static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1247 Decl *D1, Decl *D2) {
1248 // FIXME: Check for known structural equivalences via a callback of some sort.
1249
Douglas Gregorb4964f72010-02-15 23:54:17 +00001250 // Check whether we already know that these two declarations are not
1251 // structurally equivalent.
1252 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1253 D2->getCanonicalDecl())))
1254 return false;
1255
Douglas Gregor3996e242010-02-15 22:01:00 +00001256 // Determine whether we've already produced a tentative equivalence for D1.
1257 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1258 if (EquivToD1)
1259 return EquivToD1 == D2->getCanonicalDecl();
1260
1261 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1262 EquivToD1 = D2->getCanonicalDecl();
1263 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1264 return true;
1265}
1266
1267bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1268 Decl *D2) {
1269 if (!::IsStructurallyEquivalent(*this, D1, D2))
1270 return false;
1271
1272 return !Finish();
1273}
1274
1275bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1276 QualType T2) {
1277 if (!::IsStructurallyEquivalent(*this, T1, T2))
1278 return false;
1279
1280 return !Finish();
1281}
1282
1283bool StructuralEquivalenceContext::Finish() {
1284 while (!DeclsToCheck.empty()) {
1285 // Check the next declaration.
1286 Decl *D1 = DeclsToCheck.front();
1287 DeclsToCheck.pop_front();
1288
1289 Decl *D2 = TentativeEquivalences[D1];
1290 assert(D2 && "Unrecorded tentative equivalence?");
1291
Douglas Gregorb4964f72010-02-15 23:54:17 +00001292 bool Equivalent = true;
1293
Douglas Gregor3996e242010-02-15 22:01:00 +00001294 // FIXME: Switch on all declaration kinds. For now, we're just going to
1295 // check the obvious ones.
1296 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1297 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1298 // Check for equivalent structure names.
1299 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001300 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1301 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001302 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001303 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1304 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001305 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1306 !::IsStructurallyEquivalent(*this, Record1, Record2))
1307 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001308 } else {
1309 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001310 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001311 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001312 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001313 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1314 // Check for equivalent enum names.
1315 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001316 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1317 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001318 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001319 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1320 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001321 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1322 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1323 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001324 } else {
1325 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001326 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001327 }
Richard Smithdda56e42011-04-15 14:24:37 +00001328 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1329 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001330 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001331 Typedef2->getIdentifier()) ||
1332 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001333 Typedef1->getUnderlyingType(),
1334 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001335 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001336 } else {
1337 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001338 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001339 }
Douglas Gregora082a492010-11-30 19:14:50 +00001340 } else if (ClassTemplateDecl *ClassTemplate1
1341 = dyn_cast<ClassTemplateDecl>(D1)) {
1342 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1343 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1344 ClassTemplate2->getIdentifier()) ||
1345 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1346 Equivalent = false;
1347 } else {
1348 // Class template/non-class-template mismatch.
1349 Equivalent = false;
1350 }
1351 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1352 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1353 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1354 Equivalent = false;
1355 } else {
1356 // Kind mismatch.
1357 Equivalent = false;
1358 }
1359 } else if (NonTypeTemplateParmDecl *NTTP1
1360 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1361 if (NonTypeTemplateParmDecl *NTTP2
1362 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1363 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1364 Equivalent = false;
1365 } else {
1366 // Kind mismatch.
1367 Equivalent = false;
1368 }
1369 } else if (TemplateTemplateParmDecl *TTP1
1370 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1371 if (TemplateTemplateParmDecl *TTP2
1372 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1373 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1374 Equivalent = false;
1375 } else {
1376 // Kind mismatch.
1377 Equivalent = false;
1378 }
1379 }
1380
Douglas Gregorb4964f72010-02-15 23:54:17 +00001381 if (!Equivalent) {
1382 // Note that these two declarations are not equivalent (and we already
1383 // know about it).
1384 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1385 D2->getCanonicalDecl()));
1386 return true;
1387 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001388 // FIXME: Check other declaration kinds!
1389 }
1390
1391 return false;
1392}
1393
1394//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001395// Import Types
1396//----------------------------------------------------------------------------
1397
John McCall424cec92011-01-19 06:33:43 +00001398QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001399 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1400 << T->getTypeClassName();
1401 return QualType();
1402}
1403
John McCall424cec92011-01-19 06:33:43 +00001404QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001405 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001406#define SHARED_SINGLETON_TYPE(Expansion)
1407#define BUILTIN_TYPE(Id, SingletonId) \
1408 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1409#include "clang/AST/BuiltinTypes.def"
1410
1411 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1412 // context supports C++.
1413
1414 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1415 // context supports ObjC.
1416
Douglas Gregor96e578d2010-02-05 17:54:41 +00001417 case BuiltinType::Char_U:
1418 // The context we're importing from has an unsigned 'char'. If we're
1419 // importing into a context with a signed 'char', translate to
1420 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001421 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001422 return Importer.getToContext().UnsignedCharTy;
1423
1424 return Importer.getToContext().CharTy;
1425
Douglas Gregor96e578d2010-02-05 17:54:41 +00001426 case BuiltinType::Char_S:
1427 // The context we're importing from has an unsigned 'char'. If we're
1428 // importing into a context with a signed 'char', translate to
1429 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001430 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431 return Importer.getToContext().SignedCharTy;
1432
1433 return Importer.getToContext().CharTy;
1434
Chris Lattnerad3467e2010-12-25 23:25:43 +00001435 case BuiltinType::WChar_S:
1436 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001437 // FIXME: If not in C++, shall we translate to the C equivalent of
1438 // wchar_t?
1439 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001440 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001441
1442 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001443}
1444
John McCall424cec92011-01-19 06:33:43 +00001445QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001446 QualType ToElementType = Importer.Import(T->getElementType());
1447 if (ToElementType.isNull())
1448 return QualType();
1449
1450 return Importer.getToContext().getComplexType(ToElementType);
1451}
1452
John McCall424cec92011-01-19 06:33:43 +00001453QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001454 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1455 if (ToPointeeType.isNull())
1456 return QualType();
1457
1458 return Importer.getToContext().getPointerType(ToPointeeType);
1459}
1460
John McCall424cec92011-01-19 06:33:43 +00001461QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001462 // FIXME: Check for blocks support in "to" context.
1463 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1464 if (ToPointeeType.isNull())
1465 return QualType();
1466
1467 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1468}
1469
John McCall424cec92011-01-19 06:33:43 +00001470QualType
1471ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001472 // FIXME: Check for C++ support in "to" context.
1473 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1474 if (ToPointeeType.isNull())
1475 return QualType();
1476
1477 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1478}
1479
John McCall424cec92011-01-19 06:33:43 +00001480QualType
1481ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001482 // FIXME: Check for C++0x support in "to" context.
1483 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1484 if (ToPointeeType.isNull())
1485 return QualType();
1486
1487 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1488}
1489
John McCall424cec92011-01-19 06:33:43 +00001490QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001491 // FIXME: Check for C++ support in "to" context.
1492 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1493 if (ToPointeeType.isNull())
1494 return QualType();
1495
1496 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1497 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1498 ClassType.getTypePtr());
1499}
1500
John McCall424cec92011-01-19 06:33:43 +00001501QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502 QualType ToElementType = Importer.Import(T->getElementType());
1503 if (ToElementType.isNull())
1504 return QualType();
1505
1506 return Importer.getToContext().getConstantArrayType(ToElementType,
1507 T->getSize(),
1508 T->getSizeModifier(),
1509 T->getIndexTypeCVRQualifiers());
1510}
1511
John McCall424cec92011-01-19 06:33:43 +00001512QualType
1513ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001514 QualType ToElementType = Importer.Import(T->getElementType());
1515 if (ToElementType.isNull())
1516 return QualType();
1517
1518 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1519 T->getSizeModifier(),
1520 T->getIndexTypeCVRQualifiers());
1521}
1522
John McCall424cec92011-01-19 06:33:43 +00001523QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 QualType ToElementType = Importer.Import(T->getElementType());
1525 if (ToElementType.isNull())
1526 return QualType();
1527
1528 Expr *Size = Importer.Import(T->getSizeExpr());
1529 if (!Size)
1530 return QualType();
1531
1532 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1533 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1534 T->getSizeModifier(),
1535 T->getIndexTypeCVRQualifiers(),
1536 Brackets);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 QualType ToElementType = Importer.Import(T->getElementType());
1541 if (ToElementType.isNull())
1542 return QualType();
1543
1544 return Importer.getToContext().getVectorType(ToElementType,
1545 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001546 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 QualType ToElementType = Importer.Import(T->getElementType());
1551 if (ToElementType.isNull())
1552 return QualType();
1553
1554 return Importer.getToContext().getExtVectorType(ToElementType,
1555 T->getNumElements());
1556}
1557
John McCall424cec92011-01-19 06:33:43 +00001558QualType
1559ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 // FIXME: What happens if we're importing a function without a prototype
1561 // into C++? Should we make it variadic?
1562 QualType ToResultType = Importer.Import(T->getResultType());
1563 if (ToResultType.isNull())
1564 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001565
Douglas Gregor96e578d2010-02-05 17:54:41 +00001566 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001567 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568}
1569
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001570QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571 QualType ToResultType = Importer.Import(T->getResultType());
1572 if (ToResultType.isNull())
1573 return QualType();
1574
1575 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001576 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001577 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1578 AEnd = T->arg_type_end();
1579 A != AEnd; ++A) {
1580 QualType ArgType = Importer.Import(*A);
1581 if (ArgType.isNull())
1582 return QualType();
1583 ArgTypes.push_back(ArgType);
1584 }
1585
1586 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001587 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1589 EEnd = T->exception_end();
1590 E != EEnd; ++E) {
1591 QualType ExceptionType = Importer.Import(*E);
1592 if (ExceptionType.isNull())
1593 return QualType();
1594 ExceptionTypes.push_back(ExceptionType);
1595 }
John McCalldb40c7f2010-12-14 08:05:40 +00001596
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001597 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1598 FunctionProtoType::ExtProtoInfo ToEPI;
1599
1600 ToEPI.ExtInfo = FromEPI.ExtInfo;
1601 ToEPI.Variadic = FromEPI.Variadic;
1602 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1603 ToEPI.TypeQuals = FromEPI.TypeQuals;
1604 ToEPI.RefQualifier = FromEPI.RefQualifier;
1605 ToEPI.NumExceptions = ExceptionTypes.size();
1606 ToEPI.Exceptions = ExceptionTypes.data();
1607 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001608 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1609 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1610 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001611 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001612 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001613 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001614
Douglas Gregor96e578d2010-02-05 17:54:41 +00001615 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001616 ArgTypes.size(), ToEPI);
1617}
1618
Sean Callananda6df8a2011-08-11 16:56:07 +00001619QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1620 QualType ToInnerType = Importer.Import(T->getInnerType());
1621 if (ToInnerType.isNull())
1622 return QualType();
1623
1624 return Importer.getToContext().getParenType(ToInnerType);
1625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001628 TypedefNameDecl *ToDecl
1629 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001630 if (!ToDecl)
1631 return QualType();
1632
1633 return Importer.getToContext().getTypeDeclType(ToDecl);
1634}
1635
John McCall424cec92011-01-19 06:33:43 +00001636QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001637 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1638 if (!ToExpr)
1639 return QualType();
1640
1641 return Importer.getToContext().getTypeOfExprType(ToExpr);
1642}
1643
John McCall424cec92011-01-19 06:33:43 +00001644QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001645 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1646 if (ToUnderlyingType.isNull())
1647 return QualType();
1648
1649 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1650}
1651
John McCall424cec92011-01-19 06:33:43 +00001652QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001653 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1655 if (!ToExpr)
1656 return QualType();
1657
Douglas Gregor81495f32012-02-12 18:42:33 +00001658 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1659 if (UnderlyingType.isNull())
1660 return QualType();
1661
1662 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001663}
1664
Alexis Hunte852b102011-05-24 22:41:36 +00001665QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1666 QualType ToBaseType = Importer.Import(T->getBaseType());
1667 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1668 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1669 return QualType();
1670
1671 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1672 ToUnderlyingType,
1673 T->getUTTKind());
1674}
1675
Richard Smith30482bc2011-02-20 03:19:35 +00001676QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1677 // FIXME: Make sure that the "to" context supports C++0x!
1678 QualType FromDeduced = T->getDeducedType();
1679 QualType ToDeduced;
1680 if (!FromDeduced.isNull()) {
1681 ToDeduced = Importer.Import(FromDeduced);
1682 if (ToDeduced.isNull())
1683 return QualType();
1684 }
1685
1686 return Importer.getToContext().getAutoType(ToDeduced);
1687}
1688
John McCall424cec92011-01-19 06:33:43 +00001689QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001690 RecordDecl *ToDecl
1691 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1692 if (!ToDecl)
1693 return QualType();
1694
1695 return Importer.getToContext().getTagDeclType(ToDecl);
1696}
1697
John McCall424cec92011-01-19 06:33:43 +00001698QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001699 EnumDecl *ToDecl
1700 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1701 if (!ToDecl)
1702 return QualType();
1703
1704 return Importer.getToContext().getTagDeclType(ToDecl);
1705}
1706
Douglas Gregore2e50d332010-12-01 01:36:18 +00001707QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001708 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001709 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1710 if (ToTemplate.isNull())
1711 return QualType();
1712
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001713 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001714 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1715 return QualType();
1716
1717 QualType ToCanonType;
1718 if (!QualType(T, 0).isCanonical()) {
1719 QualType FromCanonType
1720 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1721 ToCanonType =Importer.Import(FromCanonType);
1722 if (ToCanonType.isNull())
1723 return QualType();
1724 }
1725 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1726 ToTemplateArgs.data(),
1727 ToTemplateArgs.size(),
1728 ToCanonType);
1729}
1730
John McCall424cec92011-01-19 06:33:43 +00001731QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001732 NestedNameSpecifier *ToQualifier = 0;
1733 // Note: the qualifier in an ElaboratedType is optional.
1734 if (T->getQualifier()) {
1735 ToQualifier = Importer.Import(T->getQualifier());
1736 if (!ToQualifier)
1737 return QualType();
1738 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001739
1740 QualType ToNamedType = Importer.Import(T->getNamedType());
1741 if (ToNamedType.isNull())
1742 return QualType();
1743
Abramo Bagnara6150c882010-05-11 21:36:43 +00001744 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1745 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001746}
1747
John McCall424cec92011-01-19 06:33:43 +00001748QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001749 ObjCInterfaceDecl *Class
1750 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1751 if (!Class)
1752 return QualType();
1753
John McCall8b07ec22010-05-15 11:32:37 +00001754 return Importer.getToContext().getObjCInterfaceType(Class);
1755}
1756
John McCall424cec92011-01-19 06:33:43 +00001757QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001758 QualType ToBaseType = Importer.Import(T->getBaseType());
1759 if (ToBaseType.isNull())
1760 return QualType();
1761
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001762 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001763 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001764 PEnd = T->qual_end();
1765 P != PEnd; ++P) {
1766 ObjCProtocolDecl *Protocol
1767 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1768 if (!Protocol)
1769 return QualType();
1770 Protocols.push_back(Protocol);
1771 }
1772
John McCall8b07ec22010-05-15 11:32:37 +00001773 return Importer.getToContext().getObjCObjectType(ToBaseType,
1774 Protocols.data(),
1775 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001776}
1777
John McCall424cec92011-01-19 06:33:43 +00001778QualType
1779ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001780 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1781 if (ToPointeeType.isNull())
1782 return QualType();
1783
John McCall8b07ec22010-05-15 11:32:37 +00001784 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001785}
1786
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001787//----------------------------------------------------------------------------
1788// Import Declarations
1789//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001790bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1791 DeclContext *&LexicalDC,
1792 DeclarationName &Name,
1793 SourceLocation &Loc) {
1794 // Import the context of this declaration.
1795 DC = Importer.ImportContext(D->getDeclContext());
1796 if (!DC)
1797 return true;
1798
1799 LexicalDC = DC;
1800 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1801 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1802 if (!LexicalDC)
1803 return true;
1804 }
1805
1806 // Import the name of this declaration.
1807 Name = Importer.Import(D->getDeclName());
1808 if (D->getDeclName() && !Name)
1809 return true;
1810
1811 // Import the location of this declaration.
1812 Loc = Importer.Import(D->getLocation());
1813 return false;
1814}
1815
Douglas Gregord451ea92011-07-29 23:31:30 +00001816void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1817 if (!FromD)
1818 return;
1819
1820 if (!ToD) {
1821 ToD = Importer.Import(FromD);
1822 if (!ToD)
1823 return;
1824 }
1825
1826 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1827 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1828 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1829 ImportDefinition(FromRecord, ToRecord);
1830 }
1831 }
1832 return;
1833 }
1834
1835 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1836 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1837 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1838 ImportDefinition(FromEnum, ToEnum);
1839 }
1840 }
1841 return;
1842 }
1843}
1844
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001845void
1846ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1847 DeclarationNameInfo& To) {
1848 // NOTE: To.Name and To.Loc are already imported.
1849 // We only have to import To.LocInfo.
1850 switch (To.getName().getNameKind()) {
1851 case DeclarationName::Identifier:
1852 case DeclarationName::ObjCZeroArgSelector:
1853 case DeclarationName::ObjCOneArgSelector:
1854 case DeclarationName::ObjCMultiArgSelector:
1855 case DeclarationName::CXXUsingDirective:
1856 return;
1857
1858 case DeclarationName::CXXOperatorName: {
1859 SourceRange Range = From.getCXXOperatorNameRange();
1860 To.setCXXOperatorNameRange(Importer.Import(Range));
1861 return;
1862 }
1863 case DeclarationName::CXXLiteralOperatorName: {
1864 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1865 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1866 return;
1867 }
1868 case DeclarationName::CXXConstructorName:
1869 case DeclarationName::CXXDestructorName:
1870 case DeclarationName::CXXConversionFunctionName: {
1871 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1872 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1873 return;
1874 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001875 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001876 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001877}
1878
Douglas Gregor2e15c842012-02-01 21:00:38 +00001879void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001880 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001881 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001882 return;
1883 }
1884
Douglas Gregor968d6332010-02-21 18:24:45 +00001885 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1886 FromEnd = FromDC->decls_end();
1887 From != FromEnd;
1888 ++From)
1889 Importer.Import(*From);
1890}
1891
Douglas Gregord451ea92011-07-29 23:31:30 +00001892bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001893 ImportDefinitionKind Kind) {
1894 if (To->getDefinition() || To->isBeingDefined()) {
1895 if (Kind == IDK_Everything)
1896 ImportDeclContext(From, /*ForceImport=*/true);
1897
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001899 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001900
1901 To->startDefinition();
1902
1903 // Add base classes.
1904 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1905 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001906
1907 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1908 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1909 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001910 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001911 ToData.Aggregate = FromData.Aggregate;
1912 ToData.PlainOldData = FromData.PlainOldData;
1913 ToData.Empty = FromData.Empty;
1914 ToData.Polymorphic = FromData.Polymorphic;
1915 ToData.Abstract = FromData.Abstract;
1916 ToData.IsStandardLayout = FromData.IsStandardLayout;
1917 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1918 ToData.HasPrivateFields = FromData.HasPrivateFields;
1919 ToData.HasProtectedFields = FromData.HasProtectedFields;
1920 ToData.HasPublicFields = FromData.HasPublicFields;
1921 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001922 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001923 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001924 ToData.HasUninitializedReferenceMember
1925 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00001926 ToData.NeedOverloadResolutionForMoveConstructor
1927 = FromData.NeedOverloadResolutionForMoveConstructor;
1928 ToData.NeedOverloadResolutionForMoveAssignment
1929 = FromData.NeedOverloadResolutionForMoveAssignment;
1930 ToData.NeedOverloadResolutionForDestructor
1931 = FromData.NeedOverloadResolutionForDestructor;
1932 ToData.DefaultedMoveConstructorIsDeleted
1933 = FromData.DefaultedMoveConstructorIsDeleted;
1934 ToData.DefaultedMoveAssignmentIsDeleted
1935 = FromData.DefaultedMoveAssignmentIsDeleted;
1936 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001937 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1938 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001939 ToData.HasConstexprNonCopyMoveConstructor
1940 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001941 ToData.DefaultedDefaultConstructorIsConstexpr
1942 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001943 ToData.HasConstexprDefaultConstructor
1944 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001945 ToData.HasNonLiteralTypeFieldsOrBases
1946 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001947 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001948 ToData.UserProvidedDefaultConstructor
1949 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001950 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00001951 ToData.ImplicitCopyConstructorHasConstParam
1952 = FromData.ImplicitCopyConstructorHasConstParam;
1953 ToData.ImplicitCopyAssignmentHasConstParam
1954 = FromData.ImplicitCopyAssignmentHasConstParam;
1955 ToData.HasDeclaredCopyConstructorWithConstParam
1956 = FromData.HasDeclaredCopyConstructorWithConstParam;
1957 ToData.HasDeclaredCopyAssignmentWithConstParam
1958 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001959 ToData.FailedImplicitMoveConstructor
1960 = FromData.FailedImplicitMoveConstructor;
1961 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001962 ToData.IsLambda = FromData.IsLambda;
1963
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001964 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001965 for (CXXRecordDecl::base_class_iterator
1966 Base1 = FromCXX->bases_begin(),
1967 FromBaseEnd = FromCXX->bases_end();
1968 Base1 != FromBaseEnd;
1969 ++Base1) {
1970 QualType T = Importer.Import(Base1->getType());
1971 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001972 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001973
1974 SourceLocation EllipsisLoc;
1975 if (Base1->isPackExpansion())
1976 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001977
1978 // Ensure that we have a definition for the base.
1979 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1980
Douglas Gregore2e50d332010-12-01 01:36:18 +00001981 Bases.push_back(
1982 new (Importer.getToContext())
1983 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1984 Base1->isVirtual(),
1985 Base1->isBaseOfClass(),
1986 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001987 Importer.Import(Base1->getTypeSourceInfo()),
1988 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001989 }
1990 if (!Bases.empty())
1991 ToCXX->setBases(Bases.data(), Bases.size());
1992 }
1993
Douglas Gregor2e15c842012-02-01 21:00:38 +00001994 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001995 ImportDeclContext(From, /*ForceImport=*/true);
1996
Douglas Gregore2e50d332010-12-01 01:36:18 +00001997 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001998 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001999}
2000
Douglas Gregord451ea92011-07-29 23:31:30 +00002001bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002002 ImportDefinitionKind Kind) {
2003 if (To->getDefinition() || To->isBeingDefined()) {
2004 if (Kind == IDK_Everything)
2005 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002006 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002007 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002008
2009 To->startDefinition();
2010
2011 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2012 if (T.isNull())
2013 return true;
2014
2015 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2016 if (ToPromotionType.isNull())
2017 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002018
2019 if (shouldForceImportDeclContext(Kind))
2020 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002021
2022 // FIXME: we might need to merge the number of positive or negative bits
2023 // if the enumerator lists don't match.
2024 To->completeDefinition(T, ToPromotionType,
2025 From->getNumPositiveBits(),
2026 From->getNumNegativeBits());
2027 return false;
2028}
2029
Douglas Gregora082a492010-11-30 19:14:50 +00002030TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2031 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002032 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002033 ToParams.reserve(Params->size());
2034 for (TemplateParameterList::iterator P = Params->begin(),
2035 PEnd = Params->end();
2036 P != PEnd; ++P) {
2037 Decl *To = Importer.Import(*P);
2038 if (!To)
2039 return 0;
2040
2041 ToParams.push_back(cast<NamedDecl>(To));
2042 }
2043
2044 return TemplateParameterList::Create(Importer.getToContext(),
2045 Importer.Import(Params->getTemplateLoc()),
2046 Importer.Import(Params->getLAngleLoc()),
2047 ToParams.data(), ToParams.size(),
2048 Importer.Import(Params->getRAngleLoc()));
2049}
2050
Douglas Gregore2e50d332010-12-01 01:36:18 +00002051TemplateArgument
2052ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2053 switch (From.getKind()) {
2054 case TemplateArgument::Null:
2055 return TemplateArgument();
2056
2057 case TemplateArgument::Type: {
2058 QualType ToType = Importer.Import(From.getAsType());
2059 if (ToType.isNull())
2060 return TemplateArgument();
2061 return TemplateArgument(ToType);
2062 }
2063
2064 case TemplateArgument::Integral: {
2065 QualType ToType = Importer.Import(From.getIntegralType());
2066 if (ToType.isNull())
2067 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002068 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002069 }
2070
Eli Friedmanb826a002012-09-26 02:36:12 +00002071 case TemplateArgument::Declaration: {
2072 ValueDecl *FromD = From.getAsDecl();
2073 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2074 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002075 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002076 }
2077
2078 case TemplateArgument::NullPtr: {
2079 QualType ToType = Importer.Import(From.getNullPtrType());
2080 if (ToType.isNull())
2081 return TemplateArgument();
2082 return TemplateArgument(ToType, /*isNullPtr*/true);
2083 }
2084
Douglas Gregore2e50d332010-12-01 01:36:18 +00002085 case TemplateArgument::Template: {
2086 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2087 if (ToTemplate.isNull())
2088 return TemplateArgument();
2089
2090 return TemplateArgument(ToTemplate);
2091 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002092
2093 case TemplateArgument::TemplateExpansion: {
2094 TemplateName ToTemplate
2095 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2096 if (ToTemplate.isNull())
2097 return TemplateArgument();
2098
Douglas Gregore1d60df2011-01-14 23:41:42 +00002099 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002100 }
2101
Douglas Gregore2e50d332010-12-01 01:36:18 +00002102 case TemplateArgument::Expression:
2103 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2104 return TemplateArgument(ToExpr);
2105 return TemplateArgument();
2106
2107 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002108 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002109 ToPack.reserve(From.pack_size());
2110 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2111 return TemplateArgument();
2112
2113 TemplateArgument *ToArgs
2114 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2115 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2116 return TemplateArgument(ToArgs, ToPack.size());
2117 }
2118 }
2119
2120 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002121}
2122
2123bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2124 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002125 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002126 for (unsigned I = 0; I != NumFromArgs; ++I) {
2127 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2128 if (To.isNull() && !FromArgs[I].isNull())
2129 return true;
2130
2131 ToArgs.push_back(To);
2132 }
2133
2134 return false;
2135}
2136
Douglas Gregor5c73e912010-02-11 00:48:18 +00002137bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002138 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002139 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002140 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002141 Importer.getNonEquivalentDecls(),
2142 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002143 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002144}
2145
Douglas Gregor98c10182010-02-12 22:17:39 +00002146bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002147 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002148 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002149 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002150 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002151}
2152
Douglas Gregor91155082012-11-14 22:29:20 +00002153bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2154 EnumConstantDecl *ToEC)
2155{
2156 const llvm::APSInt &FromVal = FromEC->getInitVal();
2157 const llvm::APSInt &ToVal = ToEC->getInitVal();
2158
2159 return FromVal.isSigned() == ToVal.isSigned() &&
2160 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2161 FromVal == ToVal;
2162}
2163
2164bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002165 ClassTemplateDecl *To) {
2166 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2167 Importer.getToContext(),
2168 Importer.getNonEquivalentDecls());
2169 return Ctx.IsStructurallyEquivalent(From, To);
2170}
2171
Douglas Gregore4c83e42010-02-09 22:48:33 +00002172Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002173 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002174 << D->getDeclKindName();
2175 return 0;
2176}
2177
Sean Callanan65198272011-11-17 23:20:56 +00002178Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2179 TranslationUnitDecl *ToD =
2180 Importer.getToContext().getTranslationUnitDecl();
2181
2182 Importer.Imported(D, ToD);
2183
2184 return ToD;
2185}
2186
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002187Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2188 // Import the major distinguishing characteristics of this namespace.
2189 DeclContext *DC, *LexicalDC;
2190 DeclarationName Name;
2191 SourceLocation Loc;
2192 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2193 return 0;
2194
2195 NamespaceDecl *MergeWithNamespace = 0;
2196 if (!Name) {
2197 // This is an anonymous namespace. Adopt an existing anonymous
2198 // namespace if we can.
2199 // FIXME: Not testable.
2200 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2201 MergeWithNamespace = TU->getAnonymousNamespace();
2202 else
2203 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2204 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002205 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002206 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2207 DC->localUncachedLookup(Name, FoundDecls);
2208 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2209 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002210 continue;
2211
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002212 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002213 MergeWithNamespace = FoundNS;
2214 ConflictingDecls.clear();
2215 break;
2216 }
2217
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002218 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002219 }
2220
2221 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002222 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002223 ConflictingDecls.data(),
2224 ConflictingDecls.size());
2225 }
2226 }
2227
2228 // Create the "to" namespace, if needed.
2229 NamespaceDecl *ToNamespace = MergeWithNamespace;
2230 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002231 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002232 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002233 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002234 Loc, Name.getAsIdentifierInfo(),
2235 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002236 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002237 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002238
2239 // If this is an anonymous namespace, register it as the anonymous
2240 // namespace within its context.
2241 if (!Name) {
2242 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2243 TU->setAnonymousNamespace(ToNamespace);
2244 else
2245 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2246 }
2247 }
2248 Importer.Imported(D, ToNamespace);
2249
2250 ImportDeclContext(D);
2251
2252 return ToNamespace;
2253}
2254
Richard Smithdda56e42011-04-15 14:24:37 +00002255Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002256 // Import the major distinguishing characteristics of this typedef.
2257 DeclContext *DC, *LexicalDC;
2258 DeclarationName Name;
2259 SourceLocation Loc;
2260 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2261 return 0;
2262
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002263 // If this typedef is not in block scope, determine whether we've
2264 // seen a typedef with the same name (that we can merge with) or any
2265 // other entity by that name (which name lookup could conflict with).
2266 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002267 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002268 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002269 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2270 DC->localUncachedLookup(Name, FoundDecls);
2271 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2272 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002273 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002274 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002275 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002276 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2277 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002278 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002279 }
2280
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002281 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002282 }
2283
2284 if (!ConflictingDecls.empty()) {
2285 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2286 ConflictingDecls.data(),
2287 ConflictingDecls.size());
2288 if (!Name)
2289 return 0;
2290 }
2291 }
2292
Douglas Gregorb4964f72010-02-15 23:54:17 +00002293 // Import the underlying type of this typedef;
2294 QualType T = Importer.Import(D->getUnderlyingType());
2295 if (T.isNull())
2296 return 0;
2297
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002298 // Create the new typedef node.
2299 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002300 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002301 TypedefNameDecl *ToTypedef;
2302 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002303 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2304 StartL, Loc,
2305 Name.getAsIdentifierInfo(),
2306 TInfo);
2307 else
Richard Smithdda56e42011-04-15 14:24:37 +00002308 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2309 StartL, Loc,
2310 Name.getAsIdentifierInfo(),
2311 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002312
Douglas Gregordd483172010-02-22 17:42:47 +00002313 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002314 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002315 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002316 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002317
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002318 return ToTypedef;
2319}
2320
Richard Smithdda56e42011-04-15 14:24:37 +00002321Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2322 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2323}
2324
2325Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2326 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2327}
2328
Douglas Gregor98c10182010-02-12 22:17:39 +00002329Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2330 // Import the major distinguishing characteristics of this enum.
2331 DeclContext *DC, *LexicalDC;
2332 DeclarationName Name;
2333 SourceLocation Loc;
2334 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2335 return 0;
2336
2337 // Figure out what enum name we're looking for.
2338 unsigned IDNS = Decl::IDNS_Tag;
2339 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002340 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2341 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002342 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002343 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002344 IDNS |= Decl::IDNS_Ordinary;
2345
2346 // We may already have an enum of the same name; try to find and match it.
2347 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002348 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002349 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2350 DC->localUncachedLookup(SearchName, FoundDecls);
2351 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2352 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002353 continue;
2354
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002355 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002356 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002357 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2358 Found = Tag->getDecl();
2359 }
2360
2361 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002362 if (IsStructuralMatch(D, FoundEnum))
2363 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002364 }
2365
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002366 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002367 }
2368
2369 if (!ConflictingDecls.empty()) {
2370 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2371 ConflictingDecls.data(),
2372 ConflictingDecls.size());
2373 }
2374 }
2375
2376 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002377 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2378 Importer.Import(D->getLocStart()),
2379 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002380 D->isScoped(), D->isScopedUsingClassTag(),
2381 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002382 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002383 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002384 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002385 D2->setLexicalDeclContext(LexicalDC);
2386 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002387 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002388
2389 // Import the integer type.
2390 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2391 if (ToIntegerType.isNull())
2392 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002393 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002394
2395 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002396 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002397 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002398
Douglas Gregor3996e242010-02-15 22:01:00 +00002399 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002400}
2401
Douglas Gregor5c73e912010-02-11 00:48:18 +00002402Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2403 // If this record has a definition in the translation unit we're coming from,
2404 // but this particular declaration is not that definition, import the
2405 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002406 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002407 if (Definition && Definition != D) {
2408 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002409 if (!ImportedDef)
2410 return 0;
2411
2412 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002413 }
2414
2415 // Import the major distinguishing characteristics of this record.
2416 DeclContext *DC, *LexicalDC;
2417 DeclarationName Name;
2418 SourceLocation Loc;
2419 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2420 return 0;
2421
2422 // Figure out what structure name we're looking for.
2423 unsigned IDNS = Decl::IDNS_Tag;
2424 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002425 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2426 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002427 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002428 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002429 IDNS |= Decl::IDNS_Ordinary;
2430
2431 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002432 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002433 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002435 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2436 DC->localUncachedLookup(SearchName, FoundDecls);
2437 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2438 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002439 continue;
2440
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002441 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002442 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002443 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2444 Found = Tag->getDecl();
2445 }
2446
2447 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002448 if (D->isAnonymousStructOrUnion() &&
2449 FoundRecord->isAnonymousStructOrUnion()) {
2450 // If both anonymous structs/unions are in a record context, make sure
2451 // they occur in the same location in the context records.
2452 if (llvm::Optional<unsigned> Index1
2453 = findAnonymousStructOrUnionIndex(D)) {
2454 if (llvm::Optional<unsigned> Index2
2455 = findAnonymousStructOrUnionIndex(FoundRecord)) {
2456 if (*Index1 != *Index2)
2457 continue;
2458 }
2459 }
2460 }
2461
Douglas Gregor25791052010-02-12 00:09:27 +00002462 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002463 if ((SearchName && !D->isCompleteDefinition())
2464 || (D->isCompleteDefinition() &&
2465 D->isAnonymousStructOrUnion()
2466 == FoundDef->isAnonymousStructOrUnion() &&
2467 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002468 // The record types structurally match, or the "from" translation
2469 // unit only had a forward declaration anyway; call it the same
2470 // function.
2471 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002472 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002473 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002474 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002475 // We have a forward declaration of this type, so adopt that forward
2476 // declaration rather than building a new one.
2477 AdoptDecl = FoundRecord;
2478 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002479 } else if (!SearchName) {
2480 continue;
2481 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002482 }
2483
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002484 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002485 }
2486
Douglas Gregordd6006f2012-07-17 21:16:27 +00002487 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002488 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2489 ConflictingDecls.data(),
2490 ConflictingDecls.size());
2491 }
2492 }
2493
2494 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002495 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002496 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002497 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002498 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002499 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002500 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002501 DC, StartLoc, Loc,
2502 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002503 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002504 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002505 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002506 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002507 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002508 }
Douglas Gregor14454802011-02-25 02:25:35 +00002509
2510 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002511 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002512 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002513 if (D->isAnonymousStructOrUnion())
2514 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002515 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002516
Douglas Gregor3996e242010-02-15 22:01:00 +00002517 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002518
Douglas Gregor95d82832012-01-24 18:36:04 +00002519 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002520 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002521
Douglas Gregor3996e242010-02-15 22:01:00 +00002522 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002523}
2524
Douglas Gregor98c10182010-02-12 22:17:39 +00002525Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2526 // Import the major distinguishing characteristics of this enumerator.
2527 DeclContext *DC, *LexicalDC;
2528 DeclarationName Name;
2529 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002530 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002532
2533 QualType T = Importer.Import(D->getType());
2534 if (T.isNull())
2535 return 0;
2536
Douglas Gregor98c10182010-02-12 22:17:39 +00002537 // Determine whether there are any other declarations with the same name and
2538 // in the same context.
2539 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002540 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002541 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002542 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2543 DC->localUncachedLookup(Name, FoundDecls);
2544 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2545 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002546 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002547
2548 if (EnumConstantDecl *FoundEnumConstant
2549 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2550 if (IsStructuralMatch(D, FoundEnumConstant))
2551 return Importer.Imported(D, FoundEnumConstant);
2552 }
2553
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002554 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002555 }
2556
2557 if (!ConflictingDecls.empty()) {
2558 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2559 ConflictingDecls.data(),
2560 ConflictingDecls.size());
2561 if (!Name)
2562 return 0;
2563 }
2564 }
2565
2566 Expr *Init = Importer.Import(D->getInitExpr());
2567 if (D->getInitExpr() && !Init)
2568 return 0;
2569
2570 EnumConstantDecl *ToEnumerator
2571 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2572 Name.getAsIdentifierInfo(), T,
2573 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002574 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002575 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002576 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002577 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002578 return ToEnumerator;
2579}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002580
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002581Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2582 // Import the major distinguishing characteristics of this function.
2583 DeclContext *DC, *LexicalDC;
2584 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002585 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002586 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002587 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002588
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002589 // Try to find a function in our own ("to") context with the same name, same
2590 // type, and in the same context as the function we're importing.
2591 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002592 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002593 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002594 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2595 DC->localUncachedLookup(Name, FoundDecls);
2596 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2597 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002598 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002599
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002600 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002601 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2602 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002603 if (Importer.IsStructurallyEquivalent(D->getType(),
2604 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002605 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002606 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002607 }
2608
2609 // FIXME: Check for overloading more carefully, e.g., by boosting
2610 // Sema::IsOverload out to the AST library.
2611
2612 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002613 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002614 continue;
2615
2616 // Complain about inconsistent function types.
2617 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002618 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002619 Importer.ToDiag(FoundFunction->getLocation(),
2620 diag::note_odr_value_here)
2621 << FoundFunction->getType();
2622 }
2623 }
2624
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002625 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002626 }
2627
2628 if (!ConflictingDecls.empty()) {
2629 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2630 ConflictingDecls.data(),
2631 ConflictingDecls.size());
2632 if (!Name)
2633 return 0;
2634 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002635 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002636
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002637 DeclarationNameInfo NameInfo(Name, Loc);
2638 // Import additional name location/type info.
2639 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2640
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002641 QualType FromTy = D->getType();
2642 bool usedDifferentExceptionSpec = false;
2643
2644 if (const FunctionProtoType *
2645 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2646 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2647 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2648 // FunctionDecl that we are importing the FunctionProtoType for.
2649 // To avoid an infinite recursion when importing, create the FunctionDecl
2650 // with a simplified function type and update it afterwards.
2651 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2652 FromEPI.NoexceptExpr) {
2653 FunctionProtoType::ExtProtoInfo DefaultEPI;
2654 FromTy = Importer.getFromContext().getFunctionType(
2655 FromFPT->getResultType(),
2656 FromFPT->arg_type_begin(),
2657 FromFPT->arg_type_end() - FromFPT->arg_type_begin(),
2658 DefaultEPI);
2659 usedDifferentExceptionSpec = true;
2660 }
2661 }
2662
Douglas Gregorb4964f72010-02-15 23:54:17 +00002663 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002664 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002665 if (T.isNull())
2666 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002667
2668 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002669 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002670 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2671 P != PEnd; ++P) {
2672 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2673 if (!ToP)
2674 return 0;
2675
2676 Parameters.push_back(ToP);
2677 }
2678
2679 // Create the imported function.
2680 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002681 FunctionDecl *ToFunction = 0;
2682 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2683 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2684 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002685 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002686 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002687 FromConstructor->isExplicit(),
2688 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002689 D->isImplicit(),
2690 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002691 } else if (isa<CXXDestructorDecl>(D)) {
2692 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2693 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002694 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002695 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002696 D->isInlineSpecified(),
2697 D->isImplicit());
2698 } else if (CXXConversionDecl *FromConversion
2699 = dyn_cast<CXXConversionDecl>(D)) {
2700 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2701 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002702 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002703 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002704 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002705 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002706 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002707 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002708 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2709 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2710 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002711 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002712 NameInfo, T, TInfo,
2713 Method->isStatic(),
2714 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002715 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002716 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002717 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002718 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002719 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002720 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002721 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002722 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002723 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002724 D->hasWrittenPrototype(),
2725 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002726 }
John McCall3e11ebe2010-03-15 10:12:16 +00002727
2728 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002729 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002730 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002731 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002732 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2733 ToFunction->setTrivial(D->isTrivial());
2734 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002735 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002736
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002737 // Set the parameters.
2738 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002739 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002740 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002741 }
David Blaikie9c70e042011-09-21 18:16:56 +00002742 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002743
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002744 if (usedDifferentExceptionSpec) {
2745 // Update FunctionProtoType::ExtProtoInfo.
2746 QualType T = Importer.Import(D->getType());
2747 if (T.isNull())
2748 return 0;
2749 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002750 }
2751
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002752 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002753
2754 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002755 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002756
Douglas Gregor43f54792010-02-17 02:12:47 +00002757 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002758}
2759
Douglas Gregor00eace12010-02-21 18:29:16 +00002760Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2761 return VisitFunctionDecl(D);
2762}
2763
2764Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2765 return VisitCXXMethodDecl(D);
2766}
2767
2768Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2769 return VisitCXXMethodDecl(D);
2770}
2771
2772Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2773 return VisitCXXMethodDecl(D);
2774}
2775
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002776static unsigned getFieldIndex(Decl *F) {
2777 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2778 if (!Owner)
2779 return 0;
2780
2781 unsigned Index = 1;
2782 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
2783 DEnd = Owner->noload_decls_end();
2784 D != DEnd; ++D) {
2785 if (*D == F)
2786 return Index;
2787
2788 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2789 ++Index;
2790 }
2791
2792 return Index;
2793}
2794
Douglas Gregor5c73e912010-02-11 00:48:18 +00002795Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2796 // Import the major distinguishing characteristics of a variable.
2797 DeclContext *DC, *LexicalDC;
2798 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002799 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002800 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2801 return 0;
2802
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002803 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002804 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2805 DC->localUncachedLookup(Name, FoundDecls);
2806 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2807 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002808 // For anonymous fields, match up by index.
2809 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2810 continue;
2811
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002812 if (Importer.IsStructurallyEquivalent(D->getType(),
2813 FoundField->getType())) {
2814 Importer.Imported(D, FoundField);
2815 return FoundField;
2816 }
2817
2818 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2819 << Name << D->getType() << FoundField->getType();
2820 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2821 << FoundField->getType();
2822 return 0;
2823 }
2824 }
2825
Douglas Gregorb4964f72010-02-15 23:54:17 +00002826 // Import the type.
2827 QualType T = Importer.Import(D->getType());
2828 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002829 return 0;
2830
2831 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2832 Expr *BitWidth = Importer.Import(D->getBitWidth());
2833 if (!BitWidth && D->getBitWidth())
2834 return 0;
2835
Abramo Bagnaradff19302011-03-08 08:55:46 +00002836 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2837 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002838 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002839 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002840 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002841 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002842 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002843 if (ToField->hasInClassInitializer())
2844 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002845 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002846 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002847 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002848 return ToField;
2849}
2850
Francois Pichet783dd6e2010-11-21 06:08:52 +00002851Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2852 // Import the major distinguishing characteristics of a variable.
2853 DeclContext *DC, *LexicalDC;
2854 DeclarationName Name;
2855 SourceLocation Loc;
2856 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2857 return 0;
2858
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002859 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002860 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2861 DC->localUncachedLookup(Name, FoundDecls);
2862 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002863 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002864 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002865 // For anonymous indirect fields, match up by index.
2866 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2867 continue;
2868
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002869 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002870 FoundField->getType(),
2871 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002872 Importer.Imported(D, FoundField);
2873 return FoundField;
2874 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002875
2876 // If there are more anonymous fields to check, continue.
2877 if (!Name && I < N-1)
2878 continue;
2879
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002880 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2881 << Name << D->getType() << FoundField->getType();
2882 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2883 << FoundField->getType();
2884 return 0;
2885 }
2886 }
2887
Francois Pichet783dd6e2010-11-21 06:08:52 +00002888 // Import the type.
2889 QualType T = Importer.Import(D->getType());
2890 if (T.isNull())
2891 return 0;
2892
2893 NamedDecl **NamedChain =
2894 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2895
2896 unsigned i = 0;
2897 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2898 PE = D->chain_end(); PI != PE; ++PI) {
2899 Decl* D = Importer.Import(*PI);
2900 if (!D)
2901 return 0;
2902 NamedChain[i++] = cast<NamedDecl>(D);
2903 }
2904
2905 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2906 Importer.getToContext(), DC,
2907 Loc, Name.getAsIdentifierInfo(), T,
2908 NamedChain, D->getChainingSize());
2909 ToIndirectField->setAccess(D->getAccess());
2910 ToIndirectField->setLexicalDeclContext(LexicalDC);
2911 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002912 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002913 return ToIndirectField;
2914}
2915
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002916Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2917 // Import the major distinguishing characteristics of an ivar.
2918 DeclContext *DC, *LexicalDC;
2919 DeclarationName Name;
2920 SourceLocation Loc;
2921 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2922 return 0;
2923
2924 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002925 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2926 DC->localUncachedLookup(Name, FoundDecls);
2927 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2928 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002929 if (Importer.IsStructurallyEquivalent(D->getType(),
2930 FoundIvar->getType())) {
2931 Importer.Imported(D, FoundIvar);
2932 return FoundIvar;
2933 }
2934
2935 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2936 << Name << D->getType() << FoundIvar->getType();
2937 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2938 << FoundIvar->getType();
2939 return 0;
2940 }
2941 }
2942
2943 // Import the type.
2944 QualType T = Importer.Import(D->getType());
2945 if (T.isNull())
2946 return 0;
2947
2948 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2949 Expr *BitWidth = Importer.Import(D->getBitWidth());
2950 if (!BitWidth && D->getBitWidth())
2951 return 0;
2952
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002953 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2954 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002955 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002956 Loc, Name.getAsIdentifierInfo(),
2957 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002958 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002959 ToIvar->setLexicalDeclContext(LexicalDC);
2960 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002961 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002962 return ToIvar;
2963
2964}
2965
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002966Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2967 // Import the major distinguishing characteristics of a variable.
2968 DeclContext *DC, *LexicalDC;
2969 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002970 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002971 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002972 return 0;
2973
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002974 // Try to find a variable in our own ("to") context with the same name and
2975 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002976 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002977 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002978 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002979 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002980 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2981 DC->localUncachedLookup(Name, FoundDecls);
2982 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2983 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002984 continue;
2985
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002986 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002987 // We have found a variable that we may need to merge with. Check it.
2988 if (isExternalLinkage(FoundVar->getLinkage()) &&
2989 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002990 if (Importer.IsStructurallyEquivalent(D->getType(),
2991 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002992 MergeWithVar = FoundVar;
2993 break;
2994 }
2995
Douglas Gregor56521c52010-02-12 17:23:39 +00002996 const ArrayType *FoundArray
2997 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2998 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002999 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003000 if (FoundArray && TArray) {
3001 if (isa<IncompleteArrayType>(FoundArray) &&
3002 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003003 // Import the type.
3004 QualType T = Importer.Import(D->getType());
3005 if (T.isNull())
3006 return 0;
3007
Douglas Gregor56521c52010-02-12 17:23:39 +00003008 FoundVar->setType(T);
3009 MergeWithVar = FoundVar;
3010 break;
3011 } else if (isa<IncompleteArrayType>(TArray) &&
3012 isa<ConstantArrayType>(FoundArray)) {
3013 MergeWithVar = FoundVar;
3014 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003015 }
3016 }
3017
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003018 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003019 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003020 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3021 << FoundVar->getType();
3022 }
3023 }
3024
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003025 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003026 }
3027
3028 if (MergeWithVar) {
3029 // An equivalent variable with external linkage has been found. Link
3030 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003031 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003032
3033 if (VarDecl *DDef = D->getDefinition()) {
3034 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3035 Importer.ToDiag(ExistingDef->getLocation(),
3036 diag::err_odr_variable_multiple_def)
3037 << Name;
3038 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3039 } else {
3040 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003041 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003042 if (DDef->isInitKnownICE()) {
3043 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3044 Eval->CheckedICE = true;
3045 Eval->IsICE = DDef->isInitICE();
3046 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003047 }
3048 }
3049
3050 return MergeWithVar;
3051 }
3052
3053 if (!ConflictingDecls.empty()) {
3054 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3055 ConflictingDecls.data(),
3056 ConflictingDecls.size());
3057 if (!Name)
3058 return 0;
3059 }
3060 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003061
Douglas Gregorb4964f72010-02-15 23:54:17 +00003062 // Import the type.
3063 QualType T = Importer.Import(D->getType());
3064 if (T.isNull())
3065 return 0;
3066
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003067 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003068 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003069 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3070 Importer.Import(D->getInnerLocStart()),
3071 Loc, Name.getAsIdentifierInfo(),
3072 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00003073 D->getStorageClass(),
3074 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00003075 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003076 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003077 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003078 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003079 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003080
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003081 // Merge the initializer.
3082 // FIXME: Can we really import any initializer? Alternatively, we could force
3083 // ourselves to import every declaration of a variable and then only use
3084 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00003085 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003086
3087 // FIXME: Other bits to merge?
3088
3089 return ToVar;
3090}
3091
Douglas Gregor8b228d72010-02-17 21:22:52 +00003092Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3093 // Parameters are created in the translation unit's context, then moved
3094 // into the function declaration's context afterward.
3095 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3096
3097 // Import the name of this declaration.
3098 DeclarationName Name = Importer.Import(D->getDeclName());
3099 if (D->getDeclName() && !Name)
3100 return 0;
3101
3102 // Import the location of this declaration.
3103 SourceLocation Loc = Importer.Import(D->getLocation());
3104
3105 // Import the parameter's type.
3106 QualType T = Importer.Import(D->getType());
3107 if (T.isNull())
3108 return 0;
3109
3110 // Create the imported parameter.
3111 ImplicitParamDecl *ToParm
3112 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3113 Loc, Name.getAsIdentifierInfo(),
3114 T);
3115 return Importer.Imported(D, ToParm);
3116}
3117
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003118Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3119 // Parameters are created in the translation unit's context, then moved
3120 // into the function declaration's context afterward.
3121 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3122
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003123 // Import the name of this declaration.
3124 DeclarationName Name = Importer.Import(D->getDeclName());
3125 if (D->getDeclName() && !Name)
3126 return 0;
3127
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003128 // Import the location of this declaration.
3129 SourceLocation Loc = Importer.Import(D->getLocation());
3130
3131 // Import the parameter's type.
3132 QualType T = Importer.Import(D->getType());
3133 if (T.isNull())
3134 return 0;
3135
3136 // Create the imported parameter.
3137 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3138 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003139 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003140 Loc, Name.getAsIdentifierInfo(),
3141 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003142 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003143 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00003144 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003145 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003146}
3147
Douglas Gregor43f54792010-02-17 02:12:47 +00003148Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3149 // Import the major distinguishing characteristics of a method.
3150 DeclContext *DC, *LexicalDC;
3151 DeclarationName Name;
3152 SourceLocation Loc;
3153 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3154 return 0;
3155
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003156 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3157 DC->localUncachedLookup(Name, FoundDecls);
3158 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3159 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003160 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3161 continue;
3162
3163 // Check return types.
3164 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3165 FoundMethod->getResultType())) {
3166 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3167 << D->isInstanceMethod() << Name
3168 << D->getResultType() << FoundMethod->getResultType();
3169 Importer.ToDiag(FoundMethod->getLocation(),
3170 diag::note_odr_objc_method_here)
3171 << D->isInstanceMethod() << Name;
3172 return 0;
3173 }
3174
3175 // Check the number of parameters.
3176 if (D->param_size() != FoundMethod->param_size()) {
3177 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3178 << D->isInstanceMethod() << Name
3179 << D->param_size() << FoundMethod->param_size();
3180 Importer.ToDiag(FoundMethod->getLocation(),
3181 diag::note_odr_objc_method_here)
3182 << D->isInstanceMethod() << Name;
3183 return 0;
3184 }
3185
3186 // Check parameter types.
3187 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3188 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3189 P != PEnd; ++P, ++FoundP) {
3190 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3191 (*FoundP)->getType())) {
3192 Importer.FromDiag((*P)->getLocation(),
3193 diag::err_odr_objc_method_param_type_inconsistent)
3194 << D->isInstanceMethod() << Name
3195 << (*P)->getType() << (*FoundP)->getType();
3196 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3197 << (*FoundP)->getType();
3198 return 0;
3199 }
3200 }
3201
3202 // Check variadic/non-variadic.
3203 // Check the number of parameters.
3204 if (D->isVariadic() != FoundMethod->isVariadic()) {
3205 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3206 << D->isInstanceMethod() << Name;
3207 Importer.ToDiag(FoundMethod->getLocation(),
3208 diag::note_odr_objc_method_here)
3209 << D->isInstanceMethod() << Name;
3210 return 0;
3211 }
3212
3213 // FIXME: Any other bits we need to merge?
3214 return Importer.Imported(D, FoundMethod);
3215 }
3216 }
3217
3218 // Import the result type.
3219 QualType ResultTy = Importer.Import(D->getResultType());
3220 if (ResultTy.isNull())
3221 return 0;
3222
Douglas Gregor12852d92010-03-08 14:59:44 +00003223 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3224
Douglas Gregor43f54792010-02-17 02:12:47 +00003225 ObjCMethodDecl *ToMethod
3226 = ObjCMethodDecl::Create(Importer.getToContext(),
3227 Loc,
3228 Importer.Import(D->getLocEnd()),
3229 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003230 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003231 D->isInstanceMethod(),
3232 D->isVariadic(),
Jordan Rosed01e83a2012-10-10 16:42:25 +00003233 D->isPropertyAccessor(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003234 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003235 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003236 D->getImplementationControl(),
3237 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003238
3239 // FIXME: When we decide to merge method definitions, we'll need to
3240 // deal with implicit parameters.
3241
3242 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003243 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003244 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3245 FromPEnd = D->param_end();
3246 FromP != FromPEnd;
3247 ++FromP) {
3248 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3249 if (!ToP)
3250 return 0;
3251
3252 ToParams.push_back(ToP);
3253 }
3254
3255 // Set the parameters.
3256 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3257 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003258 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003259 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003260 SmallVector<SourceLocation, 12> SelLocs;
3261 D->getSelectorLocs(SelLocs);
3262 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003263
3264 ToMethod->setLexicalDeclContext(LexicalDC);
3265 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003266 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003267 return ToMethod;
3268}
3269
Douglas Gregor84c51c32010-02-18 01:47:50 +00003270Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3271 // Import the major distinguishing characteristics of a category.
3272 DeclContext *DC, *LexicalDC;
3273 DeclarationName Name;
3274 SourceLocation Loc;
3275 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3276 return 0;
3277
3278 ObjCInterfaceDecl *ToInterface
3279 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3280 if (!ToInterface)
3281 return 0;
3282
3283 // Determine if we've already encountered this category.
3284 ObjCCategoryDecl *MergeWithCategory
3285 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3286 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3287 if (!ToCategory) {
3288 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003289 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003290 Loc,
3291 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003292 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003293 ToInterface,
3294 Importer.Import(D->getIvarLBraceLoc()),
3295 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003296 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003297 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003298 Importer.Imported(D, ToCategory);
3299
Douglas Gregor84c51c32010-02-18 01:47:50 +00003300 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003301 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3302 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003303 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3304 = D->protocol_loc_begin();
3305 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3306 FromProtoEnd = D->protocol_end();
3307 FromProto != FromProtoEnd;
3308 ++FromProto, ++FromProtoLoc) {
3309 ObjCProtocolDecl *ToProto
3310 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3311 if (!ToProto)
3312 return 0;
3313 Protocols.push_back(ToProto);
3314 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3315 }
3316
3317 // FIXME: If we're merging, make sure that the protocol list is the same.
3318 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3319 ProtocolLocs.data(), Importer.getToContext());
3320
3321 } else {
3322 Importer.Imported(D, ToCategory);
3323 }
3324
3325 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003326 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003327
3328 // If we have an implementation, import it as well.
3329 if (D->getImplementation()) {
3330 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003331 = cast_or_null<ObjCCategoryImplDecl>(
3332 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003333 if (!Impl)
3334 return 0;
3335
3336 ToCategory->setImplementation(Impl);
3337 }
3338
3339 return ToCategory;
3340}
3341
Douglas Gregor2aa53772012-01-24 17:42:07 +00003342bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3343 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003344 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003345 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003346 if (shouldForceImportDeclContext(Kind))
3347 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003348 return false;
3349 }
3350
3351 // Start the protocol definition
3352 To->startDefinition();
3353
3354 // Import protocols
3355 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3356 SmallVector<SourceLocation, 4> ProtocolLocs;
3357 ObjCProtocolDecl::protocol_loc_iterator
3358 FromProtoLoc = From->protocol_loc_begin();
3359 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3360 FromProtoEnd = From->protocol_end();
3361 FromProto != FromProtoEnd;
3362 ++FromProto, ++FromProtoLoc) {
3363 ObjCProtocolDecl *ToProto
3364 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3365 if (!ToProto)
3366 return true;
3367 Protocols.push_back(ToProto);
3368 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3369 }
3370
3371 // FIXME: If we're merging, make sure that the protocol list is the same.
3372 To->setProtocolList(Protocols.data(), Protocols.size(),
3373 ProtocolLocs.data(), Importer.getToContext());
3374
Douglas Gregor2e15c842012-02-01 21:00:38 +00003375 if (shouldForceImportDeclContext(Kind)) {
3376 // Import all of the members of this protocol.
3377 ImportDeclContext(From, /*ForceImport=*/true);
3378 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003379 return false;
3380}
3381
Douglas Gregor98d156a2010-02-17 16:12:00 +00003382Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003383 // If this protocol has a definition in the translation unit we're coming
3384 // from, but this particular declaration is not that definition, import the
3385 // definition and map to that.
3386 ObjCProtocolDecl *Definition = D->getDefinition();
3387 if (Definition && Definition != D) {
3388 Decl *ImportedDef = Importer.Import(Definition);
3389 if (!ImportedDef)
3390 return 0;
3391
3392 return Importer.Imported(D, ImportedDef);
3393 }
3394
Douglas Gregor84c51c32010-02-18 01:47:50 +00003395 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003396 DeclContext *DC, *LexicalDC;
3397 DeclarationName Name;
3398 SourceLocation Loc;
3399 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3400 return 0;
3401
3402 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003403 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3404 DC->localUncachedLookup(Name, FoundDecls);
3405 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3406 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003407 continue;
3408
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003409 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003410 break;
3411 }
3412
3413 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003414 if (!ToProto) {
3415 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3416 Name.getAsIdentifierInfo(), Loc,
3417 Importer.Import(D->getAtStartLoc()),
3418 /*PrevDecl=*/0);
3419 ToProto->setLexicalDeclContext(LexicalDC);
3420 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003421 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003422
3423 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003424
Douglas Gregor2aa53772012-01-24 17:42:07 +00003425 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3426 return 0;
3427
Douglas Gregor98d156a2010-02-17 16:12:00 +00003428 return ToProto;
3429}
3430
Douglas Gregor2aa53772012-01-24 17:42:07 +00003431bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3432 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003433 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003434 if (To->getDefinition()) {
3435 // Check consistency of superclass.
3436 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3437 if (FromSuper) {
3438 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3439 if (!FromSuper)
3440 return true;
3441 }
3442
3443 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3444 if ((bool)FromSuper != (bool)ToSuper ||
3445 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3446 Importer.ToDiag(To->getLocation(),
3447 diag::err_odr_objc_superclass_inconsistent)
3448 << To->getDeclName();
3449 if (ToSuper)
3450 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3451 << To->getSuperClass()->getDeclName();
3452 else
3453 Importer.ToDiag(To->getLocation(),
3454 diag::note_odr_objc_missing_superclass);
3455 if (From->getSuperClass())
3456 Importer.FromDiag(From->getSuperClassLoc(),
3457 diag::note_odr_objc_superclass)
3458 << From->getSuperClass()->getDeclName();
3459 else
3460 Importer.FromDiag(From->getLocation(),
3461 diag::note_odr_objc_missing_superclass);
3462 }
3463
Douglas Gregor2e15c842012-02-01 21:00:38 +00003464 if (shouldForceImportDeclContext(Kind))
3465 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003466 return false;
3467 }
3468
3469 // Start the definition.
3470 To->startDefinition();
3471
3472 // If this class has a superclass, import it.
3473 if (From->getSuperClass()) {
3474 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3475 Importer.Import(From->getSuperClass()));
3476 if (!Super)
3477 return true;
3478
3479 To->setSuperClass(Super);
3480 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3481 }
3482
3483 // Import protocols
3484 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3485 SmallVector<SourceLocation, 4> ProtocolLocs;
3486 ObjCInterfaceDecl::protocol_loc_iterator
3487 FromProtoLoc = From->protocol_loc_begin();
3488
3489 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3490 FromProtoEnd = From->protocol_end();
3491 FromProto != FromProtoEnd;
3492 ++FromProto, ++FromProtoLoc) {
3493 ObjCProtocolDecl *ToProto
3494 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3495 if (!ToProto)
3496 return true;
3497 Protocols.push_back(ToProto);
3498 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3499 }
3500
3501 // FIXME: If we're merging, make sure that the protocol list is the same.
3502 To->setProtocolList(Protocols.data(), Protocols.size(),
3503 ProtocolLocs.data(), Importer.getToContext());
3504
3505 // Import categories. When the categories themselves are imported, they'll
3506 // hook themselves into this interface.
3507 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3508 FromCat = FromCat->getNextClassCategory())
3509 Importer.Import(FromCat);
3510
3511 // If we have an @implementation, import it as well.
3512 if (From->getImplementation()) {
3513 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3514 Importer.Import(From->getImplementation()));
3515 if (!Impl)
3516 return true;
3517
3518 To->setImplementation(Impl);
3519 }
3520
Douglas Gregor2e15c842012-02-01 21:00:38 +00003521 if (shouldForceImportDeclContext(Kind)) {
3522 // Import all of the members of this class.
3523 ImportDeclContext(From, /*ForceImport=*/true);
3524 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003525 return false;
3526}
3527
Douglas Gregor45635322010-02-16 01:20:57 +00003528Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003529 // If this class has a definition in the translation unit we're coming from,
3530 // but this particular declaration is not that definition, import the
3531 // definition and map to that.
3532 ObjCInterfaceDecl *Definition = D->getDefinition();
3533 if (Definition && Definition != D) {
3534 Decl *ImportedDef = Importer.Import(Definition);
3535 if (!ImportedDef)
3536 return 0;
3537
3538 return Importer.Imported(D, ImportedDef);
3539 }
3540
Douglas Gregor45635322010-02-16 01:20:57 +00003541 // Import the major distinguishing characteristics of an @interface.
3542 DeclContext *DC, *LexicalDC;
3543 DeclarationName Name;
3544 SourceLocation Loc;
3545 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3546 return 0;
3547
Douglas Gregor2aa53772012-01-24 17:42:07 +00003548 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003549 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003550 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3551 DC->localUncachedLookup(Name, FoundDecls);
3552 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3553 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003554 continue;
3555
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003556 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003557 break;
3558 }
3559
Douglas Gregor2aa53772012-01-24 17:42:07 +00003560 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003561 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003562 if (!ToIface) {
3563 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3564 Importer.Import(D->getAtStartLoc()),
3565 Name.getAsIdentifierInfo(),
3566 /*PrevDecl=*/0,Loc,
3567 D->isImplicitInterfaceDecl());
3568 ToIface->setLexicalDeclContext(LexicalDC);
3569 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003570 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003571 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003572
Douglas Gregor2aa53772012-01-24 17:42:07 +00003573 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3574 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003575
Douglas Gregor98d156a2010-02-17 16:12:00 +00003576 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003577}
3578
Douglas Gregor4da9d682010-12-07 15:32:12 +00003579Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3580 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3581 Importer.Import(D->getCategoryDecl()));
3582 if (!Category)
3583 return 0;
3584
3585 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3586 if (!ToImpl) {
3587 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3588 if (!DC)
3589 return 0;
3590
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003591 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003592 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003593 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003594 Category->getClassInterface(),
3595 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003596 Importer.Import(D->getAtStartLoc()),
3597 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003598
3599 DeclContext *LexicalDC = DC;
3600 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3601 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3602 if (!LexicalDC)
3603 return 0;
3604
3605 ToImpl->setLexicalDeclContext(LexicalDC);
3606 }
3607
Sean Callanan95e74be2011-10-21 02:57:43 +00003608 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003609 Category->setImplementation(ToImpl);
3610 }
3611
3612 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003613 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003614 return ToImpl;
3615}
3616
Douglas Gregorda8025c2010-12-07 01:26:03 +00003617Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3618 // Find the corresponding interface.
3619 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3620 Importer.Import(D->getClassInterface()));
3621 if (!Iface)
3622 return 0;
3623
3624 // Import the superclass, if any.
3625 ObjCInterfaceDecl *Super = 0;
3626 if (D->getSuperClass()) {
3627 Super = cast_or_null<ObjCInterfaceDecl>(
3628 Importer.Import(D->getSuperClass()));
3629 if (!Super)
3630 return 0;
3631 }
3632
3633 ObjCImplementationDecl *Impl = Iface->getImplementation();
3634 if (!Impl) {
3635 // We haven't imported an implementation yet. Create a new @implementation
3636 // now.
3637 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3638 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003639 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003640 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003641 Importer.Import(D->getAtStartLoc()),
3642 Importer.Import(D->getIvarLBraceLoc()),
3643 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003644
3645 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3646 DeclContext *LexicalDC
3647 = Importer.ImportContext(D->getLexicalDeclContext());
3648 if (!LexicalDC)
3649 return 0;
3650 Impl->setLexicalDeclContext(LexicalDC);
3651 }
3652
3653 // Associate the implementation with the class it implements.
3654 Iface->setImplementation(Impl);
3655 Importer.Imported(D, Iface->getImplementation());
3656 } else {
3657 Importer.Imported(D, Iface->getImplementation());
3658
3659 // Verify that the existing @implementation has the same superclass.
3660 if ((Super && !Impl->getSuperClass()) ||
3661 (!Super && Impl->getSuperClass()) ||
3662 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003663 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003664 Importer.ToDiag(Impl->getLocation(),
3665 diag::err_odr_objc_superclass_inconsistent)
3666 << Iface->getDeclName();
3667 // FIXME: It would be nice to have the location of the superclass
3668 // below.
3669 if (Impl->getSuperClass())
3670 Importer.ToDiag(Impl->getLocation(),
3671 diag::note_odr_objc_superclass)
3672 << Impl->getSuperClass()->getDeclName();
3673 else
3674 Importer.ToDiag(Impl->getLocation(),
3675 diag::note_odr_objc_missing_superclass);
3676 if (D->getSuperClass())
3677 Importer.FromDiag(D->getLocation(),
3678 diag::note_odr_objc_superclass)
3679 << D->getSuperClass()->getDeclName();
3680 else
3681 Importer.FromDiag(D->getLocation(),
3682 diag::note_odr_objc_missing_superclass);
3683 return 0;
3684 }
3685 }
3686
3687 // Import all of the members of this @implementation.
3688 ImportDeclContext(D);
3689
3690 return Impl;
3691}
3692
Douglas Gregora11c4582010-02-17 18:02:10 +00003693Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3694 // Import the major distinguishing characteristics of an @property.
3695 DeclContext *DC, *LexicalDC;
3696 DeclarationName Name;
3697 SourceLocation Loc;
3698 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3699 return 0;
3700
3701 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003702 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3703 DC->localUncachedLookup(Name, FoundDecls);
3704 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003705 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003706 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003707 // Check property types.
3708 if (!Importer.IsStructurallyEquivalent(D->getType(),
3709 FoundProp->getType())) {
3710 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3711 << Name << D->getType() << FoundProp->getType();
3712 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3713 << FoundProp->getType();
3714 return 0;
3715 }
3716
3717 // FIXME: Check property attributes, getters, setters, etc.?
3718
3719 // Consider these properties to be equivalent.
3720 Importer.Imported(D, FoundProp);
3721 return FoundProp;
3722 }
3723 }
3724
3725 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003726 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3727 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003728 return 0;
3729
3730 // Create the new property.
3731 ObjCPropertyDecl *ToProperty
3732 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3733 Name.getAsIdentifierInfo(),
3734 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003735 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003736 T,
3737 D->getPropertyImplementation());
3738 Importer.Imported(D, ToProperty);
3739 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003740 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003741
3742 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003743 ToProperty->setPropertyAttributesAsWritten(
3744 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003745 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3746 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3747 ToProperty->setGetterMethodDecl(
3748 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3749 ToProperty->setSetterMethodDecl(
3750 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3751 ToProperty->setPropertyIvarDecl(
3752 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3753 return ToProperty;
3754}
3755
Douglas Gregor14a49e22010-12-07 18:32:03 +00003756Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3757 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3758 Importer.Import(D->getPropertyDecl()));
3759 if (!Property)
3760 return 0;
3761
3762 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3763 if (!DC)
3764 return 0;
3765
3766 // Import the lexical declaration context.
3767 DeclContext *LexicalDC = DC;
3768 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3769 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3770 if (!LexicalDC)
3771 return 0;
3772 }
3773
3774 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3775 if (!InImpl)
3776 return 0;
3777
3778 // Import the ivar (for an @synthesize).
3779 ObjCIvarDecl *Ivar = 0;
3780 if (D->getPropertyIvarDecl()) {
3781 Ivar = cast_or_null<ObjCIvarDecl>(
3782 Importer.Import(D->getPropertyIvarDecl()));
3783 if (!Ivar)
3784 return 0;
3785 }
3786
3787 ObjCPropertyImplDecl *ToImpl
3788 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3789 if (!ToImpl) {
3790 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3791 Importer.Import(D->getLocStart()),
3792 Importer.Import(D->getLocation()),
3793 Property,
3794 D->getPropertyImplementation(),
3795 Ivar,
3796 Importer.Import(D->getPropertyIvarDeclLoc()));
3797 ToImpl->setLexicalDeclContext(LexicalDC);
3798 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003799 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003800 } else {
3801 // Check that we have the same kind of property implementation (@synthesize
3802 // vs. @dynamic).
3803 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3804 Importer.ToDiag(ToImpl->getLocation(),
3805 diag::err_odr_objc_property_impl_kind_inconsistent)
3806 << Property->getDeclName()
3807 << (ToImpl->getPropertyImplementation()
3808 == ObjCPropertyImplDecl::Dynamic);
3809 Importer.FromDiag(D->getLocation(),
3810 diag::note_odr_objc_property_impl_kind)
3811 << D->getPropertyDecl()->getDeclName()
3812 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3813 return 0;
3814 }
3815
3816 // For @synthesize, check that we have the same
3817 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3818 Ivar != ToImpl->getPropertyIvarDecl()) {
3819 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3820 diag::err_odr_objc_synthesize_ivar_inconsistent)
3821 << Property->getDeclName()
3822 << ToImpl->getPropertyIvarDecl()->getDeclName()
3823 << Ivar->getDeclName();
3824 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3825 diag::note_odr_objc_synthesize_ivar_here)
3826 << D->getPropertyIvarDecl()->getDeclName();
3827 return 0;
3828 }
3829
3830 // Merge the existing implementation with the new implementation.
3831 Importer.Imported(D, ToImpl);
3832 }
3833
3834 return ToImpl;
3835}
3836
Douglas Gregora082a492010-11-30 19:14:50 +00003837Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3838 // For template arguments, we adopt the translation unit as our declaration
3839 // context. This context will be fixed when the actual template declaration
3840 // is created.
3841
3842 // FIXME: Import default argument.
3843 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3844 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003845 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003846 Importer.Import(D->getLocation()),
3847 D->getDepth(),
3848 D->getIndex(),
3849 Importer.Import(D->getIdentifier()),
3850 D->wasDeclaredWithTypename(),
3851 D->isParameterPack());
3852}
3853
3854Decl *
3855ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3856 // Import the name of this declaration.
3857 DeclarationName Name = Importer.Import(D->getDeclName());
3858 if (D->getDeclName() && !Name)
3859 return 0;
3860
3861 // Import the location of this declaration.
3862 SourceLocation Loc = Importer.Import(D->getLocation());
3863
3864 // Import the type of this declaration.
3865 QualType T = Importer.Import(D->getType());
3866 if (T.isNull())
3867 return 0;
3868
3869 // Import type-source information.
3870 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3871 if (D->getTypeSourceInfo() && !TInfo)
3872 return 0;
3873
3874 // FIXME: Import default argument.
3875
3876 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3877 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003878 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003879 Loc, D->getDepth(), D->getPosition(),
3880 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003881 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003882}
3883
3884Decl *
3885ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3886 // Import the name of this declaration.
3887 DeclarationName Name = Importer.Import(D->getDeclName());
3888 if (D->getDeclName() && !Name)
3889 return 0;
3890
3891 // Import the location of this declaration.
3892 SourceLocation Loc = Importer.Import(D->getLocation());
3893
3894 // Import template parameters.
3895 TemplateParameterList *TemplateParams
3896 = ImportTemplateParameterList(D->getTemplateParameters());
3897 if (!TemplateParams)
3898 return 0;
3899
3900 // FIXME: Import default argument.
3901
3902 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3903 Importer.getToContext().getTranslationUnitDecl(),
3904 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003905 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003906 Name.getAsIdentifierInfo(),
3907 TemplateParams);
3908}
3909
3910Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3911 // If this record has a definition in the translation unit we're coming from,
3912 // but this particular declaration is not that definition, import the
3913 // definition and map to that.
3914 CXXRecordDecl *Definition
3915 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3916 if (Definition && Definition != D->getTemplatedDecl()) {
3917 Decl *ImportedDef
3918 = Importer.Import(Definition->getDescribedClassTemplate());
3919 if (!ImportedDef)
3920 return 0;
3921
3922 return Importer.Imported(D, ImportedDef);
3923 }
3924
3925 // Import the major distinguishing characteristics of this class template.
3926 DeclContext *DC, *LexicalDC;
3927 DeclarationName Name;
3928 SourceLocation Loc;
3929 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3930 return 0;
3931
3932 // We may already have a template of the same name; try to find and match it.
3933 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003934 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003935 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3936 DC->localUncachedLookup(Name, FoundDecls);
3937 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3938 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003939 continue;
3940
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003941 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003942 if (ClassTemplateDecl *FoundTemplate
3943 = dyn_cast<ClassTemplateDecl>(Found)) {
3944 if (IsStructuralMatch(D, FoundTemplate)) {
3945 // The class templates structurally match; call it the same template.
3946 // FIXME: We may be filling in a forward declaration here. Handle
3947 // this case!
3948 Importer.Imported(D->getTemplatedDecl(),
3949 FoundTemplate->getTemplatedDecl());
3950 return Importer.Imported(D, FoundTemplate);
3951 }
3952 }
3953
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003954 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003955 }
3956
3957 if (!ConflictingDecls.empty()) {
3958 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3959 ConflictingDecls.data(),
3960 ConflictingDecls.size());
3961 }
3962
3963 if (!Name)
3964 return 0;
3965 }
3966
3967 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3968
3969 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003970 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3971 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003972 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3973 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003974 DC, StartLoc, IdLoc,
3975 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003976 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003977 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003978 D2Templated->setLexicalDeclContext(LexicalDC);
3979
3980 // Create the class template declaration itself.
3981 TemplateParameterList *TemplateParams
3982 = ImportTemplateParameterList(D->getTemplateParameters());
3983 if (!TemplateParams)
3984 return 0;
3985
3986 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3987 Loc, Name, TemplateParams,
3988 D2Templated,
3989 /*PrevDecl=*/0);
3990 D2Templated->setDescribedClassTemplate(D2);
3991
3992 D2->setAccess(D->getAccess());
3993 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003994 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003995
3996 // Note the relationship between the class templates.
3997 Importer.Imported(D, D2);
3998 Importer.Imported(DTemplated, D2Templated);
3999
John McCallf937c022011-10-07 06:10:15 +00004000 if (DTemplated->isCompleteDefinition() &&
4001 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004002 // FIXME: Import definition!
4003 }
4004
4005 return D2;
4006}
4007
Douglas Gregore2e50d332010-12-01 01:36:18 +00004008Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4009 ClassTemplateSpecializationDecl *D) {
4010 // If this record has a definition in the translation unit we're coming from,
4011 // but this particular declaration is not that definition, import the
4012 // definition and map to that.
4013 TagDecl *Definition = D->getDefinition();
4014 if (Definition && Definition != D) {
4015 Decl *ImportedDef = Importer.Import(Definition);
4016 if (!ImportedDef)
4017 return 0;
4018
4019 return Importer.Imported(D, ImportedDef);
4020 }
4021
4022 ClassTemplateDecl *ClassTemplate
4023 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4024 D->getSpecializedTemplate()));
4025 if (!ClassTemplate)
4026 return 0;
4027
4028 // Import the context of this declaration.
4029 DeclContext *DC = ClassTemplate->getDeclContext();
4030 if (!DC)
4031 return 0;
4032
4033 DeclContext *LexicalDC = DC;
4034 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4035 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4036 if (!LexicalDC)
4037 return 0;
4038 }
4039
4040 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004041 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4042 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004043
4044 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004045 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004046 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4047 D->getTemplateArgs().size(),
4048 TemplateArgs))
4049 return 0;
4050
4051 // Try to find an existing specialization with these template arguments.
4052 void *InsertPos = 0;
4053 ClassTemplateSpecializationDecl *D2
4054 = ClassTemplate->findSpecialization(TemplateArgs.data(),
4055 TemplateArgs.size(), InsertPos);
4056 if (D2) {
4057 // We already have a class template specialization with these template
4058 // arguments.
4059
4060 // FIXME: Check for specialization vs. instantiation errors.
4061
4062 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004063 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004064 // The record types structurally match, or the "from" translation
4065 // unit only had a forward declaration anyway; call it the same
4066 // function.
4067 return Importer.Imported(D, FoundDef);
4068 }
4069 }
4070 } else {
4071 // Create a new specialization.
4072 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4073 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004074 StartLoc, IdLoc,
4075 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004076 TemplateArgs.data(),
4077 TemplateArgs.size(),
4078 /*PrevDecl=*/0);
4079 D2->setSpecializationKind(D->getSpecializationKind());
4080
4081 // Add this specialization to the class template.
4082 ClassTemplate->AddSpecialization(D2, InsertPos);
4083
4084 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004085 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004086
4087 // Add the specialization to this context.
4088 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004089 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004090 }
4091 Importer.Imported(D, D2);
4092
John McCallf937c022011-10-07 06:10:15 +00004093 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00004094 return 0;
4095
4096 return D2;
4097}
4098
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004099//----------------------------------------------------------------------------
4100// Import Statements
4101//----------------------------------------------------------------------------
4102
4103Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4104 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4105 << S->getStmtClassName();
4106 return 0;
4107}
4108
4109//----------------------------------------------------------------------------
4110// Import Expressions
4111//----------------------------------------------------------------------------
4112Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4113 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4114 << E->getStmtClassName();
4115 return 0;
4116}
4117
Douglas Gregor52f820e2010-02-19 01:17:02 +00004118Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004119 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4120 if (!ToD)
4121 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004122
4123 NamedDecl *FoundD = 0;
4124 if (E->getDecl() != E->getFoundDecl()) {
4125 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4126 if (!FoundD)
4127 return 0;
4128 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004129
4130 QualType T = Importer.Import(E->getType());
4131 if (T.isNull())
4132 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004133
4134 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4135 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004136 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004137 ToD,
John McCall113bee02012-03-10 09:33:50 +00004138 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004139 Importer.Import(E->getLocation()),
4140 T, E->getValueKind(),
4141 FoundD,
4142 /*FIXME:TemplateArgs=*/0);
4143 if (E->hadMultipleCandidates())
4144 DRE->setHadMultipleCandidates(true);
4145 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004146}
4147
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004148Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4149 QualType T = Importer.Import(E->getType());
4150 if (T.isNull())
4151 return 0;
4152
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004153 return IntegerLiteral::Create(Importer.getToContext(),
4154 E->getValue(), T,
4155 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004156}
4157
Douglas Gregor623421d2010-02-18 02:21:22 +00004158Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4159 QualType T = Importer.Import(E->getType());
4160 if (T.isNull())
4161 return 0;
4162
Douglas Gregorfb65e592011-07-27 05:40:30 +00004163 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4164 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004165 Importer.Import(E->getLocation()));
4166}
4167
Douglas Gregorc74247e2010-02-19 01:07:06 +00004168Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4169 Expr *SubExpr = Importer.Import(E->getSubExpr());
4170 if (!SubExpr)
4171 return 0;
4172
4173 return new (Importer.getToContext())
4174 ParenExpr(Importer.Import(E->getLParen()),
4175 Importer.Import(E->getRParen()),
4176 SubExpr);
4177}
4178
4179Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4180 QualType T = Importer.Import(E->getType());
4181 if (T.isNull())
4182 return 0;
4183
4184 Expr *SubExpr = Importer.Import(E->getSubExpr());
4185 if (!SubExpr)
4186 return 0;
4187
4188 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004189 T, E->getValueKind(),
4190 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004191 Importer.Import(E->getOperatorLoc()));
4192}
4193
Peter Collingbournee190dee2011-03-11 19:24:49 +00004194Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4195 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004196 QualType ResultType = Importer.Import(E->getType());
4197
4198 if (E->isArgumentType()) {
4199 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4200 if (!TInfo)
4201 return 0;
4202
Peter Collingbournee190dee2011-03-11 19:24:49 +00004203 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4204 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004205 Importer.Import(E->getOperatorLoc()),
4206 Importer.Import(E->getRParenLoc()));
4207 }
4208
4209 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4210 if (!SubExpr)
4211 return 0;
4212
Peter Collingbournee190dee2011-03-11 19:24:49 +00004213 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4214 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004215 Importer.Import(E->getOperatorLoc()),
4216 Importer.Import(E->getRParenLoc()));
4217}
4218
Douglas Gregorc74247e2010-02-19 01:07:06 +00004219Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4220 QualType T = Importer.Import(E->getType());
4221 if (T.isNull())
4222 return 0;
4223
4224 Expr *LHS = Importer.Import(E->getLHS());
4225 if (!LHS)
4226 return 0;
4227
4228 Expr *RHS = Importer.Import(E->getRHS());
4229 if (!RHS)
4230 return 0;
4231
4232 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004233 T, E->getValueKind(),
4234 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004235 Importer.Import(E->getOperatorLoc()),
4236 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004237}
4238
4239Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4240 QualType T = Importer.Import(E->getType());
4241 if (T.isNull())
4242 return 0;
4243
4244 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4245 if (CompLHSType.isNull())
4246 return 0;
4247
4248 QualType CompResultType = Importer.Import(E->getComputationResultType());
4249 if (CompResultType.isNull())
4250 return 0;
4251
4252 Expr *LHS = Importer.Import(E->getLHS());
4253 if (!LHS)
4254 return 0;
4255
4256 Expr *RHS = Importer.Import(E->getRHS());
4257 if (!RHS)
4258 return 0;
4259
4260 return new (Importer.getToContext())
4261 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004262 T, E->getValueKind(),
4263 E->getObjectKind(),
4264 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004265 Importer.Import(E->getOperatorLoc()),
4266 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004267}
4268
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004269static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004270 if (E->path_empty()) return false;
4271
4272 // TODO: import cast paths
4273 return true;
4274}
4275
Douglas Gregor98c10182010-02-12 22:17:39 +00004276Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4277 QualType T = Importer.Import(E->getType());
4278 if (T.isNull())
4279 return 0;
4280
4281 Expr *SubExpr = Importer.Import(E->getSubExpr());
4282 if (!SubExpr)
4283 return 0;
John McCallcf142162010-08-07 06:22:56 +00004284
4285 CXXCastPath BasePath;
4286 if (ImportCastPath(E, BasePath))
4287 return 0;
4288
4289 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004290 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004291}
4292
Douglas Gregor5481d322010-02-19 01:32:14 +00004293Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4294 QualType T = Importer.Import(E->getType());
4295 if (T.isNull())
4296 return 0;
4297
4298 Expr *SubExpr = Importer.Import(E->getSubExpr());
4299 if (!SubExpr)
4300 return 0;
4301
4302 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4303 if (!TInfo && E->getTypeInfoAsWritten())
4304 return 0;
4305
John McCallcf142162010-08-07 06:22:56 +00004306 CXXCastPath BasePath;
4307 if (ImportCastPath(E, BasePath))
4308 return 0;
4309
John McCall7decc9e2010-11-18 06:31:45 +00004310 return CStyleCastExpr::Create(Importer.getToContext(), T,
4311 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004312 SubExpr, &BasePath, TInfo,
4313 Importer.Import(E->getLParenLoc()),
4314 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004315}
4316
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004317ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004318 ASTContext &FromContext, FileManager &FromFileManager,
4319 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004320 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004321 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4322 Minimal(MinimalImport)
4323{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004324 ImportedDecls[FromContext.getTranslationUnitDecl()]
4325 = ToContext.getTranslationUnitDecl();
4326}
4327
4328ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004329
4330QualType ASTImporter::Import(QualType FromT) {
4331 if (FromT.isNull())
4332 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004333
4334 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004335
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004336 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004337 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4338 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004339 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004340 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004341
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004342 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004343 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004344 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004345 if (ToT.isNull())
4346 return ToT;
4347
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004348 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004349 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004350
John McCall424cec92011-01-19 06:33:43 +00004351 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004352}
4353
Douglas Gregor62d311f2010-02-09 19:21:46 +00004354TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004355 if (!FromTSI)
4356 return FromTSI;
4357
4358 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004359 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004360 QualType T = Import(FromTSI->getType());
4361 if (T.isNull())
4362 return 0;
4363
4364 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004365 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004366}
4367
4368Decl *ASTImporter::Import(Decl *FromD) {
4369 if (!FromD)
4370 return 0;
4371
Douglas Gregord451ea92011-07-29 23:31:30 +00004372 ASTNodeImporter Importer(*this);
4373
Douglas Gregor62d311f2010-02-09 19:21:46 +00004374 // Check whether we've already imported this declaration.
4375 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004376 if (Pos != ImportedDecls.end()) {
4377 Decl *ToD = Pos->second;
4378 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4379 return ToD;
4380 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004381
4382 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004383 Decl *ToD = Importer.Visit(FromD);
4384 if (!ToD)
4385 return 0;
4386
4387 // Record the imported declaration.
4388 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004389
4390 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4391 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004392 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004393 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004394 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004395 // When we've finished transforming a typedef, see whether it was the
4396 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004397 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004398 FromTag = AnonTagsWithPendingTypedefs.begin(),
4399 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4400 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004401 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004402 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4403 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004404 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004405 AnonTagsWithPendingTypedefs.erase(FromTag);
4406 break;
4407 }
4408 }
4409 }
4410 }
4411
Douglas Gregor62d311f2010-02-09 19:21:46 +00004412 return ToD;
4413}
4414
4415DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4416 if (!FromDC)
4417 return FromDC;
4418
Douglas Gregor95d82832012-01-24 18:36:04 +00004419 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004420 if (!ToDC)
4421 return 0;
4422
4423 // When we're using a record/enum/Objective-C class/protocol as a context, we
4424 // need it to have a definition.
4425 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004426 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004427 if (ToRecord->isCompleteDefinition()) {
4428 // Do nothing.
4429 } else if (FromRecord->isCompleteDefinition()) {
4430 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4431 ASTNodeImporter::IDK_Basic);
4432 } else {
4433 CompleteDecl(ToRecord);
4434 }
4435 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4436 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4437 if (ToEnum->isCompleteDefinition()) {
4438 // Do nothing.
4439 } else if (FromEnum->isCompleteDefinition()) {
4440 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4441 ASTNodeImporter::IDK_Basic);
4442 } else {
4443 CompleteDecl(ToEnum);
4444 }
4445 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4446 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4447 if (ToClass->getDefinition()) {
4448 // Do nothing.
4449 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4450 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4451 ASTNodeImporter::IDK_Basic);
4452 } else {
4453 CompleteDecl(ToClass);
4454 }
4455 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4456 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4457 if (ToProto->getDefinition()) {
4458 // Do nothing.
4459 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4460 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4461 ASTNodeImporter::IDK_Basic);
4462 } else {
4463 CompleteDecl(ToProto);
4464 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004465 }
4466
4467 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004468}
4469
4470Expr *ASTImporter::Import(Expr *FromE) {
4471 if (!FromE)
4472 return 0;
4473
4474 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4475}
4476
4477Stmt *ASTImporter::Import(Stmt *FromS) {
4478 if (!FromS)
4479 return 0;
4480
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004481 // Check whether we've already imported this declaration.
4482 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4483 if (Pos != ImportedStmts.end())
4484 return Pos->second;
4485
4486 // Import the type
4487 ASTNodeImporter Importer(*this);
4488 Stmt *ToS = Importer.Visit(FromS);
4489 if (!ToS)
4490 return 0;
4491
4492 // Record the imported declaration.
4493 ImportedStmts[FromS] = ToS;
4494 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004495}
4496
4497NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4498 if (!FromNNS)
4499 return 0;
4500
Douglas Gregor90ebf252011-04-27 16:48:40 +00004501 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4502
4503 switch (FromNNS->getKind()) {
4504 case NestedNameSpecifier::Identifier:
4505 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4506 return NestedNameSpecifier::Create(ToContext, prefix, II);
4507 }
4508 return 0;
4509
4510 case NestedNameSpecifier::Namespace:
4511 if (NamespaceDecl *NS =
4512 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4513 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4514 }
4515 return 0;
4516
4517 case NestedNameSpecifier::NamespaceAlias:
4518 if (NamespaceAliasDecl *NSAD =
4519 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4520 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4521 }
4522 return 0;
4523
4524 case NestedNameSpecifier::Global:
4525 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4526
4527 case NestedNameSpecifier::TypeSpec:
4528 case NestedNameSpecifier::TypeSpecWithTemplate: {
4529 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4530 if (!T.isNull()) {
4531 bool bTemplate = FromNNS->getKind() ==
4532 NestedNameSpecifier::TypeSpecWithTemplate;
4533 return NestedNameSpecifier::Create(ToContext, prefix,
4534 bTemplate, T.getTypePtr());
4535 }
4536 }
4537 return 0;
4538 }
4539
4540 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004541}
4542
Douglas Gregor14454802011-02-25 02:25:35 +00004543NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4544 // FIXME: Implement!
4545 return NestedNameSpecifierLoc();
4546}
4547
Douglas Gregore2e50d332010-12-01 01:36:18 +00004548TemplateName ASTImporter::Import(TemplateName From) {
4549 switch (From.getKind()) {
4550 case TemplateName::Template:
4551 if (TemplateDecl *ToTemplate
4552 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4553 return TemplateName(ToTemplate);
4554
4555 return TemplateName();
4556
4557 case TemplateName::OverloadedTemplate: {
4558 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4559 UnresolvedSet<2> ToTemplates;
4560 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4561 E = FromStorage->end();
4562 I != E; ++I) {
4563 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4564 ToTemplates.addDecl(To);
4565 else
4566 return TemplateName();
4567 }
4568 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4569 ToTemplates.end());
4570 }
4571
4572 case TemplateName::QualifiedTemplate: {
4573 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4574 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4575 if (!Qualifier)
4576 return TemplateName();
4577
4578 if (TemplateDecl *ToTemplate
4579 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4580 return ToContext.getQualifiedTemplateName(Qualifier,
4581 QTN->hasTemplateKeyword(),
4582 ToTemplate);
4583
4584 return TemplateName();
4585 }
4586
4587 case TemplateName::DependentTemplate: {
4588 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4589 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4590 if (!Qualifier)
4591 return TemplateName();
4592
4593 if (DTN->isIdentifier()) {
4594 return ToContext.getDependentTemplateName(Qualifier,
4595 Import(DTN->getIdentifier()));
4596 }
4597
4598 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4599 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004600
4601 case TemplateName::SubstTemplateTemplateParm: {
4602 SubstTemplateTemplateParmStorage *subst
4603 = From.getAsSubstTemplateTemplateParm();
4604 TemplateTemplateParmDecl *param
4605 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4606 if (!param)
4607 return TemplateName();
4608
4609 TemplateName replacement = Import(subst->getReplacement());
4610 if (replacement.isNull()) return TemplateName();
4611
4612 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4613 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004614
4615 case TemplateName::SubstTemplateTemplateParmPack: {
4616 SubstTemplateTemplateParmPackStorage *SubstPack
4617 = From.getAsSubstTemplateTemplateParmPack();
4618 TemplateTemplateParmDecl *Param
4619 = cast_or_null<TemplateTemplateParmDecl>(
4620 Import(SubstPack->getParameterPack()));
4621 if (!Param)
4622 return TemplateName();
4623
4624 ASTNodeImporter Importer(*this);
4625 TemplateArgument ArgPack
4626 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4627 if (ArgPack.isNull())
4628 return TemplateName();
4629
4630 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4631 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004632 }
4633
4634 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004635}
4636
Douglas Gregor62d311f2010-02-09 19:21:46 +00004637SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4638 if (FromLoc.isInvalid())
4639 return SourceLocation();
4640
Douglas Gregor811663e2010-02-10 00:15:17 +00004641 SourceManager &FromSM = FromContext.getSourceManager();
4642
4643 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004644 // don't have to import macro expansions.
4645 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004646 FromLoc = FromSM.getSpellingLoc(FromLoc);
4647 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4648 SourceManager &ToSM = ToContext.getSourceManager();
4649 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004650 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004651}
4652
4653SourceRange ASTImporter::Import(SourceRange FromRange) {
4654 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4655}
4656
Douglas Gregor811663e2010-02-10 00:15:17 +00004657FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004658 llvm::DenseMap<FileID, FileID>::iterator Pos
4659 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004660 if (Pos != ImportedFileIDs.end())
4661 return Pos->second;
4662
4663 SourceManager &FromSM = FromContext.getSourceManager();
4664 SourceManager &ToSM = ToContext.getSourceManager();
4665 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004666 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004667
4668 // Include location of this file.
4669 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4670
4671 // Map the FileID for to the "to" source manager.
4672 FileID ToID;
4673 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004674 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004675 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4676 // disk again
4677 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4678 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004679 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004680 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4681 FromSLoc.getFile().getFileCharacteristic());
4682 } else {
4683 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004684 const llvm::MemoryBuffer *
4685 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004686 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004687 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004688 FromBuf->getBufferIdentifier());
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +00004689 ToID = ToSM.createFileIDForMemBuffer(ToBuf,
4690 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00004691 }
4692
4693
Sebastian Redl99219f12010-09-30 01:03:06 +00004694 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004695 return ToID;
4696}
4697
Douglas Gregor0a791672011-01-18 03:11:38 +00004698void ASTImporter::ImportDefinition(Decl *From) {
4699 Decl *To = Import(From);
4700 if (!To)
4701 return;
4702
4703 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4704 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004705
4706 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4707 if (!ToRecord->getDefinition()) {
4708 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004709 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004710 return;
4711 }
4712 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004713
4714 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4715 if (!ToEnum->getDefinition()) {
4716 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004717 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004718 return;
4719 }
4720 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004721
4722 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4723 if (!ToIFace->getDefinition()) {
4724 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004725 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004726 return;
4727 }
4728 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004729
Douglas Gregor2aa53772012-01-24 17:42:07 +00004730 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4731 if (!ToProto->getDefinition()) {
4732 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004733 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004734 return;
4735 }
4736 }
4737
Douglas Gregor0a791672011-01-18 03:11:38 +00004738 Importer.ImportDeclContext(FromDC, true);
4739 }
4740}
4741
Douglas Gregor96e578d2010-02-05 17:54:41 +00004742DeclarationName ASTImporter::Import(DeclarationName FromName) {
4743 if (!FromName)
4744 return DeclarationName();
4745
4746 switch (FromName.getNameKind()) {
4747 case DeclarationName::Identifier:
4748 return Import(FromName.getAsIdentifierInfo());
4749
4750 case DeclarationName::ObjCZeroArgSelector:
4751 case DeclarationName::ObjCOneArgSelector:
4752 case DeclarationName::ObjCMultiArgSelector:
4753 return Import(FromName.getObjCSelector());
4754
4755 case DeclarationName::CXXConstructorName: {
4756 QualType T = Import(FromName.getCXXNameType());
4757 if (T.isNull())
4758 return DeclarationName();
4759
4760 return ToContext.DeclarationNames.getCXXConstructorName(
4761 ToContext.getCanonicalType(T));
4762 }
4763
4764 case DeclarationName::CXXDestructorName: {
4765 QualType T = Import(FromName.getCXXNameType());
4766 if (T.isNull())
4767 return DeclarationName();
4768
4769 return ToContext.DeclarationNames.getCXXDestructorName(
4770 ToContext.getCanonicalType(T));
4771 }
4772
4773 case DeclarationName::CXXConversionFunctionName: {
4774 QualType T = Import(FromName.getCXXNameType());
4775 if (T.isNull())
4776 return DeclarationName();
4777
4778 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4779 ToContext.getCanonicalType(T));
4780 }
4781
4782 case DeclarationName::CXXOperatorName:
4783 return ToContext.DeclarationNames.getCXXOperatorName(
4784 FromName.getCXXOverloadedOperator());
4785
4786 case DeclarationName::CXXLiteralOperatorName:
4787 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4788 Import(FromName.getCXXLiteralIdentifier()));
4789
4790 case DeclarationName::CXXUsingDirective:
4791 // FIXME: STATICS!
4792 return DeclarationName::getUsingDirectiveName();
4793 }
4794
David Blaikiee4d798f2012-01-20 21:50:17 +00004795 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004796}
4797
Douglas Gregore2e50d332010-12-01 01:36:18 +00004798IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004799 if (!FromId)
4800 return 0;
4801
4802 return &ToContext.Idents.get(FromId->getName());
4803}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004804
Douglas Gregor43f54792010-02-17 02:12:47 +00004805Selector ASTImporter::Import(Selector FromSel) {
4806 if (FromSel.isNull())
4807 return Selector();
4808
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004809 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004810 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4811 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4812 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4813 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4814}
4815
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004816DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4817 DeclContext *DC,
4818 unsigned IDNS,
4819 NamedDecl **Decls,
4820 unsigned NumDecls) {
4821 return Name;
4822}
4823
4824DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004825 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004826}
4827
4828DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004829 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004830}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004831
Douglas Gregor2e15c842012-02-01 21:00:38 +00004832void ASTImporter::CompleteDecl (Decl *D) {
4833 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4834 if (!ID->getDefinition())
4835 ID->startDefinition();
4836 }
4837 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4838 if (!PD->getDefinition())
4839 PD->startDefinition();
4840 }
4841 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4842 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4843 TD->startDefinition();
4844 TD->setCompleteDefinition(true);
4845 }
4846 }
4847 else {
4848 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4849 }
4850}
4851
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004852Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4853 ImportedDecls[From] = To;
4854 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004855}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004856
Douglas Gregordd6006f2012-07-17 21:16:27 +00004857bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4858 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004859 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004860 = ImportedTypes.find(From.getTypePtr());
4861 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4862 return true;
4863
Douglas Gregordd6006f2012-07-17 21:16:27 +00004864 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4865 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004866 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004867}