blob: bc139207cea44fee837082ae91270111cda08c95 [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
240/// \brief Determine if two APInts have the same value, after zero-extending
241/// one of them (if needed!) to ensure that the bit-widths match.
242static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
243 if (I1.getBitWidth() == I2.getBitWidth())
244 return I1 == I2;
245
246 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000247 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor3996e242010-02-15 22:01:00 +0000248
Jay Foad6d4db0c2010-12-07 08:25:34 +0000249 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor3996e242010-02-15 22:01:00 +0000250}
251
252/// \brief Determine if two APSInts have the same value, zero- or sign-extending
253/// as needed.
254static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
255 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
256 return I1 == I2;
257
258 // Check for a bit-width mismatch.
259 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000260 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor3996e242010-02-15 22:01:00 +0000261 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000262 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000263
264 // We have a signedness mismatch. Turn the signed value into an unsigned
265 // value.
266 if (I1.isSigned()) {
267 if (I1.isNegative())
268 return false;
269
270 return llvm::APSInt(I1, true) == I2;
271 }
272
273 if (I2.isNegative())
274 return false;
275
276 return I1 == llvm::APSInt(I2, true);
277}
278
279/// \brief Determine structural equivalence of two expressions.
280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
281 Expr *E1, Expr *E2) {
282 if (!E1 || !E2)
283 return E1 == E2;
284
285 // FIXME: Actually perform a structural comparison!
286 return true;
287}
288
289/// \brief Determine whether two identifiers are equivalent.
290static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
291 const IdentifierInfo *Name2) {
292 if (!Name1 || !Name2)
293 return Name1 == Name2;
294
295 return Name1->getName() == Name2->getName();
296}
297
298/// \brief Determine whether two nested-name-specifiers are equivalent.
299static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
300 NestedNameSpecifier *NNS1,
301 NestedNameSpecifier *NNS2) {
302 // FIXME: Implement!
303 return true;
304}
305
306/// \brief Determine whether two template arguments are equivalent.
307static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
308 const TemplateArgument &Arg1,
309 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000310 if (Arg1.getKind() != Arg2.getKind())
311 return false;
312
313 switch (Arg1.getKind()) {
314 case TemplateArgument::Null:
315 return true;
316
317 case TemplateArgument::Type:
318 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
319
320 case TemplateArgument::Integral:
321 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
322 Arg2.getIntegralType()))
323 return false;
324
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000325 return IsSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000326
327 case TemplateArgument::Declaration:
Douglas Gregor31f55dc2012-04-06 22:40:38 +0000328 if (!Arg1.getAsDecl() || !Arg2.getAsDecl())
329 return !Arg1.getAsDecl() && !Arg2.getAsDecl();
Douglas Gregore2e50d332010-12-01 01:36:18 +0000330 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
331
332 case TemplateArgument::Template:
333 return IsStructurallyEquivalent(Context,
334 Arg1.getAsTemplate(),
335 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000336
337 case TemplateArgument::TemplateExpansion:
338 return IsStructurallyEquivalent(Context,
339 Arg1.getAsTemplateOrTemplatePattern(),
340 Arg2.getAsTemplateOrTemplatePattern());
341
Douglas Gregore2e50d332010-12-01 01:36:18 +0000342 case TemplateArgument::Expression:
343 return IsStructurallyEquivalent(Context,
344 Arg1.getAsExpr(), Arg2.getAsExpr());
345
346 case TemplateArgument::Pack:
347 if (Arg1.pack_size() != Arg2.pack_size())
348 return false;
349
350 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
351 if (!IsStructurallyEquivalent(Context,
352 Arg1.pack_begin()[I],
353 Arg2.pack_begin()[I]))
354 return false;
355
356 return true;
357 }
358
359 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000360}
361
362/// \brief Determine structural equivalence for the common part of array
363/// types.
364static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
365 const ArrayType *Array1,
366 const ArrayType *Array2) {
367 if (!IsStructurallyEquivalent(Context,
368 Array1->getElementType(),
369 Array2->getElementType()))
370 return false;
371 if (Array1->getSizeModifier() != Array2->getSizeModifier())
372 return false;
373 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
374 return false;
375
376 return true;
377}
378
379/// \brief Determine structural equivalence of two types.
380static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
381 QualType T1, QualType T2) {
382 if (T1.isNull() || T2.isNull())
383 return T1.isNull() && T2.isNull();
384
385 if (!Context.StrictTypeSpelling) {
386 // We aren't being strict about token-to-token equivalence of types,
387 // so map down to the canonical type.
388 T1 = Context.C1.getCanonicalType(T1);
389 T2 = Context.C2.getCanonicalType(T2);
390 }
391
392 if (T1.getQualifiers() != T2.getQualifiers())
393 return false;
394
Douglas Gregorb4964f72010-02-15 23:54:17 +0000395 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000396
Douglas Gregorb4964f72010-02-15 23:54:17 +0000397 if (T1->getTypeClass() != T2->getTypeClass()) {
398 // Compare function types with prototypes vs. without prototypes as if
399 // both did not have prototypes.
400 if (T1->getTypeClass() == Type::FunctionProto &&
401 T2->getTypeClass() == Type::FunctionNoProto)
402 TC = Type::FunctionNoProto;
403 else if (T1->getTypeClass() == Type::FunctionNoProto &&
404 T2->getTypeClass() == Type::FunctionProto)
405 TC = Type::FunctionNoProto;
406 else
407 return false;
408 }
409
410 switch (TC) {
411 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000412 // FIXME: Deal with Char_S/Char_U.
413 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
414 return false;
415 break;
416
417 case Type::Complex:
418 if (!IsStructurallyEquivalent(Context,
419 cast<ComplexType>(T1)->getElementType(),
420 cast<ComplexType>(T2)->getElementType()))
421 return false;
422 break;
423
424 case Type::Pointer:
425 if (!IsStructurallyEquivalent(Context,
426 cast<PointerType>(T1)->getPointeeType(),
427 cast<PointerType>(T2)->getPointeeType()))
428 return false;
429 break;
430
431 case Type::BlockPointer:
432 if (!IsStructurallyEquivalent(Context,
433 cast<BlockPointerType>(T1)->getPointeeType(),
434 cast<BlockPointerType>(T2)->getPointeeType()))
435 return false;
436 break;
437
438 case Type::LValueReference:
439 case Type::RValueReference: {
440 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
441 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
442 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
443 return false;
444 if (Ref1->isInnerRef() != Ref2->isInnerRef())
445 return false;
446 if (!IsStructurallyEquivalent(Context,
447 Ref1->getPointeeTypeAsWritten(),
448 Ref2->getPointeeTypeAsWritten()))
449 return false;
450 break;
451 }
452
453 case Type::MemberPointer: {
454 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
455 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
456 if (!IsStructurallyEquivalent(Context,
457 MemPtr1->getPointeeType(),
458 MemPtr2->getPointeeType()))
459 return false;
460 if (!IsStructurallyEquivalent(Context,
461 QualType(MemPtr1->getClass(), 0),
462 QualType(MemPtr2->getClass(), 0)))
463 return false;
464 break;
465 }
466
467 case Type::ConstantArray: {
468 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
469 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
470 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
471 return false;
472
473 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
474 return false;
475 break;
476 }
477
478 case Type::IncompleteArray:
479 if (!IsArrayStructurallyEquivalent(Context,
480 cast<ArrayType>(T1),
481 cast<ArrayType>(T2)))
482 return false;
483 break;
484
485 case Type::VariableArray: {
486 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
487 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
488 if (!IsStructurallyEquivalent(Context,
489 Array1->getSizeExpr(), Array2->getSizeExpr()))
490 return false;
491
492 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
493 return false;
494
495 break;
496 }
497
498 case Type::DependentSizedArray: {
499 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
500 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
501 if (!IsStructurallyEquivalent(Context,
502 Array1->getSizeExpr(), Array2->getSizeExpr()))
503 return false;
504
505 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
506 return false;
507
508 break;
509 }
510
511 case Type::DependentSizedExtVector: {
512 const DependentSizedExtVectorType *Vec1
513 = cast<DependentSizedExtVectorType>(T1);
514 const DependentSizedExtVectorType *Vec2
515 = cast<DependentSizedExtVectorType>(T2);
516 if (!IsStructurallyEquivalent(Context,
517 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
518 return false;
519 if (!IsStructurallyEquivalent(Context,
520 Vec1->getElementType(),
521 Vec2->getElementType()))
522 return false;
523 break;
524 }
525
526 case Type::Vector:
527 case Type::ExtVector: {
528 const VectorType *Vec1 = cast<VectorType>(T1);
529 const VectorType *Vec2 = cast<VectorType>(T2);
530 if (!IsStructurallyEquivalent(Context,
531 Vec1->getElementType(),
532 Vec2->getElementType()))
533 return false;
534 if (Vec1->getNumElements() != Vec2->getNumElements())
535 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000536 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000537 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000538 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 }
540
541 case Type::FunctionProto: {
542 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
543 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
544 if (Proto1->getNumArgs() != Proto2->getNumArgs())
545 return false;
546 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
547 if (!IsStructurallyEquivalent(Context,
548 Proto1->getArgType(I),
549 Proto2->getArgType(I)))
550 return false;
551 }
552 if (Proto1->isVariadic() != Proto2->isVariadic())
553 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000554 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000555 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000556 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
557 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
558 return false;
559 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
560 if (!IsStructurallyEquivalent(Context,
561 Proto1->getExceptionType(I),
562 Proto2->getExceptionType(I)))
563 return false;
564 }
565 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000566 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000567 Proto1->getNoexceptExpr(),
568 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000569 return false;
570 }
571 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
572 return false;
573
574 // Fall through to check the bits common with FunctionNoProtoType.
575 }
576
577 case Type::FunctionNoProto: {
578 const FunctionType *Function1 = cast<FunctionType>(T1);
579 const FunctionType *Function2 = cast<FunctionType>(T2);
580 if (!IsStructurallyEquivalent(Context,
581 Function1->getResultType(),
582 Function2->getResultType()))
583 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000584 if (Function1->getExtInfo() != Function2->getExtInfo())
585 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000586 break;
587 }
588
589 case Type::UnresolvedUsing:
590 if (!IsStructurallyEquivalent(Context,
591 cast<UnresolvedUsingType>(T1)->getDecl(),
592 cast<UnresolvedUsingType>(T2)->getDecl()))
593 return false;
594
595 break;
John McCall81904512011-01-06 01:58:22 +0000596
597 case Type::Attributed:
598 if (!IsStructurallyEquivalent(Context,
599 cast<AttributedType>(T1)->getModifiedType(),
600 cast<AttributedType>(T2)->getModifiedType()))
601 return false;
602 if (!IsStructurallyEquivalent(Context,
603 cast<AttributedType>(T1)->getEquivalentType(),
604 cast<AttributedType>(T2)->getEquivalentType()))
605 return false;
606 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000607
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000608 case Type::Paren:
609 if (!IsStructurallyEquivalent(Context,
610 cast<ParenType>(T1)->getInnerType(),
611 cast<ParenType>(T2)->getInnerType()))
612 return false;
613 break;
614
Douglas Gregor3996e242010-02-15 22:01:00 +0000615 case Type::Typedef:
616 if (!IsStructurallyEquivalent(Context,
617 cast<TypedefType>(T1)->getDecl(),
618 cast<TypedefType>(T2)->getDecl()))
619 return false;
620 break;
621
622 case Type::TypeOfExpr:
623 if (!IsStructurallyEquivalent(Context,
624 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
625 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
626 return false;
627 break;
628
629 case Type::TypeOf:
630 if (!IsStructurallyEquivalent(Context,
631 cast<TypeOfType>(T1)->getUnderlyingType(),
632 cast<TypeOfType>(T2)->getUnderlyingType()))
633 return false;
634 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000635
636 case Type::UnaryTransform:
637 if (!IsStructurallyEquivalent(Context,
638 cast<UnaryTransformType>(T1)->getUnderlyingType(),
639 cast<UnaryTransformType>(T1)->getUnderlyingType()))
640 return false;
641 break;
642
Douglas Gregor3996e242010-02-15 22:01:00 +0000643 case Type::Decltype:
644 if (!IsStructurallyEquivalent(Context,
645 cast<DecltypeType>(T1)->getUnderlyingExpr(),
646 cast<DecltypeType>(T2)->getUnderlyingExpr()))
647 return false;
648 break;
649
Richard Smith30482bc2011-02-20 03:19:35 +0000650 case Type::Auto:
651 if (!IsStructurallyEquivalent(Context,
652 cast<AutoType>(T1)->getDeducedType(),
653 cast<AutoType>(T2)->getDeducedType()))
654 return false;
655 break;
656
Douglas Gregor3996e242010-02-15 22:01:00 +0000657 case Type::Record:
658 case Type::Enum:
659 if (!IsStructurallyEquivalent(Context,
660 cast<TagType>(T1)->getDecl(),
661 cast<TagType>(T2)->getDecl()))
662 return false;
663 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000664
Douglas Gregor3996e242010-02-15 22:01:00 +0000665 case Type::TemplateTypeParm: {
666 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
667 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
668 if (Parm1->getDepth() != Parm2->getDepth())
669 return false;
670 if (Parm1->getIndex() != Parm2->getIndex())
671 return false;
672 if (Parm1->isParameterPack() != Parm2->isParameterPack())
673 return false;
674
675 // Names of template type parameters are never significant.
676 break;
677 }
678
679 case Type::SubstTemplateTypeParm: {
680 const SubstTemplateTypeParmType *Subst1
681 = cast<SubstTemplateTypeParmType>(T1);
682 const SubstTemplateTypeParmType *Subst2
683 = cast<SubstTemplateTypeParmType>(T2);
684 if (!IsStructurallyEquivalent(Context,
685 QualType(Subst1->getReplacedParameter(), 0),
686 QualType(Subst2->getReplacedParameter(), 0)))
687 return false;
688 if (!IsStructurallyEquivalent(Context,
689 Subst1->getReplacementType(),
690 Subst2->getReplacementType()))
691 return false;
692 break;
693 }
694
Douglas Gregorfb322d82011-01-14 05:11:40 +0000695 case Type::SubstTemplateTypeParmPack: {
696 const SubstTemplateTypeParmPackType *Subst1
697 = cast<SubstTemplateTypeParmPackType>(T1);
698 const SubstTemplateTypeParmPackType *Subst2
699 = cast<SubstTemplateTypeParmPackType>(T2);
700 if (!IsStructurallyEquivalent(Context,
701 QualType(Subst1->getReplacedParameter(), 0),
702 QualType(Subst2->getReplacedParameter(), 0)))
703 return false;
704 if (!IsStructurallyEquivalent(Context,
705 Subst1->getArgumentPack(),
706 Subst2->getArgumentPack()))
707 return false;
708 break;
709 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000710 case Type::TemplateSpecialization: {
711 const TemplateSpecializationType *Spec1
712 = cast<TemplateSpecializationType>(T1);
713 const TemplateSpecializationType *Spec2
714 = cast<TemplateSpecializationType>(T2);
715 if (!IsStructurallyEquivalent(Context,
716 Spec1->getTemplateName(),
717 Spec2->getTemplateName()))
718 return false;
719 if (Spec1->getNumArgs() != Spec2->getNumArgs())
720 return false;
721 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
722 if (!IsStructurallyEquivalent(Context,
723 Spec1->getArg(I), Spec2->getArg(I)))
724 return false;
725 }
726 break;
727 }
728
Abramo Bagnara6150c882010-05-11 21:36:43 +0000729 case Type::Elaborated: {
730 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
731 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
732 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
733 if (Elab1->getKeyword() != Elab2->getKeyword())
734 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000735 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000736 Elab1->getQualifier(),
737 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000738 return false;
739 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000740 Elab1->getNamedType(),
741 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000742 return false;
743 break;
744 }
745
John McCalle78aac42010-03-10 03:28:59 +0000746 case Type::InjectedClassName: {
747 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
748 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
749 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000750 Inj1->getInjectedSpecializationType(),
751 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000752 return false;
753 break;
754 }
755
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000756 case Type::DependentName: {
757 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
758 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000759 if (!IsStructurallyEquivalent(Context,
760 Typename1->getQualifier(),
761 Typename2->getQualifier()))
762 return false;
763 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
764 Typename2->getIdentifier()))
765 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000766
767 break;
768 }
769
John McCallc392f372010-06-11 00:33:02 +0000770 case Type::DependentTemplateSpecialization: {
771 const DependentTemplateSpecializationType *Spec1 =
772 cast<DependentTemplateSpecializationType>(T1);
773 const DependentTemplateSpecializationType *Spec2 =
774 cast<DependentTemplateSpecializationType>(T2);
775 if (!IsStructurallyEquivalent(Context,
776 Spec1->getQualifier(),
777 Spec2->getQualifier()))
778 return false;
779 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
780 Spec2->getIdentifier()))
781 return false;
782 if (Spec1->getNumArgs() != Spec2->getNumArgs())
783 return false;
784 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
785 if (!IsStructurallyEquivalent(Context,
786 Spec1->getArg(I), Spec2->getArg(I)))
787 return false;
788 }
789 break;
790 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000791
792 case Type::PackExpansion:
793 if (!IsStructurallyEquivalent(Context,
794 cast<PackExpansionType>(T1)->getPattern(),
795 cast<PackExpansionType>(T2)->getPattern()))
796 return false;
797 break;
798
Douglas Gregor3996e242010-02-15 22:01:00 +0000799 case Type::ObjCInterface: {
800 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
801 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
802 if (!IsStructurallyEquivalent(Context,
803 Iface1->getDecl(), Iface2->getDecl()))
804 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000805 break;
806 }
807
808 case Type::ObjCObject: {
809 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
810 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
811 if (!IsStructurallyEquivalent(Context,
812 Obj1->getBaseType(),
813 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000814 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000815 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
816 return false;
817 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000818 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000819 Obj1->getProtocol(I),
820 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000821 return false;
822 }
823 break;
824 }
825
826 case Type::ObjCObjectPointer: {
827 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
828 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
829 if (!IsStructurallyEquivalent(Context,
830 Ptr1->getPointeeType(),
831 Ptr2->getPointeeType()))
832 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000833 break;
834 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000835
836 case Type::Atomic: {
837 if (!IsStructurallyEquivalent(Context,
838 cast<AtomicType>(T1)->getValueType(),
839 cast<AtomicType>(T2)->getValueType()))
840 return false;
841 break;
842 }
843
Douglas Gregor3996e242010-02-15 22:01:00 +0000844 } // end switch
845
846 return true;
847}
848
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000849/// \brief Determine structural equivalence of two fields.
850static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
851 FieldDecl *Field1, FieldDecl *Field2) {
852 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
853
854 if (!IsStructurallyEquivalent(Context,
855 Field1->getType(), Field2->getType())) {
856 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
857 << Context.C2.getTypeDeclType(Owner2);
858 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
859 << Field2->getDeclName() << Field2->getType();
860 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
861 << Field1->getDeclName() << Field1->getType();
862 return false;
863 }
864
865 if (Field1->isBitField() != Field2->isBitField()) {
866 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
867 << Context.C2.getTypeDeclType(Owner2);
868 if (Field1->isBitField()) {
869 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
870 << Field1->getDeclName() << Field1->getType()
871 << Field1->getBitWidthValue(Context.C1);
872 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
873 << Field2->getDeclName();
874 } else {
875 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
876 << Field2->getDeclName() << Field2->getType()
877 << Field2->getBitWidthValue(Context.C2);
878 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
879 << Field1->getDeclName();
880 }
881 return false;
882 }
883
884 if (Field1->isBitField()) {
885 // Make sure that the bit-fields are the same length.
886 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
887 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
888
889 if (Bits1 != Bits2) {
890 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
891 << Context.C2.getTypeDeclType(Owner2);
892 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
893 << Field2->getDeclName() << Field2->getType() << Bits2;
894 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
895 << Field1->getDeclName() << Field1->getType() << Bits1;
896 return false;
897 }
898 }
899
900 return true;
901}
902
Douglas Gregor3996e242010-02-15 22:01:00 +0000903/// \brief Determine structural equivalence of two records.
904static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
905 RecordDecl *D1, RecordDecl *D2) {
906 if (D1->isUnion() != D2->isUnion()) {
907 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
908 << Context.C2.getTypeDeclType(D2);
909 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
910 << D1->getDeclName() << (unsigned)D1->getTagKind();
911 return false;
912 }
913
Douglas Gregore2e50d332010-12-01 01:36:18 +0000914 // If both declarations are class template specializations, we know
915 // the ODR applies, so check the template and template arguments.
916 ClassTemplateSpecializationDecl *Spec1
917 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
918 ClassTemplateSpecializationDecl *Spec2
919 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
920 if (Spec1 && Spec2) {
921 // Check that the specialized templates are the same.
922 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
923 Spec2->getSpecializedTemplate()))
924 return false;
925
926 // Check that the template arguments are the same.
927 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
928 return false;
929
930 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
931 if (!IsStructurallyEquivalent(Context,
932 Spec1->getTemplateArgs().get(I),
933 Spec2->getTemplateArgs().get(I)))
934 return false;
935 }
936 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000937 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000938 else if (Spec1 || Spec2)
939 return false;
940
Douglas Gregorb4964f72010-02-15 23:54:17 +0000941 // Compare the definitions of these two records. If either or both are
942 // incomplete, we assume that they are equivalent.
943 D1 = D1->getDefinition();
944 D2 = D2->getDefinition();
945 if (!D1 || !D2)
946 return true;
947
Douglas Gregor3996e242010-02-15 22:01:00 +0000948 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
949 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
950 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
951 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000952 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000953 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000954 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000955 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000956 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000957 return false;
958 }
959
960 // Check the base classes.
961 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
962 BaseEnd1 = D1CXX->bases_end(),
963 Base2 = D2CXX->bases_begin();
964 Base1 != BaseEnd1;
965 ++Base1, ++Base2) {
966 if (!IsStructurallyEquivalent(Context,
967 Base1->getType(), Base2->getType())) {
968 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
969 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000970 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000971 << Base2->getType()
972 << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000973 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000974 << Base1->getType()
975 << Base1->getSourceRange();
976 return false;
977 }
978
979 // Check virtual vs. non-virtual inheritance mismatch.
980 if (Base1->isVirtual() != Base2->isVirtual()) {
981 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
982 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000983 Context.Diag2(Base2->getLocStart(),
Douglas Gregor3996e242010-02-15 22:01:00 +0000984 diag::note_odr_virtual_base)
985 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000986 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000987 << Base1->isVirtual()
988 << Base1->getSourceRange();
989 return false;
990 }
991 }
992 } else if (D1CXX->getNumBases() > 0) {
993 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
994 << Context.C2.getTypeDeclType(D2);
995 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar62ee6412012-03-09 18:35:03 +0000996 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor3996e242010-02-15 22:01:00 +0000997 << Base1->getType()
998 << Base1->getSourceRange();
999 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1000 return false;
1001 }
1002 }
1003
1004 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001005 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001006 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001007 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001008 Field1End = D1->field_end();
1009 Field1 != Field1End;
1010 ++Field1, ++Field2) {
1011 if (Field2 == Field2End) {
1012 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1013 << Context.C2.getTypeDeclType(D2);
1014 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1015 << Field1->getDeclName() << Field1->getType();
1016 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1017 return false;
1018 }
1019
David Blaikie40ed2972012-06-06 20:45:41 +00001020 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001021 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001022 }
1023
1024 if (Field2 != Field2End) {
1025 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1026 << Context.C2.getTypeDeclType(D2);
1027 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1028 << Field2->getDeclName() << Field2->getType();
1029 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1030 return false;
1031 }
1032
1033 return true;
1034}
1035
1036/// \brief Determine structural equivalence of two enums.
1037static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1038 EnumDecl *D1, EnumDecl *D2) {
1039 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1040 EC2End = D2->enumerator_end();
1041 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1042 EC1End = D1->enumerator_end();
1043 EC1 != EC1End; ++EC1, ++EC2) {
1044 if (EC2 == EC2End) {
1045 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1046 << Context.C2.getTypeDeclType(D2);
1047 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1048 << EC1->getDeclName()
1049 << EC1->getInitVal().toString(10);
1050 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1051 return false;
1052 }
1053
1054 llvm::APSInt Val1 = EC1->getInitVal();
1055 llvm::APSInt Val2 = EC2->getInitVal();
1056 if (!IsSameValue(Val1, Val2) ||
1057 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1058 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1059 << Context.C2.getTypeDeclType(D2);
1060 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1061 << EC2->getDeclName()
1062 << EC2->getInitVal().toString(10);
1063 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1064 << EC1->getDeclName()
1065 << EC1->getInitVal().toString(10);
1066 return false;
1067 }
1068 }
1069
1070 if (EC2 != EC2End) {
1071 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1072 << Context.C2.getTypeDeclType(D2);
1073 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1074 << EC2->getDeclName()
1075 << EC2->getInitVal().toString(10);
1076 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1077 return false;
1078 }
1079
1080 return true;
1081}
Douglas Gregora082a492010-11-30 19:14:50 +00001082
1083static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1084 TemplateParameterList *Params1,
1085 TemplateParameterList *Params2) {
1086 if (Params1->size() != Params2->size()) {
1087 Context.Diag2(Params2->getTemplateLoc(),
1088 diag::err_odr_different_num_template_parameters)
1089 << Params1->size() << Params2->size();
1090 Context.Diag1(Params1->getTemplateLoc(),
1091 diag::note_odr_template_parameter_list);
1092 return false;
1093 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001094
Douglas Gregora082a492010-11-30 19:14:50 +00001095 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1096 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1097 Context.Diag2(Params2->getParam(I)->getLocation(),
1098 diag::err_odr_different_template_parameter_kind);
1099 Context.Diag1(Params1->getParam(I)->getLocation(),
1100 diag::note_odr_template_parameter_here);
1101 return false;
1102 }
1103
1104 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1105 Params2->getParam(I))) {
1106
1107 return false;
1108 }
1109 }
1110
1111 return true;
1112}
1113
1114static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1115 TemplateTypeParmDecl *D1,
1116 TemplateTypeParmDecl *D2) {
1117 if (D1->isParameterPack() != D2->isParameterPack()) {
1118 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1119 << D2->isParameterPack();
1120 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1121 << D1->isParameterPack();
1122 return false;
1123 }
1124
1125 return true;
1126}
1127
1128static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1129 NonTypeTemplateParmDecl *D1,
1130 NonTypeTemplateParmDecl *D2) {
1131 // FIXME: Enable once we have variadic templates.
1132#if 0
1133 if (D1->isParameterPack() != D2->isParameterPack()) {
1134 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1135 << D2->isParameterPack();
1136 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1137 << D1->isParameterPack();
1138 return false;
1139 }
1140#endif
1141
1142 // Check types.
1143 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1144 Context.Diag2(D2->getLocation(),
1145 diag::err_odr_non_type_parameter_type_inconsistent)
1146 << D2->getType() << D1->getType();
1147 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1148 << D1->getType();
1149 return false;
1150 }
1151
1152 return true;
1153}
1154
1155static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1156 TemplateTemplateParmDecl *D1,
1157 TemplateTemplateParmDecl *D2) {
1158 // FIXME: Enable once we have variadic templates.
1159#if 0
1160 if (D1->isParameterPack() != D2->isParameterPack()) {
1161 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1162 << D2->isParameterPack();
1163 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1164 << D1->isParameterPack();
1165 return false;
1166 }
1167#endif
1168
1169 // Check template parameter lists.
1170 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1171 D2->getTemplateParameters());
1172}
1173
1174static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1175 ClassTemplateDecl *D1,
1176 ClassTemplateDecl *D2) {
1177 // Check template parameters.
1178 if (!IsStructurallyEquivalent(Context,
1179 D1->getTemplateParameters(),
1180 D2->getTemplateParameters()))
1181 return false;
1182
1183 // Check the templated declaration.
1184 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1185 D2->getTemplatedDecl());
1186}
1187
Douglas Gregor3996e242010-02-15 22:01:00 +00001188/// \brief Determine structural equivalence of two declarations.
1189static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1190 Decl *D1, Decl *D2) {
1191 // FIXME: Check for known structural equivalences via a callback of some sort.
1192
Douglas Gregorb4964f72010-02-15 23:54:17 +00001193 // Check whether we already know that these two declarations are not
1194 // structurally equivalent.
1195 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1196 D2->getCanonicalDecl())))
1197 return false;
1198
Douglas Gregor3996e242010-02-15 22:01:00 +00001199 // Determine whether we've already produced a tentative equivalence for D1.
1200 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1201 if (EquivToD1)
1202 return EquivToD1 == D2->getCanonicalDecl();
1203
1204 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1205 EquivToD1 = D2->getCanonicalDecl();
1206 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1207 return true;
1208}
1209
1210bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1211 Decl *D2) {
1212 if (!::IsStructurallyEquivalent(*this, D1, D2))
1213 return false;
1214
1215 return !Finish();
1216}
1217
1218bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1219 QualType T2) {
1220 if (!::IsStructurallyEquivalent(*this, T1, T2))
1221 return false;
1222
1223 return !Finish();
1224}
1225
1226bool StructuralEquivalenceContext::Finish() {
1227 while (!DeclsToCheck.empty()) {
1228 // Check the next declaration.
1229 Decl *D1 = DeclsToCheck.front();
1230 DeclsToCheck.pop_front();
1231
1232 Decl *D2 = TentativeEquivalences[D1];
1233 assert(D2 && "Unrecorded tentative equivalence?");
1234
Douglas Gregorb4964f72010-02-15 23:54:17 +00001235 bool Equivalent = true;
1236
Douglas Gregor3996e242010-02-15 22:01:00 +00001237 // FIXME: Switch on all declaration kinds. For now, we're just going to
1238 // check the obvious ones.
1239 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1240 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1241 // Check for equivalent structure names.
1242 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001243 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1244 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001245 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001246 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1247 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001248 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1249 !::IsStructurallyEquivalent(*this, Record1, Record2))
1250 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001251 } else {
1252 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001253 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001254 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001255 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001256 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1257 // Check for equivalent enum names.
1258 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001259 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1260 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001261 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001262 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1263 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001264 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1265 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1266 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001267 } else {
1268 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001269 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001270 }
Richard Smithdda56e42011-04-15 14:24:37 +00001271 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1272 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001273 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001274 Typedef2->getIdentifier()) ||
1275 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001276 Typedef1->getUnderlyingType(),
1277 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001278 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001279 } else {
1280 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001281 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001282 }
Douglas Gregora082a492010-11-30 19:14:50 +00001283 } else if (ClassTemplateDecl *ClassTemplate1
1284 = dyn_cast<ClassTemplateDecl>(D1)) {
1285 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1286 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1287 ClassTemplate2->getIdentifier()) ||
1288 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1289 Equivalent = false;
1290 } else {
1291 // Class template/non-class-template mismatch.
1292 Equivalent = false;
1293 }
1294 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1295 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1296 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1297 Equivalent = false;
1298 } else {
1299 // Kind mismatch.
1300 Equivalent = false;
1301 }
1302 } else if (NonTypeTemplateParmDecl *NTTP1
1303 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1304 if (NonTypeTemplateParmDecl *NTTP2
1305 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1306 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1307 Equivalent = false;
1308 } else {
1309 // Kind mismatch.
1310 Equivalent = false;
1311 }
1312 } else if (TemplateTemplateParmDecl *TTP1
1313 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1314 if (TemplateTemplateParmDecl *TTP2
1315 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1316 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1317 Equivalent = false;
1318 } else {
1319 // Kind mismatch.
1320 Equivalent = false;
1321 }
1322 }
1323
Douglas Gregorb4964f72010-02-15 23:54:17 +00001324 if (!Equivalent) {
1325 // Note that these two declarations are not equivalent (and we already
1326 // know about it).
1327 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1328 D2->getCanonicalDecl()));
1329 return true;
1330 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001331 // FIXME: Check other declaration kinds!
1332 }
1333
1334 return false;
1335}
1336
1337//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001338// Import Types
1339//----------------------------------------------------------------------------
1340
John McCall424cec92011-01-19 06:33:43 +00001341QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001342 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1343 << T->getTypeClassName();
1344 return QualType();
1345}
1346
John McCall424cec92011-01-19 06:33:43 +00001347QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001348 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001349#define SHARED_SINGLETON_TYPE(Expansion)
1350#define BUILTIN_TYPE(Id, SingletonId) \
1351 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1352#include "clang/AST/BuiltinTypes.def"
1353
1354 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1355 // context supports C++.
1356
1357 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1358 // context supports ObjC.
1359
Douglas Gregor96e578d2010-02-05 17:54:41 +00001360 case BuiltinType::Char_U:
1361 // The context we're importing from has an unsigned 'char'. If we're
1362 // importing into a context with a signed 'char', translate to
1363 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001364 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001365 return Importer.getToContext().UnsignedCharTy;
1366
1367 return Importer.getToContext().CharTy;
1368
Douglas Gregor96e578d2010-02-05 17:54:41 +00001369 case BuiltinType::Char_S:
1370 // The context we're importing from has an unsigned 'char'. If we're
1371 // importing into a context with a signed 'char', translate to
1372 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001373 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001374 return Importer.getToContext().SignedCharTy;
1375
1376 return Importer.getToContext().CharTy;
1377
Chris Lattnerad3467e2010-12-25 23:25:43 +00001378 case BuiltinType::WChar_S:
1379 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001380 // FIXME: If not in C++, shall we translate to the C equivalent of
1381 // wchar_t?
1382 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001383 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001384
1385 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001386}
1387
John McCall424cec92011-01-19 06:33:43 +00001388QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001389 QualType ToElementType = Importer.Import(T->getElementType());
1390 if (ToElementType.isNull())
1391 return QualType();
1392
1393 return Importer.getToContext().getComplexType(ToElementType);
1394}
1395
John McCall424cec92011-01-19 06:33:43 +00001396QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001397 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1398 if (ToPointeeType.isNull())
1399 return QualType();
1400
1401 return Importer.getToContext().getPointerType(ToPointeeType);
1402}
1403
John McCall424cec92011-01-19 06:33:43 +00001404QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001405 // FIXME: Check for blocks support in "to" context.
1406 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1407 if (ToPointeeType.isNull())
1408 return QualType();
1409
1410 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1411}
1412
John McCall424cec92011-01-19 06:33:43 +00001413QualType
1414ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001415 // FIXME: Check for C++ support in "to" context.
1416 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1417 if (ToPointeeType.isNull())
1418 return QualType();
1419
1420 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1421}
1422
John McCall424cec92011-01-19 06:33:43 +00001423QualType
1424ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001425 // FIXME: Check for C++0x support in "to" context.
1426 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1427 if (ToPointeeType.isNull())
1428 return QualType();
1429
1430 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1431}
1432
John McCall424cec92011-01-19 06:33:43 +00001433QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001434 // FIXME: Check for C++ support in "to" context.
1435 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1436 if (ToPointeeType.isNull())
1437 return QualType();
1438
1439 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1440 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1441 ClassType.getTypePtr());
1442}
1443
John McCall424cec92011-01-19 06:33:43 +00001444QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001445 QualType ToElementType = Importer.Import(T->getElementType());
1446 if (ToElementType.isNull())
1447 return QualType();
1448
1449 return Importer.getToContext().getConstantArrayType(ToElementType,
1450 T->getSize(),
1451 T->getSizeModifier(),
1452 T->getIndexTypeCVRQualifiers());
1453}
1454
John McCall424cec92011-01-19 06:33:43 +00001455QualType
1456ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001457 QualType ToElementType = Importer.Import(T->getElementType());
1458 if (ToElementType.isNull())
1459 return QualType();
1460
1461 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1462 T->getSizeModifier(),
1463 T->getIndexTypeCVRQualifiers());
1464}
1465
John McCall424cec92011-01-19 06:33:43 +00001466QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001467 QualType ToElementType = Importer.Import(T->getElementType());
1468 if (ToElementType.isNull())
1469 return QualType();
1470
1471 Expr *Size = Importer.Import(T->getSizeExpr());
1472 if (!Size)
1473 return QualType();
1474
1475 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1476 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1477 T->getSizeModifier(),
1478 T->getIndexTypeCVRQualifiers(),
1479 Brackets);
1480}
1481
John McCall424cec92011-01-19 06:33:43 +00001482QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 QualType ToElementType = Importer.Import(T->getElementType());
1484 if (ToElementType.isNull())
1485 return QualType();
1486
1487 return Importer.getToContext().getVectorType(ToElementType,
1488 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001489 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001490}
1491
John McCall424cec92011-01-19 06:33:43 +00001492QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001493 QualType ToElementType = Importer.Import(T->getElementType());
1494 if (ToElementType.isNull())
1495 return QualType();
1496
1497 return Importer.getToContext().getExtVectorType(ToElementType,
1498 T->getNumElements());
1499}
1500
John McCall424cec92011-01-19 06:33:43 +00001501QualType
1502ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 // FIXME: What happens if we're importing a function without a prototype
1504 // into C++? Should we make it variadic?
1505 QualType ToResultType = Importer.Import(T->getResultType());
1506 if (ToResultType.isNull())
1507 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001508
Douglas Gregor96e578d2010-02-05 17:54:41 +00001509 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001510 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001511}
1512
John McCall424cec92011-01-19 06:33:43 +00001513QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001514 QualType ToResultType = Importer.Import(T->getResultType());
1515 if (ToResultType.isNull())
1516 return QualType();
1517
1518 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001519 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001520 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1521 AEnd = T->arg_type_end();
1522 A != AEnd; ++A) {
1523 QualType ArgType = Importer.Import(*A);
1524 if (ArgType.isNull())
1525 return QualType();
1526 ArgTypes.push_back(ArgType);
1527 }
1528
1529 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001530 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001531 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1532 EEnd = T->exception_end();
1533 E != EEnd; ++E) {
1534 QualType ExceptionType = Importer.Import(*E);
1535 if (ExceptionType.isNull())
1536 return QualType();
1537 ExceptionTypes.push_back(ExceptionType);
1538 }
John McCalldb40c7f2010-12-14 08:05:40 +00001539
1540 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1541 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001542
1543 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001544 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001545}
1546
Sean Callananda6df8a2011-08-11 16:56:07 +00001547QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1548 QualType ToInnerType = Importer.Import(T->getInnerType());
1549 if (ToInnerType.isNull())
1550 return QualType();
1551
1552 return Importer.getToContext().getParenType(ToInnerType);
1553}
1554
John McCall424cec92011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001556 TypedefNameDecl *ToDecl
1557 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001558 if (!ToDecl)
1559 return QualType();
1560
1561 return Importer.getToContext().getTypeDeclType(ToDecl);
1562}
1563
John McCall424cec92011-01-19 06:33:43 +00001564QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001565 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1566 if (!ToExpr)
1567 return QualType();
1568
1569 return Importer.getToContext().getTypeOfExprType(ToExpr);
1570}
1571
John McCall424cec92011-01-19 06:33:43 +00001572QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001573 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1574 if (ToUnderlyingType.isNull())
1575 return QualType();
1576
1577 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1578}
1579
John McCall424cec92011-01-19 06:33:43 +00001580QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001581 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001582 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1583 if (!ToExpr)
1584 return QualType();
1585
Douglas Gregor81495f32012-02-12 18:42:33 +00001586 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1587 if (UnderlyingType.isNull())
1588 return QualType();
1589
1590 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001591}
1592
Alexis Hunte852b102011-05-24 22:41:36 +00001593QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1594 QualType ToBaseType = Importer.Import(T->getBaseType());
1595 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1596 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1597 return QualType();
1598
1599 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1600 ToUnderlyingType,
1601 T->getUTTKind());
1602}
1603
Richard Smith30482bc2011-02-20 03:19:35 +00001604QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1605 // FIXME: Make sure that the "to" context supports C++0x!
1606 QualType FromDeduced = T->getDeducedType();
1607 QualType ToDeduced;
1608 if (!FromDeduced.isNull()) {
1609 ToDeduced = Importer.Import(FromDeduced);
1610 if (ToDeduced.isNull())
1611 return QualType();
1612 }
1613
1614 return Importer.getToContext().getAutoType(ToDeduced);
1615}
1616
John McCall424cec92011-01-19 06:33:43 +00001617QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001618 RecordDecl *ToDecl
1619 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1620 if (!ToDecl)
1621 return QualType();
1622
1623 return Importer.getToContext().getTagDeclType(ToDecl);
1624}
1625
John McCall424cec92011-01-19 06:33:43 +00001626QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001627 EnumDecl *ToDecl
1628 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1629 if (!ToDecl)
1630 return QualType();
1631
1632 return Importer.getToContext().getTagDeclType(ToDecl);
1633}
1634
Douglas Gregore2e50d332010-12-01 01:36:18 +00001635QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001636 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001637 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1638 if (ToTemplate.isNull())
1639 return QualType();
1640
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001641 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001642 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1643 return QualType();
1644
1645 QualType ToCanonType;
1646 if (!QualType(T, 0).isCanonical()) {
1647 QualType FromCanonType
1648 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1649 ToCanonType =Importer.Import(FromCanonType);
1650 if (ToCanonType.isNull())
1651 return QualType();
1652 }
1653 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1654 ToTemplateArgs.data(),
1655 ToTemplateArgs.size(),
1656 ToCanonType);
1657}
1658
John McCall424cec92011-01-19 06:33:43 +00001659QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001660 NestedNameSpecifier *ToQualifier = 0;
1661 // Note: the qualifier in an ElaboratedType is optional.
1662 if (T->getQualifier()) {
1663 ToQualifier = Importer.Import(T->getQualifier());
1664 if (!ToQualifier)
1665 return QualType();
1666 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001667
1668 QualType ToNamedType = Importer.Import(T->getNamedType());
1669 if (ToNamedType.isNull())
1670 return QualType();
1671
Abramo Bagnara6150c882010-05-11 21:36:43 +00001672 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1673 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001674}
1675
John McCall424cec92011-01-19 06:33:43 +00001676QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001677 ObjCInterfaceDecl *Class
1678 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1679 if (!Class)
1680 return QualType();
1681
John McCall8b07ec22010-05-15 11:32:37 +00001682 return Importer.getToContext().getObjCInterfaceType(Class);
1683}
1684
John McCall424cec92011-01-19 06:33:43 +00001685QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001686 QualType ToBaseType = Importer.Import(T->getBaseType());
1687 if (ToBaseType.isNull())
1688 return QualType();
1689
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001690 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001691 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001692 PEnd = T->qual_end();
1693 P != PEnd; ++P) {
1694 ObjCProtocolDecl *Protocol
1695 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1696 if (!Protocol)
1697 return QualType();
1698 Protocols.push_back(Protocol);
1699 }
1700
John McCall8b07ec22010-05-15 11:32:37 +00001701 return Importer.getToContext().getObjCObjectType(ToBaseType,
1702 Protocols.data(),
1703 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001704}
1705
John McCall424cec92011-01-19 06:33:43 +00001706QualType
1707ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001708 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1709 if (ToPointeeType.isNull())
1710 return QualType();
1711
John McCall8b07ec22010-05-15 11:32:37 +00001712 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001713}
1714
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001715//----------------------------------------------------------------------------
1716// Import Declarations
1717//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001718bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1719 DeclContext *&LexicalDC,
1720 DeclarationName &Name,
1721 SourceLocation &Loc) {
1722 // Import the context of this declaration.
1723 DC = Importer.ImportContext(D->getDeclContext());
1724 if (!DC)
1725 return true;
1726
1727 LexicalDC = DC;
1728 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1729 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1730 if (!LexicalDC)
1731 return true;
1732 }
1733
1734 // Import the name of this declaration.
1735 Name = Importer.Import(D->getDeclName());
1736 if (D->getDeclName() && !Name)
1737 return true;
1738
1739 // Import the location of this declaration.
1740 Loc = Importer.Import(D->getLocation());
1741 return false;
1742}
1743
Douglas Gregord451ea92011-07-29 23:31:30 +00001744void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1745 if (!FromD)
1746 return;
1747
1748 if (!ToD) {
1749 ToD = Importer.Import(FromD);
1750 if (!ToD)
1751 return;
1752 }
1753
1754 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1755 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1756 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1757 ImportDefinition(FromRecord, ToRecord);
1758 }
1759 }
1760 return;
1761 }
1762
1763 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1764 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1765 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1766 ImportDefinition(FromEnum, ToEnum);
1767 }
1768 }
1769 return;
1770 }
1771}
1772
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001773void
1774ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1775 DeclarationNameInfo& To) {
1776 // NOTE: To.Name and To.Loc are already imported.
1777 // We only have to import To.LocInfo.
1778 switch (To.getName().getNameKind()) {
1779 case DeclarationName::Identifier:
1780 case DeclarationName::ObjCZeroArgSelector:
1781 case DeclarationName::ObjCOneArgSelector:
1782 case DeclarationName::ObjCMultiArgSelector:
1783 case DeclarationName::CXXUsingDirective:
1784 return;
1785
1786 case DeclarationName::CXXOperatorName: {
1787 SourceRange Range = From.getCXXOperatorNameRange();
1788 To.setCXXOperatorNameRange(Importer.Import(Range));
1789 return;
1790 }
1791 case DeclarationName::CXXLiteralOperatorName: {
1792 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1793 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1794 return;
1795 }
1796 case DeclarationName::CXXConstructorName:
1797 case DeclarationName::CXXDestructorName:
1798 case DeclarationName::CXXConversionFunctionName: {
1799 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1800 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1801 return;
1802 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001803 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001804 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001805}
1806
Douglas Gregor2e15c842012-02-01 21:00:38 +00001807void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001808 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001809 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001810 return;
1811 }
1812
Douglas Gregor968d6332010-02-21 18:24:45 +00001813 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1814 FromEnd = FromDC->decls_end();
1815 From != FromEnd;
1816 ++From)
1817 Importer.Import(*From);
1818}
1819
Douglas Gregord451ea92011-07-29 23:31:30 +00001820bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001821 ImportDefinitionKind Kind) {
1822 if (To->getDefinition() || To->isBeingDefined()) {
1823 if (Kind == IDK_Everything)
1824 ImportDeclContext(From, /*ForceImport=*/true);
1825
Douglas Gregore2e50d332010-12-01 01:36:18 +00001826 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001827 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001828
1829 To->startDefinition();
1830
1831 // Add base classes.
1832 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1833 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001834
1835 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1836 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1837 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1838 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1839 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1840 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1841 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1842 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1843 ToData.Aggregate = FromData.Aggregate;
1844 ToData.PlainOldData = FromData.PlainOldData;
1845 ToData.Empty = FromData.Empty;
1846 ToData.Polymorphic = FromData.Polymorphic;
1847 ToData.Abstract = FromData.Abstract;
1848 ToData.IsStandardLayout = FromData.IsStandardLayout;
1849 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1850 ToData.HasPrivateFields = FromData.HasPrivateFields;
1851 ToData.HasProtectedFields = FromData.HasProtectedFields;
1852 ToData.HasPublicFields = FromData.HasPublicFields;
1853 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001854 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001855 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001856 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1857 ToData.HasConstexprNonCopyMoveConstructor
1858 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001859 ToData.DefaultedDefaultConstructorIsConstexpr
1860 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001861 ToData.HasConstexprDefaultConstructor
1862 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001863 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1864 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1865 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1866 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1867 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smith561fb152012-02-25 07:33:38 +00001868 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001869 ToData.HasNonLiteralTypeFieldsOrBases
1870 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001871 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001872 ToData.UserProvidedDefaultConstructor
1873 = FromData.UserProvidedDefaultConstructor;
1874 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1875 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1876 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1877 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1878 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1879 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1880 ToData.FailedImplicitMoveConstructor
1881 = FromData.FailedImplicitMoveConstructor;
1882 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001883 ToData.IsLambda = FromData.IsLambda;
1884
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001885 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001886 for (CXXRecordDecl::base_class_iterator
1887 Base1 = FromCXX->bases_begin(),
1888 FromBaseEnd = FromCXX->bases_end();
1889 Base1 != FromBaseEnd;
1890 ++Base1) {
1891 QualType T = Importer.Import(Base1->getType());
1892 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001893 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001894
1895 SourceLocation EllipsisLoc;
1896 if (Base1->isPackExpansion())
1897 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001898
1899 // Ensure that we have a definition for the base.
1900 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1901
Douglas Gregore2e50d332010-12-01 01:36:18 +00001902 Bases.push_back(
1903 new (Importer.getToContext())
1904 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1905 Base1->isVirtual(),
1906 Base1->isBaseOfClass(),
1907 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001908 Importer.Import(Base1->getTypeSourceInfo()),
1909 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001910 }
1911 if (!Bases.empty())
1912 ToCXX->setBases(Bases.data(), Bases.size());
1913 }
1914
Douglas Gregor2e15c842012-02-01 21:00:38 +00001915 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00001916 ImportDeclContext(From, /*ForceImport=*/true);
1917
Douglas Gregore2e50d332010-12-01 01:36:18 +00001918 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001919 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001920}
1921
Douglas Gregord451ea92011-07-29 23:31:30 +00001922bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00001923 ImportDefinitionKind Kind) {
1924 if (To->getDefinition() || To->isBeingDefined()) {
1925 if (Kind == IDK_Everything)
1926 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001927 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001928 }
Douglas Gregord451ea92011-07-29 23:31:30 +00001929
1930 To->startDefinition();
1931
1932 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1933 if (T.isNull())
1934 return true;
1935
1936 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1937 if (ToPromotionType.isNull())
1938 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00001939
1940 if (shouldForceImportDeclContext(Kind))
1941 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00001942
1943 // FIXME: we might need to merge the number of positive or negative bits
1944 // if the enumerator lists don't match.
1945 To->completeDefinition(T, ToPromotionType,
1946 From->getNumPositiveBits(),
1947 From->getNumNegativeBits());
1948 return false;
1949}
1950
Douglas Gregora082a492010-11-30 19:14:50 +00001951TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1952 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001953 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00001954 ToParams.reserve(Params->size());
1955 for (TemplateParameterList::iterator P = Params->begin(),
1956 PEnd = Params->end();
1957 P != PEnd; ++P) {
1958 Decl *To = Importer.Import(*P);
1959 if (!To)
1960 return 0;
1961
1962 ToParams.push_back(cast<NamedDecl>(To));
1963 }
1964
1965 return TemplateParameterList::Create(Importer.getToContext(),
1966 Importer.Import(Params->getTemplateLoc()),
1967 Importer.Import(Params->getLAngleLoc()),
1968 ToParams.data(), ToParams.size(),
1969 Importer.Import(Params->getRAngleLoc()));
1970}
1971
Douglas Gregore2e50d332010-12-01 01:36:18 +00001972TemplateArgument
1973ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1974 switch (From.getKind()) {
1975 case TemplateArgument::Null:
1976 return TemplateArgument();
1977
1978 case TemplateArgument::Type: {
1979 QualType ToType = Importer.Import(From.getAsType());
1980 if (ToType.isNull())
1981 return TemplateArgument();
1982 return TemplateArgument(ToType);
1983 }
1984
1985 case TemplateArgument::Integral: {
1986 QualType ToType = Importer.Import(From.getIntegralType());
1987 if (ToType.isNull())
1988 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001989 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00001990 }
1991
1992 case TemplateArgument::Declaration:
1993 if (Decl *To = Importer.Import(From.getAsDecl()))
1994 return TemplateArgument(To);
1995 return TemplateArgument();
1996
1997 case TemplateArgument::Template: {
1998 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1999 if (ToTemplate.isNull())
2000 return TemplateArgument();
2001
2002 return TemplateArgument(ToTemplate);
2003 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002004
2005 case TemplateArgument::TemplateExpansion: {
2006 TemplateName ToTemplate
2007 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2008 if (ToTemplate.isNull())
2009 return TemplateArgument();
2010
Douglas Gregore1d60df2011-01-14 23:41:42 +00002011 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002012 }
2013
Douglas Gregore2e50d332010-12-01 01:36:18 +00002014 case TemplateArgument::Expression:
2015 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2016 return TemplateArgument(ToExpr);
2017 return TemplateArgument();
2018
2019 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002020 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002021 ToPack.reserve(From.pack_size());
2022 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2023 return TemplateArgument();
2024
2025 TemplateArgument *ToArgs
2026 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2027 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2028 return TemplateArgument(ToArgs, ToPack.size());
2029 }
2030 }
2031
2032 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002033}
2034
2035bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2036 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002037 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002038 for (unsigned I = 0; I != NumFromArgs; ++I) {
2039 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2040 if (To.isNull() && !FromArgs[I].isNull())
2041 return true;
2042
2043 ToArgs.push_back(To);
2044 }
2045
2046 return false;
2047}
2048
Douglas Gregor5c73e912010-02-11 00:48:18 +00002049bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00002050 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002051 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002052 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002053 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002054 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002055}
2056
Douglas Gregor98c10182010-02-12 22:17:39 +00002057bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002058 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002059 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002060 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002061 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002062}
2063
Douglas Gregora082a492010-11-30 19:14:50 +00002064bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2065 ClassTemplateDecl *To) {
2066 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2067 Importer.getToContext(),
2068 Importer.getNonEquivalentDecls());
2069 return Ctx.IsStructurallyEquivalent(From, To);
2070}
2071
Douglas Gregore4c83e42010-02-09 22:48:33 +00002072Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002073 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002074 << D->getDeclKindName();
2075 return 0;
2076}
2077
Sean Callanan65198272011-11-17 23:20:56 +00002078Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2079 TranslationUnitDecl *ToD =
2080 Importer.getToContext().getTranslationUnitDecl();
2081
2082 Importer.Imported(D, ToD);
2083
2084 return ToD;
2085}
2086
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002087Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2088 // Import the major distinguishing characteristics of this namespace.
2089 DeclContext *DC, *LexicalDC;
2090 DeclarationName Name;
2091 SourceLocation Loc;
2092 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2093 return 0;
2094
2095 NamespaceDecl *MergeWithNamespace = 0;
2096 if (!Name) {
2097 // This is an anonymous namespace. Adopt an existing anonymous
2098 // namespace if we can.
2099 // FIXME: Not testable.
2100 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2101 MergeWithNamespace = TU->getAnonymousNamespace();
2102 else
2103 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2104 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002105 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002106 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2107 DC->localUncachedLookup(Name, FoundDecls);
2108 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2109 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002110 continue;
2111
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002112 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002113 MergeWithNamespace = FoundNS;
2114 ConflictingDecls.clear();
2115 break;
2116 }
2117
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002118 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002119 }
2120
2121 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002122 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002123 ConflictingDecls.data(),
2124 ConflictingDecls.size());
2125 }
2126 }
2127
2128 // Create the "to" namespace, if needed.
2129 NamespaceDecl *ToNamespace = MergeWithNamespace;
2130 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002131 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002132 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002133 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002134 Loc, Name.getAsIdentifierInfo(),
2135 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002136 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002137 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002138
2139 // If this is an anonymous namespace, register it as the anonymous
2140 // namespace within its context.
2141 if (!Name) {
2142 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2143 TU->setAnonymousNamespace(ToNamespace);
2144 else
2145 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2146 }
2147 }
2148 Importer.Imported(D, ToNamespace);
2149
2150 ImportDeclContext(D);
2151
2152 return ToNamespace;
2153}
2154
Richard Smithdda56e42011-04-15 14:24:37 +00002155Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002156 // Import the major distinguishing characteristics of this typedef.
2157 DeclContext *DC, *LexicalDC;
2158 DeclarationName Name;
2159 SourceLocation Loc;
2160 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2161 return 0;
2162
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002163 // If this typedef is not in block scope, determine whether we've
2164 // seen a typedef with the same name (that we can merge with) or any
2165 // other entity by that name (which name lookup could conflict with).
2166 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002167 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002168 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002169 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2170 DC->localUncachedLookup(Name, FoundDecls);
2171 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2172 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002173 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002174 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002175 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002176 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2177 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002178 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002179 }
2180
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002181 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002182 }
2183
2184 if (!ConflictingDecls.empty()) {
2185 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2186 ConflictingDecls.data(),
2187 ConflictingDecls.size());
2188 if (!Name)
2189 return 0;
2190 }
2191 }
2192
Douglas Gregorb4964f72010-02-15 23:54:17 +00002193 // Import the underlying type of this typedef;
2194 QualType T = Importer.Import(D->getUnderlyingType());
2195 if (T.isNull())
2196 return 0;
2197
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002198 // Create the new typedef node.
2199 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002200 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002201 TypedefNameDecl *ToTypedef;
2202 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002203 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2204 StartL, Loc,
2205 Name.getAsIdentifierInfo(),
2206 TInfo);
2207 else
Richard Smithdda56e42011-04-15 14:24:37 +00002208 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2209 StartL, Loc,
2210 Name.getAsIdentifierInfo(),
2211 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002212
Douglas Gregordd483172010-02-22 17:42:47 +00002213 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002214 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002215 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002216 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002217
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002218 return ToTypedef;
2219}
2220
Richard Smithdda56e42011-04-15 14:24:37 +00002221Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2222 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2223}
2224
2225Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2226 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2227}
2228
Douglas Gregor98c10182010-02-12 22:17:39 +00002229Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2230 // Import the major distinguishing characteristics of this enum.
2231 DeclContext *DC, *LexicalDC;
2232 DeclarationName Name;
2233 SourceLocation Loc;
2234 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2235 return 0;
2236
2237 // Figure out what enum name we're looking for.
2238 unsigned IDNS = Decl::IDNS_Tag;
2239 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002240 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2241 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002242 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002243 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002244 IDNS |= Decl::IDNS_Ordinary;
2245
2246 // We may already have an enum of the same name; try to find and match it.
2247 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002248 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002249 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2250 DC->localUncachedLookup(SearchName, FoundDecls);
2251 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2252 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002253 continue;
2254
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002255 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002256 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002257 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2258 Found = Tag->getDecl();
2259 }
2260
2261 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002262 if (IsStructuralMatch(D, FoundEnum))
2263 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002264 }
2265
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002266 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002267 }
2268
2269 if (!ConflictingDecls.empty()) {
2270 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2271 ConflictingDecls.data(),
2272 ConflictingDecls.size());
2273 }
2274 }
2275
2276 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002277 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2278 Importer.Import(D->getLocStart()),
2279 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002280 D->isScoped(), D->isScopedUsingClassTag(),
2281 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002282 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002283 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002284 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002285 D2->setLexicalDeclContext(LexicalDC);
2286 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002287 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002288
2289 // Import the integer type.
2290 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2291 if (ToIntegerType.isNull())
2292 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002293 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002294
2295 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002296 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002297 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002298
Douglas Gregor3996e242010-02-15 22:01:00 +00002299 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002300}
2301
Douglas Gregor5c73e912010-02-11 00:48:18 +00002302Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2303 // If this record has a definition in the translation unit we're coming from,
2304 // but this particular declaration is not that definition, import the
2305 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002306 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002307 if (Definition && Definition != D) {
2308 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002309 if (!ImportedDef)
2310 return 0;
2311
2312 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002313 }
2314
2315 // Import the major distinguishing characteristics of this record.
2316 DeclContext *DC, *LexicalDC;
2317 DeclarationName Name;
2318 SourceLocation Loc;
2319 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2320 return 0;
2321
2322 // Figure out what structure name we're looking for.
2323 unsigned IDNS = Decl::IDNS_Tag;
2324 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002325 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2326 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002327 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002328 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002329 IDNS |= Decl::IDNS_Ordinary;
2330
2331 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002332 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002333 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002334 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002335 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2336 DC->localUncachedLookup(SearchName, FoundDecls);
2337 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2338 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002339 continue;
2340
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002341 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002342 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002343 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2344 Found = Tag->getDecl();
2345 }
2346
2347 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002348 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00002349 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002350 // The record types structurally match, or the "from" translation
2351 // unit only had a forward declaration anyway; call it the same
2352 // function.
2353 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002354 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002355 }
2356 } else {
2357 // We have a forward declaration of this type, so adopt that forward
2358 // declaration rather than building a new one.
2359 AdoptDecl = FoundRecord;
2360 continue;
2361 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002362 }
2363
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002364 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002365 }
2366
2367 if (!ConflictingDecls.empty()) {
2368 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2369 ConflictingDecls.data(),
2370 ConflictingDecls.size());
2371 }
2372 }
2373
2374 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002375 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002376 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002377 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002378 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002379 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002380 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002381 DC, StartLoc, Loc,
2382 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002383 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002384 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002385 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002386 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002387 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002388 }
Douglas Gregor14454802011-02-25 02:25:35 +00002389
2390 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002391 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002392 LexicalDC->addDeclInternal(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002393 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002394
Douglas Gregor3996e242010-02-15 22:01:00 +00002395 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002396
Douglas Gregor95d82832012-01-24 18:36:04 +00002397 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002398 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002399
Douglas Gregor3996e242010-02-15 22:01:00 +00002400 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002401}
2402
Douglas Gregor98c10182010-02-12 22:17:39 +00002403Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2404 // Import the major distinguishing characteristics of this enumerator.
2405 DeclContext *DC, *LexicalDC;
2406 DeclarationName Name;
2407 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002408 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002409 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002410
2411 QualType T = Importer.Import(D->getType());
2412 if (T.isNull())
2413 return 0;
2414
Douglas Gregor98c10182010-02-12 22:17:39 +00002415 // Determine whether there are any other declarations with the same name and
2416 // in the same context.
2417 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002418 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002419 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002420 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2421 DC->localUncachedLookup(Name, FoundDecls);
2422 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2423 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002424 continue;
2425
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002426 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002427 }
2428
2429 if (!ConflictingDecls.empty()) {
2430 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2431 ConflictingDecls.data(),
2432 ConflictingDecls.size());
2433 if (!Name)
2434 return 0;
2435 }
2436 }
2437
2438 Expr *Init = Importer.Import(D->getInitExpr());
2439 if (D->getInitExpr() && !Init)
2440 return 0;
2441
2442 EnumConstantDecl *ToEnumerator
2443 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2444 Name.getAsIdentifierInfo(), T,
2445 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002446 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002447 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002448 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002449 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002450 return ToEnumerator;
2451}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002452
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002453Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2454 // Import the major distinguishing characteristics of this function.
2455 DeclContext *DC, *LexicalDC;
2456 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002457 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002458 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002459 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002460
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002461 // Try to find a function in our own ("to") context with the same name, same
2462 // type, and in the same context as the function we're importing.
2463 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002464 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002465 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002466 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2467 DC->localUncachedLookup(Name, FoundDecls);
2468 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2469 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002470 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002471
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002472 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002473 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2474 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002475 if (Importer.IsStructurallyEquivalent(D->getType(),
2476 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002477 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002478 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002479 }
2480
2481 // FIXME: Check for overloading more carefully, e.g., by boosting
2482 // Sema::IsOverload out to the AST library.
2483
2484 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002485 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002486 continue;
2487
2488 // Complain about inconsistent function types.
2489 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002490 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002491 Importer.ToDiag(FoundFunction->getLocation(),
2492 diag::note_odr_value_here)
2493 << FoundFunction->getType();
2494 }
2495 }
2496
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002497 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002498 }
2499
2500 if (!ConflictingDecls.empty()) {
2501 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2502 ConflictingDecls.data(),
2503 ConflictingDecls.size());
2504 if (!Name)
2505 return 0;
2506 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002507 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002508
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002509 DeclarationNameInfo NameInfo(Name, Loc);
2510 // Import additional name location/type info.
2511 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2512
Douglas Gregorb4964f72010-02-15 23:54:17 +00002513 // Import the type.
2514 QualType T = Importer.Import(D->getType());
2515 if (T.isNull())
2516 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002517
2518 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002519 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002520 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2521 P != PEnd; ++P) {
2522 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2523 if (!ToP)
2524 return 0;
2525
2526 Parameters.push_back(ToP);
2527 }
2528
2529 // Create the imported function.
2530 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002531 FunctionDecl *ToFunction = 0;
2532 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2533 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2534 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002535 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002536 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002537 FromConstructor->isExplicit(),
2538 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002539 D->isImplicit(),
2540 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002541 } else if (isa<CXXDestructorDecl>(D)) {
2542 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2543 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002544 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002545 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002546 D->isInlineSpecified(),
2547 D->isImplicit());
2548 } else if (CXXConversionDecl *FromConversion
2549 = dyn_cast<CXXConversionDecl>(D)) {
2550 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2551 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002552 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002553 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002554 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002555 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002556 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002557 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002558 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2559 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2560 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002561 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002562 NameInfo, T, TInfo,
2563 Method->isStatic(),
2564 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002565 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002566 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002567 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002568 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002569 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002570 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002571 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002572 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002573 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002574 D->hasWrittenPrototype(),
2575 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002576 }
John McCall3e11ebe2010-03-15 10:12:16 +00002577
2578 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002579 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002580 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002581 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002582 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2583 ToFunction->setTrivial(D->isTrivial());
2584 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002585 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002586
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002587 // Set the parameters.
2588 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002589 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002590 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002591 }
David Blaikie9c70e042011-09-21 18:16:56 +00002592 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002593
2594 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002595
2596 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002597 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002598
Douglas Gregor43f54792010-02-17 02:12:47 +00002599 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002600}
2601
Douglas Gregor00eace12010-02-21 18:29:16 +00002602Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2603 return VisitFunctionDecl(D);
2604}
2605
2606Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2607 return VisitCXXMethodDecl(D);
2608}
2609
2610Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2611 return VisitCXXMethodDecl(D);
2612}
2613
2614Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2615 return VisitCXXMethodDecl(D);
2616}
2617
Douglas Gregor5c73e912010-02-11 00:48:18 +00002618Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2619 // Import the major distinguishing characteristics of a variable.
2620 DeclContext *DC, *LexicalDC;
2621 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002622 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002623 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2624 return 0;
2625
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002626 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002627 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2628 DC->localUncachedLookup(Name, FoundDecls);
2629 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2630 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002631 if (Importer.IsStructurallyEquivalent(D->getType(),
2632 FoundField->getType())) {
2633 Importer.Imported(D, FoundField);
2634 return FoundField;
2635 }
2636
2637 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2638 << Name << D->getType() << FoundField->getType();
2639 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2640 << FoundField->getType();
2641 return 0;
2642 }
2643 }
2644
Douglas Gregorb4964f72010-02-15 23:54:17 +00002645 // Import the type.
2646 QualType T = Importer.Import(D->getType());
2647 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002648 return 0;
2649
2650 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2651 Expr *BitWidth = Importer.Import(D->getBitWidth());
2652 if (!BitWidth && D->getBitWidth())
2653 return 0;
2654
Abramo Bagnaradff19302011-03-08 08:55:46 +00002655 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2656 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002657 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002658 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002659 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002660 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002661 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002662 if (ToField->hasInClassInitializer())
2663 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002664 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002665 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002666 return ToField;
2667}
2668
Francois Pichet783dd6e2010-11-21 06:08:52 +00002669Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2670 // Import the major distinguishing characteristics of a variable.
2671 DeclContext *DC, *LexicalDC;
2672 DeclarationName Name;
2673 SourceLocation Loc;
2674 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2675 return 0;
2676
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002677 // Determine whether we've already imported this field.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002678 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2679 DC->localUncachedLookup(Name, FoundDecls);
2680 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002681 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002682 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002683 if (Importer.IsStructurallyEquivalent(D->getType(),
2684 FoundField->getType())) {
2685 Importer.Imported(D, FoundField);
2686 return FoundField;
2687 }
2688
2689 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2690 << Name << D->getType() << FoundField->getType();
2691 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2692 << FoundField->getType();
2693 return 0;
2694 }
2695 }
2696
Francois Pichet783dd6e2010-11-21 06:08:52 +00002697 // Import the type.
2698 QualType T = Importer.Import(D->getType());
2699 if (T.isNull())
2700 return 0;
2701
2702 NamedDecl **NamedChain =
2703 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2704
2705 unsigned i = 0;
2706 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2707 PE = D->chain_end(); PI != PE; ++PI) {
2708 Decl* D = Importer.Import(*PI);
2709 if (!D)
2710 return 0;
2711 NamedChain[i++] = cast<NamedDecl>(D);
2712 }
2713
2714 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2715 Importer.getToContext(), DC,
2716 Loc, Name.getAsIdentifierInfo(), T,
2717 NamedChain, D->getChainingSize());
2718 ToIndirectField->setAccess(D->getAccess());
2719 ToIndirectField->setLexicalDeclContext(LexicalDC);
2720 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002721 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002722 return ToIndirectField;
2723}
2724
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002725Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2726 // Import the major distinguishing characteristics of an ivar.
2727 DeclContext *DC, *LexicalDC;
2728 DeclarationName Name;
2729 SourceLocation Loc;
2730 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2731 return 0;
2732
2733 // Determine whether we've already imported this ivar
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002734 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2735 DC->localUncachedLookup(Name, FoundDecls);
2736 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2737 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002738 if (Importer.IsStructurallyEquivalent(D->getType(),
2739 FoundIvar->getType())) {
2740 Importer.Imported(D, FoundIvar);
2741 return FoundIvar;
2742 }
2743
2744 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2745 << Name << D->getType() << FoundIvar->getType();
2746 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2747 << FoundIvar->getType();
2748 return 0;
2749 }
2750 }
2751
2752 // Import the type.
2753 QualType T = Importer.Import(D->getType());
2754 if (T.isNull())
2755 return 0;
2756
2757 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2758 Expr *BitWidth = Importer.Import(D->getBitWidth());
2759 if (!BitWidth && D->getBitWidth())
2760 return 0;
2761
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002762 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2763 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002764 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002765 Loc, Name.getAsIdentifierInfo(),
2766 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002767 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002768 ToIvar->setLexicalDeclContext(LexicalDC);
2769 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002770 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002771 return ToIvar;
2772
2773}
2774
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002775Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2776 // Import the major distinguishing characteristics of a variable.
2777 DeclContext *DC, *LexicalDC;
2778 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002779 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002780 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002781 return 0;
2782
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002783 // Try to find a variable in our own ("to") context with the same name and
2784 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002785 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002786 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002787 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002788 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002789 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2790 DC->localUncachedLookup(Name, FoundDecls);
2791 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2792 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002793 continue;
2794
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002795 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002796 // We have found a variable that we may need to merge with. Check it.
2797 if (isExternalLinkage(FoundVar->getLinkage()) &&
2798 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002799 if (Importer.IsStructurallyEquivalent(D->getType(),
2800 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002801 MergeWithVar = FoundVar;
2802 break;
2803 }
2804
Douglas Gregor56521c52010-02-12 17:23:39 +00002805 const ArrayType *FoundArray
2806 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2807 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002808 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002809 if (FoundArray && TArray) {
2810 if (isa<IncompleteArrayType>(FoundArray) &&
2811 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002812 // Import the type.
2813 QualType T = Importer.Import(D->getType());
2814 if (T.isNull())
2815 return 0;
2816
Douglas Gregor56521c52010-02-12 17:23:39 +00002817 FoundVar->setType(T);
2818 MergeWithVar = FoundVar;
2819 break;
2820 } else if (isa<IncompleteArrayType>(TArray) &&
2821 isa<ConstantArrayType>(FoundArray)) {
2822 MergeWithVar = FoundVar;
2823 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002824 }
2825 }
2826
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002827 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002828 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002829 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2830 << FoundVar->getType();
2831 }
2832 }
2833
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002834 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002835 }
2836
2837 if (MergeWithVar) {
2838 // An equivalent variable with external linkage has been found. Link
2839 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002840 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002841
2842 if (VarDecl *DDef = D->getDefinition()) {
2843 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2844 Importer.ToDiag(ExistingDef->getLocation(),
2845 diag::err_odr_variable_multiple_def)
2846 << Name;
2847 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2848 } else {
2849 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002850 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00002851 if (DDef->isInitKnownICE()) {
2852 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2853 Eval->CheckedICE = true;
2854 Eval->IsICE = DDef->isInitICE();
2855 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002856 }
2857 }
2858
2859 return MergeWithVar;
2860 }
2861
2862 if (!ConflictingDecls.empty()) {
2863 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2864 ConflictingDecls.data(),
2865 ConflictingDecls.size());
2866 if (!Name)
2867 return 0;
2868 }
2869 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002870
Douglas Gregorb4964f72010-02-15 23:54:17 +00002871 // Import the type.
2872 QualType T = Importer.Import(D->getType());
2873 if (T.isNull())
2874 return 0;
2875
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002876 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002877 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002878 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2879 Importer.Import(D->getInnerLocStart()),
2880 Loc, Name.getAsIdentifierInfo(),
2881 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002882 D->getStorageClass(),
2883 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002884 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002885 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002886 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002887 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002888 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002889
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002890 // Merge the initializer.
2891 // FIXME: Can we really import any initializer? Alternatively, we could force
2892 // ourselves to import every declaration of a variable and then only use
2893 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002894 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002895
2896 // FIXME: Other bits to merge?
2897
2898 return ToVar;
2899}
2900
Douglas Gregor8b228d72010-02-17 21:22:52 +00002901Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2902 // Parameters are created in the translation unit's context, then moved
2903 // into the function declaration's context afterward.
2904 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2905
2906 // Import the name of this declaration.
2907 DeclarationName Name = Importer.Import(D->getDeclName());
2908 if (D->getDeclName() && !Name)
2909 return 0;
2910
2911 // Import the location of this declaration.
2912 SourceLocation Loc = Importer.Import(D->getLocation());
2913
2914 // Import the parameter's type.
2915 QualType T = Importer.Import(D->getType());
2916 if (T.isNull())
2917 return 0;
2918
2919 // Create the imported parameter.
2920 ImplicitParamDecl *ToParm
2921 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2922 Loc, Name.getAsIdentifierInfo(),
2923 T);
2924 return Importer.Imported(D, ToParm);
2925}
2926
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002927Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2928 // Parameters are created in the translation unit's context, then moved
2929 // into the function declaration's context afterward.
2930 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2931
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002932 // Import the name of this declaration.
2933 DeclarationName Name = Importer.Import(D->getDeclName());
2934 if (D->getDeclName() && !Name)
2935 return 0;
2936
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002937 // Import the location of this declaration.
2938 SourceLocation Loc = Importer.Import(D->getLocation());
2939
2940 // Import the parameter's type.
2941 QualType T = Importer.Import(D->getType());
2942 if (T.isNull())
2943 return 0;
2944
2945 // Create the imported parameter.
2946 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2947 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002948 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002949 Loc, Name.getAsIdentifierInfo(),
2950 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002951 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002952 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002953 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002954 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002955}
2956
Douglas Gregor43f54792010-02-17 02:12:47 +00002957Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2958 // Import the major distinguishing characteristics of a method.
2959 DeclContext *DC, *LexicalDC;
2960 DeclarationName Name;
2961 SourceLocation Loc;
2962 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2963 return 0;
2964
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002965 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2966 DC->localUncachedLookup(Name, FoundDecls);
2967 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2968 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002969 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2970 continue;
2971
2972 // Check return types.
2973 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2974 FoundMethod->getResultType())) {
2975 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2976 << D->isInstanceMethod() << Name
2977 << D->getResultType() << FoundMethod->getResultType();
2978 Importer.ToDiag(FoundMethod->getLocation(),
2979 diag::note_odr_objc_method_here)
2980 << D->isInstanceMethod() << Name;
2981 return 0;
2982 }
2983
2984 // Check the number of parameters.
2985 if (D->param_size() != FoundMethod->param_size()) {
2986 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2987 << D->isInstanceMethod() << Name
2988 << D->param_size() << FoundMethod->param_size();
2989 Importer.ToDiag(FoundMethod->getLocation(),
2990 diag::note_odr_objc_method_here)
2991 << D->isInstanceMethod() << Name;
2992 return 0;
2993 }
2994
2995 // Check parameter types.
2996 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2997 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2998 P != PEnd; ++P, ++FoundP) {
2999 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3000 (*FoundP)->getType())) {
3001 Importer.FromDiag((*P)->getLocation(),
3002 diag::err_odr_objc_method_param_type_inconsistent)
3003 << D->isInstanceMethod() << Name
3004 << (*P)->getType() << (*FoundP)->getType();
3005 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3006 << (*FoundP)->getType();
3007 return 0;
3008 }
3009 }
3010
3011 // Check variadic/non-variadic.
3012 // Check the number of parameters.
3013 if (D->isVariadic() != FoundMethod->isVariadic()) {
3014 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3015 << D->isInstanceMethod() << Name;
3016 Importer.ToDiag(FoundMethod->getLocation(),
3017 diag::note_odr_objc_method_here)
3018 << D->isInstanceMethod() << Name;
3019 return 0;
3020 }
3021
3022 // FIXME: Any other bits we need to merge?
3023 return Importer.Imported(D, FoundMethod);
3024 }
3025 }
3026
3027 // Import the result type.
3028 QualType ResultTy = Importer.Import(D->getResultType());
3029 if (ResultTy.isNull())
3030 return 0;
3031
Douglas Gregor12852d92010-03-08 14:59:44 +00003032 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3033
Douglas Gregor43f54792010-02-17 02:12:47 +00003034 ObjCMethodDecl *ToMethod
3035 = ObjCMethodDecl::Create(Importer.getToContext(),
3036 Loc,
3037 Importer.Import(D->getLocEnd()),
3038 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003039 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003040 D->isInstanceMethod(),
3041 D->isVariadic(),
3042 D->isSynthesized(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003043 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003044 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003045 D->getImplementationControl(),
3046 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003047
3048 // FIXME: When we decide to merge method definitions, we'll need to
3049 // deal with implicit parameters.
3050
3051 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003052 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003053 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3054 FromPEnd = D->param_end();
3055 FromP != FromPEnd;
3056 ++FromP) {
3057 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3058 if (!ToP)
3059 return 0;
3060
3061 ToParams.push_back(ToP);
3062 }
3063
3064 // Set the parameters.
3065 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3066 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003067 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003068 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003069 SmallVector<SourceLocation, 12> SelLocs;
3070 D->getSelectorLocs(SelLocs);
3071 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003072
3073 ToMethod->setLexicalDeclContext(LexicalDC);
3074 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003075 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003076 return ToMethod;
3077}
3078
Douglas Gregor84c51c32010-02-18 01:47:50 +00003079Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3080 // Import the major distinguishing characteristics of a category.
3081 DeclContext *DC, *LexicalDC;
3082 DeclarationName Name;
3083 SourceLocation Loc;
3084 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3085 return 0;
3086
3087 ObjCInterfaceDecl *ToInterface
3088 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3089 if (!ToInterface)
3090 return 0;
3091
3092 // Determine if we've already encountered this category.
3093 ObjCCategoryDecl *MergeWithCategory
3094 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3095 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3096 if (!ToCategory) {
3097 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003098 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003099 Loc,
3100 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003101 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003102 ToInterface,
3103 Importer.Import(D->getIvarLBraceLoc()),
3104 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003105 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003106 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003107 Importer.Imported(D, ToCategory);
3108
Douglas Gregor84c51c32010-02-18 01:47:50 +00003109 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003110 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3111 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003112 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3113 = D->protocol_loc_begin();
3114 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3115 FromProtoEnd = D->protocol_end();
3116 FromProto != FromProtoEnd;
3117 ++FromProto, ++FromProtoLoc) {
3118 ObjCProtocolDecl *ToProto
3119 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3120 if (!ToProto)
3121 return 0;
3122 Protocols.push_back(ToProto);
3123 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3124 }
3125
3126 // FIXME: If we're merging, make sure that the protocol list is the same.
3127 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3128 ProtocolLocs.data(), Importer.getToContext());
3129
3130 } else {
3131 Importer.Imported(D, ToCategory);
3132 }
3133
3134 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003135 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003136
3137 // If we have an implementation, import it as well.
3138 if (D->getImplementation()) {
3139 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003140 = cast_or_null<ObjCCategoryImplDecl>(
3141 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003142 if (!Impl)
3143 return 0;
3144
3145 ToCategory->setImplementation(Impl);
3146 }
3147
3148 return ToCategory;
3149}
3150
Douglas Gregor2aa53772012-01-24 17:42:07 +00003151bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3152 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003153 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003154 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003155 if (shouldForceImportDeclContext(Kind))
3156 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003157 return false;
3158 }
3159
3160 // Start the protocol definition
3161 To->startDefinition();
3162
3163 // Import protocols
3164 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3165 SmallVector<SourceLocation, 4> ProtocolLocs;
3166 ObjCProtocolDecl::protocol_loc_iterator
3167 FromProtoLoc = From->protocol_loc_begin();
3168 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3169 FromProtoEnd = From->protocol_end();
3170 FromProto != FromProtoEnd;
3171 ++FromProto, ++FromProtoLoc) {
3172 ObjCProtocolDecl *ToProto
3173 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3174 if (!ToProto)
3175 return true;
3176 Protocols.push_back(ToProto);
3177 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3178 }
3179
3180 // FIXME: If we're merging, make sure that the protocol list is the same.
3181 To->setProtocolList(Protocols.data(), Protocols.size(),
3182 ProtocolLocs.data(), Importer.getToContext());
3183
Douglas Gregor2e15c842012-02-01 21:00:38 +00003184 if (shouldForceImportDeclContext(Kind)) {
3185 // Import all of the members of this protocol.
3186 ImportDeclContext(From, /*ForceImport=*/true);
3187 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003188 return false;
3189}
3190
Douglas Gregor98d156a2010-02-17 16:12:00 +00003191Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003192 // If this protocol has a definition in the translation unit we're coming
3193 // from, but this particular declaration is not that definition, import the
3194 // definition and map to that.
3195 ObjCProtocolDecl *Definition = D->getDefinition();
3196 if (Definition && Definition != D) {
3197 Decl *ImportedDef = Importer.Import(Definition);
3198 if (!ImportedDef)
3199 return 0;
3200
3201 return Importer.Imported(D, ImportedDef);
3202 }
3203
Douglas Gregor84c51c32010-02-18 01:47:50 +00003204 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003205 DeclContext *DC, *LexicalDC;
3206 DeclarationName Name;
3207 SourceLocation Loc;
3208 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3209 return 0;
3210
3211 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003212 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3213 DC->localUncachedLookup(Name, FoundDecls);
3214 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3215 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003216 continue;
3217
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003218 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003219 break;
3220 }
3221
3222 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003223 if (!ToProto) {
3224 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3225 Name.getAsIdentifierInfo(), Loc,
3226 Importer.Import(D->getAtStartLoc()),
3227 /*PrevDecl=*/0);
3228 ToProto->setLexicalDeclContext(LexicalDC);
3229 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003230 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003231
3232 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003233
Douglas Gregor2aa53772012-01-24 17:42:07 +00003234 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3235 return 0;
3236
Douglas Gregor98d156a2010-02-17 16:12:00 +00003237 return ToProto;
3238}
3239
Douglas Gregor2aa53772012-01-24 17:42:07 +00003240bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3241 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003242 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003243 if (To->getDefinition()) {
3244 // Check consistency of superclass.
3245 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3246 if (FromSuper) {
3247 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3248 if (!FromSuper)
3249 return true;
3250 }
3251
3252 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3253 if ((bool)FromSuper != (bool)ToSuper ||
3254 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3255 Importer.ToDiag(To->getLocation(),
3256 diag::err_odr_objc_superclass_inconsistent)
3257 << To->getDeclName();
3258 if (ToSuper)
3259 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3260 << To->getSuperClass()->getDeclName();
3261 else
3262 Importer.ToDiag(To->getLocation(),
3263 diag::note_odr_objc_missing_superclass);
3264 if (From->getSuperClass())
3265 Importer.FromDiag(From->getSuperClassLoc(),
3266 diag::note_odr_objc_superclass)
3267 << From->getSuperClass()->getDeclName();
3268 else
3269 Importer.FromDiag(From->getLocation(),
3270 diag::note_odr_objc_missing_superclass);
3271 }
3272
Douglas Gregor2e15c842012-02-01 21:00:38 +00003273 if (shouldForceImportDeclContext(Kind))
3274 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003275 return false;
3276 }
3277
3278 // Start the definition.
3279 To->startDefinition();
3280
3281 // If this class has a superclass, import it.
3282 if (From->getSuperClass()) {
3283 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3284 Importer.Import(From->getSuperClass()));
3285 if (!Super)
3286 return true;
3287
3288 To->setSuperClass(Super);
3289 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3290 }
3291
3292 // Import protocols
3293 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3294 SmallVector<SourceLocation, 4> ProtocolLocs;
3295 ObjCInterfaceDecl::protocol_loc_iterator
3296 FromProtoLoc = From->protocol_loc_begin();
3297
3298 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3299 FromProtoEnd = From->protocol_end();
3300 FromProto != FromProtoEnd;
3301 ++FromProto, ++FromProtoLoc) {
3302 ObjCProtocolDecl *ToProto
3303 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3304 if (!ToProto)
3305 return true;
3306 Protocols.push_back(ToProto);
3307 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3308 }
3309
3310 // FIXME: If we're merging, make sure that the protocol list is the same.
3311 To->setProtocolList(Protocols.data(), Protocols.size(),
3312 ProtocolLocs.data(), Importer.getToContext());
3313
3314 // Import categories. When the categories themselves are imported, they'll
3315 // hook themselves into this interface.
3316 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3317 FromCat = FromCat->getNextClassCategory())
3318 Importer.Import(FromCat);
3319
3320 // If we have an @implementation, import it as well.
3321 if (From->getImplementation()) {
3322 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3323 Importer.Import(From->getImplementation()));
3324 if (!Impl)
3325 return true;
3326
3327 To->setImplementation(Impl);
3328 }
3329
Douglas Gregor2e15c842012-02-01 21:00:38 +00003330 if (shouldForceImportDeclContext(Kind)) {
3331 // Import all of the members of this class.
3332 ImportDeclContext(From, /*ForceImport=*/true);
3333 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003334 return false;
3335}
3336
Douglas Gregor45635322010-02-16 01:20:57 +00003337Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003338 // If this class has a definition in the translation unit we're coming from,
3339 // but this particular declaration is not that definition, import the
3340 // definition and map to that.
3341 ObjCInterfaceDecl *Definition = D->getDefinition();
3342 if (Definition && Definition != D) {
3343 Decl *ImportedDef = Importer.Import(Definition);
3344 if (!ImportedDef)
3345 return 0;
3346
3347 return Importer.Imported(D, ImportedDef);
3348 }
3349
Douglas Gregor45635322010-02-16 01:20:57 +00003350 // Import the major distinguishing characteristics of an @interface.
3351 DeclContext *DC, *LexicalDC;
3352 DeclarationName Name;
3353 SourceLocation Loc;
3354 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3355 return 0;
3356
Douglas Gregor2aa53772012-01-24 17:42:07 +00003357 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003358 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003359 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3360 DC->localUncachedLookup(Name, FoundDecls);
3361 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3362 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003363 continue;
3364
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003365 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003366 break;
3367 }
3368
Douglas Gregor2aa53772012-01-24 17:42:07 +00003369 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003370 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003371 if (!ToIface) {
3372 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3373 Importer.Import(D->getAtStartLoc()),
3374 Name.getAsIdentifierInfo(),
3375 /*PrevDecl=*/0,Loc,
3376 D->isImplicitInterfaceDecl());
3377 ToIface->setLexicalDeclContext(LexicalDC);
3378 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003379 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003380 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003381
Douglas Gregor2aa53772012-01-24 17:42:07 +00003382 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3383 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003384
Douglas Gregor98d156a2010-02-17 16:12:00 +00003385 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003386}
3387
Douglas Gregor4da9d682010-12-07 15:32:12 +00003388Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3389 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3390 Importer.Import(D->getCategoryDecl()));
3391 if (!Category)
3392 return 0;
3393
3394 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3395 if (!ToImpl) {
3396 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3397 if (!DC)
3398 return 0;
3399
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003400 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003401 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003402 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003403 Category->getClassInterface(),
3404 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003405 Importer.Import(D->getAtStartLoc()),
3406 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003407
3408 DeclContext *LexicalDC = DC;
3409 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3410 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3411 if (!LexicalDC)
3412 return 0;
3413
3414 ToImpl->setLexicalDeclContext(LexicalDC);
3415 }
3416
Sean Callanan95e74be2011-10-21 02:57:43 +00003417 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003418 Category->setImplementation(ToImpl);
3419 }
3420
3421 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003422 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003423 return ToImpl;
3424}
3425
Douglas Gregorda8025c2010-12-07 01:26:03 +00003426Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3427 // Find the corresponding interface.
3428 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3429 Importer.Import(D->getClassInterface()));
3430 if (!Iface)
3431 return 0;
3432
3433 // Import the superclass, if any.
3434 ObjCInterfaceDecl *Super = 0;
3435 if (D->getSuperClass()) {
3436 Super = cast_or_null<ObjCInterfaceDecl>(
3437 Importer.Import(D->getSuperClass()));
3438 if (!Super)
3439 return 0;
3440 }
3441
3442 ObjCImplementationDecl *Impl = Iface->getImplementation();
3443 if (!Impl) {
3444 // We haven't imported an implementation yet. Create a new @implementation
3445 // now.
3446 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3447 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003448 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003449 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003450 Importer.Import(D->getAtStartLoc()),
3451 Importer.Import(D->getIvarLBraceLoc()),
3452 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003453
3454 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3455 DeclContext *LexicalDC
3456 = Importer.ImportContext(D->getLexicalDeclContext());
3457 if (!LexicalDC)
3458 return 0;
3459 Impl->setLexicalDeclContext(LexicalDC);
3460 }
3461
3462 // Associate the implementation with the class it implements.
3463 Iface->setImplementation(Impl);
3464 Importer.Imported(D, Iface->getImplementation());
3465 } else {
3466 Importer.Imported(D, Iface->getImplementation());
3467
3468 // Verify that the existing @implementation has the same superclass.
3469 if ((Super && !Impl->getSuperClass()) ||
3470 (!Super && Impl->getSuperClass()) ||
3471 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003472 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003473 Importer.ToDiag(Impl->getLocation(),
3474 diag::err_odr_objc_superclass_inconsistent)
3475 << Iface->getDeclName();
3476 // FIXME: It would be nice to have the location of the superclass
3477 // below.
3478 if (Impl->getSuperClass())
3479 Importer.ToDiag(Impl->getLocation(),
3480 diag::note_odr_objc_superclass)
3481 << Impl->getSuperClass()->getDeclName();
3482 else
3483 Importer.ToDiag(Impl->getLocation(),
3484 diag::note_odr_objc_missing_superclass);
3485 if (D->getSuperClass())
3486 Importer.FromDiag(D->getLocation(),
3487 diag::note_odr_objc_superclass)
3488 << D->getSuperClass()->getDeclName();
3489 else
3490 Importer.FromDiag(D->getLocation(),
3491 diag::note_odr_objc_missing_superclass);
3492 return 0;
3493 }
3494 }
3495
3496 // Import all of the members of this @implementation.
3497 ImportDeclContext(D);
3498
3499 return Impl;
3500}
3501
Douglas Gregora11c4582010-02-17 18:02:10 +00003502Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3503 // Import the major distinguishing characteristics of an @property.
3504 DeclContext *DC, *LexicalDC;
3505 DeclarationName Name;
3506 SourceLocation Loc;
3507 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3508 return 0;
3509
3510 // Check whether we have already imported this property.
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003511 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3512 DC->localUncachedLookup(Name, FoundDecls);
3513 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003514 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003515 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003516 // Check property types.
3517 if (!Importer.IsStructurallyEquivalent(D->getType(),
3518 FoundProp->getType())) {
3519 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3520 << Name << D->getType() << FoundProp->getType();
3521 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3522 << FoundProp->getType();
3523 return 0;
3524 }
3525
3526 // FIXME: Check property attributes, getters, setters, etc.?
3527
3528 // Consider these properties to be equivalent.
3529 Importer.Imported(D, FoundProp);
3530 return FoundProp;
3531 }
3532 }
3533
3534 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003535 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3536 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003537 return 0;
3538
3539 // Create the new property.
3540 ObjCPropertyDecl *ToProperty
3541 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3542 Name.getAsIdentifierInfo(),
3543 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003544 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003545 T,
3546 D->getPropertyImplementation());
3547 Importer.Imported(D, ToProperty);
3548 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003549 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003550
3551 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003552 ToProperty->setPropertyAttributesAsWritten(
3553 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003554 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3555 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3556 ToProperty->setGetterMethodDecl(
3557 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3558 ToProperty->setSetterMethodDecl(
3559 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3560 ToProperty->setPropertyIvarDecl(
3561 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3562 return ToProperty;
3563}
3564
Douglas Gregor14a49e22010-12-07 18:32:03 +00003565Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3566 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3567 Importer.Import(D->getPropertyDecl()));
3568 if (!Property)
3569 return 0;
3570
3571 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3572 if (!DC)
3573 return 0;
3574
3575 // Import the lexical declaration context.
3576 DeclContext *LexicalDC = DC;
3577 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3578 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3579 if (!LexicalDC)
3580 return 0;
3581 }
3582
3583 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3584 if (!InImpl)
3585 return 0;
3586
3587 // Import the ivar (for an @synthesize).
3588 ObjCIvarDecl *Ivar = 0;
3589 if (D->getPropertyIvarDecl()) {
3590 Ivar = cast_or_null<ObjCIvarDecl>(
3591 Importer.Import(D->getPropertyIvarDecl()));
3592 if (!Ivar)
3593 return 0;
3594 }
3595
3596 ObjCPropertyImplDecl *ToImpl
3597 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3598 if (!ToImpl) {
3599 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3600 Importer.Import(D->getLocStart()),
3601 Importer.Import(D->getLocation()),
3602 Property,
3603 D->getPropertyImplementation(),
3604 Ivar,
3605 Importer.Import(D->getPropertyIvarDeclLoc()));
3606 ToImpl->setLexicalDeclContext(LexicalDC);
3607 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003608 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003609 } else {
3610 // Check that we have the same kind of property implementation (@synthesize
3611 // vs. @dynamic).
3612 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3613 Importer.ToDiag(ToImpl->getLocation(),
3614 diag::err_odr_objc_property_impl_kind_inconsistent)
3615 << Property->getDeclName()
3616 << (ToImpl->getPropertyImplementation()
3617 == ObjCPropertyImplDecl::Dynamic);
3618 Importer.FromDiag(D->getLocation(),
3619 diag::note_odr_objc_property_impl_kind)
3620 << D->getPropertyDecl()->getDeclName()
3621 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3622 return 0;
3623 }
3624
3625 // For @synthesize, check that we have the same
3626 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3627 Ivar != ToImpl->getPropertyIvarDecl()) {
3628 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3629 diag::err_odr_objc_synthesize_ivar_inconsistent)
3630 << Property->getDeclName()
3631 << ToImpl->getPropertyIvarDecl()->getDeclName()
3632 << Ivar->getDeclName();
3633 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3634 diag::note_odr_objc_synthesize_ivar_here)
3635 << D->getPropertyIvarDecl()->getDeclName();
3636 return 0;
3637 }
3638
3639 // Merge the existing implementation with the new implementation.
3640 Importer.Imported(D, ToImpl);
3641 }
3642
3643 return ToImpl;
3644}
3645
Douglas Gregora082a492010-11-30 19:14:50 +00003646Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3647 // For template arguments, we adopt the translation unit as our declaration
3648 // context. This context will be fixed when the actual template declaration
3649 // is created.
3650
3651 // FIXME: Import default argument.
3652 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3653 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003654 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003655 Importer.Import(D->getLocation()),
3656 D->getDepth(),
3657 D->getIndex(),
3658 Importer.Import(D->getIdentifier()),
3659 D->wasDeclaredWithTypename(),
3660 D->isParameterPack());
3661}
3662
3663Decl *
3664ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3665 // Import the name of this declaration.
3666 DeclarationName Name = Importer.Import(D->getDeclName());
3667 if (D->getDeclName() && !Name)
3668 return 0;
3669
3670 // Import the location of this declaration.
3671 SourceLocation Loc = Importer.Import(D->getLocation());
3672
3673 // Import the type of this declaration.
3674 QualType T = Importer.Import(D->getType());
3675 if (T.isNull())
3676 return 0;
3677
3678 // Import type-source information.
3679 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3680 if (D->getTypeSourceInfo() && !TInfo)
3681 return 0;
3682
3683 // FIXME: Import default argument.
3684
3685 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3686 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003687 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003688 Loc, D->getDepth(), D->getPosition(),
3689 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003690 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003691}
3692
3693Decl *
3694ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3695 // Import the name of this declaration.
3696 DeclarationName Name = Importer.Import(D->getDeclName());
3697 if (D->getDeclName() && !Name)
3698 return 0;
3699
3700 // Import the location of this declaration.
3701 SourceLocation Loc = Importer.Import(D->getLocation());
3702
3703 // Import template parameters.
3704 TemplateParameterList *TemplateParams
3705 = ImportTemplateParameterList(D->getTemplateParameters());
3706 if (!TemplateParams)
3707 return 0;
3708
3709 // FIXME: Import default argument.
3710
3711 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3712 Importer.getToContext().getTranslationUnitDecl(),
3713 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003714 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003715 Name.getAsIdentifierInfo(),
3716 TemplateParams);
3717}
3718
3719Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3720 // If this record has a definition in the translation unit we're coming from,
3721 // but this particular declaration is not that definition, import the
3722 // definition and map to that.
3723 CXXRecordDecl *Definition
3724 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3725 if (Definition && Definition != D->getTemplatedDecl()) {
3726 Decl *ImportedDef
3727 = Importer.Import(Definition->getDescribedClassTemplate());
3728 if (!ImportedDef)
3729 return 0;
3730
3731 return Importer.Imported(D, ImportedDef);
3732 }
3733
3734 // Import the major distinguishing characteristics of this class template.
3735 DeclContext *DC, *LexicalDC;
3736 DeclarationName Name;
3737 SourceLocation Loc;
3738 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3739 return 0;
3740
3741 // We may already have a template of the same name; try to find and match it.
3742 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003743 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003744 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3745 DC->localUncachedLookup(Name, FoundDecls);
3746 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3747 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003748 continue;
3749
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003750 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003751 if (ClassTemplateDecl *FoundTemplate
3752 = dyn_cast<ClassTemplateDecl>(Found)) {
3753 if (IsStructuralMatch(D, FoundTemplate)) {
3754 // The class templates structurally match; call it the same template.
3755 // FIXME: We may be filling in a forward declaration here. Handle
3756 // this case!
3757 Importer.Imported(D->getTemplatedDecl(),
3758 FoundTemplate->getTemplatedDecl());
3759 return Importer.Imported(D, FoundTemplate);
3760 }
3761 }
3762
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003763 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003764 }
3765
3766 if (!ConflictingDecls.empty()) {
3767 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3768 ConflictingDecls.data(),
3769 ConflictingDecls.size());
3770 }
3771
3772 if (!Name)
3773 return 0;
3774 }
3775
3776 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3777
3778 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003779 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3780 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003781 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3782 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003783 DC, StartLoc, IdLoc,
3784 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003785 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003786 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003787 D2Templated->setLexicalDeclContext(LexicalDC);
3788
3789 // Create the class template declaration itself.
3790 TemplateParameterList *TemplateParams
3791 = ImportTemplateParameterList(D->getTemplateParameters());
3792 if (!TemplateParams)
3793 return 0;
3794
3795 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3796 Loc, Name, TemplateParams,
3797 D2Templated,
3798 /*PrevDecl=*/0);
3799 D2Templated->setDescribedClassTemplate(D2);
3800
3801 D2->setAccess(D->getAccess());
3802 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003803 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00003804
3805 // Note the relationship between the class templates.
3806 Importer.Imported(D, D2);
3807 Importer.Imported(DTemplated, D2Templated);
3808
John McCallf937c022011-10-07 06:10:15 +00003809 if (DTemplated->isCompleteDefinition() &&
3810 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00003811 // FIXME: Import definition!
3812 }
3813
3814 return D2;
3815}
3816
Douglas Gregore2e50d332010-12-01 01:36:18 +00003817Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3818 ClassTemplateSpecializationDecl *D) {
3819 // If this record has a definition in the translation unit we're coming from,
3820 // but this particular declaration is not that definition, import the
3821 // definition and map to that.
3822 TagDecl *Definition = D->getDefinition();
3823 if (Definition && Definition != D) {
3824 Decl *ImportedDef = Importer.Import(Definition);
3825 if (!ImportedDef)
3826 return 0;
3827
3828 return Importer.Imported(D, ImportedDef);
3829 }
3830
3831 ClassTemplateDecl *ClassTemplate
3832 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3833 D->getSpecializedTemplate()));
3834 if (!ClassTemplate)
3835 return 0;
3836
3837 // Import the context of this declaration.
3838 DeclContext *DC = ClassTemplate->getDeclContext();
3839 if (!DC)
3840 return 0;
3841
3842 DeclContext *LexicalDC = DC;
3843 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3844 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3845 if (!LexicalDC)
3846 return 0;
3847 }
3848
3849 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003850 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3851 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003852
3853 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003854 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00003855 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3856 D->getTemplateArgs().size(),
3857 TemplateArgs))
3858 return 0;
3859
3860 // Try to find an existing specialization with these template arguments.
3861 void *InsertPos = 0;
3862 ClassTemplateSpecializationDecl *D2
3863 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3864 TemplateArgs.size(), InsertPos);
3865 if (D2) {
3866 // We already have a class template specialization with these template
3867 // arguments.
3868
3869 // FIXME: Check for specialization vs. instantiation errors.
3870
3871 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00003872 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00003873 // The record types structurally match, or the "from" translation
3874 // unit only had a forward declaration anyway; call it the same
3875 // function.
3876 return Importer.Imported(D, FoundDef);
3877 }
3878 }
3879 } else {
3880 // Create a new specialization.
3881 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3882 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003883 StartLoc, IdLoc,
3884 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003885 TemplateArgs.data(),
3886 TemplateArgs.size(),
3887 /*PrevDecl=*/0);
3888 D2->setSpecializationKind(D->getSpecializationKind());
3889
3890 // Add this specialization to the class template.
3891 ClassTemplate->AddSpecialization(D2, InsertPos);
3892
3893 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003894 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003895
3896 // Add the specialization to this context.
3897 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003898 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00003899 }
3900 Importer.Imported(D, D2);
3901
John McCallf937c022011-10-07 06:10:15 +00003902 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00003903 return 0;
3904
3905 return D2;
3906}
3907
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003908//----------------------------------------------------------------------------
3909// Import Statements
3910//----------------------------------------------------------------------------
3911
3912Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3913 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3914 << S->getStmtClassName();
3915 return 0;
3916}
3917
3918//----------------------------------------------------------------------------
3919// Import Expressions
3920//----------------------------------------------------------------------------
3921Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3922 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3923 << E->getStmtClassName();
3924 return 0;
3925}
3926
Douglas Gregor52f820e2010-02-19 01:17:02 +00003927Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003928 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3929 if (!ToD)
3930 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003931
3932 NamedDecl *FoundD = 0;
3933 if (E->getDecl() != E->getFoundDecl()) {
3934 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3935 if (!FoundD)
3936 return 0;
3937 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003938
3939 QualType T = Importer.Import(E->getType());
3940 if (T.isNull())
3941 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003942
3943 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3944 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00003945 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003946 ToD,
John McCall113bee02012-03-10 09:33:50 +00003947 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00003948 Importer.Import(E->getLocation()),
3949 T, E->getValueKind(),
3950 FoundD,
3951 /*FIXME:TemplateArgs=*/0);
3952 if (E->hadMultipleCandidates())
3953 DRE->setHadMultipleCandidates(true);
3954 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00003955}
3956
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003957Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3958 QualType T = Importer.Import(E->getType());
3959 if (T.isNull())
3960 return 0;
3961
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003962 return IntegerLiteral::Create(Importer.getToContext(),
3963 E->getValue(), T,
3964 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003965}
3966
Douglas Gregor623421d2010-02-18 02:21:22 +00003967Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3968 QualType T = Importer.Import(E->getType());
3969 if (T.isNull())
3970 return 0;
3971
Douglas Gregorfb65e592011-07-27 05:40:30 +00003972 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3973 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00003974 Importer.Import(E->getLocation()));
3975}
3976
Douglas Gregorc74247e2010-02-19 01:07:06 +00003977Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3978 Expr *SubExpr = Importer.Import(E->getSubExpr());
3979 if (!SubExpr)
3980 return 0;
3981
3982 return new (Importer.getToContext())
3983 ParenExpr(Importer.Import(E->getLParen()),
3984 Importer.Import(E->getRParen()),
3985 SubExpr);
3986}
3987
3988Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3989 QualType T = Importer.Import(E->getType());
3990 if (T.isNull())
3991 return 0;
3992
3993 Expr *SubExpr = Importer.Import(E->getSubExpr());
3994 if (!SubExpr)
3995 return 0;
3996
3997 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003998 T, E->getValueKind(),
3999 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004000 Importer.Import(E->getOperatorLoc()));
4001}
4002
Peter Collingbournee190dee2011-03-11 19:24:49 +00004003Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4004 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004005 QualType ResultType = Importer.Import(E->getType());
4006
4007 if (E->isArgumentType()) {
4008 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4009 if (!TInfo)
4010 return 0;
4011
Peter Collingbournee190dee2011-03-11 19:24:49 +00004012 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4013 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004014 Importer.Import(E->getOperatorLoc()),
4015 Importer.Import(E->getRParenLoc()));
4016 }
4017
4018 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4019 if (!SubExpr)
4020 return 0;
4021
Peter Collingbournee190dee2011-03-11 19:24:49 +00004022 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4023 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004024 Importer.Import(E->getOperatorLoc()),
4025 Importer.Import(E->getRParenLoc()));
4026}
4027
Douglas Gregorc74247e2010-02-19 01:07:06 +00004028Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4029 QualType T = Importer.Import(E->getType());
4030 if (T.isNull())
4031 return 0;
4032
4033 Expr *LHS = Importer.Import(E->getLHS());
4034 if (!LHS)
4035 return 0;
4036
4037 Expr *RHS = Importer.Import(E->getRHS());
4038 if (!RHS)
4039 return 0;
4040
4041 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004042 T, E->getValueKind(),
4043 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004044 Importer.Import(E->getOperatorLoc()));
4045}
4046
4047Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4048 QualType T = Importer.Import(E->getType());
4049 if (T.isNull())
4050 return 0;
4051
4052 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4053 if (CompLHSType.isNull())
4054 return 0;
4055
4056 QualType CompResultType = Importer.Import(E->getComputationResultType());
4057 if (CompResultType.isNull())
4058 return 0;
4059
4060 Expr *LHS = Importer.Import(E->getLHS());
4061 if (!LHS)
4062 return 0;
4063
4064 Expr *RHS = Importer.Import(E->getRHS());
4065 if (!RHS)
4066 return 0;
4067
4068 return new (Importer.getToContext())
4069 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004070 T, E->getValueKind(),
4071 E->getObjectKind(),
4072 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00004073 Importer.Import(E->getOperatorLoc()));
4074}
4075
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004076static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004077 if (E->path_empty()) return false;
4078
4079 // TODO: import cast paths
4080 return true;
4081}
4082
Douglas Gregor98c10182010-02-12 22:17:39 +00004083Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4084 QualType T = Importer.Import(E->getType());
4085 if (T.isNull())
4086 return 0;
4087
4088 Expr *SubExpr = Importer.Import(E->getSubExpr());
4089 if (!SubExpr)
4090 return 0;
John McCallcf142162010-08-07 06:22:56 +00004091
4092 CXXCastPath BasePath;
4093 if (ImportCastPath(E, BasePath))
4094 return 0;
4095
4096 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004097 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004098}
4099
Douglas Gregor5481d322010-02-19 01:32:14 +00004100Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4101 QualType T = Importer.Import(E->getType());
4102 if (T.isNull())
4103 return 0;
4104
4105 Expr *SubExpr = Importer.Import(E->getSubExpr());
4106 if (!SubExpr)
4107 return 0;
4108
4109 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4110 if (!TInfo && E->getTypeInfoAsWritten())
4111 return 0;
4112
John McCallcf142162010-08-07 06:22:56 +00004113 CXXCastPath BasePath;
4114 if (ImportCastPath(E, BasePath))
4115 return 0;
4116
John McCall7decc9e2010-11-18 06:31:45 +00004117 return CStyleCastExpr::Create(Importer.getToContext(), T,
4118 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004119 SubExpr, &BasePath, TInfo,
4120 Importer.Import(E->getLParenLoc()),
4121 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004122}
4123
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004124ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004125 ASTContext &FromContext, FileManager &FromFileManager,
4126 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004127 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004128 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4129 Minimal(MinimalImport)
4130{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004131 ImportedDecls[FromContext.getTranslationUnitDecl()]
4132 = ToContext.getTranslationUnitDecl();
4133}
4134
4135ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004136
4137QualType ASTImporter::Import(QualType FromT) {
4138 if (FromT.isNull())
4139 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004140
4141 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004142
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004143 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004144 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4145 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004146 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004147 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004148
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004149 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004150 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004151 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004152 if (ToT.isNull())
4153 return ToT;
4154
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004155 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004156 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004157
John McCall424cec92011-01-19 06:33:43 +00004158 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004159}
4160
Douglas Gregor62d311f2010-02-09 19:21:46 +00004161TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004162 if (!FromTSI)
4163 return FromTSI;
4164
4165 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004166 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004167 QualType T = Import(FromTSI->getType());
4168 if (T.isNull())
4169 return 0;
4170
4171 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004172 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004173}
4174
4175Decl *ASTImporter::Import(Decl *FromD) {
4176 if (!FromD)
4177 return 0;
4178
Douglas Gregord451ea92011-07-29 23:31:30 +00004179 ASTNodeImporter Importer(*this);
4180
Douglas Gregor62d311f2010-02-09 19:21:46 +00004181 // Check whether we've already imported this declaration.
4182 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004183 if (Pos != ImportedDecls.end()) {
4184 Decl *ToD = Pos->second;
4185 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4186 return ToD;
4187 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004188
4189 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004190 Decl *ToD = Importer.Visit(FromD);
4191 if (!ToD)
4192 return 0;
4193
4194 // Record the imported declaration.
4195 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004196
4197 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4198 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004199 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004200 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004201 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004202 // When we've finished transforming a typedef, see whether it was the
4203 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004204 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004205 FromTag = AnonTagsWithPendingTypedefs.begin(),
4206 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4207 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004208 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004209 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4210 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004211 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004212 AnonTagsWithPendingTypedefs.erase(FromTag);
4213 break;
4214 }
4215 }
4216 }
4217 }
4218
Douglas Gregor62d311f2010-02-09 19:21:46 +00004219 return ToD;
4220}
4221
4222DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4223 if (!FromDC)
4224 return FromDC;
4225
Douglas Gregor95d82832012-01-24 18:36:04 +00004226 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004227 if (!ToDC)
4228 return 0;
4229
4230 // When we're using a record/enum/Objective-C class/protocol as a context, we
4231 // need it to have a definition.
4232 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004233 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004234 if (ToRecord->isCompleteDefinition()) {
4235 // Do nothing.
4236 } else if (FromRecord->isCompleteDefinition()) {
4237 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4238 ASTNodeImporter::IDK_Basic);
4239 } else {
4240 CompleteDecl(ToRecord);
4241 }
4242 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4243 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4244 if (ToEnum->isCompleteDefinition()) {
4245 // Do nothing.
4246 } else if (FromEnum->isCompleteDefinition()) {
4247 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4248 ASTNodeImporter::IDK_Basic);
4249 } else {
4250 CompleteDecl(ToEnum);
4251 }
4252 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4253 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4254 if (ToClass->getDefinition()) {
4255 // Do nothing.
4256 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4257 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4258 ASTNodeImporter::IDK_Basic);
4259 } else {
4260 CompleteDecl(ToClass);
4261 }
4262 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4263 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4264 if (ToProto->getDefinition()) {
4265 // Do nothing.
4266 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4267 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4268 ASTNodeImporter::IDK_Basic);
4269 } else {
4270 CompleteDecl(ToProto);
4271 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004272 }
4273
4274 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004275}
4276
4277Expr *ASTImporter::Import(Expr *FromE) {
4278 if (!FromE)
4279 return 0;
4280
4281 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4282}
4283
4284Stmt *ASTImporter::Import(Stmt *FromS) {
4285 if (!FromS)
4286 return 0;
4287
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004288 // Check whether we've already imported this declaration.
4289 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4290 if (Pos != ImportedStmts.end())
4291 return Pos->second;
4292
4293 // Import the type
4294 ASTNodeImporter Importer(*this);
4295 Stmt *ToS = Importer.Visit(FromS);
4296 if (!ToS)
4297 return 0;
4298
4299 // Record the imported declaration.
4300 ImportedStmts[FromS] = ToS;
4301 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004302}
4303
4304NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4305 if (!FromNNS)
4306 return 0;
4307
Douglas Gregor90ebf252011-04-27 16:48:40 +00004308 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4309
4310 switch (FromNNS->getKind()) {
4311 case NestedNameSpecifier::Identifier:
4312 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4313 return NestedNameSpecifier::Create(ToContext, prefix, II);
4314 }
4315 return 0;
4316
4317 case NestedNameSpecifier::Namespace:
4318 if (NamespaceDecl *NS =
4319 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4320 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4321 }
4322 return 0;
4323
4324 case NestedNameSpecifier::NamespaceAlias:
4325 if (NamespaceAliasDecl *NSAD =
4326 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4327 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4328 }
4329 return 0;
4330
4331 case NestedNameSpecifier::Global:
4332 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4333
4334 case NestedNameSpecifier::TypeSpec:
4335 case NestedNameSpecifier::TypeSpecWithTemplate: {
4336 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4337 if (!T.isNull()) {
4338 bool bTemplate = FromNNS->getKind() ==
4339 NestedNameSpecifier::TypeSpecWithTemplate;
4340 return NestedNameSpecifier::Create(ToContext, prefix,
4341 bTemplate, T.getTypePtr());
4342 }
4343 }
4344 return 0;
4345 }
4346
4347 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004348}
4349
Douglas Gregor14454802011-02-25 02:25:35 +00004350NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4351 // FIXME: Implement!
4352 return NestedNameSpecifierLoc();
4353}
4354
Douglas Gregore2e50d332010-12-01 01:36:18 +00004355TemplateName ASTImporter::Import(TemplateName From) {
4356 switch (From.getKind()) {
4357 case TemplateName::Template:
4358 if (TemplateDecl *ToTemplate
4359 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4360 return TemplateName(ToTemplate);
4361
4362 return TemplateName();
4363
4364 case TemplateName::OverloadedTemplate: {
4365 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4366 UnresolvedSet<2> ToTemplates;
4367 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4368 E = FromStorage->end();
4369 I != E; ++I) {
4370 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4371 ToTemplates.addDecl(To);
4372 else
4373 return TemplateName();
4374 }
4375 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4376 ToTemplates.end());
4377 }
4378
4379 case TemplateName::QualifiedTemplate: {
4380 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4381 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4382 if (!Qualifier)
4383 return TemplateName();
4384
4385 if (TemplateDecl *ToTemplate
4386 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4387 return ToContext.getQualifiedTemplateName(Qualifier,
4388 QTN->hasTemplateKeyword(),
4389 ToTemplate);
4390
4391 return TemplateName();
4392 }
4393
4394 case TemplateName::DependentTemplate: {
4395 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4396 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4397 if (!Qualifier)
4398 return TemplateName();
4399
4400 if (DTN->isIdentifier()) {
4401 return ToContext.getDependentTemplateName(Qualifier,
4402 Import(DTN->getIdentifier()));
4403 }
4404
4405 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4406 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004407
4408 case TemplateName::SubstTemplateTemplateParm: {
4409 SubstTemplateTemplateParmStorage *subst
4410 = From.getAsSubstTemplateTemplateParm();
4411 TemplateTemplateParmDecl *param
4412 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4413 if (!param)
4414 return TemplateName();
4415
4416 TemplateName replacement = Import(subst->getReplacement());
4417 if (replacement.isNull()) return TemplateName();
4418
4419 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4420 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004421
4422 case TemplateName::SubstTemplateTemplateParmPack: {
4423 SubstTemplateTemplateParmPackStorage *SubstPack
4424 = From.getAsSubstTemplateTemplateParmPack();
4425 TemplateTemplateParmDecl *Param
4426 = cast_or_null<TemplateTemplateParmDecl>(
4427 Import(SubstPack->getParameterPack()));
4428 if (!Param)
4429 return TemplateName();
4430
4431 ASTNodeImporter Importer(*this);
4432 TemplateArgument ArgPack
4433 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4434 if (ArgPack.isNull())
4435 return TemplateName();
4436
4437 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4438 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004439 }
4440
4441 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004442}
4443
Douglas Gregor62d311f2010-02-09 19:21:46 +00004444SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4445 if (FromLoc.isInvalid())
4446 return SourceLocation();
4447
Douglas Gregor811663e2010-02-10 00:15:17 +00004448 SourceManager &FromSM = FromContext.getSourceManager();
4449
4450 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004451 // don't have to import macro expansions.
4452 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004453 FromLoc = FromSM.getSpellingLoc(FromLoc);
4454 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4455 SourceManager &ToSM = ToContext.getSourceManager();
4456 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004457 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004458}
4459
4460SourceRange ASTImporter::Import(SourceRange FromRange) {
4461 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4462}
4463
Douglas Gregor811663e2010-02-10 00:15:17 +00004464FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004465 llvm::DenseMap<FileID, FileID>::iterator Pos
4466 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004467 if (Pos != ImportedFileIDs.end())
4468 return Pos->second;
4469
4470 SourceManager &FromSM = FromContext.getSourceManager();
4471 SourceManager &ToSM = ToContext.getSourceManager();
4472 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004473 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004474
4475 // Include location of this file.
4476 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4477
4478 // Map the FileID for to the "to" source manager.
4479 FileID ToID;
4480 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004481 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004482 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4483 // disk again
4484 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4485 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004486 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004487 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4488 FromSLoc.getFile().getFileCharacteristic());
4489 } else {
4490 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004491 const llvm::MemoryBuffer *
4492 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004493 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004494 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004495 FromBuf->getBufferIdentifier());
4496 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4497 }
4498
4499
Sebastian Redl99219f12010-09-30 01:03:06 +00004500 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004501 return ToID;
4502}
4503
Douglas Gregor0a791672011-01-18 03:11:38 +00004504void ASTImporter::ImportDefinition(Decl *From) {
4505 Decl *To = Import(From);
4506 if (!To)
4507 return;
4508
4509 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4510 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004511
4512 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4513 if (!ToRecord->getDefinition()) {
4514 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004515 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004516 return;
4517 }
4518 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004519
4520 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4521 if (!ToEnum->getDefinition()) {
4522 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004523 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004524 return;
4525 }
4526 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004527
4528 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4529 if (!ToIFace->getDefinition()) {
4530 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004531 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004532 return;
4533 }
4534 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004535
Douglas Gregor2aa53772012-01-24 17:42:07 +00004536 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4537 if (!ToProto->getDefinition()) {
4538 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004539 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004540 return;
4541 }
4542 }
4543
Douglas Gregor0a791672011-01-18 03:11:38 +00004544 Importer.ImportDeclContext(FromDC, true);
4545 }
4546}
4547
Douglas Gregor96e578d2010-02-05 17:54:41 +00004548DeclarationName ASTImporter::Import(DeclarationName FromName) {
4549 if (!FromName)
4550 return DeclarationName();
4551
4552 switch (FromName.getNameKind()) {
4553 case DeclarationName::Identifier:
4554 return Import(FromName.getAsIdentifierInfo());
4555
4556 case DeclarationName::ObjCZeroArgSelector:
4557 case DeclarationName::ObjCOneArgSelector:
4558 case DeclarationName::ObjCMultiArgSelector:
4559 return Import(FromName.getObjCSelector());
4560
4561 case DeclarationName::CXXConstructorName: {
4562 QualType T = Import(FromName.getCXXNameType());
4563 if (T.isNull())
4564 return DeclarationName();
4565
4566 return ToContext.DeclarationNames.getCXXConstructorName(
4567 ToContext.getCanonicalType(T));
4568 }
4569
4570 case DeclarationName::CXXDestructorName: {
4571 QualType T = Import(FromName.getCXXNameType());
4572 if (T.isNull())
4573 return DeclarationName();
4574
4575 return ToContext.DeclarationNames.getCXXDestructorName(
4576 ToContext.getCanonicalType(T));
4577 }
4578
4579 case DeclarationName::CXXConversionFunctionName: {
4580 QualType T = Import(FromName.getCXXNameType());
4581 if (T.isNull())
4582 return DeclarationName();
4583
4584 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4585 ToContext.getCanonicalType(T));
4586 }
4587
4588 case DeclarationName::CXXOperatorName:
4589 return ToContext.DeclarationNames.getCXXOperatorName(
4590 FromName.getCXXOverloadedOperator());
4591
4592 case DeclarationName::CXXLiteralOperatorName:
4593 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4594 Import(FromName.getCXXLiteralIdentifier()));
4595
4596 case DeclarationName::CXXUsingDirective:
4597 // FIXME: STATICS!
4598 return DeclarationName::getUsingDirectiveName();
4599 }
4600
David Blaikiee4d798f2012-01-20 21:50:17 +00004601 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004602}
4603
Douglas Gregore2e50d332010-12-01 01:36:18 +00004604IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004605 if (!FromId)
4606 return 0;
4607
4608 return &ToContext.Idents.get(FromId->getName());
4609}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004610
Douglas Gregor43f54792010-02-17 02:12:47 +00004611Selector ASTImporter::Import(Selector FromSel) {
4612 if (FromSel.isNull())
4613 return Selector();
4614
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004615 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004616 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4617 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4618 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4619 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4620}
4621
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004622DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4623 DeclContext *DC,
4624 unsigned IDNS,
4625 NamedDecl **Decls,
4626 unsigned NumDecls) {
4627 return Name;
4628}
4629
4630DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004631 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004632}
4633
4634DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004635 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004636}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004637
Douglas Gregor2e15c842012-02-01 21:00:38 +00004638void ASTImporter::CompleteDecl (Decl *D) {
4639 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4640 if (!ID->getDefinition())
4641 ID->startDefinition();
4642 }
4643 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4644 if (!PD->getDefinition())
4645 PD->startDefinition();
4646 }
4647 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4648 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4649 TD->startDefinition();
4650 TD->setCompleteDefinition(true);
4651 }
4652 }
4653 else {
4654 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4655 }
4656}
4657
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004658Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4659 ImportedDecls[From] = To;
4660 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004661}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004662
4663bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004664 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004665 = ImportedTypes.find(From.getTypePtr());
4666 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4667 return true;
4668
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004669 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004670 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004671}