blob: e1963872481ad1c682ed0ff7939cb1bad5d440af [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
John McCall424cec92011-01-19 06:33:43 +00001485QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486 QualType ToResultType = Importer.Import(T->getResultType());
1487 if (ToResultType.isNull())
1488 return QualType();
1489
1490 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001491 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001492 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1493 AEnd = T->arg_type_end();
1494 A != AEnd; ++A) {
1495 QualType ArgType = Importer.Import(*A);
1496 if (ArgType.isNull())
1497 return QualType();
1498 ArgTypes.push_back(ArgType);
1499 }
1500
1501 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001502 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1504 EEnd = T->exception_end();
1505 E != EEnd; ++E) {
1506 QualType ExceptionType = Importer.Import(*E);
1507 if (ExceptionType.isNull())
1508 return QualType();
1509 ExceptionTypes.push_back(ExceptionType);
1510 }
John McCalldb40c7f2010-12-14 08:05:40 +00001511
1512 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1513 EPI.Exceptions = ExceptionTypes.data();
Argyrios Kyrtzidis9c71b1e2012-09-21 22:04:02 +00001514 EPI.NoexceptExpr = Importer.Import(EPI.NoexceptExpr);
Argyrios Kyrtzidis89df5ec2012-09-21 22:17:13 +00001515 EPI.ExceptionSpecDecl
1516 = cast_or_null<FunctionDecl>(Importer.Import(EPI.ExceptionSpecDecl));
1517 EPI.ExceptionSpecTemplate
1518 = cast_or_null<FunctionDecl>(Importer.Import(EPI.ExceptionSpecTemplate));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001519
1520 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001521 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001522}
1523
Sean Callananda6df8a2011-08-11 16:56:07 +00001524QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1525 QualType ToInnerType = Importer.Import(T->getInnerType());
1526 if (ToInnerType.isNull())
1527 return QualType();
1528
1529 return Importer.getToContext().getParenType(ToInnerType);
1530}
1531
John McCall424cec92011-01-19 06:33:43 +00001532QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001533 TypedefNameDecl *ToDecl
1534 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001535 if (!ToDecl)
1536 return QualType();
1537
1538 return Importer.getToContext().getTypeDeclType(ToDecl);
1539}
1540
John McCall424cec92011-01-19 06:33:43 +00001541QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001542 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1543 if (!ToExpr)
1544 return QualType();
1545
1546 return Importer.getToContext().getTypeOfExprType(ToExpr);
1547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1551 if (ToUnderlyingType.isNull())
1552 return QualType();
1553
1554 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1555}
1556
John McCall424cec92011-01-19 06:33:43 +00001557QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001558 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001559 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1560 if (!ToExpr)
1561 return QualType();
1562
Douglas Gregor81495f32012-02-12 18:42:33 +00001563 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1564 if (UnderlyingType.isNull())
1565 return QualType();
1566
1567 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568}
1569
Alexis Hunte852b102011-05-24 22:41:36 +00001570QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1571 QualType ToBaseType = Importer.Import(T->getBaseType());
1572 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1573 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1574 return QualType();
1575
1576 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1577 ToUnderlyingType,
1578 T->getUTTKind());
1579}
1580
Richard Smith30482bc2011-02-20 03:19:35 +00001581QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1582 // FIXME: Make sure that the "to" context supports C++0x!
1583 QualType FromDeduced = T->getDeducedType();
1584 QualType ToDeduced;
1585 if (!FromDeduced.isNull()) {
1586 ToDeduced = Importer.Import(FromDeduced);
1587 if (ToDeduced.isNull())
1588 return QualType();
1589 }
1590
1591 return Importer.getToContext().getAutoType(ToDeduced);
1592}
1593
John McCall424cec92011-01-19 06:33:43 +00001594QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001595 RecordDecl *ToDecl
1596 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1597 if (!ToDecl)
1598 return QualType();
1599
1600 return Importer.getToContext().getTagDeclType(ToDecl);
1601}
1602
John McCall424cec92011-01-19 06:33:43 +00001603QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001604 EnumDecl *ToDecl
1605 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1606 if (!ToDecl)
1607 return QualType();
1608
1609 return Importer.getToContext().getTagDeclType(ToDecl);
1610}
1611
Douglas Gregore2e50d332010-12-01 01:36:18 +00001612QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001613 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001614 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1615 if (ToTemplate.isNull())
1616 return QualType();
1617
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001618 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001619 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1620 return QualType();
1621
1622 QualType ToCanonType;
1623 if (!QualType(T, 0).isCanonical()) {
1624 QualType FromCanonType
1625 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1626 ToCanonType =Importer.Import(FromCanonType);
1627 if (ToCanonType.isNull())
1628 return QualType();
1629 }
1630 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1631 ToTemplateArgs.data(),
1632 ToTemplateArgs.size(),
1633 ToCanonType);
1634}
1635
John McCall424cec92011-01-19 06:33:43 +00001636QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001637 NestedNameSpecifier *ToQualifier = 0;
1638 // Note: the qualifier in an ElaboratedType is optional.
1639 if (T->getQualifier()) {
1640 ToQualifier = Importer.Import(T->getQualifier());
1641 if (!ToQualifier)
1642 return QualType();
1643 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001644
1645 QualType ToNamedType = Importer.Import(T->getNamedType());
1646 if (ToNamedType.isNull())
1647 return QualType();
1648
Abramo Bagnara6150c882010-05-11 21:36:43 +00001649 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1650 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001651}
1652
John McCall424cec92011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 ObjCInterfaceDecl *Class
1655 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1656 if (!Class)
1657 return QualType();
1658
John McCall8b07ec22010-05-15 11:32:37 +00001659 return Importer.getToContext().getObjCInterfaceType(Class);
1660}
1661
John McCall424cec92011-01-19 06:33:43 +00001662QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001663 QualType ToBaseType = Importer.Import(T->getBaseType());
1664 if (ToBaseType.isNull())
1665 return QualType();
1666
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001667 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001668 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001669 PEnd = T->qual_end();
1670 P != PEnd; ++P) {
1671 ObjCProtocolDecl *Protocol
1672 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1673 if (!Protocol)
1674 return QualType();
1675 Protocols.push_back(Protocol);
1676 }
1677
John McCall8b07ec22010-05-15 11:32:37 +00001678 return Importer.getToContext().getObjCObjectType(ToBaseType,
1679 Protocols.data(),
1680 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001681}
1682
John McCall424cec92011-01-19 06:33:43 +00001683QualType
1684ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001685 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1686 if (ToPointeeType.isNull())
1687 return QualType();
1688
John McCall8b07ec22010-05-15 11:32:37 +00001689 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001690}
1691
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001692//----------------------------------------------------------------------------
1693// Import Declarations
1694//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001695bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1696 DeclContext *&LexicalDC,
1697 DeclarationName &Name,
1698 SourceLocation &Loc) {
1699 // Import the context of this declaration.
1700 DC = Importer.ImportContext(D->getDeclContext());
1701 if (!DC)
1702 return true;
1703
1704 LexicalDC = DC;
1705 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1706 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1707 if (!LexicalDC)
1708 return true;
1709 }
1710
1711 // Import the name of this declaration.
1712 Name = Importer.Import(D->getDeclName());
1713 if (D->getDeclName() && !Name)
1714 return true;
1715
1716 // Import the location of this declaration.
1717 Loc = Importer.Import(D->getLocation());
1718 return false;
1719}
1720
Douglas Gregord451ea92011-07-29 23:31:30 +00001721void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1722 if (!FromD)
1723 return;
1724
1725 if (!ToD) {
1726 ToD = Importer.Import(FromD);
1727 if (!ToD)
1728 return;
1729 }
1730
1731 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1732 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1733 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1734 ImportDefinition(FromRecord, ToRecord);
1735 }
1736 }
1737 return;
1738 }
1739
1740 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1741 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1742 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1743 ImportDefinition(FromEnum, ToEnum);
1744 }
1745 }
1746 return;
1747 }
1748}
1749
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001750void
1751ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1752 DeclarationNameInfo& To) {
1753 // NOTE: To.Name and To.Loc are already imported.
1754 // We only have to import To.LocInfo.
1755 switch (To.getName().getNameKind()) {
1756 case DeclarationName::Identifier:
1757 case DeclarationName::ObjCZeroArgSelector:
1758 case DeclarationName::ObjCOneArgSelector:
1759 case DeclarationName::ObjCMultiArgSelector:
1760 case DeclarationName::CXXUsingDirective:
1761 return;
1762
1763 case DeclarationName::CXXOperatorName: {
1764 SourceRange Range = From.getCXXOperatorNameRange();
1765 To.setCXXOperatorNameRange(Importer.Import(Range));
1766 return;
1767 }
1768 case DeclarationName::CXXLiteralOperatorName: {
1769 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1770 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1771 return;
1772 }
1773 case DeclarationName::CXXConstructorName:
1774 case DeclarationName::CXXDestructorName:
1775 case DeclarationName::CXXConversionFunctionName: {
1776 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1777 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1778 return;
1779 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001780 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001781 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001782}
1783
Douglas Gregor2e15c842012-02-01 21:00:38 +00001784void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001785 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001786 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001787 return;
1788 }
1789
Douglas Gregor968d6332010-02-21 18:24:45 +00001790 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1791 FromEnd = FromDC->decls_end();
1792 From != FromEnd;
1793 ++From)
1794 Importer.Import(*From);
1795}
1796
Douglas Gregord451ea92011-07-29 23:31:30 +00001797bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001798 ImportDefinitionKind Kind) {
1799 if (To->getDefinition() || To->isBeingDefined()) {
1800 if (Kind == IDK_Everything)
1801 ImportDeclContext(From, /*ForceImport=*/true);
1802
Douglas Gregore2e50d332010-12-01 01:36:18 +00001803 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001804 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001805
1806 To->startDefinition();
1807
1808 // Add base classes.
1809 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1810 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001811
1812 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1813 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1814 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1815 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1816 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1817 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1818 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1819 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1820 ToData.Aggregate = FromData.Aggregate;
1821 ToData.PlainOldData = FromData.PlainOldData;
1822 ToData.Empty = FromData.Empty;
1823 ToData.Polymorphic = FromData.Polymorphic;
1824 ToData.Abstract = FromData.Abstract;
1825 ToData.IsStandardLayout = FromData.IsStandardLayout;
1826 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1827 ToData.HasPrivateFields = FromData.HasPrivateFields;
1828 ToData.HasProtectedFields = FromData.HasProtectedFields;
1829 ToData.HasPublicFields = FromData.HasPublicFields;
1830 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001831 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001832 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001833 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1834 ToData.HasConstexprNonCopyMoveConstructor
1835 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001836 ToData.DefaultedDefaultConstructorIsConstexpr
1837 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001838 ToData.HasConstexprDefaultConstructor
1839 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001840 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1841 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1842 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1843 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1844 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001845 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001846 ToData.HasNonLiteralTypeFieldsOrBases
1847 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001848 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001849 ToData.UserProvidedDefaultConstructor
1850 = FromData.UserProvidedDefaultConstructor;
1851 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1852 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1853 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1854 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1855 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1856 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1857 ToData.FailedImplicitMoveConstructor
1858 = FromData.FailedImplicitMoveConstructor;
1859 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001860 ToData.IsLambda = FromData.IsLambda;
1861
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001862 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001863 for (CXXRecordDecl::base_class_iterator
1864 Base1 = FromCXX->bases_begin(),
1865 FromBaseEnd = FromCXX->bases_end();
1866 Base1 != FromBaseEnd;
1867 ++Base1) {
1868 QualType T = Importer.Import(Base1->getType());
1869 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001870 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001871
1872 SourceLocation EllipsisLoc;
1873 if (Base1->isPackExpansion())
1874 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001875
1876 // Ensure that we have a definition for the base.
1877 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1878
Douglas Gregore2e50d332010-12-01 01:36:18 +00001879 Bases.push_back(
1880 new (Importer.getToContext())
1881 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1882 Base1->isVirtual(),
1883 Base1->isBaseOfClass(),
1884 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001885 Importer.Import(Base1->getTypeSourceInfo()),
1886 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001887 }
1888 if (!Bases.empty())
1889 ToCXX->setBases(Bases.data(), Bases.size());
1890 }
1891
Douglas Gregor2e15c842012-02-01 21:00:38 +00001892 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001893 ImportDeclContext(From, /*ForceImport=*/true);
1894
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001896 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001897}
1898
Douglas Gregord451ea92011-07-29 23:31:30 +00001899bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001900 ImportDefinitionKind Kind) {
1901 if (To->getDefinition() || To->isBeingDefined()) {
1902 if (Kind == IDK_Everything)
1903 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001904 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001905 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001906
1907 To->startDefinition();
1908
1909 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1910 if (T.isNull())
1911 return true;
1912
1913 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1914 if (ToPromotionType.isNull())
1915 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001916
1917 if (shouldForceImportDeclContext(Kind))
1918 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001919
1920 // FIXME: we might need to merge the number of positive or negative bits
1921 // if the enumerator lists don't match.
1922 To->completeDefinition(T, ToPromotionType,
1923 From->getNumPositiveBits(),
1924 From->getNumNegativeBits());
1925 return false;
1926}
1927
Douglas Gregora082a492010-11-30 19:14:50 +00001928TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1929 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001930 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001931 ToParams.reserve(Params->size());
1932 for (TemplateParameterList::iterator P = Params->begin(),
1933 PEnd = Params->end();
1934 P != PEnd; ++P) {
1935 Decl *To = Importer.Import(*P);
1936 if (!To)
1937 return 0;
1938
1939 ToParams.push_back(cast<NamedDecl>(To));
1940 }
1941
1942 return TemplateParameterList::Create(Importer.getToContext(),
1943 Importer.Import(Params->getTemplateLoc()),
1944 Importer.Import(Params->getLAngleLoc()),
1945 ToParams.data(), ToParams.size(),
1946 Importer.Import(Params->getRAngleLoc()));
1947}
1948
Douglas Gregore2e50d332010-12-01 01:36:18 +00001949TemplateArgument
1950ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1951 switch (From.getKind()) {
1952 case TemplateArgument::Null:
1953 return TemplateArgument();
1954
1955 case TemplateArgument::Type: {
1956 QualType ToType = Importer.Import(From.getAsType());
1957 if (ToType.isNull())
1958 return TemplateArgument();
1959 return TemplateArgument(ToType);
1960 }
1961
1962 case TemplateArgument::Integral: {
1963 QualType ToType = Importer.Import(From.getIntegralType());
1964 if (ToType.isNull())
1965 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001966 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001967 }
1968
1969 case TemplateArgument::Declaration:
1970 if (Decl *To = Importer.Import(From.getAsDecl()))
1971 return TemplateArgument(To);
1972 return TemplateArgument();
1973
1974 case TemplateArgument::Template: {
1975 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1976 if (ToTemplate.isNull())
1977 return TemplateArgument();
1978
1979 return TemplateArgument(ToTemplate);
1980 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001981
1982 case TemplateArgument::TemplateExpansion: {
1983 TemplateName ToTemplate
1984 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1985 if (ToTemplate.isNull())
1986 return TemplateArgument();
1987
Douglas Gregore1d60df2011-01-14 23:41:42 +00001988 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001989 }
1990
Douglas Gregore2e50d332010-12-01 01:36:18 +00001991 case TemplateArgument::Expression:
1992 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1993 return TemplateArgument(ToExpr);
1994 return TemplateArgument();
1995
1996 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001997 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001998 ToPack.reserve(From.pack_size());
1999 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2000 return TemplateArgument();
2001
2002 TemplateArgument *ToArgs
2003 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2004 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2005 return TemplateArgument(ToArgs, ToPack.size());
2006 }
2007 }
2008
2009 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002010}
2011
2012bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2013 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002014 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002015 for (unsigned I = 0; I != NumFromArgs; ++I) {
2016 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2017 if (To.isNull() && !FromArgs[I].isNull())
2018 return true;
2019
2020 ToArgs.push_back(To);
2021 }
2022
2023 return false;
2024}
2025
Douglas Gregor5c73e912010-02-11 00:48:18 +00002026bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002027 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002028 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002029 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002030 Importer.getNonEquivalentDecls(),
2031 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002032 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002033}
2034
Douglas Gregor98c10182010-02-12 22:17:39 +00002035bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002036 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002037 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002038 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002039 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002040}
2041
Douglas Gregora082a492010-11-30 19:14:50 +00002042bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2043 ClassTemplateDecl *To) {
2044 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2045 Importer.getToContext(),
2046 Importer.getNonEquivalentDecls());
2047 return Ctx.IsStructurallyEquivalent(From, To);
2048}
2049
Douglas Gregore4c83e42010-02-09 22:48:33 +00002050Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002051 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002052 << D->getDeclKindName();
2053 return 0;
2054}
2055
Sean Callanan65198272011-11-17 23:20:56 +00002056Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2057 TranslationUnitDecl *ToD =
2058 Importer.getToContext().getTranslationUnitDecl();
2059
2060 Importer.Imported(D, ToD);
2061
2062 return ToD;
2063}
2064
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002065Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2066 // Import the major distinguishing characteristics of this namespace.
2067 DeclContext *DC, *LexicalDC;
2068 DeclarationName Name;
2069 SourceLocation Loc;
2070 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2071 return 0;
2072
2073 NamespaceDecl *MergeWithNamespace = 0;
2074 if (!Name) {
2075 // This is an anonymous namespace. Adopt an existing anonymous
2076 // namespace if we can.
2077 // FIXME: Not testable.
2078 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2079 MergeWithNamespace = TU->getAnonymousNamespace();
2080 else
2081 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2082 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002083 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002084 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2085 DC->localUncachedLookup(Name, FoundDecls);
2086 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2087 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002088 continue;
2089
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002090 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002091 MergeWithNamespace = FoundNS;
2092 ConflictingDecls.clear();
2093 break;
2094 }
2095
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002096 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002097 }
2098
2099 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002100 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002101 ConflictingDecls.data(),
2102 ConflictingDecls.size());
2103 }
2104 }
2105
2106 // Create the "to" namespace, if needed.
2107 NamespaceDecl *ToNamespace = MergeWithNamespace;
2108 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002109 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002110 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002111 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002112 Loc, Name.getAsIdentifierInfo(),
2113 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002114 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002115 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002116
2117 // If this is an anonymous namespace, register it as the anonymous
2118 // namespace within its context.
2119 if (!Name) {
2120 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2121 TU->setAnonymousNamespace(ToNamespace);
2122 else
2123 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2124 }
2125 }
2126 Importer.Imported(D, ToNamespace);
2127
2128 ImportDeclContext(D);
2129
2130 return ToNamespace;
2131}
2132
Richard Smithdda56e42011-04-15 14:24:37 +00002133Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002134 // Import the major distinguishing characteristics of this typedef.
2135 DeclContext *DC, *LexicalDC;
2136 DeclarationName Name;
2137 SourceLocation Loc;
2138 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2139 return 0;
2140
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002141 // If this typedef is not in block scope, determine whether we've
2142 // seen a typedef with the same name (that we can merge with) or any
2143 // other entity by that name (which name lookup could conflict with).
2144 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002145 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002146 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002147 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2148 DC->localUncachedLookup(Name, FoundDecls);
2149 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2150 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002151 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002152 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002153 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002154 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2155 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002156 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002157 }
2158
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002159 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002160 }
2161
2162 if (!ConflictingDecls.empty()) {
2163 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2164 ConflictingDecls.data(),
2165 ConflictingDecls.size());
2166 if (!Name)
2167 return 0;
2168 }
2169 }
2170
Douglas Gregorb4964f72010-02-15 23:54:17 +00002171 // Import the underlying type of this typedef;
2172 QualType T = Importer.Import(D->getUnderlyingType());
2173 if (T.isNull())
2174 return 0;
2175
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002176 // Create the new typedef node.
2177 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002178 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002179 TypedefNameDecl *ToTypedef;
2180 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002181 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2182 StartL, Loc,
2183 Name.getAsIdentifierInfo(),
2184 TInfo);
2185 else
Richard Smithdda56e42011-04-15 14:24:37 +00002186 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2187 StartL, Loc,
2188 Name.getAsIdentifierInfo(),
2189 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002190
Douglas Gregordd483172010-02-22 17:42:47 +00002191 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002192 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002193 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002194 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002195
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002196 return ToTypedef;
2197}
2198
Richard Smithdda56e42011-04-15 14:24:37 +00002199Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2200 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2201}
2202
2203Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2204 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2205}
2206
Douglas Gregor98c10182010-02-12 22:17:39 +00002207Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2208 // Import the major distinguishing characteristics of this enum.
2209 DeclContext *DC, *LexicalDC;
2210 DeclarationName Name;
2211 SourceLocation Loc;
2212 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2213 return 0;
2214
2215 // Figure out what enum name we're looking for.
2216 unsigned IDNS = Decl::IDNS_Tag;
2217 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002218 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2219 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002220 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002221 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002222 IDNS |= Decl::IDNS_Ordinary;
2223
2224 // We may already have an enum of the same name; try to find and match it.
2225 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002226 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002227 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2228 DC->localUncachedLookup(SearchName, FoundDecls);
2229 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2230 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002231 continue;
2232
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002233 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002234 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002235 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2236 Found = Tag->getDecl();
2237 }
2238
2239 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002240 if (IsStructuralMatch(D, FoundEnum))
2241 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002242 }
2243
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002244 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002245 }
2246
2247 if (!ConflictingDecls.empty()) {
2248 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2249 ConflictingDecls.data(),
2250 ConflictingDecls.size());
2251 }
2252 }
2253
2254 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002255 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2256 Importer.Import(D->getLocStart()),
2257 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002258 D->isScoped(), D->isScopedUsingClassTag(),
2259 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002260 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002261 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002262 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002263 D2->setLexicalDeclContext(LexicalDC);
2264 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002265 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002266
2267 // Import the integer type.
2268 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2269 if (ToIntegerType.isNull())
2270 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002271 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002272
2273 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002274 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002275 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002276
Douglas Gregor3996e242010-02-15 22:01:00 +00002277 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002278}
2279
Douglas Gregor5c73e912010-02-11 00:48:18 +00002280Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2281 // If this record has a definition in the translation unit we're coming from,
2282 // but this particular declaration is not that definition, import the
2283 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002284 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002285 if (Definition && Definition != D) {
2286 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002287 if (!ImportedDef)
2288 return 0;
2289
2290 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002291 }
2292
2293 // Import the major distinguishing characteristics of this record.
2294 DeclContext *DC, *LexicalDC;
2295 DeclarationName Name;
2296 SourceLocation Loc;
2297 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2298 return 0;
2299
2300 // Figure out what structure name we're looking for.
2301 unsigned IDNS = Decl::IDNS_Tag;
2302 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002303 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2304 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002305 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002306 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002307 IDNS |= Decl::IDNS_Ordinary;
2308
2309 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002310 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002311 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002312 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002313 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2314 DC->localUncachedLookup(SearchName, FoundDecls);
2315 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2316 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002317 continue;
2318
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002319 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002320 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002321 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2322 Found = Tag->getDecl();
2323 }
2324
2325 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002326 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002327 if ((SearchName && !D->isCompleteDefinition())
2328 || (D->isCompleteDefinition() &&
2329 D->isAnonymousStructOrUnion()
2330 == FoundDef->isAnonymousStructOrUnion() &&
2331 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002332 // The record types structurally match, or the "from" translation
2333 // unit only had a forward declaration anyway; call it the same
2334 // function.
2335 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002336 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002337 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002338 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002339 // We have a forward declaration of this type, so adopt that forward
2340 // declaration rather than building a new one.
2341 AdoptDecl = FoundRecord;
2342 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002343 } else if (!SearchName) {
2344 continue;
2345 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002346 }
2347
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002348 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002349 }
2350
Douglas Gregordd6006f2012-07-17 21:16:27 +00002351 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002352 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2353 ConflictingDecls.data(),
2354 ConflictingDecls.size());
2355 }
2356 }
2357
2358 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002359 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002360 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002361 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002362 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002363 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002364 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002365 DC, StartLoc, Loc,
2366 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002367 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002368 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002369 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002370 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002371 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002372 }
Douglas Gregor14454802011-02-25 02:25:35 +00002373
2374 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002375 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002376 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002377 if (D->isAnonymousStructOrUnion())
2378 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002379 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002380
Douglas Gregor3996e242010-02-15 22:01:00 +00002381 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002382
Douglas Gregor95d82832012-01-24 18:36:04 +00002383 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002384 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002385
Douglas Gregor3996e242010-02-15 22:01:00 +00002386 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002387}
2388
Douglas Gregor98c10182010-02-12 22:17:39 +00002389Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2390 // Import the major distinguishing characteristics of this enumerator.
2391 DeclContext *DC, *LexicalDC;
2392 DeclarationName Name;
2393 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002394 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002395 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002396
2397 QualType T = Importer.Import(D->getType());
2398 if (T.isNull())
2399 return 0;
2400
Douglas Gregor98c10182010-02-12 22:17:39 +00002401 // Determine whether there are any other declarations with the same name and
2402 // in the same context.
2403 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002404 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002405 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002406 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2407 DC->localUncachedLookup(Name, FoundDecls);
2408 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2409 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002410 continue;
2411
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002412 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002413 }
2414
2415 if (!ConflictingDecls.empty()) {
2416 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2417 ConflictingDecls.data(),
2418 ConflictingDecls.size());
2419 if (!Name)
2420 return 0;
2421 }
2422 }
2423
2424 Expr *Init = Importer.Import(D->getInitExpr());
2425 if (D->getInitExpr() && !Init)
2426 return 0;
2427
2428 EnumConstantDecl *ToEnumerator
2429 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2430 Name.getAsIdentifierInfo(), T,
2431 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002432 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002433 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002434 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002435 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002436 return ToEnumerator;
2437}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002438
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002439Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2440 // Import the major distinguishing characteristics of this function.
2441 DeclContext *DC, *LexicalDC;
2442 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002443 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002444 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002445 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002446
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002447 // Try to find a function in our own ("to") context with the same name, same
2448 // type, and in the same context as the function we're importing.
2449 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002450 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002451 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002452 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2453 DC->localUncachedLookup(Name, FoundDecls);
2454 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2455 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002456 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002457
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002458 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002459 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2460 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002461 if (Importer.IsStructurallyEquivalent(D->getType(),
2462 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002463 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002464 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002465 }
2466
2467 // FIXME: Check for overloading more carefully, e.g., by boosting
2468 // Sema::IsOverload out to the AST library.
2469
2470 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002471 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002472 continue;
2473
2474 // Complain about inconsistent function types.
2475 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002476 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002477 Importer.ToDiag(FoundFunction->getLocation(),
2478 diag::note_odr_value_here)
2479 << FoundFunction->getType();
2480 }
2481 }
2482
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002483 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002484 }
2485
2486 if (!ConflictingDecls.empty()) {
2487 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2488 ConflictingDecls.data(),
2489 ConflictingDecls.size());
2490 if (!Name)
2491 return 0;
2492 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002493 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002494
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002495 DeclarationNameInfo NameInfo(Name, Loc);
2496 // Import additional name location/type info.
2497 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2498
Douglas Gregorb4964f72010-02-15 23:54:17 +00002499 // Import the type.
2500 QualType T = Importer.Import(D->getType());
2501 if (T.isNull())
2502 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002503
2504 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002505 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002506 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2507 P != PEnd; ++P) {
2508 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2509 if (!ToP)
2510 return 0;
2511
2512 Parameters.push_back(ToP);
2513 }
2514
2515 // Create the imported function.
2516 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002517 FunctionDecl *ToFunction = 0;
2518 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2519 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2520 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002521 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002522 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002523 FromConstructor->isExplicit(),
2524 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002525 D->isImplicit(),
2526 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002527 } else if (isa<CXXDestructorDecl>(D)) {
2528 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2529 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002530 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002531 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002532 D->isInlineSpecified(),
2533 D->isImplicit());
2534 } else if (CXXConversionDecl *FromConversion
2535 = dyn_cast<CXXConversionDecl>(D)) {
2536 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2537 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002538 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002539 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002540 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002541 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002542 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002543 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002544 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2545 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2546 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002547 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002548 NameInfo, T, TInfo,
2549 Method->isStatic(),
2550 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002551 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002552 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002553 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002554 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002555 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002556 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002557 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002558 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002559 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002560 D->hasWrittenPrototype(),
2561 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002562 }
John McCall3e11ebe2010-03-15 10:12:16 +00002563
2564 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002565 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002566 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002567 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002568 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2569 ToFunction->setTrivial(D->isTrivial());
2570 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002571 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002572
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002573 // Set the parameters.
2574 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002575 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002576 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002577 }
David Blaikie9c70e042011-09-21 18:16:56 +00002578 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002579
2580 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002581
2582 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002583 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002584
Douglas Gregor43f54792010-02-17 02:12:47 +00002585 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002586}
2587
Douglas Gregor00eace12010-02-21 18:29:16 +00002588Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2589 return VisitFunctionDecl(D);
2590}
2591
2592Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2593 return VisitCXXMethodDecl(D);
2594}
2595
2596Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2597 return VisitCXXMethodDecl(D);
2598}
2599
2600Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2601 return VisitCXXMethodDecl(D);
2602}
2603
Douglas Gregor5c73e912010-02-11 00:48:18 +00002604Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2605 // Import the major distinguishing characteristics of a variable.
2606 DeclContext *DC, *LexicalDC;
2607 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002608 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002609 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2610 return 0;
2611
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002612 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002613 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2614 DC->localUncachedLookup(Name, FoundDecls);
2615 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2616 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002617 if (Importer.IsStructurallyEquivalent(D->getType(),
2618 FoundField->getType())) {
2619 Importer.Imported(D, FoundField);
2620 return FoundField;
2621 }
2622
2623 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2624 << Name << D->getType() << FoundField->getType();
2625 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2626 << FoundField->getType();
2627 return 0;
2628 }
2629 }
2630
Douglas Gregorb4964f72010-02-15 23:54:17 +00002631 // Import the type.
2632 QualType T = Importer.Import(D->getType());
2633 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002634 return 0;
2635
2636 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2637 Expr *BitWidth = Importer.Import(D->getBitWidth());
2638 if (!BitWidth && D->getBitWidth())
2639 return 0;
2640
Abramo Bagnaradff19302011-03-08 08:55:46 +00002641 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2642 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002643 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002644 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002645 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002646 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002647 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002648 if (ToField->hasInClassInitializer())
2649 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002650 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002651 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002652 return ToField;
2653}
2654
Francois Pichet783dd6e2010-11-21 06:08:52 +00002655Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2656 // Import the major distinguishing characteristics of a variable.
2657 DeclContext *DC, *LexicalDC;
2658 DeclarationName Name;
2659 SourceLocation Loc;
2660 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2661 return 0;
2662
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002663 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002664 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2665 DC->localUncachedLookup(Name, FoundDecls);
2666 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002667 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002668 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002669 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002670 FoundField->getType(),
2671 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002672 Importer.Imported(D, FoundField);
2673 return FoundField;
2674 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002675
2676 // If there are more anonymous fields to check, continue.
2677 if (!Name && I < N-1)
2678 continue;
2679
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002680 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2681 << Name << D->getType() << FoundField->getType();
2682 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2683 << FoundField->getType();
2684 return 0;
2685 }
2686 }
2687
Francois Pichet783dd6e2010-11-21 06:08:52 +00002688 // Import the type.
2689 QualType T = Importer.Import(D->getType());
2690 if (T.isNull())
2691 return 0;
2692
2693 NamedDecl **NamedChain =
2694 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2695
2696 unsigned i = 0;
2697 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2698 PE = D->chain_end(); PI != PE; ++PI) {
2699 Decl* D = Importer.Import(*PI);
2700 if (!D)
2701 return 0;
2702 NamedChain[i++] = cast<NamedDecl>(D);
2703 }
2704
2705 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2706 Importer.getToContext(), DC,
2707 Loc, Name.getAsIdentifierInfo(), T,
2708 NamedChain, D->getChainingSize());
2709 ToIndirectField->setAccess(D->getAccess());
2710 ToIndirectField->setLexicalDeclContext(LexicalDC);
2711 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002712 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002713 return ToIndirectField;
2714}
2715
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002716Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2717 // Import the major distinguishing characteristics of an ivar.
2718 DeclContext *DC, *LexicalDC;
2719 DeclarationName Name;
2720 SourceLocation Loc;
2721 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2722 return 0;
2723
2724 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002725 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2726 DC->localUncachedLookup(Name, FoundDecls);
2727 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2728 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002729 if (Importer.IsStructurallyEquivalent(D->getType(),
2730 FoundIvar->getType())) {
2731 Importer.Imported(D, FoundIvar);
2732 return FoundIvar;
2733 }
2734
2735 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2736 << Name << D->getType() << FoundIvar->getType();
2737 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2738 << FoundIvar->getType();
2739 return 0;
2740 }
2741 }
2742
2743 // Import the type.
2744 QualType T = Importer.Import(D->getType());
2745 if (T.isNull())
2746 return 0;
2747
2748 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2749 Expr *BitWidth = Importer.Import(D->getBitWidth());
2750 if (!BitWidth && D->getBitWidth())
2751 return 0;
2752
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002753 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2754 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002755 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002756 Loc, Name.getAsIdentifierInfo(),
2757 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002758 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002759 ToIvar->setLexicalDeclContext(LexicalDC);
2760 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002761 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002762 return ToIvar;
2763
2764}
2765
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002766Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2767 // Import the major distinguishing characteristics of a variable.
2768 DeclContext *DC, *LexicalDC;
2769 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002770 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002771 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002772 return 0;
2773
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002774 // Try to find a variable in our own ("to") context with the same name and
2775 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002776 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002777 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002778 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002779 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002780 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2781 DC->localUncachedLookup(Name, FoundDecls);
2782 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2783 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002784 continue;
2785
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002786 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002787 // We have found a variable that we may need to merge with. Check it.
2788 if (isExternalLinkage(FoundVar->getLinkage()) &&
2789 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002790 if (Importer.IsStructurallyEquivalent(D->getType(),
2791 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002792 MergeWithVar = FoundVar;
2793 break;
2794 }
2795
Douglas Gregor56521c52010-02-12 17:23:39 +00002796 const ArrayType *FoundArray
2797 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2798 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002799 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002800 if (FoundArray && TArray) {
2801 if (isa<IncompleteArrayType>(FoundArray) &&
2802 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002803 // Import the type.
2804 QualType T = Importer.Import(D->getType());
2805 if (T.isNull())
2806 return 0;
2807
Douglas Gregor56521c52010-02-12 17:23:39 +00002808 FoundVar->setType(T);
2809 MergeWithVar = FoundVar;
2810 break;
2811 } else if (isa<IncompleteArrayType>(TArray) &&
2812 isa<ConstantArrayType>(FoundArray)) {
2813 MergeWithVar = FoundVar;
2814 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002815 }
2816 }
2817
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002818 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002819 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002820 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2821 << FoundVar->getType();
2822 }
2823 }
2824
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002825 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002826 }
2827
2828 if (MergeWithVar) {
2829 // An equivalent variable with external linkage has been found. Link
2830 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002831 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002832
2833 if (VarDecl *DDef = D->getDefinition()) {
2834 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2835 Importer.ToDiag(ExistingDef->getLocation(),
2836 diag::err_odr_variable_multiple_def)
2837 << Name;
2838 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2839 } else {
2840 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002841 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002842 if (DDef->isInitKnownICE()) {
2843 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2844 Eval->CheckedICE = true;
2845 Eval->IsICE = DDef->isInitICE();
2846 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002847 }
2848 }
2849
2850 return MergeWithVar;
2851 }
2852
2853 if (!ConflictingDecls.empty()) {
2854 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2855 ConflictingDecls.data(),
2856 ConflictingDecls.size());
2857 if (!Name)
2858 return 0;
2859 }
2860 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002861
Douglas Gregorb4964f72010-02-15 23:54:17 +00002862 // Import the type.
2863 QualType T = Importer.Import(D->getType());
2864 if (T.isNull())
2865 return 0;
2866
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002867 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002868 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002869 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2870 Importer.Import(D->getInnerLocStart()),
2871 Loc, Name.getAsIdentifierInfo(),
2872 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002873 D->getStorageClass(),
2874 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002875 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002876 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002877 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002878 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002879 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002880
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002881 // Merge the initializer.
2882 // FIXME: Can we really import any initializer? Alternatively, we could force
2883 // ourselves to import every declaration of a variable and then only use
2884 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002885 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002886
2887 // FIXME: Other bits to merge?
2888
2889 return ToVar;
2890}
2891
Douglas Gregor8b228d72010-02-17 21:22:52 +00002892Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2893 // Parameters are created in the translation unit's context, then moved
2894 // into the function declaration's context afterward.
2895 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2896
2897 // Import the name of this declaration.
2898 DeclarationName Name = Importer.Import(D->getDeclName());
2899 if (D->getDeclName() && !Name)
2900 return 0;
2901
2902 // Import the location of this declaration.
2903 SourceLocation Loc = Importer.Import(D->getLocation());
2904
2905 // Import the parameter's type.
2906 QualType T = Importer.Import(D->getType());
2907 if (T.isNull())
2908 return 0;
2909
2910 // Create the imported parameter.
2911 ImplicitParamDecl *ToParm
2912 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2913 Loc, Name.getAsIdentifierInfo(),
2914 T);
2915 return Importer.Imported(D, ToParm);
2916}
2917
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002918Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2919 // Parameters are created in the translation unit's context, then moved
2920 // into the function declaration's context afterward.
2921 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2922
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002923 // Import the name of this declaration.
2924 DeclarationName Name = Importer.Import(D->getDeclName());
2925 if (D->getDeclName() && !Name)
2926 return 0;
2927
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002928 // Import the location of this declaration.
2929 SourceLocation Loc = Importer.Import(D->getLocation());
2930
2931 // Import the parameter's type.
2932 QualType T = Importer.Import(D->getType());
2933 if (T.isNull())
2934 return 0;
2935
2936 // Create the imported parameter.
2937 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2938 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002939 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002940 Loc, Name.getAsIdentifierInfo(),
2941 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002942 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002943 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002944 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002945 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002946}
2947
Douglas Gregor43f54792010-02-17 02:12:47 +00002948Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2949 // Import the major distinguishing characteristics of a method.
2950 DeclContext *DC, *LexicalDC;
2951 DeclarationName Name;
2952 SourceLocation Loc;
2953 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2954 return 0;
2955
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002956 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2957 DC->localUncachedLookup(Name, FoundDecls);
2958 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2959 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002960 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2961 continue;
2962
2963 // Check return types.
2964 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2965 FoundMethod->getResultType())) {
2966 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2967 << D->isInstanceMethod() << Name
2968 << D->getResultType() << FoundMethod->getResultType();
2969 Importer.ToDiag(FoundMethod->getLocation(),
2970 diag::note_odr_objc_method_here)
2971 << D->isInstanceMethod() << Name;
2972 return 0;
2973 }
2974
2975 // Check the number of parameters.
2976 if (D->param_size() != FoundMethod->param_size()) {
2977 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2978 << D->isInstanceMethod() << Name
2979 << D->param_size() << FoundMethod->param_size();
2980 Importer.ToDiag(FoundMethod->getLocation(),
2981 diag::note_odr_objc_method_here)
2982 << D->isInstanceMethod() << Name;
2983 return 0;
2984 }
2985
2986 // Check parameter types.
2987 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2988 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2989 P != PEnd; ++P, ++FoundP) {
2990 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2991 (*FoundP)->getType())) {
2992 Importer.FromDiag((*P)->getLocation(),
2993 diag::err_odr_objc_method_param_type_inconsistent)
2994 << D->isInstanceMethod() << Name
2995 << (*P)->getType() << (*FoundP)->getType();
2996 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2997 << (*FoundP)->getType();
2998 return 0;
2999 }
3000 }
3001
3002 // Check variadic/non-variadic.
3003 // Check the number of parameters.
3004 if (D->isVariadic() != FoundMethod->isVariadic()) {
3005 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3006 << D->isInstanceMethod() << Name;
3007 Importer.ToDiag(FoundMethod->getLocation(),
3008 diag::note_odr_objc_method_here)
3009 << D->isInstanceMethod() << Name;
3010 return 0;
3011 }
3012
3013 // FIXME: Any other bits we need to merge?
3014 return Importer.Imported(D, FoundMethod);
3015 }
3016 }
3017
3018 // Import the result type.
3019 QualType ResultTy = Importer.Import(D->getResultType());
3020 if (ResultTy.isNull())
3021 return 0;
3022
Douglas Gregor12852d92010-03-08 14:59:44 +00003023 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3024
Douglas Gregor43f54792010-02-17 02:12:47 +00003025 ObjCMethodDecl *ToMethod
3026 = ObjCMethodDecl::Create(Importer.getToContext(),
3027 Loc,
3028 Importer.Import(D->getLocEnd()),
3029 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003030 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003031 D->isInstanceMethod(),
3032 D->isVariadic(),
3033 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003034 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003035 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003036 D->getImplementationControl(),
3037 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003038
3039 // FIXME: When we decide to merge method definitions, we'll need to
3040 // deal with implicit parameters.
3041
3042 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003043 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003044 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3045 FromPEnd = D->param_end();
3046 FromP != FromPEnd;
3047 ++FromP) {
3048 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3049 if (!ToP)
3050 return 0;
3051
3052 ToParams.push_back(ToP);
3053 }
3054
3055 // Set the parameters.
3056 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3057 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003058 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003060 SmallVector<SourceLocation, 12> SelLocs;
3061 D->getSelectorLocs(SelLocs);
3062 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003063
3064 ToMethod->setLexicalDeclContext(LexicalDC);
3065 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003066 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003067 return ToMethod;
3068}
3069
Douglas Gregor84c51c32010-02-18 01:47:50 +00003070Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3071 // Import the major distinguishing characteristics of a category.
3072 DeclContext *DC, *LexicalDC;
3073 DeclarationName Name;
3074 SourceLocation Loc;
3075 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3076 return 0;
3077
3078 ObjCInterfaceDecl *ToInterface
3079 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3080 if (!ToInterface)
3081 return 0;
3082
3083 // Determine if we've already encountered this category.
3084 ObjCCategoryDecl *MergeWithCategory
3085 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3086 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3087 if (!ToCategory) {
3088 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003089 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003090 Loc,
3091 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003092 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003093 ToInterface,
3094 Importer.Import(D->getIvarLBraceLoc()),
3095 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003096 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003097 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003098 Importer.Imported(D, ToCategory);
3099
Douglas Gregor84c51c32010-02-18 01:47:50 +00003100 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003101 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3102 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003103 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3104 = D->protocol_loc_begin();
3105 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3106 FromProtoEnd = D->protocol_end();
3107 FromProto != FromProtoEnd;
3108 ++FromProto, ++FromProtoLoc) {
3109 ObjCProtocolDecl *ToProto
3110 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3111 if (!ToProto)
3112 return 0;
3113 Protocols.push_back(ToProto);
3114 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3115 }
3116
3117 // FIXME: If we're merging, make sure that the protocol list is the same.
3118 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3119 ProtocolLocs.data(), Importer.getToContext());
3120
3121 } else {
3122 Importer.Imported(D, ToCategory);
3123 }
3124
3125 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003126 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003127
3128 // If we have an implementation, import it as well.
3129 if (D->getImplementation()) {
3130 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003131 = cast_or_null<ObjCCategoryImplDecl>(
3132 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003133 if (!Impl)
3134 return 0;
3135
3136 ToCategory->setImplementation(Impl);
3137 }
3138
3139 return ToCategory;
3140}
3141
Douglas Gregor2aa53772012-01-24 17:42:07 +00003142bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3143 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003144 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003145 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003146 if (shouldForceImportDeclContext(Kind))
3147 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003148 return false;
3149 }
3150
3151 // Start the protocol definition
3152 To->startDefinition();
3153
3154 // Import protocols
3155 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3156 SmallVector<SourceLocation, 4> ProtocolLocs;
3157 ObjCProtocolDecl::protocol_loc_iterator
3158 FromProtoLoc = From->protocol_loc_begin();
3159 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3160 FromProtoEnd = From->protocol_end();
3161 FromProto != FromProtoEnd;
3162 ++FromProto, ++FromProtoLoc) {
3163 ObjCProtocolDecl *ToProto
3164 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3165 if (!ToProto)
3166 return true;
3167 Protocols.push_back(ToProto);
3168 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3169 }
3170
3171 // FIXME: If we're merging, make sure that the protocol list is the same.
3172 To->setProtocolList(Protocols.data(), Protocols.size(),
3173 ProtocolLocs.data(), Importer.getToContext());
3174
Douglas Gregor2e15c842012-02-01 21:00:38 +00003175 if (shouldForceImportDeclContext(Kind)) {
3176 // Import all of the members of this protocol.
3177 ImportDeclContext(From, /*ForceImport=*/true);
3178 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003179 return false;
3180}
3181
Douglas Gregor98d156a2010-02-17 16:12:00 +00003182Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003183 // If this protocol has a definition in the translation unit we're coming
3184 // from, but this particular declaration is not that definition, import the
3185 // definition and map to that.
3186 ObjCProtocolDecl *Definition = D->getDefinition();
3187 if (Definition && Definition != D) {
3188 Decl *ImportedDef = Importer.Import(Definition);
3189 if (!ImportedDef)
3190 return 0;
3191
3192 return Importer.Imported(D, ImportedDef);
3193 }
3194
Douglas Gregor84c51c32010-02-18 01:47:50 +00003195 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003196 DeclContext *DC, *LexicalDC;
3197 DeclarationName Name;
3198 SourceLocation Loc;
3199 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3200 return 0;
3201
3202 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003203 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3204 DC->localUncachedLookup(Name, FoundDecls);
3205 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3206 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003207 continue;
3208
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003209 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003210 break;
3211 }
3212
3213 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003214 if (!ToProto) {
3215 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3216 Name.getAsIdentifierInfo(), Loc,
3217 Importer.Import(D->getAtStartLoc()),
3218 /*PrevDecl=*/0);
3219 ToProto->setLexicalDeclContext(LexicalDC);
3220 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003221 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003222
3223 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003224
Douglas Gregor2aa53772012-01-24 17:42:07 +00003225 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3226 return 0;
3227
Douglas Gregor98d156a2010-02-17 16:12:00 +00003228 return ToProto;
3229}
3230
Douglas Gregor2aa53772012-01-24 17:42:07 +00003231bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3232 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003233 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003234 if (To->getDefinition()) {
3235 // Check consistency of superclass.
3236 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3237 if (FromSuper) {
3238 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3239 if (!FromSuper)
3240 return true;
3241 }
3242
3243 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3244 if ((bool)FromSuper != (bool)ToSuper ||
3245 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3246 Importer.ToDiag(To->getLocation(),
3247 diag::err_odr_objc_superclass_inconsistent)
3248 << To->getDeclName();
3249 if (ToSuper)
3250 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3251 << To->getSuperClass()->getDeclName();
3252 else
3253 Importer.ToDiag(To->getLocation(),
3254 diag::note_odr_objc_missing_superclass);
3255 if (From->getSuperClass())
3256 Importer.FromDiag(From->getSuperClassLoc(),
3257 diag::note_odr_objc_superclass)
3258 << From->getSuperClass()->getDeclName();
3259 else
3260 Importer.FromDiag(From->getLocation(),
3261 diag::note_odr_objc_missing_superclass);
3262 }
3263
Douglas Gregor2e15c842012-02-01 21:00:38 +00003264 if (shouldForceImportDeclContext(Kind))
3265 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003266 return false;
3267 }
3268
3269 // Start the definition.
3270 To->startDefinition();
3271
3272 // If this class has a superclass, import it.
3273 if (From->getSuperClass()) {
3274 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3275 Importer.Import(From->getSuperClass()));
3276 if (!Super)
3277 return true;
3278
3279 To->setSuperClass(Super);
3280 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3281 }
3282
3283 // Import protocols
3284 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3285 SmallVector<SourceLocation, 4> ProtocolLocs;
3286 ObjCInterfaceDecl::protocol_loc_iterator
3287 FromProtoLoc = From->protocol_loc_begin();
3288
3289 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3290 FromProtoEnd = From->protocol_end();
3291 FromProto != FromProtoEnd;
3292 ++FromProto, ++FromProtoLoc) {
3293 ObjCProtocolDecl *ToProto
3294 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3295 if (!ToProto)
3296 return true;
3297 Protocols.push_back(ToProto);
3298 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3299 }
3300
3301 // FIXME: If we're merging, make sure that the protocol list is the same.
3302 To->setProtocolList(Protocols.data(), Protocols.size(),
3303 ProtocolLocs.data(), Importer.getToContext());
3304
3305 // Import categories. When the categories themselves are imported, they'll
3306 // hook themselves into this interface.
3307 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3308 FromCat = FromCat->getNextClassCategory())
3309 Importer.Import(FromCat);
3310
3311 // If we have an @implementation, import it as well.
3312 if (From->getImplementation()) {
3313 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3314 Importer.Import(From->getImplementation()));
3315 if (!Impl)
3316 return true;
3317
3318 To->setImplementation(Impl);
3319 }
3320
Douglas Gregor2e15c842012-02-01 21:00:38 +00003321 if (shouldForceImportDeclContext(Kind)) {
3322 // Import all of the members of this class.
3323 ImportDeclContext(From, /*ForceImport=*/true);
3324 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003325 return false;
3326}
3327
Douglas Gregor45635322010-02-16 01:20:57 +00003328Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003329 // If this class has a definition in the translation unit we're coming from,
3330 // but this particular declaration is not that definition, import the
3331 // definition and map to that.
3332 ObjCInterfaceDecl *Definition = D->getDefinition();
3333 if (Definition && Definition != D) {
3334 Decl *ImportedDef = Importer.Import(Definition);
3335 if (!ImportedDef)
3336 return 0;
3337
3338 return Importer.Imported(D, ImportedDef);
3339 }
3340
Douglas Gregor45635322010-02-16 01:20:57 +00003341 // Import the major distinguishing characteristics of an @interface.
3342 DeclContext *DC, *LexicalDC;
3343 DeclarationName Name;
3344 SourceLocation Loc;
3345 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3346 return 0;
3347
Douglas Gregor2aa53772012-01-24 17:42:07 +00003348 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003349 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003350 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3351 DC->localUncachedLookup(Name, FoundDecls);
3352 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3353 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003354 continue;
3355
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003356 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003357 break;
3358 }
3359
Douglas Gregor2aa53772012-01-24 17:42:07 +00003360 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003361 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003362 if (!ToIface) {
3363 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3364 Importer.Import(D->getAtStartLoc()),
3365 Name.getAsIdentifierInfo(),
3366 /*PrevDecl=*/0,Loc,
3367 D->isImplicitInterfaceDecl());
3368 ToIface->setLexicalDeclContext(LexicalDC);
3369 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003370 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003371 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003372
Douglas Gregor2aa53772012-01-24 17:42:07 +00003373 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3374 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003375
Douglas Gregor98d156a2010-02-17 16:12:00 +00003376 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003377}
3378
Douglas Gregor4da9d682010-12-07 15:32:12 +00003379Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3380 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3381 Importer.Import(D->getCategoryDecl()));
3382 if (!Category)
3383 return 0;
3384
3385 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3386 if (!ToImpl) {
3387 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3388 if (!DC)
3389 return 0;
3390
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003391 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003392 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003393 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003394 Category->getClassInterface(),
3395 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003396 Importer.Import(D->getAtStartLoc()),
3397 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003398
3399 DeclContext *LexicalDC = DC;
3400 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3401 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3402 if (!LexicalDC)
3403 return 0;
3404
3405 ToImpl->setLexicalDeclContext(LexicalDC);
3406 }
3407
Sean Callanan95e74be2011-10-21 02:57:43 +00003408 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003409 Category->setImplementation(ToImpl);
3410 }
3411
3412 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003413 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003414 return ToImpl;
3415}
3416
Douglas Gregorda8025c2010-12-07 01:26:03 +00003417Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3418 // Find the corresponding interface.
3419 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3420 Importer.Import(D->getClassInterface()));
3421 if (!Iface)
3422 return 0;
3423
3424 // Import the superclass, if any.
3425 ObjCInterfaceDecl *Super = 0;
3426 if (D->getSuperClass()) {
3427 Super = cast_or_null<ObjCInterfaceDecl>(
3428 Importer.Import(D->getSuperClass()));
3429 if (!Super)
3430 return 0;
3431 }
3432
3433 ObjCImplementationDecl *Impl = Iface->getImplementation();
3434 if (!Impl) {
3435 // We haven't imported an implementation yet. Create a new @implementation
3436 // now.
3437 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3438 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003439 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003440 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003441 Importer.Import(D->getAtStartLoc()),
3442 Importer.Import(D->getIvarLBraceLoc()),
3443 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003444
3445 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3446 DeclContext *LexicalDC
3447 = Importer.ImportContext(D->getLexicalDeclContext());
3448 if (!LexicalDC)
3449 return 0;
3450 Impl->setLexicalDeclContext(LexicalDC);
3451 }
3452
3453 // Associate the implementation with the class it implements.
3454 Iface->setImplementation(Impl);
3455 Importer.Imported(D, Iface->getImplementation());
3456 } else {
3457 Importer.Imported(D, Iface->getImplementation());
3458
3459 // Verify that the existing @implementation has the same superclass.
3460 if ((Super && !Impl->getSuperClass()) ||
3461 (!Super && Impl->getSuperClass()) ||
3462 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003463 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003464 Importer.ToDiag(Impl->getLocation(),
3465 diag::err_odr_objc_superclass_inconsistent)
3466 << Iface->getDeclName();
3467 // FIXME: It would be nice to have the location of the superclass
3468 // below.
3469 if (Impl->getSuperClass())
3470 Importer.ToDiag(Impl->getLocation(),
3471 diag::note_odr_objc_superclass)
3472 << Impl->getSuperClass()->getDeclName();
3473 else
3474 Importer.ToDiag(Impl->getLocation(),
3475 diag::note_odr_objc_missing_superclass);
3476 if (D->getSuperClass())
3477 Importer.FromDiag(D->getLocation(),
3478 diag::note_odr_objc_superclass)
3479 << D->getSuperClass()->getDeclName();
3480 else
3481 Importer.FromDiag(D->getLocation(),
3482 diag::note_odr_objc_missing_superclass);
3483 return 0;
3484 }
3485 }
3486
3487 // Import all of the members of this @implementation.
3488 ImportDeclContext(D);
3489
3490 return Impl;
3491}
3492
Douglas Gregora11c4582010-02-17 18:02:10 +00003493Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3494 // Import the major distinguishing characteristics of an @property.
3495 DeclContext *DC, *LexicalDC;
3496 DeclarationName Name;
3497 SourceLocation Loc;
3498 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3499 return 0;
3500
3501 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003502 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3503 DC->localUncachedLookup(Name, FoundDecls);
3504 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003505 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003506 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003507 // Check property types.
3508 if (!Importer.IsStructurallyEquivalent(D->getType(),
3509 FoundProp->getType())) {
3510 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3511 << Name << D->getType() << FoundProp->getType();
3512 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3513 << FoundProp->getType();
3514 return 0;
3515 }
3516
3517 // FIXME: Check property attributes, getters, setters, etc.?
3518
3519 // Consider these properties to be equivalent.
3520 Importer.Imported(D, FoundProp);
3521 return FoundProp;
3522 }
3523 }
3524
3525 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003526 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3527 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003528 return 0;
3529
3530 // Create the new property.
3531 ObjCPropertyDecl *ToProperty
3532 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3533 Name.getAsIdentifierInfo(),
3534 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003535 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003536 T,
3537 D->getPropertyImplementation());
3538 Importer.Imported(D, ToProperty);
3539 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003540 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003541
3542 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003543 ToProperty->setPropertyAttributesAsWritten(
3544 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003545 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3546 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3547 ToProperty->setGetterMethodDecl(
3548 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3549 ToProperty->setSetterMethodDecl(
3550 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3551 ToProperty->setPropertyIvarDecl(
3552 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3553 return ToProperty;
3554}
3555
Douglas Gregor14a49e22010-12-07 18:32:03 +00003556Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3557 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3558 Importer.Import(D->getPropertyDecl()));
3559 if (!Property)
3560 return 0;
3561
3562 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3563 if (!DC)
3564 return 0;
3565
3566 // Import the lexical declaration context.
3567 DeclContext *LexicalDC = DC;
3568 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3569 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3570 if (!LexicalDC)
3571 return 0;
3572 }
3573
3574 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3575 if (!InImpl)
3576 return 0;
3577
3578 // Import the ivar (for an @synthesize).
3579 ObjCIvarDecl *Ivar = 0;
3580 if (D->getPropertyIvarDecl()) {
3581 Ivar = cast_or_null<ObjCIvarDecl>(
3582 Importer.Import(D->getPropertyIvarDecl()));
3583 if (!Ivar)
3584 return 0;
3585 }
3586
3587 ObjCPropertyImplDecl *ToImpl
3588 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3589 if (!ToImpl) {
3590 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3591 Importer.Import(D->getLocStart()),
3592 Importer.Import(D->getLocation()),
3593 Property,
3594 D->getPropertyImplementation(),
3595 Ivar,
3596 Importer.Import(D->getPropertyIvarDeclLoc()));
3597 ToImpl->setLexicalDeclContext(LexicalDC);
3598 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003599 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003600 } else {
3601 // Check that we have the same kind of property implementation (@synthesize
3602 // vs. @dynamic).
3603 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3604 Importer.ToDiag(ToImpl->getLocation(),
3605 diag::err_odr_objc_property_impl_kind_inconsistent)
3606 << Property->getDeclName()
3607 << (ToImpl->getPropertyImplementation()
3608 == ObjCPropertyImplDecl::Dynamic);
3609 Importer.FromDiag(D->getLocation(),
3610 diag::note_odr_objc_property_impl_kind)
3611 << D->getPropertyDecl()->getDeclName()
3612 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3613 return 0;
3614 }
3615
3616 // For @synthesize, check that we have the same
3617 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3618 Ivar != ToImpl->getPropertyIvarDecl()) {
3619 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3620 diag::err_odr_objc_synthesize_ivar_inconsistent)
3621 << Property->getDeclName()
3622 << ToImpl->getPropertyIvarDecl()->getDeclName()
3623 << Ivar->getDeclName();
3624 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3625 diag::note_odr_objc_synthesize_ivar_here)
3626 << D->getPropertyIvarDecl()->getDeclName();
3627 return 0;
3628 }
3629
3630 // Merge the existing implementation with the new implementation.
3631 Importer.Imported(D, ToImpl);
3632 }
3633
3634 return ToImpl;
3635}
3636
Douglas Gregora082a492010-11-30 19:14:50 +00003637Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3638 // For template arguments, we adopt the translation unit as our declaration
3639 // context. This context will be fixed when the actual template declaration
3640 // is created.
3641
3642 // FIXME: Import default argument.
3643 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3644 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003645 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003646 Importer.Import(D->getLocation()),
3647 D->getDepth(),
3648 D->getIndex(),
3649 Importer.Import(D->getIdentifier()),
3650 D->wasDeclaredWithTypename(),
3651 D->isParameterPack());
3652}
3653
3654Decl *
3655ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3656 // Import the name of this declaration.
3657 DeclarationName Name = Importer.Import(D->getDeclName());
3658 if (D->getDeclName() && !Name)
3659 return 0;
3660
3661 // Import the location of this declaration.
3662 SourceLocation Loc = Importer.Import(D->getLocation());
3663
3664 // Import the type of this declaration.
3665 QualType T = Importer.Import(D->getType());
3666 if (T.isNull())
3667 return 0;
3668
3669 // Import type-source information.
3670 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3671 if (D->getTypeSourceInfo() && !TInfo)
3672 return 0;
3673
3674 // FIXME: Import default argument.
3675
3676 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3677 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003678 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003679 Loc, D->getDepth(), D->getPosition(),
3680 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003681 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003682}
3683
3684Decl *
3685ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3686 // Import the name of this declaration.
3687 DeclarationName Name = Importer.Import(D->getDeclName());
3688 if (D->getDeclName() && !Name)
3689 return 0;
3690
3691 // Import the location of this declaration.
3692 SourceLocation Loc = Importer.Import(D->getLocation());
3693
3694 // Import template parameters.
3695 TemplateParameterList *TemplateParams
3696 = ImportTemplateParameterList(D->getTemplateParameters());
3697 if (!TemplateParams)
3698 return 0;
3699
3700 // FIXME: Import default argument.
3701
3702 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3703 Importer.getToContext().getTranslationUnitDecl(),
3704 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003705 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003706 Name.getAsIdentifierInfo(),
3707 TemplateParams);
3708}
3709
3710Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3711 // If this record has a definition in the translation unit we're coming from,
3712 // but this particular declaration is not that definition, import the
3713 // definition and map to that.
3714 CXXRecordDecl *Definition
3715 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3716 if (Definition && Definition != D->getTemplatedDecl()) {
3717 Decl *ImportedDef
3718 = Importer.Import(Definition->getDescribedClassTemplate());
3719 if (!ImportedDef)
3720 return 0;
3721
3722 return Importer.Imported(D, ImportedDef);
3723 }
3724
3725 // Import the major distinguishing characteristics of this class template.
3726 DeclContext *DC, *LexicalDC;
3727 DeclarationName Name;
3728 SourceLocation Loc;
3729 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3730 return 0;
3731
3732 // We may already have a template of the same name; try to find and match it.
3733 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003734 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003735 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3736 DC->localUncachedLookup(Name, FoundDecls);
3737 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3738 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003739 continue;
3740
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003741 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003742 if (ClassTemplateDecl *FoundTemplate
3743 = dyn_cast<ClassTemplateDecl>(Found)) {
3744 if (IsStructuralMatch(D, FoundTemplate)) {
3745 // The class templates structurally match; call it the same template.
3746 // FIXME: We may be filling in a forward declaration here. Handle
3747 // this case!
3748 Importer.Imported(D->getTemplatedDecl(),
3749 FoundTemplate->getTemplatedDecl());
3750 return Importer.Imported(D, FoundTemplate);
3751 }
3752 }
3753
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003754 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003755 }
3756
3757 if (!ConflictingDecls.empty()) {
3758 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3759 ConflictingDecls.data(),
3760 ConflictingDecls.size());
3761 }
3762
3763 if (!Name)
3764 return 0;
3765 }
3766
3767 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3768
3769 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003770 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3771 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003772 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3773 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003774 DC, StartLoc, IdLoc,
3775 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003776 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003777 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003778 D2Templated->setLexicalDeclContext(LexicalDC);
3779
3780 // Create the class template declaration itself.
3781 TemplateParameterList *TemplateParams
3782 = ImportTemplateParameterList(D->getTemplateParameters());
3783 if (!TemplateParams)
3784 return 0;
3785
3786 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3787 Loc, Name, TemplateParams,
3788 D2Templated,
3789 /*PrevDecl=*/0);
3790 D2Templated->setDescribedClassTemplate(D2);
3791
3792 D2->setAccess(D->getAccess());
3793 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003794 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003795
3796 // Note the relationship between the class templates.
3797 Importer.Imported(D, D2);
3798 Importer.Imported(DTemplated, D2Templated);
3799
John McCallf937c022011-10-07 06:10:15 +00003800 if (DTemplated->isCompleteDefinition() &&
3801 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003802 // FIXME: Import definition!
3803 }
3804
3805 return D2;
3806}
3807
Douglas Gregore2e50d332010-12-01 01:36:18 +00003808Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3809 ClassTemplateSpecializationDecl *D) {
3810 // If this record has a definition in the translation unit we're coming from,
3811 // but this particular declaration is not that definition, import the
3812 // definition and map to that.
3813 TagDecl *Definition = D->getDefinition();
3814 if (Definition && Definition != D) {
3815 Decl *ImportedDef = Importer.Import(Definition);
3816 if (!ImportedDef)
3817 return 0;
3818
3819 return Importer.Imported(D, ImportedDef);
3820 }
3821
3822 ClassTemplateDecl *ClassTemplate
3823 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3824 D->getSpecializedTemplate()));
3825 if (!ClassTemplate)
3826 return 0;
3827
3828 // Import the context of this declaration.
3829 DeclContext *DC = ClassTemplate->getDeclContext();
3830 if (!DC)
3831 return 0;
3832
3833 DeclContext *LexicalDC = DC;
3834 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3835 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3836 if (!LexicalDC)
3837 return 0;
3838 }
3839
3840 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003841 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3842 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003843
3844 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003845 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003846 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3847 D->getTemplateArgs().size(),
3848 TemplateArgs))
3849 return 0;
3850
3851 // Try to find an existing specialization with these template arguments.
3852 void *InsertPos = 0;
3853 ClassTemplateSpecializationDecl *D2
3854 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3855 TemplateArgs.size(), InsertPos);
3856 if (D2) {
3857 // We already have a class template specialization with these template
3858 // arguments.
3859
3860 // FIXME: Check for specialization vs. instantiation errors.
3861
3862 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003863 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003864 // The record types structurally match, or the "from" translation
3865 // unit only had a forward declaration anyway; call it the same
3866 // function.
3867 return Importer.Imported(D, FoundDef);
3868 }
3869 }
3870 } else {
3871 // Create a new specialization.
3872 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3873 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003874 StartLoc, IdLoc,
3875 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003876 TemplateArgs.data(),
3877 TemplateArgs.size(),
3878 /*PrevDecl=*/0);
3879 D2->setSpecializationKind(D->getSpecializationKind());
3880
3881 // Add this specialization to the class template.
3882 ClassTemplate->AddSpecialization(D2, InsertPos);
3883
3884 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003885 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003886
3887 // Add the specialization to this context.
3888 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003889 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003890 }
3891 Importer.Imported(D, D2);
3892
John McCallf937c022011-10-07 06:10:15 +00003893 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003894 return 0;
3895
3896 return D2;
3897}
3898
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003899//----------------------------------------------------------------------------
3900// Import Statements
3901//----------------------------------------------------------------------------
3902
3903Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3904 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3905 << S->getStmtClassName();
3906 return 0;
3907}
3908
3909//----------------------------------------------------------------------------
3910// Import Expressions
3911//----------------------------------------------------------------------------
3912Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3913 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3914 << E->getStmtClassName();
3915 return 0;
3916}
3917
Douglas Gregor52f820e2010-02-19 01:17:02 +00003918Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003919 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3920 if (!ToD)
3921 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003922
3923 NamedDecl *FoundD = 0;
3924 if (E->getDecl() != E->getFoundDecl()) {
3925 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3926 if (!FoundD)
3927 return 0;
3928 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003929
3930 QualType T = Importer.Import(E->getType());
3931 if (T.isNull())
3932 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003933
3934 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3935 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003936 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003937 ToD,
John McCall113bee02012-03-10 09:33:50 +00003938 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003939 Importer.Import(E->getLocation()),
3940 T, E->getValueKind(),
3941 FoundD,
3942 /*FIXME:TemplateArgs=*/0);
3943 if (E->hadMultipleCandidates())
3944 DRE->setHadMultipleCandidates(true);
3945 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003946}
3947
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003948Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3949 QualType T = Importer.Import(E->getType());
3950 if (T.isNull())
3951 return 0;
3952
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003953 return IntegerLiteral::Create(Importer.getToContext(),
3954 E->getValue(), T,
3955 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003956}
3957
Douglas Gregor623421d2010-02-18 02:21:22 +00003958Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3959 QualType T = Importer.Import(E->getType());
3960 if (T.isNull())
3961 return 0;
3962
Douglas Gregorfb65e592011-07-27 05:40:30 +00003963 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3964 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00003965 Importer.Import(E->getLocation()));
3966}
3967
Douglas Gregorc74247e2010-02-19 01:07:06 +00003968Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3969 Expr *SubExpr = Importer.Import(E->getSubExpr());
3970 if (!SubExpr)
3971 return 0;
3972
3973 return new (Importer.getToContext())
3974 ParenExpr(Importer.Import(E->getLParen()),
3975 Importer.Import(E->getRParen()),
3976 SubExpr);
3977}
3978
3979Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3980 QualType T = Importer.Import(E->getType());
3981 if (T.isNull())
3982 return 0;
3983
3984 Expr *SubExpr = Importer.Import(E->getSubExpr());
3985 if (!SubExpr)
3986 return 0;
3987
3988 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003989 T, E->getValueKind(),
3990 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003991 Importer.Import(E->getOperatorLoc()));
3992}
3993
Peter Collingbournee190dee2011-03-11 19:24:49 +00003994Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3995 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00003996 QualType ResultType = Importer.Import(E->getType());
3997
3998 if (E->isArgumentType()) {
3999 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4000 if (!TInfo)
4001 return 0;
4002
Peter Collingbournee190dee2011-03-11 19:24:49 +00004003 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4004 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004005 Importer.Import(E->getOperatorLoc()),
4006 Importer.Import(E->getRParenLoc()));
4007 }
4008
4009 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4010 if (!SubExpr)
4011 return 0;
4012
Peter Collingbournee190dee2011-03-11 19:24:49 +00004013 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4014 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004015 Importer.Import(E->getOperatorLoc()),
4016 Importer.Import(E->getRParenLoc()));
4017}
4018
Douglas Gregorc74247e2010-02-19 01:07:06 +00004019Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4020 QualType T = Importer.Import(E->getType());
4021 if (T.isNull())
4022 return 0;
4023
4024 Expr *LHS = Importer.Import(E->getLHS());
4025 if (!LHS)
4026 return 0;
4027
4028 Expr *RHS = Importer.Import(E->getRHS());
4029 if (!RHS)
4030 return 0;
4031
4032 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004033 T, E->getValueKind(),
4034 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004035 Importer.Import(E->getOperatorLoc()));
4036}
4037
4038Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4039 QualType T = Importer.Import(E->getType());
4040 if (T.isNull())
4041 return 0;
4042
4043 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4044 if (CompLHSType.isNull())
4045 return 0;
4046
4047 QualType CompResultType = Importer.Import(E->getComputationResultType());
4048 if (CompResultType.isNull())
4049 return 0;
4050
4051 Expr *LHS = Importer.Import(E->getLHS());
4052 if (!LHS)
4053 return 0;
4054
4055 Expr *RHS = Importer.Import(E->getRHS());
4056 if (!RHS)
4057 return 0;
4058
4059 return new (Importer.getToContext())
4060 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004061 T, E->getValueKind(),
4062 E->getObjectKind(),
4063 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004064 Importer.Import(E->getOperatorLoc()));
4065}
4066
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004067static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004068 if (E->path_empty()) return false;
4069
4070 // TODO: import cast paths
4071 return true;
4072}
4073
Douglas Gregor98c10182010-02-12 22:17:39 +00004074Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4075 QualType T = Importer.Import(E->getType());
4076 if (T.isNull())
4077 return 0;
4078
4079 Expr *SubExpr = Importer.Import(E->getSubExpr());
4080 if (!SubExpr)
4081 return 0;
John McCallcf142162010-08-07 06:22:56 +00004082
4083 CXXCastPath BasePath;
4084 if (ImportCastPath(E, BasePath))
4085 return 0;
4086
4087 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004088 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004089}
4090
Douglas Gregor5481d322010-02-19 01:32:14 +00004091Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4092 QualType T = Importer.Import(E->getType());
4093 if (T.isNull())
4094 return 0;
4095
4096 Expr *SubExpr = Importer.Import(E->getSubExpr());
4097 if (!SubExpr)
4098 return 0;
4099
4100 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4101 if (!TInfo && E->getTypeInfoAsWritten())
4102 return 0;
4103
John McCallcf142162010-08-07 06:22:56 +00004104 CXXCastPath BasePath;
4105 if (ImportCastPath(E, BasePath))
4106 return 0;
4107
John McCall7decc9e2010-11-18 06:31:45 +00004108 return CStyleCastExpr::Create(Importer.getToContext(), T,
4109 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004110 SubExpr, &BasePath, TInfo,
4111 Importer.Import(E->getLParenLoc()),
4112 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004113}
4114
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004115ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004116 ASTContext &FromContext, FileManager &FromFileManager,
4117 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004118 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004119 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4120 Minimal(MinimalImport)
4121{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004122 ImportedDecls[FromContext.getTranslationUnitDecl()]
4123 = ToContext.getTranslationUnitDecl();
4124}
4125
4126ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004127
4128QualType ASTImporter::Import(QualType FromT) {
4129 if (FromT.isNull())
4130 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004131
4132 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004133
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004134 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004135 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4136 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004137 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004138 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004139
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004140 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004141 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004142 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004143 if (ToT.isNull())
4144 return ToT;
4145
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004146 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004147 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004148
John McCall424cec92011-01-19 06:33:43 +00004149 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004150}
4151
Douglas Gregor62d311f2010-02-09 19:21:46 +00004152TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004153 if (!FromTSI)
4154 return FromTSI;
4155
4156 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004157 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004158 QualType T = Import(FromTSI->getType());
4159 if (T.isNull())
4160 return 0;
4161
4162 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004163 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004164}
4165
4166Decl *ASTImporter::Import(Decl *FromD) {
4167 if (!FromD)
4168 return 0;
4169
Douglas Gregord451ea92011-07-29 23:31:30 +00004170 ASTNodeImporter Importer(*this);
4171
Douglas Gregor62d311f2010-02-09 19:21:46 +00004172 // Check whether we've already imported this declaration.
4173 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004174 if (Pos != ImportedDecls.end()) {
4175 Decl *ToD = Pos->second;
4176 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4177 return ToD;
4178 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004179
4180 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004181 Decl *ToD = Importer.Visit(FromD);
4182 if (!ToD)
4183 return 0;
4184
4185 // Record the imported declaration.
4186 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004187
4188 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4189 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004190 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004191 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004192 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004193 // When we've finished transforming a typedef, see whether it was the
4194 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004195 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004196 FromTag = AnonTagsWithPendingTypedefs.begin(),
4197 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4198 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004199 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004200 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4201 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004202 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004203 AnonTagsWithPendingTypedefs.erase(FromTag);
4204 break;
4205 }
4206 }
4207 }
4208 }
4209
Douglas Gregor62d311f2010-02-09 19:21:46 +00004210 return ToD;
4211}
4212
4213DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4214 if (!FromDC)
4215 return FromDC;
4216
Douglas Gregor95d82832012-01-24 18:36:04 +00004217 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004218 if (!ToDC)
4219 return 0;
4220
4221 // When we're using a record/enum/Objective-C class/protocol as a context, we
4222 // need it to have a definition.
4223 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004224 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004225 if (ToRecord->isCompleteDefinition()) {
4226 // Do nothing.
4227 } else if (FromRecord->isCompleteDefinition()) {
4228 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4229 ASTNodeImporter::IDK_Basic);
4230 } else {
4231 CompleteDecl(ToRecord);
4232 }
4233 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4234 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4235 if (ToEnum->isCompleteDefinition()) {
4236 // Do nothing.
4237 } else if (FromEnum->isCompleteDefinition()) {
4238 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4239 ASTNodeImporter::IDK_Basic);
4240 } else {
4241 CompleteDecl(ToEnum);
4242 }
4243 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4244 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4245 if (ToClass->getDefinition()) {
4246 // Do nothing.
4247 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4248 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4249 ASTNodeImporter::IDK_Basic);
4250 } else {
4251 CompleteDecl(ToClass);
4252 }
4253 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4254 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4255 if (ToProto->getDefinition()) {
4256 // Do nothing.
4257 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4258 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4259 ASTNodeImporter::IDK_Basic);
4260 } else {
4261 CompleteDecl(ToProto);
4262 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004263 }
4264
4265 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004266}
4267
4268Expr *ASTImporter::Import(Expr *FromE) {
4269 if (!FromE)
4270 return 0;
4271
4272 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4273}
4274
4275Stmt *ASTImporter::Import(Stmt *FromS) {
4276 if (!FromS)
4277 return 0;
4278
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004279 // Check whether we've already imported this declaration.
4280 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4281 if (Pos != ImportedStmts.end())
4282 return Pos->second;
4283
4284 // Import the type
4285 ASTNodeImporter Importer(*this);
4286 Stmt *ToS = Importer.Visit(FromS);
4287 if (!ToS)
4288 return 0;
4289
4290 // Record the imported declaration.
4291 ImportedStmts[FromS] = ToS;
4292 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004293}
4294
4295NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4296 if (!FromNNS)
4297 return 0;
4298
Douglas Gregor90ebf252011-04-27 16:48:40 +00004299 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4300
4301 switch (FromNNS->getKind()) {
4302 case NestedNameSpecifier::Identifier:
4303 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4304 return NestedNameSpecifier::Create(ToContext, prefix, II);
4305 }
4306 return 0;
4307
4308 case NestedNameSpecifier::Namespace:
4309 if (NamespaceDecl *NS =
4310 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4311 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4312 }
4313 return 0;
4314
4315 case NestedNameSpecifier::NamespaceAlias:
4316 if (NamespaceAliasDecl *NSAD =
4317 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4318 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4319 }
4320 return 0;
4321
4322 case NestedNameSpecifier::Global:
4323 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4324
4325 case NestedNameSpecifier::TypeSpec:
4326 case NestedNameSpecifier::TypeSpecWithTemplate: {
4327 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4328 if (!T.isNull()) {
4329 bool bTemplate = FromNNS->getKind() ==
4330 NestedNameSpecifier::TypeSpecWithTemplate;
4331 return NestedNameSpecifier::Create(ToContext, prefix,
4332 bTemplate, T.getTypePtr());
4333 }
4334 }
4335 return 0;
4336 }
4337
4338 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004339}
4340
Douglas Gregor14454802011-02-25 02:25:35 +00004341NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4342 // FIXME: Implement!
4343 return NestedNameSpecifierLoc();
4344}
4345
Douglas Gregore2e50d332010-12-01 01:36:18 +00004346TemplateName ASTImporter::Import(TemplateName From) {
4347 switch (From.getKind()) {
4348 case TemplateName::Template:
4349 if (TemplateDecl *ToTemplate
4350 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4351 return TemplateName(ToTemplate);
4352
4353 return TemplateName();
4354
4355 case TemplateName::OverloadedTemplate: {
4356 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4357 UnresolvedSet<2> ToTemplates;
4358 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4359 E = FromStorage->end();
4360 I != E; ++I) {
4361 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4362 ToTemplates.addDecl(To);
4363 else
4364 return TemplateName();
4365 }
4366 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4367 ToTemplates.end());
4368 }
4369
4370 case TemplateName::QualifiedTemplate: {
4371 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4372 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4373 if (!Qualifier)
4374 return TemplateName();
4375
4376 if (TemplateDecl *ToTemplate
4377 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4378 return ToContext.getQualifiedTemplateName(Qualifier,
4379 QTN->hasTemplateKeyword(),
4380 ToTemplate);
4381
4382 return TemplateName();
4383 }
4384
4385 case TemplateName::DependentTemplate: {
4386 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4387 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4388 if (!Qualifier)
4389 return TemplateName();
4390
4391 if (DTN->isIdentifier()) {
4392 return ToContext.getDependentTemplateName(Qualifier,
4393 Import(DTN->getIdentifier()));
4394 }
4395
4396 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4397 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004398
4399 case TemplateName::SubstTemplateTemplateParm: {
4400 SubstTemplateTemplateParmStorage *subst
4401 = From.getAsSubstTemplateTemplateParm();
4402 TemplateTemplateParmDecl *param
4403 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4404 if (!param)
4405 return TemplateName();
4406
4407 TemplateName replacement = Import(subst->getReplacement());
4408 if (replacement.isNull()) return TemplateName();
4409
4410 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4411 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004412
4413 case TemplateName::SubstTemplateTemplateParmPack: {
4414 SubstTemplateTemplateParmPackStorage *SubstPack
4415 = From.getAsSubstTemplateTemplateParmPack();
4416 TemplateTemplateParmDecl *Param
4417 = cast_or_null<TemplateTemplateParmDecl>(
4418 Import(SubstPack->getParameterPack()));
4419 if (!Param)
4420 return TemplateName();
4421
4422 ASTNodeImporter Importer(*this);
4423 TemplateArgument ArgPack
4424 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4425 if (ArgPack.isNull())
4426 return TemplateName();
4427
4428 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4429 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004430 }
4431
4432 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004433}
4434
Douglas Gregor62d311f2010-02-09 19:21:46 +00004435SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4436 if (FromLoc.isInvalid())
4437 return SourceLocation();
4438
Douglas Gregor811663e2010-02-10 00:15:17 +00004439 SourceManager &FromSM = FromContext.getSourceManager();
4440
4441 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004442 // don't have to import macro expansions.
4443 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004444 FromLoc = FromSM.getSpellingLoc(FromLoc);
4445 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4446 SourceManager &ToSM = ToContext.getSourceManager();
4447 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004448 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004449}
4450
4451SourceRange ASTImporter::Import(SourceRange FromRange) {
4452 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4453}
4454
Douglas Gregor811663e2010-02-10 00:15:17 +00004455FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004456 llvm::DenseMap<FileID, FileID>::iterator Pos
4457 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004458 if (Pos != ImportedFileIDs.end())
4459 return Pos->second;
4460
4461 SourceManager &FromSM = FromContext.getSourceManager();
4462 SourceManager &ToSM = ToContext.getSourceManager();
4463 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004464 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004465
4466 // Include location of this file.
4467 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4468
4469 // Map the FileID for to the "to" source manager.
4470 FileID ToID;
4471 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004472 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004473 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4474 // disk again
4475 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4476 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004477 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004478 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4479 FromSLoc.getFile().getFileCharacteristic());
4480 } else {
4481 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004482 const llvm::MemoryBuffer *
4483 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004484 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004485 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004486 FromBuf->getBufferIdentifier());
4487 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4488 }
4489
4490
Sebastian Redl99219f12010-09-30 01:03:06 +00004491 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004492 return ToID;
4493}
4494
Douglas Gregor0a791672011-01-18 03:11:38 +00004495void ASTImporter::ImportDefinition(Decl *From) {
4496 Decl *To = Import(From);
4497 if (!To)
4498 return;
4499
4500 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4501 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004502
4503 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4504 if (!ToRecord->getDefinition()) {
4505 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004506 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004507 return;
4508 }
4509 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004510
4511 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4512 if (!ToEnum->getDefinition()) {
4513 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004514 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004515 return;
4516 }
4517 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004518
4519 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4520 if (!ToIFace->getDefinition()) {
4521 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004522 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004523 return;
4524 }
4525 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004526
Douglas Gregor2aa53772012-01-24 17:42:07 +00004527 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4528 if (!ToProto->getDefinition()) {
4529 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004530 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004531 return;
4532 }
4533 }
4534
Douglas Gregor0a791672011-01-18 03:11:38 +00004535 Importer.ImportDeclContext(FromDC, true);
4536 }
4537}
4538
Douglas Gregor96e578d2010-02-05 17:54:41 +00004539DeclarationName ASTImporter::Import(DeclarationName FromName) {
4540 if (!FromName)
4541 return DeclarationName();
4542
4543 switch (FromName.getNameKind()) {
4544 case DeclarationName::Identifier:
4545 return Import(FromName.getAsIdentifierInfo());
4546
4547 case DeclarationName::ObjCZeroArgSelector:
4548 case DeclarationName::ObjCOneArgSelector:
4549 case DeclarationName::ObjCMultiArgSelector:
4550 return Import(FromName.getObjCSelector());
4551
4552 case DeclarationName::CXXConstructorName: {
4553 QualType T = Import(FromName.getCXXNameType());
4554 if (T.isNull())
4555 return DeclarationName();
4556
4557 return ToContext.DeclarationNames.getCXXConstructorName(
4558 ToContext.getCanonicalType(T));
4559 }
4560
4561 case DeclarationName::CXXDestructorName: {
4562 QualType T = Import(FromName.getCXXNameType());
4563 if (T.isNull())
4564 return DeclarationName();
4565
4566 return ToContext.DeclarationNames.getCXXDestructorName(
4567 ToContext.getCanonicalType(T));
4568 }
4569
4570 case DeclarationName::CXXConversionFunctionName: {
4571 QualType T = Import(FromName.getCXXNameType());
4572 if (T.isNull())
4573 return DeclarationName();
4574
4575 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4576 ToContext.getCanonicalType(T));
4577 }
4578
4579 case DeclarationName::CXXOperatorName:
4580 return ToContext.DeclarationNames.getCXXOperatorName(
4581 FromName.getCXXOverloadedOperator());
4582
4583 case DeclarationName::CXXLiteralOperatorName:
4584 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4585 Import(FromName.getCXXLiteralIdentifier()));
4586
4587 case DeclarationName::CXXUsingDirective:
4588 // FIXME: STATICS!
4589 return DeclarationName::getUsingDirectiveName();
4590 }
4591
David Blaikiee4d798f2012-01-20 21:50:17 +00004592 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004593}
4594
Douglas Gregore2e50d332010-12-01 01:36:18 +00004595IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004596 if (!FromId)
4597 return 0;
4598
4599 return &ToContext.Idents.get(FromId->getName());
4600}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004601
Douglas Gregor43f54792010-02-17 02:12:47 +00004602Selector ASTImporter::Import(Selector FromSel) {
4603 if (FromSel.isNull())
4604 return Selector();
4605
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004606 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004607 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4608 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4609 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4610 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4611}
4612
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004613DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4614 DeclContext *DC,
4615 unsigned IDNS,
4616 NamedDecl **Decls,
4617 unsigned NumDecls) {
4618 return Name;
4619}
4620
4621DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004622 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004623}
4624
4625DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004626 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004627}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004628
Douglas Gregor2e15c842012-02-01 21:00:38 +00004629void ASTImporter::CompleteDecl (Decl *D) {
4630 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4631 if (!ID->getDefinition())
4632 ID->startDefinition();
4633 }
4634 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4635 if (!PD->getDefinition())
4636 PD->startDefinition();
4637 }
4638 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4639 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4640 TD->startDefinition();
4641 TD->setCompleteDefinition(true);
4642 }
4643 }
4644 else {
4645 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4646 }
4647}
4648
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004649Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4650 ImportedDecls[From] = To;
4651 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004652}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004653
Douglas Gregordd6006f2012-07-17 21:16:27 +00004654bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4655 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004656 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004657 = ImportedTypes.find(From.getTypePtr());
4658 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4659 return true;
4660
Douglas Gregordd6006f2012-07-17 21:16:27 +00004661 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4662 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004663 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004664}