blob: dc1afcbd4ddfe69ce4b50cf327495de90871b026 [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 Gregora082a492010-11-30 19:14:50 +0000125 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000126 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000127 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000128 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000129 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000130 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000131 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000132 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000133 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000134 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000135 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000136 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
137 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
138 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
139 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000141 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000142 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000143 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000144 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000145 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000146 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000147 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000148 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000149 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000150 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000151 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000152 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000153 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000154 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
155 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
156 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
157 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000158 Decl *VisitClassTemplateSpecializationDecl(
159 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000160
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000161 // Importing statements
162 Stmt *VisitStmt(Stmt *S);
163
164 // Importing expressions
165 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000166 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000167 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000168 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000169 Expr *VisitParenExpr(ParenExpr *E);
170 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000171 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000172 Expr *VisitBinaryOperator(BinaryOperator *E);
173 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000174 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000175 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176 };
177}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000178using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179
180//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000181// Structural Equivalence
182//----------------------------------------------------------------------------
183
184namespace {
185 struct StructuralEquivalenceContext {
186 /// \brief AST contexts for which we are checking structural equivalence.
187 ASTContext &C1, &C2;
188
Douglas Gregor3996e242010-02-15 22:01:00 +0000189 /// \brief The set of "tentative" equivalences between two canonical
190 /// declarations, mapping from a declaration in the first context to the
191 /// declaration in the second context that we believe to be equivalent.
192 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
193
194 /// \brief Queue of declarations in the first context whose equivalence
195 /// with a declaration in the second context still needs to be verified.
196 std::deque<Decl *> DeclsToCheck;
197
Douglas Gregorb4964f72010-02-15 23:54:17 +0000198 /// \brief Declaration (from, to) pairs that are known not to be equivalent
199 /// (which we have already complained about).
200 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
201
Douglas Gregor3996e242010-02-15 22:01:00 +0000202 /// \brief Whether we're being strict about the spelling of types when
203 /// unifying two types.
204 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000205
206 /// \brief Whether to complain about failures.
207 bool Complain;
208
Douglas Gregor3996e242010-02-15 22:01:00 +0000209 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000210 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000211 bool StrictTypeSpelling = false,
212 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000213 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregordd6006f2012-07-17 21:16:27 +0000214 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000215
216 /// \brief Determine whether the two declarations are structurally
217 /// equivalent.
218 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
219
220 /// \brief Determine whether the two types are structurally equivalent.
221 bool IsStructurallyEquivalent(QualType T1, QualType T2);
222
223 private:
224 /// \brief Finish checking all of the structural equivalences.
225 ///
226 /// \returns true if an error occurred, false otherwise.
227 bool Finish();
228
229 public:
230 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000231 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000232 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000233 }
234
235 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000236 assert(Complain && "Not allowed to complain");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000237 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000238 }
239 };
240}
241
242static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
243 QualType T1, QualType T2);
244static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
245 Decl *D1, Decl *D2);
246
Douglas Gregor3996e242010-02-15 22:01:00 +0000247/// \brief Determine structural equivalence of two expressions.
248static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
249 Expr *E1, Expr *E2) {
250 if (!E1 || !E2)
251 return E1 == E2;
252
253 // FIXME: Actually perform a structural comparison!
254 return true;
255}
256
257/// \brief Determine whether two identifiers are equivalent.
258static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
259 const IdentifierInfo *Name2) {
260 if (!Name1 || !Name2)
261 return Name1 == Name2;
262
263 return Name1->getName() == Name2->getName();
264}
265
266/// \brief Determine whether two nested-name-specifiers are equivalent.
267static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
268 NestedNameSpecifier *NNS1,
269 NestedNameSpecifier *NNS2) {
270 // FIXME: Implement!
271 return true;
272}
273
274/// \brief Determine whether two template arguments are equivalent.
275static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
276 const TemplateArgument &Arg1,
277 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000278 if (Arg1.getKind() != Arg2.getKind())
279 return false;
280
281 switch (Arg1.getKind()) {
282 case TemplateArgument::Null:
283 return true;
284
285 case TemplateArgument::Type:
286 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000287
Douglas Gregore2e50d332010-12-01 01:36:18 +0000288 case TemplateArgument::Integral:
289 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
290 Arg2.getIntegralType()))
291 return false;
292
Eric Christopher6dcc3762012-07-15 00:23:57 +0000293 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000294
295 case TemplateArgument::Declaration:
296 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000297
298 case TemplateArgument::NullPtr:
299 return true; // FIXME: Is this correct?
300
Douglas Gregore2e50d332010-12-01 01:36:18 +0000301 case TemplateArgument::Template:
302 return IsStructurallyEquivalent(Context,
303 Arg1.getAsTemplate(),
304 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000305
306 case TemplateArgument::TemplateExpansion:
307 return IsStructurallyEquivalent(Context,
308 Arg1.getAsTemplateOrTemplatePattern(),
309 Arg2.getAsTemplateOrTemplatePattern());
310
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311 case TemplateArgument::Expression:
312 return IsStructurallyEquivalent(Context,
313 Arg1.getAsExpr(), Arg2.getAsExpr());
314
315 case TemplateArgument::Pack:
316 if (Arg1.pack_size() != Arg2.pack_size())
317 return false;
318
319 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
320 if (!IsStructurallyEquivalent(Context,
321 Arg1.pack_begin()[I],
322 Arg2.pack_begin()[I]))
323 return false;
324
325 return true;
326 }
327
328 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000329}
330
331/// \brief Determine structural equivalence for the common part of array
332/// types.
333static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
334 const ArrayType *Array1,
335 const ArrayType *Array2) {
336 if (!IsStructurallyEquivalent(Context,
337 Array1->getElementType(),
338 Array2->getElementType()))
339 return false;
340 if (Array1->getSizeModifier() != Array2->getSizeModifier())
341 return false;
342 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
343 return false;
344
345 return true;
346}
347
348/// \brief Determine structural equivalence of two types.
349static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
350 QualType T1, QualType T2) {
351 if (T1.isNull() || T2.isNull())
352 return T1.isNull() && T2.isNull();
353
354 if (!Context.StrictTypeSpelling) {
355 // We aren't being strict about token-to-token equivalence of types,
356 // so map down to the canonical type.
357 T1 = Context.C1.getCanonicalType(T1);
358 T2 = Context.C2.getCanonicalType(T2);
359 }
360
361 if (T1.getQualifiers() != T2.getQualifiers())
362 return false;
363
Douglas Gregorb4964f72010-02-15 23:54:17 +0000364 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000365
Douglas Gregorb4964f72010-02-15 23:54:17 +0000366 if (T1->getTypeClass() != T2->getTypeClass()) {
367 // Compare function types with prototypes vs. without prototypes as if
368 // both did not have prototypes.
369 if (T1->getTypeClass() == Type::FunctionProto &&
370 T2->getTypeClass() == Type::FunctionNoProto)
371 TC = Type::FunctionNoProto;
372 else if (T1->getTypeClass() == Type::FunctionNoProto &&
373 T2->getTypeClass() == Type::FunctionProto)
374 TC = Type::FunctionNoProto;
375 else
376 return false;
377 }
378
379 switch (TC) {
380 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000381 // FIXME: Deal with Char_S/Char_U.
382 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
383 return false;
384 break;
385
386 case Type::Complex:
387 if (!IsStructurallyEquivalent(Context,
388 cast<ComplexType>(T1)->getElementType(),
389 cast<ComplexType>(T2)->getElementType()))
390 return false;
391 break;
392
393 case Type::Pointer:
394 if (!IsStructurallyEquivalent(Context,
395 cast<PointerType>(T1)->getPointeeType(),
396 cast<PointerType>(T2)->getPointeeType()))
397 return false;
398 break;
399
400 case Type::BlockPointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<BlockPointerType>(T1)->getPointeeType(),
403 cast<BlockPointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::LValueReference:
408 case Type::RValueReference: {
409 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
410 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
411 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
412 return false;
413 if (Ref1->isInnerRef() != Ref2->isInnerRef())
414 return false;
415 if (!IsStructurallyEquivalent(Context,
416 Ref1->getPointeeTypeAsWritten(),
417 Ref2->getPointeeTypeAsWritten()))
418 return false;
419 break;
420 }
421
422 case Type::MemberPointer: {
423 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
424 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
425 if (!IsStructurallyEquivalent(Context,
426 MemPtr1->getPointeeType(),
427 MemPtr2->getPointeeType()))
428 return false;
429 if (!IsStructurallyEquivalent(Context,
430 QualType(MemPtr1->getClass(), 0),
431 QualType(MemPtr2->getClass(), 0)))
432 return false;
433 break;
434 }
435
436 case Type::ConstantArray: {
437 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
438 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000439 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000440 return false;
441
442 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
443 return false;
444 break;
445 }
446
447 case Type::IncompleteArray:
448 if (!IsArrayStructurallyEquivalent(Context,
449 cast<ArrayType>(T1),
450 cast<ArrayType>(T2)))
451 return false;
452 break;
453
454 case Type::VariableArray: {
455 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
456 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
457 if (!IsStructurallyEquivalent(Context,
458 Array1->getSizeExpr(), Array2->getSizeExpr()))
459 return false;
460
461 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
462 return false;
463
464 break;
465 }
466
467 case Type::DependentSizedArray: {
468 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
469 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
470 if (!IsStructurallyEquivalent(Context,
471 Array1->getSizeExpr(), Array2->getSizeExpr()))
472 return false;
473
474 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
475 return false;
476
477 break;
478 }
479
480 case Type::DependentSizedExtVector: {
481 const DependentSizedExtVectorType *Vec1
482 = cast<DependentSizedExtVectorType>(T1);
483 const DependentSizedExtVectorType *Vec2
484 = cast<DependentSizedExtVectorType>(T2);
485 if (!IsStructurallyEquivalent(Context,
486 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
487 return false;
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getElementType(),
490 Vec2->getElementType()))
491 return false;
492 break;
493 }
494
495 case Type::Vector:
496 case Type::ExtVector: {
497 const VectorType *Vec1 = cast<VectorType>(T1);
498 const VectorType *Vec2 = cast<VectorType>(T2);
499 if (!IsStructurallyEquivalent(Context,
500 Vec1->getElementType(),
501 Vec2->getElementType()))
502 return false;
503 if (Vec1->getNumElements() != Vec2->getNumElements())
504 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000505 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000506 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000507 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000508 }
509
510 case Type::FunctionProto: {
511 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
512 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
513 if (Proto1->getNumArgs() != Proto2->getNumArgs())
514 return false;
515 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
516 if (!IsStructurallyEquivalent(Context,
517 Proto1->getArgType(I),
518 Proto2->getArgType(I)))
519 return false;
520 }
521 if (Proto1->isVariadic() != Proto2->isVariadic())
522 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000523 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000524 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000525 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
526 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
527 return false;
528 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
529 if (!IsStructurallyEquivalent(Context,
530 Proto1->getExceptionType(I),
531 Proto2->getExceptionType(I)))
532 return false;
533 }
534 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000535 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000536 Proto1->getNoexceptExpr(),
537 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000538 return false;
539 }
540 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
541 return false;
542
543 // Fall through to check the bits common with FunctionNoProtoType.
544 }
545
546 case Type::FunctionNoProto: {
547 const FunctionType *Function1 = cast<FunctionType>(T1);
548 const FunctionType *Function2 = cast<FunctionType>(T2);
549 if (!IsStructurallyEquivalent(Context,
550 Function1->getResultType(),
551 Function2->getResultType()))
552 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000553 if (Function1->getExtInfo() != Function2->getExtInfo())
554 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000555 break;
556 }
557
558 case Type::UnresolvedUsing:
559 if (!IsStructurallyEquivalent(Context,
560 cast<UnresolvedUsingType>(T1)->getDecl(),
561 cast<UnresolvedUsingType>(T2)->getDecl()))
562 return false;
563
564 break;
John McCall81904512011-01-06 01:58:22 +0000565
566 case Type::Attributed:
567 if (!IsStructurallyEquivalent(Context,
568 cast<AttributedType>(T1)->getModifiedType(),
569 cast<AttributedType>(T2)->getModifiedType()))
570 return false;
571 if (!IsStructurallyEquivalent(Context,
572 cast<AttributedType>(T1)->getEquivalentType(),
573 cast<AttributedType>(T2)->getEquivalentType()))
574 return false;
575 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000576
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000577 case Type::Paren:
578 if (!IsStructurallyEquivalent(Context,
579 cast<ParenType>(T1)->getInnerType(),
580 cast<ParenType>(T2)->getInnerType()))
581 return false;
582 break;
583
Douglas Gregor3996e242010-02-15 22:01:00 +0000584 case Type::Typedef:
585 if (!IsStructurallyEquivalent(Context,
586 cast<TypedefType>(T1)->getDecl(),
587 cast<TypedefType>(T2)->getDecl()))
588 return false;
589 break;
590
591 case Type::TypeOfExpr:
592 if (!IsStructurallyEquivalent(Context,
593 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
594 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
595 return false;
596 break;
597
598 case Type::TypeOf:
599 if (!IsStructurallyEquivalent(Context,
600 cast<TypeOfType>(T1)->getUnderlyingType(),
601 cast<TypeOfType>(T2)->getUnderlyingType()))
602 return false;
603 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000604
605 case Type::UnaryTransform:
606 if (!IsStructurallyEquivalent(Context,
607 cast<UnaryTransformType>(T1)->getUnderlyingType(),
608 cast<UnaryTransformType>(T1)->getUnderlyingType()))
609 return false;
610 break;
611
Douglas Gregor3996e242010-02-15 22:01:00 +0000612 case Type::Decltype:
613 if (!IsStructurallyEquivalent(Context,
614 cast<DecltypeType>(T1)->getUnderlyingExpr(),
615 cast<DecltypeType>(T2)->getUnderlyingExpr()))
616 return false;
617 break;
618
Richard Smith30482bc2011-02-20 03:19:35 +0000619 case Type::Auto:
620 if (!IsStructurallyEquivalent(Context,
621 cast<AutoType>(T1)->getDeducedType(),
622 cast<AutoType>(T2)->getDeducedType()))
623 return false;
624 break;
625
Douglas Gregor3996e242010-02-15 22:01:00 +0000626 case Type::Record:
627 case Type::Enum:
628 if (!IsStructurallyEquivalent(Context,
629 cast<TagType>(T1)->getDecl(),
630 cast<TagType>(T2)->getDecl()))
631 return false;
632 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000633
Douglas Gregor3996e242010-02-15 22:01:00 +0000634 case Type::TemplateTypeParm: {
635 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
636 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
637 if (Parm1->getDepth() != Parm2->getDepth())
638 return false;
639 if (Parm1->getIndex() != Parm2->getIndex())
640 return false;
641 if (Parm1->isParameterPack() != Parm2->isParameterPack())
642 return false;
643
644 // Names of template type parameters are never significant.
645 break;
646 }
647
648 case Type::SubstTemplateTypeParm: {
649 const SubstTemplateTypeParmType *Subst1
650 = cast<SubstTemplateTypeParmType>(T1);
651 const SubstTemplateTypeParmType *Subst2
652 = cast<SubstTemplateTypeParmType>(T2);
653 if (!IsStructurallyEquivalent(Context,
654 QualType(Subst1->getReplacedParameter(), 0),
655 QualType(Subst2->getReplacedParameter(), 0)))
656 return false;
657 if (!IsStructurallyEquivalent(Context,
658 Subst1->getReplacementType(),
659 Subst2->getReplacementType()))
660 return false;
661 break;
662 }
663
Douglas Gregorfb322d82011-01-14 05:11:40 +0000664 case Type::SubstTemplateTypeParmPack: {
665 const SubstTemplateTypeParmPackType *Subst1
666 = cast<SubstTemplateTypeParmPackType>(T1);
667 const SubstTemplateTypeParmPackType *Subst2
668 = cast<SubstTemplateTypeParmPackType>(T2);
669 if (!IsStructurallyEquivalent(Context,
670 QualType(Subst1->getReplacedParameter(), 0),
671 QualType(Subst2->getReplacedParameter(), 0)))
672 return false;
673 if (!IsStructurallyEquivalent(Context,
674 Subst1->getArgumentPack(),
675 Subst2->getArgumentPack()))
676 return false;
677 break;
678 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000679 case Type::TemplateSpecialization: {
680 const TemplateSpecializationType *Spec1
681 = cast<TemplateSpecializationType>(T1);
682 const TemplateSpecializationType *Spec2
683 = cast<TemplateSpecializationType>(T2);
684 if (!IsStructurallyEquivalent(Context,
685 Spec1->getTemplateName(),
686 Spec2->getTemplateName()))
687 return false;
688 if (Spec1->getNumArgs() != Spec2->getNumArgs())
689 return false;
690 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
691 if (!IsStructurallyEquivalent(Context,
692 Spec1->getArg(I), Spec2->getArg(I)))
693 return false;
694 }
695 break;
696 }
697
Abramo Bagnara6150c882010-05-11 21:36:43 +0000698 case Type::Elaborated: {
699 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
700 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
701 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
702 if (Elab1->getKeyword() != Elab2->getKeyword())
703 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000704 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000705 Elab1->getQualifier(),
706 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 return false;
708 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000709 Elab1->getNamedType(),
710 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000711 return false;
712 break;
713 }
714
John McCalle78aac42010-03-10 03:28:59 +0000715 case Type::InjectedClassName: {
716 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
717 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
718 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000719 Inj1->getInjectedSpecializationType(),
720 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000721 return false;
722 break;
723 }
724
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000725 case Type::DependentName: {
726 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
727 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000728 if (!IsStructurallyEquivalent(Context,
729 Typename1->getQualifier(),
730 Typename2->getQualifier()))
731 return false;
732 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
733 Typename2->getIdentifier()))
734 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000735
736 break;
737 }
738
John McCallc392f372010-06-11 00:33:02 +0000739 case Type::DependentTemplateSpecialization: {
740 const DependentTemplateSpecializationType *Spec1 =
741 cast<DependentTemplateSpecializationType>(T1);
742 const DependentTemplateSpecializationType *Spec2 =
743 cast<DependentTemplateSpecializationType>(T2);
744 if (!IsStructurallyEquivalent(Context,
745 Spec1->getQualifier(),
746 Spec2->getQualifier()))
747 return false;
748 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
749 Spec2->getIdentifier()))
750 return false;
751 if (Spec1->getNumArgs() != Spec2->getNumArgs())
752 return false;
753 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
754 if (!IsStructurallyEquivalent(Context,
755 Spec1->getArg(I), Spec2->getArg(I)))
756 return false;
757 }
758 break;
759 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000760
761 case Type::PackExpansion:
762 if (!IsStructurallyEquivalent(Context,
763 cast<PackExpansionType>(T1)->getPattern(),
764 cast<PackExpansionType>(T2)->getPattern()))
765 return false;
766 break;
767
Douglas Gregor3996e242010-02-15 22:01:00 +0000768 case Type::ObjCInterface: {
769 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
770 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
771 if (!IsStructurallyEquivalent(Context,
772 Iface1->getDecl(), Iface2->getDecl()))
773 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000774 break;
775 }
776
777 case Type::ObjCObject: {
778 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
779 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Obj1->getBaseType(),
782 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000783 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000784 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
785 return false;
786 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000787 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000788 Obj1->getProtocol(I),
789 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000790 return false;
791 }
792 break;
793 }
794
795 case Type::ObjCObjectPointer: {
796 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
797 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
798 if (!IsStructurallyEquivalent(Context,
799 Ptr1->getPointeeType(),
800 Ptr2->getPointeeType()))
801 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000802 break;
803 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000804
805 case Type::Atomic: {
806 if (!IsStructurallyEquivalent(Context,
807 cast<AtomicType>(T1)->getValueType(),
808 cast<AtomicType>(T2)->getValueType()))
809 return false;
810 break;
811 }
812
Douglas Gregor3996e242010-02-15 22:01:00 +0000813 } // end switch
814
815 return true;
816}
817
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000818/// \brief Determine structural equivalence of two fields.
819static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
820 FieldDecl *Field1, FieldDecl *Field2) {
821 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000822
823 // For anonymous structs/unions, match up the anonymous struct/union type
824 // declarations directly, so that we don't go off searching for anonymous
825 // types
826 if (Field1->isAnonymousStructOrUnion() &&
827 Field2->isAnonymousStructOrUnion()) {
828 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
829 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
830 return IsStructurallyEquivalent(Context, D1, D2);
831 }
832
833 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000834 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000835 if (Context.Complain) {
836 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
837 << Context.C2.getTypeDeclType(Owner2);
838 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
839 << Field2->getDeclName() << Field2->getType();
840 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
841 << Field1->getDeclName() << Field1->getType();
842 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000843 return false;
844 }
845
846 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000847 if (Context.Complain) {
848 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
849 << Context.C2.getTypeDeclType(Owner2);
850 if (Field1->isBitField()) {
851 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
852 << Field1->getDeclName() << Field1->getType()
853 << Field1->getBitWidthValue(Context.C1);
854 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
855 << Field2->getDeclName();
856 } else {
857 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
858 << Field2->getDeclName() << Field2->getType()
859 << Field2->getBitWidthValue(Context.C2);
860 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
861 << Field1->getDeclName();
862 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000863 }
864 return false;
865 }
866
867 if (Field1->isBitField()) {
868 // Make sure that the bit-fields are the same length.
869 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
870 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
871
872 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000873 if (Context.Complain) {
874 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
875 << Context.C2.getTypeDeclType(Owner2);
876 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
877 << Field2->getDeclName() << Field2->getType() << Bits2;
878 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
879 << Field1->getDeclName() << Field1->getType() << Bits1;
880 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000881 return false;
882 }
883 }
884
885 return true;
886}
887
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000888/// \brief Find the index of the given anonymous struct/union within its
889/// context.
890///
891/// \returns Returns the index of this anonymous struct/union in its context,
892/// including the next assigned index (if none of them match). Returns an
893/// empty option if the context is not a record, i.e.. if the anonymous
894/// struct/union is at namespace or block scope.
895static llvm::Optional<unsigned>
896findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
897 ASTContext &Context = Anon->getASTContext();
898 QualType AnonTy = Context.getRecordType(Anon);
899
900 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
901 if (!Owner)
902 return llvm::Optional<unsigned>();
903
904 unsigned Index = 0;
905 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
906 DEnd = Owner->noload_decls_end();
907 D != DEnd; ++D) {
908 FieldDecl *F = dyn_cast<FieldDecl>(*D);
909 if (!F || !F->isAnonymousStructOrUnion())
910 continue;
911
912 if (Context.hasSameType(F->getType(), AnonTy))
913 break;
914
915 ++Index;
916 }
917
918 return Index;
919}
920
Douglas Gregor3996e242010-02-15 22:01:00 +0000921/// \brief Determine structural equivalence of two records.
922static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
923 RecordDecl *D1, RecordDecl *D2) {
924 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000925 if (Context.Complain) {
926 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
927 << Context.C2.getTypeDeclType(D2);
928 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
929 << D1->getDeclName() << (unsigned)D1->getTagKind();
930 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000931 return false;
932 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000933
934 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
935 // If both anonymous structs/unions are in a record context, make sure
936 // they occur in the same location in the context records.
937 if (llvm::Optional<unsigned> Index1
938 = findAnonymousStructOrUnionIndex(D1)) {
939 if (llvm::Optional<unsigned> Index2
940 = findAnonymousStructOrUnionIndex(D2)) {
941 if (*Index1 != *Index2)
942 return false;
943 }
944 }
945 }
946
Douglas Gregore2e50d332010-12-01 01:36:18 +0000947 // If both declarations are class template specializations, we know
948 // the ODR applies, so check the template and template arguments.
949 ClassTemplateSpecializationDecl *Spec1
950 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
951 ClassTemplateSpecializationDecl *Spec2
952 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
953 if (Spec1 && Spec2) {
954 // Check that the specialized templates are the same.
955 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
956 Spec2->getSpecializedTemplate()))
957 return false;
958
959 // Check that the template arguments are the same.
960 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
961 return false;
962
963 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
964 if (!IsStructurallyEquivalent(Context,
965 Spec1->getTemplateArgs().get(I),
966 Spec2->getTemplateArgs().get(I)))
967 return false;
968 }
969 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000970 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000971 else if (Spec1 || Spec2)
972 return false;
973
Douglas Gregorb4964f72010-02-15 23:54:17 +0000974 // Compare the definitions of these two records. If either or both are
975 // incomplete, we assume that they are equivalent.
976 D1 = D1->getDefinition();
977 D2 = D2->getDefinition();
978 if (!D1 || !D2)
979 return true;
980
Douglas Gregor3996e242010-02-15 22:01:00 +0000981 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
982 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
983 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000984 if (Context.Complain) {
985 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(D2);
987 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
988 << D2CXX->getNumBases();
989 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
990 << D1CXX->getNumBases();
991 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000992 return false;
993 }
994
995 // Check the base classes.
996 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
997 BaseEnd1 = D1CXX->bases_end(),
998 Base2 = D2CXX->bases_begin();
999 Base1 != BaseEnd1;
1000 ++Base1, ++Base2) {
1001 if (!IsStructurallyEquivalent(Context,
1002 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001003 if (Context.Complain) {
1004 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1005 << Context.C2.getTypeDeclType(D2);
1006 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1007 << Base2->getType()
1008 << Base2->getSourceRange();
1009 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1010 << Base1->getType()
1011 << Base1->getSourceRange();
1012 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001013 return false;
1014 }
1015
1016 // Check virtual vs. non-virtual inheritance mismatch.
1017 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001018 if (Context.Complain) {
1019 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1020 << Context.C2.getTypeDeclType(D2);
1021 Context.Diag2(Base2->getLocStart(),
1022 diag::note_odr_virtual_base)
1023 << Base2->isVirtual() << Base2->getSourceRange();
1024 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1025 << Base1->isVirtual()
1026 << Base1->getSourceRange();
1027 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001028 return false;
1029 }
1030 }
1031 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001032 if (Context.Complain) {
1033 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1034 << Context.C2.getTypeDeclType(D2);
1035 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1036 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1037 << Base1->getType()
1038 << Base1->getSourceRange();
1039 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1040 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001041 return false;
1042 }
1043 }
1044
1045 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001046 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001047 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001048 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001049 Field1End = D1->field_end();
1050 Field1 != Field1End;
1051 ++Field1, ++Field2) {
1052 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001053 if (Context.Complain) {
1054 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1055 << Context.C2.getTypeDeclType(D2);
1056 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1057 << Field1->getDeclName() << Field1->getType();
1058 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1059 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001060 return false;
1061 }
1062
David Blaikie40ed2972012-06-06 20:45:41 +00001063 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001064 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001065 }
1066
1067 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001068 if (Context.Complain) {
1069 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1070 << Context.C2.getTypeDeclType(D2);
1071 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1072 << Field2->getDeclName() << Field2->getType();
1073 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1074 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001075 return false;
1076 }
1077
1078 return true;
1079}
1080
1081/// \brief Determine structural equivalence of two enums.
1082static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083 EnumDecl *D1, EnumDecl *D2) {
1084 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1085 EC2End = D2->enumerator_end();
1086 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1087 EC1End = D1->enumerator_end();
1088 EC1 != EC1End; ++EC1, ++EC2) {
1089 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001090 if (Context.Complain) {
1091 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1092 << Context.C2.getTypeDeclType(D2);
1093 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1094 << EC1->getDeclName()
1095 << EC1->getInitVal().toString(10);
1096 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1097 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001098 return false;
1099 }
1100
1101 llvm::APSInt Val1 = EC1->getInitVal();
1102 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001103 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001104 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001105 if (Context.Complain) {
1106 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1107 << Context.C2.getTypeDeclType(D2);
1108 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1109 << EC2->getDeclName()
1110 << EC2->getInitVal().toString(10);
1111 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1112 << EC1->getDeclName()
1113 << EC1->getInitVal().toString(10);
1114 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001115 return false;
1116 }
1117 }
1118
1119 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001120 if (Context.Complain) {
1121 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1122 << Context.C2.getTypeDeclType(D2);
1123 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1124 << EC2->getDeclName()
1125 << EC2->getInitVal().toString(10);
1126 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1127 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001128 return false;
1129 }
1130
1131 return true;
1132}
Douglas Gregora082a492010-11-30 19:14:50 +00001133
1134static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1135 TemplateParameterList *Params1,
1136 TemplateParameterList *Params2) {
1137 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001138 if (Context.Complain) {
1139 Context.Diag2(Params2->getTemplateLoc(),
1140 diag::err_odr_different_num_template_parameters)
1141 << Params1->size() << Params2->size();
1142 Context.Diag1(Params1->getTemplateLoc(),
1143 diag::note_odr_template_parameter_list);
1144 }
Douglas Gregora082a492010-11-30 19:14:50 +00001145 return false;
1146 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001147
Douglas Gregora082a492010-11-30 19:14:50 +00001148 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1149 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001150 if (Context.Complain) {
1151 Context.Diag2(Params2->getParam(I)->getLocation(),
1152 diag::err_odr_different_template_parameter_kind);
1153 Context.Diag1(Params1->getParam(I)->getLocation(),
1154 diag::note_odr_template_parameter_here);
1155 }
Douglas Gregora082a492010-11-30 19:14:50 +00001156 return false;
1157 }
1158
1159 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1160 Params2->getParam(I))) {
1161
1162 return false;
1163 }
1164 }
1165
1166 return true;
1167}
1168
1169static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1170 TemplateTypeParmDecl *D1,
1171 TemplateTypeParmDecl *D2) {
1172 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001173 if (Context.Complain) {
1174 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1175 << D2->isParameterPack();
1176 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1177 << D1->isParameterPack();
1178 }
Douglas Gregora082a492010-11-30 19:14:50 +00001179 return false;
1180 }
1181
1182 return true;
1183}
1184
1185static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1186 NonTypeTemplateParmDecl *D1,
1187 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001188 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001189 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001190 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1191 << D2->isParameterPack();
1192 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1193 << D1->isParameterPack();
1194 }
Douglas Gregora082a492010-11-30 19:14:50 +00001195 return false;
1196 }
Douglas Gregora082a492010-11-30 19:14:50 +00001197
1198 // Check types.
1199 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001200 if (Context.Complain) {
1201 Context.Diag2(D2->getLocation(),
1202 diag::err_odr_non_type_parameter_type_inconsistent)
1203 << D2->getType() << D1->getType();
1204 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1205 << D1->getType();
1206 }
Douglas Gregora082a492010-11-30 19:14:50 +00001207 return false;
1208 }
1209
1210 return true;
1211}
1212
1213static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1214 TemplateTemplateParmDecl *D1,
1215 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001216 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001217 if (Context.Complain) {
1218 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1219 << D2->isParameterPack();
1220 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1221 << D1->isParameterPack();
1222 }
Douglas Gregora082a492010-11-30 19:14:50 +00001223 return false;
1224 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001225
Douglas Gregora082a492010-11-30 19:14:50 +00001226 // Check template parameter lists.
1227 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1228 D2->getTemplateParameters());
1229}
1230
1231static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1232 ClassTemplateDecl *D1,
1233 ClassTemplateDecl *D2) {
1234 // Check template parameters.
1235 if (!IsStructurallyEquivalent(Context,
1236 D1->getTemplateParameters(),
1237 D2->getTemplateParameters()))
1238 return false;
1239
1240 // Check the templated declaration.
1241 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1242 D2->getTemplatedDecl());
1243}
1244
Douglas Gregor3996e242010-02-15 22:01:00 +00001245/// \brief Determine structural equivalence of two declarations.
1246static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1247 Decl *D1, Decl *D2) {
1248 // FIXME: Check for known structural equivalences via a callback of some sort.
1249
Douglas Gregorb4964f72010-02-15 23:54:17 +00001250 // Check whether we already know that these two declarations are not
1251 // structurally equivalent.
1252 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1253 D2->getCanonicalDecl())))
1254 return false;
1255
Douglas Gregor3996e242010-02-15 22:01:00 +00001256 // Determine whether we've already produced a tentative equivalence for D1.
1257 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1258 if (EquivToD1)
1259 return EquivToD1 == D2->getCanonicalDecl();
1260
1261 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1262 EquivToD1 = D2->getCanonicalDecl();
1263 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1264 return true;
1265}
1266
1267bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1268 Decl *D2) {
1269 if (!::IsStructurallyEquivalent(*this, D1, D2))
1270 return false;
1271
1272 return !Finish();
1273}
1274
1275bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1276 QualType T2) {
1277 if (!::IsStructurallyEquivalent(*this, T1, T2))
1278 return false;
1279
1280 return !Finish();
1281}
1282
1283bool StructuralEquivalenceContext::Finish() {
1284 while (!DeclsToCheck.empty()) {
1285 // Check the next declaration.
1286 Decl *D1 = DeclsToCheck.front();
1287 DeclsToCheck.pop_front();
1288
1289 Decl *D2 = TentativeEquivalences[D1];
1290 assert(D2 && "Unrecorded tentative equivalence?");
1291
Douglas Gregorb4964f72010-02-15 23:54:17 +00001292 bool Equivalent = true;
1293
Douglas Gregor3996e242010-02-15 22:01:00 +00001294 // FIXME: Switch on all declaration kinds. For now, we're just going to
1295 // check the obvious ones.
1296 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1297 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1298 // Check for equivalent structure names.
1299 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001300 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1301 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001302 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001303 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1304 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001305 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1306 !::IsStructurallyEquivalent(*this, Record1, Record2))
1307 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001308 } else {
1309 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001310 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001311 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001312 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001313 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1314 // Check for equivalent enum names.
1315 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001316 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1317 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001318 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001319 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1320 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001321 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1322 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1323 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001324 } else {
1325 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001326 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001327 }
Richard Smithdda56e42011-04-15 14:24:37 +00001328 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1329 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001330 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001331 Typedef2->getIdentifier()) ||
1332 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001333 Typedef1->getUnderlyingType(),
1334 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001335 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001336 } else {
1337 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001338 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001339 }
Douglas Gregora082a492010-11-30 19:14:50 +00001340 } else if (ClassTemplateDecl *ClassTemplate1
1341 = dyn_cast<ClassTemplateDecl>(D1)) {
1342 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1343 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1344 ClassTemplate2->getIdentifier()) ||
1345 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1346 Equivalent = false;
1347 } else {
1348 // Class template/non-class-template mismatch.
1349 Equivalent = false;
1350 }
1351 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1352 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1353 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1354 Equivalent = false;
1355 } else {
1356 // Kind mismatch.
1357 Equivalent = false;
1358 }
1359 } else if (NonTypeTemplateParmDecl *NTTP1
1360 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1361 if (NonTypeTemplateParmDecl *NTTP2
1362 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1363 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1364 Equivalent = false;
1365 } else {
1366 // Kind mismatch.
1367 Equivalent = false;
1368 }
1369 } else if (TemplateTemplateParmDecl *TTP1
1370 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1371 if (TemplateTemplateParmDecl *TTP2
1372 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1373 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1374 Equivalent = false;
1375 } else {
1376 // Kind mismatch.
1377 Equivalent = false;
1378 }
1379 }
1380
Douglas Gregorb4964f72010-02-15 23:54:17 +00001381 if (!Equivalent) {
1382 // Note that these two declarations are not equivalent (and we already
1383 // know about it).
1384 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1385 D2->getCanonicalDecl()));
1386 return true;
1387 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001388 // FIXME: Check other declaration kinds!
1389 }
1390
1391 return false;
1392}
1393
1394//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001395// Import Types
1396//----------------------------------------------------------------------------
1397
John McCall424cec92011-01-19 06:33:43 +00001398QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001399 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1400 << T->getTypeClassName();
1401 return QualType();
1402}
1403
John McCall424cec92011-01-19 06:33:43 +00001404QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001405 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001406#define SHARED_SINGLETON_TYPE(Expansion)
1407#define BUILTIN_TYPE(Id, SingletonId) \
1408 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1409#include "clang/AST/BuiltinTypes.def"
1410
1411 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1412 // context supports C++.
1413
1414 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1415 // context supports ObjC.
1416
Douglas Gregor96e578d2010-02-05 17:54:41 +00001417 case BuiltinType::Char_U:
1418 // The context we're importing from has an unsigned 'char'. If we're
1419 // importing into a context with a signed 'char', translate to
1420 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001421 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001422 return Importer.getToContext().UnsignedCharTy;
1423
1424 return Importer.getToContext().CharTy;
1425
Douglas Gregor96e578d2010-02-05 17:54:41 +00001426 case BuiltinType::Char_S:
1427 // The context we're importing from has an unsigned 'char'. If we're
1428 // importing into a context with a signed 'char', translate to
1429 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001430 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431 return Importer.getToContext().SignedCharTy;
1432
1433 return Importer.getToContext().CharTy;
1434
Chris Lattnerad3467e2010-12-25 23:25:43 +00001435 case BuiltinType::WChar_S:
1436 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001437 // FIXME: If not in C++, shall we translate to the C equivalent of
1438 // wchar_t?
1439 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001440 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001441
1442 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001443}
1444
John McCall424cec92011-01-19 06:33:43 +00001445QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001446 QualType ToElementType = Importer.Import(T->getElementType());
1447 if (ToElementType.isNull())
1448 return QualType();
1449
1450 return Importer.getToContext().getComplexType(ToElementType);
1451}
1452
John McCall424cec92011-01-19 06:33:43 +00001453QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001454 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1455 if (ToPointeeType.isNull())
1456 return QualType();
1457
1458 return Importer.getToContext().getPointerType(ToPointeeType);
1459}
1460
John McCall424cec92011-01-19 06:33:43 +00001461QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001462 // FIXME: Check for blocks support in "to" context.
1463 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1464 if (ToPointeeType.isNull())
1465 return QualType();
1466
1467 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1468}
1469
John McCall424cec92011-01-19 06:33:43 +00001470QualType
1471ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001472 // FIXME: Check for C++ support in "to" context.
1473 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1474 if (ToPointeeType.isNull())
1475 return QualType();
1476
1477 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1478}
1479
John McCall424cec92011-01-19 06:33:43 +00001480QualType
1481ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001482 // FIXME: Check for C++0x support in "to" context.
1483 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1484 if (ToPointeeType.isNull())
1485 return QualType();
1486
1487 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1488}
1489
John McCall424cec92011-01-19 06:33:43 +00001490QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001491 // FIXME: Check for C++ support in "to" context.
1492 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1493 if (ToPointeeType.isNull())
1494 return QualType();
1495
1496 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1497 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1498 ClassType.getTypePtr());
1499}
1500
John McCall424cec92011-01-19 06:33:43 +00001501QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502 QualType ToElementType = Importer.Import(T->getElementType());
1503 if (ToElementType.isNull())
1504 return QualType();
1505
1506 return Importer.getToContext().getConstantArrayType(ToElementType,
1507 T->getSize(),
1508 T->getSizeModifier(),
1509 T->getIndexTypeCVRQualifiers());
1510}
1511
John McCall424cec92011-01-19 06:33:43 +00001512QualType
1513ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001514 QualType ToElementType = Importer.Import(T->getElementType());
1515 if (ToElementType.isNull())
1516 return QualType();
1517
1518 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1519 T->getSizeModifier(),
1520 T->getIndexTypeCVRQualifiers());
1521}
1522
John McCall424cec92011-01-19 06:33:43 +00001523QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 QualType ToElementType = Importer.Import(T->getElementType());
1525 if (ToElementType.isNull())
1526 return QualType();
1527
1528 Expr *Size = Importer.Import(T->getSizeExpr());
1529 if (!Size)
1530 return QualType();
1531
1532 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1533 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1534 T->getSizeModifier(),
1535 T->getIndexTypeCVRQualifiers(),
1536 Brackets);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 QualType ToElementType = Importer.Import(T->getElementType());
1541 if (ToElementType.isNull())
1542 return QualType();
1543
1544 return Importer.getToContext().getVectorType(ToElementType,
1545 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001546 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 QualType ToElementType = Importer.Import(T->getElementType());
1551 if (ToElementType.isNull())
1552 return QualType();
1553
1554 return Importer.getToContext().getExtVectorType(ToElementType,
1555 T->getNumElements());
1556}
1557
John McCall424cec92011-01-19 06:33:43 +00001558QualType
1559ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 // FIXME: What happens if we're importing a function without a prototype
1561 // into C++? Should we make it variadic?
1562 QualType ToResultType = Importer.Import(T->getResultType());
1563 if (ToResultType.isNull())
1564 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001565
Douglas Gregor96e578d2010-02-05 17:54:41 +00001566 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001567 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568}
1569
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001570QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571 QualType ToResultType = Importer.Import(T->getResultType());
1572 if (ToResultType.isNull())
1573 return QualType();
1574
1575 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001576 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001577 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1578 AEnd = T->arg_type_end();
1579 A != AEnd; ++A) {
1580 QualType ArgType = Importer.Import(*A);
1581 if (ArgType.isNull())
1582 return QualType();
1583 ArgTypes.push_back(ArgType);
1584 }
1585
1586 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001587 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1589 EEnd = T->exception_end();
1590 E != EEnd; ++E) {
1591 QualType ExceptionType = Importer.Import(*E);
1592 if (ExceptionType.isNull())
1593 return QualType();
1594 ExceptionTypes.push_back(ExceptionType);
1595 }
John McCalldb40c7f2010-12-14 08:05:40 +00001596
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001597 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1598 FunctionProtoType::ExtProtoInfo ToEPI;
1599
1600 ToEPI.ExtInfo = FromEPI.ExtInfo;
1601 ToEPI.Variadic = FromEPI.Variadic;
1602 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1603 ToEPI.TypeQuals = FromEPI.TypeQuals;
1604 ToEPI.RefQualifier = FromEPI.RefQualifier;
1605 ToEPI.NumExceptions = ExceptionTypes.size();
1606 ToEPI.Exceptions = ExceptionTypes.data();
1607 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001608 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1609 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1610 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001611 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001612 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001613 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001614
Douglas Gregor96e578d2010-02-05 17:54:41 +00001615 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001616 ArgTypes.size(), ToEPI);
1617}
1618
Sean Callananda6df8a2011-08-11 16:56:07 +00001619QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1620 QualType ToInnerType = Importer.Import(T->getInnerType());
1621 if (ToInnerType.isNull())
1622 return QualType();
1623
1624 return Importer.getToContext().getParenType(ToInnerType);
1625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001628 TypedefNameDecl *ToDecl
1629 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001630 if (!ToDecl)
1631 return QualType();
1632
1633 return Importer.getToContext().getTypeDeclType(ToDecl);
1634}
1635
John McCall424cec92011-01-19 06:33:43 +00001636QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001637 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1638 if (!ToExpr)
1639 return QualType();
1640
1641 return Importer.getToContext().getTypeOfExprType(ToExpr);
1642}
1643
John McCall424cec92011-01-19 06:33:43 +00001644QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001645 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1646 if (ToUnderlyingType.isNull())
1647 return QualType();
1648
1649 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1650}
1651
John McCall424cec92011-01-19 06:33:43 +00001652QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001653 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1655 if (!ToExpr)
1656 return QualType();
1657
Douglas Gregor81495f32012-02-12 18:42:33 +00001658 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1659 if (UnderlyingType.isNull())
1660 return QualType();
1661
1662 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001663}
1664
Alexis Hunte852b102011-05-24 22:41:36 +00001665QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1666 QualType ToBaseType = Importer.Import(T->getBaseType());
1667 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1668 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1669 return QualType();
1670
1671 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1672 ToUnderlyingType,
1673 T->getUTTKind());
1674}
1675
Richard Smith30482bc2011-02-20 03:19:35 +00001676QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1677 // FIXME: Make sure that the "to" context supports C++0x!
1678 QualType FromDeduced = T->getDeducedType();
1679 QualType ToDeduced;
1680 if (!FromDeduced.isNull()) {
1681 ToDeduced = Importer.Import(FromDeduced);
1682 if (ToDeduced.isNull())
1683 return QualType();
1684 }
1685
1686 return Importer.getToContext().getAutoType(ToDeduced);
1687}
1688
John McCall424cec92011-01-19 06:33:43 +00001689QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001690 RecordDecl *ToDecl
1691 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1692 if (!ToDecl)
1693 return QualType();
1694
1695 return Importer.getToContext().getTagDeclType(ToDecl);
1696}
1697
John McCall424cec92011-01-19 06:33:43 +00001698QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001699 EnumDecl *ToDecl
1700 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1701 if (!ToDecl)
1702 return QualType();
1703
1704 return Importer.getToContext().getTagDeclType(ToDecl);
1705}
1706
Douglas Gregore2e50d332010-12-01 01:36:18 +00001707QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001708 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001709 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1710 if (ToTemplate.isNull())
1711 return QualType();
1712
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001713 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001714 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1715 return QualType();
1716
1717 QualType ToCanonType;
1718 if (!QualType(T, 0).isCanonical()) {
1719 QualType FromCanonType
1720 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1721 ToCanonType =Importer.Import(FromCanonType);
1722 if (ToCanonType.isNull())
1723 return QualType();
1724 }
1725 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1726 ToTemplateArgs.data(),
1727 ToTemplateArgs.size(),
1728 ToCanonType);
1729}
1730
John McCall424cec92011-01-19 06:33:43 +00001731QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001732 NestedNameSpecifier *ToQualifier = 0;
1733 // Note: the qualifier in an ElaboratedType is optional.
1734 if (T->getQualifier()) {
1735 ToQualifier = Importer.Import(T->getQualifier());
1736 if (!ToQualifier)
1737 return QualType();
1738 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001739
1740 QualType ToNamedType = Importer.Import(T->getNamedType());
1741 if (ToNamedType.isNull())
1742 return QualType();
1743
Abramo Bagnara6150c882010-05-11 21:36:43 +00001744 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1745 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001746}
1747
John McCall424cec92011-01-19 06:33:43 +00001748QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001749 ObjCInterfaceDecl *Class
1750 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1751 if (!Class)
1752 return QualType();
1753
John McCall8b07ec22010-05-15 11:32:37 +00001754 return Importer.getToContext().getObjCInterfaceType(Class);
1755}
1756
John McCall424cec92011-01-19 06:33:43 +00001757QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001758 QualType ToBaseType = Importer.Import(T->getBaseType());
1759 if (ToBaseType.isNull())
1760 return QualType();
1761
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001762 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001763 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001764 PEnd = T->qual_end();
1765 P != PEnd; ++P) {
1766 ObjCProtocolDecl *Protocol
1767 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1768 if (!Protocol)
1769 return QualType();
1770 Protocols.push_back(Protocol);
1771 }
1772
John McCall8b07ec22010-05-15 11:32:37 +00001773 return Importer.getToContext().getObjCObjectType(ToBaseType,
1774 Protocols.data(),
1775 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001776}
1777
John McCall424cec92011-01-19 06:33:43 +00001778QualType
1779ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001780 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1781 if (ToPointeeType.isNull())
1782 return QualType();
1783
John McCall8b07ec22010-05-15 11:32:37 +00001784 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001785}
1786
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001787//----------------------------------------------------------------------------
1788// Import Declarations
1789//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001790bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1791 DeclContext *&LexicalDC,
1792 DeclarationName &Name,
1793 SourceLocation &Loc) {
1794 // Import the context of this declaration.
1795 DC = Importer.ImportContext(D->getDeclContext());
1796 if (!DC)
1797 return true;
1798
1799 LexicalDC = DC;
1800 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1801 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1802 if (!LexicalDC)
1803 return true;
1804 }
1805
1806 // Import the name of this declaration.
1807 Name = Importer.Import(D->getDeclName());
1808 if (D->getDeclName() && !Name)
1809 return true;
1810
1811 // Import the location of this declaration.
1812 Loc = Importer.Import(D->getLocation());
1813 return false;
1814}
1815
Douglas Gregord451ea92011-07-29 23:31:30 +00001816void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1817 if (!FromD)
1818 return;
1819
1820 if (!ToD) {
1821 ToD = Importer.Import(FromD);
1822 if (!ToD)
1823 return;
1824 }
1825
1826 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1827 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1828 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1829 ImportDefinition(FromRecord, ToRecord);
1830 }
1831 }
1832 return;
1833 }
1834
1835 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1836 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1837 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1838 ImportDefinition(FromEnum, ToEnum);
1839 }
1840 }
1841 return;
1842 }
1843}
1844
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001845void
1846ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1847 DeclarationNameInfo& To) {
1848 // NOTE: To.Name and To.Loc are already imported.
1849 // We only have to import To.LocInfo.
1850 switch (To.getName().getNameKind()) {
1851 case DeclarationName::Identifier:
1852 case DeclarationName::ObjCZeroArgSelector:
1853 case DeclarationName::ObjCOneArgSelector:
1854 case DeclarationName::ObjCMultiArgSelector:
1855 case DeclarationName::CXXUsingDirective:
1856 return;
1857
1858 case DeclarationName::CXXOperatorName: {
1859 SourceRange Range = From.getCXXOperatorNameRange();
1860 To.setCXXOperatorNameRange(Importer.Import(Range));
1861 return;
1862 }
1863 case DeclarationName::CXXLiteralOperatorName: {
1864 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1865 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1866 return;
1867 }
1868 case DeclarationName::CXXConstructorName:
1869 case DeclarationName::CXXDestructorName:
1870 case DeclarationName::CXXConversionFunctionName: {
1871 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1872 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1873 return;
1874 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001875 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001876 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001877}
1878
Douglas Gregor2e15c842012-02-01 21:00:38 +00001879void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001880 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001881 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001882 return;
1883 }
1884
Douglas Gregor968d6332010-02-21 18:24:45 +00001885 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1886 FromEnd = FromDC->decls_end();
1887 From != FromEnd;
1888 ++From)
1889 Importer.Import(*From);
1890}
1891
Douglas Gregord451ea92011-07-29 23:31:30 +00001892bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001893 ImportDefinitionKind Kind) {
1894 if (To->getDefinition() || To->isBeingDefined()) {
1895 if (Kind == IDK_Everything)
1896 ImportDeclContext(From, /*ForceImport=*/true);
1897
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001899 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001900
1901 To->startDefinition();
1902
1903 // Add base classes.
1904 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1905 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001906
1907 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1908 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1909 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1910 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1911 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1912 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1913 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1914 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1915 ToData.Aggregate = FromData.Aggregate;
1916 ToData.PlainOldData = FromData.PlainOldData;
1917 ToData.Empty = FromData.Empty;
1918 ToData.Polymorphic = FromData.Polymorphic;
1919 ToData.Abstract = FromData.Abstract;
1920 ToData.IsStandardLayout = FromData.IsStandardLayout;
1921 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1922 ToData.HasPrivateFields = FromData.HasPrivateFields;
1923 ToData.HasProtectedFields = FromData.HasProtectedFields;
1924 ToData.HasPublicFields = FromData.HasPublicFields;
1925 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001926 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001927 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001928 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1929 ToData.HasConstexprNonCopyMoveConstructor
1930 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001931 ToData.DefaultedDefaultConstructorIsConstexpr
1932 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001933 ToData.HasConstexprDefaultConstructor
1934 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001935 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1936 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1937 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1938 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1939 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001940 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001941 ToData.HasNonLiteralTypeFieldsOrBases
1942 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001943 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001944 ToData.UserProvidedDefaultConstructor
1945 = FromData.UserProvidedDefaultConstructor;
1946 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1947 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1948 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1949 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1950 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1951 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1952 ToData.FailedImplicitMoveConstructor
1953 = FromData.FailedImplicitMoveConstructor;
1954 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001955 ToData.IsLambda = FromData.IsLambda;
1956
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001957 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001958 for (CXXRecordDecl::base_class_iterator
1959 Base1 = FromCXX->bases_begin(),
1960 FromBaseEnd = FromCXX->bases_end();
1961 Base1 != FromBaseEnd;
1962 ++Base1) {
1963 QualType T = Importer.Import(Base1->getType());
1964 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001965 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001966
1967 SourceLocation EllipsisLoc;
1968 if (Base1->isPackExpansion())
1969 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001970
1971 // Ensure that we have a definition for the base.
1972 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1973
Douglas Gregore2e50d332010-12-01 01:36:18 +00001974 Bases.push_back(
1975 new (Importer.getToContext())
1976 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1977 Base1->isVirtual(),
1978 Base1->isBaseOfClass(),
1979 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001980 Importer.Import(Base1->getTypeSourceInfo()),
1981 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001982 }
1983 if (!Bases.empty())
1984 ToCXX->setBases(Bases.data(), Bases.size());
1985 }
1986
Douglas Gregor2e15c842012-02-01 21:00:38 +00001987 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001988 ImportDeclContext(From, /*ForceImport=*/true);
1989
Douglas Gregore2e50d332010-12-01 01:36:18 +00001990 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001991 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001992}
1993
Douglas Gregord451ea92011-07-29 23:31:30 +00001994bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001995 ImportDefinitionKind Kind) {
1996 if (To->getDefinition() || To->isBeingDefined()) {
1997 if (Kind == IDK_Everything)
1998 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001999 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002000 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002001
2002 To->startDefinition();
2003
2004 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2005 if (T.isNull())
2006 return true;
2007
2008 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2009 if (ToPromotionType.isNull())
2010 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002011
2012 if (shouldForceImportDeclContext(Kind))
2013 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002014
2015 // FIXME: we might need to merge the number of positive or negative bits
2016 // if the enumerator lists don't match.
2017 To->completeDefinition(T, ToPromotionType,
2018 From->getNumPositiveBits(),
2019 From->getNumNegativeBits());
2020 return false;
2021}
2022
Douglas Gregora082a492010-11-30 19:14:50 +00002023TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2024 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002025 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002026 ToParams.reserve(Params->size());
2027 for (TemplateParameterList::iterator P = Params->begin(),
2028 PEnd = Params->end();
2029 P != PEnd; ++P) {
2030 Decl *To = Importer.Import(*P);
2031 if (!To)
2032 return 0;
2033
2034 ToParams.push_back(cast<NamedDecl>(To));
2035 }
2036
2037 return TemplateParameterList::Create(Importer.getToContext(),
2038 Importer.Import(Params->getTemplateLoc()),
2039 Importer.Import(Params->getLAngleLoc()),
2040 ToParams.data(), ToParams.size(),
2041 Importer.Import(Params->getRAngleLoc()));
2042}
2043
Douglas Gregore2e50d332010-12-01 01:36:18 +00002044TemplateArgument
2045ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2046 switch (From.getKind()) {
2047 case TemplateArgument::Null:
2048 return TemplateArgument();
2049
2050 case TemplateArgument::Type: {
2051 QualType ToType = Importer.Import(From.getAsType());
2052 if (ToType.isNull())
2053 return TemplateArgument();
2054 return TemplateArgument(ToType);
2055 }
2056
2057 case TemplateArgument::Integral: {
2058 QualType ToType = Importer.Import(From.getIntegralType());
2059 if (ToType.isNull())
2060 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002061 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002062 }
2063
Eli Friedmanb826a002012-09-26 02:36:12 +00002064 case TemplateArgument::Declaration: {
2065 ValueDecl *FromD = From.getAsDecl();
2066 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2067 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002068 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002069 }
2070
2071 case TemplateArgument::NullPtr: {
2072 QualType ToType = Importer.Import(From.getNullPtrType());
2073 if (ToType.isNull())
2074 return TemplateArgument();
2075 return TemplateArgument(ToType, /*isNullPtr*/true);
2076 }
2077
Douglas Gregore2e50d332010-12-01 01:36:18 +00002078 case TemplateArgument::Template: {
2079 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2080 if (ToTemplate.isNull())
2081 return TemplateArgument();
2082
2083 return TemplateArgument(ToTemplate);
2084 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002085
2086 case TemplateArgument::TemplateExpansion: {
2087 TemplateName ToTemplate
2088 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2089 if (ToTemplate.isNull())
2090 return TemplateArgument();
2091
Douglas Gregore1d60df2011-01-14 23:41:42 +00002092 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002093 }
2094
Douglas Gregore2e50d332010-12-01 01:36:18 +00002095 case TemplateArgument::Expression:
2096 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2097 return TemplateArgument(ToExpr);
2098 return TemplateArgument();
2099
2100 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002101 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002102 ToPack.reserve(From.pack_size());
2103 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2104 return TemplateArgument();
2105
2106 TemplateArgument *ToArgs
2107 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2108 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2109 return TemplateArgument(ToArgs, ToPack.size());
2110 }
2111 }
2112
2113 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002114}
2115
2116bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2117 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002118 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002119 for (unsigned I = 0; I != NumFromArgs; ++I) {
2120 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2121 if (To.isNull() && !FromArgs[I].isNull())
2122 return true;
2123
2124 ToArgs.push_back(To);
2125 }
2126
2127 return false;
2128}
2129
Douglas Gregor5c73e912010-02-11 00:48:18 +00002130bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002131 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002132 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002133 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002134 Importer.getNonEquivalentDecls(),
2135 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002136 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002137}
2138
Douglas Gregor98c10182010-02-12 22:17:39 +00002139bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002140 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002141 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002142 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002143 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002144}
2145
Douglas Gregora082a492010-11-30 19:14:50 +00002146bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2147 ClassTemplateDecl *To) {
2148 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2149 Importer.getToContext(),
2150 Importer.getNonEquivalentDecls());
2151 return Ctx.IsStructurallyEquivalent(From, To);
2152}
2153
Douglas Gregore4c83e42010-02-09 22:48:33 +00002154Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002155 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002156 << D->getDeclKindName();
2157 return 0;
2158}
2159
Sean Callanan65198272011-11-17 23:20:56 +00002160Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2161 TranslationUnitDecl *ToD =
2162 Importer.getToContext().getTranslationUnitDecl();
2163
2164 Importer.Imported(D, ToD);
2165
2166 return ToD;
2167}
2168
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002169Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2170 // Import the major distinguishing characteristics of this namespace.
2171 DeclContext *DC, *LexicalDC;
2172 DeclarationName Name;
2173 SourceLocation Loc;
2174 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2175 return 0;
2176
2177 NamespaceDecl *MergeWithNamespace = 0;
2178 if (!Name) {
2179 // This is an anonymous namespace. Adopt an existing anonymous
2180 // namespace if we can.
2181 // FIXME: Not testable.
2182 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2183 MergeWithNamespace = TU->getAnonymousNamespace();
2184 else
2185 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2186 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002187 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002188 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2189 DC->localUncachedLookup(Name, FoundDecls);
2190 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2191 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002192 continue;
2193
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002194 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002195 MergeWithNamespace = FoundNS;
2196 ConflictingDecls.clear();
2197 break;
2198 }
2199
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002200 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002201 }
2202
2203 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002204 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002205 ConflictingDecls.data(),
2206 ConflictingDecls.size());
2207 }
2208 }
2209
2210 // Create the "to" namespace, if needed.
2211 NamespaceDecl *ToNamespace = MergeWithNamespace;
2212 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002213 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002214 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002215 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002216 Loc, Name.getAsIdentifierInfo(),
2217 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002218 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002219 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002220
2221 // If this is an anonymous namespace, register it as the anonymous
2222 // namespace within its context.
2223 if (!Name) {
2224 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2225 TU->setAnonymousNamespace(ToNamespace);
2226 else
2227 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2228 }
2229 }
2230 Importer.Imported(D, ToNamespace);
2231
2232 ImportDeclContext(D);
2233
2234 return ToNamespace;
2235}
2236
Richard Smithdda56e42011-04-15 14:24:37 +00002237Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002238 // Import the major distinguishing characteristics of this typedef.
2239 DeclContext *DC, *LexicalDC;
2240 DeclarationName Name;
2241 SourceLocation Loc;
2242 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2243 return 0;
2244
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002245 // If this typedef is not in block scope, determine whether we've
2246 // seen a typedef with the same name (that we can merge with) or any
2247 // other entity by that name (which name lookup could conflict with).
2248 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002249 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002250 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002251 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2252 DC->localUncachedLookup(Name, FoundDecls);
2253 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2254 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002255 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002256 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002257 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002258 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2259 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002260 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002261 }
2262
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002263 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002264 }
2265
2266 if (!ConflictingDecls.empty()) {
2267 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2268 ConflictingDecls.data(),
2269 ConflictingDecls.size());
2270 if (!Name)
2271 return 0;
2272 }
2273 }
2274
Douglas Gregorb4964f72010-02-15 23:54:17 +00002275 // Import the underlying type of this typedef;
2276 QualType T = Importer.Import(D->getUnderlyingType());
2277 if (T.isNull())
2278 return 0;
2279
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002280 // Create the new typedef node.
2281 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002282 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002283 TypedefNameDecl *ToTypedef;
2284 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002285 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2286 StartL, Loc,
2287 Name.getAsIdentifierInfo(),
2288 TInfo);
2289 else
Richard Smithdda56e42011-04-15 14:24:37 +00002290 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2291 StartL, Loc,
2292 Name.getAsIdentifierInfo(),
2293 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002294
Douglas Gregordd483172010-02-22 17:42:47 +00002295 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002296 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002297 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002298 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002299
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002300 return ToTypedef;
2301}
2302
Richard Smithdda56e42011-04-15 14:24:37 +00002303Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2304 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2305}
2306
2307Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2308 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2309}
2310
Douglas Gregor98c10182010-02-12 22:17:39 +00002311Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2312 // Import the major distinguishing characteristics of this enum.
2313 DeclContext *DC, *LexicalDC;
2314 DeclarationName Name;
2315 SourceLocation Loc;
2316 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2317 return 0;
2318
2319 // Figure out what enum name we're looking for.
2320 unsigned IDNS = Decl::IDNS_Tag;
2321 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002322 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2323 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002324 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002325 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002326 IDNS |= Decl::IDNS_Ordinary;
2327
2328 // We may already have an enum of the same name; try to find and match it.
2329 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002330 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002331 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2332 DC->localUncachedLookup(SearchName, FoundDecls);
2333 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2334 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002335 continue;
2336
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002337 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002338 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002339 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2340 Found = Tag->getDecl();
2341 }
2342
2343 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002344 if (IsStructuralMatch(D, FoundEnum))
2345 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002346 }
2347
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002348 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002349 }
2350
2351 if (!ConflictingDecls.empty()) {
2352 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2353 ConflictingDecls.data(),
2354 ConflictingDecls.size());
2355 }
2356 }
2357
2358 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002359 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2360 Importer.Import(D->getLocStart()),
2361 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002362 D->isScoped(), D->isScopedUsingClassTag(),
2363 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002364 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002365 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002366 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002367 D2->setLexicalDeclContext(LexicalDC);
2368 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002369 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002370
2371 // Import the integer type.
2372 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2373 if (ToIntegerType.isNull())
2374 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002375 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002376
2377 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002378 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002379 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002380
Douglas Gregor3996e242010-02-15 22:01:00 +00002381 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002382}
2383
Douglas Gregor5c73e912010-02-11 00:48:18 +00002384Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2385 // If this record has a definition in the translation unit we're coming from,
2386 // but this particular declaration is not that definition, import the
2387 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002388 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002389 if (Definition && Definition != D) {
2390 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002391 if (!ImportedDef)
2392 return 0;
2393
2394 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002395 }
2396
2397 // Import the major distinguishing characteristics of this record.
2398 DeclContext *DC, *LexicalDC;
2399 DeclarationName Name;
2400 SourceLocation Loc;
2401 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2402 return 0;
2403
2404 // Figure out what structure name we're looking for.
2405 unsigned IDNS = Decl::IDNS_Tag;
2406 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002407 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2408 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002409 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002410 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002411 IDNS |= Decl::IDNS_Ordinary;
2412
2413 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002414 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002415 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002416 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002417 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2418 DC->localUncachedLookup(SearchName, FoundDecls);
2419 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2420 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002421 continue;
2422
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002423 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002424 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002425 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2426 Found = Tag->getDecl();
2427 }
2428
2429 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002430 if (D->isAnonymousStructOrUnion() &&
2431 FoundRecord->isAnonymousStructOrUnion()) {
2432 // If both anonymous structs/unions are in a record context, make sure
2433 // they occur in the same location in the context records.
2434 if (llvm::Optional<unsigned> Index1
2435 = findAnonymousStructOrUnionIndex(D)) {
2436 if (llvm::Optional<unsigned> Index2
2437 = findAnonymousStructOrUnionIndex(FoundRecord)) {
2438 if (*Index1 != *Index2)
2439 continue;
2440 }
2441 }
2442 }
2443
Douglas Gregor25791052010-02-12 00:09:27 +00002444 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002445 if ((SearchName && !D->isCompleteDefinition())
2446 || (D->isCompleteDefinition() &&
2447 D->isAnonymousStructOrUnion()
2448 == FoundDef->isAnonymousStructOrUnion() &&
2449 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002450 // The record types structurally match, or the "from" translation
2451 // unit only had a forward declaration anyway; call it the same
2452 // function.
2453 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002454 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002455 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002456 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002457 // We have a forward declaration of this type, so adopt that forward
2458 // declaration rather than building a new one.
2459 AdoptDecl = FoundRecord;
2460 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002461 } else if (!SearchName) {
2462 continue;
2463 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002464 }
2465
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002466 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002467 }
2468
Douglas Gregordd6006f2012-07-17 21:16:27 +00002469 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002470 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2471 ConflictingDecls.data(),
2472 ConflictingDecls.size());
2473 }
2474 }
2475
2476 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002477 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002478 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002479 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002480 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002481 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002482 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002483 DC, StartLoc, Loc,
2484 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002485 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002486 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002487 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002488 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002489 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002490 }
Douglas Gregor14454802011-02-25 02:25:35 +00002491
2492 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002493 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002494 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002495 if (D->isAnonymousStructOrUnion())
2496 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002497 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002498
Douglas Gregor3996e242010-02-15 22:01:00 +00002499 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002500
Douglas Gregor95d82832012-01-24 18:36:04 +00002501 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002502 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002503
Douglas Gregor3996e242010-02-15 22:01:00 +00002504 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002505}
2506
Douglas Gregor98c10182010-02-12 22:17:39 +00002507Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2508 // Import the major distinguishing characteristics of this enumerator.
2509 DeclContext *DC, *LexicalDC;
2510 DeclarationName Name;
2511 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002512 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002513 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002514
2515 QualType T = Importer.Import(D->getType());
2516 if (T.isNull())
2517 return 0;
2518
Douglas Gregor98c10182010-02-12 22:17:39 +00002519 // Determine whether there are any other declarations with the same name and
2520 // in the same context.
2521 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002522 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002523 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002524 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2525 DC->localUncachedLookup(Name, FoundDecls);
2526 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2527 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002528 continue;
2529
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002530 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 }
2532
2533 if (!ConflictingDecls.empty()) {
2534 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2535 ConflictingDecls.data(),
2536 ConflictingDecls.size());
2537 if (!Name)
2538 return 0;
2539 }
2540 }
2541
2542 Expr *Init = Importer.Import(D->getInitExpr());
2543 if (D->getInitExpr() && !Init)
2544 return 0;
2545
2546 EnumConstantDecl *ToEnumerator
2547 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2548 Name.getAsIdentifierInfo(), T,
2549 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002550 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002551 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002552 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002553 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002554 return ToEnumerator;
2555}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002556
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002557Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2558 // Import the major distinguishing characteristics of this function.
2559 DeclContext *DC, *LexicalDC;
2560 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002561 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002562 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002563 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002564
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002565 // Try to find a function in our own ("to") context with the same name, same
2566 // type, and in the same context as the function we're importing.
2567 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002568 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002569 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002570 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2571 DC->localUncachedLookup(Name, FoundDecls);
2572 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2573 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002574 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002575
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002576 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002577 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2578 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002579 if (Importer.IsStructurallyEquivalent(D->getType(),
2580 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002581 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002582 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002583 }
2584
2585 // FIXME: Check for overloading more carefully, e.g., by boosting
2586 // Sema::IsOverload out to the AST library.
2587
2588 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002589 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002590 continue;
2591
2592 // Complain about inconsistent function types.
2593 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002594 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002595 Importer.ToDiag(FoundFunction->getLocation(),
2596 diag::note_odr_value_here)
2597 << FoundFunction->getType();
2598 }
2599 }
2600
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002601 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002602 }
2603
2604 if (!ConflictingDecls.empty()) {
2605 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2606 ConflictingDecls.data(),
2607 ConflictingDecls.size());
2608 if (!Name)
2609 return 0;
2610 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002611 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002612
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002613 DeclarationNameInfo NameInfo(Name, Loc);
2614 // Import additional name location/type info.
2615 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2616
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002617 QualType FromTy = D->getType();
2618 bool usedDifferentExceptionSpec = false;
2619
2620 if (const FunctionProtoType *
2621 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2622 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2623 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2624 // FunctionDecl that we are importing the FunctionProtoType for.
2625 // To avoid an infinite recursion when importing, create the FunctionDecl
2626 // with a simplified function type and update it afterwards.
2627 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2628 FromEPI.NoexceptExpr) {
2629 FunctionProtoType::ExtProtoInfo DefaultEPI;
2630 FromTy = Importer.getFromContext().getFunctionType(
2631 FromFPT->getResultType(),
2632 FromFPT->arg_type_begin(),
2633 FromFPT->arg_type_end() - FromFPT->arg_type_begin(),
2634 DefaultEPI);
2635 usedDifferentExceptionSpec = true;
2636 }
2637 }
2638
Douglas Gregorb4964f72010-02-15 23:54:17 +00002639 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002640 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002641 if (T.isNull())
2642 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002643
2644 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002645 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002646 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2647 P != PEnd; ++P) {
2648 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2649 if (!ToP)
2650 return 0;
2651
2652 Parameters.push_back(ToP);
2653 }
2654
2655 // Create the imported function.
2656 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002657 FunctionDecl *ToFunction = 0;
2658 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2659 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2660 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002661 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002662 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002663 FromConstructor->isExplicit(),
2664 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002665 D->isImplicit(),
2666 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002667 } else if (isa<CXXDestructorDecl>(D)) {
2668 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2669 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002670 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002671 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002672 D->isInlineSpecified(),
2673 D->isImplicit());
2674 } else if (CXXConversionDecl *FromConversion
2675 = dyn_cast<CXXConversionDecl>(D)) {
2676 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2677 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002678 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002679 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002680 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002681 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002682 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002683 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002684 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2685 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2686 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002687 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002688 NameInfo, T, TInfo,
2689 Method->isStatic(),
2690 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002691 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002692 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002693 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002694 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002695 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002696 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002697 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002698 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002699 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002700 D->hasWrittenPrototype(),
2701 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002702 }
John McCall3e11ebe2010-03-15 10:12:16 +00002703
2704 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002705 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002706 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002707 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002708 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2709 ToFunction->setTrivial(D->isTrivial());
2710 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002711 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002712
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002713 // Set the parameters.
2714 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002715 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002716 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002717 }
David Blaikie9c70e042011-09-21 18:16:56 +00002718 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002719
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002720 if (usedDifferentExceptionSpec) {
2721 // Update FunctionProtoType::ExtProtoInfo.
2722 QualType T = Importer.Import(D->getType());
2723 if (T.isNull())
2724 return 0;
2725 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002726 }
2727
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002728 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002729
2730 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002731 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002732
Douglas Gregor43f54792010-02-17 02:12:47 +00002733 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002734}
2735
Douglas Gregor00eace12010-02-21 18:29:16 +00002736Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2737 return VisitFunctionDecl(D);
2738}
2739
2740Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2741 return VisitCXXMethodDecl(D);
2742}
2743
2744Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2745 return VisitCXXMethodDecl(D);
2746}
2747
2748Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2749 return VisitCXXMethodDecl(D);
2750}
2751
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002752static unsigned getFieldIndex(Decl *F) {
2753 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2754 if (!Owner)
2755 return 0;
2756
2757 unsigned Index = 1;
2758 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
2759 DEnd = Owner->noload_decls_end();
2760 D != DEnd; ++D) {
2761 if (*D == F)
2762 return Index;
2763
2764 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2765 ++Index;
2766 }
2767
2768 return Index;
2769}
2770
Douglas Gregor5c73e912010-02-11 00:48:18 +00002771Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2772 // Import the major distinguishing characteristics of a variable.
2773 DeclContext *DC, *LexicalDC;
2774 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002775 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002776 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2777 return 0;
2778
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002779 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002780 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2781 DC->localUncachedLookup(Name, FoundDecls);
2782 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2783 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002784 // For anonymous fields, match up by index.
2785 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2786 continue;
2787
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002788 if (Importer.IsStructurallyEquivalent(D->getType(),
2789 FoundField->getType())) {
2790 Importer.Imported(D, FoundField);
2791 return FoundField;
2792 }
2793
2794 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2795 << Name << D->getType() << FoundField->getType();
2796 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2797 << FoundField->getType();
2798 return 0;
2799 }
2800 }
2801
Douglas Gregorb4964f72010-02-15 23:54:17 +00002802 // Import the type.
2803 QualType T = Importer.Import(D->getType());
2804 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002805 return 0;
2806
2807 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2808 Expr *BitWidth = Importer.Import(D->getBitWidth());
2809 if (!BitWidth && D->getBitWidth())
2810 return 0;
2811
Abramo Bagnaradff19302011-03-08 08:55:46 +00002812 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2813 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002814 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002815 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002816 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002817 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002818 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002819 if (ToField->hasInClassInitializer())
2820 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002821 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002822 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002823 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002824 return ToField;
2825}
2826
Francois Pichet783dd6e2010-11-21 06:08:52 +00002827Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2828 // Import the major distinguishing characteristics of a variable.
2829 DeclContext *DC, *LexicalDC;
2830 DeclarationName Name;
2831 SourceLocation Loc;
2832 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2833 return 0;
2834
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002835 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002836 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2837 DC->localUncachedLookup(Name, FoundDecls);
2838 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002839 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002840 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002841 // For anonymous indirect fields, match up by index.
2842 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2843 continue;
2844
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002845 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002846 FoundField->getType(),
2847 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002848 Importer.Imported(D, FoundField);
2849 return FoundField;
2850 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002851
2852 // If there are more anonymous fields to check, continue.
2853 if (!Name && I < N-1)
2854 continue;
2855
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002856 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2857 << Name << D->getType() << FoundField->getType();
2858 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2859 << FoundField->getType();
2860 return 0;
2861 }
2862 }
2863
Francois Pichet783dd6e2010-11-21 06:08:52 +00002864 // Import the type.
2865 QualType T = Importer.Import(D->getType());
2866 if (T.isNull())
2867 return 0;
2868
2869 NamedDecl **NamedChain =
2870 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2871
2872 unsigned i = 0;
2873 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2874 PE = D->chain_end(); PI != PE; ++PI) {
2875 Decl* D = Importer.Import(*PI);
2876 if (!D)
2877 return 0;
2878 NamedChain[i++] = cast<NamedDecl>(D);
2879 }
2880
2881 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2882 Importer.getToContext(), DC,
2883 Loc, Name.getAsIdentifierInfo(), T,
2884 NamedChain, D->getChainingSize());
2885 ToIndirectField->setAccess(D->getAccess());
2886 ToIndirectField->setLexicalDeclContext(LexicalDC);
2887 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002888 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002889 return ToIndirectField;
2890}
2891
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002892Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2893 // Import the major distinguishing characteristics of an ivar.
2894 DeclContext *DC, *LexicalDC;
2895 DeclarationName Name;
2896 SourceLocation Loc;
2897 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2898 return 0;
2899
2900 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002901 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2902 DC->localUncachedLookup(Name, FoundDecls);
2903 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2904 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002905 if (Importer.IsStructurallyEquivalent(D->getType(),
2906 FoundIvar->getType())) {
2907 Importer.Imported(D, FoundIvar);
2908 return FoundIvar;
2909 }
2910
2911 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2912 << Name << D->getType() << FoundIvar->getType();
2913 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2914 << FoundIvar->getType();
2915 return 0;
2916 }
2917 }
2918
2919 // Import the type.
2920 QualType T = Importer.Import(D->getType());
2921 if (T.isNull())
2922 return 0;
2923
2924 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2925 Expr *BitWidth = Importer.Import(D->getBitWidth());
2926 if (!BitWidth && D->getBitWidth())
2927 return 0;
2928
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002929 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2930 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002931 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002932 Loc, Name.getAsIdentifierInfo(),
2933 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002934 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002935 ToIvar->setLexicalDeclContext(LexicalDC);
2936 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002937 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002938 return ToIvar;
2939
2940}
2941
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002942Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2943 // Import the major distinguishing characteristics of a variable.
2944 DeclContext *DC, *LexicalDC;
2945 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002946 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002947 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002948 return 0;
2949
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002950 // Try to find a variable in our own ("to") context with the same name and
2951 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002952 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002953 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002954 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002955 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002956 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2957 DC->localUncachedLookup(Name, FoundDecls);
2958 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2959 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002960 continue;
2961
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002962 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002963 // We have found a variable that we may need to merge with. Check it.
2964 if (isExternalLinkage(FoundVar->getLinkage()) &&
2965 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002966 if (Importer.IsStructurallyEquivalent(D->getType(),
2967 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002968 MergeWithVar = FoundVar;
2969 break;
2970 }
2971
Douglas Gregor56521c52010-02-12 17:23:39 +00002972 const ArrayType *FoundArray
2973 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2974 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002975 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002976 if (FoundArray && TArray) {
2977 if (isa<IncompleteArrayType>(FoundArray) &&
2978 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002979 // Import the type.
2980 QualType T = Importer.Import(D->getType());
2981 if (T.isNull())
2982 return 0;
2983
Douglas Gregor56521c52010-02-12 17:23:39 +00002984 FoundVar->setType(T);
2985 MergeWithVar = FoundVar;
2986 break;
2987 } else if (isa<IncompleteArrayType>(TArray) &&
2988 isa<ConstantArrayType>(FoundArray)) {
2989 MergeWithVar = FoundVar;
2990 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002991 }
2992 }
2993
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002994 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002995 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002996 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2997 << FoundVar->getType();
2998 }
2999 }
3000
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003001 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003002 }
3003
3004 if (MergeWithVar) {
3005 // An equivalent variable with external linkage has been found. Link
3006 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003007 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003008
3009 if (VarDecl *DDef = D->getDefinition()) {
3010 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3011 Importer.ToDiag(ExistingDef->getLocation(),
3012 diag::err_odr_variable_multiple_def)
3013 << Name;
3014 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3015 } else {
3016 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003017 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003018 if (DDef->isInitKnownICE()) {
3019 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3020 Eval->CheckedICE = true;
3021 Eval->IsICE = DDef->isInitICE();
3022 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003023 }
3024 }
3025
3026 return MergeWithVar;
3027 }
3028
3029 if (!ConflictingDecls.empty()) {
3030 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3031 ConflictingDecls.data(),
3032 ConflictingDecls.size());
3033 if (!Name)
3034 return 0;
3035 }
3036 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003037
Douglas Gregorb4964f72010-02-15 23:54:17 +00003038 // Import the type.
3039 QualType T = Importer.Import(D->getType());
3040 if (T.isNull())
3041 return 0;
3042
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003043 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003044 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003045 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3046 Importer.Import(D->getInnerLocStart()),
3047 Loc, Name.getAsIdentifierInfo(),
3048 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00003049 D->getStorageClass(),
3050 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00003051 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003052 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003053 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003054 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003055 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003056
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003057 // Merge the initializer.
3058 // FIXME: Can we really import any initializer? Alternatively, we could force
3059 // ourselves to import every declaration of a variable and then only use
3060 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00003061 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003062
3063 // FIXME: Other bits to merge?
3064
3065 return ToVar;
3066}
3067
Douglas Gregor8b228d72010-02-17 21:22:52 +00003068Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3069 // Parameters are created in the translation unit's context, then moved
3070 // into the function declaration's context afterward.
3071 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3072
3073 // Import the name of this declaration.
3074 DeclarationName Name = Importer.Import(D->getDeclName());
3075 if (D->getDeclName() && !Name)
3076 return 0;
3077
3078 // Import the location of this declaration.
3079 SourceLocation Loc = Importer.Import(D->getLocation());
3080
3081 // Import the parameter's type.
3082 QualType T = Importer.Import(D->getType());
3083 if (T.isNull())
3084 return 0;
3085
3086 // Create the imported parameter.
3087 ImplicitParamDecl *ToParm
3088 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3089 Loc, Name.getAsIdentifierInfo(),
3090 T);
3091 return Importer.Imported(D, ToParm);
3092}
3093
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003094Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3095 // Parameters are created in the translation unit's context, then moved
3096 // into the function declaration's context afterward.
3097 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3098
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003099 // Import the name of this declaration.
3100 DeclarationName Name = Importer.Import(D->getDeclName());
3101 if (D->getDeclName() && !Name)
3102 return 0;
3103
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003104 // Import the location of this declaration.
3105 SourceLocation Loc = Importer.Import(D->getLocation());
3106
3107 // Import the parameter's type.
3108 QualType T = Importer.Import(D->getType());
3109 if (T.isNull())
3110 return 0;
3111
3112 // Create the imported parameter.
3113 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3114 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003115 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003116 Loc, Name.getAsIdentifierInfo(),
3117 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003118 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003119 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00003120 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003121 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003122}
3123
Douglas Gregor43f54792010-02-17 02:12:47 +00003124Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3125 // Import the major distinguishing characteristics of a method.
3126 DeclContext *DC, *LexicalDC;
3127 DeclarationName Name;
3128 SourceLocation Loc;
3129 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3130 return 0;
3131
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003132 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3133 DC->localUncachedLookup(Name, FoundDecls);
3134 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3135 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003136 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3137 continue;
3138
3139 // Check return types.
3140 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3141 FoundMethod->getResultType())) {
3142 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3143 << D->isInstanceMethod() << Name
3144 << D->getResultType() << FoundMethod->getResultType();
3145 Importer.ToDiag(FoundMethod->getLocation(),
3146 diag::note_odr_objc_method_here)
3147 << D->isInstanceMethod() << Name;
3148 return 0;
3149 }
3150
3151 // Check the number of parameters.
3152 if (D->param_size() != FoundMethod->param_size()) {
3153 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3154 << D->isInstanceMethod() << Name
3155 << D->param_size() << FoundMethod->param_size();
3156 Importer.ToDiag(FoundMethod->getLocation(),
3157 diag::note_odr_objc_method_here)
3158 << D->isInstanceMethod() << Name;
3159 return 0;
3160 }
3161
3162 // Check parameter types.
3163 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3164 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3165 P != PEnd; ++P, ++FoundP) {
3166 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3167 (*FoundP)->getType())) {
3168 Importer.FromDiag((*P)->getLocation(),
3169 diag::err_odr_objc_method_param_type_inconsistent)
3170 << D->isInstanceMethod() << Name
3171 << (*P)->getType() << (*FoundP)->getType();
3172 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3173 << (*FoundP)->getType();
3174 return 0;
3175 }
3176 }
3177
3178 // Check variadic/non-variadic.
3179 // Check the number of parameters.
3180 if (D->isVariadic() != FoundMethod->isVariadic()) {
3181 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3182 << D->isInstanceMethod() << Name;
3183 Importer.ToDiag(FoundMethod->getLocation(),
3184 diag::note_odr_objc_method_here)
3185 << D->isInstanceMethod() << Name;
3186 return 0;
3187 }
3188
3189 // FIXME: Any other bits we need to merge?
3190 return Importer.Imported(D, FoundMethod);
3191 }
3192 }
3193
3194 // Import the result type.
3195 QualType ResultTy = Importer.Import(D->getResultType());
3196 if (ResultTy.isNull())
3197 return 0;
3198
Douglas Gregor12852d92010-03-08 14:59:44 +00003199 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3200
Douglas Gregor43f54792010-02-17 02:12:47 +00003201 ObjCMethodDecl *ToMethod
3202 = ObjCMethodDecl::Create(Importer.getToContext(),
3203 Loc,
3204 Importer.Import(D->getLocEnd()),
3205 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003206 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003207 D->isInstanceMethod(),
3208 D->isVariadic(),
Jordan Rosed01e83a2012-10-10 16:42:25 +00003209 D->isPropertyAccessor(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003210 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003211 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003212 D->getImplementationControl(),
3213 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003214
3215 // FIXME: When we decide to merge method definitions, we'll need to
3216 // deal with implicit parameters.
3217
3218 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003219 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003220 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3221 FromPEnd = D->param_end();
3222 FromP != FromPEnd;
3223 ++FromP) {
3224 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3225 if (!ToP)
3226 return 0;
3227
3228 ToParams.push_back(ToP);
3229 }
3230
3231 // Set the parameters.
3232 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3233 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003234 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003235 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003236 SmallVector<SourceLocation, 12> SelLocs;
3237 D->getSelectorLocs(SelLocs);
3238 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003239
3240 ToMethod->setLexicalDeclContext(LexicalDC);
3241 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003242 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003243 return ToMethod;
3244}
3245
Douglas Gregor84c51c32010-02-18 01:47:50 +00003246Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3247 // Import the major distinguishing characteristics of a category.
3248 DeclContext *DC, *LexicalDC;
3249 DeclarationName Name;
3250 SourceLocation Loc;
3251 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3252 return 0;
3253
3254 ObjCInterfaceDecl *ToInterface
3255 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3256 if (!ToInterface)
3257 return 0;
3258
3259 // Determine if we've already encountered this category.
3260 ObjCCategoryDecl *MergeWithCategory
3261 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3262 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3263 if (!ToCategory) {
3264 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003265 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003266 Loc,
3267 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003268 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003269 ToInterface,
3270 Importer.Import(D->getIvarLBraceLoc()),
3271 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003272 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003273 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003274 Importer.Imported(D, ToCategory);
3275
Douglas Gregor84c51c32010-02-18 01:47:50 +00003276 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003277 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3278 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003279 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3280 = D->protocol_loc_begin();
3281 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3282 FromProtoEnd = D->protocol_end();
3283 FromProto != FromProtoEnd;
3284 ++FromProto, ++FromProtoLoc) {
3285 ObjCProtocolDecl *ToProto
3286 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3287 if (!ToProto)
3288 return 0;
3289 Protocols.push_back(ToProto);
3290 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3291 }
3292
3293 // FIXME: If we're merging, make sure that the protocol list is the same.
3294 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3295 ProtocolLocs.data(), Importer.getToContext());
3296
3297 } else {
3298 Importer.Imported(D, ToCategory);
3299 }
3300
3301 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003302 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003303
3304 // If we have an implementation, import it as well.
3305 if (D->getImplementation()) {
3306 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003307 = cast_or_null<ObjCCategoryImplDecl>(
3308 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003309 if (!Impl)
3310 return 0;
3311
3312 ToCategory->setImplementation(Impl);
3313 }
3314
3315 return ToCategory;
3316}
3317
Douglas Gregor2aa53772012-01-24 17:42:07 +00003318bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3319 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003320 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003321 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003322 if (shouldForceImportDeclContext(Kind))
3323 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003324 return false;
3325 }
3326
3327 // Start the protocol definition
3328 To->startDefinition();
3329
3330 // Import protocols
3331 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3332 SmallVector<SourceLocation, 4> ProtocolLocs;
3333 ObjCProtocolDecl::protocol_loc_iterator
3334 FromProtoLoc = From->protocol_loc_begin();
3335 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3336 FromProtoEnd = From->protocol_end();
3337 FromProto != FromProtoEnd;
3338 ++FromProto, ++FromProtoLoc) {
3339 ObjCProtocolDecl *ToProto
3340 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3341 if (!ToProto)
3342 return true;
3343 Protocols.push_back(ToProto);
3344 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3345 }
3346
3347 // FIXME: If we're merging, make sure that the protocol list is the same.
3348 To->setProtocolList(Protocols.data(), Protocols.size(),
3349 ProtocolLocs.data(), Importer.getToContext());
3350
Douglas Gregor2e15c842012-02-01 21:00:38 +00003351 if (shouldForceImportDeclContext(Kind)) {
3352 // Import all of the members of this protocol.
3353 ImportDeclContext(From, /*ForceImport=*/true);
3354 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003355 return false;
3356}
3357
Douglas Gregor98d156a2010-02-17 16:12:00 +00003358Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003359 // If this protocol has a definition in the translation unit we're coming
3360 // from, but this particular declaration is not that definition, import the
3361 // definition and map to that.
3362 ObjCProtocolDecl *Definition = D->getDefinition();
3363 if (Definition && Definition != D) {
3364 Decl *ImportedDef = Importer.Import(Definition);
3365 if (!ImportedDef)
3366 return 0;
3367
3368 return Importer.Imported(D, ImportedDef);
3369 }
3370
Douglas Gregor84c51c32010-02-18 01:47:50 +00003371 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003372 DeclContext *DC, *LexicalDC;
3373 DeclarationName Name;
3374 SourceLocation Loc;
3375 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3376 return 0;
3377
3378 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003379 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3380 DC->localUncachedLookup(Name, FoundDecls);
3381 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3382 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003383 continue;
3384
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003385 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003386 break;
3387 }
3388
3389 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003390 if (!ToProto) {
3391 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3392 Name.getAsIdentifierInfo(), Loc,
3393 Importer.Import(D->getAtStartLoc()),
3394 /*PrevDecl=*/0);
3395 ToProto->setLexicalDeclContext(LexicalDC);
3396 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003397 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003398
3399 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003400
Douglas Gregor2aa53772012-01-24 17:42:07 +00003401 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3402 return 0;
3403
Douglas Gregor98d156a2010-02-17 16:12:00 +00003404 return ToProto;
3405}
3406
Douglas Gregor2aa53772012-01-24 17:42:07 +00003407bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3408 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003409 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003410 if (To->getDefinition()) {
3411 // Check consistency of superclass.
3412 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3413 if (FromSuper) {
3414 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3415 if (!FromSuper)
3416 return true;
3417 }
3418
3419 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3420 if ((bool)FromSuper != (bool)ToSuper ||
3421 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3422 Importer.ToDiag(To->getLocation(),
3423 diag::err_odr_objc_superclass_inconsistent)
3424 << To->getDeclName();
3425 if (ToSuper)
3426 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3427 << To->getSuperClass()->getDeclName();
3428 else
3429 Importer.ToDiag(To->getLocation(),
3430 diag::note_odr_objc_missing_superclass);
3431 if (From->getSuperClass())
3432 Importer.FromDiag(From->getSuperClassLoc(),
3433 diag::note_odr_objc_superclass)
3434 << From->getSuperClass()->getDeclName();
3435 else
3436 Importer.FromDiag(From->getLocation(),
3437 diag::note_odr_objc_missing_superclass);
3438 }
3439
Douglas Gregor2e15c842012-02-01 21:00:38 +00003440 if (shouldForceImportDeclContext(Kind))
3441 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003442 return false;
3443 }
3444
3445 // Start the definition.
3446 To->startDefinition();
3447
3448 // If this class has a superclass, import it.
3449 if (From->getSuperClass()) {
3450 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3451 Importer.Import(From->getSuperClass()));
3452 if (!Super)
3453 return true;
3454
3455 To->setSuperClass(Super);
3456 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3457 }
3458
3459 // Import protocols
3460 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3461 SmallVector<SourceLocation, 4> ProtocolLocs;
3462 ObjCInterfaceDecl::protocol_loc_iterator
3463 FromProtoLoc = From->protocol_loc_begin();
3464
3465 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3466 FromProtoEnd = From->protocol_end();
3467 FromProto != FromProtoEnd;
3468 ++FromProto, ++FromProtoLoc) {
3469 ObjCProtocolDecl *ToProto
3470 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3471 if (!ToProto)
3472 return true;
3473 Protocols.push_back(ToProto);
3474 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3475 }
3476
3477 // FIXME: If we're merging, make sure that the protocol list is the same.
3478 To->setProtocolList(Protocols.data(), Protocols.size(),
3479 ProtocolLocs.data(), Importer.getToContext());
3480
3481 // Import categories. When the categories themselves are imported, they'll
3482 // hook themselves into this interface.
3483 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3484 FromCat = FromCat->getNextClassCategory())
3485 Importer.Import(FromCat);
3486
3487 // If we have an @implementation, import it as well.
3488 if (From->getImplementation()) {
3489 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3490 Importer.Import(From->getImplementation()));
3491 if (!Impl)
3492 return true;
3493
3494 To->setImplementation(Impl);
3495 }
3496
Douglas Gregor2e15c842012-02-01 21:00:38 +00003497 if (shouldForceImportDeclContext(Kind)) {
3498 // Import all of the members of this class.
3499 ImportDeclContext(From, /*ForceImport=*/true);
3500 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003501 return false;
3502}
3503
Douglas Gregor45635322010-02-16 01:20:57 +00003504Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003505 // If this class has a definition in the translation unit we're coming from,
3506 // but this particular declaration is not that definition, import the
3507 // definition and map to that.
3508 ObjCInterfaceDecl *Definition = D->getDefinition();
3509 if (Definition && Definition != D) {
3510 Decl *ImportedDef = Importer.Import(Definition);
3511 if (!ImportedDef)
3512 return 0;
3513
3514 return Importer.Imported(D, ImportedDef);
3515 }
3516
Douglas Gregor45635322010-02-16 01:20:57 +00003517 // Import the major distinguishing characteristics of an @interface.
3518 DeclContext *DC, *LexicalDC;
3519 DeclarationName Name;
3520 SourceLocation Loc;
3521 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3522 return 0;
3523
Douglas Gregor2aa53772012-01-24 17:42:07 +00003524 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003525 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003526 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3527 DC->localUncachedLookup(Name, FoundDecls);
3528 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3529 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003530 continue;
3531
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003532 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003533 break;
3534 }
3535
Douglas Gregor2aa53772012-01-24 17:42:07 +00003536 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003537 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003538 if (!ToIface) {
3539 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3540 Importer.Import(D->getAtStartLoc()),
3541 Name.getAsIdentifierInfo(),
3542 /*PrevDecl=*/0,Loc,
3543 D->isImplicitInterfaceDecl());
3544 ToIface->setLexicalDeclContext(LexicalDC);
3545 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003546 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003547 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003548
Douglas Gregor2aa53772012-01-24 17:42:07 +00003549 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3550 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003551
Douglas Gregor98d156a2010-02-17 16:12:00 +00003552 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003553}
3554
Douglas Gregor4da9d682010-12-07 15:32:12 +00003555Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3556 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3557 Importer.Import(D->getCategoryDecl()));
3558 if (!Category)
3559 return 0;
3560
3561 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3562 if (!ToImpl) {
3563 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3564 if (!DC)
3565 return 0;
3566
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003567 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003568 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003569 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003570 Category->getClassInterface(),
3571 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003572 Importer.Import(D->getAtStartLoc()),
3573 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003574
3575 DeclContext *LexicalDC = DC;
3576 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3577 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3578 if (!LexicalDC)
3579 return 0;
3580
3581 ToImpl->setLexicalDeclContext(LexicalDC);
3582 }
3583
Sean Callanan95e74be2011-10-21 02:57:43 +00003584 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003585 Category->setImplementation(ToImpl);
3586 }
3587
3588 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003589 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003590 return ToImpl;
3591}
3592
Douglas Gregorda8025c2010-12-07 01:26:03 +00003593Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3594 // Find the corresponding interface.
3595 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3596 Importer.Import(D->getClassInterface()));
3597 if (!Iface)
3598 return 0;
3599
3600 // Import the superclass, if any.
3601 ObjCInterfaceDecl *Super = 0;
3602 if (D->getSuperClass()) {
3603 Super = cast_or_null<ObjCInterfaceDecl>(
3604 Importer.Import(D->getSuperClass()));
3605 if (!Super)
3606 return 0;
3607 }
3608
3609 ObjCImplementationDecl *Impl = Iface->getImplementation();
3610 if (!Impl) {
3611 // We haven't imported an implementation yet. Create a new @implementation
3612 // now.
3613 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3614 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003615 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003616 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003617 Importer.Import(D->getAtStartLoc()),
3618 Importer.Import(D->getIvarLBraceLoc()),
3619 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003620
3621 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3622 DeclContext *LexicalDC
3623 = Importer.ImportContext(D->getLexicalDeclContext());
3624 if (!LexicalDC)
3625 return 0;
3626 Impl->setLexicalDeclContext(LexicalDC);
3627 }
3628
3629 // Associate the implementation with the class it implements.
3630 Iface->setImplementation(Impl);
3631 Importer.Imported(D, Iface->getImplementation());
3632 } else {
3633 Importer.Imported(D, Iface->getImplementation());
3634
3635 // Verify that the existing @implementation has the same superclass.
3636 if ((Super && !Impl->getSuperClass()) ||
3637 (!Super && Impl->getSuperClass()) ||
3638 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003639 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003640 Importer.ToDiag(Impl->getLocation(),
3641 diag::err_odr_objc_superclass_inconsistent)
3642 << Iface->getDeclName();
3643 // FIXME: It would be nice to have the location of the superclass
3644 // below.
3645 if (Impl->getSuperClass())
3646 Importer.ToDiag(Impl->getLocation(),
3647 diag::note_odr_objc_superclass)
3648 << Impl->getSuperClass()->getDeclName();
3649 else
3650 Importer.ToDiag(Impl->getLocation(),
3651 diag::note_odr_objc_missing_superclass);
3652 if (D->getSuperClass())
3653 Importer.FromDiag(D->getLocation(),
3654 diag::note_odr_objc_superclass)
3655 << D->getSuperClass()->getDeclName();
3656 else
3657 Importer.FromDiag(D->getLocation(),
3658 diag::note_odr_objc_missing_superclass);
3659 return 0;
3660 }
3661 }
3662
3663 // Import all of the members of this @implementation.
3664 ImportDeclContext(D);
3665
3666 return Impl;
3667}
3668
Douglas Gregora11c4582010-02-17 18:02:10 +00003669Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3670 // Import the major distinguishing characteristics of an @property.
3671 DeclContext *DC, *LexicalDC;
3672 DeclarationName Name;
3673 SourceLocation Loc;
3674 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3675 return 0;
3676
3677 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003678 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3679 DC->localUncachedLookup(Name, FoundDecls);
3680 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003681 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003682 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003683 // Check property types.
3684 if (!Importer.IsStructurallyEquivalent(D->getType(),
3685 FoundProp->getType())) {
3686 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3687 << Name << D->getType() << FoundProp->getType();
3688 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3689 << FoundProp->getType();
3690 return 0;
3691 }
3692
3693 // FIXME: Check property attributes, getters, setters, etc.?
3694
3695 // Consider these properties to be equivalent.
3696 Importer.Imported(D, FoundProp);
3697 return FoundProp;
3698 }
3699 }
3700
3701 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003702 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3703 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003704 return 0;
3705
3706 // Create the new property.
3707 ObjCPropertyDecl *ToProperty
3708 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3709 Name.getAsIdentifierInfo(),
3710 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003711 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003712 T,
3713 D->getPropertyImplementation());
3714 Importer.Imported(D, ToProperty);
3715 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003716 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003717
3718 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003719 ToProperty->setPropertyAttributesAsWritten(
3720 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003721 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3722 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3723 ToProperty->setGetterMethodDecl(
3724 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3725 ToProperty->setSetterMethodDecl(
3726 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3727 ToProperty->setPropertyIvarDecl(
3728 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3729 return ToProperty;
3730}
3731
Douglas Gregor14a49e22010-12-07 18:32:03 +00003732Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3733 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3734 Importer.Import(D->getPropertyDecl()));
3735 if (!Property)
3736 return 0;
3737
3738 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3739 if (!DC)
3740 return 0;
3741
3742 // Import the lexical declaration context.
3743 DeclContext *LexicalDC = DC;
3744 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3745 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3746 if (!LexicalDC)
3747 return 0;
3748 }
3749
3750 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3751 if (!InImpl)
3752 return 0;
3753
3754 // Import the ivar (for an @synthesize).
3755 ObjCIvarDecl *Ivar = 0;
3756 if (D->getPropertyIvarDecl()) {
3757 Ivar = cast_or_null<ObjCIvarDecl>(
3758 Importer.Import(D->getPropertyIvarDecl()));
3759 if (!Ivar)
3760 return 0;
3761 }
3762
3763 ObjCPropertyImplDecl *ToImpl
3764 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3765 if (!ToImpl) {
3766 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3767 Importer.Import(D->getLocStart()),
3768 Importer.Import(D->getLocation()),
3769 Property,
3770 D->getPropertyImplementation(),
3771 Ivar,
3772 Importer.Import(D->getPropertyIvarDeclLoc()));
3773 ToImpl->setLexicalDeclContext(LexicalDC);
3774 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003775 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003776 } else {
3777 // Check that we have the same kind of property implementation (@synthesize
3778 // vs. @dynamic).
3779 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3780 Importer.ToDiag(ToImpl->getLocation(),
3781 diag::err_odr_objc_property_impl_kind_inconsistent)
3782 << Property->getDeclName()
3783 << (ToImpl->getPropertyImplementation()
3784 == ObjCPropertyImplDecl::Dynamic);
3785 Importer.FromDiag(D->getLocation(),
3786 diag::note_odr_objc_property_impl_kind)
3787 << D->getPropertyDecl()->getDeclName()
3788 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3789 return 0;
3790 }
3791
3792 // For @synthesize, check that we have the same
3793 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3794 Ivar != ToImpl->getPropertyIvarDecl()) {
3795 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3796 diag::err_odr_objc_synthesize_ivar_inconsistent)
3797 << Property->getDeclName()
3798 << ToImpl->getPropertyIvarDecl()->getDeclName()
3799 << Ivar->getDeclName();
3800 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3801 diag::note_odr_objc_synthesize_ivar_here)
3802 << D->getPropertyIvarDecl()->getDeclName();
3803 return 0;
3804 }
3805
3806 // Merge the existing implementation with the new implementation.
3807 Importer.Imported(D, ToImpl);
3808 }
3809
3810 return ToImpl;
3811}
3812
Douglas Gregora082a492010-11-30 19:14:50 +00003813Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3814 // For template arguments, we adopt the translation unit as our declaration
3815 // context. This context will be fixed when the actual template declaration
3816 // is created.
3817
3818 // FIXME: Import default argument.
3819 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3820 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003821 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003822 Importer.Import(D->getLocation()),
3823 D->getDepth(),
3824 D->getIndex(),
3825 Importer.Import(D->getIdentifier()),
3826 D->wasDeclaredWithTypename(),
3827 D->isParameterPack());
3828}
3829
3830Decl *
3831ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3832 // Import the name of this declaration.
3833 DeclarationName Name = Importer.Import(D->getDeclName());
3834 if (D->getDeclName() && !Name)
3835 return 0;
3836
3837 // Import the location of this declaration.
3838 SourceLocation Loc = Importer.Import(D->getLocation());
3839
3840 // Import the type of this declaration.
3841 QualType T = Importer.Import(D->getType());
3842 if (T.isNull())
3843 return 0;
3844
3845 // Import type-source information.
3846 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3847 if (D->getTypeSourceInfo() && !TInfo)
3848 return 0;
3849
3850 // FIXME: Import default argument.
3851
3852 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3853 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003854 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003855 Loc, D->getDepth(), D->getPosition(),
3856 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003857 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003858}
3859
3860Decl *
3861ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3862 // Import the name of this declaration.
3863 DeclarationName Name = Importer.Import(D->getDeclName());
3864 if (D->getDeclName() && !Name)
3865 return 0;
3866
3867 // Import the location of this declaration.
3868 SourceLocation Loc = Importer.Import(D->getLocation());
3869
3870 // Import template parameters.
3871 TemplateParameterList *TemplateParams
3872 = ImportTemplateParameterList(D->getTemplateParameters());
3873 if (!TemplateParams)
3874 return 0;
3875
3876 // FIXME: Import default argument.
3877
3878 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3879 Importer.getToContext().getTranslationUnitDecl(),
3880 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003881 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003882 Name.getAsIdentifierInfo(),
3883 TemplateParams);
3884}
3885
3886Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3887 // If this record has a definition in the translation unit we're coming from,
3888 // but this particular declaration is not that definition, import the
3889 // definition and map to that.
3890 CXXRecordDecl *Definition
3891 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3892 if (Definition && Definition != D->getTemplatedDecl()) {
3893 Decl *ImportedDef
3894 = Importer.Import(Definition->getDescribedClassTemplate());
3895 if (!ImportedDef)
3896 return 0;
3897
3898 return Importer.Imported(D, ImportedDef);
3899 }
3900
3901 // Import the major distinguishing characteristics of this class template.
3902 DeclContext *DC, *LexicalDC;
3903 DeclarationName Name;
3904 SourceLocation Loc;
3905 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3906 return 0;
3907
3908 // We may already have a template of the same name; try to find and match it.
3909 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003910 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003911 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3912 DC->localUncachedLookup(Name, FoundDecls);
3913 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3914 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003915 continue;
3916
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003917 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003918 if (ClassTemplateDecl *FoundTemplate
3919 = dyn_cast<ClassTemplateDecl>(Found)) {
3920 if (IsStructuralMatch(D, FoundTemplate)) {
3921 // The class templates structurally match; call it the same template.
3922 // FIXME: We may be filling in a forward declaration here. Handle
3923 // this case!
3924 Importer.Imported(D->getTemplatedDecl(),
3925 FoundTemplate->getTemplatedDecl());
3926 return Importer.Imported(D, FoundTemplate);
3927 }
3928 }
3929
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003930 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003931 }
3932
3933 if (!ConflictingDecls.empty()) {
3934 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3935 ConflictingDecls.data(),
3936 ConflictingDecls.size());
3937 }
3938
3939 if (!Name)
3940 return 0;
3941 }
3942
3943 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3944
3945 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003946 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3947 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003948 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3949 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003950 DC, StartLoc, IdLoc,
3951 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003952 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003953 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003954 D2Templated->setLexicalDeclContext(LexicalDC);
3955
3956 // Create the class template declaration itself.
3957 TemplateParameterList *TemplateParams
3958 = ImportTemplateParameterList(D->getTemplateParameters());
3959 if (!TemplateParams)
3960 return 0;
3961
3962 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3963 Loc, Name, TemplateParams,
3964 D2Templated,
3965 /*PrevDecl=*/0);
3966 D2Templated->setDescribedClassTemplate(D2);
3967
3968 D2->setAccess(D->getAccess());
3969 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003970 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003971
3972 // Note the relationship between the class templates.
3973 Importer.Imported(D, D2);
3974 Importer.Imported(DTemplated, D2Templated);
3975
John McCallf937c022011-10-07 06:10:15 +00003976 if (DTemplated->isCompleteDefinition() &&
3977 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003978 // FIXME: Import definition!
3979 }
3980
3981 return D2;
3982}
3983
Douglas Gregore2e50d332010-12-01 01:36:18 +00003984Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3985 ClassTemplateSpecializationDecl *D) {
3986 // If this record has a definition in the translation unit we're coming from,
3987 // but this particular declaration is not that definition, import the
3988 // definition and map to that.
3989 TagDecl *Definition = D->getDefinition();
3990 if (Definition && Definition != D) {
3991 Decl *ImportedDef = Importer.Import(Definition);
3992 if (!ImportedDef)
3993 return 0;
3994
3995 return Importer.Imported(D, ImportedDef);
3996 }
3997
3998 ClassTemplateDecl *ClassTemplate
3999 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4000 D->getSpecializedTemplate()));
4001 if (!ClassTemplate)
4002 return 0;
4003
4004 // Import the context of this declaration.
4005 DeclContext *DC = ClassTemplate->getDeclContext();
4006 if (!DC)
4007 return 0;
4008
4009 DeclContext *LexicalDC = DC;
4010 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4011 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4012 if (!LexicalDC)
4013 return 0;
4014 }
4015
4016 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004017 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4018 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004019
4020 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004021 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004022 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4023 D->getTemplateArgs().size(),
4024 TemplateArgs))
4025 return 0;
4026
4027 // Try to find an existing specialization with these template arguments.
4028 void *InsertPos = 0;
4029 ClassTemplateSpecializationDecl *D2
4030 = ClassTemplate->findSpecialization(TemplateArgs.data(),
4031 TemplateArgs.size(), InsertPos);
4032 if (D2) {
4033 // We already have a class template specialization with these template
4034 // arguments.
4035
4036 // FIXME: Check for specialization vs. instantiation errors.
4037
4038 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004039 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004040 // The record types structurally match, or the "from" translation
4041 // unit only had a forward declaration anyway; call it the same
4042 // function.
4043 return Importer.Imported(D, FoundDef);
4044 }
4045 }
4046 } else {
4047 // Create a new specialization.
4048 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4049 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004050 StartLoc, IdLoc,
4051 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004052 TemplateArgs.data(),
4053 TemplateArgs.size(),
4054 /*PrevDecl=*/0);
4055 D2->setSpecializationKind(D->getSpecializationKind());
4056
4057 // Add this specialization to the class template.
4058 ClassTemplate->AddSpecialization(D2, InsertPos);
4059
4060 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004061 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004062
4063 // Add the specialization to this context.
4064 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004065 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004066 }
4067 Importer.Imported(D, D2);
4068
John McCallf937c022011-10-07 06:10:15 +00004069 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00004070 return 0;
4071
4072 return D2;
4073}
4074
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004075//----------------------------------------------------------------------------
4076// Import Statements
4077//----------------------------------------------------------------------------
4078
4079Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4080 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4081 << S->getStmtClassName();
4082 return 0;
4083}
4084
4085//----------------------------------------------------------------------------
4086// Import Expressions
4087//----------------------------------------------------------------------------
4088Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4089 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4090 << E->getStmtClassName();
4091 return 0;
4092}
4093
Douglas Gregor52f820e2010-02-19 01:17:02 +00004094Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004095 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4096 if (!ToD)
4097 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004098
4099 NamedDecl *FoundD = 0;
4100 if (E->getDecl() != E->getFoundDecl()) {
4101 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4102 if (!FoundD)
4103 return 0;
4104 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004105
4106 QualType T = Importer.Import(E->getType());
4107 if (T.isNull())
4108 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004109
4110 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4111 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004112 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004113 ToD,
John McCall113bee02012-03-10 09:33:50 +00004114 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004115 Importer.Import(E->getLocation()),
4116 T, E->getValueKind(),
4117 FoundD,
4118 /*FIXME:TemplateArgs=*/0);
4119 if (E->hadMultipleCandidates())
4120 DRE->setHadMultipleCandidates(true);
4121 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004122}
4123
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004124Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4125 QualType T = Importer.Import(E->getType());
4126 if (T.isNull())
4127 return 0;
4128
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004129 return IntegerLiteral::Create(Importer.getToContext(),
4130 E->getValue(), T,
4131 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004132}
4133
Douglas Gregor623421d2010-02-18 02:21:22 +00004134Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4135 QualType T = Importer.Import(E->getType());
4136 if (T.isNull())
4137 return 0;
4138
Douglas Gregorfb65e592011-07-27 05:40:30 +00004139 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4140 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004141 Importer.Import(E->getLocation()));
4142}
4143
Douglas Gregorc74247e2010-02-19 01:07:06 +00004144Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4145 Expr *SubExpr = Importer.Import(E->getSubExpr());
4146 if (!SubExpr)
4147 return 0;
4148
4149 return new (Importer.getToContext())
4150 ParenExpr(Importer.Import(E->getLParen()),
4151 Importer.Import(E->getRParen()),
4152 SubExpr);
4153}
4154
4155Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4156 QualType T = Importer.Import(E->getType());
4157 if (T.isNull())
4158 return 0;
4159
4160 Expr *SubExpr = Importer.Import(E->getSubExpr());
4161 if (!SubExpr)
4162 return 0;
4163
4164 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004165 T, E->getValueKind(),
4166 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004167 Importer.Import(E->getOperatorLoc()));
4168}
4169
Peter Collingbournee190dee2011-03-11 19:24:49 +00004170Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4171 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004172 QualType ResultType = Importer.Import(E->getType());
4173
4174 if (E->isArgumentType()) {
4175 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4176 if (!TInfo)
4177 return 0;
4178
Peter Collingbournee190dee2011-03-11 19:24:49 +00004179 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4180 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004181 Importer.Import(E->getOperatorLoc()),
4182 Importer.Import(E->getRParenLoc()));
4183 }
4184
4185 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4186 if (!SubExpr)
4187 return 0;
4188
Peter Collingbournee190dee2011-03-11 19:24:49 +00004189 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4190 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004191 Importer.Import(E->getOperatorLoc()),
4192 Importer.Import(E->getRParenLoc()));
4193}
4194
Douglas Gregorc74247e2010-02-19 01:07:06 +00004195Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4196 QualType T = Importer.Import(E->getType());
4197 if (T.isNull())
4198 return 0;
4199
4200 Expr *LHS = Importer.Import(E->getLHS());
4201 if (!LHS)
4202 return 0;
4203
4204 Expr *RHS = Importer.Import(E->getRHS());
4205 if (!RHS)
4206 return 0;
4207
4208 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004209 T, E->getValueKind(),
4210 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004211 Importer.Import(E->getOperatorLoc()),
4212 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004213}
4214
4215Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4216 QualType T = Importer.Import(E->getType());
4217 if (T.isNull())
4218 return 0;
4219
4220 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4221 if (CompLHSType.isNull())
4222 return 0;
4223
4224 QualType CompResultType = Importer.Import(E->getComputationResultType());
4225 if (CompResultType.isNull())
4226 return 0;
4227
4228 Expr *LHS = Importer.Import(E->getLHS());
4229 if (!LHS)
4230 return 0;
4231
4232 Expr *RHS = Importer.Import(E->getRHS());
4233 if (!RHS)
4234 return 0;
4235
4236 return new (Importer.getToContext())
4237 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004238 T, E->getValueKind(),
4239 E->getObjectKind(),
4240 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004241 Importer.Import(E->getOperatorLoc()),
4242 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004243}
4244
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004245static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004246 if (E->path_empty()) return false;
4247
4248 // TODO: import cast paths
4249 return true;
4250}
4251
Douglas Gregor98c10182010-02-12 22:17:39 +00004252Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4253 QualType T = Importer.Import(E->getType());
4254 if (T.isNull())
4255 return 0;
4256
4257 Expr *SubExpr = Importer.Import(E->getSubExpr());
4258 if (!SubExpr)
4259 return 0;
John McCallcf142162010-08-07 06:22:56 +00004260
4261 CXXCastPath BasePath;
4262 if (ImportCastPath(E, BasePath))
4263 return 0;
4264
4265 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004266 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004267}
4268
Douglas Gregor5481d322010-02-19 01:32:14 +00004269Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4270 QualType T = Importer.Import(E->getType());
4271 if (T.isNull())
4272 return 0;
4273
4274 Expr *SubExpr = Importer.Import(E->getSubExpr());
4275 if (!SubExpr)
4276 return 0;
4277
4278 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4279 if (!TInfo && E->getTypeInfoAsWritten())
4280 return 0;
4281
John McCallcf142162010-08-07 06:22:56 +00004282 CXXCastPath BasePath;
4283 if (ImportCastPath(E, BasePath))
4284 return 0;
4285
John McCall7decc9e2010-11-18 06:31:45 +00004286 return CStyleCastExpr::Create(Importer.getToContext(), T,
4287 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004288 SubExpr, &BasePath, TInfo,
4289 Importer.Import(E->getLParenLoc()),
4290 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004291}
4292
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004293ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004294 ASTContext &FromContext, FileManager &FromFileManager,
4295 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004296 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004297 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4298 Minimal(MinimalImport)
4299{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004300 ImportedDecls[FromContext.getTranslationUnitDecl()]
4301 = ToContext.getTranslationUnitDecl();
4302}
4303
4304ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004305
4306QualType ASTImporter::Import(QualType FromT) {
4307 if (FromT.isNull())
4308 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004309
4310 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004311
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004312 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004313 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4314 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004315 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004316 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004317
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004318 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004319 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004320 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004321 if (ToT.isNull())
4322 return ToT;
4323
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004324 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004325 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004326
John McCall424cec92011-01-19 06:33:43 +00004327 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004328}
4329
Douglas Gregor62d311f2010-02-09 19:21:46 +00004330TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004331 if (!FromTSI)
4332 return FromTSI;
4333
4334 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004335 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004336 QualType T = Import(FromTSI->getType());
4337 if (T.isNull())
4338 return 0;
4339
4340 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004341 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004342}
4343
4344Decl *ASTImporter::Import(Decl *FromD) {
4345 if (!FromD)
4346 return 0;
4347
Douglas Gregord451ea92011-07-29 23:31:30 +00004348 ASTNodeImporter Importer(*this);
4349
Douglas Gregor62d311f2010-02-09 19:21:46 +00004350 // Check whether we've already imported this declaration.
4351 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004352 if (Pos != ImportedDecls.end()) {
4353 Decl *ToD = Pos->second;
4354 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4355 return ToD;
4356 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004357
4358 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004359 Decl *ToD = Importer.Visit(FromD);
4360 if (!ToD)
4361 return 0;
4362
4363 // Record the imported declaration.
4364 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004365
4366 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4367 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004368 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004369 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004370 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004371 // When we've finished transforming a typedef, see whether it was the
4372 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004373 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004374 FromTag = AnonTagsWithPendingTypedefs.begin(),
4375 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4376 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004377 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004378 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4379 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004380 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004381 AnonTagsWithPendingTypedefs.erase(FromTag);
4382 break;
4383 }
4384 }
4385 }
4386 }
4387
Douglas Gregor62d311f2010-02-09 19:21:46 +00004388 return ToD;
4389}
4390
4391DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4392 if (!FromDC)
4393 return FromDC;
4394
Douglas Gregor95d82832012-01-24 18:36:04 +00004395 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004396 if (!ToDC)
4397 return 0;
4398
4399 // When we're using a record/enum/Objective-C class/protocol as a context, we
4400 // need it to have a definition.
4401 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004402 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004403 if (ToRecord->isCompleteDefinition()) {
4404 // Do nothing.
4405 } else if (FromRecord->isCompleteDefinition()) {
4406 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4407 ASTNodeImporter::IDK_Basic);
4408 } else {
4409 CompleteDecl(ToRecord);
4410 }
4411 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4412 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4413 if (ToEnum->isCompleteDefinition()) {
4414 // Do nothing.
4415 } else if (FromEnum->isCompleteDefinition()) {
4416 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4417 ASTNodeImporter::IDK_Basic);
4418 } else {
4419 CompleteDecl(ToEnum);
4420 }
4421 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4422 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4423 if (ToClass->getDefinition()) {
4424 // Do nothing.
4425 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4426 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4427 ASTNodeImporter::IDK_Basic);
4428 } else {
4429 CompleteDecl(ToClass);
4430 }
4431 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4432 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4433 if (ToProto->getDefinition()) {
4434 // Do nothing.
4435 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4436 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4437 ASTNodeImporter::IDK_Basic);
4438 } else {
4439 CompleteDecl(ToProto);
4440 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004441 }
4442
4443 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004444}
4445
4446Expr *ASTImporter::Import(Expr *FromE) {
4447 if (!FromE)
4448 return 0;
4449
4450 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4451}
4452
4453Stmt *ASTImporter::Import(Stmt *FromS) {
4454 if (!FromS)
4455 return 0;
4456
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004457 // Check whether we've already imported this declaration.
4458 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4459 if (Pos != ImportedStmts.end())
4460 return Pos->second;
4461
4462 // Import the type
4463 ASTNodeImporter Importer(*this);
4464 Stmt *ToS = Importer.Visit(FromS);
4465 if (!ToS)
4466 return 0;
4467
4468 // Record the imported declaration.
4469 ImportedStmts[FromS] = ToS;
4470 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004471}
4472
4473NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4474 if (!FromNNS)
4475 return 0;
4476
Douglas Gregor90ebf252011-04-27 16:48:40 +00004477 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4478
4479 switch (FromNNS->getKind()) {
4480 case NestedNameSpecifier::Identifier:
4481 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4482 return NestedNameSpecifier::Create(ToContext, prefix, II);
4483 }
4484 return 0;
4485
4486 case NestedNameSpecifier::Namespace:
4487 if (NamespaceDecl *NS =
4488 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4489 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4490 }
4491 return 0;
4492
4493 case NestedNameSpecifier::NamespaceAlias:
4494 if (NamespaceAliasDecl *NSAD =
4495 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4496 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4497 }
4498 return 0;
4499
4500 case NestedNameSpecifier::Global:
4501 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4502
4503 case NestedNameSpecifier::TypeSpec:
4504 case NestedNameSpecifier::TypeSpecWithTemplate: {
4505 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4506 if (!T.isNull()) {
4507 bool bTemplate = FromNNS->getKind() ==
4508 NestedNameSpecifier::TypeSpecWithTemplate;
4509 return NestedNameSpecifier::Create(ToContext, prefix,
4510 bTemplate, T.getTypePtr());
4511 }
4512 }
4513 return 0;
4514 }
4515
4516 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004517}
4518
Douglas Gregor14454802011-02-25 02:25:35 +00004519NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4520 // FIXME: Implement!
4521 return NestedNameSpecifierLoc();
4522}
4523
Douglas Gregore2e50d332010-12-01 01:36:18 +00004524TemplateName ASTImporter::Import(TemplateName From) {
4525 switch (From.getKind()) {
4526 case TemplateName::Template:
4527 if (TemplateDecl *ToTemplate
4528 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4529 return TemplateName(ToTemplate);
4530
4531 return TemplateName();
4532
4533 case TemplateName::OverloadedTemplate: {
4534 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4535 UnresolvedSet<2> ToTemplates;
4536 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4537 E = FromStorage->end();
4538 I != E; ++I) {
4539 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4540 ToTemplates.addDecl(To);
4541 else
4542 return TemplateName();
4543 }
4544 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4545 ToTemplates.end());
4546 }
4547
4548 case TemplateName::QualifiedTemplate: {
4549 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4550 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4551 if (!Qualifier)
4552 return TemplateName();
4553
4554 if (TemplateDecl *ToTemplate
4555 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4556 return ToContext.getQualifiedTemplateName(Qualifier,
4557 QTN->hasTemplateKeyword(),
4558 ToTemplate);
4559
4560 return TemplateName();
4561 }
4562
4563 case TemplateName::DependentTemplate: {
4564 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4565 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4566 if (!Qualifier)
4567 return TemplateName();
4568
4569 if (DTN->isIdentifier()) {
4570 return ToContext.getDependentTemplateName(Qualifier,
4571 Import(DTN->getIdentifier()));
4572 }
4573
4574 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4575 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004576
4577 case TemplateName::SubstTemplateTemplateParm: {
4578 SubstTemplateTemplateParmStorage *subst
4579 = From.getAsSubstTemplateTemplateParm();
4580 TemplateTemplateParmDecl *param
4581 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4582 if (!param)
4583 return TemplateName();
4584
4585 TemplateName replacement = Import(subst->getReplacement());
4586 if (replacement.isNull()) return TemplateName();
4587
4588 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4589 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004590
4591 case TemplateName::SubstTemplateTemplateParmPack: {
4592 SubstTemplateTemplateParmPackStorage *SubstPack
4593 = From.getAsSubstTemplateTemplateParmPack();
4594 TemplateTemplateParmDecl *Param
4595 = cast_or_null<TemplateTemplateParmDecl>(
4596 Import(SubstPack->getParameterPack()));
4597 if (!Param)
4598 return TemplateName();
4599
4600 ASTNodeImporter Importer(*this);
4601 TemplateArgument ArgPack
4602 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4603 if (ArgPack.isNull())
4604 return TemplateName();
4605
4606 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4607 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004608 }
4609
4610 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004611}
4612
Douglas Gregor62d311f2010-02-09 19:21:46 +00004613SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4614 if (FromLoc.isInvalid())
4615 return SourceLocation();
4616
Douglas Gregor811663e2010-02-10 00:15:17 +00004617 SourceManager &FromSM = FromContext.getSourceManager();
4618
4619 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004620 // don't have to import macro expansions.
4621 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004622 FromLoc = FromSM.getSpellingLoc(FromLoc);
4623 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4624 SourceManager &ToSM = ToContext.getSourceManager();
4625 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004626 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004627}
4628
4629SourceRange ASTImporter::Import(SourceRange FromRange) {
4630 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4631}
4632
Douglas Gregor811663e2010-02-10 00:15:17 +00004633FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004634 llvm::DenseMap<FileID, FileID>::iterator Pos
4635 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004636 if (Pos != ImportedFileIDs.end())
4637 return Pos->second;
4638
4639 SourceManager &FromSM = FromContext.getSourceManager();
4640 SourceManager &ToSM = ToContext.getSourceManager();
4641 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004642 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004643
4644 // Include location of this file.
4645 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4646
4647 // Map the FileID for to the "to" source manager.
4648 FileID ToID;
4649 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004650 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004651 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4652 // disk again
4653 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4654 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004655 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004656 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4657 FromSLoc.getFile().getFileCharacteristic());
4658 } else {
4659 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004660 const llvm::MemoryBuffer *
4661 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004662 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004663 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004664 FromBuf->getBufferIdentifier());
4665 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4666 }
4667
4668
Sebastian Redl99219f12010-09-30 01:03:06 +00004669 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004670 return ToID;
4671}
4672
Douglas Gregor0a791672011-01-18 03:11:38 +00004673void ASTImporter::ImportDefinition(Decl *From) {
4674 Decl *To = Import(From);
4675 if (!To)
4676 return;
4677
4678 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4679 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004680
4681 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4682 if (!ToRecord->getDefinition()) {
4683 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004684 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004685 return;
4686 }
4687 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004688
4689 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4690 if (!ToEnum->getDefinition()) {
4691 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004692 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004693 return;
4694 }
4695 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004696
4697 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4698 if (!ToIFace->getDefinition()) {
4699 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004700 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004701 return;
4702 }
4703 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004704
Douglas Gregor2aa53772012-01-24 17:42:07 +00004705 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4706 if (!ToProto->getDefinition()) {
4707 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004708 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004709 return;
4710 }
4711 }
4712
Douglas Gregor0a791672011-01-18 03:11:38 +00004713 Importer.ImportDeclContext(FromDC, true);
4714 }
4715}
4716
Douglas Gregor96e578d2010-02-05 17:54:41 +00004717DeclarationName ASTImporter::Import(DeclarationName FromName) {
4718 if (!FromName)
4719 return DeclarationName();
4720
4721 switch (FromName.getNameKind()) {
4722 case DeclarationName::Identifier:
4723 return Import(FromName.getAsIdentifierInfo());
4724
4725 case DeclarationName::ObjCZeroArgSelector:
4726 case DeclarationName::ObjCOneArgSelector:
4727 case DeclarationName::ObjCMultiArgSelector:
4728 return Import(FromName.getObjCSelector());
4729
4730 case DeclarationName::CXXConstructorName: {
4731 QualType T = Import(FromName.getCXXNameType());
4732 if (T.isNull())
4733 return DeclarationName();
4734
4735 return ToContext.DeclarationNames.getCXXConstructorName(
4736 ToContext.getCanonicalType(T));
4737 }
4738
4739 case DeclarationName::CXXDestructorName: {
4740 QualType T = Import(FromName.getCXXNameType());
4741 if (T.isNull())
4742 return DeclarationName();
4743
4744 return ToContext.DeclarationNames.getCXXDestructorName(
4745 ToContext.getCanonicalType(T));
4746 }
4747
4748 case DeclarationName::CXXConversionFunctionName: {
4749 QualType T = Import(FromName.getCXXNameType());
4750 if (T.isNull())
4751 return DeclarationName();
4752
4753 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4754 ToContext.getCanonicalType(T));
4755 }
4756
4757 case DeclarationName::CXXOperatorName:
4758 return ToContext.DeclarationNames.getCXXOperatorName(
4759 FromName.getCXXOverloadedOperator());
4760
4761 case DeclarationName::CXXLiteralOperatorName:
4762 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4763 Import(FromName.getCXXLiteralIdentifier()));
4764
4765 case DeclarationName::CXXUsingDirective:
4766 // FIXME: STATICS!
4767 return DeclarationName::getUsingDirectiveName();
4768 }
4769
David Blaikiee4d798f2012-01-20 21:50:17 +00004770 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004771}
4772
Douglas Gregore2e50d332010-12-01 01:36:18 +00004773IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004774 if (!FromId)
4775 return 0;
4776
4777 return &ToContext.Idents.get(FromId->getName());
4778}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004779
Douglas Gregor43f54792010-02-17 02:12:47 +00004780Selector ASTImporter::Import(Selector FromSel) {
4781 if (FromSel.isNull())
4782 return Selector();
4783
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004784 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004785 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4786 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4787 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4788 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4789}
4790
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004791DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4792 DeclContext *DC,
4793 unsigned IDNS,
4794 NamedDecl **Decls,
4795 unsigned NumDecls) {
4796 return Name;
4797}
4798
4799DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004800 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004801}
4802
4803DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004804 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004805}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004806
Douglas Gregor2e15c842012-02-01 21:00:38 +00004807void ASTImporter::CompleteDecl (Decl *D) {
4808 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4809 if (!ID->getDefinition())
4810 ID->startDefinition();
4811 }
4812 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4813 if (!PD->getDefinition())
4814 PD->startDefinition();
4815 }
4816 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4817 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4818 TD->startDefinition();
4819 TD->setCompleteDefinition(true);
4820 }
4821 }
4822 else {
4823 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4824 }
4825}
4826
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004827Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4828 ImportedDecls[From] = To;
4829 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004830}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004831
Douglas Gregordd6006f2012-07-17 21:16:27 +00004832bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4833 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004834 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004835 = ImportedTypes.find(From.getTypePtr());
4836 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4837 return true;
4838
Douglas Gregordd6006f2012-07-17 21:16:27 +00004839 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4840 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004841 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004842}