blob: 3403b6276c984bdfb3757caa7aa0f092e102dc1c [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 Gregor5c73e912010-02-11 00:48:18 +0000122 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +0000123 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +0000124 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000125 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000126 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000127 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000128 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000129 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000130 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000131 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000132 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000133 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000134 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000135 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
136 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
137 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
138 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000139 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000140 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000141 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000142 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000143 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000144 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000145 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000146 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000147 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000148 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000149 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000150 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000151 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000152 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000153 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
154 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
155 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
156 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000157 Decl *VisitClassTemplateSpecializationDecl(
158 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000159
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000160 // Importing statements
161 Stmt *VisitStmt(Stmt *S);
162
163 // Importing expressions
164 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000165 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000166 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000167 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000168 Expr *VisitParenExpr(ParenExpr *E);
169 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000170 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000171 Expr *VisitBinaryOperator(BinaryOperator *E);
172 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000173 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000174 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000175 };
176}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000177using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000178
179//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000180// Structural Equivalence
181//----------------------------------------------------------------------------
182
183namespace {
184 struct StructuralEquivalenceContext {
185 /// \brief AST contexts for which we are checking structural equivalence.
186 ASTContext &C1, &C2;
187
Douglas Gregor3996e242010-02-15 22:01:00 +0000188 /// \brief The set of "tentative" equivalences between two canonical
189 /// declarations, mapping from a declaration in the first context to the
190 /// declaration in the second context that we believe to be equivalent.
191 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
192
193 /// \brief Queue of declarations in the first context whose equivalence
194 /// with a declaration in the second context still needs to be verified.
195 std::deque<Decl *> DeclsToCheck;
196
Douglas Gregorb4964f72010-02-15 23:54:17 +0000197 /// \brief Declaration (from, to) pairs that are known not to be equivalent
198 /// (which we have already complained about).
199 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
200
Douglas Gregor3996e242010-02-15 22:01:00 +0000201 /// \brief Whether we're being strict about the spelling of types when
202 /// unifying two types.
203 bool StrictTypeSpelling;
204
205 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000206 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000207 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000208 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000209 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000210
211 /// \brief Determine whether the two declarations are structurally
212 /// equivalent.
213 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
214
215 /// \brief Determine whether the two types are structurally equivalent.
216 bool IsStructurallyEquivalent(QualType T1, QualType T2);
217
218 private:
219 /// \brief Finish checking all of the structural equivalences.
220 ///
221 /// \returns true if an error occurred, false otherwise.
222 bool Finish();
223
224 public:
225 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000226 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000227 }
228
229 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000230 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000231 }
232 };
233}
234
235static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
236 QualType T1, QualType T2);
237static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
238 Decl *D1, Decl *D2);
239
Douglas Gregor3996e242010-02-15 22:01:00 +0000240/// \brief Determine structural equivalence of two expressions.
241static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
242 Expr *E1, Expr *E2) {
243 if (!E1 || !E2)
244 return E1 == E2;
245
246 // FIXME: Actually perform a structural comparison!
247 return true;
248}
249
250/// \brief Determine whether two identifiers are equivalent.
251static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
252 const IdentifierInfo *Name2) {
253 if (!Name1 || !Name2)
254 return Name1 == Name2;
255
256 return Name1->getName() == Name2->getName();
257}
258
259/// \brief Determine whether two nested-name-specifiers are equivalent.
260static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
261 NestedNameSpecifier *NNS1,
262 NestedNameSpecifier *NNS2) {
263 // FIXME: Implement!
264 return true;
265}
266
267/// \brief Determine whether two template arguments are equivalent.
268static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
269 const TemplateArgument &Arg1,
270 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000271 if (Arg1.getKind() != Arg2.getKind())
272 return false;
273
274 switch (Arg1.getKind()) {
275 case TemplateArgument::Null:
276 return true;
277
278 case TemplateArgument::Type:
279 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
280
281 case TemplateArgument::Integral:
282 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
283 Arg2.getIntegralType()))
284 return false;
285
Eric Christopher6dcc3762012-07-15 00:23:57 +0000286 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000287
288 case TemplateArgument::Declaration:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000289 if (!Arg1.getAsDecl() || !Arg2.getAsDecl())
290 return !Arg1.getAsDecl() && !Arg2.getAsDecl();
Douglas Gregore2e50d332010-12-01 01:36:18 +0000291 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
292
293 case TemplateArgument::Template:
294 return IsStructurallyEquivalent(Context,
295 Arg1.getAsTemplate(),
296 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000297
298 case TemplateArgument::TemplateExpansion:
299 return IsStructurallyEquivalent(Context,
300 Arg1.getAsTemplateOrTemplatePattern(),
301 Arg2.getAsTemplateOrTemplatePattern());
302
Douglas Gregore2e50d332010-12-01 01:36:18 +0000303 case TemplateArgument::Expression:
304 return IsStructurallyEquivalent(Context,
305 Arg1.getAsExpr(), Arg2.getAsExpr());
306
307 case TemplateArgument::Pack:
308 if (Arg1.pack_size() != Arg2.pack_size())
309 return false;
310
311 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
312 if (!IsStructurallyEquivalent(Context,
313 Arg1.pack_begin()[I],
314 Arg2.pack_begin()[I]))
315 return false;
316
317 return true;
318 }
319
320 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000321}
322
323/// \brief Determine structural equivalence for the common part of array
324/// types.
325static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
326 const ArrayType *Array1,
327 const ArrayType *Array2) {
328 if (!IsStructurallyEquivalent(Context,
329 Array1->getElementType(),
330 Array2->getElementType()))
331 return false;
332 if (Array1->getSizeModifier() != Array2->getSizeModifier())
333 return false;
334 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
335 return false;
336
337 return true;
338}
339
340/// \brief Determine structural equivalence of two types.
341static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
342 QualType T1, QualType T2) {
343 if (T1.isNull() || T2.isNull())
344 return T1.isNull() && T2.isNull();
345
346 if (!Context.StrictTypeSpelling) {
347 // We aren't being strict about token-to-token equivalence of types,
348 // so map down to the canonical type.
349 T1 = Context.C1.getCanonicalType(T1);
350 T2 = Context.C2.getCanonicalType(T2);
351 }
352
353 if (T1.getQualifiers() != T2.getQualifiers())
354 return false;
355
Douglas Gregorb4964f72010-02-15 23:54:17 +0000356 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000357
Douglas Gregorb4964f72010-02-15 23:54:17 +0000358 if (T1->getTypeClass() != T2->getTypeClass()) {
359 // Compare function types with prototypes vs. without prototypes as if
360 // both did not have prototypes.
361 if (T1->getTypeClass() == Type::FunctionProto &&
362 T2->getTypeClass() == Type::FunctionNoProto)
363 TC = Type::FunctionNoProto;
364 else if (T1->getTypeClass() == Type::FunctionNoProto &&
365 T2->getTypeClass() == Type::FunctionProto)
366 TC = Type::FunctionNoProto;
367 else
368 return false;
369 }
370
371 switch (TC) {
372 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000373 // FIXME: Deal with Char_S/Char_U.
374 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
375 return false;
376 break;
377
378 case Type::Complex:
379 if (!IsStructurallyEquivalent(Context,
380 cast<ComplexType>(T1)->getElementType(),
381 cast<ComplexType>(T2)->getElementType()))
382 return false;
383 break;
384
385 case Type::Pointer:
386 if (!IsStructurallyEquivalent(Context,
387 cast<PointerType>(T1)->getPointeeType(),
388 cast<PointerType>(T2)->getPointeeType()))
389 return false;
390 break;
391
392 case Type::BlockPointer:
393 if (!IsStructurallyEquivalent(Context,
394 cast<BlockPointerType>(T1)->getPointeeType(),
395 cast<BlockPointerType>(T2)->getPointeeType()))
396 return false;
397 break;
398
399 case Type::LValueReference:
400 case Type::RValueReference: {
401 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
402 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
403 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
404 return false;
405 if (Ref1->isInnerRef() != Ref2->isInnerRef())
406 return false;
407 if (!IsStructurallyEquivalent(Context,
408 Ref1->getPointeeTypeAsWritten(),
409 Ref2->getPointeeTypeAsWritten()))
410 return false;
411 break;
412 }
413
414 case Type::MemberPointer: {
415 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
416 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
417 if (!IsStructurallyEquivalent(Context,
418 MemPtr1->getPointeeType(),
419 MemPtr2->getPointeeType()))
420 return false;
421 if (!IsStructurallyEquivalent(Context,
422 QualType(MemPtr1->getClass(), 0),
423 QualType(MemPtr2->getClass(), 0)))
424 return false;
425 break;
426 }
427
428 case Type::ConstantArray: {
429 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
430 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000431 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000432 return false;
433
434 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
435 return false;
436 break;
437 }
438
439 case Type::IncompleteArray:
440 if (!IsArrayStructurallyEquivalent(Context,
441 cast<ArrayType>(T1),
442 cast<ArrayType>(T2)))
443 return false;
444 break;
445
446 case Type::VariableArray: {
447 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
448 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
449 if (!IsStructurallyEquivalent(Context,
450 Array1->getSizeExpr(), Array2->getSizeExpr()))
451 return false;
452
453 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
454 return false;
455
456 break;
457 }
458
459 case Type::DependentSizedArray: {
460 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
461 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
462 if (!IsStructurallyEquivalent(Context,
463 Array1->getSizeExpr(), Array2->getSizeExpr()))
464 return false;
465
466 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
467 return false;
468
469 break;
470 }
471
472 case Type::DependentSizedExtVector: {
473 const DependentSizedExtVectorType *Vec1
474 = cast<DependentSizedExtVectorType>(T1);
475 const DependentSizedExtVectorType *Vec2
476 = cast<DependentSizedExtVectorType>(T2);
477 if (!IsStructurallyEquivalent(Context,
478 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
479 return false;
480 if (!IsStructurallyEquivalent(Context,
481 Vec1->getElementType(),
482 Vec2->getElementType()))
483 return false;
484 break;
485 }
486
487 case Type::Vector:
488 case Type::ExtVector: {
489 const VectorType *Vec1 = cast<VectorType>(T1);
490 const VectorType *Vec2 = cast<VectorType>(T2);
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 if (Vec1->getNumElements() != Vec2->getNumElements())
496 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000497 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000498 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000499 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000500 }
501
502 case Type::FunctionProto: {
503 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
504 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
505 if (Proto1->getNumArgs() != Proto2->getNumArgs())
506 return false;
507 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
508 if (!IsStructurallyEquivalent(Context,
509 Proto1->getArgType(I),
510 Proto2->getArgType(I)))
511 return false;
512 }
513 if (Proto1->isVariadic() != Proto2->isVariadic())
514 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000515 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000516 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000517 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
518 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
519 return false;
520 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
521 if (!IsStructurallyEquivalent(Context,
522 Proto1->getExceptionType(I),
523 Proto2->getExceptionType(I)))
524 return false;
525 }
526 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000527 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000528 Proto1->getNoexceptExpr(),
529 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000530 return false;
531 }
532 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
533 return false;
534
535 // Fall through to check the bits common with FunctionNoProtoType.
536 }
537
538 case Type::FunctionNoProto: {
539 const FunctionType *Function1 = cast<FunctionType>(T1);
540 const FunctionType *Function2 = cast<FunctionType>(T2);
541 if (!IsStructurallyEquivalent(Context,
542 Function1->getResultType(),
543 Function2->getResultType()))
544 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000545 if (Function1->getExtInfo() != Function2->getExtInfo())
546 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000547 break;
548 }
549
550 case Type::UnresolvedUsing:
551 if (!IsStructurallyEquivalent(Context,
552 cast<UnresolvedUsingType>(T1)->getDecl(),
553 cast<UnresolvedUsingType>(T2)->getDecl()))
554 return false;
555
556 break;
John McCall81904512011-01-06 01:58:22 +0000557
558 case Type::Attributed:
559 if (!IsStructurallyEquivalent(Context,
560 cast<AttributedType>(T1)->getModifiedType(),
561 cast<AttributedType>(T2)->getModifiedType()))
562 return false;
563 if (!IsStructurallyEquivalent(Context,
564 cast<AttributedType>(T1)->getEquivalentType(),
565 cast<AttributedType>(T2)->getEquivalentType()))
566 return false;
567 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000568
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000569 case Type::Paren:
570 if (!IsStructurallyEquivalent(Context,
571 cast<ParenType>(T1)->getInnerType(),
572 cast<ParenType>(T2)->getInnerType()))
573 return false;
574 break;
575
Douglas Gregor3996e242010-02-15 22:01:00 +0000576 case Type::Typedef:
577 if (!IsStructurallyEquivalent(Context,
578 cast<TypedefType>(T1)->getDecl(),
579 cast<TypedefType>(T2)->getDecl()))
580 return false;
581 break;
582
583 case Type::TypeOfExpr:
584 if (!IsStructurallyEquivalent(Context,
585 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
586 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
587 return false;
588 break;
589
590 case Type::TypeOf:
591 if (!IsStructurallyEquivalent(Context,
592 cast<TypeOfType>(T1)->getUnderlyingType(),
593 cast<TypeOfType>(T2)->getUnderlyingType()))
594 return false;
595 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000596
597 case Type::UnaryTransform:
598 if (!IsStructurallyEquivalent(Context,
599 cast<UnaryTransformType>(T1)->getUnderlyingType(),
600 cast<UnaryTransformType>(T1)->getUnderlyingType()))
601 return false;
602 break;
603
Douglas Gregor3996e242010-02-15 22:01:00 +0000604 case Type::Decltype:
605 if (!IsStructurallyEquivalent(Context,
606 cast<DecltypeType>(T1)->getUnderlyingExpr(),
607 cast<DecltypeType>(T2)->getUnderlyingExpr()))
608 return false;
609 break;
610
Richard Smith30482bc2011-02-20 03:19:35 +0000611 case Type::Auto:
612 if (!IsStructurallyEquivalent(Context,
613 cast<AutoType>(T1)->getDeducedType(),
614 cast<AutoType>(T2)->getDeducedType()))
615 return false;
616 break;
617
Douglas Gregor3996e242010-02-15 22:01:00 +0000618 case Type::Record:
619 case Type::Enum:
620 if (!IsStructurallyEquivalent(Context,
621 cast<TagType>(T1)->getDecl(),
622 cast<TagType>(T2)->getDecl()))
623 return false;
624 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000625
Douglas Gregor3996e242010-02-15 22:01:00 +0000626 case Type::TemplateTypeParm: {
627 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
628 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
629 if (Parm1->getDepth() != Parm2->getDepth())
630 return false;
631 if (Parm1->getIndex() != Parm2->getIndex())
632 return false;
633 if (Parm1->isParameterPack() != Parm2->isParameterPack())
634 return false;
635
636 // Names of template type parameters are never significant.
637 break;
638 }
639
640 case Type::SubstTemplateTypeParm: {
641 const SubstTemplateTypeParmType *Subst1
642 = cast<SubstTemplateTypeParmType>(T1);
643 const SubstTemplateTypeParmType *Subst2
644 = cast<SubstTemplateTypeParmType>(T2);
645 if (!IsStructurallyEquivalent(Context,
646 QualType(Subst1->getReplacedParameter(), 0),
647 QualType(Subst2->getReplacedParameter(), 0)))
648 return false;
649 if (!IsStructurallyEquivalent(Context,
650 Subst1->getReplacementType(),
651 Subst2->getReplacementType()))
652 return false;
653 break;
654 }
655
Douglas Gregorfb322d82011-01-14 05:11:40 +0000656 case Type::SubstTemplateTypeParmPack: {
657 const SubstTemplateTypeParmPackType *Subst1
658 = cast<SubstTemplateTypeParmPackType>(T1);
659 const SubstTemplateTypeParmPackType *Subst2
660 = cast<SubstTemplateTypeParmPackType>(T2);
661 if (!IsStructurallyEquivalent(Context,
662 QualType(Subst1->getReplacedParameter(), 0),
663 QualType(Subst2->getReplacedParameter(), 0)))
664 return false;
665 if (!IsStructurallyEquivalent(Context,
666 Subst1->getArgumentPack(),
667 Subst2->getArgumentPack()))
668 return false;
669 break;
670 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000671 case Type::TemplateSpecialization: {
672 const TemplateSpecializationType *Spec1
673 = cast<TemplateSpecializationType>(T1);
674 const TemplateSpecializationType *Spec2
675 = cast<TemplateSpecializationType>(T2);
676 if (!IsStructurallyEquivalent(Context,
677 Spec1->getTemplateName(),
678 Spec2->getTemplateName()))
679 return false;
680 if (Spec1->getNumArgs() != Spec2->getNumArgs())
681 return false;
682 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
683 if (!IsStructurallyEquivalent(Context,
684 Spec1->getArg(I), Spec2->getArg(I)))
685 return false;
686 }
687 break;
688 }
689
Abramo Bagnara6150c882010-05-11 21:36:43 +0000690 case Type::Elaborated: {
691 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
692 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
693 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
694 if (Elab1->getKeyword() != Elab2->getKeyword())
695 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000696 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000697 Elab1->getQualifier(),
698 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000699 return false;
700 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000701 Elab1->getNamedType(),
702 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000703 return false;
704 break;
705 }
706
John McCalle78aac42010-03-10 03:28:59 +0000707 case Type::InjectedClassName: {
708 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
709 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
710 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000711 Inj1->getInjectedSpecializationType(),
712 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000713 return false;
714 break;
715 }
716
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000717 case Type::DependentName: {
718 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
719 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000720 if (!IsStructurallyEquivalent(Context,
721 Typename1->getQualifier(),
722 Typename2->getQualifier()))
723 return false;
724 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
725 Typename2->getIdentifier()))
726 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000727
728 break;
729 }
730
John McCallc392f372010-06-11 00:33:02 +0000731 case Type::DependentTemplateSpecialization: {
732 const DependentTemplateSpecializationType *Spec1 =
733 cast<DependentTemplateSpecializationType>(T1);
734 const DependentTemplateSpecializationType *Spec2 =
735 cast<DependentTemplateSpecializationType>(T2);
736 if (!IsStructurallyEquivalent(Context,
737 Spec1->getQualifier(),
738 Spec2->getQualifier()))
739 return false;
740 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
741 Spec2->getIdentifier()))
742 return false;
743 if (Spec1->getNumArgs() != Spec2->getNumArgs())
744 return false;
745 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
746 if (!IsStructurallyEquivalent(Context,
747 Spec1->getArg(I), Spec2->getArg(I)))
748 return false;
749 }
750 break;
751 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000752
753 case Type::PackExpansion:
754 if (!IsStructurallyEquivalent(Context,
755 cast<PackExpansionType>(T1)->getPattern(),
756 cast<PackExpansionType>(T2)->getPattern()))
757 return false;
758 break;
759
Douglas Gregor3996e242010-02-15 22:01:00 +0000760 case Type::ObjCInterface: {
761 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
762 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
763 if (!IsStructurallyEquivalent(Context,
764 Iface1->getDecl(), Iface2->getDecl()))
765 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000766 break;
767 }
768
769 case Type::ObjCObject: {
770 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
771 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
772 if (!IsStructurallyEquivalent(Context,
773 Obj1->getBaseType(),
774 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000775 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000776 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
777 return false;
778 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000779 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000780 Obj1->getProtocol(I),
781 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000782 return false;
783 }
784 break;
785 }
786
787 case Type::ObjCObjectPointer: {
788 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
789 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
790 if (!IsStructurallyEquivalent(Context,
791 Ptr1->getPointeeType(),
792 Ptr2->getPointeeType()))
793 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000794 break;
795 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000796
797 case Type::Atomic: {
798 if (!IsStructurallyEquivalent(Context,
799 cast<AtomicType>(T1)->getValueType(),
800 cast<AtomicType>(T2)->getValueType()))
801 return false;
802 break;
803 }
804
Douglas Gregor3996e242010-02-15 22:01:00 +0000805 } // end switch
806
807 return true;
808}
809
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000810/// \brief Determine structural equivalence of two fields.
811static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
812 FieldDecl *Field1, FieldDecl *Field2) {
813 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
814
815 if (!IsStructurallyEquivalent(Context,
816 Field1->getType(), Field2->getType())) {
817 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
818 << Context.C2.getTypeDeclType(Owner2);
819 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
820 << Field2->getDeclName() << Field2->getType();
821 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
822 << Field1->getDeclName() << Field1->getType();
823 return false;
824 }
825
826 if (Field1->isBitField() != Field2->isBitField()) {
827 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
828 << Context.C2.getTypeDeclType(Owner2);
829 if (Field1->isBitField()) {
830 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
831 << Field1->getDeclName() << Field1->getType()
832 << Field1->getBitWidthValue(Context.C1);
833 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
834 << Field2->getDeclName();
835 } else {
836 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
837 << Field2->getDeclName() << Field2->getType()
838 << Field2->getBitWidthValue(Context.C2);
839 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
840 << Field1->getDeclName();
841 }
842 return false;
843 }
844
845 if (Field1->isBitField()) {
846 // Make sure that the bit-fields are the same length.
847 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
848 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
849
850 if (Bits1 != Bits2) {
851 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
852 << Context.C2.getTypeDeclType(Owner2);
853 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
854 << Field2->getDeclName() << Field2->getType() << Bits2;
855 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
856 << Field1->getDeclName() << Field1->getType() << Bits1;
857 return false;
858 }
859 }
860
861 return true;
862}
863
Douglas Gregor3996e242010-02-15 22:01:00 +0000864/// \brief Determine structural equivalence of two records.
865static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
866 RecordDecl *D1, RecordDecl *D2) {
867 if (D1->isUnion() != D2->isUnion()) {
868 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
869 << Context.C2.getTypeDeclType(D2);
870 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
871 << D1->getDeclName() << (unsigned)D1->getTagKind();
872 return false;
873 }
874
Douglas Gregore2e50d332010-12-01 01:36:18 +0000875 // If both declarations are class template specializations, we know
876 // the ODR applies, so check the template and template arguments.
877 ClassTemplateSpecializationDecl *Spec1
878 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
879 ClassTemplateSpecializationDecl *Spec2
880 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
881 if (Spec1 && Spec2) {
882 // Check that the specialized templates are the same.
883 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
884 Spec2->getSpecializedTemplate()))
885 return false;
886
887 // Check that the template arguments are the same.
888 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
889 return false;
890
891 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
892 if (!IsStructurallyEquivalent(Context,
893 Spec1->getTemplateArgs().get(I),
894 Spec2->getTemplateArgs().get(I)))
895 return false;
896 }
897 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000898 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000899 else if (Spec1 || Spec2)
900 return false;
901
Douglas Gregorb4964f72010-02-15 23:54:17 +0000902 // Compare the definitions of these two records. If either or both are
903 // incomplete, we assume that they are equivalent.
904 D1 = D1->getDefinition();
905 D2 = D2->getDefinition();
906 if (!D1 || !D2)
907 return true;
908
Douglas Gregor3996e242010-02-15 22:01:00 +0000909 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
910 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
911 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
912 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000913 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000914 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000915 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000916 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000917 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000918 return false;
919 }
920
921 // Check the base classes.
922 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
923 BaseEnd1 = D1CXX->bases_end(),
924 Base2 = D2CXX->bases_begin();
925 Base1 != BaseEnd1;
926 ++Base1, ++Base2) {
927 if (!IsStructurallyEquivalent(Context,
928 Base1->getType(), Base2->getType())) {
929 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
930 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000931 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000932 << Base2->getType()
933 << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000934 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000935 << Base1->getType()
936 << Base1->getSourceRange();
937 return false;
938 }
939
940 // Check virtual vs. non-virtual inheritance mismatch.
941 if (Base1->isVirtual() != Base2->isVirtual()) {
942 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
943 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000944 Context.Diag2(Base2->getLocStart(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000945 diag::note_odr_virtual_base)
946 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000947 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000948 << Base1->isVirtual()
949 << Base1->getSourceRange();
950 return false;
951 }
952 }
953 } else if (D1CXX->getNumBases() > 0) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
956 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000957 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000958 << Base1->getType()
959 << Base1->getSourceRange();
960 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
961 return false;
962 }
963 }
964
965 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000966 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000967 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +0000968 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000969 Field1End = D1->field_end();
970 Field1 != Field1End;
971 ++Field1, ++Field2) {
972 if (Field2 == Field2End) {
973 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
974 << Context.C2.getTypeDeclType(D2);
975 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
976 << Field1->getDeclName() << Field1->getType();
977 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
978 return false;
979 }
980
David Blaikie40ed2972012-06-06 20:45:41 +0000981 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000982 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000983 }
984
985 if (Field2 != Field2End) {
986 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
987 << Context.C2.getTypeDeclType(D2);
988 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
989 << Field2->getDeclName() << Field2->getType();
990 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
991 return false;
992 }
993
994 return true;
995}
996
997/// \brief Determine structural equivalence of two enums.
998static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
999 EnumDecl *D1, EnumDecl *D2) {
1000 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1001 EC2End = D2->enumerator_end();
1002 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1003 EC1End = D1->enumerator_end();
1004 EC1 != EC1End; ++EC1, ++EC2) {
1005 if (EC2 == EC2End) {
1006 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1007 << Context.C2.getTypeDeclType(D2);
1008 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1009 << EC1->getDeclName()
1010 << EC1->getInitVal().toString(10);
1011 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1012 return false;
1013 }
1014
1015 llvm::APSInt Val1 = EC1->getInitVal();
1016 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001017 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001018 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1019 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1020 << Context.C2.getTypeDeclType(D2);
1021 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1022 << EC2->getDeclName()
1023 << EC2->getInitVal().toString(10);
1024 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1025 << EC1->getDeclName()
1026 << EC1->getInitVal().toString(10);
1027 return false;
1028 }
1029 }
1030
1031 if (EC2 != EC2End) {
1032 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1033 << Context.C2.getTypeDeclType(D2);
1034 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1035 << EC2->getDeclName()
1036 << EC2->getInitVal().toString(10);
1037 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1038 return false;
1039 }
1040
1041 return true;
1042}
Douglas Gregora082a492010-11-30 19:14:50 +00001043
1044static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1045 TemplateParameterList *Params1,
1046 TemplateParameterList *Params2) {
1047 if (Params1->size() != Params2->size()) {
1048 Context.Diag2(Params2->getTemplateLoc(),
1049 diag::err_odr_different_num_template_parameters)
1050 << Params1->size() << Params2->size();
1051 Context.Diag1(Params1->getTemplateLoc(),
1052 diag::note_odr_template_parameter_list);
1053 return false;
1054 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001055
Douglas Gregora082a492010-11-30 19:14:50 +00001056 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1057 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1058 Context.Diag2(Params2->getParam(I)->getLocation(),
1059 diag::err_odr_different_template_parameter_kind);
1060 Context.Diag1(Params1->getParam(I)->getLocation(),
1061 diag::note_odr_template_parameter_here);
1062 return false;
1063 }
1064
1065 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1066 Params2->getParam(I))) {
1067
1068 return false;
1069 }
1070 }
1071
1072 return true;
1073}
1074
1075static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1076 TemplateTypeParmDecl *D1,
1077 TemplateTypeParmDecl *D2) {
1078 if (D1->isParameterPack() != D2->isParameterPack()) {
1079 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1080 << D2->isParameterPack();
1081 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1082 << D1->isParameterPack();
1083 return false;
1084 }
1085
1086 return true;
1087}
1088
1089static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1090 NonTypeTemplateParmDecl *D1,
1091 NonTypeTemplateParmDecl *D2) {
1092 // FIXME: Enable once we have variadic templates.
1093#if 0
1094 if (D1->isParameterPack() != D2->isParameterPack()) {
1095 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1096 << D2->isParameterPack();
1097 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1098 << D1->isParameterPack();
1099 return false;
1100 }
1101#endif
1102
1103 // Check types.
1104 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1105 Context.Diag2(D2->getLocation(),
1106 diag::err_odr_non_type_parameter_type_inconsistent)
1107 << D2->getType() << D1->getType();
1108 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1109 << D1->getType();
1110 return false;
1111 }
1112
1113 return true;
1114}
1115
1116static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1117 TemplateTemplateParmDecl *D1,
1118 TemplateTemplateParmDecl *D2) {
1119 // FIXME: Enable once we have variadic templates.
1120#if 0
1121 if (D1->isParameterPack() != D2->isParameterPack()) {
1122 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1123 << D2->isParameterPack();
1124 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1125 << D1->isParameterPack();
1126 return false;
1127 }
1128#endif
1129
1130 // Check template parameter lists.
1131 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1132 D2->getTemplateParameters());
1133}
1134
1135static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1136 ClassTemplateDecl *D1,
1137 ClassTemplateDecl *D2) {
1138 // Check template parameters.
1139 if (!IsStructurallyEquivalent(Context,
1140 D1->getTemplateParameters(),
1141 D2->getTemplateParameters()))
1142 return false;
1143
1144 // Check the templated declaration.
1145 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1146 D2->getTemplatedDecl());
1147}
1148
Douglas Gregor3996e242010-02-15 22:01:00 +00001149/// \brief Determine structural equivalence of two declarations.
1150static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1151 Decl *D1, Decl *D2) {
1152 // FIXME: Check for known structural equivalences via a callback of some sort.
1153
Douglas Gregorb4964f72010-02-15 23:54:17 +00001154 // Check whether we already know that these two declarations are not
1155 // structurally equivalent.
1156 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1157 D2->getCanonicalDecl())))
1158 return false;
1159
Douglas Gregor3996e242010-02-15 22:01:00 +00001160 // Determine whether we've already produced a tentative equivalence for D1.
1161 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1162 if (EquivToD1)
1163 return EquivToD1 == D2->getCanonicalDecl();
1164
1165 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1166 EquivToD1 = D2->getCanonicalDecl();
1167 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1168 return true;
1169}
1170
1171bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1172 Decl *D2) {
1173 if (!::IsStructurallyEquivalent(*this, D1, D2))
1174 return false;
1175
1176 return !Finish();
1177}
1178
1179bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1180 QualType T2) {
1181 if (!::IsStructurallyEquivalent(*this, T1, T2))
1182 return false;
1183
1184 return !Finish();
1185}
1186
1187bool StructuralEquivalenceContext::Finish() {
1188 while (!DeclsToCheck.empty()) {
1189 // Check the next declaration.
1190 Decl *D1 = DeclsToCheck.front();
1191 DeclsToCheck.pop_front();
1192
1193 Decl *D2 = TentativeEquivalences[D1];
1194 assert(D2 && "Unrecorded tentative equivalence?");
1195
Douglas Gregorb4964f72010-02-15 23:54:17 +00001196 bool Equivalent = true;
1197
Douglas Gregor3996e242010-02-15 22:01:00 +00001198 // FIXME: Switch on all declaration kinds. For now, we're just going to
1199 // check the obvious ones.
1200 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1201 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1202 // Check for equivalent structure names.
1203 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001204 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1205 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001206 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001207 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1208 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001209 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1210 !::IsStructurallyEquivalent(*this, Record1, Record2))
1211 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001212 } else {
1213 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001214 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001215 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001216 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001217 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1218 // Check for equivalent enum names.
1219 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001220 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1221 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001222 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001223 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1224 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001225 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1226 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1227 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 } else {
1229 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001230 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001231 }
Richard Smithdda56e42011-04-15 14:24:37 +00001232 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1233 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001234 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001235 Typedef2->getIdentifier()) ||
1236 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001237 Typedef1->getUnderlyingType(),
1238 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001239 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001240 } else {
1241 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001242 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001243 }
Douglas Gregora082a492010-11-30 19:14:50 +00001244 } else if (ClassTemplateDecl *ClassTemplate1
1245 = dyn_cast<ClassTemplateDecl>(D1)) {
1246 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1247 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1248 ClassTemplate2->getIdentifier()) ||
1249 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1250 Equivalent = false;
1251 } else {
1252 // Class template/non-class-template mismatch.
1253 Equivalent = false;
1254 }
1255 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1256 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1257 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1258 Equivalent = false;
1259 } else {
1260 // Kind mismatch.
1261 Equivalent = false;
1262 }
1263 } else if (NonTypeTemplateParmDecl *NTTP1
1264 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1265 if (NonTypeTemplateParmDecl *NTTP2
1266 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1267 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1268 Equivalent = false;
1269 } else {
1270 // Kind mismatch.
1271 Equivalent = false;
1272 }
1273 } else if (TemplateTemplateParmDecl *TTP1
1274 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1275 if (TemplateTemplateParmDecl *TTP2
1276 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1277 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1278 Equivalent = false;
1279 } else {
1280 // Kind mismatch.
1281 Equivalent = false;
1282 }
1283 }
1284
Douglas Gregorb4964f72010-02-15 23:54:17 +00001285 if (!Equivalent) {
1286 // Note that these two declarations are not equivalent (and we already
1287 // know about it).
1288 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1289 D2->getCanonicalDecl()));
1290 return true;
1291 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001292 // FIXME: Check other declaration kinds!
1293 }
1294
1295 return false;
1296}
1297
1298//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001299// Import Types
1300//----------------------------------------------------------------------------
1301
John McCall424cec92011-01-19 06:33:43 +00001302QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001303 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1304 << T->getTypeClassName();
1305 return QualType();
1306}
1307
John McCall424cec92011-01-19 06:33:43 +00001308QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001309 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001310#define SHARED_SINGLETON_TYPE(Expansion)
1311#define BUILTIN_TYPE(Id, SingletonId) \
1312 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1313#include "clang/AST/BuiltinTypes.def"
1314
1315 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1316 // context supports C++.
1317
1318 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1319 // context supports ObjC.
1320
Douglas Gregor96e578d2010-02-05 17:54:41 +00001321 case BuiltinType::Char_U:
1322 // The context we're importing from has an unsigned 'char'. If we're
1323 // importing into a context with a signed 'char', translate to
1324 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001325 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001326 return Importer.getToContext().UnsignedCharTy;
1327
1328 return Importer.getToContext().CharTy;
1329
Douglas Gregor96e578d2010-02-05 17:54:41 +00001330 case BuiltinType::Char_S:
1331 // The context we're importing from has an unsigned 'char'. If we're
1332 // importing into a context with a signed 'char', translate to
1333 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001334 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001335 return Importer.getToContext().SignedCharTy;
1336
1337 return Importer.getToContext().CharTy;
1338
Chris Lattnerad3467e2010-12-25 23:25:43 +00001339 case BuiltinType::WChar_S:
1340 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001341 // FIXME: If not in C++, shall we translate to the C equivalent of
1342 // wchar_t?
1343 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001344 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001345
1346 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001347}
1348
John McCall424cec92011-01-19 06:33:43 +00001349QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001350 QualType ToElementType = Importer.Import(T->getElementType());
1351 if (ToElementType.isNull())
1352 return QualType();
1353
1354 return Importer.getToContext().getComplexType(ToElementType);
1355}
1356
John McCall424cec92011-01-19 06:33:43 +00001357QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001358 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1359 if (ToPointeeType.isNull())
1360 return QualType();
1361
1362 return Importer.getToContext().getPointerType(ToPointeeType);
1363}
1364
John McCall424cec92011-01-19 06:33:43 +00001365QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001366 // FIXME: Check for blocks support in "to" context.
1367 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1368 if (ToPointeeType.isNull())
1369 return QualType();
1370
1371 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1372}
1373
John McCall424cec92011-01-19 06:33:43 +00001374QualType
1375ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001376 // FIXME: Check for C++ support in "to" context.
1377 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1378 if (ToPointeeType.isNull())
1379 return QualType();
1380
1381 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1382}
1383
John McCall424cec92011-01-19 06:33:43 +00001384QualType
1385ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001386 // FIXME: Check for C++0x support in "to" context.
1387 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1388 if (ToPointeeType.isNull())
1389 return QualType();
1390
1391 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1392}
1393
John McCall424cec92011-01-19 06:33:43 +00001394QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001395 // FIXME: Check for C++ support in "to" context.
1396 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1397 if (ToPointeeType.isNull())
1398 return QualType();
1399
1400 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1401 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1402 ClassType.getTypePtr());
1403}
1404
John McCall424cec92011-01-19 06:33:43 +00001405QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001406 QualType ToElementType = Importer.Import(T->getElementType());
1407 if (ToElementType.isNull())
1408 return QualType();
1409
1410 return Importer.getToContext().getConstantArrayType(ToElementType,
1411 T->getSize(),
1412 T->getSizeModifier(),
1413 T->getIndexTypeCVRQualifiers());
1414}
1415
John McCall424cec92011-01-19 06:33:43 +00001416QualType
1417ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001418 QualType ToElementType = Importer.Import(T->getElementType());
1419 if (ToElementType.isNull())
1420 return QualType();
1421
1422 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1423 T->getSizeModifier(),
1424 T->getIndexTypeCVRQualifiers());
1425}
1426
John McCall424cec92011-01-19 06:33:43 +00001427QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001428 QualType ToElementType = Importer.Import(T->getElementType());
1429 if (ToElementType.isNull())
1430 return QualType();
1431
1432 Expr *Size = Importer.Import(T->getSizeExpr());
1433 if (!Size)
1434 return QualType();
1435
1436 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1437 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1438 T->getSizeModifier(),
1439 T->getIndexTypeCVRQualifiers(),
1440 Brackets);
1441}
1442
John McCall424cec92011-01-19 06:33:43 +00001443QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444 QualType ToElementType = Importer.Import(T->getElementType());
1445 if (ToElementType.isNull())
1446 return QualType();
1447
1448 return Importer.getToContext().getVectorType(ToElementType,
1449 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001450 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001451}
1452
John McCall424cec92011-01-19 06:33:43 +00001453QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001454 QualType ToElementType = Importer.Import(T->getElementType());
1455 if (ToElementType.isNull())
1456 return QualType();
1457
1458 return Importer.getToContext().getExtVectorType(ToElementType,
1459 T->getNumElements());
1460}
1461
John McCall424cec92011-01-19 06:33:43 +00001462QualType
1463ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001464 // FIXME: What happens if we're importing a function without a prototype
1465 // into C++? Should we make it variadic?
1466 QualType ToResultType = Importer.Import(T->getResultType());
1467 if (ToResultType.isNull())
1468 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001469
Douglas Gregor96e578d2010-02-05 17:54:41 +00001470 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001471 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001472}
1473
John McCall424cec92011-01-19 06:33:43 +00001474QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001475 QualType ToResultType = Importer.Import(T->getResultType());
1476 if (ToResultType.isNull())
1477 return QualType();
1478
1479 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001480 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1482 AEnd = T->arg_type_end();
1483 A != AEnd; ++A) {
1484 QualType ArgType = Importer.Import(*A);
1485 if (ArgType.isNull())
1486 return QualType();
1487 ArgTypes.push_back(ArgType);
1488 }
1489
1490 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001491 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001492 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1493 EEnd = T->exception_end();
1494 E != EEnd; ++E) {
1495 QualType ExceptionType = Importer.Import(*E);
1496 if (ExceptionType.isNull())
1497 return QualType();
1498 ExceptionTypes.push_back(ExceptionType);
1499 }
John McCalldb40c7f2010-12-14 08:05:40 +00001500
1501 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1502 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503
1504 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001505 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506}
1507
Sean Callananda6df8a2011-08-11 16:56:07 +00001508QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1509 QualType ToInnerType = Importer.Import(T->getInnerType());
1510 if (ToInnerType.isNull())
1511 return QualType();
1512
1513 return Importer.getToContext().getParenType(ToInnerType);
1514}
1515
John McCall424cec92011-01-19 06:33:43 +00001516QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001517 TypedefNameDecl *ToDecl
1518 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001519 if (!ToDecl)
1520 return QualType();
1521
1522 return Importer.getToContext().getTypeDeclType(ToDecl);
1523}
1524
John McCall424cec92011-01-19 06:33:43 +00001525QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001526 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1527 if (!ToExpr)
1528 return QualType();
1529
1530 return Importer.getToContext().getTypeOfExprType(ToExpr);
1531}
1532
John McCall424cec92011-01-19 06:33:43 +00001533QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001534 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1535 if (ToUnderlyingType.isNull())
1536 return QualType();
1537
1538 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1539}
1540
John McCall424cec92011-01-19 06:33:43 +00001541QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001542 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001543 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1544 if (!ToExpr)
1545 return QualType();
1546
Douglas Gregor81495f32012-02-12 18:42:33 +00001547 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1548 if (UnderlyingType.isNull())
1549 return QualType();
1550
1551 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001552}
1553
Alexis Hunte852b102011-05-24 22:41:36 +00001554QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1555 QualType ToBaseType = Importer.Import(T->getBaseType());
1556 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1557 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1558 return QualType();
1559
1560 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1561 ToUnderlyingType,
1562 T->getUTTKind());
1563}
1564
Richard Smith30482bc2011-02-20 03:19:35 +00001565QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1566 // FIXME: Make sure that the "to" context supports C++0x!
1567 QualType FromDeduced = T->getDeducedType();
1568 QualType ToDeduced;
1569 if (!FromDeduced.isNull()) {
1570 ToDeduced = Importer.Import(FromDeduced);
1571 if (ToDeduced.isNull())
1572 return QualType();
1573 }
1574
1575 return Importer.getToContext().getAutoType(ToDeduced);
1576}
1577
John McCall424cec92011-01-19 06:33:43 +00001578QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001579 RecordDecl *ToDecl
1580 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1581 if (!ToDecl)
1582 return QualType();
1583
1584 return Importer.getToContext().getTagDeclType(ToDecl);
1585}
1586
John McCall424cec92011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 EnumDecl *ToDecl
1589 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1590 if (!ToDecl)
1591 return QualType();
1592
1593 return Importer.getToContext().getTagDeclType(ToDecl);
1594}
1595
Douglas Gregore2e50d332010-12-01 01:36:18 +00001596QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001597 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001598 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1599 if (ToTemplate.isNull())
1600 return QualType();
1601
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001602 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001603 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1604 return QualType();
1605
1606 QualType ToCanonType;
1607 if (!QualType(T, 0).isCanonical()) {
1608 QualType FromCanonType
1609 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1610 ToCanonType =Importer.Import(FromCanonType);
1611 if (ToCanonType.isNull())
1612 return QualType();
1613 }
1614 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1615 ToTemplateArgs.data(),
1616 ToTemplateArgs.size(),
1617 ToCanonType);
1618}
1619
John McCall424cec92011-01-19 06:33:43 +00001620QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001621 NestedNameSpecifier *ToQualifier = 0;
1622 // Note: the qualifier in an ElaboratedType is optional.
1623 if (T->getQualifier()) {
1624 ToQualifier = Importer.Import(T->getQualifier());
1625 if (!ToQualifier)
1626 return QualType();
1627 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001628
1629 QualType ToNamedType = Importer.Import(T->getNamedType());
1630 if (ToNamedType.isNull())
1631 return QualType();
1632
Abramo Bagnara6150c882010-05-11 21:36:43 +00001633 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1634 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001635}
1636
John McCall424cec92011-01-19 06:33:43 +00001637QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 ObjCInterfaceDecl *Class
1639 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1640 if (!Class)
1641 return QualType();
1642
John McCall8b07ec22010-05-15 11:32:37 +00001643 return Importer.getToContext().getObjCInterfaceType(Class);
1644}
1645
John McCall424cec92011-01-19 06:33:43 +00001646QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001647 QualType ToBaseType = Importer.Import(T->getBaseType());
1648 if (ToBaseType.isNull())
1649 return QualType();
1650
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001651 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001652 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001653 PEnd = T->qual_end();
1654 P != PEnd; ++P) {
1655 ObjCProtocolDecl *Protocol
1656 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1657 if (!Protocol)
1658 return QualType();
1659 Protocols.push_back(Protocol);
1660 }
1661
John McCall8b07ec22010-05-15 11:32:37 +00001662 return Importer.getToContext().getObjCObjectType(ToBaseType,
1663 Protocols.data(),
1664 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001665}
1666
John McCall424cec92011-01-19 06:33:43 +00001667QualType
1668ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001669 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1670 if (ToPointeeType.isNull())
1671 return QualType();
1672
John McCall8b07ec22010-05-15 11:32:37 +00001673 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001674}
1675
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001676//----------------------------------------------------------------------------
1677// Import Declarations
1678//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001679bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1680 DeclContext *&LexicalDC,
1681 DeclarationName &Name,
1682 SourceLocation &Loc) {
1683 // Import the context of this declaration.
1684 DC = Importer.ImportContext(D->getDeclContext());
1685 if (!DC)
1686 return true;
1687
1688 LexicalDC = DC;
1689 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1690 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1691 if (!LexicalDC)
1692 return true;
1693 }
1694
1695 // Import the name of this declaration.
1696 Name = Importer.Import(D->getDeclName());
1697 if (D->getDeclName() && !Name)
1698 return true;
1699
1700 // Import the location of this declaration.
1701 Loc = Importer.Import(D->getLocation());
1702 return false;
1703}
1704
Douglas Gregord451ea92011-07-29 23:31:30 +00001705void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1706 if (!FromD)
1707 return;
1708
1709 if (!ToD) {
1710 ToD = Importer.Import(FromD);
1711 if (!ToD)
1712 return;
1713 }
1714
1715 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1716 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1717 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1718 ImportDefinition(FromRecord, ToRecord);
1719 }
1720 }
1721 return;
1722 }
1723
1724 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1725 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1726 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1727 ImportDefinition(FromEnum, ToEnum);
1728 }
1729 }
1730 return;
1731 }
1732}
1733
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001734void
1735ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1736 DeclarationNameInfo& To) {
1737 // NOTE: To.Name and To.Loc are already imported.
1738 // We only have to import To.LocInfo.
1739 switch (To.getName().getNameKind()) {
1740 case DeclarationName::Identifier:
1741 case DeclarationName::ObjCZeroArgSelector:
1742 case DeclarationName::ObjCOneArgSelector:
1743 case DeclarationName::ObjCMultiArgSelector:
1744 case DeclarationName::CXXUsingDirective:
1745 return;
1746
1747 case DeclarationName::CXXOperatorName: {
1748 SourceRange Range = From.getCXXOperatorNameRange();
1749 To.setCXXOperatorNameRange(Importer.Import(Range));
1750 return;
1751 }
1752 case DeclarationName::CXXLiteralOperatorName: {
1753 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1754 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1755 return;
1756 }
1757 case DeclarationName::CXXConstructorName:
1758 case DeclarationName::CXXDestructorName:
1759 case DeclarationName::CXXConversionFunctionName: {
1760 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1761 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1762 return;
1763 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001764 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001765 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001766}
1767
Douglas Gregor2e15c842012-02-01 21:00:38 +00001768void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001769 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001770 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001771 return;
1772 }
1773
Douglas Gregor968d6332010-02-21 18:24:45 +00001774 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1775 FromEnd = FromDC->decls_end();
1776 From != FromEnd;
1777 ++From)
1778 Importer.Import(*From);
1779}
1780
Douglas Gregord451ea92011-07-29 23:31:30 +00001781bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001782 ImportDefinitionKind Kind) {
1783 if (To->getDefinition() || To->isBeingDefined()) {
1784 if (Kind == IDK_Everything)
1785 ImportDeclContext(From, /*ForceImport=*/true);
1786
Douglas Gregore2e50d332010-12-01 01:36:18 +00001787 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001788 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001789
1790 To->startDefinition();
1791
1792 // Add base classes.
1793 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1794 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001795
1796 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1797 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1798 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1799 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1800 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1801 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1802 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1803 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1804 ToData.Aggregate = FromData.Aggregate;
1805 ToData.PlainOldData = FromData.PlainOldData;
1806 ToData.Empty = FromData.Empty;
1807 ToData.Polymorphic = FromData.Polymorphic;
1808 ToData.Abstract = FromData.Abstract;
1809 ToData.IsStandardLayout = FromData.IsStandardLayout;
1810 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1811 ToData.HasPrivateFields = FromData.HasPrivateFields;
1812 ToData.HasProtectedFields = FromData.HasProtectedFields;
1813 ToData.HasPublicFields = FromData.HasPublicFields;
1814 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001815 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001816 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001817 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1818 ToData.HasConstexprNonCopyMoveConstructor
1819 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001820 ToData.DefaultedDefaultConstructorIsConstexpr
1821 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001822 ToData.HasConstexprDefaultConstructor
1823 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001824 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1825 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1826 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1827 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1828 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001829 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001830 ToData.HasNonLiteralTypeFieldsOrBases
1831 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001832 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001833 ToData.UserProvidedDefaultConstructor
1834 = FromData.UserProvidedDefaultConstructor;
1835 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1836 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1837 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1838 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1839 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1840 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1841 ToData.FailedImplicitMoveConstructor
1842 = FromData.FailedImplicitMoveConstructor;
1843 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001844 ToData.IsLambda = FromData.IsLambda;
1845
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001846 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001847 for (CXXRecordDecl::base_class_iterator
1848 Base1 = FromCXX->bases_begin(),
1849 FromBaseEnd = FromCXX->bases_end();
1850 Base1 != FromBaseEnd;
1851 ++Base1) {
1852 QualType T = Importer.Import(Base1->getType());
1853 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001854 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001855
1856 SourceLocation EllipsisLoc;
1857 if (Base1->isPackExpansion())
1858 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001859
1860 // Ensure that we have a definition for the base.
1861 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1862
Douglas Gregore2e50d332010-12-01 01:36:18 +00001863 Bases.push_back(
1864 new (Importer.getToContext())
1865 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1866 Base1->isVirtual(),
1867 Base1->isBaseOfClass(),
1868 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001869 Importer.Import(Base1->getTypeSourceInfo()),
1870 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001871 }
1872 if (!Bases.empty())
1873 ToCXX->setBases(Bases.data(), Bases.size());
1874 }
1875
Douglas Gregor2e15c842012-02-01 21:00:38 +00001876 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001877 ImportDeclContext(From, /*ForceImport=*/true);
1878
Douglas Gregore2e50d332010-12-01 01:36:18 +00001879 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001880 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001881}
1882
Douglas Gregord451ea92011-07-29 23:31:30 +00001883bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001884 ImportDefinitionKind Kind) {
1885 if (To->getDefinition() || To->isBeingDefined()) {
1886 if (Kind == IDK_Everything)
1887 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001888 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001889 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001890
1891 To->startDefinition();
1892
1893 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1894 if (T.isNull())
1895 return true;
1896
1897 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1898 if (ToPromotionType.isNull())
1899 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001900
1901 if (shouldForceImportDeclContext(Kind))
1902 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001903
1904 // FIXME: we might need to merge the number of positive or negative bits
1905 // if the enumerator lists don't match.
1906 To->completeDefinition(T, ToPromotionType,
1907 From->getNumPositiveBits(),
1908 From->getNumNegativeBits());
1909 return false;
1910}
1911
Douglas Gregora082a492010-11-30 19:14:50 +00001912TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1913 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001914 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001915 ToParams.reserve(Params->size());
1916 for (TemplateParameterList::iterator P = Params->begin(),
1917 PEnd = Params->end();
1918 P != PEnd; ++P) {
1919 Decl *To = Importer.Import(*P);
1920 if (!To)
1921 return 0;
1922
1923 ToParams.push_back(cast<NamedDecl>(To));
1924 }
1925
1926 return TemplateParameterList::Create(Importer.getToContext(),
1927 Importer.Import(Params->getTemplateLoc()),
1928 Importer.Import(Params->getLAngleLoc()),
1929 ToParams.data(), ToParams.size(),
1930 Importer.Import(Params->getRAngleLoc()));
1931}
1932
Douglas Gregore2e50d332010-12-01 01:36:18 +00001933TemplateArgument
1934ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1935 switch (From.getKind()) {
1936 case TemplateArgument::Null:
1937 return TemplateArgument();
1938
1939 case TemplateArgument::Type: {
1940 QualType ToType = Importer.Import(From.getAsType());
1941 if (ToType.isNull())
1942 return TemplateArgument();
1943 return TemplateArgument(ToType);
1944 }
1945
1946 case TemplateArgument::Integral: {
1947 QualType ToType = Importer.Import(From.getIntegralType());
1948 if (ToType.isNull())
1949 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001950 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001951 }
1952
1953 case TemplateArgument::Declaration:
1954 if (Decl *To = Importer.Import(From.getAsDecl()))
1955 return TemplateArgument(To);
1956 return TemplateArgument();
1957
1958 case TemplateArgument::Template: {
1959 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1960 if (ToTemplate.isNull())
1961 return TemplateArgument();
1962
1963 return TemplateArgument(ToTemplate);
1964 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001965
1966 case TemplateArgument::TemplateExpansion: {
1967 TemplateName ToTemplate
1968 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1969 if (ToTemplate.isNull())
1970 return TemplateArgument();
1971
Douglas Gregore1d60df2011-01-14 23:41:42 +00001972 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001973 }
1974
Douglas Gregore2e50d332010-12-01 01:36:18 +00001975 case TemplateArgument::Expression:
1976 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1977 return TemplateArgument(ToExpr);
1978 return TemplateArgument();
1979
1980 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001981 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001982 ToPack.reserve(From.pack_size());
1983 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1984 return TemplateArgument();
1985
1986 TemplateArgument *ToArgs
1987 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1988 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1989 return TemplateArgument(ToArgs, ToPack.size());
1990 }
1991 }
1992
1993 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00001994}
1995
1996bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1997 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001998 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001999 for (unsigned I = 0; I != NumFromArgs; ++I) {
2000 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2001 if (To.isNull() && !FromArgs[I].isNull())
2002 return true;
2003
2004 ToArgs.push_back(To);
2005 }
2006
2007 return false;
2008}
2009
Douglas Gregor5c73e912010-02-11 00:48:18 +00002010bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00002011 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002012 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002013 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002014 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002015 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002016}
2017
Douglas Gregor98c10182010-02-12 22:17:39 +00002018bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002019 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002020 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002021 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002022 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002023}
2024
Douglas Gregora082a492010-11-30 19:14:50 +00002025bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2026 ClassTemplateDecl *To) {
2027 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2028 Importer.getToContext(),
2029 Importer.getNonEquivalentDecls());
2030 return Ctx.IsStructurallyEquivalent(From, To);
2031}
2032
Douglas Gregore4c83e42010-02-09 22:48:33 +00002033Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002034 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002035 << D->getDeclKindName();
2036 return 0;
2037}
2038
Sean Callanan65198272011-11-17 23:20:56 +00002039Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2040 TranslationUnitDecl *ToD =
2041 Importer.getToContext().getTranslationUnitDecl();
2042
2043 Importer.Imported(D, ToD);
2044
2045 return ToD;
2046}
2047
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002048Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2049 // Import the major distinguishing characteristics of this namespace.
2050 DeclContext *DC, *LexicalDC;
2051 DeclarationName Name;
2052 SourceLocation Loc;
2053 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2054 return 0;
2055
2056 NamespaceDecl *MergeWithNamespace = 0;
2057 if (!Name) {
2058 // This is an anonymous namespace. Adopt an existing anonymous
2059 // namespace if we can.
2060 // FIXME: Not testable.
2061 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2062 MergeWithNamespace = TU->getAnonymousNamespace();
2063 else
2064 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2065 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002066 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002067 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2068 DC->localUncachedLookup(Name, FoundDecls);
2069 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2070 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002071 continue;
2072
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002073 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002074 MergeWithNamespace = FoundNS;
2075 ConflictingDecls.clear();
2076 break;
2077 }
2078
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002079 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002080 }
2081
2082 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002083 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002084 ConflictingDecls.data(),
2085 ConflictingDecls.size());
2086 }
2087 }
2088
2089 // Create the "to" namespace, if needed.
2090 NamespaceDecl *ToNamespace = MergeWithNamespace;
2091 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002092 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002093 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002094 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002095 Loc, Name.getAsIdentifierInfo(),
2096 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002097 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002098 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002099
2100 // If this is an anonymous namespace, register it as the anonymous
2101 // namespace within its context.
2102 if (!Name) {
2103 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2104 TU->setAnonymousNamespace(ToNamespace);
2105 else
2106 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2107 }
2108 }
2109 Importer.Imported(D, ToNamespace);
2110
2111 ImportDeclContext(D);
2112
2113 return ToNamespace;
2114}
2115
Richard Smithdda56e42011-04-15 14:24:37 +00002116Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002117 // Import the major distinguishing characteristics of this typedef.
2118 DeclContext *DC, *LexicalDC;
2119 DeclarationName Name;
2120 SourceLocation Loc;
2121 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2122 return 0;
2123
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002124 // If this typedef is not in block scope, determine whether we've
2125 // seen a typedef with the same name (that we can merge with) or any
2126 // other entity by that name (which name lookup could conflict with).
2127 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002128 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002129 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002130 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2131 DC->localUncachedLookup(Name, FoundDecls);
2132 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2133 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002134 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002135 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002136 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002137 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2138 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002139 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002140 }
2141
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002142 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002143 }
2144
2145 if (!ConflictingDecls.empty()) {
2146 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2147 ConflictingDecls.data(),
2148 ConflictingDecls.size());
2149 if (!Name)
2150 return 0;
2151 }
2152 }
2153
Douglas Gregorb4964f72010-02-15 23:54:17 +00002154 // Import the underlying type of this typedef;
2155 QualType T = Importer.Import(D->getUnderlyingType());
2156 if (T.isNull())
2157 return 0;
2158
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002159 // Create the new typedef node.
2160 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002161 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002162 TypedefNameDecl *ToTypedef;
2163 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002164 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2165 StartL, Loc,
2166 Name.getAsIdentifierInfo(),
2167 TInfo);
2168 else
Richard Smithdda56e42011-04-15 14:24:37 +00002169 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2170 StartL, Loc,
2171 Name.getAsIdentifierInfo(),
2172 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002173
Douglas Gregordd483172010-02-22 17:42:47 +00002174 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002175 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002176 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002177 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002178
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002179 return ToTypedef;
2180}
2181
Richard Smithdda56e42011-04-15 14:24:37 +00002182Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2183 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2184}
2185
2186Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2187 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2188}
2189
Douglas Gregor98c10182010-02-12 22:17:39 +00002190Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2191 // Import the major distinguishing characteristics of this enum.
2192 DeclContext *DC, *LexicalDC;
2193 DeclarationName Name;
2194 SourceLocation Loc;
2195 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2196 return 0;
2197
2198 // Figure out what enum name we're looking for.
2199 unsigned IDNS = Decl::IDNS_Tag;
2200 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002201 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2202 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002203 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002204 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002205 IDNS |= Decl::IDNS_Ordinary;
2206
2207 // We may already have an enum of the same name; try to find and match it.
2208 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002209 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002210 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2211 DC->localUncachedLookup(SearchName, FoundDecls);
2212 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2213 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002214 continue;
2215
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002216 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002217 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002218 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2219 Found = Tag->getDecl();
2220 }
2221
2222 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002223 if (IsStructuralMatch(D, FoundEnum))
2224 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002225 }
2226
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002227 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002228 }
2229
2230 if (!ConflictingDecls.empty()) {
2231 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2232 ConflictingDecls.data(),
2233 ConflictingDecls.size());
2234 }
2235 }
2236
2237 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002238 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2239 Importer.Import(D->getLocStart()),
2240 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002241 D->isScoped(), D->isScopedUsingClassTag(),
2242 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002243 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002244 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002245 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002246 D2->setLexicalDeclContext(LexicalDC);
2247 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002248 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002249
2250 // Import the integer type.
2251 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2252 if (ToIntegerType.isNull())
2253 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002254 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002255
2256 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002257 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002258 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002259
Douglas Gregor3996e242010-02-15 22:01:00 +00002260 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002261}
2262
Douglas Gregor5c73e912010-02-11 00:48:18 +00002263Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2264 // If this record has a definition in the translation unit we're coming from,
2265 // but this particular declaration is not that definition, import the
2266 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002267 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002268 if (Definition && Definition != D) {
2269 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002270 if (!ImportedDef)
2271 return 0;
2272
2273 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002274 }
2275
2276 // Import the major distinguishing characteristics of this record.
2277 DeclContext *DC, *LexicalDC;
2278 DeclarationName Name;
2279 SourceLocation Loc;
2280 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2281 return 0;
2282
2283 // Figure out what structure name we're looking for.
2284 unsigned IDNS = Decl::IDNS_Tag;
2285 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002286 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2287 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002288 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002289 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002290 IDNS |= Decl::IDNS_Ordinary;
2291
2292 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002293 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002294 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002295 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002296 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2297 DC->localUncachedLookup(SearchName, FoundDecls);
2298 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2299 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002300 continue;
2301
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002302 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002303 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002304 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2305 Found = Tag->getDecl();
2306 }
2307
2308 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002309 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00002310 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002311 // The record types structurally match, or the "from" translation
2312 // unit only had a forward declaration anyway; call it the same
2313 // function.
2314 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002315 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002316 }
2317 } else {
2318 // We have a forward declaration of this type, so adopt that forward
2319 // declaration rather than building a new one.
2320 AdoptDecl = FoundRecord;
2321 continue;
2322 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002323 }
2324
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002325 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002326 }
2327
2328 if (!ConflictingDecls.empty()) {
2329 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2330 ConflictingDecls.data(),
2331 ConflictingDecls.size());
2332 }
2333 }
2334
2335 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002336 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002337 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002338 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002339 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002340 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002341 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002342 DC, StartLoc, Loc,
2343 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002344 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002345 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002346 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002347 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002348 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002349 }
Douglas Gregor14454802011-02-25 02:25:35 +00002350
2351 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002352 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002353 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002354 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002355
Douglas Gregor3996e242010-02-15 22:01:00 +00002356 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002357
Douglas Gregor95d82832012-01-24 18:36:04 +00002358 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002359 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002360
Douglas Gregor3996e242010-02-15 22:01:00 +00002361 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002362}
2363
Douglas Gregor98c10182010-02-12 22:17:39 +00002364Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2365 // Import the major distinguishing characteristics of this enumerator.
2366 DeclContext *DC, *LexicalDC;
2367 DeclarationName Name;
2368 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002369 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002370 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002371
2372 QualType T = Importer.Import(D->getType());
2373 if (T.isNull())
2374 return 0;
2375
Douglas Gregor98c10182010-02-12 22:17:39 +00002376 // Determine whether there are any other declarations with the same name and
2377 // in the same context.
2378 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002379 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002380 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002381 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2382 DC->localUncachedLookup(Name, FoundDecls);
2383 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2384 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002385 continue;
2386
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002387 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002388 }
2389
2390 if (!ConflictingDecls.empty()) {
2391 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2392 ConflictingDecls.data(),
2393 ConflictingDecls.size());
2394 if (!Name)
2395 return 0;
2396 }
2397 }
2398
2399 Expr *Init = Importer.Import(D->getInitExpr());
2400 if (D->getInitExpr() && !Init)
2401 return 0;
2402
2403 EnumConstantDecl *ToEnumerator
2404 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2405 Name.getAsIdentifierInfo(), T,
2406 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002407 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002408 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002409 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002410 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002411 return ToEnumerator;
2412}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002413
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002414Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2415 // Import the major distinguishing characteristics of this function.
2416 DeclContext *DC, *LexicalDC;
2417 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002418 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002419 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002420 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002421
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002422 // Try to find a function in our own ("to") context with the same name, same
2423 // type, and in the same context as the function we're importing.
2424 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002425 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002426 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002427 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2428 DC->localUncachedLookup(Name, FoundDecls);
2429 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2430 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002431 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002432
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002433 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002434 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2435 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002436 if (Importer.IsStructurallyEquivalent(D->getType(),
2437 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002438 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002439 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002440 }
2441
2442 // FIXME: Check for overloading more carefully, e.g., by boosting
2443 // Sema::IsOverload out to the AST library.
2444
2445 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002446 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002447 continue;
2448
2449 // Complain about inconsistent function types.
2450 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002451 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002452 Importer.ToDiag(FoundFunction->getLocation(),
2453 diag::note_odr_value_here)
2454 << FoundFunction->getType();
2455 }
2456 }
2457
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002458 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002459 }
2460
2461 if (!ConflictingDecls.empty()) {
2462 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2463 ConflictingDecls.data(),
2464 ConflictingDecls.size());
2465 if (!Name)
2466 return 0;
2467 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002468 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002469
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002470 DeclarationNameInfo NameInfo(Name, Loc);
2471 // Import additional name location/type info.
2472 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2473
Douglas Gregorb4964f72010-02-15 23:54:17 +00002474 // Import the type.
2475 QualType T = Importer.Import(D->getType());
2476 if (T.isNull())
2477 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002478
2479 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002480 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002481 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2482 P != PEnd; ++P) {
2483 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2484 if (!ToP)
2485 return 0;
2486
2487 Parameters.push_back(ToP);
2488 }
2489
2490 // Create the imported function.
2491 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002492 FunctionDecl *ToFunction = 0;
2493 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2494 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2495 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002496 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002497 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002498 FromConstructor->isExplicit(),
2499 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002500 D->isImplicit(),
2501 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002502 } else if (isa<CXXDestructorDecl>(D)) {
2503 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2504 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002505 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002506 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002507 D->isInlineSpecified(),
2508 D->isImplicit());
2509 } else if (CXXConversionDecl *FromConversion
2510 = dyn_cast<CXXConversionDecl>(D)) {
2511 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2512 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002513 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002514 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002515 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002516 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002517 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002518 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002519 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2520 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2521 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002522 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002523 NameInfo, T, TInfo,
2524 Method->isStatic(),
2525 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002526 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002527 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002528 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002529 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002530 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002531 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002532 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002533 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002534 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002535 D->hasWrittenPrototype(),
2536 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002537 }
John McCall3e11ebe2010-03-15 10:12:16 +00002538
2539 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002540 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002541 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002542 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002543 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2544 ToFunction->setTrivial(D->isTrivial());
2545 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002546 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002547
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002548 // Set the parameters.
2549 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002550 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002551 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002552 }
David Blaikie9c70e042011-09-21 18:16:56 +00002553 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002554
2555 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002556
2557 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002558 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002559
Douglas Gregor43f54792010-02-17 02:12:47 +00002560 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002561}
2562
Douglas Gregor00eace12010-02-21 18:29:16 +00002563Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2564 return VisitFunctionDecl(D);
2565}
2566
2567Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2568 return VisitCXXMethodDecl(D);
2569}
2570
2571Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2572 return VisitCXXMethodDecl(D);
2573}
2574
2575Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2576 return VisitCXXMethodDecl(D);
2577}
2578
Douglas Gregor5c73e912010-02-11 00:48:18 +00002579Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2580 // Import the major distinguishing characteristics of a variable.
2581 DeclContext *DC, *LexicalDC;
2582 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002583 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002584 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2585 return 0;
2586
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002587 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002588 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2589 DC->localUncachedLookup(Name, FoundDecls);
2590 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2591 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002592 if (Importer.IsStructurallyEquivalent(D->getType(),
2593 FoundField->getType())) {
2594 Importer.Imported(D, FoundField);
2595 return FoundField;
2596 }
2597
2598 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2599 << Name << D->getType() << FoundField->getType();
2600 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2601 << FoundField->getType();
2602 return 0;
2603 }
2604 }
2605
Douglas Gregorb4964f72010-02-15 23:54:17 +00002606 // Import the type.
2607 QualType T = Importer.Import(D->getType());
2608 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002609 return 0;
2610
2611 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2612 Expr *BitWidth = Importer.Import(D->getBitWidth());
2613 if (!BitWidth && D->getBitWidth())
2614 return 0;
2615
Abramo Bagnaradff19302011-03-08 08:55:46 +00002616 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2617 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002618 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002619 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002620 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002621 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002622 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002623 if (ToField->hasInClassInitializer())
2624 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002625 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002626 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002627 return ToField;
2628}
2629
Francois Pichet783dd6e2010-11-21 06:08:52 +00002630Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2631 // Import the major distinguishing characteristics of a variable.
2632 DeclContext *DC, *LexicalDC;
2633 DeclarationName Name;
2634 SourceLocation Loc;
2635 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2636 return 0;
2637
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002638 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002639 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2640 DC->localUncachedLookup(Name, FoundDecls);
2641 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002642 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002643 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002644 if (Importer.IsStructurallyEquivalent(D->getType(),
2645 FoundField->getType())) {
2646 Importer.Imported(D, FoundField);
2647 return FoundField;
2648 }
2649
2650 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2651 << Name << D->getType() << FoundField->getType();
2652 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2653 << FoundField->getType();
2654 return 0;
2655 }
2656 }
2657
Francois Pichet783dd6e2010-11-21 06:08:52 +00002658 // Import the type.
2659 QualType T = Importer.Import(D->getType());
2660 if (T.isNull())
2661 return 0;
2662
2663 NamedDecl **NamedChain =
2664 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2665
2666 unsigned i = 0;
2667 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2668 PE = D->chain_end(); PI != PE; ++PI) {
2669 Decl* D = Importer.Import(*PI);
2670 if (!D)
2671 return 0;
2672 NamedChain[i++] = cast<NamedDecl>(D);
2673 }
2674
2675 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2676 Importer.getToContext(), DC,
2677 Loc, Name.getAsIdentifierInfo(), T,
2678 NamedChain, D->getChainingSize());
2679 ToIndirectField->setAccess(D->getAccess());
2680 ToIndirectField->setLexicalDeclContext(LexicalDC);
2681 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002682 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002683 return ToIndirectField;
2684}
2685
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002686Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2687 // Import the major distinguishing characteristics of an ivar.
2688 DeclContext *DC, *LexicalDC;
2689 DeclarationName Name;
2690 SourceLocation Loc;
2691 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2692 return 0;
2693
2694 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002695 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2696 DC->localUncachedLookup(Name, FoundDecls);
2697 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2698 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002699 if (Importer.IsStructurallyEquivalent(D->getType(),
2700 FoundIvar->getType())) {
2701 Importer.Imported(D, FoundIvar);
2702 return FoundIvar;
2703 }
2704
2705 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2706 << Name << D->getType() << FoundIvar->getType();
2707 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2708 << FoundIvar->getType();
2709 return 0;
2710 }
2711 }
2712
2713 // Import the type.
2714 QualType T = Importer.Import(D->getType());
2715 if (T.isNull())
2716 return 0;
2717
2718 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2719 Expr *BitWidth = Importer.Import(D->getBitWidth());
2720 if (!BitWidth && D->getBitWidth())
2721 return 0;
2722
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002723 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2724 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002725 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002726 Loc, Name.getAsIdentifierInfo(),
2727 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002728 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002729 ToIvar->setLexicalDeclContext(LexicalDC);
2730 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002731 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002732 return ToIvar;
2733
2734}
2735
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002736Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2737 // Import the major distinguishing characteristics of a variable.
2738 DeclContext *DC, *LexicalDC;
2739 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002740 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002741 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002742 return 0;
2743
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002744 // Try to find a variable in our own ("to") context with the same name and
2745 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002746 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002747 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002748 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002749 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002750 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2751 DC->localUncachedLookup(Name, FoundDecls);
2752 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2753 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002754 continue;
2755
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002756 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002757 // We have found a variable that we may need to merge with. Check it.
2758 if (isExternalLinkage(FoundVar->getLinkage()) &&
2759 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002760 if (Importer.IsStructurallyEquivalent(D->getType(),
2761 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002762 MergeWithVar = FoundVar;
2763 break;
2764 }
2765
Douglas Gregor56521c52010-02-12 17:23:39 +00002766 const ArrayType *FoundArray
2767 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2768 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002769 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002770 if (FoundArray && TArray) {
2771 if (isa<IncompleteArrayType>(FoundArray) &&
2772 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002773 // Import the type.
2774 QualType T = Importer.Import(D->getType());
2775 if (T.isNull())
2776 return 0;
2777
Douglas Gregor56521c52010-02-12 17:23:39 +00002778 FoundVar->setType(T);
2779 MergeWithVar = FoundVar;
2780 break;
2781 } else if (isa<IncompleteArrayType>(TArray) &&
2782 isa<ConstantArrayType>(FoundArray)) {
2783 MergeWithVar = FoundVar;
2784 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002785 }
2786 }
2787
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002788 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002789 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002790 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2791 << FoundVar->getType();
2792 }
2793 }
2794
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002795 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002796 }
2797
2798 if (MergeWithVar) {
2799 // An equivalent variable with external linkage has been found. Link
2800 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002801 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002802
2803 if (VarDecl *DDef = D->getDefinition()) {
2804 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2805 Importer.ToDiag(ExistingDef->getLocation(),
2806 diag::err_odr_variable_multiple_def)
2807 << Name;
2808 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2809 } else {
2810 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002811 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002812 if (DDef->isInitKnownICE()) {
2813 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2814 Eval->CheckedICE = true;
2815 Eval->IsICE = DDef->isInitICE();
2816 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002817 }
2818 }
2819
2820 return MergeWithVar;
2821 }
2822
2823 if (!ConflictingDecls.empty()) {
2824 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2825 ConflictingDecls.data(),
2826 ConflictingDecls.size());
2827 if (!Name)
2828 return 0;
2829 }
2830 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002831
Douglas Gregorb4964f72010-02-15 23:54:17 +00002832 // Import the type.
2833 QualType T = Importer.Import(D->getType());
2834 if (T.isNull())
2835 return 0;
2836
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002837 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002838 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002839 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2840 Importer.Import(D->getInnerLocStart()),
2841 Loc, Name.getAsIdentifierInfo(),
2842 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002843 D->getStorageClass(),
2844 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002845 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002846 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002847 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002848 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002849 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002850
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002851 // Merge the initializer.
2852 // FIXME: Can we really import any initializer? Alternatively, we could force
2853 // ourselves to import every declaration of a variable and then only use
2854 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002855 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002856
2857 // FIXME: Other bits to merge?
2858
2859 return ToVar;
2860}
2861
Douglas Gregor8b228d72010-02-17 21:22:52 +00002862Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2863 // Parameters are created in the translation unit's context, then moved
2864 // into the function declaration's context afterward.
2865 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2866
2867 // Import the name of this declaration.
2868 DeclarationName Name = Importer.Import(D->getDeclName());
2869 if (D->getDeclName() && !Name)
2870 return 0;
2871
2872 // Import the location of this declaration.
2873 SourceLocation Loc = Importer.Import(D->getLocation());
2874
2875 // Import the parameter's type.
2876 QualType T = Importer.Import(D->getType());
2877 if (T.isNull())
2878 return 0;
2879
2880 // Create the imported parameter.
2881 ImplicitParamDecl *ToParm
2882 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2883 Loc, Name.getAsIdentifierInfo(),
2884 T);
2885 return Importer.Imported(D, ToParm);
2886}
2887
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002888Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2889 // Parameters are created in the translation unit's context, then moved
2890 // into the function declaration's context afterward.
2891 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2892
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002893 // Import the name of this declaration.
2894 DeclarationName Name = Importer.Import(D->getDeclName());
2895 if (D->getDeclName() && !Name)
2896 return 0;
2897
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002898 // Import the location of this declaration.
2899 SourceLocation Loc = Importer.Import(D->getLocation());
2900
2901 // Import the parameter's type.
2902 QualType T = Importer.Import(D->getType());
2903 if (T.isNull())
2904 return 0;
2905
2906 // Create the imported parameter.
2907 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2908 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002909 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002910 Loc, Name.getAsIdentifierInfo(),
2911 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002912 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002913 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002914 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002915 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002916}
2917
Douglas Gregor43f54792010-02-17 02:12:47 +00002918Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2919 // Import the major distinguishing characteristics of a method.
2920 DeclContext *DC, *LexicalDC;
2921 DeclarationName Name;
2922 SourceLocation Loc;
2923 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2924 return 0;
2925
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002926 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2927 DC->localUncachedLookup(Name, FoundDecls);
2928 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2929 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002930 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2931 continue;
2932
2933 // Check return types.
2934 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2935 FoundMethod->getResultType())) {
2936 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2937 << D->isInstanceMethod() << Name
2938 << D->getResultType() << FoundMethod->getResultType();
2939 Importer.ToDiag(FoundMethod->getLocation(),
2940 diag::note_odr_objc_method_here)
2941 << D->isInstanceMethod() << Name;
2942 return 0;
2943 }
2944
2945 // Check the number of parameters.
2946 if (D->param_size() != FoundMethod->param_size()) {
2947 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2948 << D->isInstanceMethod() << Name
2949 << D->param_size() << FoundMethod->param_size();
2950 Importer.ToDiag(FoundMethod->getLocation(),
2951 diag::note_odr_objc_method_here)
2952 << D->isInstanceMethod() << Name;
2953 return 0;
2954 }
2955
2956 // Check parameter types.
2957 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2958 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2959 P != PEnd; ++P, ++FoundP) {
2960 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2961 (*FoundP)->getType())) {
2962 Importer.FromDiag((*P)->getLocation(),
2963 diag::err_odr_objc_method_param_type_inconsistent)
2964 << D->isInstanceMethod() << Name
2965 << (*P)->getType() << (*FoundP)->getType();
2966 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2967 << (*FoundP)->getType();
2968 return 0;
2969 }
2970 }
2971
2972 // Check variadic/non-variadic.
2973 // Check the number of parameters.
2974 if (D->isVariadic() != FoundMethod->isVariadic()) {
2975 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2976 << D->isInstanceMethod() << Name;
2977 Importer.ToDiag(FoundMethod->getLocation(),
2978 diag::note_odr_objc_method_here)
2979 << D->isInstanceMethod() << Name;
2980 return 0;
2981 }
2982
2983 // FIXME: Any other bits we need to merge?
2984 return Importer.Imported(D, FoundMethod);
2985 }
2986 }
2987
2988 // Import the result type.
2989 QualType ResultTy = Importer.Import(D->getResultType());
2990 if (ResultTy.isNull())
2991 return 0;
2992
Douglas Gregor12852d92010-03-08 14:59:44 +00002993 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2994
Douglas Gregor43f54792010-02-17 02:12:47 +00002995 ObjCMethodDecl *ToMethod
2996 = ObjCMethodDecl::Create(Importer.getToContext(),
2997 Loc,
2998 Importer.Import(D->getLocEnd()),
2999 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003000 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003001 D->isInstanceMethod(),
3002 D->isVariadic(),
3003 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003004 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003005 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003006 D->getImplementationControl(),
3007 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003008
3009 // FIXME: When we decide to merge method definitions, we'll need to
3010 // deal with implicit parameters.
3011
3012 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003013 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003014 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3015 FromPEnd = D->param_end();
3016 FromP != FromPEnd;
3017 ++FromP) {
3018 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3019 if (!ToP)
3020 return 0;
3021
3022 ToParams.push_back(ToP);
3023 }
3024
3025 // Set the parameters.
3026 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3027 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003028 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003029 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003030 SmallVector<SourceLocation, 12> SelLocs;
3031 D->getSelectorLocs(SelLocs);
3032 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003033
3034 ToMethod->setLexicalDeclContext(LexicalDC);
3035 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003036 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003037 return ToMethod;
3038}
3039
Douglas Gregor84c51c32010-02-18 01:47:50 +00003040Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3041 // Import the major distinguishing characteristics of a category.
3042 DeclContext *DC, *LexicalDC;
3043 DeclarationName Name;
3044 SourceLocation Loc;
3045 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3046 return 0;
3047
3048 ObjCInterfaceDecl *ToInterface
3049 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3050 if (!ToInterface)
3051 return 0;
3052
3053 // Determine if we've already encountered this category.
3054 ObjCCategoryDecl *MergeWithCategory
3055 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3056 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3057 if (!ToCategory) {
3058 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003059 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003060 Loc,
3061 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003062 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003063 ToInterface,
3064 Importer.Import(D->getIvarLBraceLoc()),
3065 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003066 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003067 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003068 Importer.Imported(D, ToCategory);
3069
Douglas Gregor84c51c32010-02-18 01:47:50 +00003070 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003071 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3072 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003073 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3074 = D->protocol_loc_begin();
3075 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3076 FromProtoEnd = D->protocol_end();
3077 FromProto != FromProtoEnd;
3078 ++FromProto, ++FromProtoLoc) {
3079 ObjCProtocolDecl *ToProto
3080 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3081 if (!ToProto)
3082 return 0;
3083 Protocols.push_back(ToProto);
3084 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3085 }
3086
3087 // FIXME: If we're merging, make sure that the protocol list is the same.
3088 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3089 ProtocolLocs.data(), Importer.getToContext());
3090
3091 } else {
3092 Importer.Imported(D, ToCategory);
3093 }
3094
3095 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003096 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003097
3098 // If we have an implementation, import it as well.
3099 if (D->getImplementation()) {
3100 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003101 = cast_or_null<ObjCCategoryImplDecl>(
3102 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003103 if (!Impl)
3104 return 0;
3105
3106 ToCategory->setImplementation(Impl);
3107 }
3108
3109 return ToCategory;
3110}
3111
Douglas Gregor2aa53772012-01-24 17:42:07 +00003112bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3113 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003114 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003115 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003116 if (shouldForceImportDeclContext(Kind))
3117 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003118 return false;
3119 }
3120
3121 // Start the protocol definition
3122 To->startDefinition();
3123
3124 // Import protocols
3125 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3126 SmallVector<SourceLocation, 4> ProtocolLocs;
3127 ObjCProtocolDecl::protocol_loc_iterator
3128 FromProtoLoc = From->protocol_loc_begin();
3129 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3130 FromProtoEnd = From->protocol_end();
3131 FromProto != FromProtoEnd;
3132 ++FromProto, ++FromProtoLoc) {
3133 ObjCProtocolDecl *ToProto
3134 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3135 if (!ToProto)
3136 return true;
3137 Protocols.push_back(ToProto);
3138 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3139 }
3140
3141 // FIXME: If we're merging, make sure that the protocol list is the same.
3142 To->setProtocolList(Protocols.data(), Protocols.size(),
3143 ProtocolLocs.data(), Importer.getToContext());
3144
Douglas Gregor2e15c842012-02-01 21:00:38 +00003145 if (shouldForceImportDeclContext(Kind)) {
3146 // Import all of the members of this protocol.
3147 ImportDeclContext(From, /*ForceImport=*/true);
3148 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003149 return false;
3150}
3151
Douglas Gregor98d156a2010-02-17 16:12:00 +00003152Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003153 // If this protocol has a definition in the translation unit we're coming
3154 // from, but this particular declaration is not that definition, import the
3155 // definition and map to that.
3156 ObjCProtocolDecl *Definition = D->getDefinition();
3157 if (Definition && Definition != D) {
3158 Decl *ImportedDef = Importer.Import(Definition);
3159 if (!ImportedDef)
3160 return 0;
3161
3162 return Importer.Imported(D, ImportedDef);
3163 }
3164
Douglas Gregor84c51c32010-02-18 01:47:50 +00003165 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003166 DeclContext *DC, *LexicalDC;
3167 DeclarationName Name;
3168 SourceLocation Loc;
3169 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3170 return 0;
3171
3172 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003173 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3174 DC->localUncachedLookup(Name, FoundDecls);
3175 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3176 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003177 continue;
3178
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003179 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003180 break;
3181 }
3182
3183 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003184 if (!ToProto) {
3185 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3186 Name.getAsIdentifierInfo(), Loc,
3187 Importer.Import(D->getAtStartLoc()),
3188 /*PrevDecl=*/0);
3189 ToProto->setLexicalDeclContext(LexicalDC);
3190 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003191 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003192
3193 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003194
Douglas Gregor2aa53772012-01-24 17:42:07 +00003195 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3196 return 0;
3197
Douglas Gregor98d156a2010-02-17 16:12:00 +00003198 return ToProto;
3199}
3200
Douglas Gregor2aa53772012-01-24 17:42:07 +00003201bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3202 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003203 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003204 if (To->getDefinition()) {
3205 // Check consistency of superclass.
3206 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3207 if (FromSuper) {
3208 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3209 if (!FromSuper)
3210 return true;
3211 }
3212
3213 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3214 if ((bool)FromSuper != (bool)ToSuper ||
3215 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3216 Importer.ToDiag(To->getLocation(),
3217 diag::err_odr_objc_superclass_inconsistent)
3218 << To->getDeclName();
3219 if (ToSuper)
3220 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3221 << To->getSuperClass()->getDeclName();
3222 else
3223 Importer.ToDiag(To->getLocation(),
3224 diag::note_odr_objc_missing_superclass);
3225 if (From->getSuperClass())
3226 Importer.FromDiag(From->getSuperClassLoc(),
3227 diag::note_odr_objc_superclass)
3228 << From->getSuperClass()->getDeclName();
3229 else
3230 Importer.FromDiag(From->getLocation(),
3231 diag::note_odr_objc_missing_superclass);
3232 }
3233
Douglas Gregor2e15c842012-02-01 21:00:38 +00003234 if (shouldForceImportDeclContext(Kind))
3235 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003236 return false;
3237 }
3238
3239 // Start the definition.
3240 To->startDefinition();
3241
3242 // If this class has a superclass, import it.
3243 if (From->getSuperClass()) {
3244 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3245 Importer.Import(From->getSuperClass()));
3246 if (!Super)
3247 return true;
3248
3249 To->setSuperClass(Super);
3250 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3251 }
3252
3253 // Import protocols
3254 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3255 SmallVector<SourceLocation, 4> ProtocolLocs;
3256 ObjCInterfaceDecl::protocol_loc_iterator
3257 FromProtoLoc = From->protocol_loc_begin();
3258
3259 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3260 FromProtoEnd = From->protocol_end();
3261 FromProto != FromProtoEnd;
3262 ++FromProto, ++FromProtoLoc) {
3263 ObjCProtocolDecl *ToProto
3264 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3265 if (!ToProto)
3266 return true;
3267 Protocols.push_back(ToProto);
3268 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3269 }
3270
3271 // FIXME: If we're merging, make sure that the protocol list is the same.
3272 To->setProtocolList(Protocols.data(), Protocols.size(),
3273 ProtocolLocs.data(), Importer.getToContext());
3274
3275 // Import categories. When the categories themselves are imported, they'll
3276 // hook themselves into this interface.
3277 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3278 FromCat = FromCat->getNextClassCategory())
3279 Importer.Import(FromCat);
3280
3281 // If we have an @implementation, import it as well.
3282 if (From->getImplementation()) {
3283 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3284 Importer.Import(From->getImplementation()));
3285 if (!Impl)
3286 return true;
3287
3288 To->setImplementation(Impl);
3289 }
3290
Douglas Gregor2e15c842012-02-01 21:00:38 +00003291 if (shouldForceImportDeclContext(Kind)) {
3292 // Import all of the members of this class.
3293 ImportDeclContext(From, /*ForceImport=*/true);
3294 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003295 return false;
3296}
3297
Douglas Gregor45635322010-02-16 01:20:57 +00003298Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003299 // If this class has a definition in the translation unit we're coming from,
3300 // but this particular declaration is not that definition, import the
3301 // definition and map to that.
3302 ObjCInterfaceDecl *Definition = D->getDefinition();
3303 if (Definition && Definition != D) {
3304 Decl *ImportedDef = Importer.Import(Definition);
3305 if (!ImportedDef)
3306 return 0;
3307
3308 return Importer.Imported(D, ImportedDef);
3309 }
3310
Douglas Gregor45635322010-02-16 01:20:57 +00003311 // Import the major distinguishing characteristics of an @interface.
3312 DeclContext *DC, *LexicalDC;
3313 DeclarationName Name;
3314 SourceLocation Loc;
3315 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3316 return 0;
3317
Douglas Gregor2aa53772012-01-24 17:42:07 +00003318 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003319 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003320 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3321 DC->localUncachedLookup(Name, FoundDecls);
3322 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3323 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003324 continue;
3325
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003326 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003327 break;
3328 }
3329
Douglas Gregor2aa53772012-01-24 17:42:07 +00003330 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003331 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003332 if (!ToIface) {
3333 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3334 Importer.Import(D->getAtStartLoc()),
3335 Name.getAsIdentifierInfo(),
3336 /*PrevDecl=*/0,Loc,
3337 D->isImplicitInterfaceDecl());
3338 ToIface->setLexicalDeclContext(LexicalDC);
3339 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003340 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003341 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003342
Douglas Gregor2aa53772012-01-24 17:42:07 +00003343 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3344 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003345
Douglas Gregor98d156a2010-02-17 16:12:00 +00003346 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003347}
3348
Douglas Gregor4da9d682010-12-07 15:32:12 +00003349Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3350 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3351 Importer.Import(D->getCategoryDecl()));
3352 if (!Category)
3353 return 0;
3354
3355 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3356 if (!ToImpl) {
3357 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3358 if (!DC)
3359 return 0;
3360
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003361 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003362 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003363 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003364 Category->getClassInterface(),
3365 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003366 Importer.Import(D->getAtStartLoc()),
3367 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003368
3369 DeclContext *LexicalDC = DC;
3370 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3371 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3372 if (!LexicalDC)
3373 return 0;
3374
3375 ToImpl->setLexicalDeclContext(LexicalDC);
3376 }
3377
Sean Callanan95e74be2011-10-21 02:57:43 +00003378 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003379 Category->setImplementation(ToImpl);
3380 }
3381
3382 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003383 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003384 return ToImpl;
3385}
3386
Douglas Gregorda8025c2010-12-07 01:26:03 +00003387Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3388 // Find the corresponding interface.
3389 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3390 Importer.Import(D->getClassInterface()));
3391 if (!Iface)
3392 return 0;
3393
3394 // Import the superclass, if any.
3395 ObjCInterfaceDecl *Super = 0;
3396 if (D->getSuperClass()) {
3397 Super = cast_or_null<ObjCInterfaceDecl>(
3398 Importer.Import(D->getSuperClass()));
3399 if (!Super)
3400 return 0;
3401 }
3402
3403 ObjCImplementationDecl *Impl = Iface->getImplementation();
3404 if (!Impl) {
3405 // We haven't imported an implementation yet. Create a new @implementation
3406 // now.
3407 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3408 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003409 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003410 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003411 Importer.Import(D->getAtStartLoc()),
3412 Importer.Import(D->getIvarLBraceLoc()),
3413 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003414
3415 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3416 DeclContext *LexicalDC
3417 = Importer.ImportContext(D->getLexicalDeclContext());
3418 if (!LexicalDC)
3419 return 0;
3420 Impl->setLexicalDeclContext(LexicalDC);
3421 }
3422
3423 // Associate the implementation with the class it implements.
3424 Iface->setImplementation(Impl);
3425 Importer.Imported(D, Iface->getImplementation());
3426 } else {
3427 Importer.Imported(D, Iface->getImplementation());
3428
3429 // Verify that the existing @implementation has the same superclass.
3430 if ((Super && !Impl->getSuperClass()) ||
3431 (!Super && Impl->getSuperClass()) ||
3432 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003433 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003434 Importer.ToDiag(Impl->getLocation(),
3435 diag::err_odr_objc_superclass_inconsistent)
3436 << Iface->getDeclName();
3437 // FIXME: It would be nice to have the location of the superclass
3438 // below.
3439 if (Impl->getSuperClass())
3440 Importer.ToDiag(Impl->getLocation(),
3441 diag::note_odr_objc_superclass)
3442 << Impl->getSuperClass()->getDeclName();
3443 else
3444 Importer.ToDiag(Impl->getLocation(),
3445 diag::note_odr_objc_missing_superclass);
3446 if (D->getSuperClass())
3447 Importer.FromDiag(D->getLocation(),
3448 diag::note_odr_objc_superclass)
3449 << D->getSuperClass()->getDeclName();
3450 else
3451 Importer.FromDiag(D->getLocation(),
3452 diag::note_odr_objc_missing_superclass);
3453 return 0;
3454 }
3455 }
3456
3457 // Import all of the members of this @implementation.
3458 ImportDeclContext(D);
3459
3460 return Impl;
3461}
3462
Douglas Gregora11c4582010-02-17 18:02:10 +00003463Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3464 // Import the major distinguishing characteristics of an @property.
3465 DeclContext *DC, *LexicalDC;
3466 DeclarationName Name;
3467 SourceLocation Loc;
3468 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3469 return 0;
3470
3471 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003472 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3473 DC->localUncachedLookup(Name, FoundDecls);
3474 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003475 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003476 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003477 // Check property types.
3478 if (!Importer.IsStructurallyEquivalent(D->getType(),
3479 FoundProp->getType())) {
3480 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3481 << Name << D->getType() << FoundProp->getType();
3482 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3483 << FoundProp->getType();
3484 return 0;
3485 }
3486
3487 // FIXME: Check property attributes, getters, setters, etc.?
3488
3489 // Consider these properties to be equivalent.
3490 Importer.Imported(D, FoundProp);
3491 return FoundProp;
3492 }
3493 }
3494
3495 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003496 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3497 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003498 return 0;
3499
3500 // Create the new property.
3501 ObjCPropertyDecl *ToProperty
3502 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3503 Name.getAsIdentifierInfo(),
3504 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003505 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003506 T,
3507 D->getPropertyImplementation());
3508 Importer.Imported(D, ToProperty);
3509 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003510 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003511
3512 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003513 ToProperty->setPropertyAttributesAsWritten(
3514 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003515 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3516 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3517 ToProperty->setGetterMethodDecl(
3518 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3519 ToProperty->setSetterMethodDecl(
3520 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3521 ToProperty->setPropertyIvarDecl(
3522 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3523 return ToProperty;
3524}
3525
Douglas Gregor14a49e22010-12-07 18:32:03 +00003526Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3527 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3528 Importer.Import(D->getPropertyDecl()));
3529 if (!Property)
3530 return 0;
3531
3532 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3533 if (!DC)
3534 return 0;
3535
3536 // Import the lexical declaration context.
3537 DeclContext *LexicalDC = DC;
3538 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3539 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3540 if (!LexicalDC)
3541 return 0;
3542 }
3543
3544 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3545 if (!InImpl)
3546 return 0;
3547
3548 // Import the ivar (for an @synthesize).
3549 ObjCIvarDecl *Ivar = 0;
3550 if (D->getPropertyIvarDecl()) {
3551 Ivar = cast_or_null<ObjCIvarDecl>(
3552 Importer.Import(D->getPropertyIvarDecl()));
3553 if (!Ivar)
3554 return 0;
3555 }
3556
3557 ObjCPropertyImplDecl *ToImpl
3558 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3559 if (!ToImpl) {
3560 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3561 Importer.Import(D->getLocStart()),
3562 Importer.Import(D->getLocation()),
3563 Property,
3564 D->getPropertyImplementation(),
3565 Ivar,
3566 Importer.Import(D->getPropertyIvarDeclLoc()));
3567 ToImpl->setLexicalDeclContext(LexicalDC);
3568 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003569 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003570 } else {
3571 // Check that we have the same kind of property implementation (@synthesize
3572 // vs. @dynamic).
3573 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3574 Importer.ToDiag(ToImpl->getLocation(),
3575 diag::err_odr_objc_property_impl_kind_inconsistent)
3576 << Property->getDeclName()
3577 << (ToImpl->getPropertyImplementation()
3578 == ObjCPropertyImplDecl::Dynamic);
3579 Importer.FromDiag(D->getLocation(),
3580 diag::note_odr_objc_property_impl_kind)
3581 << D->getPropertyDecl()->getDeclName()
3582 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3583 return 0;
3584 }
3585
3586 // For @synthesize, check that we have the same
3587 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3588 Ivar != ToImpl->getPropertyIvarDecl()) {
3589 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3590 diag::err_odr_objc_synthesize_ivar_inconsistent)
3591 << Property->getDeclName()
3592 << ToImpl->getPropertyIvarDecl()->getDeclName()
3593 << Ivar->getDeclName();
3594 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3595 diag::note_odr_objc_synthesize_ivar_here)
3596 << D->getPropertyIvarDecl()->getDeclName();
3597 return 0;
3598 }
3599
3600 // Merge the existing implementation with the new implementation.
3601 Importer.Imported(D, ToImpl);
3602 }
3603
3604 return ToImpl;
3605}
3606
Douglas Gregora082a492010-11-30 19:14:50 +00003607Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3608 // For template arguments, we adopt the translation unit as our declaration
3609 // context. This context will be fixed when the actual template declaration
3610 // is created.
3611
3612 // FIXME: Import default argument.
3613 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3614 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003615 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003616 Importer.Import(D->getLocation()),
3617 D->getDepth(),
3618 D->getIndex(),
3619 Importer.Import(D->getIdentifier()),
3620 D->wasDeclaredWithTypename(),
3621 D->isParameterPack());
3622}
3623
3624Decl *
3625ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3626 // Import the name of this declaration.
3627 DeclarationName Name = Importer.Import(D->getDeclName());
3628 if (D->getDeclName() && !Name)
3629 return 0;
3630
3631 // Import the location of this declaration.
3632 SourceLocation Loc = Importer.Import(D->getLocation());
3633
3634 // Import the type of this declaration.
3635 QualType T = Importer.Import(D->getType());
3636 if (T.isNull())
3637 return 0;
3638
3639 // Import type-source information.
3640 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3641 if (D->getTypeSourceInfo() && !TInfo)
3642 return 0;
3643
3644 // FIXME: Import default argument.
3645
3646 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3647 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003648 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003649 Loc, D->getDepth(), D->getPosition(),
3650 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003651 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003652}
3653
3654Decl *
3655ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3656 // Import the name of this declaration.
3657 DeclarationName Name = Importer.Import(D->getDeclName());
3658 if (D->getDeclName() && !Name)
3659 return 0;
3660
3661 // Import the location of this declaration.
3662 SourceLocation Loc = Importer.Import(D->getLocation());
3663
3664 // Import template parameters.
3665 TemplateParameterList *TemplateParams
3666 = ImportTemplateParameterList(D->getTemplateParameters());
3667 if (!TemplateParams)
3668 return 0;
3669
3670 // FIXME: Import default argument.
3671
3672 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3673 Importer.getToContext().getTranslationUnitDecl(),
3674 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003675 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003676 Name.getAsIdentifierInfo(),
3677 TemplateParams);
3678}
3679
3680Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3681 // If this record has a definition in the translation unit we're coming from,
3682 // but this particular declaration is not that definition, import the
3683 // definition and map to that.
3684 CXXRecordDecl *Definition
3685 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3686 if (Definition && Definition != D->getTemplatedDecl()) {
3687 Decl *ImportedDef
3688 = Importer.Import(Definition->getDescribedClassTemplate());
3689 if (!ImportedDef)
3690 return 0;
3691
3692 return Importer.Imported(D, ImportedDef);
3693 }
3694
3695 // Import the major distinguishing characteristics of this class template.
3696 DeclContext *DC, *LexicalDC;
3697 DeclarationName Name;
3698 SourceLocation Loc;
3699 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3700 return 0;
3701
3702 // We may already have a template of the same name; try to find and match it.
3703 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003704 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003705 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3706 DC->localUncachedLookup(Name, FoundDecls);
3707 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3708 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003709 continue;
3710
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003711 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003712 if (ClassTemplateDecl *FoundTemplate
3713 = dyn_cast<ClassTemplateDecl>(Found)) {
3714 if (IsStructuralMatch(D, FoundTemplate)) {
3715 // The class templates structurally match; call it the same template.
3716 // FIXME: We may be filling in a forward declaration here. Handle
3717 // this case!
3718 Importer.Imported(D->getTemplatedDecl(),
3719 FoundTemplate->getTemplatedDecl());
3720 return Importer.Imported(D, FoundTemplate);
3721 }
3722 }
3723
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003724 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003725 }
3726
3727 if (!ConflictingDecls.empty()) {
3728 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3729 ConflictingDecls.data(),
3730 ConflictingDecls.size());
3731 }
3732
3733 if (!Name)
3734 return 0;
3735 }
3736
3737 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3738
3739 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003740 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3741 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003742 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3743 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003744 DC, StartLoc, IdLoc,
3745 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003746 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003747 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003748 D2Templated->setLexicalDeclContext(LexicalDC);
3749
3750 // Create the class template declaration itself.
3751 TemplateParameterList *TemplateParams
3752 = ImportTemplateParameterList(D->getTemplateParameters());
3753 if (!TemplateParams)
3754 return 0;
3755
3756 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3757 Loc, Name, TemplateParams,
3758 D2Templated,
3759 /*PrevDecl=*/0);
3760 D2Templated->setDescribedClassTemplate(D2);
3761
3762 D2->setAccess(D->getAccess());
3763 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003764 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003765
3766 // Note the relationship between the class templates.
3767 Importer.Imported(D, D2);
3768 Importer.Imported(DTemplated, D2Templated);
3769
John McCallf937c022011-10-07 06:10:15 +00003770 if (DTemplated->isCompleteDefinition() &&
3771 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003772 // FIXME: Import definition!
3773 }
3774
3775 return D2;
3776}
3777
Douglas Gregore2e50d332010-12-01 01:36:18 +00003778Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3779 ClassTemplateSpecializationDecl *D) {
3780 // If this record has a definition in the translation unit we're coming from,
3781 // but this particular declaration is not that definition, import the
3782 // definition and map to that.
3783 TagDecl *Definition = D->getDefinition();
3784 if (Definition && Definition != D) {
3785 Decl *ImportedDef = Importer.Import(Definition);
3786 if (!ImportedDef)
3787 return 0;
3788
3789 return Importer.Imported(D, ImportedDef);
3790 }
3791
3792 ClassTemplateDecl *ClassTemplate
3793 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3794 D->getSpecializedTemplate()));
3795 if (!ClassTemplate)
3796 return 0;
3797
3798 // Import the context of this declaration.
3799 DeclContext *DC = ClassTemplate->getDeclContext();
3800 if (!DC)
3801 return 0;
3802
3803 DeclContext *LexicalDC = DC;
3804 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3805 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3806 if (!LexicalDC)
3807 return 0;
3808 }
3809
3810 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003811 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3812 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003813
3814 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003815 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003816 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3817 D->getTemplateArgs().size(),
3818 TemplateArgs))
3819 return 0;
3820
3821 // Try to find an existing specialization with these template arguments.
3822 void *InsertPos = 0;
3823 ClassTemplateSpecializationDecl *D2
3824 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3825 TemplateArgs.size(), InsertPos);
3826 if (D2) {
3827 // We already have a class template specialization with these template
3828 // arguments.
3829
3830 // FIXME: Check for specialization vs. instantiation errors.
3831
3832 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003833 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003834 // The record types structurally match, or the "from" translation
3835 // unit only had a forward declaration anyway; call it the same
3836 // function.
3837 return Importer.Imported(D, FoundDef);
3838 }
3839 }
3840 } else {
3841 // Create a new specialization.
3842 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3843 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003844 StartLoc, IdLoc,
3845 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003846 TemplateArgs.data(),
3847 TemplateArgs.size(),
3848 /*PrevDecl=*/0);
3849 D2->setSpecializationKind(D->getSpecializationKind());
3850
3851 // Add this specialization to the class template.
3852 ClassTemplate->AddSpecialization(D2, InsertPos);
3853
3854 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003855 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003856
3857 // Add the specialization to this context.
3858 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003859 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003860 }
3861 Importer.Imported(D, D2);
3862
John McCallf937c022011-10-07 06:10:15 +00003863 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003864 return 0;
3865
3866 return D2;
3867}
3868
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003869//----------------------------------------------------------------------------
3870// Import Statements
3871//----------------------------------------------------------------------------
3872
3873Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3874 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3875 << S->getStmtClassName();
3876 return 0;
3877}
3878
3879//----------------------------------------------------------------------------
3880// Import Expressions
3881//----------------------------------------------------------------------------
3882Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3883 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3884 << E->getStmtClassName();
3885 return 0;
3886}
3887
Douglas Gregor52f820e2010-02-19 01:17:02 +00003888Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003889 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3890 if (!ToD)
3891 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003892
3893 NamedDecl *FoundD = 0;
3894 if (E->getDecl() != E->getFoundDecl()) {
3895 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3896 if (!FoundD)
3897 return 0;
3898 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003899
3900 QualType T = Importer.Import(E->getType());
3901 if (T.isNull())
3902 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003903
3904 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3905 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003906 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003907 ToD,
John McCall113bee02012-03-10 09:33:50 +00003908 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003909 Importer.Import(E->getLocation()),
3910 T, E->getValueKind(),
3911 FoundD,
3912 /*FIXME:TemplateArgs=*/0);
3913 if (E->hadMultipleCandidates())
3914 DRE->setHadMultipleCandidates(true);
3915 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003916}
3917
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003918Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3919 QualType T = Importer.Import(E->getType());
3920 if (T.isNull())
3921 return 0;
3922
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003923 return IntegerLiteral::Create(Importer.getToContext(),
3924 E->getValue(), T,
3925 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003926}
3927
Douglas Gregor623421d2010-02-18 02:21:22 +00003928Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3929 QualType T = Importer.Import(E->getType());
3930 if (T.isNull())
3931 return 0;
3932
Douglas Gregorfb65e592011-07-27 05:40:30 +00003933 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3934 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00003935 Importer.Import(E->getLocation()));
3936}
3937
Douglas Gregorc74247e2010-02-19 01:07:06 +00003938Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3939 Expr *SubExpr = Importer.Import(E->getSubExpr());
3940 if (!SubExpr)
3941 return 0;
3942
3943 return new (Importer.getToContext())
3944 ParenExpr(Importer.Import(E->getLParen()),
3945 Importer.Import(E->getRParen()),
3946 SubExpr);
3947}
3948
3949Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3950 QualType T = Importer.Import(E->getType());
3951 if (T.isNull())
3952 return 0;
3953
3954 Expr *SubExpr = Importer.Import(E->getSubExpr());
3955 if (!SubExpr)
3956 return 0;
3957
3958 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003959 T, E->getValueKind(),
3960 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003961 Importer.Import(E->getOperatorLoc()));
3962}
3963
Peter Collingbournee190dee2011-03-11 19:24:49 +00003964Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3965 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00003966 QualType ResultType = Importer.Import(E->getType());
3967
3968 if (E->isArgumentType()) {
3969 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3970 if (!TInfo)
3971 return 0;
3972
Peter Collingbournee190dee2011-03-11 19:24:49 +00003973 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3974 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003975 Importer.Import(E->getOperatorLoc()),
3976 Importer.Import(E->getRParenLoc()));
3977 }
3978
3979 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3980 if (!SubExpr)
3981 return 0;
3982
Peter Collingbournee190dee2011-03-11 19:24:49 +00003983 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3984 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003985 Importer.Import(E->getOperatorLoc()),
3986 Importer.Import(E->getRParenLoc()));
3987}
3988
Douglas Gregorc74247e2010-02-19 01:07:06 +00003989Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3990 QualType T = Importer.Import(E->getType());
3991 if (T.isNull())
3992 return 0;
3993
3994 Expr *LHS = Importer.Import(E->getLHS());
3995 if (!LHS)
3996 return 0;
3997
3998 Expr *RHS = Importer.Import(E->getRHS());
3999 if (!RHS)
4000 return 0;
4001
4002 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004003 T, E->getValueKind(),
4004 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004005 Importer.Import(E->getOperatorLoc()));
4006}
4007
4008Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4009 QualType T = Importer.Import(E->getType());
4010 if (T.isNull())
4011 return 0;
4012
4013 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4014 if (CompLHSType.isNull())
4015 return 0;
4016
4017 QualType CompResultType = Importer.Import(E->getComputationResultType());
4018 if (CompResultType.isNull())
4019 return 0;
4020
4021 Expr *LHS = Importer.Import(E->getLHS());
4022 if (!LHS)
4023 return 0;
4024
4025 Expr *RHS = Importer.Import(E->getRHS());
4026 if (!RHS)
4027 return 0;
4028
4029 return new (Importer.getToContext())
4030 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004031 T, E->getValueKind(),
4032 E->getObjectKind(),
4033 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004034 Importer.Import(E->getOperatorLoc()));
4035}
4036
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004037static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004038 if (E->path_empty()) return false;
4039
4040 // TODO: import cast paths
4041 return true;
4042}
4043
Douglas Gregor98c10182010-02-12 22:17:39 +00004044Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4045 QualType T = Importer.Import(E->getType());
4046 if (T.isNull())
4047 return 0;
4048
4049 Expr *SubExpr = Importer.Import(E->getSubExpr());
4050 if (!SubExpr)
4051 return 0;
John McCallcf142162010-08-07 06:22:56 +00004052
4053 CXXCastPath BasePath;
4054 if (ImportCastPath(E, BasePath))
4055 return 0;
4056
4057 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004058 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004059}
4060
Douglas Gregor5481d322010-02-19 01:32:14 +00004061Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4062 QualType T = Importer.Import(E->getType());
4063 if (T.isNull())
4064 return 0;
4065
4066 Expr *SubExpr = Importer.Import(E->getSubExpr());
4067 if (!SubExpr)
4068 return 0;
4069
4070 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4071 if (!TInfo && E->getTypeInfoAsWritten())
4072 return 0;
4073
John McCallcf142162010-08-07 06:22:56 +00004074 CXXCastPath BasePath;
4075 if (ImportCastPath(E, BasePath))
4076 return 0;
4077
John McCall7decc9e2010-11-18 06:31:45 +00004078 return CStyleCastExpr::Create(Importer.getToContext(), T,
4079 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004080 SubExpr, &BasePath, TInfo,
4081 Importer.Import(E->getLParenLoc()),
4082 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004083}
4084
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004085ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004086 ASTContext &FromContext, FileManager &FromFileManager,
4087 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004088 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004089 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4090 Minimal(MinimalImport)
4091{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004092 ImportedDecls[FromContext.getTranslationUnitDecl()]
4093 = ToContext.getTranslationUnitDecl();
4094}
4095
4096ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004097
4098QualType ASTImporter::Import(QualType FromT) {
4099 if (FromT.isNull())
4100 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004101
4102 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004103
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004104 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004105 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4106 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004107 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004108 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004109
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004110 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004111 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004112 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004113 if (ToT.isNull())
4114 return ToT;
4115
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004116 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004117 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004118
John McCall424cec92011-01-19 06:33:43 +00004119 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004120}
4121
Douglas Gregor62d311f2010-02-09 19:21:46 +00004122TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004123 if (!FromTSI)
4124 return FromTSI;
4125
4126 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004127 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004128 QualType T = Import(FromTSI->getType());
4129 if (T.isNull())
4130 return 0;
4131
4132 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004133 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004134}
4135
4136Decl *ASTImporter::Import(Decl *FromD) {
4137 if (!FromD)
4138 return 0;
4139
Douglas Gregord451ea92011-07-29 23:31:30 +00004140 ASTNodeImporter Importer(*this);
4141
Douglas Gregor62d311f2010-02-09 19:21:46 +00004142 // Check whether we've already imported this declaration.
4143 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004144 if (Pos != ImportedDecls.end()) {
4145 Decl *ToD = Pos->second;
4146 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4147 return ToD;
4148 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004149
4150 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004151 Decl *ToD = Importer.Visit(FromD);
4152 if (!ToD)
4153 return 0;
4154
4155 // Record the imported declaration.
4156 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004157
4158 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4159 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004160 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004161 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004162 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004163 // When we've finished transforming a typedef, see whether it was the
4164 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004165 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004166 FromTag = AnonTagsWithPendingTypedefs.begin(),
4167 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4168 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004169 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004170 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4171 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004172 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004173 AnonTagsWithPendingTypedefs.erase(FromTag);
4174 break;
4175 }
4176 }
4177 }
4178 }
4179
Douglas Gregor62d311f2010-02-09 19:21:46 +00004180 return ToD;
4181}
4182
4183DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4184 if (!FromDC)
4185 return FromDC;
4186
Douglas Gregor95d82832012-01-24 18:36:04 +00004187 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004188 if (!ToDC)
4189 return 0;
4190
4191 // When we're using a record/enum/Objective-C class/protocol as a context, we
4192 // need it to have a definition.
4193 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004194 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004195 if (ToRecord->isCompleteDefinition()) {
4196 // Do nothing.
4197 } else if (FromRecord->isCompleteDefinition()) {
4198 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4199 ASTNodeImporter::IDK_Basic);
4200 } else {
4201 CompleteDecl(ToRecord);
4202 }
4203 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4204 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4205 if (ToEnum->isCompleteDefinition()) {
4206 // Do nothing.
4207 } else if (FromEnum->isCompleteDefinition()) {
4208 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4209 ASTNodeImporter::IDK_Basic);
4210 } else {
4211 CompleteDecl(ToEnum);
4212 }
4213 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4214 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4215 if (ToClass->getDefinition()) {
4216 // Do nothing.
4217 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4218 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4219 ASTNodeImporter::IDK_Basic);
4220 } else {
4221 CompleteDecl(ToClass);
4222 }
4223 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4224 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4225 if (ToProto->getDefinition()) {
4226 // Do nothing.
4227 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4228 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4229 ASTNodeImporter::IDK_Basic);
4230 } else {
4231 CompleteDecl(ToProto);
4232 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004233 }
4234
4235 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004236}
4237
4238Expr *ASTImporter::Import(Expr *FromE) {
4239 if (!FromE)
4240 return 0;
4241
4242 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4243}
4244
4245Stmt *ASTImporter::Import(Stmt *FromS) {
4246 if (!FromS)
4247 return 0;
4248
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004249 // Check whether we've already imported this declaration.
4250 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4251 if (Pos != ImportedStmts.end())
4252 return Pos->second;
4253
4254 // Import the type
4255 ASTNodeImporter Importer(*this);
4256 Stmt *ToS = Importer.Visit(FromS);
4257 if (!ToS)
4258 return 0;
4259
4260 // Record the imported declaration.
4261 ImportedStmts[FromS] = ToS;
4262 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004263}
4264
4265NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4266 if (!FromNNS)
4267 return 0;
4268
Douglas Gregor90ebf252011-04-27 16:48:40 +00004269 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4270
4271 switch (FromNNS->getKind()) {
4272 case NestedNameSpecifier::Identifier:
4273 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4274 return NestedNameSpecifier::Create(ToContext, prefix, II);
4275 }
4276 return 0;
4277
4278 case NestedNameSpecifier::Namespace:
4279 if (NamespaceDecl *NS =
4280 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4281 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4282 }
4283 return 0;
4284
4285 case NestedNameSpecifier::NamespaceAlias:
4286 if (NamespaceAliasDecl *NSAD =
4287 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4288 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4289 }
4290 return 0;
4291
4292 case NestedNameSpecifier::Global:
4293 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4294
4295 case NestedNameSpecifier::TypeSpec:
4296 case NestedNameSpecifier::TypeSpecWithTemplate: {
4297 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4298 if (!T.isNull()) {
4299 bool bTemplate = FromNNS->getKind() ==
4300 NestedNameSpecifier::TypeSpecWithTemplate;
4301 return NestedNameSpecifier::Create(ToContext, prefix,
4302 bTemplate, T.getTypePtr());
4303 }
4304 }
4305 return 0;
4306 }
4307
4308 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004309}
4310
Douglas Gregor14454802011-02-25 02:25:35 +00004311NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4312 // FIXME: Implement!
4313 return NestedNameSpecifierLoc();
4314}
4315
Douglas Gregore2e50d332010-12-01 01:36:18 +00004316TemplateName ASTImporter::Import(TemplateName From) {
4317 switch (From.getKind()) {
4318 case TemplateName::Template:
4319 if (TemplateDecl *ToTemplate
4320 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4321 return TemplateName(ToTemplate);
4322
4323 return TemplateName();
4324
4325 case TemplateName::OverloadedTemplate: {
4326 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4327 UnresolvedSet<2> ToTemplates;
4328 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4329 E = FromStorage->end();
4330 I != E; ++I) {
4331 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4332 ToTemplates.addDecl(To);
4333 else
4334 return TemplateName();
4335 }
4336 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4337 ToTemplates.end());
4338 }
4339
4340 case TemplateName::QualifiedTemplate: {
4341 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4342 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4343 if (!Qualifier)
4344 return TemplateName();
4345
4346 if (TemplateDecl *ToTemplate
4347 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4348 return ToContext.getQualifiedTemplateName(Qualifier,
4349 QTN->hasTemplateKeyword(),
4350 ToTemplate);
4351
4352 return TemplateName();
4353 }
4354
4355 case TemplateName::DependentTemplate: {
4356 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4357 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4358 if (!Qualifier)
4359 return TemplateName();
4360
4361 if (DTN->isIdentifier()) {
4362 return ToContext.getDependentTemplateName(Qualifier,
4363 Import(DTN->getIdentifier()));
4364 }
4365
4366 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4367 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004368
4369 case TemplateName::SubstTemplateTemplateParm: {
4370 SubstTemplateTemplateParmStorage *subst
4371 = From.getAsSubstTemplateTemplateParm();
4372 TemplateTemplateParmDecl *param
4373 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4374 if (!param)
4375 return TemplateName();
4376
4377 TemplateName replacement = Import(subst->getReplacement());
4378 if (replacement.isNull()) return TemplateName();
4379
4380 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4381 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004382
4383 case TemplateName::SubstTemplateTemplateParmPack: {
4384 SubstTemplateTemplateParmPackStorage *SubstPack
4385 = From.getAsSubstTemplateTemplateParmPack();
4386 TemplateTemplateParmDecl *Param
4387 = cast_or_null<TemplateTemplateParmDecl>(
4388 Import(SubstPack->getParameterPack()));
4389 if (!Param)
4390 return TemplateName();
4391
4392 ASTNodeImporter Importer(*this);
4393 TemplateArgument ArgPack
4394 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4395 if (ArgPack.isNull())
4396 return TemplateName();
4397
4398 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4399 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004400 }
4401
4402 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004403}
4404
Douglas Gregor62d311f2010-02-09 19:21:46 +00004405SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4406 if (FromLoc.isInvalid())
4407 return SourceLocation();
4408
Douglas Gregor811663e2010-02-10 00:15:17 +00004409 SourceManager &FromSM = FromContext.getSourceManager();
4410
4411 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004412 // don't have to import macro expansions.
4413 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004414 FromLoc = FromSM.getSpellingLoc(FromLoc);
4415 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4416 SourceManager &ToSM = ToContext.getSourceManager();
4417 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004418 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004419}
4420
4421SourceRange ASTImporter::Import(SourceRange FromRange) {
4422 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4423}
4424
Douglas Gregor811663e2010-02-10 00:15:17 +00004425FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004426 llvm::DenseMap<FileID, FileID>::iterator Pos
4427 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004428 if (Pos != ImportedFileIDs.end())
4429 return Pos->second;
4430
4431 SourceManager &FromSM = FromContext.getSourceManager();
4432 SourceManager &ToSM = ToContext.getSourceManager();
4433 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004434 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004435
4436 // Include location of this file.
4437 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4438
4439 // Map the FileID for to the "to" source manager.
4440 FileID ToID;
4441 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004442 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004443 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4444 // disk again
4445 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4446 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004447 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004448 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4449 FromSLoc.getFile().getFileCharacteristic());
4450 } else {
4451 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004452 const llvm::MemoryBuffer *
4453 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004454 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004455 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004456 FromBuf->getBufferIdentifier());
4457 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4458 }
4459
4460
Sebastian Redl99219f12010-09-30 01:03:06 +00004461 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004462 return ToID;
4463}
4464
Douglas Gregor0a791672011-01-18 03:11:38 +00004465void ASTImporter::ImportDefinition(Decl *From) {
4466 Decl *To = Import(From);
4467 if (!To)
4468 return;
4469
4470 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4471 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004472
4473 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4474 if (!ToRecord->getDefinition()) {
4475 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004476 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004477 return;
4478 }
4479 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004480
4481 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4482 if (!ToEnum->getDefinition()) {
4483 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004484 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004485 return;
4486 }
4487 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004488
4489 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4490 if (!ToIFace->getDefinition()) {
4491 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004492 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004493 return;
4494 }
4495 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004496
Douglas Gregor2aa53772012-01-24 17:42:07 +00004497 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4498 if (!ToProto->getDefinition()) {
4499 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004500 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004501 return;
4502 }
4503 }
4504
Douglas Gregor0a791672011-01-18 03:11:38 +00004505 Importer.ImportDeclContext(FromDC, true);
4506 }
4507}
4508
Douglas Gregor96e578d2010-02-05 17:54:41 +00004509DeclarationName ASTImporter::Import(DeclarationName FromName) {
4510 if (!FromName)
4511 return DeclarationName();
4512
4513 switch (FromName.getNameKind()) {
4514 case DeclarationName::Identifier:
4515 return Import(FromName.getAsIdentifierInfo());
4516
4517 case DeclarationName::ObjCZeroArgSelector:
4518 case DeclarationName::ObjCOneArgSelector:
4519 case DeclarationName::ObjCMultiArgSelector:
4520 return Import(FromName.getObjCSelector());
4521
4522 case DeclarationName::CXXConstructorName: {
4523 QualType T = Import(FromName.getCXXNameType());
4524 if (T.isNull())
4525 return DeclarationName();
4526
4527 return ToContext.DeclarationNames.getCXXConstructorName(
4528 ToContext.getCanonicalType(T));
4529 }
4530
4531 case DeclarationName::CXXDestructorName: {
4532 QualType T = Import(FromName.getCXXNameType());
4533 if (T.isNull())
4534 return DeclarationName();
4535
4536 return ToContext.DeclarationNames.getCXXDestructorName(
4537 ToContext.getCanonicalType(T));
4538 }
4539
4540 case DeclarationName::CXXConversionFunctionName: {
4541 QualType T = Import(FromName.getCXXNameType());
4542 if (T.isNull())
4543 return DeclarationName();
4544
4545 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4546 ToContext.getCanonicalType(T));
4547 }
4548
4549 case DeclarationName::CXXOperatorName:
4550 return ToContext.DeclarationNames.getCXXOperatorName(
4551 FromName.getCXXOverloadedOperator());
4552
4553 case DeclarationName::CXXLiteralOperatorName:
4554 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4555 Import(FromName.getCXXLiteralIdentifier()));
4556
4557 case DeclarationName::CXXUsingDirective:
4558 // FIXME: STATICS!
4559 return DeclarationName::getUsingDirectiveName();
4560 }
4561
David Blaikiee4d798f2012-01-20 21:50:17 +00004562 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004563}
4564
Douglas Gregore2e50d332010-12-01 01:36:18 +00004565IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004566 if (!FromId)
4567 return 0;
4568
4569 return &ToContext.Idents.get(FromId->getName());
4570}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004571
Douglas Gregor43f54792010-02-17 02:12:47 +00004572Selector ASTImporter::Import(Selector FromSel) {
4573 if (FromSel.isNull())
4574 return Selector();
4575
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004576 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004577 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4578 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4579 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4580 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4581}
4582
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004583DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4584 DeclContext *DC,
4585 unsigned IDNS,
4586 NamedDecl **Decls,
4587 unsigned NumDecls) {
4588 return Name;
4589}
4590
4591DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004592 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004593}
4594
4595DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004596 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004597}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004598
Douglas Gregor2e15c842012-02-01 21:00:38 +00004599void ASTImporter::CompleteDecl (Decl *D) {
4600 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4601 if (!ID->getDefinition())
4602 ID->startDefinition();
4603 }
4604 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4605 if (!PD->getDefinition())
4606 PD->startDefinition();
4607 }
4608 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4609 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4610 TD->startDefinition();
4611 TD->setCompleteDefinition(true);
4612 }
4613 }
4614 else {
4615 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4616 }
4617}
4618
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004619Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4620 ImportedDecls[From] = To;
4621 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004622}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004623
4624bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004625 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004626 = ImportedTypes.find(From.getTypePtr());
4627 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4628 return true;
4629
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004630 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004631 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004632}