blob: 906cc3b4d0cfc63955acb9f8487366215a42947d [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"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
Douglas Gregor3c2404b2011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
33
34 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000040
41 // Importing types
John McCall424cec92011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
43 QualType VisitBuiltinType(const BuiltinType *T);
44 QualType VisitComplexType(const ComplexType *T);
45 QualType VisitPointerType(const PointerType *T);
46 QualType VisitBlockPointerType(const BlockPointerType *T);
47 QualType VisitLValueReferenceType(const LValueReferenceType *T);
48 QualType VisitRValueReferenceType(const RValueReferenceType *T);
49 QualType VisitMemberPointerType(const MemberPointerType *T);
50 QualType VisitConstantArrayType(const ConstantArrayType *T);
51 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
52 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000055 QualType VisitVectorType(const VectorType *T);
56 QualType VisitExtVectorType(const ExtVectorType *T);
57 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
58 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
Douglas Gregor95d82832012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregord451ea92011-07-29 23:31:30 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000089
Douglas Gregor95d82832012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregord451ea92011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000110 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000112 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000116 TemplateParameterList *ImportTemplateParameterList(
117 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000118 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
119 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
120 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000121 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000122 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
123 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000124 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000125 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000126 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000127 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000128 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000129 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000130 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000131 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000132 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000133 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000134 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000135 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000136 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000137 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
138 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
139 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
140 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000141 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000142 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000143 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000144 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000145 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000146 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000147 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000148 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000149 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000150 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000151 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000152 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000153 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000154 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000155 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
156 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
157 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
158 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000159 Decl *VisitClassTemplateSpecializationDecl(
160 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000161
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000162 // Importing statements
163 Stmt *VisitStmt(Stmt *S);
164
165 // Importing expressions
166 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000167 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000168 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000169 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000170 Expr *VisitParenExpr(ParenExpr *E);
171 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000172 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000173 Expr *VisitBinaryOperator(BinaryOperator *E);
174 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000175 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000176 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000177 };
178}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000179using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000180
181//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000182// Structural Equivalence
183//----------------------------------------------------------------------------
184
185namespace {
186 struct StructuralEquivalenceContext {
187 /// \brief AST contexts for which we are checking structural equivalence.
188 ASTContext &C1, &C2;
189
Douglas Gregor3996e242010-02-15 22:01:00 +0000190 /// \brief The set of "tentative" equivalences between two canonical
191 /// declarations, mapping from a declaration in the first context to the
192 /// declaration in the second context that we believe to be equivalent.
193 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
194
195 /// \brief Queue of declarations in the first context whose equivalence
196 /// with a declaration in the second context still needs to be verified.
197 std::deque<Decl *> DeclsToCheck;
198
Douglas Gregorb4964f72010-02-15 23:54:17 +0000199 /// \brief Declaration (from, to) pairs that are known not to be equivalent
200 /// (which we have already complained about).
201 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
202
Douglas Gregor3996e242010-02-15 22:01:00 +0000203 /// \brief Whether we're being strict about the spelling of types when
204 /// unifying two types.
205 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000206
207 /// \brief Whether to complain about failures.
208 bool Complain;
209
Douglas Gregor3996e242010-02-15 22:01:00 +0000210 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000211 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000212 bool StrictTypeSpelling = false,
213 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000214 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregordd6006f2012-07-17 21:16:27 +0000215 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000216
217 /// \brief Determine whether the two declarations are structurally
218 /// equivalent.
219 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
220
221 /// \brief Determine whether the two types are structurally equivalent.
222 bool IsStructurallyEquivalent(QualType T1, QualType T2);
223
224 private:
225 /// \brief Finish checking all of the structural equivalences.
226 ///
227 /// \returns true if an error occurred, false otherwise.
228 bool Finish();
229
230 public:
231 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000232 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000233 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000234 }
235
236 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000237 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000238 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000239 }
240 };
241}
242
243static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
244 QualType T1, QualType T2);
245static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
246 Decl *D1, Decl *D2);
247
Douglas Gregor3996e242010-02-15 22:01:00 +0000248/// \brief Determine structural equivalence of two expressions.
249static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
250 Expr *E1, Expr *E2) {
251 if (!E1 || !E2)
252 return E1 == E2;
253
254 // FIXME: Actually perform a structural comparison!
255 return true;
256}
257
258/// \brief Determine whether two identifiers are equivalent.
259static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
260 const IdentifierInfo *Name2) {
261 if (!Name1 || !Name2)
262 return Name1 == Name2;
263
264 return Name1->getName() == Name2->getName();
265}
266
267/// \brief Determine whether two nested-name-specifiers are equivalent.
268static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
269 NestedNameSpecifier *NNS1,
270 NestedNameSpecifier *NNS2) {
271 // FIXME: Implement!
272 return true;
273}
274
275/// \brief Determine whether two template arguments are equivalent.
276static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
277 const TemplateArgument &Arg1,
278 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000279 if (Arg1.getKind() != Arg2.getKind())
280 return false;
281
282 switch (Arg1.getKind()) {
283 case TemplateArgument::Null:
284 return true;
285
286 case TemplateArgument::Type:
287 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000288
Douglas Gregore2e50d332010-12-01 01:36:18 +0000289 case TemplateArgument::Integral:
290 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
291 Arg2.getIntegralType()))
292 return false;
293
Eric Christopher6dcc3762012-07-15 00:23:57 +0000294 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000295
296 case TemplateArgument::Declaration:
297 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000298
299 case TemplateArgument::NullPtr:
300 return true; // FIXME: Is this correct?
301
Douglas Gregore2e50d332010-12-01 01:36:18 +0000302 case TemplateArgument::Template:
303 return IsStructurallyEquivalent(Context,
304 Arg1.getAsTemplate(),
305 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000306
307 case TemplateArgument::TemplateExpansion:
308 return IsStructurallyEquivalent(Context,
309 Arg1.getAsTemplateOrTemplatePattern(),
310 Arg2.getAsTemplateOrTemplatePattern());
311
Douglas Gregore2e50d332010-12-01 01:36:18 +0000312 case TemplateArgument::Expression:
313 return IsStructurallyEquivalent(Context,
314 Arg1.getAsExpr(), Arg2.getAsExpr());
315
316 case TemplateArgument::Pack:
317 if (Arg1.pack_size() != Arg2.pack_size())
318 return false;
319
320 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
321 if (!IsStructurallyEquivalent(Context,
322 Arg1.pack_begin()[I],
323 Arg2.pack_begin()[I]))
324 return false;
325
326 return true;
327 }
328
329 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000330}
331
332/// \brief Determine structural equivalence for the common part of array
333/// types.
334static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
335 const ArrayType *Array1,
336 const ArrayType *Array2) {
337 if (!IsStructurallyEquivalent(Context,
338 Array1->getElementType(),
339 Array2->getElementType()))
340 return false;
341 if (Array1->getSizeModifier() != Array2->getSizeModifier())
342 return false;
343 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
344 return false;
345
346 return true;
347}
348
349/// \brief Determine structural equivalence of two types.
350static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
351 QualType T1, QualType T2) {
352 if (T1.isNull() || T2.isNull())
353 return T1.isNull() && T2.isNull();
354
355 if (!Context.StrictTypeSpelling) {
356 // We aren't being strict about token-to-token equivalence of types,
357 // so map down to the canonical type.
358 T1 = Context.C1.getCanonicalType(T1);
359 T2 = Context.C2.getCanonicalType(T2);
360 }
361
362 if (T1.getQualifiers() != T2.getQualifiers())
363 return false;
364
Douglas Gregorb4964f72010-02-15 23:54:17 +0000365 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000366
Douglas Gregorb4964f72010-02-15 23:54:17 +0000367 if (T1->getTypeClass() != T2->getTypeClass()) {
368 // Compare function types with prototypes vs. without prototypes as if
369 // both did not have prototypes.
370 if (T1->getTypeClass() == Type::FunctionProto &&
371 T2->getTypeClass() == Type::FunctionNoProto)
372 TC = Type::FunctionNoProto;
373 else if (T1->getTypeClass() == Type::FunctionNoProto &&
374 T2->getTypeClass() == Type::FunctionProto)
375 TC = Type::FunctionNoProto;
376 else
377 return false;
378 }
379
380 switch (TC) {
381 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000382 // FIXME: Deal with Char_S/Char_U.
383 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
384 return false;
385 break;
386
387 case Type::Complex:
388 if (!IsStructurallyEquivalent(Context,
389 cast<ComplexType>(T1)->getElementType(),
390 cast<ComplexType>(T2)->getElementType()))
391 return false;
392 break;
393
394 case Type::Pointer:
395 if (!IsStructurallyEquivalent(Context,
396 cast<PointerType>(T1)->getPointeeType(),
397 cast<PointerType>(T2)->getPointeeType()))
398 return false;
399 break;
400
401 case Type::BlockPointer:
402 if (!IsStructurallyEquivalent(Context,
403 cast<BlockPointerType>(T1)->getPointeeType(),
404 cast<BlockPointerType>(T2)->getPointeeType()))
405 return false;
406 break;
407
408 case Type::LValueReference:
409 case Type::RValueReference: {
410 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
411 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
412 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
413 return false;
414 if (Ref1->isInnerRef() != Ref2->isInnerRef())
415 return false;
416 if (!IsStructurallyEquivalent(Context,
417 Ref1->getPointeeTypeAsWritten(),
418 Ref2->getPointeeTypeAsWritten()))
419 return false;
420 break;
421 }
422
423 case Type::MemberPointer: {
424 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
425 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
426 if (!IsStructurallyEquivalent(Context,
427 MemPtr1->getPointeeType(),
428 MemPtr2->getPointeeType()))
429 return false;
430 if (!IsStructurallyEquivalent(Context,
431 QualType(MemPtr1->getClass(), 0),
432 QualType(MemPtr2->getClass(), 0)))
433 return false;
434 break;
435 }
436
437 case Type::ConstantArray: {
438 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
439 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000440 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000441 return false;
442
443 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
444 return false;
445 break;
446 }
447
448 case Type::IncompleteArray:
449 if (!IsArrayStructurallyEquivalent(Context,
450 cast<ArrayType>(T1),
451 cast<ArrayType>(T2)))
452 return false;
453 break;
454
455 case Type::VariableArray: {
456 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
457 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
458 if (!IsStructurallyEquivalent(Context,
459 Array1->getSizeExpr(), Array2->getSizeExpr()))
460 return false;
461
462 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
463 return false;
464
465 break;
466 }
467
468 case Type::DependentSizedArray: {
469 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
470 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
471 if (!IsStructurallyEquivalent(Context,
472 Array1->getSizeExpr(), Array2->getSizeExpr()))
473 return false;
474
475 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
476 return false;
477
478 break;
479 }
480
481 case Type::DependentSizedExtVector: {
482 const DependentSizedExtVectorType *Vec1
483 = cast<DependentSizedExtVectorType>(T1);
484 const DependentSizedExtVectorType *Vec2
485 = cast<DependentSizedExtVectorType>(T2);
486 if (!IsStructurallyEquivalent(Context,
487 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
488 return false;
489 if (!IsStructurallyEquivalent(Context,
490 Vec1->getElementType(),
491 Vec2->getElementType()))
492 return false;
493 break;
494 }
495
496 case Type::Vector:
497 case Type::ExtVector: {
498 const VectorType *Vec1 = cast<VectorType>(T1);
499 const VectorType *Vec2 = cast<VectorType>(T2);
500 if (!IsStructurallyEquivalent(Context,
501 Vec1->getElementType(),
502 Vec2->getElementType()))
503 return false;
504 if (Vec1->getNumElements() != Vec2->getNumElements())
505 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000506 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000507 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000508 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000509 }
510
511 case Type::FunctionProto: {
512 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
513 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
514 if (Proto1->getNumArgs() != Proto2->getNumArgs())
515 return false;
516 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
517 if (!IsStructurallyEquivalent(Context,
518 Proto1->getArgType(I),
519 Proto2->getArgType(I)))
520 return false;
521 }
522 if (Proto1->isVariadic() != Proto2->isVariadic())
523 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000524 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000525 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
527 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
528 return false;
529 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
530 if (!IsStructurallyEquivalent(Context,
531 Proto1->getExceptionType(I),
532 Proto2->getExceptionType(I)))
533 return false;
534 }
535 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000536 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000537 Proto1->getNoexceptExpr(),
538 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 return false;
540 }
541 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
542 return false;
543
544 // Fall through to check the bits common with FunctionNoProtoType.
545 }
546
547 case Type::FunctionNoProto: {
548 const FunctionType *Function1 = cast<FunctionType>(T1);
549 const FunctionType *Function2 = cast<FunctionType>(T2);
550 if (!IsStructurallyEquivalent(Context,
551 Function1->getResultType(),
552 Function2->getResultType()))
553 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000554 if (Function1->getExtInfo() != Function2->getExtInfo())
555 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000556 break;
557 }
558
559 case Type::UnresolvedUsing:
560 if (!IsStructurallyEquivalent(Context,
561 cast<UnresolvedUsingType>(T1)->getDecl(),
562 cast<UnresolvedUsingType>(T2)->getDecl()))
563 return false;
564
565 break;
John McCall81904512011-01-06 01:58:22 +0000566
567 case Type::Attributed:
568 if (!IsStructurallyEquivalent(Context,
569 cast<AttributedType>(T1)->getModifiedType(),
570 cast<AttributedType>(T2)->getModifiedType()))
571 return false;
572 if (!IsStructurallyEquivalent(Context,
573 cast<AttributedType>(T1)->getEquivalentType(),
574 cast<AttributedType>(T2)->getEquivalentType()))
575 return false;
576 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000577
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000578 case Type::Paren:
579 if (!IsStructurallyEquivalent(Context,
580 cast<ParenType>(T1)->getInnerType(),
581 cast<ParenType>(T2)->getInnerType()))
582 return false;
583 break;
584
Douglas Gregor3996e242010-02-15 22:01:00 +0000585 case Type::Typedef:
586 if (!IsStructurallyEquivalent(Context,
587 cast<TypedefType>(T1)->getDecl(),
588 cast<TypedefType>(T2)->getDecl()))
589 return false;
590 break;
591
592 case Type::TypeOfExpr:
593 if (!IsStructurallyEquivalent(Context,
594 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
595 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
596 return false;
597 break;
598
599 case Type::TypeOf:
600 if (!IsStructurallyEquivalent(Context,
601 cast<TypeOfType>(T1)->getUnderlyingType(),
602 cast<TypeOfType>(T2)->getUnderlyingType()))
603 return false;
604 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000605
606 case Type::UnaryTransform:
607 if (!IsStructurallyEquivalent(Context,
608 cast<UnaryTransformType>(T1)->getUnderlyingType(),
609 cast<UnaryTransformType>(T1)->getUnderlyingType()))
610 return false;
611 break;
612
Douglas Gregor3996e242010-02-15 22:01:00 +0000613 case Type::Decltype:
614 if (!IsStructurallyEquivalent(Context,
615 cast<DecltypeType>(T1)->getUnderlyingExpr(),
616 cast<DecltypeType>(T2)->getUnderlyingExpr()))
617 return false;
618 break;
619
Richard Smith30482bc2011-02-20 03:19:35 +0000620 case Type::Auto:
621 if (!IsStructurallyEquivalent(Context,
622 cast<AutoType>(T1)->getDeducedType(),
623 cast<AutoType>(T2)->getDeducedType()))
624 return false;
625 break;
626
Douglas Gregor3996e242010-02-15 22:01:00 +0000627 case Type::Record:
628 case Type::Enum:
629 if (!IsStructurallyEquivalent(Context,
630 cast<TagType>(T1)->getDecl(),
631 cast<TagType>(T2)->getDecl()))
632 return false;
633 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000634
Douglas Gregor3996e242010-02-15 22:01:00 +0000635 case Type::TemplateTypeParm: {
636 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
637 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
638 if (Parm1->getDepth() != Parm2->getDepth())
639 return false;
640 if (Parm1->getIndex() != Parm2->getIndex())
641 return false;
642 if (Parm1->isParameterPack() != Parm2->isParameterPack())
643 return false;
644
645 // Names of template type parameters are never significant.
646 break;
647 }
648
649 case Type::SubstTemplateTypeParm: {
650 const SubstTemplateTypeParmType *Subst1
651 = cast<SubstTemplateTypeParmType>(T1);
652 const SubstTemplateTypeParmType *Subst2
653 = cast<SubstTemplateTypeParmType>(T2);
654 if (!IsStructurallyEquivalent(Context,
655 QualType(Subst1->getReplacedParameter(), 0),
656 QualType(Subst2->getReplacedParameter(), 0)))
657 return false;
658 if (!IsStructurallyEquivalent(Context,
659 Subst1->getReplacementType(),
660 Subst2->getReplacementType()))
661 return false;
662 break;
663 }
664
Douglas Gregorfb322d82011-01-14 05:11:40 +0000665 case Type::SubstTemplateTypeParmPack: {
666 const SubstTemplateTypeParmPackType *Subst1
667 = cast<SubstTemplateTypeParmPackType>(T1);
668 const SubstTemplateTypeParmPackType *Subst2
669 = cast<SubstTemplateTypeParmPackType>(T2);
670 if (!IsStructurallyEquivalent(Context,
671 QualType(Subst1->getReplacedParameter(), 0),
672 QualType(Subst2->getReplacedParameter(), 0)))
673 return false;
674 if (!IsStructurallyEquivalent(Context,
675 Subst1->getArgumentPack(),
676 Subst2->getArgumentPack()))
677 return false;
678 break;
679 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000680 case Type::TemplateSpecialization: {
681 const TemplateSpecializationType *Spec1
682 = cast<TemplateSpecializationType>(T1);
683 const TemplateSpecializationType *Spec2
684 = cast<TemplateSpecializationType>(T2);
685 if (!IsStructurallyEquivalent(Context,
686 Spec1->getTemplateName(),
687 Spec2->getTemplateName()))
688 return false;
689 if (Spec1->getNumArgs() != Spec2->getNumArgs())
690 return false;
691 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
692 if (!IsStructurallyEquivalent(Context,
693 Spec1->getArg(I), Spec2->getArg(I)))
694 return false;
695 }
696 break;
697 }
698
Abramo Bagnara6150c882010-05-11 21:36:43 +0000699 case Type::Elaborated: {
700 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
701 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
702 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
703 if (Elab1->getKeyword() != Elab2->getKeyword())
704 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000705 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000706 Elab1->getQualifier(),
707 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000708 return false;
709 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000710 Elab1->getNamedType(),
711 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000712 return false;
713 break;
714 }
715
John McCalle78aac42010-03-10 03:28:59 +0000716 case Type::InjectedClassName: {
717 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
718 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
719 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000720 Inj1->getInjectedSpecializationType(),
721 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000722 return false;
723 break;
724 }
725
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000726 case Type::DependentName: {
727 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
728 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000729 if (!IsStructurallyEquivalent(Context,
730 Typename1->getQualifier(),
731 Typename2->getQualifier()))
732 return false;
733 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
734 Typename2->getIdentifier()))
735 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000736
737 break;
738 }
739
John McCallc392f372010-06-11 00:33:02 +0000740 case Type::DependentTemplateSpecialization: {
741 const DependentTemplateSpecializationType *Spec1 =
742 cast<DependentTemplateSpecializationType>(T1);
743 const DependentTemplateSpecializationType *Spec2 =
744 cast<DependentTemplateSpecializationType>(T2);
745 if (!IsStructurallyEquivalent(Context,
746 Spec1->getQualifier(),
747 Spec2->getQualifier()))
748 return false;
749 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
750 Spec2->getIdentifier()))
751 return false;
752 if (Spec1->getNumArgs() != Spec2->getNumArgs())
753 return false;
754 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
755 if (!IsStructurallyEquivalent(Context,
756 Spec1->getArg(I), Spec2->getArg(I)))
757 return false;
758 }
759 break;
760 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000761
762 case Type::PackExpansion:
763 if (!IsStructurallyEquivalent(Context,
764 cast<PackExpansionType>(T1)->getPattern(),
765 cast<PackExpansionType>(T2)->getPattern()))
766 return false;
767 break;
768
Douglas Gregor3996e242010-02-15 22:01:00 +0000769 case Type::ObjCInterface: {
770 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
771 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
772 if (!IsStructurallyEquivalent(Context,
773 Iface1->getDecl(), Iface2->getDecl()))
774 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000775 break;
776 }
777
778 case Type::ObjCObject: {
779 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
780 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
781 if (!IsStructurallyEquivalent(Context,
782 Obj1->getBaseType(),
783 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000784 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000785 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
786 return false;
787 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000788 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000789 Obj1->getProtocol(I),
790 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 return false;
792 }
793 break;
794 }
795
796 case Type::ObjCObjectPointer: {
797 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
798 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
799 if (!IsStructurallyEquivalent(Context,
800 Ptr1->getPointeeType(),
801 Ptr2->getPointeeType()))
802 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000803 break;
804 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000805
806 case Type::Atomic: {
807 if (!IsStructurallyEquivalent(Context,
808 cast<AtomicType>(T1)->getValueType(),
809 cast<AtomicType>(T2)->getValueType()))
810 return false;
811 break;
812 }
813
Douglas Gregor3996e242010-02-15 22:01:00 +0000814 } // end switch
815
816 return true;
817}
818
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000819/// \brief Determine structural equivalence of two fields.
820static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
821 FieldDecl *Field1, FieldDecl *Field2) {
822 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000823
824 // For anonymous structs/unions, match up the anonymous struct/union type
825 // declarations directly, so that we don't go off searching for anonymous
826 // types
827 if (Field1->isAnonymousStructOrUnion() &&
828 Field2->isAnonymousStructOrUnion()) {
829 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
830 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
831 return IsStructurallyEquivalent(Context, D1, D2);
832 }
833
834 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000835 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000836 if (Context.Complain) {
837 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
838 << Context.C2.getTypeDeclType(Owner2);
839 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
840 << Field2->getDeclName() << Field2->getType();
841 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
842 << Field1->getDeclName() << Field1->getType();
843 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000844 return false;
845 }
846
847 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000848 if (Context.Complain) {
849 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
850 << Context.C2.getTypeDeclType(Owner2);
851 if (Field1->isBitField()) {
852 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
853 << Field1->getDeclName() << Field1->getType()
854 << Field1->getBitWidthValue(Context.C1);
855 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
856 << Field2->getDeclName();
857 } else {
858 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
859 << Field2->getDeclName() << Field2->getType()
860 << Field2->getBitWidthValue(Context.C2);
861 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
862 << Field1->getDeclName();
863 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000864 }
865 return false;
866 }
867
868 if (Field1->isBitField()) {
869 // Make sure that the bit-fields are the same length.
870 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
871 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
872
873 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000874 if (Context.Complain) {
875 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876 << Context.C2.getTypeDeclType(Owner2);
877 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
878 << Field2->getDeclName() << Field2->getType() << Bits2;
879 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
880 << Field1->getDeclName() << Field1->getType() << Bits1;
881 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000882 return false;
883 }
884 }
885
886 return true;
887}
888
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000889/// \brief Find the index of the given anonymous struct/union within its
890/// context.
891///
892/// \returns Returns the index of this anonymous struct/union in its context,
893/// including the next assigned index (if none of them match). Returns an
894/// empty option if the context is not a record, i.e.. if the anonymous
895/// struct/union is at namespace or block scope.
896static llvm::Optional<unsigned>
897findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
898 ASTContext &Context = Anon->getASTContext();
899 QualType AnonTy = Context.getRecordType(Anon);
900
901 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
902 if (!Owner)
903 return llvm::Optional<unsigned>();
904
905 unsigned Index = 0;
906 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
907 DEnd = Owner->noload_decls_end();
908 D != DEnd; ++D) {
909 FieldDecl *F = dyn_cast<FieldDecl>(*D);
910 if (!F || !F->isAnonymousStructOrUnion())
911 continue;
912
913 if (Context.hasSameType(F->getType(), AnonTy))
914 break;
915
916 ++Index;
917 }
918
919 return Index;
920}
921
Douglas Gregor3996e242010-02-15 22:01:00 +0000922/// \brief Determine structural equivalence of two records.
923static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
924 RecordDecl *D1, RecordDecl *D2) {
925 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000926 if (Context.Complain) {
927 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
928 << Context.C2.getTypeDeclType(D2);
929 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
930 << D1->getDeclName() << (unsigned)D1->getTagKind();
931 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000932 return false;
933 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000934
935 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
936 // If both anonymous structs/unions are in a record context, make sure
937 // they occur in the same location in the context records.
938 if (llvm::Optional<unsigned> Index1
939 = findAnonymousStructOrUnionIndex(D1)) {
940 if (llvm::Optional<unsigned> Index2
941 = findAnonymousStructOrUnionIndex(D2)) {
942 if (*Index1 != *Index2)
943 return false;
944 }
945 }
946 }
947
Douglas Gregore2e50d332010-12-01 01:36:18 +0000948 // If both declarations are class template specializations, we know
949 // the ODR applies, so check the template and template arguments.
950 ClassTemplateSpecializationDecl *Spec1
951 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
952 ClassTemplateSpecializationDecl *Spec2
953 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
954 if (Spec1 && Spec2) {
955 // Check that the specialized templates are the same.
956 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
957 Spec2->getSpecializedTemplate()))
958 return false;
959
960 // Check that the template arguments are the same.
961 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
962 return false;
963
964 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
965 if (!IsStructurallyEquivalent(Context,
966 Spec1->getTemplateArgs().get(I),
967 Spec2->getTemplateArgs().get(I)))
968 return false;
969 }
970 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000971 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000972 else if (Spec1 || Spec2)
973 return false;
974
Douglas Gregorb4964f72010-02-15 23:54:17 +0000975 // Compare the definitions of these two records. If either or both are
976 // incomplete, we assume that they are equivalent.
977 D1 = D1->getDefinition();
978 D2 = D2->getDefinition();
979 if (!D1 || !D2)
980 return true;
981
Douglas Gregor3996e242010-02-15 22:01:00 +0000982 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
983 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
984 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000985 if (Context.Complain) {
986 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
987 << Context.C2.getTypeDeclType(D2);
988 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
989 << D2CXX->getNumBases();
990 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
991 << D1CXX->getNumBases();
992 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000993 return false;
994 }
995
996 // Check the base classes.
997 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
998 BaseEnd1 = D1CXX->bases_end(),
999 Base2 = D2CXX->bases_begin();
1000 Base1 != BaseEnd1;
1001 ++Base1, ++Base2) {
1002 if (!IsStructurallyEquivalent(Context,
1003 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001004 if (Context.Complain) {
1005 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1006 << Context.C2.getTypeDeclType(D2);
1007 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1008 << Base2->getType()
1009 << Base2->getSourceRange();
1010 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1011 << Base1->getType()
1012 << Base1->getSourceRange();
1013 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001014 return false;
1015 }
1016
1017 // Check virtual vs. non-virtual inheritance mismatch.
1018 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001019 if (Context.Complain) {
1020 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1021 << Context.C2.getTypeDeclType(D2);
1022 Context.Diag2(Base2->getLocStart(),
1023 diag::note_odr_virtual_base)
1024 << Base2->isVirtual() << Base2->getSourceRange();
1025 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1026 << Base1->isVirtual()
1027 << Base1->getSourceRange();
1028 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001029 return false;
1030 }
1031 }
1032 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001033 if (Context.Complain) {
1034 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1035 << Context.C2.getTypeDeclType(D2);
1036 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1037 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1038 << Base1->getType()
1039 << Base1->getSourceRange();
1040 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1041 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001042 return false;
1043 }
1044 }
1045
1046 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001047 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001048 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001049 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001050 Field1End = D1->field_end();
1051 Field1 != Field1End;
1052 ++Field1, ++Field2) {
1053 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001054 if (Context.Complain) {
1055 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1056 << Context.C2.getTypeDeclType(D2);
1057 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1058 << Field1->getDeclName() << Field1->getType();
1059 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1060 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001061 return false;
1062 }
1063
David Blaikie40ed2972012-06-06 20:45:41 +00001064 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001065 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001066 }
1067
1068 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001069 if (Context.Complain) {
1070 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1071 << Context.C2.getTypeDeclType(D2);
1072 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1073 << Field2->getDeclName() << Field2->getType();
1074 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1075 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001076 return false;
1077 }
1078
1079 return true;
1080}
1081
1082/// \brief Determine structural equivalence of two enums.
1083static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1084 EnumDecl *D1, EnumDecl *D2) {
1085 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1086 EC2End = D2->enumerator_end();
1087 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1088 EC1End = D1->enumerator_end();
1089 EC1 != EC1End; ++EC1, ++EC2) {
1090 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001091 if (Context.Complain) {
1092 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1093 << Context.C2.getTypeDeclType(D2);
1094 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1095 << EC1->getDeclName()
1096 << EC1->getInitVal().toString(10);
1097 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1098 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001099 return false;
1100 }
1101
1102 llvm::APSInt Val1 = EC1->getInitVal();
1103 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001104 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001105 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001106 if (Context.Complain) {
1107 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1108 << Context.C2.getTypeDeclType(D2);
1109 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1110 << EC2->getDeclName()
1111 << EC2->getInitVal().toString(10);
1112 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1113 << EC1->getDeclName()
1114 << EC1->getInitVal().toString(10);
1115 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001116 return false;
1117 }
1118 }
1119
1120 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001121 if (Context.Complain) {
1122 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1123 << Context.C2.getTypeDeclType(D2);
1124 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1125 << EC2->getDeclName()
1126 << EC2->getInitVal().toString(10);
1127 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1128 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001129 return false;
1130 }
1131
1132 return true;
1133}
Douglas Gregora082a492010-11-30 19:14:50 +00001134
1135static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1136 TemplateParameterList *Params1,
1137 TemplateParameterList *Params2) {
1138 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001139 if (Context.Complain) {
1140 Context.Diag2(Params2->getTemplateLoc(),
1141 diag::err_odr_different_num_template_parameters)
1142 << Params1->size() << Params2->size();
1143 Context.Diag1(Params1->getTemplateLoc(),
1144 diag::note_odr_template_parameter_list);
1145 }
Douglas Gregora082a492010-11-30 19:14:50 +00001146 return false;
1147 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001148
Douglas Gregora082a492010-11-30 19:14:50 +00001149 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1150 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001151 if (Context.Complain) {
1152 Context.Diag2(Params2->getParam(I)->getLocation(),
1153 diag::err_odr_different_template_parameter_kind);
1154 Context.Diag1(Params1->getParam(I)->getLocation(),
1155 diag::note_odr_template_parameter_here);
1156 }
Douglas Gregora082a492010-11-30 19:14:50 +00001157 return false;
1158 }
1159
1160 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1161 Params2->getParam(I))) {
1162
1163 return false;
1164 }
1165 }
1166
1167 return true;
1168}
1169
1170static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1171 TemplateTypeParmDecl *D1,
1172 TemplateTypeParmDecl *D2) {
1173 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001174 if (Context.Complain) {
1175 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1176 << D2->isParameterPack();
1177 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1178 << D1->isParameterPack();
1179 }
Douglas Gregora082a492010-11-30 19:14:50 +00001180 return false;
1181 }
1182
1183 return true;
1184}
1185
1186static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1187 NonTypeTemplateParmDecl *D1,
1188 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001189 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001190 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001191 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1192 << D2->isParameterPack();
1193 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1194 << D1->isParameterPack();
1195 }
Douglas Gregora082a492010-11-30 19:14:50 +00001196 return false;
1197 }
Douglas Gregora082a492010-11-30 19:14:50 +00001198
1199 // Check types.
1200 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001201 if (Context.Complain) {
1202 Context.Diag2(D2->getLocation(),
1203 diag::err_odr_non_type_parameter_type_inconsistent)
1204 << D2->getType() << D1->getType();
1205 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1206 << D1->getType();
1207 }
Douglas Gregora082a492010-11-30 19:14:50 +00001208 return false;
1209 }
1210
1211 return true;
1212}
1213
1214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1215 TemplateTemplateParmDecl *D1,
1216 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001217 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001218 if (Context.Complain) {
1219 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1220 << D2->isParameterPack();
1221 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1222 << D1->isParameterPack();
1223 }
Douglas Gregora082a492010-11-30 19:14:50 +00001224 return false;
1225 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001226
Douglas Gregora082a492010-11-30 19:14:50 +00001227 // Check template parameter lists.
1228 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1229 D2->getTemplateParameters());
1230}
1231
1232static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1233 ClassTemplateDecl *D1,
1234 ClassTemplateDecl *D2) {
1235 // Check template parameters.
1236 if (!IsStructurallyEquivalent(Context,
1237 D1->getTemplateParameters(),
1238 D2->getTemplateParameters()))
1239 return false;
1240
1241 // Check the templated declaration.
1242 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1243 D2->getTemplatedDecl());
1244}
1245
Douglas Gregor3996e242010-02-15 22:01:00 +00001246/// \brief Determine structural equivalence of two declarations.
1247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1248 Decl *D1, Decl *D2) {
1249 // FIXME: Check for known structural equivalences via a callback of some sort.
1250
Douglas Gregorb4964f72010-02-15 23:54:17 +00001251 // Check whether we already know that these two declarations are not
1252 // structurally equivalent.
1253 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1254 D2->getCanonicalDecl())))
1255 return false;
1256
Douglas Gregor3996e242010-02-15 22:01:00 +00001257 // Determine whether we've already produced a tentative equivalence for D1.
1258 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1259 if (EquivToD1)
1260 return EquivToD1 == D2->getCanonicalDecl();
1261
1262 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1263 EquivToD1 = D2->getCanonicalDecl();
1264 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1265 return true;
1266}
1267
1268bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1269 Decl *D2) {
1270 if (!::IsStructurallyEquivalent(*this, D1, D2))
1271 return false;
1272
1273 return !Finish();
1274}
1275
1276bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1277 QualType T2) {
1278 if (!::IsStructurallyEquivalent(*this, T1, T2))
1279 return false;
1280
1281 return !Finish();
1282}
1283
1284bool StructuralEquivalenceContext::Finish() {
1285 while (!DeclsToCheck.empty()) {
1286 // Check the next declaration.
1287 Decl *D1 = DeclsToCheck.front();
1288 DeclsToCheck.pop_front();
1289
1290 Decl *D2 = TentativeEquivalences[D1];
1291 assert(D2 && "Unrecorded tentative equivalence?");
1292
Douglas Gregorb4964f72010-02-15 23:54:17 +00001293 bool Equivalent = true;
1294
Douglas Gregor3996e242010-02-15 22:01:00 +00001295 // FIXME: Switch on all declaration kinds. For now, we're just going to
1296 // check the obvious ones.
1297 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1298 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1299 // Check for equivalent structure names.
1300 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001301 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1302 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001303 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001304 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1305 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001306 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1307 !::IsStructurallyEquivalent(*this, Record1, Record2))
1308 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001309 } else {
1310 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001311 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001312 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001313 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001314 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1315 // Check for equivalent enum names.
1316 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001317 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1318 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001319 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001320 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1321 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001322 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1323 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1324 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001325 } else {
1326 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001327 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001328 }
Richard Smithdda56e42011-04-15 14:24:37 +00001329 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1330 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001331 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001332 Typedef2->getIdentifier()) ||
1333 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001334 Typedef1->getUnderlyingType(),
1335 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001336 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001337 } else {
1338 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001339 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001340 }
Douglas Gregora082a492010-11-30 19:14:50 +00001341 } else if (ClassTemplateDecl *ClassTemplate1
1342 = dyn_cast<ClassTemplateDecl>(D1)) {
1343 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1344 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1345 ClassTemplate2->getIdentifier()) ||
1346 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1347 Equivalent = false;
1348 } else {
1349 // Class template/non-class-template mismatch.
1350 Equivalent = false;
1351 }
1352 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1353 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1354 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1355 Equivalent = false;
1356 } else {
1357 // Kind mismatch.
1358 Equivalent = false;
1359 }
1360 } else if (NonTypeTemplateParmDecl *NTTP1
1361 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1362 if (NonTypeTemplateParmDecl *NTTP2
1363 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1364 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1365 Equivalent = false;
1366 } else {
1367 // Kind mismatch.
1368 Equivalent = false;
1369 }
1370 } else if (TemplateTemplateParmDecl *TTP1
1371 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1372 if (TemplateTemplateParmDecl *TTP2
1373 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1374 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1375 Equivalent = false;
1376 } else {
1377 // Kind mismatch.
1378 Equivalent = false;
1379 }
1380 }
1381
Douglas Gregorb4964f72010-02-15 23:54:17 +00001382 if (!Equivalent) {
1383 // Note that these two declarations are not equivalent (and we already
1384 // know about it).
1385 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1386 D2->getCanonicalDecl()));
1387 return true;
1388 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001389 // FIXME: Check other declaration kinds!
1390 }
1391
1392 return false;
1393}
1394
1395//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001396// Import Types
1397//----------------------------------------------------------------------------
1398
John McCall424cec92011-01-19 06:33:43 +00001399QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001400 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1401 << T->getTypeClassName();
1402 return QualType();
1403}
1404
John McCall424cec92011-01-19 06:33:43 +00001405QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001406 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001407#define SHARED_SINGLETON_TYPE(Expansion)
1408#define BUILTIN_TYPE(Id, SingletonId) \
1409 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1410#include "clang/AST/BuiltinTypes.def"
1411
1412 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1413 // context supports C++.
1414
1415 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1416 // context supports ObjC.
1417
Douglas Gregor96e578d2010-02-05 17:54:41 +00001418 case BuiltinType::Char_U:
1419 // The context we're importing from has an unsigned 'char'. If we're
1420 // importing into a context with a signed 'char', translate to
1421 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001422 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001423 return Importer.getToContext().UnsignedCharTy;
1424
1425 return Importer.getToContext().CharTy;
1426
Douglas Gregor96e578d2010-02-05 17:54:41 +00001427 case BuiltinType::Char_S:
1428 // The context we're importing from has an unsigned 'char'. If we're
1429 // importing into a context with a signed 'char', translate to
1430 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001431 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001432 return Importer.getToContext().SignedCharTy;
1433
1434 return Importer.getToContext().CharTy;
1435
Chris Lattnerad3467e2010-12-25 23:25:43 +00001436 case BuiltinType::WChar_S:
1437 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001438 // FIXME: If not in C++, shall we translate to the C equivalent of
1439 // wchar_t?
1440 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001441 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001442
1443 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444}
1445
John McCall424cec92011-01-19 06:33:43 +00001446QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001447 QualType ToElementType = Importer.Import(T->getElementType());
1448 if (ToElementType.isNull())
1449 return QualType();
1450
1451 return Importer.getToContext().getComplexType(ToElementType);
1452}
1453
John McCall424cec92011-01-19 06:33:43 +00001454QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001455 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1456 if (ToPointeeType.isNull())
1457 return QualType();
1458
1459 return Importer.getToContext().getPointerType(ToPointeeType);
1460}
1461
John McCall424cec92011-01-19 06:33:43 +00001462QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001463 // FIXME: Check for blocks support in "to" context.
1464 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1465 if (ToPointeeType.isNull())
1466 return QualType();
1467
1468 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1469}
1470
John McCall424cec92011-01-19 06:33:43 +00001471QualType
1472ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001473 // FIXME: Check for C++ support in "to" context.
1474 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1475 if (ToPointeeType.isNull())
1476 return QualType();
1477
1478 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1479}
1480
John McCall424cec92011-01-19 06:33:43 +00001481QualType
1482ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 // FIXME: Check for C++0x support in "to" context.
1484 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1485 if (ToPointeeType.isNull())
1486 return QualType();
1487
1488 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1489}
1490
John McCall424cec92011-01-19 06:33:43 +00001491QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001492 // FIXME: Check for C++ support in "to" context.
1493 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1494 if (ToPointeeType.isNull())
1495 return QualType();
1496
1497 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1498 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1499 ClassType.getTypePtr());
1500}
1501
John McCall424cec92011-01-19 06:33:43 +00001502QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 QualType ToElementType = Importer.Import(T->getElementType());
1504 if (ToElementType.isNull())
1505 return QualType();
1506
1507 return Importer.getToContext().getConstantArrayType(ToElementType,
1508 T->getSize(),
1509 T->getSizeModifier(),
1510 T->getIndexTypeCVRQualifiers());
1511}
1512
John McCall424cec92011-01-19 06:33:43 +00001513QualType
1514ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 QualType ToElementType = Importer.Import(T->getElementType());
1516 if (ToElementType.isNull())
1517 return QualType();
1518
1519 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1520 T->getSizeModifier(),
1521 T->getIndexTypeCVRQualifiers());
1522}
1523
John McCall424cec92011-01-19 06:33:43 +00001524QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001525 QualType ToElementType = Importer.Import(T->getElementType());
1526 if (ToElementType.isNull())
1527 return QualType();
1528
1529 Expr *Size = Importer.Import(T->getSizeExpr());
1530 if (!Size)
1531 return QualType();
1532
1533 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1534 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1535 T->getSizeModifier(),
1536 T->getIndexTypeCVRQualifiers(),
1537 Brackets);
1538}
1539
John McCall424cec92011-01-19 06:33:43 +00001540QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001541 QualType ToElementType = Importer.Import(T->getElementType());
1542 if (ToElementType.isNull())
1543 return QualType();
1544
1545 return Importer.getToContext().getVectorType(ToElementType,
1546 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001547 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001548}
1549
John McCall424cec92011-01-19 06:33:43 +00001550QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001551 QualType ToElementType = Importer.Import(T->getElementType());
1552 if (ToElementType.isNull())
1553 return QualType();
1554
1555 return Importer.getToContext().getExtVectorType(ToElementType,
1556 T->getNumElements());
1557}
1558
John McCall424cec92011-01-19 06:33:43 +00001559QualType
1560ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001561 // FIXME: What happens if we're importing a function without a prototype
1562 // into C++? Should we make it variadic?
1563 QualType ToResultType = Importer.Import(T->getResultType());
1564 if (ToResultType.isNull())
1565 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001566
Douglas Gregor96e578d2010-02-05 17:54:41 +00001567 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001568 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001569}
1570
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001571QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001572 QualType ToResultType = Importer.Import(T->getResultType());
1573 if (ToResultType.isNull())
1574 return QualType();
1575
1576 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001577 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001578 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1579 AEnd = T->arg_type_end();
1580 A != AEnd; ++A) {
1581 QualType ArgType = Importer.Import(*A);
1582 if (ArgType.isNull())
1583 return QualType();
1584 ArgTypes.push_back(ArgType);
1585 }
1586
1587 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001588 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001589 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1590 EEnd = T->exception_end();
1591 E != EEnd; ++E) {
1592 QualType ExceptionType = Importer.Import(*E);
1593 if (ExceptionType.isNull())
1594 return QualType();
1595 ExceptionTypes.push_back(ExceptionType);
1596 }
John McCalldb40c7f2010-12-14 08:05:40 +00001597
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001598 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1599 FunctionProtoType::ExtProtoInfo ToEPI;
1600
1601 ToEPI.ExtInfo = FromEPI.ExtInfo;
1602 ToEPI.Variadic = FromEPI.Variadic;
1603 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1604 ToEPI.TypeQuals = FromEPI.TypeQuals;
1605 ToEPI.RefQualifier = FromEPI.RefQualifier;
1606 ToEPI.NumExceptions = ExceptionTypes.size();
1607 ToEPI.Exceptions = ExceptionTypes.data();
1608 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001609 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1610 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1611 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001612 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001613 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001614 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001615
Douglas Gregor96e578d2010-02-05 17:54:41 +00001616 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001617 ArgTypes.size(), ToEPI);
1618}
1619
Sean Callananda6df8a2011-08-11 16:56:07 +00001620QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1621 QualType ToInnerType = Importer.Import(T->getInnerType());
1622 if (ToInnerType.isNull())
1623 return QualType();
1624
1625 return Importer.getToContext().getParenType(ToInnerType);
1626}
1627
John McCall424cec92011-01-19 06:33:43 +00001628QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001629 TypedefNameDecl *ToDecl
1630 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001631 if (!ToDecl)
1632 return QualType();
1633
1634 return Importer.getToContext().getTypeDeclType(ToDecl);
1635}
1636
John McCall424cec92011-01-19 06:33:43 +00001637QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1639 if (!ToExpr)
1640 return QualType();
1641
1642 return Importer.getToContext().getTypeOfExprType(ToExpr);
1643}
1644
John McCall424cec92011-01-19 06:33:43 +00001645QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001646 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1647 if (ToUnderlyingType.isNull())
1648 return QualType();
1649
1650 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1651}
1652
John McCall424cec92011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001654 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001655 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1656 if (!ToExpr)
1657 return QualType();
1658
Douglas Gregor81495f32012-02-12 18:42:33 +00001659 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1660 if (UnderlyingType.isNull())
1661 return QualType();
1662
1663 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001664}
1665
Alexis Hunte852b102011-05-24 22:41:36 +00001666QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1667 QualType ToBaseType = Importer.Import(T->getBaseType());
1668 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1669 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1670 return QualType();
1671
1672 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1673 ToUnderlyingType,
1674 T->getUTTKind());
1675}
1676
Richard Smith30482bc2011-02-20 03:19:35 +00001677QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1678 // FIXME: Make sure that the "to" context supports C++0x!
1679 QualType FromDeduced = T->getDeducedType();
1680 QualType ToDeduced;
1681 if (!FromDeduced.isNull()) {
1682 ToDeduced = Importer.Import(FromDeduced);
1683 if (ToDeduced.isNull())
1684 return QualType();
1685 }
1686
1687 return Importer.getToContext().getAutoType(ToDeduced);
1688}
1689
John McCall424cec92011-01-19 06:33:43 +00001690QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001691 RecordDecl *ToDecl
1692 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1693 if (!ToDecl)
1694 return QualType();
1695
1696 return Importer.getToContext().getTagDeclType(ToDecl);
1697}
1698
John McCall424cec92011-01-19 06:33:43 +00001699QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001700 EnumDecl *ToDecl
1701 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1702 if (!ToDecl)
1703 return QualType();
1704
1705 return Importer.getToContext().getTagDeclType(ToDecl);
1706}
1707
Douglas Gregore2e50d332010-12-01 01:36:18 +00001708QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001709 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001710 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1711 if (ToTemplate.isNull())
1712 return QualType();
1713
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001714 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001715 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1716 return QualType();
1717
1718 QualType ToCanonType;
1719 if (!QualType(T, 0).isCanonical()) {
1720 QualType FromCanonType
1721 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1722 ToCanonType =Importer.Import(FromCanonType);
1723 if (ToCanonType.isNull())
1724 return QualType();
1725 }
1726 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1727 ToTemplateArgs.data(),
1728 ToTemplateArgs.size(),
1729 ToCanonType);
1730}
1731
John McCall424cec92011-01-19 06:33:43 +00001732QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001733 NestedNameSpecifier *ToQualifier = 0;
1734 // Note: the qualifier in an ElaboratedType is optional.
1735 if (T->getQualifier()) {
1736 ToQualifier = Importer.Import(T->getQualifier());
1737 if (!ToQualifier)
1738 return QualType();
1739 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001740
1741 QualType ToNamedType = Importer.Import(T->getNamedType());
1742 if (ToNamedType.isNull())
1743 return QualType();
1744
Abramo Bagnara6150c882010-05-11 21:36:43 +00001745 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1746 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001747}
1748
John McCall424cec92011-01-19 06:33:43 +00001749QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001750 ObjCInterfaceDecl *Class
1751 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1752 if (!Class)
1753 return QualType();
1754
John McCall8b07ec22010-05-15 11:32:37 +00001755 return Importer.getToContext().getObjCInterfaceType(Class);
1756}
1757
John McCall424cec92011-01-19 06:33:43 +00001758QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001759 QualType ToBaseType = Importer.Import(T->getBaseType());
1760 if (ToBaseType.isNull())
1761 return QualType();
1762
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001763 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001764 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765 PEnd = T->qual_end();
1766 P != PEnd; ++P) {
1767 ObjCProtocolDecl *Protocol
1768 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1769 if (!Protocol)
1770 return QualType();
1771 Protocols.push_back(Protocol);
1772 }
1773
John McCall8b07ec22010-05-15 11:32:37 +00001774 return Importer.getToContext().getObjCObjectType(ToBaseType,
1775 Protocols.data(),
1776 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001777}
1778
John McCall424cec92011-01-19 06:33:43 +00001779QualType
1780ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001781 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1782 if (ToPointeeType.isNull())
1783 return QualType();
1784
John McCall8b07ec22010-05-15 11:32:37 +00001785 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001786}
1787
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001788//----------------------------------------------------------------------------
1789// Import Declarations
1790//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001791bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1792 DeclContext *&LexicalDC,
1793 DeclarationName &Name,
1794 SourceLocation &Loc) {
1795 // Import the context of this declaration.
1796 DC = Importer.ImportContext(D->getDeclContext());
1797 if (!DC)
1798 return true;
1799
1800 LexicalDC = DC;
1801 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1802 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1803 if (!LexicalDC)
1804 return true;
1805 }
1806
1807 // Import the name of this declaration.
1808 Name = Importer.Import(D->getDeclName());
1809 if (D->getDeclName() && !Name)
1810 return true;
1811
1812 // Import the location of this declaration.
1813 Loc = Importer.Import(D->getLocation());
1814 return false;
1815}
1816
Douglas Gregord451ea92011-07-29 23:31:30 +00001817void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1818 if (!FromD)
1819 return;
1820
1821 if (!ToD) {
1822 ToD = Importer.Import(FromD);
1823 if (!ToD)
1824 return;
1825 }
1826
1827 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1828 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1829 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1830 ImportDefinition(FromRecord, ToRecord);
1831 }
1832 }
1833 return;
1834 }
1835
1836 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1837 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1838 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1839 ImportDefinition(FromEnum, ToEnum);
1840 }
1841 }
1842 return;
1843 }
1844}
1845
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001846void
1847ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1848 DeclarationNameInfo& To) {
1849 // NOTE: To.Name and To.Loc are already imported.
1850 // We only have to import To.LocInfo.
1851 switch (To.getName().getNameKind()) {
1852 case DeclarationName::Identifier:
1853 case DeclarationName::ObjCZeroArgSelector:
1854 case DeclarationName::ObjCOneArgSelector:
1855 case DeclarationName::ObjCMultiArgSelector:
1856 case DeclarationName::CXXUsingDirective:
1857 return;
1858
1859 case DeclarationName::CXXOperatorName: {
1860 SourceRange Range = From.getCXXOperatorNameRange();
1861 To.setCXXOperatorNameRange(Importer.Import(Range));
1862 return;
1863 }
1864 case DeclarationName::CXXLiteralOperatorName: {
1865 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1866 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1867 return;
1868 }
1869 case DeclarationName::CXXConstructorName:
1870 case DeclarationName::CXXDestructorName:
1871 case DeclarationName::CXXConversionFunctionName: {
1872 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1873 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1874 return;
1875 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001876 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001877 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001878}
1879
Douglas Gregor2e15c842012-02-01 21:00:38 +00001880void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001881 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001882 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001883 return;
1884 }
1885
Douglas Gregor968d6332010-02-21 18:24:45 +00001886 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1887 FromEnd = FromDC->decls_end();
1888 From != FromEnd;
1889 ++From)
1890 Importer.Import(*From);
1891}
1892
Douglas Gregord451ea92011-07-29 23:31:30 +00001893bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001894 ImportDefinitionKind Kind) {
1895 if (To->getDefinition() || To->isBeingDefined()) {
1896 if (Kind == IDK_Everything)
1897 ImportDeclContext(From, /*ForceImport=*/true);
1898
Douglas Gregore2e50d332010-12-01 01:36:18 +00001899 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001900 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001901
1902 To->startDefinition();
1903
1904 // Add base classes.
1905 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1906 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001907
1908 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1909 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1910 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1911 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1912 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1913 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1914 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1915 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1916 ToData.Aggregate = FromData.Aggregate;
1917 ToData.PlainOldData = FromData.PlainOldData;
1918 ToData.Empty = FromData.Empty;
1919 ToData.Polymorphic = FromData.Polymorphic;
1920 ToData.Abstract = FromData.Abstract;
1921 ToData.IsStandardLayout = FromData.IsStandardLayout;
1922 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1923 ToData.HasPrivateFields = FromData.HasPrivateFields;
1924 ToData.HasProtectedFields = FromData.HasProtectedFields;
1925 ToData.HasPublicFields = FromData.HasPublicFields;
1926 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001927 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001928 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001929 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1930 ToData.HasConstexprNonCopyMoveConstructor
1931 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001932 ToData.DefaultedDefaultConstructorIsConstexpr
1933 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001934 ToData.HasConstexprDefaultConstructor
1935 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001936 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1937 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1938 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1939 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1940 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001941 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001942 ToData.HasNonLiteralTypeFieldsOrBases
1943 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001944 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001945 ToData.UserProvidedDefaultConstructor
1946 = FromData.UserProvidedDefaultConstructor;
1947 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1948 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1949 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1950 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1951 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1952 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1953 ToData.FailedImplicitMoveConstructor
1954 = FromData.FailedImplicitMoveConstructor;
1955 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001956 ToData.IsLambda = FromData.IsLambda;
1957
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001958 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001959 for (CXXRecordDecl::base_class_iterator
1960 Base1 = FromCXX->bases_begin(),
1961 FromBaseEnd = FromCXX->bases_end();
1962 Base1 != FromBaseEnd;
1963 ++Base1) {
1964 QualType T = Importer.Import(Base1->getType());
1965 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001966 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001967
1968 SourceLocation EllipsisLoc;
1969 if (Base1->isPackExpansion())
1970 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001971
1972 // Ensure that we have a definition for the base.
1973 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1974
Douglas Gregore2e50d332010-12-01 01:36:18 +00001975 Bases.push_back(
1976 new (Importer.getToContext())
1977 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1978 Base1->isVirtual(),
1979 Base1->isBaseOfClass(),
1980 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001981 Importer.Import(Base1->getTypeSourceInfo()),
1982 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001983 }
1984 if (!Bases.empty())
1985 ToCXX->setBases(Bases.data(), Bases.size());
1986 }
1987
Douglas Gregor2e15c842012-02-01 21:00:38 +00001988 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001989 ImportDeclContext(From, /*ForceImport=*/true);
1990
Douglas Gregore2e50d332010-12-01 01:36:18 +00001991 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001992 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001993}
1994
Douglas Gregord451ea92011-07-29 23:31:30 +00001995bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001996 ImportDefinitionKind Kind) {
1997 if (To->getDefinition() || To->isBeingDefined()) {
1998 if (Kind == IDK_Everything)
1999 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002000 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002001 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002002
2003 To->startDefinition();
2004
2005 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2006 if (T.isNull())
2007 return true;
2008
2009 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2010 if (ToPromotionType.isNull())
2011 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002012
2013 if (shouldForceImportDeclContext(Kind))
2014 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002015
2016 // FIXME: we might need to merge the number of positive or negative bits
2017 // if the enumerator lists don't match.
2018 To->completeDefinition(T, ToPromotionType,
2019 From->getNumPositiveBits(),
2020 From->getNumNegativeBits());
2021 return false;
2022}
2023
Douglas Gregora082a492010-11-30 19:14:50 +00002024TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2025 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002026 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002027 ToParams.reserve(Params->size());
2028 for (TemplateParameterList::iterator P = Params->begin(),
2029 PEnd = Params->end();
2030 P != PEnd; ++P) {
2031 Decl *To = Importer.Import(*P);
2032 if (!To)
2033 return 0;
2034
2035 ToParams.push_back(cast<NamedDecl>(To));
2036 }
2037
2038 return TemplateParameterList::Create(Importer.getToContext(),
2039 Importer.Import(Params->getTemplateLoc()),
2040 Importer.Import(Params->getLAngleLoc()),
2041 ToParams.data(), ToParams.size(),
2042 Importer.Import(Params->getRAngleLoc()));
2043}
2044
Douglas Gregore2e50d332010-12-01 01:36:18 +00002045TemplateArgument
2046ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2047 switch (From.getKind()) {
2048 case TemplateArgument::Null:
2049 return TemplateArgument();
2050
2051 case TemplateArgument::Type: {
2052 QualType ToType = Importer.Import(From.getAsType());
2053 if (ToType.isNull())
2054 return TemplateArgument();
2055 return TemplateArgument(ToType);
2056 }
2057
2058 case TemplateArgument::Integral: {
2059 QualType ToType = Importer.Import(From.getIntegralType());
2060 if (ToType.isNull())
2061 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002062 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002063 }
2064
Eli Friedmanb826a002012-09-26 02:36:12 +00002065 case TemplateArgument::Declaration: {
2066 ValueDecl *FromD = From.getAsDecl();
2067 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2068 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002069 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002070 }
2071
2072 case TemplateArgument::NullPtr: {
2073 QualType ToType = Importer.Import(From.getNullPtrType());
2074 if (ToType.isNull())
2075 return TemplateArgument();
2076 return TemplateArgument(ToType, /*isNullPtr*/true);
2077 }
2078
Douglas Gregore2e50d332010-12-01 01:36:18 +00002079 case TemplateArgument::Template: {
2080 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2081 if (ToTemplate.isNull())
2082 return TemplateArgument();
2083
2084 return TemplateArgument(ToTemplate);
2085 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002086
2087 case TemplateArgument::TemplateExpansion: {
2088 TemplateName ToTemplate
2089 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2090 if (ToTemplate.isNull())
2091 return TemplateArgument();
2092
Douglas Gregore1d60df2011-01-14 23:41:42 +00002093 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002094 }
2095
Douglas Gregore2e50d332010-12-01 01:36:18 +00002096 case TemplateArgument::Expression:
2097 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2098 return TemplateArgument(ToExpr);
2099 return TemplateArgument();
2100
2101 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002102 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002103 ToPack.reserve(From.pack_size());
2104 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2105 return TemplateArgument();
2106
2107 TemplateArgument *ToArgs
2108 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2109 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2110 return TemplateArgument(ToArgs, ToPack.size());
2111 }
2112 }
2113
2114 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002115}
2116
2117bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2118 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002119 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002120 for (unsigned I = 0; I != NumFromArgs; ++I) {
2121 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2122 if (To.isNull() && !FromArgs[I].isNull())
2123 return true;
2124
2125 ToArgs.push_back(To);
2126 }
2127
2128 return false;
2129}
2130
Douglas Gregor5c73e912010-02-11 00:48:18 +00002131bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002132 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002133 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002134 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002135 Importer.getNonEquivalentDecls(),
2136 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002137 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002138}
2139
Douglas Gregor98c10182010-02-12 22:17:39 +00002140bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002141 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002142 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002143 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002144 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002145}
2146
Douglas Gregor91155082012-11-14 22:29:20 +00002147bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2148 EnumConstantDecl *ToEC)
2149{
2150 const llvm::APSInt &FromVal = FromEC->getInitVal();
2151 const llvm::APSInt &ToVal = ToEC->getInitVal();
2152
2153 return FromVal.isSigned() == ToVal.isSigned() &&
2154 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2155 FromVal == ToVal;
2156}
2157
2158bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002159 ClassTemplateDecl *To) {
2160 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2161 Importer.getToContext(),
2162 Importer.getNonEquivalentDecls());
2163 return Ctx.IsStructurallyEquivalent(From, To);
2164}
2165
Douglas Gregore4c83e42010-02-09 22:48:33 +00002166Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002167 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002168 << D->getDeclKindName();
2169 return 0;
2170}
2171
Sean Callanan65198272011-11-17 23:20:56 +00002172Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2173 TranslationUnitDecl *ToD =
2174 Importer.getToContext().getTranslationUnitDecl();
2175
2176 Importer.Imported(D, ToD);
2177
2178 return ToD;
2179}
2180
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002181Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2182 // Import the major distinguishing characteristics of this namespace.
2183 DeclContext *DC, *LexicalDC;
2184 DeclarationName Name;
2185 SourceLocation Loc;
2186 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2187 return 0;
2188
2189 NamespaceDecl *MergeWithNamespace = 0;
2190 if (!Name) {
2191 // This is an anonymous namespace. Adopt an existing anonymous
2192 // namespace if we can.
2193 // FIXME: Not testable.
2194 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2195 MergeWithNamespace = TU->getAnonymousNamespace();
2196 else
2197 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2198 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002199 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002200 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2201 DC->localUncachedLookup(Name, FoundDecls);
2202 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2203 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002204 continue;
2205
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002206 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002207 MergeWithNamespace = FoundNS;
2208 ConflictingDecls.clear();
2209 break;
2210 }
2211
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002212 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002213 }
2214
2215 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002216 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002217 ConflictingDecls.data(),
2218 ConflictingDecls.size());
2219 }
2220 }
2221
2222 // Create the "to" namespace, if needed.
2223 NamespaceDecl *ToNamespace = MergeWithNamespace;
2224 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002225 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002226 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002227 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002228 Loc, Name.getAsIdentifierInfo(),
2229 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002230 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002231 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002232
2233 // If this is an anonymous namespace, register it as the anonymous
2234 // namespace within its context.
2235 if (!Name) {
2236 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2237 TU->setAnonymousNamespace(ToNamespace);
2238 else
2239 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2240 }
2241 }
2242 Importer.Imported(D, ToNamespace);
2243
2244 ImportDeclContext(D);
2245
2246 return ToNamespace;
2247}
2248
Richard Smithdda56e42011-04-15 14:24:37 +00002249Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002250 // Import the major distinguishing characteristics of this typedef.
2251 DeclContext *DC, *LexicalDC;
2252 DeclarationName Name;
2253 SourceLocation Loc;
2254 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2255 return 0;
2256
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002257 // If this typedef is not in block scope, determine whether we've
2258 // seen a typedef with the same name (that we can merge with) or any
2259 // other entity by that name (which name lookup could conflict with).
2260 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002261 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002262 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002263 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2264 DC->localUncachedLookup(Name, FoundDecls);
2265 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2266 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002267 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002268 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002269 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002270 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2271 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002272 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002273 }
2274
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002275 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002276 }
2277
2278 if (!ConflictingDecls.empty()) {
2279 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2280 ConflictingDecls.data(),
2281 ConflictingDecls.size());
2282 if (!Name)
2283 return 0;
2284 }
2285 }
2286
Douglas Gregorb4964f72010-02-15 23:54:17 +00002287 // Import the underlying type of this typedef;
2288 QualType T = Importer.Import(D->getUnderlyingType());
2289 if (T.isNull())
2290 return 0;
2291
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002292 // Create the new typedef node.
2293 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002294 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002295 TypedefNameDecl *ToTypedef;
2296 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002297 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2298 StartL, Loc,
2299 Name.getAsIdentifierInfo(),
2300 TInfo);
2301 else
Richard Smithdda56e42011-04-15 14:24:37 +00002302 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2303 StartL, Loc,
2304 Name.getAsIdentifierInfo(),
2305 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002306
Douglas Gregordd483172010-02-22 17:42:47 +00002307 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002308 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002309 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002310 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002311
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002312 return ToTypedef;
2313}
2314
Richard Smithdda56e42011-04-15 14:24:37 +00002315Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2316 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2317}
2318
2319Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2320 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2321}
2322
Douglas Gregor98c10182010-02-12 22:17:39 +00002323Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2324 // Import the major distinguishing characteristics of this enum.
2325 DeclContext *DC, *LexicalDC;
2326 DeclarationName Name;
2327 SourceLocation Loc;
2328 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2329 return 0;
2330
2331 // Figure out what enum name we're looking for.
2332 unsigned IDNS = Decl::IDNS_Tag;
2333 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002334 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2335 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002336 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002337 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002338 IDNS |= Decl::IDNS_Ordinary;
2339
2340 // We may already have an enum of the same name; try to find and match it.
2341 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002342 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002343 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2344 DC->localUncachedLookup(SearchName, FoundDecls);
2345 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2346 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002347 continue;
2348
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002349 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002350 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002351 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2352 Found = Tag->getDecl();
2353 }
2354
2355 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002356 if (IsStructuralMatch(D, FoundEnum))
2357 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002358 }
2359
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002360 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002361 }
2362
2363 if (!ConflictingDecls.empty()) {
2364 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2365 ConflictingDecls.data(),
2366 ConflictingDecls.size());
2367 }
2368 }
2369
2370 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002371 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2372 Importer.Import(D->getLocStart()),
2373 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002374 D->isScoped(), D->isScopedUsingClassTag(),
2375 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002376 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002377 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002378 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002379 D2->setLexicalDeclContext(LexicalDC);
2380 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002381 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002382
2383 // Import the integer type.
2384 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2385 if (ToIntegerType.isNull())
2386 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002387 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002388
2389 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002390 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002391 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002392
Douglas Gregor3996e242010-02-15 22:01:00 +00002393 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002394}
2395
Douglas Gregor5c73e912010-02-11 00:48:18 +00002396Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2397 // If this record has a definition in the translation unit we're coming from,
2398 // but this particular declaration is not that definition, import the
2399 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002400 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002401 if (Definition && Definition != D) {
2402 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002403 if (!ImportedDef)
2404 return 0;
2405
2406 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002407 }
2408
2409 // Import the major distinguishing characteristics of this record.
2410 DeclContext *DC, *LexicalDC;
2411 DeclarationName Name;
2412 SourceLocation Loc;
2413 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2414 return 0;
2415
2416 // Figure out what structure name we're looking for.
2417 unsigned IDNS = Decl::IDNS_Tag;
2418 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002419 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2420 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002421 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002422 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002423 IDNS |= Decl::IDNS_Ordinary;
2424
2425 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002426 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002427 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002428 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002429 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2430 DC->localUncachedLookup(SearchName, FoundDecls);
2431 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2432 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002433 continue;
2434
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002435 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002436 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002437 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2438 Found = Tag->getDecl();
2439 }
2440
2441 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002442 if (D->isAnonymousStructOrUnion() &&
2443 FoundRecord->isAnonymousStructOrUnion()) {
2444 // If both anonymous structs/unions are in a record context, make sure
2445 // they occur in the same location in the context records.
2446 if (llvm::Optional<unsigned> Index1
2447 = findAnonymousStructOrUnionIndex(D)) {
2448 if (llvm::Optional<unsigned> Index2
2449 = findAnonymousStructOrUnionIndex(FoundRecord)) {
2450 if (*Index1 != *Index2)
2451 continue;
2452 }
2453 }
2454 }
2455
Douglas Gregor25791052010-02-12 00:09:27 +00002456 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002457 if ((SearchName && !D->isCompleteDefinition())
2458 || (D->isCompleteDefinition() &&
2459 D->isAnonymousStructOrUnion()
2460 == FoundDef->isAnonymousStructOrUnion() &&
2461 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002462 // The record types structurally match, or the "from" translation
2463 // unit only had a forward declaration anyway; call it the same
2464 // function.
2465 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002466 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002467 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002468 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002469 // We have a forward declaration of this type, so adopt that forward
2470 // declaration rather than building a new one.
2471 AdoptDecl = FoundRecord;
2472 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002473 } else if (!SearchName) {
2474 continue;
2475 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002476 }
2477
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002478 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002479 }
2480
Douglas Gregordd6006f2012-07-17 21:16:27 +00002481 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002482 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2483 ConflictingDecls.data(),
2484 ConflictingDecls.size());
2485 }
2486 }
2487
2488 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002489 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002490 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002491 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002492 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002493 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002494 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002495 DC, StartLoc, Loc,
2496 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002497 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002498 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002499 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002500 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002501 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002502 }
Douglas Gregor14454802011-02-25 02:25:35 +00002503
2504 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002505 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002506 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002507 if (D->isAnonymousStructOrUnion())
2508 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002509 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002510
Douglas Gregor3996e242010-02-15 22:01:00 +00002511 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002512
Douglas Gregor95d82832012-01-24 18:36:04 +00002513 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002514 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002515
Douglas Gregor3996e242010-02-15 22:01:00 +00002516 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002517}
2518
Douglas Gregor98c10182010-02-12 22:17:39 +00002519Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2520 // Import the major distinguishing characteristics of this enumerator.
2521 DeclContext *DC, *LexicalDC;
2522 DeclarationName Name;
2523 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002524 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002525 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002526
2527 QualType T = Importer.Import(D->getType());
2528 if (T.isNull())
2529 return 0;
2530
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 // Determine whether there are any other declarations with the same name and
2532 // in the same context.
2533 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002534 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002535 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002536 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2537 DC->localUncachedLookup(Name, FoundDecls);
2538 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2539 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002540 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002541
2542 if (EnumConstantDecl *FoundEnumConstant
2543 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2544 if (IsStructuralMatch(D, FoundEnumConstant))
2545 return Importer.Imported(D, FoundEnumConstant);
2546 }
2547
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002548 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002549 }
2550
2551 if (!ConflictingDecls.empty()) {
2552 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2553 ConflictingDecls.data(),
2554 ConflictingDecls.size());
2555 if (!Name)
2556 return 0;
2557 }
2558 }
2559
2560 Expr *Init = Importer.Import(D->getInitExpr());
2561 if (D->getInitExpr() && !Init)
2562 return 0;
2563
2564 EnumConstantDecl *ToEnumerator
2565 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2566 Name.getAsIdentifierInfo(), T,
2567 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002568 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002569 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002570 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002571 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002572 return ToEnumerator;
2573}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002574
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002575Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2576 // Import the major distinguishing characteristics of this function.
2577 DeclContext *DC, *LexicalDC;
2578 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002579 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002580 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002581 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002582
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002583 // Try to find a function in our own ("to") context with the same name, same
2584 // type, and in the same context as the function we're importing.
2585 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002586 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002587 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002588 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2589 DC->localUncachedLookup(Name, FoundDecls);
2590 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2591 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002592 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002593
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002594 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002595 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2596 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002597 if (Importer.IsStructurallyEquivalent(D->getType(),
2598 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002599 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002600 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002601 }
2602
2603 // FIXME: Check for overloading more carefully, e.g., by boosting
2604 // Sema::IsOverload out to the AST library.
2605
2606 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002607 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002608 continue;
2609
2610 // Complain about inconsistent function types.
2611 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002612 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002613 Importer.ToDiag(FoundFunction->getLocation(),
2614 diag::note_odr_value_here)
2615 << FoundFunction->getType();
2616 }
2617 }
2618
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002619 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002620 }
2621
2622 if (!ConflictingDecls.empty()) {
2623 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2624 ConflictingDecls.data(),
2625 ConflictingDecls.size());
2626 if (!Name)
2627 return 0;
2628 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002629 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002630
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002631 DeclarationNameInfo NameInfo(Name, Loc);
2632 // Import additional name location/type info.
2633 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2634
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002635 QualType FromTy = D->getType();
2636 bool usedDifferentExceptionSpec = false;
2637
2638 if (const FunctionProtoType *
2639 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2640 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2641 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2642 // FunctionDecl that we are importing the FunctionProtoType for.
2643 // To avoid an infinite recursion when importing, create the FunctionDecl
2644 // with a simplified function type and update it afterwards.
2645 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2646 FromEPI.NoexceptExpr) {
2647 FunctionProtoType::ExtProtoInfo DefaultEPI;
2648 FromTy = Importer.getFromContext().getFunctionType(
2649 FromFPT->getResultType(),
2650 FromFPT->arg_type_begin(),
2651 FromFPT->arg_type_end() - FromFPT->arg_type_begin(),
2652 DefaultEPI);
2653 usedDifferentExceptionSpec = true;
2654 }
2655 }
2656
Douglas Gregorb4964f72010-02-15 23:54:17 +00002657 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002658 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002659 if (T.isNull())
2660 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002661
2662 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002663 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002664 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2665 P != PEnd; ++P) {
2666 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2667 if (!ToP)
2668 return 0;
2669
2670 Parameters.push_back(ToP);
2671 }
2672
2673 // Create the imported function.
2674 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002675 FunctionDecl *ToFunction = 0;
2676 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2677 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2678 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002679 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002680 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002681 FromConstructor->isExplicit(),
2682 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002683 D->isImplicit(),
2684 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002685 } else if (isa<CXXDestructorDecl>(D)) {
2686 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2687 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002688 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002689 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002690 D->isInlineSpecified(),
2691 D->isImplicit());
2692 } else if (CXXConversionDecl *FromConversion
2693 = dyn_cast<CXXConversionDecl>(D)) {
2694 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2695 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002696 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002697 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002698 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002699 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002700 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002701 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002702 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2703 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2704 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002705 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002706 NameInfo, T, TInfo,
2707 Method->isStatic(),
2708 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002709 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002710 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002711 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002712 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002713 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002714 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002715 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002716 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002717 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002718 D->hasWrittenPrototype(),
2719 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002720 }
John McCall3e11ebe2010-03-15 10:12:16 +00002721
2722 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002723 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002724 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002725 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002726 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2727 ToFunction->setTrivial(D->isTrivial());
2728 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002729 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002730
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002731 // Set the parameters.
2732 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002733 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002734 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002735 }
David Blaikie9c70e042011-09-21 18:16:56 +00002736 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002737
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002738 if (usedDifferentExceptionSpec) {
2739 // Update FunctionProtoType::ExtProtoInfo.
2740 QualType T = Importer.Import(D->getType());
2741 if (T.isNull())
2742 return 0;
2743 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002744 }
2745
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002746 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002747
2748 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002749 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002750
Douglas Gregor43f54792010-02-17 02:12:47 +00002751 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002752}
2753
Douglas Gregor00eace12010-02-21 18:29:16 +00002754Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2755 return VisitFunctionDecl(D);
2756}
2757
2758Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2759 return VisitCXXMethodDecl(D);
2760}
2761
2762Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2763 return VisitCXXMethodDecl(D);
2764}
2765
2766Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2767 return VisitCXXMethodDecl(D);
2768}
2769
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002770static unsigned getFieldIndex(Decl *F) {
2771 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2772 if (!Owner)
2773 return 0;
2774
2775 unsigned Index = 1;
2776 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
2777 DEnd = Owner->noload_decls_end();
2778 D != DEnd; ++D) {
2779 if (*D == F)
2780 return Index;
2781
2782 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2783 ++Index;
2784 }
2785
2786 return Index;
2787}
2788
Douglas Gregor5c73e912010-02-11 00:48:18 +00002789Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2790 // Import the major distinguishing characteristics of a variable.
2791 DeclContext *DC, *LexicalDC;
2792 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002793 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002794 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2795 return 0;
2796
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002797 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002798 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2799 DC->localUncachedLookup(Name, FoundDecls);
2800 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2801 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002802 // For anonymous fields, match up by index.
2803 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2804 continue;
2805
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002806 if (Importer.IsStructurallyEquivalent(D->getType(),
2807 FoundField->getType())) {
2808 Importer.Imported(D, FoundField);
2809 return FoundField;
2810 }
2811
2812 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2813 << Name << D->getType() << FoundField->getType();
2814 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2815 << FoundField->getType();
2816 return 0;
2817 }
2818 }
2819
Douglas Gregorb4964f72010-02-15 23:54:17 +00002820 // Import the type.
2821 QualType T = Importer.Import(D->getType());
2822 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002823 return 0;
2824
2825 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2826 Expr *BitWidth = Importer.Import(D->getBitWidth());
2827 if (!BitWidth && D->getBitWidth())
2828 return 0;
2829
Abramo Bagnaradff19302011-03-08 08:55:46 +00002830 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2831 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002832 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002833 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002834 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002835 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002836 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002837 if (ToField->hasInClassInitializer())
2838 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002839 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002840 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002841 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002842 return ToField;
2843}
2844
Francois Pichet783dd6e2010-11-21 06:08:52 +00002845Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2846 // Import the major distinguishing characteristics of a variable.
2847 DeclContext *DC, *LexicalDC;
2848 DeclarationName Name;
2849 SourceLocation Loc;
2850 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2851 return 0;
2852
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002853 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002854 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2855 DC->localUncachedLookup(Name, FoundDecls);
2856 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002857 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002858 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002859 // For anonymous indirect fields, match up by index.
2860 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2861 continue;
2862
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002863 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002864 FoundField->getType(),
2865 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002866 Importer.Imported(D, FoundField);
2867 return FoundField;
2868 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002869
2870 // If there are more anonymous fields to check, continue.
2871 if (!Name && I < N-1)
2872 continue;
2873
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002874 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2875 << Name << D->getType() << FoundField->getType();
2876 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2877 << FoundField->getType();
2878 return 0;
2879 }
2880 }
2881
Francois Pichet783dd6e2010-11-21 06:08:52 +00002882 // Import the type.
2883 QualType T = Importer.Import(D->getType());
2884 if (T.isNull())
2885 return 0;
2886
2887 NamedDecl **NamedChain =
2888 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2889
2890 unsigned i = 0;
2891 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2892 PE = D->chain_end(); PI != PE; ++PI) {
2893 Decl* D = Importer.Import(*PI);
2894 if (!D)
2895 return 0;
2896 NamedChain[i++] = cast<NamedDecl>(D);
2897 }
2898
2899 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2900 Importer.getToContext(), DC,
2901 Loc, Name.getAsIdentifierInfo(), T,
2902 NamedChain, D->getChainingSize());
2903 ToIndirectField->setAccess(D->getAccess());
2904 ToIndirectField->setLexicalDeclContext(LexicalDC);
2905 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002906 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002907 return ToIndirectField;
2908}
2909
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002910Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2911 // Import the major distinguishing characteristics of an ivar.
2912 DeclContext *DC, *LexicalDC;
2913 DeclarationName Name;
2914 SourceLocation Loc;
2915 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2916 return 0;
2917
2918 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002919 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2920 DC->localUncachedLookup(Name, FoundDecls);
2921 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2922 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002923 if (Importer.IsStructurallyEquivalent(D->getType(),
2924 FoundIvar->getType())) {
2925 Importer.Imported(D, FoundIvar);
2926 return FoundIvar;
2927 }
2928
2929 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2930 << Name << D->getType() << FoundIvar->getType();
2931 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2932 << FoundIvar->getType();
2933 return 0;
2934 }
2935 }
2936
2937 // Import the type.
2938 QualType T = Importer.Import(D->getType());
2939 if (T.isNull())
2940 return 0;
2941
2942 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2943 Expr *BitWidth = Importer.Import(D->getBitWidth());
2944 if (!BitWidth && D->getBitWidth())
2945 return 0;
2946
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002947 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2948 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002949 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002950 Loc, Name.getAsIdentifierInfo(),
2951 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002952 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002953 ToIvar->setLexicalDeclContext(LexicalDC);
2954 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002955 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002956 return ToIvar;
2957
2958}
2959
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002960Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2961 // Import the major distinguishing characteristics of a variable.
2962 DeclContext *DC, *LexicalDC;
2963 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002964 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002965 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002966 return 0;
2967
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002968 // Try to find a variable in our own ("to") context with the same name and
2969 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002970 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002971 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002972 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002973 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002974 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2975 DC->localUncachedLookup(Name, FoundDecls);
2976 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2977 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002978 continue;
2979
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002980 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002981 // We have found a variable that we may need to merge with. Check it.
2982 if (isExternalLinkage(FoundVar->getLinkage()) &&
2983 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002984 if (Importer.IsStructurallyEquivalent(D->getType(),
2985 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002986 MergeWithVar = FoundVar;
2987 break;
2988 }
2989
Douglas Gregor56521c52010-02-12 17:23:39 +00002990 const ArrayType *FoundArray
2991 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2992 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002993 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002994 if (FoundArray && TArray) {
2995 if (isa<IncompleteArrayType>(FoundArray) &&
2996 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002997 // Import the type.
2998 QualType T = Importer.Import(D->getType());
2999 if (T.isNull())
3000 return 0;
3001
Douglas Gregor56521c52010-02-12 17:23:39 +00003002 FoundVar->setType(T);
3003 MergeWithVar = FoundVar;
3004 break;
3005 } else if (isa<IncompleteArrayType>(TArray) &&
3006 isa<ConstantArrayType>(FoundArray)) {
3007 MergeWithVar = FoundVar;
3008 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003009 }
3010 }
3011
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003012 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003013 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003014 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3015 << FoundVar->getType();
3016 }
3017 }
3018
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003019 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003020 }
3021
3022 if (MergeWithVar) {
3023 // An equivalent variable with external linkage has been found. Link
3024 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003025 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003026
3027 if (VarDecl *DDef = D->getDefinition()) {
3028 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3029 Importer.ToDiag(ExistingDef->getLocation(),
3030 diag::err_odr_variable_multiple_def)
3031 << Name;
3032 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3033 } else {
3034 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003035 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003036 if (DDef->isInitKnownICE()) {
3037 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3038 Eval->CheckedICE = true;
3039 Eval->IsICE = DDef->isInitICE();
3040 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003041 }
3042 }
3043
3044 return MergeWithVar;
3045 }
3046
3047 if (!ConflictingDecls.empty()) {
3048 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3049 ConflictingDecls.data(),
3050 ConflictingDecls.size());
3051 if (!Name)
3052 return 0;
3053 }
3054 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003055
Douglas Gregorb4964f72010-02-15 23:54:17 +00003056 // Import the type.
3057 QualType T = Importer.Import(D->getType());
3058 if (T.isNull())
3059 return 0;
3060
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003061 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003062 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003063 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3064 Importer.Import(D->getInnerLocStart()),
3065 Loc, Name.getAsIdentifierInfo(),
3066 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00003067 D->getStorageClass(),
3068 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00003069 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003070 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003071 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003072 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003073 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003074
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003075 // Merge the initializer.
3076 // FIXME: Can we really import any initializer? Alternatively, we could force
3077 // ourselves to import every declaration of a variable and then only use
3078 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00003079 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003080
3081 // FIXME: Other bits to merge?
3082
3083 return ToVar;
3084}
3085
Douglas Gregor8b228d72010-02-17 21:22:52 +00003086Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3087 // Parameters are created in the translation unit's context, then moved
3088 // into the function declaration's context afterward.
3089 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3090
3091 // Import the name of this declaration.
3092 DeclarationName Name = Importer.Import(D->getDeclName());
3093 if (D->getDeclName() && !Name)
3094 return 0;
3095
3096 // Import the location of this declaration.
3097 SourceLocation Loc = Importer.Import(D->getLocation());
3098
3099 // Import the parameter's type.
3100 QualType T = Importer.Import(D->getType());
3101 if (T.isNull())
3102 return 0;
3103
3104 // Create the imported parameter.
3105 ImplicitParamDecl *ToParm
3106 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3107 Loc, Name.getAsIdentifierInfo(),
3108 T);
3109 return Importer.Imported(D, ToParm);
3110}
3111
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003112Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3113 // Parameters are created in the translation unit's context, then moved
3114 // into the function declaration's context afterward.
3115 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3116
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003117 // Import the name of this declaration.
3118 DeclarationName Name = Importer.Import(D->getDeclName());
3119 if (D->getDeclName() && !Name)
3120 return 0;
3121
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003122 // Import the location of this declaration.
3123 SourceLocation Loc = Importer.Import(D->getLocation());
3124
3125 // Import the parameter's type.
3126 QualType T = Importer.Import(D->getType());
3127 if (T.isNull())
3128 return 0;
3129
3130 // Create the imported parameter.
3131 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3132 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003133 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003134 Loc, Name.getAsIdentifierInfo(),
3135 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003136 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003137 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00003138 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003139 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003140}
3141
Douglas Gregor43f54792010-02-17 02:12:47 +00003142Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3143 // Import the major distinguishing characteristics of a method.
3144 DeclContext *DC, *LexicalDC;
3145 DeclarationName Name;
3146 SourceLocation Loc;
3147 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3148 return 0;
3149
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003150 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3151 DC->localUncachedLookup(Name, FoundDecls);
3152 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3153 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003154 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3155 continue;
3156
3157 // Check return types.
3158 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3159 FoundMethod->getResultType())) {
3160 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3161 << D->isInstanceMethod() << Name
3162 << D->getResultType() << FoundMethod->getResultType();
3163 Importer.ToDiag(FoundMethod->getLocation(),
3164 diag::note_odr_objc_method_here)
3165 << D->isInstanceMethod() << Name;
3166 return 0;
3167 }
3168
3169 // Check the number of parameters.
3170 if (D->param_size() != FoundMethod->param_size()) {
3171 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3172 << D->isInstanceMethod() << Name
3173 << D->param_size() << FoundMethod->param_size();
3174 Importer.ToDiag(FoundMethod->getLocation(),
3175 diag::note_odr_objc_method_here)
3176 << D->isInstanceMethod() << Name;
3177 return 0;
3178 }
3179
3180 // Check parameter types.
3181 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3182 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3183 P != PEnd; ++P, ++FoundP) {
3184 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3185 (*FoundP)->getType())) {
3186 Importer.FromDiag((*P)->getLocation(),
3187 diag::err_odr_objc_method_param_type_inconsistent)
3188 << D->isInstanceMethod() << Name
3189 << (*P)->getType() << (*FoundP)->getType();
3190 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3191 << (*FoundP)->getType();
3192 return 0;
3193 }
3194 }
3195
3196 // Check variadic/non-variadic.
3197 // Check the number of parameters.
3198 if (D->isVariadic() != FoundMethod->isVariadic()) {
3199 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3200 << D->isInstanceMethod() << Name;
3201 Importer.ToDiag(FoundMethod->getLocation(),
3202 diag::note_odr_objc_method_here)
3203 << D->isInstanceMethod() << Name;
3204 return 0;
3205 }
3206
3207 // FIXME: Any other bits we need to merge?
3208 return Importer.Imported(D, FoundMethod);
3209 }
3210 }
3211
3212 // Import the result type.
3213 QualType ResultTy = Importer.Import(D->getResultType());
3214 if (ResultTy.isNull())
3215 return 0;
3216
Douglas Gregor12852d92010-03-08 14:59:44 +00003217 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3218
Douglas Gregor43f54792010-02-17 02:12:47 +00003219 ObjCMethodDecl *ToMethod
3220 = ObjCMethodDecl::Create(Importer.getToContext(),
3221 Loc,
3222 Importer.Import(D->getLocEnd()),
3223 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003224 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003225 D->isInstanceMethod(),
3226 D->isVariadic(),
Jordan Rosed01e83a2012-10-10 16:42:25 +00003227 D->isPropertyAccessor(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003228 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003229 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003230 D->getImplementationControl(),
3231 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003232
3233 // FIXME: When we decide to merge method definitions, we'll need to
3234 // deal with implicit parameters.
3235
3236 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003237 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003238 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3239 FromPEnd = D->param_end();
3240 FromP != FromPEnd;
3241 ++FromP) {
3242 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3243 if (!ToP)
3244 return 0;
3245
3246 ToParams.push_back(ToP);
3247 }
3248
3249 // Set the parameters.
3250 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3251 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003252 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003253 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003254 SmallVector<SourceLocation, 12> SelLocs;
3255 D->getSelectorLocs(SelLocs);
3256 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003257
3258 ToMethod->setLexicalDeclContext(LexicalDC);
3259 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003260 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003261 return ToMethod;
3262}
3263
Douglas Gregor84c51c32010-02-18 01:47:50 +00003264Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3265 // Import the major distinguishing characteristics of a category.
3266 DeclContext *DC, *LexicalDC;
3267 DeclarationName Name;
3268 SourceLocation Loc;
3269 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3270 return 0;
3271
3272 ObjCInterfaceDecl *ToInterface
3273 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3274 if (!ToInterface)
3275 return 0;
3276
3277 // Determine if we've already encountered this category.
3278 ObjCCategoryDecl *MergeWithCategory
3279 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3280 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3281 if (!ToCategory) {
3282 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003283 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003284 Loc,
3285 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003286 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003287 ToInterface,
3288 Importer.Import(D->getIvarLBraceLoc()),
3289 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003290 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003291 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003292 Importer.Imported(D, ToCategory);
3293
Douglas Gregor84c51c32010-02-18 01:47:50 +00003294 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003295 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3296 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003297 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3298 = D->protocol_loc_begin();
3299 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3300 FromProtoEnd = D->protocol_end();
3301 FromProto != FromProtoEnd;
3302 ++FromProto, ++FromProtoLoc) {
3303 ObjCProtocolDecl *ToProto
3304 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3305 if (!ToProto)
3306 return 0;
3307 Protocols.push_back(ToProto);
3308 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3309 }
3310
3311 // FIXME: If we're merging, make sure that the protocol list is the same.
3312 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3313 ProtocolLocs.data(), Importer.getToContext());
3314
3315 } else {
3316 Importer.Imported(D, ToCategory);
3317 }
3318
3319 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003320 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003321
3322 // If we have an implementation, import it as well.
3323 if (D->getImplementation()) {
3324 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003325 = cast_or_null<ObjCCategoryImplDecl>(
3326 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003327 if (!Impl)
3328 return 0;
3329
3330 ToCategory->setImplementation(Impl);
3331 }
3332
3333 return ToCategory;
3334}
3335
Douglas Gregor2aa53772012-01-24 17:42:07 +00003336bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3337 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003338 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003339 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003340 if (shouldForceImportDeclContext(Kind))
3341 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003342 return false;
3343 }
3344
3345 // Start the protocol definition
3346 To->startDefinition();
3347
3348 // Import protocols
3349 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3350 SmallVector<SourceLocation, 4> ProtocolLocs;
3351 ObjCProtocolDecl::protocol_loc_iterator
3352 FromProtoLoc = From->protocol_loc_begin();
3353 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3354 FromProtoEnd = From->protocol_end();
3355 FromProto != FromProtoEnd;
3356 ++FromProto, ++FromProtoLoc) {
3357 ObjCProtocolDecl *ToProto
3358 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3359 if (!ToProto)
3360 return true;
3361 Protocols.push_back(ToProto);
3362 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3363 }
3364
3365 // FIXME: If we're merging, make sure that the protocol list is the same.
3366 To->setProtocolList(Protocols.data(), Protocols.size(),
3367 ProtocolLocs.data(), Importer.getToContext());
3368
Douglas Gregor2e15c842012-02-01 21:00:38 +00003369 if (shouldForceImportDeclContext(Kind)) {
3370 // Import all of the members of this protocol.
3371 ImportDeclContext(From, /*ForceImport=*/true);
3372 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003373 return false;
3374}
3375
Douglas Gregor98d156a2010-02-17 16:12:00 +00003376Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003377 // If this protocol has a definition in the translation unit we're coming
3378 // from, but this particular declaration is not that definition, import the
3379 // definition and map to that.
3380 ObjCProtocolDecl *Definition = D->getDefinition();
3381 if (Definition && Definition != D) {
3382 Decl *ImportedDef = Importer.Import(Definition);
3383 if (!ImportedDef)
3384 return 0;
3385
3386 return Importer.Imported(D, ImportedDef);
3387 }
3388
Douglas Gregor84c51c32010-02-18 01:47:50 +00003389 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003390 DeclContext *DC, *LexicalDC;
3391 DeclarationName Name;
3392 SourceLocation Loc;
3393 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3394 return 0;
3395
3396 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003397 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3398 DC->localUncachedLookup(Name, FoundDecls);
3399 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3400 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003401 continue;
3402
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003403 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003404 break;
3405 }
3406
3407 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003408 if (!ToProto) {
3409 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3410 Name.getAsIdentifierInfo(), Loc,
3411 Importer.Import(D->getAtStartLoc()),
3412 /*PrevDecl=*/0);
3413 ToProto->setLexicalDeclContext(LexicalDC);
3414 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003415 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003416
3417 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003418
Douglas Gregor2aa53772012-01-24 17:42:07 +00003419 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3420 return 0;
3421
Douglas Gregor98d156a2010-02-17 16:12:00 +00003422 return ToProto;
3423}
3424
Douglas Gregor2aa53772012-01-24 17:42:07 +00003425bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3426 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003427 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003428 if (To->getDefinition()) {
3429 // Check consistency of superclass.
3430 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3431 if (FromSuper) {
3432 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3433 if (!FromSuper)
3434 return true;
3435 }
3436
3437 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3438 if ((bool)FromSuper != (bool)ToSuper ||
3439 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3440 Importer.ToDiag(To->getLocation(),
3441 diag::err_odr_objc_superclass_inconsistent)
3442 << To->getDeclName();
3443 if (ToSuper)
3444 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3445 << To->getSuperClass()->getDeclName();
3446 else
3447 Importer.ToDiag(To->getLocation(),
3448 diag::note_odr_objc_missing_superclass);
3449 if (From->getSuperClass())
3450 Importer.FromDiag(From->getSuperClassLoc(),
3451 diag::note_odr_objc_superclass)
3452 << From->getSuperClass()->getDeclName();
3453 else
3454 Importer.FromDiag(From->getLocation(),
3455 diag::note_odr_objc_missing_superclass);
3456 }
3457
Douglas Gregor2e15c842012-02-01 21:00:38 +00003458 if (shouldForceImportDeclContext(Kind))
3459 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003460 return false;
3461 }
3462
3463 // Start the definition.
3464 To->startDefinition();
3465
3466 // If this class has a superclass, import it.
3467 if (From->getSuperClass()) {
3468 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3469 Importer.Import(From->getSuperClass()));
3470 if (!Super)
3471 return true;
3472
3473 To->setSuperClass(Super);
3474 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3475 }
3476
3477 // Import protocols
3478 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3479 SmallVector<SourceLocation, 4> ProtocolLocs;
3480 ObjCInterfaceDecl::protocol_loc_iterator
3481 FromProtoLoc = From->protocol_loc_begin();
3482
3483 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3484 FromProtoEnd = From->protocol_end();
3485 FromProto != FromProtoEnd;
3486 ++FromProto, ++FromProtoLoc) {
3487 ObjCProtocolDecl *ToProto
3488 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3489 if (!ToProto)
3490 return true;
3491 Protocols.push_back(ToProto);
3492 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3493 }
3494
3495 // FIXME: If we're merging, make sure that the protocol list is the same.
3496 To->setProtocolList(Protocols.data(), Protocols.size(),
3497 ProtocolLocs.data(), Importer.getToContext());
3498
3499 // Import categories. When the categories themselves are imported, they'll
3500 // hook themselves into this interface.
3501 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3502 FromCat = FromCat->getNextClassCategory())
3503 Importer.Import(FromCat);
3504
3505 // If we have an @implementation, import it as well.
3506 if (From->getImplementation()) {
3507 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3508 Importer.Import(From->getImplementation()));
3509 if (!Impl)
3510 return true;
3511
3512 To->setImplementation(Impl);
3513 }
3514
Douglas Gregor2e15c842012-02-01 21:00:38 +00003515 if (shouldForceImportDeclContext(Kind)) {
3516 // Import all of the members of this class.
3517 ImportDeclContext(From, /*ForceImport=*/true);
3518 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003519 return false;
3520}
3521
Douglas Gregor45635322010-02-16 01:20:57 +00003522Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003523 // If this class has a definition in the translation unit we're coming from,
3524 // but this particular declaration is not that definition, import the
3525 // definition and map to that.
3526 ObjCInterfaceDecl *Definition = D->getDefinition();
3527 if (Definition && Definition != D) {
3528 Decl *ImportedDef = Importer.Import(Definition);
3529 if (!ImportedDef)
3530 return 0;
3531
3532 return Importer.Imported(D, ImportedDef);
3533 }
3534
Douglas Gregor45635322010-02-16 01:20:57 +00003535 // Import the major distinguishing characteristics of an @interface.
3536 DeclContext *DC, *LexicalDC;
3537 DeclarationName Name;
3538 SourceLocation Loc;
3539 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3540 return 0;
3541
Douglas Gregor2aa53772012-01-24 17:42:07 +00003542 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003543 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003544 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3545 DC->localUncachedLookup(Name, FoundDecls);
3546 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3547 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003548 continue;
3549
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003550 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003551 break;
3552 }
3553
Douglas Gregor2aa53772012-01-24 17:42:07 +00003554 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003555 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003556 if (!ToIface) {
3557 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3558 Importer.Import(D->getAtStartLoc()),
3559 Name.getAsIdentifierInfo(),
3560 /*PrevDecl=*/0,Loc,
3561 D->isImplicitInterfaceDecl());
3562 ToIface->setLexicalDeclContext(LexicalDC);
3563 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003564 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003565 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003566
Douglas Gregor2aa53772012-01-24 17:42:07 +00003567 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3568 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003569
Douglas Gregor98d156a2010-02-17 16:12:00 +00003570 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003571}
3572
Douglas Gregor4da9d682010-12-07 15:32:12 +00003573Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3574 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3575 Importer.Import(D->getCategoryDecl()));
3576 if (!Category)
3577 return 0;
3578
3579 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3580 if (!ToImpl) {
3581 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3582 if (!DC)
3583 return 0;
3584
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003585 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003586 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003587 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003588 Category->getClassInterface(),
3589 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003590 Importer.Import(D->getAtStartLoc()),
3591 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003592
3593 DeclContext *LexicalDC = DC;
3594 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3595 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3596 if (!LexicalDC)
3597 return 0;
3598
3599 ToImpl->setLexicalDeclContext(LexicalDC);
3600 }
3601
Sean Callanan95e74be2011-10-21 02:57:43 +00003602 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003603 Category->setImplementation(ToImpl);
3604 }
3605
3606 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003607 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003608 return ToImpl;
3609}
3610
Douglas Gregorda8025c2010-12-07 01:26:03 +00003611Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3612 // Find the corresponding interface.
3613 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3614 Importer.Import(D->getClassInterface()));
3615 if (!Iface)
3616 return 0;
3617
3618 // Import the superclass, if any.
3619 ObjCInterfaceDecl *Super = 0;
3620 if (D->getSuperClass()) {
3621 Super = cast_or_null<ObjCInterfaceDecl>(
3622 Importer.Import(D->getSuperClass()));
3623 if (!Super)
3624 return 0;
3625 }
3626
3627 ObjCImplementationDecl *Impl = Iface->getImplementation();
3628 if (!Impl) {
3629 // We haven't imported an implementation yet. Create a new @implementation
3630 // now.
3631 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3632 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003633 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003634 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003635 Importer.Import(D->getAtStartLoc()),
3636 Importer.Import(D->getIvarLBraceLoc()),
3637 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003638
3639 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3640 DeclContext *LexicalDC
3641 = Importer.ImportContext(D->getLexicalDeclContext());
3642 if (!LexicalDC)
3643 return 0;
3644 Impl->setLexicalDeclContext(LexicalDC);
3645 }
3646
3647 // Associate the implementation with the class it implements.
3648 Iface->setImplementation(Impl);
3649 Importer.Imported(D, Iface->getImplementation());
3650 } else {
3651 Importer.Imported(D, Iface->getImplementation());
3652
3653 // Verify that the existing @implementation has the same superclass.
3654 if ((Super && !Impl->getSuperClass()) ||
3655 (!Super && Impl->getSuperClass()) ||
3656 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003657 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003658 Importer.ToDiag(Impl->getLocation(),
3659 diag::err_odr_objc_superclass_inconsistent)
3660 << Iface->getDeclName();
3661 // FIXME: It would be nice to have the location of the superclass
3662 // below.
3663 if (Impl->getSuperClass())
3664 Importer.ToDiag(Impl->getLocation(),
3665 diag::note_odr_objc_superclass)
3666 << Impl->getSuperClass()->getDeclName();
3667 else
3668 Importer.ToDiag(Impl->getLocation(),
3669 diag::note_odr_objc_missing_superclass);
3670 if (D->getSuperClass())
3671 Importer.FromDiag(D->getLocation(),
3672 diag::note_odr_objc_superclass)
3673 << D->getSuperClass()->getDeclName();
3674 else
3675 Importer.FromDiag(D->getLocation(),
3676 diag::note_odr_objc_missing_superclass);
3677 return 0;
3678 }
3679 }
3680
3681 // Import all of the members of this @implementation.
3682 ImportDeclContext(D);
3683
3684 return Impl;
3685}
3686
Douglas Gregora11c4582010-02-17 18:02:10 +00003687Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3688 // Import the major distinguishing characteristics of an @property.
3689 DeclContext *DC, *LexicalDC;
3690 DeclarationName Name;
3691 SourceLocation Loc;
3692 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3693 return 0;
3694
3695 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003696 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3697 DC->localUncachedLookup(Name, FoundDecls);
3698 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003699 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003700 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003701 // Check property types.
3702 if (!Importer.IsStructurallyEquivalent(D->getType(),
3703 FoundProp->getType())) {
3704 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3705 << Name << D->getType() << FoundProp->getType();
3706 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3707 << FoundProp->getType();
3708 return 0;
3709 }
3710
3711 // FIXME: Check property attributes, getters, setters, etc.?
3712
3713 // Consider these properties to be equivalent.
3714 Importer.Imported(D, FoundProp);
3715 return FoundProp;
3716 }
3717 }
3718
3719 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003720 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3721 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003722 return 0;
3723
3724 // Create the new property.
3725 ObjCPropertyDecl *ToProperty
3726 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3727 Name.getAsIdentifierInfo(),
3728 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003729 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003730 T,
3731 D->getPropertyImplementation());
3732 Importer.Imported(D, ToProperty);
3733 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003734 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003735
3736 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003737 ToProperty->setPropertyAttributesAsWritten(
3738 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003739 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3740 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3741 ToProperty->setGetterMethodDecl(
3742 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3743 ToProperty->setSetterMethodDecl(
3744 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3745 ToProperty->setPropertyIvarDecl(
3746 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3747 return ToProperty;
3748}
3749
Douglas Gregor14a49e22010-12-07 18:32:03 +00003750Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3751 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3752 Importer.Import(D->getPropertyDecl()));
3753 if (!Property)
3754 return 0;
3755
3756 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3757 if (!DC)
3758 return 0;
3759
3760 // Import the lexical declaration context.
3761 DeclContext *LexicalDC = DC;
3762 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3763 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3764 if (!LexicalDC)
3765 return 0;
3766 }
3767
3768 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3769 if (!InImpl)
3770 return 0;
3771
3772 // Import the ivar (for an @synthesize).
3773 ObjCIvarDecl *Ivar = 0;
3774 if (D->getPropertyIvarDecl()) {
3775 Ivar = cast_or_null<ObjCIvarDecl>(
3776 Importer.Import(D->getPropertyIvarDecl()));
3777 if (!Ivar)
3778 return 0;
3779 }
3780
3781 ObjCPropertyImplDecl *ToImpl
3782 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3783 if (!ToImpl) {
3784 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3785 Importer.Import(D->getLocStart()),
3786 Importer.Import(D->getLocation()),
3787 Property,
3788 D->getPropertyImplementation(),
3789 Ivar,
3790 Importer.Import(D->getPropertyIvarDeclLoc()));
3791 ToImpl->setLexicalDeclContext(LexicalDC);
3792 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003793 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003794 } else {
3795 // Check that we have the same kind of property implementation (@synthesize
3796 // vs. @dynamic).
3797 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3798 Importer.ToDiag(ToImpl->getLocation(),
3799 diag::err_odr_objc_property_impl_kind_inconsistent)
3800 << Property->getDeclName()
3801 << (ToImpl->getPropertyImplementation()
3802 == ObjCPropertyImplDecl::Dynamic);
3803 Importer.FromDiag(D->getLocation(),
3804 diag::note_odr_objc_property_impl_kind)
3805 << D->getPropertyDecl()->getDeclName()
3806 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3807 return 0;
3808 }
3809
3810 // For @synthesize, check that we have the same
3811 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3812 Ivar != ToImpl->getPropertyIvarDecl()) {
3813 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3814 diag::err_odr_objc_synthesize_ivar_inconsistent)
3815 << Property->getDeclName()
3816 << ToImpl->getPropertyIvarDecl()->getDeclName()
3817 << Ivar->getDeclName();
3818 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3819 diag::note_odr_objc_synthesize_ivar_here)
3820 << D->getPropertyIvarDecl()->getDeclName();
3821 return 0;
3822 }
3823
3824 // Merge the existing implementation with the new implementation.
3825 Importer.Imported(D, ToImpl);
3826 }
3827
3828 return ToImpl;
3829}
3830
Douglas Gregora082a492010-11-30 19:14:50 +00003831Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3832 // For template arguments, we adopt the translation unit as our declaration
3833 // context. This context will be fixed when the actual template declaration
3834 // is created.
3835
3836 // FIXME: Import default argument.
3837 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3838 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003839 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003840 Importer.Import(D->getLocation()),
3841 D->getDepth(),
3842 D->getIndex(),
3843 Importer.Import(D->getIdentifier()),
3844 D->wasDeclaredWithTypename(),
3845 D->isParameterPack());
3846}
3847
3848Decl *
3849ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3850 // Import the name of this declaration.
3851 DeclarationName Name = Importer.Import(D->getDeclName());
3852 if (D->getDeclName() && !Name)
3853 return 0;
3854
3855 // Import the location of this declaration.
3856 SourceLocation Loc = Importer.Import(D->getLocation());
3857
3858 // Import the type of this declaration.
3859 QualType T = Importer.Import(D->getType());
3860 if (T.isNull())
3861 return 0;
3862
3863 // Import type-source information.
3864 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3865 if (D->getTypeSourceInfo() && !TInfo)
3866 return 0;
3867
3868 // FIXME: Import default argument.
3869
3870 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3871 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003872 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003873 Loc, D->getDepth(), D->getPosition(),
3874 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003875 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003876}
3877
3878Decl *
3879ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3880 // Import the name of this declaration.
3881 DeclarationName Name = Importer.Import(D->getDeclName());
3882 if (D->getDeclName() && !Name)
3883 return 0;
3884
3885 // Import the location of this declaration.
3886 SourceLocation Loc = Importer.Import(D->getLocation());
3887
3888 // Import template parameters.
3889 TemplateParameterList *TemplateParams
3890 = ImportTemplateParameterList(D->getTemplateParameters());
3891 if (!TemplateParams)
3892 return 0;
3893
3894 // FIXME: Import default argument.
3895
3896 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3897 Importer.getToContext().getTranslationUnitDecl(),
3898 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003899 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003900 Name.getAsIdentifierInfo(),
3901 TemplateParams);
3902}
3903
3904Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3905 // If this record has a definition in the translation unit we're coming from,
3906 // but this particular declaration is not that definition, import the
3907 // definition and map to that.
3908 CXXRecordDecl *Definition
3909 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3910 if (Definition && Definition != D->getTemplatedDecl()) {
3911 Decl *ImportedDef
3912 = Importer.Import(Definition->getDescribedClassTemplate());
3913 if (!ImportedDef)
3914 return 0;
3915
3916 return Importer.Imported(D, ImportedDef);
3917 }
3918
3919 // Import the major distinguishing characteristics of this class template.
3920 DeclContext *DC, *LexicalDC;
3921 DeclarationName Name;
3922 SourceLocation Loc;
3923 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3924 return 0;
3925
3926 // We may already have a template of the same name; try to find and match it.
3927 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003928 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003929 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3930 DC->localUncachedLookup(Name, FoundDecls);
3931 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3932 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003933 continue;
3934
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003935 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003936 if (ClassTemplateDecl *FoundTemplate
3937 = dyn_cast<ClassTemplateDecl>(Found)) {
3938 if (IsStructuralMatch(D, FoundTemplate)) {
3939 // The class templates structurally match; call it the same template.
3940 // FIXME: We may be filling in a forward declaration here. Handle
3941 // this case!
3942 Importer.Imported(D->getTemplatedDecl(),
3943 FoundTemplate->getTemplatedDecl());
3944 return Importer.Imported(D, FoundTemplate);
3945 }
3946 }
3947
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003948 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003949 }
3950
3951 if (!ConflictingDecls.empty()) {
3952 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3953 ConflictingDecls.data(),
3954 ConflictingDecls.size());
3955 }
3956
3957 if (!Name)
3958 return 0;
3959 }
3960
3961 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3962
3963 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003964 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3965 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003966 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3967 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003968 DC, StartLoc, IdLoc,
3969 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003970 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003971 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003972 D2Templated->setLexicalDeclContext(LexicalDC);
3973
3974 // Create the class template declaration itself.
3975 TemplateParameterList *TemplateParams
3976 = ImportTemplateParameterList(D->getTemplateParameters());
3977 if (!TemplateParams)
3978 return 0;
3979
3980 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3981 Loc, Name, TemplateParams,
3982 D2Templated,
3983 /*PrevDecl=*/0);
3984 D2Templated->setDescribedClassTemplate(D2);
3985
3986 D2->setAccess(D->getAccess());
3987 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003988 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003989
3990 // Note the relationship between the class templates.
3991 Importer.Imported(D, D2);
3992 Importer.Imported(DTemplated, D2Templated);
3993
John McCallf937c022011-10-07 06:10:15 +00003994 if (DTemplated->isCompleteDefinition() &&
3995 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003996 // FIXME: Import definition!
3997 }
3998
3999 return D2;
4000}
4001
Douglas Gregore2e50d332010-12-01 01:36:18 +00004002Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4003 ClassTemplateSpecializationDecl *D) {
4004 // If this record has a definition in the translation unit we're coming from,
4005 // but this particular declaration is not that definition, import the
4006 // definition and map to that.
4007 TagDecl *Definition = D->getDefinition();
4008 if (Definition && Definition != D) {
4009 Decl *ImportedDef = Importer.Import(Definition);
4010 if (!ImportedDef)
4011 return 0;
4012
4013 return Importer.Imported(D, ImportedDef);
4014 }
4015
4016 ClassTemplateDecl *ClassTemplate
4017 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4018 D->getSpecializedTemplate()));
4019 if (!ClassTemplate)
4020 return 0;
4021
4022 // Import the context of this declaration.
4023 DeclContext *DC = ClassTemplate->getDeclContext();
4024 if (!DC)
4025 return 0;
4026
4027 DeclContext *LexicalDC = DC;
4028 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4029 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4030 if (!LexicalDC)
4031 return 0;
4032 }
4033
4034 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004035 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4036 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004037
4038 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004039 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004040 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4041 D->getTemplateArgs().size(),
4042 TemplateArgs))
4043 return 0;
4044
4045 // Try to find an existing specialization with these template arguments.
4046 void *InsertPos = 0;
4047 ClassTemplateSpecializationDecl *D2
4048 = ClassTemplate->findSpecialization(TemplateArgs.data(),
4049 TemplateArgs.size(), InsertPos);
4050 if (D2) {
4051 // We already have a class template specialization with these template
4052 // arguments.
4053
4054 // FIXME: Check for specialization vs. instantiation errors.
4055
4056 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004057 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004058 // The record types structurally match, or the "from" translation
4059 // unit only had a forward declaration anyway; call it the same
4060 // function.
4061 return Importer.Imported(D, FoundDef);
4062 }
4063 }
4064 } else {
4065 // Create a new specialization.
4066 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4067 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004068 StartLoc, IdLoc,
4069 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004070 TemplateArgs.data(),
4071 TemplateArgs.size(),
4072 /*PrevDecl=*/0);
4073 D2->setSpecializationKind(D->getSpecializationKind());
4074
4075 // Add this specialization to the class template.
4076 ClassTemplate->AddSpecialization(D2, InsertPos);
4077
4078 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004079 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004080
4081 // Add the specialization to this context.
4082 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004083 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004084 }
4085 Importer.Imported(D, D2);
4086
John McCallf937c022011-10-07 06:10:15 +00004087 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00004088 return 0;
4089
4090 return D2;
4091}
4092
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004093//----------------------------------------------------------------------------
4094// Import Statements
4095//----------------------------------------------------------------------------
4096
4097Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4098 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4099 << S->getStmtClassName();
4100 return 0;
4101}
4102
4103//----------------------------------------------------------------------------
4104// Import Expressions
4105//----------------------------------------------------------------------------
4106Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4107 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4108 << E->getStmtClassName();
4109 return 0;
4110}
4111
Douglas Gregor52f820e2010-02-19 01:17:02 +00004112Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004113 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4114 if (!ToD)
4115 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004116
4117 NamedDecl *FoundD = 0;
4118 if (E->getDecl() != E->getFoundDecl()) {
4119 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4120 if (!FoundD)
4121 return 0;
4122 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004123
4124 QualType T = Importer.Import(E->getType());
4125 if (T.isNull())
4126 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004127
4128 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4129 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004130 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004131 ToD,
John McCall113bee02012-03-10 09:33:50 +00004132 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004133 Importer.Import(E->getLocation()),
4134 T, E->getValueKind(),
4135 FoundD,
4136 /*FIXME:TemplateArgs=*/0);
4137 if (E->hadMultipleCandidates())
4138 DRE->setHadMultipleCandidates(true);
4139 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004140}
4141
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004142Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4143 QualType T = Importer.Import(E->getType());
4144 if (T.isNull())
4145 return 0;
4146
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004147 return IntegerLiteral::Create(Importer.getToContext(),
4148 E->getValue(), T,
4149 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004150}
4151
Douglas Gregor623421d2010-02-18 02:21:22 +00004152Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4153 QualType T = Importer.Import(E->getType());
4154 if (T.isNull())
4155 return 0;
4156
Douglas Gregorfb65e592011-07-27 05:40:30 +00004157 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4158 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004159 Importer.Import(E->getLocation()));
4160}
4161
Douglas Gregorc74247e2010-02-19 01:07:06 +00004162Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4163 Expr *SubExpr = Importer.Import(E->getSubExpr());
4164 if (!SubExpr)
4165 return 0;
4166
4167 return new (Importer.getToContext())
4168 ParenExpr(Importer.Import(E->getLParen()),
4169 Importer.Import(E->getRParen()),
4170 SubExpr);
4171}
4172
4173Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4174 QualType T = Importer.Import(E->getType());
4175 if (T.isNull())
4176 return 0;
4177
4178 Expr *SubExpr = Importer.Import(E->getSubExpr());
4179 if (!SubExpr)
4180 return 0;
4181
4182 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004183 T, E->getValueKind(),
4184 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004185 Importer.Import(E->getOperatorLoc()));
4186}
4187
Peter Collingbournee190dee2011-03-11 19:24:49 +00004188Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4189 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004190 QualType ResultType = Importer.Import(E->getType());
4191
4192 if (E->isArgumentType()) {
4193 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4194 if (!TInfo)
4195 return 0;
4196
Peter Collingbournee190dee2011-03-11 19:24:49 +00004197 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4198 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004199 Importer.Import(E->getOperatorLoc()),
4200 Importer.Import(E->getRParenLoc()));
4201 }
4202
4203 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4204 if (!SubExpr)
4205 return 0;
4206
Peter Collingbournee190dee2011-03-11 19:24:49 +00004207 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4208 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004209 Importer.Import(E->getOperatorLoc()),
4210 Importer.Import(E->getRParenLoc()));
4211}
4212
Douglas Gregorc74247e2010-02-19 01:07:06 +00004213Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4214 QualType T = Importer.Import(E->getType());
4215 if (T.isNull())
4216 return 0;
4217
4218 Expr *LHS = Importer.Import(E->getLHS());
4219 if (!LHS)
4220 return 0;
4221
4222 Expr *RHS = Importer.Import(E->getRHS());
4223 if (!RHS)
4224 return 0;
4225
4226 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004227 T, E->getValueKind(),
4228 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004229 Importer.Import(E->getOperatorLoc()),
4230 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004231}
4232
4233Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4234 QualType T = Importer.Import(E->getType());
4235 if (T.isNull())
4236 return 0;
4237
4238 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4239 if (CompLHSType.isNull())
4240 return 0;
4241
4242 QualType CompResultType = Importer.Import(E->getComputationResultType());
4243 if (CompResultType.isNull())
4244 return 0;
4245
4246 Expr *LHS = Importer.Import(E->getLHS());
4247 if (!LHS)
4248 return 0;
4249
4250 Expr *RHS = Importer.Import(E->getRHS());
4251 if (!RHS)
4252 return 0;
4253
4254 return new (Importer.getToContext())
4255 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004256 T, E->getValueKind(),
4257 E->getObjectKind(),
4258 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004259 Importer.Import(E->getOperatorLoc()),
4260 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004261}
4262
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004263static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004264 if (E->path_empty()) return false;
4265
4266 // TODO: import cast paths
4267 return true;
4268}
4269
Douglas Gregor98c10182010-02-12 22:17:39 +00004270Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4271 QualType T = Importer.Import(E->getType());
4272 if (T.isNull())
4273 return 0;
4274
4275 Expr *SubExpr = Importer.Import(E->getSubExpr());
4276 if (!SubExpr)
4277 return 0;
John McCallcf142162010-08-07 06:22:56 +00004278
4279 CXXCastPath BasePath;
4280 if (ImportCastPath(E, BasePath))
4281 return 0;
4282
4283 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004284 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004285}
4286
Douglas Gregor5481d322010-02-19 01:32:14 +00004287Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4288 QualType T = Importer.Import(E->getType());
4289 if (T.isNull())
4290 return 0;
4291
4292 Expr *SubExpr = Importer.Import(E->getSubExpr());
4293 if (!SubExpr)
4294 return 0;
4295
4296 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4297 if (!TInfo && E->getTypeInfoAsWritten())
4298 return 0;
4299
John McCallcf142162010-08-07 06:22:56 +00004300 CXXCastPath BasePath;
4301 if (ImportCastPath(E, BasePath))
4302 return 0;
4303
John McCall7decc9e2010-11-18 06:31:45 +00004304 return CStyleCastExpr::Create(Importer.getToContext(), T,
4305 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004306 SubExpr, &BasePath, TInfo,
4307 Importer.Import(E->getLParenLoc()),
4308 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004309}
4310
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004311ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004312 ASTContext &FromContext, FileManager &FromFileManager,
4313 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004314 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004315 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4316 Minimal(MinimalImport)
4317{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004318 ImportedDecls[FromContext.getTranslationUnitDecl()]
4319 = ToContext.getTranslationUnitDecl();
4320}
4321
4322ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004323
4324QualType ASTImporter::Import(QualType FromT) {
4325 if (FromT.isNull())
4326 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004327
4328 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004329
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004330 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004331 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4332 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004333 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004334 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004335
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004336 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004337 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004338 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004339 if (ToT.isNull())
4340 return ToT;
4341
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004342 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004343 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004344
John McCall424cec92011-01-19 06:33:43 +00004345 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004346}
4347
Douglas Gregor62d311f2010-02-09 19:21:46 +00004348TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004349 if (!FromTSI)
4350 return FromTSI;
4351
4352 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004353 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004354 QualType T = Import(FromTSI->getType());
4355 if (T.isNull())
4356 return 0;
4357
4358 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004359 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004360}
4361
4362Decl *ASTImporter::Import(Decl *FromD) {
4363 if (!FromD)
4364 return 0;
4365
Douglas Gregord451ea92011-07-29 23:31:30 +00004366 ASTNodeImporter Importer(*this);
4367
Douglas Gregor62d311f2010-02-09 19:21:46 +00004368 // Check whether we've already imported this declaration.
4369 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004370 if (Pos != ImportedDecls.end()) {
4371 Decl *ToD = Pos->second;
4372 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4373 return ToD;
4374 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004375
4376 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004377 Decl *ToD = Importer.Visit(FromD);
4378 if (!ToD)
4379 return 0;
4380
4381 // Record the imported declaration.
4382 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004383
4384 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4385 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004386 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004387 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004388 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004389 // When we've finished transforming a typedef, see whether it was the
4390 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004391 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004392 FromTag = AnonTagsWithPendingTypedefs.begin(),
4393 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4394 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004395 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004396 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4397 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004398 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004399 AnonTagsWithPendingTypedefs.erase(FromTag);
4400 break;
4401 }
4402 }
4403 }
4404 }
4405
Douglas Gregor62d311f2010-02-09 19:21:46 +00004406 return ToD;
4407}
4408
4409DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4410 if (!FromDC)
4411 return FromDC;
4412
Douglas Gregor95d82832012-01-24 18:36:04 +00004413 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004414 if (!ToDC)
4415 return 0;
4416
4417 // When we're using a record/enum/Objective-C class/protocol as a context, we
4418 // need it to have a definition.
4419 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004420 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004421 if (ToRecord->isCompleteDefinition()) {
4422 // Do nothing.
4423 } else if (FromRecord->isCompleteDefinition()) {
4424 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4425 ASTNodeImporter::IDK_Basic);
4426 } else {
4427 CompleteDecl(ToRecord);
4428 }
4429 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4430 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4431 if (ToEnum->isCompleteDefinition()) {
4432 // Do nothing.
4433 } else if (FromEnum->isCompleteDefinition()) {
4434 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4435 ASTNodeImporter::IDK_Basic);
4436 } else {
4437 CompleteDecl(ToEnum);
4438 }
4439 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4440 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4441 if (ToClass->getDefinition()) {
4442 // Do nothing.
4443 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4444 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4445 ASTNodeImporter::IDK_Basic);
4446 } else {
4447 CompleteDecl(ToClass);
4448 }
4449 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4450 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4451 if (ToProto->getDefinition()) {
4452 // Do nothing.
4453 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4454 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4455 ASTNodeImporter::IDK_Basic);
4456 } else {
4457 CompleteDecl(ToProto);
4458 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004459 }
4460
4461 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004462}
4463
4464Expr *ASTImporter::Import(Expr *FromE) {
4465 if (!FromE)
4466 return 0;
4467
4468 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4469}
4470
4471Stmt *ASTImporter::Import(Stmt *FromS) {
4472 if (!FromS)
4473 return 0;
4474
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004475 // Check whether we've already imported this declaration.
4476 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4477 if (Pos != ImportedStmts.end())
4478 return Pos->second;
4479
4480 // Import the type
4481 ASTNodeImporter Importer(*this);
4482 Stmt *ToS = Importer.Visit(FromS);
4483 if (!ToS)
4484 return 0;
4485
4486 // Record the imported declaration.
4487 ImportedStmts[FromS] = ToS;
4488 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004489}
4490
4491NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4492 if (!FromNNS)
4493 return 0;
4494
Douglas Gregor90ebf252011-04-27 16:48:40 +00004495 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4496
4497 switch (FromNNS->getKind()) {
4498 case NestedNameSpecifier::Identifier:
4499 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4500 return NestedNameSpecifier::Create(ToContext, prefix, II);
4501 }
4502 return 0;
4503
4504 case NestedNameSpecifier::Namespace:
4505 if (NamespaceDecl *NS =
4506 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4507 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4508 }
4509 return 0;
4510
4511 case NestedNameSpecifier::NamespaceAlias:
4512 if (NamespaceAliasDecl *NSAD =
4513 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4514 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4515 }
4516 return 0;
4517
4518 case NestedNameSpecifier::Global:
4519 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4520
4521 case NestedNameSpecifier::TypeSpec:
4522 case NestedNameSpecifier::TypeSpecWithTemplate: {
4523 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4524 if (!T.isNull()) {
4525 bool bTemplate = FromNNS->getKind() ==
4526 NestedNameSpecifier::TypeSpecWithTemplate;
4527 return NestedNameSpecifier::Create(ToContext, prefix,
4528 bTemplate, T.getTypePtr());
4529 }
4530 }
4531 return 0;
4532 }
4533
4534 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004535}
4536
Douglas Gregor14454802011-02-25 02:25:35 +00004537NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4538 // FIXME: Implement!
4539 return NestedNameSpecifierLoc();
4540}
4541
Douglas Gregore2e50d332010-12-01 01:36:18 +00004542TemplateName ASTImporter::Import(TemplateName From) {
4543 switch (From.getKind()) {
4544 case TemplateName::Template:
4545 if (TemplateDecl *ToTemplate
4546 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4547 return TemplateName(ToTemplate);
4548
4549 return TemplateName();
4550
4551 case TemplateName::OverloadedTemplate: {
4552 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4553 UnresolvedSet<2> ToTemplates;
4554 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4555 E = FromStorage->end();
4556 I != E; ++I) {
4557 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4558 ToTemplates.addDecl(To);
4559 else
4560 return TemplateName();
4561 }
4562 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4563 ToTemplates.end());
4564 }
4565
4566 case TemplateName::QualifiedTemplate: {
4567 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4568 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4569 if (!Qualifier)
4570 return TemplateName();
4571
4572 if (TemplateDecl *ToTemplate
4573 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4574 return ToContext.getQualifiedTemplateName(Qualifier,
4575 QTN->hasTemplateKeyword(),
4576 ToTemplate);
4577
4578 return TemplateName();
4579 }
4580
4581 case TemplateName::DependentTemplate: {
4582 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4583 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4584 if (!Qualifier)
4585 return TemplateName();
4586
4587 if (DTN->isIdentifier()) {
4588 return ToContext.getDependentTemplateName(Qualifier,
4589 Import(DTN->getIdentifier()));
4590 }
4591
4592 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4593 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004594
4595 case TemplateName::SubstTemplateTemplateParm: {
4596 SubstTemplateTemplateParmStorage *subst
4597 = From.getAsSubstTemplateTemplateParm();
4598 TemplateTemplateParmDecl *param
4599 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4600 if (!param)
4601 return TemplateName();
4602
4603 TemplateName replacement = Import(subst->getReplacement());
4604 if (replacement.isNull()) return TemplateName();
4605
4606 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4607 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004608
4609 case TemplateName::SubstTemplateTemplateParmPack: {
4610 SubstTemplateTemplateParmPackStorage *SubstPack
4611 = From.getAsSubstTemplateTemplateParmPack();
4612 TemplateTemplateParmDecl *Param
4613 = cast_or_null<TemplateTemplateParmDecl>(
4614 Import(SubstPack->getParameterPack()));
4615 if (!Param)
4616 return TemplateName();
4617
4618 ASTNodeImporter Importer(*this);
4619 TemplateArgument ArgPack
4620 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4621 if (ArgPack.isNull())
4622 return TemplateName();
4623
4624 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4625 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004626 }
4627
4628 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004629}
4630
Douglas Gregor62d311f2010-02-09 19:21:46 +00004631SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4632 if (FromLoc.isInvalid())
4633 return SourceLocation();
4634
Douglas Gregor811663e2010-02-10 00:15:17 +00004635 SourceManager &FromSM = FromContext.getSourceManager();
4636
4637 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004638 // don't have to import macro expansions.
4639 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004640 FromLoc = FromSM.getSpellingLoc(FromLoc);
4641 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4642 SourceManager &ToSM = ToContext.getSourceManager();
4643 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004644 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004645}
4646
4647SourceRange ASTImporter::Import(SourceRange FromRange) {
4648 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4649}
4650
Douglas Gregor811663e2010-02-10 00:15:17 +00004651FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004652 llvm::DenseMap<FileID, FileID>::iterator Pos
4653 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004654 if (Pos != ImportedFileIDs.end())
4655 return Pos->second;
4656
4657 SourceManager &FromSM = FromContext.getSourceManager();
4658 SourceManager &ToSM = ToContext.getSourceManager();
4659 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004660 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004661
4662 // Include location of this file.
4663 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4664
4665 // Map the FileID for to the "to" source manager.
4666 FileID ToID;
4667 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004668 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004669 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4670 // disk again
4671 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4672 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004673 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004674 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4675 FromSLoc.getFile().getFileCharacteristic());
4676 } else {
4677 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004678 const llvm::MemoryBuffer *
4679 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004680 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004681 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004682 FromBuf->getBufferIdentifier());
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +00004683 ToID = ToSM.createFileIDForMemBuffer(ToBuf,
4684 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00004685 }
4686
4687
Sebastian Redl99219f12010-09-30 01:03:06 +00004688 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004689 return ToID;
4690}
4691
Douglas Gregor0a791672011-01-18 03:11:38 +00004692void ASTImporter::ImportDefinition(Decl *From) {
4693 Decl *To = Import(From);
4694 if (!To)
4695 return;
4696
4697 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4698 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004699
4700 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4701 if (!ToRecord->getDefinition()) {
4702 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004703 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004704 return;
4705 }
4706 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004707
4708 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4709 if (!ToEnum->getDefinition()) {
4710 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004711 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004712 return;
4713 }
4714 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004715
4716 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4717 if (!ToIFace->getDefinition()) {
4718 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004719 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004720 return;
4721 }
4722 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004723
Douglas Gregor2aa53772012-01-24 17:42:07 +00004724 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4725 if (!ToProto->getDefinition()) {
4726 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004727 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004728 return;
4729 }
4730 }
4731
Douglas Gregor0a791672011-01-18 03:11:38 +00004732 Importer.ImportDeclContext(FromDC, true);
4733 }
4734}
4735
Douglas Gregor96e578d2010-02-05 17:54:41 +00004736DeclarationName ASTImporter::Import(DeclarationName FromName) {
4737 if (!FromName)
4738 return DeclarationName();
4739
4740 switch (FromName.getNameKind()) {
4741 case DeclarationName::Identifier:
4742 return Import(FromName.getAsIdentifierInfo());
4743
4744 case DeclarationName::ObjCZeroArgSelector:
4745 case DeclarationName::ObjCOneArgSelector:
4746 case DeclarationName::ObjCMultiArgSelector:
4747 return Import(FromName.getObjCSelector());
4748
4749 case DeclarationName::CXXConstructorName: {
4750 QualType T = Import(FromName.getCXXNameType());
4751 if (T.isNull())
4752 return DeclarationName();
4753
4754 return ToContext.DeclarationNames.getCXXConstructorName(
4755 ToContext.getCanonicalType(T));
4756 }
4757
4758 case DeclarationName::CXXDestructorName: {
4759 QualType T = Import(FromName.getCXXNameType());
4760 if (T.isNull())
4761 return DeclarationName();
4762
4763 return ToContext.DeclarationNames.getCXXDestructorName(
4764 ToContext.getCanonicalType(T));
4765 }
4766
4767 case DeclarationName::CXXConversionFunctionName: {
4768 QualType T = Import(FromName.getCXXNameType());
4769 if (T.isNull())
4770 return DeclarationName();
4771
4772 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4773 ToContext.getCanonicalType(T));
4774 }
4775
4776 case DeclarationName::CXXOperatorName:
4777 return ToContext.DeclarationNames.getCXXOperatorName(
4778 FromName.getCXXOverloadedOperator());
4779
4780 case DeclarationName::CXXLiteralOperatorName:
4781 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4782 Import(FromName.getCXXLiteralIdentifier()));
4783
4784 case DeclarationName::CXXUsingDirective:
4785 // FIXME: STATICS!
4786 return DeclarationName::getUsingDirectiveName();
4787 }
4788
David Blaikiee4d798f2012-01-20 21:50:17 +00004789 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004790}
4791
Douglas Gregore2e50d332010-12-01 01:36:18 +00004792IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004793 if (!FromId)
4794 return 0;
4795
4796 return &ToContext.Idents.get(FromId->getName());
4797}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004798
Douglas Gregor43f54792010-02-17 02:12:47 +00004799Selector ASTImporter::Import(Selector FromSel) {
4800 if (FromSel.isNull())
4801 return Selector();
4802
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004803 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004804 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4805 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4806 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4807 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4808}
4809
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004810DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4811 DeclContext *DC,
4812 unsigned IDNS,
4813 NamedDecl **Decls,
4814 unsigned NumDecls) {
4815 return Name;
4816}
4817
4818DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004819 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004820}
4821
4822DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004823 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004824}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004825
Douglas Gregor2e15c842012-02-01 21:00:38 +00004826void ASTImporter::CompleteDecl (Decl *D) {
4827 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4828 if (!ID->getDefinition())
4829 ID->startDefinition();
4830 }
4831 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4832 if (!PD->getDefinition())
4833 PD->startDefinition();
4834 }
4835 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4836 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4837 TD->startDefinition();
4838 TD->setCompleteDefinition(true);
4839 }
4840 }
4841 else {
4842 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4843 }
4844}
4845
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004846Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4847 ImportedDecls[From] = To;
4848 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004849}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004850
Douglas Gregordd6006f2012-07-17 21:16:27 +00004851bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4852 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004853 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004854 = ImportedTypes.find(From.getTypePtr());
4855 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4856 return true;
4857
Douglas Gregordd6006f2012-07-17 21:16:27 +00004858 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4859 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004860 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004861}