blob: 6ee8ba3a5f3b40f6e5bf3efa25f569ea83771257 [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 Gregor040afae2010-11-30 19:14:50 +0000130 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
131 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
132 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
133 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000134 Decl *VisitClassTemplateSpecializationDecl(
135 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000136
Douglas Gregor4800d952010-02-11 19:21:55 +0000137 // Importing statements
138 Stmt *VisitStmt(Stmt *S);
139
140 // Importing expressions
141 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000142 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000143 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000144 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000145 Expr *VisitParenExpr(ParenExpr *E);
146 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000147 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitBinaryOperator(BinaryOperator *E);
149 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000150 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000151 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000152 };
153}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000154using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155
156//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000157// Structural Equivalence
158//----------------------------------------------------------------------------
159
160namespace {
161 struct StructuralEquivalenceContext {
162 /// \brief AST contexts for which we are checking structural equivalence.
163 ASTContext &C1, &C2;
164
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000165 /// \brief The set of "tentative" equivalences between two canonical
166 /// declarations, mapping from a declaration in the first context to the
167 /// declaration in the second context that we believe to be equivalent.
168 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
169
170 /// \brief Queue of declarations in the first context whose equivalence
171 /// with a declaration in the second context still needs to be verified.
172 std::deque<Decl *> DeclsToCheck;
173
Douglas Gregorea35d112010-02-15 23:54:17 +0000174 /// \brief Declaration (from, to) pairs that are known not to be equivalent
175 /// (which we have already complained about).
176 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
177
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000178 /// \brief Whether we're being strict about the spelling of types when
179 /// unifying two types.
180 bool StrictTypeSpelling;
181
182 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000183 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000184 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000185 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000186 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000187
188 /// \brief Determine whether the two declarations are structurally
189 /// equivalent.
190 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
191
192 /// \brief Determine whether the two types are structurally equivalent.
193 bool IsStructurallyEquivalent(QualType T1, QualType T2);
194
195 private:
196 /// \brief Finish checking all of the structural equivalences.
197 ///
198 /// \returns true if an error occurred, false otherwise.
199 bool Finish();
200
201 public:
202 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000203 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000204 }
205
206 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000207 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000208 }
209 };
210}
211
212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
213 QualType T1, QualType T2);
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 Decl *D1, Decl *D2);
216
217/// \brief Determine if two APInts have the same value, after zero-extending
218/// one of them (if needed!) to ensure that the bit-widths match.
219static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
220 if (I1.getBitWidth() == I2.getBitWidth())
221 return I1 == I2;
222
223 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000224 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000225
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227}
228
229/// \brief Determine if two APSInts have the same value, zero- or sign-extending
230/// as needed.
231static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
232 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
233 return I1 == I2;
234
235 // Check for a bit-width mismatch.
236 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000237 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000238 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240
241 // We have a signedness mismatch. Turn the signed value into an unsigned
242 // value.
243 if (I1.isSigned()) {
244 if (I1.isNegative())
245 return false;
246
247 return llvm::APSInt(I1, true) == I2;
248 }
249
250 if (I2.isNegative())
251 return false;
252
253 return I1 == llvm::APSInt(I2, true);
254}
255
256/// \brief Determine structural equivalence of two expressions.
257static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
258 Expr *E1, Expr *E2) {
259 if (!E1 || !E2)
260 return E1 == E2;
261
262 // FIXME: Actually perform a structural comparison!
263 return true;
264}
265
266/// \brief Determine whether two identifiers are equivalent.
267static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
268 const IdentifierInfo *Name2) {
269 if (!Name1 || !Name2)
270 return Name1 == Name2;
271
272 return Name1->getName() == Name2->getName();
273}
274
275/// \brief Determine whether two nested-name-specifiers are equivalent.
276static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
277 NestedNameSpecifier *NNS1,
278 NestedNameSpecifier *NNS2) {
279 // FIXME: Implement!
280 return true;
281}
282
283/// \brief Determine whether two template arguments are equivalent.
284static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
285 const TemplateArgument &Arg1,
286 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000287 if (Arg1.getKind() != Arg2.getKind())
288 return false;
289
290 switch (Arg1.getKind()) {
291 case TemplateArgument::Null:
292 return true;
293
294 case TemplateArgument::Type:
295 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
296
297 case TemplateArgument::Integral:
298 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
299 Arg2.getIntegralType()))
300 return false;
301
302 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
303
304 case TemplateArgument::Declaration:
305 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
306
307 case TemplateArgument::Template:
308 return IsStructurallyEquivalent(Context,
309 Arg1.getAsTemplate(),
310 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000311
312 case TemplateArgument::TemplateExpansion:
313 return IsStructurallyEquivalent(Context,
314 Arg1.getAsTemplateOrTemplatePattern(),
315 Arg2.getAsTemplateOrTemplatePattern());
316
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000317 case TemplateArgument::Expression:
318 return IsStructurallyEquivalent(Context,
319 Arg1.getAsExpr(), Arg2.getAsExpr());
320
321 case TemplateArgument::Pack:
322 if (Arg1.pack_size() != Arg2.pack_size())
323 return false;
324
325 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
326 if (!IsStructurallyEquivalent(Context,
327 Arg1.pack_begin()[I],
328 Arg2.pack_begin()[I]))
329 return false;
330
331 return true;
332 }
333
334 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000335 return true;
336}
337
338/// \brief Determine structural equivalence for the common part of array
339/// types.
340static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
341 const ArrayType *Array1,
342 const ArrayType *Array2) {
343 if (!IsStructurallyEquivalent(Context,
344 Array1->getElementType(),
345 Array2->getElementType()))
346 return false;
347 if (Array1->getSizeModifier() != Array2->getSizeModifier())
348 return false;
349 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
350 return false;
351
352 return true;
353}
354
355/// \brief Determine structural equivalence of two types.
356static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
357 QualType T1, QualType T2) {
358 if (T1.isNull() || T2.isNull())
359 return T1.isNull() && T2.isNull();
360
361 if (!Context.StrictTypeSpelling) {
362 // We aren't being strict about token-to-token equivalence of types,
363 // so map down to the canonical type.
364 T1 = Context.C1.getCanonicalType(T1);
365 T2 = Context.C2.getCanonicalType(T2);
366 }
367
368 if (T1.getQualifiers() != T2.getQualifiers())
369 return false;
370
Douglas Gregorea35d112010-02-15 23:54:17 +0000371 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 if (T1->getTypeClass() != T2->getTypeClass()) {
374 // Compare function types with prototypes vs. without prototypes as if
375 // both did not have prototypes.
376 if (T1->getTypeClass() == Type::FunctionProto &&
377 T2->getTypeClass() == Type::FunctionNoProto)
378 TC = Type::FunctionNoProto;
379 else if (T1->getTypeClass() == Type::FunctionNoProto &&
380 T2->getTypeClass() == Type::FunctionProto)
381 TC = Type::FunctionNoProto;
382 else
383 return false;
384 }
385
386 switch (TC) {
387 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000388 // FIXME: Deal with Char_S/Char_U.
389 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
390 return false;
391 break;
392
393 case Type::Complex:
394 if (!IsStructurallyEquivalent(Context,
395 cast<ComplexType>(T1)->getElementType(),
396 cast<ComplexType>(T2)->getElementType()))
397 return false;
398 break;
399
400 case Type::Pointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<PointerType>(T1)->getPointeeType(),
403 cast<PointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::BlockPointer:
408 if (!IsStructurallyEquivalent(Context,
409 cast<BlockPointerType>(T1)->getPointeeType(),
410 cast<BlockPointerType>(T2)->getPointeeType()))
411 return false;
412 break;
413
414 case Type::LValueReference:
415 case Type::RValueReference: {
416 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
417 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
418 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
419 return false;
420 if (Ref1->isInnerRef() != Ref2->isInnerRef())
421 return false;
422 if (!IsStructurallyEquivalent(Context,
423 Ref1->getPointeeTypeAsWritten(),
424 Ref2->getPointeeTypeAsWritten()))
425 return false;
426 break;
427 }
428
429 case Type::MemberPointer: {
430 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
431 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
432 if (!IsStructurallyEquivalent(Context,
433 MemPtr1->getPointeeType(),
434 MemPtr2->getPointeeType()))
435 return false;
436 if (!IsStructurallyEquivalent(Context,
437 QualType(MemPtr1->getClass(), 0),
438 QualType(MemPtr2->getClass(), 0)))
439 return false;
440 break;
441 }
442
443 case Type::ConstantArray: {
444 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
445 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
446 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
447 return false;
448
449 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
450 return false;
451 break;
452 }
453
454 case Type::IncompleteArray:
455 if (!IsArrayStructurallyEquivalent(Context,
456 cast<ArrayType>(T1),
457 cast<ArrayType>(T2)))
458 return false;
459 break;
460
461 case Type::VariableArray: {
462 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
463 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
464 if (!IsStructurallyEquivalent(Context,
465 Array1->getSizeExpr(), Array2->getSizeExpr()))
466 return false;
467
468 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
469 return false;
470
471 break;
472 }
473
474 case Type::DependentSizedArray: {
475 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
476 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
477 if (!IsStructurallyEquivalent(Context,
478 Array1->getSizeExpr(), Array2->getSizeExpr()))
479 return false;
480
481 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
482 return false;
483
484 break;
485 }
486
487 case Type::DependentSizedExtVector: {
488 const DependentSizedExtVectorType *Vec1
489 = cast<DependentSizedExtVectorType>(T1);
490 const DependentSizedExtVectorType *Vec2
491 = cast<DependentSizedExtVectorType>(T2);
492 if (!IsStructurallyEquivalent(Context,
493 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
494 return false;
495 if (!IsStructurallyEquivalent(Context,
496 Vec1->getElementType(),
497 Vec2->getElementType()))
498 return false;
499 break;
500 }
501
502 case Type::Vector:
503 case Type::ExtVector: {
504 const VectorType *Vec1 = cast<VectorType>(T1);
505 const VectorType *Vec2 = cast<VectorType>(T2);
506 if (!IsStructurallyEquivalent(Context,
507 Vec1->getElementType(),
508 Vec2->getElementType()))
509 return false;
510 if (Vec1->getNumElements() != Vec2->getNumElements())
511 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000512 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000513 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000514 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 }
516
517 case Type::FunctionProto: {
518 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
519 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
520 if (Proto1->getNumArgs() != Proto2->getNumArgs())
521 return false;
522 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
523 if (!IsStructurallyEquivalent(Context,
524 Proto1->getArgType(I),
525 Proto2->getArgType(I)))
526 return false;
527 }
528 if (Proto1->isVariadic() != Proto2->isVariadic())
529 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000530 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
533 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
534 return false;
535 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
536 if (!IsStructurallyEquivalent(Context,
537 Proto1->getExceptionType(I),
538 Proto2->getExceptionType(I)))
539 return false;
540 }
541 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000542 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000543 Proto1->getNoexceptExpr(),
544 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 return false;
546 }
547 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
548 return false;
549
550 // Fall through to check the bits common with FunctionNoProtoType.
551 }
552
553 case Type::FunctionNoProto: {
554 const FunctionType *Function1 = cast<FunctionType>(T1);
555 const FunctionType *Function2 = cast<FunctionType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 Function1->getResultType(),
558 Function2->getResultType()))
559 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000560 if (Function1->getExtInfo() != Function2->getExtInfo())
561 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000562 break;
563 }
564
565 case Type::UnresolvedUsing:
566 if (!IsStructurallyEquivalent(Context,
567 cast<UnresolvedUsingType>(T1)->getDecl(),
568 cast<UnresolvedUsingType>(T2)->getDecl()))
569 return false;
570
571 break;
John McCall9d156a72011-01-06 01:58:22 +0000572
573 case Type::Attributed:
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getModifiedType(),
576 cast<AttributedType>(T2)->getModifiedType()))
577 return false;
578 if (!IsStructurallyEquivalent(Context,
579 cast<AttributedType>(T1)->getEquivalentType(),
580 cast<AttributedType>(T2)->getEquivalentType()))
581 return false;
582 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000583
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000584 case Type::Paren:
585 if (!IsStructurallyEquivalent(Context,
586 cast<ParenType>(T1)->getInnerType(),
587 cast<ParenType>(T2)->getInnerType()))
588 return false;
589 break;
590
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000591 case Type::Typedef:
592 if (!IsStructurallyEquivalent(Context,
593 cast<TypedefType>(T1)->getDecl(),
594 cast<TypedefType>(T2)->getDecl()))
595 return false;
596 break;
597
598 case Type::TypeOfExpr:
599 if (!IsStructurallyEquivalent(Context,
600 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
601 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
602 return false;
603 break;
604
605 case Type::TypeOf:
606 if (!IsStructurallyEquivalent(Context,
607 cast<TypeOfType>(T1)->getUnderlyingType(),
608 cast<TypeOfType>(T2)->getUnderlyingType()))
609 return false;
610 break;
Sean Huntca63c202011-05-24 22:41:36 +0000611
612 case Type::UnaryTransform:
613 if (!IsStructurallyEquivalent(Context,
614 cast<UnaryTransformType>(T1)->getUnderlyingType(),
615 cast<UnaryTransformType>(T1)->getUnderlyingType()))
616 return false;
617 break;
618
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000619 case Type::Decltype:
620 if (!IsStructurallyEquivalent(Context,
621 cast<DecltypeType>(T1)->getUnderlyingExpr(),
622 cast<DecltypeType>(T2)->getUnderlyingExpr()))
623 return false;
624 break;
625
Richard Smith34b41d92011-02-20 03:19:35 +0000626 case Type::Auto:
627 if (!IsStructurallyEquivalent(Context,
628 cast<AutoType>(T1)->getDeducedType(),
629 cast<AutoType>(T2)->getDeducedType()))
630 return false;
631 break;
632
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000633 case Type::Record:
634 case Type::Enum:
635 if (!IsStructurallyEquivalent(Context,
636 cast<TagType>(T1)->getDecl(),
637 cast<TagType>(T2)->getDecl()))
638 return false;
639 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000640
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000641 case Type::TemplateTypeParm: {
642 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
643 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
644 if (Parm1->getDepth() != Parm2->getDepth())
645 return false;
646 if (Parm1->getIndex() != Parm2->getIndex())
647 return false;
648 if (Parm1->isParameterPack() != Parm2->isParameterPack())
649 return false;
650
651 // Names of template type parameters are never significant.
652 break;
653 }
654
655 case Type::SubstTemplateTypeParm: {
656 const SubstTemplateTypeParmType *Subst1
657 = cast<SubstTemplateTypeParmType>(T1);
658 const SubstTemplateTypeParmType *Subst2
659 = cast<SubstTemplateTypeParmType>(T2);
660 if (!IsStructurallyEquivalent(Context,
661 QualType(Subst1->getReplacedParameter(), 0),
662 QualType(Subst2->getReplacedParameter(), 0)))
663 return false;
664 if (!IsStructurallyEquivalent(Context,
665 Subst1->getReplacementType(),
666 Subst2->getReplacementType()))
667 return false;
668 break;
669 }
670
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000671 case Type::SubstTemplateTypeParmPack: {
672 const SubstTemplateTypeParmPackType *Subst1
673 = cast<SubstTemplateTypeParmPackType>(T1);
674 const SubstTemplateTypeParmPackType *Subst2
675 = cast<SubstTemplateTypeParmPackType>(T2);
676 if (!IsStructurallyEquivalent(Context,
677 QualType(Subst1->getReplacedParameter(), 0),
678 QualType(Subst2->getReplacedParameter(), 0)))
679 return false;
680 if (!IsStructurallyEquivalent(Context,
681 Subst1->getArgumentPack(),
682 Subst2->getArgumentPack()))
683 return false;
684 break;
685 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000686 case Type::TemplateSpecialization: {
687 const TemplateSpecializationType *Spec1
688 = cast<TemplateSpecializationType>(T1);
689 const TemplateSpecializationType *Spec2
690 = cast<TemplateSpecializationType>(T2);
691 if (!IsStructurallyEquivalent(Context,
692 Spec1->getTemplateName(),
693 Spec2->getTemplateName()))
694 return false;
695 if (Spec1->getNumArgs() != Spec2->getNumArgs())
696 return false;
697 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
698 if (!IsStructurallyEquivalent(Context,
699 Spec1->getArg(I), Spec2->getArg(I)))
700 return false;
701 }
702 break;
703 }
704
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000705 case Type::Elaborated: {
706 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
707 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
708 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
709 if (Elab1->getKeyword() != Elab2->getKeyword())
710 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000711 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000712 Elab1->getQualifier(),
713 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000714 return false;
715 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000716 Elab1->getNamedType(),
717 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000718 return false;
719 break;
720 }
721
John McCall3cb0ebd2010-03-10 03:28:59 +0000722 case Type::InjectedClassName: {
723 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
724 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
725 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000726 Inj1->getInjectedSpecializationType(),
727 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000728 return false;
729 break;
730 }
731
Douglas Gregor4714c122010-03-31 17:34:00 +0000732 case Type::DependentName: {
733 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
734 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000735 if (!IsStructurallyEquivalent(Context,
736 Typename1->getQualifier(),
737 Typename2->getQualifier()))
738 return false;
739 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
740 Typename2->getIdentifier()))
741 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000742
743 break;
744 }
745
John McCall33500952010-06-11 00:33:02 +0000746 case Type::DependentTemplateSpecialization: {
747 const DependentTemplateSpecializationType *Spec1 =
748 cast<DependentTemplateSpecializationType>(T1);
749 const DependentTemplateSpecializationType *Spec2 =
750 cast<DependentTemplateSpecializationType>(T2);
751 if (!IsStructurallyEquivalent(Context,
752 Spec1->getQualifier(),
753 Spec2->getQualifier()))
754 return false;
755 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
756 Spec2->getIdentifier()))
757 return false;
758 if (Spec1->getNumArgs() != Spec2->getNumArgs())
759 return false;
760 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
761 if (!IsStructurallyEquivalent(Context,
762 Spec1->getArg(I), Spec2->getArg(I)))
763 return false;
764 }
765 break;
766 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000767
768 case Type::PackExpansion:
769 if (!IsStructurallyEquivalent(Context,
770 cast<PackExpansionType>(T1)->getPattern(),
771 cast<PackExpansionType>(T2)->getPattern()))
772 return false;
773 break;
774
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000775 case Type::ObjCInterface: {
776 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
777 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
778 if (!IsStructurallyEquivalent(Context,
779 Iface1->getDecl(), Iface2->getDecl()))
780 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000781 break;
782 }
783
784 case Type::ObjCObject: {
785 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
786 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
787 if (!IsStructurallyEquivalent(Context,
788 Obj1->getBaseType(),
789 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000790 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000791 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
792 return false;
793 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000794 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000795 Obj1->getProtocol(I),
796 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000797 return false;
798 }
799 break;
800 }
801
802 case Type::ObjCObjectPointer: {
803 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
804 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
805 if (!IsStructurallyEquivalent(Context,
806 Ptr1->getPointeeType(),
807 Ptr2->getPointeeType()))
808 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000809 break;
810 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000811
812 case Type::Atomic: {
813 if (!IsStructurallyEquivalent(Context,
814 cast<AtomicType>(T1)->getValueType(),
815 cast<AtomicType>(T2)->getValueType()))
816 return false;
817 break;
818 }
819
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000820 } // end switch
821
822 return true;
823}
824
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000825/// \brief Determine structural equivalence of two fields.
826static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
827 FieldDecl *Field1, FieldDecl *Field2) {
828 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
829
830 if (!IsStructurallyEquivalent(Context,
831 Field1->getType(), Field2->getType())) {
832 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
833 << Context.C2.getTypeDeclType(Owner2);
834 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
835 << Field2->getDeclName() << Field2->getType();
836 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
837 << Field1->getDeclName() << Field1->getType();
838 return false;
839 }
840
841 if (Field1->isBitField() != Field2->isBitField()) {
842 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
843 << Context.C2.getTypeDeclType(Owner2);
844 if (Field1->isBitField()) {
845 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
846 << Field1->getDeclName() << Field1->getType()
847 << Field1->getBitWidthValue(Context.C1);
848 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
849 << Field2->getDeclName();
850 } else {
851 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
852 << Field2->getDeclName() << Field2->getType()
853 << Field2->getBitWidthValue(Context.C2);
854 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
855 << Field1->getDeclName();
856 }
857 return false;
858 }
859
860 if (Field1->isBitField()) {
861 // Make sure that the bit-fields are the same length.
862 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
863 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
864
865 if (Bits1 != Bits2) {
866 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
867 << Context.C2.getTypeDeclType(Owner2);
868 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
869 << Field2->getDeclName() << Field2->getType() << Bits2;
870 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
871 << Field1->getDeclName() << Field1->getType() << Bits1;
872 return false;
873 }
874 }
875
876 return true;
877}
878
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000879/// \brief Determine structural equivalence of two records.
880static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
881 RecordDecl *D1, RecordDecl *D2) {
882 if (D1->isUnion() != D2->isUnion()) {
883 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
884 << Context.C2.getTypeDeclType(D2);
885 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
886 << D1->getDeclName() << (unsigned)D1->getTagKind();
887 return false;
888 }
889
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000890 // If both declarations are class template specializations, we know
891 // the ODR applies, so check the template and template arguments.
892 ClassTemplateSpecializationDecl *Spec1
893 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
894 ClassTemplateSpecializationDecl *Spec2
895 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
896 if (Spec1 && Spec2) {
897 // Check that the specialized templates are the same.
898 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
899 Spec2->getSpecializedTemplate()))
900 return false;
901
902 // Check that the template arguments are the same.
903 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
904 return false;
905
906 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
907 if (!IsStructurallyEquivalent(Context,
908 Spec1->getTemplateArgs().get(I),
909 Spec2->getTemplateArgs().get(I)))
910 return false;
911 }
912 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000913 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000914 else if (Spec1 || Spec2)
915 return false;
916
Douglas Gregorea35d112010-02-15 23:54:17 +0000917 // Compare the definitions of these two records. If either or both are
918 // incomplete, we assume that they are equivalent.
919 D1 = D1->getDefinition();
920 D2 = D2->getDefinition();
921 if (!D1 || !D2)
922 return true;
923
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000924 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
925 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
926 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
927 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000928 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000929 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000930 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000931 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000932 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000933 return false;
934 }
935
936 // Check the base classes.
937 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
938 BaseEnd1 = D1CXX->bases_end(),
939 Base2 = D2CXX->bases_begin();
940 Base1 != BaseEnd1;
941 ++Base1, ++Base2) {
942 if (!IsStructurallyEquivalent(Context,
943 Base1->getType(), Base2->getType())) {
944 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
945 << Context.C2.getTypeDeclType(D2);
946 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
947 << Base2->getType()
948 << Base2->getSourceRange();
949 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
950 << Base1->getType()
951 << Base1->getSourceRange();
952 return false;
953 }
954
955 // Check virtual vs. non-virtual inheritance mismatch.
956 if (Base1->isVirtual() != Base2->isVirtual()) {
957 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
958 << Context.C2.getTypeDeclType(D2);
959 Context.Diag2(Base2->getSourceRange().getBegin(),
960 diag::note_odr_virtual_base)
961 << Base2->isVirtual() << Base2->getSourceRange();
962 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
963 << Base1->isVirtual()
964 << Base1->getSourceRange();
965 return false;
966 }
967 }
968 } else if (D1CXX->getNumBases() > 0) {
969 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
970 << Context.C2.getTypeDeclType(D2);
971 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
972 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
973 << Base1->getType()
974 << Base1->getSourceRange();
975 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
976 return false;
977 }
978 }
979
980 // Check the fields for consistency.
981 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
982 Field2End = D2->field_end();
983 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
984 Field1End = D1->field_end();
985 Field1 != Field1End;
986 ++Field1, ++Field2) {
987 if (Field2 == Field2End) {
988 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
989 << Context.C2.getTypeDeclType(D2);
990 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
991 << Field1->getDeclName() << Field1->getType();
992 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
993 return false;
994 }
995
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000996 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
997 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000998 }
999
1000 if (Field2 != Field2End) {
1001 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1002 << Context.C2.getTypeDeclType(D2);
1003 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1004 << Field2->getDeclName() << Field2->getType();
1005 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1006 return false;
1007 }
1008
1009 return true;
1010}
1011
1012/// \brief Determine structural equivalence of two enums.
1013static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1014 EnumDecl *D1, EnumDecl *D2) {
1015 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1016 EC2End = D2->enumerator_end();
1017 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1018 EC1End = D1->enumerator_end();
1019 EC1 != EC1End; ++EC1, ++EC2) {
1020 if (EC2 == EC2End) {
1021 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1022 << Context.C2.getTypeDeclType(D2);
1023 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1024 << EC1->getDeclName()
1025 << EC1->getInitVal().toString(10);
1026 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1027 return false;
1028 }
1029
1030 llvm::APSInt Val1 = EC1->getInitVal();
1031 llvm::APSInt Val2 = EC2->getInitVal();
1032 if (!IsSameValue(Val1, Val2) ||
1033 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1034 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1035 << Context.C2.getTypeDeclType(D2);
1036 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1037 << EC2->getDeclName()
1038 << EC2->getInitVal().toString(10);
1039 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1040 << EC1->getDeclName()
1041 << EC1->getInitVal().toString(10);
1042 return false;
1043 }
1044 }
1045
1046 if (EC2 != EC2End) {
1047 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1048 << Context.C2.getTypeDeclType(D2);
1049 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1050 << EC2->getDeclName()
1051 << EC2->getInitVal().toString(10);
1052 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1053 return false;
1054 }
1055
1056 return true;
1057}
Douglas Gregor040afae2010-11-30 19:14:50 +00001058
1059static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1060 TemplateParameterList *Params1,
1061 TemplateParameterList *Params2) {
1062 if (Params1->size() != Params2->size()) {
1063 Context.Diag2(Params2->getTemplateLoc(),
1064 diag::err_odr_different_num_template_parameters)
1065 << Params1->size() << Params2->size();
1066 Context.Diag1(Params1->getTemplateLoc(),
1067 diag::note_odr_template_parameter_list);
1068 return false;
1069 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001070
Douglas Gregor040afae2010-11-30 19:14:50 +00001071 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1072 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1073 Context.Diag2(Params2->getParam(I)->getLocation(),
1074 diag::err_odr_different_template_parameter_kind);
1075 Context.Diag1(Params1->getParam(I)->getLocation(),
1076 diag::note_odr_template_parameter_here);
1077 return false;
1078 }
1079
1080 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1081 Params2->getParam(I))) {
1082
1083 return false;
1084 }
1085 }
1086
1087 return true;
1088}
1089
1090static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1091 TemplateTypeParmDecl *D1,
1092 TemplateTypeParmDecl *D2) {
1093 if (D1->isParameterPack() != D2->isParameterPack()) {
1094 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1095 << D2->isParameterPack();
1096 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1097 << D1->isParameterPack();
1098 return false;
1099 }
1100
1101 return true;
1102}
1103
1104static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1105 NonTypeTemplateParmDecl *D1,
1106 NonTypeTemplateParmDecl *D2) {
1107 // FIXME: Enable once we have variadic templates.
1108#if 0
1109 if (D1->isParameterPack() != D2->isParameterPack()) {
1110 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1111 << D2->isParameterPack();
1112 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1113 << D1->isParameterPack();
1114 return false;
1115 }
1116#endif
1117
1118 // Check types.
1119 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1120 Context.Diag2(D2->getLocation(),
1121 diag::err_odr_non_type_parameter_type_inconsistent)
1122 << D2->getType() << D1->getType();
1123 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1124 << D1->getType();
1125 return false;
1126 }
1127
1128 return true;
1129}
1130
1131static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1132 TemplateTemplateParmDecl *D1,
1133 TemplateTemplateParmDecl *D2) {
1134 // FIXME: Enable once we have variadic templates.
1135#if 0
1136 if (D1->isParameterPack() != D2->isParameterPack()) {
1137 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1138 << D2->isParameterPack();
1139 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1140 << D1->isParameterPack();
1141 return false;
1142 }
1143#endif
1144
1145 // Check template parameter lists.
1146 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1147 D2->getTemplateParameters());
1148}
1149
1150static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1151 ClassTemplateDecl *D1,
1152 ClassTemplateDecl *D2) {
1153 // Check template parameters.
1154 if (!IsStructurallyEquivalent(Context,
1155 D1->getTemplateParameters(),
1156 D2->getTemplateParameters()))
1157 return false;
1158
1159 // Check the templated declaration.
1160 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1161 D2->getTemplatedDecl());
1162}
1163
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001164/// \brief Determine structural equivalence of two declarations.
1165static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1166 Decl *D1, Decl *D2) {
1167 // FIXME: Check for known structural equivalences via a callback of some sort.
1168
Douglas Gregorea35d112010-02-15 23:54:17 +00001169 // Check whether we already know that these two declarations are not
1170 // structurally equivalent.
1171 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1172 D2->getCanonicalDecl())))
1173 return false;
1174
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001175 // Determine whether we've already produced a tentative equivalence for D1.
1176 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1177 if (EquivToD1)
1178 return EquivToD1 == D2->getCanonicalDecl();
1179
1180 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1181 EquivToD1 = D2->getCanonicalDecl();
1182 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1183 return true;
1184}
1185
1186bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1187 Decl *D2) {
1188 if (!::IsStructurallyEquivalent(*this, D1, D2))
1189 return false;
1190
1191 return !Finish();
1192}
1193
1194bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1195 QualType T2) {
1196 if (!::IsStructurallyEquivalent(*this, T1, T2))
1197 return false;
1198
1199 return !Finish();
1200}
1201
1202bool StructuralEquivalenceContext::Finish() {
1203 while (!DeclsToCheck.empty()) {
1204 // Check the next declaration.
1205 Decl *D1 = DeclsToCheck.front();
1206 DeclsToCheck.pop_front();
1207
1208 Decl *D2 = TentativeEquivalences[D1];
1209 assert(D2 && "Unrecorded tentative equivalence?");
1210
Douglas Gregorea35d112010-02-15 23:54:17 +00001211 bool Equivalent = true;
1212
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001213 // FIXME: Switch on all declaration kinds. For now, we're just going to
1214 // check the obvious ones.
1215 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1216 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1217 // Check for equivalent structure names.
1218 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001219 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1220 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001221 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001222 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1223 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001224 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1225 !::IsStructurallyEquivalent(*this, Record1, Record2))
1226 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001227 } else {
1228 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001229 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001230 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001231 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001232 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1233 // Check for equivalent enum names.
1234 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001235 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1236 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001237 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001238 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1239 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001240 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1241 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1242 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001243 } else {
1244 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001245 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001246 }
Richard Smith162e1c12011-04-15 14:24:37 +00001247 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1248 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001249 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001250 Typedef2->getIdentifier()) ||
1251 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001252 Typedef1->getUnderlyingType(),
1253 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001254 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001255 } else {
1256 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001257 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001258 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001259 } else if (ClassTemplateDecl *ClassTemplate1
1260 = dyn_cast<ClassTemplateDecl>(D1)) {
1261 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1262 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1263 ClassTemplate2->getIdentifier()) ||
1264 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1265 Equivalent = false;
1266 } else {
1267 // Class template/non-class-template mismatch.
1268 Equivalent = false;
1269 }
1270 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1271 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1272 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1273 Equivalent = false;
1274 } else {
1275 // Kind mismatch.
1276 Equivalent = false;
1277 }
1278 } else if (NonTypeTemplateParmDecl *NTTP1
1279 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1280 if (NonTypeTemplateParmDecl *NTTP2
1281 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1282 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1283 Equivalent = false;
1284 } else {
1285 // Kind mismatch.
1286 Equivalent = false;
1287 }
1288 } else if (TemplateTemplateParmDecl *TTP1
1289 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1290 if (TemplateTemplateParmDecl *TTP2
1291 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1292 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1293 Equivalent = false;
1294 } else {
1295 // Kind mismatch.
1296 Equivalent = false;
1297 }
1298 }
1299
Douglas Gregorea35d112010-02-15 23:54:17 +00001300 if (!Equivalent) {
1301 // Note that these two declarations are not equivalent (and we already
1302 // know about it).
1303 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1304 D2->getCanonicalDecl()));
1305 return true;
1306 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001307 // FIXME: Check other declaration kinds!
1308 }
1309
1310 return false;
1311}
1312
1313//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001314// Import Types
1315//----------------------------------------------------------------------------
1316
John McCallf4c73712011-01-19 06:33:43 +00001317QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001318 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1319 << T->getTypeClassName();
1320 return QualType();
1321}
1322
John McCallf4c73712011-01-19 06:33:43 +00001323QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001324 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001325#define SHARED_SINGLETON_TYPE(Expansion)
1326#define BUILTIN_TYPE(Id, SingletonId) \
1327 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1328#include "clang/AST/BuiltinTypes.def"
1329
1330 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1331 // context supports C++.
1332
1333 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1334 // context supports ObjC.
1335
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001336 case BuiltinType::Char_U:
1337 // The context we're importing from has an unsigned 'char'. If we're
1338 // importing into a context with a signed 'char', translate to
1339 // 'unsigned char' instead.
1340 if (Importer.getToContext().getLangOptions().CharIsSigned)
1341 return Importer.getToContext().UnsignedCharTy;
1342
1343 return Importer.getToContext().CharTy;
1344
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001345 case BuiltinType::Char_S:
1346 // The context we're importing from has an unsigned 'char'. If we're
1347 // importing into a context with a signed 'char', translate to
1348 // 'unsigned char' instead.
1349 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1350 return Importer.getToContext().SignedCharTy;
1351
1352 return Importer.getToContext().CharTy;
1353
Chris Lattner3f59c972010-12-25 23:25:43 +00001354 case BuiltinType::WChar_S:
1355 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001356 // FIXME: If not in C++, shall we translate to the C equivalent of
1357 // wchar_t?
1358 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001359 }
1360
1361 return QualType();
1362}
1363
John McCallf4c73712011-01-19 06:33:43 +00001364QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001365 QualType ToElementType = Importer.Import(T->getElementType());
1366 if (ToElementType.isNull())
1367 return QualType();
1368
1369 return Importer.getToContext().getComplexType(ToElementType);
1370}
1371
John McCallf4c73712011-01-19 06:33:43 +00001372QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001373 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1374 if (ToPointeeType.isNull())
1375 return QualType();
1376
1377 return Importer.getToContext().getPointerType(ToPointeeType);
1378}
1379
John McCallf4c73712011-01-19 06:33:43 +00001380QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001381 // FIXME: Check for blocks support in "to" context.
1382 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1383 if (ToPointeeType.isNull())
1384 return QualType();
1385
1386 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1387}
1388
John McCallf4c73712011-01-19 06:33:43 +00001389QualType
1390ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001391 // FIXME: Check for C++ support in "to" context.
1392 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1393 if (ToPointeeType.isNull())
1394 return QualType();
1395
1396 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1397}
1398
John McCallf4c73712011-01-19 06:33:43 +00001399QualType
1400ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001401 // FIXME: Check for C++0x support in "to" context.
1402 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1403 if (ToPointeeType.isNull())
1404 return QualType();
1405
1406 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1407}
1408
John McCallf4c73712011-01-19 06:33:43 +00001409QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001410 // FIXME: Check for C++ support in "to" context.
1411 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1412 if (ToPointeeType.isNull())
1413 return QualType();
1414
1415 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1416 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1417 ClassType.getTypePtr());
1418}
1419
John McCallf4c73712011-01-19 06:33:43 +00001420QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421 QualType ToElementType = Importer.Import(T->getElementType());
1422 if (ToElementType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getConstantArrayType(ToElementType,
1426 T->getSize(),
1427 T->getSizeModifier(),
1428 T->getIndexTypeCVRQualifiers());
1429}
1430
John McCallf4c73712011-01-19 06:33:43 +00001431QualType
1432ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001433 QualType ToElementType = Importer.Import(T->getElementType());
1434 if (ToElementType.isNull())
1435 return QualType();
1436
1437 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1438 T->getSizeModifier(),
1439 T->getIndexTypeCVRQualifiers());
1440}
1441
John McCallf4c73712011-01-19 06:33:43 +00001442QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001443 QualType ToElementType = Importer.Import(T->getElementType());
1444 if (ToElementType.isNull())
1445 return QualType();
1446
1447 Expr *Size = Importer.Import(T->getSizeExpr());
1448 if (!Size)
1449 return QualType();
1450
1451 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1452 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1453 T->getSizeModifier(),
1454 T->getIndexTypeCVRQualifiers(),
1455 Brackets);
1456}
1457
John McCallf4c73712011-01-19 06:33:43 +00001458QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001459 QualType ToElementType = Importer.Import(T->getElementType());
1460 if (ToElementType.isNull())
1461 return QualType();
1462
1463 return Importer.getToContext().getVectorType(ToElementType,
1464 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001465 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001466}
1467
John McCallf4c73712011-01-19 06:33:43 +00001468QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001469 QualType ToElementType = Importer.Import(T->getElementType());
1470 if (ToElementType.isNull())
1471 return QualType();
1472
1473 return Importer.getToContext().getExtVectorType(ToElementType,
1474 T->getNumElements());
1475}
1476
John McCallf4c73712011-01-19 06:33:43 +00001477QualType
1478ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001479 // FIXME: What happens if we're importing a function without a prototype
1480 // into C++? Should we make it variadic?
1481 QualType ToResultType = Importer.Import(T->getResultType());
1482 if (ToResultType.isNull())
1483 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001484
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001485 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001486 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001487}
1488
John McCallf4c73712011-01-19 06:33:43 +00001489QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490 QualType ToResultType = Importer.Import(T->getResultType());
1491 if (ToResultType.isNull())
1492 return QualType();
1493
1494 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001495 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001496 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1497 AEnd = T->arg_type_end();
1498 A != AEnd; ++A) {
1499 QualType ArgType = Importer.Import(*A);
1500 if (ArgType.isNull())
1501 return QualType();
1502 ArgTypes.push_back(ArgType);
1503 }
1504
1505 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001506 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001507 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1508 EEnd = T->exception_end();
1509 E != EEnd; ++E) {
1510 QualType ExceptionType = Importer.Import(*E);
1511 if (ExceptionType.isNull())
1512 return QualType();
1513 ExceptionTypes.push_back(ExceptionType);
1514 }
John McCalle23cf432010-12-14 08:05:40 +00001515
1516 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1517 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001518
1519 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001520 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001521}
1522
Sean Callanan0aeb2892011-08-11 16:56:07 +00001523QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1524 QualType ToInnerType = Importer.Import(T->getInnerType());
1525 if (ToInnerType.isNull())
1526 return QualType();
1527
1528 return Importer.getToContext().getParenType(ToInnerType);
1529}
1530
John McCallf4c73712011-01-19 06:33:43 +00001531QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001532 TypedefNameDecl *ToDecl
1533 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001534 if (!ToDecl)
1535 return QualType();
1536
1537 return Importer.getToContext().getTypeDeclType(ToDecl);
1538}
1539
John McCallf4c73712011-01-19 06:33:43 +00001540QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001541 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1542 if (!ToExpr)
1543 return QualType();
1544
1545 return Importer.getToContext().getTypeOfExprType(ToExpr);
1546}
1547
John McCallf4c73712011-01-19 06:33:43 +00001548QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001549 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1550 if (ToUnderlyingType.isNull())
1551 return QualType();
1552
1553 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1554}
1555
John McCallf4c73712011-01-19 06:33:43 +00001556QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001557 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001558 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1559 if (!ToExpr)
1560 return QualType();
1561
1562 return Importer.getToContext().getDecltypeType(ToExpr);
1563}
1564
Sean Huntca63c202011-05-24 22:41:36 +00001565QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1566 QualType ToBaseType = Importer.Import(T->getBaseType());
1567 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1568 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1569 return QualType();
1570
1571 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1572 ToUnderlyingType,
1573 T->getUTTKind());
1574}
1575
Richard Smith34b41d92011-02-20 03:19:35 +00001576QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1577 // FIXME: Make sure that the "to" context supports C++0x!
1578 QualType FromDeduced = T->getDeducedType();
1579 QualType ToDeduced;
1580 if (!FromDeduced.isNull()) {
1581 ToDeduced = Importer.Import(FromDeduced);
1582 if (ToDeduced.isNull())
1583 return QualType();
1584 }
1585
1586 return Importer.getToContext().getAutoType(ToDeduced);
1587}
1588
John McCallf4c73712011-01-19 06:33:43 +00001589QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001590 RecordDecl *ToDecl
1591 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1592 if (!ToDecl)
1593 return QualType();
1594
1595 return Importer.getToContext().getTagDeclType(ToDecl);
1596}
1597
John McCallf4c73712011-01-19 06:33:43 +00001598QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001599 EnumDecl *ToDecl
1600 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1601 if (!ToDecl)
1602 return QualType();
1603
1604 return Importer.getToContext().getTagDeclType(ToDecl);
1605}
1606
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001607QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001608 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001609 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1610 if (ToTemplate.isNull())
1611 return QualType();
1612
Chris Lattner5f9e2722011-07-23 10:55:15 +00001613 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001614 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1615 return QualType();
1616
1617 QualType ToCanonType;
1618 if (!QualType(T, 0).isCanonical()) {
1619 QualType FromCanonType
1620 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1621 ToCanonType =Importer.Import(FromCanonType);
1622 if (ToCanonType.isNull())
1623 return QualType();
1624 }
1625 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1626 ToTemplateArgs.data(),
1627 ToTemplateArgs.size(),
1628 ToCanonType);
1629}
1630
John McCallf4c73712011-01-19 06:33:43 +00001631QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001632 NestedNameSpecifier *ToQualifier = 0;
1633 // Note: the qualifier in an ElaboratedType is optional.
1634 if (T->getQualifier()) {
1635 ToQualifier = Importer.Import(T->getQualifier());
1636 if (!ToQualifier)
1637 return QualType();
1638 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001639
1640 QualType ToNamedType = Importer.Import(T->getNamedType());
1641 if (ToNamedType.isNull())
1642 return QualType();
1643
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001644 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1645 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001646}
1647
John McCallf4c73712011-01-19 06:33:43 +00001648QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001649 ObjCInterfaceDecl *Class
1650 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1651 if (!Class)
1652 return QualType();
1653
John McCallc12c5bb2010-05-15 11:32:37 +00001654 return Importer.getToContext().getObjCInterfaceType(Class);
1655}
1656
John McCallf4c73712011-01-19 06:33:43 +00001657QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001658 QualType ToBaseType = Importer.Import(T->getBaseType());
1659 if (ToBaseType.isNull())
1660 return QualType();
1661
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001663 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001664 PEnd = T->qual_end();
1665 P != PEnd; ++P) {
1666 ObjCProtocolDecl *Protocol
1667 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1668 if (!Protocol)
1669 return QualType();
1670 Protocols.push_back(Protocol);
1671 }
1672
John McCallc12c5bb2010-05-15 11:32:37 +00001673 return Importer.getToContext().getObjCObjectType(ToBaseType,
1674 Protocols.data(),
1675 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001676}
1677
John McCallf4c73712011-01-19 06:33:43 +00001678QualType
1679ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001680 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1681 if (ToPointeeType.isNull())
1682 return QualType();
1683
John McCallc12c5bb2010-05-15 11:32:37 +00001684 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001685}
1686
Douglas Gregor089459a2010-02-08 21:09:39 +00001687//----------------------------------------------------------------------------
1688// Import Declarations
1689//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001690bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1691 DeclContext *&LexicalDC,
1692 DeclarationName &Name,
1693 SourceLocation &Loc) {
1694 // Import the context of this declaration.
1695 DC = Importer.ImportContext(D->getDeclContext());
1696 if (!DC)
1697 return true;
1698
1699 LexicalDC = DC;
1700 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1701 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1702 if (!LexicalDC)
1703 return true;
1704 }
1705
1706 // Import the name of this declaration.
1707 Name = Importer.Import(D->getDeclName());
1708 if (D->getDeclName() && !Name)
1709 return true;
1710
1711 // Import the location of this declaration.
1712 Loc = Importer.Import(D->getLocation());
1713 return false;
1714}
1715
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001716void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1717 if (!FromD)
1718 return;
1719
1720 if (!ToD) {
1721 ToD = Importer.Import(FromD);
1722 if (!ToD)
1723 return;
1724 }
1725
1726 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1727 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1728 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1729 ImportDefinition(FromRecord, ToRecord);
1730 }
1731 }
1732 return;
1733 }
1734
1735 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1736 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1737 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1738 ImportDefinition(FromEnum, ToEnum);
1739 }
1740 }
1741 return;
1742 }
1743}
1744
Abramo Bagnara25777432010-08-11 22:01:17 +00001745void
1746ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1747 DeclarationNameInfo& To) {
1748 // NOTE: To.Name and To.Loc are already imported.
1749 // We only have to import To.LocInfo.
1750 switch (To.getName().getNameKind()) {
1751 case DeclarationName::Identifier:
1752 case DeclarationName::ObjCZeroArgSelector:
1753 case DeclarationName::ObjCOneArgSelector:
1754 case DeclarationName::ObjCMultiArgSelector:
1755 case DeclarationName::CXXUsingDirective:
1756 return;
1757
1758 case DeclarationName::CXXOperatorName: {
1759 SourceRange Range = From.getCXXOperatorNameRange();
1760 To.setCXXOperatorNameRange(Importer.Import(Range));
1761 return;
1762 }
1763 case DeclarationName::CXXLiteralOperatorName: {
1764 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1765 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1766 return;
1767 }
1768 case DeclarationName::CXXConstructorName:
1769 case DeclarationName::CXXDestructorName:
1770 case DeclarationName::CXXConversionFunctionName: {
1771 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1772 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1773 return;
1774 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001775 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001776 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001777}
1778
Douglas Gregord8868a62011-01-18 03:11:38 +00001779void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1780 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001781 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001782 return;
1783 }
1784
Douglas Gregor083a8212010-02-21 18:24:45 +00001785 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1786 FromEnd = FromDC->decls_end();
1787 From != FromEnd;
1788 ++From)
1789 Importer.Import(*From);
1790}
1791
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001792bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1793 bool ForceImport) {
1794 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001795 return false;
1796
1797 To->startDefinition();
1798
1799 // Add base classes.
1800 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1801 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001802
1803 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1804 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1805 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1806 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1807 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1808 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1809 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1810 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1811 ToData.Aggregate = FromData.Aggregate;
1812 ToData.PlainOldData = FromData.PlainOldData;
1813 ToData.Empty = FromData.Empty;
1814 ToData.Polymorphic = FromData.Polymorphic;
1815 ToData.Abstract = FromData.Abstract;
1816 ToData.IsStandardLayout = FromData.IsStandardLayout;
1817 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1818 ToData.HasPrivateFields = FromData.HasPrivateFields;
1819 ToData.HasProtectedFields = FromData.HasProtectedFields;
1820 ToData.HasPublicFields = FromData.HasPublicFields;
1821 ToData.HasMutableFields = FromData.HasMutableFields;
1822 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1823 ToData.HasConstexprNonCopyMoveConstructor
1824 = FromData.HasConstexprNonCopyMoveConstructor;
1825 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1826 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1827 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1828 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1829 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1830 ToData.HasNonLiteralTypeFieldsOrBases
1831 = FromData.HasNonLiteralTypeFieldsOrBases;
1832 ToData.UserProvidedDefaultConstructor
1833 = FromData.UserProvidedDefaultConstructor;
1834 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1835 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1836 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1837 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1838 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1839 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1840 ToData.FailedImplicitMoveConstructor
1841 = FromData.FailedImplicitMoveConstructor;
1842 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001843
Chris Lattner5f9e2722011-07-23 10:55:15 +00001844 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001845 for (CXXRecordDecl::base_class_iterator
1846 Base1 = FromCXX->bases_begin(),
1847 FromBaseEnd = FromCXX->bases_end();
1848 Base1 != FromBaseEnd;
1849 ++Base1) {
1850 QualType T = Importer.Import(Base1->getType());
1851 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001852 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001853
1854 SourceLocation EllipsisLoc;
1855 if (Base1->isPackExpansion())
1856 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001857
1858 // Ensure that we have a definition for the base.
1859 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1860
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001861 Bases.push_back(
1862 new (Importer.getToContext())
1863 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1864 Base1->isVirtual(),
1865 Base1->isBaseOfClass(),
1866 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001867 Importer.Import(Base1->getTypeSourceInfo()),
1868 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001869 }
1870 if (!Bases.empty())
1871 ToCXX->setBases(Bases.data(), Bases.size());
1872 }
1873
Sean Callanan673e7752011-07-19 22:38:25 +00001874 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001875 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001876 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001877}
1878
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001879bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1880 bool ForceImport) {
1881 if (To->getDefinition() || To->isBeingDefined())
1882 return false;
1883
1884 To->startDefinition();
1885
1886 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1887 if (T.isNull())
1888 return true;
1889
1890 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1891 if (ToPromotionType.isNull())
1892 return true;
1893
1894 ImportDeclContext(From, ForceImport);
1895
1896 // FIXME: we might need to merge the number of positive or negative bits
1897 // if the enumerator lists don't match.
1898 To->completeDefinition(T, ToPromotionType,
1899 From->getNumPositiveBits(),
1900 From->getNumNegativeBits());
1901 return false;
1902}
1903
Douglas Gregor040afae2010-11-30 19:14:50 +00001904TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1905 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001907 ToParams.reserve(Params->size());
1908 for (TemplateParameterList::iterator P = Params->begin(),
1909 PEnd = Params->end();
1910 P != PEnd; ++P) {
1911 Decl *To = Importer.Import(*P);
1912 if (!To)
1913 return 0;
1914
1915 ToParams.push_back(cast<NamedDecl>(To));
1916 }
1917
1918 return TemplateParameterList::Create(Importer.getToContext(),
1919 Importer.Import(Params->getTemplateLoc()),
1920 Importer.Import(Params->getLAngleLoc()),
1921 ToParams.data(), ToParams.size(),
1922 Importer.Import(Params->getRAngleLoc()));
1923}
1924
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001925TemplateArgument
1926ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1927 switch (From.getKind()) {
1928 case TemplateArgument::Null:
1929 return TemplateArgument();
1930
1931 case TemplateArgument::Type: {
1932 QualType ToType = Importer.Import(From.getAsType());
1933 if (ToType.isNull())
1934 return TemplateArgument();
1935 return TemplateArgument(ToType);
1936 }
1937
1938 case TemplateArgument::Integral: {
1939 QualType ToType = Importer.Import(From.getIntegralType());
1940 if (ToType.isNull())
1941 return TemplateArgument();
1942 return TemplateArgument(*From.getAsIntegral(), ToType);
1943 }
1944
1945 case TemplateArgument::Declaration:
1946 if (Decl *To = Importer.Import(From.getAsDecl()))
1947 return TemplateArgument(To);
1948 return TemplateArgument();
1949
1950 case TemplateArgument::Template: {
1951 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1952 if (ToTemplate.isNull())
1953 return TemplateArgument();
1954
1955 return TemplateArgument(ToTemplate);
1956 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001957
1958 case TemplateArgument::TemplateExpansion: {
1959 TemplateName ToTemplate
1960 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1961 if (ToTemplate.isNull())
1962 return TemplateArgument();
1963
Douglas Gregor2be29f42011-01-14 23:41:42 +00001964 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001965 }
1966
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001967 case TemplateArgument::Expression:
1968 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1969 return TemplateArgument(ToExpr);
1970 return TemplateArgument();
1971
1972 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001974 ToPack.reserve(From.pack_size());
1975 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1976 return TemplateArgument();
1977
1978 TemplateArgument *ToArgs
1979 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1980 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1981 return TemplateArgument(ToArgs, ToPack.size());
1982 }
1983 }
1984
1985 llvm_unreachable("Invalid template argument kind");
1986 return TemplateArgument();
1987}
1988
1989bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1990 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001991 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001992 for (unsigned I = 0; I != NumFromArgs; ++I) {
1993 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1994 if (To.isNull() && !FromArgs[I].isNull())
1995 return true;
1996
1997 ToArgs.push_back(To);
1998 }
1999
2000 return false;
2001}
2002
Douglas Gregor96a01b42010-02-11 00:48:18 +00002003bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002004 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002005 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002007 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002008 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002009}
2010
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002011bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002012 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002013 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002014 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002015 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002016}
2017
Douglas Gregor040afae2010-11-30 19:14:50 +00002018bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2019 ClassTemplateDecl *To) {
2020 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2021 Importer.getToContext(),
2022 Importer.getNonEquivalentDecls());
2023 return Ctx.IsStructurallyEquivalent(From, To);
2024}
2025
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002026Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002027 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002028 << D->getDeclKindName();
2029 return 0;
2030}
2031
Sean Callananf1b69462011-11-17 23:20:56 +00002032Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2033 TranslationUnitDecl *ToD =
2034 Importer.getToContext().getTranslationUnitDecl();
2035
2036 Importer.Imported(D, ToD);
2037
2038 return ToD;
2039}
2040
Douglas Gregor788c62d2010-02-21 18:26:36 +00002041Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2042 // Import the major distinguishing characteristics of this namespace.
2043 DeclContext *DC, *LexicalDC;
2044 DeclarationName Name;
2045 SourceLocation Loc;
2046 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2047 return 0;
2048
2049 NamespaceDecl *MergeWithNamespace = 0;
2050 if (!Name) {
2051 // This is an anonymous namespace. Adopt an existing anonymous
2052 // namespace if we can.
2053 // FIXME: Not testable.
2054 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2055 MergeWithNamespace = TU->getAnonymousNamespace();
2056 else
2057 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2058 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002059 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002060 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2061 DC->localUncachedLookup(Name, FoundDecls);
2062 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2063 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002064 continue;
2065
Douglas Gregorb75a3452011-10-15 00:10:27 +00002066 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002067 MergeWithNamespace = FoundNS;
2068 ConflictingDecls.clear();
2069 break;
2070 }
2071
Douglas Gregorb75a3452011-10-15 00:10:27 +00002072 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002073 }
2074
2075 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002076 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002077 ConflictingDecls.data(),
2078 ConflictingDecls.size());
2079 }
2080 }
2081
2082 // Create the "to" namespace, if needed.
2083 NamespaceDecl *ToNamespace = MergeWithNamespace;
2084 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002085 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2086 Importer.Import(D->getLocStart()),
2087 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002088 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002089 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002090
2091 // If this is an anonymous namespace, register it as the anonymous
2092 // namespace within its context.
2093 if (!Name) {
2094 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2095 TU->setAnonymousNamespace(ToNamespace);
2096 else
2097 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2098 }
2099 }
2100 Importer.Imported(D, ToNamespace);
2101
2102 ImportDeclContext(D);
2103
2104 return ToNamespace;
2105}
2106
Richard Smith162e1c12011-04-15 14:24:37 +00002107Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002108 // Import the major distinguishing characteristics of this typedef.
2109 DeclContext *DC, *LexicalDC;
2110 DeclarationName Name;
2111 SourceLocation Loc;
2112 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2113 return 0;
2114
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002115 // If this typedef is not in block scope, determine whether we've
2116 // seen a typedef with the same name (that we can merge with) or any
2117 // other entity by that name (which name lookup could conflict with).
2118 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002119 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002120 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002121 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2122 DC->localUncachedLookup(Name, FoundDecls);
2123 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2124 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002125 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002126 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002127 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002128 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2129 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002130 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002131 }
2132
Douglas Gregorb75a3452011-10-15 00:10:27 +00002133 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002134 }
2135
2136 if (!ConflictingDecls.empty()) {
2137 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2138 ConflictingDecls.data(),
2139 ConflictingDecls.size());
2140 if (!Name)
2141 return 0;
2142 }
2143 }
2144
Douglas Gregorea35d112010-02-15 23:54:17 +00002145 // Import the underlying type of this typedef;
2146 QualType T = Importer.Import(D->getUnderlyingType());
2147 if (T.isNull())
2148 return 0;
2149
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002150 // Create the new typedef node.
2151 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002152 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002153 TypedefNameDecl *ToTypedef;
2154 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002155 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2156 StartL, Loc,
2157 Name.getAsIdentifierInfo(),
2158 TInfo);
2159 else
Richard Smith162e1c12011-04-15 14:24:37 +00002160 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2161 StartL, Loc,
2162 Name.getAsIdentifierInfo(),
2163 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002164
Douglas Gregor325bf172010-02-22 17:42:47 +00002165 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002166 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002167 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002168 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002169
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002170 return ToTypedef;
2171}
2172
Richard Smith162e1c12011-04-15 14:24:37 +00002173Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2174 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2175}
2176
2177Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2178 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2179}
2180
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002181Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2182 // Import the major distinguishing characteristics of this enum.
2183 DeclContext *DC, *LexicalDC;
2184 DeclarationName Name;
2185 SourceLocation Loc;
2186 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2187 return 0;
2188
2189 // Figure out what enum name we're looking for.
2190 unsigned IDNS = Decl::IDNS_Tag;
2191 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002192 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2193 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002194 IDNS = Decl::IDNS_Ordinary;
2195 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2196 IDNS |= Decl::IDNS_Ordinary;
2197
2198 // We may already have an enum of the same name; try to find and match it.
2199 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002200 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002201 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2202 DC->localUncachedLookup(SearchName, FoundDecls);
2203 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2204 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002205 continue;
2206
Douglas Gregorb75a3452011-10-15 00:10:27 +00002207 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002208 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002209 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2210 Found = Tag->getDecl();
2211 }
2212
2213 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002214 if (IsStructuralMatch(D, FoundEnum))
2215 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002216 }
2217
Douglas Gregorb75a3452011-10-15 00:10:27 +00002218 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002219 }
2220
2221 if (!ConflictingDecls.empty()) {
2222 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2223 ConflictingDecls.data(),
2224 ConflictingDecls.size());
2225 }
2226 }
2227
2228 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002229 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2230 Importer.Import(D->getLocStart()),
2231 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002232 D->isScoped(), D->isScopedUsingClassTag(),
2233 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002234 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002235 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002236 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002237 D2->setLexicalDeclContext(LexicalDC);
2238 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002239 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002240
2241 // Import the integer type.
2242 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2243 if (ToIntegerType.isNull())
2244 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002245 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002246
2247 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002248 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002249 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002250
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002251 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002252}
2253
Douglas Gregor96a01b42010-02-11 00:48:18 +00002254Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2255 // If this record has a definition in the translation unit we're coming from,
2256 // but this particular declaration is not that definition, import the
2257 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002258 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002259 if (Definition && Definition != D) {
2260 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002261 if (!ImportedDef)
2262 return 0;
2263
2264 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002265 }
2266
2267 // Import the major distinguishing characteristics of this record.
2268 DeclContext *DC, *LexicalDC;
2269 DeclarationName Name;
2270 SourceLocation Loc;
2271 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2272 return 0;
2273
2274 // Figure out what structure name we're looking for.
2275 unsigned IDNS = Decl::IDNS_Tag;
2276 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002277 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2278 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002279 IDNS = Decl::IDNS_Ordinary;
2280 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2281 IDNS |= Decl::IDNS_Ordinary;
2282
2283 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002284 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002285 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002286 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002287 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2288 DC->localUncachedLookup(SearchName, FoundDecls);
2289 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2290 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002291 continue;
2292
Douglas Gregorb75a3452011-10-15 00:10:27 +00002293 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002294 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002295 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2296 Found = Tag->getDecl();
2297 }
2298
2299 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002300 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002301 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002302 // The record types structurally match, or the "from" translation
2303 // unit only had a forward declaration anyway; call it the same
2304 // function.
2305 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002306 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002307 }
2308 } else {
2309 // We have a forward declaration of this type, so adopt that forward
2310 // declaration rather than building a new one.
2311 AdoptDecl = FoundRecord;
2312 continue;
2313 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002314 }
2315
Douglas Gregorb75a3452011-10-15 00:10:27 +00002316 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002317 }
2318
2319 if (!ConflictingDecls.empty()) {
2320 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2321 ConflictingDecls.data(),
2322 ConflictingDecls.size());
2323 }
2324 }
2325
2326 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002327 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002328 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002329 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002330 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002332 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002333 DC, StartLoc, Loc,
2334 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002335 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002336 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002337 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002338 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002339 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002340 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002341
2342 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002343 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002344 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002345 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002346
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002347 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002348
John McCall5e1cdac2011-10-07 06:10:15 +00002349 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002350 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002351
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002352 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002353}
2354
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002355Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2356 // Import the major distinguishing characteristics of this enumerator.
2357 DeclContext *DC, *LexicalDC;
2358 DeclarationName Name;
2359 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002360 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002361 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002362
2363 QualType T = Importer.Import(D->getType());
2364 if (T.isNull())
2365 return 0;
2366
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002367 // Determine whether there are any other declarations with the same name and
2368 // in the same context.
2369 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002370 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002371 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002372 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2373 DC->localUncachedLookup(Name, FoundDecls);
2374 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2375 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002376 continue;
2377
Douglas Gregorb75a3452011-10-15 00:10:27 +00002378 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002379 }
2380
2381 if (!ConflictingDecls.empty()) {
2382 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2383 ConflictingDecls.data(),
2384 ConflictingDecls.size());
2385 if (!Name)
2386 return 0;
2387 }
2388 }
2389
2390 Expr *Init = Importer.Import(D->getInitExpr());
2391 if (D->getInitExpr() && !Init)
2392 return 0;
2393
2394 EnumConstantDecl *ToEnumerator
2395 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2396 Name.getAsIdentifierInfo(), T,
2397 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002398 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002399 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002400 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002401 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002402 return ToEnumerator;
2403}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002404
Douglas Gregora404ea62010-02-10 19:54:31 +00002405Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2406 // Import the major distinguishing characteristics of this function.
2407 DeclContext *DC, *LexicalDC;
2408 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002409 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002410 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002411 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002412
Douglas Gregora404ea62010-02-10 19:54:31 +00002413 // Try to find a function in our own ("to") context with the same name, same
2414 // type, and in the same context as the function we're importing.
2415 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002416 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002417 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002418 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2419 DC->localUncachedLookup(Name, FoundDecls);
2420 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2421 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002422 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002423
Douglas Gregorb75a3452011-10-15 00:10:27 +00002424 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002425 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2426 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002427 if (Importer.IsStructurallyEquivalent(D->getType(),
2428 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002429 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002430 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002431 }
2432
2433 // FIXME: Check for overloading more carefully, e.g., by boosting
2434 // Sema::IsOverload out to the AST library.
2435
2436 // Function overloading is okay in C++.
2437 if (Importer.getToContext().getLangOptions().CPlusPlus)
2438 continue;
2439
2440 // Complain about inconsistent function types.
2441 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002442 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002443 Importer.ToDiag(FoundFunction->getLocation(),
2444 diag::note_odr_value_here)
2445 << FoundFunction->getType();
2446 }
2447 }
2448
Douglas Gregorb75a3452011-10-15 00:10:27 +00002449 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002450 }
2451
2452 if (!ConflictingDecls.empty()) {
2453 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2454 ConflictingDecls.data(),
2455 ConflictingDecls.size());
2456 if (!Name)
2457 return 0;
2458 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002459 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002460
Abramo Bagnara25777432010-08-11 22:01:17 +00002461 DeclarationNameInfo NameInfo(Name, Loc);
2462 // Import additional name location/type info.
2463 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2464
Douglas Gregorea35d112010-02-15 23:54:17 +00002465 // Import the type.
2466 QualType T = Importer.Import(D->getType());
2467 if (T.isNull())
2468 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002469
2470 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002471 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002472 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2473 P != PEnd; ++P) {
2474 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2475 if (!ToP)
2476 return 0;
2477
2478 Parameters.push_back(ToP);
2479 }
2480
2481 // Create the imported function.
2482 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002483 FunctionDecl *ToFunction = 0;
2484 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2485 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2486 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002487 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002488 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002489 FromConstructor->isExplicit(),
2490 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002491 D->isImplicit(),
2492 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002493 } else if (isa<CXXDestructorDecl>(D)) {
2494 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2495 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002496 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002497 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002498 D->isInlineSpecified(),
2499 D->isImplicit());
2500 } else if (CXXConversionDecl *FromConversion
2501 = dyn_cast<CXXConversionDecl>(D)) {
2502 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2503 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002504 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002505 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002506 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002507 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002508 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002509 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002510 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2511 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2512 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002513 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002514 NameInfo, T, TInfo,
2515 Method->isStatic(),
2516 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002517 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002518 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002519 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002520 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002521 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002522 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002523 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002524 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002525 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002526 D->hasWrittenPrototype(),
2527 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002528 }
John McCallb6217662010-03-15 10:12:16 +00002529
2530 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002531 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002532 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002533 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002534 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2535 ToFunction->setTrivial(D->isTrivial());
2536 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002537 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002538
Douglas Gregora404ea62010-02-10 19:54:31 +00002539 // Set the parameters.
2540 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002541 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002542 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002543 }
David Blaikie4278c652011-09-21 18:16:56 +00002544 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002545
2546 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002547
2548 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002549 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002550
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002551 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002552}
2553
Douglas Gregorc144f352010-02-21 18:29:16 +00002554Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2555 return VisitFunctionDecl(D);
2556}
2557
2558Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2559 return VisitCXXMethodDecl(D);
2560}
2561
2562Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2563 return VisitCXXMethodDecl(D);
2564}
2565
2566Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2567 return VisitCXXMethodDecl(D);
2568}
2569
Douglas Gregor96a01b42010-02-11 00:48:18 +00002570Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2571 // Import the major distinguishing characteristics of a variable.
2572 DeclContext *DC, *LexicalDC;
2573 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002574 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002575 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2576 return 0;
2577
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002578 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002579 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2580 DC->localUncachedLookup(Name, FoundDecls);
2581 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2582 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002583 if (Importer.IsStructurallyEquivalent(D->getType(),
2584 FoundField->getType())) {
2585 Importer.Imported(D, FoundField);
2586 return FoundField;
2587 }
2588
2589 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2590 << Name << D->getType() << FoundField->getType();
2591 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2592 << FoundField->getType();
2593 return 0;
2594 }
2595 }
2596
Douglas Gregorea35d112010-02-15 23:54:17 +00002597 // Import the type.
2598 QualType T = Importer.Import(D->getType());
2599 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002600 return 0;
2601
2602 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2603 Expr *BitWidth = Importer.Import(D->getBitWidth());
2604 if (!BitWidth && D->getBitWidth())
2605 return 0;
2606
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002607 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2608 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002609 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002610 T, TInfo, BitWidth, D->isMutable(),
2611 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002612 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002613 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002614 if (ToField->hasInClassInitializer())
2615 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002616 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002617 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002618 return ToField;
2619}
2620
Francois Pichet87c2e122010-11-21 06:08:52 +00002621Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2622 // Import the major distinguishing characteristics of a variable.
2623 DeclContext *DC, *LexicalDC;
2624 DeclarationName Name;
2625 SourceLocation Loc;
2626 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2627 return 0;
2628
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002629 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002630 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2631 DC->localUncachedLookup(Name, FoundDecls);
2632 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002633 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002634 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002635 if (Importer.IsStructurallyEquivalent(D->getType(),
2636 FoundField->getType())) {
2637 Importer.Imported(D, FoundField);
2638 return FoundField;
2639 }
2640
2641 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2642 << Name << D->getType() << FoundField->getType();
2643 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2644 << FoundField->getType();
2645 return 0;
2646 }
2647 }
2648
Francois Pichet87c2e122010-11-21 06:08:52 +00002649 // Import the type.
2650 QualType T = Importer.Import(D->getType());
2651 if (T.isNull())
2652 return 0;
2653
2654 NamedDecl **NamedChain =
2655 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2656
2657 unsigned i = 0;
2658 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2659 PE = D->chain_end(); PI != PE; ++PI) {
2660 Decl* D = Importer.Import(*PI);
2661 if (!D)
2662 return 0;
2663 NamedChain[i++] = cast<NamedDecl>(D);
2664 }
2665
2666 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2667 Importer.getToContext(), DC,
2668 Loc, Name.getAsIdentifierInfo(), T,
2669 NamedChain, D->getChainingSize());
2670 ToIndirectField->setAccess(D->getAccess());
2671 ToIndirectField->setLexicalDeclContext(LexicalDC);
2672 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002673 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002674 return ToIndirectField;
2675}
2676
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002677Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2678 // Import the major distinguishing characteristics of an ivar.
2679 DeclContext *DC, *LexicalDC;
2680 DeclarationName Name;
2681 SourceLocation Loc;
2682 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2683 return 0;
2684
2685 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002686 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2687 DC->localUncachedLookup(Name, FoundDecls);
2688 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2689 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002690 if (Importer.IsStructurallyEquivalent(D->getType(),
2691 FoundIvar->getType())) {
2692 Importer.Imported(D, FoundIvar);
2693 return FoundIvar;
2694 }
2695
2696 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2697 << Name << D->getType() << FoundIvar->getType();
2698 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2699 << FoundIvar->getType();
2700 return 0;
2701 }
2702 }
2703
2704 // Import the type.
2705 QualType T = Importer.Import(D->getType());
2706 if (T.isNull())
2707 return 0;
2708
2709 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2710 Expr *BitWidth = Importer.Import(D->getBitWidth());
2711 if (!BitWidth && D->getBitWidth())
2712 return 0;
2713
Daniel Dunbara0654922010-04-02 20:10:03 +00002714 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2715 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002716 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002717 Loc, Name.getAsIdentifierInfo(),
2718 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002719 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002720 ToIvar->setLexicalDeclContext(LexicalDC);
2721 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002722 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002723 return ToIvar;
2724
2725}
2726
Douglas Gregora404ea62010-02-10 19:54:31 +00002727Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2728 // Import the major distinguishing characteristics of a variable.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002731 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002732 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002733 return 0;
2734
Douglas Gregor089459a2010-02-08 21:09:39 +00002735 // Try to find a variable in our own ("to") context with the same name and
2736 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002737 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002738 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002739 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002740 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002741 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2742 DC->localUncachedLookup(Name, FoundDecls);
2743 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2744 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002745 continue;
2746
Douglas Gregorb75a3452011-10-15 00:10:27 +00002747 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002748 // We have found a variable that we may need to merge with. Check it.
2749 if (isExternalLinkage(FoundVar->getLinkage()) &&
2750 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002751 if (Importer.IsStructurallyEquivalent(D->getType(),
2752 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002753 MergeWithVar = FoundVar;
2754 break;
2755 }
2756
Douglas Gregord0145422010-02-12 17:23:39 +00002757 const ArrayType *FoundArray
2758 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2759 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002760 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002761 if (FoundArray && TArray) {
2762 if (isa<IncompleteArrayType>(FoundArray) &&
2763 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002764 // Import the type.
2765 QualType T = Importer.Import(D->getType());
2766 if (T.isNull())
2767 return 0;
2768
Douglas Gregord0145422010-02-12 17:23:39 +00002769 FoundVar->setType(T);
2770 MergeWithVar = FoundVar;
2771 break;
2772 } else if (isa<IncompleteArrayType>(TArray) &&
2773 isa<ConstantArrayType>(FoundArray)) {
2774 MergeWithVar = FoundVar;
2775 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002776 }
2777 }
2778
Douglas Gregor089459a2010-02-08 21:09:39 +00002779 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002780 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002781 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2782 << FoundVar->getType();
2783 }
2784 }
2785
Douglas Gregorb75a3452011-10-15 00:10:27 +00002786 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002787 }
2788
2789 if (MergeWithVar) {
2790 // An equivalent variable with external linkage has been found. Link
2791 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002792 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002793
2794 if (VarDecl *DDef = D->getDefinition()) {
2795 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2796 Importer.ToDiag(ExistingDef->getLocation(),
2797 diag::err_odr_variable_multiple_def)
2798 << Name;
2799 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2800 } else {
2801 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002802 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002803 if (DDef->isInitKnownICE()) {
2804 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2805 Eval->CheckedICE = true;
2806 Eval->IsICE = DDef->isInitICE();
2807 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002808 }
2809 }
2810
2811 return MergeWithVar;
2812 }
2813
2814 if (!ConflictingDecls.empty()) {
2815 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2816 ConflictingDecls.data(),
2817 ConflictingDecls.size());
2818 if (!Name)
2819 return 0;
2820 }
2821 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002822
Douglas Gregorea35d112010-02-15 23:54:17 +00002823 // Import the type.
2824 QualType T = Importer.Import(D->getType());
2825 if (T.isNull())
2826 return 0;
2827
Douglas Gregor089459a2010-02-08 21:09:39 +00002828 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002829 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002830 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2831 Importer.Import(D->getInnerLocStart()),
2832 Loc, Name.getAsIdentifierInfo(),
2833 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002834 D->getStorageClass(),
2835 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002836 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002837 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002838 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002839 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002840 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002841
Douglas Gregor089459a2010-02-08 21:09:39 +00002842 // Merge the initializer.
2843 // FIXME: Can we really import any initializer? Alternatively, we could force
2844 // ourselves to import every declaration of a variable and then only use
2845 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002846 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002847
2848 // FIXME: Other bits to merge?
2849
2850 return ToVar;
2851}
2852
Douglas Gregor2cd00932010-02-17 21:22:52 +00002853Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2854 // Parameters are created in the translation unit's context, then moved
2855 // into the function declaration's context afterward.
2856 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2857
2858 // Import the name of this declaration.
2859 DeclarationName Name = Importer.Import(D->getDeclName());
2860 if (D->getDeclName() && !Name)
2861 return 0;
2862
2863 // Import the location of this declaration.
2864 SourceLocation Loc = Importer.Import(D->getLocation());
2865
2866 // Import the parameter's type.
2867 QualType T = Importer.Import(D->getType());
2868 if (T.isNull())
2869 return 0;
2870
2871 // Create the imported parameter.
2872 ImplicitParamDecl *ToParm
2873 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2874 Loc, Name.getAsIdentifierInfo(),
2875 T);
2876 return Importer.Imported(D, ToParm);
2877}
2878
Douglas Gregora404ea62010-02-10 19:54:31 +00002879Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2880 // Parameters are created in the translation unit's context, then moved
2881 // into the function declaration's context afterward.
2882 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2883
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002884 // Import the name of this declaration.
2885 DeclarationName Name = Importer.Import(D->getDeclName());
2886 if (D->getDeclName() && !Name)
2887 return 0;
2888
Douglas Gregora404ea62010-02-10 19:54:31 +00002889 // Import the location of this declaration.
2890 SourceLocation Loc = Importer.Import(D->getLocation());
2891
2892 // Import the parameter's type.
2893 QualType T = Importer.Import(D->getType());
2894 if (T.isNull())
2895 return 0;
2896
2897 // Create the imported parameter.
2898 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2899 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002900 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002901 Loc, Name.getAsIdentifierInfo(),
2902 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002903 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002904 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002905 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002906 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002907}
2908
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002909Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2910 // Import the major distinguishing characteristics of a method.
2911 DeclContext *DC, *LexicalDC;
2912 DeclarationName Name;
2913 SourceLocation Loc;
2914 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2915 return 0;
2916
Douglas Gregorb75a3452011-10-15 00:10:27 +00002917 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2918 DC->localUncachedLookup(Name, FoundDecls);
2919 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2920 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002921 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2922 continue;
2923
2924 // Check return types.
2925 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2926 FoundMethod->getResultType())) {
2927 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2928 << D->isInstanceMethod() << Name
2929 << D->getResultType() << FoundMethod->getResultType();
2930 Importer.ToDiag(FoundMethod->getLocation(),
2931 diag::note_odr_objc_method_here)
2932 << D->isInstanceMethod() << Name;
2933 return 0;
2934 }
2935
2936 // Check the number of parameters.
2937 if (D->param_size() != FoundMethod->param_size()) {
2938 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2939 << D->isInstanceMethod() << Name
2940 << D->param_size() << FoundMethod->param_size();
2941 Importer.ToDiag(FoundMethod->getLocation(),
2942 diag::note_odr_objc_method_here)
2943 << D->isInstanceMethod() << Name;
2944 return 0;
2945 }
2946
2947 // Check parameter types.
2948 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2949 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2950 P != PEnd; ++P, ++FoundP) {
2951 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2952 (*FoundP)->getType())) {
2953 Importer.FromDiag((*P)->getLocation(),
2954 diag::err_odr_objc_method_param_type_inconsistent)
2955 << D->isInstanceMethod() << Name
2956 << (*P)->getType() << (*FoundP)->getType();
2957 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2958 << (*FoundP)->getType();
2959 return 0;
2960 }
2961 }
2962
2963 // Check variadic/non-variadic.
2964 // Check the number of parameters.
2965 if (D->isVariadic() != FoundMethod->isVariadic()) {
2966 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2967 << D->isInstanceMethod() << Name;
2968 Importer.ToDiag(FoundMethod->getLocation(),
2969 diag::note_odr_objc_method_here)
2970 << D->isInstanceMethod() << Name;
2971 return 0;
2972 }
2973
2974 // FIXME: Any other bits we need to merge?
2975 return Importer.Imported(D, FoundMethod);
2976 }
2977 }
2978
2979 // Import the result type.
2980 QualType ResultTy = Importer.Import(D->getResultType());
2981 if (ResultTy.isNull())
2982 return 0;
2983
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002984 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2985
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002986 ObjCMethodDecl *ToMethod
2987 = ObjCMethodDecl::Create(Importer.getToContext(),
2988 Loc,
2989 Importer.Import(D->getLocEnd()),
2990 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002991 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002992 D->isInstanceMethod(),
2993 D->isVariadic(),
2994 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002995 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002996 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002997 D->getImplementationControl(),
2998 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002999
3000 // FIXME: When we decide to merge method definitions, we'll need to
3001 // deal with implicit parameters.
3002
3003 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003005 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3006 FromPEnd = D->param_end();
3007 FromP != FromPEnd;
3008 ++FromP) {
3009 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3010 if (!ToP)
3011 return 0;
3012
3013 ToParams.push_back(ToP);
3014 }
3015
3016 // Set the parameters.
3017 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3018 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003019 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003020 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003021 SmallVector<SourceLocation, 12> SelLocs;
3022 D->getSelectorLocs(SelLocs);
3023 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003024
3025 ToMethod->setLexicalDeclContext(LexicalDC);
3026 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003027 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003028 return ToMethod;
3029}
3030
Douglas Gregorb4677b62010-02-18 01:47:50 +00003031Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3032 // Import the major distinguishing characteristics of a category.
3033 DeclContext *DC, *LexicalDC;
3034 DeclarationName Name;
3035 SourceLocation Loc;
3036 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3037 return 0;
3038
3039 ObjCInterfaceDecl *ToInterface
3040 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3041 if (!ToInterface)
3042 return 0;
3043
3044 // Determine if we've already encountered this category.
3045 ObjCCategoryDecl *MergeWithCategory
3046 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3047 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3048 if (!ToCategory) {
3049 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003050 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003051 Loc,
3052 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003053 Name.getAsIdentifierInfo(),
3054 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003055 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003056 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003057 Importer.Imported(D, ToCategory);
3058
Douglas Gregorb4677b62010-02-18 01:47:50 +00003059 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003060 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3061 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003062 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3063 = D->protocol_loc_begin();
3064 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3065 FromProtoEnd = D->protocol_end();
3066 FromProto != FromProtoEnd;
3067 ++FromProto, ++FromProtoLoc) {
3068 ObjCProtocolDecl *ToProto
3069 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3070 if (!ToProto)
3071 return 0;
3072 Protocols.push_back(ToProto);
3073 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3074 }
3075
3076 // FIXME: If we're merging, make sure that the protocol list is the same.
3077 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3078 ProtocolLocs.data(), Importer.getToContext());
3079
3080 } else {
3081 Importer.Imported(D, ToCategory);
3082 }
3083
3084 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003085 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003086
3087 // If we have an implementation, import it as well.
3088 if (D->getImplementation()) {
3089 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003090 = cast_or_null<ObjCCategoryImplDecl>(
3091 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003092 if (!Impl)
3093 return 0;
3094
3095 ToCategory->setImplementation(Impl);
3096 }
3097
3098 return ToCategory;
3099}
3100
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003101Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003102 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003103 DeclContext *DC, *LexicalDC;
3104 DeclarationName Name;
3105 SourceLocation Loc;
3106 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3107 return 0;
3108
3109 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003110 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3111 DC->localUncachedLookup(Name, FoundDecls);
3112 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3113 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003114 continue;
3115
Douglas Gregorb75a3452011-10-15 00:10:27 +00003116 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003117 break;
3118 }
3119
3120 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003121 if (!ToProto || !ToProto->hasDefinition()) {
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003122 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003123 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3124 Name.getAsIdentifierInfo(), Loc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003125 Importer.Import(D->getAtStartLoc()),
Douglas Gregor27c6da22012-01-01 20:30:41 +00003126 /*PrevDecl=*/0,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003127 D->isInitiallyForwardDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003128 ToProto->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003129 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003130 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003131 if (!ToProto->hasDefinition())
3132 ToProto->startDefinition();
3133
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003134 Importer.Imported(D, ToProto);
3135
3136 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003137 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3138 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003139 ObjCProtocolDecl::protocol_loc_iterator
3140 FromProtoLoc = D->protocol_loc_begin();
3141 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3142 FromProtoEnd = D->protocol_end();
3143 FromProto != FromProtoEnd;
3144 ++FromProto, ++FromProtoLoc) {
3145 ObjCProtocolDecl *ToProto
3146 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3147 if (!ToProto)
3148 return 0;
3149 Protocols.push_back(ToProto);
3150 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3151 }
3152
3153 // FIXME: If we're merging, make sure that the protocol list is the same.
3154 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3155 ProtocolLocs.data(), Importer.getToContext());
3156 } else {
3157 Importer.Imported(D, ToProto);
3158 }
3159
Douglas Gregorb4677b62010-02-18 01:47:50 +00003160 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003161 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003162
3163 return ToProto;
3164}
3165
Douglas Gregora12d2942010-02-16 01:20:57 +00003166Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3167 // Import the major distinguishing characteristics of an @interface.
3168 DeclContext *DC, *LexicalDC;
3169 DeclarationName Name;
3170 SourceLocation Loc;
3171 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3172 return 0;
3173
3174 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003175 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3176 DC->localUncachedLookup(Name, FoundDecls);
3177 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3178 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003179 continue;
3180
Douglas Gregorb75a3452011-10-15 00:10:27 +00003181 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003182 break;
3183 }
3184
3185 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003186 if (!ToIface || !ToIface->hasDefinition()) {
Douglas Gregora12d2942010-02-16 01:20:57 +00003187 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003188 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3189 Importer.Import(D->getAtStartLoc()),
Douglas Gregor0af55012011-12-16 03:12:41 +00003190 Name.getAsIdentifierInfo(),
3191 /*PrevDecl=*/0,Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003192 D->isImplicitInterfaceDecl());
3193 ToIface->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003194 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003195 }
3196 Importer.Imported(D, ToIface);
3197
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003198 if (D->hasDefinition()) {
3199 if (!ToIface->hasDefinition())
3200 ToIface->startDefinition();
Douglas Gregora12d2942010-02-16 01:20:57 +00003201
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003202 if (D->getSuperClass()) {
3203 ObjCInterfaceDecl *Super
3204 = cast_or_null<ObjCInterfaceDecl>(
3205 Importer.Import(D->getSuperClass()));
3206 if (!Super)
3207 return 0;
3208
3209 ToIface->setSuperClass(Super);
3210 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3211 }
3212
3213 // Import protocols
3214 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3215 SmallVector<SourceLocation, 4> ProtocolLocs;
3216 ObjCInterfaceDecl::protocol_loc_iterator
3217 FromProtoLoc = D->protocol_loc_begin();
3218
3219 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3220 FromProtoEnd = D->protocol_end();
3221 FromProto != FromProtoEnd;
3222 ++FromProto, ++FromProtoLoc) {
3223 ObjCProtocolDecl *ToProto
3224 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3225 if (!ToProto)
3226 return 0;
3227 Protocols.push_back(ToProto);
3228 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3229 }
3230
3231 // FIXME: If we're merging, make sure that the protocol list is the same.
3232 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3233 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregora12d2942010-02-16 01:20:57 +00003234 }
3235
Douglas Gregora12d2942010-02-16 01:20:57 +00003236 // Import @end range
3237 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3238 } else {
3239 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003240
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003241 if (D->hasDefinition()) {
3242 // Check for consistency of superclasses.
3243 DeclarationName FromSuperName, ToSuperName;
3244
3245 // If the superclass hasn't been imported yet, do so before checking.
3246 ObjCInterfaceDecl *DSuperClass = D->getSuperClass();
3247 ObjCInterfaceDecl *ToIfaceSuperClass = ToIface->getSuperClass();
3248
3249 if (DSuperClass && !ToIfaceSuperClass) {
3250 Decl *ImportedSuperClass = Importer.Import(DSuperClass);
3251 ObjCInterfaceDecl *ImportedSuperIface
3252 = cast<ObjCInterfaceDecl>(ImportedSuperClass);
3253
3254 ToIface->setSuperClass(ImportedSuperIface);
3255 }
Sean Callananb1ce7302011-11-11 17:39:52 +00003256
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003257 if (D->getSuperClass())
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003258 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3259 if (ToIface->getSuperClass())
3260 ToSuperName = ToIface->getSuperClass()->getDeclName();
3261 if (FromSuperName != ToSuperName) {
3262 Importer.ToDiag(ToIface->getLocation(),
3263 diag::err_odr_objc_superclass_inconsistent)
3264 << ToIface->getDeclName();
3265 if (ToIface->getSuperClass())
3266 Importer.ToDiag(ToIface->getSuperClassLoc(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003267 diag::note_odr_objc_superclass)
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003268 << ToIface->getSuperClass()->getDeclName();
3269 else
3270 Importer.ToDiag(ToIface->getLocation(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003271 diag::note_odr_objc_missing_superclass);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003272 if (D->getSuperClass())
3273 Importer.FromDiag(D->getSuperClassLoc(),
3274 diag::note_odr_objc_superclass)
3275 << D->getSuperClass()->getDeclName();
3276 else
3277 Importer.FromDiag(D->getLocation(),
3278 diag::note_odr_objc_missing_superclass);
3279 return 0;
3280 }
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003281 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003282 }
3283
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003284 if (!D->hasDefinition())
3285 return ToIface;
3286
Douglas Gregorb4677b62010-02-18 01:47:50 +00003287 // Import categories. When the categories themselves are imported, they'll
3288 // hook themselves into this interface.
3289 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3290 FromCat = FromCat->getNextClassCategory())
3291 Importer.Import(FromCat);
3292
Douglas Gregora12d2942010-02-16 01:20:57 +00003293 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003294 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003295
3296 // If we have an @implementation, import it as well.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003297 if ( D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003298 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3299 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003300 if (!Impl)
3301 return 0;
3302
3303 ToIface->setImplementation(Impl);
3304 }
3305
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003306 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003307}
3308
Douglas Gregor3daef292010-12-07 15:32:12 +00003309Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3310 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3311 Importer.Import(D->getCategoryDecl()));
3312 if (!Category)
3313 return 0;
3314
3315 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3316 if (!ToImpl) {
3317 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3318 if (!DC)
3319 return 0;
3320
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003321 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003322 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003323 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003324 Category->getClassInterface(),
3325 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003326 Importer.Import(D->getAtStartLoc()),
3327 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003328
3329 DeclContext *LexicalDC = DC;
3330 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3331 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3332 if (!LexicalDC)
3333 return 0;
3334
3335 ToImpl->setLexicalDeclContext(LexicalDC);
3336 }
3337
Sean Callanan9faf8102011-10-21 02:57:43 +00003338 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003339 Category->setImplementation(ToImpl);
3340 }
3341
3342 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003343 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003344 return ToImpl;
3345}
3346
Douglas Gregordd182ff2010-12-07 01:26:03 +00003347Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3348 // Find the corresponding interface.
3349 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3350 Importer.Import(D->getClassInterface()));
3351 if (!Iface)
3352 return 0;
3353
3354 // Import the superclass, if any.
3355 ObjCInterfaceDecl *Super = 0;
3356 if (D->getSuperClass()) {
3357 Super = cast_or_null<ObjCInterfaceDecl>(
3358 Importer.Import(D->getSuperClass()));
3359 if (!Super)
3360 return 0;
3361 }
3362
3363 ObjCImplementationDecl *Impl = Iface->getImplementation();
3364 if (!Impl) {
3365 // We haven't imported an implementation yet. Create a new @implementation
3366 // now.
3367 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3368 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003369 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003370 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003371 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003372
3373 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3374 DeclContext *LexicalDC
3375 = Importer.ImportContext(D->getLexicalDeclContext());
3376 if (!LexicalDC)
3377 return 0;
3378 Impl->setLexicalDeclContext(LexicalDC);
3379 }
3380
3381 // Associate the implementation with the class it implements.
3382 Iface->setImplementation(Impl);
3383 Importer.Imported(D, Iface->getImplementation());
3384 } else {
3385 Importer.Imported(D, Iface->getImplementation());
3386
3387 // Verify that the existing @implementation has the same superclass.
3388 if ((Super && !Impl->getSuperClass()) ||
3389 (!Super && Impl->getSuperClass()) ||
3390 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003391 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003392 Importer.ToDiag(Impl->getLocation(),
3393 diag::err_odr_objc_superclass_inconsistent)
3394 << Iface->getDeclName();
3395 // FIXME: It would be nice to have the location of the superclass
3396 // below.
3397 if (Impl->getSuperClass())
3398 Importer.ToDiag(Impl->getLocation(),
3399 diag::note_odr_objc_superclass)
3400 << Impl->getSuperClass()->getDeclName();
3401 else
3402 Importer.ToDiag(Impl->getLocation(),
3403 diag::note_odr_objc_missing_superclass);
3404 if (D->getSuperClass())
3405 Importer.FromDiag(D->getLocation(),
3406 diag::note_odr_objc_superclass)
3407 << D->getSuperClass()->getDeclName();
3408 else
3409 Importer.FromDiag(D->getLocation(),
3410 diag::note_odr_objc_missing_superclass);
3411 return 0;
3412 }
3413 }
3414
3415 // Import all of the members of this @implementation.
3416 ImportDeclContext(D);
3417
3418 return Impl;
3419}
3420
Douglas Gregore3261622010-02-17 18:02:10 +00003421Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3422 // Import the major distinguishing characteristics of an @property.
3423 DeclContext *DC, *LexicalDC;
3424 DeclarationName Name;
3425 SourceLocation Loc;
3426 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3427 return 0;
3428
3429 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003430 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3431 DC->localUncachedLookup(Name, FoundDecls);
3432 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003433 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003434 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003435 // Check property types.
3436 if (!Importer.IsStructurallyEquivalent(D->getType(),
3437 FoundProp->getType())) {
3438 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3439 << Name << D->getType() << FoundProp->getType();
3440 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3441 << FoundProp->getType();
3442 return 0;
3443 }
3444
3445 // FIXME: Check property attributes, getters, setters, etc.?
3446
3447 // Consider these properties to be equivalent.
3448 Importer.Imported(D, FoundProp);
3449 return FoundProp;
3450 }
3451 }
3452
3453 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003454 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3455 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003456 return 0;
3457
3458 // Create the new property.
3459 ObjCPropertyDecl *ToProperty
3460 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3461 Name.getAsIdentifierInfo(),
3462 Importer.Import(D->getAtLoc()),
3463 T,
3464 D->getPropertyImplementation());
3465 Importer.Imported(D, ToProperty);
3466 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003467 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003468
3469 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003470 ToProperty->setPropertyAttributesAsWritten(
3471 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003472 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3473 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3474 ToProperty->setGetterMethodDecl(
3475 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3476 ToProperty->setSetterMethodDecl(
3477 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3478 ToProperty->setPropertyIvarDecl(
3479 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3480 return ToProperty;
3481}
3482
Douglas Gregor954e0c72010-12-07 18:32:03 +00003483Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3484 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3485 Importer.Import(D->getPropertyDecl()));
3486 if (!Property)
3487 return 0;
3488
3489 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3490 if (!DC)
3491 return 0;
3492
3493 // Import the lexical declaration context.
3494 DeclContext *LexicalDC = DC;
3495 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3496 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3497 if (!LexicalDC)
3498 return 0;
3499 }
3500
3501 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3502 if (!InImpl)
3503 return 0;
3504
3505 // Import the ivar (for an @synthesize).
3506 ObjCIvarDecl *Ivar = 0;
3507 if (D->getPropertyIvarDecl()) {
3508 Ivar = cast_or_null<ObjCIvarDecl>(
3509 Importer.Import(D->getPropertyIvarDecl()));
3510 if (!Ivar)
3511 return 0;
3512 }
3513
3514 ObjCPropertyImplDecl *ToImpl
3515 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3516 if (!ToImpl) {
3517 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3518 Importer.Import(D->getLocStart()),
3519 Importer.Import(D->getLocation()),
3520 Property,
3521 D->getPropertyImplementation(),
3522 Ivar,
3523 Importer.Import(D->getPropertyIvarDeclLoc()));
3524 ToImpl->setLexicalDeclContext(LexicalDC);
3525 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003526 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003527 } else {
3528 // Check that we have the same kind of property implementation (@synthesize
3529 // vs. @dynamic).
3530 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3531 Importer.ToDiag(ToImpl->getLocation(),
3532 diag::err_odr_objc_property_impl_kind_inconsistent)
3533 << Property->getDeclName()
3534 << (ToImpl->getPropertyImplementation()
3535 == ObjCPropertyImplDecl::Dynamic);
3536 Importer.FromDiag(D->getLocation(),
3537 diag::note_odr_objc_property_impl_kind)
3538 << D->getPropertyDecl()->getDeclName()
3539 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3540 return 0;
3541 }
3542
3543 // For @synthesize, check that we have the same
3544 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3545 Ivar != ToImpl->getPropertyIvarDecl()) {
3546 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3547 diag::err_odr_objc_synthesize_ivar_inconsistent)
3548 << Property->getDeclName()
3549 << ToImpl->getPropertyIvarDecl()->getDeclName()
3550 << Ivar->getDeclName();
3551 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3552 diag::note_odr_objc_synthesize_ivar_here)
3553 << D->getPropertyIvarDecl()->getDeclName();
3554 return 0;
3555 }
3556
3557 // Merge the existing implementation with the new implementation.
3558 Importer.Imported(D, ToImpl);
3559 }
3560
3561 return ToImpl;
3562}
3563
Douglas Gregor040afae2010-11-30 19:14:50 +00003564Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3565 // For template arguments, we adopt the translation unit as our declaration
3566 // context. This context will be fixed when the actual template declaration
3567 // is created.
3568
3569 // FIXME: Import default argument.
3570 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3571 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003572 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003573 Importer.Import(D->getLocation()),
3574 D->getDepth(),
3575 D->getIndex(),
3576 Importer.Import(D->getIdentifier()),
3577 D->wasDeclaredWithTypename(),
3578 D->isParameterPack());
3579}
3580
3581Decl *
3582ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3583 // Import the name of this declaration.
3584 DeclarationName Name = Importer.Import(D->getDeclName());
3585 if (D->getDeclName() && !Name)
3586 return 0;
3587
3588 // Import the location of this declaration.
3589 SourceLocation Loc = Importer.Import(D->getLocation());
3590
3591 // Import the type of this declaration.
3592 QualType T = Importer.Import(D->getType());
3593 if (T.isNull())
3594 return 0;
3595
3596 // Import type-source information.
3597 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3598 if (D->getTypeSourceInfo() && !TInfo)
3599 return 0;
3600
3601 // FIXME: Import default argument.
3602
3603 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3604 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003605 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003606 Loc, D->getDepth(), D->getPosition(),
3607 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003608 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003609}
3610
3611Decl *
3612ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3613 // Import the name of this declaration.
3614 DeclarationName Name = Importer.Import(D->getDeclName());
3615 if (D->getDeclName() && !Name)
3616 return 0;
3617
3618 // Import the location of this declaration.
3619 SourceLocation Loc = Importer.Import(D->getLocation());
3620
3621 // Import template parameters.
3622 TemplateParameterList *TemplateParams
3623 = ImportTemplateParameterList(D->getTemplateParameters());
3624 if (!TemplateParams)
3625 return 0;
3626
3627 // FIXME: Import default argument.
3628
3629 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3630 Importer.getToContext().getTranslationUnitDecl(),
3631 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003632 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003633 Name.getAsIdentifierInfo(),
3634 TemplateParams);
3635}
3636
3637Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3638 // If this record has a definition in the translation unit we're coming from,
3639 // but this particular declaration is not that definition, import the
3640 // definition and map to that.
3641 CXXRecordDecl *Definition
3642 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3643 if (Definition && Definition != D->getTemplatedDecl()) {
3644 Decl *ImportedDef
3645 = Importer.Import(Definition->getDescribedClassTemplate());
3646 if (!ImportedDef)
3647 return 0;
3648
3649 return Importer.Imported(D, ImportedDef);
3650 }
3651
3652 // Import the major distinguishing characteristics of this class template.
3653 DeclContext *DC, *LexicalDC;
3654 DeclarationName Name;
3655 SourceLocation Loc;
3656 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3657 return 0;
3658
3659 // We may already have a template of the same name; try to find and match it.
3660 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003661 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003662 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3663 DC->localUncachedLookup(Name, FoundDecls);
3664 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3665 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003666 continue;
3667
Douglas Gregorb75a3452011-10-15 00:10:27 +00003668 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003669 if (ClassTemplateDecl *FoundTemplate
3670 = dyn_cast<ClassTemplateDecl>(Found)) {
3671 if (IsStructuralMatch(D, FoundTemplate)) {
3672 // The class templates structurally match; call it the same template.
3673 // FIXME: We may be filling in a forward declaration here. Handle
3674 // this case!
3675 Importer.Imported(D->getTemplatedDecl(),
3676 FoundTemplate->getTemplatedDecl());
3677 return Importer.Imported(D, FoundTemplate);
3678 }
3679 }
3680
Douglas Gregorb75a3452011-10-15 00:10:27 +00003681 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003682 }
3683
3684 if (!ConflictingDecls.empty()) {
3685 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3686 ConflictingDecls.data(),
3687 ConflictingDecls.size());
3688 }
3689
3690 if (!Name)
3691 return 0;
3692 }
3693
3694 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3695
3696 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003697 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3698 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003699 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3700 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003701 DC, StartLoc, IdLoc,
3702 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003703 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003704 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003705 D2Templated->setLexicalDeclContext(LexicalDC);
3706
3707 // Create the class template declaration itself.
3708 TemplateParameterList *TemplateParams
3709 = ImportTemplateParameterList(D->getTemplateParameters());
3710 if (!TemplateParams)
3711 return 0;
3712
3713 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3714 Loc, Name, TemplateParams,
3715 D2Templated,
3716 /*PrevDecl=*/0);
3717 D2Templated->setDescribedClassTemplate(D2);
3718
3719 D2->setAccess(D->getAccess());
3720 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003721 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003722
3723 // Note the relationship between the class templates.
3724 Importer.Imported(D, D2);
3725 Importer.Imported(DTemplated, D2Templated);
3726
John McCall5e1cdac2011-10-07 06:10:15 +00003727 if (DTemplated->isCompleteDefinition() &&
3728 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003729 // FIXME: Import definition!
3730 }
3731
3732 return D2;
3733}
3734
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003735Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3736 ClassTemplateSpecializationDecl *D) {
3737 // If this record has a definition in the translation unit we're coming from,
3738 // but this particular declaration is not that definition, import the
3739 // definition and map to that.
3740 TagDecl *Definition = D->getDefinition();
3741 if (Definition && Definition != D) {
3742 Decl *ImportedDef = Importer.Import(Definition);
3743 if (!ImportedDef)
3744 return 0;
3745
3746 return Importer.Imported(D, ImportedDef);
3747 }
3748
3749 ClassTemplateDecl *ClassTemplate
3750 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3751 D->getSpecializedTemplate()));
3752 if (!ClassTemplate)
3753 return 0;
3754
3755 // Import the context of this declaration.
3756 DeclContext *DC = ClassTemplate->getDeclContext();
3757 if (!DC)
3758 return 0;
3759
3760 DeclContext *LexicalDC = DC;
3761 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3762 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3763 if (!LexicalDC)
3764 return 0;
3765 }
3766
3767 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003768 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3769 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003770
3771 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003772 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003773 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3774 D->getTemplateArgs().size(),
3775 TemplateArgs))
3776 return 0;
3777
3778 // Try to find an existing specialization with these template arguments.
3779 void *InsertPos = 0;
3780 ClassTemplateSpecializationDecl *D2
3781 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3782 TemplateArgs.size(), InsertPos);
3783 if (D2) {
3784 // We already have a class template specialization with these template
3785 // arguments.
3786
3787 // FIXME: Check for specialization vs. instantiation errors.
3788
3789 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003790 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003791 // The record types structurally match, or the "from" translation
3792 // unit only had a forward declaration anyway; call it the same
3793 // function.
3794 return Importer.Imported(D, FoundDef);
3795 }
3796 }
3797 } else {
3798 // Create a new specialization.
3799 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3800 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003801 StartLoc, IdLoc,
3802 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003803 TemplateArgs.data(),
3804 TemplateArgs.size(),
3805 /*PrevDecl=*/0);
3806 D2->setSpecializationKind(D->getSpecializationKind());
3807
3808 // Add this specialization to the class template.
3809 ClassTemplate->AddSpecialization(D2, InsertPos);
3810
3811 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003812 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003813
3814 // Add the specialization to this context.
3815 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003816 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003817 }
3818 Importer.Imported(D, D2);
3819
John McCall5e1cdac2011-10-07 06:10:15 +00003820 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003821 return 0;
3822
3823 return D2;
3824}
3825
Douglas Gregor4800d952010-02-11 19:21:55 +00003826//----------------------------------------------------------------------------
3827// Import Statements
3828//----------------------------------------------------------------------------
3829
3830Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3831 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3832 << S->getStmtClassName();
3833 return 0;
3834}
3835
3836//----------------------------------------------------------------------------
3837// Import Expressions
3838//----------------------------------------------------------------------------
3839Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3840 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3841 << E->getStmtClassName();
3842 return 0;
3843}
3844
Douglas Gregor44080632010-02-19 01:17:02 +00003845Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003846 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3847 if (!ToD)
3848 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003849
3850 NamedDecl *FoundD = 0;
3851 if (E->getDecl() != E->getFoundDecl()) {
3852 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3853 if (!FoundD)
3854 return 0;
3855 }
Douglas Gregor44080632010-02-19 01:17:02 +00003856
3857 QualType T = Importer.Import(E->getType());
3858 if (T.isNull())
3859 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003860
3861 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3862 Importer.Import(E->getQualifierLoc()),
3863 ToD,
3864 Importer.Import(E->getLocation()),
3865 T, E->getValueKind(),
3866 FoundD,
3867 /*FIXME:TemplateArgs=*/0);
3868 if (E->hadMultipleCandidates())
3869 DRE->setHadMultipleCandidates(true);
3870 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003871}
3872
Douglas Gregor4800d952010-02-11 19:21:55 +00003873Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3874 QualType T = Importer.Import(E->getType());
3875 if (T.isNull())
3876 return 0;
3877
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003878 return IntegerLiteral::Create(Importer.getToContext(),
3879 E->getValue(), T,
3880 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003881}
3882
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003883Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3884 QualType T = Importer.Import(E->getType());
3885 if (T.isNull())
3886 return 0;
3887
Douglas Gregor5cee1192011-07-27 05:40:30 +00003888 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3889 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003890 Importer.Import(E->getLocation()));
3891}
3892
Douglas Gregorf638f952010-02-19 01:07:06 +00003893Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3894 Expr *SubExpr = Importer.Import(E->getSubExpr());
3895 if (!SubExpr)
3896 return 0;
3897
3898 return new (Importer.getToContext())
3899 ParenExpr(Importer.Import(E->getLParen()),
3900 Importer.Import(E->getRParen()),
3901 SubExpr);
3902}
3903
3904Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3905 QualType T = Importer.Import(E->getType());
3906 if (T.isNull())
3907 return 0;
3908
3909 Expr *SubExpr = Importer.Import(E->getSubExpr());
3910 if (!SubExpr)
3911 return 0;
3912
3913 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003914 T, E->getValueKind(),
3915 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003916 Importer.Import(E->getOperatorLoc()));
3917}
3918
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003919Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3920 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003921 QualType ResultType = Importer.Import(E->getType());
3922
3923 if (E->isArgumentType()) {
3924 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3925 if (!TInfo)
3926 return 0;
3927
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003928 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3929 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003930 Importer.Import(E->getOperatorLoc()),
3931 Importer.Import(E->getRParenLoc()));
3932 }
3933
3934 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3935 if (!SubExpr)
3936 return 0;
3937
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003938 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3939 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003940 Importer.Import(E->getOperatorLoc()),
3941 Importer.Import(E->getRParenLoc()));
3942}
3943
Douglas Gregorf638f952010-02-19 01:07:06 +00003944Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3945 QualType T = Importer.Import(E->getType());
3946 if (T.isNull())
3947 return 0;
3948
3949 Expr *LHS = Importer.Import(E->getLHS());
3950 if (!LHS)
3951 return 0;
3952
3953 Expr *RHS = Importer.Import(E->getRHS());
3954 if (!RHS)
3955 return 0;
3956
3957 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003958 T, E->getValueKind(),
3959 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003960 Importer.Import(E->getOperatorLoc()));
3961}
3962
3963Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3964 QualType T = Importer.Import(E->getType());
3965 if (T.isNull())
3966 return 0;
3967
3968 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3969 if (CompLHSType.isNull())
3970 return 0;
3971
3972 QualType CompResultType = Importer.Import(E->getComputationResultType());
3973 if (CompResultType.isNull())
3974 return 0;
3975
3976 Expr *LHS = Importer.Import(E->getLHS());
3977 if (!LHS)
3978 return 0;
3979
3980 Expr *RHS = Importer.Import(E->getRHS());
3981 if (!RHS)
3982 return 0;
3983
3984 return new (Importer.getToContext())
3985 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003986 T, E->getValueKind(),
3987 E->getObjectKind(),
3988 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003989 Importer.Import(E->getOperatorLoc()));
3990}
3991
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003992static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003993 if (E->path_empty()) return false;
3994
3995 // TODO: import cast paths
3996 return true;
3997}
3998
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003999Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4000 QualType T = Importer.Import(E->getType());
4001 if (T.isNull())
4002 return 0;
4003
4004 Expr *SubExpr = Importer.Import(E->getSubExpr());
4005 if (!SubExpr)
4006 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004007
4008 CXXCastPath BasePath;
4009 if (ImportCastPath(E, BasePath))
4010 return 0;
4011
4012 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004013 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004014}
4015
Douglas Gregor008847a2010-02-19 01:32:14 +00004016Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4017 QualType T = Importer.Import(E->getType());
4018 if (T.isNull())
4019 return 0;
4020
4021 Expr *SubExpr = Importer.Import(E->getSubExpr());
4022 if (!SubExpr)
4023 return 0;
4024
4025 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4026 if (!TInfo && E->getTypeInfoAsWritten())
4027 return 0;
4028
John McCallf871d0c2010-08-07 06:22:56 +00004029 CXXCastPath BasePath;
4030 if (ImportCastPath(E, BasePath))
4031 return 0;
4032
John McCallf89e55a2010-11-18 06:31:45 +00004033 return CStyleCastExpr::Create(Importer.getToContext(), T,
4034 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004035 SubExpr, &BasePath, TInfo,
4036 Importer.Import(E->getLParenLoc()),
4037 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004038}
4039
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004040ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004041 ASTContext &FromContext, FileManager &FromFileManager,
4042 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004043 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004044 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4045 Minimal(MinimalImport)
4046{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004047 ImportedDecls[FromContext.getTranslationUnitDecl()]
4048 = ToContext.getTranslationUnitDecl();
4049}
4050
4051ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004052
4053QualType ASTImporter::Import(QualType FromT) {
4054 if (FromT.isNull())
4055 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004056
4057 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004058
Douglas Gregor169fba52010-02-08 15:18:58 +00004059 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004060 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4061 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004062 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004063 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004064
Douglas Gregor169fba52010-02-08 15:18:58 +00004065 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004066 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004067 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004068 if (ToT.isNull())
4069 return ToT;
4070
Douglas Gregor169fba52010-02-08 15:18:58 +00004071 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004072 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004073
John McCallf4c73712011-01-19 06:33:43 +00004074 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004075}
4076
Douglas Gregor9bed8792010-02-09 19:21:46 +00004077TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004078 if (!FromTSI)
4079 return FromTSI;
4080
4081 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004082 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004083 QualType T = Import(FromTSI->getType());
4084 if (T.isNull())
4085 return 0;
4086
4087 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004088 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004089}
4090
4091Decl *ASTImporter::Import(Decl *FromD) {
4092 if (!FromD)
4093 return 0;
4094
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004095 ASTNodeImporter Importer(*this);
4096
Douglas Gregor9bed8792010-02-09 19:21:46 +00004097 // Check whether we've already imported this declaration.
4098 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004099 if (Pos != ImportedDecls.end()) {
4100 Decl *ToD = Pos->second;
4101 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4102 return ToD;
4103 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004104
4105 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004106 Decl *ToD = Importer.Visit(FromD);
4107 if (!ToD)
4108 return 0;
4109
4110 // Record the imported declaration.
4111 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004112
4113 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4114 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004115 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004116 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004117 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004118 // When we've finished transforming a typedef, see whether it was the
4119 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004120 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004121 FromTag = AnonTagsWithPendingTypedefs.begin(),
4122 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4123 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004124 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004125 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4126 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004127 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004128 AnonTagsWithPendingTypedefs.erase(FromTag);
4129 break;
4130 }
4131 }
4132 }
4133 }
4134
Douglas Gregor9bed8792010-02-09 19:21:46 +00004135 return ToD;
4136}
4137
4138DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4139 if (!FromDC)
4140 return FromDC;
4141
4142 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4143}
4144
4145Expr *ASTImporter::Import(Expr *FromE) {
4146 if (!FromE)
4147 return 0;
4148
4149 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4150}
4151
4152Stmt *ASTImporter::Import(Stmt *FromS) {
4153 if (!FromS)
4154 return 0;
4155
Douglas Gregor4800d952010-02-11 19:21:55 +00004156 // Check whether we've already imported this declaration.
4157 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4158 if (Pos != ImportedStmts.end())
4159 return Pos->second;
4160
4161 // Import the type
4162 ASTNodeImporter Importer(*this);
4163 Stmt *ToS = Importer.Visit(FromS);
4164 if (!ToS)
4165 return 0;
4166
4167 // Record the imported declaration.
4168 ImportedStmts[FromS] = ToS;
4169 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004170}
4171
4172NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4173 if (!FromNNS)
4174 return 0;
4175
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004176 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4177
4178 switch (FromNNS->getKind()) {
4179 case NestedNameSpecifier::Identifier:
4180 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4181 return NestedNameSpecifier::Create(ToContext, prefix, II);
4182 }
4183 return 0;
4184
4185 case NestedNameSpecifier::Namespace:
4186 if (NamespaceDecl *NS =
4187 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4188 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4189 }
4190 return 0;
4191
4192 case NestedNameSpecifier::NamespaceAlias:
4193 if (NamespaceAliasDecl *NSAD =
4194 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4195 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4196 }
4197 return 0;
4198
4199 case NestedNameSpecifier::Global:
4200 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4201
4202 case NestedNameSpecifier::TypeSpec:
4203 case NestedNameSpecifier::TypeSpecWithTemplate: {
4204 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4205 if (!T.isNull()) {
4206 bool bTemplate = FromNNS->getKind() ==
4207 NestedNameSpecifier::TypeSpecWithTemplate;
4208 return NestedNameSpecifier::Create(ToContext, prefix,
4209 bTemplate, T.getTypePtr());
4210 }
4211 }
4212 return 0;
4213 }
4214
4215 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004216 return 0;
4217}
4218
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004219NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4220 // FIXME: Implement!
4221 return NestedNameSpecifierLoc();
4222}
4223
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004224TemplateName ASTImporter::Import(TemplateName From) {
4225 switch (From.getKind()) {
4226 case TemplateName::Template:
4227 if (TemplateDecl *ToTemplate
4228 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4229 return TemplateName(ToTemplate);
4230
4231 return TemplateName();
4232
4233 case TemplateName::OverloadedTemplate: {
4234 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4235 UnresolvedSet<2> ToTemplates;
4236 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4237 E = FromStorage->end();
4238 I != E; ++I) {
4239 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4240 ToTemplates.addDecl(To);
4241 else
4242 return TemplateName();
4243 }
4244 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4245 ToTemplates.end());
4246 }
4247
4248 case TemplateName::QualifiedTemplate: {
4249 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4250 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4251 if (!Qualifier)
4252 return TemplateName();
4253
4254 if (TemplateDecl *ToTemplate
4255 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4256 return ToContext.getQualifiedTemplateName(Qualifier,
4257 QTN->hasTemplateKeyword(),
4258 ToTemplate);
4259
4260 return TemplateName();
4261 }
4262
4263 case TemplateName::DependentTemplate: {
4264 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4265 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4266 if (!Qualifier)
4267 return TemplateName();
4268
4269 if (DTN->isIdentifier()) {
4270 return ToContext.getDependentTemplateName(Qualifier,
4271 Import(DTN->getIdentifier()));
4272 }
4273
4274 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4275 }
John McCall14606042011-06-30 08:33:18 +00004276
4277 case TemplateName::SubstTemplateTemplateParm: {
4278 SubstTemplateTemplateParmStorage *subst
4279 = From.getAsSubstTemplateTemplateParm();
4280 TemplateTemplateParmDecl *param
4281 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4282 if (!param)
4283 return TemplateName();
4284
4285 TemplateName replacement = Import(subst->getReplacement());
4286 if (replacement.isNull()) return TemplateName();
4287
4288 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4289 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004290
4291 case TemplateName::SubstTemplateTemplateParmPack: {
4292 SubstTemplateTemplateParmPackStorage *SubstPack
4293 = From.getAsSubstTemplateTemplateParmPack();
4294 TemplateTemplateParmDecl *Param
4295 = cast_or_null<TemplateTemplateParmDecl>(
4296 Import(SubstPack->getParameterPack()));
4297 if (!Param)
4298 return TemplateName();
4299
4300 ASTNodeImporter Importer(*this);
4301 TemplateArgument ArgPack
4302 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4303 if (ArgPack.isNull())
4304 return TemplateName();
4305
4306 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4307 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004308 }
4309
4310 llvm_unreachable("Invalid template name kind");
4311 return TemplateName();
4312}
4313
Douglas Gregor9bed8792010-02-09 19:21:46 +00004314SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4315 if (FromLoc.isInvalid())
4316 return SourceLocation();
4317
Douglas Gregor88523732010-02-10 00:15:17 +00004318 SourceManager &FromSM = FromContext.getSourceManager();
4319
4320 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004321 // don't have to import macro expansions.
4322 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004323 FromLoc = FromSM.getSpellingLoc(FromLoc);
4324 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4325 SourceManager &ToSM = ToContext.getSourceManager();
4326 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004327 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004328}
4329
4330SourceRange ASTImporter::Import(SourceRange FromRange) {
4331 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4332}
4333
Douglas Gregor88523732010-02-10 00:15:17 +00004334FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004335 llvm::DenseMap<FileID, FileID>::iterator Pos
4336 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004337 if (Pos != ImportedFileIDs.end())
4338 return Pos->second;
4339
4340 SourceManager &FromSM = FromContext.getSourceManager();
4341 SourceManager &ToSM = ToContext.getSourceManager();
4342 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004343 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004344
4345 // Include location of this file.
4346 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4347
4348 // Map the FileID for to the "to" source manager.
4349 FileID ToID;
4350 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004351 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004352 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4353 // disk again
4354 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4355 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004356 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004357 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4358 FromSLoc.getFile().getFileCharacteristic());
4359 } else {
4360 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004361 const llvm::MemoryBuffer *
4362 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004363 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004364 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004365 FromBuf->getBufferIdentifier());
4366 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4367 }
4368
4369
Sebastian Redl535a3e22010-09-30 01:03:06 +00004370 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004371 return ToID;
4372}
4373
Douglas Gregord8868a62011-01-18 03:11:38 +00004374void ASTImporter::ImportDefinition(Decl *From) {
4375 Decl *To = Import(From);
4376 if (!To)
4377 return;
4378
4379 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4380 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004381
4382 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4383 if (!ToRecord->getDefinition()) {
4384 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4385 /*ForceImport=*/true);
4386 return;
4387 }
4388 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004389
4390 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4391 if (!ToEnum->getDefinition()) {
4392 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4393 /*ForceImport=*/true);
4394 return;
4395 }
4396 }
4397
Douglas Gregord8868a62011-01-18 03:11:38 +00004398 Importer.ImportDeclContext(FromDC, true);
4399 }
4400}
4401
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004402DeclarationName ASTImporter::Import(DeclarationName FromName) {
4403 if (!FromName)
4404 return DeclarationName();
4405
4406 switch (FromName.getNameKind()) {
4407 case DeclarationName::Identifier:
4408 return Import(FromName.getAsIdentifierInfo());
4409
4410 case DeclarationName::ObjCZeroArgSelector:
4411 case DeclarationName::ObjCOneArgSelector:
4412 case DeclarationName::ObjCMultiArgSelector:
4413 return Import(FromName.getObjCSelector());
4414
4415 case DeclarationName::CXXConstructorName: {
4416 QualType T = Import(FromName.getCXXNameType());
4417 if (T.isNull())
4418 return DeclarationName();
4419
4420 return ToContext.DeclarationNames.getCXXConstructorName(
4421 ToContext.getCanonicalType(T));
4422 }
4423
4424 case DeclarationName::CXXDestructorName: {
4425 QualType T = Import(FromName.getCXXNameType());
4426 if (T.isNull())
4427 return DeclarationName();
4428
4429 return ToContext.DeclarationNames.getCXXDestructorName(
4430 ToContext.getCanonicalType(T));
4431 }
4432
4433 case DeclarationName::CXXConversionFunctionName: {
4434 QualType T = Import(FromName.getCXXNameType());
4435 if (T.isNull())
4436 return DeclarationName();
4437
4438 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4439 ToContext.getCanonicalType(T));
4440 }
4441
4442 case DeclarationName::CXXOperatorName:
4443 return ToContext.DeclarationNames.getCXXOperatorName(
4444 FromName.getCXXOverloadedOperator());
4445
4446 case DeclarationName::CXXLiteralOperatorName:
4447 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4448 Import(FromName.getCXXLiteralIdentifier()));
4449
4450 case DeclarationName::CXXUsingDirective:
4451 // FIXME: STATICS!
4452 return DeclarationName::getUsingDirectiveName();
4453 }
4454
4455 // Silence bogus GCC warning
4456 return DeclarationName();
4457}
4458
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004459IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004460 if (!FromId)
4461 return 0;
4462
4463 return &ToContext.Idents.get(FromId->getName());
4464}
Douglas Gregor089459a2010-02-08 21:09:39 +00004465
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004466Selector ASTImporter::Import(Selector FromSel) {
4467 if (FromSel.isNull())
4468 return Selector();
4469
Chris Lattner5f9e2722011-07-23 10:55:15 +00004470 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004471 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4472 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4473 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4474 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4475}
4476
Douglas Gregor089459a2010-02-08 21:09:39 +00004477DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4478 DeclContext *DC,
4479 unsigned IDNS,
4480 NamedDecl **Decls,
4481 unsigned NumDecls) {
4482 return Name;
4483}
4484
4485DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004486 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004487}
4488
4489DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004490 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004491}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004492
4493Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4494 ImportedDecls[From] = To;
4495 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004496}
Douglas Gregorea35d112010-02-15 23:54:17 +00004497
4498bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004499 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004500 = ImportedTypes.find(From.getTypePtr());
4501 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4502 return true;
4503
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004504 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004505 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004506}