blob: 34e699496bcab797fb6800250007a477eae76f7e [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 Gregordd6006f2012-07-17 21:16:27 +0000231 if (!Complain)
232 return DiagnosticBuilder::getEmpty();
233
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000234 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000235 }
236
237 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregordd6006f2012-07-17 21:16:27 +0000238 if (!Complain)
239 return DiagnosticBuilder::getEmpty();
240
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000241 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000242 }
243 };
244}
245
246static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
247 QualType T1, QualType T2);
248static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
249 Decl *D1, Decl *D2);
250
Douglas Gregor3996e242010-02-15 22:01:00 +0000251/// \brief Determine structural equivalence of two expressions.
252static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
253 Expr *E1, Expr *E2) {
254 if (!E1 || !E2)
255 return E1 == E2;
256
257 // FIXME: Actually perform a structural comparison!
258 return true;
259}
260
261/// \brief Determine whether two identifiers are equivalent.
262static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
263 const IdentifierInfo *Name2) {
264 if (!Name1 || !Name2)
265 return Name1 == Name2;
266
267 return Name1->getName() == Name2->getName();
268}
269
270/// \brief Determine whether two nested-name-specifiers are equivalent.
271static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
272 NestedNameSpecifier *NNS1,
273 NestedNameSpecifier *NNS2) {
274 // FIXME: Implement!
275 return true;
276}
277
278/// \brief Determine whether two template arguments are equivalent.
279static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
280 const TemplateArgument &Arg1,
281 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000282 if (Arg1.getKind() != Arg2.getKind())
283 return false;
284
285 switch (Arg1.getKind()) {
286 case TemplateArgument::Null:
287 return true;
288
289 case TemplateArgument::Type:
290 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
291
292 case TemplateArgument::Integral:
293 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
294 Arg2.getIntegralType()))
295 return false;
296
Eric Christopher6dcc3762012-07-15 00:23:57 +0000297 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000298
299 case TemplateArgument::Declaration:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000300 if (!Arg1.getAsDecl() || !Arg2.getAsDecl())
301 return !Arg1.getAsDecl() && !Arg2.getAsDecl();
Douglas Gregore2e50d332010-12-01 01:36:18 +0000302 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
303
304 case TemplateArgument::Template:
305 return IsStructurallyEquivalent(Context,
306 Arg1.getAsTemplate(),
307 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000308
309 case TemplateArgument::TemplateExpansion:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplateOrTemplatePattern(),
312 Arg2.getAsTemplateOrTemplatePattern());
313
Douglas Gregore2e50d332010-12-01 01:36:18 +0000314 case TemplateArgument::Expression:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsExpr(), Arg2.getAsExpr());
317
318 case TemplateArgument::Pack:
319 if (Arg1.pack_size() != Arg2.pack_size())
320 return false;
321
322 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
323 if (!IsStructurallyEquivalent(Context,
324 Arg1.pack_begin()[I],
325 Arg2.pack_begin()[I]))
326 return false;
327
328 return true;
329 }
330
331 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000332}
333
334/// \brief Determine structural equivalence for the common part of array
335/// types.
336static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
337 const ArrayType *Array1,
338 const ArrayType *Array2) {
339 if (!IsStructurallyEquivalent(Context,
340 Array1->getElementType(),
341 Array2->getElementType()))
342 return false;
343 if (Array1->getSizeModifier() != Array2->getSizeModifier())
344 return false;
345 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
346 return false;
347
348 return true;
349}
350
351/// \brief Determine structural equivalence of two types.
352static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
353 QualType T1, QualType T2) {
354 if (T1.isNull() || T2.isNull())
355 return T1.isNull() && T2.isNull();
356
357 if (!Context.StrictTypeSpelling) {
358 // We aren't being strict about token-to-token equivalence of types,
359 // so map down to the canonical type.
360 T1 = Context.C1.getCanonicalType(T1);
361 T2 = Context.C2.getCanonicalType(T2);
362 }
363
364 if (T1.getQualifiers() != T2.getQualifiers())
365 return false;
366
Douglas Gregorb4964f72010-02-15 23:54:17 +0000367 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000368
Douglas Gregorb4964f72010-02-15 23:54:17 +0000369 if (T1->getTypeClass() != T2->getTypeClass()) {
370 // Compare function types with prototypes vs. without prototypes as if
371 // both did not have prototypes.
372 if (T1->getTypeClass() == Type::FunctionProto &&
373 T2->getTypeClass() == Type::FunctionNoProto)
374 TC = Type::FunctionNoProto;
375 else if (T1->getTypeClass() == Type::FunctionNoProto &&
376 T2->getTypeClass() == Type::FunctionProto)
377 TC = Type::FunctionNoProto;
378 else
379 return false;
380 }
381
382 switch (TC) {
383 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000384 // FIXME: Deal with Char_S/Char_U.
385 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
386 return false;
387 break;
388
389 case Type::Complex:
390 if (!IsStructurallyEquivalent(Context,
391 cast<ComplexType>(T1)->getElementType(),
392 cast<ComplexType>(T2)->getElementType()))
393 return false;
394 break;
395
396 case Type::Pointer:
397 if (!IsStructurallyEquivalent(Context,
398 cast<PointerType>(T1)->getPointeeType(),
399 cast<PointerType>(T2)->getPointeeType()))
400 return false;
401 break;
402
403 case Type::BlockPointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<BlockPointerType>(T1)->getPointeeType(),
406 cast<BlockPointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::LValueReference:
411 case Type::RValueReference: {
412 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
413 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
414 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
415 return false;
416 if (Ref1->isInnerRef() != Ref2->isInnerRef())
417 return false;
418 if (!IsStructurallyEquivalent(Context,
419 Ref1->getPointeeTypeAsWritten(),
420 Ref2->getPointeeTypeAsWritten()))
421 return false;
422 break;
423 }
424
425 case Type::MemberPointer: {
426 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
427 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
428 if (!IsStructurallyEquivalent(Context,
429 MemPtr1->getPointeeType(),
430 MemPtr2->getPointeeType()))
431 return false;
432 if (!IsStructurallyEquivalent(Context,
433 QualType(MemPtr1->getClass(), 0),
434 QualType(MemPtr2->getClass(), 0)))
435 return false;
436 break;
437 }
438
439 case Type::ConstantArray: {
440 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
441 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000442 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000443 return false;
444
445 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
446 return false;
447 break;
448 }
449
450 case Type::IncompleteArray:
451 if (!IsArrayStructurallyEquivalent(Context,
452 cast<ArrayType>(T1),
453 cast<ArrayType>(T2)))
454 return false;
455 break;
456
457 case Type::VariableArray: {
458 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
459 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
460 if (!IsStructurallyEquivalent(Context,
461 Array1->getSizeExpr(), Array2->getSizeExpr()))
462 return false;
463
464 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
465 return false;
466
467 break;
468 }
469
470 case Type::DependentSizedArray: {
471 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
472 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
473 if (!IsStructurallyEquivalent(Context,
474 Array1->getSizeExpr(), Array2->getSizeExpr()))
475 return false;
476
477 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
478 return false;
479
480 break;
481 }
482
483 case Type::DependentSizedExtVector: {
484 const DependentSizedExtVectorType *Vec1
485 = cast<DependentSizedExtVectorType>(T1);
486 const DependentSizedExtVectorType *Vec2
487 = cast<DependentSizedExtVectorType>(T2);
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
490 return false;
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 break;
496 }
497
498 case Type::Vector:
499 case Type::ExtVector: {
500 const VectorType *Vec1 = cast<VectorType>(T1);
501 const VectorType *Vec2 = cast<VectorType>(T2);
502 if (!IsStructurallyEquivalent(Context,
503 Vec1->getElementType(),
504 Vec2->getElementType()))
505 return false;
506 if (Vec1->getNumElements() != Vec2->getNumElements())
507 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000508 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000509 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000510 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000511 }
512
513 case Type::FunctionProto: {
514 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
515 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
516 if (Proto1->getNumArgs() != Proto2->getNumArgs())
517 return false;
518 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
519 if (!IsStructurallyEquivalent(Context,
520 Proto1->getArgType(I),
521 Proto2->getArgType(I)))
522 return false;
523 }
524 if (Proto1->isVariadic() != Proto2->isVariadic())
525 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000527 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000528 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
529 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
530 return false;
531 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
532 if (!IsStructurallyEquivalent(Context,
533 Proto1->getExceptionType(I),
534 Proto2->getExceptionType(I)))
535 return false;
536 }
537 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000538 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000539 Proto1->getNoexceptExpr(),
540 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000541 return false;
542 }
543 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
544 return false;
545
546 // Fall through to check the bits common with FunctionNoProtoType.
547 }
548
549 case Type::FunctionNoProto: {
550 const FunctionType *Function1 = cast<FunctionType>(T1);
551 const FunctionType *Function2 = cast<FunctionType>(T2);
552 if (!IsStructurallyEquivalent(Context,
553 Function1->getResultType(),
554 Function2->getResultType()))
555 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000556 if (Function1->getExtInfo() != Function2->getExtInfo())
557 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000558 break;
559 }
560
561 case Type::UnresolvedUsing:
562 if (!IsStructurallyEquivalent(Context,
563 cast<UnresolvedUsingType>(T1)->getDecl(),
564 cast<UnresolvedUsingType>(T2)->getDecl()))
565 return false;
566
567 break;
John McCall81904512011-01-06 01:58:22 +0000568
569 case Type::Attributed:
570 if (!IsStructurallyEquivalent(Context,
571 cast<AttributedType>(T1)->getModifiedType(),
572 cast<AttributedType>(T2)->getModifiedType()))
573 return false;
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getEquivalentType(),
576 cast<AttributedType>(T2)->getEquivalentType()))
577 return false;
578 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000579
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000580 case Type::Paren:
581 if (!IsStructurallyEquivalent(Context,
582 cast<ParenType>(T1)->getInnerType(),
583 cast<ParenType>(T2)->getInnerType()))
584 return false;
585 break;
586
Douglas Gregor3996e242010-02-15 22:01:00 +0000587 case Type::Typedef:
588 if (!IsStructurallyEquivalent(Context,
589 cast<TypedefType>(T1)->getDecl(),
590 cast<TypedefType>(T2)->getDecl()))
591 return false;
592 break;
593
594 case Type::TypeOfExpr:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
597 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
598 return false;
599 break;
600
601 case Type::TypeOf:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfType>(T1)->getUnderlyingType(),
604 cast<TypeOfType>(T2)->getUnderlyingType()))
605 return false;
606 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000607
608 case Type::UnaryTransform:
609 if (!IsStructurallyEquivalent(Context,
610 cast<UnaryTransformType>(T1)->getUnderlyingType(),
611 cast<UnaryTransformType>(T1)->getUnderlyingType()))
612 return false;
613 break;
614
Douglas Gregor3996e242010-02-15 22:01:00 +0000615 case Type::Decltype:
616 if (!IsStructurallyEquivalent(Context,
617 cast<DecltypeType>(T1)->getUnderlyingExpr(),
618 cast<DecltypeType>(T2)->getUnderlyingExpr()))
619 return false;
620 break;
621
Richard Smith30482bc2011-02-20 03:19:35 +0000622 case Type::Auto:
623 if (!IsStructurallyEquivalent(Context,
624 cast<AutoType>(T1)->getDeducedType(),
625 cast<AutoType>(T2)->getDeducedType()))
626 return false;
627 break;
628
Douglas Gregor3996e242010-02-15 22:01:00 +0000629 case Type::Record:
630 case Type::Enum:
631 if (!IsStructurallyEquivalent(Context,
632 cast<TagType>(T1)->getDecl(),
633 cast<TagType>(T2)->getDecl()))
634 return false;
635 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000636
Douglas Gregor3996e242010-02-15 22:01:00 +0000637 case Type::TemplateTypeParm: {
638 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
639 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
640 if (Parm1->getDepth() != Parm2->getDepth())
641 return false;
642 if (Parm1->getIndex() != Parm2->getIndex())
643 return false;
644 if (Parm1->isParameterPack() != Parm2->isParameterPack())
645 return false;
646
647 // Names of template type parameters are never significant.
648 break;
649 }
650
651 case Type::SubstTemplateTypeParm: {
652 const SubstTemplateTypeParmType *Subst1
653 = cast<SubstTemplateTypeParmType>(T1);
654 const SubstTemplateTypeParmType *Subst2
655 = cast<SubstTemplateTypeParmType>(T2);
656 if (!IsStructurallyEquivalent(Context,
657 QualType(Subst1->getReplacedParameter(), 0),
658 QualType(Subst2->getReplacedParameter(), 0)))
659 return false;
660 if (!IsStructurallyEquivalent(Context,
661 Subst1->getReplacementType(),
662 Subst2->getReplacementType()))
663 return false;
664 break;
665 }
666
Douglas Gregorfb322d82011-01-14 05:11:40 +0000667 case Type::SubstTemplateTypeParmPack: {
668 const SubstTemplateTypeParmPackType *Subst1
669 = cast<SubstTemplateTypeParmPackType>(T1);
670 const SubstTemplateTypeParmPackType *Subst2
671 = cast<SubstTemplateTypeParmPackType>(T2);
672 if (!IsStructurallyEquivalent(Context,
673 QualType(Subst1->getReplacedParameter(), 0),
674 QualType(Subst2->getReplacedParameter(), 0)))
675 return false;
676 if (!IsStructurallyEquivalent(Context,
677 Subst1->getArgumentPack(),
678 Subst2->getArgumentPack()))
679 return false;
680 break;
681 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000682 case Type::TemplateSpecialization: {
683 const TemplateSpecializationType *Spec1
684 = cast<TemplateSpecializationType>(T1);
685 const TemplateSpecializationType *Spec2
686 = cast<TemplateSpecializationType>(T2);
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getTemplateName(),
689 Spec2->getTemplateName()))
690 return false;
691 if (Spec1->getNumArgs() != Spec2->getNumArgs())
692 return false;
693 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getArg(I), Spec2->getArg(I)))
696 return false;
697 }
698 break;
699 }
700
Abramo Bagnara6150c882010-05-11 21:36:43 +0000701 case Type::Elaborated: {
702 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
703 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
704 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
705 if (Elab1->getKeyword() != Elab2->getKeyword())
706 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000708 Elab1->getQualifier(),
709 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000710 return false;
711 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000712 Elab1->getNamedType(),
713 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000714 return false;
715 break;
716 }
717
John McCalle78aac42010-03-10 03:28:59 +0000718 case Type::InjectedClassName: {
719 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
720 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
721 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000722 Inj1->getInjectedSpecializationType(),
723 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000724 return false;
725 break;
726 }
727
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000728 case Type::DependentName: {
729 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
730 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000731 if (!IsStructurallyEquivalent(Context,
732 Typename1->getQualifier(),
733 Typename2->getQualifier()))
734 return false;
735 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
736 Typename2->getIdentifier()))
737 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000738
739 break;
740 }
741
John McCallc392f372010-06-11 00:33:02 +0000742 case Type::DependentTemplateSpecialization: {
743 const DependentTemplateSpecializationType *Spec1 =
744 cast<DependentTemplateSpecializationType>(T1);
745 const DependentTemplateSpecializationType *Spec2 =
746 cast<DependentTemplateSpecializationType>(T2);
747 if (!IsStructurallyEquivalent(Context,
748 Spec1->getQualifier(),
749 Spec2->getQualifier()))
750 return false;
751 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
752 Spec2->getIdentifier()))
753 return false;
754 if (Spec1->getNumArgs() != Spec2->getNumArgs())
755 return false;
756 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
757 if (!IsStructurallyEquivalent(Context,
758 Spec1->getArg(I), Spec2->getArg(I)))
759 return false;
760 }
761 break;
762 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000763
764 case Type::PackExpansion:
765 if (!IsStructurallyEquivalent(Context,
766 cast<PackExpansionType>(T1)->getPattern(),
767 cast<PackExpansionType>(T2)->getPattern()))
768 return false;
769 break;
770
Douglas Gregor3996e242010-02-15 22:01:00 +0000771 case Type::ObjCInterface: {
772 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
773 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
774 if (!IsStructurallyEquivalent(Context,
775 Iface1->getDecl(), Iface2->getDecl()))
776 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000777 break;
778 }
779
780 case Type::ObjCObject: {
781 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
782 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
783 if (!IsStructurallyEquivalent(Context,
784 Obj1->getBaseType(),
785 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000786 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000787 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
788 return false;
789 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000790 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000791 Obj1->getProtocol(I),
792 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000793 return false;
794 }
795 break;
796 }
797
798 case Type::ObjCObjectPointer: {
799 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
800 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
801 if (!IsStructurallyEquivalent(Context,
802 Ptr1->getPointeeType(),
803 Ptr2->getPointeeType()))
804 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000805 break;
806 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000807
808 case Type::Atomic: {
809 if (!IsStructurallyEquivalent(Context,
810 cast<AtomicType>(T1)->getValueType(),
811 cast<AtomicType>(T2)->getValueType()))
812 return false;
813 break;
814 }
815
Douglas Gregor3996e242010-02-15 22:01:00 +0000816 } // end switch
817
818 return true;
819}
820
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000821/// \brief Determine structural equivalence of two fields.
822static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
823 FieldDecl *Field1, FieldDecl *Field2) {
824 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
825
826 if (!IsStructurallyEquivalent(Context,
827 Field1->getType(), Field2->getType())) {
828 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
829 << Context.C2.getTypeDeclType(Owner2);
830 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
831 << Field2->getDeclName() << Field2->getType();
832 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
833 << Field1->getDeclName() << Field1->getType();
834 return false;
835 }
836
837 if (Field1->isBitField() != Field2->isBitField()) {
838 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
839 << Context.C2.getTypeDeclType(Owner2);
840 if (Field1->isBitField()) {
841 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
842 << Field1->getDeclName() << Field1->getType()
843 << Field1->getBitWidthValue(Context.C1);
844 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
845 << Field2->getDeclName();
846 } else {
847 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
848 << Field2->getDeclName() << Field2->getType()
849 << Field2->getBitWidthValue(Context.C2);
850 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
851 << Field1->getDeclName();
852 }
853 return false;
854 }
855
856 if (Field1->isBitField()) {
857 // Make sure that the bit-fields are the same length.
858 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
859 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
860
861 if (Bits1 != Bits2) {
862 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(Owner2);
864 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
865 << Field2->getDeclName() << Field2->getType() << Bits2;
866 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
867 << Field1->getDeclName() << Field1->getType() << Bits1;
868 return false;
869 }
870 }
871
872 return true;
873}
874
Douglas Gregor3996e242010-02-15 22:01:00 +0000875/// \brief Determine structural equivalence of two records.
876static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
877 RecordDecl *D1, RecordDecl *D2) {
878 if (D1->isUnion() != D2->isUnion()) {
879 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
880 << Context.C2.getTypeDeclType(D2);
881 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
882 << D1->getDeclName() << (unsigned)D1->getTagKind();
883 return false;
884 }
885
Douglas Gregore2e50d332010-12-01 01:36:18 +0000886 // If both declarations are class template specializations, we know
887 // the ODR applies, so check the template and template arguments.
888 ClassTemplateSpecializationDecl *Spec1
889 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
890 ClassTemplateSpecializationDecl *Spec2
891 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
892 if (Spec1 && Spec2) {
893 // Check that the specialized templates are the same.
894 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
895 Spec2->getSpecializedTemplate()))
896 return false;
897
898 // Check that the template arguments are the same.
899 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
900 return false;
901
902 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
903 if (!IsStructurallyEquivalent(Context,
904 Spec1->getTemplateArgs().get(I),
905 Spec2->getTemplateArgs().get(I)))
906 return false;
907 }
908 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000909 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000910 else if (Spec1 || Spec2)
911 return false;
912
Douglas Gregorb4964f72010-02-15 23:54:17 +0000913 // Compare the definitions of these two records. If either or both are
914 // incomplete, we assume that they are equivalent.
915 D1 = D1->getDefinition();
916 D2 = D2->getDefinition();
917 if (!D1 || !D2)
918 return true;
919
Douglas Gregor3996e242010-02-15 22:01:00 +0000920 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
921 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
922 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
923 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000924 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000926 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000927 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000928 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000929 return false;
930 }
931
932 // Check the base classes.
933 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
934 BaseEnd1 = D1CXX->bases_end(),
935 Base2 = D2CXX->bases_begin();
936 Base1 != BaseEnd1;
937 ++Base1, ++Base2) {
938 if (!IsStructurallyEquivalent(Context,
939 Base1->getType(), Base2->getType())) {
940 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
941 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000942 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000943 << Base2->getType()
944 << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000945 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000946 << Base1->getType()
947 << Base1->getSourceRange();
948 return false;
949 }
950
951 // Check virtual vs. non-virtual inheritance mismatch.
952 if (Base1->isVirtual() != Base2->isVirtual()) {
953 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
954 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000955 Context.Diag2(Base2->getLocStart(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000956 diag::note_odr_virtual_base)
957 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000958 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000959 << Base1->isVirtual()
960 << Base1->getSourceRange();
961 return false;
962 }
963 }
964 } else if (D1CXX->getNumBases() > 0) {
965 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
966 << Context.C2.getTypeDeclType(D2);
967 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000968 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000969 << Base1->getType()
970 << Base1->getSourceRange();
971 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
972 return false;
973 }
974 }
975
976 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000977 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000978 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000979 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000980 Field1End = D1->field_end();
981 Field1 != Field1End;
982 ++Field1, ++Field2) {
983 if (Field2 == Field2End) {
984 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
985 << Context.C2.getTypeDeclType(D2);
986 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
987 << Field1->getDeclName() << Field1->getType();
988 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
989 return false;
990 }
991
David Blaikie40ed2972012-06-06 20:45:41 +0000992 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000993 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000994 }
995
996 if (Field2 != Field2End) {
997 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
998 << Context.C2.getTypeDeclType(D2);
999 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1000 << Field2->getDeclName() << Field2->getType();
1001 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1002 return false;
1003 }
1004
1005 return true;
1006}
1007
1008/// \brief Determine structural equivalence of two enums.
1009static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1010 EnumDecl *D1, EnumDecl *D2) {
1011 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1012 EC2End = D2->enumerator_end();
1013 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1014 EC1End = D1->enumerator_end();
1015 EC1 != EC1End; ++EC1, ++EC2) {
1016 if (EC2 == EC2End) {
1017 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1018 << Context.C2.getTypeDeclType(D2);
1019 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1020 << EC1->getDeclName()
1021 << EC1->getInitVal().toString(10);
1022 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1023 return false;
1024 }
1025
1026 llvm::APSInt Val1 = EC1->getInitVal();
1027 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001028 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001029 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1030 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1031 << Context.C2.getTypeDeclType(D2);
1032 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1033 << EC2->getDeclName()
1034 << EC2->getInitVal().toString(10);
1035 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1036 << EC1->getDeclName()
1037 << EC1->getInitVal().toString(10);
1038 return false;
1039 }
1040 }
1041
1042 if (EC2 != EC2End) {
1043 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1044 << Context.C2.getTypeDeclType(D2);
1045 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1046 << EC2->getDeclName()
1047 << EC2->getInitVal().toString(10);
1048 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1049 return false;
1050 }
1051
1052 return true;
1053}
Douglas Gregora082a492010-11-30 19:14:50 +00001054
1055static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1056 TemplateParameterList *Params1,
1057 TemplateParameterList *Params2) {
1058 if (Params1->size() != Params2->size()) {
1059 Context.Diag2(Params2->getTemplateLoc(),
1060 diag::err_odr_different_num_template_parameters)
1061 << Params1->size() << Params2->size();
1062 Context.Diag1(Params1->getTemplateLoc(),
1063 diag::note_odr_template_parameter_list);
1064 return false;
1065 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001066
Douglas Gregora082a492010-11-30 19:14:50 +00001067 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1068 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1069 Context.Diag2(Params2->getParam(I)->getLocation(),
1070 diag::err_odr_different_template_parameter_kind);
1071 Context.Diag1(Params1->getParam(I)->getLocation(),
1072 diag::note_odr_template_parameter_here);
1073 return false;
1074 }
1075
1076 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1077 Params2->getParam(I))) {
1078
1079 return false;
1080 }
1081 }
1082
1083 return true;
1084}
1085
1086static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1087 TemplateTypeParmDecl *D1,
1088 TemplateTypeParmDecl *D2) {
1089 if (D1->isParameterPack() != D2->isParameterPack()) {
1090 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1091 << D2->isParameterPack();
1092 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1093 << D1->isParameterPack();
1094 return false;
1095 }
1096
1097 return true;
1098}
1099
1100static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1101 NonTypeTemplateParmDecl *D1,
1102 NonTypeTemplateParmDecl *D2) {
1103 // FIXME: Enable once we have variadic templates.
1104#if 0
1105 if (D1->isParameterPack() != D2->isParameterPack()) {
1106 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1107 << D2->isParameterPack();
1108 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1109 << D1->isParameterPack();
1110 return false;
1111 }
1112#endif
1113
1114 // Check types.
1115 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1116 Context.Diag2(D2->getLocation(),
1117 diag::err_odr_non_type_parameter_type_inconsistent)
1118 << D2->getType() << D1->getType();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1120 << D1->getType();
1121 return false;
1122 }
1123
1124 return true;
1125}
1126
1127static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1128 TemplateTemplateParmDecl *D1,
1129 TemplateTemplateParmDecl *D2) {
1130 // FIXME: Enable once we have variadic templates.
1131#if 0
1132 if (D1->isParameterPack() != D2->isParameterPack()) {
1133 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1134 << D2->isParameterPack();
1135 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1136 << D1->isParameterPack();
1137 return false;
1138 }
1139#endif
1140
1141 // Check template parameter lists.
1142 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1143 D2->getTemplateParameters());
1144}
1145
1146static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1147 ClassTemplateDecl *D1,
1148 ClassTemplateDecl *D2) {
1149 // Check template parameters.
1150 if (!IsStructurallyEquivalent(Context,
1151 D1->getTemplateParameters(),
1152 D2->getTemplateParameters()))
1153 return false;
1154
1155 // Check the templated declaration.
1156 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1157 D2->getTemplatedDecl());
1158}
1159
Douglas Gregor3996e242010-02-15 22:01:00 +00001160/// \brief Determine structural equivalence of two declarations.
1161static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1162 Decl *D1, Decl *D2) {
1163 // FIXME: Check for known structural equivalences via a callback of some sort.
1164
Douglas Gregorb4964f72010-02-15 23:54:17 +00001165 // Check whether we already know that these two declarations are not
1166 // structurally equivalent.
1167 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1168 D2->getCanonicalDecl())))
1169 return false;
1170
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 // Determine whether we've already produced a tentative equivalence for D1.
1172 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1173 if (EquivToD1)
1174 return EquivToD1 == D2->getCanonicalDecl();
1175
1176 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1177 EquivToD1 = D2->getCanonicalDecl();
1178 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1179 return true;
1180}
1181
1182bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1183 Decl *D2) {
1184 if (!::IsStructurallyEquivalent(*this, D1, D2))
1185 return false;
1186
1187 return !Finish();
1188}
1189
1190bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1191 QualType T2) {
1192 if (!::IsStructurallyEquivalent(*this, T1, T2))
1193 return false;
1194
1195 return !Finish();
1196}
1197
1198bool StructuralEquivalenceContext::Finish() {
1199 while (!DeclsToCheck.empty()) {
1200 // Check the next declaration.
1201 Decl *D1 = DeclsToCheck.front();
1202 DeclsToCheck.pop_front();
1203
1204 Decl *D2 = TentativeEquivalences[D1];
1205 assert(D2 && "Unrecorded tentative equivalence?");
1206
Douglas Gregorb4964f72010-02-15 23:54:17 +00001207 bool Equivalent = true;
1208
Douglas Gregor3996e242010-02-15 22:01:00 +00001209 // FIXME: Switch on all declaration kinds. For now, we're just going to
1210 // check the obvious ones.
1211 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1212 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1213 // Check for equivalent structure names.
1214 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001215 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1216 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001217 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001218 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1219 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001220 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1221 !::IsStructurallyEquivalent(*this, Record1, Record2))
1222 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001223 } else {
1224 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001225 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001226 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001227 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1229 // Check for equivalent enum names.
1230 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001231 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1232 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001233 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001234 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1235 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001236 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1237 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1238 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001239 } else {
1240 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001241 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001242 }
Richard Smithdda56e42011-04-15 14:24:37 +00001243 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1244 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001245 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001246 Typedef2->getIdentifier()) ||
1247 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001248 Typedef1->getUnderlyingType(),
1249 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001250 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001251 } else {
1252 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001253 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001254 }
Douglas Gregora082a492010-11-30 19:14:50 +00001255 } else if (ClassTemplateDecl *ClassTemplate1
1256 = dyn_cast<ClassTemplateDecl>(D1)) {
1257 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1258 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1259 ClassTemplate2->getIdentifier()) ||
1260 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1261 Equivalent = false;
1262 } else {
1263 // Class template/non-class-template mismatch.
1264 Equivalent = false;
1265 }
1266 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1267 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1268 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1269 Equivalent = false;
1270 } else {
1271 // Kind mismatch.
1272 Equivalent = false;
1273 }
1274 } else if (NonTypeTemplateParmDecl *NTTP1
1275 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1276 if (NonTypeTemplateParmDecl *NTTP2
1277 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1278 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1279 Equivalent = false;
1280 } else {
1281 // Kind mismatch.
1282 Equivalent = false;
1283 }
1284 } else if (TemplateTemplateParmDecl *TTP1
1285 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1286 if (TemplateTemplateParmDecl *TTP2
1287 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1288 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1289 Equivalent = false;
1290 } else {
1291 // Kind mismatch.
1292 Equivalent = false;
1293 }
1294 }
1295
Douglas Gregorb4964f72010-02-15 23:54:17 +00001296 if (!Equivalent) {
1297 // Note that these two declarations are not equivalent (and we already
1298 // know about it).
1299 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1300 D2->getCanonicalDecl()));
1301 return true;
1302 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001303 // FIXME: Check other declaration kinds!
1304 }
1305
1306 return false;
1307}
1308
1309//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001310// Import Types
1311//----------------------------------------------------------------------------
1312
John McCall424cec92011-01-19 06:33:43 +00001313QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001314 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1315 << T->getTypeClassName();
1316 return QualType();
1317}
1318
John McCall424cec92011-01-19 06:33:43 +00001319QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001320 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001321#define SHARED_SINGLETON_TYPE(Expansion)
1322#define BUILTIN_TYPE(Id, SingletonId) \
1323 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1324#include "clang/AST/BuiltinTypes.def"
1325
1326 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1327 // context supports C++.
1328
1329 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1330 // context supports ObjC.
1331
Douglas Gregor96e578d2010-02-05 17:54:41 +00001332 case BuiltinType::Char_U:
1333 // The context we're importing from has an unsigned 'char'. If we're
1334 // importing into a context with a signed 'char', translate to
1335 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001336 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001337 return Importer.getToContext().UnsignedCharTy;
1338
1339 return Importer.getToContext().CharTy;
1340
Douglas Gregor96e578d2010-02-05 17:54:41 +00001341 case BuiltinType::Char_S:
1342 // The context we're importing from has an unsigned 'char'. If we're
1343 // importing into a context with a signed 'char', translate to
1344 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001345 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346 return Importer.getToContext().SignedCharTy;
1347
1348 return Importer.getToContext().CharTy;
1349
Chris Lattnerad3467e2010-12-25 23:25:43 +00001350 case BuiltinType::WChar_S:
1351 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001352 // FIXME: If not in C++, shall we translate to the C equivalent of
1353 // wchar_t?
1354 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001355 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001356
1357 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001358}
1359
John McCall424cec92011-01-19 06:33:43 +00001360QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001361 QualType ToElementType = Importer.Import(T->getElementType());
1362 if (ToElementType.isNull())
1363 return QualType();
1364
1365 return Importer.getToContext().getComplexType(ToElementType);
1366}
1367
John McCall424cec92011-01-19 06:33:43 +00001368QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001369 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1370 if (ToPointeeType.isNull())
1371 return QualType();
1372
1373 return Importer.getToContext().getPointerType(ToPointeeType);
1374}
1375
John McCall424cec92011-01-19 06:33:43 +00001376QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001377 // FIXME: Check for blocks support in "to" context.
1378 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1379 if (ToPointeeType.isNull())
1380 return QualType();
1381
1382 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1383}
1384
John McCall424cec92011-01-19 06:33:43 +00001385QualType
1386ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001387 // FIXME: Check for C++ support in "to" context.
1388 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1389 if (ToPointeeType.isNull())
1390 return QualType();
1391
1392 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1393}
1394
John McCall424cec92011-01-19 06:33:43 +00001395QualType
1396ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001397 // FIXME: Check for C++0x support in "to" context.
1398 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1399 if (ToPointeeType.isNull())
1400 return QualType();
1401
1402 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1403}
1404
John McCall424cec92011-01-19 06:33:43 +00001405QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001406 // FIXME: Check for C++ support in "to" context.
1407 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1408 if (ToPointeeType.isNull())
1409 return QualType();
1410
1411 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1412 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1413 ClassType.getTypePtr());
1414}
1415
John McCall424cec92011-01-19 06:33:43 +00001416QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001417 QualType ToElementType = Importer.Import(T->getElementType());
1418 if (ToElementType.isNull())
1419 return QualType();
1420
1421 return Importer.getToContext().getConstantArrayType(ToElementType,
1422 T->getSize(),
1423 T->getSizeModifier(),
1424 T->getIndexTypeCVRQualifiers());
1425}
1426
John McCall424cec92011-01-19 06:33:43 +00001427QualType
1428ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001429 QualType ToElementType = Importer.Import(T->getElementType());
1430 if (ToElementType.isNull())
1431 return QualType();
1432
1433 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1434 T->getSizeModifier(),
1435 T->getIndexTypeCVRQualifiers());
1436}
1437
John McCall424cec92011-01-19 06:33:43 +00001438QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439 QualType ToElementType = Importer.Import(T->getElementType());
1440 if (ToElementType.isNull())
1441 return QualType();
1442
1443 Expr *Size = Importer.Import(T->getSizeExpr());
1444 if (!Size)
1445 return QualType();
1446
1447 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1448 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1449 T->getSizeModifier(),
1450 T->getIndexTypeCVRQualifiers(),
1451 Brackets);
1452}
1453
John McCall424cec92011-01-19 06:33:43 +00001454QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001455 QualType ToElementType = Importer.Import(T->getElementType());
1456 if (ToElementType.isNull())
1457 return QualType();
1458
1459 return Importer.getToContext().getVectorType(ToElementType,
1460 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001461 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001462}
1463
John McCall424cec92011-01-19 06:33:43 +00001464QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001465 QualType ToElementType = Importer.Import(T->getElementType());
1466 if (ToElementType.isNull())
1467 return QualType();
1468
1469 return Importer.getToContext().getExtVectorType(ToElementType,
1470 T->getNumElements());
1471}
1472
John McCall424cec92011-01-19 06:33:43 +00001473QualType
1474ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001475 // FIXME: What happens if we're importing a function without a prototype
1476 // into C++? Should we make it variadic?
1477 QualType ToResultType = Importer.Import(T->getResultType());
1478 if (ToResultType.isNull())
1479 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001480
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001482 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483}
1484
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001485QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486 QualType ToResultType = Importer.Import(T->getResultType());
1487 if (ToResultType.isNull())
1488 return QualType();
1489
1490 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001491 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001492 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1493 AEnd = T->arg_type_end();
1494 A != AEnd; ++A) {
1495 QualType ArgType = Importer.Import(*A);
1496 if (ArgType.isNull())
1497 return QualType();
1498 ArgTypes.push_back(ArgType);
1499 }
1500
1501 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001502 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1504 EEnd = T->exception_end();
1505 E != EEnd; ++E) {
1506 QualType ExceptionType = Importer.Import(*E);
1507 if (ExceptionType.isNull())
1508 return QualType();
1509 ExceptionTypes.push_back(ExceptionType);
1510 }
John McCalldb40c7f2010-12-14 08:05:40 +00001511
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001512 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1513 FunctionProtoType::ExtProtoInfo ToEPI;
1514
1515 ToEPI.ExtInfo = FromEPI.ExtInfo;
1516 ToEPI.Variadic = FromEPI.Variadic;
1517 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1518 ToEPI.TypeQuals = FromEPI.TypeQuals;
1519 ToEPI.RefQualifier = FromEPI.RefQualifier;
1520 ToEPI.NumExceptions = ExceptionTypes.size();
1521 ToEPI.Exceptions = ExceptionTypes.data();
1522 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001523 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1524 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1525 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001526 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001527 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001528 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001529
Douglas Gregor96e578d2010-02-05 17:54:41 +00001530 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001531 ArgTypes.size(), ToEPI);
1532}
1533
Sean Callananda6df8a2011-08-11 16:56:07 +00001534QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1535 QualType ToInnerType = Importer.Import(T->getInnerType());
1536 if (ToInnerType.isNull())
1537 return QualType();
1538
1539 return Importer.getToContext().getParenType(ToInnerType);
1540}
1541
John McCall424cec92011-01-19 06:33:43 +00001542QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001543 TypedefNameDecl *ToDecl
1544 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001545 if (!ToDecl)
1546 return QualType();
1547
1548 return Importer.getToContext().getTypeDeclType(ToDecl);
1549}
1550
John McCall424cec92011-01-19 06:33:43 +00001551QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001552 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1553 if (!ToExpr)
1554 return QualType();
1555
1556 return Importer.getToContext().getTypeOfExprType(ToExpr);
1557}
1558
John McCall424cec92011-01-19 06:33:43 +00001559QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1561 if (ToUnderlyingType.isNull())
1562 return QualType();
1563
1564 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1565}
1566
John McCall424cec92011-01-19 06:33:43 +00001567QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001568 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001569 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1570 if (!ToExpr)
1571 return QualType();
1572
Douglas Gregor81495f32012-02-12 18:42:33 +00001573 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1574 if (UnderlyingType.isNull())
1575 return QualType();
1576
1577 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001578}
1579
Alexis Hunte852b102011-05-24 22:41:36 +00001580QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1581 QualType ToBaseType = Importer.Import(T->getBaseType());
1582 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1583 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1584 return QualType();
1585
1586 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1587 ToUnderlyingType,
1588 T->getUTTKind());
1589}
1590
Richard Smith30482bc2011-02-20 03:19:35 +00001591QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1592 // FIXME: Make sure that the "to" context supports C++0x!
1593 QualType FromDeduced = T->getDeducedType();
1594 QualType ToDeduced;
1595 if (!FromDeduced.isNull()) {
1596 ToDeduced = Importer.Import(FromDeduced);
1597 if (ToDeduced.isNull())
1598 return QualType();
1599 }
1600
1601 return Importer.getToContext().getAutoType(ToDeduced);
1602}
1603
John McCall424cec92011-01-19 06:33:43 +00001604QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001605 RecordDecl *ToDecl
1606 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1607 if (!ToDecl)
1608 return QualType();
1609
1610 return Importer.getToContext().getTagDeclType(ToDecl);
1611}
1612
John McCall424cec92011-01-19 06:33:43 +00001613QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001614 EnumDecl *ToDecl
1615 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1616 if (!ToDecl)
1617 return QualType();
1618
1619 return Importer.getToContext().getTagDeclType(ToDecl);
1620}
1621
Douglas Gregore2e50d332010-12-01 01:36:18 +00001622QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001623 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001624 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1625 if (ToTemplate.isNull())
1626 return QualType();
1627
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001628 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001629 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1630 return QualType();
1631
1632 QualType ToCanonType;
1633 if (!QualType(T, 0).isCanonical()) {
1634 QualType FromCanonType
1635 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1636 ToCanonType =Importer.Import(FromCanonType);
1637 if (ToCanonType.isNull())
1638 return QualType();
1639 }
1640 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1641 ToTemplateArgs.data(),
1642 ToTemplateArgs.size(),
1643 ToCanonType);
1644}
1645
John McCall424cec92011-01-19 06:33:43 +00001646QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001647 NestedNameSpecifier *ToQualifier = 0;
1648 // Note: the qualifier in an ElaboratedType is optional.
1649 if (T->getQualifier()) {
1650 ToQualifier = Importer.Import(T->getQualifier());
1651 if (!ToQualifier)
1652 return QualType();
1653 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654
1655 QualType ToNamedType = Importer.Import(T->getNamedType());
1656 if (ToNamedType.isNull())
1657 return QualType();
1658
Abramo Bagnara6150c882010-05-11 21:36:43 +00001659 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1660 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001661}
1662
John McCall424cec92011-01-19 06:33:43 +00001663QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001664 ObjCInterfaceDecl *Class
1665 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1666 if (!Class)
1667 return QualType();
1668
John McCall8b07ec22010-05-15 11:32:37 +00001669 return Importer.getToContext().getObjCInterfaceType(Class);
1670}
1671
John McCall424cec92011-01-19 06:33:43 +00001672QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001673 QualType ToBaseType = Importer.Import(T->getBaseType());
1674 if (ToBaseType.isNull())
1675 return QualType();
1676
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001677 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001678 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001679 PEnd = T->qual_end();
1680 P != PEnd; ++P) {
1681 ObjCProtocolDecl *Protocol
1682 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1683 if (!Protocol)
1684 return QualType();
1685 Protocols.push_back(Protocol);
1686 }
1687
John McCall8b07ec22010-05-15 11:32:37 +00001688 return Importer.getToContext().getObjCObjectType(ToBaseType,
1689 Protocols.data(),
1690 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001691}
1692
John McCall424cec92011-01-19 06:33:43 +00001693QualType
1694ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001695 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1696 if (ToPointeeType.isNull())
1697 return QualType();
1698
John McCall8b07ec22010-05-15 11:32:37 +00001699 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001700}
1701
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001702//----------------------------------------------------------------------------
1703// Import Declarations
1704//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001705bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1706 DeclContext *&LexicalDC,
1707 DeclarationName &Name,
1708 SourceLocation &Loc) {
1709 // Import the context of this declaration.
1710 DC = Importer.ImportContext(D->getDeclContext());
1711 if (!DC)
1712 return true;
1713
1714 LexicalDC = DC;
1715 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1716 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1717 if (!LexicalDC)
1718 return true;
1719 }
1720
1721 // Import the name of this declaration.
1722 Name = Importer.Import(D->getDeclName());
1723 if (D->getDeclName() && !Name)
1724 return true;
1725
1726 // Import the location of this declaration.
1727 Loc = Importer.Import(D->getLocation());
1728 return false;
1729}
1730
Douglas Gregord451ea92011-07-29 23:31:30 +00001731void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1732 if (!FromD)
1733 return;
1734
1735 if (!ToD) {
1736 ToD = Importer.Import(FromD);
1737 if (!ToD)
1738 return;
1739 }
1740
1741 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1742 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1743 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1744 ImportDefinition(FromRecord, ToRecord);
1745 }
1746 }
1747 return;
1748 }
1749
1750 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1751 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1752 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1753 ImportDefinition(FromEnum, ToEnum);
1754 }
1755 }
1756 return;
1757 }
1758}
1759
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001760void
1761ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1762 DeclarationNameInfo& To) {
1763 // NOTE: To.Name and To.Loc are already imported.
1764 // We only have to import To.LocInfo.
1765 switch (To.getName().getNameKind()) {
1766 case DeclarationName::Identifier:
1767 case DeclarationName::ObjCZeroArgSelector:
1768 case DeclarationName::ObjCOneArgSelector:
1769 case DeclarationName::ObjCMultiArgSelector:
1770 case DeclarationName::CXXUsingDirective:
1771 return;
1772
1773 case DeclarationName::CXXOperatorName: {
1774 SourceRange Range = From.getCXXOperatorNameRange();
1775 To.setCXXOperatorNameRange(Importer.Import(Range));
1776 return;
1777 }
1778 case DeclarationName::CXXLiteralOperatorName: {
1779 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1780 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1781 return;
1782 }
1783 case DeclarationName::CXXConstructorName:
1784 case DeclarationName::CXXDestructorName:
1785 case DeclarationName::CXXConversionFunctionName: {
1786 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1787 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1788 return;
1789 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001790 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001791 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001792}
1793
Douglas Gregor2e15c842012-02-01 21:00:38 +00001794void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001795 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001796 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001797 return;
1798 }
1799
Douglas Gregor968d6332010-02-21 18:24:45 +00001800 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1801 FromEnd = FromDC->decls_end();
1802 From != FromEnd;
1803 ++From)
1804 Importer.Import(*From);
1805}
1806
Douglas Gregord451ea92011-07-29 23:31:30 +00001807bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001808 ImportDefinitionKind Kind) {
1809 if (To->getDefinition() || To->isBeingDefined()) {
1810 if (Kind == IDK_Everything)
1811 ImportDeclContext(From, /*ForceImport=*/true);
1812
Douglas Gregore2e50d332010-12-01 01:36:18 +00001813 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001814 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001815
1816 To->startDefinition();
1817
1818 // Add base classes.
1819 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1820 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001821
1822 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1823 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1824 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1825 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1826 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1827 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1828 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1829 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1830 ToData.Aggregate = FromData.Aggregate;
1831 ToData.PlainOldData = FromData.PlainOldData;
1832 ToData.Empty = FromData.Empty;
1833 ToData.Polymorphic = FromData.Polymorphic;
1834 ToData.Abstract = FromData.Abstract;
1835 ToData.IsStandardLayout = FromData.IsStandardLayout;
1836 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1837 ToData.HasPrivateFields = FromData.HasPrivateFields;
1838 ToData.HasProtectedFields = FromData.HasProtectedFields;
1839 ToData.HasPublicFields = FromData.HasPublicFields;
1840 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001841 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001842 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001843 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1844 ToData.HasConstexprNonCopyMoveConstructor
1845 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001846 ToData.DefaultedDefaultConstructorIsConstexpr
1847 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001848 ToData.HasConstexprDefaultConstructor
1849 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001850 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1851 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1852 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1853 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1854 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001855 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001856 ToData.HasNonLiteralTypeFieldsOrBases
1857 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001858 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001859 ToData.UserProvidedDefaultConstructor
1860 = FromData.UserProvidedDefaultConstructor;
1861 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1862 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1863 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1864 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1865 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1866 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1867 ToData.FailedImplicitMoveConstructor
1868 = FromData.FailedImplicitMoveConstructor;
1869 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001870 ToData.IsLambda = FromData.IsLambda;
1871
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001872 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001873 for (CXXRecordDecl::base_class_iterator
1874 Base1 = FromCXX->bases_begin(),
1875 FromBaseEnd = FromCXX->bases_end();
1876 Base1 != FromBaseEnd;
1877 ++Base1) {
1878 QualType T = Importer.Import(Base1->getType());
1879 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001880 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001881
1882 SourceLocation EllipsisLoc;
1883 if (Base1->isPackExpansion())
1884 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001885
1886 // Ensure that we have a definition for the base.
1887 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1888
Douglas Gregore2e50d332010-12-01 01:36:18 +00001889 Bases.push_back(
1890 new (Importer.getToContext())
1891 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1892 Base1->isVirtual(),
1893 Base1->isBaseOfClass(),
1894 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001895 Importer.Import(Base1->getTypeSourceInfo()),
1896 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001897 }
1898 if (!Bases.empty())
1899 ToCXX->setBases(Bases.data(), Bases.size());
1900 }
1901
Douglas Gregor2e15c842012-02-01 21:00:38 +00001902 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001903 ImportDeclContext(From, /*ForceImport=*/true);
1904
Douglas Gregore2e50d332010-12-01 01:36:18 +00001905 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001906 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001907}
1908
Douglas Gregord451ea92011-07-29 23:31:30 +00001909bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001910 ImportDefinitionKind Kind) {
1911 if (To->getDefinition() || To->isBeingDefined()) {
1912 if (Kind == IDK_Everything)
1913 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001914 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001915 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001916
1917 To->startDefinition();
1918
1919 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1920 if (T.isNull())
1921 return true;
1922
1923 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1924 if (ToPromotionType.isNull())
1925 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001926
1927 if (shouldForceImportDeclContext(Kind))
1928 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001929
1930 // FIXME: we might need to merge the number of positive or negative bits
1931 // if the enumerator lists don't match.
1932 To->completeDefinition(T, ToPromotionType,
1933 From->getNumPositiveBits(),
1934 From->getNumNegativeBits());
1935 return false;
1936}
1937
Douglas Gregora082a492010-11-30 19:14:50 +00001938TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1939 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001940 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001941 ToParams.reserve(Params->size());
1942 for (TemplateParameterList::iterator P = Params->begin(),
1943 PEnd = Params->end();
1944 P != PEnd; ++P) {
1945 Decl *To = Importer.Import(*P);
1946 if (!To)
1947 return 0;
1948
1949 ToParams.push_back(cast<NamedDecl>(To));
1950 }
1951
1952 return TemplateParameterList::Create(Importer.getToContext(),
1953 Importer.Import(Params->getTemplateLoc()),
1954 Importer.Import(Params->getLAngleLoc()),
1955 ToParams.data(), ToParams.size(),
1956 Importer.Import(Params->getRAngleLoc()));
1957}
1958
Douglas Gregore2e50d332010-12-01 01:36:18 +00001959TemplateArgument
1960ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1961 switch (From.getKind()) {
1962 case TemplateArgument::Null:
1963 return TemplateArgument();
1964
1965 case TemplateArgument::Type: {
1966 QualType ToType = Importer.Import(From.getAsType());
1967 if (ToType.isNull())
1968 return TemplateArgument();
1969 return TemplateArgument(ToType);
1970 }
1971
1972 case TemplateArgument::Integral: {
1973 QualType ToType = Importer.Import(From.getIntegralType());
1974 if (ToType.isNull())
1975 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001976 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001977 }
1978
1979 case TemplateArgument::Declaration:
1980 if (Decl *To = Importer.Import(From.getAsDecl()))
1981 return TemplateArgument(To);
1982 return TemplateArgument();
1983
1984 case TemplateArgument::Template: {
1985 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1986 if (ToTemplate.isNull())
1987 return TemplateArgument();
1988
1989 return TemplateArgument(ToTemplate);
1990 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001991
1992 case TemplateArgument::TemplateExpansion: {
1993 TemplateName ToTemplate
1994 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1995 if (ToTemplate.isNull())
1996 return TemplateArgument();
1997
Douglas Gregore1d60df2011-01-14 23:41:42 +00001998 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001999 }
2000
Douglas Gregore2e50d332010-12-01 01:36:18 +00002001 case TemplateArgument::Expression:
2002 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2003 return TemplateArgument(ToExpr);
2004 return TemplateArgument();
2005
2006 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002007 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002008 ToPack.reserve(From.pack_size());
2009 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2010 return TemplateArgument();
2011
2012 TemplateArgument *ToArgs
2013 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2014 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2015 return TemplateArgument(ToArgs, ToPack.size());
2016 }
2017 }
2018
2019 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002020}
2021
2022bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2023 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002024 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002025 for (unsigned I = 0; I != NumFromArgs; ++I) {
2026 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2027 if (To.isNull() && !FromArgs[I].isNull())
2028 return true;
2029
2030 ToArgs.push_back(To);
2031 }
2032
2033 return false;
2034}
2035
Douglas Gregor5c73e912010-02-11 00:48:18 +00002036bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002037 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002038 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002039 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002040 Importer.getNonEquivalentDecls(),
2041 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002042 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002043}
2044
Douglas Gregor98c10182010-02-12 22:17:39 +00002045bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002046 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002047 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002048 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002049 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002050}
2051
Douglas Gregora082a492010-11-30 19:14:50 +00002052bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2053 ClassTemplateDecl *To) {
2054 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2055 Importer.getToContext(),
2056 Importer.getNonEquivalentDecls());
2057 return Ctx.IsStructurallyEquivalent(From, To);
2058}
2059
Douglas Gregore4c83e42010-02-09 22:48:33 +00002060Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002061 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002062 << D->getDeclKindName();
2063 return 0;
2064}
2065
Sean Callanan65198272011-11-17 23:20:56 +00002066Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2067 TranslationUnitDecl *ToD =
2068 Importer.getToContext().getTranslationUnitDecl();
2069
2070 Importer.Imported(D, ToD);
2071
2072 return ToD;
2073}
2074
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002075Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2076 // Import the major distinguishing characteristics of this namespace.
2077 DeclContext *DC, *LexicalDC;
2078 DeclarationName Name;
2079 SourceLocation Loc;
2080 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2081 return 0;
2082
2083 NamespaceDecl *MergeWithNamespace = 0;
2084 if (!Name) {
2085 // This is an anonymous namespace. Adopt an existing anonymous
2086 // namespace if we can.
2087 // FIXME: Not testable.
2088 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2089 MergeWithNamespace = TU->getAnonymousNamespace();
2090 else
2091 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2092 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002093 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002094 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2095 DC->localUncachedLookup(Name, FoundDecls);
2096 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2097 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002098 continue;
2099
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002100 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002101 MergeWithNamespace = FoundNS;
2102 ConflictingDecls.clear();
2103 break;
2104 }
2105
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002106 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002107 }
2108
2109 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002110 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002111 ConflictingDecls.data(),
2112 ConflictingDecls.size());
2113 }
2114 }
2115
2116 // Create the "to" namespace, if needed.
2117 NamespaceDecl *ToNamespace = MergeWithNamespace;
2118 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002119 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002120 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002121 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002122 Loc, Name.getAsIdentifierInfo(),
2123 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002124 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002125 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002126
2127 // If this is an anonymous namespace, register it as the anonymous
2128 // namespace within its context.
2129 if (!Name) {
2130 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2131 TU->setAnonymousNamespace(ToNamespace);
2132 else
2133 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2134 }
2135 }
2136 Importer.Imported(D, ToNamespace);
2137
2138 ImportDeclContext(D);
2139
2140 return ToNamespace;
2141}
2142
Richard Smithdda56e42011-04-15 14:24:37 +00002143Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002144 // Import the major distinguishing characteristics of this typedef.
2145 DeclContext *DC, *LexicalDC;
2146 DeclarationName Name;
2147 SourceLocation Loc;
2148 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2149 return 0;
2150
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002151 // If this typedef is not in block scope, determine whether we've
2152 // seen a typedef with the same name (that we can merge with) or any
2153 // other entity by that name (which name lookup could conflict with).
2154 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002155 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002156 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002157 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2158 DC->localUncachedLookup(Name, FoundDecls);
2159 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2160 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002161 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002162 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002163 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002164 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2165 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002166 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002167 }
2168
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002169 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002170 }
2171
2172 if (!ConflictingDecls.empty()) {
2173 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2174 ConflictingDecls.data(),
2175 ConflictingDecls.size());
2176 if (!Name)
2177 return 0;
2178 }
2179 }
2180
Douglas Gregorb4964f72010-02-15 23:54:17 +00002181 // Import the underlying type of this typedef;
2182 QualType T = Importer.Import(D->getUnderlyingType());
2183 if (T.isNull())
2184 return 0;
2185
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002186 // Create the new typedef node.
2187 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002188 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002189 TypedefNameDecl *ToTypedef;
2190 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002191 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2192 StartL, Loc,
2193 Name.getAsIdentifierInfo(),
2194 TInfo);
2195 else
Richard Smithdda56e42011-04-15 14:24:37 +00002196 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2197 StartL, Loc,
2198 Name.getAsIdentifierInfo(),
2199 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002200
Douglas Gregordd483172010-02-22 17:42:47 +00002201 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002202 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002203 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002204 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002205
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002206 return ToTypedef;
2207}
2208
Richard Smithdda56e42011-04-15 14:24:37 +00002209Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2210 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2211}
2212
2213Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2214 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2215}
2216
Douglas Gregor98c10182010-02-12 22:17:39 +00002217Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2218 // Import the major distinguishing characteristics of this enum.
2219 DeclContext *DC, *LexicalDC;
2220 DeclarationName Name;
2221 SourceLocation Loc;
2222 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2223 return 0;
2224
2225 // Figure out what enum name we're looking for.
2226 unsigned IDNS = Decl::IDNS_Tag;
2227 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002228 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2229 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002230 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002231 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002232 IDNS |= Decl::IDNS_Ordinary;
2233
2234 // We may already have an enum of the same name; try to find and match it.
2235 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002236 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002237 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2238 DC->localUncachedLookup(SearchName, FoundDecls);
2239 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2240 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002241 continue;
2242
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002243 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002244 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002245 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2246 Found = Tag->getDecl();
2247 }
2248
2249 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002250 if (IsStructuralMatch(D, FoundEnum))
2251 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002252 }
2253
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002254 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002255 }
2256
2257 if (!ConflictingDecls.empty()) {
2258 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2259 ConflictingDecls.data(),
2260 ConflictingDecls.size());
2261 }
2262 }
2263
2264 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002265 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2266 Importer.Import(D->getLocStart()),
2267 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002268 D->isScoped(), D->isScopedUsingClassTag(),
2269 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002270 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002271 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002272 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002273 D2->setLexicalDeclContext(LexicalDC);
2274 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002275 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002276
2277 // Import the integer type.
2278 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2279 if (ToIntegerType.isNull())
2280 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002281 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002282
2283 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002284 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002285 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002286
Douglas Gregor3996e242010-02-15 22:01:00 +00002287 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002288}
2289
Douglas Gregor5c73e912010-02-11 00:48:18 +00002290Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2291 // If this record has a definition in the translation unit we're coming from,
2292 // but this particular declaration is not that definition, import the
2293 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002294 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002295 if (Definition && Definition != D) {
2296 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002297 if (!ImportedDef)
2298 return 0;
2299
2300 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002301 }
2302
2303 // Import the major distinguishing characteristics of this record.
2304 DeclContext *DC, *LexicalDC;
2305 DeclarationName Name;
2306 SourceLocation Loc;
2307 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2308 return 0;
2309
2310 // Figure out what structure name we're looking for.
2311 unsigned IDNS = Decl::IDNS_Tag;
2312 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002313 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2314 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002315 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002316 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002317 IDNS |= Decl::IDNS_Ordinary;
2318
2319 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002320 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002321 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002322 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002323 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2324 DC->localUncachedLookup(SearchName, FoundDecls);
2325 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2326 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002327 continue;
2328
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002329 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002330 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002331 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2332 Found = Tag->getDecl();
2333 }
2334
2335 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002336 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002337 if ((SearchName && !D->isCompleteDefinition())
2338 || (D->isCompleteDefinition() &&
2339 D->isAnonymousStructOrUnion()
2340 == FoundDef->isAnonymousStructOrUnion() &&
2341 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002342 // The record types structurally match, or the "from" translation
2343 // unit only had a forward declaration anyway; call it the same
2344 // function.
2345 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002346 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002347 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002348 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002349 // We have a forward declaration of this type, so adopt that forward
2350 // declaration rather than building a new one.
2351 AdoptDecl = FoundRecord;
2352 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002353 } else if (!SearchName) {
2354 continue;
2355 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002356 }
2357
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002358 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002359 }
2360
Douglas Gregordd6006f2012-07-17 21:16:27 +00002361 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002362 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2363 ConflictingDecls.data(),
2364 ConflictingDecls.size());
2365 }
2366 }
2367
2368 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002369 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002370 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002371 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002372 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002373 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002374 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002375 DC, StartLoc, Loc,
2376 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002377 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002378 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002379 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002380 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002381 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002382 }
Douglas Gregor14454802011-02-25 02:25:35 +00002383
2384 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002385 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002386 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002387 if (D->isAnonymousStructOrUnion())
2388 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002389 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002390
Douglas Gregor3996e242010-02-15 22:01:00 +00002391 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002392
Douglas Gregor95d82832012-01-24 18:36:04 +00002393 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002394 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002395
Douglas Gregor3996e242010-02-15 22:01:00 +00002396 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002397}
2398
Douglas Gregor98c10182010-02-12 22:17:39 +00002399Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2400 // Import the major distinguishing characteristics of this enumerator.
2401 DeclContext *DC, *LexicalDC;
2402 DeclarationName Name;
2403 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002404 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002405 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002406
2407 QualType T = Importer.Import(D->getType());
2408 if (T.isNull())
2409 return 0;
2410
Douglas Gregor98c10182010-02-12 22:17:39 +00002411 // Determine whether there are any other declarations with the same name and
2412 // in the same context.
2413 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002414 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002415 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002416 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2417 DC->localUncachedLookup(Name, FoundDecls);
2418 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2419 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002420 continue;
2421
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002422 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002423 }
2424
2425 if (!ConflictingDecls.empty()) {
2426 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2427 ConflictingDecls.data(),
2428 ConflictingDecls.size());
2429 if (!Name)
2430 return 0;
2431 }
2432 }
2433
2434 Expr *Init = Importer.Import(D->getInitExpr());
2435 if (D->getInitExpr() && !Init)
2436 return 0;
2437
2438 EnumConstantDecl *ToEnumerator
2439 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2440 Name.getAsIdentifierInfo(), T,
2441 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002442 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002443 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002444 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002445 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002446 return ToEnumerator;
2447}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002448
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002449Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2450 // Import the major distinguishing characteristics of this function.
2451 DeclContext *DC, *LexicalDC;
2452 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002453 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002454 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002455 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002456
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002457 // Try to find a function in our own ("to") context with the same name, same
2458 // type, and in the same context as the function we're importing.
2459 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002460 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002461 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002462 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2463 DC->localUncachedLookup(Name, FoundDecls);
2464 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2465 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002466 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002467
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002468 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002469 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2470 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002471 if (Importer.IsStructurallyEquivalent(D->getType(),
2472 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002473 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002474 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002475 }
2476
2477 // FIXME: Check for overloading more carefully, e.g., by boosting
2478 // Sema::IsOverload out to the AST library.
2479
2480 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002481 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002482 continue;
2483
2484 // Complain about inconsistent function types.
2485 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002486 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002487 Importer.ToDiag(FoundFunction->getLocation(),
2488 diag::note_odr_value_here)
2489 << FoundFunction->getType();
2490 }
2491 }
2492
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002493 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002494 }
2495
2496 if (!ConflictingDecls.empty()) {
2497 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2498 ConflictingDecls.data(),
2499 ConflictingDecls.size());
2500 if (!Name)
2501 return 0;
2502 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002503 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002504
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002505 DeclarationNameInfo NameInfo(Name, Loc);
2506 // Import additional name location/type info.
2507 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2508
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002509 QualType FromTy = D->getType();
2510 bool usedDifferentExceptionSpec = false;
2511
2512 if (const FunctionProtoType *
2513 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2514 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2515 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2516 // FunctionDecl that we are importing the FunctionProtoType for.
2517 // To avoid an infinite recursion when importing, create the FunctionDecl
2518 // with a simplified function type and update it afterwards.
2519 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2520 FromEPI.NoexceptExpr) {
2521 FunctionProtoType::ExtProtoInfo DefaultEPI;
2522 FromTy = Importer.getFromContext().getFunctionType(
2523 FromFPT->getResultType(),
2524 FromFPT->arg_type_begin(),
2525 FromFPT->arg_type_end() - FromFPT->arg_type_begin(),
2526 DefaultEPI);
2527 usedDifferentExceptionSpec = true;
2528 }
2529 }
2530
Douglas Gregorb4964f72010-02-15 23:54:17 +00002531 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002532 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002533 if (T.isNull())
2534 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002535
2536 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002537 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002538 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2539 P != PEnd; ++P) {
2540 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2541 if (!ToP)
2542 return 0;
2543
2544 Parameters.push_back(ToP);
2545 }
2546
2547 // Create the imported function.
2548 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002549 FunctionDecl *ToFunction = 0;
2550 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2551 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2552 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002553 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002554 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002555 FromConstructor->isExplicit(),
2556 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002557 D->isImplicit(),
2558 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002559 } else if (isa<CXXDestructorDecl>(D)) {
2560 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2561 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002562 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002563 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002564 D->isInlineSpecified(),
2565 D->isImplicit());
2566 } else if (CXXConversionDecl *FromConversion
2567 = dyn_cast<CXXConversionDecl>(D)) {
2568 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2569 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002570 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002571 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002572 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002573 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002574 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002575 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002576 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2577 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2578 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002579 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002580 NameInfo, T, TInfo,
2581 Method->isStatic(),
2582 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002583 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002584 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002585 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002586 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002587 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002588 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002589 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002590 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002591 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002592 D->hasWrittenPrototype(),
2593 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002594 }
John McCall3e11ebe2010-03-15 10:12:16 +00002595
2596 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002597 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002598 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002599 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002600 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2601 ToFunction->setTrivial(D->isTrivial());
2602 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002603 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002604
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002605 // Set the parameters.
2606 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002607 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002608 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002609 }
David Blaikie9c70e042011-09-21 18:16:56 +00002610 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002611
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002612 if (usedDifferentExceptionSpec) {
2613 // Update FunctionProtoType::ExtProtoInfo.
2614 QualType T = Importer.Import(D->getType());
2615 if (T.isNull())
2616 return 0;
2617 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002618 }
2619
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002620 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002621
2622 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002623 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002624
Douglas Gregor43f54792010-02-17 02:12:47 +00002625 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002626}
2627
Douglas Gregor00eace12010-02-21 18:29:16 +00002628Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2629 return VisitFunctionDecl(D);
2630}
2631
2632Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2633 return VisitCXXMethodDecl(D);
2634}
2635
2636Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2637 return VisitCXXMethodDecl(D);
2638}
2639
2640Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2641 return VisitCXXMethodDecl(D);
2642}
2643
Douglas Gregor5c73e912010-02-11 00:48:18 +00002644Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2645 // Import the major distinguishing characteristics of a variable.
2646 DeclContext *DC, *LexicalDC;
2647 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002648 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002649 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2650 return 0;
2651
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002652 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002653 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2654 DC->localUncachedLookup(Name, FoundDecls);
2655 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2656 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002657 if (Importer.IsStructurallyEquivalent(D->getType(),
2658 FoundField->getType())) {
2659 Importer.Imported(D, FoundField);
2660 return FoundField;
2661 }
2662
2663 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2664 << Name << D->getType() << FoundField->getType();
2665 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2666 << FoundField->getType();
2667 return 0;
2668 }
2669 }
2670
Douglas Gregorb4964f72010-02-15 23:54:17 +00002671 // Import the type.
2672 QualType T = Importer.Import(D->getType());
2673 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002674 return 0;
2675
2676 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2677 Expr *BitWidth = Importer.Import(D->getBitWidth());
2678 if (!BitWidth && D->getBitWidth())
2679 return 0;
2680
Abramo Bagnaradff19302011-03-08 08:55:46 +00002681 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2682 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002683 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002684 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002685 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002686 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002687 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002688 if (ToField->hasInClassInitializer())
2689 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002690 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002691 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002692 return ToField;
2693}
2694
Francois Pichet783dd6e2010-11-21 06:08:52 +00002695Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2696 // Import the major distinguishing characteristics of a variable.
2697 DeclContext *DC, *LexicalDC;
2698 DeclarationName Name;
2699 SourceLocation Loc;
2700 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2701 return 0;
2702
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002703 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002704 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2705 DC->localUncachedLookup(Name, FoundDecls);
2706 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002707 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002708 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002709 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002710 FoundField->getType(),
2711 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002712 Importer.Imported(D, FoundField);
2713 return FoundField;
2714 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002715
2716 // If there are more anonymous fields to check, continue.
2717 if (!Name && I < N-1)
2718 continue;
2719
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002720 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2721 << Name << D->getType() << FoundField->getType();
2722 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2723 << FoundField->getType();
2724 return 0;
2725 }
2726 }
2727
Francois Pichet783dd6e2010-11-21 06:08:52 +00002728 // Import the type.
2729 QualType T = Importer.Import(D->getType());
2730 if (T.isNull())
2731 return 0;
2732
2733 NamedDecl **NamedChain =
2734 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2735
2736 unsigned i = 0;
2737 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2738 PE = D->chain_end(); PI != PE; ++PI) {
2739 Decl* D = Importer.Import(*PI);
2740 if (!D)
2741 return 0;
2742 NamedChain[i++] = cast<NamedDecl>(D);
2743 }
2744
2745 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2746 Importer.getToContext(), DC,
2747 Loc, Name.getAsIdentifierInfo(), T,
2748 NamedChain, D->getChainingSize());
2749 ToIndirectField->setAccess(D->getAccess());
2750 ToIndirectField->setLexicalDeclContext(LexicalDC);
2751 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002752 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002753 return ToIndirectField;
2754}
2755
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002756Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2757 // Import the major distinguishing characteristics of an ivar.
2758 DeclContext *DC, *LexicalDC;
2759 DeclarationName Name;
2760 SourceLocation Loc;
2761 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2762 return 0;
2763
2764 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002765 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2766 DC->localUncachedLookup(Name, FoundDecls);
2767 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2768 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002769 if (Importer.IsStructurallyEquivalent(D->getType(),
2770 FoundIvar->getType())) {
2771 Importer.Imported(D, FoundIvar);
2772 return FoundIvar;
2773 }
2774
2775 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2776 << Name << D->getType() << FoundIvar->getType();
2777 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2778 << FoundIvar->getType();
2779 return 0;
2780 }
2781 }
2782
2783 // Import the type.
2784 QualType T = Importer.Import(D->getType());
2785 if (T.isNull())
2786 return 0;
2787
2788 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2789 Expr *BitWidth = Importer.Import(D->getBitWidth());
2790 if (!BitWidth && D->getBitWidth())
2791 return 0;
2792
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002793 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2794 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002795 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002796 Loc, Name.getAsIdentifierInfo(),
2797 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002798 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002799 ToIvar->setLexicalDeclContext(LexicalDC);
2800 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002801 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002802 return ToIvar;
2803
2804}
2805
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002806Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2807 // Import the major distinguishing characteristics of a variable.
2808 DeclContext *DC, *LexicalDC;
2809 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002810 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002811 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002812 return 0;
2813
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002814 // Try to find a variable in our own ("to") context with the same name and
2815 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002816 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002817 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002818 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002819 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002820 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2821 DC->localUncachedLookup(Name, FoundDecls);
2822 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2823 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002824 continue;
2825
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002826 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002827 // We have found a variable that we may need to merge with. Check it.
2828 if (isExternalLinkage(FoundVar->getLinkage()) &&
2829 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002830 if (Importer.IsStructurallyEquivalent(D->getType(),
2831 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002832 MergeWithVar = FoundVar;
2833 break;
2834 }
2835
Douglas Gregor56521c52010-02-12 17:23:39 +00002836 const ArrayType *FoundArray
2837 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2838 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002839 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002840 if (FoundArray && TArray) {
2841 if (isa<IncompleteArrayType>(FoundArray) &&
2842 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002843 // Import the type.
2844 QualType T = Importer.Import(D->getType());
2845 if (T.isNull())
2846 return 0;
2847
Douglas Gregor56521c52010-02-12 17:23:39 +00002848 FoundVar->setType(T);
2849 MergeWithVar = FoundVar;
2850 break;
2851 } else if (isa<IncompleteArrayType>(TArray) &&
2852 isa<ConstantArrayType>(FoundArray)) {
2853 MergeWithVar = FoundVar;
2854 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002855 }
2856 }
2857
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002858 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002859 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002860 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2861 << FoundVar->getType();
2862 }
2863 }
2864
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002865 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002866 }
2867
2868 if (MergeWithVar) {
2869 // An equivalent variable with external linkage has been found. Link
2870 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002871 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002872
2873 if (VarDecl *DDef = D->getDefinition()) {
2874 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2875 Importer.ToDiag(ExistingDef->getLocation(),
2876 diag::err_odr_variable_multiple_def)
2877 << Name;
2878 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2879 } else {
2880 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002881 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002882 if (DDef->isInitKnownICE()) {
2883 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2884 Eval->CheckedICE = true;
2885 Eval->IsICE = DDef->isInitICE();
2886 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002887 }
2888 }
2889
2890 return MergeWithVar;
2891 }
2892
2893 if (!ConflictingDecls.empty()) {
2894 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2895 ConflictingDecls.data(),
2896 ConflictingDecls.size());
2897 if (!Name)
2898 return 0;
2899 }
2900 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002901
Douglas Gregorb4964f72010-02-15 23:54:17 +00002902 // Import the type.
2903 QualType T = Importer.Import(D->getType());
2904 if (T.isNull())
2905 return 0;
2906
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002907 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002908 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002909 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2910 Importer.Import(D->getInnerLocStart()),
2911 Loc, Name.getAsIdentifierInfo(),
2912 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002913 D->getStorageClass(),
2914 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002915 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002916 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002917 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002918 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002919 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002920
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002921 // Merge the initializer.
2922 // FIXME: Can we really import any initializer? Alternatively, we could force
2923 // ourselves to import every declaration of a variable and then only use
2924 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002925 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002926
2927 // FIXME: Other bits to merge?
2928
2929 return ToVar;
2930}
2931
Douglas Gregor8b228d72010-02-17 21:22:52 +00002932Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2933 // Parameters are created in the translation unit's context, then moved
2934 // into the function declaration's context afterward.
2935 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2936
2937 // Import the name of this declaration.
2938 DeclarationName Name = Importer.Import(D->getDeclName());
2939 if (D->getDeclName() && !Name)
2940 return 0;
2941
2942 // Import the location of this declaration.
2943 SourceLocation Loc = Importer.Import(D->getLocation());
2944
2945 // Import the parameter's type.
2946 QualType T = Importer.Import(D->getType());
2947 if (T.isNull())
2948 return 0;
2949
2950 // Create the imported parameter.
2951 ImplicitParamDecl *ToParm
2952 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2953 Loc, Name.getAsIdentifierInfo(),
2954 T);
2955 return Importer.Imported(D, ToParm);
2956}
2957
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002958Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2959 // Parameters are created in the translation unit's context, then moved
2960 // into the function declaration's context afterward.
2961 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2962
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002963 // Import the name of this declaration.
2964 DeclarationName Name = Importer.Import(D->getDeclName());
2965 if (D->getDeclName() && !Name)
2966 return 0;
2967
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002968 // Import the location of this declaration.
2969 SourceLocation Loc = Importer.Import(D->getLocation());
2970
2971 // Import the parameter's type.
2972 QualType T = Importer.Import(D->getType());
2973 if (T.isNull())
2974 return 0;
2975
2976 // Create the imported parameter.
2977 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2978 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002979 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002980 Loc, Name.getAsIdentifierInfo(),
2981 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002982 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002983 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002984 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002985 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002986}
2987
Douglas Gregor43f54792010-02-17 02:12:47 +00002988Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2989 // Import the major distinguishing characteristics of a method.
2990 DeclContext *DC, *LexicalDC;
2991 DeclarationName Name;
2992 SourceLocation Loc;
2993 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2994 return 0;
2995
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002996 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2997 DC->localUncachedLookup(Name, FoundDecls);
2998 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2999 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003000 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3001 continue;
3002
3003 // Check return types.
3004 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3005 FoundMethod->getResultType())) {
3006 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3007 << D->isInstanceMethod() << Name
3008 << D->getResultType() << FoundMethod->getResultType();
3009 Importer.ToDiag(FoundMethod->getLocation(),
3010 diag::note_odr_objc_method_here)
3011 << D->isInstanceMethod() << Name;
3012 return 0;
3013 }
3014
3015 // Check the number of parameters.
3016 if (D->param_size() != FoundMethod->param_size()) {
3017 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3018 << D->isInstanceMethod() << Name
3019 << D->param_size() << FoundMethod->param_size();
3020 Importer.ToDiag(FoundMethod->getLocation(),
3021 diag::note_odr_objc_method_here)
3022 << D->isInstanceMethod() << Name;
3023 return 0;
3024 }
3025
3026 // Check parameter types.
3027 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3028 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3029 P != PEnd; ++P, ++FoundP) {
3030 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3031 (*FoundP)->getType())) {
3032 Importer.FromDiag((*P)->getLocation(),
3033 diag::err_odr_objc_method_param_type_inconsistent)
3034 << D->isInstanceMethod() << Name
3035 << (*P)->getType() << (*FoundP)->getType();
3036 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3037 << (*FoundP)->getType();
3038 return 0;
3039 }
3040 }
3041
3042 // Check variadic/non-variadic.
3043 // Check the number of parameters.
3044 if (D->isVariadic() != FoundMethod->isVariadic()) {
3045 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3046 << D->isInstanceMethod() << Name;
3047 Importer.ToDiag(FoundMethod->getLocation(),
3048 diag::note_odr_objc_method_here)
3049 << D->isInstanceMethod() << Name;
3050 return 0;
3051 }
3052
3053 // FIXME: Any other bits we need to merge?
3054 return Importer.Imported(D, FoundMethod);
3055 }
3056 }
3057
3058 // Import the result type.
3059 QualType ResultTy = Importer.Import(D->getResultType());
3060 if (ResultTy.isNull())
3061 return 0;
3062
Douglas Gregor12852d92010-03-08 14:59:44 +00003063 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3064
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 ObjCMethodDecl *ToMethod
3066 = ObjCMethodDecl::Create(Importer.getToContext(),
3067 Loc,
3068 Importer.Import(D->getLocEnd()),
3069 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003070 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003071 D->isInstanceMethod(),
3072 D->isVariadic(),
3073 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003074 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003075 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003076 D->getImplementationControl(),
3077 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003078
3079 // FIXME: When we decide to merge method definitions, we'll need to
3080 // deal with implicit parameters.
3081
3082 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003083 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003084 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3085 FromPEnd = D->param_end();
3086 FromP != FromPEnd;
3087 ++FromP) {
3088 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3089 if (!ToP)
3090 return 0;
3091
3092 ToParams.push_back(ToP);
3093 }
3094
3095 // Set the parameters.
3096 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3097 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003098 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003099 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003100 SmallVector<SourceLocation, 12> SelLocs;
3101 D->getSelectorLocs(SelLocs);
3102 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003103
3104 ToMethod->setLexicalDeclContext(LexicalDC);
3105 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003106 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003107 return ToMethod;
3108}
3109
Douglas Gregor84c51c32010-02-18 01:47:50 +00003110Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3111 // Import the major distinguishing characteristics of a category.
3112 DeclContext *DC, *LexicalDC;
3113 DeclarationName Name;
3114 SourceLocation Loc;
3115 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3116 return 0;
3117
3118 ObjCInterfaceDecl *ToInterface
3119 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3120 if (!ToInterface)
3121 return 0;
3122
3123 // Determine if we've already encountered this category.
3124 ObjCCategoryDecl *MergeWithCategory
3125 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3126 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3127 if (!ToCategory) {
3128 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003129 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003130 Loc,
3131 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003132 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003133 ToInterface,
3134 Importer.Import(D->getIvarLBraceLoc()),
3135 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003136 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003137 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003138 Importer.Imported(D, ToCategory);
3139
Douglas Gregor84c51c32010-02-18 01:47:50 +00003140 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003141 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3142 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003143 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3144 = D->protocol_loc_begin();
3145 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3146 FromProtoEnd = D->protocol_end();
3147 FromProto != FromProtoEnd;
3148 ++FromProto, ++FromProtoLoc) {
3149 ObjCProtocolDecl *ToProto
3150 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3151 if (!ToProto)
3152 return 0;
3153 Protocols.push_back(ToProto);
3154 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3155 }
3156
3157 // FIXME: If we're merging, make sure that the protocol list is the same.
3158 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3159 ProtocolLocs.data(), Importer.getToContext());
3160
3161 } else {
3162 Importer.Imported(D, ToCategory);
3163 }
3164
3165 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003166 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003167
3168 // If we have an implementation, import it as well.
3169 if (D->getImplementation()) {
3170 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003171 = cast_or_null<ObjCCategoryImplDecl>(
3172 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003173 if (!Impl)
3174 return 0;
3175
3176 ToCategory->setImplementation(Impl);
3177 }
3178
3179 return ToCategory;
3180}
3181
Douglas Gregor2aa53772012-01-24 17:42:07 +00003182bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3183 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003184 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003185 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003186 if (shouldForceImportDeclContext(Kind))
3187 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003188 return false;
3189 }
3190
3191 // Start the protocol definition
3192 To->startDefinition();
3193
3194 // Import protocols
3195 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3196 SmallVector<SourceLocation, 4> ProtocolLocs;
3197 ObjCProtocolDecl::protocol_loc_iterator
3198 FromProtoLoc = From->protocol_loc_begin();
3199 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3200 FromProtoEnd = From->protocol_end();
3201 FromProto != FromProtoEnd;
3202 ++FromProto, ++FromProtoLoc) {
3203 ObjCProtocolDecl *ToProto
3204 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3205 if (!ToProto)
3206 return true;
3207 Protocols.push_back(ToProto);
3208 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3209 }
3210
3211 // FIXME: If we're merging, make sure that the protocol list is the same.
3212 To->setProtocolList(Protocols.data(), Protocols.size(),
3213 ProtocolLocs.data(), Importer.getToContext());
3214
Douglas Gregor2e15c842012-02-01 21:00:38 +00003215 if (shouldForceImportDeclContext(Kind)) {
3216 // Import all of the members of this protocol.
3217 ImportDeclContext(From, /*ForceImport=*/true);
3218 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003219 return false;
3220}
3221
Douglas Gregor98d156a2010-02-17 16:12:00 +00003222Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003223 // If this protocol has a definition in the translation unit we're coming
3224 // from, but this particular declaration is not that definition, import the
3225 // definition and map to that.
3226 ObjCProtocolDecl *Definition = D->getDefinition();
3227 if (Definition && Definition != D) {
3228 Decl *ImportedDef = Importer.Import(Definition);
3229 if (!ImportedDef)
3230 return 0;
3231
3232 return Importer.Imported(D, ImportedDef);
3233 }
3234
Douglas Gregor84c51c32010-02-18 01:47:50 +00003235 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003236 DeclContext *DC, *LexicalDC;
3237 DeclarationName Name;
3238 SourceLocation Loc;
3239 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3240 return 0;
3241
3242 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003243 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3244 DC->localUncachedLookup(Name, FoundDecls);
3245 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3246 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003247 continue;
3248
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003249 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003250 break;
3251 }
3252
3253 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003254 if (!ToProto) {
3255 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3256 Name.getAsIdentifierInfo(), Loc,
3257 Importer.Import(D->getAtStartLoc()),
3258 /*PrevDecl=*/0);
3259 ToProto->setLexicalDeclContext(LexicalDC);
3260 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003261 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003262
3263 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003264
Douglas Gregor2aa53772012-01-24 17:42:07 +00003265 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3266 return 0;
3267
Douglas Gregor98d156a2010-02-17 16:12:00 +00003268 return ToProto;
3269}
3270
Douglas Gregor2aa53772012-01-24 17:42:07 +00003271bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3272 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003273 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003274 if (To->getDefinition()) {
3275 // Check consistency of superclass.
3276 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3277 if (FromSuper) {
3278 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3279 if (!FromSuper)
3280 return true;
3281 }
3282
3283 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3284 if ((bool)FromSuper != (bool)ToSuper ||
3285 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3286 Importer.ToDiag(To->getLocation(),
3287 diag::err_odr_objc_superclass_inconsistent)
3288 << To->getDeclName();
3289 if (ToSuper)
3290 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3291 << To->getSuperClass()->getDeclName();
3292 else
3293 Importer.ToDiag(To->getLocation(),
3294 diag::note_odr_objc_missing_superclass);
3295 if (From->getSuperClass())
3296 Importer.FromDiag(From->getSuperClassLoc(),
3297 diag::note_odr_objc_superclass)
3298 << From->getSuperClass()->getDeclName();
3299 else
3300 Importer.FromDiag(From->getLocation(),
3301 diag::note_odr_objc_missing_superclass);
3302 }
3303
Douglas Gregor2e15c842012-02-01 21:00:38 +00003304 if (shouldForceImportDeclContext(Kind))
3305 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003306 return false;
3307 }
3308
3309 // Start the definition.
3310 To->startDefinition();
3311
3312 // If this class has a superclass, import it.
3313 if (From->getSuperClass()) {
3314 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3315 Importer.Import(From->getSuperClass()));
3316 if (!Super)
3317 return true;
3318
3319 To->setSuperClass(Super);
3320 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3321 }
3322
3323 // Import protocols
3324 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3325 SmallVector<SourceLocation, 4> ProtocolLocs;
3326 ObjCInterfaceDecl::protocol_loc_iterator
3327 FromProtoLoc = From->protocol_loc_begin();
3328
3329 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3330 FromProtoEnd = From->protocol_end();
3331 FromProto != FromProtoEnd;
3332 ++FromProto, ++FromProtoLoc) {
3333 ObjCProtocolDecl *ToProto
3334 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3335 if (!ToProto)
3336 return true;
3337 Protocols.push_back(ToProto);
3338 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3339 }
3340
3341 // FIXME: If we're merging, make sure that the protocol list is the same.
3342 To->setProtocolList(Protocols.data(), Protocols.size(),
3343 ProtocolLocs.data(), Importer.getToContext());
3344
3345 // Import categories. When the categories themselves are imported, they'll
3346 // hook themselves into this interface.
3347 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3348 FromCat = FromCat->getNextClassCategory())
3349 Importer.Import(FromCat);
3350
3351 // If we have an @implementation, import it as well.
3352 if (From->getImplementation()) {
3353 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3354 Importer.Import(From->getImplementation()));
3355 if (!Impl)
3356 return true;
3357
3358 To->setImplementation(Impl);
3359 }
3360
Douglas Gregor2e15c842012-02-01 21:00:38 +00003361 if (shouldForceImportDeclContext(Kind)) {
3362 // Import all of the members of this class.
3363 ImportDeclContext(From, /*ForceImport=*/true);
3364 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003365 return false;
3366}
3367
Douglas Gregor45635322010-02-16 01:20:57 +00003368Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003369 // If this class has a definition in the translation unit we're coming from,
3370 // but this particular declaration is not that definition, import the
3371 // definition and map to that.
3372 ObjCInterfaceDecl *Definition = D->getDefinition();
3373 if (Definition && Definition != D) {
3374 Decl *ImportedDef = Importer.Import(Definition);
3375 if (!ImportedDef)
3376 return 0;
3377
3378 return Importer.Imported(D, ImportedDef);
3379 }
3380
Douglas Gregor45635322010-02-16 01:20:57 +00003381 // Import the major distinguishing characteristics of an @interface.
3382 DeclContext *DC, *LexicalDC;
3383 DeclarationName Name;
3384 SourceLocation Loc;
3385 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3386 return 0;
3387
Douglas Gregor2aa53772012-01-24 17:42:07 +00003388 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003389 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003390 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3391 DC->localUncachedLookup(Name, FoundDecls);
3392 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3393 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003394 continue;
3395
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003396 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003397 break;
3398 }
3399
Douglas Gregor2aa53772012-01-24 17:42:07 +00003400 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003401 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003402 if (!ToIface) {
3403 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3404 Importer.Import(D->getAtStartLoc()),
3405 Name.getAsIdentifierInfo(),
3406 /*PrevDecl=*/0,Loc,
3407 D->isImplicitInterfaceDecl());
3408 ToIface->setLexicalDeclContext(LexicalDC);
3409 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003410 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003411 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003412
Douglas Gregor2aa53772012-01-24 17:42:07 +00003413 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3414 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003415
Douglas Gregor98d156a2010-02-17 16:12:00 +00003416 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003417}
3418
Douglas Gregor4da9d682010-12-07 15:32:12 +00003419Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3420 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3421 Importer.Import(D->getCategoryDecl()));
3422 if (!Category)
3423 return 0;
3424
3425 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3426 if (!ToImpl) {
3427 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3428 if (!DC)
3429 return 0;
3430
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003431 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003432 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003433 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003434 Category->getClassInterface(),
3435 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003436 Importer.Import(D->getAtStartLoc()),
3437 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003438
3439 DeclContext *LexicalDC = DC;
3440 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3441 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3442 if (!LexicalDC)
3443 return 0;
3444
3445 ToImpl->setLexicalDeclContext(LexicalDC);
3446 }
3447
Sean Callanan95e74be2011-10-21 02:57:43 +00003448 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003449 Category->setImplementation(ToImpl);
3450 }
3451
3452 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003453 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003454 return ToImpl;
3455}
3456
Douglas Gregorda8025c2010-12-07 01:26:03 +00003457Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3458 // Find the corresponding interface.
3459 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3460 Importer.Import(D->getClassInterface()));
3461 if (!Iface)
3462 return 0;
3463
3464 // Import the superclass, if any.
3465 ObjCInterfaceDecl *Super = 0;
3466 if (D->getSuperClass()) {
3467 Super = cast_or_null<ObjCInterfaceDecl>(
3468 Importer.Import(D->getSuperClass()));
3469 if (!Super)
3470 return 0;
3471 }
3472
3473 ObjCImplementationDecl *Impl = Iface->getImplementation();
3474 if (!Impl) {
3475 // We haven't imported an implementation yet. Create a new @implementation
3476 // now.
3477 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3478 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003479 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003480 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003481 Importer.Import(D->getAtStartLoc()),
3482 Importer.Import(D->getIvarLBraceLoc()),
3483 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003484
3485 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3486 DeclContext *LexicalDC
3487 = Importer.ImportContext(D->getLexicalDeclContext());
3488 if (!LexicalDC)
3489 return 0;
3490 Impl->setLexicalDeclContext(LexicalDC);
3491 }
3492
3493 // Associate the implementation with the class it implements.
3494 Iface->setImplementation(Impl);
3495 Importer.Imported(D, Iface->getImplementation());
3496 } else {
3497 Importer.Imported(D, Iface->getImplementation());
3498
3499 // Verify that the existing @implementation has the same superclass.
3500 if ((Super && !Impl->getSuperClass()) ||
3501 (!Super && Impl->getSuperClass()) ||
3502 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003503 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003504 Importer.ToDiag(Impl->getLocation(),
3505 diag::err_odr_objc_superclass_inconsistent)
3506 << Iface->getDeclName();
3507 // FIXME: It would be nice to have the location of the superclass
3508 // below.
3509 if (Impl->getSuperClass())
3510 Importer.ToDiag(Impl->getLocation(),
3511 diag::note_odr_objc_superclass)
3512 << Impl->getSuperClass()->getDeclName();
3513 else
3514 Importer.ToDiag(Impl->getLocation(),
3515 diag::note_odr_objc_missing_superclass);
3516 if (D->getSuperClass())
3517 Importer.FromDiag(D->getLocation(),
3518 diag::note_odr_objc_superclass)
3519 << D->getSuperClass()->getDeclName();
3520 else
3521 Importer.FromDiag(D->getLocation(),
3522 diag::note_odr_objc_missing_superclass);
3523 return 0;
3524 }
3525 }
3526
3527 // Import all of the members of this @implementation.
3528 ImportDeclContext(D);
3529
3530 return Impl;
3531}
3532
Douglas Gregora11c4582010-02-17 18:02:10 +00003533Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3534 // Import the major distinguishing characteristics of an @property.
3535 DeclContext *DC, *LexicalDC;
3536 DeclarationName Name;
3537 SourceLocation Loc;
3538 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3539 return 0;
3540
3541 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003542 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3543 DC->localUncachedLookup(Name, FoundDecls);
3544 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003545 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003546 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003547 // Check property types.
3548 if (!Importer.IsStructurallyEquivalent(D->getType(),
3549 FoundProp->getType())) {
3550 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3551 << Name << D->getType() << FoundProp->getType();
3552 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3553 << FoundProp->getType();
3554 return 0;
3555 }
3556
3557 // FIXME: Check property attributes, getters, setters, etc.?
3558
3559 // Consider these properties to be equivalent.
3560 Importer.Imported(D, FoundProp);
3561 return FoundProp;
3562 }
3563 }
3564
3565 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003566 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3567 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003568 return 0;
3569
3570 // Create the new property.
3571 ObjCPropertyDecl *ToProperty
3572 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3573 Name.getAsIdentifierInfo(),
3574 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003575 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003576 T,
3577 D->getPropertyImplementation());
3578 Importer.Imported(D, ToProperty);
3579 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003580 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003581
3582 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003583 ToProperty->setPropertyAttributesAsWritten(
3584 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003585 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3586 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3587 ToProperty->setGetterMethodDecl(
3588 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3589 ToProperty->setSetterMethodDecl(
3590 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3591 ToProperty->setPropertyIvarDecl(
3592 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3593 return ToProperty;
3594}
3595
Douglas Gregor14a49e22010-12-07 18:32:03 +00003596Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3597 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3598 Importer.Import(D->getPropertyDecl()));
3599 if (!Property)
3600 return 0;
3601
3602 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3603 if (!DC)
3604 return 0;
3605
3606 // Import the lexical declaration context.
3607 DeclContext *LexicalDC = DC;
3608 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3609 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3610 if (!LexicalDC)
3611 return 0;
3612 }
3613
3614 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3615 if (!InImpl)
3616 return 0;
3617
3618 // Import the ivar (for an @synthesize).
3619 ObjCIvarDecl *Ivar = 0;
3620 if (D->getPropertyIvarDecl()) {
3621 Ivar = cast_or_null<ObjCIvarDecl>(
3622 Importer.Import(D->getPropertyIvarDecl()));
3623 if (!Ivar)
3624 return 0;
3625 }
3626
3627 ObjCPropertyImplDecl *ToImpl
3628 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3629 if (!ToImpl) {
3630 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3631 Importer.Import(D->getLocStart()),
3632 Importer.Import(D->getLocation()),
3633 Property,
3634 D->getPropertyImplementation(),
3635 Ivar,
3636 Importer.Import(D->getPropertyIvarDeclLoc()));
3637 ToImpl->setLexicalDeclContext(LexicalDC);
3638 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003639 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003640 } else {
3641 // Check that we have the same kind of property implementation (@synthesize
3642 // vs. @dynamic).
3643 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3644 Importer.ToDiag(ToImpl->getLocation(),
3645 diag::err_odr_objc_property_impl_kind_inconsistent)
3646 << Property->getDeclName()
3647 << (ToImpl->getPropertyImplementation()
3648 == ObjCPropertyImplDecl::Dynamic);
3649 Importer.FromDiag(D->getLocation(),
3650 diag::note_odr_objc_property_impl_kind)
3651 << D->getPropertyDecl()->getDeclName()
3652 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3653 return 0;
3654 }
3655
3656 // For @synthesize, check that we have the same
3657 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3658 Ivar != ToImpl->getPropertyIvarDecl()) {
3659 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3660 diag::err_odr_objc_synthesize_ivar_inconsistent)
3661 << Property->getDeclName()
3662 << ToImpl->getPropertyIvarDecl()->getDeclName()
3663 << Ivar->getDeclName();
3664 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3665 diag::note_odr_objc_synthesize_ivar_here)
3666 << D->getPropertyIvarDecl()->getDeclName();
3667 return 0;
3668 }
3669
3670 // Merge the existing implementation with the new implementation.
3671 Importer.Imported(D, ToImpl);
3672 }
3673
3674 return ToImpl;
3675}
3676
Douglas Gregora082a492010-11-30 19:14:50 +00003677Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3678 // For template arguments, we adopt the translation unit as our declaration
3679 // context. This context will be fixed when the actual template declaration
3680 // is created.
3681
3682 // FIXME: Import default argument.
3683 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3684 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003685 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003686 Importer.Import(D->getLocation()),
3687 D->getDepth(),
3688 D->getIndex(),
3689 Importer.Import(D->getIdentifier()),
3690 D->wasDeclaredWithTypename(),
3691 D->isParameterPack());
3692}
3693
3694Decl *
3695ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3696 // Import the name of this declaration.
3697 DeclarationName Name = Importer.Import(D->getDeclName());
3698 if (D->getDeclName() && !Name)
3699 return 0;
3700
3701 // Import the location of this declaration.
3702 SourceLocation Loc = Importer.Import(D->getLocation());
3703
3704 // Import the type of this declaration.
3705 QualType T = Importer.Import(D->getType());
3706 if (T.isNull())
3707 return 0;
3708
3709 // Import type-source information.
3710 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3711 if (D->getTypeSourceInfo() && !TInfo)
3712 return 0;
3713
3714 // FIXME: Import default argument.
3715
3716 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3717 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003718 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003719 Loc, D->getDepth(), D->getPosition(),
3720 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003721 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003722}
3723
3724Decl *
3725ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3726 // Import the name of this declaration.
3727 DeclarationName Name = Importer.Import(D->getDeclName());
3728 if (D->getDeclName() && !Name)
3729 return 0;
3730
3731 // Import the location of this declaration.
3732 SourceLocation Loc = Importer.Import(D->getLocation());
3733
3734 // Import template parameters.
3735 TemplateParameterList *TemplateParams
3736 = ImportTemplateParameterList(D->getTemplateParameters());
3737 if (!TemplateParams)
3738 return 0;
3739
3740 // FIXME: Import default argument.
3741
3742 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3743 Importer.getToContext().getTranslationUnitDecl(),
3744 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003745 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003746 Name.getAsIdentifierInfo(),
3747 TemplateParams);
3748}
3749
3750Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3751 // If this record has a definition in the translation unit we're coming from,
3752 // but this particular declaration is not that definition, import the
3753 // definition and map to that.
3754 CXXRecordDecl *Definition
3755 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3756 if (Definition && Definition != D->getTemplatedDecl()) {
3757 Decl *ImportedDef
3758 = Importer.Import(Definition->getDescribedClassTemplate());
3759 if (!ImportedDef)
3760 return 0;
3761
3762 return Importer.Imported(D, ImportedDef);
3763 }
3764
3765 // Import the major distinguishing characteristics of this class template.
3766 DeclContext *DC, *LexicalDC;
3767 DeclarationName Name;
3768 SourceLocation Loc;
3769 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3770 return 0;
3771
3772 // We may already have a template of the same name; try to find and match it.
3773 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003774 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003775 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3776 DC->localUncachedLookup(Name, FoundDecls);
3777 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3778 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003779 continue;
3780
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003781 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003782 if (ClassTemplateDecl *FoundTemplate
3783 = dyn_cast<ClassTemplateDecl>(Found)) {
3784 if (IsStructuralMatch(D, FoundTemplate)) {
3785 // The class templates structurally match; call it the same template.
3786 // FIXME: We may be filling in a forward declaration here. Handle
3787 // this case!
3788 Importer.Imported(D->getTemplatedDecl(),
3789 FoundTemplate->getTemplatedDecl());
3790 return Importer.Imported(D, FoundTemplate);
3791 }
3792 }
3793
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003794 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003795 }
3796
3797 if (!ConflictingDecls.empty()) {
3798 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3799 ConflictingDecls.data(),
3800 ConflictingDecls.size());
3801 }
3802
3803 if (!Name)
3804 return 0;
3805 }
3806
3807 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3808
3809 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003810 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3811 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003812 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3813 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003814 DC, StartLoc, IdLoc,
3815 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003816 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003817 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003818 D2Templated->setLexicalDeclContext(LexicalDC);
3819
3820 // Create the class template declaration itself.
3821 TemplateParameterList *TemplateParams
3822 = ImportTemplateParameterList(D->getTemplateParameters());
3823 if (!TemplateParams)
3824 return 0;
3825
3826 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3827 Loc, Name, TemplateParams,
3828 D2Templated,
3829 /*PrevDecl=*/0);
3830 D2Templated->setDescribedClassTemplate(D2);
3831
3832 D2->setAccess(D->getAccess());
3833 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003834 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003835
3836 // Note the relationship between the class templates.
3837 Importer.Imported(D, D2);
3838 Importer.Imported(DTemplated, D2Templated);
3839
John McCallf937c022011-10-07 06:10:15 +00003840 if (DTemplated->isCompleteDefinition() &&
3841 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003842 // FIXME: Import definition!
3843 }
3844
3845 return D2;
3846}
3847
Douglas Gregore2e50d332010-12-01 01:36:18 +00003848Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3849 ClassTemplateSpecializationDecl *D) {
3850 // If this record has a definition in the translation unit we're coming from,
3851 // but this particular declaration is not that definition, import the
3852 // definition and map to that.
3853 TagDecl *Definition = D->getDefinition();
3854 if (Definition && Definition != D) {
3855 Decl *ImportedDef = Importer.Import(Definition);
3856 if (!ImportedDef)
3857 return 0;
3858
3859 return Importer.Imported(D, ImportedDef);
3860 }
3861
3862 ClassTemplateDecl *ClassTemplate
3863 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3864 D->getSpecializedTemplate()));
3865 if (!ClassTemplate)
3866 return 0;
3867
3868 // Import the context of this declaration.
3869 DeclContext *DC = ClassTemplate->getDeclContext();
3870 if (!DC)
3871 return 0;
3872
3873 DeclContext *LexicalDC = DC;
3874 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3875 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3876 if (!LexicalDC)
3877 return 0;
3878 }
3879
3880 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003881 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3882 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003883
3884 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003885 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003886 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3887 D->getTemplateArgs().size(),
3888 TemplateArgs))
3889 return 0;
3890
3891 // Try to find an existing specialization with these template arguments.
3892 void *InsertPos = 0;
3893 ClassTemplateSpecializationDecl *D2
3894 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3895 TemplateArgs.size(), InsertPos);
3896 if (D2) {
3897 // We already have a class template specialization with these template
3898 // arguments.
3899
3900 // FIXME: Check for specialization vs. instantiation errors.
3901
3902 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003903 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003904 // The record types structurally match, or the "from" translation
3905 // unit only had a forward declaration anyway; call it the same
3906 // function.
3907 return Importer.Imported(D, FoundDef);
3908 }
3909 }
3910 } else {
3911 // Create a new specialization.
3912 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3913 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003914 StartLoc, IdLoc,
3915 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003916 TemplateArgs.data(),
3917 TemplateArgs.size(),
3918 /*PrevDecl=*/0);
3919 D2->setSpecializationKind(D->getSpecializationKind());
3920
3921 // Add this specialization to the class template.
3922 ClassTemplate->AddSpecialization(D2, InsertPos);
3923
3924 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003925 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003926
3927 // Add the specialization to this context.
3928 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003929 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003930 }
3931 Importer.Imported(D, D2);
3932
John McCallf937c022011-10-07 06:10:15 +00003933 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003934 return 0;
3935
3936 return D2;
3937}
3938
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003939//----------------------------------------------------------------------------
3940// Import Statements
3941//----------------------------------------------------------------------------
3942
3943Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3944 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3945 << S->getStmtClassName();
3946 return 0;
3947}
3948
3949//----------------------------------------------------------------------------
3950// Import Expressions
3951//----------------------------------------------------------------------------
3952Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3953 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3954 << E->getStmtClassName();
3955 return 0;
3956}
3957
Douglas Gregor52f820e2010-02-19 01:17:02 +00003958Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003959 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3960 if (!ToD)
3961 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003962
3963 NamedDecl *FoundD = 0;
3964 if (E->getDecl() != E->getFoundDecl()) {
3965 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3966 if (!FoundD)
3967 return 0;
3968 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003969
3970 QualType T = Importer.Import(E->getType());
3971 if (T.isNull())
3972 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003973
3974 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3975 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003976 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003977 ToD,
John McCall113bee02012-03-10 09:33:50 +00003978 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003979 Importer.Import(E->getLocation()),
3980 T, E->getValueKind(),
3981 FoundD,
3982 /*FIXME:TemplateArgs=*/0);
3983 if (E->hadMultipleCandidates())
3984 DRE->setHadMultipleCandidates(true);
3985 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003986}
3987
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003988Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3989 QualType T = Importer.Import(E->getType());
3990 if (T.isNull())
3991 return 0;
3992
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003993 return IntegerLiteral::Create(Importer.getToContext(),
3994 E->getValue(), T,
3995 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003996}
3997
Douglas Gregor623421d2010-02-18 02:21:22 +00003998Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3999 QualType T = Importer.Import(E->getType());
4000 if (T.isNull())
4001 return 0;
4002
Douglas Gregorfb65e592011-07-27 05:40:30 +00004003 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4004 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004005 Importer.Import(E->getLocation()));
4006}
4007
Douglas Gregorc74247e2010-02-19 01:07:06 +00004008Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4009 Expr *SubExpr = Importer.Import(E->getSubExpr());
4010 if (!SubExpr)
4011 return 0;
4012
4013 return new (Importer.getToContext())
4014 ParenExpr(Importer.Import(E->getLParen()),
4015 Importer.Import(E->getRParen()),
4016 SubExpr);
4017}
4018
4019Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4020 QualType T = Importer.Import(E->getType());
4021 if (T.isNull())
4022 return 0;
4023
4024 Expr *SubExpr = Importer.Import(E->getSubExpr());
4025 if (!SubExpr)
4026 return 0;
4027
4028 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004029 T, E->getValueKind(),
4030 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004031 Importer.Import(E->getOperatorLoc()));
4032}
4033
Peter Collingbournee190dee2011-03-11 19:24:49 +00004034Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4035 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004036 QualType ResultType = Importer.Import(E->getType());
4037
4038 if (E->isArgumentType()) {
4039 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4040 if (!TInfo)
4041 return 0;
4042
Peter Collingbournee190dee2011-03-11 19:24:49 +00004043 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4044 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004045 Importer.Import(E->getOperatorLoc()),
4046 Importer.Import(E->getRParenLoc()));
4047 }
4048
4049 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4050 if (!SubExpr)
4051 return 0;
4052
Peter Collingbournee190dee2011-03-11 19:24:49 +00004053 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4054 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004055 Importer.Import(E->getOperatorLoc()),
4056 Importer.Import(E->getRParenLoc()));
4057}
4058
Douglas Gregorc74247e2010-02-19 01:07:06 +00004059Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4060 QualType T = Importer.Import(E->getType());
4061 if (T.isNull())
4062 return 0;
4063
4064 Expr *LHS = Importer.Import(E->getLHS());
4065 if (!LHS)
4066 return 0;
4067
4068 Expr *RHS = Importer.Import(E->getRHS());
4069 if (!RHS)
4070 return 0;
4071
4072 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004073 T, E->getValueKind(),
4074 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004075 Importer.Import(E->getOperatorLoc()));
4076}
4077
4078Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4079 QualType T = Importer.Import(E->getType());
4080 if (T.isNull())
4081 return 0;
4082
4083 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4084 if (CompLHSType.isNull())
4085 return 0;
4086
4087 QualType CompResultType = Importer.Import(E->getComputationResultType());
4088 if (CompResultType.isNull())
4089 return 0;
4090
4091 Expr *LHS = Importer.Import(E->getLHS());
4092 if (!LHS)
4093 return 0;
4094
4095 Expr *RHS = Importer.Import(E->getRHS());
4096 if (!RHS)
4097 return 0;
4098
4099 return new (Importer.getToContext())
4100 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004101 T, E->getValueKind(),
4102 E->getObjectKind(),
4103 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004104 Importer.Import(E->getOperatorLoc()));
4105}
4106
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004107static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004108 if (E->path_empty()) return false;
4109
4110 // TODO: import cast paths
4111 return true;
4112}
4113
Douglas Gregor98c10182010-02-12 22:17:39 +00004114Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4115 QualType T = Importer.Import(E->getType());
4116 if (T.isNull())
4117 return 0;
4118
4119 Expr *SubExpr = Importer.Import(E->getSubExpr());
4120 if (!SubExpr)
4121 return 0;
John McCallcf142162010-08-07 06:22:56 +00004122
4123 CXXCastPath BasePath;
4124 if (ImportCastPath(E, BasePath))
4125 return 0;
4126
4127 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004128 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004129}
4130
Douglas Gregor5481d322010-02-19 01:32:14 +00004131Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4132 QualType T = Importer.Import(E->getType());
4133 if (T.isNull())
4134 return 0;
4135
4136 Expr *SubExpr = Importer.Import(E->getSubExpr());
4137 if (!SubExpr)
4138 return 0;
4139
4140 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4141 if (!TInfo && E->getTypeInfoAsWritten())
4142 return 0;
4143
John McCallcf142162010-08-07 06:22:56 +00004144 CXXCastPath BasePath;
4145 if (ImportCastPath(E, BasePath))
4146 return 0;
4147
John McCall7decc9e2010-11-18 06:31:45 +00004148 return CStyleCastExpr::Create(Importer.getToContext(), T,
4149 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004150 SubExpr, &BasePath, TInfo,
4151 Importer.Import(E->getLParenLoc()),
4152 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004153}
4154
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004155ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004156 ASTContext &FromContext, FileManager &FromFileManager,
4157 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004158 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004159 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4160 Minimal(MinimalImport)
4161{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004162 ImportedDecls[FromContext.getTranslationUnitDecl()]
4163 = ToContext.getTranslationUnitDecl();
4164}
4165
4166ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004167
4168QualType ASTImporter::Import(QualType FromT) {
4169 if (FromT.isNull())
4170 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004171
4172 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004173
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004174 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004175 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4176 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004177 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004178 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004179
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004180 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004181 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004182 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004183 if (ToT.isNull())
4184 return ToT;
4185
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004186 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004187 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004188
John McCall424cec92011-01-19 06:33:43 +00004189 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004190}
4191
Douglas Gregor62d311f2010-02-09 19:21:46 +00004192TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004193 if (!FromTSI)
4194 return FromTSI;
4195
4196 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004197 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004198 QualType T = Import(FromTSI->getType());
4199 if (T.isNull())
4200 return 0;
4201
4202 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004203 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004204}
4205
4206Decl *ASTImporter::Import(Decl *FromD) {
4207 if (!FromD)
4208 return 0;
4209
Douglas Gregord451ea92011-07-29 23:31:30 +00004210 ASTNodeImporter Importer(*this);
4211
Douglas Gregor62d311f2010-02-09 19:21:46 +00004212 // Check whether we've already imported this declaration.
4213 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004214 if (Pos != ImportedDecls.end()) {
4215 Decl *ToD = Pos->second;
4216 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4217 return ToD;
4218 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004219
4220 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004221 Decl *ToD = Importer.Visit(FromD);
4222 if (!ToD)
4223 return 0;
4224
4225 // Record the imported declaration.
4226 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004227
4228 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4229 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004230 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004231 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004232 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004233 // When we've finished transforming a typedef, see whether it was the
4234 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004235 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004236 FromTag = AnonTagsWithPendingTypedefs.begin(),
4237 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4238 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004239 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004240 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4241 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004242 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004243 AnonTagsWithPendingTypedefs.erase(FromTag);
4244 break;
4245 }
4246 }
4247 }
4248 }
4249
Douglas Gregor62d311f2010-02-09 19:21:46 +00004250 return ToD;
4251}
4252
4253DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4254 if (!FromDC)
4255 return FromDC;
4256
Douglas Gregor95d82832012-01-24 18:36:04 +00004257 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004258 if (!ToDC)
4259 return 0;
4260
4261 // When we're using a record/enum/Objective-C class/protocol as a context, we
4262 // need it to have a definition.
4263 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004264 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004265 if (ToRecord->isCompleteDefinition()) {
4266 // Do nothing.
4267 } else if (FromRecord->isCompleteDefinition()) {
4268 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4269 ASTNodeImporter::IDK_Basic);
4270 } else {
4271 CompleteDecl(ToRecord);
4272 }
4273 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4274 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4275 if (ToEnum->isCompleteDefinition()) {
4276 // Do nothing.
4277 } else if (FromEnum->isCompleteDefinition()) {
4278 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4279 ASTNodeImporter::IDK_Basic);
4280 } else {
4281 CompleteDecl(ToEnum);
4282 }
4283 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4284 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4285 if (ToClass->getDefinition()) {
4286 // Do nothing.
4287 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4288 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4289 ASTNodeImporter::IDK_Basic);
4290 } else {
4291 CompleteDecl(ToClass);
4292 }
4293 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4294 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4295 if (ToProto->getDefinition()) {
4296 // Do nothing.
4297 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4298 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4299 ASTNodeImporter::IDK_Basic);
4300 } else {
4301 CompleteDecl(ToProto);
4302 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004303 }
4304
4305 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004306}
4307
4308Expr *ASTImporter::Import(Expr *FromE) {
4309 if (!FromE)
4310 return 0;
4311
4312 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4313}
4314
4315Stmt *ASTImporter::Import(Stmt *FromS) {
4316 if (!FromS)
4317 return 0;
4318
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004319 // Check whether we've already imported this declaration.
4320 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4321 if (Pos != ImportedStmts.end())
4322 return Pos->second;
4323
4324 // Import the type
4325 ASTNodeImporter Importer(*this);
4326 Stmt *ToS = Importer.Visit(FromS);
4327 if (!ToS)
4328 return 0;
4329
4330 // Record the imported declaration.
4331 ImportedStmts[FromS] = ToS;
4332 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004333}
4334
4335NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4336 if (!FromNNS)
4337 return 0;
4338
Douglas Gregor90ebf252011-04-27 16:48:40 +00004339 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4340
4341 switch (FromNNS->getKind()) {
4342 case NestedNameSpecifier::Identifier:
4343 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4344 return NestedNameSpecifier::Create(ToContext, prefix, II);
4345 }
4346 return 0;
4347
4348 case NestedNameSpecifier::Namespace:
4349 if (NamespaceDecl *NS =
4350 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4351 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4352 }
4353 return 0;
4354
4355 case NestedNameSpecifier::NamespaceAlias:
4356 if (NamespaceAliasDecl *NSAD =
4357 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4358 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4359 }
4360 return 0;
4361
4362 case NestedNameSpecifier::Global:
4363 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4364
4365 case NestedNameSpecifier::TypeSpec:
4366 case NestedNameSpecifier::TypeSpecWithTemplate: {
4367 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4368 if (!T.isNull()) {
4369 bool bTemplate = FromNNS->getKind() ==
4370 NestedNameSpecifier::TypeSpecWithTemplate;
4371 return NestedNameSpecifier::Create(ToContext, prefix,
4372 bTemplate, T.getTypePtr());
4373 }
4374 }
4375 return 0;
4376 }
4377
4378 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004379}
4380
Douglas Gregor14454802011-02-25 02:25:35 +00004381NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4382 // FIXME: Implement!
4383 return NestedNameSpecifierLoc();
4384}
4385
Douglas Gregore2e50d332010-12-01 01:36:18 +00004386TemplateName ASTImporter::Import(TemplateName From) {
4387 switch (From.getKind()) {
4388 case TemplateName::Template:
4389 if (TemplateDecl *ToTemplate
4390 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4391 return TemplateName(ToTemplate);
4392
4393 return TemplateName();
4394
4395 case TemplateName::OverloadedTemplate: {
4396 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4397 UnresolvedSet<2> ToTemplates;
4398 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4399 E = FromStorage->end();
4400 I != E; ++I) {
4401 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4402 ToTemplates.addDecl(To);
4403 else
4404 return TemplateName();
4405 }
4406 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4407 ToTemplates.end());
4408 }
4409
4410 case TemplateName::QualifiedTemplate: {
4411 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4412 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4413 if (!Qualifier)
4414 return TemplateName();
4415
4416 if (TemplateDecl *ToTemplate
4417 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4418 return ToContext.getQualifiedTemplateName(Qualifier,
4419 QTN->hasTemplateKeyword(),
4420 ToTemplate);
4421
4422 return TemplateName();
4423 }
4424
4425 case TemplateName::DependentTemplate: {
4426 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4427 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4428 if (!Qualifier)
4429 return TemplateName();
4430
4431 if (DTN->isIdentifier()) {
4432 return ToContext.getDependentTemplateName(Qualifier,
4433 Import(DTN->getIdentifier()));
4434 }
4435
4436 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4437 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004438
4439 case TemplateName::SubstTemplateTemplateParm: {
4440 SubstTemplateTemplateParmStorage *subst
4441 = From.getAsSubstTemplateTemplateParm();
4442 TemplateTemplateParmDecl *param
4443 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4444 if (!param)
4445 return TemplateName();
4446
4447 TemplateName replacement = Import(subst->getReplacement());
4448 if (replacement.isNull()) return TemplateName();
4449
4450 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4451 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004452
4453 case TemplateName::SubstTemplateTemplateParmPack: {
4454 SubstTemplateTemplateParmPackStorage *SubstPack
4455 = From.getAsSubstTemplateTemplateParmPack();
4456 TemplateTemplateParmDecl *Param
4457 = cast_or_null<TemplateTemplateParmDecl>(
4458 Import(SubstPack->getParameterPack()));
4459 if (!Param)
4460 return TemplateName();
4461
4462 ASTNodeImporter Importer(*this);
4463 TemplateArgument ArgPack
4464 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4465 if (ArgPack.isNull())
4466 return TemplateName();
4467
4468 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4469 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004470 }
4471
4472 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004473}
4474
Douglas Gregor62d311f2010-02-09 19:21:46 +00004475SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4476 if (FromLoc.isInvalid())
4477 return SourceLocation();
4478
Douglas Gregor811663e2010-02-10 00:15:17 +00004479 SourceManager &FromSM = FromContext.getSourceManager();
4480
4481 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004482 // don't have to import macro expansions.
4483 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004484 FromLoc = FromSM.getSpellingLoc(FromLoc);
4485 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4486 SourceManager &ToSM = ToContext.getSourceManager();
4487 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004488 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004489}
4490
4491SourceRange ASTImporter::Import(SourceRange FromRange) {
4492 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4493}
4494
Douglas Gregor811663e2010-02-10 00:15:17 +00004495FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004496 llvm::DenseMap<FileID, FileID>::iterator Pos
4497 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004498 if (Pos != ImportedFileIDs.end())
4499 return Pos->second;
4500
4501 SourceManager &FromSM = FromContext.getSourceManager();
4502 SourceManager &ToSM = ToContext.getSourceManager();
4503 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004504 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004505
4506 // Include location of this file.
4507 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4508
4509 // Map the FileID for to the "to" source manager.
4510 FileID ToID;
4511 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004512 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004513 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4514 // disk again
4515 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4516 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004517 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004518 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4519 FromSLoc.getFile().getFileCharacteristic());
4520 } else {
4521 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004522 const llvm::MemoryBuffer *
4523 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004524 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004525 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004526 FromBuf->getBufferIdentifier());
4527 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4528 }
4529
4530
Sebastian Redl99219f12010-09-30 01:03:06 +00004531 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004532 return ToID;
4533}
4534
Douglas Gregor0a791672011-01-18 03:11:38 +00004535void ASTImporter::ImportDefinition(Decl *From) {
4536 Decl *To = Import(From);
4537 if (!To)
4538 return;
4539
4540 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4541 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004542
4543 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4544 if (!ToRecord->getDefinition()) {
4545 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004546 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004547 return;
4548 }
4549 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004550
4551 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4552 if (!ToEnum->getDefinition()) {
4553 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004554 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004555 return;
4556 }
4557 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004558
4559 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4560 if (!ToIFace->getDefinition()) {
4561 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004562 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004563 return;
4564 }
4565 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004566
Douglas Gregor2aa53772012-01-24 17:42:07 +00004567 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4568 if (!ToProto->getDefinition()) {
4569 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004570 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004571 return;
4572 }
4573 }
4574
Douglas Gregor0a791672011-01-18 03:11:38 +00004575 Importer.ImportDeclContext(FromDC, true);
4576 }
4577}
4578
Douglas Gregor96e578d2010-02-05 17:54:41 +00004579DeclarationName ASTImporter::Import(DeclarationName FromName) {
4580 if (!FromName)
4581 return DeclarationName();
4582
4583 switch (FromName.getNameKind()) {
4584 case DeclarationName::Identifier:
4585 return Import(FromName.getAsIdentifierInfo());
4586
4587 case DeclarationName::ObjCZeroArgSelector:
4588 case DeclarationName::ObjCOneArgSelector:
4589 case DeclarationName::ObjCMultiArgSelector:
4590 return Import(FromName.getObjCSelector());
4591
4592 case DeclarationName::CXXConstructorName: {
4593 QualType T = Import(FromName.getCXXNameType());
4594 if (T.isNull())
4595 return DeclarationName();
4596
4597 return ToContext.DeclarationNames.getCXXConstructorName(
4598 ToContext.getCanonicalType(T));
4599 }
4600
4601 case DeclarationName::CXXDestructorName: {
4602 QualType T = Import(FromName.getCXXNameType());
4603 if (T.isNull())
4604 return DeclarationName();
4605
4606 return ToContext.DeclarationNames.getCXXDestructorName(
4607 ToContext.getCanonicalType(T));
4608 }
4609
4610 case DeclarationName::CXXConversionFunctionName: {
4611 QualType T = Import(FromName.getCXXNameType());
4612 if (T.isNull())
4613 return DeclarationName();
4614
4615 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4616 ToContext.getCanonicalType(T));
4617 }
4618
4619 case DeclarationName::CXXOperatorName:
4620 return ToContext.DeclarationNames.getCXXOperatorName(
4621 FromName.getCXXOverloadedOperator());
4622
4623 case DeclarationName::CXXLiteralOperatorName:
4624 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4625 Import(FromName.getCXXLiteralIdentifier()));
4626
4627 case DeclarationName::CXXUsingDirective:
4628 // FIXME: STATICS!
4629 return DeclarationName::getUsingDirectiveName();
4630 }
4631
David Blaikiee4d798f2012-01-20 21:50:17 +00004632 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004633}
4634
Douglas Gregore2e50d332010-12-01 01:36:18 +00004635IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004636 if (!FromId)
4637 return 0;
4638
4639 return &ToContext.Idents.get(FromId->getName());
4640}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004641
Douglas Gregor43f54792010-02-17 02:12:47 +00004642Selector ASTImporter::Import(Selector FromSel) {
4643 if (FromSel.isNull())
4644 return Selector();
4645
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004646 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004647 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4648 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4649 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4650 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4651}
4652
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004653DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4654 DeclContext *DC,
4655 unsigned IDNS,
4656 NamedDecl **Decls,
4657 unsigned NumDecls) {
4658 return Name;
4659}
4660
4661DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004662 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004663}
4664
4665DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004666 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004667}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004668
Douglas Gregor2e15c842012-02-01 21:00:38 +00004669void ASTImporter::CompleteDecl (Decl *D) {
4670 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4671 if (!ID->getDefinition())
4672 ID->startDefinition();
4673 }
4674 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4675 if (!PD->getDefinition())
4676 PD->startDefinition();
4677 }
4678 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4679 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4680 TD->startDefinition();
4681 TD->setCompleteDefinition(true);
4682 }
4683 }
4684 else {
4685 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4686 }
4687}
4688
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004689Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4690 ImportedDecls[From] = To;
4691 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004692}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004693
Douglas Gregordd6006f2012-07-17 21:16:27 +00004694bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4695 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004696 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004697 = ImportedTypes.find(From.getTypePtr());
4698 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4699 return true;
4700
Douglas Gregordd6006f2012-07-17 21:16:27 +00004701 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4702 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004703 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004704}