blob: 7639493cc5a4d32dfc8dab5f3d839b03a7c2733a [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
Douglas Gregor3c2404b2011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
33
34 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000040
41 // Importing types
John McCall424cec92011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
43 QualType VisitBuiltinType(const BuiltinType *T);
44 QualType VisitComplexType(const ComplexType *T);
45 QualType VisitPointerType(const PointerType *T);
46 QualType VisitBlockPointerType(const BlockPointerType *T);
47 QualType VisitLValueReferenceType(const LValueReferenceType *T);
48 QualType VisitRValueReferenceType(const RValueReferenceType *T);
49 QualType VisitMemberPointerType(const MemberPointerType *T);
50 QualType VisitConstantArrayType(const ConstantArrayType *T);
51 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
52 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000055 QualType VisitVectorType(const VectorType *T);
56 QualType VisitExtVectorType(const ExtVectorType *T);
57 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
58 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
Douglas Gregor95d82832012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregord451ea92011-07-29 23:31:30 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000089
Douglas Gregor95d82832012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregord451ea92011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000110 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000112 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000116 TemplateParameterList *ImportTemplateParameterList(
117 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000118 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
119 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
120 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000121 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000122 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
123 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000124 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +0000125 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000126 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000127 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000128 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000129 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000130 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000131 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000132 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000133 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000134 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000135 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000136 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
137 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
138 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
139 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000141 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000142 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000143 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000144 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000145 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000146 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000147 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000148 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000149 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000150 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000151 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000152 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000153 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000154 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
155 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
156 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
157 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000158 Decl *VisitClassTemplateSpecializationDecl(
159 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000160
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000161 // Importing statements
162 Stmt *VisitStmt(Stmt *S);
163
164 // Importing expressions
165 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000166 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000167 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000168 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000169 Expr *VisitParenExpr(ParenExpr *E);
170 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000171 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000172 Expr *VisitBinaryOperator(BinaryOperator *E);
173 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000174 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000175 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176 };
177}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000178using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179
180//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000181// Structural Equivalence
182//----------------------------------------------------------------------------
183
184namespace {
185 struct StructuralEquivalenceContext {
186 /// \brief AST contexts for which we are checking structural equivalence.
187 ASTContext &C1, &C2;
188
Douglas Gregor3996e242010-02-15 22:01:00 +0000189 /// \brief The set of "tentative" equivalences between two canonical
190 /// declarations, mapping from a declaration in the first context to the
191 /// declaration in the second context that we believe to be equivalent.
192 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
193
194 /// \brief Queue of declarations in the first context whose equivalence
195 /// with a declaration in the second context still needs to be verified.
196 std::deque<Decl *> DeclsToCheck;
197
Douglas Gregorb4964f72010-02-15 23:54:17 +0000198 /// \brief Declaration (from, to) pairs that are known not to be equivalent
199 /// (which we have already complained about).
200 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
201
Douglas Gregor3996e242010-02-15 22:01:00 +0000202 /// \brief Whether we're being strict about the spelling of types when
203 /// unifying two types.
204 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000205
206 /// \brief Whether to complain about failures.
207 bool Complain;
208
Douglas Gregor3996e242010-02-15 22:01:00 +0000209 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000210 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000211 bool StrictTypeSpelling = false,
212 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000213 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregordd6006f2012-07-17 21:16:27 +0000214 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000215
216 /// \brief Determine whether the two declarations are structurally
217 /// equivalent.
218 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
219
220 /// \brief Determine whether the two types are structurally equivalent.
221 bool IsStructurallyEquivalent(QualType T1, QualType T2);
222
223 private:
224 /// \brief Finish checking all of the structural equivalences.
225 ///
226 /// \returns true if an error occurred, false otherwise.
227 bool Finish();
228
229 public:
230 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregordd6006f2012-07-17 21:16:27 +0000231 if (!Complain)
232 return DiagnosticBuilder::getEmpty();
233
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000234 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000235 }
236
237 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregordd6006f2012-07-17 21:16:27 +0000238 if (!Complain)
239 return DiagnosticBuilder::getEmpty();
240
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000241 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000242 }
243 };
244}
245
246static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
247 QualType T1, QualType T2);
248static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
249 Decl *D1, Decl *D2);
250
Douglas Gregor3996e242010-02-15 22:01:00 +0000251/// \brief Determine structural equivalence of two expressions.
252static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
253 Expr *E1, Expr *E2) {
254 if (!E1 || !E2)
255 return E1 == E2;
256
257 // FIXME: Actually perform a structural comparison!
258 return true;
259}
260
261/// \brief Determine whether two identifiers are equivalent.
262static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
263 const IdentifierInfo *Name2) {
264 if (!Name1 || !Name2)
265 return Name1 == Name2;
266
267 return Name1->getName() == Name2->getName();
268}
269
270/// \brief Determine whether two nested-name-specifiers are equivalent.
271static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
272 NestedNameSpecifier *NNS1,
273 NestedNameSpecifier *NNS2) {
274 // FIXME: Implement!
275 return true;
276}
277
278/// \brief Determine whether two template arguments are equivalent.
279static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
280 const TemplateArgument &Arg1,
281 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000282 if (Arg1.getKind() != Arg2.getKind())
283 return false;
284
285 switch (Arg1.getKind()) {
286 case TemplateArgument::Null:
287 return true;
288
289 case TemplateArgument::Type:
290 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
291
292 case TemplateArgument::Integral:
293 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
294 Arg2.getIntegralType()))
295 return false;
296
Eric Christopher6dcc3762012-07-15 00:23:57 +0000297 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000298
299 case TemplateArgument::Declaration:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000300 if (!Arg1.getAsDecl() || !Arg2.getAsDecl())
301 return !Arg1.getAsDecl() && !Arg2.getAsDecl();
Douglas Gregore2e50d332010-12-01 01:36:18 +0000302 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
303
304 case TemplateArgument::Template:
305 return IsStructurallyEquivalent(Context,
306 Arg1.getAsTemplate(),
307 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000308
309 case TemplateArgument::TemplateExpansion:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplateOrTemplatePattern(),
312 Arg2.getAsTemplateOrTemplatePattern());
313
Douglas Gregore2e50d332010-12-01 01:36:18 +0000314 case TemplateArgument::Expression:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsExpr(), Arg2.getAsExpr());
317
318 case TemplateArgument::Pack:
319 if (Arg1.pack_size() != Arg2.pack_size())
320 return false;
321
322 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
323 if (!IsStructurallyEquivalent(Context,
324 Arg1.pack_begin()[I],
325 Arg2.pack_begin()[I]))
326 return false;
327
328 return true;
329 }
330
331 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000332}
333
334/// \brief Determine structural equivalence for the common part of array
335/// types.
336static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
337 const ArrayType *Array1,
338 const ArrayType *Array2) {
339 if (!IsStructurallyEquivalent(Context,
340 Array1->getElementType(),
341 Array2->getElementType()))
342 return false;
343 if (Array1->getSizeModifier() != Array2->getSizeModifier())
344 return false;
345 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
346 return false;
347
348 return true;
349}
350
351/// \brief Determine structural equivalence of two types.
352static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
353 QualType T1, QualType T2) {
354 if (T1.isNull() || T2.isNull())
355 return T1.isNull() && T2.isNull();
356
357 if (!Context.StrictTypeSpelling) {
358 // We aren't being strict about token-to-token equivalence of types,
359 // so map down to the canonical type.
360 T1 = Context.C1.getCanonicalType(T1);
361 T2 = Context.C2.getCanonicalType(T2);
362 }
363
364 if (T1.getQualifiers() != T2.getQualifiers())
365 return false;
366
Douglas Gregorb4964f72010-02-15 23:54:17 +0000367 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000368
Douglas Gregorb4964f72010-02-15 23:54:17 +0000369 if (T1->getTypeClass() != T2->getTypeClass()) {
370 // Compare function types with prototypes vs. without prototypes as if
371 // both did not have prototypes.
372 if (T1->getTypeClass() == Type::FunctionProto &&
373 T2->getTypeClass() == Type::FunctionNoProto)
374 TC = Type::FunctionNoProto;
375 else if (T1->getTypeClass() == Type::FunctionNoProto &&
376 T2->getTypeClass() == Type::FunctionProto)
377 TC = Type::FunctionNoProto;
378 else
379 return false;
380 }
381
382 switch (TC) {
383 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000384 // FIXME: Deal with Char_S/Char_U.
385 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
386 return false;
387 break;
388
389 case Type::Complex:
390 if (!IsStructurallyEquivalent(Context,
391 cast<ComplexType>(T1)->getElementType(),
392 cast<ComplexType>(T2)->getElementType()))
393 return false;
394 break;
395
396 case Type::Pointer:
397 if (!IsStructurallyEquivalent(Context,
398 cast<PointerType>(T1)->getPointeeType(),
399 cast<PointerType>(T2)->getPointeeType()))
400 return false;
401 break;
402
403 case Type::BlockPointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<BlockPointerType>(T1)->getPointeeType(),
406 cast<BlockPointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::LValueReference:
411 case Type::RValueReference: {
412 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
413 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
414 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
415 return false;
416 if (Ref1->isInnerRef() != Ref2->isInnerRef())
417 return false;
418 if (!IsStructurallyEquivalent(Context,
419 Ref1->getPointeeTypeAsWritten(),
420 Ref2->getPointeeTypeAsWritten()))
421 return false;
422 break;
423 }
424
425 case Type::MemberPointer: {
426 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
427 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
428 if (!IsStructurallyEquivalent(Context,
429 MemPtr1->getPointeeType(),
430 MemPtr2->getPointeeType()))
431 return false;
432 if (!IsStructurallyEquivalent(Context,
433 QualType(MemPtr1->getClass(), 0),
434 QualType(MemPtr2->getClass(), 0)))
435 return false;
436 break;
437 }
438
439 case Type::ConstantArray: {
440 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
441 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000442 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000443 return false;
444
445 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
446 return false;
447 break;
448 }
449
450 case Type::IncompleteArray:
451 if (!IsArrayStructurallyEquivalent(Context,
452 cast<ArrayType>(T1),
453 cast<ArrayType>(T2)))
454 return false;
455 break;
456
457 case Type::VariableArray: {
458 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
459 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
460 if (!IsStructurallyEquivalent(Context,
461 Array1->getSizeExpr(), Array2->getSizeExpr()))
462 return false;
463
464 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
465 return false;
466
467 break;
468 }
469
470 case Type::DependentSizedArray: {
471 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
472 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
473 if (!IsStructurallyEquivalent(Context,
474 Array1->getSizeExpr(), Array2->getSizeExpr()))
475 return false;
476
477 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
478 return false;
479
480 break;
481 }
482
483 case Type::DependentSizedExtVector: {
484 const DependentSizedExtVectorType *Vec1
485 = cast<DependentSizedExtVectorType>(T1);
486 const DependentSizedExtVectorType *Vec2
487 = cast<DependentSizedExtVectorType>(T2);
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
490 return false;
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 break;
496 }
497
498 case Type::Vector:
499 case Type::ExtVector: {
500 const VectorType *Vec1 = cast<VectorType>(T1);
501 const VectorType *Vec2 = cast<VectorType>(T2);
502 if (!IsStructurallyEquivalent(Context,
503 Vec1->getElementType(),
504 Vec2->getElementType()))
505 return false;
506 if (Vec1->getNumElements() != Vec2->getNumElements())
507 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000508 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000509 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000510 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000511 }
512
513 case Type::FunctionProto: {
514 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
515 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
516 if (Proto1->getNumArgs() != Proto2->getNumArgs())
517 return false;
518 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
519 if (!IsStructurallyEquivalent(Context,
520 Proto1->getArgType(I),
521 Proto2->getArgType(I)))
522 return false;
523 }
524 if (Proto1->isVariadic() != Proto2->isVariadic())
525 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000527 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000528 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
529 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
530 return false;
531 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
532 if (!IsStructurallyEquivalent(Context,
533 Proto1->getExceptionType(I),
534 Proto2->getExceptionType(I)))
535 return false;
536 }
537 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000538 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000539 Proto1->getNoexceptExpr(),
540 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000541 return false;
542 }
543 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
544 return false;
545
546 // Fall through to check the bits common with FunctionNoProtoType.
547 }
548
549 case Type::FunctionNoProto: {
550 const FunctionType *Function1 = cast<FunctionType>(T1);
551 const FunctionType *Function2 = cast<FunctionType>(T2);
552 if (!IsStructurallyEquivalent(Context,
553 Function1->getResultType(),
554 Function2->getResultType()))
555 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000556 if (Function1->getExtInfo() != Function2->getExtInfo())
557 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000558 break;
559 }
560
561 case Type::UnresolvedUsing:
562 if (!IsStructurallyEquivalent(Context,
563 cast<UnresolvedUsingType>(T1)->getDecl(),
564 cast<UnresolvedUsingType>(T2)->getDecl()))
565 return false;
566
567 break;
John McCall81904512011-01-06 01:58:22 +0000568
569 case Type::Attributed:
570 if (!IsStructurallyEquivalent(Context,
571 cast<AttributedType>(T1)->getModifiedType(),
572 cast<AttributedType>(T2)->getModifiedType()))
573 return false;
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getEquivalentType(),
576 cast<AttributedType>(T2)->getEquivalentType()))
577 return false;
578 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000579
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000580 case Type::Paren:
581 if (!IsStructurallyEquivalent(Context,
582 cast<ParenType>(T1)->getInnerType(),
583 cast<ParenType>(T2)->getInnerType()))
584 return false;
585 break;
586
Douglas Gregor3996e242010-02-15 22:01:00 +0000587 case Type::Typedef:
588 if (!IsStructurallyEquivalent(Context,
589 cast<TypedefType>(T1)->getDecl(),
590 cast<TypedefType>(T2)->getDecl()))
591 return false;
592 break;
593
594 case Type::TypeOfExpr:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
597 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
598 return false;
599 break;
600
601 case Type::TypeOf:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfType>(T1)->getUnderlyingType(),
604 cast<TypeOfType>(T2)->getUnderlyingType()))
605 return false;
606 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000607
608 case Type::UnaryTransform:
609 if (!IsStructurallyEquivalent(Context,
610 cast<UnaryTransformType>(T1)->getUnderlyingType(),
611 cast<UnaryTransformType>(T1)->getUnderlyingType()))
612 return false;
613 break;
614
Douglas Gregor3996e242010-02-15 22:01:00 +0000615 case Type::Decltype:
616 if (!IsStructurallyEquivalent(Context,
617 cast<DecltypeType>(T1)->getUnderlyingExpr(),
618 cast<DecltypeType>(T2)->getUnderlyingExpr()))
619 return false;
620 break;
621
Richard Smith30482bc2011-02-20 03:19:35 +0000622 case Type::Auto:
623 if (!IsStructurallyEquivalent(Context,
624 cast<AutoType>(T1)->getDeducedType(),
625 cast<AutoType>(T2)->getDeducedType()))
626 return false;
627 break;
628
Douglas Gregor3996e242010-02-15 22:01:00 +0000629 case Type::Record:
630 case Type::Enum:
631 if (!IsStructurallyEquivalent(Context,
632 cast<TagType>(T1)->getDecl(),
633 cast<TagType>(T2)->getDecl()))
634 return false;
635 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000636
Douglas Gregor3996e242010-02-15 22:01:00 +0000637 case Type::TemplateTypeParm: {
638 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
639 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
640 if (Parm1->getDepth() != Parm2->getDepth())
641 return false;
642 if (Parm1->getIndex() != Parm2->getIndex())
643 return false;
644 if (Parm1->isParameterPack() != Parm2->isParameterPack())
645 return false;
646
647 // Names of template type parameters are never significant.
648 break;
649 }
650
651 case Type::SubstTemplateTypeParm: {
652 const SubstTemplateTypeParmType *Subst1
653 = cast<SubstTemplateTypeParmType>(T1);
654 const SubstTemplateTypeParmType *Subst2
655 = cast<SubstTemplateTypeParmType>(T2);
656 if (!IsStructurallyEquivalent(Context,
657 QualType(Subst1->getReplacedParameter(), 0),
658 QualType(Subst2->getReplacedParameter(), 0)))
659 return false;
660 if (!IsStructurallyEquivalent(Context,
661 Subst1->getReplacementType(),
662 Subst2->getReplacementType()))
663 return false;
664 break;
665 }
666
Douglas Gregorfb322d82011-01-14 05:11:40 +0000667 case Type::SubstTemplateTypeParmPack: {
668 const SubstTemplateTypeParmPackType *Subst1
669 = cast<SubstTemplateTypeParmPackType>(T1);
670 const SubstTemplateTypeParmPackType *Subst2
671 = cast<SubstTemplateTypeParmPackType>(T2);
672 if (!IsStructurallyEquivalent(Context,
673 QualType(Subst1->getReplacedParameter(), 0),
674 QualType(Subst2->getReplacedParameter(), 0)))
675 return false;
676 if (!IsStructurallyEquivalent(Context,
677 Subst1->getArgumentPack(),
678 Subst2->getArgumentPack()))
679 return false;
680 break;
681 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000682 case Type::TemplateSpecialization: {
683 const TemplateSpecializationType *Spec1
684 = cast<TemplateSpecializationType>(T1);
685 const TemplateSpecializationType *Spec2
686 = cast<TemplateSpecializationType>(T2);
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getTemplateName(),
689 Spec2->getTemplateName()))
690 return false;
691 if (Spec1->getNumArgs() != Spec2->getNumArgs())
692 return false;
693 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getArg(I), Spec2->getArg(I)))
696 return false;
697 }
698 break;
699 }
700
Abramo Bagnara6150c882010-05-11 21:36:43 +0000701 case Type::Elaborated: {
702 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
703 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
704 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
705 if (Elab1->getKeyword() != Elab2->getKeyword())
706 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000708 Elab1->getQualifier(),
709 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000710 return false;
711 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000712 Elab1->getNamedType(),
713 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000714 return false;
715 break;
716 }
717
John McCalle78aac42010-03-10 03:28:59 +0000718 case Type::InjectedClassName: {
719 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
720 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
721 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000722 Inj1->getInjectedSpecializationType(),
723 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000724 return false;
725 break;
726 }
727
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000728 case Type::DependentName: {
729 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
730 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000731 if (!IsStructurallyEquivalent(Context,
732 Typename1->getQualifier(),
733 Typename2->getQualifier()))
734 return false;
735 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
736 Typename2->getIdentifier()))
737 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000738
739 break;
740 }
741
John McCallc392f372010-06-11 00:33:02 +0000742 case Type::DependentTemplateSpecialization: {
743 const DependentTemplateSpecializationType *Spec1 =
744 cast<DependentTemplateSpecializationType>(T1);
745 const DependentTemplateSpecializationType *Spec2 =
746 cast<DependentTemplateSpecializationType>(T2);
747 if (!IsStructurallyEquivalent(Context,
748 Spec1->getQualifier(),
749 Spec2->getQualifier()))
750 return false;
751 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
752 Spec2->getIdentifier()))
753 return false;
754 if (Spec1->getNumArgs() != Spec2->getNumArgs())
755 return false;
756 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
757 if (!IsStructurallyEquivalent(Context,
758 Spec1->getArg(I), Spec2->getArg(I)))
759 return false;
760 }
761 break;
762 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000763
764 case Type::PackExpansion:
765 if (!IsStructurallyEquivalent(Context,
766 cast<PackExpansionType>(T1)->getPattern(),
767 cast<PackExpansionType>(T2)->getPattern()))
768 return false;
769 break;
770
Douglas Gregor3996e242010-02-15 22:01:00 +0000771 case Type::ObjCInterface: {
772 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
773 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
774 if (!IsStructurallyEquivalent(Context,
775 Iface1->getDecl(), Iface2->getDecl()))
776 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000777 break;
778 }
779
780 case Type::ObjCObject: {
781 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
782 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
783 if (!IsStructurallyEquivalent(Context,
784 Obj1->getBaseType(),
785 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000786 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000787 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
788 return false;
789 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000790 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000791 Obj1->getProtocol(I),
792 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000793 return false;
794 }
795 break;
796 }
797
798 case Type::ObjCObjectPointer: {
799 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
800 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
801 if (!IsStructurallyEquivalent(Context,
802 Ptr1->getPointeeType(),
803 Ptr2->getPointeeType()))
804 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000805 break;
806 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000807
808 case Type::Atomic: {
809 if (!IsStructurallyEquivalent(Context,
810 cast<AtomicType>(T1)->getValueType(),
811 cast<AtomicType>(T2)->getValueType()))
812 return false;
813 break;
814 }
815
Douglas Gregor3996e242010-02-15 22:01:00 +0000816 } // end switch
817
818 return true;
819}
820
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000821/// \brief Determine structural equivalence of two fields.
822static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
823 FieldDecl *Field1, FieldDecl *Field2) {
824 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
825
826 if (!IsStructurallyEquivalent(Context,
827 Field1->getType(), Field2->getType())) {
828 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
829 << Context.C2.getTypeDeclType(Owner2);
830 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
831 << Field2->getDeclName() << Field2->getType();
832 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
833 << Field1->getDeclName() << Field1->getType();
834 return false;
835 }
836
837 if (Field1->isBitField() != Field2->isBitField()) {
838 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
839 << Context.C2.getTypeDeclType(Owner2);
840 if (Field1->isBitField()) {
841 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
842 << Field1->getDeclName() << Field1->getType()
843 << Field1->getBitWidthValue(Context.C1);
844 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
845 << Field2->getDeclName();
846 } else {
847 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
848 << Field2->getDeclName() << Field2->getType()
849 << Field2->getBitWidthValue(Context.C2);
850 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
851 << Field1->getDeclName();
852 }
853 return false;
854 }
855
856 if (Field1->isBitField()) {
857 // Make sure that the bit-fields are the same length.
858 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
859 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
860
861 if (Bits1 != Bits2) {
862 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(Owner2);
864 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
865 << Field2->getDeclName() << Field2->getType() << Bits2;
866 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
867 << Field1->getDeclName() << Field1->getType() << Bits1;
868 return false;
869 }
870 }
871
872 return true;
873}
874
Douglas Gregor3996e242010-02-15 22:01:00 +0000875/// \brief Determine structural equivalence of two records.
876static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
877 RecordDecl *D1, RecordDecl *D2) {
878 if (D1->isUnion() != D2->isUnion()) {
879 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
880 << Context.C2.getTypeDeclType(D2);
881 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
882 << D1->getDeclName() << (unsigned)D1->getTagKind();
883 return false;
884 }
885
Douglas Gregore2e50d332010-12-01 01:36:18 +0000886 // If both declarations are class template specializations, we know
887 // the ODR applies, so check the template and template arguments.
888 ClassTemplateSpecializationDecl *Spec1
889 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
890 ClassTemplateSpecializationDecl *Spec2
891 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
892 if (Spec1 && Spec2) {
893 // Check that the specialized templates are the same.
894 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
895 Spec2->getSpecializedTemplate()))
896 return false;
897
898 // Check that the template arguments are the same.
899 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
900 return false;
901
902 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
903 if (!IsStructurallyEquivalent(Context,
904 Spec1->getTemplateArgs().get(I),
905 Spec2->getTemplateArgs().get(I)))
906 return false;
907 }
908 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000909 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000910 else if (Spec1 || Spec2)
911 return false;
912
Douglas Gregorb4964f72010-02-15 23:54:17 +0000913 // Compare the definitions of these two records. If either or both are
914 // incomplete, we assume that they are equivalent.
915 D1 = D1->getDefinition();
916 D2 = D2->getDefinition();
917 if (!D1 || !D2)
918 return true;
919
Douglas Gregor3996e242010-02-15 22:01:00 +0000920 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
921 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
922 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
923 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000924 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000926 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000927 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000928 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000929 return false;
930 }
931
932 // Check the base classes.
933 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
934 BaseEnd1 = D1CXX->bases_end(),
935 Base2 = D2CXX->bases_begin();
936 Base1 != BaseEnd1;
937 ++Base1, ++Base2) {
938 if (!IsStructurallyEquivalent(Context,
939 Base1->getType(), Base2->getType())) {
940 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
941 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000942 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000943 << Base2->getType()
944 << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000945 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000946 << Base1->getType()
947 << Base1->getSourceRange();
948 return false;
949 }
950
951 // Check virtual vs. non-virtual inheritance mismatch.
952 if (Base1->isVirtual() != Base2->isVirtual()) {
953 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
954 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000955 Context.Diag2(Base2->getLocStart(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000956 diag::note_odr_virtual_base)
957 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000958 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000959 << Base1->isVirtual()
960 << Base1->getSourceRange();
961 return false;
962 }
963 }
964 } else if (D1CXX->getNumBases() > 0) {
965 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
966 << Context.C2.getTypeDeclType(D2);
967 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000968 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000969 << Base1->getType()
970 << Base1->getSourceRange();
971 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
972 return false;
973 }
974 }
975
976 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000977 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000978 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000979 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000980 Field1End = D1->field_end();
981 Field1 != Field1End;
982 ++Field1, ++Field2) {
983 if (Field2 == Field2End) {
984 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
985 << Context.C2.getTypeDeclType(D2);
986 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
987 << Field1->getDeclName() << Field1->getType();
988 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
989 return false;
990 }
991
David Blaikie40ed2972012-06-06 20:45:41 +0000992 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000993 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000994 }
995
996 if (Field2 != Field2End) {
997 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
998 << Context.C2.getTypeDeclType(D2);
999 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1000 << Field2->getDeclName() << Field2->getType();
1001 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1002 return false;
1003 }
1004
1005 return true;
1006}
1007
1008/// \brief Determine structural equivalence of two enums.
1009static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1010 EnumDecl *D1, EnumDecl *D2) {
1011 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1012 EC2End = D2->enumerator_end();
1013 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1014 EC1End = D1->enumerator_end();
1015 EC1 != EC1End; ++EC1, ++EC2) {
1016 if (EC2 == EC2End) {
1017 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1018 << Context.C2.getTypeDeclType(D2);
1019 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1020 << EC1->getDeclName()
1021 << EC1->getInitVal().toString(10);
1022 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1023 return false;
1024 }
1025
1026 llvm::APSInt Val1 = EC1->getInitVal();
1027 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001028 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001029 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1030 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1031 << Context.C2.getTypeDeclType(D2);
1032 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1033 << EC2->getDeclName()
1034 << EC2->getInitVal().toString(10);
1035 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1036 << EC1->getDeclName()
1037 << EC1->getInitVal().toString(10);
1038 return false;
1039 }
1040 }
1041
1042 if (EC2 != EC2End) {
1043 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1044 << Context.C2.getTypeDeclType(D2);
1045 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1046 << EC2->getDeclName()
1047 << EC2->getInitVal().toString(10);
1048 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1049 return false;
1050 }
1051
1052 return true;
1053}
Douglas Gregora082a492010-11-30 19:14:50 +00001054
1055static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1056 TemplateParameterList *Params1,
1057 TemplateParameterList *Params2) {
1058 if (Params1->size() != Params2->size()) {
1059 Context.Diag2(Params2->getTemplateLoc(),
1060 diag::err_odr_different_num_template_parameters)
1061 << Params1->size() << Params2->size();
1062 Context.Diag1(Params1->getTemplateLoc(),
1063 diag::note_odr_template_parameter_list);
1064 return false;
1065 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001066
Douglas Gregora082a492010-11-30 19:14:50 +00001067 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1068 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1069 Context.Diag2(Params2->getParam(I)->getLocation(),
1070 diag::err_odr_different_template_parameter_kind);
1071 Context.Diag1(Params1->getParam(I)->getLocation(),
1072 diag::note_odr_template_parameter_here);
1073 return false;
1074 }
1075
1076 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1077 Params2->getParam(I))) {
1078
1079 return false;
1080 }
1081 }
1082
1083 return true;
1084}
1085
1086static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1087 TemplateTypeParmDecl *D1,
1088 TemplateTypeParmDecl *D2) {
1089 if (D1->isParameterPack() != D2->isParameterPack()) {
1090 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1091 << D2->isParameterPack();
1092 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1093 << D1->isParameterPack();
1094 return false;
1095 }
1096
1097 return true;
1098}
1099
1100static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1101 NonTypeTemplateParmDecl *D1,
1102 NonTypeTemplateParmDecl *D2) {
1103 // FIXME: Enable once we have variadic templates.
1104#if 0
1105 if (D1->isParameterPack() != D2->isParameterPack()) {
1106 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1107 << D2->isParameterPack();
1108 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1109 << D1->isParameterPack();
1110 return false;
1111 }
1112#endif
1113
1114 // Check types.
1115 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1116 Context.Diag2(D2->getLocation(),
1117 diag::err_odr_non_type_parameter_type_inconsistent)
1118 << D2->getType() << D1->getType();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1120 << D1->getType();
1121 return false;
1122 }
1123
1124 return true;
1125}
1126
1127static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1128 TemplateTemplateParmDecl *D1,
1129 TemplateTemplateParmDecl *D2) {
1130 // FIXME: Enable once we have variadic templates.
1131#if 0
1132 if (D1->isParameterPack() != D2->isParameterPack()) {
1133 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1134 << D2->isParameterPack();
1135 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1136 << D1->isParameterPack();
1137 return false;
1138 }
1139#endif
1140
1141 // Check template parameter lists.
1142 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1143 D2->getTemplateParameters());
1144}
1145
1146static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1147 ClassTemplateDecl *D1,
1148 ClassTemplateDecl *D2) {
1149 // Check template parameters.
1150 if (!IsStructurallyEquivalent(Context,
1151 D1->getTemplateParameters(),
1152 D2->getTemplateParameters()))
1153 return false;
1154
1155 // Check the templated declaration.
1156 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1157 D2->getTemplatedDecl());
1158}
1159
Douglas Gregor3996e242010-02-15 22:01:00 +00001160/// \brief Determine structural equivalence of two declarations.
1161static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1162 Decl *D1, Decl *D2) {
1163 // FIXME: Check for known structural equivalences via a callback of some sort.
1164
Douglas Gregorb4964f72010-02-15 23:54:17 +00001165 // Check whether we already know that these two declarations are not
1166 // structurally equivalent.
1167 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1168 D2->getCanonicalDecl())))
1169 return false;
1170
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 // Determine whether we've already produced a tentative equivalence for D1.
1172 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1173 if (EquivToD1)
1174 return EquivToD1 == D2->getCanonicalDecl();
1175
1176 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1177 EquivToD1 = D2->getCanonicalDecl();
1178 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1179 return true;
1180}
1181
1182bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1183 Decl *D2) {
1184 if (!::IsStructurallyEquivalent(*this, D1, D2))
1185 return false;
1186
1187 return !Finish();
1188}
1189
1190bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1191 QualType T2) {
1192 if (!::IsStructurallyEquivalent(*this, T1, T2))
1193 return false;
1194
1195 return !Finish();
1196}
1197
1198bool StructuralEquivalenceContext::Finish() {
1199 while (!DeclsToCheck.empty()) {
1200 // Check the next declaration.
1201 Decl *D1 = DeclsToCheck.front();
1202 DeclsToCheck.pop_front();
1203
1204 Decl *D2 = TentativeEquivalences[D1];
1205 assert(D2 && "Unrecorded tentative equivalence?");
1206
Douglas Gregorb4964f72010-02-15 23:54:17 +00001207 bool Equivalent = true;
1208
Douglas Gregor3996e242010-02-15 22:01:00 +00001209 // FIXME: Switch on all declaration kinds. For now, we're just going to
1210 // check the obvious ones.
1211 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1212 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1213 // Check for equivalent structure names.
1214 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001215 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1216 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001217 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001218 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1219 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001220 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1221 !::IsStructurallyEquivalent(*this, Record1, Record2))
1222 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001223 } else {
1224 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001225 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001226 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001227 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1229 // Check for equivalent enum names.
1230 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001231 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1232 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001233 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001234 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1235 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001236 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1237 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1238 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001239 } else {
1240 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001241 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001242 }
Richard Smithdda56e42011-04-15 14:24:37 +00001243 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1244 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001245 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001246 Typedef2->getIdentifier()) ||
1247 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001248 Typedef1->getUnderlyingType(),
1249 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001250 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001251 } else {
1252 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001253 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001254 }
Douglas Gregora082a492010-11-30 19:14:50 +00001255 } else if (ClassTemplateDecl *ClassTemplate1
1256 = dyn_cast<ClassTemplateDecl>(D1)) {
1257 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1258 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1259 ClassTemplate2->getIdentifier()) ||
1260 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1261 Equivalent = false;
1262 } else {
1263 // Class template/non-class-template mismatch.
1264 Equivalent = false;
1265 }
1266 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1267 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1268 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1269 Equivalent = false;
1270 } else {
1271 // Kind mismatch.
1272 Equivalent = false;
1273 }
1274 } else if (NonTypeTemplateParmDecl *NTTP1
1275 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1276 if (NonTypeTemplateParmDecl *NTTP2
1277 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1278 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1279 Equivalent = false;
1280 } else {
1281 // Kind mismatch.
1282 Equivalent = false;
1283 }
1284 } else if (TemplateTemplateParmDecl *TTP1
1285 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1286 if (TemplateTemplateParmDecl *TTP2
1287 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1288 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1289 Equivalent = false;
1290 } else {
1291 // Kind mismatch.
1292 Equivalent = false;
1293 }
1294 }
1295
Douglas Gregorb4964f72010-02-15 23:54:17 +00001296 if (!Equivalent) {
1297 // Note that these two declarations are not equivalent (and we already
1298 // know about it).
1299 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1300 D2->getCanonicalDecl()));
1301 return true;
1302 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001303 // FIXME: Check other declaration kinds!
1304 }
1305
1306 return false;
1307}
1308
1309//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001310// Import Types
1311//----------------------------------------------------------------------------
1312
John McCall424cec92011-01-19 06:33:43 +00001313QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001314 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1315 << T->getTypeClassName();
1316 return QualType();
1317}
1318
John McCall424cec92011-01-19 06:33:43 +00001319QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001320 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001321#define SHARED_SINGLETON_TYPE(Expansion)
1322#define BUILTIN_TYPE(Id, SingletonId) \
1323 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1324#include "clang/AST/BuiltinTypes.def"
1325
1326 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1327 // context supports C++.
1328
1329 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1330 // context supports ObjC.
1331
Douglas Gregor96e578d2010-02-05 17:54:41 +00001332 case BuiltinType::Char_U:
1333 // The context we're importing from has an unsigned 'char'. If we're
1334 // importing into a context with a signed 'char', translate to
1335 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001336 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001337 return Importer.getToContext().UnsignedCharTy;
1338
1339 return Importer.getToContext().CharTy;
1340
Douglas Gregor96e578d2010-02-05 17:54:41 +00001341 case BuiltinType::Char_S:
1342 // The context we're importing from has an unsigned 'char'. If we're
1343 // importing into a context with a signed 'char', translate to
1344 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001345 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346 return Importer.getToContext().SignedCharTy;
1347
1348 return Importer.getToContext().CharTy;
1349
Chris Lattnerad3467e2010-12-25 23:25:43 +00001350 case BuiltinType::WChar_S:
1351 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001352 // FIXME: If not in C++, shall we translate to the C equivalent of
1353 // wchar_t?
1354 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001355 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001356
1357 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001358}
1359
John McCall424cec92011-01-19 06:33:43 +00001360QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001361 QualType ToElementType = Importer.Import(T->getElementType());
1362 if (ToElementType.isNull())
1363 return QualType();
1364
1365 return Importer.getToContext().getComplexType(ToElementType);
1366}
1367
John McCall424cec92011-01-19 06:33:43 +00001368QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001369 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1370 if (ToPointeeType.isNull())
1371 return QualType();
1372
1373 return Importer.getToContext().getPointerType(ToPointeeType);
1374}
1375
John McCall424cec92011-01-19 06:33:43 +00001376QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001377 // FIXME: Check for blocks support in "to" context.
1378 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1379 if (ToPointeeType.isNull())
1380 return QualType();
1381
1382 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1383}
1384
John McCall424cec92011-01-19 06:33:43 +00001385QualType
1386ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001387 // FIXME: Check for C++ support in "to" context.
1388 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1389 if (ToPointeeType.isNull())
1390 return QualType();
1391
1392 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1393}
1394
John McCall424cec92011-01-19 06:33:43 +00001395QualType
1396ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001397 // FIXME: Check for C++0x support in "to" context.
1398 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1399 if (ToPointeeType.isNull())
1400 return QualType();
1401
1402 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1403}
1404
John McCall424cec92011-01-19 06:33:43 +00001405QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001406 // FIXME: Check for C++ support in "to" context.
1407 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1408 if (ToPointeeType.isNull())
1409 return QualType();
1410
1411 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1412 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1413 ClassType.getTypePtr());
1414}
1415
John McCall424cec92011-01-19 06:33:43 +00001416QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001417 QualType ToElementType = Importer.Import(T->getElementType());
1418 if (ToElementType.isNull())
1419 return QualType();
1420
1421 return Importer.getToContext().getConstantArrayType(ToElementType,
1422 T->getSize(),
1423 T->getSizeModifier(),
1424 T->getIndexTypeCVRQualifiers());
1425}
1426
John McCall424cec92011-01-19 06:33:43 +00001427QualType
1428ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001429 QualType ToElementType = Importer.Import(T->getElementType());
1430 if (ToElementType.isNull())
1431 return QualType();
1432
1433 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1434 T->getSizeModifier(),
1435 T->getIndexTypeCVRQualifiers());
1436}
1437
John McCall424cec92011-01-19 06:33:43 +00001438QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439 QualType ToElementType = Importer.Import(T->getElementType());
1440 if (ToElementType.isNull())
1441 return QualType();
1442
1443 Expr *Size = Importer.Import(T->getSizeExpr());
1444 if (!Size)
1445 return QualType();
1446
1447 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1448 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1449 T->getSizeModifier(),
1450 T->getIndexTypeCVRQualifiers(),
1451 Brackets);
1452}
1453
John McCall424cec92011-01-19 06:33:43 +00001454QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001455 QualType ToElementType = Importer.Import(T->getElementType());
1456 if (ToElementType.isNull())
1457 return QualType();
1458
1459 return Importer.getToContext().getVectorType(ToElementType,
1460 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001461 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001462}
1463
John McCall424cec92011-01-19 06:33:43 +00001464QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001465 QualType ToElementType = Importer.Import(T->getElementType());
1466 if (ToElementType.isNull())
1467 return QualType();
1468
1469 return Importer.getToContext().getExtVectorType(ToElementType,
1470 T->getNumElements());
1471}
1472
John McCall424cec92011-01-19 06:33:43 +00001473QualType
1474ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001475 // FIXME: What happens if we're importing a function without a prototype
1476 // into C++? Should we make it variadic?
1477 QualType ToResultType = Importer.Import(T->getResultType());
1478 if (ToResultType.isNull())
1479 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001480
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001482 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483}
1484
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001485static QualType importFunctionProtoType(ASTImporter &Importer,
1486 const FunctionProtoType *T,
1487 bool importExceptionSpecDecls) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001488 QualType ToResultType = Importer.Import(T->getResultType());
1489 if (ToResultType.isNull())
1490 return QualType();
1491
1492 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001493 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1495 AEnd = T->arg_type_end();
1496 A != AEnd; ++A) {
1497 QualType ArgType = Importer.Import(*A);
1498 if (ArgType.isNull())
1499 return QualType();
1500 ArgTypes.push_back(ArgType);
1501 }
1502
1503 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001504 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001505 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1506 EEnd = T->exception_end();
1507 E != EEnd; ++E) {
1508 QualType ExceptionType = Importer.Import(*E);
1509 if (ExceptionType.isNull())
1510 return QualType();
1511 ExceptionTypes.push_back(ExceptionType);
1512 }
John McCalldb40c7f2010-12-14 08:05:40 +00001513
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001514 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1515 FunctionProtoType::ExtProtoInfo ToEPI;
1516
1517 ToEPI.ExtInfo = FromEPI.ExtInfo;
1518 ToEPI.Variadic = FromEPI.Variadic;
1519 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1520 ToEPI.TypeQuals = FromEPI.TypeQuals;
1521 ToEPI.RefQualifier = FromEPI.RefQualifier;
1522 ToEPI.NumExceptions = ExceptionTypes.size();
1523 ToEPI.Exceptions = ExceptionTypes.data();
1524 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
1525
1526 if (importExceptionSpecDecls) {
1527 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1528 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1529 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
1530 Importer.Import(FromEPI.ExceptionSpecDecl));
1531 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
1532 Importer.Import(FromEPI.ExceptionSpecTemplate));
1533 }
1534
Douglas Gregor96e578d2010-02-05 17:54:41 +00001535 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001536 ArgTypes.size(), ToEPI);
1537}
1538
1539QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
1540 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
1541 // FunctionDecl that we are importing this FunctionProtoType for.
1542 // Update it in ASTNodeImporter::VisitFunctionDecl after the FunctionDecl has
1543 // been created.
1544 return importFunctionProtoType(Importer, T,
1545 /*importExceptionSpecDecls=*/false);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001546}
1547
Sean Callananda6df8a2011-08-11 16:56:07 +00001548QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1549 QualType ToInnerType = Importer.Import(T->getInnerType());
1550 if (ToInnerType.isNull())
1551 return QualType();
1552
1553 return Importer.getToContext().getParenType(ToInnerType);
1554}
1555
John McCall424cec92011-01-19 06:33:43 +00001556QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001557 TypedefNameDecl *ToDecl
1558 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001559 if (!ToDecl)
1560 return QualType();
1561
1562 return Importer.getToContext().getTypeDeclType(ToDecl);
1563}
1564
John McCall424cec92011-01-19 06:33:43 +00001565QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001566 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1567 if (!ToExpr)
1568 return QualType();
1569
1570 return Importer.getToContext().getTypeOfExprType(ToExpr);
1571}
1572
John McCall424cec92011-01-19 06:33:43 +00001573QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001574 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1575 if (ToUnderlyingType.isNull())
1576 return QualType();
1577
1578 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1579}
1580
John McCall424cec92011-01-19 06:33:43 +00001581QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001582 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001583 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1584 if (!ToExpr)
1585 return QualType();
1586
Douglas Gregor81495f32012-02-12 18:42:33 +00001587 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1588 if (UnderlyingType.isNull())
1589 return QualType();
1590
1591 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592}
1593
Alexis Hunte852b102011-05-24 22:41:36 +00001594QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1595 QualType ToBaseType = Importer.Import(T->getBaseType());
1596 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1597 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1598 return QualType();
1599
1600 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1601 ToUnderlyingType,
1602 T->getUTTKind());
1603}
1604
Richard Smith30482bc2011-02-20 03:19:35 +00001605QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1606 // FIXME: Make sure that the "to" context supports C++0x!
1607 QualType FromDeduced = T->getDeducedType();
1608 QualType ToDeduced;
1609 if (!FromDeduced.isNull()) {
1610 ToDeduced = Importer.Import(FromDeduced);
1611 if (ToDeduced.isNull())
1612 return QualType();
1613 }
1614
1615 return Importer.getToContext().getAutoType(ToDeduced);
1616}
1617
John McCall424cec92011-01-19 06:33:43 +00001618QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001619 RecordDecl *ToDecl
1620 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1621 if (!ToDecl)
1622 return QualType();
1623
1624 return Importer.getToContext().getTagDeclType(ToDecl);
1625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001628 EnumDecl *ToDecl
1629 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1630 if (!ToDecl)
1631 return QualType();
1632
1633 return Importer.getToContext().getTagDeclType(ToDecl);
1634}
1635
Douglas Gregore2e50d332010-12-01 01:36:18 +00001636QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001637 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001638 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1639 if (ToTemplate.isNull())
1640 return QualType();
1641
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001642 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001643 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1644 return QualType();
1645
1646 QualType ToCanonType;
1647 if (!QualType(T, 0).isCanonical()) {
1648 QualType FromCanonType
1649 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1650 ToCanonType =Importer.Import(FromCanonType);
1651 if (ToCanonType.isNull())
1652 return QualType();
1653 }
1654 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1655 ToTemplateArgs.data(),
1656 ToTemplateArgs.size(),
1657 ToCanonType);
1658}
1659
John McCall424cec92011-01-19 06:33:43 +00001660QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001661 NestedNameSpecifier *ToQualifier = 0;
1662 // Note: the qualifier in an ElaboratedType is optional.
1663 if (T->getQualifier()) {
1664 ToQualifier = Importer.Import(T->getQualifier());
1665 if (!ToQualifier)
1666 return QualType();
1667 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001668
1669 QualType ToNamedType = Importer.Import(T->getNamedType());
1670 if (ToNamedType.isNull())
1671 return QualType();
1672
Abramo Bagnara6150c882010-05-11 21:36:43 +00001673 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1674 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001675}
1676
John McCall424cec92011-01-19 06:33:43 +00001677QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001678 ObjCInterfaceDecl *Class
1679 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1680 if (!Class)
1681 return QualType();
1682
John McCall8b07ec22010-05-15 11:32:37 +00001683 return Importer.getToContext().getObjCInterfaceType(Class);
1684}
1685
John McCall424cec92011-01-19 06:33:43 +00001686QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001687 QualType ToBaseType = Importer.Import(T->getBaseType());
1688 if (ToBaseType.isNull())
1689 return QualType();
1690
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001691 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001692 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001693 PEnd = T->qual_end();
1694 P != PEnd; ++P) {
1695 ObjCProtocolDecl *Protocol
1696 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1697 if (!Protocol)
1698 return QualType();
1699 Protocols.push_back(Protocol);
1700 }
1701
John McCall8b07ec22010-05-15 11:32:37 +00001702 return Importer.getToContext().getObjCObjectType(ToBaseType,
1703 Protocols.data(),
1704 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001705}
1706
John McCall424cec92011-01-19 06:33:43 +00001707QualType
1708ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001709 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1710 if (ToPointeeType.isNull())
1711 return QualType();
1712
John McCall8b07ec22010-05-15 11:32:37 +00001713 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001714}
1715
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001716//----------------------------------------------------------------------------
1717// Import Declarations
1718//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001719bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1720 DeclContext *&LexicalDC,
1721 DeclarationName &Name,
1722 SourceLocation &Loc) {
1723 // Import the context of this declaration.
1724 DC = Importer.ImportContext(D->getDeclContext());
1725 if (!DC)
1726 return true;
1727
1728 LexicalDC = DC;
1729 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1730 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1731 if (!LexicalDC)
1732 return true;
1733 }
1734
1735 // Import the name of this declaration.
1736 Name = Importer.Import(D->getDeclName());
1737 if (D->getDeclName() && !Name)
1738 return true;
1739
1740 // Import the location of this declaration.
1741 Loc = Importer.Import(D->getLocation());
1742 return false;
1743}
1744
Douglas Gregord451ea92011-07-29 23:31:30 +00001745void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1746 if (!FromD)
1747 return;
1748
1749 if (!ToD) {
1750 ToD = Importer.Import(FromD);
1751 if (!ToD)
1752 return;
1753 }
1754
1755 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1756 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1757 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1758 ImportDefinition(FromRecord, ToRecord);
1759 }
1760 }
1761 return;
1762 }
1763
1764 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1765 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1766 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1767 ImportDefinition(FromEnum, ToEnum);
1768 }
1769 }
1770 return;
1771 }
1772}
1773
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001774void
1775ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1776 DeclarationNameInfo& To) {
1777 // NOTE: To.Name and To.Loc are already imported.
1778 // We only have to import To.LocInfo.
1779 switch (To.getName().getNameKind()) {
1780 case DeclarationName::Identifier:
1781 case DeclarationName::ObjCZeroArgSelector:
1782 case DeclarationName::ObjCOneArgSelector:
1783 case DeclarationName::ObjCMultiArgSelector:
1784 case DeclarationName::CXXUsingDirective:
1785 return;
1786
1787 case DeclarationName::CXXOperatorName: {
1788 SourceRange Range = From.getCXXOperatorNameRange();
1789 To.setCXXOperatorNameRange(Importer.Import(Range));
1790 return;
1791 }
1792 case DeclarationName::CXXLiteralOperatorName: {
1793 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1794 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1795 return;
1796 }
1797 case DeclarationName::CXXConstructorName:
1798 case DeclarationName::CXXDestructorName:
1799 case DeclarationName::CXXConversionFunctionName: {
1800 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1801 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1802 return;
1803 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001804 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001805 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001806}
1807
Douglas Gregor2e15c842012-02-01 21:00:38 +00001808void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001809 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001810 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001811 return;
1812 }
1813
Douglas Gregor968d6332010-02-21 18:24:45 +00001814 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1815 FromEnd = FromDC->decls_end();
1816 From != FromEnd;
1817 ++From)
1818 Importer.Import(*From);
1819}
1820
Douglas Gregord451ea92011-07-29 23:31:30 +00001821bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001822 ImportDefinitionKind Kind) {
1823 if (To->getDefinition() || To->isBeingDefined()) {
1824 if (Kind == IDK_Everything)
1825 ImportDeclContext(From, /*ForceImport=*/true);
1826
Douglas Gregore2e50d332010-12-01 01:36:18 +00001827 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001828 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001829
1830 To->startDefinition();
1831
1832 // Add base classes.
1833 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1834 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001835
1836 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1837 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1838 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1839 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1840 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1841 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1842 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1843 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1844 ToData.Aggregate = FromData.Aggregate;
1845 ToData.PlainOldData = FromData.PlainOldData;
1846 ToData.Empty = FromData.Empty;
1847 ToData.Polymorphic = FromData.Polymorphic;
1848 ToData.Abstract = FromData.Abstract;
1849 ToData.IsStandardLayout = FromData.IsStandardLayout;
1850 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1851 ToData.HasPrivateFields = FromData.HasPrivateFields;
1852 ToData.HasProtectedFields = FromData.HasProtectedFields;
1853 ToData.HasPublicFields = FromData.HasPublicFields;
1854 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001855 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001856 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001857 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1858 ToData.HasConstexprNonCopyMoveConstructor
1859 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001860 ToData.DefaultedDefaultConstructorIsConstexpr
1861 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001862 ToData.HasConstexprDefaultConstructor
1863 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001864 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1865 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1866 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1867 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1868 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001869 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001870 ToData.HasNonLiteralTypeFieldsOrBases
1871 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001872 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001873 ToData.UserProvidedDefaultConstructor
1874 = FromData.UserProvidedDefaultConstructor;
1875 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1876 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1877 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1878 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1879 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1880 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1881 ToData.FailedImplicitMoveConstructor
1882 = FromData.FailedImplicitMoveConstructor;
1883 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001884 ToData.IsLambda = FromData.IsLambda;
1885
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001886 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001887 for (CXXRecordDecl::base_class_iterator
1888 Base1 = FromCXX->bases_begin(),
1889 FromBaseEnd = FromCXX->bases_end();
1890 Base1 != FromBaseEnd;
1891 ++Base1) {
1892 QualType T = Importer.Import(Base1->getType());
1893 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001894 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001895
1896 SourceLocation EllipsisLoc;
1897 if (Base1->isPackExpansion())
1898 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001899
1900 // Ensure that we have a definition for the base.
1901 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1902
Douglas Gregore2e50d332010-12-01 01:36:18 +00001903 Bases.push_back(
1904 new (Importer.getToContext())
1905 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1906 Base1->isVirtual(),
1907 Base1->isBaseOfClass(),
1908 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001909 Importer.Import(Base1->getTypeSourceInfo()),
1910 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001911 }
1912 if (!Bases.empty())
1913 ToCXX->setBases(Bases.data(), Bases.size());
1914 }
1915
Douglas Gregor2e15c842012-02-01 21:00:38 +00001916 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001917 ImportDeclContext(From, /*ForceImport=*/true);
1918
Douglas Gregore2e50d332010-12-01 01:36:18 +00001919 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001920 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001921}
1922
Douglas Gregord451ea92011-07-29 23:31:30 +00001923bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001924 ImportDefinitionKind Kind) {
1925 if (To->getDefinition() || To->isBeingDefined()) {
1926 if (Kind == IDK_Everything)
1927 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001928 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001929 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001930
1931 To->startDefinition();
1932
1933 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1934 if (T.isNull())
1935 return true;
1936
1937 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1938 if (ToPromotionType.isNull())
1939 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001940
1941 if (shouldForceImportDeclContext(Kind))
1942 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001943
1944 // FIXME: we might need to merge the number of positive or negative bits
1945 // if the enumerator lists don't match.
1946 To->completeDefinition(T, ToPromotionType,
1947 From->getNumPositiveBits(),
1948 From->getNumNegativeBits());
1949 return false;
1950}
1951
Douglas Gregora082a492010-11-30 19:14:50 +00001952TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1953 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001954 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001955 ToParams.reserve(Params->size());
1956 for (TemplateParameterList::iterator P = Params->begin(),
1957 PEnd = Params->end();
1958 P != PEnd; ++P) {
1959 Decl *To = Importer.Import(*P);
1960 if (!To)
1961 return 0;
1962
1963 ToParams.push_back(cast<NamedDecl>(To));
1964 }
1965
1966 return TemplateParameterList::Create(Importer.getToContext(),
1967 Importer.Import(Params->getTemplateLoc()),
1968 Importer.Import(Params->getLAngleLoc()),
1969 ToParams.data(), ToParams.size(),
1970 Importer.Import(Params->getRAngleLoc()));
1971}
1972
Douglas Gregore2e50d332010-12-01 01:36:18 +00001973TemplateArgument
1974ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1975 switch (From.getKind()) {
1976 case TemplateArgument::Null:
1977 return TemplateArgument();
1978
1979 case TemplateArgument::Type: {
1980 QualType ToType = Importer.Import(From.getAsType());
1981 if (ToType.isNull())
1982 return TemplateArgument();
1983 return TemplateArgument(ToType);
1984 }
1985
1986 case TemplateArgument::Integral: {
1987 QualType ToType = Importer.Import(From.getIntegralType());
1988 if (ToType.isNull())
1989 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001990 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001991 }
1992
1993 case TemplateArgument::Declaration:
1994 if (Decl *To = Importer.Import(From.getAsDecl()))
1995 return TemplateArgument(To);
1996 return TemplateArgument();
1997
1998 case TemplateArgument::Template: {
1999 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2000 if (ToTemplate.isNull())
2001 return TemplateArgument();
2002
2003 return TemplateArgument(ToTemplate);
2004 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002005
2006 case TemplateArgument::TemplateExpansion: {
2007 TemplateName ToTemplate
2008 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2009 if (ToTemplate.isNull())
2010 return TemplateArgument();
2011
Douglas Gregore1d60df2011-01-14 23:41:42 +00002012 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002013 }
2014
Douglas Gregore2e50d332010-12-01 01:36:18 +00002015 case TemplateArgument::Expression:
2016 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2017 return TemplateArgument(ToExpr);
2018 return TemplateArgument();
2019
2020 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002021 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002022 ToPack.reserve(From.pack_size());
2023 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2024 return TemplateArgument();
2025
2026 TemplateArgument *ToArgs
2027 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2028 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2029 return TemplateArgument(ToArgs, ToPack.size());
2030 }
2031 }
2032
2033 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002034}
2035
2036bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2037 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002038 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002039 for (unsigned I = 0; I != NumFromArgs; ++I) {
2040 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2041 if (To.isNull() && !FromArgs[I].isNull())
2042 return true;
2043
2044 ToArgs.push_back(To);
2045 }
2046
2047 return false;
2048}
2049
Douglas Gregor5c73e912010-02-11 00:48:18 +00002050bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002051 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002052 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002053 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002054 Importer.getNonEquivalentDecls(),
2055 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002056 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002057}
2058
Douglas Gregor98c10182010-02-12 22:17:39 +00002059bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002060 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002061 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002062 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002063 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002064}
2065
Douglas Gregora082a492010-11-30 19:14:50 +00002066bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2067 ClassTemplateDecl *To) {
2068 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2069 Importer.getToContext(),
2070 Importer.getNonEquivalentDecls());
2071 return Ctx.IsStructurallyEquivalent(From, To);
2072}
2073
Douglas Gregore4c83e42010-02-09 22:48:33 +00002074Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002075 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002076 << D->getDeclKindName();
2077 return 0;
2078}
2079
Sean Callanan65198272011-11-17 23:20:56 +00002080Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2081 TranslationUnitDecl *ToD =
2082 Importer.getToContext().getTranslationUnitDecl();
2083
2084 Importer.Imported(D, ToD);
2085
2086 return ToD;
2087}
2088
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002089Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2090 // Import the major distinguishing characteristics of this namespace.
2091 DeclContext *DC, *LexicalDC;
2092 DeclarationName Name;
2093 SourceLocation Loc;
2094 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2095 return 0;
2096
2097 NamespaceDecl *MergeWithNamespace = 0;
2098 if (!Name) {
2099 // This is an anonymous namespace. Adopt an existing anonymous
2100 // namespace if we can.
2101 // FIXME: Not testable.
2102 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2103 MergeWithNamespace = TU->getAnonymousNamespace();
2104 else
2105 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2106 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002107 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002108 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2109 DC->localUncachedLookup(Name, FoundDecls);
2110 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2111 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002112 continue;
2113
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002114 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002115 MergeWithNamespace = FoundNS;
2116 ConflictingDecls.clear();
2117 break;
2118 }
2119
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002120 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002121 }
2122
2123 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002124 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002125 ConflictingDecls.data(),
2126 ConflictingDecls.size());
2127 }
2128 }
2129
2130 // Create the "to" namespace, if needed.
2131 NamespaceDecl *ToNamespace = MergeWithNamespace;
2132 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002133 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002134 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002135 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002136 Loc, Name.getAsIdentifierInfo(),
2137 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002138 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002139 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002140
2141 // If this is an anonymous namespace, register it as the anonymous
2142 // namespace within its context.
2143 if (!Name) {
2144 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2145 TU->setAnonymousNamespace(ToNamespace);
2146 else
2147 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2148 }
2149 }
2150 Importer.Imported(D, ToNamespace);
2151
2152 ImportDeclContext(D);
2153
2154 return ToNamespace;
2155}
2156
Richard Smithdda56e42011-04-15 14:24:37 +00002157Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002158 // Import the major distinguishing characteristics of this typedef.
2159 DeclContext *DC, *LexicalDC;
2160 DeclarationName Name;
2161 SourceLocation Loc;
2162 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2163 return 0;
2164
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002165 // If this typedef is not in block scope, determine whether we've
2166 // seen a typedef with the same name (that we can merge with) or any
2167 // other entity by that name (which name lookup could conflict with).
2168 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002169 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002170 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002171 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2172 DC->localUncachedLookup(Name, FoundDecls);
2173 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2174 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002175 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002176 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002177 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002178 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2179 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002180 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002181 }
2182
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002183 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002184 }
2185
2186 if (!ConflictingDecls.empty()) {
2187 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2188 ConflictingDecls.data(),
2189 ConflictingDecls.size());
2190 if (!Name)
2191 return 0;
2192 }
2193 }
2194
Douglas Gregorb4964f72010-02-15 23:54:17 +00002195 // Import the underlying type of this typedef;
2196 QualType T = Importer.Import(D->getUnderlyingType());
2197 if (T.isNull())
2198 return 0;
2199
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002200 // Create the new typedef node.
2201 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002202 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002203 TypedefNameDecl *ToTypedef;
2204 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002205 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2206 StartL, Loc,
2207 Name.getAsIdentifierInfo(),
2208 TInfo);
2209 else
Richard Smithdda56e42011-04-15 14:24:37 +00002210 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2211 StartL, Loc,
2212 Name.getAsIdentifierInfo(),
2213 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002214
Douglas Gregordd483172010-02-22 17:42:47 +00002215 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002216 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002217 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002218 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002219
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002220 return ToTypedef;
2221}
2222
Richard Smithdda56e42011-04-15 14:24:37 +00002223Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2224 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2225}
2226
2227Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2228 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2229}
2230
Douglas Gregor98c10182010-02-12 22:17:39 +00002231Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2232 // Import the major distinguishing characteristics of this enum.
2233 DeclContext *DC, *LexicalDC;
2234 DeclarationName Name;
2235 SourceLocation Loc;
2236 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2237 return 0;
2238
2239 // Figure out what enum name we're looking for.
2240 unsigned IDNS = Decl::IDNS_Tag;
2241 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002242 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2243 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002244 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002245 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002246 IDNS |= Decl::IDNS_Ordinary;
2247
2248 // We may already have an enum of the same name; try to find and match it.
2249 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002250 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002251 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2252 DC->localUncachedLookup(SearchName, FoundDecls);
2253 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2254 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002255 continue;
2256
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002257 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002258 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002259 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2260 Found = Tag->getDecl();
2261 }
2262
2263 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002264 if (IsStructuralMatch(D, FoundEnum))
2265 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002266 }
2267
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002268 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002269 }
2270
2271 if (!ConflictingDecls.empty()) {
2272 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2273 ConflictingDecls.data(),
2274 ConflictingDecls.size());
2275 }
2276 }
2277
2278 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002279 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2280 Importer.Import(D->getLocStart()),
2281 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002282 D->isScoped(), D->isScopedUsingClassTag(),
2283 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002284 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002285 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002286 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002287 D2->setLexicalDeclContext(LexicalDC);
2288 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002289 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002290
2291 // Import the integer type.
2292 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2293 if (ToIntegerType.isNull())
2294 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002295 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002296
2297 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002298 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002299 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002300
Douglas Gregor3996e242010-02-15 22:01:00 +00002301 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002302}
2303
Douglas Gregor5c73e912010-02-11 00:48:18 +00002304Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2305 // If this record has a definition in the translation unit we're coming from,
2306 // but this particular declaration is not that definition, import the
2307 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002308 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002309 if (Definition && Definition != D) {
2310 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002311 if (!ImportedDef)
2312 return 0;
2313
2314 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002315 }
2316
2317 // Import the major distinguishing characteristics of this record.
2318 DeclContext *DC, *LexicalDC;
2319 DeclarationName Name;
2320 SourceLocation Loc;
2321 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2322 return 0;
2323
2324 // Figure out what structure name we're looking for.
2325 unsigned IDNS = Decl::IDNS_Tag;
2326 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002327 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2328 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002329 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002330 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002331 IDNS |= Decl::IDNS_Ordinary;
2332
2333 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002334 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002335 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002336 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002337 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2338 DC->localUncachedLookup(SearchName, FoundDecls);
2339 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2340 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002341 continue;
2342
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002343 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002344 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002345 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2346 Found = Tag->getDecl();
2347 }
2348
2349 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002350 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002351 if ((SearchName && !D->isCompleteDefinition())
2352 || (D->isCompleteDefinition() &&
2353 D->isAnonymousStructOrUnion()
2354 == FoundDef->isAnonymousStructOrUnion() &&
2355 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002356 // The record types structurally match, or the "from" translation
2357 // unit only had a forward declaration anyway; call it the same
2358 // function.
2359 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002360 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002361 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002362 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002363 // We have a forward declaration of this type, so adopt that forward
2364 // declaration rather than building a new one.
2365 AdoptDecl = FoundRecord;
2366 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002367 } else if (!SearchName) {
2368 continue;
2369 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002370 }
2371
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002372 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002373 }
2374
Douglas Gregordd6006f2012-07-17 21:16:27 +00002375 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002376 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2377 ConflictingDecls.data(),
2378 ConflictingDecls.size());
2379 }
2380 }
2381
2382 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002383 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002384 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002385 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002386 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002387 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002388 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002389 DC, StartLoc, Loc,
2390 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002391 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002392 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002393 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002394 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002395 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002396 }
Douglas Gregor14454802011-02-25 02:25:35 +00002397
2398 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002399 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002400 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002401 if (D->isAnonymousStructOrUnion())
2402 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002403 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002404
Douglas Gregor3996e242010-02-15 22:01:00 +00002405 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002406
Douglas Gregor95d82832012-01-24 18:36:04 +00002407 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002408 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002409
Douglas Gregor3996e242010-02-15 22:01:00 +00002410 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002411}
2412
Douglas Gregor98c10182010-02-12 22:17:39 +00002413Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2414 // Import the major distinguishing characteristics of this enumerator.
2415 DeclContext *DC, *LexicalDC;
2416 DeclarationName Name;
2417 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002418 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002419 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002420
2421 QualType T = Importer.Import(D->getType());
2422 if (T.isNull())
2423 return 0;
2424
Douglas Gregor98c10182010-02-12 22:17:39 +00002425 // Determine whether there are any other declarations with the same name and
2426 // in the same context.
2427 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002428 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002429 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002430 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2431 DC->localUncachedLookup(Name, FoundDecls);
2432 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2433 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002434 continue;
2435
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002436 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002437 }
2438
2439 if (!ConflictingDecls.empty()) {
2440 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2441 ConflictingDecls.data(),
2442 ConflictingDecls.size());
2443 if (!Name)
2444 return 0;
2445 }
2446 }
2447
2448 Expr *Init = Importer.Import(D->getInitExpr());
2449 if (D->getInitExpr() && !Init)
2450 return 0;
2451
2452 EnumConstantDecl *ToEnumerator
2453 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2454 Name.getAsIdentifierInfo(), T,
2455 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002456 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002457 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002458 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002459 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002460 return ToEnumerator;
2461}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002462
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002463Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2464 // Import the major distinguishing characteristics of this function.
2465 DeclContext *DC, *LexicalDC;
2466 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002467 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002468 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002469 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002470
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002471 // Try to find a function in our own ("to") context with the same name, same
2472 // type, and in the same context as the function we're importing.
2473 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002474 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002475 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002476 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2477 DC->localUncachedLookup(Name, FoundDecls);
2478 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2479 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002480 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002481
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002482 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002483 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2484 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002485 if (Importer.IsStructurallyEquivalent(D->getType(),
2486 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002487 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002488 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002489 }
2490
2491 // FIXME: Check for overloading more carefully, e.g., by boosting
2492 // Sema::IsOverload out to the AST library.
2493
2494 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002495 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002496 continue;
2497
2498 // Complain about inconsistent function types.
2499 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002500 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002501 Importer.ToDiag(FoundFunction->getLocation(),
2502 diag::note_odr_value_here)
2503 << FoundFunction->getType();
2504 }
2505 }
2506
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002507 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002508 }
2509
2510 if (!ConflictingDecls.empty()) {
2511 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2512 ConflictingDecls.data(),
2513 ConflictingDecls.size());
2514 if (!Name)
2515 return 0;
2516 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002517 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002518
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002519 DeclarationNameInfo NameInfo(Name, Loc);
2520 // Import additional name location/type info.
2521 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2522
Douglas Gregorb4964f72010-02-15 23:54:17 +00002523 // Import the type.
2524 QualType T = Importer.Import(D->getType());
2525 if (T.isNull())
2526 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002527
2528 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002529 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002530 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2531 P != PEnd; ++P) {
2532 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2533 if (!ToP)
2534 return 0;
2535
2536 Parameters.push_back(ToP);
2537 }
2538
2539 // Create the imported function.
2540 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002541 FunctionDecl *ToFunction = 0;
2542 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2543 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2544 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002545 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002546 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002547 FromConstructor->isExplicit(),
2548 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002549 D->isImplicit(),
2550 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002551 } else if (isa<CXXDestructorDecl>(D)) {
2552 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2553 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002554 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002555 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002556 D->isInlineSpecified(),
2557 D->isImplicit());
2558 } else if (CXXConversionDecl *FromConversion
2559 = dyn_cast<CXXConversionDecl>(D)) {
2560 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2561 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002562 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002563 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002564 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002565 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002566 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002567 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002568 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2569 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2570 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002571 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002572 NameInfo, T, TInfo,
2573 Method->isStatic(),
2574 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002575 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002576 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002577 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002578 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002579 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002580 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002581 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002582 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002583 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002584 D->hasWrittenPrototype(),
2585 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002586 }
John McCall3e11ebe2010-03-15 10:12:16 +00002587
2588 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002589 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002590 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002591 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002592 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2593 ToFunction->setTrivial(D->isTrivial());
2594 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002595 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002596
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002597 // Set the parameters.
2598 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002599 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002600 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002601 }
David Blaikie9c70e042011-09-21 18:16:56 +00002602 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002603
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002604 // Update FunctionProtoType::ExtProtoInfo.
2605 if (const FunctionProtoType *
2606 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2607 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2608 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate) {
2609 ToFunction->setType(importFunctionProtoType(Importer, FromFPT,
2610 /*importExceptionSpecDecls=*/true));
2611 }
2612 }
2613
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002614 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002615
2616 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002617 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002618
Douglas Gregor43f54792010-02-17 02:12:47 +00002619 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002620}
2621
Douglas Gregor00eace12010-02-21 18:29:16 +00002622Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2623 return VisitFunctionDecl(D);
2624}
2625
2626Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2627 return VisitCXXMethodDecl(D);
2628}
2629
2630Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2631 return VisitCXXMethodDecl(D);
2632}
2633
2634Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2635 return VisitCXXMethodDecl(D);
2636}
2637
Douglas Gregor5c73e912010-02-11 00:48:18 +00002638Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2639 // Import the major distinguishing characteristics of a variable.
2640 DeclContext *DC, *LexicalDC;
2641 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002642 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002643 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2644 return 0;
2645
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002646 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002647 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2648 DC->localUncachedLookup(Name, FoundDecls);
2649 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2650 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002651 if (Importer.IsStructurallyEquivalent(D->getType(),
2652 FoundField->getType())) {
2653 Importer.Imported(D, FoundField);
2654 return FoundField;
2655 }
2656
2657 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2658 << Name << D->getType() << FoundField->getType();
2659 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2660 << FoundField->getType();
2661 return 0;
2662 }
2663 }
2664
Douglas Gregorb4964f72010-02-15 23:54:17 +00002665 // Import the type.
2666 QualType T = Importer.Import(D->getType());
2667 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002668 return 0;
2669
2670 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2671 Expr *BitWidth = Importer.Import(D->getBitWidth());
2672 if (!BitWidth && D->getBitWidth())
2673 return 0;
2674
Abramo Bagnaradff19302011-03-08 08:55:46 +00002675 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2676 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002677 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002678 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002679 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002680 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002681 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002682 if (ToField->hasInClassInitializer())
2683 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002684 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002685 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002686 return ToField;
2687}
2688
Francois Pichet783dd6e2010-11-21 06:08:52 +00002689Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2690 // Import the major distinguishing characteristics of a variable.
2691 DeclContext *DC, *LexicalDC;
2692 DeclarationName Name;
2693 SourceLocation Loc;
2694 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2695 return 0;
2696
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002697 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002698 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2699 DC->localUncachedLookup(Name, FoundDecls);
2700 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002701 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002702 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002703 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002704 FoundField->getType(),
2705 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002706 Importer.Imported(D, FoundField);
2707 return FoundField;
2708 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002709
2710 // If there are more anonymous fields to check, continue.
2711 if (!Name && I < N-1)
2712 continue;
2713
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002714 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2715 << Name << D->getType() << FoundField->getType();
2716 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2717 << FoundField->getType();
2718 return 0;
2719 }
2720 }
2721
Francois Pichet783dd6e2010-11-21 06:08:52 +00002722 // Import the type.
2723 QualType T = Importer.Import(D->getType());
2724 if (T.isNull())
2725 return 0;
2726
2727 NamedDecl **NamedChain =
2728 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2729
2730 unsigned i = 0;
2731 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2732 PE = D->chain_end(); PI != PE; ++PI) {
2733 Decl* D = Importer.Import(*PI);
2734 if (!D)
2735 return 0;
2736 NamedChain[i++] = cast<NamedDecl>(D);
2737 }
2738
2739 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2740 Importer.getToContext(), DC,
2741 Loc, Name.getAsIdentifierInfo(), T,
2742 NamedChain, D->getChainingSize());
2743 ToIndirectField->setAccess(D->getAccess());
2744 ToIndirectField->setLexicalDeclContext(LexicalDC);
2745 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002746 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002747 return ToIndirectField;
2748}
2749
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002750Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2751 // Import the major distinguishing characteristics of an ivar.
2752 DeclContext *DC, *LexicalDC;
2753 DeclarationName Name;
2754 SourceLocation Loc;
2755 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2756 return 0;
2757
2758 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002759 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2760 DC->localUncachedLookup(Name, FoundDecls);
2761 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2762 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002763 if (Importer.IsStructurallyEquivalent(D->getType(),
2764 FoundIvar->getType())) {
2765 Importer.Imported(D, FoundIvar);
2766 return FoundIvar;
2767 }
2768
2769 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2770 << Name << D->getType() << FoundIvar->getType();
2771 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2772 << FoundIvar->getType();
2773 return 0;
2774 }
2775 }
2776
2777 // Import the type.
2778 QualType T = Importer.Import(D->getType());
2779 if (T.isNull())
2780 return 0;
2781
2782 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2783 Expr *BitWidth = Importer.Import(D->getBitWidth());
2784 if (!BitWidth && D->getBitWidth())
2785 return 0;
2786
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002787 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2788 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002789 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002790 Loc, Name.getAsIdentifierInfo(),
2791 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002792 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002793 ToIvar->setLexicalDeclContext(LexicalDC);
2794 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002795 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002796 return ToIvar;
2797
2798}
2799
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002800Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2801 // Import the major distinguishing characteristics of a variable.
2802 DeclContext *DC, *LexicalDC;
2803 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002804 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002805 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002806 return 0;
2807
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002808 // Try to find a variable in our own ("to") context with the same name and
2809 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002810 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002811 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002812 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002813 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002814 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2815 DC->localUncachedLookup(Name, FoundDecls);
2816 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2817 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002818 continue;
2819
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002820 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002821 // We have found a variable that we may need to merge with. Check it.
2822 if (isExternalLinkage(FoundVar->getLinkage()) &&
2823 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002824 if (Importer.IsStructurallyEquivalent(D->getType(),
2825 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002826 MergeWithVar = FoundVar;
2827 break;
2828 }
2829
Douglas Gregor56521c52010-02-12 17:23:39 +00002830 const ArrayType *FoundArray
2831 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2832 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002833 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002834 if (FoundArray && TArray) {
2835 if (isa<IncompleteArrayType>(FoundArray) &&
2836 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002837 // Import the type.
2838 QualType T = Importer.Import(D->getType());
2839 if (T.isNull())
2840 return 0;
2841
Douglas Gregor56521c52010-02-12 17:23:39 +00002842 FoundVar->setType(T);
2843 MergeWithVar = FoundVar;
2844 break;
2845 } else if (isa<IncompleteArrayType>(TArray) &&
2846 isa<ConstantArrayType>(FoundArray)) {
2847 MergeWithVar = FoundVar;
2848 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002849 }
2850 }
2851
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002852 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002853 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002854 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2855 << FoundVar->getType();
2856 }
2857 }
2858
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002859 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002860 }
2861
2862 if (MergeWithVar) {
2863 // An equivalent variable with external linkage has been found. Link
2864 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002865 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002866
2867 if (VarDecl *DDef = D->getDefinition()) {
2868 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2869 Importer.ToDiag(ExistingDef->getLocation(),
2870 diag::err_odr_variable_multiple_def)
2871 << Name;
2872 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2873 } else {
2874 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002875 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002876 if (DDef->isInitKnownICE()) {
2877 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2878 Eval->CheckedICE = true;
2879 Eval->IsICE = DDef->isInitICE();
2880 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002881 }
2882 }
2883
2884 return MergeWithVar;
2885 }
2886
2887 if (!ConflictingDecls.empty()) {
2888 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2889 ConflictingDecls.data(),
2890 ConflictingDecls.size());
2891 if (!Name)
2892 return 0;
2893 }
2894 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002895
Douglas Gregorb4964f72010-02-15 23:54:17 +00002896 // Import the type.
2897 QualType T = Importer.Import(D->getType());
2898 if (T.isNull())
2899 return 0;
2900
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002901 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002902 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002903 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2904 Importer.Import(D->getInnerLocStart()),
2905 Loc, Name.getAsIdentifierInfo(),
2906 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002907 D->getStorageClass(),
2908 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002909 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002910 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002911 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002912 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002913 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002914
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002915 // Merge the initializer.
2916 // FIXME: Can we really import any initializer? Alternatively, we could force
2917 // ourselves to import every declaration of a variable and then only use
2918 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002919 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002920
2921 // FIXME: Other bits to merge?
2922
2923 return ToVar;
2924}
2925
Douglas Gregor8b228d72010-02-17 21:22:52 +00002926Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2927 // Parameters are created in the translation unit's context, then moved
2928 // into the function declaration's context afterward.
2929 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2930
2931 // Import the name of this declaration.
2932 DeclarationName Name = Importer.Import(D->getDeclName());
2933 if (D->getDeclName() && !Name)
2934 return 0;
2935
2936 // Import the location of this declaration.
2937 SourceLocation Loc = Importer.Import(D->getLocation());
2938
2939 // Import the parameter's type.
2940 QualType T = Importer.Import(D->getType());
2941 if (T.isNull())
2942 return 0;
2943
2944 // Create the imported parameter.
2945 ImplicitParamDecl *ToParm
2946 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2947 Loc, Name.getAsIdentifierInfo(),
2948 T);
2949 return Importer.Imported(D, ToParm);
2950}
2951
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002952Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2953 // Parameters are created in the translation unit's context, then moved
2954 // into the function declaration's context afterward.
2955 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2956
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002957 // Import the name of this declaration.
2958 DeclarationName Name = Importer.Import(D->getDeclName());
2959 if (D->getDeclName() && !Name)
2960 return 0;
2961
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002962 // Import the location of this declaration.
2963 SourceLocation Loc = Importer.Import(D->getLocation());
2964
2965 // Import the parameter's type.
2966 QualType T = Importer.Import(D->getType());
2967 if (T.isNull())
2968 return 0;
2969
2970 // Create the imported parameter.
2971 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2972 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002973 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002974 Loc, Name.getAsIdentifierInfo(),
2975 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002976 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002977 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002978 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002979 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002980}
2981
Douglas Gregor43f54792010-02-17 02:12:47 +00002982Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2983 // Import the major distinguishing characteristics of a method.
2984 DeclContext *DC, *LexicalDC;
2985 DeclarationName Name;
2986 SourceLocation Loc;
2987 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2988 return 0;
2989
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002990 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2991 DC->localUncachedLookup(Name, FoundDecls);
2992 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2993 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002994 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2995 continue;
2996
2997 // Check return types.
2998 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2999 FoundMethod->getResultType())) {
3000 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3001 << D->isInstanceMethod() << Name
3002 << D->getResultType() << FoundMethod->getResultType();
3003 Importer.ToDiag(FoundMethod->getLocation(),
3004 diag::note_odr_objc_method_here)
3005 << D->isInstanceMethod() << Name;
3006 return 0;
3007 }
3008
3009 // Check the number of parameters.
3010 if (D->param_size() != FoundMethod->param_size()) {
3011 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3012 << D->isInstanceMethod() << Name
3013 << D->param_size() << FoundMethod->param_size();
3014 Importer.ToDiag(FoundMethod->getLocation(),
3015 diag::note_odr_objc_method_here)
3016 << D->isInstanceMethod() << Name;
3017 return 0;
3018 }
3019
3020 // Check parameter types.
3021 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3022 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3023 P != PEnd; ++P, ++FoundP) {
3024 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3025 (*FoundP)->getType())) {
3026 Importer.FromDiag((*P)->getLocation(),
3027 diag::err_odr_objc_method_param_type_inconsistent)
3028 << D->isInstanceMethod() << Name
3029 << (*P)->getType() << (*FoundP)->getType();
3030 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3031 << (*FoundP)->getType();
3032 return 0;
3033 }
3034 }
3035
3036 // Check variadic/non-variadic.
3037 // Check the number of parameters.
3038 if (D->isVariadic() != FoundMethod->isVariadic()) {
3039 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3040 << D->isInstanceMethod() << Name;
3041 Importer.ToDiag(FoundMethod->getLocation(),
3042 diag::note_odr_objc_method_here)
3043 << D->isInstanceMethod() << Name;
3044 return 0;
3045 }
3046
3047 // FIXME: Any other bits we need to merge?
3048 return Importer.Imported(D, FoundMethod);
3049 }
3050 }
3051
3052 // Import the result type.
3053 QualType ResultTy = Importer.Import(D->getResultType());
3054 if (ResultTy.isNull())
3055 return 0;
3056
Douglas Gregor12852d92010-03-08 14:59:44 +00003057 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3058
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 ObjCMethodDecl *ToMethod
3060 = ObjCMethodDecl::Create(Importer.getToContext(),
3061 Loc,
3062 Importer.Import(D->getLocEnd()),
3063 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003064 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 D->isInstanceMethod(),
3066 D->isVariadic(),
3067 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003068 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003069 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003070 D->getImplementationControl(),
3071 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003072
3073 // FIXME: When we decide to merge method definitions, we'll need to
3074 // deal with implicit parameters.
3075
3076 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003077 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003078 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3079 FromPEnd = D->param_end();
3080 FromP != FromPEnd;
3081 ++FromP) {
3082 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3083 if (!ToP)
3084 return 0;
3085
3086 ToParams.push_back(ToP);
3087 }
3088
3089 // Set the parameters.
3090 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3091 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003092 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003093 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003094 SmallVector<SourceLocation, 12> SelLocs;
3095 D->getSelectorLocs(SelLocs);
3096 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003097
3098 ToMethod->setLexicalDeclContext(LexicalDC);
3099 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003100 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003101 return ToMethod;
3102}
3103
Douglas Gregor84c51c32010-02-18 01:47:50 +00003104Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3105 // Import the major distinguishing characteristics of a category.
3106 DeclContext *DC, *LexicalDC;
3107 DeclarationName Name;
3108 SourceLocation Loc;
3109 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3110 return 0;
3111
3112 ObjCInterfaceDecl *ToInterface
3113 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3114 if (!ToInterface)
3115 return 0;
3116
3117 // Determine if we've already encountered this category.
3118 ObjCCategoryDecl *MergeWithCategory
3119 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3120 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3121 if (!ToCategory) {
3122 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003123 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003124 Loc,
3125 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003126 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003127 ToInterface,
3128 Importer.Import(D->getIvarLBraceLoc()),
3129 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003130 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003131 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003132 Importer.Imported(D, ToCategory);
3133
Douglas Gregor84c51c32010-02-18 01:47:50 +00003134 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003135 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3136 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003137 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3138 = D->protocol_loc_begin();
3139 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3140 FromProtoEnd = D->protocol_end();
3141 FromProto != FromProtoEnd;
3142 ++FromProto, ++FromProtoLoc) {
3143 ObjCProtocolDecl *ToProto
3144 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3145 if (!ToProto)
3146 return 0;
3147 Protocols.push_back(ToProto);
3148 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3149 }
3150
3151 // FIXME: If we're merging, make sure that the protocol list is the same.
3152 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3153 ProtocolLocs.data(), Importer.getToContext());
3154
3155 } else {
3156 Importer.Imported(D, ToCategory);
3157 }
3158
3159 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003160 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003161
3162 // If we have an implementation, import it as well.
3163 if (D->getImplementation()) {
3164 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003165 = cast_or_null<ObjCCategoryImplDecl>(
3166 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003167 if (!Impl)
3168 return 0;
3169
3170 ToCategory->setImplementation(Impl);
3171 }
3172
3173 return ToCategory;
3174}
3175
Douglas Gregor2aa53772012-01-24 17:42:07 +00003176bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3177 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003178 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003179 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003180 if (shouldForceImportDeclContext(Kind))
3181 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003182 return false;
3183 }
3184
3185 // Start the protocol definition
3186 To->startDefinition();
3187
3188 // Import protocols
3189 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3190 SmallVector<SourceLocation, 4> ProtocolLocs;
3191 ObjCProtocolDecl::protocol_loc_iterator
3192 FromProtoLoc = From->protocol_loc_begin();
3193 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3194 FromProtoEnd = From->protocol_end();
3195 FromProto != FromProtoEnd;
3196 ++FromProto, ++FromProtoLoc) {
3197 ObjCProtocolDecl *ToProto
3198 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3199 if (!ToProto)
3200 return true;
3201 Protocols.push_back(ToProto);
3202 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3203 }
3204
3205 // FIXME: If we're merging, make sure that the protocol list is the same.
3206 To->setProtocolList(Protocols.data(), Protocols.size(),
3207 ProtocolLocs.data(), Importer.getToContext());
3208
Douglas Gregor2e15c842012-02-01 21:00:38 +00003209 if (shouldForceImportDeclContext(Kind)) {
3210 // Import all of the members of this protocol.
3211 ImportDeclContext(From, /*ForceImport=*/true);
3212 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003213 return false;
3214}
3215
Douglas Gregor98d156a2010-02-17 16:12:00 +00003216Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003217 // If this protocol has a definition in the translation unit we're coming
3218 // from, but this particular declaration is not that definition, import the
3219 // definition and map to that.
3220 ObjCProtocolDecl *Definition = D->getDefinition();
3221 if (Definition && Definition != D) {
3222 Decl *ImportedDef = Importer.Import(Definition);
3223 if (!ImportedDef)
3224 return 0;
3225
3226 return Importer.Imported(D, ImportedDef);
3227 }
3228
Douglas Gregor84c51c32010-02-18 01:47:50 +00003229 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003230 DeclContext *DC, *LexicalDC;
3231 DeclarationName Name;
3232 SourceLocation Loc;
3233 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3234 return 0;
3235
3236 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003237 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3238 DC->localUncachedLookup(Name, FoundDecls);
3239 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3240 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003241 continue;
3242
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003243 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003244 break;
3245 }
3246
3247 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003248 if (!ToProto) {
3249 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3250 Name.getAsIdentifierInfo(), Loc,
3251 Importer.Import(D->getAtStartLoc()),
3252 /*PrevDecl=*/0);
3253 ToProto->setLexicalDeclContext(LexicalDC);
3254 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003255 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003256
3257 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003258
Douglas Gregor2aa53772012-01-24 17:42:07 +00003259 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3260 return 0;
3261
Douglas Gregor98d156a2010-02-17 16:12:00 +00003262 return ToProto;
3263}
3264
Douglas Gregor2aa53772012-01-24 17:42:07 +00003265bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3266 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003267 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003268 if (To->getDefinition()) {
3269 // Check consistency of superclass.
3270 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3271 if (FromSuper) {
3272 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3273 if (!FromSuper)
3274 return true;
3275 }
3276
3277 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3278 if ((bool)FromSuper != (bool)ToSuper ||
3279 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3280 Importer.ToDiag(To->getLocation(),
3281 diag::err_odr_objc_superclass_inconsistent)
3282 << To->getDeclName();
3283 if (ToSuper)
3284 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3285 << To->getSuperClass()->getDeclName();
3286 else
3287 Importer.ToDiag(To->getLocation(),
3288 diag::note_odr_objc_missing_superclass);
3289 if (From->getSuperClass())
3290 Importer.FromDiag(From->getSuperClassLoc(),
3291 diag::note_odr_objc_superclass)
3292 << From->getSuperClass()->getDeclName();
3293 else
3294 Importer.FromDiag(From->getLocation(),
3295 diag::note_odr_objc_missing_superclass);
3296 }
3297
Douglas Gregor2e15c842012-02-01 21:00:38 +00003298 if (shouldForceImportDeclContext(Kind))
3299 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003300 return false;
3301 }
3302
3303 // Start the definition.
3304 To->startDefinition();
3305
3306 // If this class has a superclass, import it.
3307 if (From->getSuperClass()) {
3308 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3309 Importer.Import(From->getSuperClass()));
3310 if (!Super)
3311 return true;
3312
3313 To->setSuperClass(Super);
3314 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3315 }
3316
3317 // Import protocols
3318 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3319 SmallVector<SourceLocation, 4> ProtocolLocs;
3320 ObjCInterfaceDecl::protocol_loc_iterator
3321 FromProtoLoc = From->protocol_loc_begin();
3322
3323 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3324 FromProtoEnd = From->protocol_end();
3325 FromProto != FromProtoEnd;
3326 ++FromProto, ++FromProtoLoc) {
3327 ObjCProtocolDecl *ToProto
3328 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3329 if (!ToProto)
3330 return true;
3331 Protocols.push_back(ToProto);
3332 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3333 }
3334
3335 // FIXME: If we're merging, make sure that the protocol list is the same.
3336 To->setProtocolList(Protocols.data(), Protocols.size(),
3337 ProtocolLocs.data(), Importer.getToContext());
3338
3339 // Import categories. When the categories themselves are imported, they'll
3340 // hook themselves into this interface.
3341 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3342 FromCat = FromCat->getNextClassCategory())
3343 Importer.Import(FromCat);
3344
3345 // If we have an @implementation, import it as well.
3346 if (From->getImplementation()) {
3347 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3348 Importer.Import(From->getImplementation()));
3349 if (!Impl)
3350 return true;
3351
3352 To->setImplementation(Impl);
3353 }
3354
Douglas Gregor2e15c842012-02-01 21:00:38 +00003355 if (shouldForceImportDeclContext(Kind)) {
3356 // Import all of the members of this class.
3357 ImportDeclContext(From, /*ForceImport=*/true);
3358 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003359 return false;
3360}
3361
Douglas Gregor45635322010-02-16 01:20:57 +00003362Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003363 // If this class has a definition in the translation unit we're coming from,
3364 // but this particular declaration is not that definition, import the
3365 // definition and map to that.
3366 ObjCInterfaceDecl *Definition = D->getDefinition();
3367 if (Definition && Definition != D) {
3368 Decl *ImportedDef = Importer.Import(Definition);
3369 if (!ImportedDef)
3370 return 0;
3371
3372 return Importer.Imported(D, ImportedDef);
3373 }
3374
Douglas Gregor45635322010-02-16 01:20:57 +00003375 // Import the major distinguishing characteristics of an @interface.
3376 DeclContext *DC, *LexicalDC;
3377 DeclarationName Name;
3378 SourceLocation Loc;
3379 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3380 return 0;
3381
Douglas Gregor2aa53772012-01-24 17:42:07 +00003382 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003383 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003384 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3385 DC->localUncachedLookup(Name, FoundDecls);
3386 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3387 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003388 continue;
3389
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003390 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003391 break;
3392 }
3393
Douglas Gregor2aa53772012-01-24 17:42:07 +00003394 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003395 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003396 if (!ToIface) {
3397 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3398 Importer.Import(D->getAtStartLoc()),
3399 Name.getAsIdentifierInfo(),
3400 /*PrevDecl=*/0,Loc,
3401 D->isImplicitInterfaceDecl());
3402 ToIface->setLexicalDeclContext(LexicalDC);
3403 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003404 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003405 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003406
Douglas Gregor2aa53772012-01-24 17:42:07 +00003407 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3408 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003409
Douglas Gregor98d156a2010-02-17 16:12:00 +00003410 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003411}
3412
Douglas Gregor4da9d682010-12-07 15:32:12 +00003413Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3414 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3415 Importer.Import(D->getCategoryDecl()));
3416 if (!Category)
3417 return 0;
3418
3419 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3420 if (!ToImpl) {
3421 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3422 if (!DC)
3423 return 0;
3424
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003425 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003426 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003427 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003428 Category->getClassInterface(),
3429 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003430 Importer.Import(D->getAtStartLoc()),
3431 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003432
3433 DeclContext *LexicalDC = DC;
3434 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3435 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3436 if (!LexicalDC)
3437 return 0;
3438
3439 ToImpl->setLexicalDeclContext(LexicalDC);
3440 }
3441
Sean Callanan95e74be2011-10-21 02:57:43 +00003442 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003443 Category->setImplementation(ToImpl);
3444 }
3445
3446 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003447 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003448 return ToImpl;
3449}
3450
Douglas Gregorda8025c2010-12-07 01:26:03 +00003451Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3452 // Find the corresponding interface.
3453 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3454 Importer.Import(D->getClassInterface()));
3455 if (!Iface)
3456 return 0;
3457
3458 // Import the superclass, if any.
3459 ObjCInterfaceDecl *Super = 0;
3460 if (D->getSuperClass()) {
3461 Super = cast_or_null<ObjCInterfaceDecl>(
3462 Importer.Import(D->getSuperClass()));
3463 if (!Super)
3464 return 0;
3465 }
3466
3467 ObjCImplementationDecl *Impl = Iface->getImplementation();
3468 if (!Impl) {
3469 // We haven't imported an implementation yet. Create a new @implementation
3470 // now.
3471 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3472 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003473 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003474 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003475 Importer.Import(D->getAtStartLoc()),
3476 Importer.Import(D->getIvarLBraceLoc()),
3477 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003478
3479 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3480 DeclContext *LexicalDC
3481 = Importer.ImportContext(D->getLexicalDeclContext());
3482 if (!LexicalDC)
3483 return 0;
3484 Impl->setLexicalDeclContext(LexicalDC);
3485 }
3486
3487 // Associate the implementation with the class it implements.
3488 Iface->setImplementation(Impl);
3489 Importer.Imported(D, Iface->getImplementation());
3490 } else {
3491 Importer.Imported(D, Iface->getImplementation());
3492
3493 // Verify that the existing @implementation has the same superclass.
3494 if ((Super && !Impl->getSuperClass()) ||
3495 (!Super && Impl->getSuperClass()) ||
3496 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003497 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003498 Importer.ToDiag(Impl->getLocation(),
3499 diag::err_odr_objc_superclass_inconsistent)
3500 << Iface->getDeclName();
3501 // FIXME: It would be nice to have the location of the superclass
3502 // below.
3503 if (Impl->getSuperClass())
3504 Importer.ToDiag(Impl->getLocation(),
3505 diag::note_odr_objc_superclass)
3506 << Impl->getSuperClass()->getDeclName();
3507 else
3508 Importer.ToDiag(Impl->getLocation(),
3509 diag::note_odr_objc_missing_superclass);
3510 if (D->getSuperClass())
3511 Importer.FromDiag(D->getLocation(),
3512 diag::note_odr_objc_superclass)
3513 << D->getSuperClass()->getDeclName();
3514 else
3515 Importer.FromDiag(D->getLocation(),
3516 diag::note_odr_objc_missing_superclass);
3517 return 0;
3518 }
3519 }
3520
3521 // Import all of the members of this @implementation.
3522 ImportDeclContext(D);
3523
3524 return Impl;
3525}
3526
Douglas Gregora11c4582010-02-17 18:02:10 +00003527Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3528 // Import the major distinguishing characteristics of an @property.
3529 DeclContext *DC, *LexicalDC;
3530 DeclarationName Name;
3531 SourceLocation Loc;
3532 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3533 return 0;
3534
3535 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003536 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3537 DC->localUncachedLookup(Name, FoundDecls);
3538 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003539 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003540 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003541 // Check property types.
3542 if (!Importer.IsStructurallyEquivalent(D->getType(),
3543 FoundProp->getType())) {
3544 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3545 << Name << D->getType() << FoundProp->getType();
3546 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3547 << FoundProp->getType();
3548 return 0;
3549 }
3550
3551 // FIXME: Check property attributes, getters, setters, etc.?
3552
3553 // Consider these properties to be equivalent.
3554 Importer.Imported(D, FoundProp);
3555 return FoundProp;
3556 }
3557 }
3558
3559 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003560 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3561 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003562 return 0;
3563
3564 // Create the new property.
3565 ObjCPropertyDecl *ToProperty
3566 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3567 Name.getAsIdentifierInfo(),
3568 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003569 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003570 T,
3571 D->getPropertyImplementation());
3572 Importer.Imported(D, ToProperty);
3573 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003574 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003575
3576 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003577 ToProperty->setPropertyAttributesAsWritten(
3578 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003579 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3580 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3581 ToProperty->setGetterMethodDecl(
3582 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3583 ToProperty->setSetterMethodDecl(
3584 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3585 ToProperty->setPropertyIvarDecl(
3586 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3587 return ToProperty;
3588}
3589
Douglas Gregor14a49e22010-12-07 18:32:03 +00003590Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3591 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3592 Importer.Import(D->getPropertyDecl()));
3593 if (!Property)
3594 return 0;
3595
3596 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3597 if (!DC)
3598 return 0;
3599
3600 // Import the lexical declaration context.
3601 DeclContext *LexicalDC = DC;
3602 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3603 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3604 if (!LexicalDC)
3605 return 0;
3606 }
3607
3608 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3609 if (!InImpl)
3610 return 0;
3611
3612 // Import the ivar (for an @synthesize).
3613 ObjCIvarDecl *Ivar = 0;
3614 if (D->getPropertyIvarDecl()) {
3615 Ivar = cast_or_null<ObjCIvarDecl>(
3616 Importer.Import(D->getPropertyIvarDecl()));
3617 if (!Ivar)
3618 return 0;
3619 }
3620
3621 ObjCPropertyImplDecl *ToImpl
3622 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3623 if (!ToImpl) {
3624 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3625 Importer.Import(D->getLocStart()),
3626 Importer.Import(D->getLocation()),
3627 Property,
3628 D->getPropertyImplementation(),
3629 Ivar,
3630 Importer.Import(D->getPropertyIvarDeclLoc()));
3631 ToImpl->setLexicalDeclContext(LexicalDC);
3632 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003633 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003634 } else {
3635 // Check that we have the same kind of property implementation (@synthesize
3636 // vs. @dynamic).
3637 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3638 Importer.ToDiag(ToImpl->getLocation(),
3639 diag::err_odr_objc_property_impl_kind_inconsistent)
3640 << Property->getDeclName()
3641 << (ToImpl->getPropertyImplementation()
3642 == ObjCPropertyImplDecl::Dynamic);
3643 Importer.FromDiag(D->getLocation(),
3644 diag::note_odr_objc_property_impl_kind)
3645 << D->getPropertyDecl()->getDeclName()
3646 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3647 return 0;
3648 }
3649
3650 // For @synthesize, check that we have the same
3651 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3652 Ivar != ToImpl->getPropertyIvarDecl()) {
3653 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3654 diag::err_odr_objc_synthesize_ivar_inconsistent)
3655 << Property->getDeclName()
3656 << ToImpl->getPropertyIvarDecl()->getDeclName()
3657 << Ivar->getDeclName();
3658 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3659 diag::note_odr_objc_synthesize_ivar_here)
3660 << D->getPropertyIvarDecl()->getDeclName();
3661 return 0;
3662 }
3663
3664 // Merge the existing implementation with the new implementation.
3665 Importer.Imported(D, ToImpl);
3666 }
3667
3668 return ToImpl;
3669}
3670
Douglas Gregora082a492010-11-30 19:14:50 +00003671Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3672 // For template arguments, we adopt the translation unit as our declaration
3673 // context. This context will be fixed when the actual template declaration
3674 // is created.
3675
3676 // FIXME: Import default argument.
3677 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3678 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003679 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003680 Importer.Import(D->getLocation()),
3681 D->getDepth(),
3682 D->getIndex(),
3683 Importer.Import(D->getIdentifier()),
3684 D->wasDeclaredWithTypename(),
3685 D->isParameterPack());
3686}
3687
3688Decl *
3689ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3690 // Import the name of this declaration.
3691 DeclarationName Name = Importer.Import(D->getDeclName());
3692 if (D->getDeclName() && !Name)
3693 return 0;
3694
3695 // Import the location of this declaration.
3696 SourceLocation Loc = Importer.Import(D->getLocation());
3697
3698 // Import the type of this declaration.
3699 QualType T = Importer.Import(D->getType());
3700 if (T.isNull())
3701 return 0;
3702
3703 // Import type-source information.
3704 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3705 if (D->getTypeSourceInfo() && !TInfo)
3706 return 0;
3707
3708 // FIXME: Import default argument.
3709
3710 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3711 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003712 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003713 Loc, D->getDepth(), D->getPosition(),
3714 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003715 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003716}
3717
3718Decl *
3719ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3720 // Import the name of this declaration.
3721 DeclarationName Name = Importer.Import(D->getDeclName());
3722 if (D->getDeclName() && !Name)
3723 return 0;
3724
3725 // Import the location of this declaration.
3726 SourceLocation Loc = Importer.Import(D->getLocation());
3727
3728 // Import template parameters.
3729 TemplateParameterList *TemplateParams
3730 = ImportTemplateParameterList(D->getTemplateParameters());
3731 if (!TemplateParams)
3732 return 0;
3733
3734 // FIXME: Import default argument.
3735
3736 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3737 Importer.getToContext().getTranslationUnitDecl(),
3738 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003739 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003740 Name.getAsIdentifierInfo(),
3741 TemplateParams);
3742}
3743
3744Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3745 // If this record has a definition in the translation unit we're coming from,
3746 // but this particular declaration is not that definition, import the
3747 // definition and map to that.
3748 CXXRecordDecl *Definition
3749 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3750 if (Definition && Definition != D->getTemplatedDecl()) {
3751 Decl *ImportedDef
3752 = Importer.Import(Definition->getDescribedClassTemplate());
3753 if (!ImportedDef)
3754 return 0;
3755
3756 return Importer.Imported(D, ImportedDef);
3757 }
3758
3759 // Import the major distinguishing characteristics of this class template.
3760 DeclContext *DC, *LexicalDC;
3761 DeclarationName Name;
3762 SourceLocation Loc;
3763 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3764 return 0;
3765
3766 // We may already have a template of the same name; try to find and match it.
3767 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003768 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003769 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3770 DC->localUncachedLookup(Name, FoundDecls);
3771 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3772 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003773 continue;
3774
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003775 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003776 if (ClassTemplateDecl *FoundTemplate
3777 = dyn_cast<ClassTemplateDecl>(Found)) {
3778 if (IsStructuralMatch(D, FoundTemplate)) {
3779 // The class templates structurally match; call it the same template.
3780 // FIXME: We may be filling in a forward declaration here. Handle
3781 // this case!
3782 Importer.Imported(D->getTemplatedDecl(),
3783 FoundTemplate->getTemplatedDecl());
3784 return Importer.Imported(D, FoundTemplate);
3785 }
3786 }
3787
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003788 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003789 }
3790
3791 if (!ConflictingDecls.empty()) {
3792 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3793 ConflictingDecls.data(),
3794 ConflictingDecls.size());
3795 }
3796
3797 if (!Name)
3798 return 0;
3799 }
3800
3801 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3802
3803 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003804 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3805 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003806 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3807 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003808 DC, StartLoc, IdLoc,
3809 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003810 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003811 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003812 D2Templated->setLexicalDeclContext(LexicalDC);
3813
3814 // Create the class template declaration itself.
3815 TemplateParameterList *TemplateParams
3816 = ImportTemplateParameterList(D->getTemplateParameters());
3817 if (!TemplateParams)
3818 return 0;
3819
3820 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3821 Loc, Name, TemplateParams,
3822 D2Templated,
3823 /*PrevDecl=*/0);
3824 D2Templated->setDescribedClassTemplate(D2);
3825
3826 D2->setAccess(D->getAccess());
3827 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003828 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003829
3830 // Note the relationship between the class templates.
3831 Importer.Imported(D, D2);
3832 Importer.Imported(DTemplated, D2Templated);
3833
John McCallf937c022011-10-07 06:10:15 +00003834 if (DTemplated->isCompleteDefinition() &&
3835 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003836 // FIXME: Import definition!
3837 }
3838
3839 return D2;
3840}
3841
Douglas Gregore2e50d332010-12-01 01:36:18 +00003842Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3843 ClassTemplateSpecializationDecl *D) {
3844 // If this record has a definition in the translation unit we're coming from,
3845 // but this particular declaration is not that definition, import the
3846 // definition and map to that.
3847 TagDecl *Definition = D->getDefinition();
3848 if (Definition && Definition != D) {
3849 Decl *ImportedDef = Importer.Import(Definition);
3850 if (!ImportedDef)
3851 return 0;
3852
3853 return Importer.Imported(D, ImportedDef);
3854 }
3855
3856 ClassTemplateDecl *ClassTemplate
3857 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3858 D->getSpecializedTemplate()));
3859 if (!ClassTemplate)
3860 return 0;
3861
3862 // Import the context of this declaration.
3863 DeclContext *DC = ClassTemplate->getDeclContext();
3864 if (!DC)
3865 return 0;
3866
3867 DeclContext *LexicalDC = DC;
3868 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3869 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3870 if (!LexicalDC)
3871 return 0;
3872 }
3873
3874 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003875 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3876 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003877
3878 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003879 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003880 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3881 D->getTemplateArgs().size(),
3882 TemplateArgs))
3883 return 0;
3884
3885 // Try to find an existing specialization with these template arguments.
3886 void *InsertPos = 0;
3887 ClassTemplateSpecializationDecl *D2
3888 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3889 TemplateArgs.size(), InsertPos);
3890 if (D2) {
3891 // We already have a class template specialization with these template
3892 // arguments.
3893
3894 // FIXME: Check for specialization vs. instantiation errors.
3895
3896 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003897 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003898 // The record types structurally match, or the "from" translation
3899 // unit only had a forward declaration anyway; call it the same
3900 // function.
3901 return Importer.Imported(D, FoundDef);
3902 }
3903 }
3904 } else {
3905 // Create a new specialization.
3906 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3907 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003908 StartLoc, IdLoc,
3909 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003910 TemplateArgs.data(),
3911 TemplateArgs.size(),
3912 /*PrevDecl=*/0);
3913 D2->setSpecializationKind(D->getSpecializationKind());
3914
3915 // Add this specialization to the class template.
3916 ClassTemplate->AddSpecialization(D2, InsertPos);
3917
3918 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003919 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003920
3921 // Add the specialization to this context.
3922 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003923 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003924 }
3925 Importer.Imported(D, D2);
3926
John McCallf937c022011-10-07 06:10:15 +00003927 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003928 return 0;
3929
3930 return D2;
3931}
3932
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003933//----------------------------------------------------------------------------
3934// Import Statements
3935//----------------------------------------------------------------------------
3936
3937Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3938 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3939 << S->getStmtClassName();
3940 return 0;
3941}
3942
3943//----------------------------------------------------------------------------
3944// Import Expressions
3945//----------------------------------------------------------------------------
3946Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3947 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3948 << E->getStmtClassName();
3949 return 0;
3950}
3951
Douglas Gregor52f820e2010-02-19 01:17:02 +00003952Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003953 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3954 if (!ToD)
3955 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003956
3957 NamedDecl *FoundD = 0;
3958 if (E->getDecl() != E->getFoundDecl()) {
3959 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3960 if (!FoundD)
3961 return 0;
3962 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003963
3964 QualType T = Importer.Import(E->getType());
3965 if (T.isNull())
3966 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003967
3968 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3969 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003970 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003971 ToD,
John McCall113bee02012-03-10 09:33:50 +00003972 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003973 Importer.Import(E->getLocation()),
3974 T, E->getValueKind(),
3975 FoundD,
3976 /*FIXME:TemplateArgs=*/0);
3977 if (E->hadMultipleCandidates())
3978 DRE->setHadMultipleCandidates(true);
3979 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003980}
3981
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003982Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3983 QualType T = Importer.Import(E->getType());
3984 if (T.isNull())
3985 return 0;
3986
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003987 return IntegerLiteral::Create(Importer.getToContext(),
3988 E->getValue(), T,
3989 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003990}
3991
Douglas Gregor623421d2010-02-18 02:21:22 +00003992Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3993 QualType T = Importer.Import(E->getType());
3994 if (T.isNull())
3995 return 0;
3996
Douglas Gregorfb65e592011-07-27 05:40:30 +00003997 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3998 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00003999 Importer.Import(E->getLocation()));
4000}
4001
Douglas Gregorc74247e2010-02-19 01:07:06 +00004002Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4003 Expr *SubExpr = Importer.Import(E->getSubExpr());
4004 if (!SubExpr)
4005 return 0;
4006
4007 return new (Importer.getToContext())
4008 ParenExpr(Importer.Import(E->getLParen()),
4009 Importer.Import(E->getRParen()),
4010 SubExpr);
4011}
4012
4013Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4014 QualType T = Importer.Import(E->getType());
4015 if (T.isNull())
4016 return 0;
4017
4018 Expr *SubExpr = Importer.Import(E->getSubExpr());
4019 if (!SubExpr)
4020 return 0;
4021
4022 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004023 T, E->getValueKind(),
4024 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004025 Importer.Import(E->getOperatorLoc()));
4026}
4027
Peter Collingbournee190dee2011-03-11 19:24:49 +00004028Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4029 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004030 QualType ResultType = Importer.Import(E->getType());
4031
4032 if (E->isArgumentType()) {
4033 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4034 if (!TInfo)
4035 return 0;
4036
Peter Collingbournee190dee2011-03-11 19:24:49 +00004037 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4038 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004039 Importer.Import(E->getOperatorLoc()),
4040 Importer.Import(E->getRParenLoc()));
4041 }
4042
4043 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4044 if (!SubExpr)
4045 return 0;
4046
Peter Collingbournee190dee2011-03-11 19:24:49 +00004047 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4048 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004049 Importer.Import(E->getOperatorLoc()),
4050 Importer.Import(E->getRParenLoc()));
4051}
4052
Douglas Gregorc74247e2010-02-19 01:07:06 +00004053Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4054 QualType T = Importer.Import(E->getType());
4055 if (T.isNull())
4056 return 0;
4057
4058 Expr *LHS = Importer.Import(E->getLHS());
4059 if (!LHS)
4060 return 0;
4061
4062 Expr *RHS = Importer.Import(E->getRHS());
4063 if (!RHS)
4064 return 0;
4065
4066 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004067 T, E->getValueKind(),
4068 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004069 Importer.Import(E->getOperatorLoc()));
4070}
4071
4072Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4073 QualType T = Importer.Import(E->getType());
4074 if (T.isNull())
4075 return 0;
4076
4077 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4078 if (CompLHSType.isNull())
4079 return 0;
4080
4081 QualType CompResultType = Importer.Import(E->getComputationResultType());
4082 if (CompResultType.isNull())
4083 return 0;
4084
4085 Expr *LHS = Importer.Import(E->getLHS());
4086 if (!LHS)
4087 return 0;
4088
4089 Expr *RHS = Importer.Import(E->getRHS());
4090 if (!RHS)
4091 return 0;
4092
4093 return new (Importer.getToContext())
4094 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004095 T, E->getValueKind(),
4096 E->getObjectKind(),
4097 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004098 Importer.Import(E->getOperatorLoc()));
4099}
4100
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004101static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004102 if (E->path_empty()) return false;
4103
4104 // TODO: import cast paths
4105 return true;
4106}
4107
Douglas Gregor98c10182010-02-12 22:17:39 +00004108Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4109 QualType T = Importer.Import(E->getType());
4110 if (T.isNull())
4111 return 0;
4112
4113 Expr *SubExpr = Importer.Import(E->getSubExpr());
4114 if (!SubExpr)
4115 return 0;
John McCallcf142162010-08-07 06:22:56 +00004116
4117 CXXCastPath BasePath;
4118 if (ImportCastPath(E, BasePath))
4119 return 0;
4120
4121 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004122 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004123}
4124
Douglas Gregor5481d322010-02-19 01:32:14 +00004125Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4126 QualType T = Importer.Import(E->getType());
4127 if (T.isNull())
4128 return 0;
4129
4130 Expr *SubExpr = Importer.Import(E->getSubExpr());
4131 if (!SubExpr)
4132 return 0;
4133
4134 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4135 if (!TInfo && E->getTypeInfoAsWritten())
4136 return 0;
4137
John McCallcf142162010-08-07 06:22:56 +00004138 CXXCastPath BasePath;
4139 if (ImportCastPath(E, BasePath))
4140 return 0;
4141
John McCall7decc9e2010-11-18 06:31:45 +00004142 return CStyleCastExpr::Create(Importer.getToContext(), T,
4143 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004144 SubExpr, &BasePath, TInfo,
4145 Importer.Import(E->getLParenLoc()),
4146 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004147}
4148
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004149ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004150 ASTContext &FromContext, FileManager &FromFileManager,
4151 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004152 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004153 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4154 Minimal(MinimalImport)
4155{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004156 ImportedDecls[FromContext.getTranslationUnitDecl()]
4157 = ToContext.getTranslationUnitDecl();
4158}
4159
4160ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004161
4162QualType ASTImporter::Import(QualType FromT) {
4163 if (FromT.isNull())
4164 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004165
4166 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004167
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004168 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004169 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4170 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004171 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004172 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004173
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004174 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004175 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004176 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004177 if (ToT.isNull())
4178 return ToT;
4179
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004180 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004181 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004182
John McCall424cec92011-01-19 06:33:43 +00004183 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004184}
4185
Douglas Gregor62d311f2010-02-09 19:21:46 +00004186TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004187 if (!FromTSI)
4188 return FromTSI;
4189
4190 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004191 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004192 QualType T = Import(FromTSI->getType());
4193 if (T.isNull())
4194 return 0;
4195
4196 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004197 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004198}
4199
4200Decl *ASTImporter::Import(Decl *FromD) {
4201 if (!FromD)
4202 return 0;
4203
Douglas Gregord451ea92011-07-29 23:31:30 +00004204 ASTNodeImporter Importer(*this);
4205
Douglas Gregor62d311f2010-02-09 19:21:46 +00004206 // Check whether we've already imported this declaration.
4207 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004208 if (Pos != ImportedDecls.end()) {
4209 Decl *ToD = Pos->second;
4210 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4211 return ToD;
4212 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004213
4214 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004215 Decl *ToD = Importer.Visit(FromD);
4216 if (!ToD)
4217 return 0;
4218
4219 // Record the imported declaration.
4220 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004221
4222 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4223 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004224 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004225 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004226 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004227 // When we've finished transforming a typedef, see whether it was the
4228 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004229 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004230 FromTag = AnonTagsWithPendingTypedefs.begin(),
4231 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4232 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004233 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004234 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4235 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004236 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004237 AnonTagsWithPendingTypedefs.erase(FromTag);
4238 break;
4239 }
4240 }
4241 }
4242 }
4243
Douglas Gregor62d311f2010-02-09 19:21:46 +00004244 return ToD;
4245}
4246
4247DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4248 if (!FromDC)
4249 return FromDC;
4250
Douglas Gregor95d82832012-01-24 18:36:04 +00004251 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004252 if (!ToDC)
4253 return 0;
4254
4255 // When we're using a record/enum/Objective-C class/protocol as a context, we
4256 // need it to have a definition.
4257 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004258 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004259 if (ToRecord->isCompleteDefinition()) {
4260 // Do nothing.
4261 } else if (FromRecord->isCompleteDefinition()) {
4262 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4263 ASTNodeImporter::IDK_Basic);
4264 } else {
4265 CompleteDecl(ToRecord);
4266 }
4267 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4268 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4269 if (ToEnum->isCompleteDefinition()) {
4270 // Do nothing.
4271 } else if (FromEnum->isCompleteDefinition()) {
4272 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4273 ASTNodeImporter::IDK_Basic);
4274 } else {
4275 CompleteDecl(ToEnum);
4276 }
4277 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4278 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4279 if (ToClass->getDefinition()) {
4280 // Do nothing.
4281 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4282 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4283 ASTNodeImporter::IDK_Basic);
4284 } else {
4285 CompleteDecl(ToClass);
4286 }
4287 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4288 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4289 if (ToProto->getDefinition()) {
4290 // Do nothing.
4291 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4292 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4293 ASTNodeImporter::IDK_Basic);
4294 } else {
4295 CompleteDecl(ToProto);
4296 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004297 }
4298
4299 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004300}
4301
4302Expr *ASTImporter::Import(Expr *FromE) {
4303 if (!FromE)
4304 return 0;
4305
4306 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4307}
4308
4309Stmt *ASTImporter::Import(Stmt *FromS) {
4310 if (!FromS)
4311 return 0;
4312
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004313 // Check whether we've already imported this declaration.
4314 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4315 if (Pos != ImportedStmts.end())
4316 return Pos->second;
4317
4318 // Import the type
4319 ASTNodeImporter Importer(*this);
4320 Stmt *ToS = Importer.Visit(FromS);
4321 if (!ToS)
4322 return 0;
4323
4324 // Record the imported declaration.
4325 ImportedStmts[FromS] = ToS;
4326 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004327}
4328
4329NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4330 if (!FromNNS)
4331 return 0;
4332
Douglas Gregor90ebf252011-04-27 16:48:40 +00004333 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4334
4335 switch (FromNNS->getKind()) {
4336 case NestedNameSpecifier::Identifier:
4337 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4338 return NestedNameSpecifier::Create(ToContext, prefix, II);
4339 }
4340 return 0;
4341
4342 case NestedNameSpecifier::Namespace:
4343 if (NamespaceDecl *NS =
4344 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4345 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4346 }
4347 return 0;
4348
4349 case NestedNameSpecifier::NamespaceAlias:
4350 if (NamespaceAliasDecl *NSAD =
4351 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4352 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4353 }
4354 return 0;
4355
4356 case NestedNameSpecifier::Global:
4357 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4358
4359 case NestedNameSpecifier::TypeSpec:
4360 case NestedNameSpecifier::TypeSpecWithTemplate: {
4361 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4362 if (!T.isNull()) {
4363 bool bTemplate = FromNNS->getKind() ==
4364 NestedNameSpecifier::TypeSpecWithTemplate;
4365 return NestedNameSpecifier::Create(ToContext, prefix,
4366 bTemplate, T.getTypePtr());
4367 }
4368 }
4369 return 0;
4370 }
4371
4372 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004373}
4374
Douglas Gregor14454802011-02-25 02:25:35 +00004375NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4376 // FIXME: Implement!
4377 return NestedNameSpecifierLoc();
4378}
4379
Douglas Gregore2e50d332010-12-01 01:36:18 +00004380TemplateName ASTImporter::Import(TemplateName From) {
4381 switch (From.getKind()) {
4382 case TemplateName::Template:
4383 if (TemplateDecl *ToTemplate
4384 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4385 return TemplateName(ToTemplate);
4386
4387 return TemplateName();
4388
4389 case TemplateName::OverloadedTemplate: {
4390 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4391 UnresolvedSet<2> ToTemplates;
4392 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4393 E = FromStorage->end();
4394 I != E; ++I) {
4395 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4396 ToTemplates.addDecl(To);
4397 else
4398 return TemplateName();
4399 }
4400 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4401 ToTemplates.end());
4402 }
4403
4404 case TemplateName::QualifiedTemplate: {
4405 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4406 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4407 if (!Qualifier)
4408 return TemplateName();
4409
4410 if (TemplateDecl *ToTemplate
4411 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4412 return ToContext.getQualifiedTemplateName(Qualifier,
4413 QTN->hasTemplateKeyword(),
4414 ToTemplate);
4415
4416 return TemplateName();
4417 }
4418
4419 case TemplateName::DependentTemplate: {
4420 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4421 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4422 if (!Qualifier)
4423 return TemplateName();
4424
4425 if (DTN->isIdentifier()) {
4426 return ToContext.getDependentTemplateName(Qualifier,
4427 Import(DTN->getIdentifier()));
4428 }
4429
4430 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4431 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004432
4433 case TemplateName::SubstTemplateTemplateParm: {
4434 SubstTemplateTemplateParmStorage *subst
4435 = From.getAsSubstTemplateTemplateParm();
4436 TemplateTemplateParmDecl *param
4437 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4438 if (!param)
4439 return TemplateName();
4440
4441 TemplateName replacement = Import(subst->getReplacement());
4442 if (replacement.isNull()) return TemplateName();
4443
4444 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4445 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004446
4447 case TemplateName::SubstTemplateTemplateParmPack: {
4448 SubstTemplateTemplateParmPackStorage *SubstPack
4449 = From.getAsSubstTemplateTemplateParmPack();
4450 TemplateTemplateParmDecl *Param
4451 = cast_or_null<TemplateTemplateParmDecl>(
4452 Import(SubstPack->getParameterPack()));
4453 if (!Param)
4454 return TemplateName();
4455
4456 ASTNodeImporter Importer(*this);
4457 TemplateArgument ArgPack
4458 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4459 if (ArgPack.isNull())
4460 return TemplateName();
4461
4462 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4463 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004464 }
4465
4466 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004467}
4468
Douglas Gregor62d311f2010-02-09 19:21:46 +00004469SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4470 if (FromLoc.isInvalid())
4471 return SourceLocation();
4472
Douglas Gregor811663e2010-02-10 00:15:17 +00004473 SourceManager &FromSM = FromContext.getSourceManager();
4474
4475 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004476 // don't have to import macro expansions.
4477 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004478 FromLoc = FromSM.getSpellingLoc(FromLoc);
4479 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4480 SourceManager &ToSM = ToContext.getSourceManager();
4481 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004482 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004483}
4484
4485SourceRange ASTImporter::Import(SourceRange FromRange) {
4486 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4487}
4488
Douglas Gregor811663e2010-02-10 00:15:17 +00004489FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004490 llvm::DenseMap<FileID, FileID>::iterator Pos
4491 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004492 if (Pos != ImportedFileIDs.end())
4493 return Pos->second;
4494
4495 SourceManager &FromSM = FromContext.getSourceManager();
4496 SourceManager &ToSM = ToContext.getSourceManager();
4497 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004498 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004499
4500 // Include location of this file.
4501 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4502
4503 // Map the FileID for to the "to" source manager.
4504 FileID ToID;
4505 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004506 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004507 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4508 // disk again
4509 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4510 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004511 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004512 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4513 FromSLoc.getFile().getFileCharacteristic());
4514 } else {
4515 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004516 const llvm::MemoryBuffer *
4517 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004518 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004519 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004520 FromBuf->getBufferIdentifier());
4521 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4522 }
4523
4524
Sebastian Redl99219f12010-09-30 01:03:06 +00004525 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004526 return ToID;
4527}
4528
Douglas Gregor0a791672011-01-18 03:11:38 +00004529void ASTImporter::ImportDefinition(Decl *From) {
4530 Decl *To = Import(From);
4531 if (!To)
4532 return;
4533
4534 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4535 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004536
4537 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4538 if (!ToRecord->getDefinition()) {
4539 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004540 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004541 return;
4542 }
4543 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004544
4545 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4546 if (!ToEnum->getDefinition()) {
4547 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004548 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004549 return;
4550 }
4551 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004552
4553 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4554 if (!ToIFace->getDefinition()) {
4555 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004556 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004557 return;
4558 }
4559 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004560
Douglas Gregor2aa53772012-01-24 17:42:07 +00004561 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4562 if (!ToProto->getDefinition()) {
4563 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004564 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004565 return;
4566 }
4567 }
4568
Douglas Gregor0a791672011-01-18 03:11:38 +00004569 Importer.ImportDeclContext(FromDC, true);
4570 }
4571}
4572
Douglas Gregor96e578d2010-02-05 17:54:41 +00004573DeclarationName ASTImporter::Import(DeclarationName FromName) {
4574 if (!FromName)
4575 return DeclarationName();
4576
4577 switch (FromName.getNameKind()) {
4578 case DeclarationName::Identifier:
4579 return Import(FromName.getAsIdentifierInfo());
4580
4581 case DeclarationName::ObjCZeroArgSelector:
4582 case DeclarationName::ObjCOneArgSelector:
4583 case DeclarationName::ObjCMultiArgSelector:
4584 return Import(FromName.getObjCSelector());
4585
4586 case DeclarationName::CXXConstructorName: {
4587 QualType T = Import(FromName.getCXXNameType());
4588 if (T.isNull())
4589 return DeclarationName();
4590
4591 return ToContext.DeclarationNames.getCXXConstructorName(
4592 ToContext.getCanonicalType(T));
4593 }
4594
4595 case DeclarationName::CXXDestructorName: {
4596 QualType T = Import(FromName.getCXXNameType());
4597 if (T.isNull())
4598 return DeclarationName();
4599
4600 return ToContext.DeclarationNames.getCXXDestructorName(
4601 ToContext.getCanonicalType(T));
4602 }
4603
4604 case DeclarationName::CXXConversionFunctionName: {
4605 QualType T = Import(FromName.getCXXNameType());
4606 if (T.isNull())
4607 return DeclarationName();
4608
4609 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4610 ToContext.getCanonicalType(T));
4611 }
4612
4613 case DeclarationName::CXXOperatorName:
4614 return ToContext.DeclarationNames.getCXXOperatorName(
4615 FromName.getCXXOverloadedOperator());
4616
4617 case DeclarationName::CXXLiteralOperatorName:
4618 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4619 Import(FromName.getCXXLiteralIdentifier()));
4620
4621 case DeclarationName::CXXUsingDirective:
4622 // FIXME: STATICS!
4623 return DeclarationName::getUsingDirectiveName();
4624 }
4625
David Blaikiee4d798f2012-01-20 21:50:17 +00004626 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004627}
4628
Douglas Gregore2e50d332010-12-01 01:36:18 +00004629IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004630 if (!FromId)
4631 return 0;
4632
4633 return &ToContext.Idents.get(FromId->getName());
4634}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004635
Douglas Gregor43f54792010-02-17 02:12:47 +00004636Selector ASTImporter::Import(Selector FromSel) {
4637 if (FromSel.isNull())
4638 return Selector();
4639
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004640 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004641 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4642 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4643 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4644 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4645}
4646
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004647DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4648 DeclContext *DC,
4649 unsigned IDNS,
4650 NamedDecl **Decls,
4651 unsigned NumDecls) {
4652 return Name;
4653}
4654
4655DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004656 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004657}
4658
4659DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004660 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004661}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004662
Douglas Gregor2e15c842012-02-01 21:00:38 +00004663void ASTImporter::CompleteDecl (Decl *D) {
4664 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4665 if (!ID->getDefinition())
4666 ID->startDefinition();
4667 }
4668 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4669 if (!PD->getDefinition())
4670 PD->startDefinition();
4671 }
4672 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4673 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4674 TD->startDefinition();
4675 TD->setCompleteDefinition(true);
4676 }
4677 }
4678 else {
4679 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4680 }
4681}
4682
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004683Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4684 ImportedDecls[From] = To;
4685 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004686}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004687
Douglas Gregordd6006f2012-07-17 21:16:27 +00004688bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4689 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004690 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004691 = ImportedTypes.find(From.getTypePtr());
4692 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4693 return true;
4694
Douglas Gregordd6006f2012-07-17 21:16:27 +00004695 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4696 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004697 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004698}