blob: c9968e427a4a482ba20912417c21e25447be1281 [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);
1515 EPI.ExceptionSpecDecl = Importer.Import(EPI.ExceptionSpecDecl);
1516 EPI.ExceptionSpecTemplate = Importer.Import(EPI.ExceptionSpecTemplate);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001517
1518 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001519 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001520}
1521
Sean Callananda6df8a2011-08-11 16:56:07 +00001522QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1523 QualType ToInnerType = Importer.Import(T->getInnerType());
1524 if (ToInnerType.isNull())
1525 return QualType();
1526
1527 return Importer.getToContext().getParenType(ToInnerType);
1528}
1529
John McCall424cec92011-01-19 06:33:43 +00001530QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001531 TypedefNameDecl *ToDecl
1532 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001533 if (!ToDecl)
1534 return QualType();
1535
1536 return Importer.getToContext().getTypeDeclType(ToDecl);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1541 if (!ToExpr)
1542 return QualType();
1543
1544 return Importer.getToContext().getTypeOfExprType(ToExpr);
1545}
1546
John McCall424cec92011-01-19 06:33:43 +00001547QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001548 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1549 if (ToUnderlyingType.isNull())
1550 return QualType();
1551
1552 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1553}
1554
John McCall424cec92011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001556 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001557 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1558 if (!ToExpr)
1559 return QualType();
1560
Douglas Gregor81495f32012-02-12 18:42:33 +00001561 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1562 if (UnderlyingType.isNull())
1563 return QualType();
1564
1565 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001566}
1567
Alexis Hunte852b102011-05-24 22:41:36 +00001568QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1569 QualType ToBaseType = Importer.Import(T->getBaseType());
1570 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1571 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1572 return QualType();
1573
1574 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1575 ToUnderlyingType,
1576 T->getUTTKind());
1577}
1578
Richard Smith30482bc2011-02-20 03:19:35 +00001579QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1580 // FIXME: Make sure that the "to" context supports C++0x!
1581 QualType FromDeduced = T->getDeducedType();
1582 QualType ToDeduced;
1583 if (!FromDeduced.isNull()) {
1584 ToDeduced = Importer.Import(FromDeduced);
1585 if (ToDeduced.isNull())
1586 return QualType();
1587 }
1588
1589 return Importer.getToContext().getAutoType(ToDeduced);
1590}
1591
John McCall424cec92011-01-19 06:33:43 +00001592QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001593 RecordDecl *ToDecl
1594 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1595 if (!ToDecl)
1596 return QualType();
1597
1598 return Importer.getToContext().getTagDeclType(ToDecl);
1599}
1600
John McCall424cec92011-01-19 06:33:43 +00001601QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001602 EnumDecl *ToDecl
1603 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1604 if (!ToDecl)
1605 return QualType();
1606
1607 return Importer.getToContext().getTagDeclType(ToDecl);
1608}
1609
Douglas Gregore2e50d332010-12-01 01:36:18 +00001610QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001611 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001612 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1613 if (ToTemplate.isNull())
1614 return QualType();
1615
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001616 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001617 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1618 return QualType();
1619
1620 QualType ToCanonType;
1621 if (!QualType(T, 0).isCanonical()) {
1622 QualType FromCanonType
1623 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1624 ToCanonType =Importer.Import(FromCanonType);
1625 if (ToCanonType.isNull())
1626 return QualType();
1627 }
1628 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1629 ToTemplateArgs.data(),
1630 ToTemplateArgs.size(),
1631 ToCanonType);
1632}
1633
John McCall424cec92011-01-19 06:33:43 +00001634QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001635 NestedNameSpecifier *ToQualifier = 0;
1636 // Note: the qualifier in an ElaboratedType is optional.
1637 if (T->getQualifier()) {
1638 ToQualifier = Importer.Import(T->getQualifier());
1639 if (!ToQualifier)
1640 return QualType();
1641 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001642
1643 QualType ToNamedType = Importer.Import(T->getNamedType());
1644 if (ToNamedType.isNull())
1645 return QualType();
1646
Abramo Bagnara6150c882010-05-11 21:36:43 +00001647 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1648 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001649}
1650
John McCall424cec92011-01-19 06:33:43 +00001651QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001652 ObjCInterfaceDecl *Class
1653 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1654 if (!Class)
1655 return QualType();
1656
John McCall8b07ec22010-05-15 11:32:37 +00001657 return Importer.getToContext().getObjCInterfaceType(Class);
1658}
1659
John McCall424cec92011-01-19 06:33:43 +00001660QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001661 QualType ToBaseType = Importer.Import(T->getBaseType());
1662 if (ToBaseType.isNull())
1663 return QualType();
1664
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001665 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001666 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001667 PEnd = T->qual_end();
1668 P != PEnd; ++P) {
1669 ObjCProtocolDecl *Protocol
1670 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1671 if (!Protocol)
1672 return QualType();
1673 Protocols.push_back(Protocol);
1674 }
1675
John McCall8b07ec22010-05-15 11:32:37 +00001676 return Importer.getToContext().getObjCObjectType(ToBaseType,
1677 Protocols.data(),
1678 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001679}
1680
John McCall424cec92011-01-19 06:33:43 +00001681QualType
1682ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001683 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1684 if (ToPointeeType.isNull())
1685 return QualType();
1686
John McCall8b07ec22010-05-15 11:32:37 +00001687 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001688}
1689
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001690//----------------------------------------------------------------------------
1691// Import Declarations
1692//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001693bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1694 DeclContext *&LexicalDC,
1695 DeclarationName &Name,
1696 SourceLocation &Loc) {
1697 // Import the context of this declaration.
1698 DC = Importer.ImportContext(D->getDeclContext());
1699 if (!DC)
1700 return true;
1701
1702 LexicalDC = DC;
1703 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1704 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1705 if (!LexicalDC)
1706 return true;
1707 }
1708
1709 // Import the name of this declaration.
1710 Name = Importer.Import(D->getDeclName());
1711 if (D->getDeclName() && !Name)
1712 return true;
1713
1714 // Import the location of this declaration.
1715 Loc = Importer.Import(D->getLocation());
1716 return false;
1717}
1718
Douglas Gregord451ea92011-07-29 23:31:30 +00001719void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1720 if (!FromD)
1721 return;
1722
1723 if (!ToD) {
1724 ToD = Importer.Import(FromD);
1725 if (!ToD)
1726 return;
1727 }
1728
1729 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1730 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1731 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1732 ImportDefinition(FromRecord, ToRecord);
1733 }
1734 }
1735 return;
1736 }
1737
1738 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1739 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1740 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1741 ImportDefinition(FromEnum, ToEnum);
1742 }
1743 }
1744 return;
1745 }
1746}
1747
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001748void
1749ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1750 DeclarationNameInfo& To) {
1751 // NOTE: To.Name and To.Loc are already imported.
1752 // We only have to import To.LocInfo.
1753 switch (To.getName().getNameKind()) {
1754 case DeclarationName::Identifier:
1755 case DeclarationName::ObjCZeroArgSelector:
1756 case DeclarationName::ObjCOneArgSelector:
1757 case DeclarationName::ObjCMultiArgSelector:
1758 case DeclarationName::CXXUsingDirective:
1759 return;
1760
1761 case DeclarationName::CXXOperatorName: {
1762 SourceRange Range = From.getCXXOperatorNameRange();
1763 To.setCXXOperatorNameRange(Importer.Import(Range));
1764 return;
1765 }
1766 case DeclarationName::CXXLiteralOperatorName: {
1767 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1768 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1769 return;
1770 }
1771 case DeclarationName::CXXConstructorName:
1772 case DeclarationName::CXXDestructorName:
1773 case DeclarationName::CXXConversionFunctionName: {
1774 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1775 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1776 return;
1777 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001778 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001779 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001780}
1781
Douglas Gregor2e15c842012-02-01 21:00:38 +00001782void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001783 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001784 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001785 return;
1786 }
1787
Douglas Gregor968d6332010-02-21 18:24:45 +00001788 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1789 FromEnd = FromDC->decls_end();
1790 From != FromEnd;
1791 ++From)
1792 Importer.Import(*From);
1793}
1794
Douglas Gregord451ea92011-07-29 23:31:30 +00001795bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001796 ImportDefinitionKind Kind) {
1797 if (To->getDefinition() || To->isBeingDefined()) {
1798 if (Kind == IDK_Everything)
1799 ImportDeclContext(From, /*ForceImport=*/true);
1800
Douglas Gregore2e50d332010-12-01 01:36:18 +00001801 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001802 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001803
1804 To->startDefinition();
1805
1806 // Add base classes.
1807 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1808 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001809
1810 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1811 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1812 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1813 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1814 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1815 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1816 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1817 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1818 ToData.Aggregate = FromData.Aggregate;
1819 ToData.PlainOldData = FromData.PlainOldData;
1820 ToData.Empty = FromData.Empty;
1821 ToData.Polymorphic = FromData.Polymorphic;
1822 ToData.Abstract = FromData.Abstract;
1823 ToData.IsStandardLayout = FromData.IsStandardLayout;
1824 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1825 ToData.HasPrivateFields = FromData.HasPrivateFields;
1826 ToData.HasProtectedFields = FromData.HasProtectedFields;
1827 ToData.HasPublicFields = FromData.HasPublicFields;
1828 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001829 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001830 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001831 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1832 ToData.HasConstexprNonCopyMoveConstructor
1833 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001834 ToData.DefaultedDefaultConstructorIsConstexpr
1835 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001836 ToData.HasConstexprDefaultConstructor
1837 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001838 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1839 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1840 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1841 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1842 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001843 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001844 ToData.HasNonLiteralTypeFieldsOrBases
1845 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001846 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001847 ToData.UserProvidedDefaultConstructor
1848 = FromData.UserProvidedDefaultConstructor;
1849 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1850 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1851 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1852 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1853 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1854 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1855 ToData.FailedImplicitMoveConstructor
1856 = FromData.FailedImplicitMoveConstructor;
1857 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001858 ToData.IsLambda = FromData.IsLambda;
1859
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001860 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001861 for (CXXRecordDecl::base_class_iterator
1862 Base1 = FromCXX->bases_begin(),
1863 FromBaseEnd = FromCXX->bases_end();
1864 Base1 != FromBaseEnd;
1865 ++Base1) {
1866 QualType T = Importer.Import(Base1->getType());
1867 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001868 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001869
1870 SourceLocation EllipsisLoc;
1871 if (Base1->isPackExpansion())
1872 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001873
1874 // Ensure that we have a definition for the base.
1875 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1876
Douglas Gregore2e50d332010-12-01 01:36:18 +00001877 Bases.push_back(
1878 new (Importer.getToContext())
1879 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1880 Base1->isVirtual(),
1881 Base1->isBaseOfClass(),
1882 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001883 Importer.Import(Base1->getTypeSourceInfo()),
1884 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001885 }
1886 if (!Bases.empty())
1887 ToCXX->setBases(Bases.data(), Bases.size());
1888 }
1889
Douglas Gregor2e15c842012-02-01 21:00:38 +00001890 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001891 ImportDeclContext(From, /*ForceImport=*/true);
1892
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001894 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001895}
1896
Douglas Gregord451ea92011-07-29 23:31:30 +00001897bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001898 ImportDefinitionKind Kind) {
1899 if (To->getDefinition() || To->isBeingDefined()) {
1900 if (Kind == IDK_Everything)
1901 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001902 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001903 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001904
1905 To->startDefinition();
1906
1907 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1908 if (T.isNull())
1909 return true;
1910
1911 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1912 if (ToPromotionType.isNull())
1913 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001914
1915 if (shouldForceImportDeclContext(Kind))
1916 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001917
1918 // FIXME: we might need to merge the number of positive or negative bits
1919 // if the enumerator lists don't match.
1920 To->completeDefinition(T, ToPromotionType,
1921 From->getNumPositiveBits(),
1922 From->getNumNegativeBits());
1923 return false;
1924}
1925
Douglas Gregora082a492010-11-30 19:14:50 +00001926TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1927 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001928 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001929 ToParams.reserve(Params->size());
1930 for (TemplateParameterList::iterator P = Params->begin(),
1931 PEnd = Params->end();
1932 P != PEnd; ++P) {
1933 Decl *To = Importer.Import(*P);
1934 if (!To)
1935 return 0;
1936
1937 ToParams.push_back(cast<NamedDecl>(To));
1938 }
1939
1940 return TemplateParameterList::Create(Importer.getToContext(),
1941 Importer.Import(Params->getTemplateLoc()),
1942 Importer.Import(Params->getLAngleLoc()),
1943 ToParams.data(), ToParams.size(),
1944 Importer.Import(Params->getRAngleLoc()));
1945}
1946
Douglas Gregore2e50d332010-12-01 01:36:18 +00001947TemplateArgument
1948ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1949 switch (From.getKind()) {
1950 case TemplateArgument::Null:
1951 return TemplateArgument();
1952
1953 case TemplateArgument::Type: {
1954 QualType ToType = Importer.Import(From.getAsType());
1955 if (ToType.isNull())
1956 return TemplateArgument();
1957 return TemplateArgument(ToType);
1958 }
1959
1960 case TemplateArgument::Integral: {
1961 QualType ToType = Importer.Import(From.getIntegralType());
1962 if (ToType.isNull())
1963 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001964 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001965 }
1966
1967 case TemplateArgument::Declaration:
1968 if (Decl *To = Importer.Import(From.getAsDecl()))
1969 return TemplateArgument(To);
1970 return TemplateArgument();
1971
1972 case TemplateArgument::Template: {
1973 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1974 if (ToTemplate.isNull())
1975 return TemplateArgument();
1976
1977 return TemplateArgument(ToTemplate);
1978 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001979
1980 case TemplateArgument::TemplateExpansion: {
1981 TemplateName ToTemplate
1982 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1983 if (ToTemplate.isNull())
1984 return TemplateArgument();
1985
Douglas Gregore1d60df2011-01-14 23:41:42 +00001986 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001987 }
1988
Douglas Gregore2e50d332010-12-01 01:36:18 +00001989 case TemplateArgument::Expression:
1990 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1991 return TemplateArgument(ToExpr);
1992 return TemplateArgument();
1993
1994 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001995 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001996 ToPack.reserve(From.pack_size());
1997 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1998 return TemplateArgument();
1999
2000 TemplateArgument *ToArgs
2001 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2002 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2003 return TemplateArgument(ToArgs, ToPack.size());
2004 }
2005 }
2006
2007 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002008}
2009
2010bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2011 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002012 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002013 for (unsigned I = 0; I != NumFromArgs; ++I) {
2014 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2015 if (To.isNull() && !FromArgs[I].isNull())
2016 return true;
2017
2018 ToArgs.push_back(To);
2019 }
2020
2021 return false;
2022}
2023
Douglas Gregor5c73e912010-02-11 00:48:18 +00002024bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002025 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002026 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002027 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002028 Importer.getNonEquivalentDecls(),
2029 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002030 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002031}
2032
Douglas Gregor98c10182010-02-12 22:17:39 +00002033bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002034 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002035 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002036 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002037 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002038}
2039
Douglas Gregora082a492010-11-30 19:14:50 +00002040bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2041 ClassTemplateDecl *To) {
2042 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2043 Importer.getToContext(),
2044 Importer.getNonEquivalentDecls());
2045 return Ctx.IsStructurallyEquivalent(From, To);
2046}
2047
Douglas Gregore4c83e42010-02-09 22:48:33 +00002048Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002049 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002050 << D->getDeclKindName();
2051 return 0;
2052}
2053
Sean Callanan65198272011-11-17 23:20:56 +00002054Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2055 TranslationUnitDecl *ToD =
2056 Importer.getToContext().getTranslationUnitDecl();
2057
2058 Importer.Imported(D, ToD);
2059
2060 return ToD;
2061}
2062
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002063Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2064 // Import the major distinguishing characteristics of this namespace.
2065 DeclContext *DC, *LexicalDC;
2066 DeclarationName Name;
2067 SourceLocation Loc;
2068 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2069 return 0;
2070
2071 NamespaceDecl *MergeWithNamespace = 0;
2072 if (!Name) {
2073 // This is an anonymous namespace. Adopt an existing anonymous
2074 // namespace if we can.
2075 // FIXME: Not testable.
2076 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2077 MergeWithNamespace = TU->getAnonymousNamespace();
2078 else
2079 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2080 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002081 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002082 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2083 DC->localUncachedLookup(Name, FoundDecls);
2084 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2085 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002086 continue;
2087
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002088 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002089 MergeWithNamespace = FoundNS;
2090 ConflictingDecls.clear();
2091 break;
2092 }
2093
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002094 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002095 }
2096
2097 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002098 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002099 ConflictingDecls.data(),
2100 ConflictingDecls.size());
2101 }
2102 }
2103
2104 // Create the "to" namespace, if needed.
2105 NamespaceDecl *ToNamespace = MergeWithNamespace;
2106 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002107 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002108 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002109 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002110 Loc, Name.getAsIdentifierInfo(),
2111 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002112 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002113 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002114
2115 // If this is an anonymous namespace, register it as the anonymous
2116 // namespace within its context.
2117 if (!Name) {
2118 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2119 TU->setAnonymousNamespace(ToNamespace);
2120 else
2121 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2122 }
2123 }
2124 Importer.Imported(D, ToNamespace);
2125
2126 ImportDeclContext(D);
2127
2128 return ToNamespace;
2129}
2130
Richard Smithdda56e42011-04-15 14:24:37 +00002131Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002132 // Import the major distinguishing characteristics of this typedef.
2133 DeclContext *DC, *LexicalDC;
2134 DeclarationName Name;
2135 SourceLocation Loc;
2136 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2137 return 0;
2138
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002139 // If this typedef is not in block scope, determine whether we've
2140 // seen a typedef with the same name (that we can merge with) or any
2141 // other entity by that name (which name lookup could conflict with).
2142 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002143 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002144 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002145 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2146 DC->localUncachedLookup(Name, FoundDecls);
2147 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2148 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002149 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002150 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002151 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002152 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2153 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002154 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002155 }
2156
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002157 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002158 }
2159
2160 if (!ConflictingDecls.empty()) {
2161 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2162 ConflictingDecls.data(),
2163 ConflictingDecls.size());
2164 if (!Name)
2165 return 0;
2166 }
2167 }
2168
Douglas Gregorb4964f72010-02-15 23:54:17 +00002169 // Import the underlying type of this typedef;
2170 QualType T = Importer.Import(D->getUnderlyingType());
2171 if (T.isNull())
2172 return 0;
2173
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002174 // Create the new typedef node.
2175 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002176 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002177 TypedefNameDecl *ToTypedef;
2178 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002179 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2180 StartL, Loc,
2181 Name.getAsIdentifierInfo(),
2182 TInfo);
2183 else
Richard Smithdda56e42011-04-15 14:24:37 +00002184 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2185 StartL, Loc,
2186 Name.getAsIdentifierInfo(),
2187 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002188
Douglas Gregordd483172010-02-22 17:42:47 +00002189 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002190 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002191 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002192 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002193
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002194 return ToTypedef;
2195}
2196
Richard Smithdda56e42011-04-15 14:24:37 +00002197Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2198 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2199}
2200
2201Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2202 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2203}
2204
Douglas Gregor98c10182010-02-12 22:17:39 +00002205Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2206 // Import the major distinguishing characteristics of this enum.
2207 DeclContext *DC, *LexicalDC;
2208 DeclarationName Name;
2209 SourceLocation Loc;
2210 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2211 return 0;
2212
2213 // Figure out what enum name we're looking for.
2214 unsigned IDNS = Decl::IDNS_Tag;
2215 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002216 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2217 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002218 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002219 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002220 IDNS |= Decl::IDNS_Ordinary;
2221
2222 // We may already have an enum of the same name; try to find and match it.
2223 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002224 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002225 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2226 DC->localUncachedLookup(SearchName, FoundDecls);
2227 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2228 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002229 continue;
2230
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002231 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002232 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002233 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2234 Found = Tag->getDecl();
2235 }
2236
2237 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002238 if (IsStructuralMatch(D, FoundEnum))
2239 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002240 }
2241
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002242 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002243 }
2244
2245 if (!ConflictingDecls.empty()) {
2246 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2247 ConflictingDecls.data(),
2248 ConflictingDecls.size());
2249 }
2250 }
2251
2252 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002253 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2254 Importer.Import(D->getLocStart()),
2255 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002256 D->isScoped(), D->isScopedUsingClassTag(),
2257 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002258 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002259 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002260 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002261 D2->setLexicalDeclContext(LexicalDC);
2262 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002263 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002264
2265 // Import the integer type.
2266 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2267 if (ToIntegerType.isNull())
2268 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002269 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002270
2271 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002272 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002273 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002274
Douglas Gregor3996e242010-02-15 22:01:00 +00002275 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002276}
2277
Douglas Gregor5c73e912010-02-11 00:48:18 +00002278Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2279 // If this record has a definition in the translation unit we're coming from,
2280 // but this particular declaration is not that definition, import the
2281 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002282 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002283 if (Definition && Definition != D) {
2284 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002285 if (!ImportedDef)
2286 return 0;
2287
2288 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002289 }
2290
2291 // Import the major distinguishing characteristics of this record.
2292 DeclContext *DC, *LexicalDC;
2293 DeclarationName Name;
2294 SourceLocation Loc;
2295 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2296 return 0;
2297
2298 // Figure out what structure name we're looking for.
2299 unsigned IDNS = Decl::IDNS_Tag;
2300 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002301 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2302 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002303 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002304 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002305 IDNS |= Decl::IDNS_Ordinary;
2306
2307 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002308 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002309 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002310 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002311 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2312 DC->localUncachedLookup(SearchName, FoundDecls);
2313 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2314 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002315 continue;
2316
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002317 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002318 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002319 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2320 Found = Tag->getDecl();
2321 }
2322
2323 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002324 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002325 if ((SearchName && !D->isCompleteDefinition())
2326 || (D->isCompleteDefinition() &&
2327 D->isAnonymousStructOrUnion()
2328 == FoundDef->isAnonymousStructOrUnion() &&
2329 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002330 // The record types structurally match, or the "from" translation
2331 // unit only had a forward declaration anyway; call it the same
2332 // function.
2333 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002334 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002335 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002336 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002337 // We have a forward declaration of this type, so adopt that forward
2338 // declaration rather than building a new one.
2339 AdoptDecl = FoundRecord;
2340 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002341 } else if (!SearchName) {
2342 continue;
2343 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002344 }
2345
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002346 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002347 }
2348
Douglas Gregordd6006f2012-07-17 21:16:27 +00002349 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002350 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2351 ConflictingDecls.data(),
2352 ConflictingDecls.size());
2353 }
2354 }
2355
2356 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002357 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002358 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002359 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002360 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002361 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002362 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002363 DC, StartLoc, Loc,
2364 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002365 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002366 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002367 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002368 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002369 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002370 }
Douglas Gregor14454802011-02-25 02:25:35 +00002371
2372 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002373 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002374 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002375 if (D->isAnonymousStructOrUnion())
2376 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002377 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002378
Douglas Gregor3996e242010-02-15 22:01:00 +00002379 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002380
Douglas Gregor95d82832012-01-24 18:36:04 +00002381 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002382 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002383
Douglas Gregor3996e242010-02-15 22:01:00 +00002384 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002385}
2386
Douglas Gregor98c10182010-02-12 22:17:39 +00002387Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2388 // Import the major distinguishing characteristics of this enumerator.
2389 DeclContext *DC, *LexicalDC;
2390 DeclarationName Name;
2391 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002392 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002393 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002394
2395 QualType T = Importer.Import(D->getType());
2396 if (T.isNull())
2397 return 0;
2398
Douglas Gregor98c10182010-02-12 22:17:39 +00002399 // Determine whether there are any other declarations with the same name and
2400 // in the same context.
2401 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002402 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002403 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002404 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2405 DC->localUncachedLookup(Name, FoundDecls);
2406 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2407 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002408 continue;
2409
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002410 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002411 }
2412
2413 if (!ConflictingDecls.empty()) {
2414 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2415 ConflictingDecls.data(),
2416 ConflictingDecls.size());
2417 if (!Name)
2418 return 0;
2419 }
2420 }
2421
2422 Expr *Init = Importer.Import(D->getInitExpr());
2423 if (D->getInitExpr() && !Init)
2424 return 0;
2425
2426 EnumConstantDecl *ToEnumerator
2427 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2428 Name.getAsIdentifierInfo(), T,
2429 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002430 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002432 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002433 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002434 return ToEnumerator;
2435}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002436
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002437Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2438 // Import the major distinguishing characteristics of this function.
2439 DeclContext *DC, *LexicalDC;
2440 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002441 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002442 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002443 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002444
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002445 // Try to find a function in our own ("to") context with the same name, same
2446 // type, and in the same context as the function we're importing.
2447 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002448 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002449 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002450 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2451 DC->localUncachedLookup(Name, FoundDecls);
2452 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2453 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002454 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002455
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002456 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002457 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2458 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002459 if (Importer.IsStructurallyEquivalent(D->getType(),
2460 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002461 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002462 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002463 }
2464
2465 // FIXME: Check for overloading more carefully, e.g., by boosting
2466 // Sema::IsOverload out to the AST library.
2467
2468 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002469 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002470 continue;
2471
2472 // Complain about inconsistent function types.
2473 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002474 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002475 Importer.ToDiag(FoundFunction->getLocation(),
2476 diag::note_odr_value_here)
2477 << FoundFunction->getType();
2478 }
2479 }
2480
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002481 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002482 }
2483
2484 if (!ConflictingDecls.empty()) {
2485 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2486 ConflictingDecls.data(),
2487 ConflictingDecls.size());
2488 if (!Name)
2489 return 0;
2490 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002491 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002492
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002493 DeclarationNameInfo NameInfo(Name, Loc);
2494 // Import additional name location/type info.
2495 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2496
Douglas Gregorb4964f72010-02-15 23:54:17 +00002497 // Import the type.
2498 QualType T = Importer.Import(D->getType());
2499 if (T.isNull())
2500 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002501
2502 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002503 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002504 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2505 P != PEnd; ++P) {
2506 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2507 if (!ToP)
2508 return 0;
2509
2510 Parameters.push_back(ToP);
2511 }
2512
2513 // Create the imported function.
2514 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002515 FunctionDecl *ToFunction = 0;
2516 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2517 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2518 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002519 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002520 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002521 FromConstructor->isExplicit(),
2522 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002523 D->isImplicit(),
2524 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002525 } else if (isa<CXXDestructorDecl>(D)) {
2526 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2527 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002528 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002529 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002530 D->isInlineSpecified(),
2531 D->isImplicit());
2532 } else if (CXXConversionDecl *FromConversion
2533 = dyn_cast<CXXConversionDecl>(D)) {
2534 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2535 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002536 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002537 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002538 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002539 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002540 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002541 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002542 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2543 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2544 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002545 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002546 NameInfo, T, TInfo,
2547 Method->isStatic(),
2548 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002549 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002550 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002551 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002552 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002553 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002554 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002555 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002556 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002557 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002558 D->hasWrittenPrototype(),
2559 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002560 }
John McCall3e11ebe2010-03-15 10:12:16 +00002561
2562 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002563 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002564 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002565 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002566 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2567 ToFunction->setTrivial(D->isTrivial());
2568 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002569 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002570
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002571 // Set the parameters.
2572 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002573 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002574 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002575 }
David Blaikie9c70e042011-09-21 18:16:56 +00002576 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002577
2578 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002579
2580 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002581 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002582
Douglas Gregor43f54792010-02-17 02:12:47 +00002583 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002584}
2585
Douglas Gregor00eace12010-02-21 18:29:16 +00002586Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2587 return VisitFunctionDecl(D);
2588}
2589
2590Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2591 return VisitCXXMethodDecl(D);
2592}
2593
2594Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2595 return VisitCXXMethodDecl(D);
2596}
2597
2598Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2599 return VisitCXXMethodDecl(D);
2600}
2601
Douglas Gregor5c73e912010-02-11 00:48:18 +00002602Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2603 // Import the major distinguishing characteristics of a variable.
2604 DeclContext *DC, *LexicalDC;
2605 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002606 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002607 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2608 return 0;
2609
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002610 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002611 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2612 DC->localUncachedLookup(Name, FoundDecls);
2613 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2614 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002615 if (Importer.IsStructurallyEquivalent(D->getType(),
2616 FoundField->getType())) {
2617 Importer.Imported(D, FoundField);
2618 return FoundField;
2619 }
2620
2621 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2622 << Name << D->getType() << FoundField->getType();
2623 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2624 << FoundField->getType();
2625 return 0;
2626 }
2627 }
2628
Douglas Gregorb4964f72010-02-15 23:54:17 +00002629 // Import the type.
2630 QualType T = Importer.Import(D->getType());
2631 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002632 return 0;
2633
2634 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2635 Expr *BitWidth = Importer.Import(D->getBitWidth());
2636 if (!BitWidth && D->getBitWidth())
2637 return 0;
2638
Abramo Bagnaradff19302011-03-08 08:55:46 +00002639 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2640 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002641 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002642 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002643 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002644 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002645 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002646 if (ToField->hasInClassInitializer())
2647 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002648 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002649 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002650 return ToField;
2651}
2652
Francois Pichet783dd6e2010-11-21 06:08:52 +00002653Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2654 // Import the major distinguishing characteristics of a variable.
2655 DeclContext *DC, *LexicalDC;
2656 DeclarationName Name;
2657 SourceLocation Loc;
2658 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2659 return 0;
2660
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002661 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002662 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2663 DC->localUncachedLookup(Name, FoundDecls);
2664 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002665 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002666 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002667 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002668 FoundField->getType(),
2669 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002670 Importer.Imported(D, FoundField);
2671 return FoundField;
2672 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002673
2674 // If there are more anonymous fields to check, continue.
2675 if (!Name && I < N-1)
2676 continue;
2677
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002678 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2679 << Name << D->getType() << FoundField->getType();
2680 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2681 << FoundField->getType();
2682 return 0;
2683 }
2684 }
2685
Francois Pichet783dd6e2010-11-21 06:08:52 +00002686 // Import the type.
2687 QualType T = Importer.Import(D->getType());
2688 if (T.isNull())
2689 return 0;
2690
2691 NamedDecl **NamedChain =
2692 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2693
2694 unsigned i = 0;
2695 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2696 PE = D->chain_end(); PI != PE; ++PI) {
2697 Decl* D = Importer.Import(*PI);
2698 if (!D)
2699 return 0;
2700 NamedChain[i++] = cast<NamedDecl>(D);
2701 }
2702
2703 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2704 Importer.getToContext(), DC,
2705 Loc, Name.getAsIdentifierInfo(), T,
2706 NamedChain, D->getChainingSize());
2707 ToIndirectField->setAccess(D->getAccess());
2708 ToIndirectField->setLexicalDeclContext(LexicalDC);
2709 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002710 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002711 return ToIndirectField;
2712}
2713
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002714Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2715 // Import the major distinguishing characteristics of an ivar.
2716 DeclContext *DC, *LexicalDC;
2717 DeclarationName Name;
2718 SourceLocation Loc;
2719 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2720 return 0;
2721
2722 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002723 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2724 DC->localUncachedLookup(Name, FoundDecls);
2725 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2726 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002727 if (Importer.IsStructurallyEquivalent(D->getType(),
2728 FoundIvar->getType())) {
2729 Importer.Imported(D, FoundIvar);
2730 return FoundIvar;
2731 }
2732
2733 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2734 << Name << D->getType() << FoundIvar->getType();
2735 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2736 << FoundIvar->getType();
2737 return 0;
2738 }
2739 }
2740
2741 // Import the type.
2742 QualType T = Importer.Import(D->getType());
2743 if (T.isNull())
2744 return 0;
2745
2746 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2747 Expr *BitWidth = Importer.Import(D->getBitWidth());
2748 if (!BitWidth && D->getBitWidth())
2749 return 0;
2750
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002751 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2752 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002753 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002754 Loc, Name.getAsIdentifierInfo(),
2755 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002756 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002757 ToIvar->setLexicalDeclContext(LexicalDC);
2758 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002759 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002760 return ToIvar;
2761
2762}
2763
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002764Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2765 // Import the major distinguishing characteristics of a variable.
2766 DeclContext *DC, *LexicalDC;
2767 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002768 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002769 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002770 return 0;
2771
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002772 // Try to find a variable in our own ("to") context with the same name and
2773 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002774 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002775 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002776 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002777 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002778 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2779 DC->localUncachedLookup(Name, FoundDecls);
2780 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2781 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002782 continue;
2783
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002784 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002785 // We have found a variable that we may need to merge with. Check it.
2786 if (isExternalLinkage(FoundVar->getLinkage()) &&
2787 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002788 if (Importer.IsStructurallyEquivalent(D->getType(),
2789 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002790 MergeWithVar = FoundVar;
2791 break;
2792 }
2793
Douglas Gregor56521c52010-02-12 17:23:39 +00002794 const ArrayType *FoundArray
2795 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2796 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002797 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002798 if (FoundArray && TArray) {
2799 if (isa<IncompleteArrayType>(FoundArray) &&
2800 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002801 // Import the type.
2802 QualType T = Importer.Import(D->getType());
2803 if (T.isNull())
2804 return 0;
2805
Douglas Gregor56521c52010-02-12 17:23:39 +00002806 FoundVar->setType(T);
2807 MergeWithVar = FoundVar;
2808 break;
2809 } else if (isa<IncompleteArrayType>(TArray) &&
2810 isa<ConstantArrayType>(FoundArray)) {
2811 MergeWithVar = FoundVar;
2812 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002813 }
2814 }
2815
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002816 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002817 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002818 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2819 << FoundVar->getType();
2820 }
2821 }
2822
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002823 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002824 }
2825
2826 if (MergeWithVar) {
2827 // An equivalent variable with external linkage has been found. Link
2828 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002829 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002830
2831 if (VarDecl *DDef = D->getDefinition()) {
2832 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2833 Importer.ToDiag(ExistingDef->getLocation(),
2834 diag::err_odr_variable_multiple_def)
2835 << Name;
2836 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2837 } else {
2838 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002839 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002840 if (DDef->isInitKnownICE()) {
2841 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2842 Eval->CheckedICE = true;
2843 Eval->IsICE = DDef->isInitICE();
2844 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002845 }
2846 }
2847
2848 return MergeWithVar;
2849 }
2850
2851 if (!ConflictingDecls.empty()) {
2852 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2853 ConflictingDecls.data(),
2854 ConflictingDecls.size());
2855 if (!Name)
2856 return 0;
2857 }
2858 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002859
Douglas Gregorb4964f72010-02-15 23:54:17 +00002860 // Import the type.
2861 QualType T = Importer.Import(D->getType());
2862 if (T.isNull())
2863 return 0;
2864
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002865 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002866 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002867 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2868 Importer.Import(D->getInnerLocStart()),
2869 Loc, Name.getAsIdentifierInfo(),
2870 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002871 D->getStorageClass(),
2872 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002873 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002874 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002875 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002876 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002877 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002878
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002879 // Merge the initializer.
2880 // FIXME: Can we really import any initializer? Alternatively, we could force
2881 // ourselves to import every declaration of a variable and then only use
2882 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002883 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002884
2885 // FIXME: Other bits to merge?
2886
2887 return ToVar;
2888}
2889
Douglas Gregor8b228d72010-02-17 21:22:52 +00002890Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2891 // Parameters are created in the translation unit's context, then moved
2892 // into the function declaration's context afterward.
2893 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2894
2895 // Import the name of this declaration.
2896 DeclarationName Name = Importer.Import(D->getDeclName());
2897 if (D->getDeclName() && !Name)
2898 return 0;
2899
2900 // Import the location of this declaration.
2901 SourceLocation Loc = Importer.Import(D->getLocation());
2902
2903 // Import the parameter's type.
2904 QualType T = Importer.Import(D->getType());
2905 if (T.isNull())
2906 return 0;
2907
2908 // Create the imported parameter.
2909 ImplicitParamDecl *ToParm
2910 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2911 Loc, Name.getAsIdentifierInfo(),
2912 T);
2913 return Importer.Imported(D, ToParm);
2914}
2915
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002916Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2917 // Parameters are created in the translation unit's context, then moved
2918 // into the function declaration's context afterward.
2919 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2920
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002921 // Import the name of this declaration.
2922 DeclarationName Name = Importer.Import(D->getDeclName());
2923 if (D->getDeclName() && !Name)
2924 return 0;
2925
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002926 // Import the location of this declaration.
2927 SourceLocation Loc = Importer.Import(D->getLocation());
2928
2929 // Import the parameter's type.
2930 QualType T = Importer.Import(D->getType());
2931 if (T.isNull())
2932 return 0;
2933
2934 // Create the imported parameter.
2935 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2936 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002937 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002938 Loc, Name.getAsIdentifierInfo(),
2939 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002940 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002941 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002942 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002943 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002944}
2945
Douglas Gregor43f54792010-02-17 02:12:47 +00002946Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2947 // Import the major distinguishing characteristics of a method.
2948 DeclContext *DC, *LexicalDC;
2949 DeclarationName Name;
2950 SourceLocation Loc;
2951 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2952 return 0;
2953
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002954 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2955 DC->localUncachedLookup(Name, FoundDecls);
2956 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2957 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002958 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2959 continue;
2960
2961 // Check return types.
2962 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2963 FoundMethod->getResultType())) {
2964 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2965 << D->isInstanceMethod() << Name
2966 << D->getResultType() << FoundMethod->getResultType();
2967 Importer.ToDiag(FoundMethod->getLocation(),
2968 diag::note_odr_objc_method_here)
2969 << D->isInstanceMethod() << Name;
2970 return 0;
2971 }
2972
2973 // Check the number of parameters.
2974 if (D->param_size() != FoundMethod->param_size()) {
2975 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2976 << D->isInstanceMethod() << Name
2977 << D->param_size() << FoundMethod->param_size();
2978 Importer.ToDiag(FoundMethod->getLocation(),
2979 diag::note_odr_objc_method_here)
2980 << D->isInstanceMethod() << Name;
2981 return 0;
2982 }
2983
2984 // Check parameter types.
2985 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2986 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2987 P != PEnd; ++P, ++FoundP) {
2988 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2989 (*FoundP)->getType())) {
2990 Importer.FromDiag((*P)->getLocation(),
2991 diag::err_odr_objc_method_param_type_inconsistent)
2992 << D->isInstanceMethod() << Name
2993 << (*P)->getType() << (*FoundP)->getType();
2994 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2995 << (*FoundP)->getType();
2996 return 0;
2997 }
2998 }
2999
3000 // Check variadic/non-variadic.
3001 // Check the number of parameters.
3002 if (D->isVariadic() != FoundMethod->isVariadic()) {
3003 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3004 << D->isInstanceMethod() << Name;
3005 Importer.ToDiag(FoundMethod->getLocation(),
3006 diag::note_odr_objc_method_here)
3007 << D->isInstanceMethod() << Name;
3008 return 0;
3009 }
3010
3011 // FIXME: Any other bits we need to merge?
3012 return Importer.Imported(D, FoundMethod);
3013 }
3014 }
3015
3016 // Import the result type.
3017 QualType ResultTy = Importer.Import(D->getResultType());
3018 if (ResultTy.isNull())
3019 return 0;
3020
Douglas Gregor12852d92010-03-08 14:59:44 +00003021 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3022
Douglas Gregor43f54792010-02-17 02:12:47 +00003023 ObjCMethodDecl *ToMethod
3024 = ObjCMethodDecl::Create(Importer.getToContext(),
3025 Loc,
3026 Importer.Import(D->getLocEnd()),
3027 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003028 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003029 D->isInstanceMethod(),
3030 D->isVariadic(),
3031 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003032 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003033 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003034 D->getImplementationControl(),
3035 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003036
3037 // FIXME: When we decide to merge method definitions, we'll need to
3038 // deal with implicit parameters.
3039
3040 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003041 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003042 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3043 FromPEnd = D->param_end();
3044 FromP != FromPEnd;
3045 ++FromP) {
3046 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3047 if (!ToP)
3048 return 0;
3049
3050 ToParams.push_back(ToP);
3051 }
3052
3053 // Set the parameters.
3054 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3055 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003056 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003057 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003058 SmallVector<SourceLocation, 12> SelLocs;
3059 D->getSelectorLocs(SelLocs);
3060 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003061
3062 ToMethod->setLexicalDeclContext(LexicalDC);
3063 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003064 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003065 return ToMethod;
3066}
3067
Douglas Gregor84c51c32010-02-18 01:47:50 +00003068Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3069 // Import the major distinguishing characteristics of a category.
3070 DeclContext *DC, *LexicalDC;
3071 DeclarationName Name;
3072 SourceLocation Loc;
3073 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3074 return 0;
3075
3076 ObjCInterfaceDecl *ToInterface
3077 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3078 if (!ToInterface)
3079 return 0;
3080
3081 // Determine if we've already encountered this category.
3082 ObjCCategoryDecl *MergeWithCategory
3083 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3084 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3085 if (!ToCategory) {
3086 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003087 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003088 Loc,
3089 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003090 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003091 ToInterface,
3092 Importer.Import(D->getIvarLBraceLoc()),
3093 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003094 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003095 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003096 Importer.Imported(D, ToCategory);
3097
Douglas Gregor84c51c32010-02-18 01:47:50 +00003098 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003099 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3100 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003101 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3102 = D->protocol_loc_begin();
3103 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3104 FromProtoEnd = D->protocol_end();
3105 FromProto != FromProtoEnd;
3106 ++FromProto, ++FromProtoLoc) {
3107 ObjCProtocolDecl *ToProto
3108 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3109 if (!ToProto)
3110 return 0;
3111 Protocols.push_back(ToProto);
3112 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3113 }
3114
3115 // FIXME: If we're merging, make sure that the protocol list is the same.
3116 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3117 ProtocolLocs.data(), Importer.getToContext());
3118
3119 } else {
3120 Importer.Imported(D, ToCategory);
3121 }
3122
3123 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003124 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003125
3126 // If we have an implementation, import it as well.
3127 if (D->getImplementation()) {
3128 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003129 = cast_or_null<ObjCCategoryImplDecl>(
3130 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003131 if (!Impl)
3132 return 0;
3133
3134 ToCategory->setImplementation(Impl);
3135 }
3136
3137 return ToCategory;
3138}
3139
Douglas Gregor2aa53772012-01-24 17:42:07 +00003140bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3141 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003142 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003143 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003144 if (shouldForceImportDeclContext(Kind))
3145 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003146 return false;
3147 }
3148
3149 // Start the protocol definition
3150 To->startDefinition();
3151
3152 // Import protocols
3153 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3154 SmallVector<SourceLocation, 4> ProtocolLocs;
3155 ObjCProtocolDecl::protocol_loc_iterator
3156 FromProtoLoc = From->protocol_loc_begin();
3157 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3158 FromProtoEnd = From->protocol_end();
3159 FromProto != FromProtoEnd;
3160 ++FromProto, ++FromProtoLoc) {
3161 ObjCProtocolDecl *ToProto
3162 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3163 if (!ToProto)
3164 return true;
3165 Protocols.push_back(ToProto);
3166 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3167 }
3168
3169 // FIXME: If we're merging, make sure that the protocol list is the same.
3170 To->setProtocolList(Protocols.data(), Protocols.size(),
3171 ProtocolLocs.data(), Importer.getToContext());
3172
Douglas Gregor2e15c842012-02-01 21:00:38 +00003173 if (shouldForceImportDeclContext(Kind)) {
3174 // Import all of the members of this protocol.
3175 ImportDeclContext(From, /*ForceImport=*/true);
3176 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003177 return false;
3178}
3179
Douglas Gregor98d156a2010-02-17 16:12:00 +00003180Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003181 // If this protocol has a definition in the translation unit we're coming
3182 // from, but this particular declaration is not that definition, import the
3183 // definition and map to that.
3184 ObjCProtocolDecl *Definition = D->getDefinition();
3185 if (Definition && Definition != D) {
3186 Decl *ImportedDef = Importer.Import(Definition);
3187 if (!ImportedDef)
3188 return 0;
3189
3190 return Importer.Imported(D, ImportedDef);
3191 }
3192
Douglas Gregor84c51c32010-02-18 01:47:50 +00003193 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003194 DeclContext *DC, *LexicalDC;
3195 DeclarationName Name;
3196 SourceLocation Loc;
3197 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3198 return 0;
3199
3200 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003201 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3202 DC->localUncachedLookup(Name, FoundDecls);
3203 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3204 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003205 continue;
3206
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003207 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003208 break;
3209 }
3210
3211 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003212 if (!ToProto) {
3213 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3214 Name.getAsIdentifierInfo(), Loc,
3215 Importer.Import(D->getAtStartLoc()),
3216 /*PrevDecl=*/0);
3217 ToProto->setLexicalDeclContext(LexicalDC);
3218 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003219 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003220
3221 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003222
Douglas Gregor2aa53772012-01-24 17:42:07 +00003223 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3224 return 0;
3225
Douglas Gregor98d156a2010-02-17 16:12:00 +00003226 return ToProto;
3227}
3228
Douglas Gregor2aa53772012-01-24 17:42:07 +00003229bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3230 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003231 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003232 if (To->getDefinition()) {
3233 // Check consistency of superclass.
3234 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3235 if (FromSuper) {
3236 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3237 if (!FromSuper)
3238 return true;
3239 }
3240
3241 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3242 if ((bool)FromSuper != (bool)ToSuper ||
3243 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3244 Importer.ToDiag(To->getLocation(),
3245 diag::err_odr_objc_superclass_inconsistent)
3246 << To->getDeclName();
3247 if (ToSuper)
3248 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3249 << To->getSuperClass()->getDeclName();
3250 else
3251 Importer.ToDiag(To->getLocation(),
3252 diag::note_odr_objc_missing_superclass);
3253 if (From->getSuperClass())
3254 Importer.FromDiag(From->getSuperClassLoc(),
3255 diag::note_odr_objc_superclass)
3256 << From->getSuperClass()->getDeclName();
3257 else
3258 Importer.FromDiag(From->getLocation(),
3259 diag::note_odr_objc_missing_superclass);
3260 }
3261
Douglas Gregor2e15c842012-02-01 21:00:38 +00003262 if (shouldForceImportDeclContext(Kind))
3263 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003264 return false;
3265 }
3266
3267 // Start the definition.
3268 To->startDefinition();
3269
3270 // If this class has a superclass, import it.
3271 if (From->getSuperClass()) {
3272 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3273 Importer.Import(From->getSuperClass()));
3274 if (!Super)
3275 return true;
3276
3277 To->setSuperClass(Super);
3278 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3279 }
3280
3281 // Import protocols
3282 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3283 SmallVector<SourceLocation, 4> ProtocolLocs;
3284 ObjCInterfaceDecl::protocol_loc_iterator
3285 FromProtoLoc = From->protocol_loc_begin();
3286
3287 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3288 FromProtoEnd = From->protocol_end();
3289 FromProto != FromProtoEnd;
3290 ++FromProto, ++FromProtoLoc) {
3291 ObjCProtocolDecl *ToProto
3292 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3293 if (!ToProto)
3294 return true;
3295 Protocols.push_back(ToProto);
3296 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3297 }
3298
3299 // FIXME: If we're merging, make sure that the protocol list is the same.
3300 To->setProtocolList(Protocols.data(), Protocols.size(),
3301 ProtocolLocs.data(), Importer.getToContext());
3302
3303 // Import categories. When the categories themselves are imported, they'll
3304 // hook themselves into this interface.
3305 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3306 FromCat = FromCat->getNextClassCategory())
3307 Importer.Import(FromCat);
3308
3309 // If we have an @implementation, import it as well.
3310 if (From->getImplementation()) {
3311 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3312 Importer.Import(From->getImplementation()));
3313 if (!Impl)
3314 return true;
3315
3316 To->setImplementation(Impl);
3317 }
3318
Douglas Gregor2e15c842012-02-01 21:00:38 +00003319 if (shouldForceImportDeclContext(Kind)) {
3320 // Import all of the members of this class.
3321 ImportDeclContext(From, /*ForceImport=*/true);
3322 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003323 return false;
3324}
3325
Douglas Gregor45635322010-02-16 01:20:57 +00003326Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003327 // If this class has a definition in the translation unit we're coming from,
3328 // but this particular declaration is not that definition, import the
3329 // definition and map to that.
3330 ObjCInterfaceDecl *Definition = D->getDefinition();
3331 if (Definition && Definition != D) {
3332 Decl *ImportedDef = Importer.Import(Definition);
3333 if (!ImportedDef)
3334 return 0;
3335
3336 return Importer.Imported(D, ImportedDef);
3337 }
3338
Douglas Gregor45635322010-02-16 01:20:57 +00003339 // Import the major distinguishing characteristics of an @interface.
3340 DeclContext *DC, *LexicalDC;
3341 DeclarationName Name;
3342 SourceLocation Loc;
3343 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3344 return 0;
3345
Douglas Gregor2aa53772012-01-24 17:42:07 +00003346 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003347 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003348 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3349 DC->localUncachedLookup(Name, FoundDecls);
3350 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3351 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003352 continue;
3353
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003354 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003355 break;
3356 }
3357
Douglas Gregor2aa53772012-01-24 17:42:07 +00003358 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003359 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003360 if (!ToIface) {
3361 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3362 Importer.Import(D->getAtStartLoc()),
3363 Name.getAsIdentifierInfo(),
3364 /*PrevDecl=*/0,Loc,
3365 D->isImplicitInterfaceDecl());
3366 ToIface->setLexicalDeclContext(LexicalDC);
3367 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003368 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003369 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003370
Douglas Gregor2aa53772012-01-24 17:42:07 +00003371 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3372 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003373
Douglas Gregor98d156a2010-02-17 16:12:00 +00003374 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003375}
3376
Douglas Gregor4da9d682010-12-07 15:32:12 +00003377Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3378 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3379 Importer.Import(D->getCategoryDecl()));
3380 if (!Category)
3381 return 0;
3382
3383 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3384 if (!ToImpl) {
3385 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3386 if (!DC)
3387 return 0;
3388
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003389 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003390 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003391 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003392 Category->getClassInterface(),
3393 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003394 Importer.Import(D->getAtStartLoc()),
3395 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003396
3397 DeclContext *LexicalDC = DC;
3398 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3399 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3400 if (!LexicalDC)
3401 return 0;
3402
3403 ToImpl->setLexicalDeclContext(LexicalDC);
3404 }
3405
Sean Callanan95e74be2011-10-21 02:57:43 +00003406 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003407 Category->setImplementation(ToImpl);
3408 }
3409
3410 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003411 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003412 return ToImpl;
3413}
3414
Douglas Gregorda8025c2010-12-07 01:26:03 +00003415Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3416 // Find the corresponding interface.
3417 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3418 Importer.Import(D->getClassInterface()));
3419 if (!Iface)
3420 return 0;
3421
3422 // Import the superclass, if any.
3423 ObjCInterfaceDecl *Super = 0;
3424 if (D->getSuperClass()) {
3425 Super = cast_or_null<ObjCInterfaceDecl>(
3426 Importer.Import(D->getSuperClass()));
3427 if (!Super)
3428 return 0;
3429 }
3430
3431 ObjCImplementationDecl *Impl = Iface->getImplementation();
3432 if (!Impl) {
3433 // We haven't imported an implementation yet. Create a new @implementation
3434 // now.
3435 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3436 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003437 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003438 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003439 Importer.Import(D->getAtStartLoc()),
3440 Importer.Import(D->getIvarLBraceLoc()),
3441 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003442
3443 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3444 DeclContext *LexicalDC
3445 = Importer.ImportContext(D->getLexicalDeclContext());
3446 if (!LexicalDC)
3447 return 0;
3448 Impl->setLexicalDeclContext(LexicalDC);
3449 }
3450
3451 // Associate the implementation with the class it implements.
3452 Iface->setImplementation(Impl);
3453 Importer.Imported(D, Iface->getImplementation());
3454 } else {
3455 Importer.Imported(D, Iface->getImplementation());
3456
3457 // Verify that the existing @implementation has the same superclass.
3458 if ((Super && !Impl->getSuperClass()) ||
3459 (!Super && Impl->getSuperClass()) ||
3460 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003461 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003462 Importer.ToDiag(Impl->getLocation(),
3463 diag::err_odr_objc_superclass_inconsistent)
3464 << Iface->getDeclName();
3465 // FIXME: It would be nice to have the location of the superclass
3466 // below.
3467 if (Impl->getSuperClass())
3468 Importer.ToDiag(Impl->getLocation(),
3469 diag::note_odr_objc_superclass)
3470 << Impl->getSuperClass()->getDeclName();
3471 else
3472 Importer.ToDiag(Impl->getLocation(),
3473 diag::note_odr_objc_missing_superclass);
3474 if (D->getSuperClass())
3475 Importer.FromDiag(D->getLocation(),
3476 diag::note_odr_objc_superclass)
3477 << D->getSuperClass()->getDeclName();
3478 else
3479 Importer.FromDiag(D->getLocation(),
3480 diag::note_odr_objc_missing_superclass);
3481 return 0;
3482 }
3483 }
3484
3485 // Import all of the members of this @implementation.
3486 ImportDeclContext(D);
3487
3488 return Impl;
3489}
3490
Douglas Gregora11c4582010-02-17 18:02:10 +00003491Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3492 // Import the major distinguishing characteristics of an @property.
3493 DeclContext *DC, *LexicalDC;
3494 DeclarationName Name;
3495 SourceLocation Loc;
3496 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3497 return 0;
3498
3499 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003500 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3501 DC->localUncachedLookup(Name, FoundDecls);
3502 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003503 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003504 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003505 // Check property types.
3506 if (!Importer.IsStructurallyEquivalent(D->getType(),
3507 FoundProp->getType())) {
3508 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3509 << Name << D->getType() << FoundProp->getType();
3510 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3511 << FoundProp->getType();
3512 return 0;
3513 }
3514
3515 // FIXME: Check property attributes, getters, setters, etc.?
3516
3517 // Consider these properties to be equivalent.
3518 Importer.Imported(D, FoundProp);
3519 return FoundProp;
3520 }
3521 }
3522
3523 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003524 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3525 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003526 return 0;
3527
3528 // Create the new property.
3529 ObjCPropertyDecl *ToProperty
3530 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3531 Name.getAsIdentifierInfo(),
3532 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003533 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003534 T,
3535 D->getPropertyImplementation());
3536 Importer.Imported(D, ToProperty);
3537 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003538 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003539
3540 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003541 ToProperty->setPropertyAttributesAsWritten(
3542 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003543 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3544 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3545 ToProperty->setGetterMethodDecl(
3546 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3547 ToProperty->setSetterMethodDecl(
3548 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3549 ToProperty->setPropertyIvarDecl(
3550 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3551 return ToProperty;
3552}
3553
Douglas Gregor14a49e22010-12-07 18:32:03 +00003554Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3555 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3556 Importer.Import(D->getPropertyDecl()));
3557 if (!Property)
3558 return 0;
3559
3560 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3561 if (!DC)
3562 return 0;
3563
3564 // Import the lexical declaration context.
3565 DeclContext *LexicalDC = DC;
3566 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3567 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3568 if (!LexicalDC)
3569 return 0;
3570 }
3571
3572 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3573 if (!InImpl)
3574 return 0;
3575
3576 // Import the ivar (for an @synthesize).
3577 ObjCIvarDecl *Ivar = 0;
3578 if (D->getPropertyIvarDecl()) {
3579 Ivar = cast_or_null<ObjCIvarDecl>(
3580 Importer.Import(D->getPropertyIvarDecl()));
3581 if (!Ivar)
3582 return 0;
3583 }
3584
3585 ObjCPropertyImplDecl *ToImpl
3586 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3587 if (!ToImpl) {
3588 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3589 Importer.Import(D->getLocStart()),
3590 Importer.Import(D->getLocation()),
3591 Property,
3592 D->getPropertyImplementation(),
3593 Ivar,
3594 Importer.Import(D->getPropertyIvarDeclLoc()));
3595 ToImpl->setLexicalDeclContext(LexicalDC);
3596 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003597 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003598 } else {
3599 // Check that we have the same kind of property implementation (@synthesize
3600 // vs. @dynamic).
3601 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3602 Importer.ToDiag(ToImpl->getLocation(),
3603 diag::err_odr_objc_property_impl_kind_inconsistent)
3604 << Property->getDeclName()
3605 << (ToImpl->getPropertyImplementation()
3606 == ObjCPropertyImplDecl::Dynamic);
3607 Importer.FromDiag(D->getLocation(),
3608 diag::note_odr_objc_property_impl_kind)
3609 << D->getPropertyDecl()->getDeclName()
3610 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3611 return 0;
3612 }
3613
3614 // For @synthesize, check that we have the same
3615 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3616 Ivar != ToImpl->getPropertyIvarDecl()) {
3617 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3618 diag::err_odr_objc_synthesize_ivar_inconsistent)
3619 << Property->getDeclName()
3620 << ToImpl->getPropertyIvarDecl()->getDeclName()
3621 << Ivar->getDeclName();
3622 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3623 diag::note_odr_objc_synthesize_ivar_here)
3624 << D->getPropertyIvarDecl()->getDeclName();
3625 return 0;
3626 }
3627
3628 // Merge the existing implementation with the new implementation.
3629 Importer.Imported(D, ToImpl);
3630 }
3631
3632 return ToImpl;
3633}
3634
Douglas Gregora082a492010-11-30 19:14:50 +00003635Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3636 // For template arguments, we adopt the translation unit as our declaration
3637 // context. This context will be fixed when the actual template declaration
3638 // is created.
3639
3640 // FIXME: Import default argument.
3641 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3642 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003643 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003644 Importer.Import(D->getLocation()),
3645 D->getDepth(),
3646 D->getIndex(),
3647 Importer.Import(D->getIdentifier()),
3648 D->wasDeclaredWithTypename(),
3649 D->isParameterPack());
3650}
3651
3652Decl *
3653ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3654 // Import the name of this declaration.
3655 DeclarationName Name = Importer.Import(D->getDeclName());
3656 if (D->getDeclName() && !Name)
3657 return 0;
3658
3659 // Import the location of this declaration.
3660 SourceLocation Loc = Importer.Import(D->getLocation());
3661
3662 // Import the type of this declaration.
3663 QualType T = Importer.Import(D->getType());
3664 if (T.isNull())
3665 return 0;
3666
3667 // Import type-source information.
3668 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3669 if (D->getTypeSourceInfo() && !TInfo)
3670 return 0;
3671
3672 // FIXME: Import default argument.
3673
3674 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3675 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003676 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003677 Loc, D->getDepth(), D->getPosition(),
3678 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003679 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003680}
3681
3682Decl *
3683ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3684 // Import the name of this declaration.
3685 DeclarationName Name = Importer.Import(D->getDeclName());
3686 if (D->getDeclName() && !Name)
3687 return 0;
3688
3689 // Import the location of this declaration.
3690 SourceLocation Loc = Importer.Import(D->getLocation());
3691
3692 // Import template parameters.
3693 TemplateParameterList *TemplateParams
3694 = ImportTemplateParameterList(D->getTemplateParameters());
3695 if (!TemplateParams)
3696 return 0;
3697
3698 // FIXME: Import default argument.
3699
3700 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3701 Importer.getToContext().getTranslationUnitDecl(),
3702 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003703 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003704 Name.getAsIdentifierInfo(),
3705 TemplateParams);
3706}
3707
3708Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3709 // If this record has a definition in the translation unit we're coming from,
3710 // but this particular declaration is not that definition, import the
3711 // definition and map to that.
3712 CXXRecordDecl *Definition
3713 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3714 if (Definition && Definition != D->getTemplatedDecl()) {
3715 Decl *ImportedDef
3716 = Importer.Import(Definition->getDescribedClassTemplate());
3717 if (!ImportedDef)
3718 return 0;
3719
3720 return Importer.Imported(D, ImportedDef);
3721 }
3722
3723 // Import the major distinguishing characteristics of this class template.
3724 DeclContext *DC, *LexicalDC;
3725 DeclarationName Name;
3726 SourceLocation Loc;
3727 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3728 return 0;
3729
3730 // We may already have a template of the same name; try to find and match it.
3731 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003732 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003733 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3734 DC->localUncachedLookup(Name, FoundDecls);
3735 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3736 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003737 continue;
3738
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003739 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003740 if (ClassTemplateDecl *FoundTemplate
3741 = dyn_cast<ClassTemplateDecl>(Found)) {
3742 if (IsStructuralMatch(D, FoundTemplate)) {
3743 // The class templates structurally match; call it the same template.
3744 // FIXME: We may be filling in a forward declaration here. Handle
3745 // this case!
3746 Importer.Imported(D->getTemplatedDecl(),
3747 FoundTemplate->getTemplatedDecl());
3748 return Importer.Imported(D, FoundTemplate);
3749 }
3750 }
3751
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003752 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003753 }
3754
3755 if (!ConflictingDecls.empty()) {
3756 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3757 ConflictingDecls.data(),
3758 ConflictingDecls.size());
3759 }
3760
3761 if (!Name)
3762 return 0;
3763 }
3764
3765 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3766
3767 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003768 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3769 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003770 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3771 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003772 DC, StartLoc, IdLoc,
3773 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003774 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003775 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003776 D2Templated->setLexicalDeclContext(LexicalDC);
3777
3778 // Create the class template declaration itself.
3779 TemplateParameterList *TemplateParams
3780 = ImportTemplateParameterList(D->getTemplateParameters());
3781 if (!TemplateParams)
3782 return 0;
3783
3784 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3785 Loc, Name, TemplateParams,
3786 D2Templated,
3787 /*PrevDecl=*/0);
3788 D2Templated->setDescribedClassTemplate(D2);
3789
3790 D2->setAccess(D->getAccess());
3791 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003792 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003793
3794 // Note the relationship between the class templates.
3795 Importer.Imported(D, D2);
3796 Importer.Imported(DTemplated, D2Templated);
3797
John McCallf937c022011-10-07 06:10:15 +00003798 if (DTemplated->isCompleteDefinition() &&
3799 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003800 // FIXME: Import definition!
3801 }
3802
3803 return D2;
3804}
3805
Douglas Gregore2e50d332010-12-01 01:36:18 +00003806Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3807 ClassTemplateSpecializationDecl *D) {
3808 // If this record has a definition in the translation unit we're coming from,
3809 // but this particular declaration is not that definition, import the
3810 // definition and map to that.
3811 TagDecl *Definition = D->getDefinition();
3812 if (Definition && Definition != D) {
3813 Decl *ImportedDef = Importer.Import(Definition);
3814 if (!ImportedDef)
3815 return 0;
3816
3817 return Importer.Imported(D, ImportedDef);
3818 }
3819
3820 ClassTemplateDecl *ClassTemplate
3821 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3822 D->getSpecializedTemplate()));
3823 if (!ClassTemplate)
3824 return 0;
3825
3826 // Import the context of this declaration.
3827 DeclContext *DC = ClassTemplate->getDeclContext();
3828 if (!DC)
3829 return 0;
3830
3831 DeclContext *LexicalDC = DC;
3832 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3833 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3834 if (!LexicalDC)
3835 return 0;
3836 }
3837
3838 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003839 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3840 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003841
3842 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003843 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003844 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3845 D->getTemplateArgs().size(),
3846 TemplateArgs))
3847 return 0;
3848
3849 // Try to find an existing specialization with these template arguments.
3850 void *InsertPos = 0;
3851 ClassTemplateSpecializationDecl *D2
3852 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3853 TemplateArgs.size(), InsertPos);
3854 if (D2) {
3855 // We already have a class template specialization with these template
3856 // arguments.
3857
3858 // FIXME: Check for specialization vs. instantiation errors.
3859
3860 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003861 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003862 // The record types structurally match, or the "from" translation
3863 // unit only had a forward declaration anyway; call it the same
3864 // function.
3865 return Importer.Imported(D, FoundDef);
3866 }
3867 }
3868 } else {
3869 // Create a new specialization.
3870 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3871 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003872 StartLoc, IdLoc,
3873 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003874 TemplateArgs.data(),
3875 TemplateArgs.size(),
3876 /*PrevDecl=*/0);
3877 D2->setSpecializationKind(D->getSpecializationKind());
3878
3879 // Add this specialization to the class template.
3880 ClassTemplate->AddSpecialization(D2, InsertPos);
3881
3882 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003883 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003884
3885 // Add the specialization to this context.
3886 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003887 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003888 }
3889 Importer.Imported(D, D2);
3890
John McCallf937c022011-10-07 06:10:15 +00003891 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003892 return 0;
3893
3894 return D2;
3895}
3896
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003897//----------------------------------------------------------------------------
3898// Import Statements
3899//----------------------------------------------------------------------------
3900
3901Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3902 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3903 << S->getStmtClassName();
3904 return 0;
3905}
3906
3907//----------------------------------------------------------------------------
3908// Import Expressions
3909//----------------------------------------------------------------------------
3910Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3911 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3912 << E->getStmtClassName();
3913 return 0;
3914}
3915
Douglas Gregor52f820e2010-02-19 01:17:02 +00003916Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003917 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3918 if (!ToD)
3919 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003920
3921 NamedDecl *FoundD = 0;
3922 if (E->getDecl() != E->getFoundDecl()) {
3923 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3924 if (!FoundD)
3925 return 0;
3926 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003927
3928 QualType T = Importer.Import(E->getType());
3929 if (T.isNull())
3930 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003931
3932 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3933 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003934 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003935 ToD,
John McCall113bee02012-03-10 09:33:50 +00003936 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003937 Importer.Import(E->getLocation()),
3938 T, E->getValueKind(),
3939 FoundD,
3940 /*FIXME:TemplateArgs=*/0);
3941 if (E->hadMultipleCandidates())
3942 DRE->setHadMultipleCandidates(true);
3943 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003944}
3945
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003946Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3947 QualType T = Importer.Import(E->getType());
3948 if (T.isNull())
3949 return 0;
3950
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003951 return IntegerLiteral::Create(Importer.getToContext(),
3952 E->getValue(), T,
3953 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003954}
3955
Douglas Gregor623421d2010-02-18 02:21:22 +00003956Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3957 QualType T = Importer.Import(E->getType());
3958 if (T.isNull())
3959 return 0;
3960
Douglas Gregorfb65e592011-07-27 05:40:30 +00003961 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3962 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00003963 Importer.Import(E->getLocation()));
3964}
3965
Douglas Gregorc74247e2010-02-19 01:07:06 +00003966Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3967 Expr *SubExpr = Importer.Import(E->getSubExpr());
3968 if (!SubExpr)
3969 return 0;
3970
3971 return new (Importer.getToContext())
3972 ParenExpr(Importer.Import(E->getLParen()),
3973 Importer.Import(E->getRParen()),
3974 SubExpr);
3975}
3976
3977Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3978 QualType T = Importer.Import(E->getType());
3979 if (T.isNull())
3980 return 0;
3981
3982 Expr *SubExpr = Importer.Import(E->getSubExpr());
3983 if (!SubExpr)
3984 return 0;
3985
3986 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003987 T, E->getValueKind(),
3988 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003989 Importer.Import(E->getOperatorLoc()));
3990}
3991
Peter Collingbournee190dee2011-03-11 19:24:49 +00003992Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3993 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00003994 QualType ResultType = Importer.Import(E->getType());
3995
3996 if (E->isArgumentType()) {
3997 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3998 if (!TInfo)
3999 return 0;
4000
Peter Collingbournee190dee2011-03-11 19:24:49 +00004001 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4002 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004003 Importer.Import(E->getOperatorLoc()),
4004 Importer.Import(E->getRParenLoc()));
4005 }
4006
4007 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4008 if (!SubExpr)
4009 return 0;
4010
Peter Collingbournee190dee2011-03-11 19:24:49 +00004011 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4012 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004013 Importer.Import(E->getOperatorLoc()),
4014 Importer.Import(E->getRParenLoc()));
4015}
4016
Douglas Gregorc74247e2010-02-19 01:07:06 +00004017Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4018 QualType T = Importer.Import(E->getType());
4019 if (T.isNull())
4020 return 0;
4021
4022 Expr *LHS = Importer.Import(E->getLHS());
4023 if (!LHS)
4024 return 0;
4025
4026 Expr *RHS = Importer.Import(E->getRHS());
4027 if (!RHS)
4028 return 0;
4029
4030 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004031 T, E->getValueKind(),
4032 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004033 Importer.Import(E->getOperatorLoc()));
4034}
4035
4036Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4037 QualType T = Importer.Import(E->getType());
4038 if (T.isNull())
4039 return 0;
4040
4041 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4042 if (CompLHSType.isNull())
4043 return 0;
4044
4045 QualType CompResultType = Importer.Import(E->getComputationResultType());
4046 if (CompResultType.isNull())
4047 return 0;
4048
4049 Expr *LHS = Importer.Import(E->getLHS());
4050 if (!LHS)
4051 return 0;
4052
4053 Expr *RHS = Importer.Import(E->getRHS());
4054 if (!RHS)
4055 return 0;
4056
4057 return new (Importer.getToContext())
4058 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004059 T, E->getValueKind(),
4060 E->getObjectKind(),
4061 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004062 Importer.Import(E->getOperatorLoc()));
4063}
4064
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004065static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004066 if (E->path_empty()) return false;
4067
4068 // TODO: import cast paths
4069 return true;
4070}
4071
Douglas Gregor98c10182010-02-12 22:17:39 +00004072Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4073 QualType T = Importer.Import(E->getType());
4074 if (T.isNull())
4075 return 0;
4076
4077 Expr *SubExpr = Importer.Import(E->getSubExpr());
4078 if (!SubExpr)
4079 return 0;
John McCallcf142162010-08-07 06:22:56 +00004080
4081 CXXCastPath BasePath;
4082 if (ImportCastPath(E, BasePath))
4083 return 0;
4084
4085 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004086 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004087}
4088
Douglas Gregor5481d322010-02-19 01:32:14 +00004089Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4090 QualType T = Importer.Import(E->getType());
4091 if (T.isNull())
4092 return 0;
4093
4094 Expr *SubExpr = Importer.Import(E->getSubExpr());
4095 if (!SubExpr)
4096 return 0;
4097
4098 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4099 if (!TInfo && E->getTypeInfoAsWritten())
4100 return 0;
4101
John McCallcf142162010-08-07 06:22:56 +00004102 CXXCastPath BasePath;
4103 if (ImportCastPath(E, BasePath))
4104 return 0;
4105
John McCall7decc9e2010-11-18 06:31:45 +00004106 return CStyleCastExpr::Create(Importer.getToContext(), T,
4107 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004108 SubExpr, &BasePath, TInfo,
4109 Importer.Import(E->getLParenLoc()),
4110 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004111}
4112
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004113ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004114 ASTContext &FromContext, FileManager &FromFileManager,
4115 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004116 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004117 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4118 Minimal(MinimalImport)
4119{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004120 ImportedDecls[FromContext.getTranslationUnitDecl()]
4121 = ToContext.getTranslationUnitDecl();
4122}
4123
4124ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004125
4126QualType ASTImporter::Import(QualType FromT) {
4127 if (FromT.isNull())
4128 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004129
4130 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004131
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004132 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004133 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4134 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004135 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004136 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004137
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004138 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004139 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004140 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004141 if (ToT.isNull())
4142 return ToT;
4143
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004144 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004145 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004146
John McCall424cec92011-01-19 06:33:43 +00004147 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004148}
4149
Douglas Gregor62d311f2010-02-09 19:21:46 +00004150TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004151 if (!FromTSI)
4152 return FromTSI;
4153
4154 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004155 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004156 QualType T = Import(FromTSI->getType());
4157 if (T.isNull())
4158 return 0;
4159
4160 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004161 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004162}
4163
4164Decl *ASTImporter::Import(Decl *FromD) {
4165 if (!FromD)
4166 return 0;
4167
Douglas Gregord451ea92011-07-29 23:31:30 +00004168 ASTNodeImporter Importer(*this);
4169
Douglas Gregor62d311f2010-02-09 19:21:46 +00004170 // Check whether we've already imported this declaration.
4171 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004172 if (Pos != ImportedDecls.end()) {
4173 Decl *ToD = Pos->second;
4174 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4175 return ToD;
4176 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004177
4178 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004179 Decl *ToD = Importer.Visit(FromD);
4180 if (!ToD)
4181 return 0;
4182
4183 // Record the imported declaration.
4184 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004185
4186 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4187 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004188 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004189 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004190 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004191 // When we've finished transforming a typedef, see whether it was the
4192 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004193 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004194 FromTag = AnonTagsWithPendingTypedefs.begin(),
4195 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4196 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004197 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004198 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4199 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004200 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004201 AnonTagsWithPendingTypedefs.erase(FromTag);
4202 break;
4203 }
4204 }
4205 }
4206 }
4207
Douglas Gregor62d311f2010-02-09 19:21:46 +00004208 return ToD;
4209}
4210
4211DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4212 if (!FromDC)
4213 return FromDC;
4214
Douglas Gregor95d82832012-01-24 18:36:04 +00004215 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004216 if (!ToDC)
4217 return 0;
4218
4219 // When we're using a record/enum/Objective-C class/protocol as a context, we
4220 // need it to have a definition.
4221 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004222 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004223 if (ToRecord->isCompleteDefinition()) {
4224 // Do nothing.
4225 } else if (FromRecord->isCompleteDefinition()) {
4226 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4227 ASTNodeImporter::IDK_Basic);
4228 } else {
4229 CompleteDecl(ToRecord);
4230 }
4231 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4232 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4233 if (ToEnum->isCompleteDefinition()) {
4234 // Do nothing.
4235 } else if (FromEnum->isCompleteDefinition()) {
4236 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4237 ASTNodeImporter::IDK_Basic);
4238 } else {
4239 CompleteDecl(ToEnum);
4240 }
4241 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4242 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4243 if (ToClass->getDefinition()) {
4244 // Do nothing.
4245 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4246 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4247 ASTNodeImporter::IDK_Basic);
4248 } else {
4249 CompleteDecl(ToClass);
4250 }
4251 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4252 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4253 if (ToProto->getDefinition()) {
4254 // Do nothing.
4255 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4256 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4257 ASTNodeImporter::IDK_Basic);
4258 } else {
4259 CompleteDecl(ToProto);
4260 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004261 }
4262
4263 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004264}
4265
4266Expr *ASTImporter::Import(Expr *FromE) {
4267 if (!FromE)
4268 return 0;
4269
4270 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4271}
4272
4273Stmt *ASTImporter::Import(Stmt *FromS) {
4274 if (!FromS)
4275 return 0;
4276
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004277 // Check whether we've already imported this declaration.
4278 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4279 if (Pos != ImportedStmts.end())
4280 return Pos->second;
4281
4282 // Import the type
4283 ASTNodeImporter Importer(*this);
4284 Stmt *ToS = Importer.Visit(FromS);
4285 if (!ToS)
4286 return 0;
4287
4288 // Record the imported declaration.
4289 ImportedStmts[FromS] = ToS;
4290 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004291}
4292
4293NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4294 if (!FromNNS)
4295 return 0;
4296
Douglas Gregor90ebf252011-04-27 16:48:40 +00004297 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4298
4299 switch (FromNNS->getKind()) {
4300 case NestedNameSpecifier::Identifier:
4301 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4302 return NestedNameSpecifier::Create(ToContext, prefix, II);
4303 }
4304 return 0;
4305
4306 case NestedNameSpecifier::Namespace:
4307 if (NamespaceDecl *NS =
4308 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4309 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4310 }
4311 return 0;
4312
4313 case NestedNameSpecifier::NamespaceAlias:
4314 if (NamespaceAliasDecl *NSAD =
4315 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4316 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4317 }
4318 return 0;
4319
4320 case NestedNameSpecifier::Global:
4321 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4322
4323 case NestedNameSpecifier::TypeSpec:
4324 case NestedNameSpecifier::TypeSpecWithTemplate: {
4325 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4326 if (!T.isNull()) {
4327 bool bTemplate = FromNNS->getKind() ==
4328 NestedNameSpecifier::TypeSpecWithTemplate;
4329 return NestedNameSpecifier::Create(ToContext, prefix,
4330 bTemplate, T.getTypePtr());
4331 }
4332 }
4333 return 0;
4334 }
4335
4336 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004337}
4338
Douglas Gregor14454802011-02-25 02:25:35 +00004339NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4340 // FIXME: Implement!
4341 return NestedNameSpecifierLoc();
4342}
4343
Douglas Gregore2e50d332010-12-01 01:36:18 +00004344TemplateName ASTImporter::Import(TemplateName From) {
4345 switch (From.getKind()) {
4346 case TemplateName::Template:
4347 if (TemplateDecl *ToTemplate
4348 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4349 return TemplateName(ToTemplate);
4350
4351 return TemplateName();
4352
4353 case TemplateName::OverloadedTemplate: {
4354 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4355 UnresolvedSet<2> ToTemplates;
4356 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4357 E = FromStorage->end();
4358 I != E; ++I) {
4359 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4360 ToTemplates.addDecl(To);
4361 else
4362 return TemplateName();
4363 }
4364 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4365 ToTemplates.end());
4366 }
4367
4368 case TemplateName::QualifiedTemplate: {
4369 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4370 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4371 if (!Qualifier)
4372 return TemplateName();
4373
4374 if (TemplateDecl *ToTemplate
4375 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4376 return ToContext.getQualifiedTemplateName(Qualifier,
4377 QTN->hasTemplateKeyword(),
4378 ToTemplate);
4379
4380 return TemplateName();
4381 }
4382
4383 case TemplateName::DependentTemplate: {
4384 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4385 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4386 if (!Qualifier)
4387 return TemplateName();
4388
4389 if (DTN->isIdentifier()) {
4390 return ToContext.getDependentTemplateName(Qualifier,
4391 Import(DTN->getIdentifier()));
4392 }
4393
4394 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4395 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004396
4397 case TemplateName::SubstTemplateTemplateParm: {
4398 SubstTemplateTemplateParmStorage *subst
4399 = From.getAsSubstTemplateTemplateParm();
4400 TemplateTemplateParmDecl *param
4401 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4402 if (!param)
4403 return TemplateName();
4404
4405 TemplateName replacement = Import(subst->getReplacement());
4406 if (replacement.isNull()) return TemplateName();
4407
4408 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4409 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004410
4411 case TemplateName::SubstTemplateTemplateParmPack: {
4412 SubstTemplateTemplateParmPackStorage *SubstPack
4413 = From.getAsSubstTemplateTemplateParmPack();
4414 TemplateTemplateParmDecl *Param
4415 = cast_or_null<TemplateTemplateParmDecl>(
4416 Import(SubstPack->getParameterPack()));
4417 if (!Param)
4418 return TemplateName();
4419
4420 ASTNodeImporter Importer(*this);
4421 TemplateArgument ArgPack
4422 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4423 if (ArgPack.isNull())
4424 return TemplateName();
4425
4426 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4427 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004428 }
4429
4430 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004431}
4432
Douglas Gregor62d311f2010-02-09 19:21:46 +00004433SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4434 if (FromLoc.isInvalid())
4435 return SourceLocation();
4436
Douglas Gregor811663e2010-02-10 00:15:17 +00004437 SourceManager &FromSM = FromContext.getSourceManager();
4438
4439 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004440 // don't have to import macro expansions.
4441 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004442 FromLoc = FromSM.getSpellingLoc(FromLoc);
4443 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4444 SourceManager &ToSM = ToContext.getSourceManager();
4445 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004446 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004447}
4448
4449SourceRange ASTImporter::Import(SourceRange FromRange) {
4450 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4451}
4452
Douglas Gregor811663e2010-02-10 00:15:17 +00004453FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004454 llvm::DenseMap<FileID, FileID>::iterator Pos
4455 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004456 if (Pos != ImportedFileIDs.end())
4457 return Pos->second;
4458
4459 SourceManager &FromSM = FromContext.getSourceManager();
4460 SourceManager &ToSM = ToContext.getSourceManager();
4461 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004462 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004463
4464 // Include location of this file.
4465 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4466
4467 // Map the FileID for to the "to" source manager.
4468 FileID ToID;
4469 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004470 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004471 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4472 // disk again
4473 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4474 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004475 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004476 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4477 FromSLoc.getFile().getFileCharacteristic());
4478 } else {
4479 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004480 const llvm::MemoryBuffer *
4481 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004482 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004483 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004484 FromBuf->getBufferIdentifier());
4485 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4486 }
4487
4488
Sebastian Redl99219f12010-09-30 01:03:06 +00004489 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004490 return ToID;
4491}
4492
Douglas Gregor0a791672011-01-18 03:11:38 +00004493void ASTImporter::ImportDefinition(Decl *From) {
4494 Decl *To = Import(From);
4495 if (!To)
4496 return;
4497
4498 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4499 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004500
4501 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4502 if (!ToRecord->getDefinition()) {
4503 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004504 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004505 return;
4506 }
4507 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004508
4509 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4510 if (!ToEnum->getDefinition()) {
4511 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004512 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004513 return;
4514 }
4515 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004516
4517 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4518 if (!ToIFace->getDefinition()) {
4519 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004520 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004521 return;
4522 }
4523 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004524
Douglas Gregor2aa53772012-01-24 17:42:07 +00004525 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4526 if (!ToProto->getDefinition()) {
4527 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004528 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004529 return;
4530 }
4531 }
4532
Douglas Gregor0a791672011-01-18 03:11:38 +00004533 Importer.ImportDeclContext(FromDC, true);
4534 }
4535}
4536
Douglas Gregor96e578d2010-02-05 17:54:41 +00004537DeclarationName ASTImporter::Import(DeclarationName FromName) {
4538 if (!FromName)
4539 return DeclarationName();
4540
4541 switch (FromName.getNameKind()) {
4542 case DeclarationName::Identifier:
4543 return Import(FromName.getAsIdentifierInfo());
4544
4545 case DeclarationName::ObjCZeroArgSelector:
4546 case DeclarationName::ObjCOneArgSelector:
4547 case DeclarationName::ObjCMultiArgSelector:
4548 return Import(FromName.getObjCSelector());
4549
4550 case DeclarationName::CXXConstructorName: {
4551 QualType T = Import(FromName.getCXXNameType());
4552 if (T.isNull())
4553 return DeclarationName();
4554
4555 return ToContext.DeclarationNames.getCXXConstructorName(
4556 ToContext.getCanonicalType(T));
4557 }
4558
4559 case DeclarationName::CXXDestructorName: {
4560 QualType T = Import(FromName.getCXXNameType());
4561 if (T.isNull())
4562 return DeclarationName();
4563
4564 return ToContext.DeclarationNames.getCXXDestructorName(
4565 ToContext.getCanonicalType(T));
4566 }
4567
4568 case DeclarationName::CXXConversionFunctionName: {
4569 QualType T = Import(FromName.getCXXNameType());
4570 if (T.isNull())
4571 return DeclarationName();
4572
4573 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4574 ToContext.getCanonicalType(T));
4575 }
4576
4577 case DeclarationName::CXXOperatorName:
4578 return ToContext.DeclarationNames.getCXXOperatorName(
4579 FromName.getCXXOverloadedOperator());
4580
4581 case DeclarationName::CXXLiteralOperatorName:
4582 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4583 Import(FromName.getCXXLiteralIdentifier()));
4584
4585 case DeclarationName::CXXUsingDirective:
4586 // FIXME: STATICS!
4587 return DeclarationName::getUsingDirectiveName();
4588 }
4589
David Blaikiee4d798f2012-01-20 21:50:17 +00004590 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004591}
4592
Douglas Gregore2e50d332010-12-01 01:36:18 +00004593IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004594 if (!FromId)
4595 return 0;
4596
4597 return &ToContext.Idents.get(FromId->getName());
4598}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004599
Douglas Gregor43f54792010-02-17 02:12:47 +00004600Selector ASTImporter::Import(Selector FromSel) {
4601 if (FromSel.isNull())
4602 return Selector();
4603
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004604 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004605 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4606 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4607 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4608 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4609}
4610
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004611DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4612 DeclContext *DC,
4613 unsigned IDNS,
4614 NamedDecl **Decls,
4615 unsigned NumDecls) {
4616 return Name;
4617}
4618
4619DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004620 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004621}
4622
4623DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004624 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004625}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004626
Douglas Gregor2e15c842012-02-01 21:00:38 +00004627void ASTImporter::CompleteDecl (Decl *D) {
4628 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4629 if (!ID->getDefinition())
4630 ID->startDefinition();
4631 }
4632 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4633 if (!PD->getDefinition())
4634 PD->startDefinition();
4635 }
4636 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4637 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4638 TD->startDefinition();
4639 TD->setCompleteDefinition(true);
4640 }
4641 }
4642 else {
4643 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4644 }
4645}
4646
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004647Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4648 ImportedDecls[From] = To;
4649 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004650}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004651
Douglas Gregordd6006f2012-07-17 21:16:27 +00004652bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4653 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004654 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004655 = ImportedTypes.find(From.getTypePtr());
4656 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4657 return true;
4658
Douglas Gregordd6006f2012-07-17 21:16:27 +00004659 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4660 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004661 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004662}