blob: 9931961c0573b653e55ee1dd55aa3de828b8e960 [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
81 // 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 Gregor1cf038c2011-07-29 23:31:30 +000089 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
90 bool ForceImport = false);
91 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
92 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000093 TemplateParameterList *ImportTemplateParameterList(
94 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000095 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
96 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
97 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +000099 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000100 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000101 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000102 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000103 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000104 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000105 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000106 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000107 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000108 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000109 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000110 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000111 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000112 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
113 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
114 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
115 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000116 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000117 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000118 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000119 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000120 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000121 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000122 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000123 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000124 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000125 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000126 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000127 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000128 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000129 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000130 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000131 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
132 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
133 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
134 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000135 Decl *VisitClassTemplateSpecializationDecl(
136 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000137
Douglas Gregor4800d952010-02-11 19:21:55 +0000138 // Importing statements
139 Stmt *VisitStmt(Stmt *S);
140
141 // Importing expressions
142 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000143 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000144 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000145 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000146 Expr *VisitParenExpr(ParenExpr *E);
147 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000148 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000149 Expr *VisitBinaryOperator(BinaryOperator *E);
150 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000151 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000152 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000153 };
154}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000155using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000156
157//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000158// Structural Equivalence
159//----------------------------------------------------------------------------
160
161namespace {
162 struct StructuralEquivalenceContext {
163 /// \brief AST contexts for which we are checking structural equivalence.
164 ASTContext &C1, &C2;
165
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000166 /// \brief The set of "tentative" equivalences between two canonical
167 /// declarations, mapping from a declaration in the first context to the
168 /// declaration in the second context that we believe to be equivalent.
169 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
170
171 /// \brief Queue of declarations in the first context whose equivalence
172 /// with a declaration in the second context still needs to be verified.
173 std::deque<Decl *> DeclsToCheck;
174
Douglas Gregorea35d112010-02-15 23:54:17 +0000175 /// \brief Declaration (from, to) pairs that are known not to be equivalent
176 /// (which we have already complained about).
177 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
178
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000179 /// \brief Whether we're being strict about the spelling of types when
180 /// unifying two types.
181 bool StrictTypeSpelling;
182
183 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000184 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000185 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000186 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000187 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000188
189 /// \brief Determine whether the two declarations are structurally
190 /// equivalent.
191 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
192
193 /// \brief Determine whether the two types are structurally equivalent.
194 bool IsStructurallyEquivalent(QualType T1, QualType T2);
195
196 private:
197 /// \brief Finish checking all of the structural equivalences.
198 ///
199 /// \returns true if an error occurred, false otherwise.
200 bool Finish();
201
202 public:
203 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000204 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000205 }
206
207 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000208 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000209 }
210 };
211}
212
213static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
214 QualType T1, QualType T2);
215static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
216 Decl *D1, Decl *D2);
217
218/// \brief Determine if two APInts have the same value, after zero-extending
219/// one of them (if needed!) to ensure that the bit-widths match.
220static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
221 if (I1.getBitWidth() == I2.getBitWidth())
222 return I1 == I2;
223
224 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000225 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000226
Jay Foad9f71a8f2010-12-07 08:25:34 +0000227 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000228}
229
230/// \brief Determine if two APSInts have the same value, zero- or sign-extending
231/// as needed.
232static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
233 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
234 return I1 == I2;
235
236 // Check for a bit-width mismatch.
237 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000238 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000239 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000240 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000241
242 // We have a signedness mismatch. Turn the signed value into an unsigned
243 // value.
244 if (I1.isSigned()) {
245 if (I1.isNegative())
246 return false;
247
248 return llvm::APSInt(I1, true) == I2;
249 }
250
251 if (I2.isNegative())
252 return false;
253
254 return I1 == llvm::APSInt(I2, true);
255}
256
257/// \brief Determine structural equivalence of two expressions.
258static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
259 Expr *E1, Expr *E2) {
260 if (!E1 || !E2)
261 return E1 == E2;
262
263 // FIXME: Actually perform a structural comparison!
264 return true;
265}
266
267/// \brief Determine whether two identifiers are equivalent.
268static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
269 const IdentifierInfo *Name2) {
270 if (!Name1 || !Name2)
271 return Name1 == Name2;
272
273 return Name1->getName() == Name2->getName();
274}
275
276/// \brief Determine whether two nested-name-specifiers are equivalent.
277static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
278 NestedNameSpecifier *NNS1,
279 NestedNameSpecifier *NNS2) {
280 // FIXME: Implement!
281 return true;
282}
283
284/// \brief Determine whether two template arguments are equivalent.
285static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
286 const TemplateArgument &Arg1,
287 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000288 if (Arg1.getKind() != Arg2.getKind())
289 return false;
290
291 switch (Arg1.getKind()) {
292 case TemplateArgument::Null:
293 return true;
294
295 case TemplateArgument::Type:
296 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
297
298 case TemplateArgument::Integral:
299 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
300 Arg2.getIntegralType()))
301 return false;
302
303 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
304
305 case TemplateArgument::Declaration:
306 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
307
308 case TemplateArgument::Template:
309 return IsStructurallyEquivalent(Context,
310 Arg1.getAsTemplate(),
311 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000312
313 case TemplateArgument::TemplateExpansion:
314 return IsStructurallyEquivalent(Context,
315 Arg1.getAsTemplateOrTemplatePattern(),
316 Arg2.getAsTemplateOrTemplatePattern());
317
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000318 case TemplateArgument::Expression:
319 return IsStructurallyEquivalent(Context,
320 Arg1.getAsExpr(), Arg2.getAsExpr());
321
322 case TemplateArgument::Pack:
323 if (Arg1.pack_size() != Arg2.pack_size())
324 return false;
325
326 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
327 if (!IsStructurallyEquivalent(Context,
328 Arg1.pack_begin()[I],
329 Arg2.pack_begin()[I]))
330 return false;
331
332 return true;
333 }
334
335 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000336 return true;
337}
338
339/// \brief Determine structural equivalence for the common part of array
340/// types.
341static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
342 const ArrayType *Array1,
343 const ArrayType *Array2) {
344 if (!IsStructurallyEquivalent(Context,
345 Array1->getElementType(),
346 Array2->getElementType()))
347 return false;
348 if (Array1->getSizeModifier() != Array2->getSizeModifier())
349 return false;
350 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
351 return false;
352
353 return true;
354}
355
356/// \brief Determine structural equivalence of two types.
357static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
358 QualType T1, QualType T2) {
359 if (T1.isNull() || T2.isNull())
360 return T1.isNull() && T2.isNull();
361
362 if (!Context.StrictTypeSpelling) {
363 // We aren't being strict about token-to-token equivalence of types,
364 // so map down to the canonical type.
365 T1 = Context.C1.getCanonicalType(T1);
366 T2 = Context.C2.getCanonicalType(T2);
367 }
368
369 if (T1.getQualifiers() != T2.getQualifiers())
370 return false;
371
Douglas Gregorea35d112010-02-15 23:54:17 +0000372 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000373
Douglas Gregorea35d112010-02-15 23:54:17 +0000374 if (T1->getTypeClass() != T2->getTypeClass()) {
375 // Compare function types with prototypes vs. without prototypes as if
376 // both did not have prototypes.
377 if (T1->getTypeClass() == Type::FunctionProto &&
378 T2->getTypeClass() == Type::FunctionNoProto)
379 TC = Type::FunctionNoProto;
380 else if (T1->getTypeClass() == Type::FunctionNoProto &&
381 T2->getTypeClass() == Type::FunctionProto)
382 TC = Type::FunctionNoProto;
383 else
384 return false;
385 }
386
387 switch (TC) {
388 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000389 // FIXME: Deal with Char_S/Char_U.
390 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
391 return false;
392 break;
393
394 case Type::Complex:
395 if (!IsStructurallyEquivalent(Context,
396 cast<ComplexType>(T1)->getElementType(),
397 cast<ComplexType>(T2)->getElementType()))
398 return false;
399 break;
400
401 case Type::Pointer:
402 if (!IsStructurallyEquivalent(Context,
403 cast<PointerType>(T1)->getPointeeType(),
404 cast<PointerType>(T2)->getPointeeType()))
405 return false;
406 break;
407
408 case Type::BlockPointer:
409 if (!IsStructurallyEquivalent(Context,
410 cast<BlockPointerType>(T1)->getPointeeType(),
411 cast<BlockPointerType>(T2)->getPointeeType()))
412 return false;
413 break;
414
415 case Type::LValueReference:
416 case Type::RValueReference: {
417 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
418 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
419 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
420 return false;
421 if (Ref1->isInnerRef() != Ref2->isInnerRef())
422 return false;
423 if (!IsStructurallyEquivalent(Context,
424 Ref1->getPointeeTypeAsWritten(),
425 Ref2->getPointeeTypeAsWritten()))
426 return false;
427 break;
428 }
429
430 case Type::MemberPointer: {
431 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
432 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
433 if (!IsStructurallyEquivalent(Context,
434 MemPtr1->getPointeeType(),
435 MemPtr2->getPointeeType()))
436 return false;
437 if (!IsStructurallyEquivalent(Context,
438 QualType(MemPtr1->getClass(), 0),
439 QualType(MemPtr2->getClass(), 0)))
440 return false;
441 break;
442 }
443
444 case Type::ConstantArray: {
445 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
446 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
447 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
448 return false;
449
450 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
451 return false;
452 break;
453 }
454
455 case Type::IncompleteArray:
456 if (!IsArrayStructurallyEquivalent(Context,
457 cast<ArrayType>(T1),
458 cast<ArrayType>(T2)))
459 return false;
460 break;
461
462 case Type::VariableArray: {
463 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
464 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
465 if (!IsStructurallyEquivalent(Context,
466 Array1->getSizeExpr(), Array2->getSizeExpr()))
467 return false;
468
469 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
470 return false;
471
472 break;
473 }
474
475 case Type::DependentSizedArray: {
476 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
477 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
478 if (!IsStructurallyEquivalent(Context,
479 Array1->getSizeExpr(), Array2->getSizeExpr()))
480 return false;
481
482 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
483 return false;
484
485 break;
486 }
487
488 case Type::DependentSizedExtVector: {
489 const DependentSizedExtVectorType *Vec1
490 = cast<DependentSizedExtVectorType>(T1);
491 const DependentSizedExtVectorType *Vec2
492 = cast<DependentSizedExtVectorType>(T2);
493 if (!IsStructurallyEquivalent(Context,
494 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
495 return false;
496 if (!IsStructurallyEquivalent(Context,
497 Vec1->getElementType(),
498 Vec2->getElementType()))
499 return false;
500 break;
501 }
502
503 case Type::Vector:
504 case Type::ExtVector: {
505 const VectorType *Vec1 = cast<VectorType>(T1);
506 const VectorType *Vec2 = cast<VectorType>(T2);
507 if (!IsStructurallyEquivalent(Context,
508 Vec1->getElementType(),
509 Vec2->getElementType()))
510 return false;
511 if (Vec1->getNumElements() != Vec2->getNumElements())
512 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000513 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000514 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000515 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000516 }
517
518 case Type::FunctionProto: {
519 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
520 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
521 if (Proto1->getNumArgs() != Proto2->getNumArgs())
522 return false;
523 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
524 if (!IsStructurallyEquivalent(Context,
525 Proto1->getArgType(I),
526 Proto2->getArgType(I)))
527 return false;
528 }
529 if (Proto1->isVariadic() != Proto2->isVariadic())
530 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000531 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000532 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000533 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
534 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
535 return false;
536 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
537 if (!IsStructurallyEquivalent(Context,
538 Proto1->getExceptionType(I),
539 Proto2->getExceptionType(I)))
540 return false;
541 }
542 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000543 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000544 Proto1->getNoexceptExpr(),
545 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000546 return false;
547 }
548 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
549 return false;
550
551 // Fall through to check the bits common with FunctionNoProtoType.
552 }
553
554 case Type::FunctionNoProto: {
555 const FunctionType *Function1 = cast<FunctionType>(T1);
556 const FunctionType *Function2 = cast<FunctionType>(T2);
557 if (!IsStructurallyEquivalent(Context,
558 Function1->getResultType(),
559 Function2->getResultType()))
560 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000561 if (Function1->getExtInfo() != Function2->getExtInfo())
562 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000563 break;
564 }
565
566 case Type::UnresolvedUsing:
567 if (!IsStructurallyEquivalent(Context,
568 cast<UnresolvedUsingType>(T1)->getDecl(),
569 cast<UnresolvedUsingType>(T2)->getDecl()))
570 return false;
571
572 break;
John McCall9d156a72011-01-06 01:58:22 +0000573
574 case Type::Attributed:
575 if (!IsStructurallyEquivalent(Context,
576 cast<AttributedType>(T1)->getModifiedType(),
577 cast<AttributedType>(T2)->getModifiedType()))
578 return false;
579 if (!IsStructurallyEquivalent(Context,
580 cast<AttributedType>(T1)->getEquivalentType(),
581 cast<AttributedType>(T2)->getEquivalentType()))
582 return false;
583 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000584
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000585 case Type::Paren:
586 if (!IsStructurallyEquivalent(Context,
587 cast<ParenType>(T1)->getInnerType(),
588 cast<ParenType>(T2)->getInnerType()))
589 return false;
590 break;
591
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000592 case Type::Typedef:
593 if (!IsStructurallyEquivalent(Context,
594 cast<TypedefType>(T1)->getDecl(),
595 cast<TypedefType>(T2)->getDecl()))
596 return false;
597 break;
598
599 case Type::TypeOfExpr:
600 if (!IsStructurallyEquivalent(Context,
601 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
602 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
603 return false;
604 break;
605
606 case Type::TypeOf:
607 if (!IsStructurallyEquivalent(Context,
608 cast<TypeOfType>(T1)->getUnderlyingType(),
609 cast<TypeOfType>(T2)->getUnderlyingType()))
610 return false;
611 break;
Sean Huntca63c202011-05-24 22:41:36 +0000612
613 case Type::UnaryTransform:
614 if (!IsStructurallyEquivalent(Context,
615 cast<UnaryTransformType>(T1)->getUnderlyingType(),
616 cast<UnaryTransformType>(T1)->getUnderlyingType()))
617 return false;
618 break;
619
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000620 case Type::Decltype:
621 if (!IsStructurallyEquivalent(Context,
622 cast<DecltypeType>(T1)->getUnderlyingExpr(),
623 cast<DecltypeType>(T2)->getUnderlyingExpr()))
624 return false;
625 break;
626
Richard Smith34b41d92011-02-20 03:19:35 +0000627 case Type::Auto:
628 if (!IsStructurallyEquivalent(Context,
629 cast<AutoType>(T1)->getDeducedType(),
630 cast<AutoType>(T2)->getDeducedType()))
631 return false;
632 break;
633
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000634 case Type::Record:
635 case Type::Enum:
636 if (!IsStructurallyEquivalent(Context,
637 cast<TagType>(T1)->getDecl(),
638 cast<TagType>(T2)->getDecl()))
639 return false;
640 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000641
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000642 case Type::TemplateTypeParm: {
643 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
644 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
645 if (Parm1->getDepth() != Parm2->getDepth())
646 return false;
647 if (Parm1->getIndex() != Parm2->getIndex())
648 return false;
649 if (Parm1->isParameterPack() != Parm2->isParameterPack())
650 return false;
651
652 // Names of template type parameters are never significant.
653 break;
654 }
655
656 case Type::SubstTemplateTypeParm: {
657 const SubstTemplateTypeParmType *Subst1
658 = cast<SubstTemplateTypeParmType>(T1);
659 const SubstTemplateTypeParmType *Subst2
660 = cast<SubstTemplateTypeParmType>(T2);
661 if (!IsStructurallyEquivalent(Context,
662 QualType(Subst1->getReplacedParameter(), 0),
663 QualType(Subst2->getReplacedParameter(), 0)))
664 return false;
665 if (!IsStructurallyEquivalent(Context,
666 Subst1->getReplacementType(),
667 Subst2->getReplacementType()))
668 return false;
669 break;
670 }
671
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000672 case Type::SubstTemplateTypeParmPack: {
673 const SubstTemplateTypeParmPackType *Subst1
674 = cast<SubstTemplateTypeParmPackType>(T1);
675 const SubstTemplateTypeParmPackType *Subst2
676 = cast<SubstTemplateTypeParmPackType>(T2);
677 if (!IsStructurallyEquivalent(Context,
678 QualType(Subst1->getReplacedParameter(), 0),
679 QualType(Subst2->getReplacedParameter(), 0)))
680 return false;
681 if (!IsStructurallyEquivalent(Context,
682 Subst1->getArgumentPack(),
683 Subst2->getArgumentPack()))
684 return false;
685 break;
686 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000687 case Type::TemplateSpecialization: {
688 const TemplateSpecializationType *Spec1
689 = cast<TemplateSpecializationType>(T1);
690 const TemplateSpecializationType *Spec2
691 = cast<TemplateSpecializationType>(T2);
692 if (!IsStructurallyEquivalent(Context,
693 Spec1->getTemplateName(),
694 Spec2->getTemplateName()))
695 return false;
696 if (Spec1->getNumArgs() != Spec2->getNumArgs())
697 return false;
698 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
699 if (!IsStructurallyEquivalent(Context,
700 Spec1->getArg(I), Spec2->getArg(I)))
701 return false;
702 }
703 break;
704 }
705
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000706 case Type::Elaborated: {
707 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
708 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
709 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
710 if (Elab1->getKeyword() != Elab2->getKeyword())
711 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000712 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000713 Elab1->getQualifier(),
714 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000715 return false;
716 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000717 Elab1->getNamedType(),
718 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000719 return false;
720 break;
721 }
722
John McCall3cb0ebd2010-03-10 03:28:59 +0000723 case Type::InjectedClassName: {
724 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
725 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
726 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000727 Inj1->getInjectedSpecializationType(),
728 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000729 return false;
730 break;
731 }
732
Douglas Gregor4714c122010-03-31 17:34:00 +0000733 case Type::DependentName: {
734 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
735 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000736 if (!IsStructurallyEquivalent(Context,
737 Typename1->getQualifier(),
738 Typename2->getQualifier()))
739 return false;
740 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
741 Typename2->getIdentifier()))
742 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000743
744 break;
745 }
746
John McCall33500952010-06-11 00:33:02 +0000747 case Type::DependentTemplateSpecialization: {
748 const DependentTemplateSpecializationType *Spec1 =
749 cast<DependentTemplateSpecializationType>(T1);
750 const DependentTemplateSpecializationType *Spec2 =
751 cast<DependentTemplateSpecializationType>(T2);
752 if (!IsStructurallyEquivalent(Context,
753 Spec1->getQualifier(),
754 Spec2->getQualifier()))
755 return false;
756 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
757 Spec2->getIdentifier()))
758 return false;
759 if (Spec1->getNumArgs() != Spec2->getNumArgs())
760 return false;
761 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
762 if (!IsStructurallyEquivalent(Context,
763 Spec1->getArg(I), Spec2->getArg(I)))
764 return false;
765 }
766 break;
767 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000768
769 case Type::PackExpansion:
770 if (!IsStructurallyEquivalent(Context,
771 cast<PackExpansionType>(T1)->getPattern(),
772 cast<PackExpansionType>(T2)->getPattern()))
773 return false;
774 break;
775
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000776 case Type::ObjCInterface: {
777 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
778 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
779 if (!IsStructurallyEquivalent(Context,
780 Iface1->getDecl(), Iface2->getDecl()))
781 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000782 break;
783 }
784
785 case Type::ObjCObject: {
786 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
787 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
788 if (!IsStructurallyEquivalent(Context,
789 Obj1->getBaseType(),
790 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000791 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000792 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
793 return false;
794 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000795 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000796 Obj1->getProtocol(I),
797 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 return false;
799 }
800 break;
801 }
802
803 case Type::ObjCObjectPointer: {
804 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
805 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
806 if (!IsStructurallyEquivalent(Context,
807 Ptr1->getPointeeType(),
808 Ptr2->getPointeeType()))
809 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000810 break;
811 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000812
813 case Type::Atomic: {
814 if (!IsStructurallyEquivalent(Context,
815 cast<AtomicType>(T1)->getValueType(),
816 cast<AtomicType>(T2)->getValueType()))
817 return false;
818 break;
819 }
820
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000821 } // end switch
822
823 return true;
824}
825
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000826/// \brief Determine structural equivalence of two fields.
827static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
828 FieldDecl *Field1, FieldDecl *Field2) {
829 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
830
831 if (!IsStructurallyEquivalent(Context,
832 Field1->getType(), Field2->getType())) {
833 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
834 << Context.C2.getTypeDeclType(Owner2);
835 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
836 << Field2->getDeclName() << Field2->getType();
837 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
838 << Field1->getDeclName() << Field1->getType();
839 return false;
840 }
841
842 if (Field1->isBitField() != Field2->isBitField()) {
843 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
844 << Context.C2.getTypeDeclType(Owner2);
845 if (Field1->isBitField()) {
846 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
847 << Field1->getDeclName() << Field1->getType()
848 << Field1->getBitWidthValue(Context.C1);
849 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
850 << Field2->getDeclName();
851 } else {
852 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
853 << Field2->getDeclName() << Field2->getType()
854 << Field2->getBitWidthValue(Context.C2);
855 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
856 << Field1->getDeclName();
857 }
858 return false;
859 }
860
861 if (Field1->isBitField()) {
862 // Make sure that the bit-fields are the same length.
863 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
864 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
865
866 if (Bits1 != Bits2) {
867 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
868 << Context.C2.getTypeDeclType(Owner2);
869 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
870 << Field2->getDeclName() << Field2->getType() << Bits2;
871 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
872 << Field1->getDeclName() << Field1->getType() << Bits1;
873 return false;
874 }
875 }
876
877 return true;
878}
879
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000880/// \brief Determine structural equivalence of two records.
881static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
882 RecordDecl *D1, RecordDecl *D2) {
883 if (D1->isUnion() != D2->isUnion()) {
884 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
885 << Context.C2.getTypeDeclType(D2);
886 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
887 << D1->getDeclName() << (unsigned)D1->getTagKind();
888 return false;
889 }
890
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000891 // If both declarations are class template specializations, we know
892 // the ODR applies, so check the template and template arguments.
893 ClassTemplateSpecializationDecl *Spec1
894 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
895 ClassTemplateSpecializationDecl *Spec2
896 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
897 if (Spec1 && Spec2) {
898 // Check that the specialized templates are the same.
899 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
900 Spec2->getSpecializedTemplate()))
901 return false;
902
903 // Check that the template arguments are the same.
904 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
905 return false;
906
907 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
908 if (!IsStructurallyEquivalent(Context,
909 Spec1->getTemplateArgs().get(I),
910 Spec2->getTemplateArgs().get(I)))
911 return false;
912 }
913 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000914 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000915 else if (Spec1 || Spec2)
916 return false;
917
Douglas Gregorea35d112010-02-15 23:54:17 +0000918 // Compare the definitions of these two records. If either or both are
919 // incomplete, we assume that they are equivalent.
920 D1 = D1->getDefinition();
921 D2 = D2->getDefinition();
922 if (!D1 || !D2)
923 return true;
924
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000925 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
926 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
927 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
928 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000929 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000930 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000931 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000932 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000933 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000934 return false;
935 }
936
937 // Check the base classes.
938 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
939 BaseEnd1 = D1CXX->bases_end(),
940 Base2 = D2CXX->bases_begin();
941 Base1 != BaseEnd1;
942 ++Base1, ++Base2) {
943 if (!IsStructurallyEquivalent(Context,
944 Base1->getType(), Base2->getType())) {
945 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
946 << Context.C2.getTypeDeclType(D2);
947 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
948 << Base2->getType()
949 << Base2->getSourceRange();
950 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
951 << Base1->getType()
952 << Base1->getSourceRange();
953 return false;
954 }
955
956 // Check virtual vs. non-virtual inheritance mismatch.
957 if (Base1->isVirtual() != Base2->isVirtual()) {
958 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
959 << Context.C2.getTypeDeclType(D2);
960 Context.Diag2(Base2->getSourceRange().getBegin(),
961 diag::note_odr_virtual_base)
962 << Base2->isVirtual() << Base2->getSourceRange();
963 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
964 << Base1->isVirtual()
965 << Base1->getSourceRange();
966 return false;
967 }
968 }
969 } else if (D1CXX->getNumBases() > 0) {
970 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
971 << Context.C2.getTypeDeclType(D2);
972 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
973 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
974 << Base1->getType()
975 << Base1->getSourceRange();
976 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
977 return false;
978 }
979 }
980
981 // Check the fields for consistency.
982 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
983 Field2End = D2->field_end();
984 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
985 Field1End = D1->field_end();
986 Field1 != Field1End;
987 ++Field1, ++Field2) {
988 if (Field2 == Field2End) {
989 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
990 << Context.C2.getTypeDeclType(D2);
991 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
992 << Field1->getDeclName() << Field1->getType();
993 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
994 return false;
995 }
996
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000997 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
998 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000999 }
1000
1001 if (Field2 != Field2End) {
1002 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1003 << Context.C2.getTypeDeclType(D2);
1004 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1005 << Field2->getDeclName() << Field2->getType();
1006 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1007 return false;
1008 }
1009
1010 return true;
1011}
1012
1013/// \brief Determine structural equivalence of two enums.
1014static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1015 EnumDecl *D1, EnumDecl *D2) {
1016 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1017 EC2End = D2->enumerator_end();
1018 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1019 EC1End = D1->enumerator_end();
1020 EC1 != EC1End; ++EC1, ++EC2) {
1021 if (EC2 == EC2End) {
1022 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1023 << Context.C2.getTypeDeclType(D2);
1024 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1025 << EC1->getDeclName()
1026 << EC1->getInitVal().toString(10);
1027 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1028 return false;
1029 }
1030
1031 llvm::APSInt Val1 = EC1->getInitVal();
1032 llvm::APSInt Val2 = EC2->getInitVal();
1033 if (!IsSameValue(Val1, Val2) ||
1034 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1035 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1036 << Context.C2.getTypeDeclType(D2);
1037 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1038 << EC2->getDeclName()
1039 << EC2->getInitVal().toString(10);
1040 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1041 << EC1->getDeclName()
1042 << EC1->getInitVal().toString(10);
1043 return false;
1044 }
1045 }
1046
1047 if (EC2 != EC2End) {
1048 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1049 << Context.C2.getTypeDeclType(D2);
1050 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1051 << EC2->getDeclName()
1052 << EC2->getInitVal().toString(10);
1053 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1054 return false;
1055 }
1056
1057 return true;
1058}
Douglas Gregor040afae2010-11-30 19:14:50 +00001059
1060static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1061 TemplateParameterList *Params1,
1062 TemplateParameterList *Params2) {
1063 if (Params1->size() != Params2->size()) {
1064 Context.Diag2(Params2->getTemplateLoc(),
1065 diag::err_odr_different_num_template_parameters)
1066 << Params1->size() << Params2->size();
1067 Context.Diag1(Params1->getTemplateLoc(),
1068 diag::note_odr_template_parameter_list);
1069 return false;
1070 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001071
Douglas Gregor040afae2010-11-30 19:14:50 +00001072 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1073 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1074 Context.Diag2(Params2->getParam(I)->getLocation(),
1075 diag::err_odr_different_template_parameter_kind);
1076 Context.Diag1(Params1->getParam(I)->getLocation(),
1077 diag::note_odr_template_parameter_here);
1078 return false;
1079 }
1080
1081 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1082 Params2->getParam(I))) {
1083
1084 return false;
1085 }
1086 }
1087
1088 return true;
1089}
1090
1091static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1092 TemplateTypeParmDecl *D1,
1093 TemplateTypeParmDecl *D2) {
1094 if (D1->isParameterPack() != D2->isParameterPack()) {
1095 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1096 << D2->isParameterPack();
1097 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1098 << D1->isParameterPack();
1099 return false;
1100 }
1101
1102 return true;
1103}
1104
1105static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1106 NonTypeTemplateParmDecl *D1,
1107 NonTypeTemplateParmDecl *D2) {
1108 // FIXME: Enable once we have variadic templates.
1109#if 0
1110 if (D1->isParameterPack() != D2->isParameterPack()) {
1111 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1112 << D2->isParameterPack();
1113 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1114 << D1->isParameterPack();
1115 return false;
1116 }
1117#endif
1118
1119 // Check types.
1120 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1121 Context.Diag2(D2->getLocation(),
1122 diag::err_odr_non_type_parameter_type_inconsistent)
1123 << D2->getType() << D1->getType();
1124 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1125 << D1->getType();
1126 return false;
1127 }
1128
1129 return true;
1130}
1131
1132static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1133 TemplateTemplateParmDecl *D1,
1134 TemplateTemplateParmDecl *D2) {
1135 // FIXME: Enable once we have variadic templates.
1136#if 0
1137 if (D1->isParameterPack() != D2->isParameterPack()) {
1138 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1139 << D2->isParameterPack();
1140 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1141 << D1->isParameterPack();
1142 return false;
1143 }
1144#endif
1145
1146 // Check template parameter lists.
1147 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1148 D2->getTemplateParameters());
1149}
1150
1151static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1152 ClassTemplateDecl *D1,
1153 ClassTemplateDecl *D2) {
1154 // Check template parameters.
1155 if (!IsStructurallyEquivalent(Context,
1156 D1->getTemplateParameters(),
1157 D2->getTemplateParameters()))
1158 return false;
1159
1160 // Check the templated declaration.
1161 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1162 D2->getTemplatedDecl());
1163}
1164
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001165/// \brief Determine structural equivalence of two declarations.
1166static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1167 Decl *D1, Decl *D2) {
1168 // FIXME: Check for known structural equivalences via a callback of some sort.
1169
Douglas Gregorea35d112010-02-15 23:54:17 +00001170 // Check whether we already know that these two declarations are not
1171 // structurally equivalent.
1172 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1173 D2->getCanonicalDecl())))
1174 return false;
1175
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001176 // Determine whether we've already produced a tentative equivalence for D1.
1177 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1178 if (EquivToD1)
1179 return EquivToD1 == D2->getCanonicalDecl();
1180
1181 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1182 EquivToD1 = D2->getCanonicalDecl();
1183 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1184 return true;
1185}
1186
1187bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1188 Decl *D2) {
1189 if (!::IsStructurallyEquivalent(*this, D1, D2))
1190 return false;
1191
1192 return !Finish();
1193}
1194
1195bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1196 QualType T2) {
1197 if (!::IsStructurallyEquivalent(*this, T1, T2))
1198 return false;
1199
1200 return !Finish();
1201}
1202
1203bool StructuralEquivalenceContext::Finish() {
1204 while (!DeclsToCheck.empty()) {
1205 // Check the next declaration.
1206 Decl *D1 = DeclsToCheck.front();
1207 DeclsToCheck.pop_front();
1208
1209 Decl *D2 = TentativeEquivalences[D1];
1210 assert(D2 && "Unrecorded tentative equivalence?");
1211
Douglas Gregorea35d112010-02-15 23:54:17 +00001212 bool Equivalent = true;
1213
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001214 // FIXME: Switch on all declaration kinds. For now, we're just going to
1215 // check the obvious ones.
1216 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1217 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1218 // Check for equivalent structure names.
1219 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001220 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1221 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001222 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001223 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1224 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001225 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1226 !::IsStructurallyEquivalent(*this, Record1, Record2))
1227 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001228 } else {
1229 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001230 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001231 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001232 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001233 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1234 // Check for equivalent enum names.
1235 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001236 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1237 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001238 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001239 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1240 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001241 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1242 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1243 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001244 } else {
1245 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001246 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001247 }
Richard Smith162e1c12011-04-15 14:24:37 +00001248 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1249 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001250 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001251 Typedef2->getIdentifier()) ||
1252 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001253 Typedef1->getUnderlyingType(),
1254 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001255 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001256 } else {
1257 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001258 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001259 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001260 } else if (ClassTemplateDecl *ClassTemplate1
1261 = dyn_cast<ClassTemplateDecl>(D1)) {
1262 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1263 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1264 ClassTemplate2->getIdentifier()) ||
1265 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1266 Equivalent = false;
1267 } else {
1268 // Class template/non-class-template mismatch.
1269 Equivalent = false;
1270 }
1271 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1272 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1273 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1274 Equivalent = false;
1275 } else {
1276 // Kind mismatch.
1277 Equivalent = false;
1278 }
1279 } else if (NonTypeTemplateParmDecl *NTTP1
1280 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1281 if (NonTypeTemplateParmDecl *NTTP2
1282 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1283 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1284 Equivalent = false;
1285 } else {
1286 // Kind mismatch.
1287 Equivalent = false;
1288 }
1289 } else if (TemplateTemplateParmDecl *TTP1
1290 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1291 if (TemplateTemplateParmDecl *TTP2
1292 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1293 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1294 Equivalent = false;
1295 } else {
1296 // Kind mismatch.
1297 Equivalent = false;
1298 }
1299 }
1300
Douglas Gregorea35d112010-02-15 23:54:17 +00001301 if (!Equivalent) {
1302 // Note that these two declarations are not equivalent (and we already
1303 // know about it).
1304 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1305 D2->getCanonicalDecl()));
1306 return true;
1307 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001308 // FIXME: Check other declaration kinds!
1309 }
1310
1311 return false;
1312}
1313
1314//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001315// Import Types
1316//----------------------------------------------------------------------------
1317
John McCallf4c73712011-01-19 06:33:43 +00001318QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001319 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1320 << T->getTypeClassName();
1321 return QualType();
1322}
1323
John McCallf4c73712011-01-19 06:33:43 +00001324QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001325 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001326#define SHARED_SINGLETON_TYPE(Expansion)
1327#define BUILTIN_TYPE(Id, SingletonId) \
1328 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1329#include "clang/AST/BuiltinTypes.def"
1330
1331 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1332 // context supports C++.
1333
1334 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1335 // context supports ObjC.
1336
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001337 case BuiltinType::Char_U:
1338 // The context we're importing from has an unsigned 'char'. If we're
1339 // importing into a context with a signed 'char', translate to
1340 // 'unsigned char' instead.
1341 if (Importer.getToContext().getLangOptions().CharIsSigned)
1342 return Importer.getToContext().UnsignedCharTy;
1343
1344 return Importer.getToContext().CharTy;
1345
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001346 case BuiltinType::Char_S:
1347 // The context we're importing from has an unsigned 'char'. If we're
1348 // importing into a context with a signed 'char', translate to
1349 // 'unsigned char' instead.
1350 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1351 return Importer.getToContext().SignedCharTy;
1352
1353 return Importer.getToContext().CharTy;
1354
Chris Lattner3f59c972010-12-25 23:25:43 +00001355 case BuiltinType::WChar_S:
1356 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001357 // FIXME: If not in C++, shall we translate to the C equivalent of
1358 // wchar_t?
1359 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001360 }
1361
1362 return QualType();
1363}
1364
John McCallf4c73712011-01-19 06:33:43 +00001365QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001366 QualType ToElementType = Importer.Import(T->getElementType());
1367 if (ToElementType.isNull())
1368 return QualType();
1369
1370 return Importer.getToContext().getComplexType(ToElementType);
1371}
1372
John McCallf4c73712011-01-19 06:33:43 +00001373QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001374 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1375 if (ToPointeeType.isNull())
1376 return QualType();
1377
1378 return Importer.getToContext().getPointerType(ToPointeeType);
1379}
1380
John McCallf4c73712011-01-19 06:33:43 +00001381QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001382 // FIXME: Check for blocks support in "to" context.
1383 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1384 if (ToPointeeType.isNull())
1385 return QualType();
1386
1387 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1388}
1389
John McCallf4c73712011-01-19 06:33:43 +00001390QualType
1391ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001392 // FIXME: Check for C++ support in "to" context.
1393 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1394 if (ToPointeeType.isNull())
1395 return QualType();
1396
1397 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1398}
1399
John McCallf4c73712011-01-19 06:33:43 +00001400QualType
1401ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001402 // FIXME: Check for C++0x support in "to" context.
1403 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1404 if (ToPointeeType.isNull())
1405 return QualType();
1406
1407 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1408}
1409
John McCallf4c73712011-01-19 06:33:43 +00001410QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001411 // FIXME: Check for C++ support in "to" context.
1412 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1413 if (ToPointeeType.isNull())
1414 return QualType();
1415
1416 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1417 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1418 ClassType.getTypePtr());
1419}
1420
John McCallf4c73712011-01-19 06:33:43 +00001421QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001422 QualType ToElementType = Importer.Import(T->getElementType());
1423 if (ToElementType.isNull())
1424 return QualType();
1425
1426 return Importer.getToContext().getConstantArrayType(ToElementType,
1427 T->getSize(),
1428 T->getSizeModifier(),
1429 T->getIndexTypeCVRQualifiers());
1430}
1431
John McCallf4c73712011-01-19 06:33:43 +00001432QualType
1433ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001434 QualType ToElementType = Importer.Import(T->getElementType());
1435 if (ToElementType.isNull())
1436 return QualType();
1437
1438 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1439 T->getSizeModifier(),
1440 T->getIndexTypeCVRQualifiers());
1441}
1442
John McCallf4c73712011-01-19 06:33:43 +00001443QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001444 QualType ToElementType = Importer.Import(T->getElementType());
1445 if (ToElementType.isNull())
1446 return QualType();
1447
1448 Expr *Size = Importer.Import(T->getSizeExpr());
1449 if (!Size)
1450 return QualType();
1451
1452 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1453 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1454 T->getSizeModifier(),
1455 T->getIndexTypeCVRQualifiers(),
1456 Brackets);
1457}
1458
John McCallf4c73712011-01-19 06:33:43 +00001459QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001460 QualType ToElementType = Importer.Import(T->getElementType());
1461 if (ToElementType.isNull())
1462 return QualType();
1463
1464 return Importer.getToContext().getVectorType(ToElementType,
1465 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001466 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001467}
1468
John McCallf4c73712011-01-19 06:33:43 +00001469QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001470 QualType ToElementType = Importer.Import(T->getElementType());
1471 if (ToElementType.isNull())
1472 return QualType();
1473
1474 return Importer.getToContext().getExtVectorType(ToElementType,
1475 T->getNumElements());
1476}
1477
John McCallf4c73712011-01-19 06:33:43 +00001478QualType
1479ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001480 // FIXME: What happens if we're importing a function without a prototype
1481 // into C++? Should we make it variadic?
1482 QualType ToResultType = Importer.Import(T->getResultType());
1483 if (ToResultType.isNull())
1484 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001485
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001486 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001487 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001488}
1489
John McCallf4c73712011-01-19 06:33:43 +00001490QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001491 QualType ToResultType = Importer.Import(T->getResultType());
1492 if (ToResultType.isNull())
1493 return QualType();
1494
1495 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001496 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001497 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1498 AEnd = T->arg_type_end();
1499 A != AEnd; ++A) {
1500 QualType ArgType = Importer.Import(*A);
1501 if (ArgType.isNull())
1502 return QualType();
1503 ArgTypes.push_back(ArgType);
1504 }
1505
1506 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001507 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001508 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1509 EEnd = T->exception_end();
1510 E != EEnd; ++E) {
1511 QualType ExceptionType = Importer.Import(*E);
1512 if (ExceptionType.isNull())
1513 return QualType();
1514 ExceptionTypes.push_back(ExceptionType);
1515 }
John McCalle23cf432010-12-14 08:05:40 +00001516
1517 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1518 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001519
1520 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001521 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001522}
1523
Sean Callanan0aeb2892011-08-11 16:56:07 +00001524QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1525 QualType ToInnerType = Importer.Import(T->getInnerType());
1526 if (ToInnerType.isNull())
1527 return QualType();
1528
1529 return Importer.getToContext().getParenType(ToInnerType);
1530}
1531
John McCallf4c73712011-01-19 06:33:43 +00001532QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001533 TypedefNameDecl *ToDecl
1534 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001535 if (!ToDecl)
1536 return QualType();
1537
1538 return Importer.getToContext().getTypeDeclType(ToDecl);
1539}
1540
John McCallf4c73712011-01-19 06:33:43 +00001541QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001542 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1543 if (!ToExpr)
1544 return QualType();
1545
1546 return Importer.getToContext().getTypeOfExprType(ToExpr);
1547}
1548
John McCallf4c73712011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001550 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1551 if (ToUnderlyingType.isNull())
1552 return QualType();
1553
1554 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1555}
1556
John McCallf4c73712011-01-19 06:33:43 +00001557QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001558 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001559 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1560 if (!ToExpr)
1561 return QualType();
1562
1563 return Importer.getToContext().getDecltypeType(ToExpr);
1564}
1565
Sean Huntca63c202011-05-24 22:41:36 +00001566QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1567 QualType ToBaseType = Importer.Import(T->getBaseType());
1568 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1569 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1570 return QualType();
1571
1572 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1573 ToUnderlyingType,
1574 T->getUTTKind());
1575}
1576
Richard Smith34b41d92011-02-20 03:19:35 +00001577QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1578 // FIXME: Make sure that the "to" context supports C++0x!
1579 QualType FromDeduced = T->getDeducedType();
1580 QualType ToDeduced;
1581 if (!FromDeduced.isNull()) {
1582 ToDeduced = Importer.Import(FromDeduced);
1583 if (ToDeduced.isNull())
1584 return QualType();
1585 }
1586
1587 return Importer.getToContext().getAutoType(ToDeduced);
1588}
1589
John McCallf4c73712011-01-19 06:33:43 +00001590QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001591 RecordDecl *ToDecl
1592 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1593 if (!ToDecl)
1594 return QualType();
1595
1596 return Importer.getToContext().getTagDeclType(ToDecl);
1597}
1598
John McCallf4c73712011-01-19 06:33:43 +00001599QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001600 EnumDecl *ToDecl
1601 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1602 if (!ToDecl)
1603 return QualType();
1604
1605 return Importer.getToContext().getTagDeclType(ToDecl);
1606}
1607
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001608QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001609 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001610 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1611 if (ToTemplate.isNull())
1612 return QualType();
1613
Chris Lattner5f9e2722011-07-23 10:55:15 +00001614 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001615 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1616 return QualType();
1617
1618 QualType ToCanonType;
1619 if (!QualType(T, 0).isCanonical()) {
1620 QualType FromCanonType
1621 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1622 ToCanonType =Importer.Import(FromCanonType);
1623 if (ToCanonType.isNull())
1624 return QualType();
1625 }
1626 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1627 ToTemplateArgs.data(),
1628 ToTemplateArgs.size(),
1629 ToCanonType);
1630}
1631
John McCallf4c73712011-01-19 06:33:43 +00001632QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001633 NestedNameSpecifier *ToQualifier = 0;
1634 // Note: the qualifier in an ElaboratedType is optional.
1635 if (T->getQualifier()) {
1636 ToQualifier = Importer.Import(T->getQualifier());
1637 if (!ToQualifier)
1638 return QualType();
1639 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001640
1641 QualType ToNamedType = Importer.Import(T->getNamedType());
1642 if (ToNamedType.isNull())
1643 return QualType();
1644
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001645 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1646 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001647}
1648
John McCallf4c73712011-01-19 06:33:43 +00001649QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001650 ObjCInterfaceDecl *Class
1651 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1652 if (!Class)
1653 return QualType();
1654
John McCallc12c5bb2010-05-15 11:32:37 +00001655 return Importer.getToContext().getObjCInterfaceType(Class);
1656}
1657
John McCallf4c73712011-01-19 06:33:43 +00001658QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001659 QualType ToBaseType = Importer.Import(T->getBaseType());
1660 if (ToBaseType.isNull())
1661 return QualType();
1662
Chris Lattner5f9e2722011-07-23 10:55:15 +00001663 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001664 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001665 PEnd = T->qual_end();
1666 P != PEnd; ++P) {
1667 ObjCProtocolDecl *Protocol
1668 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1669 if (!Protocol)
1670 return QualType();
1671 Protocols.push_back(Protocol);
1672 }
1673
John McCallc12c5bb2010-05-15 11:32:37 +00001674 return Importer.getToContext().getObjCObjectType(ToBaseType,
1675 Protocols.data(),
1676 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001677}
1678
John McCallf4c73712011-01-19 06:33:43 +00001679QualType
1680ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001681 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1682 if (ToPointeeType.isNull())
1683 return QualType();
1684
John McCallc12c5bb2010-05-15 11:32:37 +00001685 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001686}
1687
Douglas Gregor089459a2010-02-08 21:09:39 +00001688//----------------------------------------------------------------------------
1689// Import Declarations
1690//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001691bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1692 DeclContext *&LexicalDC,
1693 DeclarationName &Name,
1694 SourceLocation &Loc) {
1695 // Import the context of this declaration.
1696 DC = Importer.ImportContext(D->getDeclContext());
1697 if (!DC)
1698 return true;
1699
1700 LexicalDC = DC;
1701 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1702 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1703 if (!LexicalDC)
1704 return true;
1705 }
1706
1707 // Import the name of this declaration.
1708 Name = Importer.Import(D->getDeclName());
1709 if (D->getDeclName() && !Name)
1710 return true;
1711
1712 // Import the location of this declaration.
1713 Loc = Importer.Import(D->getLocation());
1714 return false;
1715}
1716
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001717void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1718 if (!FromD)
1719 return;
1720
1721 if (!ToD) {
1722 ToD = Importer.Import(FromD);
1723 if (!ToD)
1724 return;
1725 }
1726
1727 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1728 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1729 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1730 ImportDefinition(FromRecord, ToRecord);
1731 }
1732 }
1733 return;
1734 }
1735
1736 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1737 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1738 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1739 ImportDefinition(FromEnum, ToEnum);
1740 }
1741 }
1742 return;
1743 }
1744}
1745
Abramo Bagnara25777432010-08-11 22:01:17 +00001746void
1747ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1748 DeclarationNameInfo& To) {
1749 // NOTE: To.Name and To.Loc are already imported.
1750 // We only have to import To.LocInfo.
1751 switch (To.getName().getNameKind()) {
1752 case DeclarationName::Identifier:
1753 case DeclarationName::ObjCZeroArgSelector:
1754 case DeclarationName::ObjCOneArgSelector:
1755 case DeclarationName::ObjCMultiArgSelector:
1756 case DeclarationName::CXXUsingDirective:
1757 return;
1758
1759 case DeclarationName::CXXOperatorName: {
1760 SourceRange Range = From.getCXXOperatorNameRange();
1761 To.setCXXOperatorNameRange(Importer.Import(Range));
1762 return;
1763 }
1764 case DeclarationName::CXXLiteralOperatorName: {
1765 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1766 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1767 return;
1768 }
1769 case DeclarationName::CXXConstructorName:
1770 case DeclarationName::CXXDestructorName:
1771 case DeclarationName::CXXConversionFunctionName: {
1772 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1773 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1774 return;
1775 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001776 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001777 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001778}
1779
Douglas Gregord8868a62011-01-18 03:11:38 +00001780void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1781 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001782 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001783 return;
1784 }
1785
Douglas Gregor083a8212010-02-21 18:24:45 +00001786 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1787 FromEnd = FromDC->decls_end();
1788 From != FromEnd;
1789 ++From)
1790 Importer.Import(*From);
1791}
1792
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001793bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1794 bool ForceImport) {
1795 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001796 return false;
1797
1798 To->startDefinition();
1799
1800 // Add base classes.
1801 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1802 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001803
1804 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1805 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1806 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1807 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1808 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1809 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1810 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1811 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1812 ToData.Aggregate = FromData.Aggregate;
1813 ToData.PlainOldData = FromData.PlainOldData;
1814 ToData.Empty = FromData.Empty;
1815 ToData.Polymorphic = FromData.Polymorphic;
1816 ToData.Abstract = FromData.Abstract;
1817 ToData.IsStandardLayout = FromData.IsStandardLayout;
1818 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1819 ToData.HasPrivateFields = FromData.HasPrivateFields;
1820 ToData.HasProtectedFields = FromData.HasProtectedFields;
1821 ToData.HasPublicFields = FromData.HasPublicFields;
1822 ToData.HasMutableFields = FromData.HasMutableFields;
1823 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1824 ToData.HasConstexprNonCopyMoveConstructor
1825 = FromData.HasConstexprNonCopyMoveConstructor;
1826 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1827 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1828 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1829 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1830 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1831 ToData.HasNonLiteralTypeFieldsOrBases
1832 = FromData.HasNonLiteralTypeFieldsOrBases;
1833 ToData.UserProvidedDefaultConstructor
1834 = FromData.UserProvidedDefaultConstructor;
1835 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1836 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1837 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1838 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1839 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1840 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1841 ToData.FailedImplicitMoveConstructor
1842 = FromData.FailedImplicitMoveConstructor;
1843 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001844
Chris Lattner5f9e2722011-07-23 10:55:15 +00001845 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001846 for (CXXRecordDecl::base_class_iterator
1847 Base1 = FromCXX->bases_begin(),
1848 FromBaseEnd = FromCXX->bases_end();
1849 Base1 != FromBaseEnd;
1850 ++Base1) {
1851 QualType T = Importer.Import(Base1->getType());
1852 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001853 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001854
1855 SourceLocation EllipsisLoc;
1856 if (Base1->isPackExpansion())
1857 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001858
1859 // Ensure that we have a definition for the base.
1860 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1861
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001862 Bases.push_back(
1863 new (Importer.getToContext())
1864 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1865 Base1->isVirtual(),
1866 Base1->isBaseOfClass(),
1867 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001868 Importer.Import(Base1->getTypeSourceInfo()),
1869 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001870 }
1871 if (!Bases.empty())
1872 ToCXX->setBases(Bases.data(), Bases.size());
1873 }
1874
Sean Callanan673e7752011-07-19 22:38:25 +00001875 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001876 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001877 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001878}
1879
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001880bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1881 bool ForceImport) {
1882 if (To->getDefinition() || To->isBeingDefined())
1883 return false;
1884
1885 To->startDefinition();
1886
1887 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1888 if (T.isNull())
1889 return true;
1890
1891 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1892 if (ToPromotionType.isNull())
1893 return true;
1894
1895 ImportDeclContext(From, ForceImport);
1896
1897 // FIXME: we might need to merge the number of positive or negative bits
1898 // if the enumerator lists don't match.
1899 To->completeDefinition(T, ToPromotionType,
1900 From->getNumPositiveBits(),
1901 From->getNumNegativeBits());
1902 return false;
1903}
1904
Douglas Gregor040afae2010-11-30 19:14:50 +00001905TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1906 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001907 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001908 ToParams.reserve(Params->size());
1909 for (TemplateParameterList::iterator P = Params->begin(),
1910 PEnd = Params->end();
1911 P != PEnd; ++P) {
1912 Decl *To = Importer.Import(*P);
1913 if (!To)
1914 return 0;
1915
1916 ToParams.push_back(cast<NamedDecl>(To));
1917 }
1918
1919 return TemplateParameterList::Create(Importer.getToContext(),
1920 Importer.Import(Params->getTemplateLoc()),
1921 Importer.Import(Params->getLAngleLoc()),
1922 ToParams.data(), ToParams.size(),
1923 Importer.Import(Params->getRAngleLoc()));
1924}
1925
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001926TemplateArgument
1927ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1928 switch (From.getKind()) {
1929 case TemplateArgument::Null:
1930 return TemplateArgument();
1931
1932 case TemplateArgument::Type: {
1933 QualType ToType = Importer.Import(From.getAsType());
1934 if (ToType.isNull())
1935 return TemplateArgument();
1936 return TemplateArgument(ToType);
1937 }
1938
1939 case TemplateArgument::Integral: {
1940 QualType ToType = Importer.Import(From.getIntegralType());
1941 if (ToType.isNull())
1942 return TemplateArgument();
1943 return TemplateArgument(*From.getAsIntegral(), ToType);
1944 }
1945
1946 case TemplateArgument::Declaration:
1947 if (Decl *To = Importer.Import(From.getAsDecl()))
1948 return TemplateArgument(To);
1949 return TemplateArgument();
1950
1951 case TemplateArgument::Template: {
1952 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1953 if (ToTemplate.isNull())
1954 return TemplateArgument();
1955
1956 return TemplateArgument(ToTemplate);
1957 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001958
1959 case TemplateArgument::TemplateExpansion: {
1960 TemplateName ToTemplate
1961 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1962 if (ToTemplate.isNull())
1963 return TemplateArgument();
1964
Douglas Gregor2be29f42011-01-14 23:41:42 +00001965 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001966 }
1967
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001968 case TemplateArgument::Expression:
1969 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1970 return TemplateArgument(ToExpr);
1971 return TemplateArgument();
1972
1973 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001974 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001975 ToPack.reserve(From.pack_size());
1976 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1977 return TemplateArgument();
1978
1979 TemplateArgument *ToArgs
1980 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1981 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1982 return TemplateArgument(ToArgs, ToPack.size());
1983 }
1984 }
1985
1986 llvm_unreachable("Invalid template argument kind");
1987 return TemplateArgument();
1988}
1989
1990bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1991 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001992 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001993 for (unsigned I = 0; I != NumFromArgs; ++I) {
1994 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1995 if (To.isNull() && !FromArgs[I].isNull())
1996 return true;
1997
1998 ToArgs.push_back(To);
1999 }
2000
2001 return false;
2002}
2003
Douglas Gregor96a01b42010-02-11 00:48:18 +00002004bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002005 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002006 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002007 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002008 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002009 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002010}
2011
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002012bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002013 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002014 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002015 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002016 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002017}
2018
Douglas Gregor040afae2010-11-30 19:14:50 +00002019bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2020 ClassTemplateDecl *To) {
2021 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2022 Importer.getToContext(),
2023 Importer.getNonEquivalentDecls());
2024 return Ctx.IsStructurallyEquivalent(From, To);
2025}
2026
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002027Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002028 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002029 << D->getDeclKindName();
2030 return 0;
2031}
2032
Sean Callananf1b69462011-11-17 23:20:56 +00002033Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2034 TranslationUnitDecl *ToD =
2035 Importer.getToContext().getTranslationUnitDecl();
2036
2037 Importer.Imported(D, ToD);
2038
2039 return ToD;
2040}
2041
Douglas Gregor788c62d2010-02-21 18:26:36 +00002042Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2043 // Import the major distinguishing characteristics of this namespace.
2044 DeclContext *DC, *LexicalDC;
2045 DeclarationName Name;
2046 SourceLocation Loc;
2047 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2048 return 0;
2049
2050 NamespaceDecl *MergeWithNamespace = 0;
2051 if (!Name) {
2052 // This is an anonymous namespace. Adopt an existing anonymous
2053 // namespace if we can.
2054 // FIXME: Not testable.
2055 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2056 MergeWithNamespace = TU->getAnonymousNamespace();
2057 else
2058 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2059 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002060 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002061 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2062 DC->localUncachedLookup(Name, FoundDecls);
2063 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2064 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002065 continue;
2066
Douglas Gregorb75a3452011-10-15 00:10:27 +00002067 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002068 MergeWithNamespace = FoundNS;
2069 ConflictingDecls.clear();
2070 break;
2071 }
2072
Douglas Gregorb75a3452011-10-15 00:10:27 +00002073 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002074 }
2075
2076 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002077 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002078 ConflictingDecls.data(),
2079 ConflictingDecls.size());
2080 }
2081 }
2082
2083 // Create the "to" namespace, if needed.
2084 NamespaceDecl *ToNamespace = MergeWithNamespace;
2085 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002086 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2087 Importer.Import(D->getLocStart()),
2088 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002089 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002090 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002091
2092 // If this is an anonymous namespace, register it as the anonymous
2093 // namespace within its context.
2094 if (!Name) {
2095 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2096 TU->setAnonymousNamespace(ToNamespace);
2097 else
2098 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2099 }
2100 }
2101 Importer.Imported(D, ToNamespace);
2102
2103 ImportDeclContext(D);
2104
2105 return ToNamespace;
2106}
2107
Richard Smith162e1c12011-04-15 14:24:37 +00002108Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002109 // Import the major distinguishing characteristics of this typedef.
2110 DeclContext *DC, *LexicalDC;
2111 DeclarationName Name;
2112 SourceLocation Loc;
2113 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2114 return 0;
2115
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002116 // If this typedef is not in block scope, determine whether we've
2117 // seen a typedef with the same name (that we can merge with) or any
2118 // other entity by that name (which name lookup could conflict with).
2119 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002120 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002121 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002122 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2123 DC->localUncachedLookup(Name, FoundDecls);
2124 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2125 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002126 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002127 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002128 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002129 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2130 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002131 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002132 }
2133
Douglas Gregorb75a3452011-10-15 00:10:27 +00002134 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002135 }
2136
2137 if (!ConflictingDecls.empty()) {
2138 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2139 ConflictingDecls.data(),
2140 ConflictingDecls.size());
2141 if (!Name)
2142 return 0;
2143 }
2144 }
2145
Douglas Gregorea35d112010-02-15 23:54:17 +00002146 // Import the underlying type of this typedef;
2147 QualType T = Importer.Import(D->getUnderlyingType());
2148 if (T.isNull())
2149 return 0;
2150
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002151 // Create the new typedef node.
2152 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002153 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002154 TypedefNameDecl *ToTypedef;
2155 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002156 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2157 StartL, Loc,
2158 Name.getAsIdentifierInfo(),
2159 TInfo);
2160 else
Richard Smith162e1c12011-04-15 14:24:37 +00002161 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2162 StartL, Loc,
2163 Name.getAsIdentifierInfo(),
2164 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002165
Douglas Gregor325bf172010-02-22 17:42:47 +00002166 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002167 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002168 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002169 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002170
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002171 return ToTypedef;
2172}
2173
Richard Smith162e1c12011-04-15 14:24:37 +00002174Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2175 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2176}
2177
2178Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2179 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2180}
2181
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002182Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2183 // Import the major distinguishing characteristics of this enum.
2184 DeclContext *DC, *LexicalDC;
2185 DeclarationName Name;
2186 SourceLocation Loc;
2187 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2188 return 0;
2189
2190 // Figure out what enum name we're looking for.
2191 unsigned IDNS = Decl::IDNS_Tag;
2192 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002193 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2194 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002195 IDNS = Decl::IDNS_Ordinary;
2196 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2197 IDNS |= Decl::IDNS_Ordinary;
2198
2199 // We may already have an enum of the same name; try to find and match it.
2200 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002201 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002202 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2203 DC->localUncachedLookup(SearchName, FoundDecls);
2204 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2205 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002206 continue;
2207
Douglas Gregorb75a3452011-10-15 00:10:27 +00002208 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002209 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002210 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2211 Found = Tag->getDecl();
2212 }
2213
2214 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002215 if (IsStructuralMatch(D, FoundEnum))
2216 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002217 }
2218
Douglas Gregorb75a3452011-10-15 00:10:27 +00002219 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002220 }
2221
2222 if (!ConflictingDecls.empty()) {
2223 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2224 ConflictingDecls.data(),
2225 ConflictingDecls.size());
2226 }
2227 }
2228
2229 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002230 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2231 Importer.Import(D->getLocStart()),
2232 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002233 D->isScoped(), D->isScopedUsingClassTag(),
2234 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002235 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002236 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002237 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002238 D2->setLexicalDeclContext(LexicalDC);
2239 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002240 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002241
2242 // Import the integer type.
2243 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2244 if (ToIntegerType.isNull())
2245 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002246 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002247
2248 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002249 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002250 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002251
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002252 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002253}
2254
Douglas Gregor96a01b42010-02-11 00:48:18 +00002255Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2256 // If this record has a definition in the translation unit we're coming from,
2257 // but this particular declaration is not that definition, import the
2258 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002259 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002260 if (Definition && Definition != D) {
2261 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002262 if (!ImportedDef)
2263 return 0;
2264
2265 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002266 }
2267
2268 // Import the major distinguishing characteristics of this record.
2269 DeclContext *DC, *LexicalDC;
2270 DeclarationName Name;
2271 SourceLocation Loc;
2272 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2273 return 0;
2274
2275 // Figure out what structure name we're looking for.
2276 unsigned IDNS = Decl::IDNS_Tag;
2277 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002278 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2279 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002280 IDNS = Decl::IDNS_Ordinary;
2281 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2282 IDNS |= Decl::IDNS_Ordinary;
2283
2284 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002285 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002286 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002287 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002288 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2289 DC->localUncachedLookup(SearchName, FoundDecls);
2290 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2291 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002292 continue;
2293
Douglas Gregorb75a3452011-10-15 00:10:27 +00002294 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002295 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002296 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2297 Found = Tag->getDecl();
2298 }
2299
2300 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002301 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002302 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002303 // The record types structurally match, or the "from" translation
2304 // unit only had a forward declaration anyway; call it the same
2305 // function.
2306 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002307 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002308 }
2309 } else {
2310 // We have a forward declaration of this type, so adopt that forward
2311 // declaration rather than building a new one.
2312 AdoptDecl = FoundRecord;
2313 continue;
2314 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002315 }
2316
Douglas Gregorb75a3452011-10-15 00:10:27 +00002317 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002318 }
2319
2320 if (!ConflictingDecls.empty()) {
2321 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2322 ConflictingDecls.data(),
2323 ConflictingDecls.size());
2324 }
2325 }
2326
2327 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002328 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002329 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002330 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002331 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002332 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002333 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002334 DC, StartLoc, Loc,
2335 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002336 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002337 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002338 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002339 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002340 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002341 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002342
2343 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002344 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002345 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002346 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002347
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002348 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002349
John McCall5e1cdac2011-10-07 06:10:15 +00002350 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002351 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002352
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002353 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002354}
2355
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002356Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2357 // Import the major distinguishing characteristics of this enumerator.
2358 DeclContext *DC, *LexicalDC;
2359 DeclarationName Name;
2360 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002361 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002362 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002363
2364 QualType T = Importer.Import(D->getType());
2365 if (T.isNull())
2366 return 0;
2367
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002368 // Determine whether there are any other declarations with the same name and
2369 // in the same context.
2370 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002371 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002372 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002373 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2374 DC->localUncachedLookup(Name, FoundDecls);
2375 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2376 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002377 continue;
2378
Douglas Gregorb75a3452011-10-15 00:10:27 +00002379 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002380 }
2381
2382 if (!ConflictingDecls.empty()) {
2383 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2384 ConflictingDecls.data(),
2385 ConflictingDecls.size());
2386 if (!Name)
2387 return 0;
2388 }
2389 }
2390
2391 Expr *Init = Importer.Import(D->getInitExpr());
2392 if (D->getInitExpr() && !Init)
2393 return 0;
2394
2395 EnumConstantDecl *ToEnumerator
2396 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2397 Name.getAsIdentifierInfo(), T,
2398 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002399 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002400 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002401 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002402 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002403 return ToEnumerator;
2404}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002405
Douglas Gregora404ea62010-02-10 19:54:31 +00002406Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2407 // Import the major distinguishing characteristics of this function.
2408 DeclContext *DC, *LexicalDC;
2409 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002410 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002411 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002412 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002413
Douglas Gregora404ea62010-02-10 19:54:31 +00002414 // Try to find a function in our own ("to") context with the same name, same
2415 // type, and in the same context as the function we're importing.
2416 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002417 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002418 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002419 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2420 DC->localUncachedLookup(Name, FoundDecls);
2421 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2422 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002423 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002424
Douglas Gregorb75a3452011-10-15 00:10:27 +00002425 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002426 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2427 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002428 if (Importer.IsStructurallyEquivalent(D->getType(),
2429 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002430 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002431 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002432 }
2433
2434 // FIXME: Check for overloading more carefully, e.g., by boosting
2435 // Sema::IsOverload out to the AST library.
2436
2437 // Function overloading is okay in C++.
2438 if (Importer.getToContext().getLangOptions().CPlusPlus)
2439 continue;
2440
2441 // Complain about inconsistent function types.
2442 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002443 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002444 Importer.ToDiag(FoundFunction->getLocation(),
2445 diag::note_odr_value_here)
2446 << FoundFunction->getType();
2447 }
2448 }
2449
Douglas Gregorb75a3452011-10-15 00:10:27 +00002450 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002451 }
2452
2453 if (!ConflictingDecls.empty()) {
2454 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2455 ConflictingDecls.data(),
2456 ConflictingDecls.size());
2457 if (!Name)
2458 return 0;
2459 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002460 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002461
Abramo Bagnara25777432010-08-11 22:01:17 +00002462 DeclarationNameInfo NameInfo(Name, Loc);
2463 // Import additional name location/type info.
2464 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2465
Douglas Gregorea35d112010-02-15 23:54:17 +00002466 // Import the type.
2467 QualType T = Importer.Import(D->getType());
2468 if (T.isNull())
2469 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002470
2471 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002472 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002473 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2474 P != PEnd; ++P) {
2475 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2476 if (!ToP)
2477 return 0;
2478
2479 Parameters.push_back(ToP);
2480 }
2481
2482 // Create the imported function.
2483 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002484 FunctionDecl *ToFunction = 0;
2485 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2486 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2487 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002488 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002489 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002490 FromConstructor->isExplicit(),
2491 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002492 D->isImplicit(),
2493 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002494 } else if (isa<CXXDestructorDecl>(D)) {
2495 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2496 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002497 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002498 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002499 D->isInlineSpecified(),
2500 D->isImplicit());
2501 } else if (CXXConversionDecl *FromConversion
2502 = dyn_cast<CXXConversionDecl>(D)) {
2503 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2504 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002505 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002506 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002507 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002508 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002509 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002510 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002511 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2512 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2513 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002514 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002515 NameInfo, T, TInfo,
2516 Method->isStatic(),
2517 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002518 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002519 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002520 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002521 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002522 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002523 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002524 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002525 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002526 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002527 D->hasWrittenPrototype(),
2528 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002529 }
John McCallb6217662010-03-15 10:12:16 +00002530
2531 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002532 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002533 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002534 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002535 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2536 ToFunction->setTrivial(D->isTrivial());
2537 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002538 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002539
Douglas Gregora404ea62010-02-10 19:54:31 +00002540 // Set the parameters.
2541 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002542 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002543 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002544 }
David Blaikie4278c652011-09-21 18:16:56 +00002545 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002546
2547 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002548
2549 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002550 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002551
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002552 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002553}
2554
Douglas Gregorc144f352010-02-21 18:29:16 +00002555Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2556 return VisitFunctionDecl(D);
2557}
2558
2559Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2560 return VisitCXXMethodDecl(D);
2561}
2562
2563Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2564 return VisitCXXMethodDecl(D);
2565}
2566
2567Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2568 return VisitCXXMethodDecl(D);
2569}
2570
Douglas Gregor96a01b42010-02-11 00:48:18 +00002571Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2572 // Import the major distinguishing characteristics of a variable.
2573 DeclContext *DC, *LexicalDC;
2574 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002575 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002576 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2577 return 0;
2578
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002579 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002580 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2581 DC->localUncachedLookup(Name, FoundDecls);
2582 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2583 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002584 if (Importer.IsStructurallyEquivalent(D->getType(),
2585 FoundField->getType())) {
2586 Importer.Imported(D, FoundField);
2587 return FoundField;
2588 }
2589
2590 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2591 << Name << D->getType() << FoundField->getType();
2592 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2593 << FoundField->getType();
2594 return 0;
2595 }
2596 }
2597
Douglas Gregorea35d112010-02-15 23:54:17 +00002598 // Import the type.
2599 QualType T = Importer.Import(D->getType());
2600 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002601 return 0;
2602
2603 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2604 Expr *BitWidth = Importer.Import(D->getBitWidth());
2605 if (!BitWidth && D->getBitWidth())
2606 return 0;
2607
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002608 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2609 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002610 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002611 T, TInfo, BitWidth, D->isMutable(),
2612 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002613 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002614 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002615 if (ToField->hasInClassInitializer())
2616 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002617 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002618 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002619 return ToField;
2620}
2621
Francois Pichet87c2e122010-11-21 06:08:52 +00002622Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2623 // Import the major distinguishing characteristics of a variable.
2624 DeclContext *DC, *LexicalDC;
2625 DeclarationName Name;
2626 SourceLocation Loc;
2627 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2628 return 0;
2629
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002630 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002631 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2632 DC->localUncachedLookup(Name, FoundDecls);
2633 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002634 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002635 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002636 if (Importer.IsStructurallyEquivalent(D->getType(),
2637 FoundField->getType())) {
2638 Importer.Imported(D, FoundField);
2639 return FoundField;
2640 }
2641
2642 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2643 << Name << D->getType() << FoundField->getType();
2644 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2645 << FoundField->getType();
2646 return 0;
2647 }
2648 }
2649
Francois Pichet87c2e122010-11-21 06:08:52 +00002650 // Import the type.
2651 QualType T = Importer.Import(D->getType());
2652 if (T.isNull())
2653 return 0;
2654
2655 NamedDecl **NamedChain =
2656 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2657
2658 unsigned i = 0;
2659 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2660 PE = D->chain_end(); PI != PE; ++PI) {
2661 Decl* D = Importer.Import(*PI);
2662 if (!D)
2663 return 0;
2664 NamedChain[i++] = cast<NamedDecl>(D);
2665 }
2666
2667 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2668 Importer.getToContext(), DC,
2669 Loc, Name.getAsIdentifierInfo(), T,
2670 NamedChain, D->getChainingSize());
2671 ToIndirectField->setAccess(D->getAccess());
2672 ToIndirectField->setLexicalDeclContext(LexicalDC);
2673 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002674 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002675 return ToIndirectField;
2676}
2677
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002678Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2679 // Import the major distinguishing characteristics of an ivar.
2680 DeclContext *DC, *LexicalDC;
2681 DeclarationName Name;
2682 SourceLocation Loc;
2683 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2684 return 0;
2685
2686 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002687 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2688 DC->localUncachedLookup(Name, FoundDecls);
2689 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2690 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002691 if (Importer.IsStructurallyEquivalent(D->getType(),
2692 FoundIvar->getType())) {
2693 Importer.Imported(D, FoundIvar);
2694 return FoundIvar;
2695 }
2696
2697 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2698 << Name << D->getType() << FoundIvar->getType();
2699 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2700 << FoundIvar->getType();
2701 return 0;
2702 }
2703 }
2704
2705 // Import the type.
2706 QualType T = Importer.Import(D->getType());
2707 if (T.isNull())
2708 return 0;
2709
2710 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2711 Expr *BitWidth = Importer.Import(D->getBitWidth());
2712 if (!BitWidth && D->getBitWidth())
2713 return 0;
2714
Daniel Dunbara0654922010-04-02 20:10:03 +00002715 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2716 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002717 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002718 Loc, Name.getAsIdentifierInfo(),
2719 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002720 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002721 ToIvar->setLexicalDeclContext(LexicalDC);
2722 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002723 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002724 return ToIvar;
2725
2726}
2727
Douglas Gregora404ea62010-02-10 19:54:31 +00002728Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2729 // Import the major distinguishing characteristics of a variable.
2730 DeclContext *DC, *LexicalDC;
2731 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002732 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002733 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002734 return 0;
2735
Douglas Gregor089459a2010-02-08 21:09:39 +00002736 // Try to find a variable in our own ("to") context with the same name and
2737 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002738 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002739 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002740 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002741 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002742 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2743 DC->localUncachedLookup(Name, FoundDecls);
2744 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2745 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002746 continue;
2747
Douglas Gregorb75a3452011-10-15 00:10:27 +00002748 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002749 // We have found a variable that we may need to merge with. Check it.
2750 if (isExternalLinkage(FoundVar->getLinkage()) &&
2751 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002752 if (Importer.IsStructurallyEquivalent(D->getType(),
2753 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002754 MergeWithVar = FoundVar;
2755 break;
2756 }
2757
Douglas Gregord0145422010-02-12 17:23:39 +00002758 const ArrayType *FoundArray
2759 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2760 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002761 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002762 if (FoundArray && TArray) {
2763 if (isa<IncompleteArrayType>(FoundArray) &&
2764 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002765 // Import the type.
2766 QualType T = Importer.Import(D->getType());
2767 if (T.isNull())
2768 return 0;
2769
Douglas Gregord0145422010-02-12 17:23:39 +00002770 FoundVar->setType(T);
2771 MergeWithVar = FoundVar;
2772 break;
2773 } else if (isa<IncompleteArrayType>(TArray) &&
2774 isa<ConstantArrayType>(FoundArray)) {
2775 MergeWithVar = FoundVar;
2776 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002777 }
2778 }
2779
Douglas Gregor089459a2010-02-08 21:09:39 +00002780 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002781 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002782 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2783 << FoundVar->getType();
2784 }
2785 }
2786
Douglas Gregorb75a3452011-10-15 00:10:27 +00002787 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002788 }
2789
2790 if (MergeWithVar) {
2791 // An equivalent variable with external linkage has been found. Link
2792 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002793 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002794
2795 if (VarDecl *DDef = D->getDefinition()) {
2796 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2797 Importer.ToDiag(ExistingDef->getLocation(),
2798 diag::err_odr_variable_multiple_def)
2799 << Name;
2800 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2801 } else {
2802 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002803 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002804 if (DDef->isInitKnownICE()) {
2805 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2806 Eval->CheckedICE = true;
2807 Eval->IsICE = DDef->isInitICE();
2808 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002809 }
2810 }
2811
2812 return MergeWithVar;
2813 }
2814
2815 if (!ConflictingDecls.empty()) {
2816 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2817 ConflictingDecls.data(),
2818 ConflictingDecls.size());
2819 if (!Name)
2820 return 0;
2821 }
2822 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002823
Douglas Gregorea35d112010-02-15 23:54:17 +00002824 // Import the type.
2825 QualType T = Importer.Import(D->getType());
2826 if (T.isNull())
2827 return 0;
2828
Douglas Gregor089459a2010-02-08 21:09:39 +00002829 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002830 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002831 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2832 Importer.Import(D->getInnerLocStart()),
2833 Loc, Name.getAsIdentifierInfo(),
2834 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002835 D->getStorageClass(),
2836 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002837 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002838 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002839 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002840 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002841 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002842
Douglas Gregor089459a2010-02-08 21:09:39 +00002843 // Merge the initializer.
2844 // FIXME: Can we really import any initializer? Alternatively, we could force
2845 // ourselves to import every declaration of a variable and then only use
2846 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002847 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002848
2849 // FIXME: Other bits to merge?
2850
2851 return ToVar;
2852}
2853
Douglas Gregor2cd00932010-02-17 21:22:52 +00002854Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2855 // Parameters are created in the translation unit's context, then moved
2856 // into the function declaration's context afterward.
2857 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2858
2859 // Import the name of this declaration.
2860 DeclarationName Name = Importer.Import(D->getDeclName());
2861 if (D->getDeclName() && !Name)
2862 return 0;
2863
2864 // Import the location of this declaration.
2865 SourceLocation Loc = Importer.Import(D->getLocation());
2866
2867 // Import the parameter's type.
2868 QualType T = Importer.Import(D->getType());
2869 if (T.isNull())
2870 return 0;
2871
2872 // Create the imported parameter.
2873 ImplicitParamDecl *ToParm
2874 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2875 Loc, Name.getAsIdentifierInfo(),
2876 T);
2877 return Importer.Imported(D, ToParm);
2878}
2879
Douglas Gregora404ea62010-02-10 19:54:31 +00002880Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2881 // Parameters are created in the translation unit's context, then moved
2882 // into the function declaration's context afterward.
2883 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2884
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002885 // Import the name of this declaration.
2886 DeclarationName Name = Importer.Import(D->getDeclName());
2887 if (D->getDeclName() && !Name)
2888 return 0;
2889
Douglas Gregora404ea62010-02-10 19:54:31 +00002890 // Import the location of this declaration.
2891 SourceLocation Loc = Importer.Import(D->getLocation());
2892
2893 // Import the parameter's type.
2894 QualType T = Importer.Import(D->getType());
2895 if (T.isNull())
2896 return 0;
2897
2898 // Create the imported parameter.
2899 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2900 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002901 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002902 Loc, Name.getAsIdentifierInfo(),
2903 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002904 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002905 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002906 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002907 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002908}
2909
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002910Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2911 // Import the major distinguishing characteristics of a method.
2912 DeclContext *DC, *LexicalDC;
2913 DeclarationName Name;
2914 SourceLocation Loc;
2915 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2916 return 0;
2917
Douglas Gregorb75a3452011-10-15 00:10:27 +00002918 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2919 DC->localUncachedLookup(Name, FoundDecls);
2920 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2921 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002922 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2923 continue;
2924
2925 // Check return types.
2926 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2927 FoundMethod->getResultType())) {
2928 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2929 << D->isInstanceMethod() << Name
2930 << D->getResultType() << FoundMethod->getResultType();
2931 Importer.ToDiag(FoundMethod->getLocation(),
2932 diag::note_odr_objc_method_here)
2933 << D->isInstanceMethod() << Name;
2934 return 0;
2935 }
2936
2937 // Check the number of parameters.
2938 if (D->param_size() != FoundMethod->param_size()) {
2939 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2940 << D->isInstanceMethod() << Name
2941 << D->param_size() << FoundMethod->param_size();
2942 Importer.ToDiag(FoundMethod->getLocation(),
2943 diag::note_odr_objc_method_here)
2944 << D->isInstanceMethod() << Name;
2945 return 0;
2946 }
2947
2948 // Check parameter types.
2949 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2950 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2951 P != PEnd; ++P, ++FoundP) {
2952 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2953 (*FoundP)->getType())) {
2954 Importer.FromDiag((*P)->getLocation(),
2955 diag::err_odr_objc_method_param_type_inconsistent)
2956 << D->isInstanceMethod() << Name
2957 << (*P)->getType() << (*FoundP)->getType();
2958 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2959 << (*FoundP)->getType();
2960 return 0;
2961 }
2962 }
2963
2964 // Check variadic/non-variadic.
2965 // Check the number of parameters.
2966 if (D->isVariadic() != FoundMethod->isVariadic()) {
2967 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2968 << D->isInstanceMethod() << Name;
2969 Importer.ToDiag(FoundMethod->getLocation(),
2970 diag::note_odr_objc_method_here)
2971 << D->isInstanceMethod() << Name;
2972 return 0;
2973 }
2974
2975 // FIXME: Any other bits we need to merge?
2976 return Importer.Imported(D, FoundMethod);
2977 }
2978 }
2979
2980 // Import the result type.
2981 QualType ResultTy = Importer.Import(D->getResultType());
2982 if (ResultTy.isNull())
2983 return 0;
2984
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002985 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2986
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002987 ObjCMethodDecl *ToMethod
2988 = ObjCMethodDecl::Create(Importer.getToContext(),
2989 Loc,
2990 Importer.Import(D->getLocEnd()),
2991 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002992 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002993 D->isInstanceMethod(),
2994 D->isVariadic(),
2995 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002996 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002997 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002998 D->getImplementationControl(),
2999 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003000
3001 // FIXME: When we decide to merge method definitions, we'll need to
3002 // deal with implicit parameters.
3003
3004 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003005 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003006 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3007 FromPEnd = D->param_end();
3008 FromP != FromPEnd;
3009 ++FromP) {
3010 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3011 if (!ToP)
3012 return 0;
3013
3014 ToParams.push_back(ToP);
3015 }
3016
3017 // Set the parameters.
3018 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3019 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003020 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003021 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003022 SmallVector<SourceLocation, 12> SelLocs;
3023 D->getSelectorLocs(SelLocs);
3024 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003025
3026 ToMethod->setLexicalDeclContext(LexicalDC);
3027 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003028 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003029 return ToMethod;
3030}
3031
Douglas Gregorb4677b62010-02-18 01:47:50 +00003032Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3033 // Import the major distinguishing characteristics of a category.
3034 DeclContext *DC, *LexicalDC;
3035 DeclarationName Name;
3036 SourceLocation Loc;
3037 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3038 return 0;
3039
3040 ObjCInterfaceDecl *ToInterface
3041 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3042 if (!ToInterface)
3043 return 0;
3044
3045 // Determine if we've already encountered this category.
3046 ObjCCategoryDecl *MergeWithCategory
3047 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3048 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3049 if (!ToCategory) {
3050 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003051 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003052 Loc,
3053 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003054 Name.getAsIdentifierInfo(),
3055 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003056 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003057 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003058 Importer.Imported(D, ToCategory);
3059
Douglas Gregorb4677b62010-02-18 01:47:50 +00003060 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003061 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3062 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003063 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3064 = D->protocol_loc_begin();
3065 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3066 FromProtoEnd = D->protocol_end();
3067 FromProto != FromProtoEnd;
3068 ++FromProto, ++FromProtoLoc) {
3069 ObjCProtocolDecl *ToProto
3070 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3071 if (!ToProto)
3072 return 0;
3073 Protocols.push_back(ToProto);
3074 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3075 }
3076
3077 // FIXME: If we're merging, make sure that the protocol list is the same.
3078 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3079 ProtocolLocs.data(), Importer.getToContext());
3080
3081 } else {
3082 Importer.Imported(D, ToCategory);
3083 }
3084
3085 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003086 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003087
3088 // If we have an implementation, import it as well.
3089 if (D->getImplementation()) {
3090 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003091 = cast_or_null<ObjCCategoryImplDecl>(
3092 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003093 if (!Impl)
3094 return 0;
3095
3096 ToCategory->setImplementation(Impl);
3097 }
3098
3099 return ToCategory;
3100}
3101
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003102Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003103 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003104 DeclContext *DC, *LexicalDC;
3105 DeclarationName Name;
3106 SourceLocation Loc;
3107 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3108 return 0;
3109
3110 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003111 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3112 DC->localUncachedLookup(Name, FoundDecls);
3113 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3114 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003115 continue;
3116
Douglas Gregorb75a3452011-10-15 00:10:27 +00003117 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003118 break;
3119 }
3120
3121 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003122 if (!ToProto || !ToProto->hasDefinition()) {
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003123 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003124 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3125 Name.getAsIdentifierInfo(), Loc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003126 Importer.Import(D->getAtStartLoc()),
Douglas Gregor27c6da22012-01-01 20:30:41 +00003127 /*PrevDecl=*/0,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003128 D->isInitiallyForwardDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003129 ToProto->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003130 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003131 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003132 if (!ToProto->hasDefinition())
3133 ToProto->startDefinition();
3134
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003135 Importer.Imported(D, ToProto);
3136
3137 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003138 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3139 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003140 ObjCProtocolDecl::protocol_loc_iterator
3141 FromProtoLoc = D->protocol_loc_begin();
3142 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3143 FromProtoEnd = D->protocol_end();
3144 FromProto != FromProtoEnd;
3145 ++FromProto, ++FromProtoLoc) {
3146 ObjCProtocolDecl *ToProto
3147 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3148 if (!ToProto)
3149 return 0;
3150 Protocols.push_back(ToProto);
3151 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3152 }
3153
3154 // FIXME: If we're merging, make sure that the protocol list is the same.
3155 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3156 ProtocolLocs.data(), Importer.getToContext());
3157 } else {
3158 Importer.Imported(D, ToProto);
3159 }
3160
Douglas Gregorb4677b62010-02-18 01:47:50 +00003161 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003162 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003163
3164 return ToProto;
3165}
3166
Douglas Gregora12d2942010-02-16 01:20:57 +00003167Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3168 // Import the major distinguishing characteristics of an @interface.
3169 DeclContext *DC, *LexicalDC;
3170 DeclarationName Name;
3171 SourceLocation Loc;
3172 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3173 return 0;
3174
3175 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003176 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3177 DC->localUncachedLookup(Name, FoundDecls);
3178 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3179 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003180 continue;
3181
Douglas Gregorb75a3452011-10-15 00:10:27 +00003182 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003183 break;
3184 }
3185
3186 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003187 if (!ToIface || !ToIface->hasDefinition()) {
Douglas Gregora12d2942010-02-16 01:20:57 +00003188 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003189 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3190 Importer.Import(D->getAtStartLoc()),
Douglas Gregor0af55012011-12-16 03:12:41 +00003191 Name.getAsIdentifierInfo(),
3192 /*PrevDecl=*/0,Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003193 D->isImplicitInterfaceDecl());
3194 ToIface->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003195 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003196 }
3197 Importer.Imported(D, ToIface);
3198
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003199 if (D->hasDefinition()) {
3200 if (!ToIface->hasDefinition())
3201 ToIface->startDefinition();
Douglas Gregora12d2942010-02-16 01:20:57 +00003202
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003203 if (D->getSuperClass()) {
3204 ObjCInterfaceDecl *Super
3205 = cast_or_null<ObjCInterfaceDecl>(
3206 Importer.Import(D->getSuperClass()));
3207 if (!Super)
3208 return 0;
3209
3210 ToIface->setSuperClass(Super);
3211 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3212 }
3213
3214 // Import protocols
3215 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3216 SmallVector<SourceLocation, 4> ProtocolLocs;
3217 ObjCInterfaceDecl::protocol_loc_iterator
3218 FromProtoLoc = D->protocol_loc_begin();
3219
3220 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3221 FromProtoEnd = D->protocol_end();
3222 FromProto != FromProtoEnd;
3223 ++FromProto, ++FromProtoLoc) {
3224 ObjCProtocolDecl *ToProto
3225 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3226 if (!ToProto)
3227 return 0;
3228 Protocols.push_back(ToProto);
3229 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3230 }
3231
3232 // FIXME: If we're merging, make sure that the protocol list is the same.
3233 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3234 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregora12d2942010-02-16 01:20:57 +00003235 }
3236
Douglas Gregora12d2942010-02-16 01:20:57 +00003237 // Import @end range
3238 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3239 } else {
3240 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003241
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003242 if (D->hasDefinition()) {
3243 // Check for consistency of superclasses.
3244 DeclarationName FromSuperName, ToSuperName;
3245
3246 // If the superclass hasn't been imported yet, do so before checking.
3247 ObjCInterfaceDecl *DSuperClass = D->getSuperClass();
3248 ObjCInterfaceDecl *ToIfaceSuperClass = ToIface->getSuperClass();
3249
3250 if (DSuperClass && !ToIfaceSuperClass) {
3251 Decl *ImportedSuperClass = Importer.Import(DSuperClass);
3252 ObjCInterfaceDecl *ImportedSuperIface
3253 = cast<ObjCInterfaceDecl>(ImportedSuperClass);
3254
3255 ToIface->setSuperClass(ImportedSuperIface);
3256 }
Sean Callananb1ce7302011-11-11 17:39:52 +00003257
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003258 if (D->getSuperClass())
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003259 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3260 if (ToIface->getSuperClass())
3261 ToSuperName = ToIface->getSuperClass()->getDeclName();
3262 if (FromSuperName != ToSuperName) {
3263 Importer.ToDiag(ToIface->getLocation(),
3264 diag::err_odr_objc_superclass_inconsistent)
3265 << ToIface->getDeclName();
3266 if (ToIface->getSuperClass())
3267 Importer.ToDiag(ToIface->getSuperClassLoc(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003268 diag::note_odr_objc_superclass)
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003269 << ToIface->getSuperClass()->getDeclName();
3270 else
3271 Importer.ToDiag(ToIface->getLocation(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003272 diag::note_odr_objc_missing_superclass);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003273 if (D->getSuperClass())
3274 Importer.FromDiag(D->getSuperClassLoc(),
3275 diag::note_odr_objc_superclass)
3276 << D->getSuperClass()->getDeclName();
3277 else
3278 Importer.FromDiag(D->getLocation(),
3279 diag::note_odr_objc_missing_superclass);
3280 return 0;
3281 }
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003282 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003283 }
3284
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003285 if (!D->hasDefinition())
3286 return ToIface;
3287
Douglas Gregorb4677b62010-02-18 01:47:50 +00003288 // Import categories. When the categories themselves are imported, they'll
3289 // hook themselves into this interface.
3290 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3291 FromCat = FromCat->getNextClassCategory())
3292 Importer.Import(FromCat);
3293
Douglas Gregora12d2942010-02-16 01:20:57 +00003294 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003295 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003296
3297 // If we have an @implementation, import it as well.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003298 if ( D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003299 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3300 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003301 if (!Impl)
3302 return 0;
3303
3304 ToIface->setImplementation(Impl);
3305 }
3306
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003307 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003308}
3309
Douglas Gregor3daef292010-12-07 15:32:12 +00003310Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3311 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3312 Importer.Import(D->getCategoryDecl()));
3313 if (!Category)
3314 return 0;
3315
3316 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3317 if (!ToImpl) {
3318 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3319 if (!DC)
3320 return 0;
3321
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003322 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003323 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003324 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003325 Category->getClassInterface(),
3326 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003327 Importer.Import(D->getAtStartLoc()),
3328 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003329
3330 DeclContext *LexicalDC = DC;
3331 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3332 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3333 if (!LexicalDC)
3334 return 0;
3335
3336 ToImpl->setLexicalDeclContext(LexicalDC);
3337 }
3338
Sean Callanan9faf8102011-10-21 02:57:43 +00003339 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003340 Category->setImplementation(ToImpl);
3341 }
3342
3343 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003344 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003345 return ToImpl;
3346}
3347
Douglas Gregordd182ff2010-12-07 01:26:03 +00003348Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3349 // Find the corresponding interface.
3350 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3351 Importer.Import(D->getClassInterface()));
3352 if (!Iface)
3353 return 0;
3354
3355 // Import the superclass, if any.
3356 ObjCInterfaceDecl *Super = 0;
3357 if (D->getSuperClass()) {
3358 Super = cast_or_null<ObjCInterfaceDecl>(
3359 Importer.Import(D->getSuperClass()));
3360 if (!Super)
3361 return 0;
3362 }
3363
3364 ObjCImplementationDecl *Impl = Iface->getImplementation();
3365 if (!Impl) {
3366 // We haven't imported an implementation yet. Create a new @implementation
3367 // now.
3368 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3369 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003370 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003371 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003372 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003373
3374 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3375 DeclContext *LexicalDC
3376 = Importer.ImportContext(D->getLexicalDeclContext());
3377 if (!LexicalDC)
3378 return 0;
3379 Impl->setLexicalDeclContext(LexicalDC);
3380 }
3381
3382 // Associate the implementation with the class it implements.
3383 Iface->setImplementation(Impl);
3384 Importer.Imported(D, Iface->getImplementation());
3385 } else {
3386 Importer.Imported(D, Iface->getImplementation());
3387
3388 // Verify that the existing @implementation has the same superclass.
3389 if ((Super && !Impl->getSuperClass()) ||
3390 (!Super && Impl->getSuperClass()) ||
3391 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003392 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003393 Importer.ToDiag(Impl->getLocation(),
3394 diag::err_odr_objc_superclass_inconsistent)
3395 << Iface->getDeclName();
3396 // FIXME: It would be nice to have the location of the superclass
3397 // below.
3398 if (Impl->getSuperClass())
3399 Importer.ToDiag(Impl->getLocation(),
3400 diag::note_odr_objc_superclass)
3401 << Impl->getSuperClass()->getDeclName();
3402 else
3403 Importer.ToDiag(Impl->getLocation(),
3404 diag::note_odr_objc_missing_superclass);
3405 if (D->getSuperClass())
3406 Importer.FromDiag(D->getLocation(),
3407 diag::note_odr_objc_superclass)
3408 << D->getSuperClass()->getDeclName();
3409 else
3410 Importer.FromDiag(D->getLocation(),
3411 diag::note_odr_objc_missing_superclass);
3412 return 0;
3413 }
3414 }
3415
3416 // Import all of the members of this @implementation.
3417 ImportDeclContext(D);
3418
3419 return Impl;
3420}
3421
Douglas Gregore3261622010-02-17 18:02:10 +00003422Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3423 // Import the major distinguishing characteristics of an @property.
3424 DeclContext *DC, *LexicalDC;
3425 DeclarationName Name;
3426 SourceLocation Loc;
3427 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3428 return 0;
3429
3430 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003431 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3432 DC->localUncachedLookup(Name, FoundDecls);
3433 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003434 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003435 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003436 // Check property types.
3437 if (!Importer.IsStructurallyEquivalent(D->getType(),
3438 FoundProp->getType())) {
3439 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3440 << Name << D->getType() << FoundProp->getType();
3441 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3442 << FoundProp->getType();
3443 return 0;
3444 }
3445
3446 // FIXME: Check property attributes, getters, setters, etc.?
3447
3448 // Consider these properties to be equivalent.
3449 Importer.Imported(D, FoundProp);
3450 return FoundProp;
3451 }
3452 }
3453
3454 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003455 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3456 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003457 return 0;
3458
3459 // Create the new property.
3460 ObjCPropertyDecl *ToProperty
3461 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3462 Name.getAsIdentifierInfo(),
3463 Importer.Import(D->getAtLoc()),
3464 T,
3465 D->getPropertyImplementation());
3466 Importer.Imported(D, ToProperty);
3467 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003468 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003469
3470 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003471 ToProperty->setPropertyAttributesAsWritten(
3472 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003473 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3474 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3475 ToProperty->setGetterMethodDecl(
3476 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3477 ToProperty->setSetterMethodDecl(
3478 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3479 ToProperty->setPropertyIvarDecl(
3480 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3481 return ToProperty;
3482}
3483
Douglas Gregor954e0c72010-12-07 18:32:03 +00003484Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3485 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3486 Importer.Import(D->getPropertyDecl()));
3487 if (!Property)
3488 return 0;
3489
3490 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3491 if (!DC)
3492 return 0;
3493
3494 // Import the lexical declaration context.
3495 DeclContext *LexicalDC = DC;
3496 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3497 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3498 if (!LexicalDC)
3499 return 0;
3500 }
3501
3502 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3503 if (!InImpl)
3504 return 0;
3505
3506 // Import the ivar (for an @synthesize).
3507 ObjCIvarDecl *Ivar = 0;
3508 if (D->getPropertyIvarDecl()) {
3509 Ivar = cast_or_null<ObjCIvarDecl>(
3510 Importer.Import(D->getPropertyIvarDecl()));
3511 if (!Ivar)
3512 return 0;
3513 }
3514
3515 ObjCPropertyImplDecl *ToImpl
3516 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3517 if (!ToImpl) {
3518 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3519 Importer.Import(D->getLocStart()),
3520 Importer.Import(D->getLocation()),
3521 Property,
3522 D->getPropertyImplementation(),
3523 Ivar,
3524 Importer.Import(D->getPropertyIvarDeclLoc()));
3525 ToImpl->setLexicalDeclContext(LexicalDC);
3526 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003527 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003528 } else {
3529 // Check that we have the same kind of property implementation (@synthesize
3530 // vs. @dynamic).
3531 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3532 Importer.ToDiag(ToImpl->getLocation(),
3533 diag::err_odr_objc_property_impl_kind_inconsistent)
3534 << Property->getDeclName()
3535 << (ToImpl->getPropertyImplementation()
3536 == ObjCPropertyImplDecl::Dynamic);
3537 Importer.FromDiag(D->getLocation(),
3538 diag::note_odr_objc_property_impl_kind)
3539 << D->getPropertyDecl()->getDeclName()
3540 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3541 return 0;
3542 }
3543
3544 // For @synthesize, check that we have the same
3545 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3546 Ivar != ToImpl->getPropertyIvarDecl()) {
3547 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3548 diag::err_odr_objc_synthesize_ivar_inconsistent)
3549 << Property->getDeclName()
3550 << ToImpl->getPropertyIvarDecl()->getDeclName()
3551 << Ivar->getDeclName();
3552 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3553 diag::note_odr_objc_synthesize_ivar_here)
3554 << D->getPropertyIvarDecl()->getDeclName();
3555 return 0;
3556 }
3557
3558 // Merge the existing implementation with the new implementation.
3559 Importer.Imported(D, ToImpl);
3560 }
3561
3562 return ToImpl;
3563}
3564
Douglas Gregor2b785022010-02-18 02:12:22 +00003565Decl *
3566ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3567 // Import the context of this declaration.
3568 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3569 if (!DC)
3570 return 0;
3571
3572 DeclContext *LexicalDC = DC;
3573 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3574 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3575 if (!LexicalDC)
3576 return 0;
3577 }
3578
3579 // Import the location of this declaration.
3580 SourceLocation Loc = Importer.Import(D->getLocation());
3581
Chris Lattner5f9e2722011-07-23 10:55:15 +00003582 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3583 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003584 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3585 = D->protocol_loc_begin();
3586 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3587 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3588 FromProto != FromProtoEnd;
3589 ++FromProto, ++FromProtoLoc) {
3590 ObjCProtocolDecl *ToProto
3591 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3592 if (!ToProto)
3593 continue;
3594
3595 Protocols.push_back(ToProto);
3596 Locations.push_back(Importer.Import(*FromProtoLoc));
3597 }
3598
3599 ObjCForwardProtocolDecl *ToForward
3600 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3601 Protocols.data(), Protocols.size(),
3602 Locations.data());
3603 ToForward->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003604 LexicalDC->addDeclInternal(ToForward);
Douglas Gregor2b785022010-02-18 02:12:22 +00003605 Importer.Imported(D, ToForward);
3606 return ToForward;
3607}
3608
Douglas Gregor040afae2010-11-30 19:14:50 +00003609Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3610 // For template arguments, we adopt the translation unit as our declaration
3611 // context. This context will be fixed when the actual template declaration
3612 // is created.
3613
3614 // FIXME: Import default argument.
3615 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3616 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003617 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003618 Importer.Import(D->getLocation()),
3619 D->getDepth(),
3620 D->getIndex(),
3621 Importer.Import(D->getIdentifier()),
3622 D->wasDeclaredWithTypename(),
3623 D->isParameterPack());
3624}
3625
3626Decl *
3627ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3628 // Import the name of this declaration.
3629 DeclarationName Name = Importer.Import(D->getDeclName());
3630 if (D->getDeclName() && !Name)
3631 return 0;
3632
3633 // Import the location of this declaration.
3634 SourceLocation Loc = Importer.Import(D->getLocation());
3635
3636 // Import the type of this declaration.
3637 QualType T = Importer.Import(D->getType());
3638 if (T.isNull())
3639 return 0;
3640
3641 // Import type-source information.
3642 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3643 if (D->getTypeSourceInfo() && !TInfo)
3644 return 0;
3645
3646 // FIXME: Import default argument.
3647
3648 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3649 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003650 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003651 Loc, D->getDepth(), D->getPosition(),
3652 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003653 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003654}
3655
3656Decl *
3657ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3658 // Import the name of this declaration.
3659 DeclarationName Name = Importer.Import(D->getDeclName());
3660 if (D->getDeclName() && !Name)
3661 return 0;
3662
3663 // Import the location of this declaration.
3664 SourceLocation Loc = Importer.Import(D->getLocation());
3665
3666 // Import template parameters.
3667 TemplateParameterList *TemplateParams
3668 = ImportTemplateParameterList(D->getTemplateParameters());
3669 if (!TemplateParams)
3670 return 0;
3671
3672 // FIXME: Import default argument.
3673
3674 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3675 Importer.getToContext().getTranslationUnitDecl(),
3676 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003677 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003678 Name.getAsIdentifierInfo(),
3679 TemplateParams);
3680}
3681
3682Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3683 // If this record has a definition in the translation unit we're coming from,
3684 // but this particular declaration is not that definition, import the
3685 // definition and map to that.
3686 CXXRecordDecl *Definition
3687 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3688 if (Definition && Definition != D->getTemplatedDecl()) {
3689 Decl *ImportedDef
3690 = Importer.Import(Definition->getDescribedClassTemplate());
3691 if (!ImportedDef)
3692 return 0;
3693
3694 return Importer.Imported(D, ImportedDef);
3695 }
3696
3697 // Import the major distinguishing characteristics of this class template.
3698 DeclContext *DC, *LexicalDC;
3699 DeclarationName Name;
3700 SourceLocation Loc;
3701 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3702 return 0;
3703
3704 // We may already have a template of the same name; try to find and match it.
3705 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003706 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003707 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3708 DC->localUncachedLookup(Name, FoundDecls);
3709 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3710 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003711 continue;
3712
Douglas Gregorb75a3452011-10-15 00:10:27 +00003713 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003714 if (ClassTemplateDecl *FoundTemplate
3715 = dyn_cast<ClassTemplateDecl>(Found)) {
3716 if (IsStructuralMatch(D, FoundTemplate)) {
3717 // The class templates structurally match; call it the same template.
3718 // FIXME: We may be filling in a forward declaration here. Handle
3719 // this case!
3720 Importer.Imported(D->getTemplatedDecl(),
3721 FoundTemplate->getTemplatedDecl());
3722 return Importer.Imported(D, FoundTemplate);
3723 }
3724 }
3725
Douglas Gregorb75a3452011-10-15 00:10:27 +00003726 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003727 }
3728
3729 if (!ConflictingDecls.empty()) {
3730 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3731 ConflictingDecls.data(),
3732 ConflictingDecls.size());
3733 }
3734
3735 if (!Name)
3736 return 0;
3737 }
3738
3739 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3740
3741 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003742 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3743 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003744 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3745 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003746 DC, StartLoc, IdLoc,
3747 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003748 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003749 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003750 D2Templated->setLexicalDeclContext(LexicalDC);
3751
3752 // Create the class template declaration itself.
3753 TemplateParameterList *TemplateParams
3754 = ImportTemplateParameterList(D->getTemplateParameters());
3755 if (!TemplateParams)
3756 return 0;
3757
3758 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3759 Loc, Name, TemplateParams,
3760 D2Templated,
3761 /*PrevDecl=*/0);
3762 D2Templated->setDescribedClassTemplate(D2);
3763
3764 D2->setAccess(D->getAccess());
3765 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003766 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003767
3768 // Note the relationship between the class templates.
3769 Importer.Imported(D, D2);
3770 Importer.Imported(DTemplated, D2Templated);
3771
John McCall5e1cdac2011-10-07 06:10:15 +00003772 if (DTemplated->isCompleteDefinition() &&
3773 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003774 // FIXME: Import definition!
3775 }
3776
3777 return D2;
3778}
3779
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003780Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3781 ClassTemplateSpecializationDecl *D) {
3782 // If this record has a definition in the translation unit we're coming from,
3783 // but this particular declaration is not that definition, import the
3784 // definition and map to that.
3785 TagDecl *Definition = D->getDefinition();
3786 if (Definition && Definition != D) {
3787 Decl *ImportedDef = Importer.Import(Definition);
3788 if (!ImportedDef)
3789 return 0;
3790
3791 return Importer.Imported(D, ImportedDef);
3792 }
3793
3794 ClassTemplateDecl *ClassTemplate
3795 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3796 D->getSpecializedTemplate()));
3797 if (!ClassTemplate)
3798 return 0;
3799
3800 // Import the context of this declaration.
3801 DeclContext *DC = ClassTemplate->getDeclContext();
3802 if (!DC)
3803 return 0;
3804
3805 DeclContext *LexicalDC = DC;
3806 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3807 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3808 if (!LexicalDC)
3809 return 0;
3810 }
3811
3812 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003813 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3814 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003815
3816 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003817 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003818 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3819 D->getTemplateArgs().size(),
3820 TemplateArgs))
3821 return 0;
3822
3823 // Try to find an existing specialization with these template arguments.
3824 void *InsertPos = 0;
3825 ClassTemplateSpecializationDecl *D2
3826 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3827 TemplateArgs.size(), InsertPos);
3828 if (D2) {
3829 // We already have a class template specialization with these template
3830 // arguments.
3831
3832 // FIXME: Check for specialization vs. instantiation errors.
3833
3834 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003835 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003836 // The record types structurally match, or the "from" translation
3837 // unit only had a forward declaration anyway; call it the same
3838 // function.
3839 return Importer.Imported(D, FoundDef);
3840 }
3841 }
3842 } else {
3843 // Create a new specialization.
3844 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3845 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003846 StartLoc, IdLoc,
3847 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003848 TemplateArgs.data(),
3849 TemplateArgs.size(),
3850 /*PrevDecl=*/0);
3851 D2->setSpecializationKind(D->getSpecializationKind());
3852
3853 // Add this specialization to the class template.
3854 ClassTemplate->AddSpecialization(D2, InsertPos);
3855
3856 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003857 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003858
3859 // Add the specialization to this context.
3860 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003861 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003862 }
3863 Importer.Imported(D, D2);
3864
John McCall5e1cdac2011-10-07 06:10:15 +00003865 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003866 return 0;
3867
3868 return D2;
3869}
3870
Douglas Gregor4800d952010-02-11 19:21:55 +00003871//----------------------------------------------------------------------------
3872// Import Statements
3873//----------------------------------------------------------------------------
3874
3875Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3876 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3877 << S->getStmtClassName();
3878 return 0;
3879}
3880
3881//----------------------------------------------------------------------------
3882// Import Expressions
3883//----------------------------------------------------------------------------
3884Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3885 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3886 << E->getStmtClassName();
3887 return 0;
3888}
3889
Douglas Gregor44080632010-02-19 01:17:02 +00003890Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003891 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3892 if (!ToD)
3893 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003894
3895 NamedDecl *FoundD = 0;
3896 if (E->getDecl() != E->getFoundDecl()) {
3897 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3898 if (!FoundD)
3899 return 0;
3900 }
Douglas Gregor44080632010-02-19 01:17:02 +00003901
3902 QualType T = Importer.Import(E->getType());
3903 if (T.isNull())
3904 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003905
3906 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3907 Importer.Import(E->getQualifierLoc()),
3908 ToD,
3909 Importer.Import(E->getLocation()),
3910 T, E->getValueKind(),
3911 FoundD,
3912 /*FIXME:TemplateArgs=*/0);
3913 if (E->hadMultipleCandidates())
3914 DRE->setHadMultipleCandidates(true);
3915 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003916}
3917
Douglas Gregor4800d952010-02-11 19:21:55 +00003918Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3919 QualType T = Importer.Import(E->getType());
3920 if (T.isNull())
3921 return 0;
3922
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003923 return IntegerLiteral::Create(Importer.getToContext(),
3924 E->getValue(), T,
3925 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003926}
3927
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003928Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3929 QualType T = Importer.Import(E->getType());
3930 if (T.isNull())
3931 return 0;
3932
Douglas Gregor5cee1192011-07-27 05:40:30 +00003933 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3934 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003935 Importer.Import(E->getLocation()));
3936}
3937
Douglas Gregorf638f952010-02-19 01:07:06 +00003938Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3939 Expr *SubExpr = Importer.Import(E->getSubExpr());
3940 if (!SubExpr)
3941 return 0;
3942
3943 return new (Importer.getToContext())
3944 ParenExpr(Importer.Import(E->getLParen()),
3945 Importer.Import(E->getRParen()),
3946 SubExpr);
3947}
3948
3949Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3950 QualType T = Importer.Import(E->getType());
3951 if (T.isNull())
3952 return 0;
3953
3954 Expr *SubExpr = Importer.Import(E->getSubExpr());
3955 if (!SubExpr)
3956 return 0;
3957
3958 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003959 T, E->getValueKind(),
3960 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003961 Importer.Import(E->getOperatorLoc()));
3962}
3963
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003964Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3965 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003966 QualType ResultType = Importer.Import(E->getType());
3967
3968 if (E->isArgumentType()) {
3969 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3970 if (!TInfo)
3971 return 0;
3972
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003973 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3974 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003975 Importer.Import(E->getOperatorLoc()),
3976 Importer.Import(E->getRParenLoc()));
3977 }
3978
3979 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3980 if (!SubExpr)
3981 return 0;
3982
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003983 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3984 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003985 Importer.Import(E->getOperatorLoc()),
3986 Importer.Import(E->getRParenLoc()));
3987}
3988
Douglas Gregorf638f952010-02-19 01:07:06 +00003989Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3990 QualType T = Importer.Import(E->getType());
3991 if (T.isNull())
3992 return 0;
3993
3994 Expr *LHS = Importer.Import(E->getLHS());
3995 if (!LHS)
3996 return 0;
3997
3998 Expr *RHS = Importer.Import(E->getRHS());
3999 if (!RHS)
4000 return 0;
4001
4002 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004003 T, E->getValueKind(),
4004 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004005 Importer.Import(E->getOperatorLoc()));
4006}
4007
4008Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4009 QualType T = Importer.Import(E->getType());
4010 if (T.isNull())
4011 return 0;
4012
4013 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4014 if (CompLHSType.isNull())
4015 return 0;
4016
4017 QualType CompResultType = Importer.Import(E->getComputationResultType());
4018 if (CompResultType.isNull())
4019 return 0;
4020
4021 Expr *LHS = Importer.Import(E->getLHS());
4022 if (!LHS)
4023 return 0;
4024
4025 Expr *RHS = Importer.Import(E->getRHS());
4026 if (!RHS)
4027 return 0;
4028
4029 return new (Importer.getToContext())
4030 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004031 T, E->getValueKind(),
4032 E->getObjectKind(),
4033 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004034 Importer.Import(E->getOperatorLoc()));
4035}
4036
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004037static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004038 if (E->path_empty()) return false;
4039
4040 // TODO: import cast paths
4041 return true;
4042}
4043
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004044Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4045 QualType T = Importer.Import(E->getType());
4046 if (T.isNull())
4047 return 0;
4048
4049 Expr *SubExpr = Importer.Import(E->getSubExpr());
4050 if (!SubExpr)
4051 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004052
4053 CXXCastPath BasePath;
4054 if (ImportCastPath(E, BasePath))
4055 return 0;
4056
4057 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004058 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004059}
4060
Douglas Gregor008847a2010-02-19 01:32:14 +00004061Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4062 QualType T = Importer.Import(E->getType());
4063 if (T.isNull())
4064 return 0;
4065
4066 Expr *SubExpr = Importer.Import(E->getSubExpr());
4067 if (!SubExpr)
4068 return 0;
4069
4070 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4071 if (!TInfo && E->getTypeInfoAsWritten())
4072 return 0;
4073
John McCallf871d0c2010-08-07 06:22:56 +00004074 CXXCastPath BasePath;
4075 if (ImportCastPath(E, BasePath))
4076 return 0;
4077
John McCallf89e55a2010-11-18 06:31:45 +00004078 return CStyleCastExpr::Create(Importer.getToContext(), T,
4079 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004080 SubExpr, &BasePath, TInfo,
4081 Importer.Import(E->getLParenLoc()),
4082 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004083}
4084
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004085ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004086 ASTContext &FromContext, FileManager &FromFileManager,
4087 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004088 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004089 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4090 Minimal(MinimalImport)
4091{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004092 ImportedDecls[FromContext.getTranslationUnitDecl()]
4093 = ToContext.getTranslationUnitDecl();
4094}
4095
4096ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004097
4098QualType ASTImporter::Import(QualType FromT) {
4099 if (FromT.isNull())
4100 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004101
4102 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004103
Douglas Gregor169fba52010-02-08 15:18:58 +00004104 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004105 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4106 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004107 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004108 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004109
Douglas Gregor169fba52010-02-08 15:18:58 +00004110 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004111 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004112 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004113 if (ToT.isNull())
4114 return ToT;
4115
Douglas Gregor169fba52010-02-08 15:18:58 +00004116 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004117 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004118
John McCallf4c73712011-01-19 06:33:43 +00004119 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004120}
4121
Douglas Gregor9bed8792010-02-09 19:21:46 +00004122TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004123 if (!FromTSI)
4124 return FromTSI;
4125
4126 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004127 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004128 QualType T = Import(FromTSI->getType());
4129 if (T.isNull())
4130 return 0;
4131
4132 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004133 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004134}
4135
4136Decl *ASTImporter::Import(Decl *FromD) {
4137 if (!FromD)
4138 return 0;
4139
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004140 ASTNodeImporter Importer(*this);
4141
Douglas Gregor9bed8792010-02-09 19:21:46 +00004142 // Check whether we've already imported this declaration.
4143 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004144 if (Pos != ImportedDecls.end()) {
4145 Decl *ToD = Pos->second;
4146 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4147 return ToD;
4148 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004149
4150 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004151 Decl *ToD = Importer.Visit(FromD);
4152 if (!ToD)
4153 return 0;
4154
4155 // Record the imported declaration.
4156 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004157
4158 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4159 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004160 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004161 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004162 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004163 // When we've finished transforming a typedef, see whether it was the
4164 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004165 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004166 FromTag = AnonTagsWithPendingTypedefs.begin(),
4167 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4168 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004169 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004170 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4171 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004172 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004173 AnonTagsWithPendingTypedefs.erase(FromTag);
4174 break;
4175 }
4176 }
4177 }
4178 }
4179
Douglas Gregor9bed8792010-02-09 19:21:46 +00004180 return ToD;
4181}
4182
4183DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4184 if (!FromDC)
4185 return FromDC;
4186
4187 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4188}
4189
4190Expr *ASTImporter::Import(Expr *FromE) {
4191 if (!FromE)
4192 return 0;
4193
4194 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4195}
4196
4197Stmt *ASTImporter::Import(Stmt *FromS) {
4198 if (!FromS)
4199 return 0;
4200
Douglas Gregor4800d952010-02-11 19:21:55 +00004201 // Check whether we've already imported this declaration.
4202 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4203 if (Pos != ImportedStmts.end())
4204 return Pos->second;
4205
4206 // Import the type
4207 ASTNodeImporter Importer(*this);
4208 Stmt *ToS = Importer.Visit(FromS);
4209 if (!ToS)
4210 return 0;
4211
4212 // Record the imported declaration.
4213 ImportedStmts[FromS] = ToS;
4214 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004215}
4216
4217NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4218 if (!FromNNS)
4219 return 0;
4220
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004221 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4222
4223 switch (FromNNS->getKind()) {
4224 case NestedNameSpecifier::Identifier:
4225 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4226 return NestedNameSpecifier::Create(ToContext, prefix, II);
4227 }
4228 return 0;
4229
4230 case NestedNameSpecifier::Namespace:
4231 if (NamespaceDecl *NS =
4232 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4233 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4234 }
4235 return 0;
4236
4237 case NestedNameSpecifier::NamespaceAlias:
4238 if (NamespaceAliasDecl *NSAD =
4239 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4240 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4241 }
4242 return 0;
4243
4244 case NestedNameSpecifier::Global:
4245 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4246
4247 case NestedNameSpecifier::TypeSpec:
4248 case NestedNameSpecifier::TypeSpecWithTemplate: {
4249 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4250 if (!T.isNull()) {
4251 bool bTemplate = FromNNS->getKind() ==
4252 NestedNameSpecifier::TypeSpecWithTemplate;
4253 return NestedNameSpecifier::Create(ToContext, prefix,
4254 bTemplate, T.getTypePtr());
4255 }
4256 }
4257 return 0;
4258 }
4259
4260 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004261 return 0;
4262}
4263
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004264NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4265 // FIXME: Implement!
4266 return NestedNameSpecifierLoc();
4267}
4268
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004269TemplateName ASTImporter::Import(TemplateName From) {
4270 switch (From.getKind()) {
4271 case TemplateName::Template:
4272 if (TemplateDecl *ToTemplate
4273 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4274 return TemplateName(ToTemplate);
4275
4276 return TemplateName();
4277
4278 case TemplateName::OverloadedTemplate: {
4279 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4280 UnresolvedSet<2> ToTemplates;
4281 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4282 E = FromStorage->end();
4283 I != E; ++I) {
4284 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4285 ToTemplates.addDecl(To);
4286 else
4287 return TemplateName();
4288 }
4289 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4290 ToTemplates.end());
4291 }
4292
4293 case TemplateName::QualifiedTemplate: {
4294 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4295 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4296 if (!Qualifier)
4297 return TemplateName();
4298
4299 if (TemplateDecl *ToTemplate
4300 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4301 return ToContext.getQualifiedTemplateName(Qualifier,
4302 QTN->hasTemplateKeyword(),
4303 ToTemplate);
4304
4305 return TemplateName();
4306 }
4307
4308 case TemplateName::DependentTemplate: {
4309 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4310 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4311 if (!Qualifier)
4312 return TemplateName();
4313
4314 if (DTN->isIdentifier()) {
4315 return ToContext.getDependentTemplateName(Qualifier,
4316 Import(DTN->getIdentifier()));
4317 }
4318
4319 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4320 }
John McCall14606042011-06-30 08:33:18 +00004321
4322 case TemplateName::SubstTemplateTemplateParm: {
4323 SubstTemplateTemplateParmStorage *subst
4324 = From.getAsSubstTemplateTemplateParm();
4325 TemplateTemplateParmDecl *param
4326 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4327 if (!param)
4328 return TemplateName();
4329
4330 TemplateName replacement = Import(subst->getReplacement());
4331 if (replacement.isNull()) return TemplateName();
4332
4333 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4334 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004335
4336 case TemplateName::SubstTemplateTemplateParmPack: {
4337 SubstTemplateTemplateParmPackStorage *SubstPack
4338 = From.getAsSubstTemplateTemplateParmPack();
4339 TemplateTemplateParmDecl *Param
4340 = cast_or_null<TemplateTemplateParmDecl>(
4341 Import(SubstPack->getParameterPack()));
4342 if (!Param)
4343 return TemplateName();
4344
4345 ASTNodeImporter Importer(*this);
4346 TemplateArgument ArgPack
4347 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4348 if (ArgPack.isNull())
4349 return TemplateName();
4350
4351 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4352 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004353 }
4354
4355 llvm_unreachable("Invalid template name kind");
4356 return TemplateName();
4357}
4358
Douglas Gregor9bed8792010-02-09 19:21:46 +00004359SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4360 if (FromLoc.isInvalid())
4361 return SourceLocation();
4362
Douglas Gregor88523732010-02-10 00:15:17 +00004363 SourceManager &FromSM = FromContext.getSourceManager();
4364
4365 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004366 // don't have to import macro expansions.
4367 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004368 FromLoc = FromSM.getSpellingLoc(FromLoc);
4369 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4370 SourceManager &ToSM = ToContext.getSourceManager();
4371 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004372 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004373}
4374
4375SourceRange ASTImporter::Import(SourceRange FromRange) {
4376 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4377}
4378
Douglas Gregor88523732010-02-10 00:15:17 +00004379FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004380 llvm::DenseMap<FileID, FileID>::iterator Pos
4381 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004382 if (Pos != ImportedFileIDs.end())
4383 return Pos->second;
4384
4385 SourceManager &FromSM = FromContext.getSourceManager();
4386 SourceManager &ToSM = ToContext.getSourceManager();
4387 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004388 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004389
4390 // Include location of this file.
4391 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4392
4393 // Map the FileID for to the "to" source manager.
4394 FileID ToID;
4395 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004396 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004397 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4398 // disk again
4399 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4400 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004401 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004402 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4403 FromSLoc.getFile().getFileCharacteristic());
4404 } else {
4405 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004406 const llvm::MemoryBuffer *
4407 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004408 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004409 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004410 FromBuf->getBufferIdentifier());
4411 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4412 }
4413
4414
Sebastian Redl535a3e22010-09-30 01:03:06 +00004415 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004416 return ToID;
4417}
4418
Douglas Gregord8868a62011-01-18 03:11:38 +00004419void ASTImporter::ImportDefinition(Decl *From) {
4420 Decl *To = Import(From);
4421 if (!To)
4422 return;
4423
4424 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4425 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004426
4427 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4428 if (!ToRecord->getDefinition()) {
4429 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4430 /*ForceImport=*/true);
4431 return;
4432 }
4433 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004434
4435 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4436 if (!ToEnum->getDefinition()) {
4437 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4438 /*ForceImport=*/true);
4439 return;
4440 }
4441 }
4442
Douglas Gregord8868a62011-01-18 03:11:38 +00004443 Importer.ImportDeclContext(FromDC, true);
4444 }
4445}
4446
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004447DeclarationName ASTImporter::Import(DeclarationName FromName) {
4448 if (!FromName)
4449 return DeclarationName();
4450
4451 switch (FromName.getNameKind()) {
4452 case DeclarationName::Identifier:
4453 return Import(FromName.getAsIdentifierInfo());
4454
4455 case DeclarationName::ObjCZeroArgSelector:
4456 case DeclarationName::ObjCOneArgSelector:
4457 case DeclarationName::ObjCMultiArgSelector:
4458 return Import(FromName.getObjCSelector());
4459
4460 case DeclarationName::CXXConstructorName: {
4461 QualType T = Import(FromName.getCXXNameType());
4462 if (T.isNull())
4463 return DeclarationName();
4464
4465 return ToContext.DeclarationNames.getCXXConstructorName(
4466 ToContext.getCanonicalType(T));
4467 }
4468
4469 case DeclarationName::CXXDestructorName: {
4470 QualType T = Import(FromName.getCXXNameType());
4471 if (T.isNull())
4472 return DeclarationName();
4473
4474 return ToContext.DeclarationNames.getCXXDestructorName(
4475 ToContext.getCanonicalType(T));
4476 }
4477
4478 case DeclarationName::CXXConversionFunctionName: {
4479 QualType T = Import(FromName.getCXXNameType());
4480 if (T.isNull())
4481 return DeclarationName();
4482
4483 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4484 ToContext.getCanonicalType(T));
4485 }
4486
4487 case DeclarationName::CXXOperatorName:
4488 return ToContext.DeclarationNames.getCXXOperatorName(
4489 FromName.getCXXOverloadedOperator());
4490
4491 case DeclarationName::CXXLiteralOperatorName:
4492 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4493 Import(FromName.getCXXLiteralIdentifier()));
4494
4495 case DeclarationName::CXXUsingDirective:
4496 // FIXME: STATICS!
4497 return DeclarationName::getUsingDirectiveName();
4498 }
4499
4500 // Silence bogus GCC warning
4501 return DeclarationName();
4502}
4503
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004504IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004505 if (!FromId)
4506 return 0;
4507
4508 return &ToContext.Idents.get(FromId->getName());
4509}
Douglas Gregor089459a2010-02-08 21:09:39 +00004510
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004511Selector ASTImporter::Import(Selector FromSel) {
4512 if (FromSel.isNull())
4513 return Selector();
4514
Chris Lattner5f9e2722011-07-23 10:55:15 +00004515 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004516 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4517 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4518 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4519 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4520}
4521
Douglas Gregor089459a2010-02-08 21:09:39 +00004522DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4523 DeclContext *DC,
4524 unsigned IDNS,
4525 NamedDecl **Decls,
4526 unsigned NumDecls) {
4527 return Name;
4528}
4529
4530DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004531 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004532}
4533
4534DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004535 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004536}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004537
4538Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4539 ImportedDecls[From] = To;
4540 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004541}
Douglas Gregorea35d112010-02-15 23:54:17 +00004542
4543bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004544 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004545 = ImportedTypes.find(From.getTypePtr());
4546 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4547 return true;
4548
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004549 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004550 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004551}