blob: 82307e8e98034b1d7e68d77df3ca505c5430a6d8 [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 Gregorc9d3c7e2012-01-01 22:06:18 +00003126 /*PrevDecl=*/0);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003127 ToProto->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003128 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003129 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003130 if (!ToProto->hasDefinition())
3131 ToProto->startDefinition();
3132
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003133 Importer.Imported(D, ToProto);
3134
3135 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003136 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3137 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003138 ObjCProtocolDecl::protocol_loc_iterator
3139 FromProtoLoc = D->protocol_loc_begin();
3140 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3141 FromProtoEnd = D->protocol_end();
3142 FromProto != FromProtoEnd;
3143 ++FromProto, ++FromProtoLoc) {
3144 ObjCProtocolDecl *ToProto
3145 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3146 if (!ToProto)
3147 return 0;
3148 Protocols.push_back(ToProto);
3149 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3150 }
3151
3152 // FIXME: If we're merging, make sure that the protocol list is the same.
3153 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3154 ProtocolLocs.data(), Importer.getToContext());
3155 } else {
3156 Importer.Imported(D, ToProto);
3157 }
3158
Douglas Gregorb4677b62010-02-18 01:47:50 +00003159 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003160 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003161
3162 return ToProto;
3163}
3164
Douglas Gregora12d2942010-02-16 01:20:57 +00003165Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3166 // Import the major distinguishing characteristics of an @interface.
3167 DeclContext *DC, *LexicalDC;
3168 DeclarationName Name;
3169 SourceLocation Loc;
3170 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3171 return 0;
3172
3173 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003174 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3175 DC->localUncachedLookup(Name, FoundDecls);
3176 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3177 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003178 continue;
3179
Douglas Gregorb75a3452011-10-15 00:10:27 +00003180 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003181 break;
3182 }
3183
3184 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003185 if (!ToIface || !ToIface->hasDefinition()) {
Douglas Gregora12d2942010-02-16 01:20:57 +00003186 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003187 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3188 Importer.Import(D->getAtStartLoc()),
Douglas Gregor0af55012011-12-16 03:12:41 +00003189 Name.getAsIdentifierInfo(),
3190 /*PrevDecl=*/0,Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003191 D->isImplicitInterfaceDecl());
3192 ToIface->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003193 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003194 }
3195 Importer.Imported(D, ToIface);
3196
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003197 if (D->hasDefinition()) {
3198 if (!ToIface->hasDefinition())
3199 ToIface->startDefinition();
Douglas Gregora12d2942010-02-16 01:20:57 +00003200
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003201 if (D->getSuperClass()) {
3202 ObjCInterfaceDecl *Super
3203 = cast_or_null<ObjCInterfaceDecl>(
3204 Importer.Import(D->getSuperClass()));
3205 if (!Super)
3206 return 0;
3207
3208 ToIface->setSuperClass(Super);
3209 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3210 }
3211
3212 // Import protocols
3213 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3214 SmallVector<SourceLocation, 4> ProtocolLocs;
3215 ObjCInterfaceDecl::protocol_loc_iterator
3216 FromProtoLoc = D->protocol_loc_begin();
3217
3218 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3219 FromProtoEnd = D->protocol_end();
3220 FromProto != FromProtoEnd;
3221 ++FromProto, ++FromProtoLoc) {
3222 ObjCProtocolDecl *ToProto
3223 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3224 if (!ToProto)
3225 return 0;
3226 Protocols.push_back(ToProto);
3227 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3228 }
3229
3230 // FIXME: If we're merging, make sure that the protocol list is the same.
3231 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3232 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregora12d2942010-02-16 01:20:57 +00003233 }
3234
Douglas Gregora12d2942010-02-16 01:20:57 +00003235 // Import @end range
3236 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3237 } else {
3238 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003239
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003240 if (D->hasDefinition()) {
3241 // Check for consistency of superclasses.
3242 DeclarationName FromSuperName, ToSuperName;
3243
3244 // If the superclass hasn't been imported yet, do so before checking.
3245 ObjCInterfaceDecl *DSuperClass = D->getSuperClass();
3246 ObjCInterfaceDecl *ToIfaceSuperClass = ToIface->getSuperClass();
3247
3248 if (DSuperClass && !ToIfaceSuperClass) {
3249 Decl *ImportedSuperClass = Importer.Import(DSuperClass);
3250 ObjCInterfaceDecl *ImportedSuperIface
3251 = cast<ObjCInterfaceDecl>(ImportedSuperClass);
3252
3253 ToIface->setSuperClass(ImportedSuperIface);
3254 }
Sean Callananb1ce7302011-11-11 17:39:52 +00003255
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003256 if (D->getSuperClass())
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003257 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3258 if (ToIface->getSuperClass())
3259 ToSuperName = ToIface->getSuperClass()->getDeclName();
3260 if (FromSuperName != ToSuperName) {
3261 Importer.ToDiag(ToIface->getLocation(),
3262 diag::err_odr_objc_superclass_inconsistent)
3263 << ToIface->getDeclName();
3264 if (ToIface->getSuperClass())
3265 Importer.ToDiag(ToIface->getSuperClassLoc(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003266 diag::note_odr_objc_superclass)
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003267 << ToIface->getSuperClass()->getDeclName();
3268 else
3269 Importer.ToDiag(ToIface->getLocation(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003270 diag::note_odr_objc_missing_superclass);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003271 if (D->getSuperClass())
3272 Importer.FromDiag(D->getSuperClassLoc(),
3273 diag::note_odr_objc_superclass)
3274 << D->getSuperClass()->getDeclName();
3275 else
3276 Importer.FromDiag(D->getLocation(),
3277 diag::note_odr_objc_missing_superclass);
3278 return 0;
3279 }
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003280 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003281 }
3282
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003283 if (!D->hasDefinition())
3284 return ToIface;
3285
Douglas Gregorb4677b62010-02-18 01:47:50 +00003286 // Import categories. When the categories themselves are imported, they'll
3287 // hook themselves into this interface.
3288 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3289 FromCat = FromCat->getNextClassCategory())
3290 Importer.Import(FromCat);
3291
Douglas Gregora12d2942010-02-16 01:20:57 +00003292 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003293 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003294
3295 // If we have an @implementation, import it as well.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003296 if ( D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003297 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3298 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003299 if (!Impl)
3300 return 0;
3301
3302 ToIface->setImplementation(Impl);
3303 }
3304
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003305 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003306}
3307
Douglas Gregor3daef292010-12-07 15:32:12 +00003308Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3309 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3310 Importer.Import(D->getCategoryDecl()));
3311 if (!Category)
3312 return 0;
3313
3314 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3315 if (!ToImpl) {
3316 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3317 if (!DC)
3318 return 0;
3319
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003320 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003321 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003322 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003323 Category->getClassInterface(),
3324 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003325 Importer.Import(D->getAtStartLoc()),
3326 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003327
3328 DeclContext *LexicalDC = DC;
3329 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3330 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3331 if (!LexicalDC)
3332 return 0;
3333
3334 ToImpl->setLexicalDeclContext(LexicalDC);
3335 }
3336
Sean Callanan9faf8102011-10-21 02:57:43 +00003337 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003338 Category->setImplementation(ToImpl);
3339 }
3340
3341 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003342 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003343 return ToImpl;
3344}
3345
Douglas Gregordd182ff2010-12-07 01:26:03 +00003346Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3347 // Find the corresponding interface.
3348 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3349 Importer.Import(D->getClassInterface()));
3350 if (!Iface)
3351 return 0;
3352
3353 // Import the superclass, if any.
3354 ObjCInterfaceDecl *Super = 0;
3355 if (D->getSuperClass()) {
3356 Super = cast_or_null<ObjCInterfaceDecl>(
3357 Importer.Import(D->getSuperClass()));
3358 if (!Super)
3359 return 0;
3360 }
3361
3362 ObjCImplementationDecl *Impl = Iface->getImplementation();
3363 if (!Impl) {
3364 // We haven't imported an implementation yet. Create a new @implementation
3365 // now.
3366 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3367 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003368 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003369 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003370 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003371
3372 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3373 DeclContext *LexicalDC
3374 = Importer.ImportContext(D->getLexicalDeclContext());
3375 if (!LexicalDC)
3376 return 0;
3377 Impl->setLexicalDeclContext(LexicalDC);
3378 }
3379
3380 // Associate the implementation with the class it implements.
3381 Iface->setImplementation(Impl);
3382 Importer.Imported(D, Iface->getImplementation());
3383 } else {
3384 Importer.Imported(D, Iface->getImplementation());
3385
3386 // Verify that the existing @implementation has the same superclass.
3387 if ((Super && !Impl->getSuperClass()) ||
3388 (!Super && Impl->getSuperClass()) ||
3389 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003390 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003391 Importer.ToDiag(Impl->getLocation(),
3392 diag::err_odr_objc_superclass_inconsistent)
3393 << Iface->getDeclName();
3394 // FIXME: It would be nice to have the location of the superclass
3395 // below.
3396 if (Impl->getSuperClass())
3397 Importer.ToDiag(Impl->getLocation(),
3398 diag::note_odr_objc_superclass)
3399 << Impl->getSuperClass()->getDeclName();
3400 else
3401 Importer.ToDiag(Impl->getLocation(),
3402 diag::note_odr_objc_missing_superclass);
3403 if (D->getSuperClass())
3404 Importer.FromDiag(D->getLocation(),
3405 diag::note_odr_objc_superclass)
3406 << D->getSuperClass()->getDeclName();
3407 else
3408 Importer.FromDiag(D->getLocation(),
3409 diag::note_odr_objc_missing_superclass);
3410 return 0;
3411 }
3412 }
3413
3414 // Import all of the members of this @implementation.
3415 ImportDeclContext(D);
3416
3417 return Impl;
3418}
3419
Douglas Gregore3261622010-02-17 18:02:10 +00003420Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3421 // Import the major distinguishing characteristics of an @property.
3422 DeclContext *DC, *LexicalDC;
3423 DeclarationName Name;
3424 SourceLocation Loc;
3425 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3426 return 0;
3427
3428 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003429 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3430 DC->localUncachedLookup(Name, FoundDecls);
3431 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003432 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003433 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003434 // Check property types.
3435 if (!Importer.IsStructurallyEquivalent(D->getType(),
3436 FoundProp->getType())) {
3437 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3438 << Name << D->getType() << FoundProp->getType();
3439 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3440 << FoundProp->getType();
3441 return 0;
3442 }
3443
3444 // FIXME: Check property attributes, getters, setters, etc.?
3445
3446 // Consider these properties to be equivalent.
3447 Importer.Imported(D, FoundProp);
3448 return FoundProp;
3449 }
3450 }
3451
3452 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003453 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3454 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003455 return 0;
3456
3457 // Create the new property.
3458 ObjCPropertyDecl *ToProperty
3459 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3460 Name.getAsIdentifierInfo(),
3461 Importer.Import(D->getAtLoc()),
3462 T,
3463 D->getPropertyImplementation());
3464 Importer.Imported(D, ToProperty);
3465 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003466 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003467
3468 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003469 ToProperty->setPropertyAttributesAsWritten(
3470 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003471 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3472 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3473 ToProperty->setGetterMethodDecl(
3474 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3475 ToProperty->setSetterMethodDecl(
3476 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3477 ToProperty->setPropertyIvarDecl(
3478 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3479 return ToProperty;
3480}
3481
Douglas Gregor954e0c72010-12-07 18:32:03 +00003482Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3483 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3484 Importer.Import(D->getPropertyDecl()));
3485 if (!Property)
3486 return 0;
3487
3488 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3489 if (!DC)
3490 return 0;
3491
3492 // Import the lexical declaration context.
3493 DeclContext *LexicalDC = DC;
3494 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3495 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3496 if (!LexicalDC)
3497 return 0;
3498 }
3499
3500 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3501 if (!InImpl)
3502 return 0;
3503
3504 // Import the ivar (for an @synthesize).
3505 ObjCIvarDecl *Ivar = 0;
3506 if (D->getPropertyIvarDecl()) {
3507 Ivar = cast_or_null<ObjCIvarDecl>(
3508 Importer.Import(D->getPropertyIvarDecl()));
3509 if (!Ivar)
3510 return 0;
3511 }
3512
3513 ObjCPropertyImplDecl *ToImpl
3514 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3515 if (!ToImpl) {
3516 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3517 Importer.Import(D->getLocStart()),
3518 Importer.Import(D->getLocation()),
3519 Property,
3520 D->getPropertyImplementation(),
3521 Ivar,
3522 Importer.Import(D->getPropertyIvarDeclLoc()));
3523 ToImpl->setLexicalDeclContext(LexicalDC);
3524 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003525 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003526 } else {
3527 // Check that we have the same kind of property implementation (@synthesize
3528 // vs. @dynamic).
3529 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3530 Importer.ToDiag(ToImpl->getLocation(),
3531 diag::err_odr_objc_property_impl_kind_inconsistent)
3532 << Property->getDeclName()
3533 << (ToImpl->getPropertyImplementation()
3534 == ObjCPropertyImplDecl::Dynamic);
3535 Importer.FromDiag(D->getLocation(),
3536 diag::note_odr_objc_property_impl_kind)
3537 << D->getPropertyDecl()->getDeclName()
3538 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3539 return 0;
3540 }
3541
3542 // For @synthesize, check that we have the same
3543 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3544 Ivar != ToImpl->getPropertyIvarDecl()) {
3545 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3546 diag::err_odr_objc_synthesize_ivar_inconsistent)
3547 << Property->getDeclName()
3548 << ToImpl->getPropertyIvarDecl()->getDeclName()
3549 << Ivar->getDeclName();
3550 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3551 diag::note_odr_objc_synthesize_ivar_here)
3552 << D->getPropertyIvarDecl()->getDeclName();
3553 return 0;
3554 }
3555
3556 // Merge the existing implementation with the new implementation.
3557 Importer.Imported(D, ToImpl);
3558 }
3559
3560 return ToImpl;
3561}
3562
Douglas Gregor040afae2010-11-30 19:14:50 +00003563Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3564 // For template arguments, we adopt the translation unit as our declaration
3565 // context. This context will be fixed when the actual template declaration
3566 // is created.
3567
3568 // FIXME: Import default argument.
3569 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3570 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003571 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003572 Importer.Import(D->getLocation()),
3573 D->getDepth(),
3574 D->getIndex(),
3575 Importer.Import(D->getIdentifier()),
3576 D->wasDeclaredWithTypename(),
3577 D->isParameterPack());
3578}
3579
3580Decl *
3581ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3582 // Import the name of this declaration.
3583 DeclarationName Name = Importer.Import(D->getDeclName());
3584 if (D->getDeclName() && !Name)
3585 return 0;
3586
3587 // Import the location of this declaration.
3588 SourceLocation Loc = Importer.Import(D->getLocation());
3589
3590 // Import the type of this declaration.
3591 QualType T = Importer.Import(D->getType());
3592 if (T.isNull())
3593 return 0;
3594
3595 // Import type-source information.
3596 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3597 if (D->getTypeSourceInfo() && !TInfo)
3598 return 0;
3599
3600 // FIXME: Import default argument.
3601
3602 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3603 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003604 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003605 Loc, D->getDepth(), D->getPosition(),
3606 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003607 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003608}
3609
3610Decl *
3611ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3612 // Import the name of this declaration.
3613 DeclarationName Name = Importer.Import(D->getDeclName());
3614 if (D->getDeclName() && !Name)
3615 return 0;
3616
3617 // Import the location of this declaration.
3618 SourceLocation Loc = Importer.Import(D->getLocation());
3619
3620 // Import template parameters.
3621 TemplateParameterList *TemplateParams
3622 = ImportTemplateParameterList(D->getTemplateParameters());
3623 if (!TemplateParams)
3624 return 0;
3625
3626 // FIXME: Import default argument.
3627
3628 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3629 Importer.getToContext().getTranslationUnitDecl(),
3630 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003631 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003632 Name.getAsIdentifierInfo(),
3633 TemplateParams);
3634}
3635
3636Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3637 // If this record has a definition in the translation unit we're coming from,
3638 // but this particular declaration is not that definition, import the
3639 // definition and map to that.
3640 CXXRecordDecl *Definition
3641 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3642 if (Definition && Definition != D->getTemplatedDecl()) {
3643 Decl *ImportedDef
3644 = Importer.Import(Definition->getDescribedClassTemplate());
3645 if (!ImportedDef)
3646 return 0;
3647
3648 return Importer.Imported(D, ImportedDef);
3649 }
3650
3651 // Import the major distinguishing characteristics of this class template.
3652 DeclContext *DC, *LexicalDC;
3653 DeclarationName Name;
3654 SourceLocation Loc;
3655 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3656 return 0;
3657
3658 // We may already have a template of the same name; try to find and match it.
3659 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003660 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003661 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3662 DC->localUncachedLookup(Name, FoundDecls);
3663 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3664 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003665 continue;
3666
Douglas Gregorb75a3452011-10-15 00:10:27 +00003667 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003668 if (ClassTemplateDecl *FoundTemplate
3669 = dyn_cast<ClassTemplateDecl>(Found)) {
3670 if (IsStructuralMatch(D, FoundTemplate)) {
3671 // The class templates structurally match; call it the same template.
3672 // FIXME: We may be filling in a forward declaration here. Handle
3673 // this case!
3674 Importer.Imported(D->getTemplatedDecl(),
3675 FoundTemplate->getTemplatedDecl());
3676 return Importer.Imported(D, FoundTemplate);
3677 }
3678 }
3679
Douglas Gregorb75a3452011-10-15 00:10:27 +00003680 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003681 }
3682
3683 if (!ConflictingDecls.empty()) {
3684 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3685 ConflictingDecls.data(),
3686 ConflictingDecls.size());
3687 }
3688
3689 if (!Name)
3690 return 0;
3691 }
3692
3693 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3694
3695 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003696 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3697 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003698 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3699 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003700 DC, StartLoc, IdLoc,
3701 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003702 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003703 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003704 D2Templated->setLexicalDeclContext(LexicalDC);
3705
3706 // Create the class template declaration itself.
3707 TemplateParameterList *TemplateParams
3708 = ImportTemplateParameterList(D->getTemplateParameters());
3709 if (!TemplateParams)
3710 return 0;
3711
3712 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3713 Loc, Name, TemplateParams,
3714 D2Templated,
3715 /*PrevDecl=*/0);
3716 D2Templated->setDescribedClassTemplate(D2);
3717
3718 D2->setAccess(D->getAccess());
3719 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003720 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003721
3722 // Note the relationship between the class templates.
3723 Importer.Imported(D, D2);
3724 Importer.Imported(DTemplated, D2Templated);
3725
John McCall5e1cdac2011-10-07 06:10:15 +00003726 if (DTemplated->isCompleteDefinition() &&
3727 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003728 // FIXME: Import definition!
3729 }
3730
3731 return D2;
3732}
3733
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003734Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3735 ClassTemplateSpecializationDecl *D) {
3736 // If this record has a definition in the translation unit we're coming from,
3737 // but this particular declaration is not that definition, import the
3738 // definition and map to that.
3739 TagDecl *Definition = D->getDefinition();
3740 if (Definition && Definition != D) {
3741 Decl *ImportedDef = Importer.Import(Definition);
3742 if (!ImportedDef)
3743 return 0;
3744
3745 return Importer.Imported(D, ImportedDef);
3746 }
3747
3748 ClassTemplateDecl *ClassTemplate
3749 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3750 D->getSpecializedTemplate()));
3751 if (!ClassTemplate)
3752 return 0;
3753
3754 // Import the context of this declaration.
3755 DeclContext *DC = ClassTemplate->getDeclContext();
3756 if (!DC)
3757 return 0;
3758
3759 DeclContext *LexicalDC = DC;
3760 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3761 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3762 if (!LexicalDC)
3763 return 0;
3764 }
3765
3766 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003767 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3768 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003769
3770 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003771 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003772 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3773 D->getTemplateArgs().size(),
3774 TemplateArgs))
3775 return 0;
3776
3777 // Try to find an existing specialization with these template arguments.
3778 void *InsertPos = 0;
3779 ClassTemplateSpecializationDecl *D2
3780 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3781 TemplateArgs.size(), InsertPos);
3782 if (D2) {
3783 // We already have a class template specialization with these template
3784 // arguments.
3785
3786 // FIXME: Check for specialization vs. instantiation errors.
3787
3788 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003789 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003790 // The record types structurally match, or the "from" translation
3791 // unit only had a forward declaration anyway; call it the same
3792 // function.
3793 return Importer.Imported(D, FoundDef);
3794 }
3795 }
3796 } else {
3797 // Create a new specialization.
3798 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3799 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003800 StartLoc, IdLoc,
3801 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003802 TemplateArgs.data(),
3803 TemplateArgs.size(),
3804 /*PrevDecl=*/0);
3805 D2->setSpecializationKind(D->getSpecializationKind());
3806
3807 // Add this specialization to the class template.
3808 ClassTemplate->AddSpecialization(D2, InsertPos);
3809
3810 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003811 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003812
3813 // Add the specialization to this context.
3814 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003815 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003816 }
3817 Importer.Imported(D, D2);
3818
John McCall5e1cdac2011-10-07 06:10:15 +00003819 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003820 return 0;
3821
3822 return D2;
3823}
3824
Douglas Gregor4800d952010-02-11 19:21:55 +00003825//----------------------------------------------------------------------------
3826// Import Statements
3827//----------------------------------------------------------------------------
3828
3829Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3830 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3831 << S->getStmtClassName();
3832 return 0;
3833}
3834
3835//----------------------------------------------------------------------------
3836// Import Expressions
3837//----------------------------------------------------------------------------
3838Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3839 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3840 << E->getStmtClassName();
3841 return 0;
3842}
3843
Douglas Gregor44080632010-02-19 01:17:02 +00003844Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003845 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3846 if (!ToD)
3847 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003848
3849 NamedDecl *FoundD = 0;
3850 if (E->getDecl() != E->getFoundDecl()) {
3851 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3852 if (!FoundD)
3853 return 0;
3854 }
Douglas Gregor44080632010-02-19 01:17:02 +00003855
3856 QualType T = Importer.Import(E->getType());
3857 if (T.isNull())
3858 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003859
3860 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3861 Importer.Import(E->getQualifierLoc()),
3862 ToD,
3863 Importer.Import(E->getLocation()),
3864 T, E->getValueKind(),
3865 FoundD,
3866 /*FIXME:TemplateArgs=*/0);
3867 if (E->hadMultipleCandidates())
3868 DRE->setHadMultipleCandidates(true);
3869 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003870}
3871
Douglas Gregor4800d952010-02-11 19:21:55 +00003872Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3873 QualType T = Importer.Import(E->getType());
3874 if (T.isNull())
3875 return 0;
3876
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003877 return IntegerLiteral::Create(Importer.getToContext(),
3878 E->getValue(), T,
3879 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003880}
3881
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003882Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3883 QualType T = Importer.Import(E->getType());
3884 if (T.isNull())
3885 return 0;
3886
Douglas Gregor5cee1192011-07-27 05:40:30 +00003887 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3888 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003889 Importer.Import(E->getLocation()));
3890}
3891
Douglas Gregorf638f952010-02-19 01:07:06 +00003892Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3893 Expr *SubExpr = Importer.Import(E->getSubExpr());
3894 if (!SubExpr)
3895 return 0;
3896
3897 return new (Importer.getToContext())
3898 ParenExpr(Importer.Import(E->getLParen()),
3899 Importer.Import(E->getRParen()),
3900 SubExpr);
3901}
3902
3903Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3904 QualType T = Importer.Import(E->getType());
3905 if (T.isNull())
3906 return 0;
3907
3908 Expr *SubExpr = Importer.Import(E->getSubExpr());
3909 if (!SubExpr)
3910 return 0;
3911
3912 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003913 T, E->getValueKind(),
3914 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003915 Importer.Import(E->getOperatorLoc()));
3916}
3917
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003918Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3919 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003920 QualType ResultType = Importer.Import(E->getType());
3921
3922 if (E->isArgumentType()) {
3923 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3924 if (!TInfo)
3925 return 0;
3926
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003927 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3928 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003929 Importer.Import(E->getOperatorLoc()),
3930 Importer.Import(E->getRParenLoc()));
3931 }
3932
3933 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3934 if (!SubExpr)
3935 return 0;
3936
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003937 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3938 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003939 Importer.Import(E->getOperatorLoc()),
3940 Importer.Import(E->getRParenLoc()));
3941}
3942
Douglas Gregorf638f952010-02-19 01:07:06 +00003943Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3944 QualType T = Importer.Import(E->getType());
3945 if (T.isNull())
3946 return 0;
3947
3948 Expr *LHS = Importer.Import(E->getLHS());
3949 if (!LHS)
3950 return 0;
3951
3952 Expr *RHS = Importer.Import(E->getRHS());
3953 if (!RHS)
3954 return 0;
3955
3956 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003957 T, E->getValueKind(),
3958 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003959 Importer.Import(E->getOperatorLoc()));
3960}
3961
3962Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3963 QualType T = Importer.Import(E->getType());
3964 if (T.isNull())
3965 return 0;
3966
3967 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3968 if (CompLHSType.isNull())
3969 return 0;
3970
3971 QualType CompResultType = Importer.Import(E->getComputationResultType());
3972 if (CompResultType.isNull())
3973 return 0;
3974
3975 Expr *LHS = Importer.Import(E->getLHS());
3976 if (!LHS)
3977 return 0;
3978
3979 Expr *RHS = Importer.Import(E->getRHS());
3980 if (!RHS)
3981 return 0;
3982
3983 return new (Importer.getToContext())
3984 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003985 T, E->getValueKind(),
3986 E->getObjectKind(),
3987 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003988 Importer.Import(E->getOperatorLoc()));
3989}
3990
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003991static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003992 if (E->path_empty()) return false;
3993
3994 // TODO: import cast paths
3995 return true;
3996}
3997
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003998Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3999 QualType T = Importer.Import(E->getType());
4000 if (T.isNull())
4001 return 0;
4002
4003 Expr *SubExpr = Importer.Import(E->getSubExpr());
4004 if (!SubExpr)
4005 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004006
4007 CXXCastPath BasePath;
4008 if (ImportCastPath(E, BasePath))
4009 return 0;
4010
4011 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004012 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004013}
4014
Douglas Gregor008847a2010-02-19 01:32:14 +00004015Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4016 QualType T = Importer.Import(E->getType());
4017 if (T.isNull())
4018 return 0;
4019
4020 Expr *SubExpr = Importer.Import(E->getSubExpr());
4021 if (!SubExpr)
4022 return 0;
4023
4024 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4025 if (!TInfo && E->getTypeInfoAsWritten())
4026 return 0;
4027
John McCallf871d0c2010-08-07 06:22:56 +00004028 CXXCastPath BasePath;
4029 if (ImportCastPath(E, BasePath))
4030 return 0;
4031
John McCallf89e55a2010-11-18 06:31:45 +00004032 return CStyleCastExpr::Create(Importer.getToContext(), T,
4033 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004034 SubExpr, &BasePath, TInfo,
4035 Importer.Import(E->getLParenLoc()),
4036 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004037}
4038
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004039ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004040 ASTContext &FromContext, FileManager &FromFileManager,
4041 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004042 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004043 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4044 Minimal(MinimalImport)
4045{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004046 ImportedDecls[FromContext.getTranslationUnitDecl()]
4047 = ToContext.getTranslationUnitDecl();
4048}
4049
4050ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004051
4052QualType ASTImporter::Import(QualType FromT) {
4053 if (FromT.isNull())
4054 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004055
4056 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004057
Douglas Gregor169fba52010-02-08 15:18:58 +00004058 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004059 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4060 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004061 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004062 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004063
Douglas Gregor169fba52010-02-08 15:18:58 +00004064 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004065 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004066 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004067 if (ToT.isNull())
4068 return ToT;
4069
Douglas Gregor169fba52010-02-08 15:18:58 +00004070 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004071 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004072
John McCallf4c73712011-01-19 06:33:43 +00004073 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004074}
4075
Douglas Gregor9bed8792010-02-09 19:21:46 +00004076TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004077 if (!FromTSI)
4078 return FromTSI;
4079
4080 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004081 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004082 QualType T = Import(FromTSI->getType());
4083 if (T.isNull())
4084 return 0;
4085
4086 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004087 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004088}
4089
4090Decl *ASTImporter::Import(Decl *FromD) {
4091 if (!FromD)
4092 return 0;
4093
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004094 ASTNodeImporter Importer(*this);
4095
Douglas Gregor9bed8792010-02-09 19:21:46 +00004096 // Check whether we've already imported this declaration.
4097 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004098 if (Pos != ImportedDecls.end()) {
4099 Decl *ToD = Pos->second;
4100 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4101 return ToD;
4102 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004103
4104 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004105 Decl *ToD = Importer.Visit(FromD);
4106 if (!ToD)
4107 return 0;
4108
4109 // Record the imported declaration.
4110 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004111
4112 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4113 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004114 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004115 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004116 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004117 // When we've finished transforming a typedef, see whether it was the
4118 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004119 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004120 FromTag = AnonTagsWithPendingTypedefs.begin(),
4121 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4122 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004123 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004124 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4125 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004126 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004127 AnonTagsWithPendingTypedefs.erase(FromTag);
4128 break;
4129 }
4130 }
4131 }
4132 }
4133
Douglas Gregor9bed8792010-02-09 19:21:46 +00004134 return ToD;
4135}
4136
4137DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4138 if (!FromDC)
4139 return FromDC;
4140
4141 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4142}
4143
4144Expr *ASTImporter::Import(Expr *FromE) {
4145 if (!FromE)
4146 return 0;
4147
4148 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4149}
4150
4151Stmt *ASTImporter::Import(Stmt *FromS) {
4152 if (!FromS)
4153 return 0;
4154
Douglas Gregor4800d952010-02-11 19:21:55 +00004155 // Check whether we've already imported this declaration.
4156 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4157 if (Pos != ImportedStmts.end())
4158 return Pos->second;
4159
4160 // Import the type
4161 ASTNodeImporter Importer(*this);
4162 Stmt *ToS = Importer.Visit(FromS);
4163 if (!ToS)
4164 return 0;
4165
4166 // Record the imported declaration.
4167 ImportedStmts[FromS] = ToS;
4168 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004169}
4170
4171NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4172 if (!FromNNS)
4173 return 0;
4174
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004175 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4176
4177 switch (FromNNS->getKind()) {
4178 case NestedNameSpecifier::Identifier:
4179 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4180 return NestedNameSpecifier::Create(ToContext, prefix, II);
4181 }
4182 return 0;
4183
4184 case NestedNameSpecifier::Namespace:
4185 if (NamespaceDecl *NS =
4186 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4187 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4188 }
4189 return 0;
4190
4191 case NestedNameSpecifier::NamespaceAlias:
4192 if (NamespaceAliasDecl *NSAD =
4193 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4194 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4195 }
4196 return 0;
4197
4198 case NestedNameSpecifier::Global:
4199 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4200
4201 case NestedNameSpecifier::TypeSpec:
4202 case NestedNameSpecifier::TypeSpecWithTemplate: {
4203 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4204 if (!T.isNull()) {
4205 bool bTemplate = FromNNS->getKind() ==
4206 NestedNameSpecifier::TypeSpecWithTemplate;
4207 return NestedNameSpecifier::Create(ToContext, prefix,
4208 bTemplate, T.getTypePtr());
4209 }
4210 }
4211 return 0;
4212 }
4213
4214 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004215 return 0;
4216}
4217
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004218NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4219 // FIXME: Implement!
4220 return NestedNameSpecifierLoc();
4221}
4222
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004223TemplateName ASTImporter::Import(TemplateName From) {
4224 switch (From.getKind()) {
4225 case TemplateName::Template:
4226 if (TemplateDecl *ToTemplate
4227 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4228 return TemplateName(ToTemplate);
4229
4230 return TemplateName();
4231
4232 case TemplateName::OverloadedTemplate: {
4233 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4234 UnresolvedSet<2> ToTemplates;
4235 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4236 E = FromStorage->end();
4237 I != E; ++I) {
4238 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4239 ToTemplates.addDecl(To);
4240 else
4241 return TemplateName();
4242 }
4243 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4244 ToTemplates.end());
4245 }
4246
4247 case TemplateName::QualifiedTemplate: {
4248 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4249 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4250 if (!Qualifier)
4251 return TemplateName();
4252
4253 if (TemplateDecl *ToTemplate
4254 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4255 return ToContext.getQualifiedTemplateName(Qualifier,
4256 QTN->hasTemplateKeyword(),
4257 ToTemplate);
4258
4259 return TemplateName();
4260 }
4261
4262 case TemplateName::DependentTemplate: {
4263 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4264 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4265 if (!Qualifier)
4266 return TemplateName();
4267
4268 if (DTN->isIdentifier()) {
4269 return ToContext.getDependentTemplateName(Qualifier,
4270 Import(DTN->getIdentifier()));
4271 }
4272
4273 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4274 }
John McCall14606042011-06-30 08:33:18 +00004275
4276 case TemplateName::SubstTemplateTemplateParm: {
4277 SubstTemplateTemplateParmStorage *subst
4278 = From.getAsSubstTemplateTemplateParm();
4279 TemplateTemplateParmDecl *param
4280 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4281 if (!param)
4282 return TemplateName();
4283
4284 TemplateName replacement = Import(subst->getReplacement());
4285 if (replacement.isNull()) return TemplateName();
4286
4287 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4288 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004289
4290 case TemplateName::SubstTemplateTemplateParmPack: {
4291 SubstTemplateTemplateParmPackStorage *SubstPack
4292 = From.getAsSubstTemplateTemplateParmPack();
4293 TemplateTemplateParmDecl *Param
4294 = cast_or_null<TemplateTemplateParmDecl>(
4295 Import(SubstPack->getParameterPack()));
4296 if (!Param)
4297 return TemplateName();
4298
4299 ASTNodeImporter Importer(*this);
4300 TemplateArgument ArgPack
4301 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4302 if (ArgPack.isNull())
4303 return TemplateName();
4304
4305 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4306 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004307 }
4308
4309 llvm_unreachable("Invalid template name kind");
4310 return TemplateName();
4311}
4312
Douglas Gregor9bed8792010-02-09 19:21:46 +00004313SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4314 if (FromLoc.isInvalid())
4315 return SourceLocation();
4316
Douglas Gregor88523732010-02-10 00:15:17 +00004317 SourceManager &FromSM = FromContext.getSourceManager();
4318
4319 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004320 // don't have to import macro expansions.
4321 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004322 FromLoc = FromSM.getSpellingLoc(FromLoc);
4323 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4324 SourceManager &ToSM = ToContext.getSourceManager();
4325 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004326 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004327}
4328
4329SourceRange ASTImporter::Import(SourceRange FromRange) {
4330 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4331}
4332
Douglas Gregor88523732010-02-10 00:15:17 +00004333FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004334 llvm::DenseMap<FileID, FileID>::iterator Pos
4335 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004336 if (Pos != ImportedFileIDs.end())
4337 return Pos->second;
4338
4339 SourceManager &FromSM = FromContext.getSourceManager();
4340 SourceManager &ToSM = ToContext.getSourceManager();
4341 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004342 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004343
4344 // Include location of this file.
4345 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4346
4347 // Map the FileID for to the "to" source manager.
4348 FileID ToID;
4349 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004350 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004351 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4352 // disk again
4353 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4354 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004355 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004356 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4357 FromSLoc.getFile().getFileCharacteristic());
4358 } else {
4359 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004360 const llvm::MemoryBuffer *
4361 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004362 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004363 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004364 FromBuf->getBufferIdentifier());
4365 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4366 }
4367
4368
Sebastian Redl535a3e22010-09-30 01:03:06 +00004369 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004370 return ToID;
4371}
4372
Douglas Gregord8868a62011-01-18 03:11:38 +00004373void ASTImporter::ImportDefinition(Decl *From) {
4374 Decl *To = Import(From);
4375 if (!To)
4376 return;
4377
4378 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4379 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004380
4381 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4382 if (!ToRecord->getDefinition()) {
4383 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4384 /*ForceImport=*/true);
4385 return;
4386 }
4387 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004388
4389 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4390 if (!ToEnum->getDefinition()) {
4391 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4392 /*ForceImport=*/true);
4393 return;
4394 }
4395 }
4396
Douglas Gregord8868a62011-01-18 03:11:38 +00004397 Importer.ImportDeclContext(FromDC, true);
4398 }
4399}
4400
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004401DeclarationName ASTImporter::Import(DeclarationName FromName) {
4402 if (!FromName)
4403 return DeclarationName();
4404
4405 switch (FromName.getNameKind()) {
4406 case DeclarationName::Identifier:
4407 return Import(FromName.getAsIdentifierInfo());
4408
4409 case DeclarationName::ObjCZeroArgSelector:
4410 case DeclarationName::ObjCOneArgSelector:
4411 case DeclarationName::ObjCMultiArgSelector:
4412 return Import(FromName.getObjCSelector());
4413
4414 case DeclarationName::CXXConstructorName: {
4415 QualType T = Import(FromName.getCXXNameType());
4416 if (T.isNull())
4417 return DeclarationName();
4418
4419 return ToContext.DeclarationNames.getCXXConstructorName(
4420 ToContext.getCanonicalType(T));
4421 }
4422
4423 case DeclarationName::CXXDestructorName: {
4424 QualType T = Import(FromName.getCXXNameType());
4425 if (T.isNull())
4426 return DeclarationName();
4427
4428 return ToContext.DeclarationNames.getCXXDestructorName(
4429 ToContext.getCanonicalType(T));
4430 }
4431
4432 case DeclarationName::CXXConversionFunctionName: {
4433 QualType T = Import(FromName.getCXXNameType());
4434 if (T.isNull())
4435 return DeclarationName();
4436
4437 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4438 ToContext.getCanonicalType(T));
4439 }
4440
4441 case DeclarationName::CXXOperatorName:
4442 return ToContext.DeclarationNames.getCXXOperatorName(
4443 FromName.getCXXOverloadedOperator());
4444
4445 case DeclarationName::CXXLiteralOperatorName:
4446 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4447 Import(FromName.getCXXLiteralIdentifier()));
4448
4449 case DeclarationName::CXXUsingDirective:
4450 // FIXME: STATICS!
4451 return DeclarationName::getUsingDirectiveName();
4452 }
4453
4454 // Silence bogus GCC warning
4455 return DeclarationName();
4456}
4457
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004458IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004459 if (!FromId)
4460 return 0;
4461
4462 return &ToContext.Idents.get(FromId->getName());
4463}
Douglas Gregor089459a2010-02-08 21:09:39 +00004464
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004465Selector ASTImporter::Import(Selector FromSel) {
4466 if (FromSel.isNull())
4467 return Selector();
4468
Chris Lattner5f9e2722011-07-23 10:55:15 +00004469 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004470 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4471 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4472 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4473 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4474}
4475
Douglas Gregor089459a2010-02-08 21:09:39 +00004476DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4477 DeclContext *DC,
4478 unsigned IDNS,
4479 NamedDecl **Decls,
4480 unsigned NumDecls) {
4481 return Name;
4482}
4483
4484DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004485 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004486}
4487
4488DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004489 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004490}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004491
4492Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4493 ImportedDecls[From] = To;
4494 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004495}
Douglas Gregorea35d112010-02-15 23:54:17 +00004496
4497bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004498 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004499 = ImportedTypes.find(From.getTypePtr());
4500 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4501 return true;
4502
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004503 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004504 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004505}