blob: 87e5e533d3f02122461554e928b7f3574614f37a [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
Douglas Gregor27c72d82011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor089459a2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000040
41 // Importing types
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
Douglas Gregorcd0d56a2012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregorac32ff92012-02-01 21:00:38 +000089
Douglas Gregorcd0d56a2012-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 Gregorac32ff92012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000110 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000112 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor040afae2010-11-30 19:14:50 +0000116 TemplateParameterList *ImportTemplateParameterList(
117 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000118 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
119 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
120 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000122 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000123 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000124 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000125 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000126 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000127 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000128 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000129 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000130 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000131 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000132 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000133 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000134 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-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 Gregor96a01b42010-02-11 00:48:18 +0000139 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000140 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000141 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000142 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000143 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000144 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000145 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000146 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000147 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000148 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000149 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000150 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000151 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000152 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor040afae2010-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 Gregord5dc83a2010-12-01 01:36:18 +0000157 Decl *VisitClassTemplateSpecializationDecl(
158 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000159
Douglas Gregor4800d952010-02-11 19:21:55 +0000160 // Importing statements
161 Stmt *VisitStmt(Stmt *S);
162
163 // Importing expressions
164 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000165 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000166 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000167 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000168 Expr *VisitParenExpr(ParenExpr *E);
169 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000170 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000171 Expr *VisitBinaryOperator(BinaryOperator *E);
172 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000173 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000174 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000175 };
176}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000177using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000178
179//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-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 Gregor73dc30b2010-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 Gregorea35d112010-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 Gregor73dc30b2010-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 Gregorea35d112010-02-15 23:54:17 +0000206 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000207 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000208 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000209 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-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 Kyrtzidis33e4e702010-11-18 20:06:41 +0000226 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227 }
228
229 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000230 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-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 Foad9f71a8f2010-12-07 08:25:34 +0000247 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000248
Jay Foad9f71a8f2010-12-07 08:25:34 +0000249 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-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 Foad9f71a8f2010-12-07 08:25:34 +0000260 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000261 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000262 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-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 Gregord5dc83a2010-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
325 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
326
327 case TemplateArgument::Declaration:
Douglas Gregord2008e22012-04-06 22:40:38 +0000328 if (!Arg1.getAsDecl() || !Arg2.getAsDecl())
329 return !Arg1.getAsDecl() && !Arg2.getAsDecl();
Douglas Gregord5dc83a2010-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 Gregora7fc9012011-01-05 18:58:31 +0000336
337 case TemplateArgument::TemplateExpansion:
338 return IsStructurallyEquivalent(Context,
339 Arg1.getAsTemplateOrTemplatePattern(),
340 Arg2.getAsTemplateOrTemplatePattern());
341
Douglas Gregord5dc83a2010-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 Gregor73dc30b2010-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 Gregorea35d112010-02-15 23:54:17 +0000395 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000396
Douglas Gregorea35d112010-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 Gregor73dc30b2010-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 Wilsone86d78c2010-11-10 21:56:12 +0000536 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000537 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000538 break;
Douglas Gregor73dc30b2010-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 Redl60618fa2011-03-12 11:50:43 +0000554 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000555 return false;
Sebastian Redl60618fa2011-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 Gregor73dc30b2010-02-15 22:01:00 +0000566 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000567 Proto1->getNoexceptExpr(),
568 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-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 Espindola264ba482010-03-30 20:24:48 +0000584 if (Function1->getExtInfo() != Function2->getExtInfo())
585 return false;
Douglas Gregor73dc30b2010-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 McCall9d156a72011-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 Gregor73dc30b2010-02-15 22:01:00 +0000607
Abramo Bagnara075f8f12010-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 Gregor73dc30b2010-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;
Sean Huntca63c202011-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 Gregor73dc30b2010-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 Smith34b41d92011-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 Gregor73dc30b2010-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 Bagnara465d41b2010-05-11 21:36:43 +0000664
Douglas Gregor73dc30b2010-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 Gregor0bc15d92011-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 Gregor73dc30b2010-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 Bagnara465d41b2010-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 Gregor73dc30b2010-02-15 22:01:00 +0000735 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000736 Elab1->getQualifier(),
737 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000738 return false;
739 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000740 Elab1->getNamedType(),
741 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000742 return false;
743 break;
744 }
745
John McCall3cb0ebd2010-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 McCall31f17ec2010-04-27 00:57:59 +0000750 Inj1->getInjectedSpecializationType(),
751 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000752 return false;
753 break;
754 }
755
Douglas Gregor4714c122010-03-31 17:34:00 +0000756 case Type::DependentName: {
757 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
758 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-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 Gregor73dc30b2010-02-15 22:01:00 +0000766
767 break;
768 }
769
John McCall33500952010-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 Gregor7536dd52010-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 Gregor73dc30b2010-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 McCallc12c5bb2010-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 Gregor73dc30b2010-02-15 22:01:00 +0000814 return false;
John McCallc12c5bb2010-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 Gregor73dc30b2010-02-15 22:01:00 +0000818 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000819 Obj1->getProtocol(I),
820 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-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 Gregor73dc30b2010-02-15 22:01:00 +0000833 break;
834 }
Eli Friedmanb001de72011-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 Gregor73dc30b2010-02-15 22:01:00 +0000844 } // end switch
845
846 return true;
847}
848
Douglas Gregor7c9412c2011-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 Gregor73dc30b2010-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 Gregord5dc83a2010-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 Lattnerfc8f0e12011-04-15 05:22:18 +0000937 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000938 else if (Spec1 || Spec2)
939 return false;
940
Douglas Gregorea35d112010-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 Gregor73dc30b2010-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 Gregor040afae2010-11-30 19:14:50 +0000952 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000953 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000954 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000955 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000956 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-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 Dunbar96a00142012-03-09 18:35:03 +0000970 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000971 << Base2->getType()
972 << Base2->getSourceRange();
Daniel Dunbar96a00142012-03-09 18:35:03 +0000973 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-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 Dunbar96a00142012-03-09 18:35:03 +0000983 Context.Diag2(Base2->getLocStart(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000984 diag::note_odr_virtual_base)
985 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar96a00142012-03-09 18:35:03 +0000986 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-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 Dunbar96a00142012-03-09 18:35:03 +0000996 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-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.
1005 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
1006 Field2End = D2->field_end();
1007 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
1008 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 Blaikie262bc182012-04-30 02:36:29 +00001020 if (!IsStructurallyEquivalent(Context, &*Field1, &*Field2))
Douglas Gregor7c9412c2011-10-14 21:54:42 +00001021 return false;
Douglas Gregor73dc30b2010-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 Gregor040afae2010-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 Gregor73dc30b2010-02-15 22:01:00 +00001094
Douglas Gregor040afae2010-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 Gregor73dc30b2010-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 Gregorea35d112010-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 Gregor73dc30b2010-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 Gregorea35d112010-02-15 23:54:17 +00001235 bool Equivalent = true;
1236
Douglas Gregor73dc30b2010-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 Smith162e1c12011-04-15 14:24:37 +00001243 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1244 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001245 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001246 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1247 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001248 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1249 !::IsStructurallyEquivalent(*this, Record1, Record2))
1250 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001251 } else {
1252 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001253 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001254 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001255 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-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 Smith162e1c12011-04-15 14:24:37 +00001259 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1260 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001261 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001262 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1263 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001264 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1265 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1266 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001267 } else {
1268 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001269 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001270 }
Richard Smith162e1c12011-04-15 14:24:37 +00001271 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1272 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001273 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001274 Typedef2->getIdentifier()) ||
1275 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001276 Typedef1->getUnderlyingType(),
1277 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001278 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001279 } else {
1280 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001281 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001282 }
Douglas Gregor040afae2010-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 Gregorea35d112010-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 Gregor73dc30b2010-02-15 22:01:00 +00001331 // FIXME: Check other declaration kinds!
1332 }
1333
1334 return false;
1335}
1336
1337//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001338// Import Types
1339//----------------------------------------------------------------------------
1340
John McCallf4c73712011-01-19 06:33:43 +00001341QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001342 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1343 << T->getTypeClassName();
1344 return QualType();
1345}
1346
John McCallf4c73712011-01-19 06:33:43 +00001347QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001348 switch (T->getKind()) {
John McCalle0a22d02011-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 Gregor1b2949d2010-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 Blaikie4e4d0842012-03-11 07:00:24 +00001364 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001365 return Importer.getToContext().UnsignedCharTy;
1366
1367 return Importer.getToContext().CharTy;
1368
Douglas Gregor1b2949d2010-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 Blaikie4e4d0842012-03-11 07:00:24 +00001373 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001374 return Importer.getToContext().SignedCharTy;
1375
1376 return Importer.getToContext().CharTy;
1377
Chris Lattner3f59c972010-12-25 23:25:43 +00001378 case BuiltinType::WChar_S:
1379 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-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 Gregor1b2949d2010-02-05 17:54:41 +00001383 }
David Blaikie30263482012-01-20 21:50:17 +00001384
1385 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001386}
1387
John McCallf4c73712011-01-19 06:33:43 +00001388QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001396QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001404QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001413QualType
1414ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001423QualType
1424ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001433QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001444QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001455QualType
1456ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001466QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001482QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-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 Wilsone86d78c2010-11-10 21:56:12 +00001489 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490}
1491
John McCallf4c73712011-01-19 06:33:43 +00001492QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001501QualType
1502ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-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 Espindola264ba482010-03-30 20:24:48 +00001508
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001509 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001510 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001511}
1512
John McCallf4c73712011-01-19 06:33:43 +00001513QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-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 Lattner5f9e2722011-07-23 10:55:15 +00001519 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-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 Lattner5f9e2722011-07-23 10:55:15 +00001530 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-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 McCalle23cf432010-12-14 08:05:40 +00001539
1540 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1541 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001542
1543 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001544 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001545}
1546
Sean Callanan0aeb2892011-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 McCallf4c73712011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001556 TypedefNameDecl *ToDecl
1557 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001558 if (!ToDecl)
1559 return QualType();
1560
1561 return Importer.getToContext().getTypeDeclType(ToDecl);
1562}
1563
John McCallf4c73712011-01-19 06:33:43 +00001564QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001572QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001580QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001581 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001582 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1583 if (!ToExpr)
1584 return QualType();
1585
Douglas Gregorf8af9822012-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 Gregor1b2949d2010-02-05 17:54:41 +00001591}
1592
Sean Huntca63c202011-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 Smith34b41d92011-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 McCallf4c73712011-01-19 06:33:43 +00001617QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-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 McCallf4c73712011-01-19 06:33:43 +00001626QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-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 Gregord5dc83a2010-12-01 01:36:18 +00001635QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001636 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001637 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1638 if (ToTemplate.isNull())
1639 return QualType();
1640
Chris Lattner5f9e2722011-07-23 10:55:15 +00001641 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-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 McCallf4c73712011-01-19 06:33:43 +00001659QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-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 Gregor1b2949d2010-02-05 17:54:41 +00001667
1668 QualType ToNamedType = Importer.Import(T->getNamedType());
1669 if (ToNamedType.isNull())
1670 return QualType();
1671
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001672 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1673 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001674}
1675
John McCallf4c73712011-01-19 06:33:43 +00001676QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-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 McCallc12c5bb2010-05-15 11:32:37 +00001682 return Importer.getToContext().getObjCInterfaceType(Class);
1683}
1684
John McCallf4c73712011-01-19 06:33:43 +00001685QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001686 QualType ToBaseType = Importer.Import(T->getBaseType());
1687 if (ToBaseType.isNull())
1688 return QualType();
1689
Chris Lattner5f9e2722011-07-23 10:55:15 +00001690 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001691 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-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 McCallc12c5bb2010-05-15 11:32:37 +00001701 return Importer.getToContext().getObjCObjectType(ToBaseType,
1702 Protocols.data(),
1703 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001704}
1705
John McCallf4c73712011-01-19 06:33:43 +00001706QualType
1707ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001708 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1709 if (ToPointeeType.isNull())
1710 return QualType();
1711
John McCallc12c5bb2010-05-15 11:32:37 +00001712 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001713}
1714
Douglas Gregor089459a2010-02-08 21:09:39 +00001715//----------------------------------------------------------------------------
1716// Import Declarations
1717//----------------------------------------------------------------------------
Douglas Gregora404ea62010-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 Gregor1cf038c2011-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 Bagnara25777432010-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 Bagnara25777432010-08-11 22:01:17 +00001803 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001804 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001805}
1806
Douglas Gregorac32ff92012-02-01 21:00:38 +00001807void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregord8868a62011-01-18 03:11:38 +00001808 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001809 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001810 return;
1811 }
1812
Douglas Gregor083a8212010-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 Gregor1cf038c2011-07-29 23:31:30 +00001820bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-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 Gregord5dc83a2010-12-01 01:36:18 +00001826 return false;
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001827 }
Douglas Gregord5dc83a2010-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 Gregor27c72d82011-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 Smithdfefb842012-02-25 07:33:38 +00001854 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithd079abf2012-05-07 01:07:30 +00001855 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001856 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1857 ToData.HasConstexprNonCopyMoveConstructor
1858 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smithdfefb842012-02-25 07:33:38 +00001859 ToData.DefaultedDefaultConstructorIsConstexpr
1860 = FromData.DefaultedDefaultConstructorIsConstexpr;
1861 ToData.DefaultedCopyConstructorIsConstexpr
1862 = FromData.DefaultedCopyConstructorIsConstexpr;
1863 ToData.DefaultedMoveConstructorIsConstexpr
1864 = FromData.DefaultedMoveConstructorIsConstexpr;
1865 ToData.HasConstexprDefaultConstructor
1866 = FromData.HasConstexprDefaultConstructor;
1867 ToData.HasConstexprCopyConstructor = FromData.HasConstexprCopyConstructor;
1868 ToData.HasConstexprMoveConstructor = FromData.HasConstexprMoveConstructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001869 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1870 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1871 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1872 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1873 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smithdfefb842012-02-25 07:33:38 +00001874 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001875 ToData.HasNonLiteralTypeFieldsOrBases
1876 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smithdfefb842012-02-25 07:33:38 +00001877 // ComputedVisibleConversions not imported.
Douglas Gregor27c72d82011-11-03 18:07:07 +00001878 ToData.UserProvidedDefaultConstructor
1879 = FromData.UserProvidedDefaultConstructor;
1880 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1881 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1882 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1883 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1884 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1885 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1886 ToData.FailedImplicitMoveConstructor
1887 = FromData.FailedImplicitMoveConstructor;
1888 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smithdfefb842012-02-25 07:33:38 +00001889 ToData.IsLambda = FromData.IsLambda;
1890
Chris Lattner5f9e2722011-07-23 10:55:15 +00001891 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001892 for (CXXRecordDecl::base_class_iterator
1893 Base1 = FromCXX->bases_begin(),
1894 FromBaseEnd = FromCXX->bases_end();
1895 Base1 != FromBaseEnd;
1896 ++Base1) {
1897 QualType T = Importer.Import(Base1->getType());
1898 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001899 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001900
1901 SourceLocation EllipsisLoc;
1902 if (Base1->isPackExpansion())
1903 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001904
1905 // Ensure that we have a definition for the base.
1906 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1907
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001908 Bases.push_back(
1909 new (Importer.getToContext())
1910 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1911 Base1->isVirtual(),
1912 Base1->isBaseOfClass(),
1913 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001914 Importer.Import(Base1->getTypeSourceInfo()),
1915 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001916 }
1917 if (!Bases.empty())
1918 ToCXX->setBases(Bases.data(), Bases.size());
1919 }
1920
Douglas Gregorac32ff92012-02-01 21:00:38 +00001921 if (shouldForceImportDeclContext(Kind))
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001922 ImportDeclContext(From, /*ForceImport=*/true);
1923
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001924 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001925 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001926}
1927
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001928bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00001929 ImportDefinitionKind Kind) {
1930 if (To->getDefinition() || To->isBeingDefined()) {
1931 if (Kind == IDK_Everything)
1932 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001933 return false;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001934 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001935
1936 To->startDefinition();
1937
1938 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1939 if (T.isNull())
1940 return true;
1941
1942 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1943 if (ToPromotionType.isNull())
1944 return true;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001945
1946 if (shouldForceImportDeclContext(Kind))
1947 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001948
1949 // FIXME: we might need to merge the number of positive or negative bits
1950 // if the enumerator lists don't match.
1951 To->completeDefinition(T, ToPromotionType,
1952 From->getNumPositiveBits(),
1953 From->getNumNegativeBits());
1954 return false;
1955}
1956
Douglas Gregor040afae2010-11-30 19:14:50 +00001957TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1958 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001959 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001960 ToParams.reserve(Params->size());
1961 for (TemplateParameterList::iterator P = Params->begin(),
1962 PEnd = Params->end();
1963 P != PEnd; ++P) {
1964 Decl *To = Importer.Import(*P);
1965 if (!To)
1966 return 0;
1967
1968 ToParams.push_back(cast<NamedDecl>(To));
1969 }
1970
1971 return TemplateParameterList::Create(Importer.getToContext(),
1972 Importer.Import(Params->getTemplateLoc()),
1973 Importer.Import(Params->getLAngleLoc()),
1974 ToParams.data(), ToParams.size(),
1975 Importer.Import(Params->getRAngleLoc()));
1976}
1977
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001978TemplateArgument
1979ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1980 switch (From.getKind()) {
1981 case TemplateArgument::Null:
1982 return TemplateArgument();
1983
1984 case TemplateArgument::Type: {
1985 QualType ToType = Importer.Import(From.getAsType());
1986 if (ToType.isNull())
1987 return TemplateArgument();
1988 return TemplateArgument(ToType);
1989 }
1990
1991 case TemplateArgument::Integral: {
1992 QualType ToType = Importer.Import(From.getIntegralType());
1993 if (ToType.isNull())
1994 return TemplateArgument();
1995 return TemplateArgument(*From.getAsIntegral(), ToType);
1996 }
1997
1998 case TemplateArgument::Declaration:
1999 if (Decl *To = Importer.Import(From.getAsDecl()))
2000 return TemplateArgument(To);
2001 return TemplateArgument();
2002
2003 case TemplateArgument::Template: {
2004 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2005 if (ToTemplate.isNull())
2006 return TemplateArgument();
2007
2008 return TemplateArgument(ToTemplate);
2009 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002010
2011 case TemplateArgument::TemplateExpansion: {
2012 TemplateName ToTemplate
2013 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2014 if (ToTemplate.isNull())
2015 return TemplateArgument();
2016
Douglas Gregor2be29f42011-01-14 23:41:42 +00002017 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00002018 }
2019
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002020 case TemplateArgument::Expression:
2021 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2022 return TemplateArgument(ToExpr);
2023 return TemplateArgument();
2024
2025 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002026 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002027 ToPack.reserve(From.pack_size());
2028 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2029 return TemplateArgument();
2030
2031 TemplateArgument *ToArgs
2032 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2033 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2034 return TemplateArgument(ToArgs, ToPack.size());
2035 }
2036 }
2037
2038 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002039}
2040
2041bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2042 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002043 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002044 for (unsigned I = 0; I != NumFromArgs; ++I) {
2045 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2046 if (To.isNull() && !FromArgs[I].isNull())
2047 return true;
2048
2049 ToArgs.push_back(To);
2050 }
2051
2052 return false;
2053}
2054
Douglas Gregor96a01b42010-02-11 00:48:18 +00002055bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002056 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002057 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002058 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002059 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002060 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002061}
2062
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002063bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002064 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002065 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002066 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002067 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002068}
2069
Douglas Gregor040afae2010-11-30 19:14:50 +00002070bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2071 ClassTemplateDecl *To) {
2072 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2073 Importer.getToContext(),
2074 Importer.getNonEquivalentDecls());
2075 return Ctx.IsStructurallyEquivalent(From, To);
2076}
2077
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002078Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002079 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002080 << D->getDeclKindName();
2081 return 0;
2082}
2083
Sean Callananf1b69462011-11-17 23:20:56 +00002084Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2085 TranslationUnitDecl *ToD =
2086 Importer.getToContext().getTranslationUnitDecl();
2087
2088 Importer.Imported(D, ToD);
2089
2090 return ToD;
2091}
2092
Douglas Gregor788c62d2010-02-21 18:26:36 +00002093Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2094 // Import the major distinguishing characteristics of this namespace.
2095 DeclContext *DC, *LexicalDC;
2096 DeclarationName Name;
2097 SourceLocation Loc;
2098 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2099 return 0;
2100
2101 NamespaceDecl *MergeWithNamespace = 0;
2102 if (!Name) {
2103 // This is an anonymous namespace. Adopt an existing anonymous
2104 // namespace if we can.
2105 // FIXME: Not testable.
2106 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2107 MergeWithNamespace = TU->getAnonymousNamespace();
2108 else
2109 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2110 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002111 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002112 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2113 DC->localUncachedLookup(Name, FoundDecls);
2114 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2115 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002116 continue;
2117
Douglas Gregorb75a3452011-10-15 00:10:27 +00002118 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002119 MergeWithNamespace = FoundNS;
2120 ConflictingDecls.clear();
2121 break;
2122 }
2123
Douglas Gregorb75a3452011-10-15 00:10:27 +00002124 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002125 }
2126
2127 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002128 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002129 ConflictingDecls.data(),
2130 ConflictingDecls.size());
2131 }
2132 }
2133
2134 // Create the "to" namespace, if needed.
2135 NamespaceDecl *ToNamespace = MergeWithNamespace;
2136 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002137 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002138 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002139 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002140 Loc, Name.getAsIdentifierInfo(),
2141 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002142 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002143 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002144
2145 // If this is an anonymous namespace, register it as the anonymous
2146 // namespace within its context.
2147 if (!Name) {
2148 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2149 TU->setAnonymousNamespace(ToNamespace);
2150 else
2151 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2152 }
2153 }
2154 Importer.Imported(D, ToNamespace);
2155
2156 ImportDeclContext(D);
2157
2158 return ToNamespace;
2159}
2160
Richard Smith162e1c12011-04-15 14:24:37 +00002161Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002162 // Import the major distinguishing characteristics of this typedef.
2163 DeclContext *DC, *LexicalDC;
2164 DeclarationName Name;
2165 SourceLocation Loc;
2166 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2167 return 0;
2168
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002169 // If this typedef is not in block scope, determine whether we've
2170 // seen a typedef with the same name (that we can merge with) or any
2171 // other entity by that name (which name lookup could conflict with).
2172 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002173 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002174 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002175 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2176 DC->localUncachedLookup(Name, FoundDecls);
2177 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2178 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002179 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002180 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002181 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002182 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2183 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002184 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002185 }
2186
Douglas Gregorb75a3452011-10-15 00:10:27 +00002187 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002188 }
2189
2190 if (!ConflictingDecls.empty()) {
2191 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2192 ConflictingDecls.data(),
2193 ConflictingDecls.size());
2194 if (!Name)
2195 return 0;
2196 }
2197 }
2198
Douglas Gregorea35d112010-02-15 23:54:17 +00002199 // Import the underlying type of this typedef;
2200 QualType T = Importer.Import(D->getUnderlyingType());
2201 if (T.isNull())
2202 return 0;
2203
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002204 // Create the new typedef node.
2205 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002206 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002207 TypedefNameDecl *ToTypedef;
2208 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002209 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2210 StartL, Loc,
2211 Name.getAsIdentifierInfo(),
2212 TInfo);
2213 else
Richard Smith162e1c12011-04-15 14:24:37 +00002214 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2215 StartL, Loc,
2216 Name.getAsIdentifierInfo(),
2217 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002218
Douglas Gregor325bf172010-02-22 17:42:47 +00002219 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002220 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002221 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002222 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002223
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002224 return ToTypedef;
2225}
2226
Richard Smith162e1c12011-04-15 14:24:37 +00002227Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2228 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2229}
2230
2231Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2232 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2233}
2234
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002235Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2236 // Import the major distinguishing characteristics of this enum.
2237 DeclContext *DC, *LexicalDC;
2238 DeclarationName Name;
2239 SourceLocation Loc;
2240 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2241 return 0;
2242
2243 // Figure out what enum name we're looking for.
2244 unsigned IDNS = Decl::IDNS_Tag;
2245 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002246 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2247 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002248 IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00002249 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002250 IDNS |= Decl::IDNS_Ordinary;
2251
2252 // We may already have an enum of the same name; try to find and match it.
2253 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002254 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002255 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2256 DC->localUncachedLookup(SearchName, FoundDecls);
2257 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2258 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002259 continue;
2260
Douglas Gregorb75a3452011-10-15 00:10:27 +00002261 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002262 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002263 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2264 Found = Tag->getDecl();
2265 }
2266
2267 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002268 if (IsStructuralMatch(D, FoundEnum))
2269 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002270 }
2271
Douglas Gregorb75a3452011-10-15 00:10:27 +00002272 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002273 }
2274
2275 if (!ConflictingDecls.empty()) {
2276 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2277 ConflictingDecls.data(),
2278 ConflictingDecls.size());
2279 }
2280 }
2281
2282 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002283 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2284 Importer.Import(D->getLocStart()),
2285 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002286 D->isScoped(), D->isScopedUsingClassTag(),
2287 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002288 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002289 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002290 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002291 D2->setLexicalDeclContext(LexicalDC);
2292 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002293 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002294
2295 // Import the integer type.
2296 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2297 if (ToIntegerType.isNull())
2298 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002299 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002300
2301 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002302 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002303 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002304
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002305 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002306}
2307
Douglas Gregor96a01b42010-02-11 00:48:18 +00002308Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2309 // If this record has a definition in the translation unit we're coming from,
2310 // but this particular declaration is not that definition, import the
2311 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002312 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002313 if (Definition && Definition != D) {
2314 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002315 if (!ImportedDef)
2316 return 0;
2317
2318 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002319 }
2320
2321 // Import the major distinguishing characteristics of this record.
2322 DeclContext *DC, *LexicalDC;
2323 DeclarationName Name;
2324 SourceLocation Loc;
2325 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2326 return 0;
2327
2328 // Figure out what structure name we're looking for.
2329 unsigned IDNS = Decl::IDNS_Tag;
2330 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002331 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2332 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002333 IDNS = Decl::IDNS_Ordinary;
David Blaikie4e4d0842012-03-11 07:00:24 +00002334 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor96a01b42010-02-11 00:48:18 +00002335 IDNS |= Decl::IDNS_Ordinary;
2336
2337 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002338 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002339 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002340 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002341 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2342 DC->localUncachedLookup(SearchName, FoundDecls);
2343 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2344 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002345 continue;
2346
Douglas Gregorb75a3452011-10-15 00:10:27 +00002347 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002348 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002349 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2350 Found = Tag->getDecl();
2351 }
2352
2353 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002354 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002355 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002356 // The record types structurally match, or the "from" translation
2357 // unit only had a forward declaration anyway; call it the same
2358 // function.
2359 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002360 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002361 }
2362 } else {
2363 // We have a forward declaration of this type, so adopt that forward
2364 // declaration rather than building a new one.
2365 AdoptDecl = FoundRecord;
2366 continue;
2367 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002368 }
2369
Douglas Gregorb75a3452011-10-15 00:10:27 +00002370 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002371 }
2372
2373 if (!ConflictingDecls.empty()) {
2374 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2375 ConflictingDecls.data(),
2376 ConflictingDecls.size());
2377 }
2378 }
2379
2380 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002381 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002382 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002383 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002384 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002385 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002386 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002387 DC, StartLoc, Loc,
2388 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002389 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002390 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002391 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002392 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002393 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002394 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002395
2396 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002397 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002398 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002399 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002400
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002401 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002402
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002403 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002404 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002405
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002406 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002407}
2408
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002409Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2410 // Import the major distinguishing characteristics of this enumerator.
2411 DeclContext *DC, *LexicalDC;
2412 DeclarationName Name;
2413 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002414 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002415 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002416
2417 QualType T = Importer.Import(D->getType());
2418 if (T.isNull())
2419 return 0;
2420
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002421 // Determine whether there are any other declarations with the same name and
2422 // in the same context.
2423 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002424 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002425 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002426 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2427 DC->localUncachedLookup(Name, FoundDecls);
2428 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2429 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002430 continue;
2431
Douglas Gregorb75a3452011-10-15 00:10:27 +00002432 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002433 }
2434
2435 if (!ConflictingDecls.empty()) {
2436 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2437 ConflictingDecls.data(),
2438 ConflictingDecls.size());
2439 if (!Name)
2440 return 0;
2441 }
2442 }
2443
2444 Expr *Init = Importer.Import(D->getInitExpr());
2445 if (D->getInitExpr() && !Init)
2446 return 0;
2447
2448 EnumConstantDecl *ToEnumerator
2449 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2450 Name.getAsIdentifierInfo(), T,
2451 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002452 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002453 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002454 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002455 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002456 return ToEnumerator;
2457}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002458
Douglas Gregora404ea62010-02-10 19:54:31 +00002459Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2460 // Import the major distinguishing characteristics of this function.
2461 DeclContext *DC, *LexicalDC;
2462 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002463 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002464 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002465 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002466
Douglas Gregora404ea62010-02-10 19:54:31 +00002467 // Try to find a function in our own ("to") context with the same name, same
2468 // type, and in the same context as the function we're importing.
2469 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002470 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002471 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002472 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2473 DC->localUncachedLookup(Name, FoundDecls);
2474 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2475 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002476 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002477
Douglas Gregorb75a3452011-10-15 00:10:27 +00002478 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002479 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2480 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002481 if (Importer.IsStructurallyEquivalent(D->getType(),
2482 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002483 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002484 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002485 }
2486
2487 // FIXME: Check for overloading more carefully, e.g., by boosting
2488 // Sema::IsOverload out to the AST library.
2489
2490 // Function overloading is okay in C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002491 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregora404ea62010-02-10 19:54:31 +00002492 continue;
2493
2494 // Complain about inconsistent function types.
2495 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002496 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002497 Importer.ToDiag(FoundFunction->getLocation(),
2498 diag::note_odr_value_here)
2499 << FoundFunction->getType();
2500 }
2501 }
2502
Douglas Gregorb75a3452011-10-15 00:10:27 +00002503 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002504 }
2505
2506 if (!ConflictingDecls.empty()) {
2507 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2508 ConflictingDecls.data(),
2509 ConflictingDecls.size());
2510 if (!Name)
2511 return 0;
2512 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002513 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002514
Abramo Bagnara25777432010-08-11 22:01:17 +00002515 DeclarationNameInfo NameInfo(Name, Loc);
2516 // Import additional name location/type info.
2517 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2518
Douglas Gregorea35d112010-02-15 23:54:17 +00002519 // Import the type.
2520 QualType T = Importer.Import(D->getType());
2521 if (T.isNull())
2522 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002523
2524 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002525 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002526 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2527 P != PEnd; ++P) {
2528 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2529 if (!ToP)
2530 return 0;
2531
2532 Parameters.push_back(ToP);
2533 }
2534
2535 // Create the imported function.
2536 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002537 FunctionDecl *ToFunction = 0;
2538 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2539 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2540 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002541 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002542 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002543 FromConstructor->isExplicit(),
2544 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002545 D->isImplicit(),
2546 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002547 } else if (isa<CXXDestructorDecl>(D)) {
2548 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2549 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002550 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002551 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002552 D->isInlineSpecified(),
2553 D->isImplicit());
2554 } else if (CXXConversionDecl *FromConversion
2555 = dyn_cast<CXXConversionDecl>(D)) {
2556 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2557 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002558 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002559 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002560 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002561 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002562 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002563 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002564 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2565 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2566 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002567 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002568 NameInfo, T, TInfo,
2569 Method->isStatic(),
2570 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002571 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002572 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002573 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002574 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002575 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002576 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002577 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002578 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002579 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002580 D->hasWrittenPrototype(),
2581 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002582 }
John McCallb6217662010-03-15 10:12:16 +00002583
2584 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002585 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002586 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002587 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002588 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2589 ToFunction->setTrivial(D->isTrivial());
2590 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002591 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002592
Douglas Gregora404ea62010-02-10 19:54:31 +00002593 // Set the parameters.
2594 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002595 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002596 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002597 }
David Blaikie4278c652011-09-21 18:16:56 +00002598 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002599
2600 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002601
2602 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002603 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002604
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002605 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002606}
2607
Douglas Gregorc144f352010-02-21 18:29:16 +00002608Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2609 return VisitFunctionDecl(D);
2610}
2611
2612Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2613 return VisitCXXMethodDecl(D);
2614}
2615
2616Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2617 return VisitCXXMethodDecl(D);
2618}
2619
2620Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2621 return VisitCXXMethodDecl(D);
2622}
2623
Douglas Gregor96a01b42010-02-11 00:48:18 +00002624Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2625 // Import the major distinguishing characteristics of a variable.
2626 DeclContext *DC, *LexicalDC;
2627 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002628 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002629 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2630 return 0;
2631
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002632 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002633 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2634 DC->localUncachedLookup(Name, FoundDecls);
2635 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2636 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002637 if (Importer.IsStructurallyEquivalent(D->getType(),
2638 FoundField->getType())) {
2639 Importer.Imported(D, FoundField);
2640 return FoundField;
2641 }
2642
2643 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2644 << Name << D->getType() << FoundField->getType();
2645 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2646 << FoundField->getType();
2647 return 0;
2648 }
2649 }
2650
Douglas Gregorea35d112010-02-15 23:54:17 +00002651 // Import the type.
2652 QualType T = Importer.Import(D->getType());
2653 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002654 return 0;
2655
2656 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2657 Expr *BitWidth = Importer.Import(D->getBitWidth());
2658 if (!BitWidth && D->getBitWidth())
2659 return 0;
2660
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002661 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2662 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002663 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002664 T, TInfo, BitWidth, D->isMutable(),
2665 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002666 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002667 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002668 if (ToField->hasInClassInitializer())
2669 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002670 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002671 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002672 return ToField;
2673}
2674
Francois Pichet87c2e122010-11-21 06:08:52 +00002675Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2676 // Import the major distinguishing characteristics of a variable.
2677 DeclContext *DC, *LexicalDC;
2678 DeclarationName Name;
2679 SourceLocation Loc;
2680 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2681 return 0;
2682
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002683 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002684 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2685 DC->localUncachedLookup(Name, FoundDecls);
2686 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002687 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002688 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002689 if (Importer.IsStructurallyEquivalent(D->getType(),
2690 FoundField->getType())) {
2691 Importer.Imported(D, FoundField);
2692 return FoundField;
2693 }
2694
2695 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2696 << Name << D->getType() << FoundField->getType();
2697 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2698 << FoundField->getType();
2699 return 0;
2700 }
2701 }
2702
Francois Pichet87c2e122010-11-21 06:08:52 +00002703 // Import the type.
2704 QualType T = Importer.Import(D->getType());
2705 if (T.isNull())
2706 return 0;
2707
2708 NamedDecl **NamedChain =
2709 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2710
2711 unsigned i = 0;
2712 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2713 PE = D->chain_end(); PI != PE; ++PI) {
2714 Decl* D = Importer.Import(*PI);
2715 if (!D)
2716 return 0;
2717 NamedChain[i++] = cast<NamedDecl>(D);
2718 }
2719
2720 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2721 Importer.getToContext(), DC,
2722 Loc, Name.getAsIdentifierInfo(), T,
2723 NamedChain, D->getChainingSize());
2724 ToIndirectField->setAccess(D->getAccess());
2725 ToIndirectField->setLexicalDeclContext(LexicalDC);
2726 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002727 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002728 return ToIndirectField;
2729}
2730
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002731Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2732 // Import the major distinguishing characteristics of an ivar.
2733 DeclContext *DC, *LexicalDC;
2734 DeclarationName Name;
2735 SourceLocation Loc;
2736 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2737 return 0;
2738
2739 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002740 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2741 DC->localUncachedLookup(Name, FoundDecls);
2742 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2743 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002744 if (Importer.IsStructurallyEquivalent(D->getType(),
2745 FoundIvar->getType())) {
2746 Importer.Imported(D, FoundIvar);
2747 return FoundIvar;
2748 }
2749
2750 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2751 << Name << D->getType() << FoundIvar->getType();
2752 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2753 << FoundIvar->getType();
2754 return 0;
2755 }
2756 }
2757
2758 // Import the type.
2759 QualType T = Importer.Import(D->getType());
2760 if (T.isNull())
2761 return 0;
2762
2763 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2764 Expr *BitWidth = Importer.Import(D->getBitWidth());
2765 if (!BitWidth && D->getBitWidth())
2766 return 0;
2767
Daniel Dunbara0654922010-04-02 20:10:03 +00002768 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2769 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002770 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002771 Loc, Name.getAsIdentifierInfo(),
2772 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002773 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002774 ToIvar->setLexicalDeclContext(LexicalDC);
2775 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002776 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002777 return ToIvar;
2778
2779}
2780
Douglas Gregora404ea62010-02-10 19:54:31 +00002781Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2782 // Import the major distinguishing characteristics of a variable.
2783 DeclContext *DC, *LexicalDC;
2784 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002785 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002786 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002787 return 0;
2788
Douglas Gregor089459a2010-02-08 21:09:39 +00002789 // Try to find a variable in our own ("to") context with the same name and
2790 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002791 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002792 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002793 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002794 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002795 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2796 DC->localUncachedLookup(Name, FoundDecls);
2797 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2798 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002799 continue;
2800
Douglas Gregorb75a3452011-10-15 00:10:27 +00002801 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002802 // We have found a variable that we may need to merge with. Check it.
2803 if (isExternalLinkage(FoundVar->getLinkage()) &&
2804 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002805 if (Importer.IsStructurallyEquivalent(D->getType(),
2806 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002807 MergeWithVar = FoundVar;
2808 break;
2809 }
2810
Douglas Gregord0145422010-02-12 17:23:39 +00002811 const ArrayType *FoundArray
2812 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2813 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002814 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002815 if (FoundArray && TArray) {
2816 if (isa<IncompleteArrayType>(FoundArray) &&
2817 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002818 // Import the type.
2819 QualType T = Importer.Import(D->getType());
2820 if (T.isNull())
2821 return 0;
2822
Douglas Gregord0145422010-02-12 17:23:39 +00002823 FoundVar->setType(T);
2824 MergeWithVar = FoundVar;
2825 break;
2826 } else if (isa<IncompleteArrayType>(TArray) &&
2827 isa<ConstantArrayType>(FoundArray)) {
2828 MergeWithVar = FoundVar;
2829 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002830 }
2831 }
2832
Douglas Gregor089459a2010-02-08 21:09:39 +00002833 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002834 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002835 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2836 << FoundVar->getType();
2837 }
2838 }
2839
Douglas Gregorb75a3452011-10-15 00:10:27 +00002840 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002841 }
2842
2843 if (MergeWithVar) {
2844 // An equivalent variable with external linkage has been found. Link
2845 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002846 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002847
2848 if (VarDecl *DDef = D->getDefinition()) {
2849 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2850 Importer.ToDiag(ExistingDef->getLocation(),
2851 diag::err_odr_variable_multiple_def)
2852 << Name;
2853 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2854 } else {
2855 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002856 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002857 if (DDef->isInitKnownICE()) {
2858 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2859 Eval->CheckedICE = true;
2860 Eval->IsICE = DDef->isInitICE();
2861 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002862 }
2863 }
2864
2865 return MergeWithVar;
2866 }
2867
2868 if (!ConflictingDecls.empty()) {
2869 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2870 ConflictingDecls.data(),
2871 ConflictingDecls.size());
2872 if (!Name)
2873 return 0;
2874 }
2875 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002876
Douglas Gregorea35d112010-02-15 23:54:17 +00002877 // Import the type.
2878 QualType T = Importer.Import(D->getType());
2879 if (T.isNull())
2880 return 0;
2881
Douglas Gregor089459a2010-02-08 21:09:39 +00002882 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002883 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002884 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2885 Importer.Import(D->getInnerLocStart()),
2886 Loc, Name.getAsIdentifierInfo(),
2887 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002888 D->getStorageClass(),
2889 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002890 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002891 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002892 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002893 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002894 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002895
Douglas Gregor089459a2010-02-08 21:09:39 +00002896 // Merge the initializer.
2897 // FIXME: Can we really import any initializer? Alternatively, we could force
2898 // ourselves to import every declaration of a variable and then only use
2899 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002900 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002901
2902 // FIXME: Other bits to merge?
2903
2904 return ToVar;
2905}
2906
Douglas Gregor2cd00932010-02-17 21:22:52 +00002907Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2908 // Parameters are created in the translation unit's context, then moved
2909 // into the function declaration's context afterward.
2910 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2911
2912 // Import the name of this declaration.
2913 DeclarationName Name = Importer.Import(D->getDeclName());
2914 if (D->getDeclName() && !Name)
2915 return 0;
2916
2917 // Import the location of this declaration.
2918 SourceLocation Loc = Importer.Import(D->getLocation());
2919
2920 // Import the parameter's type.
2921 QualType T = Importer.Import(D->getType());
2922 if (T.isNull())
2923 return 0;
2924
2925 // Create the imported parameter.
2926 ImplicitParamDecl *ToParm
2927 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2928 Loc, Name.getAsIdentifierInfo(),
2929 T);
2930 return Importer.Imported(D, ToParm);
2931}
2932
Douglas Gregora404ea62010-02-10 19:54:31 +00002933Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2934 // Parameters are created in the translation unit's context, then moved
2935 // into the function declaration's context afterward.
2936 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2937
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002938 // Import the name of this declaration.
2939 DeclarationName Name = Importer.Import(D->getDeclName());
2940 if (D->getDeclName() && !Name)
2941 return 0;
2942
Douglas Gregora404ea62010-02-10 19:54:31 +00002943 // Import the location of this declaration.
2944 SourceLocation Loc = Importer.Import(D->getLocation());
2945
2946 // Import the parameter's type.
2947 QualType T = Importer.Import(D->getType());
2948 if (T.isNull())
2949 return 0;
2950
2951 // Create the imported parameter.
2952 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2953 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002954 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002955 Loc, Name.getAsIdentifierInfo(),
2956 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002957 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002958 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002959 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002960 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002961}
2962
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002963Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2964 // Import the major distinguishing characteristics of a method.
2965 DeclContext *DC, *LexicalDC;
2966 DeclarationName Name;
2967 SourceLocation Loc;
2968 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2969 return 0;
2970
Douglas Gregorb75a3452011-10-15 00:10:27 +00002971 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2972 DC->localUncachedLookup(Name, FoundDecls);
2973 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2974 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002975 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2976 continue;
2977
2978 // Check return types.
2979 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2980 FoundMethod->getResultType())) {
2981 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2982 << D->isInstanceMethod() << Name
2983 << D->getResultType() << FoundMethod->getResultType();
2984 Importer.ToDiag(FoundMethod->getLocation(),
2985 diag::note_odr_objc_method_here)
2986 << D->isInstanceMethod() << Name;
2987 return 0;
2988 }
2989
2990 // Check the number of parameters.
2991 if (D->param_size() != FoundMethod->param_size()) {
2992 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2993 << D->isInstanceMethod() << Name
2994 << D->param_size() << FoundMethod->param_size();
2995 Importer.ToDiag(FoundMethod->getLocation(),
2996 diag::note_odr_objc_method_here)
2997 << D->isInstanceMethod() << Name;
2998 return 0;
2999 }
3000
3001 // Check parameter types.
3002 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3003 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3004 P != PEnd; ++P, ++FoundP) {
3005 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3006 (*FoundP)->getType())) {
3007 Importer.FromDiag((*P)->getLocation(),
3008 diag::err_odr_objc_method_param_type_inconsistent)
3009 << D->isInstanceMethod() << Name
3010 << (*P)->getType() << (*FoundP)->getType();
3011 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3012 << (*FoundP)->getType();
3013 return 0;
3014 }
3015 }
3016
3017 // Check variadic/non-variadic.
3018 // Check the number of parameters.
3019 if (D->isVariadic() != FoundMethod->isVariadic()) {
3020 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3021 << D->isInstanceMethod() << Name;
3022 Importer.ToDiag(FoundMethod->getLocation(),
3023 diag::note_odr_objc_method_here)
3024 << D->isInstanceMethod() << Name;
3025 return 0;
3026 }
3027
3028 // FIXME: Any other bits we need to merge?
3029 return Importer.Imported(D, FoundMethod);
3030 }
3031 }
3032
3033 // Import the result type.
3034 QualType ResultTy = Importer.Import(D->getResultType());
3035 if (ResultTy.isNull())
3036 return 0;
3037
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003038 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3039
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003040 ObjCMethodDecl *ToMethod
3041 = ObjCMethodDecl::Create(Importer.getToContext(),
3042 Loc,
3043 Importer.Import(D->getLocEnd()),
3044 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003045 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003046 D->isInstanceMethod(),
3047 D->isVariadic(),
3048 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003049 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003050 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00003051 D->getImplementationControl(),
3052 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003053
3054 // FIXME: When we decide to merge method definitions, we'll need to
3055 // deal with implicit parameters.
3056
3057 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003058 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003059 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3060 FromPEnd = D->param_end();
3061 FromP != FromPEnd;
3062 ++FromP) {
3063 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3064 if (!ToP)
3065 return 0;
3066
3067 ToParams.push_back(ToP);
3068 }
3069
3070 // Set the parameters.
3071 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3072 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003073 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003074 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003075 SmallVector<SourceLocation, 12> SelLocs;
3076 D->getSelectorLocs(SelLocs);
3077 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003078
3079 ToMethod->setLexicalDeclContext(LexicalDC);
3080 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003081 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003082 return ToMethod;
3083}
3084
Douglas Gregorb4677b62010-02-18 01:47:50 +00003085Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3086 // Import the major distinguishing characteristics of a category.
3087 DeclContext *DC, *LexicalDC;
3088 DeclarationName Name;
3089 SourceLocation Loc;
3090 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3091 return 0;
3092
3093 ObjCInterfaceDecl *ToInterface
3094 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3095 if (!ToInterface)
3096 return 0;
3097
3098 // Determine if we've already encountered this category.
3099 ObjCCategoryDecl *MergeWithCategory
3100 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3101 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3102 if (!ToCategory) {
3103 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003104 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003105 Loc,
3106 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003107 Name.getAsIdentifierInfo(),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003108 ToInterface,
3109 Importer.Import(D->getIvarLBraceLoc()),
3110 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003111 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003112 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003113 Importer.Imported(D, ToCategory);
3114
Douglas Gregorb4677b62010-02-18 01:47:50 +00003115 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003116 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3117 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003118 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3119 = D->protocol_loc_begin();
3120 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3121 FromProtoEnd = D->protocol_end();
3122 FromProto != FromProtoEnd;
3123 ++FromProto, ++FromProtoLoc) {
3124 ObjCProtocolDecl *ToProto
3125 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3126 if (!ToProto)
3127 return 0;
3128 Protocols.push_back(ToProto);
3129 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3130 }
3131
3132 // FIXME: If we're merging, make sure that the protocol list is the same.
3133 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3134 ProtocolLocs.data(), Importer.getToContext());
3135
3136 } else {
3137 Importer.Imported(D, ToCategory);
3138 }
3139
3140 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003141 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003142
3143 // If we have an implementation, import it as well.
3144 if (D->getImplementation()) {
3145 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003146 = cast_or_null<ObjCCategoryImplDecl>(
3147 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003148 if (!Impl)
3149 return 0;
3150
3151 ToCategory->setImplementation(Impl);
3152 }
3153
3154 return ToCategory;
3155}
3156
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003157bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3158 ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003159 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003160 if (To->getDefinition()) {
Douglas Gregorac32ff92012-02-01 21:00:38 +00003161 if (shouldForceImportDeclContext(Kind))
3162 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003163 return false;
3164 }
3165
3166 // Start the protocol definition
3167 To->startDefinition();
3168
3169 // Import protocols
3170 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3171 SmallVector<SourceLocation, 4> ProtocolLocs;
3172 ObjCProtocolDecl::protocol_loc_iterator
3173 FromProtoLoc = From->protocol_loc_begin();
3174 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3175 FromProtoEnd = From->protocol_end();
3176 FromProto != FromProtoEnd;
3177 ++FromProto, ++FromProtoLoc) {
3178 ObjCProtocolDecl *ToProto
3179 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3180 if (!ToProto)
3181 return true;
3182 Protocols.push_back(ToProto);
3183 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3184 }
3185
3186 // FIXME: If we're merging, make sure that the protocol list is the same.
3187 To->setProtocolList(Protocols.data(), Protocols.size(),
3188 ProtocolLocs.data(), Importer.getToContext());
3189
Douglas Gregorac32ff92012-02-01 21:00:38 +00003190 if (shouldForceImportDeclContext(Kind)) {
3191 // Import all of the members of this protocol.
3192 ImportDeclContext(From, /*ForceImport=*/true);
3193 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003194 return false;
3195}
3196
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003197Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003198 // If this protocol has a definition in the translation unit we're coming
3199 // from, but this particular declaration is not that definition, import the
3200 // definition and map to that.
3201 ObjCProtocolDecl *Definition = D->getDefinition();
3202 if (Definition && Definition != D) {
3203 Decl *ImportedDef = Importer.Import(Definition);
3204 if (!ImportedDef)
3205 return 0;
3206
3207 return Importer.Imported(D, ImportedDef);
3208 }
3209
Douglas Gregorb4677b62010-02-18 01:47:50 +00003210 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003211 DeclContext *DC, *LexicalDC;
3212 DeclarationName Name;
3213 SourceLocation Loc;
3214 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3215 return 0;
3216
3217 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003218 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3219 DC->localUncachedLookup(Name, FoundDecls);
3220 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3221 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003222 continue;
3223
Douglas Gregorb75a3452011-10-15 00:10:27 +00003224 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003225 break;
3226 }
3227
3228 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003229 if (!ToProto) {
3230 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3231 Name.getAsIdentifierInfo(), Loc,
3232 Importer.Import(D->getAtStartLoc()),
3233 /*PrevDecl=*/0);
3234 ToProto->setLexicalDeclContext(LexicalDC);
3235 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003236 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003237
3238 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003239
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003240 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3241 return 0;
3242
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003243 return ToProto;
3244}
3245
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003246bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3247 ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003248 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003249 if (To->getDefinition()) {
3250 // Check consistency of superclass.
3251 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3252 if (FromSuper) {
3253 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3254 if (!FromSuper)
3255 return true;
3256 }
3257
3258 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3259 if ((bool)FromSuper != (bool)ToSuper ||
3260 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3261 Importer.ToDiag(To->getLocation(),
3262 diag::err_odr_objc_superclass_inconsistent)
3263 << To->getDeclName();
3264 if (ToSuper)
3265 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3266 << To->getSuperClass()->getDeclName();
3267 else
3268 Importer.ToDiag(To->getLocation(),
3269 diag::note_odr_objc_missing_superclass);
3270 if (From->getSuperClass())
3271 Importer.FromDiag(From->getSuperClassLoc(),
3272 diag::note_odr_objc_superclass)
3273 << From->getSuperClass()->getDeclName();
3274 else
3275 Importer.FromDiag(From->getLocation(),
3276 diag::note_odr_objc_missing_superclass);
3277 }
3278
Douglas Gregorac32ff92012-02-01 21:00:38 +00003279 if (shouldForceImportDeclContext(Kind))
3280 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003281 return false;
3282 }
3283
3284 // Start the definition.
3285 To->startDefinition();
3286
3287 // If this class has a superclass, import it.
3288 if (From->getSuperClass()) {
3289 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3290 Importer.Import(From->getSuperClass()));
3291 if (!Super)
3292 return true;
3293
3294 To->setSuperClass(Super);
3295 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3296 }
3297
3298 // Import protocols
3299 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3300 SmallVector<SourceLocation, 4> ProtocolLocs;
3301 ObjCInterfaceDecl::protocol_loc_iterator
3302 FromProtoLoc = From->protocol_loc_begin();
3303
3304 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3305 FromProtoEnd = From->protocol_end();
3306 FromProto != FromProtoEnd;
3307 ++FromProto, ++FromProtoLoc) {
3308 ObjCProtocolDecl *ToProto
3309 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3310 if (!ToProto)
3311 return true;
3312 Protocols.push_back(ToProto);
3313 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3314 }
3315
3316 // FIXME: If we're merging, make sure that the protocol list is the same.
3317 To->setProtocolList(Protocols.data(), Protocols.size(),
3318 ProtocolLocs.data(), Importer.getToContext());
3319
3320 // Import categories. When the categories themselves are imported, they'll
3321 // hook themselves into this interface.
3322 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3323 FromCat = FromCat->getNextClassCategory())
3324 Importer.Import(FromCat);
3325
3326 // If we have an @implementation, import it as well.
3327 if (From->getImplementation()) {
3328 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3329 Importer.Import(From->getImplementation()));
3330 if (!Impl)
3331 return true;
3332
3333 To->setImplementation(Impl);
3334 }
3335
Douglas Gregorac32ff92012-02-01 21:00:38 +00003336 if (shouldForceImportDeclContext(Kind)) {
3337 // Import all of the members of this class.
3338 ImportDeclContext(From, /*ForceImport=*/true);
3339 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003340 return false;
3341}
3342
Douglas Gregora12d2942010-02-16 01:20:57 +00003343Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003344 // If this class has a definition in the translation unit we're coming from,
3345 // but this particular declaration is not that definition, import the
3346 // definition and map to that.
3347 ObjCInterfaceDecl *Definition = D->getDefinition();
3348 if (Definition && Definition != D) {
3349 Decl *ImportedDef = Importer.Import(Definition);
3350 if (!ImportedDef)
3351 return 0;
3352
3353 return Importer.Imported(D, ImportedDef);
3354 }
3355
Douglas Gregora12d2942010-02-16 01:20:57 +00003356 // Import the major distinguishing characteristics of an @interface.
3357 DeclContext *DC, *LexicalDC;
3358 DeclarationName Name;
3359 SourceLocation Loc;
3360 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3361 return 0;
3362
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003363 // Look for an existing interface with the same name.
Douglas Gregora12d2942010-02-16 01:20:57 +00003364 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003365 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3366 DC->localUncachedLookup(Name, FoundDecls);
3367 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3368 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003369 continue;
3370
Douglas Gregorb75a3452011-10-15 00:10:27 +00003371 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003372 break;
3373 }
3374
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003375 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003376 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003377 if (!ToIface) {
3378 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3379 Importer.Import(D->getAtStartLoc()),
3380 Name.getAsIdentifierInfo(),
3381 /*PrevDecl=*/0,Loc,
3382 D->isImplicitInterfaceDecl());
3383 ToIface->setLexicalDeclContext(LexicalDC);
3384 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003385 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003386 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003387
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003388 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3389 return 0;
Douglas Gregora12d2942010-02-16 01:20:57 +00003390
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003391 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003392}
3393
Douglas Gregor3daef292010-12-07 15:32:12 +00003394Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3395 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3396 Importer.Import(D->getCategoryDecl()));
3397 if (!Category)
3398 return 0;
3399
3400 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3401 if (!ToImpl) {
3402 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3403 if (!DC)
3404 return 0;
3405
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003406 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003407 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003408 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003409 Category->getClassInterface(),
3410 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003411 Importer.Import(D->getAtStartLoc()),
3412 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003413
3414 DeclContext *LexicalDC = DC;
3415 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3416 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3417 if (!LexicalDC)
3418 return 0;
3419
3420 ToImpl->setLexicalDeclContext(LexicalDC);
3421 }
3422
Sean Callanan9faf8102011-10-21 02:57:43 +00003423 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003424 Category->setImplementation(ToImpl);
3425 }
3426
3427 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003428 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003429 return ToImpl;
3430}
3431
Douglas Gregordd182ff2010-12-07 01:26:03 +00003432Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3433 // Find the corresponding interface.
3434 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3435 Importer.Import(D->getClassInterface()));
3436 if (!Iface)
3437 return 0;
3438
3439 // Import the superclass, if any.
3440 ObjCInterfaceDecl *Super = 0;
3441 if (D->getSuperClass()) {
3442 Super = cast_or_null<ObjCInterfaceDecl>(
3443 Importer.Import(D->getSuperClass()));
3444 if (!Super)
3445 return 0;
3446 }
3447
3448 ObjCImplementationDecl *Impl = Iface->getImplementation();
3449 if (!Impl) {
3450 // We haven't imported an implementation yet. Create a new @implementation
3451 // now.
3452 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3453 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003454 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003455 Importer.Import(D->getLocation()),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003456 Importer.Import(D->getAtStartLoc()),
3457 Importer.Import(D->getIvarLBraceLoc()),
3458 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003459
3460 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3461 DeclContext *LexicalDC
3462 = Importer.ImportContext(D->getLexicalDeclContext());
3463 if (!LexicalDC)
3464 return 0;
3465 Impl->setLexicalDeclContext(LexicalDC);
3466 }
3467
3468 // Associate the implementation with the class it implements.
3469 Iface->setImplementation(Impl);
3470 Importer.Imported(D, Iface->getImplementation());
3471 } else {
3472 Importer.Imported(D, Iface->getImplementation());
3473
3474 // Verify that the existing @implementation has the same superclass.
3475 if ((Super && !Impl->getSuperClass()) ||
3476 (!Super && Impl->getSuperClass()) ||
3477 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003478 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003479 Importer.ToDiag(Impl->getLocation(),
3480 diag::err_odr_objc_superclass_inconsistent)
3481 << Iface->getDeclName();
3482 // FIXME: It would be nice to have the location of the superclass
3483 // below.
3484 if (Impl->getSuperClass())
3485 Importer.ToDiag(Impl->getLocation(),
3486 diag::note_odr_objc_superclass)
3487 << Impl->getSuperClass()->getDeclName();
3488 else
3489 Importer.ToDiag(Impl->getLocation(),
3490 diag::note_odr_objc_missing_superclass);
3491 if (D->getSuperClass())
3492 Importer.FromDiag(D->getLocation(),
3493 diag::note_odr_objc_superclass)
3494 << D->getSuperClass()->getDeclName();
3495 else
3496 Importer.FromDiag(D->getLocation(),
3497 diag::note_odr_objc_missing_superclass);
3498 return 0;
3499 }
3500 }
3501
3502 // Import all of the members of this @implementation.
3503 ImportDeclContext(D);
3504
3505 return Impl;
3506}
3507
Douglas Gregore3261622010-02-17 18:02:10 +00003508Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3509 // Import the major distinguishing characteristics of an @property.
3510 DeclContext *DC, *LexicalDC;
3511 DeclarationName Name;
3512 SourceLocation Loc;
3513 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3514 return 0;
3515
3516 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003517 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3518 DC->localUncachedLookup(Name, FoundDecls);
3519 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003520 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003521 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003522 // Check property types.
3523 if (!Importer.IsStructurallyEquivalent(D->getType(),
3524 FoundProp->getType())) {
3525 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3526 << Name << D->getType() << FoundProp->getType();
3527 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3528 << FoundProp->getType();
3529 return 0;
3530 }
3531
3532 // FIXME: Check property attributes, getters, setters, etc.?
3533
3534 // Consider these properties to be equivalent.
3535 Importer.Imported(D, FoundProp);
3536 return FoundProp;
3537 }
3538 }
3539
3540 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003541 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3542 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003543 return 0;
3544
3545 // Create the new property.
3546 ObjCPropertyDecl *ToProperty
3547 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3548 Name.getAsIdentifierInfo(),
3549 Importer.Import(D->getAtLoc()),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00003550 Importer.Import(D->getLParenLoc()),
Douglas Gregore3261622010-02-17 18:02:10 +00003551 T,
3552 D->getPropertyImplementation());
3553 Importer.Imported(D, ToProperty);
3554 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003555 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003556
3557 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003558 ToProperty->setPropertyAttributesAsWritten(
3559 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003560 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3561 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3562 ToProperty->setGetterMethodDecl(
3563 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3564 ToProperty->setSetterMethodDecl(
3565 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3566 ToProperty->setPropertyIvarDecl(
3567 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3568 return ToProperty;
3569}
3570
Douglas Gregor954e0c72010-12-07 18:32:03 +00003571Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3572 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3573 Importer.Import(D->getPropertyDecl()));
3574 if (!Property)
3575 return 0;
3576
3577 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3578 if (!DC)
3579 return 0;
3580
3581 // Import the lexical declaration context.
3582 DeclContext *LexicalDC = DC;
3583 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3584 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3585 if (!LexicalDC)
3586 return 0;
3587 }
3588
3589 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3590 if (!InImpl)
3591 return 0;
3592
3593 // Import the ivar (for an @synthesize).
3594 ObjCIvarDecl *Ivar = 0;
3595 if (D->getPropertyIvarDecl()) {
3596 Ivar = cast_or_null<ObjCIvarDecl>(
3597 Importer.Import(D->getPropertyIvarDecl()));
3598 if (!Ivar)
3599 return 0;
3600 }
3601
3602 ObjCPropertyImplDecl *ToImpl
3603 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3604 if (!ToImpl) {
3605 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3606 Importer.Import(D->getLocStart()),
3607 Importer.Import(D->getLocation()),
3608 Property,
3609 D->getPropertyImplementation(),
3610 Ivar,
3611 Importer.Import(D->getPropertyIvarDeclLoc()));
3612 ToImpl->setLexicalDeclContext(LexicalDC);
3613 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003614 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003615 } else {
3616 // Check that we have the same kind of property implementation (@synthesize
3617 // vs. @dynamic).
3618 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3619 Importer.ToDiag(ToImpl->getLocation(),
3620 diag::err_odr_objc_property_impl_kind_inconsistent)
3621 << Property->getDeclName()
3622 << (ToImpl->getPropertyImplementation()
3623 == ObjCPropertyImplDecl::Dynamic);
3624 Importer.FromDiag(D->getLocation(),
3625 diag::note_odr_objc_property_impl_kind)
3626 << D->getPropertyDecl()->getDeclName()
3627 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3628 return 0;
3629 }
3630
3631 // For @synthesize, check that we have the same
3632 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3633 Ivar != ToImpl->getPropertyIvarDecl()) {
3634 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3635 diag::err_odr_objc_synthesize_ivar_inconsistent)
3636 << Property->getDeclName()
3637 << ToImpl->getPropertyIvarDecl()->getDeclName()
3638 << Ivar->getDeclName();
3639 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3640 diag::note_odr_objc_synthesize_ivar_here)
3641 << D->getPropertyIvarDecl()->getDeclName();
3642 return 0;
3643 }
3644
3645 // Merge the existing implementation with the new implementation.
3646 Importer.Imported(D, ToImpl);
3647 }
3648
3649 return ToImpl;
3650}
3651
Douglas Gregor040afae2010-11-30 19:14:50 +00003652Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3653 // For template arguments, we adopt the translation unit as our declaration
3654 // context. This context will be fixed when the actual template declaration
3655 // is created.
3656
3657 // FIXME: Import default argument.
3658 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3659 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003660 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003661 Importer.Import(D->getLocation()),
3662 D->getDepth(),
3663 D->getIndex(),
3664 Importer.Import(D->getIdentifier()),
3665 D->wasDeclaredWithTypename(),
3666 D->isParameterPack());
3667}
3668
3669Decl *
3670ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3671 // Import the name of this declaration.
3672 DeclarationName Name = Importer.Import(D->getDeclName());
3673 if (D->getDeclName() && !Name)
3674 return 0;
3675
3676 // Import the location of this declaration.
3677 SourceLocation Loc = Importer.Import(D->getLocation());
3678
3679 // Import the type of this declaration.
3680 QualType T = Importer.Import(D->getType());
3681 if (T.isNull())
3682 return 0;
3683
3684 // Import type-source information.
3685 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3686 if (D->getTypeSourceInfo() && !TInfo)
3687 return 0;
3688
3689 // FIXME: Import default argument.
3690
3691 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3692 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003693 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003694 Loc, D->getDepth(), D->getPosition(),
3695 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003696 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003697}
3698
3699Decl *
3700ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3701 // Import the name of this declaration.
3702 DeclarationName Name = Importer.Import(D->getDeclName());
3703 if (D->getDeclName() && !Name)
3704 return 0;
3705
3706 // Import the location of this declaration.
3707 SourceLocation Loc = Importer.Import(D->getLocation());
3708
3709 // Import template parameters.
3710 TemplateParameterList *TemplateParams
3711 = ImportTemplateParameterList(D->getTemplateParameters());
3712 if (!TemplateParams)
3713 return 0;
3714
3715 // FIXME: Import default argument.
3716
3717 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3718 Importer.getToContext().getTranslationUnitDecl(),
3719 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003720 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003721 Name.getAsIdentifierInfo(),
3722 TemplateParams);
3723}
3724
3725Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3726 // If this record has a definition in the translation unit we're coming from,
3727 // but this particular declaration is not that definition, import the
3728 // definition and map to that.
3729 CXXRecordDecl *Definition
3730 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3731 if (Definition && Definition != D->getTemplatedDecl()) {
3732 Decl *ImportedDef
3733 = Importer.Import(Definition->getDescribedClassTemplate());
3734 if (!ImportedDef)
3735 return 0;
3736
3737 return Importer.Imported(D, ImportedDef);
3738 }
3739
3740 // Import the major distinguishing characteristics of this class template.
3741 DeclContext *DC, *LexicalDC;
3742 DeclarationName Name;
3743 SourceLocation Loc;
3744 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3745 return 0;
3746
3747 // We may already have a template of the same name; try to find and match it.
3748 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003749 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003750 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3751 DC->localUncachedLookup(Name, FoundDecls);
3752 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3753 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003754 continue;
3755
Douglas Gregorb75a3452011-10-15 00:10:27 +00003756 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003757 if (ClassTemplateDecl *FoundTemplate
3758 = dyn_cast<ClassTemplateDecl>(Found)) {
3759 if (IsStructuralMatch(D, FoundTemplate)) {
3760 // The class templates structurally match; call it the same template.
3761 // FIXME: We may be filling in a forward declaration here. Handle
3762 // this case!
3763 Importer.Imported(D->getTemplatedDecl(),
3764 FoundTemplate->getTemplatedDecl());
3765 return Importer.Imported(D, FoundTemplate);
3766 }
3767 }
3768
Douglas Gregorb75a3452011-10-15 00:10:27 +00003769 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003770 }
3771
3772 if (!ConflictingDecls.empty()) {
3773 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3774 ConflictingDecls.data(),
3775 ConflictingDecls.size());
3776 }
3777
3778 if (!Name)
3779 return 0;
3780 }
3781
3782 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3783
3784 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003785 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3786 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003787 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3788 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003789 DC, StartLoc, IdLoc,
3790 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003791 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003792 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003793 D2Templated->setLexicalDeclContext(LexicalDC);
3794
3795 // Create the class template declaration itself.
3796 TemplateParameterList *TemplateParams
3797 = ImportTemplateParameterList(D->getTemplateParameters());
3798 if (!TemplateParams)
3799 return 0;
3800
3801 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3802 Loc, Name, TemplateParams,
3803 D2Templated,
3804 /*PrevDecl=*/0);
3805 D2Templated->setDescribedClassTemplate(D2);
3806
3807 D2->setAccess(D->getAccess());
3808 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003809 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003810
3811 // Note the relationship between the class templates.
3812 Importer.Imported(D, D2);
3813 Importer.Imported(DTemplated, D2Templated);
3814
John McCall5e1cdac2011-10-07 06:10:15 +00003815 if (DTemplated->isCompleteDefinition() &&
3816 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003817 // FIXME: Import definition!
3818 }
3819
3820 return D2;
3821}
3822
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003823Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3824 ClassTemplateSpecializationDecl *D) {
3825 // If this record has a definition in the translation unit we're coming from,
3826 // but this particular declaration is not that definition, import the
3827 // definition and map to that.
3828 TagDecl *Definition = D->getDefinition();
3829 if (Definition && Definition != D) {
3830 Decl *ImportedDef = Importer.Import(Definition);
3831 if (!ImportedDef)
3832 return 0;
3833
3834 return Importer.Imported(D, ImportedDef);
3835 }
3836
3837 ClassTemplateDecl *ClassTemplate
3838 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3839 D->getSpecializedTemplate()));
3840 if (!ClassTemplate)
3841 return 0;
3842
3843 // Import the context of this declaration.
3844 DeclContext *DC = ClassTemplate->getDeclContext();
3845 if (!DC)
3846 return 0;
3847
3848 DeclContext *LexicalDC = DC;
3849 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3850 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3851 if (!LexicalDC)
3852 return 0;
3853 }
3854
3855 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003856 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3857 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003858
3859 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003860 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003861 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3862 D->getTemplateArgs().size(),
3863 TemplateArgs))
3864 return 0;
3865
3866 // Try to find an existing specialization with these template arguments.
3867 void *InsertPos = 0;
3868 ClassTemplateSpecializationDecl *D2
3869 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3870 TemplateArgs.size(), InsertPos);
3871 if (D2) {
3872 // We already have a class template specialization with these template
3873 // arguments.
3874
3875 // FIXME: Check for specialization vs. instantiation errors.
3876
3877 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003878 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003879 // The record types structurally match, or the "from" translation
3880 // unit only had a forward declaration anyway; call it the same
3881 // function.
3882 return Importer.Imported(D, FoundDef);
3883 }
3884 }
3885 } else {
3886 // Create a new specialization.
3887 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3888 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003889 StartLoc, IdLoc,
3890 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003891 TemplateArgs.data(),
3892 TemplateArgs.size(),
3893 /*PrevDecl=*/0);
3894 D2->setSpecializationKind(D->getSpecializationKind());
3895
3896 // Add this specialization to the class template.
3897 ClassTemplate->AddSpecialization(D2, InsertPos);
3898
3899 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003900 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003901
3902 // Add the specialization to this context.
3903 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003904 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003905 }
3906 Importer.Imported(D, D2);
3907
John McCall5e1cdac2011-10-07 06:10:15 +00003908 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003909 return 0;
3910
3911 return D2;
3912}
3913
Douglas Gregor4800d952010-02-11 19:21:55 +00003914//----------------------------------------------------------------------------
3915// Import Statements
3916//----------------------------------------------------------------------------
3917
3918Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3919 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3920 << S->getStmtClassName();
3921 return 0;
3922}
3923
3924//----------------------------------------------------------------------------
3925// Import Expressions
3926//----------------------------------------------------------------------------
3927Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3928 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3929 << E->getStmtClassName();
3930 return 0;
3931}
3932
Douglas Gregor44080632010-02-19 01:17:02 +00003933Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003934 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3935 if (!ToD)
3936 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003937
3938 NamedDecl *FoundD = 0;
3939 if (E->getDecl() != E->getFoundDecl()) {
3940 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3941 if (!FoundD)
3942 return 0;
3943 }
Douglas Gregor44080632010-02-19 01:17:02 +00003944
3945 QualType T = Importer.Import(E->getType());
3946 if (T.isNull())
3947 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003948
3949 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3950 Importer.Import(E->getQualifierLoc()),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003951 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003952 ToD,
John McCallf4b88a42012-03-10 09:33:50 +00003953 E->refersToEnclosingLocal(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003954 Importer.Import(E->getLocation()),
3955 T, E->getValueKind(),
3956 FoundD,
3957 /*FIXME:TemplateArgs=*/0);
3958 if (E->hadMultipleCandidates())
3959 DRE->setHadMultipleCandidates(true);
3960 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003961}
3962
Douglas Gregor4800d952010-02-11 19:21:55 +00003963Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3964 QualType T = Importer.Import(E->getType());
3965 if (T.isNull())
3966 return 0;
3967
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003968 return IntegerLiteral::Create(Importer.getToContext(),
3969 E->getValue(), T,
3970 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003971}
3972
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003973Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3974 QualType T = Importer.Import(E->getType());
3975 if (T.isNull())
3976 return 0;
3977
Douglas Gregor5cee1192011-07-27 05:40:30 +00003978 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3979 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003980 Importer.Import(E->getLocation()));
3981}
3982
Douglas Gregorf638f952010-02-19 01:07:06 +00003983Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3984 Expr *SubExpr = Importer.Import(E->getSubExpr());
3985 if (!SubExpr)
3986 return 0;
3987
3988 return new (Importer.getToContext())
3989 ParenExpr(Importer.Import(E->getLParen()),
3990 Importer.Import(E->getRParen()),
3991 SubExpr);
3992}
3993
3994Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3995 QualType T = Importer.Import(E->getType());
3996 if (T.isNull())
3997 return 0;
3998
3999 Expr *SubExpr = Importer.Import(E->getSubExpr());
4000 if (!SubExpr)
4001 return 0;
4002
4003 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004004 T, E->getValueKind(),
4005 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004006 Importer.Import(E->getOperatorLoc()));
4007}
4008
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004009Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4010 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00004011 QualType ResultType = Importer.Import(E->getType());
4012
4013 if (E->isArgumentType()) {
4014 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4015 if (!TInfo)
4016 return 0;
4017
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004018 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4019 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004020 Importer.Import(E->getOperatorLoc()),
4021 Importer.Import(E->getRParenLoc()));
4022 }
4023
4024 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4025 if (!SubExpr)
4026 return 0;
4027
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004028 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4029 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004030 Importer.Import(E->getOperatorLoc()),
4031 Importer.Import(E->getRParenLoc()));
4032}
4033
Douglas Gregorf638f952010-02-19 01:07:06 +00004034Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4035 QualType T = Importer.Import(E->getType());
4036 if (T.isNull())
4037 return 0;
4038
4039 Expr *LHS = Importer.Import(E->getLHS());
4040 if (!LHS)
4041 return 0;
4042
4043 Expr *RHS = Importer.Import(E->getRHS());
4044 if (!RHS)
4045 return 0;
4046
4047 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004048 T, E->getValueKind(),
4049 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004050 Importer.Import(E->getOperatorLoc()));
4051}
4052
4053Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4054 QualType T = Importer.Import(E->getType());
4055 if (T.isNull())
4056 return 0;
4057
4058 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4059 if (CompLHSType.isNull())
4060 return 0;
4061
4062 QualType CompResultType = Importer.Import(E->getComputationResultType());
4063 if (CompResultType.isNull())
4064 return 0;
4065
4066 Expr *LHS = Importer.Import(E->getLHS());
4067 if (!LHS)
4068 return 0;
4069
4070 Expr *RHS = Importer.Import(E->getRHS());
4071 if (!RHS)
4072 return 0;
4073
4074 return new (Importer.getToContext())
4075 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004076 T, E->getValueKind(),
4077 E->getObjectKind(),
4078 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004079 Importer.Import(E->getOperatorLoc()));
4080}
4081
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004082static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004083 if (E->path_empty()) return false;
4084
4085 // TODO: import cast paths
4086 return true;
4087}
4088
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004089Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4090 QualType T = Importer.Import(E->getType());
4091 if (T.isNull())
4092 return 0;
4093
4094 Expr *SubExpr = Importer.Import(E->getSubExpr());
4095 if (!SubExpr)
4096 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004097
4098 CXXCastPath BasePath;
4099 if (ImportCastPath(E, BasePath))
4100 return 0;
4101
4102 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004103 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004104}
4105
Douglas Gregor008847a2010-02-19 01:32:14 +00004106Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4107 QualType T = Importer.Import(E->getType());
4108 if (T.isNull())
4109 return 0;
4110
4111 Expr *SubExpr = Importer.Import(E->getSubExpr());
4112 if (!SubExpr)
4113 return 0;
4114
4115 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4116 if (!TInfo && E->getTypeInfoAsWritten())
4117 return 0;
4118
John McCallf871d0c2010-08-07 06:22:56 +00004119 CXXCastPath BasePath;
4120 if (ImportCastPath(E, BasePath))
4121 return 0;
4122
John McCallf89e55a2010-11-18 06:31:45 +00004123 return CStyleCastExpr::Create(Importer.getToContext(), T,
4124 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004125 SubExpr, &BasePath, TInfo,
4126 Importer.Import(E->getLParenLoc()),
4127 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004128}
4129
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004130ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004131 ASTContext &FromContext, FileManager &FromFileManager,
4132 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004133 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004134 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4135 Minimal(MinimalImport)
4136{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004137 ImportedDecls[FromContext.getTranslationUnitDecl()]
4138 = ToContext.getTranslationUnitDecl();
4139}
4140
4141ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004142
4143QualType ASTImporter::Import(QualType FromT) {
4144 if (FromT.isNull())
4145 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004146
4147 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004148
Douglas Gregor169fba52010-02-08 15:18:58 +00004149 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004150 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4151 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004152 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004153 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004154
Douglas Gregor169fba52010-02-08 15:18:58 +00004155 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004156 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004157 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004158 if (ToT.isNull())
4159 return ToT;
4160
Douglas Gregor169fba52010-02-08 15:18:58 +00004161 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004162 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004163
John McCallf4c73712011-01-19 06:33:43 +00004164 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004165}
4166
Douglas Gregor9bed8792010-02-09 19:21:46 +00004167TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004168 if (!FromTSI)
4169 return FromTSI;
4170
4171 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004172 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004173 QualType T = Import(FromTSI->getType());
4174 if (T.isNull())
4175 return 0;
4176
4177 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar96a00142012-03-09 18:35:03 +00004178 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004179}
4180
4181Decl *ASTImporter::Import(Decl *FromD) {
4182 if (!FromD)
4183 return 0;
4184
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004185 ASTNodeImporter Importer(*this);
4186
Douglas Gregor9bed8792010-02-09 19:21:46 +00004187 // Check whether we've already imported this declaration.
4188 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004189 if (Pos != ImportedDecls.end()) {
4190 Decl *ToD = Pos->second;
4191 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4192 return ToD;
4193 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004194
4195 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004196 Decl *ToD = Importer.Visit(FromD);
4197 if (!ToD)
4198 return 0;
4199
4200 // Record the imported declaration.
4201 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004202
4203 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4204 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004205 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004206 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004207 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004208 // When we've finished transforming a typedef, see whether it was the
4209 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004210 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004211 FromTag = AnonTagsWithPendingTypedefs.begin(),
4212 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4213 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004214 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004215 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4216 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004217 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004218 AnonTagsWithPendingTypedefs.erase(FromTag);
4219 break;
4220 }
4221 }
4222 }
4223 }
4224
Douglas Gregor9bed8792010-02-09 19:21:46 +00004225 return ToD;
4226}
4227
4228DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4229 if (!FromDC)
4230 return FromDC;
4231
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004232 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregorac32ff92012-02-01 21:00:38 +00004233 if (!ToDC)
4234 return 0;
4235
4236 // When we're using a record/enum/Objective-C class/protocol as a context, we
4237 // need it to have a definition.
4238 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor568991b2012-01-25 01:13:20 +00004239 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregorac32ff92012-02-01 21:00:38 +00004240 if (ToRecord->isCompleteDefinition()) {
4241 // Do nothing.
4242 } else if (FromRecord->isCompleteDefinition()) {
4243 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4244 ASTNodeImporter::IDK_Basic);
4245 } else {
4246 CompleteDecl(ToRecord);
4247 }
4248 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4249 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4250 if (ToEnum->isCompleteDefinition()) {
4251 // Do nothing.
4252 } else if (FromEnum->isCompleteDefinition()) {
4253 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4254 ASTNodeImporter::IDK_Basic);
4255 } else {
4256 CompleteDecl(ToEnum);
4257 }
4258 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4259 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4260 if (ToClass->getDefinition()) {
4261 // Do nothing.
4262 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4263 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4264 ASTNodeImporter::IDK_Basic);
4265 } else {
4266 CompleteDecl(ToClass);
4267 }
4268 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4269 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4270 if (ToProto->getDefinition()) {
4271 // Do nothing.
4272 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4273 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4274 ASTNodeImporter::IDK_Basic);
4275 } else {
4276 CompleteDecl(ToProto);
4277 }
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004278 }
4279
4280 return ToDC;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004281}
4282
4283Expr *ASTImporter::Import(Expr *FromE) {
4284 if (!FromE)
4285 return 0;
4286
4287 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4288}
4289
4290Stmt *ASTImporter::Import(Stmt *FromS) {
4291 if (!FromS)
4292 return 0;
4293
Douglas Gregor4800d952010-02-11 19:21:55 +00004294 // Check whether we've already imported this declaration.
4295 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4296 if (Pos != ImportedStmts.end())
4297 return Pos->second;
4298
4299 // Import the type
4300 ASTNodeImporter Importer(*this);
4301 Stmt *ToS = Importer.Visit(FromS);
4302 if (!ToS)
4303 return 0;
4304
4305 // Record the imported declaration.
4306 ImportedStmts[FromS] = ToS;
4307 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004308}
4309
4310NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4311 if (!FromNNS)
4312 return 0;
4313
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004314 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4315
4316 switch (FromNNS->getKind()) {
4317 case NestedNameSpecifier::Identifier:
4318 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4319 return NestedNameSpecifier::Create(ToContext, prefix, II);
4320 }
4321 return 0;
4322
4323 case NestedNameSpecifier::Namespace:
4324 if (NamespaceDecl *NS =
4325 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4326 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4327 }
4328 return 0;
4329
4330 case NestedNameSpecifier::NamespaceAlias:
4331 if (NamespaceAliasDecl *NSAD =
4332 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4333 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4334 }
4335 return 0;
4336
4337 case NestedNameSpecifier::Global:
4338 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4339
4340 case NestedNameSpecifier::TypeSpec:
4341 case NestedNameSpecifier::TypeSpecWithTemplate: {
4342 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4343 if (!T.isNull()) {
4344 bool bTemplate = FromNNS->getKind() ==
4345 NestedNameSpecifier::TypeSpecWithTemplate;
4346 return NestedNameSpecifier::Create(ToContext, prefix,
4347 bTemplate, T.getTypePtr());
4348 }
4349 }
4350 return 0;
4351 }
4352
4353 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004354}
4355
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004356NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4357 // FIXME: Implement!
4358 return NestedNameSpecifierLoc();
4359}
4360
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004361TemplateName ASTImporter::Import(TemplateName From) {
4362 switch (From.getKind()) {
4363 case TemplateName::Template:
4364 if (TemplateDecl *ToTemplate
4365 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4366 return TemplateName(ToTemplate);
4367
4368 return TemplateName();
4369
4370 case TemplateName::OverloadedTemplate: {
4371 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4372 UnresolvedSet<2> ToTemplates;
4373 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4374 E = FromStorage->end();
4375 I != E; ++I) {
4376 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4377 ToTemplates.addDecl(To);
4378 else
4379 return TemplateName();
4380 }
4381 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4382 ToTemplates.end());
4383 }
4384
4385 case TemplateName::QualifiedTemplate: {
4386 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4387 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4388 if (!Qualifier)
4389 return TemplateName();
4390
4391 if (TemplateDecl *ToTemplate
4392 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4393 return ToContext.getQualifiedTemplateName(Qualifier,
4394 QTN->hasTemplateKeyword(),
4395 ToTemplate);
4396
4397 return TemplateName();
4398 }
4399
4400 case TemplateName::DependentTemplate: {
4401 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4402 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4403 if (!Qualifier)
4404 return TemplateName();
4405
4406 if (DTN->isIdentifier()) {
4407 return ToContext.getDependentTemplateName(Qualifier,
4408 Import(DTN->getIdentifier()));
4409 }
4410
4411 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4412 }
John McCall14606042011-06-30 08:33:18 +00004413
4414 case TemplateName::SubstTemplateTemplateParm: {
4415 SubstTemplateTemplateParmStorage *subst
4416 = From.getAsSubstTemplateTemplateParm();
4417 TemplateTemplateParmDecl *param
4418 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4419 if (!param)
4420 return TemplateName();
4421
4422 TemplateName replacement = Import(subst->getReplacement());
4423 if (replacement.isNull()) return TemplateName();
4424
4425 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4426 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004427
4428 case TemplateName::SubstTemplateTemplateParmPack: {
4429 SubstTemplateTemplateParmPackStorage *SubstPack
4430 = From.getAsSubstTemplateTemplateParmPack();
4431 TemplateTemplateParmDecl *Param
4432 = cast_or_null<TemplateTemplateParmDecl>(
4433 Import(SubstPack->getParameterPack()));
4434 if (!Param)
4435 return TemplateName();
4436
4437 ASTNodeImporter Importer(*this);
4438 TemplateArgument ArgPack
4439 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4440 if (ArgPack.isNull())
4441 return TemplateName();
4442
4443 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4444 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004445 }
4446
4447 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004448}
4449
Douglas Gregor9bed8792010-02-09 19:21:46 +00004450SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4451 if (FromLoc.isInvalid())
4452 return SourceLocation();
4453
Douglas Gregor88523732010-02-10 00:15:17 +00004454 SourceManager &FromSM = FromContext.getSourceManager();
4455
4456 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004457 // don't have to import macro expansions.
4458 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004459 FromLoc = FromSM.getSpellingLoc(FromLoc);
4460 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4461 SourceManager &ToSM = ToContext.getSourceManager();
4462 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004463 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004464}
4465
4466SourceRange ASTImporter::Import(SourceRange FromRange) {
4467 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4468}
4469
Douglas Gregor88523732010-02-10 00:15:17 +00004470FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004471 llvm::DenseMap<FileID, FileID>::iterator Pos
4472 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004473 if (Pos != ImportedFileIDs.end())
4474 return Pos->second;
4475
4476 SourceManager &FromSM = FromContext.getSourceManager();
4477 SourceManager &ToSM = ToContext.getSourceManager();
4478 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004479 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004480
4481 // Include location of this file.
4482 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4483
4484 // Map the FileID for to the "to" source manager.
4485 FileID ToID;
4486 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004487 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004488 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4489 // disk again
4490 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4491 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004492 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004493 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4494 FromSLoc.getFile().getFileCharacteristic());
4495 } else {
4496 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004497 const llvm::MemoryBuffer *
4498 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004499 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004500 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004501 FromBuf->getBufferIdentifier());
4502 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4503 }
4504
4505
Sebastian Redl535a3e22010-09-30 01:03:06 +00004506 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004507 return ToID;
4508}
4509
Douglas Gregord8868a62011-01-18 03:11:38 +00004510void ASTImporter::ImportDefinition(Decl *From) {
4511 Decl *To = Import(From);
4512 if (!To)
4513 return;
4514
4515 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4516 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004517
4518 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4519 if (!ToRecord->getDefinition()) {
4520 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004521 ASTNodeImporter::IDK_Everything);
Sean Callanan673e7752011-07-19 22:38:25 +00004522 return;
4523 }
4524 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004525
4526 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4527 if (!ToEnum->getDefinition()) {
4528 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004529 ASTNodeImporter::IDK_Everything);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004530 return;
4531 }
4532 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004533
4534 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4535 if (!ToIFace->getDefinition()) {
4536 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004537 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004538 return;
4539 }
4540 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004541
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004542 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4543 if (!ToProto->getDefinition()) {
4544 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004545 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004546 return;
4547 }
4548 }
4549
Douglas Gregord8868a62011-01-18 03:11:38 +00004550 Importer.ImportDeclContext(FromDC, true);
4551 }
4552}
4553
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004554DeclarationName ASTImporter::Import(DeclarationName FromName) {
4555 if (!FromName)
4556 return DeclarationName();
4557
4558 switch (FromName.getNameKind()) {
4559 case DeclarationName::Identifier:
4560 return Import(FromName.getAsIdentifierInfo());
4561
4562 case DeclarationName::ObjCZeroArgSelector:
4563 case DeclarationName::ObjCOneArgSelector:
4564 case DeclarationName::ObjCMultiArgSelector:
4565 return Import(FromName.getObjCSelector());
4566
4567 case DeclarationName::CXXConstructorName: {
4568 QualType T = Import(FromName.getCXXNameType());
4569 if (T.isNull())
4570 return DeclarationName();
4571
4572 return ToContext.DeclarationNames.getCXXConstructorName(
4573 ToContext.getCanonicalType(T));
4574 }
4575
4576 case DeclarationName::CXXDestructorName: {
4577 QualType T = Import(FromName.getCXXNameType());
4578 if (T.isNull())
4579 return DeclarationName();
4580
4581 return ToContext.DeclarationNames.getCXXDestructorName(
4582 ToContext.getCanonicalType(T));
4583 }
4584
4585 case DeclarationName::CXXConversionFunctionName: {
4586 QualType T = Import(FromName.getCXXNameType());
4587 if (T.isNull())
4588 return DeclarationName();
4589
4590 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4591 ToContext.getCanonicalType(T));
4592 }
4593
4594 case DeclarationName::CXXOperatorName:
4595 return ToContext.DeclarationNames.getCXXOperatorName(
4596 FromName.getCXXOverloadedOperator());
4597
4598 case DeclarationName::CXXLiteralOperatorName:
4599 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4600 Import(FromName.getCXXLiteralIdentifier()));
4601
4602 case DeclarationName::CXXUsingDirective:
4603 // FIXME: STATICS!
4604 return DeclarationName::getUsingDirectiveName();
4605 }
4606
David Blaikie30263482012-01-20 21:50:17 +00004607 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004608}
4609
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004610IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004611 if (!FromId)
4612 return 0;
4613
4614 return &ToContext.Idents.get(FromId->getName());
4615}
Douglas Gregor089459a2010-02-08 21:09:39 +00004616
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004617Selector ASTImporter::Import(Selector FromSel) {
4618 if (FromSel.isNull())
4619 return Selector();
4620
Chris Lattner5f9e2722011-07-23 10:55:15 +00004621 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004622 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4623 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4624 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4625 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4626}
4627
Douglas Gregor089459a2010-02-08 21:09:39 +00004628DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4629 DeclContext *DC,
4630 unsigned IDNS,
4631 NamedDecl **Decls,
4632 unsigned NumDecls) {
4633 return Name;
4634}
4635
4636DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004637 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004638}
4639
4640DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004641 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004642}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004643
Douglas Gregorac32ff92012-02-01 21:00:38 +00004644void ASTImporter::CompleteDecl (Decl *D) {
4645 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4646 if (!ID->getDefinition())
4647 ID->startDefinition();
4648 }
4649 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4650 if (!PD->getDefinition())
4651 PD->startDefinition();
4652 }
4653 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4654 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4655 TD->startDefinition();
4656 TD->setCompleteDefinition(true);
4657 }
4658 }
4659 else {
4660 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4661 }
4662}
4663
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004664Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4665 ImportedDecls[From] = To;
4666 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004667}
Douglas Gregorea35d112010-02-15 23:54:17 +00004668
4669bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004670 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004671 = ImportedTypes.find(From.getTypePtr());
4672 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4673 return true;
4674
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004675 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004676 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004677}