blob: a90026072bd071af4672ded7b416185a65f8a055 [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());
Eli Friedmanb826a002012-09-26 02:36:12 +0000291
Douglas Gregore2e50d332010-12-01 01:36:18 +0000292 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:
300 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000301
302 case TemplateArgument::NullPtr:
303 return true; // FIXME: Is this correct?
304
Douglas Gregore2e50d332010-12-01 01:36:18 +0000305 case TemplateArgument::Template:
306 return IsStructurallyEquivalent(Context,
307 Arg1.getAsTemplate(),
308 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000309
310 case TemplateArgument::TemplateExpansion:
311 return IsStructurallyEquivalent(Context,
312 Arg1.getAsTemplateOrTemplatePattern(),
313 Arg2.getAsTemplateOrTemplatePattern());
314
Douglas Gregore2e50d332010-12-01 01:36:18 +0000315 case TemplateArgument::Expression:
316 return IsStructurallyEquivalent(Context,
317 Arg1.getAsExpr(), Arg2.getAsExpr());
318
319 case TemplateArgument::Pack:
320 if (Arg1.pack_size() != Arg2.pack_size())
321 return false;
322
323 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
324 if (!IsStructurallyEquivalent(Context,
325 Arg1.pack_begin()[I],
326 Arg2.pack_begin()[I]))
327 return false;
328
329 return true;
330 }
331
332 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000333}
334
335/// \brief Determine structural equivalence for the common part of array
336/// types.
337static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
338 const ArrayType *Array1,
339 const ArrayType *Array2) {
340 if (!IsStructurallyEquivalent(Context,
341 Array1->getElementType(),
342 Array2->getElementType()))
343 return false;
344 if (Array1->getSizeModifier() != Array2->getSizeModifier())
345 return false;
346 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
347 return false;
348
349 return true;
350}
351
352/// \brief Determine structural equivalence of two types.
353static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
354 QualType T1, QualType T2) {
355 if (T1.isNull() || T2.isNull())
356 return T1.isNull() && T2.isNull();
357
358 if (!Context.StrictTypeSpelling) {
359 // We aren't being strict about token-to-token equivalence of types,
360 // so map down to the canonical type.
361 T1 = Context.C1.getCanonicalType(T1);
362 T2 = Context.C2.getCanonicalType(T2);
363 }
364
365 if (T1.getQualifiers() != T2.getQualifiers())
366 return false;
367
Douglas Gregorb4964f72010-02-15 23:54:17 +0000368 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000369
Douglas Gregorb4964f72010-02-15 23:54:17 +0000370 if (T1->getTypeClass() != T2->getTypeClass()) {
371 // Compare function types with prototypes vs. without prototypes as if
372 // both did not have prototypes.
373 if (T1->getTypeClass() == Type::FunctionProto &&
374 T2->getTypeClass() == Type::FunctionNoProto)
375 TC = Type::FunctionNoProto;
376 else if (T1->getTypeClass() == Type::FunctionNoProto &&
377 T2->getTypeClass() == Type::FunctionProto)
378 TC = Type::FunctionNoProto;
379 else
380 return false;
381 }
382
383 switch (TC) {
384 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000385 // FIXME: Deal with Char_S/Char_U.
386 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
387 return false;
388 break;
389
390 case Type::Complex:
391 if (!IsStructurallyEquivalent(Context,
392 cast<ComplexType>(T1)->getElementType(),
393 cast<ComplexType>(T2)->getElementType()))
394 return false;
395 break;
396
397 case Type::Pointer:
398 if (!IsStructurallyEquivalent(Context,
399 cast<PointerType>(T1)->getPointeeType(),
400 cast<PointerType>(T2)->getPointeeType()))
401 return false;
402 break;
403
404 case Type::BlockPointer:
405 if (!IsStructurallyEquivalent(Context,
406 cast<BlockPointerType>(T1)->getPointeeType(),
407 cast<BlockPointerType>(T2)->getPointeeType()))
408 return false;
409 break;
410
411 case Type::LValueReference:
412 case Type::RValueReference: {
413 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
414 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
415 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
416 return false;
417 if (Ref1->isInnerRef() != Ref2->isInnerRef())
418 return false;
419 if (!IsStructurallyEquivalent(Context,
420 Ref1->getPointeeTypeAsWritten(),
421 Ref2->getPointeeTypeAsWritten()))
422 return false;
423 break;
424 }
425
426 case Type::MemberPointer: {
427 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
428 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
429 if (!IsStructurallyEquivalent(Context,
430 MemPtr1->getPointeeType(),
431 MemPtr2->getPointeeType()))
432 return false;
433 if (!IsStructurallyEquivalent(Context,
434 QualType(MemPtr1->getClass(), 0),
435 QualType(MemPtr2->getClass(), 0)))
436 return false;
437 break;
438 }
439
440 case Type::ConstantArray: {
441 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
442 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000443 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000444 return false;
445
446 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
447 return false;
448 break;
449 }
450
451 case Type::IncompleteArray:
452 if (!IsArrayStructurallyEquivalent(Context,
453 cast<ArrayType>(T1),
454 cast<ArrayType>(T2)))
455 return false;
456 break;
457
458 case Type::VariableArray: {
459 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
460 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
461 if (!IsStructurallyEquivalent(Context,
462 Array1->getSizeExpr(), Array2->getSizeExpr()))
463 return false;
464
465 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
466 return false;
467
468 break;
469 }
470
471 case Type::DependentSizedArray: {
472 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
473 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
474 if (!IsStructurallyEquivalent(Context,
475 Array1->getSizeExpr(), Array2->getSizeExpr()))
476 return false;
477
478 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
479 return false;
480
481 break;
482 }
483
484 case Type::DependentSizedExtVector: {
485 const DependentSizedExtVectorType *Vec1
486 = cast<DependentSizedExtVectorType>(T1);
487 const DependentSizedExtVectorType *Vec2
488 = cast<DependentSizedExtVectorType>(T2);
489 if (!IsStructurallyEquivalent(Context,
490 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
491 return false;
492 if (!IsStructurallyEquivalent(Context,
493 Vec1->getElementType(),
494 Vec2->getElementType()))
495 return false;
496 break;
497 }
498
499 case Type::Vector:
500 case Type::ExtVector: {
501 const VectorType *Vec1 = cast<VectorType>(T1);
502 const VectorType *Vec2 = cast<VectorType>(T2);
503 if (!IsStructurallyEquivalent(Context,
504 Vec1->getElementType(),
505 Vec2->getElementType()))
506 return false;
507 if (Vec1->getNumElements() != Vec2->getNumElements())
508 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000509 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000510 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000511 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000512 }
513
514 case Type::FunctionProto: {
515 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
516 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
517 if (Proto1->getNumArgs() != Proto2->getNumArgs())
518 return false;
519 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
520 if (!IsStructurallyEquivalent(Context,
521 Proto1->getArgType(I),
522 Proto2->getArgType(I)))
523 return false;
524 }
525 if (Proto1->isVariadic() != Proto2->isVariadic())
526 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000527 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000528 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000529 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
530 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
531 return false;
532 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
533 if (!IsStructurallyEquivalent(Context,
534 Proto1->getExceptionType(I),
535 Proto2->getExceptionType(I)))
536 return false;
537 }
538 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000540 Proto1->getNoexceptExpr(),
541 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000542 return false;
543 }
544 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
545 return false;
546
547 // Fall through to check the bits common with FunctionNoProtoType.
548 }
549
550 case Type::FunctionNoProto: {
551 const FunctionType *Function1 = cast<FunctionType>(T1);
552 const FunctionType *Function2 = cast<FunctionType>(T2);
553 if (!IsStructurallyEquivalent(Context,
554 Function1->getResultType(),
555 Function2->getResultType()))
556 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000557 if (Function1->getExtInfo() != Function2->getExtInfo())
558 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000559 break;
560 }
561
562 case Type::UnresolvedUsing:
563 if (!IsStructurallyEquivalent(Context,
564 cast<UnresolvedUsingType>(T1)->getDecl(),
565 cast<UnresolvedUsingType>(T2)->getDecl()))
566 return false;
567
568 break;
John McCall81904512011-01-06 01:58:22 +0000569
570 case Type::Attributed:
571 if (!IsStructurallyEquivalent(Context,
572 cast<AttributedType>(T1)->getModifiedType(),
573 cast<AttributedType>(T2)->getModifiedType()))
574 return false;
575 if (!IsStructurallyEquivalent(Context,
576 cast<AttributedType>(T1)->getEquivalentType(),
577 cast<AttributedType>(T2)->getEquivalentType()))
578 return false;
579 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000580
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000581 case Type::Paren:
582 if (!IsStructurallyEquivalent(Context,
583 cast<ParenType>(T1)->getInnerType(),
584 cast<ParenType>(T2)->getInnerType()))
585 return false;
586 break;
587
Douglas Gregor3996e242010-02-15 22:01:00 +0000588 case Type::Typedef:
589 if (!IsStructurallyEquivalent(Context,
590 cast<TypedefType>(T1)->getDecl(),
591 cast<TypedefType>(T2)->getDecl()))
592 return false;
593 break;
594
595 case Type::TypeOfExpr:
596 if (!IsStructurallyEquivalent(Context,
597 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
598 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
599 return false;
600 break;
601
602 case Type::TypeOf:
603 if (!IsStructurallyEquivalent(Context,
604 cast<TypeOfType>(T1)->getUnderlyingType(),
605 cast<TypeOfType>(T2)->getUnderlyingType()))
606 return false;
607 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000608
609 case Type::UnaryTransform:
610 if (!IsStructurallyEquivalent(Context,
611 cast<UnaryTransformType>(T1)->getUnderlyingType(),
612 cast<UnaryTransformType>(T1)->getUnderlyingType()))
613 return false;
614 break;
615
Douglas Gregor3996e242010-02-15 22:01:00 +0000616 case Type::Decltype:
617 if (!IsStructurallyEquivalent(Context,
618 cast<DecltypeType>(T1)->getUnderlyingExpr(),
619 cast<DecltypeType>(T2)->getUnderlyingExpr()))
620 return false;
621 break;
622
Richard Smith30482bc2011-02-20 03:19:35 +0000623 case Type::Auto:
624 if (!IsStructurallyEquivalent(Context,
625 cast<AutoType>(T1)->getDeducedType(),
626 cast<AutoType>(T2)->getDeducedType()))
627 return false;
628 break;
629
Douglas Gregor3996e242010-02-15 22:01:00 +0000630 case Type::Record:
631 case Type::Enum:
632 if (!IsStructurallyEquivalent(Context,
633 cast<TagType>(T1)->getDecl(),
634 cast<TagType>(T2)->getDecl()))
635 return false;
636 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000637
Douglas Gregor3996e242010-02-15 22:01:00 +0000638 case Type::TemplateTypeParm: {
639 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
640 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
641 if (Parm1->getDepth() != Parm2->getDepth())
642 return false;
643 if (Parm1->getIndex() != Parm2->getIndex())
644 return false;
645 if (Parm1->isParameterPack() != Parm2->isParameterPack())
646 return false;
647
648 // Names of template type parameters are never significant.
649 break;
650 }
651
652 case Type::SubstTemplateTypeParm: {
653 const SubstTemplateTypeParmType *Subst1
654 = cast<SubstTemplateTypeParmType>(T1);
655 const SubstTemplateTypeParmType *Subst2
656 = cast<SubstTemplateTypeParmType>(T2);
657 if (!IsStructurallyEquivalent(Context,
658 QualType(Subst1->getReplacedParameter(), 0),
659 QualType(Subst2->getReplacedParameter(), 0)))
660 return false;
661 if (!IsStructurallyEquivalent(Context,
662 Subst1->getReplacementType(),
663 Subst2->getReplacementType()))
664 return false;
665 break;
666 }
667
Douglas Gregorfb322d82011-01-14 05:11:40 +0000668 case Type::SubstTemplateTypeParmPack: {
669 const SubstTemplateTypeParmPackType *Subst1
670 = cast<SubstTemplateTypeParmPackType>(T1);
671 const SubstTemplateTypeParmPackType *Subst2
672 = cast<SubstTemplateTypeParmPackType>(T2);
673 if (!IsStructurallyEquivalent(Context,
674 QualType(Subst1->getReplacedParameter(), 0),
675 QualType(Subst2->getReplacedParameter(), 0)))
676 return false;
677 if (!IsStructurallyEquivalent(Context,
678 Subst1->getArgumentPack(),
679 Subst2->getArgumentPack()))
680 return false;
681 break;
682 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000683 case Type::TemplateSpecialization: {
684 const TemplateSpecializationType *Spec1
685 = cast<TemplateSpecializationType>(T1);
686 const TemplateSpecializationType *Spec2
687 = cast<TemplateSpecializationType>(T2);
688 if (!IsStructurallyEquivalent(Context,
689 Spec1->getTemplateName(),
690 Spec2->getTemplateName()))
691 return false;
692 if (Spec1->getNumArgs() != Spec2->getNumArgs())
693 return false;
694 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
695 if (!IsStructurallyEquivalent(Context,
696 Spec1->getArg(I), Spec2->getArg(I)))
697 return false;
698 }
699 break;
700 }
701
Abramo Bagnara6150c882010-05-11 21:36:43 +0000702 case Type::Elaborated: {
703 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
704 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
705 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
706 if (Elab1->getKeyword() != Elab2->getKeyword())
707 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000708 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000709 Elab1->getQualifier(),
710 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000711 return false;
712 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000713 Elab1->getNamedType(),
714 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000715 return false;
716 break;
717 }
718
John McCalle78aac42010-03-10 03:28:59 +0000719 case Type::InjectedClassName: {
720 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
721 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
722 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000723 Inj1->getInjectedSpecializationType(),
724 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000725 return false;
726 break;
727 }
728
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000729 case Type::DependentName: {
730 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
731 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000732 if (!IsStructurallyEquivalent(Context,
733 Typename1->getQualifier(),
734 Typename2->getQualifier()))
735 return false;
736 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
737 Typename2->getIdentifier()))
738 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000739
740 break;
741 }
742
John McCallc392f372010-06-11 00:33:02 +0000743 case Type::DependentTemplateSpecialization: {
744 const DependentTemplateSpecializationType *Spec1 =
745 cast<DependentTemplateSpecializationType>(T1);
746 const DependentTemplateSpecializationType *Spec2 =
747 cast<DependentTemplateSpecializationType>(T2);
748 if (!IsStructurallyEquivalent(Context,
749 Spec1->getQualifier(),
750 Spec2->getQualifier()))
751 return false;
752 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
753 Spec2->getIdentifier()))
754 return false;
755 if (Spec1->getNumArgs() != Spec2->getNumArgs())
756 return false;
757 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
758 if (!IsStructurallyEquivalent(Context,
759 Spec1->getArg(I), Spec2->getArg(I)))
760 return false;
761 }
762 break;
763 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000764
765 case Type::PackExpansion:
766 if (!IsStructurallyEquivalent(Context,
767 cast<PackExpansionType>(T1)->getPattern(),
768 cast<PackExpansionType>(T2)->getPattern()))
769 return false;
770 break;
771
Douglas Gregor3996e242010-02-15 22:01:00 +0000772 case Type::ObjCInterface: {
773 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
774 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
775 if (!IsStructurallyEquivalent(Context,
776 Iface1->getDecl(), Iface2->getDecl()))
777 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000778 break;
779 }
780
781 case Type::ObjCObject: {
782 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
783 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
784 if (!IsStructurallyEquivalent(Context,
785 Obj1->getBaseType(),
786 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000787 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000788 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
789 return false;
790 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000792 Obj1->getProtocol(I),
793 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000794 return false;
795 }
796 break;
797 }
798
799 case Type::ObjCObjectPointer: {
800 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
801 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
802 if (!IsStructurallyEquivalent(Context,
803 Ptr1->getPointeeType(),
804 Ptr2->getPointeeType()))
805 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000806 break;
807 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000808
809 case Type::Atomic: {
810 if (!IsStructurallyEquivalent(Context,
811 cast<AtomicType>(T1)->getValueType(),
812 cast<AtomicType>(T2)->getValueType()))
813 return false;
814 break;
815 }
816
Douglas Gregor3996e242010-02-15 22:01:00 +0000817 } // end switch
818
819 return true;
820}
821
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000822/// \brief Determine structural equivalence of two fields.
823static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
824 FieldDecl *Field1, FieldDecl *Field2) {
825 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
826
827 if (!IsStructurallyEquivalent(Context,
828 Field1->getType(), Field2->getType())) {
829 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
830 << Context.C2.getTypeDeclType(Owner2);
831 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
832 << Field2->getDeclName() << Field2->getType();
833 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
834 << Field1->getDeclName() << Field1->getType();
835 return false;
836 }
837
838 if (Field1->isBitField() != Field2->isBitField()) {
839 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
840 << Context.C2.getTypeDeclType(Owner2);
841 if (Field1->isBitField()) {
842 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
843 << Field1->getDeclName() << Field1->getType()
844 << Field1->getBitWidthValue(Context.C1);
845 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
846 << Field2->getDeclName();
847 } else {
848 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
849 << Field2->getDeclName() << Field2->getType()
850 << Field2->getBitWidthValue(Context.C2);
851 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
852 << Field1->getDeclName();
853 }
854 return false;
855 }
856
857 if (Field1->isBitField()) {
858 // Make sure that the bit-fields are the same length.
859 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
860 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
861
862 if (Bits1 != Bits2) {
863 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
864 << Context.C2.getTypeDeclType(Owner2);
865 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
866 << Field2->getDeclName() << Field2->getType() << Bits2;
867 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
868 << Field1->getDeclName() << Field1->getType() << Bits1;
869 return false;
870 }
871 }
872
873 return true;
874}
875
Douglas Gregor3996e242010-02-15 22:01:00 +0000876/// \brief Determine structural equivalence of two records.
877static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
878 RecordDecl *D1, RecordDecl *D2) {
879 if (D1->isUnion() != D2->isUnion()) {
880 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
881 << Context.C2.getTypeDeclType(D2);
882 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
883 << D1->getDeclName() << (unsigned)D1->getTagKind();
884 return false;
885 }
886
Douglas Gregore2e50d332010-12-01 01:36:18 +0000887 // If both declarations are class template specializations, we know
888 // the ODR applies, so check the template and template arguments.
889 ClassTemplateSpecializationDecl *Spec1
890 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
891 ClassTemplateSpecializationDecl *Spec2
892 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
893 if (Spec1 && Spec2) {
894 // Check that the specialized templates are the same.
895 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
896 Spec2->getSpecializedTemplate()))
897 return false;
898
899 // Check that the template arguments are the same.
900 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
901 return false;
902
903 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
904 if (!IsStructurallyEquivalent(Context,
905 Spec1->getTemplateArgs().get(I),
906 Spec2->getTemplateArgs().get(I)))
907 return false;
908 }
909 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000910 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000911 else if (Spec1 || Spec2)
912 return false;
913
Douglas Gregorb4964f72010-02-15 23:54:17 +0000914 // Compare the definitions of these two records. If either or both are
915 // incomplete, we assume that they are equivalent.
916 D1 = D1->getDefinition();
917 D2 = D2->getDefinition();
918 if (!D1 || !D2)
919 return true;
920
Douglas Gregor3996e242010-02-15 22:01:00 +0000921 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
922 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
923 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
924 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000925 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000926 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000927 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000928 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000929 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000930 return false;
931 }
932
933 // Check the base classes.
934 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
935 BaseEnd1 = D1CXX->bases_end(),
936 Base2 = D2CXX->bases_begin();
937 Base1 != BaseEnd1;
938 ++Base1, ++Base2) {
939 if (!IsStructurallyEquivalent(Context,
940 Base1->getType(), Base2->getType())) {
941 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
942 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000943 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000944 << Base2->getType()
945 << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000946 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000947 << Base1->getType()
948 << Base1->getSourceRange();
949 return false;
950 }
951
952 // Check virtual vs. non-virtual inheritance mismatch.
953 if (Base1->isVirtual() != Base2->isVirtual()) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000956 Context.Diag2(Base2->getLocStart(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000957 diag::note_odr_virtual_base)
958 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000959 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000960 << Base1->isVirtual()
961 << Base1->getSourceRange();
962 return false;
963 }
964 }
965 } else if (D1CXX->getNumBases() > 0) {
966 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
967 << Context.C2.getTypeDeclType(D2);
968 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000969 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000970 << Base1->getType()
971 << Base1->getSourceRange();
972 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
973 return false;
974 }
975 }
976
977 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000978 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000979 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000980 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000981 Field1End = D1->field_end();
982 Field1 != Field1End;
983 ++Field1, ++Field2) {
984 if (Field2 == Field2End) {
985 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
986 << Context.C2.getTypeDeclType(D2);
987 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
988 << Field1->getDeclName() << Field1->getType();
989 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
990 return false;
991 }
992
David Blaikie40ed2972012-06-06 20:45:41 +0000993 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000994 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000995 }
996
997 if (Field2 != Field2End) {
998 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
999 << Context.C2.getTypeDeclType(D2);
1000 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1001 << Field2->getDeclName() << Field2->getType();
1002 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1003 return false;
1004 }
1005
1006 return true;
1007}
1008
1009/// \brief Determine structural equivalence of two enums.
1010static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1011 EnumDecl *D1, EnumDecl *D2) {
1012 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1013 EC2End = D2->enumerator_end();
1014 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1015 EC1End = D1->enumerator_end();
1016 EC1 != EC1End; ++EC1, ++EC2) {
1017 if (EC2 == EC2End) {
1018 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1019 << Context.C2.getTypeDeclType(D2);
1020 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1021 << EC1->getDeclName()
1022 << EC1->getInitVal().toString(10);
1023 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1024 return false;
1025 }
1026
1027 llvm::APSInt Val1 = EC1->getInitVal();
1028 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001029 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001030 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1031 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1032 << Context.C2.getTypeDeclType(D2);
1033 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1034 << EC2->getDeclName()
1035 << EC2->getInitVal().toString(10);
1036 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1037 << EC1->getDeclName()
1038 << EC1->getInitVal().toString(10);
1039 return false;
1040 }
1041 }
1042
1043 if (EC2 != EC2End) {
1044 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1045 << Context.C2.getTypeDeclType(D2);
1046 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1047 << EC2->getDeclName()
1048 << EC2->getInitVal().toString(10);
1049 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1050 return false;
1051 }
1052
1053 return true;
1054}
Douglas Gregora082a492010-11-30 19:14:50 +00001055
1056static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1057 TemplateParameterList *Params1,
1058 TemplateParameterList *Params2) {
1059 if (Params1->size() != Params2->size()) {
1060 Context.Diag2(Params2->getTemplateLoc(),
1061 diag::err_odr_different_num_template_parameters)
1062 << Params1->size() << Params2->size();
1063 Context.Diag1(Params1->getTemplateLoc(),
1064 diag::note_odr_template_parameter_list);
1065 return false;
1066 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001067
Douglas Gregora082a492010-11-30 19:14:50 +00001068 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1069 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1070 Context.Diag2(Params2->getParam(I)->getLocation(),
1071 diag::err_odr_different_template_parameter_kind);
1072 Context.Diag1(Params1->getParam(I)->getLocation(),
1073 diag::note_odr_template_parameter_here);
1074 return false;
1075 }
1076
1077 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1078 Params2->getParam(I))) {
1079
1080 return false;
1081 }
1082 }
1083
1084 return true;
1085}
1086
1087static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1088 TemplateTypeParmDecl *D1,
1089 TemplateTypeParmDecl *D2) {
1090 if (D1->isParameterPack() != D2->isParameterPack()) {
1091 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1092 << D2->isParameterPack();
1093 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1094 << D1->isParameterPack();
1095 return false;
1096 }
1097
1098 return true;
1099}
1100
1101static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1102 NonTypeTemplateParmDecl *D1,
1103 NonTypeTemplateParmDecl *D2) {
1104 // FIXME: Enable once we have variadic templates.
1105#if 0
1106 if (D1->isParameterPack() != D2->isParameterPack()) {
1107 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1108 << D2->isParameterPack();
1109 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1110 << D1->isParameterPack();
1111 return false;
1112 }
1113#endif
1114
1115 // Check types.
1116 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1117 Context.Diag2(D2->getLocation(),
1118 diag::err_odr_non_type_parameter_type_inconsistent)
1119 << D2->getType() << D1->getType();
1120 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1121 << D1->getType();
1122 return false;
1123 }
1124
1125 return true;
1126}
1127
1128static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1129 TemplateTemplateParmDecl *D1,
1130 TemplateTemplateParmDecl *D2) {
1131 // FIXME: Enable once we have variadic templates.
1132#if 0
1133 if (D1->isParameterPack() != D2->isParameterPack()) {
1134 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1135 << D2->isParameterPack();
1136 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1137 << D1->isParameterPack();
1138 return false;
1139 }
1140#endif
1141
1142 // Check template parameter lists.
1143 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1144 D2->getTemplateParameters());
1145}
1146
1147static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1148 ClassTemplateDecl *D1,
1149 ClassTemplateDecl *D2) {
1150 // Check template parameters.
1151 if (!IsStructurallyEquivalent(Context,
1152 D1->getTemplateParameters(),
1153 D2->getTemplateParameters()))
1154 return false;
1155
1156 // Check the templated declaration.
1157 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1158 D2->getTemplatedDecl());
1159}
1160
Douglas Gregor3996e242010-02-15 22:01:00 +00001161/// \brief Determine structural equivalence of two declarations.
1162static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1163 Decl *D1, Decl *D2) {
1164 // FIXME: Check for known structural equivalences via a callback of some sort.
1165
Douglas Gregorb4964f72010-02-15 23:54:17 +00001166 // Check whether we already know that these two declarations are not
1167 // structurally equivalent.
1168 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1169 D2->getCanonicalDecl())))
1170 return false;
1171
Douglas Gregor3996e242010-02-15 22:01:00 +00001172 // Determine whether we've already produced a tentative equivalence for D1.
1173 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1174 if (EquivToD1)
1175 return EquivToD1 == D2->getCanonicalDecl();
1176
1177 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1178 EquivToD1 = D2->getCanonicalDecl();
1179 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1180 return true;
1181}
1182
1183bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1184 Decl *D2) {
1185 if (!::IsStructurallyEquivalent(*this, D1, D2))
1186 return false;
1187
1188 return !Finish();
1189}
1190
1191bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1192 QualType T2) {
1193 if (!::IsStructurallyEquivalent(*this, T1, T2))
1194 return false;
1195
1196 return !Finish();
1197}
1198
1199bool StructuralEquivalenceContext::Finish() {
1200 while (!DeclsToCheck.empty()) {
1201 // Check the next declaration.
1202 Decl *D1 = DeclsToCheck.front();
1203 DeclsToCheck.pop_front();
1204
1205 Decl *D2 = TentativeEquivalences[D1];
1206 assert(D2 && "Unrecorded tentative equivalence?");
1207
Douglas Gregorb4964f72010-02-15 23:54:17 +00001208 bool Equivalent = true;
1209
Douglas Gregor3996e242010-02-15 22:01:00 +00001210 // FIXME: Switch on all declaration kinds. For now, we're just going to
1211 // check the obvious ones.
1212 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1213 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1214 // Check for equivalent structure names.
1215 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001216 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1217 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001218 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001219 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1220 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001221 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1222 !::IsStructurallyEquivalent(*this, Record1, Record2))
1223 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001224 } else {
1225 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001226 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001227 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001228 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001229 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1230 // Check for equivalent enum names.
1231 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001232 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1233 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001234 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001235 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1236 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001237 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1238 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1239 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001240 } else {
1241 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001242 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001243 }
Richard Smithdda56e42011-04-15 14:24:37 +00001244 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1245 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001246 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001247 Typedef2->getIdentifier()) ||
1248 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001249 Typedef1->getUnderlyingType(),
1250 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001251 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001252 } else {
1253 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001254 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001255 }
Douglas Gregora082a492010-11-30 19:14:50 +00001256 } else if (ClassTemplateDecl *ClassTemplate1
1257 = dyn_cast<ClassTemplateDecl>(D1)) {
1258 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1259 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1260 ClassTemplate2->getIdentifier()) ||
1261 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1262 Equivalent = false;
1263 } else {
1264 // Class template/non-class-template mismatch.
1265 Equivalent = false;
1266 }
1267 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1268 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1269 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1270 Equivalent = false;
1271 } else {
1272 // Kind mismatch.
1273 Equivalent = false;
1274 }
1275 } else if (NonTypeTemplateParmDecl *NTTP1
1276 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1277 if (NonTypeTemplateParmDecl *NTTP2
1278 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1279 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1280 Equivalent = false;
1281 } else {
1282 // Kind mismatch.
1283 Equivalent = false;
1284 }
1285 } else if (TemplateTemplateParmDecl *TTP1
1286 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1287 if (TemplateTemplateParmDecl *TTP2
1288 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1289 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1290 Equivalent = false;
1291 } else {
1292 // Kind mismatch.
1293 Equivalent = false;
1294 }
1295 }
1296
Douglas Gregorb4964f72010-02-15 23:54:17 +00001297 if (!Equivalent) {
1298 // Note that these two declarations are not equivalent (and we already
1299 // know about it).
1300 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1301 D2->getCanonicalDecl()));
1302 return true;
1303 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001304 // FIXME: Check other declaration kinds!
1305 }
1306
1307 return false;
1308}
1309
1310//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001311// Import Types
1312//----------------------------------------------------------------------------
1313
John McCall424cec92011-01-19 06:33:43 +00001314QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001315 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1316 << T->getTypeClassName();
1317 return QualType();
1318}
1319
John McCall424cec92011-01-19 06:33:43 +00001320QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001321 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001322#define SHARED_SINGLETON_TYPE(Expansion)
1323#define BUILTIN_TYPE(Id, SingletonId) \
1324 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1325#include "clang/AST/BuiltinTypes.def"
1326
1327 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1328 // context supports C++.
1329
1330 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1331 // context supports ObjC.
1332
Douglas Gregor96e578d2010-02-05 17:54:41 +00001333 case BuiltinType::Char_U:
1334 // The context we're importing from has an unsigned 'char'. If we're
1335 // importing into a context with a signed 'char', translate to
1336 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001337 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001338 return Importer.getToContext().UnsignedCharTy;
1339
1340 return Importer.getToContext().CharTy;
1341
Douglas Gregor96e578d2010-02-05 17:54:41 +00001342 case BuiltinType::Char_S:
1343 // The context we're importing from has an unsigned 'char'. If we're
1344 // importing into a context with a signed 'char', translate to
1345 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001346 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001347 return Importer.getToContext().SignedCharTy;
1348
1349 return Importer.getToContext().CharTy;
1350
Chris Lattnerad3467e2010-12-25 23:25:43 +00001351 case BuiltinType::WChar_S:
1352 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001353 // FIXME: If not in C++, shall we translate to the C equivalent of
1354 // wchar_t?
1355 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001356 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001357
1358 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001359}
1360
John McCall424cec92011-01-19 06:33:43 +00001361QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001362 QualType ToElementType = Importer.Import(T->getElementType());
1363 if (ToElementType.isNull())
1364 return QualType();
1365
1366 return Importer.getToContext().getComplexType(ToElementType);
1367}
1368
John McCall424cec92011-01-19 06:33:43 +00001369QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001370 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1371 if (ToPointeeType.isNull())
1372 return QualType();
1373
1374 return Importer.getToContext().getPointerType(ToPointeeType);
1375}
1376
John McCall424cec92011-01-19 06:33:43 +00001377QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001378 // FIXME: Check for blocks support in "to" context.
1379 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1380 if (ToPointeeType.isNull())
1381 return QualType();
1382
1383 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1384}
1385
John McCall424cec92011-01-19 06:33:43 +00001386QualType
1387ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001388 // FIXME: Check for C++ support in "to" context.
1389 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1390 if (ToPointeeType.isNull())
1391 return QualType();
1392
1393 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1394}
1395
John McCall424cec92011-01-19 06:33:43 +00001396QualType
1397ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001398 // FIXME: Check for C++0x support in "to" context.
1399 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1400 if (ToPointeeType.isNull())
1401 return QualType();
1402
1403 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1404}
1405
John McCall424cec92011-01-19 06:33:43 +00001406QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001407 // FIXME: Check for C++ support in "to" context.
1408 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1409 if (ToPointeeType.isNull())
1410 return QualType();
1411
1412 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1413 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1414 ClassType.getTypePtr());
1415}
1416
John McCall424cec92011-01-19 06:33:43 +00001417QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001418 QualType ToElementType = Importer.Import(T->getElementType());
1419 if (ToElementType.isNull())
1420 return QualType();
1421
1422 return Importer.getToContext().getConstantArrayType(ToElementType,
1423 T->getSize(),
1424 T->getSizeModifier(),
1425 T->getIndexTypeCVRQualifiers());
1426}
1427
John McCall424cec92011-01-19 06:33:43 +00001428QualType
1429ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430 QualType ToElementType = Importer.Import(T->getElementType());
1431 if (ToElementType.isNull())
1432 return QualType();
1433
1434 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1435 T->getSizeModifier(),
1436 T->getIndexTypeCVRQualifiers());
1437}
1438
John McCall424cec92011-01-19 06:33:43 +00001439QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001440 QualType ToElementType = Importer.Import(T->getElementType());
1441 if (ToElementType.isNull())
1442 return QualType();
1443
1444 Expr *Size = Importer.Import(T->getSizeExpr());
1445 if (!Size)
1446 return QualType();
1447
1448 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1449 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1450 T->getSizeModifier(),
1451 T->getIndexTypeCVRQualifiers(),
1452 Brackets);
1453}
1454
John McCall424cec92011-01-19 06:33:43 +00001455QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001456 QualType ToElementType = Importer.Import(T->getElementType());
1457 if (ToElementType.isNull())
1458 return QualType();
1459
1460 return Importer.getToContext().getVectorType(ToElementType,
1461 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001462 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001463}
1464
John McCall424cec92011-01-19 06:33:43 +00001465QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001466 QualType ToElementType = Importer.Import(T->getElementType());
1467 if (ToElementType.isNull())
1468 return QualType();
1469
1470 return Importer.getToContext().getExtVectorType(ToElementType,
1471 T->getNumElements());
1472}
1473
John McCall424cec92011-01-19 06:33:43 +00001474QualType
1475ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001476 // FIXME: What happens if we're importing a function without a prototype
1477 // into C++? Should we make it variadic?
1478 QualType ToResultType = Importer.Import(T->getResultType());
1479 if (ToResultType.isNull())
1480 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001481
Douglas Gregor96e578d2010-02-05 17:54:41 +00001482 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001483 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001484}
1485
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001486QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001487 QualType ToResultType = Importer.Import(T->getResultType());
1488 if (ToResultType.isNull())
1489 return QualType();
1490
1491 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001492 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1494 AEnd = T->arg_type_end();
1495 A != AEnd; ++A) {
1496 QualType ArgType = Importer.Import(*A);
1497 if (ArgType.isNull())
1498 return QualType();
1499 ArgTypes.push_back(ArgType);
1500 }
1501
1502 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001503 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1505 EEnd = T->exception_end();
1506 E != EEnd; ++E) {
1507 QualType ExceptionType = Importer.Import(*E);
1508 if (ExceptionType.isNull())
1509 return QualType();
1510 ExceptionTypes.push_back(ExceptionType);
1511 }
John McCalldb40c7f2010-12-14 08:05:40 +00001512
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001513 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1514 FunctionProtoType::ExtProtoInfo ToEPI;
1515
1516 ToEPI.ExtInfo = FromEPI.ExtInfo;
1517 ToEPI.Variadic = FromEPI.Variadic;
1518 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1519 ToEPI.TypeQuals = FromEPI.TypeQuals;
1520 ToEPI.RefQualifier = FromEPI.RefQualifier;
1521 ToEPI.NumExceptions = ExceptionTypes.size();
1522 ToEPI.Exceptions = ExceptionTypes.data();
1523 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001524 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1525 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1526 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001527 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001528 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001529 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001530
Douglas Gregor96e578d2010-02-05 17:54:41 +00001531 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001532 ArgTypes.size(), ToEPI);
1533}
1534
Sean Callananda6df8a2011-08-11 16:56:07 +00001535QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1536 QualType ToInnerType = Importer.Import(T->getInnerType());
1537 if (ToInnerType.isNull())
1538 return QualType();
1539
1540 return Importer.getToContext().getParenType(ToInnerType);
1541}
1542
John McCall424cec92011-01-19 06:33:43 +00001543QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001544 TypedefNameDecl *ToDecl
1545 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001546 if (!ToDecl)
1547 return QualType();
1548
1549 return Importer.getToContext().getTypeDeclType(ToDecl);
1550}
1551
John McCall424cec92011-01-19 06:33:43 +00001552QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1554 if (!ToExpr)
1555 return QualType();
1556
1557 return Importer.getToContext().getTypeOfExprType(ToExpr);
1558}
1559
John McCall424cec92011-01-19 06:33:43 +00001560QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001561 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1562 if (ToUnderlyingType.isNull())
1563 return QualType();
1564
1565 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1566}
1567
John McCall424cec92011-01-19 06:33:43 +00001568QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001569 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001570 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1571 if (!ToExpr)
1572 return QualType();
1573
Douglas Gregor81495f32012-02-12 18:42:33 +00001574 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1575 if (UnderlyingType.isNull())
1576 return QualType();
1577
1578 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001579}
1580
Alexis Hunte852b102011-05-24 22:41:36 +00001581QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1582 QualType ToBaseType = Importer.Import(T->getBaseType());
1583 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1584 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1585 return QualType();
1586
1587 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1588 ToUnderlyingType,
1589 T->getUTTKind());
1590}
1591
Richard Smith30482bc2011-02-20 03:19:35 +00001592QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1593 // FIXME: Make sure that the "to" context supports C++0x!
1594 QualType FromDeduced = T->getDeducedType();
1595 QualType ToDeduced;
1596 if (!FromDeduced.isNull()) {
1597 ToDeduced = Importer.Import(FromDeduced);
1598 if (ToDeduced.isNull())
1599 return QualType();
1600 }
1601
1602 return Importer.getToContext().getAutoType(ToDeduced);
1603}
1604
John McCall424cec92011-01-19 06:33:43 +00001605QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606 RecordDecl *ToDecl
1607 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1608 if (!ToDecl)
1609 return QualType();
1610
1611 return Importer.getToContext().getTagDeclType(ToDecl);
1612}
1613
John McCall424cec92011-01-19 06:33:43 +00001614QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001615 EnumDecl *ToDecl
1616 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1617 if (!ToDecl)
1618 return QualType();
1619
1620 return Importer.getToContext().getTagDeclType(ToDecl);
1621}
1622
Douglas Gregore2e50d332010-12-01 01:36:18 +00001623QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001624 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001625 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1626 if (ToTemplate.isNull())
1627 return QualType();
1628
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001629 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001630 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1631 return QualType();
1632
1633 QualType ToCanonType;
1634 if (!QualType(T, 0).isCanonical()) {
1635 QualType FromCanonType
1636 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1637 ToCanonType =Importer.Import(FromCanonType);
1638 if (ToCanonType.isNull())
1639 return QualType();
1640 }
1641 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1642 ToTemplateArgs.data(),
1643 ToTemplateArgs.size(),
1644 ToCanonType);
1645}
1646
John McCall424cec92011-01-19 06:33:43 +00001647QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001648 NestedNameSpecifier *ToQualifier = 0;
1649 // Note: the qualifier in an ElaboratedType is optional.
1650 if (T->getQualifier()) {
1651 ToQualifier = Importer.Import(T->getQualifier());
1652 if (!ToQualifier)
1653 return QualType();
1654 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001655
1656 QualType ToNamedType = Importer.Import(T->getNamedType());
1657 if (ToNamedType.isNull())
1658 return QualType();
1659
Abramo Bagnara6150c882010-05-11 21:36:43 +00001660 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1661 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001662}
1663
John McCall424cec92011-01-19 06:33:43 +00001664QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001665 ObjCInterfaceDecl *Class
1666 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1667 if (!Class)
1668 return QualType();
1669
John McCall8b07ec22010-05-15 11:32:37 +00001670 return Importer.getToContext().getObjCInterfaceType(Class);
1671}
1672
John McCall424cec92011-01-19 06:33:43 +00001673QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001674 QualType ToBaseType = Importer.Import(T->getBaseType());
1675 if (ToBaseType.isNull())
1676 return QualType();
1677
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001678 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001679 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001680 PEnd = T->qual_end();
1681 P != PEnd; ++P) {
1682 ObjCProtocolDecl *Protocol
1683 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1684 if (!Protocol)
1685 return QualType();
1686 Protocols.push_back(Protocol);
1687 }
1688
John McCall8b07ec22010-05-15 11:32:37 +00001689 return Importer.getToContext().getObjCObjectType(ToBaseType,
1690 Protocols.data(),
1691 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001692}
1693
John McCall424cec92011-01-19 06:33:43 +00001694QualType
1695ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001696 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1697 if (ToPointeeType.isNull())
1698 return QualType();
1699
John McCall8b07ec22010-05-15 11:32:37 +00001700 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001701}
1702
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001703//----------------------------------------------------------------------------
1704// Import Declarations
1705//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001706bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1707 DeclContext *&LexicalDC,
1708 DeclarationName &Name,
1709 SourceLocation &Loc) {
1710 // Import the context of this declaration.
1711 DC = Importer.ImportContext(D->getDeclContext());
1712 if (!DC)
1713 return true;
1714
1715 LexicalDC = DC;
1716 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1717 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1718 if (!LexicalDC)
1719 return true;
1720 }
1721
1722 // Import the name of this declaration.
1723 Name = Importer.Import(D->getDeclName());
1724 if (D->getDeclName() && !Name)
1725 return true;
1726
1727 // Import the location of this declaration.
1728 Loc = Importer.Import(D->getLocation());
1729 return false;
1730}
1731
Douglas Gregord451ea92011-07-29 23:31:30 +00001732void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1733 if (!FromD)
1734 return;
1735
1736 if (!ToD) {
1737 ToD = Importer.Import(FromD);
1738 if (!ToD)
1739 return;
1740 }
1741
1742 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1743 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1744 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1745 ImportDefinition(FromRecord, ToRecord);
1746 }
1747 }
1748 return;
1749 }
1750
1751 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1752 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1753 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1754 ImportDefinition(FromEnum, ToEnum);
1755 }
1756 }
1757 return;
1758 }
1759}
1760
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001761void
1762ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1763 DeclarationNameInfo& To) {
1764 // NOTE: To.Name and To.Loc are already imported.
1765 // We only have to import To.LocInfo.
1766 switch (To.getName().getNameKind()) {
1767 case DeclarationName::Identifier:
1768 case DeclarationName::ObjCZeroArgSelector:
1769 case DeclarationName::ObjCOneArgSelector:
1770 case DeclarationName::ObjCMultiArgSelector:
1771 case DeclarationName::CXXUsingDirective:
1772 return;
1773
1774 case DeclarationName::CXXOperatorName: {
1775 SourceRange Range = From.getCXXOperatorNameRange();
1776 To.setCXXOperatorNameRange(Importer.Import(Range));
1777 return;
1778 }
1779 case DeclarationName::CXXLiteralOperatorName: {
1780 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1781 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1782 return;
1783 }
1784 case DeclarationName::CXXConstructorName:
1785 case DeclarationName::CXXDestructorName:
1786 case DeclarationName::CXXConversionFunctionName: {
1787 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1788 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1789 return;
1790 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001791 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001792 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001793}
1794
Douglas Gregor2e15c842012-02-01 21:00:38 +00001795void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001796 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001797 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001798 return;
1799 }
1800
Douglas Gregor968d6332010-02-21 18:24:45 +00001801 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1802 FromEnd = FromDC->decls_end();
1803 From != FromEnd;
1804 ++From)
1805 Importer.Import(*From);
1806}
1807
Douglas Gregord451ea92011-07-29 23:31:30 +00001808bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001809 ImportDefinitionKind Kind) {
1810 if (To->getDefinition() || To->isBeingDefined()) {
1811 if (Kind == IDK_Everything)
1812 ImportDeclContext(From, /*ForceImport=*/true);
1813
Douglas Gregore2e50d332010-12-01 01:36:18 +00001814 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001815 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001816
1817 To->startDefinition();
1818
1819 // Add base classes.
1820 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1821 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001822
1823 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1824 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1825 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1826 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1827 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1828 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1829 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1830 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1831 ToData.Aggregate = FromData.Aggregate;
1832 ToData.PlainOldData = FromData.PlainOldData;
1833 ToData.Empty = FromData.Empty;
1834 ToData.Polymorphic = FromData.Polymorphic;
1835 ToData.Abstract = FromData.Abstract;
1836 ToData.IsStandardLayout = FromData.IsStandardLayout;
1837 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1838 ToData.HasPrivateFields = FromData.HasPrivateFields;
1839 ToData.HasProtectedFields = FromData.HasProtectedFields;
1840 ToData.HasPublicFields = FromData.HasPublicFields;
1841 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001842 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001843 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001844 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1845 ToData.HasConstexprNonCopyMoveConstructor
1846 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001847 ToData.DefaultedDefaultConstructorIsConstexpr
1848 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001849 ToData.HasConstexprDefaultConstructor
1850 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001851 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1852 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1853 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1854 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1855 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001856 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001857 ToData.HasNonLiteralTypeFieldsOrBases
1858 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001859 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001860 ToData.UserProvidedDefaultConstructor
1861 = FromData.UserProvidedDefaultConstructor;
1862 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1863 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1864 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1865 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1866 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1867 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1868 ToData.FailedImplicitMoveConstructor
1869 = FromData.FailedImplicitMoveConstructor;
1870 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001871 ToData.IsLambda = FromData.IsLambda;
1872
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001873 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001874 for (CXXRecordDecl::base_class_iterator
1875 Base1 = FromCXX->bases_begin(),
1876 FromBaseEnd = FromCXX->bases_end();
1877 Base1 != FromBaseEnd;
1878 ++Base1) {
1879 QualType T = Importer.Import(Base1->getType());
1880 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001881 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001882
1883 SourceLocation EllipsisLoc;
1884 if (Base1->isPackExpansion())
1885 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001886
1887 // Ensure that we have a definition for the base.
1888 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1889
Douglas Gregore2e50d332010-12-01 01:36:18 +00001890 Bases.push_back(
1891 new (Importer.getToContext())
1892 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1893 Base1->isVirtual(),
1894 Base1->isBaseOfClass(),
1895 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001896 Importer.Import(Base1->getTypeSourceInfo()),
1897 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898 }
1899 if (!Bases.empty())
1900 ToCXX->setBases(Bases.data(), Bases.size());
1901 }
1902
Douglas Gregor2e15c842012-02-01 21:00:38 +00001903 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001904 ImportDeclContext(From, /*ForceImport=*/true);
1905
Douglas Gregore2e50d332010-12-01 01:36:18 +00001906 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001907 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001908}
1909
Douglas Gregord451ea92011-07-29 23:31:30 +00001910bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001911 ImportDefinitionKind Kind) {
1912 if (To->getDefinition() || To->isBeingDefined()) {
1913 if (Kind == IDK_Everything)
1914 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001915 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001916 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001917
1918 To->startDefinition();
1919
1920 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1921 if (T.isNull())
1922 return true;
1923
1924 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1925 if (ToPromotionType.isNull())
1926 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001927
1928 if (shouldForceImportDeclContext(Kind))
1929 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001930
1931 // FIXME: we might need to merge the number of positive or negative bits
1932 // if the enumerator lists don't match.
1933 To->completeDefinition(T, ToPromotionType,
1934 From->getNumPositiveBits(),
1935 From->getNumNegativeBits());
1936 return false;
1937}
1938
Douglas Gregora082a492010-11-30 19:14:50 +00001939TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1940 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001941 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001942 ToParams.reserve(Params->size());
1943 for (TemplateParameterList::iterator P = Params->begin(),
1944 PEnd = Params->end();
1945 P != PEnd; ++P) {
1946 Decl *To = Importer.Import(*P);
1947 if (!To)
1948 return 0;
1949
1950 ToParams.push_back(cast<NamedDecl>(To));
1951 }
1952
1953 return TemplateParameterList::Create(Importer.getToContext(),
1954 Importer.Import(Params->getTemplateLoc()),
1955 Importer.Import(Params->getLAngleLoc()),
1956 ToParams.data(), ToParams.size(),
1957 Importer.Import(Params->getRAngleLoc()));
1958}
1959
Douglas Gregore2e50d332010-12-01 01:36:18 +00001960TemplateArgument
1961ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1962 switch (From.getKind()) {
1963 case TemplateArgument::Null:
1964 return TemplateArgument();
1965
1966 case TemplateArgument::Type: {
1967 QualType ToType = Importer.Import(From.getAsType());
1968 if (ToType.isNull())
1969 return TemplateArgument();
1970 return TemplateArgument(ToType);
1971 }
1972
1973 case TemplateArgument::Integral: {
1974 QualType ToType = Importer.Import(From.getIntegralType());
1975 if (ToType.isNull())
1976 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001977 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001978 }
1979
Eli Friedmanb826a002012-09-26 02:36:12 +00001980 case TemplateArgument::Declaration: {
1981 ValueDecl *FromD = From.getAsDecl();
1982 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
1983 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001984 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00001985 }
1986
1987 case TemplateArgument::NullPtr: {
1988 QualType ToType = Importer.Import(From.getNullPtrType());
1989 if (ToType.isNull())
1990 return TemplateArgument();
1991 return TemplateArgument(ToType, /*isNullPtr*/true);
1992 }
1993
Douglas Gregore2e50d332010-12-01 01:36:18 +00001994 case TemplateArgument::Template: {
1995 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1996 if (ToTemplate.isNull())
1997 return TemplateArgument();
1998
1999 return TemplateArgument(ToTemplate);
2000 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002001
2002 case TemplateArgument::TemplateExpansion: {
2003 TemplateName ToTemplate
2004 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2005 if (ToTemplate.isNull())
2006 return TemplateArgument();
2007
Douglas Gregore1d60df2011-01-14 23:41:42 +00002008 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002009 }
2010
Douglas Gregore2e50d332010-12-01 01:36:18 +00002011 case TemplateArgument::Expression:
2012 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2013 return TemplateArgument(ToExpr);
2014 return TemplateArgument();
2015
2016 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002017 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002018 ToPack.reserve(From.pack_size());
2019 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2020 return TemplateArgument();
2021
2022 TemplateArgument *ToArgs
2023 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2024 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2025 return TemplateArgument(ToArgs, ToPack.size());
2026 }
2027 }
2028
2029 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002030}
2031
2032bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2033 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002034 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002035 for (unsigned I = 0; I != NumFromArgs; ++I) {
2036 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2037 if (To.isNull() && !FromArgs[I].isNull())
2038 return true;
2039
2040 ToArgs.push_back(To);
2041 }
2042
2043 return false;
2044}
2045
Douglas Gregor5c73e912010-02-11 00:48:18 +00002046bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002047 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002048 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002049 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002050 Importer.getNonEquivalentDecls(),
2051 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002052 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002053}
2054
Douglas Gregor98c10182010-02-12 22:17:39 +00002055bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002056 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002057 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002058 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002059 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002060}
2061
Douglas Gregora082a492010-11-30 19:14:50 +00002062bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2063 ClassTemplateDecl *To) {
2064 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2065 Importer.getToContext(),
2066 Importer.getNonEquivalentDecls());
2067 return Ctx.IsStructurallyEquivalent(From, To);
2068}
2069
Douglas Gregore4c83e42010-02-09 22:48:33 +00002070Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002071 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002072 << D->getDeclKindName();
2073 return 0;
2074}
2075
Sean Callanan65198272011-11-17 23:20:56 +00002076Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2077 TranslationUnitDecl *ToD =
2078 Importer.getToContext().getTranslationUnitDecl();
2079
2080 Importer.Imported(D, ToD);
2081
2082 return ToD;
2083}
2084
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002085Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2086 // Import the major distinguishing characteristics of this namespace.
2087 DeclContext *DC, *LexicalDC;
2088 DeclarationName Name;
2089 SourceLocation Loc;
2090 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2091 return 0;
2092
2093 NamespaceDecl *MergeWithNamespace = 0;
2094 if (!Name) {
2095 // This is an anonymous namespace. Adopt an existing anonymous
2096 // namespace if we can.
2097 // FIXME: Not testable.
2098 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2099 MergeWithNamespace = TU->getAnonymousNamespace();
2100 else
2101 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2102 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002103 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002104 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2105 DC->localUncachedLookup(Name, FoundDecls);
2106 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2107 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002108 continue;
2109
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002110 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002111 MergeWithNamespace = FoundNS;
2112 ConflictingDecls.clear();
2113 break;
2114 }
2115
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002116 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002117 }
2118
2119 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002120 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002121 ConflictingDecls.data(),
2122 ConflictingDecls.size());
2123 }
2124 }
2125
2126 // Create the "to" namespace, if needed.
2127 NamespaceDecl *ToNamespace = MergeWithNamespace;
2128 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002129 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002130 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002131 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002132 Loc, Name.getAsIdentifierInfo(),
2133 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002134 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002135 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002136
2137 // If this is an anonymous namespace, register it as the anonymous
2138 // namespace within its context.
2139 if (!Name) {
2140 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2141 TU->setAnonymousNamespace(ToNamespace);
2142 else
2143 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2144 }
2145 }
2146 Importer.Imported(D, ToNamespace);
2147
2148 ImportDeclContext(D);
2149
2150 return ToNamespace;
2151}
2152
Richard Smithdda56e42011-04-15 14:24:37 +00002153Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002154 // Import the major distinguishing characteristics of this typedef.
2155 DeclContext *DC, *LexicalDC;
2156 DeclarationName Name;
2157 SourceLocation Loc;
2158 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2159 return 0;
2160
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002161 // If this typedef is not in block scope, determine whether we've
2162 // seen a typedef with the same name (that we can merge with) or any
2163 // other entity by that name (which name lookup could conflict with).
2164 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002165 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002166 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002167 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2168 DC->localUncachedLookup(Name, FoundDecls);
2169 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2170 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002171 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002172 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002173 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002174 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2175 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002176 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002177 }
2178
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002179 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002180 }
2181
2182 if (!ConflictingDecls.empty()) {
2183 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2184 ConflictingDecls.data(),
2185 ConflictingDecls.size());
2186 if (!Name)
2187 return 0;
2188 }
2189 }
2190
Douglas Gregorb4964f72010-02-15 23:54:17 +00002191 // Import the underlying type of this typedef;
2192 QualType T = Importer.Import(D->getUnderlyingType());
2193 if (T.isNull())
2194 return 0;
2195
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002196 // Create the new typedef node.
2197 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002198 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002199 TypedefNameDecl *ToTypedef;
2200 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002201 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2202 StartL, Loc,
2203 Name.getAsIdentifierInfo(),
2204 TInfo);
2205 else
Richard Smithdda56e42011-04-15 14:24:37 +00002206 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2207 StartL, Loc,
2208 Name.getAsIdentifierInfo(),
2209 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002210
Douglas Gregordd483172010-02-22 17:42:47 +00002211 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002212 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002213 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002214 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002215
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002216 return ToTypedef;
2217}
2218
Richard Smithdda56e42011-04-15 14:24:37 +00002219Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2220 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2221}
2222
2223Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2224 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2225}
2226
Douglas Gregor98c10182010-02-12 22:17:39 +00002227Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2228 // Import the major distinguishing characteristics of this enum.
2229 DeclContext *DC, *LexicalDC;
2230 DeclarationName Name;
2231 SourceLocation Loc;
2232 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2233 return 0;
2234
2235 // Figure out what enum name we're looking for.
2236 unsigned IDNS = Decl::IDNS_Tag;
2237 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002238 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2239 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002240 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002241 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002242 IDNS |= Decl::IDNS_Ordinary;
2243
2244 // We may already have an enum of the same name; try to find and match it.
2245 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002246 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002247 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2248 DC->localUncachedLookup(SearchName, FoundDecls);
2249 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2250 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002251 continue;
2252
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002253 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002254 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002255 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2256 Found = Tag->getDecl();
2257 }
2258
2259 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002260 if (IsStructuralMatch(D, FoundEnum))
2261 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002262 }
2263
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002264 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002265 }
2266
2267 if (!ConflictingDecls.empty()) {
2268 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2269 ConflictingDecls.data(),
2270 ConflictingDecls.size());
2271 }
2272 }
2273
2274 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002275 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2276 Importer.Import(D->getLocStart()),
2277 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002278 D->isScoped(), D->isScopedUsingClassTag(),
2279 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002280 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002281 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002282 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002283 D2->setLexicalDeclContext(LexicalDC);
2284 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002285 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002286
2287 // Import the integer type.
2288 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2289 if (ToIntegerType.isNull())
2290 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002291 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002292
2293 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002294 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002295 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002296
Douglas Gregor3996e242010-02-15 22:01:00 +00002297 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002298}
2299
Douglas Gregor5c73e912010-02-11 00:48:18 +00002300Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2301 // If this record has a definition in the translation unit we're coming from,
2302 // but this particular declaration is not that definition, import the
2303 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002304 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002305 if (Definition && Definition != D) {
2306 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002307 if (!ImportedDef)
2308 return 0;
2309
2310 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002311 }
2312
2313 // Import the major distinguishing characteristics of this record.
2314 DeclContext *DC, *LexicalDC;
2315 DeclarationName Name;
2316 SourceLocation Loc;
2317 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2318 return 0;
2319
2320 // Figure out what structure name we're looking for.
2321 unsigned IDNS = Decl::IDNS_Tag;
2322 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002323 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2324 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002325 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002326 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002327 IDNS |= Decl::IDNS_Ordinary;
2328
2329 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002330 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002331 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002332 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002333 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2334 DC->localUncachedLookup(SearchName, FoundDecls);
2335 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2336 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002337 continue;
2338
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002339 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002340 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002341 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2342 Found = Tag->getDecl();
2343 }
2344
2345 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002346 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002347 if ((SearchName && !D->isCompleteDefinition())
2348 || (D->isCompleteDefinition() &&
2349 D->isAnonymousStructOrUnion()
2350 == FoundDef->isAnonymousStructOrUnion() &&
2351 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002352 // The record types structurally match, or the "from" translation
2353 // unit only had a forward declaration anyway; call it the same
2354 // function.
2355 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002356 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002357 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002358 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002359 // We have a forward declaration of this type, so adopt that forward
2360 // declaration rather than building a new one.
2361 AdoptDecl = FoundRecord;
2362 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002363 } else if (!SearchName) {
2364 continue;
2365 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002366 }
2367
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002368 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002369 }
2370
Douglas Gregordd6006f2012-07-17 21:16:27 +00002371 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002372 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2373 ConflictingDecls.data(),
2374 ConflictingDecls.size());
2375 }
2376 }
2377
2378 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002379 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002380 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002381 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002382 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002383 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002384 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002385 DC, StartLoc, Loc,
2386 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002387 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002388 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002389 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002390 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002391 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002392 }
Douglas Gregor14454802011-02-25 02:25:35 +00002393
2394 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002395 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002396 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002397 if (D->isAnonymousStructOrUnion())
2398 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002399 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002400
Douglas Gregor3996e242010-02-15 22:01:00 +00002401 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002402
Douglas Gregor95d82832012-01-24 18:36:04 +00002403 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002404 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002405
Douglas Gregor3996e242010-02-15 22:01:00 +00002406 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002407}
2408
Douglas Gregor98c10182010-02-12 22:17:39 +00002409Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2410 // Import the major distinguishing characteristics of this enumerator.
2411 DeclContext *DC, *LexicalDC;
2412 DeclarationName Name;
2413 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002414 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002415 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002416
2417 QualType T = Importer.Import(D->getType());
2418 if (T.isNull())
2419 return 0;
2420
Douglas Gregor98c10182010-02-12 22:17:39 +00002421 // Determine whether there are any other declarations with the same name and
2422 // in the same context.
2423 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002424 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002425 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002426 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2427 DC->localUncachedLookup(Name, FoundDecls);
2428 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2429 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002430 continue;
2431
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002432 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 }
2434
2435 if (!ConflictingDecls.empty()) {
2436 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2437 ConflictingDecls.data(),
2438 ConflictingDecls.size());
2439 if (!Name)
2440 return 0;
2441 }
2442 }
2443
2444 Expr *Init = Importer.Import(D->getInitExpr());
2445 if (D->getInitExpr() && !Init)
2446 return 0;
2447
2448 EnumConstantDecl *ToEnumerator
2449 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2450 Name.getAsIdentifierInfo(), T,
2451 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002452 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002453 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002454 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002455 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002456 return ToEnumerator;
2457}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002458
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002459Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2460 // Import the major distinguishing characteristics of this function.
2461 DeclContext *DC, *LexicalDC;
2462 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002463 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002464 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002465 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002466
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002467 // Try to find a function in our own ("to") context with the same name, same
2468 // type, and in the same context as the function we're importing.
2469 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002470 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002471 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002472 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2473 DC->localUncachedLookup(Name, FoundDecls);
2474 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2475 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002476 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002477
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002478 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002479 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2480 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002481 if (Importer.IsStructurallyEquivalent(D->getType(),
2482 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002483 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002484 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002485 }
2486
2487 // FIXME: Check for overloading more carefully, e.g., by boosting
2488 // Sema::IsOverload out to the AST library.
2489
2490 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002491 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002492 continue;
2493
2494 // Complain about inconsistent function types.
2495 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002496 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002497 Importer.ToDiag(FoundFunction->getLocation(),
2498 diag::note_odr_value_here)
2499 << FoundFunction->getType();
2500 }
2501 }
2502
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002503 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002504 }
2505
2506 if (!ConflictingDecls.empty()) {
2507 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2508 ConflictingDecls.data(),
2509 ConflictingDecls.size());
2510 if (!Name)
2511 return 0;
2512 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002513 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002514
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002515 DeclarationNameInfo NameInfo(Name, Loc);
2516 // Import additional name location/type info.
2517 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2518
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002519 QualType FromTy = D->getType();
2520 bool usedDifferentExceptionSpec = false;
2521
2522 if (const FunctionProtoType *
2523 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2524 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2525 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2526 // FunctionDecl that we are importing the FunctionProtoType for.
2527 // To avoid an infinite recursion when importing, create the FunctionDecl
2528 // with a simplified function type and update it afterwards.
2529 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2530 FromEPI.NoexceptExpr) {
2531 FunctionProtoType::ExtProtoInfo DefaultEPI;
2532 FromTy = Importer.getFromContext().getFunctionType(
2533 FromFPT->getResultType(),
2534 FromFPT->arg_type_begin(),
2535 FromFPT->arg_type_end() - FromFPT->arg_type_begin(),
2536 DefaultEPI);
2537 usedDifferentExceptionSpec = true;
2538 }
2539 }
2540
Douglas Gregorb4964f72010-02-15 23:54:17 +00002541 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002542 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002543 if (T.isNull())
2544 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002545
2546 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002547 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002548 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2549 P != PEnd; ++P) {
2550 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2551 if (!ToP)
2552 return 0;
2553
2554 Parameters.push_back(ToP);
2555 }
2556
2557 // Create the imported function.
2558 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002559 FunctionDecl *ToFunction = 0;
2560 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2561 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2562 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002563 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002564 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002565 FromConstructor->isExplicit(),
2566 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002567 D->isImplicit(),
2568 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002569 } else if (isa<CXXDestructorDecl>(D)) {
2570 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2571 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002572 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002573 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002574 D->isInlineSpecified(),
2575 D->isImplicit());
2576 } else if (CXXConversionDecl *FromConversion
2577 = dyn_cast<CXXConversionDecl>(D)) {
2578 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2579 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002580 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002581 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002582 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002583 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002584 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002585 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002586 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2587 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2588 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002589 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002590 NameInfo, T, TInfo,
2591 Method->isStatic(),
2592 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002593 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002594 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002595 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002596 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002597 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002598 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002599 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002600 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002601 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002602 D->hasWrittenPrototype(),
2603 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002604 }
John McCall3e11ebe2010-03-15 10:12:16 +00002605
2606 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002607 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002608 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002609 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002610 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2611 ToFunction->setTrivial(D->isTrivial());
2612 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002613 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002614
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002615 // Set the parameters.
2616 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002617 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002618 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002619 }
David Blaikie9c70e042011-09-21 18:16:56 +00002620 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002621
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002622 if (usedDifferentExceptionSpec) {
2623 // Update FunctionProtoType::ExtProtoInfo.
2624 QualType T = Importer.Import(D->getType());
2625 if (T.isNull())
2626 return 0;
2627 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002628 }
2629
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002630 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002631
2632 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002633 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002634
Douglas Gregor43f54792010-02-17 02:12:47 +00002635 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002636}
2637
Douglas Gregor00eace12010-02-21 18:29:16 +00002638Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2639 return VisitFunctionDecl(D);
2640}
2641
2642Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2643 return VisitCXXMethodDecl(D);
2644}
2645
2646Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2647 return VisitCXXMethodDecl(D);
2648}
2649
2650Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2651 return VisitCXXMethodDecl(D);
2652}
2653
Douglas Gregor5c73e912010-02-11 00:48:18 +00002654Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2655 // Import the major distinguishing characteristics of a variable.
2656 DeclContext *DC, *LexicalDC;
2657 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002658 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002659 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2660 return 0;
2661
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002662 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002663 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2664 DC->localUncachedLookup(Name, FoundDecls);
2665 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2666 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002667 if (Importer.IsStructurallyEquivalent(D->getType(),
2668 FoundField->getType())) {
2669 Importer.Imported(D, FoundField);
2670 return FoundField;
2671 }
2672
2673 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2674 << Name << D->getType() << FoundField->getType();
2675 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2676 << FoundField->getType();
2677 return 0;
2678 }
2679 }
2680
Douglas Gregorb4964f72010-02-15 23:54:17 +00002681 // Import the type.
2682 QualType T = Importer.Import(D->getType());
2683 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002684 return 0;
2685
2686 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2687 Expr *BitWidth = Importer.Import(D->getBitWidth());
2688 if (!BitWidth && D->getBitWidth())
2689 return 0;
2690
Abramo Bagnaradff19302011-03-08 08:55:46 +00002691 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2692 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002693 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002694 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002695 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002696 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002697 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002698 if (ToField->hasInClassInitializer())
2699 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002700 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002701 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002702 return ToField;
2703}
2704
Francois Pichet783dd6e2010-11-21 06:08:52 +00002705Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2706 // Import the major distinguishing characteristics of a variable.
2707 DeclContext *DC, *LexicalDC;
2708 DeclarationName Name;
2709 SourceLocation Loc;
2710 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2711 return 0;
2712
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002713 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002714 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2715 DC->localUncachedLookup(Name, FoundDecls);
2716 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002717 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002718 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002719 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002720 FoundField->getType(),
2721 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002722 Importer.Imported(D, FoundField);
2723 return FoundField;
2724 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002725
2726 // If there are more anonymous fields to check, continue.
2727 if (!Name && I < N-1)
2728 continue;
2729
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002730 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2731 << Name << D->getType() << FoundField->getType();
2732 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2733 << FoundField->getType();
2734 return 0;
2735 }
2736 }
2737
Francois Pichet783dd6e2010-11-21 06:08:52 +00002738 // Import the type.
2739 QualType T = Importer.Import(D->getType());
2740 if (T.isNull())
2741 return 0;
2742
2743 NamedDecl **NamedChain =
2744 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2745
2746 unsigned i = 0;
2747 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2748 PE = D->chain_end(); PI != PE; ++PI) {
2749 Decl* D = Importer.Import(*PI);
2750 if (!D)
2751 return 0;
2752 NamedChain[i++] = cast<NamedDecl>(D);
2753 }
2754
2755 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2756 Importer.getToContext(), DC,
2757 Loc, Name.getAsIdentifierInfo(), T,
2758 NamedChain, D->getChainingSize());
2759 ToIndirectField->setAccess(D->getAccess());
2760 ToIndirectField->setLexicalDeclContext(LexicalDC);
2761 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002762 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002763 return ToIndirectField;
2764}
2765
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002766Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2767 // Import the major distinguishing characteristics of an ivar.
2768 DeclContext *DC, *LexicalDC;
2769 DeclarationName Name;
2770 SourceLocation Loc;
2771 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2772 return 0;
2773
2774 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002775 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2776 DC->localUncachedLookup(Name, FoundDecls);
2777 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2778 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002779 if (Importer.IsStructurallyEquivalent(D->getType(),
2780 FoundIvar->getType())) {
2781 Importer.Imported(D, FoundIvar);
2782 return FoundIvar;
2783 }
2784
2785 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2786 << Name << D->getType() << FoundIvar->getType();
2787 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2788 << FoundIvar->getType();
2789 return 0;
2790 }
2791 }
2792
2793 // Import the type.
2794 QualType T = Importer.Import(D->getType());
2795 if (T.isNull())
2796 return 0;
2797
2798 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2799 Expr *BitWidth = Importer.Import(D->getBitWidth());
2800 if (!BitWidth && D->getBitWidth())
2801 return 0;
2802
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002803 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2804 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002805 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002806 Loc, Name.getAsIdentifierInfo(),
2807 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002808 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002809 ToIvar->setLexicalDeclContext(LexicalDC);
2810 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002811 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002812 return ToIvar;
2813
2814}
2815
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002816Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2817 // Import the major distinguishing characteristics of a variable.
2818 DeclContext *DC, *LexicalDC;
2819 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002820 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002821 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002822 return 0;
2823
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002824 // Try to find a variable in our own ("to") context with the same name and
2825 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002826 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002827 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002828 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002829 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002830 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2831 DC->localUncachedLookup(Name, FoundDecls);
2832 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2833 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002834 continue;
2835
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002836 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002837 // We have found a variable that we may need to merge with. Check it.
2838 if (isExternalLinkage(FoundVar->getLinkage()) &&
2839 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002840 if (Importer.IsStructurallyEquivalent(D->getType(),
2841 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002842 MergeWithVar = FoundVar;
2843 break;
2844 }
2845
Douglas Gregor56521c52010-02-12 17:23:39 +00002846 const ArrayType *FoundArray
2847 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2848 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002849 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002850 if (FoundArray && TArray) {
2851 if (isa<IncompleteArrayType>(FoundArray) &&
2852 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002853 // Import the type.
2854 QualType T = Importer.Import(D->getType());
2855 if (T.isNull())
2856 return 0;
2857
Douglas Gregor56521c52010-02-12 17:23:39 +00002858 FoundVar->setType(T);
2859 MergeWithVar = FoundVar;
2860 break;
2861 } else if (isa<IncompleteArrayType>(TArray) &&
2862 isa<ConstantArrayType>(FoundArray)) {
2863 MergeWithVar = FoundVar;
2864 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002865 }
2866 }
2867
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002868 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002869 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002870 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2871 << FoundVar->getType();
2872 }
2873 }
2874
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002875 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002876 }
2877
2878 if (MergeWithVar) {
2879 // An equivalent variable with external linkage has been found. Link
2880 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002881 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002882
2883 if (VarDecl *DDef = D->getDefinition()) {
2884 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2885 Importer.ToDiag(ExistingDef->getLocation(),
2886 diag::err_odr_variable_multiple_def)
2887 << Name;
2888 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2889 } else {
2890 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002891 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002892 if (DDef->isInitKnownICE()) {
2893 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2894 Eval->CheckedICE = true;
2895 Eval->IsICE = DDef->isInitICE();
2896 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002897 }
2898 }
2899
2900 return MergeWithVar;
2901 }
2902
2903 if (!ConflictingDecls.empty()) {
2904 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2905 ConflictingDecls.data(),
2906 ConflictingDecls.size());
2907 if (!Name)
2908 return 0;
2909 }
2910 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002911
Douglas Gregorb4964f72010-02-15 23:54:17 +00002912 // Import the type.
2913 QualType T = Importer.Import(D->getType());
2914 if (T.isNull())
2915 return 0;
2916
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002917 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002918 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002919 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2920 Importer.Import(D->getInnerLocStart()),
2921 Loc, Name.getAsIdentifierInfo(),
2922 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002923 D->getStorageClass(),
2924 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002925 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002926 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002927 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002928 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002929 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002930
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002931 // Merge the initializer.
2932 // FIXME: Can we really import any initializer? Alternatively, we could force
2933 // ourselves to import every declaration of a variable and then only use
2934 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002935 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002936
2937 // FIXME: Other bits to merge?
2938
2939 return ToVar;
2940}
2941
Douglas Gregor8b228d72010-02-17 21:22:52 +00002942Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2943 // Parameters are created in the translation unit's context, then moved
2944 // into the function declaration's context afterward.
2945 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2946
2947 // Import the name of this declaration.
2948 DeclarationName Name = Importer.Import(D->getDeclName());
2949 if (D->getDeclName() && !Name)
2950 return 0;
2951
2952 // Import the location of this declaration.
2953 SourceLocation Loc = Importer.Import(D->getLocation());
2954
2955 // Import the parameter's type.
2956 QualType T = Importer.Import(D->getType());
2957 if (T.isNull())
2958 return 0;
2959
2960 // Create the imported parameter.
2961 ImplicitParamDecl *ToParm
2962 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2963 Loc, Name.getAsIdentifierInfo(),
2964 T);
2965 return Importer.Imported(D, ToParm);
2966}
2967
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002968Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2969 // Parameters are created in the translation unit's context, then moved
2970 // into the function declaration's context afterward.
2971 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2972
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002973 // Import the name of this declaration.
2974 DeclarationName Name = Importer.Import(D->getDeclName());
2975 if (D->getDeclName() && !Name)
2976 return 0;
2977
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002978 // Import the location of this declaration.
2979 SourceLocation Loc = Importer.Import(D->getLocation());
2980
2981 // Import the parameter's type.
2982 QualType T = Importer.Import(D->getType());
2983 if (T.isNull())
2984 return 0;
2985
2986 // Create the imported parameter.
2987 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2988 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002989 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002990 Loc, Name.getAsIdentifierInfo(),
2991 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002992 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002993 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002994 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002995 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002996}
2997
Douglas Gregor43f54792010-02-17 02:12:47 +00002998Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2999 // Import the major distinguishing characteristics of a method.
3000 DeclContext *DC, *LexicalDC;
3001 DeclarationName Name;
3002 SourceLocation Loc;
3003 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3004 return 0;
3005
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003006 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3007 DC->localUncachedLookup(Name, FoundDecls);
3008 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3009 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003010 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3011 continue;
3012
3013 // Check return types.
3014 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3015 FoundMethod->getResultType())) {
3016 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3017 << D->isInstanceMethod() << Name
3018 << D->getResultType() << FoundMethod->getResultType();
3019 Importer.ToDiag(FoundMethod->getLocation(),
3020 diag::note_odr_objc_method_here)
3021 << D->isInstanceMethod() << Name;
3022 return 0;
3023 }
3024
3025 // Check the number of parameters.
3026 if (D->param_size() != FoundMethod->param_size()) {
3027 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3028 << D->isInstanceMethod() << Name
3029 << D->param_size() << FoundMethod->param_size();
3030 Importer.ToDiag(FoundMethod->getLocation(),
3031 diag::note_odr_objc_method_here)
3032 << D->isInstanceMethod() << Name;
3033 return 0;
3034 }
3035
3036 // Check parameter types.
3037 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3038 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3039 P != PEnd; ++P, ++FoundP) {
3040 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3041 (*FoundP)->getType())) {
3042 Importer.FromDiag((*P)->getLocation(),
3043 diag::err_odr_objc_method_param_type_inconsistent)
3044 << D->isInstanceMethod() << Name
3045 << (*P)->getType() << (*FoundP)->getType();
3046 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3047 << (*FoundP)->getType();
3048 return 0;
3049 }
3050 }
3051
3052 // Check variadic/non-variadic.
3053 // Check the number of parameters.
3054 if (D->isVariadic() != FoundMethod->isVariadic()) {
3055 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3056 << D->isInstanceMethod() << Name;
3057 Importer.ToDiag(FoundMethod->getLocation(),
3058 diag::note_odr_objc_method_here)
3059 << D->isInstanceMethod() << Name;
3060 return 0;
3061 }
3062
3063 // FIXME: Any other bits we need to merge?
3064 return Importer.Imported(D, FoundMethod);
3065 }
3066 }
3067
3068 // Import the result type.
3069 QualType ResultTy = Importer.Import(D->getResultType());
3070 if (ResultTy.isNull())
3071 return 0;
3072
Douglas Gregor12852d92010-03-08 14:59:44 +00003073 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3074
Douglas Gregor43f54792010-02-17 02:12:47 +00003075 ObjCMethodDecl *ToMethod
3076 = ObjCMethodDecl::Create(Importer.getToContext(),
3077 Loc,
3078 Importer.Import(D->getLocEnd()),
3079 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003080 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003081 D->isInstanceMethod(),
3082 D->isVariadic(),
3083 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003084 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003085 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003086 D->getImplementationControl(),
3087 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003088
3089 // FIXME: When we decide to merge method definitions, we'll need to
3090 // deal with implicit parameters.
3091
3092 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003093 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003094 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3095 FromPEnd = D->param_end();
3096 FromP != FromPEnd;
3097 ++FromP) {
3098 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3099 if (!ToP)
3100 return 0;
3101
3102 ToParams.push_back(ToP);
3103 }
3104
3105 // Set the parameters.
3106 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3107 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003108 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003109 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003110 SmallVector<SourceLocation, 12> SelLocs;
3111 D->getSelectorLocs(SelLocs);
3112 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003113
3114 ToMethod->setLexicalDeclContext(LexicalDC);
3115 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003116 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003117 return ToMethod;
3118}
3119
Douglas Gregor84c51c32010-02-18 01:47:50 +00003120Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3121 // Import the major distinguishing characteristics of a category.
3122 DeclContext *DC, *LexicalDC;
3123 DeclarationName Name;
3124 SourceLocation Loc;
3125 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3126 return 0;
3127
3128 ObjCInterfaceDecl *ToInterface
3129 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3130 if (!ToInterface)
3131 return 0;
3132
3133 // Determine if we've already encountered this category.
3134 ObjCCategoryDecl *MergeWithCategory
3135 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3136 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3137 if (!ToCategory) {
3138 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003139 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003140 Loc,
3141 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003142 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003143 ToInterface,
3144 Importer.Import(D->getIvarLBraceLoc()),
3145 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003146 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003147 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003148 Importer.Imported(D, ToCategory);
3149
Douglas Gregor84c51c32010-02-18 01:47:50 +00003150 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003151 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3152 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003153 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3154 = D->protocol_loc_begin();
3155 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3156 FromProtoEnd = D->protocol_end();
3157 FromProto != FromProtoEnd;
3158 ++FromProto, ++FromProtoLoc) {
3159 ObjCProtocolDecl *ToProto
3160 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3161 if (!ToProto)
3162 return 0;
3163 Protocols.push_back(ToProto);
3164 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3165 }
3166
3167 // FIXME: If we're merging, make sure that the protocol list is the same.
3168 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3169 ProtocolLocs.data(), Importer.getToContext());
3170
3171 } else {
3172 Importer.Imported(D, ToCategory);
3173 }
3174
3175 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003176 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003177
3178 // If we have an implementation, import it as well.
3179 if (D->getImplementation()) {
3180 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003181 = cast_or_null<ObjCCategoryImplDecl>(
3182 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003183 if (!Impl)
3184 return 0;
3185
3186 ToCategory->setImplementation(Impl);
3187 }
3188
3189 return ToCategory;
3190}
3191
Douglas Gregor2aa53772012-01-24 17:42:07 +00003192bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3193 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003194 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003195 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003196 if (shouldForceImportDeclContext(Kind))
3197 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003198 return false;
3199 }
3200
3201 // Start the protocol definition
3202 To->startDefinition();
3203
3204 // Import protocols
3205 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3206 SmallVector<SourceLocation, 4> ProtocolLocs;
3207 ObjCProtocolDecl::protocol_loc_iterator
3208 FromProtoLoc = From->protocol_loc_begin();
3209 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3210 FromProtoEnd = From->protocol_end();
3211 FromProto != FromProtoEnd;
3212 ++FromProto, ++FromProtoLoc) {
3213 ObjCProtocolDecl *ToProto
3214 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3215 if (!ToProto)
3216 return true;
3217 Protocols.push_back(ToProto);
3218 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3219 }
3220
3221 // FIXME: If we're merging, make sure that the protocol list is the same.
3222 To->setProtocolList(Protocols.data(), Protocols.size(),
3223 ProtocolLocs.data(), Importer.getToContext());
3224
Douglas Gregor2e15c842012-02-01 21:00:38 +00003225 if (shouldForceImportDeclContext(Kind)) {
3226 // Import all of the members of this protocol.
3227 ImportDeclContext(From, /*ForceImport=*/true);
3228 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003229 return false;
3230}
3231
Douglas Gregor98d156a2010-02-17 16:12:00 +00003232Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003233 // If this protocol has a definition in the translation unit we're coming
3234 // from, but this particular declaration is not that definition, import the
3235 // definition and map to that.
3236 ObjCProtocolDecl *Definition = D->getDefinition();
3237 if (Definition && Definition != D) {
3238 Decl *ImportedDef = Importer.Import(Definition);
3239 if (!ImportedDef)
3240 return 0;
3241
3242 return Importer.Imported(D, ImportedDef);
3243 }
3244
Douglas Gregor84c51c32010-02-18 01:47:50 +00003245 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003246 DeclContext *DC, *LexicalDC;
3247 DeclarationName Name;
3248 SourceLocation Loc;
3249 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3250 return 0;
3251
3252 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003253 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3254 DC->localUncachedLookup(Name, FoundDecls);
3255 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3256 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003257 continue;
3258
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003259 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003260 break;
3261 }
3262
3263 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003264 if (!ToProto) {
3265 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3266 Name.getAsIdentifierInfo(), Loc,
3267 Importer.Import(D->getAtStartLoc()),
3268 /*PrevDecl=*/0);
3269 ToProto->setLexicalDeclContext(LexicalDC);
3270 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003271 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003272
3273 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003274
Douglas Gregor2aa53772012-01-24 17:42:07 +00003275 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3276 return 0;
3277
Douglas Gregor98d156a2010-02-17 16:12:00 +00003278 return ToProto;
3279}
3280
Douglas Gregor2aa53772012-01-24 17:42:07 +00003281bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3282 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003283 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003284 if (To->getDefinition()) {
3285 // Check consistency of superclass.
3286 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3287 if (FromSuper) {
3288 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3289 if (!FromSuper)
3290 return true;
3291 }
3292
3293 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3294 if ((bool)FromSuper != (bool)ToSuper ||
3295 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3296 Importer.ToDiag(To->getLocation(),
3297 diag::err_odr_objc_superclass_inconsistent)
3298 << To->getDeclName();
3299 if (ToSuper)
3300 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3301 << To->getSuperClass()->getDeclName();
3302 else
3303 Importer.ToDiag(To->getLocation(),
3304 diag::note_odr_objc_missing_superclass);
3305 if (From->getSuperClass())
3306 Importer.FromDiag(From->getSuperClassLoc(),
3307 diag::note_odr_objc_superclass)
3308 << From->getSuperClass()->getDeclName();
3309 else
3310 Importer.FromDiag(From->getLocation(),
3311 diag::note_odr_objc_missing_superclass);
3312 }
3313
Douglas Gregor2e15c842012-02-01 21:00:38 +00003314 if (shouldForceImportDeclContext(Kind))
3315 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003316 return false;
3317 }
3318
3319 // Start the definition.
3320 To->startDefinition();
3321
3322 // If this class has a superclass, import it.
3323 if (From->getSuperClass()) {
3324 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3325 Importer.Import(From->getSuperClass()));
3326 if (!Super)
3327 return true;
3328
3329 To->setSuperClass(Super);
3330 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3331 }
3332
3333 // Import protocols
3334 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3335 SmallVector<SourceLocation, 4> ProtocolLocs;
3336 ObjCInterfaceDecl::protocol_loc_iterator
3337 FromProtoLoc = From->protocol_loc_begin();
3338
3339 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3340 FromProtoEnd = From->protocol_end();
3341 FromProto != FromProtoEnd;
3342 ++FromProto, ++FromProtoLoc) {
3343 ObjCProtocolDecl *ToProto
3344 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3345 if (!ToProto)
3346 return true;
3347 Protocols.push_back(ToProto);
3348 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3349 }
3350
3351 // FIXME: If we're merging, make sure that the protocol list is the same.
3352 To->setProtocolList(Protocols.data(), Protocols.size(),
3353 ProtocolLocs.data(), Importer.getToContext());
3354
3355 // Import categories. When the categories themselves are imported, they'll
3356 // hook themselves into this interface.
3357 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3358 FromCat = FromCat->getNextClassCategory())
3359 Importer.Import(FromCat);
3360
3361 // If we have an @implementation, import it as well.
3362 if (From->getImplementation()) {
3363 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3364 Importer.Import(From->getImplementation()));
3365 if (!Impl)
3366 return true;
3367
3368 To->setImplementation(Impl);
3369 }
3370
Douglas Gregor2e15c842012-02-01 21:00:38 +00003371 if (shouldForceImportDeclContext(Kind)) {
3372 // Import all of the members of this class.
3373 ImportDeclContext(From, /*ForceImport=*/true);
3374 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003375 return false;
3376}
3377
Douglas Gregor45635322010-02-16 01:20:57 +00003378Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003379 // If this class has a definition in the translation unit we're coming from,
3380 // but this particular declaration is not that definition, import the
3381 // definition and map to that.
3382 ObjCInterfaceDecl *Definition = D->getDefinition();
3383 if (Definition && Definition != D) {
3384 Decl *ImportedDef = Importer.Import(Definition);
3385 if (!ImportedDef)
3386 return 0;
3387
3388 return Importer.Imported(D, ImportedDef);
3389 }
3390
Douglas Gregor45635322010-02-16 01:20:57 +00003391 // Import the major distinguishing characteristics of an @interface.
3392 DeclContext *DC, *LexicalDC;
3393 DeclarationName Name;
3394 SourceLocation Loc;
3395 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3396 return 0;
3397
Douglas Gregor2aa53772012-01-24 17:42:07 +00003398 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003399 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003400 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3401 DC->localUncachedLookup(Name, FoundDecls);
3402 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3403 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003404 continue;
3405
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003406 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003407 break;
3408 }
3409
Douglas Gregor2aa53772012-01-24 17:42:07 +00003410 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003411 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003412 if (!ToIface) {
3413 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3414 Importer.Import(D->getAtStartLoc()),
3415 Name.getAsIdentifierInfo(),
3416 /*PrevDecl=*/0,Loc,
3417 D->isImplicitInterfaceDecl());
3418 ToIface->setLexicalDeclContext(LexicalDC);
3419 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003420 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003421 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003422
Douglas Gregor2aa53772012-01-24 17:42:07 +00003423 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3424 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003425
Douglas Gregor98d156a2010-02-17 16:12:00 +00003426 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003427}
3428
Douglas Gregor4da9d682010-12-07 15:32:12 +00003429Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3430 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3431 Importer.Import(D->getCategoryDecl()));
3432 if (!Category)
3433 return 0;
3434
3435 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3436 if (!ToImpl) {
3437 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3438 if (!DC)
3439 return 0;
3440
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003441 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003442 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003443 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003444 Category->getClassInterface(),
3445 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003446 Importer.Import(D->getAtStartLoc()),
3447 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003448
3449 DeclContext *LexicalDC = DC;
3450 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3451 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3452 if (!LexicalDC)
3453 return 0;
3454
3455 ToImpl->setLexicalDeclContext(LexicalDC);
3456 }
3457
Sean Callanan95e74be2011-10-21 02:57:43 +00003458 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003459 Category->setImplementation(ToImpl);
3460 }
3461
3462 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003463 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003464 return ToImpl;
3465}
3466
Douglas Gregorda8025c2010-12-07 01:26:03 +00003467Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3468 // Find the corresponding interface.
3469 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3470 Importer.Import(D->getClassInterface()));
3471 if (!Iface)
3472 return 0;
3473
3474 // Import the superclass, if any.
3475 ObjCInterfaceDecl *Super = 0;
3476 if (D->getSuperClass()) {
3477 Super = cast_or_null<ObjCInterfaceDecl>(
3478 Importer.Import(D->getSuperClass()));
3479 if (!Super)
3480 return 0;
3481 }
3482
3483 ObjCImplementationDecl *Impl = Iface->getImplementation();
3484 if (!Impl) {
3485 // We haven't imported an implementation yet. Create a new @implementation
3486 // now.
3487 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3488 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003489 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003490 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003491 Importer.Import(D->getAtStartLoc()),
3492 Importer.Import(D->getIvarLBraceLoc()),
3493 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003494
3495 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3496 DeclContext *LexicalDC
3497 = Importer.ImportContext(D->getLexicalDeclContext());
3498 if (!LexicalDC)
3499 return 0;
3500 Impl->setLexicalDeclContext(LexicalDC);
3501 }
3502
3503 // Associate the implementation with the class it implements.
3504 Iface->setImplementation(Impl);
3505 Importer.Imported(D, Iface->getImplementation());
3506 } else {
3507 Importer.Imported(D, Iface->getImplementation());
3508
3509 // Verify that the existing @implementation has the same superclass.
3510 if ((Super && !Impl->getSuperClass()) ||
3511 (!Super && Impl->getSuperClass()) ||
3512 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003513 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003514 Importer.ToDiag(Impl->getLocation(),
3515 diag::err_odr_objc_superclass_inconsistent)
3516 << Iface->getDeclName();
3517 // FIXME: It would be nice to have the location of the superclass
3518 // below.
3519 if (Impl->getSuperClass())
3520 Importer.ToDiag(Impl->getLocation(),
3521 diag::note_odr_objc_superclass)
3522 << Impl->getSuperClass()->getDeclName();
3523 else
3524 Importer.ToDiag(Impl->getLocation(),
3525 diag::note_odr_objc_missing_superclass);
3526 if (D->getSuperClass())
3527 Importer.FromDiag(D->getLocation(),
3528 diag::note_odr_objc_superclass)
3529 << D->getSuperClass()->getDeclName();
3530 else
3531 Importer.FromDiag(D->getLocation(),
3532 diag::note_odr_objc_missing_superclass);
3533 return 0;
3534 }
3535 }
3536
3537 // Import all of the members of this @implementation.
3538 ImportDeclContext(D);
3539
3540 return Impl;
3541}
3542
Douglas Gregora11c4582010-02-17 18:02:10 +00003543Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3544 // Import the major distinguishing characteristics of an @property.
3545 DeclContext *DC, *LexicalDC;
3546 DeclarationName Name;
3547 SourceLocation Loc;
3548 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3549 return 0;
3550
3551 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003552 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3553 DC->localUncachedLookup(Name, FoundDecls);
3554 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003555 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003556 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003557 // Check property types.
3558 if (!Importer.IsStructurallyEquivalent(D->getType(),
3559 FoundProp->getType())) {
3560 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3561 << Name << D->getType() << FoundProp->getType();
3562 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3563 << FoundProp->getType();
3564 return 0;
3565 }
3566
3567 // FIXME: Check property attributes, getters, setters, etc.?
3568
3569 // Consider these properties to be equivalent.
3570 Importer.Imported(D, FoundProp);
3571 return FoundProp;
3572 }
3573 }
3574
3575 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003576 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3577 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003578 return 0;
3579
3580 // Create the new property.
3581 ObjCPropertyDecl *ToProperty
3582 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3583 Name.getAsIdentifierInfo(),
3584 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003585 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003586 T,
3587 D->getPropertyImplementation());
3588 Importer.Imported(D, ToProperty);
3589 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003590 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003591
3592 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003593 ToProperty->setPropertyAttributesAsWritten(
3594 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003595 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3596 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3597 ToProperty->setGetterMethodDecl(
3598 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3599 ToProperty->setSetterMethodDecl(
3600 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3601 ToProperty->setPropertyIvarDecl(
3602 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3603 return ToProperty;
3604}
3605
Douglas Gregor14a49e22010-12-07 18:32:03 +00003606Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3607 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3608 Importer.Import(D->getPropertyDecl()));
3609 if (!Property)
3610 return 0;
3611
3612 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3613 if (!DC)
3614 return 0;
3615
3616 // Import the lexical declaration context.
3617 DeclContext *LexicalDC = DC;
3618 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3619 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3620 if (!LexicalDC)
3621 return 0;
3622 }
3623
3624 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3625 if (!InImpl)
3626 return 0;
3627
3628 // Import the ivar (for an @synthesize).
3629 ObjCIvarDecl *Ivar = 0;
3630 if (D->getPropertyIvarDecl()) {
3631 Ivar = cast_or_null<ObjCIvarDecl>(
3632 Importer.Import(D->getPropertyIvarDecl()));
3633 if (!Ivar)
3634 return 0;
3635 }
3636
3637 ObjCPropertyImplDecl *ToImpl
3638 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3639 if (!ToImpl) {
3640 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3641 Importer.Import(D->getLocStart()),
3642 Importer.Import(D->getLocation()),
3643 Property,
3644 D->getPropertyImplementation(),
3645 Ivar,
3646 Importer.Import(D->getPropertyIvarDeclLoc()));
3647 ToImpl->setLexicalDeclContext(LexicalDC);
3648 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003649 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003650 } else {
3651 // Check that we have the same kind of property implementation (@synthesize
3652 // vs. @dynamic).
3653 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3654 Importer.ToDiag(ToImpl->getLocation(),
3655 diag::err_odr_objc_property_impl_kind_inconsistent)
3656 << Property->getDeclName()
3657 << (ToImpl->getPropertyImplementation()
3658 == ObjCPropertyImplDecl::Dynamic);
3659 Importer.FromDiag(D->getLocation(),
3660 diag::note_odr_objc_property_impl_kind)
3661 << D->getPropertyDecl()->getDeclName()
3662 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3663 return 0;
3664 }
3665
3666 // For @synthesize, check that we have the same
3667 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3668 Ivar != ToImpl->getPropertyIvarDecl()) {
3669 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3670 diag::err_odr_objc_synthesize_ivar_inconsistent)
3671 << Property->getDeclName()
3672 << ToImpl->getPropertyIvarDecl()->getDeclName()
3673 << Ivar->getDeclName();
3674 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3675 diag::note_odr_objc_synthesize_ivar_here)
3676 << D->getPropertyIvarDecl()->getDeclName();
3677 return 0;
3678 }
3679
3680 // Merge the existing implementation with the new implementation.
3681 Importer.Imported(D, ToImpl);
3682 }
3683
3684 return ToImpl;
3685}
3686
Douglas Gregora082a492010-11-30 19:14:50 +00003687Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3688 // For template arguments, we adopt the translation unit as our declaration
3689 // context. This context will be fixed when the actual template declaration
3690 // is created.
3691
3692 // FIXME: Import default argument.
3693 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3694 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003695 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003696 Importer.Import(D->getLocation()),
3697 D->getDepth(),
3698 D->getIndex(),
3699 Importer.Import(D->getIdentifier()),
3700 D->wasDeclaredWithTypename(),
3701 D->isParameterPack());
3702}
3703
3704Decl *
3705ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3706 // Import the name of this declaration.
3707 DeclarationName Name = Importer.Import(D->getDeclName());
3708 if (D->getDeclName() && !Name)
3709 return 0;
3710
3711 // Import the location of this declaration.
3712 SourceLocation Loc = Importer.Import(D->getLocation());
3713
3714 // Import the type of this declaration.
3715 QualType T = Importer.Import(D->getType());
3716 if (T.isNull())
3717 return 0;
3718
3719 // Import type-source information.
3720 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3721 if (D->getTypeSourceInfo() && !TInfo)
3722 return 0;
3723
3724 // FIXME: Import default argument.
3725
3726 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3727 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003728 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003729 Loc, D->getDepth(), D->getPosition(),
3730 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003731 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003732}
3733
3734Decl *
3735ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3736 // Import the name of this declaration.
3737 DeclarationName Name = Importer.Import(D->getDeclName());
3738 if (D->getDeclName() && !Name)
3739 return 0;
3740
3741 // Import the location of this declaration.
3742 SourceLocation Loc = Importer.Import(D->getLocation());
3743
3744 // Import template parameters.
3745 TemplateParameterList *TemplateParams
3746 = ImportTemplateParameterList(D->getTemplateParameters());
3747 if (!TemplateParams)
3748 return 0;
3749
3750 // FIXME: Import default argument.
3751
3752 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3753 Importer.getToContext().getTranslationUnitDecl(),
3754 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003755 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003756 Name.getAsIdentifierInfo(),
3757 TemplateParams);
3758}
3759
3760Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3761 // If this record has a definition in the translation unit we're coming from,
3762 // but this particular declaration is not that definition, import the
3763 // definition and map to that.
3764 CXXRecordDecl *Definition
3765 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3766 if (Definition && Definition != D->getTemplatedDecl()) {
3767 Decl *ImportedDef
3768 = Importer.Import(Definition->getDescribedClassTemplate());
3769 if (!ImportedDef)
3770 return 0;
3771
3772 return Importer.Imported(D, ImportedDef);
3773 }
3774
3775 // Import the major distinguishing characteristics of this class template.
3776 DeclContext *DC, *LexicalDC;
3777 DeclarationName Name;
3778 SourceLocation Loc;
3779 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3780 return 0;
3781
3782 // We may already have a template of the same name; try to find and match it.
3783 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003784 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003785 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3786 DC->localUncachedLookup(Name, FoundDecls);
3787 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3788 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003789 continue;
3790
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003791 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003792 if (ClassTemplateDecl *FoundTemplate
3793 = dyn_cast<ClassTemplateDecl>(Found)) {
3794 if (IsStructuralMatch(D, FoundTemplate)) {
3795 // The class templates structurally match; call it the same template.
3796 // FIXME: We may be filling in a forward declaration here. Handle
3797 // this case!
3798 Importer.Imported(D->getTemplatedDecl(),
3799 FoundTemplate->getTemplatedDecl());
3800 return Importer.Imported(D, FoundTemplate);
3801 }
3802 }
3803
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003804 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003805 }
3806
3807 if (!ConflictingDecls.empty()) {
3808 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3809 ConflictingDecls.data(),
3810 ConflictingDecls.size());
3811 }
3812
3813 if (!Name)
3814 return 0;
3815 }
3816
3817 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3818
3819 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003820 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3821 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003822 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3823 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003824 DC, StartLoc, IdLoc,
3825 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003826 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003827 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003828 D2Templated->setLexicalDeclContext(LexicalDC);
3829
3830 // Create the class template declaration itself.
3831 TemplateParameterList *TemplateParams
3832 = ImportTemplateParameterList(D->getTemplateParameters());
3833 if (!TemplateParams)
3834 return 0;
3835
3836 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3837 Loc, Name, TemplateParams,
3838 D2Templated,
3839 /*PrevDecl=*/0);
3840 D2Templated->setDescribedClassTemplate(D2);
3841
3842 D2->setAccess(D->getAccess());
3843 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003844 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003845
3846 // Note the relationship between the class templates.
3847 Importer.Imported(D, D2);
3848 Importer.Imported(DTemplated, D2Templated);
3849
John McCallf937c022011-10-07 06:10:15 +00003850 if (DTemplated->isCompleteDefinition() &&
3851 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003852 // FIXME: Import definition!
3853 }
3854
3855 return D2;
3856}
3857
Douglas Gregore2e50d332010-12-01 01:36:18 +00003858Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3859 ClassTemplateSpecializationDecl *D) {
3860 // If this record has a definition in the translation unit we're coming from,
3861 // but this particular declaration is not that definition, import the
3862 // definition and map to that.
3863 TagDecl *Definition = D->getDefinition();
3864 if (Definition && Definition != D) {
3865 Decl *ImportedDef = Importer.Import(Definition);
3866 if (!ImportedDef)
3867 return 0;
3868
3869 return Importer.Imported(D, ImportedDef);
3870 }
3871
3872 ClassTemplateDecl *ClassTemplate
3873 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3874 D->getSpecializedTemplate()));
3875 if (!ClassTemplate)
3876 return 0;
3877
3878 // Import the context of this declaration.
3879 DeclContext *DC = ClassTemplate->getDeclContext();
3880 if (!DC)
3881 return 0;
3882
3883 DeclContext *LexicalDC = DC;
3884 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3885 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3886 if (!LexicalDC)
3887 return 0;
3888 }
3889
3890 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003891 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3892 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003893
3894 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003895 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003896 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3897 D->getTemplateArgs().size(),
3898 TemplateArgs))
3899 return 0;
3900
3901 // Try to find an existing specialization with these template arguments.
3902 void *InsertPos = 0;
3903 ClassTemplateSpecializationDecl *D2
3904 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3905 TemplateArgs.size(), InsertPos);
3906 if (D2) {
3907 // We already have a class template specialization with these template
3908 // arguments.
3909
3910 // FIXME: Check for specialization vs. instantiation errors.
3911
3912 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003913 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003914 // The record types structurally match, or the "from" translation
3915 // unit only had a forward declaration anyway; call it the same
3916 // function.
3917 return Importer.Imported(D, FoundDef);
3918 }
3919 }
3920 } else {
3921 // Create a new specialization.
3922 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3923 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003924 StartLoc, IdLoc,
3925 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003926 TemplateArgs.data(),
3927 TemplateArgs.size(),
3928 /*PrevDecl=*/0);
3929 D2->setSpecializationKind(D->getSpecializationKind());
3930
3931 // Add this specialization to the class template.
3932 ClassTemplate->AddSpecialization(D2, InsertPos);
3933
3934 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003935 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003936
3937 // Add the specialization to this context.
3938 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003939 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003940 }
3941 Importer.Imported(D, D2);
3942
John McCallf937c022011-10-07 06:10:15 +00003943 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003944 return 0;
3945
3946 return D2;
3947}
3948
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003949//----------------------------------------------------------------------------
3950// Import Statements
3951//----------------------------------------------------------------------------
3952
3953Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3954 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3955 << S->getStmtClassName();
3956 return 0;
3957}
3958
3959//----------------------------------------------------------------------------
3960// Import Expressions
3961//----------------------------------------------------------------------------
3962Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3963 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3964 << E->getStmtClassName();
3965 return 0;
3966}
3967
Douglas Gregor52f820e2010-02-19 01:17:02 +00003968Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003969 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3970 if (!ToD)
3971 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003972
3973 NamedDecl *FoundD = 0;
3974 if (E->getDecl() != E->getFoundDecl()) {
3975 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3976 if (!FoundD)
3977 return 0;
3978 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003979
3980 QualType T = Importer.Import(E->getType());
3981 if (T.isNull())
3982 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003983
3984 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3985 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003986 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003987 ToD,
John McCall113bee02012-03-10 09:33:50 +00003988 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003989 Importer.Import(E->getLocation()),
3990 T, E->getValueKind(),
3991 FoundD,
3992 /*FIXME:TemplateArgs=*/0);
3993 if (E->hadMultipleCandidates())
3994 DRE->setHadMultipleCandidates(true);
3995 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003996}
3997
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003998Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3999 QualType T = Importer.Import(E->getType());
4000 if (T.isNull())
4001 return 0;
4002
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004003 return IntegerLiteral::Create(Importer.getToContext(),
4004 E->getValue(), T,
4005 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004006}
4007
Douglas Gregor623421d2010-02-18 02:21:22 +00004008Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4009 QualType T = Importer.Import(E->getType());
4010 if (T.isNull())
4011 return 0;
4012
Douglas Gregorfb65e592011-07-27 05:40:30 +00004013 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4014 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004015 Importer.Import(E->getLocation()));
4016}
4017
Douglas Gregorc74247e2010-02-19 01:07:06 +00004018Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4019 Expr *SubExpr = Importer.Import(E->getSubExpr());
4020 if (!SubExpr)
4021 return 0;
4022
4023 return new (Importer.getToContext())
4024 ParenExpr(Importer.Import(E->getLParen()),
4025 Importer.Import(E->getRParen()),
4026 SubExpr);
4027}
4028
4029Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4030 QualType T = Importer.Import(E->getType());
4031 if (T.isNull())
4032 return 0;
4033
4034 Expr *SubExpr = Importer.Import(E->getSubExpr());
4035 if (!SubExpr)
4036 return 0;
4037
4038 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004039 T, E->getValueKind(),
4040 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004041 Importer.Import(E->getOperatorLoc()));
4042}
4043
Peter Collingbournee190dee2011-03-11 19:24:49 +00004044Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4045 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004046 QualType ResultType = Importer.Import(E->getType());
4047
4048 if (E->isArgumentType()) {
4049 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4050 if (!TInfo)
4051 return 0;
4052
Peter Collingbournee190dee2011-03-11 19:24:49 +00004053 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4054 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004055 Importer.Import(E->getOperatorLoc()),
4056 Importer.Import(E->getRParenLoc()));
4057 }
4058
4059 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4060 if (!SubExpr)
4061 return 0;
4062
Peter Collingbournee190dee2011-03-11 19:24:49 +00004063 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4064 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004065 Importer.Import(E->getOperatorLoc()),
4066 Importer.Import(E->getRParenLoc()));
4067}
4068
Douglas Gregorc74247e2010-02-19 01:07:06 +00004069Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4070 QualType T = Importer.Import(E->getType());
4071 if (T.isNull())
4072 return 0;
4073
4074 Expr *LHS = Importer.Import(E->getLHS());
4075 if (!LHS)
4076 return 0;
4077
4078 Expr *RHS = Importer.Import(E->getRHS());
4079 if (!RHS)
4080 return 0;
4081
4082 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004083 T, E->getValueKind(),
4084 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004085 Importer.Import(E->getOperatorLoc()));
4086}
4087
4088Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4089 QualType T = Importer.Import(E->getType());
4090 if (T.isNull())
4091 return 0;
4092
4093 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4094 if (CompLHSType.isNull())
4095 return 0;
4096
4097 QualType CompResultType = Importer.Import(E->getComputationResultType());
4098 if (CompResultType.isNull())
4099 return 0;
4100
4101 Expr *LHS = Importer.Import(E->getLHS());
4102 if (!LHS)
4103 return 0;
4104
4105 Expr *RHS = Importer.Import(E->getRHS());
4106 if (!RHS)
4107 return 0;
4108
4109 return new (Importer.getToContext())
4110 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004111 T, E->getValueKind(),
4112 E->getObjectKind(),
4113 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004114 Importer.Import(E->getOperatorLoc()));
4115}
4116
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004117static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004118 if (E->path_empty()) return false;
4119
4120 // TODO: import cast paths
4121 return true;
4122}
4123
Douglas Gregor98c10182010-02-12 22:17:39 +00004124Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4125 QualType T = Importer.Import(E->getType());
4126 if (T.isNull())
4127 return 0;
4128
4129 Expr *SubExpr = Importer.Import(E->getSubExpr());
4130 if (!SubExpr)
4131 return 0;
John McCallcf142162010-08-07 06:22:56 +00004132
4133 CXXCastPath BasePath;
4134 if (ImportCastPath(E, BasePath))
4135 return 0;
4136
4137 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004138 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004139}
4140
Douglas Gregor5481d322010-02-19 01:32:14 +00004141Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4142 QualType T = Importer.Import(E->getType());
4143 if (T.isNull())
4144 return 0;
4145
4146 Expr *SubExpr = Importer.Import(E->getSubExpr());
4147 if (!SubExpr)
4148 return 0;
4149
4150 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4151 if (!TInfo && E->getTypeInfoAsWritten())
4152 return 0;
4153
John McCallcf142162010-08-07 06:22:56 +00004154 CXXCastPath BasePath;
4155 if (ImportCastPath(E, BasePath))
4156 return 0;
4157
John McCall7decc9e2010-11-18 06:31:45 +00004158 return CStyleCastExpr::Create(Importer.getToContext(), T,
4159 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004160 SubExpr, &BasePath, TInfo,
4161 Importer.Import(E->getLParenLoc()),
4162 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004163}
4164
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004165ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004166 ASTContext &FromContext, FileManager &FromFileManager,
4167 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004168 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004169 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4170 Minimal(MinimalImport)
4171{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004172 ImportedDecls[FromContext.getTranslationUnitDecl()]
4173 = ToContext.getTranslationUnitDecl();
4174}
4175
4176ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004177
4178QualType ASTImporter::Import(QualType FromT) {
4179 if (FromT.isNull())
4180 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004181
4182 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004183
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004184 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004185 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4186 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004187 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004188 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004189
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004190 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004191 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004192 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004193 if (ToT.isNull())
4194 return ToT;
4195
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004196 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004197 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004198
John McCall424cec92011-01-19 06:33:43 +00004199 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004200}
4201
Douglas Gregor62d311f2010-02-09 19:21:46 +00004202TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004203 if (!FromTSI)
4204 return FromTSI;
4205
4206 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004207 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004208 QualType T = Import(FromTSI->getType());
4209 if (T.isNull())
4210 return 0;
4211
4212 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004213 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004214}
4215
4216Decl *ASTImporter::Import(Decl *FromD) {
4217 if (!FromD)
4218 return 0;
4219
Douglas Gregord451ea92011-07-29 23:31:30 +00004220 ASTNodeImporter Importer(*this);
4221
Douglas Gregor62d311f2010-02-09 19:21:46 +00004222 // Check whether we've already imported this declaration.
4223 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004224 if (Pos != ImportedDecls.end()) {
4225 Decl *ToD = Pos->second;
4226 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4227 return ToD;
4228 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004229
4230 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004231 Decl *ToD = Importer.Visit(FromD);
4232 if (!ToD)
4233 return 0;
4234
4235 // Record the imported declaration.
4236 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004237
4238 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4239 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004240 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004241 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004242 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004243 // When we've finished transforming a typedef, see whether it was the
4244 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004245 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004246 FromTag = AnonTagsWithPendingTypedefs.begin(),
4247 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4248 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004249 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004250 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4251 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004252 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004253 AnonTagsWithPendingTypedefs.erase(FromTag);
4254 break;
4255 }
4256 }
4257 }
4258 }
4259
Douglas Gregor62d311f2010-02-09 19:21:46 +00004260 return ToD;
4261}
4262
4263DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4264 if (!FromDC)
4265 return FromDC;
4266
Douglas Gregor95d82832012-01-24 18:36:04 +00004267 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004268 if (!ToDC)
4269 return 0;
4270
4271 // When we're using a record/enum/Objective-C class/protocol as a context, we
4272 // need it to have a definition.
4273 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004274 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004275 if (ToRecord->isCompleteDefinition()) {
4276 // Do nothing.
4277 } else if (FromRecord->isCompleteDefinition()) {
4278 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4279 ASTNodeImporter::IDK_Basic);
4280 } else {
4281 CompleteDecl(ToRecord);
4282 }
4283 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4284 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4285 if (ToEnum->isCompleteDefinition()) {
4286 // Do nothing.
4287 } else if (FromEnum->isCompleteDefinition()) {
4288 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4289 ASTNodeImporter::IDK_Basic);
4290 } else {
4291 CompleteDecl(ToEnum);
4292 }
4293 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4294 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4295 if (ToClass->getDefinition()) {
4296 // Do nothing.
4297 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4298 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4299 ASTNodeImporter::IDK_Basic);
4300 } else {
4301 CompleteDecl(ToClass);
4302 }
4303 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4304 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4305 if (ToProto->getDefinition()) {
4306 // Do nothing.
4307 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4308 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4309 ASTNodeImporter::IDK_Basic);
4310 } else {
4311 CompleteDecl(ToProto);
4312 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004313 }
4314
4315 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004316}
4317
4318Expr *ASTImporter::Import(Expr *FromE) {
4319 if (!FromE)
4320 return 0;
4321
4322 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4323}
4324
4325Stmt *ASTImporter::Import(Stmt *FromS) {
4326 if (!FromS)
4327 return 0;
4328
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004329 // Check whether we've already imported this declaration.
4330 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4331 if (Pos != ImportedStmts.end())
4332 return Pos->second;
4333
4334 // Import the type
4335 ASTNodeImporter Importer(*this);
4336 Stmt *ToS = Importer.Visit(FromS);
4337 if (!ToS)
4338 return 0;
4339
4340 // Record the imported declaration.
4341 ImportedStmts[FromS] = ToS;
4342 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004343}
4344
4345NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4346 if (!FromNNS)
4347 return 0;
4348
Douglas Gregor90ebf252011-04-27 16:48:40 +00004349 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4350
4351 switch (FromNNS->getKind()) {
4352 case NestedNameSpecifier::Identifier:
4353 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4354 return NestedNameSpecifier::Create(ToContext, prefix, II);
4355 }
4356 return 0;
4357
4358 case NestedNameSpecifier::Namespace:
4359 if (NamespaceDecl *NS =
4360 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4361 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4362 }
4363 return 0;
4364
4365 case NestedNameSpecifier::NamespaceAlias:
4366 if (NamespaceAliasDecl *NSAD =
4367 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4368 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4369 }
4370 return 0;
4371
4372 case NestedNameSpecifier::Global:
4373 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4374
4375 case NestedNameSpecifier::TypeSpec:
4376 case NestedNameSpecifier::TypeSpecWithTemplate: {
4377 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4378 if (!T.isNull()) {
4379 bool bTemplate = FromNNS->getKind() ==
4380 NestedNameSpecifier::TypeSpecWithTemplate;
4381 return NestedNameSpecifier::Create(ToContext, prefix,
4382 bTemplate, T.getTypePtr());
4383 }
4384 }
4385 return 0;
4386 }
4387
4388 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004389}
4390
Douglas Gregor14454802011-02-25 02:25:35 +00004391NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4392 // FIXME: Implement!
4393 return NestedNameSpecifierLoc();
4394}
4395
Douglas Gregore2e50d332010-12-01 01:36:18 +00004396TemplateName ASTImporter::Import(TemplateName From) {
4397 switch (From.getKind()) {
4398 case TemplateName::Template:
4399 if (TemplateDecl *ToTemplate
4400 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4401 return TemplateName(ToTemplate);
4402
4403 return TemplateName();
4404
4405 case TemplateName::OverloadedTemplate: {
4406 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4407 UnresolvedSet<2> ToTemplates;
4408 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4409 E = FromStorage->end();
4410 I != E; ++I) {
4411 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4412 ToTemplates.addDecl(To);
4413 else
4414 return TemplateName();
4415 }
4416 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4417 ToTemplates.end());
4418 }
4419
4420 case TemplateName::QualifiedTemplate: {
4421 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4422 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4423 if (!Qualifier)
4424 return TemplateName();
4425
4426 if (TemplateDecl *ToTemplate
4427 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4428 return ToContext.getQualifiedTemplateName(Qualifier,
4429 QTN->hasTemplateKeyword(),
4430 ToTemplate);
4431
4432 return TemplateName();
4433 }
4434
4435 case TemplateName::DependentTemplate: {
4436 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4437 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4438 if (!Qualifier)
4439 return TemplateName();
4440
4441 if (DTN->isIdentifier()) {
4442 return ToContext.getDependentTemplateName(Qualifier,
4443 Import(DTN->getIdentifier()));
4444 }
4445
4446 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4447 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004448
4449 case TemplateName::SubstTemplateTemplateParm: {
4450 SubstTemplateTemplateParmStorage *subst
4451 = From.getAsSubstTemplateTemplateParm();
4452 TemplateTemplateParmDecl *param
4453 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4454 if (!param)
4455 return TemplateName();
4456
4457 TemplateName replacement = Import(subst->getReplacement());
4458 if (replacement.isNull()) return TemplateName();
4459
4460 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4461 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004462
4463 case TemplateName::SubstTemplateTemplateParmPack: {
4464 SubstTemplateTemplateParmPackStorage *SubstPack
4465 = From.getAsSubstTemplateTemplateParmPack();
4466 TemplateTemplateParmDecl *Param
4467 = cast_or_null<TemplateTemplateParmDecl>(
4468 Import(SubstPack->getParameterPack()));
4469 if (!Param)
4470 return TemplateName();
4471
4472 ASTNodeImporter Importer(*this);
4473 TemplateArgument ArgPack
4474 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4475 if (ArgPack.isNull())
4476 return TemplateName();
4477
4478 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4479 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004480 }
4481
4482 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004483}
4484
Douglas Gregor62d311f2010-02-09 19:21:46 +00004485SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4486 if (FromLoc.isInvalid())
4487 return SourceLocation();
4488
Douglas Gregor811663e2010-02-10 00:15:17 +00004489 SourceManager &FromSM = FromContext.getSourceManager();
4490
4491 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004492 // don't have to import macro expansions.
4493 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004494 FromLoc = FromSM.getSpellingLoc(FromLoc);
4495 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4496 SourceManager &ToSM = ToContext.getSourceManager();
4497 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004498 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004499}
4500
4501SourceRange ASTImporter::Import(SourceRange FromRange) {
4502 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4503}
4504
Douglas Gregor811663e2010-02-10 00:15:17 +00004505FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004506 llvm::DenseMap<FileID, FileID>::iterator Pos
4507 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004508 if (Pos != ImportedFileIDs.end())
4509 return Pos->second;
4510
4511 SourceManager &FromSM = FromContext.getSourceManager();
4512 SourceManager &ToSM = ToContext.getSourceManager();
4513 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004514 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004515
4516 // Include location of this file.
4517 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4518
4519 // Map the FileID for to the "to" source manager.
4520 FileID ToID;
4521 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004522 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004523 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4524 // disk again
4525 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4526 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004527 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004528 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4529 FromSLoc.getFile().getFileCharacteristic());
4530 } else {
4531 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004532 const llvm::MemoryBuffer *
4533 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004534 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004535 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004536 FromBuf->getBufferIdentifier());
4537 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4538 }
4539
4540
Sebastian Redl99219f12010-09-30 01:03:06 +00004541 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004542 return ToID;
4543}
4544
Douglas Gregor0a791672011-01-18 03:11:38 +00004545void ASTImporter::ImportDefinition(Decl *From) {
4546 Decl *To = Import(From);
4547 if (!To)
4548 return;
4549
4550 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4551 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004552
4553 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4554 if (!ToRecord->getDefinition()) {
4555 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004556 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004557 return;
4558 }
4559 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004560
4561 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4562 if (!ToEnum->getDefinition()) {
4563 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004564 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004565 return;
4566 }
4567 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004568
4569 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4570 if (!ToIFace->getDefinition()) {
4571 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004572 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004573 return;
4574 }
4575 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004576
Douglas Gregor2aa53772012-01-24 17:42:07 +00004577 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4578 if (!ToProto->getDefinition()) {
4579 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004580 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004581 return;
4582 }
4583 }
4584
Douglas Gregor0a791672011-01-18 03:11:38 +00004585 Importer.ImportDeclContext(FromDC, true);
4586 }
4587}
4588
Douglas Gregor96e578d2010-02-05 17:54:41 +00004589DeclarationName ASTImporter::Import(DeclarationName FromName) {
4590 if (!FromName)
4591 return DeclarationName();
4592
4593 switch (FromName.getNameKind()) {
4594 case DeclarationName::Identifier:
4595 return Import(FromName.getAsIdentifierInfo());
4596
4597 case DeclarationName::ObjCZeroArgSelector:
4598 case DeclarationName::ObjCOneArgSelector:
4599 case DeclarationName::ObjCMultiArgSelector:
4600 return Import(FromName.getObjCSelector());
4601
4602 case DeclarationName::CXXConstructorName: {
4603 QualType T = Import(FromName.getCXXNameType());
4604 if (T.isNull())
4605 return DeclarationName();
4606
4607 return ToContext.DeclarationNames.getCXXConstructorName(
4608 ToContext.getCanonicalType(T));
4609 }
4610
4611 case DeclarationName::CXXDestructorName: {
4612 QualType T = Import(FromName.getCXXNameType());
4613 if (T.isNull())
4614 return DeclarationName();
4615
4616 return ToContext.DeclarationNames.getCXXDestructorName(
4617 ToContext.getCanonicalType(T));
4618 }
4619
4620 case DeclarationName::CXXConversionFunctionName: {
4621 QualType T = Import(FromName.getCXXNameType());
4622 if (T.isNull())
4623 return DeclarationName();
4624
4625 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4626 ToContext.getCanonicalType(T));
4627 }
4628
4629 case DeclarationName::CXXOperatorName:
4630 return ToContext.DeclarationNames.getCXXOperatorName(
4631 FromName.getCXXOverloadedOperator());
4632
4633 case DeclarationName::CXXLiteralOperatorName:
4634 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4635 Import(FromName.getCXXLiteralIdentifier()));
4636
4637 case DeclarationName::CXXUsingDirective:
4638 // FIXME: STATICS!
4639 return DeclarationName::getUsingDirectiveName();
4640 }
4641
David Blaikiee4d798f2012-01-20 21:50:17 +00004642 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004643}
4644
Douglas Gregore2e50d332010-12-01 01:36:18 +00004645IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004646 if (!FromId)
4647 return 0;
4648
4649 return &ToContext.Idents.get(FromId->getName());
4650}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004651
Douglas Gregor43f54792010-02-17 02:12:47 +00004652Selector ASTImporter::Import(Selector FromSel) {
4653 if (FromSel.isNull())
4654 return Selector();
4655
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004656 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004657 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4658 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4659 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4660 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4661}
4662
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004663DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4664 DeclContext *DC,
4665 unsigned IDNS,
4666 NamedDecl **Decls,
4667 unsigned NumDecls) {
4668 return Name;
4669}
4670
4671DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004672 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004673}
4674
4675DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004676 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004677}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004678
Douglas Gregor2e15c842012-02-01 21:00:38 +00004679void ASTImporter::CompleteDecl (Decl *D) {
4680 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4681 if (!ID->getDefinition())
4682 ID->startDefinition();
4683 }
4684 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4685 if (!PD->getDefinition())
4686 PD->startDefinition();
4687 }
4688 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4689 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4690 TD->startDefinition();
4691 TD->setCompleteDefinition(true);
4692 }
4693 }
4694 else {
4695 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4696 }
4697}
4698
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004699Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4700 ImportedDecls[From] = To;
4701 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004702}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004703
Douglas Gregordd6006f2012-07-17 21:16:27 +00004704bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4705 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004706 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004707 = ImportedTypes.find(From.getTypePtr());
4708 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4709 return true;
4710
Douglas Gregordd6006f2012-07-17 21:16:27 +00004711 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4712 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004713 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004714}