blob: 9ea9a5789dae316a51a32ecb8b755ef9cd46272c [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
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
John McCallf4c73712011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000073 // FIXME: TemplateTypeParmType
74 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000075 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000077 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000078 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000079 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80 QualType VisitObjCObjectType(const ObjCObjectType *T);
81 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000082
83 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000084 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000086 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000087 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000088 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
89 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000090 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000091 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
92 bool ForceImport = false);
93 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
94 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000095 TemplateParameterList *ImportTemplateParameterList(
96 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000097 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
98 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
99 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000102 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000103 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000104 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000105 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000106 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000107 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000108 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000109 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000110 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000111 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000112 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000113 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
114 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
115 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
116 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000117 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000118 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000119 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000120 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000121 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000122 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000123 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000124 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000125 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000126 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000127 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000128 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000129 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000130 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000131 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000132 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000133 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
134 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
135 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
136 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000137 Decl *VisitClassTemplateSpecializationDecl(
138 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000139
Douglas Gregor4800d952010-02-11 19:21:55 +0000140 // Importing statements
141 Stmt *VisitStmt(Stmt *S);
142
143 // Importing expressions
144 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000145 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000146 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000147 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitParenExpr(ParenExpr *E);
149 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000150 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000151 Expr *VisitBinaryOperator(BinaryOperator *E);
152 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000153 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000154 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155 };
156}
157
158//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000159// Structural Equivalence
160//----------------------------------------------------------------------------
161
162namespace {
163 struct StructuralEquivalenceContext {
164 /// \brief AST contexts for which we are checking structural equivalence.
165 ASTContext &C1, &C2;
166
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000167 /// \brief The set of "tentative" equivalences between two canonical
168 /// declarations, mapping from a declaration in the first context to the
169 /// declaration in the second context that we believe to be equivalent.
170 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
171
172 /// \brief Queue of declarations in the first context whose equivalence
173 /// with a declaration in the second context still needs to be verified.
174 std::deque<Decl *> DeclsToCheck;
175
Douglas Gregorea35d112010-02-15 23:54:17 +0000176 /// \brief Declaration (from, to) pairs that are known not to be equivalent
177 /// (which we have already complained about).
178 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
179
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180 /// \brief Whether we're being strict about the spelling of types when
181 /// unifying two types.
182 bool StrictTypeSpelling;
183
184 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000185 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000186 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000187 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000188 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000189
190 /// \brief Determine whether the two declarations are structurally
191 /// equivalent.
192 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
193
194 /// \brief Determine whether the two types are structurally equivalent.
195 bool IsStructurallyEquivalent(QualType T1, QualType T2);
196
197 private:
198 /// \brief Finish checking all of the structural equivalences.
199 ///
200 /// \returns true if an error occurred, false otherwise.
201 bool Finish();
202
203 public:
204 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000205 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000206 }
207
208 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000209 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000210 }
211 };
212}
213
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 QualType T1, QualType T2);
216static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
217 Decl *D1, Decl *D2);
218
219/// \brief Determine if two APInts have the same value, after zero-extending
220/// one of them (if needed!) to ensure that the bit-widths match.
221static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
222 if (I1.getBitWidth() == I2.getBitWidth())
223 return I1 == I2;
224
225 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227
Jay Foad9f71a8f2010-12-07 08:25:34 +0000228 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000229}
230
231/// \brief Determine if two APSInts have the same value, zero- or sign-extending
232/// as needed.
233static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
234 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
235 return I1 == I2;
236
237 // Check for a bit-width mismatch.
238 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000241 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000242
243 // We have a signedness mismatch. Turn the signed value into an unsigned
244 // value.
245 if (I1.isSigned()) {
246 if (I1.isNegative())
247 return false;
248
249 return llvm::APSInt(I1, true) == I2;
250 }
251
252 if (I2.isNegative())
253 return false;
254
255 return I1 == llvm::APSInt(I2, true);
256}
257
258/// \brief Determine structural equivalence of two expressions.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 Expr *E1, Expr *E2) {
261 if (!E1 || !E2)
262 return E1 == E2;
263
264 // FIXME: Actually perform a structural comparison!
265 return true;
266}
267
268/// \brief Determine whether two identifiers are equivalent.
269static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
270 const IdentifierInfo *Name2) {
271 if (!Name1 || !Name2)
272 return Name1 == Name2;
273
274 return Name1->getName() == Name2->getName();
275}
276
277/// \brief Determine whether two nested-name-specifiers are equivalent.
278static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
279 NestedNameSpecifier *NNS1,
280 NestedNameSpecifier *NNS2) {
281 // FIXME: Implement!
282 return true;
283}
284
285/// \brief Determine whether two template arguments are equivalent.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 const TemplateArgument &Arg1,
288 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000289 if (Arg1.getKind() != Arg2.getKind())
290 return false;
291
292 switch (Arg1.getKind()) {
293 case TemplateArgument::Null:
294 return true;
295
296 case TemplateArgument::Type:
297 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
298
299 case TemplateArgument::Integral:
300 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
301 Arg2.getIntegralType()))
302 return false;
303
304 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
305
306 case TemplateArgument::Declaration:
307 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
308
309 case TemplateArgument::Template:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplate(),
312 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000313
314 case TemplateArgument::TemplateExpansion:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsTemplateOrTemplatePattern(),
317 Arg2.getAsTemplateOrTemplatePattern());
318
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000319 case TemplateArgument::Expression:
320 return IsStructurallyEquivalent(Context,
321 Arg1.getAsExpr(), Arg2.getAsExpr());
322
323 case TemplateArgument::Pack:
324 if (Arg1.pack_size() != Arg2.pack_size())
325 return false;
326
327 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
328 if (!IsStructurallyEquivalent(Context,
329 Arg1.pack_begin()[I],
330 Arg2.pack_begin()[I]))
331 return false;
332
333 return true;
334 }
335
336 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000337 return true;
338}
339
340/// \brief Determine structural equivalence for the common part of array
341/// types.
342static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
343 const ArrayType *Array1,
344 const ArrayType *Array2) {
345 if (!IsStructurallyEquivalent(Context,
346 Array1->getElementType(),
347 Array2->getElementType()))
348 return false;
349 if (Array1->getSizeModifier() != Array2->getSizeModifier())
350 return false;
351 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
352 return false;
353
354 return true;
355}
356
357/// \brief Determine structural equivalence of two types.
358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
359 QualType T1, QualType T2) {
360 if (T1.isNull() || T2.isNull())
361 return T1.isNull() && T2.isNull();
362
363 if (!Context.StrictTypeSpelling) {
364 // We aren't being strict about token-to-token equivalence of types,
365 // so map down to the canonical type.
366 T1 = Context.C1.getCanonicalType(T1);
367 T2 = Context.C2.getCanonicalType(T2);
368 }
369
370 if (T1.getQualifiers() != T2.getQualifiers())
371 return false;
372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000374
Douglas Gregorea35d112010-02-15 23:54:17 +0000375 if (T1->getTypeClass() != T2->getTypeClass()) {
376 // Compare function types with prototypes vs. without prototypes as if
377 // both did not have prototypes.
378 if (T1->getTypeClass() == Type::FunctionProto &&
379 T2->getTypeClass() == Type::FunctionNoProto)
380 TC = Type::FunctionNoProto;
381 else if (T1->getTypeClass() == Type::FunctionNoProto &&
382 T2->getTypeClass() == Type::FunctionProto)
383 TC = Type::FunctionNoProto;
384 else
385 return false;
386 }
387
388 switch (TC) {
389 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000390 // FIXME: Deal with Char_S/Char_U.
391 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
392 return false;
393 break;
394
395 case Type::Complex:
396 if (!IsStructurallyEquivalent(Context,
397 cast<ComplexType>(T1)->getElementType(),
398 cast<ComplexType>(T2)->getElementType()))
399 return false;
400 break;
401
402 case Type::Pointer:
403 if (!IsStructurallyEquivalent(Context,
404 cast<PointerType>(T1)->getPointeeType(),
405 cast<PointerType>(T2)->getPointeeType()))
406 return false;
407 break;
408
409 case Type::BlockPointer:
410 if (!IsStructurallyEquivalent(Context,
411 cast<BlockPointerType>(T1)->getPointeeType(),
412 cast<BlockPointerType>(T2)->getPointeeType()))
413 return false;
414 break;
415
416 case Type::LValueReference:
417 case Type::RValueReference: {
418 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
419 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
420 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
421 return false;
422 if (Ref1->isInnerRef() != Ref2->isInnerRef())
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 Ref1->getPointeeTypeAsWritten(),
426 Ref2->getPointeeTypeAsWritten()))
427 return false;
428 break;
429 }
430
431 case Type::MemberPointer: {
432 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
433 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
434 if (!IsStructurallyEquivalent(Context,
435 MemPtr1->getPointeeType(),
436 MemPtr2->getPointeeType()))
437 return false;
438 if (!IsStructurallyEquivalent(Context,
439 QualType(MemPtr1->getClass(), 0),
440 QualType(MemPtr2->getClass(), 0)))
441 return false;
442 break;
443 }
444
445 case Type::ConstantArray: {
446 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
447 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
448 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
449 return false;
450
451 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
452 return false;
453 break;
454 }
455
456 case Type::IncompleteArray:
457 if (!IsArrayStructurallyEquivalent(Context,
458 cast<ArrayType>(T1),
459 cast<ArrayType>(T2)))
460 return false;
461 break;
462
463 case Type::VariableArray: {
464 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
465 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
466 if (!IsStructurallyEquivalent(Context,
467 Array1->getSizeExpr(), Array2->getSizeExpr()))
468 return false;
469
470 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
471 return false;
472
473 break;
474 }
475
476 case Type::DependentSizedArray: {
477 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
478 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Array1->getSizeExpr(), Array2->getSizeExpr()))
481 return false;
482
483 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
484 return false;
485
486 break;
487 }
488
489 case Type::DependentSizedExtVector: {
490 const DependentSizedExtVectorType *Vec1
491 = cast<DependentSizedExtVectorType>(T1);
492 const DependentSizedExtVectorType *Vec2
493 = cast<DependentSizedExtVectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
496 return false;
497 if (!IsStructurallyEquivalent(Context,
498 Vec1->getElementType(),
499 Vec2->getElementType()))
500 return false;
501 break;
502 }
503
504 case Type::Vector:
505 case Type::ExtVector: {
506 const VectorType *Vec1 = cast<VectorType>(T1);
507 const VectorType *Vec2 = cast<VectorType>(T2);
508 if (!IsStructurallyEquivalent(Context,
509 Vec1->getElementType(),
510 Vec2->getElementType()))
511 return false;
512 if (Vec1->getNumElements() != Vec2->getNumElements())
513 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000514 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000516 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000517 }
518
519 case Type::FunctionProto: {
520 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
521 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
522 if (Proto1->getNumArgs() != Proto2->getNumArgs())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getArgType(I),
527 Proto2->getArgType(I)))
528 return false;
529 }
530 if (Proto1->isVariadic() != Proto2->isVariadic())
531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000533 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000534 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
535 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
536 return false;
537 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
538 if (!IsStructurallyEquivalent(Context,
539 Proto1->getExceptionType(I),
540 Proto2->getExceptionType(I)))
541 return false;
542 }
543 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000544 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000545 Proto1->getNoexceptExpr(),
546 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000547 return false;
548 }
549 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
550 return false;
551
552 // Fall through to check the bits common with FunctionNoProtoType.
553 }
554
555 case Type::FunctionNoProto: {
556 const FunctionType *Function1 = cast<FunctionType>(T1);
557 const FunctionType *Function2 = cast<FunctionType>(T2);
558 if (!IsStructurallyEquivalent(Context,
559 Function1->getResultType(),
560 Function2->getResultType()))
561 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000562 if (Function1->getExtInfo() != Function2->getExtInfo())
563 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 break;
565 }
566
567 case Type::UnresolvedUsing:
568 if (!IsStructurallyEquivalent(Context,
569 cast<UnresolvedUsingType>(T1)->getDecl(),
570 cast<UnresolvedUsingType>(T2)->getDecl()))
571 return false;
572
573 break;
John McCall9d156a72011-01-06 01:58:22 +0000574
575 case Type::Attributed:
576 if (!IsStructurallyEquivalent(Context,
577 cast<AttributedType>(T1)->getModifiedType(),
578 cast<AttributedType>(T2)->getModifiedType()))
579 return false;
580 if (!IsStructurallyEquivalent(Context,
581 cast<AttributedType>(T1)->getEquivalentType(),
582 cast<AttributedType>(T2)->getEquivalentType()))
583 return false;
584 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000585
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000586 case Type::Paren:
587 if (!IsStructurallyEquivalent(Context,
588 cast<ParenType>(T1)->getInnerType(),
589 cast<ParenType>(T2)->getInnerType()))
590 return false;
591 break;
592
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000593 case Type::Typedef:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypedefType>(T1)->getDecl(),
596 cast<TypedefType>(T2)->getDecl()))
597 return false;
598 break;
599
600 case Type::TypeOfExpr:
601 if (!IsStructurallyEquivalent(Context,
602 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
603 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::TypeOf:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypeOfType>(T1)->getUnderlyingType(),
610 cast<TypeOfType>(T2)->getUnderlyingType()))
611 return false;
612 break;
Sean Huntca63c202011-05-24 22:41:36 +0000613
614 case Type::UnaryTransform:
615 if (!IsStructurallyEquivalent(Context,
616 cast<UnaryTransformType>(T1)->getUnderlyingType(),
617 cast<UnaryTransformType>(T1)->getUnderlyingType()))
618 return false;
619 break;
620
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000621 case Type::Decltype:
622 if (!IsStructurallyEquivalent(Context,
623 cast<DecltypeType>(T1)->getUnderlyingExpr(),
624 cast<DecltypeType>(T2)->getUnderlyingExpr()))
625 return false;
626 break;
627
Richard Smith34b41d92011-02-20 03:19:35 +0000628 case Type::Auto:
629 if (!IsStructurallyEquivalent(Context,
630 cast<AutoType>(T1)->getDeducedType(),
631 cast<AutoType>(T2)->getDeducedType()))
632 return false;
633 break;
634
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000635 case Type::Record:
636 case Type::Enum:
637 if (!IsStructurallyEquivalent(Context,
638 cast<TagType>(T1)->getDecl(),
639 cast<TagType>(T2)->getDecl()))
640 return false;
641 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000642
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000643 case Type::TemplateTypeParm: {
644 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
645 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
646 if (Parm1->getDepth() != Parm2->getDepth())
647 return false;
648 if (Parm1->getIndex() != Parm2->getIndex())
649 return false;
650 if (Parm1->isParameterPack() != Parm2->isParameterPack())
651 return false;
652
653 // Names of template type parameters are never significant.
654 break;
655 }
656
657 case Type::SubstTemplateTypeParm: {
658 const SubstTemplateTypeParmType *Subst1
659 = cast<SubstTemplateTypeParmType>(T1);
660 const SubstTemplateTypeParmType *Subst2
661 = cast<SubstTemplateTypeParmType>(T2);
662 if (!IsStructurallyEquivalent(Context,
663 QualType(Subst1->getReplacedParameter(), 0),
664 QualType(Subst2->getReplacedParameter(), 0)))
665 return false;
666 if (!IsStructurallyEquivalent(Context,
667 Subst1->getReplacementType(),
668 Subst2->getReplacementType()))
669 return false;
670 break;
671 }
672
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000673 case Type::SubstTemplateTypeParmPack: {
674 const SubstTemplateTypeParmPackType *Subst1
675 = cast<SubstTemplateTypeParmPackType>(T1);
676 const SubstTemplateTypeParmPackType *Subst2
677 = cast<SubstTemplateTypeParmPackType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 QualType(Subst1->getReplacedParameter(), 0),
680 QualType(Subst2->getReplacedParameter(), 0)))
681 return false;
682 if (!IsStructurallyEquivalent(Context,
683 Subst1->getArgumentPack(),
684 Subst2->getArgumentPack()))
685 return false;
686 break;
687 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000688 case Type::TemplateSpecialization: {
689 const TemplateSpecializationType *Spec1
690 = cast<TemplateSpecializationType>(T1);
691 const TemplateSpecializationType *Spec2
692 = cast<TemplateSpecializationType>(T2);
693 if (!IsStructurallyEquivalent(Context,
694 Spec1->getTemplateName(),
695 Spec2->getTemplateName()))
696 return false;
697 if (Spec1->getNumArgs() != Spec2->getNumArgs())
698 return false;
699 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
700 if (!IsStructurallyEquivalent(Context,
701 Spec1->getArg(I), Spec2->getArg(I)))
702 return false;
703 }
704 break;
705 }
706
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000707 case Type::Elaborated: {
708 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
709 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
710 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
711 if (Elab1->getKeyword() != Elab2->getKeyword())
712 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000713 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000714 Elab1->getQualifier(),
715 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000716 return false;
717 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000718 Elab1->getNamedType(),
719 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000720 return false;
721 break;
722 }
723
John McCall3cb0ebd2010-03-10 03:28:59 +0000724 case Type::InjectedClassName: {
725 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
726 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
727 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000728 Inj1->getInjectedSpecializationType(),
729 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000730 return false;
731 break;
732 }
733
Douglas Gregor4714c122010-03-31 17:34:00 +0000734 case Type::DependentName: {
735 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
736 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000737 if (!IsStructurallyEquivalent(Context,
738 Typename1->getQualifier(),
739 Typename2->getQualifier()))
740 return false;
741 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
742 Typename2->getIdentifier()))
743 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000744
745 break;
746 }
747
John McCall33500952010-06-11 00:33:02 +0000748 case Type::DependentTemplateSpecialization: {
749 const DependentTemplateSpecializationType *Spec1 =
750 cast<DependentTemplateSpecializationType>(T1);
751 const DependentTemplateSpecializationType *Spec2 =
752 cast<DependentTemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getQualifier(),
755 Spec2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
758 Spec2->getIdentifier()))
759 return false;
760 if (Spec1->getNumArgs() != Spec2->getNumArgs())
761 return false;
762 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
763 if (!IsStructurallyEquivalent(Context,
764 Spec1->getArg(I), Spec2->getArg(I)))
765 return false;
766 }
767 break;
768 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000769
770 case Type::PackExpansion:
771 if (!IsStructurallyEquivalent(Context,
772 cast<PackExpansionType>(T1)->getPattern(),
773 cast<PackExpansionType>(T2)->getPattern()))
774 return false;
775 break;
776
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000777 case Type::ObjCInterface: {
778 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
779 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Iface1->getDecl(), Iface2->getDecl()))
782 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000783 break;
784 }
785
786 case Type::ObjCObject: {
787 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
788 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
789 if (!IsStructurallyEquivalent(Context,
790 Obj1->getBaseType(),
791 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000792 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000793 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
794 return false;
795 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000797 Obj1->getProtocol(I),
798 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000799 return false;
800 }
801 break;
802 }
803
804 case Type::ObjCObjectPointer: {
805 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
806 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Ptr1->getPointeeType(),
809 Ptr2->getPointeeType()))
810 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000811 break;
812 }
813
814 } // end switch
815
816 return true;
817}
818
819/// \brief Determine structural equivalence of two records.
820static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
821 RecordDecl *D1, RecordDecl *D2) {
822 if (D1->isUnion() != D2->isUnion()) {
823 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
824 << Context.C2.getTypeDeclType(D2);
825 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
826 << D1->getDeclName() << (unsigned)D1->getTagKind();
827 return false;
828 }
829
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000830 // If both declarations are class template specializations, we know
831 // the ODR applies, so check the template and template arguments.
832 ClassTemplateSpecializationDecl *Spec1
833 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
834 ClassTemplateSpecializationDecl *Spec2
835 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
836 if (Spec1 && Spec2) {
837 // Check that the specialized templates are the same.
838 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
839 Spec2->getSpecializedTemplate()))
840 return false;
841
842 // Check that the template arguments are the same.
843 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
844 return false;
845
846 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
847 if (!IsStructurallyEquivalent(Context,
848 Spec1->getTemplateArgs().get(I),
849 Spec2->getTemplateArgs().get(I)))
850 return false;
851 }
852 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000853 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000854 else if (Spec1 || Spec2)
855 return false;
856
Douglas Gregorea35d112010-02-15 23:54:17 +0000857 // Compare the definitions of these two records. If either or both are
858 // incomplete, we assume that they are equivalent.
859 D1 = D1->getDefinition();
860 D2 = D2->getDefinition();
861 if (!D1 || !D2)
862 return true;
863
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000864 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
865 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
866 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
867 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000868 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000869 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000870 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000871 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000872 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000873 return false;
874 }
875
876 // Check the base classes.
877 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
878 BaseEnd1 = D1CXX->bases_end(),
879 Base2 = D2CXX->bases_begin();
880 Base1 != BaseEnd1;
881 ++Base1, ++Base2) {
882 if (!IsStructurallyEquivalent(Context,
883 Base1->getType(), Base2->getType())) {
884 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
885 << Context.C2.getTypeDeclType(D2);
886 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
887 << Base2->getType()
888 << Base2->getSourceRange();
889 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
890 << Base1->getType()
891 << Base1->getSourceRange();
892 return false;
893 }
894
895 // Check virtual vs. non-virtual inheritance mismatch.
896 if (Base1->isVirtual() != Base2->isVirtual()) {
897 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
898 << Context.C2.getTypeDeclType(D2);
899 Context.Diag2(Base2->getSourceRange().getBegin(),
900 diag::note_odr_virtual_base)
901 << Base2->isVirtual() << Base2->getSourceRange();
902 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
903 << Base1->isVirtual()
904 << Base1->getSourceRange();
905 return false;
906 }
907 }
908 } else if (D1CXX->getNumBases() > 0) {
909 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
910 << Context.C2.getTypeDeclType(D2);
911 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
912 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
913 << Base1->getType()
914 << Base1->getSourceRange();
915 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
916 return false;
917 }
918 }
919
920 // Check the fields for consistency.
921 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
922 Field2End = D2->field_end();
923 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
924 Field1End = D1->field_end();
925 Field1 != Field1End;
926 ++Field1, ++Field2) {
927 if (Field2 == Field2End) {
928 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
929 << Context.C2.getTypeDeclType(D2);
930 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
931 << Field1->getDeclName() << Field1->getType();
932 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
933 return false;
934 }
935
936 if (!IsStructurallyEquivalent(Context,
937 Field1->getType(), Field2->getType())) {
938 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
939 << Context.C2.getTypeDeclType(D2);
940 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
941 << Field2->getDeclName() << Field2->getType();
942 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
943 << Field1->getDeclName() << Field1->getType();
944 return false;
945 }
946
947 if (Field1->isBitField() != Field2->isBitField()) {
948 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
949 << Context.C2.getTypeDeclType(D2);
950 if (Field1->isBitField()) {
951 llvm::APSInt Bits;
952 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
953 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
954 << Field1->getDeclName() << Field1->getType()
955 << Bits.toString(10, false);
956 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
957 << Field2->getDeclName();
958 } else {
959 llvm::APSInt Bits;
960 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
961 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
962 << Field2->getDeclName() << Field2->getType()
963 << Bits.toString(10, false);
964 Context.Diag1(Field1->getLocation(),
965 diag::note_odr_not_bit_field)
966 << Field1->getDeclName();
967 }
968 return false;
969 }
970
971 if (Field1->isBitField()) {
972 // Make sure that the bit-fields are the same length.
973 llvm::APSInt Bits1, Bits2;
974 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
975 return false;
976 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
977 return false;
978
979 if (!IsSameValue(Bits1, Bits2)) {
980 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
981 << Context.C2.getTypeDeclType(D2);
982 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
983 << Field2->getDeclName() << Field2->getType()
984 << Bits2.toString(10, false);
985 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
986 << Field1->getDeclName() << Field1->getType()
987 << Bits1.toString(10, false);
988 return false;
989 }
990 }
991 }
992
993 if (Field2 != Field2End) {
994 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
995 << Context.C2.getTypeDeclType(D2);
996 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
997 << Field2->getDeclName() << Field2->getType();
998 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
999 return false;
1000 }
1001
1002 return true;
1003}
1004
1005/// \brief Determine structural equivalence of two enums.
1006static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1007 EnumDecl *D1, EnumDecl *D2) {
1008 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1009 EC2End = D2->enumerator_end();
1010 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1011 EC1End = D1->enumerator_end();
1012 EC1 != EC1End; ++EC1, ++EC2) {
1013 if (EC2 == EC2End) {
1014 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1015 << Context.C2.getTypeDeclType(D2);
1016 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1017 << EC1->getDeclName()
1018 << EC1->getInitVal().toString(10);
1019 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1020 return false;
1021 }
1022
1023 llvm::APSInt Val1 = EC1->getInitVal();
1024 llvm::APSInt Val2 = EC2->getInitVal();
1025 if (!IsSameValue(Val1, Val2) ||
1026 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1027 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1028 << Context.C2.getTypeDeclType(D2);
1029 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1030 << EC2->getDeclName()
1031 << EC2->getInitVal().toString(10);
1032 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1033 << EC1->getDeclName()
1034 << EC1->getInitVal().toString(10);
1035 return false;
1036 }
1037 }
1038
1039 if (EC2 != EC2End) {
1040 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1041 << Context.C2.getTypeDeclType(D2);
1042 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1043 << EC2->getDeclName()
1044 << EC2->getInitVal().toString(10);
1045 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1046 return false;
1047 }
1048
1049 return true;
1050}
Douglas Gregor040afae2010-11-30 19:14:50 +00001051
1052static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1053 TemplateParameterList *Params1,
1054 TemplateParameterList *Params2) {
1055 if (Params1->size() != Params2->size()) {
1056 Context.Diag2(Params2->getTemplateLoc(),
1057 diag::err_odr_different_num_template_parameters)
1058 << Params1->size() << Params2->size();
1059 Context.Diag1(Params1->getTemplateLoc(),
1060 diag::note_odr_template_parameter_list);
1061 return false;
1062 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001063
Douglas Gregor040afae2010-11-30 19:14:50 +00001064 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1065 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1066 Context.Diag2(Params2->getParam(I)->getLocation(),
1067 diag::err_odr_different_template_parameter_kind);
1068 Context.Diag1(Params1->getParam(I)->getLocation(),
1069 diag::note_odr_template_parameter_here);
1070 return false;
1071 }
1072
1073 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1074 Params2->getParam(I))) {
1075
1076 return false;
1077 }
1078 }
1079
1080 return true;
1081}
1082
1083static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1084 TemplateTypeParmDecl *D1,
1085 TemplateTypeParmDecl *D2) {
1086 if (D1->isParameterPack() != D2->isParameterPack()) {
1087 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1088 << D2->isParameterPack();
1089 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1090 << D1->isParameterPack();
1091 return false;
1092 }
1093
1094 return true;
1095}
1096
1097static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1098 NonTypeTemplateParmDecl *D1,
1099 NonTypeTemplateParmDecl *D2) {
1100 // FIXME: Enable once we have variadic templates.
1101#if 0
1102 if (D1->isParameterPack() != D2->isParameterPack()) {
1103 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1104 << D2->isParameterPack();
1105 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1106 << D1->isParameterPack();
1107 return false;
1108 }
1109#endif
1110
1111 // Check types.
1112 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1113 Context.Diag2(D2->getLocation(),
1114 diag::err_odr_non_type_parameter_type_inconsistent)
1115 << D2->getType() << D1->getType();
1116 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1117 << D1->getType();
1118 return false;
1119 }
1120
1121 return true;
1122}
1123
1124static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1125 TemplateTemplateParmDecl *D1,
1126 TemplateTemplateParmDecl *D2) {
1127 // FIXME: Enable once we have variadic templates.
1128#if 0
1129 if (D1->isParameterPack() != D2->isParameterPack()) {
1130 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1131 << D2->isParameterPack();
1132 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1133 << D1->isParameterPack();
1134 return false;
1135 }
1136#endif
1137
1138 // Check template parameter lists.
1139 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1140 D2->getTemplateParameters());
1141}
1142
1143static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1144 ClassTemplateDecl *D1,
1145 ClassTemplateDecl *D2) {
1146 // Check template parameters.
1147 if (!IsStructurallyEquivalent(Context,
1148 D1->getTemplateParameters(),
1149 D2->getTemplateParameters()))
1150 return false;
1151
1152 // Check the templated declaration.
1153 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1154 D2->getTemplatedDecl());
1155}
1156
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001157/// \brief Determine structural equivalence of two declarations.
1158static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1159 Decl *D1, Decl *D2) {
1160 // FIXME: Check for known structural equivalences via a callback of some sort.
1161
Douglas Gregorea35d112010-02-15 23:54:17 +00001162 // Check whether we already know that these two declarations are not
1163 // structurally equivalent.
1164 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1165 D2->getCanonicalDecl())))
1166 return false;
1167
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001168 // Determine whether we've already produced a tentative equivalence for D1.
1169 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1170 if (EquivToD1)
1171 return EquivToD1 == D2->getCanonicalDecl();
1172
1173 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1174 EquivToD1 = D2->getCanonicalDecl();
1175 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1176 return true;
1177}
1178
1179bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1180 Decl *D2) {
1181 if (!::IsStructurallyEquivalent(*this, D1, D2))
1182 return false;
1183
1184 return !Finish();
1185}
1186
1187bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1188 QualType T2) {
1189 if (!::IsStructurallyEquivalent(*this, T1, T2))
1190 return false;
1191
1192 return !Finish();
1193}
1194
1195bool StructuralEquivalenceContext::Finish() {
1196 while (!DeclsToCheck.empty()) {
1197 // Check the next declaration.
1198 Decl *D1 = DeclsToCheck.front();
1199 DeclsToCheck.pop_front();
1200
1201 Decl *D2 = TentativeEquivalences[D1];
1202 assert(D2 && "Unrecorded tentative equivalence?");
1203
Douglas Gregorea35d112010-02-15 23:54:17 +00001204 bool Equivalent = true;
1205
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001206 // FIXME: Switch on all declaration kinds. For now, we're just going to
1207 // check the obvious ones.
1208 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1209 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1210 // Check for equivalent structure names.
1211 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001212 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1213 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001214 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001215 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1216 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001217 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1218 !::IsStructurallyEquivalent(*this, Record1, Record2))
1219 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001220 } else {
1221 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001222 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001223 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001224 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001225 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1226 // Check for equivalent enum names.
1227 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001228 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1229 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001230 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001231 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1232 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001233 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1234 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1235 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001236 } else {
1237 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001238 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001239 }
Richard Smith162e1c12011-04-15 14:24:37 +00001240 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1241 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001242 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001243 Typedef2->getIdentifier()) ||
1244 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001245 Typedef1->getUnderlyingType(),
1246 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001247 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001248 } else {
1249 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001250 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001251 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001252 } else if (ClassTemplateDecl *ClassTemplate1
1253 = dyn_cast<ClassTemplateDecl>(D1)) {
1254 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1255 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1256 ClassTemplate2->getIdentifier()) ||
1257 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1258 Equivalent = false;
1259 } else {
1260 // Class template/non-class-template mismatch.
1261 Equivalent = false;
1262 }
1263 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1264 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1265 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1266 Equivalent = false;
1267 } else {
1268 // Kind mismatch.
1269 Equivalent = false;
1270 }
1271 } else if (NonTypeTemplateParmDecl *NTTP1
1272 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1273 if (NonTypeTemplateParmDecl *NTTP2
1274 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1275 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1276 Equivalent = false;
1277 } else {
1278 // Kind mismatch.
1279 Equivalent = false;
1280 }
1281 } else if (TemplateTemplateParmDecl *TTP1
1282 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1283 if (TemplateTemplateParmDecl *TTP2
1284 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1285 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1286 Equivalent = false;
1287 } else {
1288 // Kind mismatch.
1289 Equivalent = false;
1290 }
1291 }
1292
Douglas Gregorea35d112010-02-15 23:54:17 +00001293 if (!Equivalent) {
1294 // Note that these two declarations are not equivalent (and we already
1295 // know about it).
1296 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1297 D2->getCanonicalDecl()));
1298 return true;
1299 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001300 // FIXME: Check other declaration kinds!
1301 }
1302
1303 return false;
1304}
1305
1306//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001307// Import Types
1308//----------------------------------------------------------------------------
1309
John McCallf4c73712011-01-19 06:33:43 +00001310QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001311 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1312 << T->getTypeClassName();
1313 return QualType();
1314}
1315
John McCallf4c73712011-01-19 06:33:43 +00001316QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001317 switch (T->getKind()) {
1318 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1319 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1320
1321 case BuiltinType::Char_U:
1322 // The context we're importing from has an unsigned 'char'. If we're
1323 // importing into a context with a signed 'char', translate to
1324 // 'unsigned char' instead.
1325 if (Importer.getToContext().getLangOptions().CharIsSigned)
1326 return Importer.getToContext().UnsignedCharTy;
1327
1328 return Importer.getToContext().CharTy;
1329
1330 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1331
1332 case BuiltinType::Char16:
1333 // FIXME: Make sure that the "to" context supports C++!
1334 return Importer.getToContext().Char16Ty;
1335
1336 case BuiltinType::Char32:
1337 // FIXME: Make sure that the "to" context supports C++!
1338 return Importer.getToContext().Char32Ty;
1339
1340 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1341 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1342 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1343 case BuiltinType::ULongLong:
1344 return Importer.getToContext().UnsignedLongLongTy;
1345 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1346
1347 case BuiltinType::Char_S:
1348 // The context we're importing from has an unsigned 'char'. If we're
1349 // importing into a context with a signed 'char', translate to
1350 // 'unsigned char' instead.
1351 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1352 return Importer.getToContext().SignedCharTy;
1353
1354 return Importer.getToContext().CharTy;
1355
1356 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001357 case BuiltinType::WChar_S:
1358 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001359 // FIXME: If not in C++, shall we translate to the C equivalent of
1360 // wchar_t?
1361 return Importer.getToContext().WCharTy;
1362
1363 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1364 case BuiltinType::Int : return Importer.getToContext().IntTy;
1365 case BuiltinType::Long : return Importer.getToContext().LongTy;
1366 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1367 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1368 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1369 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1370 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1371
1372 case BuiltinType::NullPtr:
1373 // FIXME: Make sure that the "to" context supports C++0x!
1374 return Importer.getToContext().NullPtrTy;
1375
1376 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1377 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001378 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001379 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001380
1381 case BuiltinType::ObjCId:
1382 // FIXME: Make sure that the "to" context supports Objective-C!
1383 return Importer.getToContext().ObjCBuiltinIdTy;
1384
1385 case BuiltinType::ObjCClass:
1386 return Importer.getToContext().ObjCBuiltinClassTy;
1387
1388 case BuiltinType::ObjCSel:
1389 return Importer.getToContext().ObjCBuiltinSelTy;
1390 }
1391
1392 return QualType();
1393}
1394
John McCallf4c73712011-01-19 06:33:43 +00001395QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001396 QualType ToElementType = Importer.Import(T->getElementType());
1397 if (ToElementType.isNull())
1398 return QualType();
1399
1400 return Importer.getToContext().getComplexType(ToElementType);
1401}
1402
John McCallf4c73712011-01-19 06:33:43 +00001403QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001404 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1405 if (ToPointeeType.isNull())
1406 return QualType();
1407
1408 return Importer.getToContext().getPointerType(ToPointeeType);
1409}
1410
John McCallf4c73712011-01-19 06:33:43 +00001411QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001412 // FIXME: Check for blocks support in "to" context.
1413 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1414 if (ToPointeeType.isNull())
1415 return QualType();
1416
1417 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1418}
1419
John McCallf4c73712011-01-19 06:33:43 +00001420QualType
1421ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001422 // FIXME: Check for C++ support in "to" context.
1423 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1424 if (ToPointeeType.isNull())
1425 return QualType();
1426
1427 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1428}
1429
John McCallf4c73712011-01-19 06:33:43 +00001430QualType
1431ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001432 // FIXME: Check for C++0x support in "to" context.
1433 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1434 if (ToPointeeType.isNull())
1435 return QualType();
1436
1437 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1438}
1439
John McCallf4c73712011-01-19 06:33:43 +00001440QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001441 // FIXME: Check for C++ support in "to" context.
1442 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1443 if (ToPointeeType.isNull())
1444 return QualType();
1445
1446 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1447 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1448 ClassType.getTypePtr());
1449}
1450
John McCallf4c73712011-01-19 06:33:43 +00001451QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001452 QualType ToElementType = Importer.Import(T->getElementType());
1453 if (ToElementType.isNull())
1454 return QualType();
1455
1456 return Importer.getToContext().getConstantArrayType(ToElementType,
1457 T->getSize(),
1458 T->getSizeModifier(),
1459 T->getIndexTypeCVRQualifiers());
1460}
1461
John McCallf4c73712011-01-19 06:33:43 +00001462QualType
1463ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001464 QualType ToElementType = Importer.Import(T->getElementType());
1465 if (ToElementType.isNull())
1466 return QualType();
1467
1468 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1469 T->getSizeModifier(),
1470 T->getIndexTypeCVRQualifiers());
1471}
1472
John McCallf4c73712011-01-19 06:33:43 +00001473QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001474 QualType ToElementType = Importer.Import(T->getElementType());
1475 if (ToElementType.isNull())
1476 return QualType();
1477
1478 Expr *Size = Importer.Import(T->getSizeExpr());
1479 if (!Size)
1480 return QualType();
1481
1482 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1483 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1484 T->getSizeModifier(),
1485 T->getIndexTypeCVRQualifiers(),
1486 Brackets);
1487}
1488
John McCallf4c73712011-01-19 06:33:43 +00001489QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490 QualType ToElementType = Importer.Import(T->getElementType());
1491 if (ToElementType.isNull())
1492 return QualType();
1493
1494 return Importer.getToContext().getVectorType(ToElementType,
1495 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001496 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001497}
1498
John McCallf4c73712011-01-19 06:33:43 +00001499QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001500 QualType ToElementType = Importer.Import(T->getElementType());
1501 if (ToElementType.isNull())
1502 return QualType();
1503
1504 return Importer.getToContext().getExtVectorType(ToElementType,
1505 T->getNumElements());
1506}
1507
John McCallf4c73712011-01-19 06:33:43 +00001508QualType
1509ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001510 // FIXME: What happens if we're importing a function without a prototype
1511 // into C++? Should we make it variadic?
1512 QualType ToResultType = Importer.Import(T->getResultType());
1513 if (ToResultType.isNull())
1514 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001515
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001516 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001517 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001518}
1519
John McCallf4c73712011-01-19 06:33:43 +00001520QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001521 QualType ToResultType = Importer.Import(T->getResultType());
1522 if (ToResultType.isNull())
1523 return QualType();
1524
1525 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001526 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001527 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1528 AEnd = T->arg_type_end();
1529 A != AEnd; ++A) {
1530 QualType ArgType = Importer.Import(*A);
1531 if (ArgType.isNull())
1532 return QualType();
1533 ArgTypes.push_back(ArgType);
1534 }
1535
1536 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001537 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001538 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1539 EEnd = T->exception_end();
1540 E != EEnd; ++E) {
1541 QualType ExceptionType = Importer.Import(*E);
1542 if (ExceptionType.isNull())
1543 return QualType();
1544 ExceptionTypes.push_back(ExceptionType);
1545 }
John McCalle23cf432010-12-14 08:05:40 +00001546
1547 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1548 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001549
1550 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001551 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001552}
1553
Sean Callanan0aeb2892011-08-11 16:56:07 +00001554QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1555 QualType ToInnerType = Importer.Import(T->getInnerType());
1556 if (ToInnerType.isNull())
1557 return QualType();
1558
1559 return Importer.getToContext().getParenType(ToInnerType);
1560}
1561
John McCallf4c73712011-01-19 06:33:43 +00001562QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001563 TypedefNameDecl *ToDecl
1564 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001565 if (!ToDecl)
1566 return QualType();
1567
1568 return Importer.getToContext().getTypeDeclType(ToDecl);
1569}
1570
John McCallf4c73712011-01-19 06:33:43 +00001571QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001572 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1573 if (!ToExpr)
1574 return QualType();
1575
1576 return Importer.getToContext().getTypeOfExprType(ToExpr);
1577}
1578
John McCallf4c73712011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001580 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1581 if (ToUnderlyingType.isNull())
1582 return QualType();
1583
1584 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1585}
1586
John McCallf4c73712011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001588 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1590 if (!ToExpr)
1591 return QualType();
1592
1593 return Importer.getToContext().getDecltypeType(ToExpr);
1594}
1595
Sean Huntca63c202011-05-24 22:41:36 +00001596QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1597 QualType ToBaseType = Importer.Import(T->getBaseType());
1598 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1599 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1600 return QualType();
1601
1602 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1603 ToUnderlyingType,
1604 T->getUTTKind());
1605}
1606
Richard Smith34b41d92011-02-20 03:19:35 +00001607QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1608 // FIXME: Make sure that the "to" context supports C++0x!
1609 QualType FromDeduced = T->getDeducedType();
1610 QualType ToDeduced;
1611 if (!FromDeduced.isNull()) {
1612 ToDeduced = Importer.Import(FromDeduced);
1613 if (ToDeduced.isNull())
1614 return QualType();
1615 }
1616
1617 return Importer.getToContext().getAutoType(ToDeduced);
1618}
1619
John McCallf4c73712011-01-19 06:33:43 +00001620QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001621 RecordDecl *ToDecl
1622 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1623 if (!ToDecl)
1624 return QualType();
1625
1626 return Importer.getToContext().getTagDeclType(ToDecl);
1627}
1628
John McCallf4c73712011-01-19 06:33:43 +00001629QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001630 EnumDecl *ToDecl
1631 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1632 if (!ToDecl)
1633 return QualType();
1634
1635 return Importer.getToContext().getTagDeclType(ToDecl);
1636}
1637
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001638QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001639 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001640 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1641 if (ToTemplate.isNull())
1642 return QualType();
1643
Chris Lattner5f9e2722011-07-23 10:55:15 +00001644 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001645 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1646 return QualType();
1647
1648 QualType ToCanonType;
1649 if (!QualType(T, 0).isCanonical()) {
1650 QualType FromCanonType
1651 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1652 ToCanonType =Importer.Import(FromCanonType);
1653 if (ToCanonType.isNull())
1654 return QualType();
1655 }
1656 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1657 ToTemplateArgs.data(),
1658 ToTemplateArgs.size(),
1659 ToCanonType);
1660}
1661
John McCallf4c73712011-01-19 06:33:43 +00001662QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001663 NestedNameSpecifier *ToQualifier = 0;
1664 // Note: the qualifier in an ElaboratedType is optional.
1665 if (T->getQualifier()) {
1666 ToQualifier = Importer.Import(T->getQualifier());
1667 if (!ToQualifier)
1668 return QualType();
1669 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001670
1671 QualType ToNamedType = Importer.Import(T->getNamedType());
1672 if (ToNamedType.isNull())
1673 return QualType();
1674
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001675 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1676 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001677}
1678
John McCallf4c73712011-01-19 06:33:43 +00001679QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001680 ObjCInterfaceDecl *Class
1681 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1682 if (!Class)
1683 return QualType();
1684
John McCallc12c5bb2010-05-15 11:32:37 +00001685 return Importer.getToContext().getObjCInterfaceType(Class);
1686}
1687
John McCallf4c73712011-01-19 06:33:43 +00001688QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001689 QualType ToBaseType = Importer.Import(T->getBaseType());
1690 if (ToBaseType.isNull())
1691 return QualType();
1692
Chris Lattner5f9e2722011-07-23 10:55:15 +00001693 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001694 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001695 PEnd = T->qual_end();
1696 P != PEnd; ++P) {
1697 ObjCProtocolDecl *Protocol
1698 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1699 if (!Protocol)
1700 return QualType();
1701 Protocols.push_back(Protocol);
1702 }
1703
John McCallc12c5bb2010-05-15 11:32:37 +00001704 return Importer.getToContext().getObjCObjectType(ToBaseType,
1705 Protocols.data(),
1706 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001707}
1708
John McCallf4c73712011-01-19 06:33:43 +00001709QualType
1710ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001711 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1712 if (ToPointeeType.isNull())
1713 return QualType();
1714
John McCallc12c5bb2010-05-15 11:32:37 +00001715 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001716}
1717
Douglas Gregor089459a2010-02-08 21:09:39 +00001718//----------------------------------------------------------------------------
1719// Import Declarations
1720//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001721bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1722 DeclContext *&LexicalDC,
1723 DeclarationName &Name,
1724 SourceLocation &Loc) {
1725 // Import the context of this declaration.
1726 DC = Importer.ImportContext(D->getDeclContext());
1727 if (!DC)
1728 return true;
1729
1730 LexicalDC = DC;
1731 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1732 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1733 if (!LexicalDC)
1734 return true;
1735 }
1736
1737 // Import the name of this declaration.
1738 Name = Importer.Import(D->getDeclName());
1739 if (D->getDeclName() && !Name)
1740 return true;
1741
1742 // Import the location of this declaration.
1743 Loc = Importer.Import(D->getLocation());
1744 return false;
1745}
1746
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001747void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1748 if (!FromD)
1749 return;
1750
1751 if (!ToD) {
1752 ToD = Importer.Import(FromD);
1753 if (!ToD)
1754 return;
1755 }
1756
1757 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1758 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1759 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1760 ImportDefinition(FromRecord, ToRecord);
1761 }
1762 }
1763 return;
1764 }
1765
1766 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1767 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1768 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1769 ImportDefinition(FromEnum, ToEnum);
1770 }
1771 }
1772 return;
1773 }
1774}
1775
Abramo Bagnara25777432010-08-11 22:01:17 +00001776void
1777ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1778 DeclarationNameInfo& To) {
1779 // NOTE: To.Name and To.Loc are already imported.
1780 // We only have to import To.LocInfo.
1781 switch (To.getName().getNameKind()) {
1782 case DeclarationName::Identifier:
1783 case DeclarationName::ObjCZeroArgSelector:
1784 case DeclarationName::ObjCOneArgSelector:
1785 case DeclarationName::ObjCMultiArgSelector:
1786 case DeclarationName::CXXUsingDirective:
1787 return;
1788
1789 case DeclarationName::CXXOperatorName: {
1790 SourceRange Range = From.getCXXOperatorNameRange();
1791 To.setCXXOperatorNameRange(Importer.Import(Range));
1792 return;
1793 }
1794 case DeclarationName::CXXLiteralOperatorName: {
1795 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1796 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1797 return;
1798 }
1799 case DeclarationName::CXXConstructorName:
1800 case DeclarationName::CXXDestructorName:
1801 case DeclarationName::CXXConversionFunctionName: {
1802 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1803 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1804 return;
1805 }
1806 assert(0 && "Unknown name kind.");
1807 }
1808}
1809
Douglas Gregord8868a62011-01-18 03:11:38 +00001810void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1811 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001812 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001813 return;
1814 }
1815
Douglas Gregor083a8212010-02-21 18:24:45 +00001816 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1817 FromEnd = FromDC->decls_end();
1818 From != FromEnd;
1819 ++From)
1820 Importer.Import(*From);
1821}
1822
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001823bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1824 bool ForceImport) {
1825 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001826 return false;
1827
1828 To->startDefinition();
1829
1830 // Add base classes.
1831 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1832 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1833
Chris Lattner5f9e2722011-07-23 10:55:15 +00001834 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001835 for (CXXRecordDecl::base_class_iterator
1836 Base1 = FromCXX->bases_begin(),
1837 FromBaseEnd = FromCXX->bases_end();
1838 Base1 != FromBaseEnd;
1839 ++Base1) {
1840 QualType T = Importer.Import(Base1->getType());
1841 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001842 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001843
1844 SourceLocation EllipsisLoc;
1845 if (Base1->isPackExpansion())
1846 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001847
1848 // Ensure that we have a definition for the base.
1849 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1850
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001851 Bases.push_back(
1852 new (Importer.getToContext())
1853 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1854 Base1->isVirtual(),
1855 Base1->isBaseOfClass(),
1856 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001857 Importer.Import(Base1->getTypeSourceInfo()),
1858 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001859 }
1860 if (!Bases.empty())
1861 ToCXX->setBases(Bases.data(), Bases.size());
1862 }
1863
Sean Callanan673e7752011-07-19 22:38:25 +00001864 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001865 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001866 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001867}
1868
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001869bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1870 bool ForceImport) {
1871 if (To->getDefinition() || To->isBeingDefined())
1872 return false;
1873
1874 To->startDefinition();
1875
1876 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1877 if (T.isNull())
1878 return true;
1879
1880 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1881 if (ToPromotionType.isNull())
1882 return true;
1883
1884 ImportDeclContext(From, ForceImport);
1885
1886 // FIXME: we might need to merge the number of positive or negative bits
1887 // if the enumerator lists don't match.
1888 To->completeDefinition(T, ToPromotionType,
1889 From->getNumPositiveBits(),
1890 From->getNumNegativeBits());
1891 return false;
1892}
1893
Douglas Gregor040afae2010-11-30 19:14:50 +00001894TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1895 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001896 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001897 ToParams.reserve(Params->size());
1898 for (TemplateParameterList::iterator P = Params->begin(),
1899 PEnd = Params->end();
1900 P != PEnd; ++P) {
1901 Decl *To = Importer.Import(*P);
1902 if (!To)
1903 return 0;
1904
1905 ToParams.push_back(cast<NamedDecl>(To));
1906 }
1907
1908 return TemplateParameterList::Create(Importer.getToContext(),
1909 Importer.Import(Params->getTemplateLoc()),
1910 Importer.Import(Params->getLAngleLoc()),
1911 ToParams.data(), ToParams.size(),
1912 Importer.Import(Params->getRAngleLoc()));
1913}
1914
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001915TemplateArgument
1916ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1917 switch (From.getKind()) {
1918 case TemplateArgument::Null:
1919 return TemplateArgument();
1920
1921 case TemplateArgument::Type: {
1922 QualType ToType = Importer.Import(From.getAsType());
1923 if (ToType.isNull())
1924 return TemplateArgument();
1925 return TemplateArgument(ToType);
1926 }
1927
1928 case TemplateArgument::Integral: {
1929 QualType ToType = Importer.Import(From.getIntegralType());
1930 if (ToType.isNull())
1931 return TemplateArgument();
1932 return TemplateArgument(*From.getAsIntegral(), ToType);
1933 }
1934
1935 case TemplateArgument::Declaration:
1936 if (Decl *To = Importer.Import(From.getAsDecl()))
1937 return TemplateArgument(To);
1938 return TemplateArgument();
1939
1940 case TemplateArgument::Template: {
1941 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1942 if (ToTemplate.isNull())
1943 return TemplateArgument();
1944
1945 return TemplateArgument(ToTemplate);
1946 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001947
1948 case TemplateArgument::TemplateExpansion: {
1949 TemplateName ToTemplate
1950 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1951 if (ToTemplate.isNull())
1952 return TemplateArgument();
1953
Douglas Gregor2be29f42011-01-14 23:41:42 +00001954 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001955 }
1956
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001957 case TemplateArgument::Expression:
1958 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1959 return TemplateArgument(ToExpr);
1960 return TemplateArgument();
1961
1962 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001963 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001964 ToPack.reserve(From.pack_size());
1965 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1966 return TemplateArgument();
1967
1968 TemplateArgument *ToArgs
1969 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1970 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1971 return TemplateArgument(ToArgs, ToPack.size());
1972 }
1973 }
1974
1975 llvm_unreachable("Invalid template argument kind");
1976 return TemplateArgument();
1977}
1978
1979bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1980 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001981 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001982 for (unsigned I = 0; I != NumFromArgs; ++I) {
1983 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1984 if (To.isNull() && !FromArgs[I].isNull())
1985 return true;
1986
1987 ToArgs.push_back(To);
1988 }
1989
1990 return false;
1991}
1992
Douglas Gregor96a01b42010-02-11 00:48:18 +00001993bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001994 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001995 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001996 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001997 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001998 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001999}
2000
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002001bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002002 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002003 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002004 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002005 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002006}
2007
Douglas Gregor040afae2010-11-30 19:14:50 +00002008bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2009 ClassTemplateDecl *To) {
2010 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2011 Importer.getToContext(),
2012 Importer.getNonEquivalentDecls());
2013 return Ctx.IsStructurallyEquivalent(From, To);
2014}
2015
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002016Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002017 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002018 << D->getDeclKindName();
2019 return 0;
2020}
2021
Douglas Gregor788c62d2010-02-21 18:26:36 +00002022Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2023 // Import the major distinguishing characteristics of this namespace.
2024 DeclContext *DC, *LexicalDC;
2025 DeclarationName Name;
2026 SourceLocation Loc;
2027 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2028 return 0;
2029
2030 NamespaceDecl *MergeWithNamespace = 0;
2031 if (!Name) {
2032 // This is an anonymous namespace. Adopt an existing anonymous
2033 // namespace if we can.
2034 // FIXME: Not testable.
2035 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2036 MergeWithNamespace = TU->getAnonymousNamespace();
2037 else
2038 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2039 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002040 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002041 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2042 Lookup.first != Lookup.second;
2043 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00002044 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002045 continue;
2046
2047 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2048 MergeWithNamespace = FoundNS;
2049 ConflictingDecls.clear();
2050 break;
2051 }
2052
2053 ConflictingDecls.push_back(*Lookup.first);
2054 }
2055
2056 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002057 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002058 ConflictingDecls.data(),
2059 ConflictingDecls.size());
2060 }
2061 }
2062
2063 // Create the "to" namespace, if needed.
2064 NamespaceDecl *ToNamespace = MergeWithNamespace;
2065 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002066 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2067 Importer.Import(D->getLocStart()),
2068 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002069 ToNamespace->setLexicalDeclContext(LexicalDC);
2070 LexicalDC->addDecl(ToNamespace);
2071
2072 // If this is an anonymous namespace, register it as the anonymous
2073 // namespace within its context.
2074 if (!Name) {
2075 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2076 TU->setAnonymousNamespace(ToNamespace);
2077 else
2078 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2079 }
2080 }
2081 Importer.Imported(D, ToNamespace);
2082
2083 ImportDeclContext(D);
2084
2085 return ToNamespace;
2086}
2087
Richard Smith162e1c12011-04-15 14:24:37 +00002088Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002089 // Import the major distinguishing characteristics of this typedef.
2090 DeclContext *DC, *LexicalDC;
2091 DeclarationName Name;
2092 SourceLocation Loc;
2093 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2094 return 0;
2095
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002096 // If this typedef is not in block scope, determine whether we've
2097 // seen a typedef with the same name (that we can merge with) or any
2098 // other entity by that name (which name lookup could conflict with).
2099 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002100 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002101 unsigned IDNS = Decl::IDNS_Ordinary;
2102 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2103 Lookup.first != Lookup.second;
2104 ++Lookup.first) {
2105 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2106 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002107 if (TypedefNameDecl *FoundTypedef =
2108 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002109 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2110 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002111 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002112 }
2113
2114 ConflictingDecls.push_back(*Lookup.first);
2115 }
2116
2117 if (!ConflictingDecls.empty()) {
2118 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2119 ConflictingDecls.data(),
2120 ConflictingDecls.size());
2121 if (!Name)
2122 return 0;
2123 }
2124 }
2125
Douglas Gregorea35d112010-02-15 23:54:17 +00002126 // Import the underlying type of this typedef;
2127 QualType T = Importer.Import(D->getUnderlyingType());
2128 if (T.isNull())
2129 return 0;
2130
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002131 // Create the new typedef node.
2132 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002133 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002134 TypedefNameDecl *ToTypedef;
2135 if (IsAlias)
2136 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2137 StartL, Loc,
2138 Name.getAsIdentifierInfo(),
2139 TInfo);
2140 else
2141 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2142 StartL, Loc,
2143 Name.getAsIdentifierInfo(),
2144 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002145 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002146 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002147 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002148 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002149
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002150 return ToTypedef;
2151}
2152
Richard Smith162e1c12011-04-15 14:24:37 +00002153Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2154 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2155}
2156
2157Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2158 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2159}
2160
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002161Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2162 // Import the major distinguishing characteristics of this enum.
2163 DeclContext *DC, *LexicalDC;
2164 DeclarationName Name;
2165 SourceLocation Loc;
2166 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2167 return 0;
2168
2169 // Figure out what enum name we're looking for.
2170 unsigned IDNS = Decl::IDNS_Tag;
2171 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002172 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2173 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002174 IDNS = Decl::IDNS_Ordinary;
2175 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2176 IDNS |= Decl::IDNS_Ordinary;
2177
2178 // We may already have an enum of the same name; try to find and match it.
2179 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002180 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002181 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2182 Lookup.first != Lookup.second;
2183 ++Lookup.first) {
2184 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2185 continue;
2186
2187 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002188 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002189 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2190 Found = Tag->getDecl();
2191 }
2192
2193 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002194 if (IsStructuralMatch(D, FoundEnum))
2195 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002196 }
2197
2198 ConflictingDecls.push_back(*Lookup.first);
2199 }
2200
2201 if (!ConflictingDecls.empty()) {
2202 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2203 ConflictingDecls.data(),
2204 ConflictingDecls.size());
2205 }
2206 }
2207
2208 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002209 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2210 Importer.Import(D->getLocStart()),
2211 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002212 D->isScoped(), D->isScopedUsingClassTag(),
2213 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002214 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002215 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002216 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002217 D2->setLexicalDeclContext(LexicalDC);
2218 Importer.Imported(D, D2);
2219 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002220
2221 // Import the integer type.
2222 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2223 if (ToIntegerType.isNull())
2224 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002225 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002226
2227 // Import the definition
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002228 if (D->isDefinition() && ImportDefinition(D, D2))
2229 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002230
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002231 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002232}
2233
Douglas Gregor96a01b42010-02-11 00:48:18 +00002234Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2235 // If this record has a definition in the translation unit we're coming from,
2236 // but this particular declaration is not that definition, import the
2237 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002238 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002239 if (Definition && Definition != D) {
2240 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002241 if (!ImportedDef)
2242 return 0;
2243
2244 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002245 }
2246
2247 // Import the major distinguishing characteristics of this record.
2248 DeclContext *DC, *LexicalDC;
2249 DeclarationName Name;
2250 SourceLocation Loc;
2251 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2252 return 0;
2253
2254 // Figure out what structure name we're looking for.
2255 unsigned IDNS = Decl::IDNS_Tag;
2256 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002257 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2258 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002259 IDNS = Decl::IDNS_Ordinary;
2260 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2261 IDNS |= Decl::IDNS_Ordinary;
2262
2263 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002264 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002265 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002266 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002267 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2268 Lookup.first != Lookup.second;
2269 ++Lookup.first) {
2270 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2271 continue;
2272
2273 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002274 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002275 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2276 Found = Tag->getDecl();
2277 }
2278
2279 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002280 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2281 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2282 // The record types structurally match, or the "from" translation
2283 // unit only had a forward declaration anyway; call it the same
2284 // function.
2285 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002286 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002287 }
2288 } else {
2289 // We have a forward declaration of this type, so adopt that forward
2290 // declaration rather than building a new one.
2291 AdoptDecl = FoundRecord;
2292 continue;
2293 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002294 }
2295
2296 ConflictingDecls.push_back(*Lookup.first);
2297 }
2298
2299 if (!ConflictingDecls.empty()) {
2300 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2301 ConflictingDecls.data(),
2302 ConflictingDecls.size());
2303 }
2304 }
2305
2306 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002307 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002308 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002309 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002310 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002311 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002312 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002313 DC, StartLoc, Loc,
2314 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002315 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002316 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002317 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002318 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002319 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002320 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002321
2322 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002323 D2->setLexicalDeclContext(LexicalDC);
2324 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002325 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002326
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002327 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002328
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002329 if (D->isDefinition() && ImportDefinition(D, D2))
2330 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002331
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002332 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002333}
2334
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002335Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2336 // Import the major distinguishing characteristics of this enumerator.
2337 DeclContext *DC, *LexicalDC;
2338 DeclarationName Name;
2339 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002340 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002341 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002342
2343 QualType T = Importer.Import(D->getType());
2344 if (T.isNull())
2345 return 0;
2346
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002347 // Determine whether there are any other declarations with the same name and
2348 // in the same context.
2349 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002350 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002351 unsigned IDNS = Decl::IDNS_Ordinary;
2352 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2353 Lookup.first != Lookup.second;
2354 ++Lookup.first) {
2355 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2356 continue;
2357
2358 ConflictingDecls.push_back(*Lookup.first);
2359 }
2360
2361 if (!ConflictingDecls.empty()) {
2362 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2363 ConflictingDecls.data(),
2364 ConflictingDecls.size());
2365 if (!Name)
2366 return 0;
2367 }
2368 }
2369
2370 Expr *Init = Importer.Import(D->getInitExpr());
2371 if (D->getInitExpr() && !Init)
2372 return 0;
2373
2374 EnumConstantDecl *ToEnumerator
2375 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2376 Name.getAsIdentifierInfo(), T,
2377 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002378 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002379 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002380 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002381 LexicalDC->addDecl(ToEnumerator);
2382 return ToEnumerator;
2383}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002384
Douglas Gregora404ea62010-02-10 19:54:31 +00002385Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2386 // Import the major distinguishing characteristics of this function.
2387 DeclContext *DC, *LexicalDC;
2388 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002389 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002390 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002391 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002392
Douglas Gregora404ea62010-02-10 19:54:31 +00002393 // Try to find a function in our own ("to") context with the same name, same
2394 // type, and in the same context as the function we're importing.
2395 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002396 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002397 unsigned IDNS = Decl::IDNS_Ordinary;
2398 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2399 Lookup.first != Lookup.second;
2400 ++Lookup.first) {
2401 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2402 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002403
Douglas Gregora404ea62010-02-10 19:54:31 +00002404 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2405 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2406 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002407 if (Importer.IsStructurallyEquivalent(D->getType(),
2408 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002409 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002410 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002411 }
2412
2413 // FIXME: Check for overloading more carefully, e.g., by boosting
2414 // Sema::IsOverload out to the AST library.
2415
2416 // Function overloading is okay in C++.
2417 if (Importer.getToContext().getLangOptions().CPlusPlus)
2418 continue;
2419
2420 // Complain about inconsistent function types.
2421 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002422 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002423 Importer.ToDiag(FoundFunction->getLocation(),
2424 diag::note_odr_value_here)
2425 << FoundFunction->getType();
2426 }
2427 }
2428
2429 ConflictingDecls.push_back(*Lookup.first);
2430 }
2431
2432 if (!ConflictingDecls.empty()) {
2433 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2434 ConflictingDecls.data(),
2435 ConflictingDecls.size());
2436 if (!Name)
2437 return 0;
2438 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002439 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002440
Abramo Bagnara25777432010-08-11 22:01:17 +00002441 DeclarationNameInfo NameInfo(Name, Loc);
2442 // Import additional name location/type info.
2443 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2444
Douglas Gregorea35d112010-02-15 23:54:17 +00002445 // Import the type.
2446 QualType T = Importer.Import(D->getType());
2447 if (T.isNull())
2448 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002449
2450 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002451 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002452 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2453 P != PEnd; ++P) {
2454 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2455 if (!ToP)
2456 return 0;
2457
2458 Parameters.push_back(ToP);
2459 }
2460
2461 // Create the imported function.
2462 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002463 FunctionDecl *ToFunction = 0;
2464 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2465 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2466 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002467 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002468 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002469 FromConstructor->isExplicit(),
2470 D->isInlineSpecified(),
Sean Hunt5f802e52011-05-06 00:11:07 +00002471 D->isImplicit());
Douglas Gregorc144f352010-02-21 18:29:16 +00002472 } else if (isa<CXXDestructorDecl>(D)) {
2473 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2474 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002475 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002476 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002477 D->isInlineSpecified(),
2478 D->isImplicit());
2479 } else if (CXXConversionDecl *FromConversion
2480 = dyn_cast<CXXConversionDecl>(D)) {
2481 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2482 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002483 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002484 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002485 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002486 FromConversion->isExplicit(),
2487 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002488 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2489 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2490 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002491 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002492 NameInfo, T, TInfo,
2493 Method->isStatic(),
2494 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002495 Method->isInlineSpecified(),
2496 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002497 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002498 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002499 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002500 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002501 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002502 D->isInlineSpecified(),
2503 D->hasWrittenPrototype());
2504 }
John McCallb6217662010-03-15 10:12:16 +00002505
2506 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002507 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002508 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002509 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002510 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2511 ToFunction->setTrivial(D->isTrivial());
2512 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002513 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002514
Douglas Gregora404ea62010-02-10 19:54:31 +00002515 // Set the parameters.
2516 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002517 Parameters[I]->setOwningFunction(ToFunction);
2518 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002519 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002520 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002521
2522 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002523
2524 // Add this function to the lexical context.
2525 LexicalDC->addDecl(ToFunction);
2526
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002527 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002528}
2529
Douglas Gregorc144f352010-02-21 18:29:16 +00002530Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2531 return VisitFunctionDecl(D);
2532}
2533
2534Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2535 return VisitCXXMethodDecl(D);
2536}
2537
2538Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2539 return VisitCXXMethodDecl(D);
2540}
2541
2542Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2543 return VisitCXXMethodDecl(D);
2544}
2545
Douglas Gregor96a01b42010-02-11 00:48:18 +00002546Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2547 // Import the major distinguishing characteristics of a variable.
2548 DeclContext *DC, *LexicalDC;
2549 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002550 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002551 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2552 return 0;
2553
2554 // Import the type.
2555 QualType T = Importer.Import(D->getType());
2556 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002557 return 0;
2558
2559 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2560 Expr *BitWidth = Importer.Import(D->getBitWidth());
2561 if (!BitWidth && D->getBitWidth())
2562 return 0;
2563
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002564 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2565 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002566 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002567 T, TInfo, BitWidth, D->isMutable(),
2568 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002569 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002570 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002571 if (ToField->hasInClassInitializer())
2572 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002573 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002574 LexicalDC->addDecl(ToField);
2575 return ToField;
2576}
2577
Francois Pichet87c2e122010-11-21 06:08:52 +00002578Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2579 // Import the major distinguishing characteristics of a variable.
2580 DeclContext *DC, *LexicalDC;
2581 DeclarationName Name;
2582 SourceLocation Loc;
2583 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2584 return 0;
2585
2586 // Import the type.
2587 QualType T = Importer.Import(D->getType());
2588 if (T.isNull())
2589 return 0;
2590
2591 NamedDecl **NamedChain =
2592 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2593
2594 unsigned i = 0;
2595 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2596 PE = D->chain_end(); PI != PE; ++PI) {
2597 Decl* D = Importer.Import(*PI);
2598 if (!D)
2599 return 0;
2600 NamedChain[i++] = cast<NamedDecl>(D);
2601 }
2602
2603 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2604 Importer.getToContext(), DC,
2605 Loc, Name.getAsIdentifierInfo(), T,
2606 NamedChain, D->getChainingSize());
2607 ToIndirectField->setAccess(D->getAccess());
2608 ToIndirectField->setLexicalDeclContext(LexicalDC);
2609 Importer.Imported(D, ToIndirectField);
2610 LexicalDC->addDecl(ToIndirectField);
2611 return ToIndirectField;
2612}
2613
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002614Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2615 // Import the major distinguishing characteristics of an ivar.
2616 DeclContext *DC, *LexicalDC;
2617 DeclarationName Name;
2618 SourceLocation Loc;
2619 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2620 return 0;
2621
2622 // Determine whether we've already imported this ivar
2623 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2624 Lookup.first != Lookup.second;
2625 ++Lookup.first) {
2626 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2627 if (Importer.IsStructurallyEquivalent(D->getType(),
2628 FoundIvar->getType())) {
2629 Importer.Imported(D, FoundIvar);
2630 return FoundIvar;
2631 }
2632
2633 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2634 << Name << D->getType() << FoundIvar->getType();
2635 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2636 << FoundIvar->getType();
2637 return 0;
2638 }
2639 }
2640
2641 // Import the type.
2642 QualType T = Importer.Import(D->getType());
2643 if (T.isNull())
2644 return 0;
2645
2646 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2647 Expr *BitWidth = Importer.Import(D->getBitWidth());
2648 if (!BitWidth && D->getBitWidth())
2649 return 0;
2650
Daniel Dunbara0654922010-04-02 20:10:03 +00002651 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2652 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002653 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002654 Loc, Name.getAsIdentifierInfo(),
2655 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002656 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002657 ToIvar->setLexicalDeclContext(LexicalDC);
2658 Importer.Imported(D, ToIvar);
2659 LexicalDC->addDecl(ToIvar);
2660 return ToIvar;
2661
2662}
2663
Douglas Gregora404ea62010-02-10 19:54:31 +00002664Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2665 // Import the major distinguishing characteristics of a variable.
2666 DeclContext *DC, *LexicalDC;
2667 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002668 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002669 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002670 return 0;
2671
Douglas Gregor089459a2010-02-08 21:09:39 +00002672 // Try to find a variable in our own ("to") context with the same name and
2673 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002674 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002675 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002676 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002677 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002678 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002679 Lookup.first != Lookup.second;
2680 ++Lookup.first) {
2681 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2682 continue;
2683
2684 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2685 // We have found a variable that we may need to merge with. Check it.
2686 if (isExternalLinkage(FoundVar->getLinkage()) &&
2687 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002688 if (Importer.IsStructurallyEquivalent(D->getType(),
2689 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002690 MergeWithVar = FoundVar;
2691 break;
2692 }
2693
Douglas Gregord0145422010-02-12 17:23:39 +00002694 const ArrayType *FoundArray
2695 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2696 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002697 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002698 if (FoundArray && TArray) {
2699 if (isa<IncompleteArrayType>(FoundArray) &&
2700 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002701 // Import the type.
2702 QualType T = Importer.Import(D->getType());
2703 if (T.isNull())
2704 return 0;
2705
Douglas Gregord0145422010-02-12 17:23:39 +00002706 FoundVar->setType(T);
2707 MergeWithVar = FoundVar;
2708 break;
2709 } else if (isa<IncompleteArrayType>(TArray) &&
2710 isa<ConstantArrayType>(FoundArray)) {
2711 MergeWithVar = FoundVar;
2712 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002713 }
2714 }
2715
Douglas Gregor089459a2010-02-08 21:09:39 +00002716 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002717 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002718 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2719 << FoundVar->getType();
2720 }
2721 }
2722
2723 ConflictingDecls.push_back(*Lookup.first);
2724 }
2725
2726 if (MergeWithVar) {
2727 // An equivalent variable with external linkage has been found. Link
2728 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002729 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002730
2731 if (VarDecl *DDef = D->getDefinition()) {
2732 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2733 Importer.ToDiag(ExistingDef->getLocation(),
2734 diag::err_odr_variable_multiple_def)
2735 << Name;
2736 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2737 } else {
2738 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002739 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002740 }
2741 }
2742
2743 return MergeWithVar;
2744 }
2745
2746 if (!ConflictingDecls.empty()) {
2747 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2748 ConflictingDecls.data(),
2749 ConflictingDecls.size());
2750 if (!Name)
2751 return 0;
2752 }
2753 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002754
Douglas Gregorea35d112010-02-15 23:54:17 +00002755 // Import the type.
2756 QualType T = Importer.Import(D->getType());
2757 if (T.isNull())
2758 return 0;
2759
Douglas Gregor089459a2010-02-08 21:09:39 +00002760 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002761 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002762 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2763 Importer.Import(D->getInnerLocStart()),
2764 Loc, Name.getAsIdentifierInfo(),
2765 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002766 D->getStorageClass(),
2767 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002768 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002769 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002770 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002771 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002772 LexicalDC->addDecl(ToVar);
2773
Douglas Gregor089459a2010-02-08 21:09:39 +00002774 // Merge the initializer.
2775 // FIXME: Can we really import any initializer? Alternatively, we could force
2776 // ourselves to import every declaration of a variable and then only use
2777 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002778 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002779
2780 // FIXME: Other bits to merge?
2781
2782 return ToVar;
2783}
2784
Douglas Gregor2cd00932010-02-17 21:22:52 +00002785Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2786 // Parameters are created in the translation unit's context, then moved
2787 // into the function declaration's context afterward.
2788 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2789
2790 // Import the name of this declaration.
2791 DeclarationName Name = Importer.Import(D->getDeclName());
2792 if (D->getDeclName() && !Name)
2793 return 0;
2794
2795 // Import the location of this declaration.
2796 SourceLocation Loc = Importer.Import(D->getLocation());
2797
2798 // Import the parameter's type.
2799 QualType T = Importer.Import(D->getType());
2800 if (T.isNull())
2801 return 0;
2802
2803 // Create the imported parameter.
2804 ImplicitParamDecl *ToParm
2805 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2806 Loc, Name.getAsIdentifierInfo(),
2807 T);
2808 return Importer.Imported(D, ToParm);
2809}
2810
Douglas Gregora404ea62010-02-10 19:54:31 +00002811Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2812 // Parameters are created in the translation unit's context, then moved
2813 // into the function declaration's context afterward.
2814 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2815
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002816 // Import the name of this declaration.
2817 DeclarationName Name = Importer.Import(D->getDeclName());
2818 if (D->getDeclName() && !Name)
2819 return 0;
2820
Douglas Gregora404ea62010-02-10 19:54:31 +00002821 // Import the location of this declaration.
2822 SourceLocation Loc = Importer.Import(D->getLocation());
2823
2824 // Import the parameter's type.
2825 QualType T = Importer.Import(D->getType());
2826 if (T.isNull())
2827 return 0;
2828
2829 // Create the imported parameter.
2830 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2831 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002832 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002833 Loc, Name.getAsIdentifierInfo(),
2834 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002835 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002836 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002837 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002838 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002839}
2840
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002841Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2842 // Import the major distinguishing characteristics of a method.
2843 DeclContext *DC, *LexicalDC;
2844 DeclarationName Name;
2845 SourceLocation Loc;
2846 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2847 return 0;
2848
2849 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2850 Lookup.first != Lookup.second;
2851 ++Lookup.first) {
2852 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2853 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2854 continue;
2855
2856 // Check return types.
2857 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2858 FoundMethod->getResultType())) {
2859 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2860 << D->isInstanceMethod() << Name
2861 << D->getResultType() << FoundMethod->getResultType();
2862 Importer.ToDiag(FoundMethod->getLocation(),
2863 diag::note_odr_objc_method_here)
2864 << D->isInstanceMethod() << Name;
2865 return 0;
2866 }
2867
2868 // Check the number of parameters.
2869 if (D->param_size() != FoundMethod->param_size()) {
2870 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2871 << D->isInstanceMethod() << Name
2872 << D->param_size() << FoundMethod->param_size();
2873 Importer.ToDiag(FoundMethod->getLocation(),
2874 diag::note_odr_objc_method_here)
2875 << D->isInstanceMethod() << Name;
2876 return 0;
2877 }
2878
2879 // Check parameter types.
2880 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2881 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2882 P != PEnd; ++P, ++FoundP) {
2883 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2884 (*FoundP)->getType())) {
2885 Importer.FromDiag((*P)->getLocation(),
2886 diag::err_odr_objc_method_param_type_inconsistent)
2887 << D->isInstanceMethod() << Name
2888 << (*P)->getType() << (*FoundP)->getType();
2889 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2890 << (*FoundP)->getType();
2891 return 0;
2892 }
2893 }
2894
2895 // Check variadic/non-variadic.
2896 // Check the number of parameters.
2897 if (D->isVariadic() != FoundMethod->isVariadic()) {
2898 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2899 << D->isInstanceMethod() << Name;
2900 Importer.ToDiag(FoundMethod->getLocation(),
2901 diag::note_odr_objc_method_here)
2902 << D->isInstanceMethod() << Name;
2903 return 0;
2904 }
2905
2906 // FIXME: Any other bits we need to merge?
2907 return Importer.Imported(D, FoundMethod);
2908 }
2909 }
2910
2911 // Import the result type.
2912 QualType ResultTy = Importer.Import(D->getResultType());
2913 if (ResultTy.isNull())
2914 return 0;
2915
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002916 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2917
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002918 ObjCMethodDecl *ToMethod
2919 = ObjCMethodDecl::Create(Importer.getToContext(),
2920 Loc,
2921 Importer.Import(D->getLocEnd()),
2922 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002923 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002924 D->isInstanceMethod(),
2925 D->isVariadic(),
2926 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002927 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002928 D->getImplementationControl(),
2929 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002930
2931 // FIXME: When we decide to merge method definitions, we'll need to
2932 // deal with implicit parameters.
2933
2934 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002935 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002936 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2937 FromPEnd = D->param_end();
2938 FromP != FromPEnd;
2939 ++FromP) {
2940 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2941 if (!ToP)
2942 return 0;
2943
2944 ToParams.push_back(ToP);
2945 }
2946
2947 // Set the parameters.
2948 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2949 ToParams[I]->setOwningFunction(ToMethod);
2950 ToMethod->addDecl(ToParams[I]);
2951 }
2952 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002953 ToParams.data(), ToParams.size(),
2954 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002955
2956 ToMethod->setLexicalDeclContext(LexicalDC);
2957 Importer.Imported(D, ToMethod);
2958 LexicalDC->addDecl(ToMethod);
2959 return ToMethod;
2960}
2961
Douglas Gregorb4677b62010-02-18 01:47:50 +00002962Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2963 // Import the major distinguishing characteristics of a category.
2964 DeclContext *DC, *LexicalDC;
2965 DeclarationName Name;
2966 SourceLocation Loc;
2967 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2968 return 0;
2969
2970 ObjCInterfaceDecl *ToInterface
2971 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2972 if (!ToInterface)
2973 return 0;
2974
2975 // Determine if we've already encountered this category.
2976 ObjCCategoryDecl *MergeWithCategory
2977 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2978 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2979 if (!ToCategory) {
2980 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2981 Importer.Import(D->getAtLoc()),
2982 Loc,
2983 Importer.Import(D->getCategoryNameLoc()),
2984 Name.getAsIdentifierInfo());
2985 ToCategory->setLexicalDeclContext(LexicalDC);
2986 LexicalDC->addDecl(ToCategory);
2987 Importer.Imported(D, ToCategory);
2988
2989 // Link this category into its class's category list.
2990 ToCategory->setClassInterface(ToInterface);
2991 ToCategory->insertNextClassCategory();
2992
2993 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00002994 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2995 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00002996 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2997 = D->protocol_loc_begin();
2998 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2999 FromProtoEnd = D->protocol_end();
3000 FromProto != FromProtoEnd;
3001 ++FromProto, ++FromProtoLoc) {
3002 ObjCProtocolDecl *ToProto
3003 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3004 if (!ToProto)
3005 return 0;
3006 Protocols.push_back(ToProto);
3007 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3008 }
3009
3010 // FIXME: If we're merging, make sure that the protocol list is the same.
3011 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3012 ProtocolLocs.data(), Importer.getToContext());
3013
3014 } else {
3015 Importer.Imported(D, ToCategory);
3016 }
3017
3018 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003019 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003020
3021 // If we have an implementation, import it as well.
3022 if (D->getImplementation()) {
3023 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003024 = cast_or_null<ObjCCategoryImplDecl>(
3025 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003026 if (!Impl)
3027 return 0;
3028
3029 ToCategory->setImplementation(Impl);
3030 }
3031
3032 return ToCategory;
3033}
3034
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003035Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003036 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003037 DeclContext *DC, *LexicalDC;
3038 DeclarationName Name;
3039 SourceLocation Loc;
3040 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3041 return 0;
3042
3043 ObjCProtocolDecl *MergeWithProtocol = 0;
3044 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3045 Lookup.first != Lookup.second;
3046 ++Lookup.first) {
3047 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3048 continue;
3049
3050 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
3051 break;
3052 }
3053
3054 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3055 if (!ToProto || ToProto->isForwardDecl()) {
3056 if (!ToProto) {
3057 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3058 Name.getAsIdentifierInfo());
3059 ToProto->setForwardDecl(D->isForwardDecl());
3060 ToProto->setLexicalDeclContext(LexicalDC);
3061 LexicalDC->addDecl(ToProto);
3062 }
3063 Importer.Imported(D, ToProto);
3064
3065 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003066 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3067 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003068 ObjCProtocolDecl::protocol_loc_iterator
3069 FromProtoLoc = D->protocol_loc_begin();
3070 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3071 FromProtoEnd = D->protocol_end();
3072 FromProto != FromProtoEnd;
3073 ++FromProto, ++FromProtoLoc) {
3074 ObjCProtocolDecl *ToProto
3075 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3076 if (!ToProto)
3077 return 0;
3078 Protocols.push_back(ToProto);
3079 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3080 }
3081
3082 // FIXME: If we're merging, make sure that the protocol list is the same.
3083 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3084 ProtocolLocs.data(), Importer.getToContext());
3085 } else {
3086 Importer.Imported(D, ToProto);
3087 }
3088
Douglas Gregorb4677b62010-02-18 01:47:50 +00003089 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003090 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003091
3092 return ToProto;
3093}
3094
Douglas Gregora12d2942010-02-16 01:20:57 +00003095Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3096 // Import the major distinguishing characteristics of an @interface.
3097 DeclContext *DC, *LexicalDC;
3098 DeclarationName Name;
3099 SourceLocation Loc;
3100 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3101 return 0;
3102
3103 ObjCInterfaceDecl *MergeWithIface = 0;
3104 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3105 Lookup.first != Lookup.second;
3106 ++Lookup.first) {
3107 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3108 continue;
3109
3110 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3111 break;
3112 }
3113
3114 ObjCInterfaceDecl *ToIface = MergeWithIface;
3115 if (!ToIface || ToIface->isForwardDecl()) {
3116 if (!ToIface) {
3117 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3118 DC, Loc,
3119 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00003120 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00003121 D->isForwardDecl(),
3122 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003123 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003124 ToIface->setLexicalDeclContext(LexicalDC);
3125 LexicalDC->addDecl(ToIface);
3126 }
3127 Importer.Imported(D, ToIface);
3128
Douglas Gregora12d2942010-02-16 01:20:57 +00003129 if (D->getSuperClass()) {
3130 ObjCInterfaceDecl *Super
3131 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3132 if (!Super)
3133 return 0;
3134
3135 ToIface->setSuperClass(Super);
3136 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3137 }
3138
3139 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003140 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3141 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003142 ObjCInterfaceDecl::protocol_loc_iterator
3143 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003144
3145 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003146 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3147 FromProtoEnd = D->protocol_end();
3148 FromProto != FromProtoEnd;
3149 ++FromProto, ++FromProtoLoc) {
3150 ObjCProtocolDecl *ToProto
3151 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3152 if (!ToProto)
3153 return 0;
3154 Protocols.push_back(ToProto);
3155 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3156 }
3157
3158 // FIXME: If we're merging, make sure that the protocol list is the same.
3159 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3160 ProtocolLocs.data(), Importer.getToContext());
3161
Douglas Gregora12d2942010-02-16 01:20:57 +00003162 // Import @end range
3163 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3164 } else {
3165 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003166
3167 // Check for consistency of superclasses.
3168 DeclarationName FromSuperName, ToSuperName;
3169 if (D->getSuperClass())
3170 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3171 if (ToIface->getSuperClass())
3172 ToSuperName = ToIface->getSuperClass()->getDeclName();
3173 if (FromSuperName != ToSuperName) {
3174 Importer.ToDiag(ToIface->getLocation(),
3175 diag::err_odr_objc_superclass_inconsistent)
3176 << ToIface->getDeclName();
3177 if (ToIface->getSuperClass())
3178 Importer.ToDiag(ToIface->getSuperClassLoc(),
3179 diag::note_odr_objc_superclass)
3180 << ToIface->getSuperClass()->getDeclName();
3181 else
3182 Importer.ToDiag(ToIface->getLocation(),
3183 diag::note_odr_objc_missing_superclass);
3184 if (D->getSuperClass())
3185 Importer.FromDiag(D->getSuperClassLoc(),
3186 diag::note_odr_objc_superclass)
3187 << D->getSuperClass()->getDeclName();
3188 else
3189 Importer.FromDiag(D->getLocation(),
3190 diag::note_odr_objc_missing_superclass);
3191 return 0;
3192 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003193 }
3194
Douglas Gregorb4677b62010-02-18 01:47:50 +00003195 // Import categories. When the categories themselves are imported, they'll
3196 // hook themselves into this interface.
3197 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3198 FromCat = FromCat->getNextClassCategory())
3199 Importer.Import(FromCat);
3200
Douglas Gregora12d2942010-02-16 01:20:57 +00003201 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003202 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003203
3204 // If we have an @implementation, import it as well.
3205 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003206 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3207 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003208 if (!Impl)
3209 return 0;
3210
3211 ToIface->setImplementation(Impl);
3212 }
3213
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003214 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003215}
3216
Douglas Gregor3daef292010-12-07 15:32:12 +00003217Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3218 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3219 Importer.Import(D->getCategoryDecl()));
3220 if (!Category)
3221 return 0;
3222
3223 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3224 if (!ToImpl) {
3225 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3226 if (!DC)
3227 return 0;
3228
3229 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3230 Importer.Import(D->getLocation()),
3231 Importer.Import(D->getIdentifier()),
3232 Category->getClassInterface());
3233
3234 DeclContext *LexicalDC = DC;
3235 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3236 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3237 if (!LexicalDC)
3238 return 0;
3239
3240 ToImpl->setLexicalDeclContext(LexicalDC);
3241 }
3242
3243 LexicalDC->addDecl(ToImpl);
3244 Category->setImplementation(ToImpl);
3245 }
3246
3247 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003248 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003249 return ToImpl;
3250}
3251
Douglas Gregordd182ff2010-12-07 01:26:03 +00003252Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3253 // Find the corresponding interface.
3254 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3255 Importer.Import(D->getClassInterface()));
3256 if (!Iface)
3257 return 0;
3258
3259 // Import the superclass, if any.
3260 ObjCInterfaceDecl *Super = 0;
3261 if (D->getSuperClass()) {
3262 Super = cast_or_null<ObjCInterfaceDecl>(
3263 Importer.Import(D->getSuperClass()));
3264 if (!Super)
3265 return 0;
3266 }
3267
3268 ObjCImplementationDecl *Impl = Iface->getImplementation();
3269 if (!Impl) {
3270 // We haven't imported an implementation yet. Create a new @implementation
3271 // now.
3272 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3273 Importer.ImportContext(D->getDeclContext()),
3274 Importer.Import(D->getLocation()),
3275 Iface, Super);
3276
3277 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3278 DeclContext *LexicalDC
3279 = Importer.ImportContext(D->getLexicalDeclContext());
3280 if (!LexicalDC)
3281 return 0;
3282 Impl->setLexicalDeclContext(LexicalDC);
3283 }
3284
3285 // Associate the implementation with the class it implements.
3286 Iface->setImplementation(Impl);
3287 Importer.Imported(D, Iface->getImplementation());
3288 } else {
3289 Importer.Imported(D, Iface->getImplementation());
3290
3291 // Verify that the existing @implementation has the same superclass.
3292 if ((Super && !Impl->getSuperClass()) ||
3293 (!Super && Impl->getSuperClass()) ||
3294 (Super && Impl->getSuperClass() &&
3295 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3296 Importer.ToDiag(Impl->getLocation(),
3297 diag::err_odr_objc_superclass_inconsistent)
3298 << Iface->getDeclName();
3299 // FIXME: It would be nice to have the location of the superclass
3300 // below.
3301 if (Impl->getSuperClass())
3302 Importer.ToDiag(Impl->getLocation(),
3303 diag::note_odr_objc_superclass)
3304 << Impl->getSuperClass()->getDeclName();
3305 else
3306 Importer.ToDiag(Impl->getLocation(),
3307 diag::note_odr_objc_missing_superclass);
3308 if (D->getSuperClass())
3309 Importer.FromDiag(D->getLocation(),
3310 diag::note_odr_objc_superclass)
3311 << D->getSuperClass()->getDeclName();
3312 else
3313 Importer.FromDiag(D->getLocation(),
3314 diag::note_odr_objc_missing_superclass);
3315 return 0;
3316 }
3317 }
3318
3319 // Import all of the members of this @implementation.
3320 ImportDeclContext(D);
3321
3322 return Impl;
3323}
3324
Douglas Gregore3261622010-02-17 18:02:10 +00003325Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3326 // Import the major distinguishing characteristics of an @property.
3327 DeclContext *DC, *LexicalDC;
3328 DeclarationName Name;
3329 SourceLocation Loc;
3330 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3331 return 0;
3332
3333 // Check whether we have already imported this property.
3334 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3335 Lookup.first != Lookup.second;
3336 ++Lookup.first) {
3337 if (ObjCPropertyDecl *FoundProp
3338 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3339 // Check property types.
3340 if (!Importer.IsStructurallyEquivalent(D->getType(),
3341 FoundProp->getType())) {
3342 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3343 << Name << D->getType() << FoundProp->getType();
3344 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3345 << FoundProp->getType();
3346 return 0;
3347 }
3348
3349 // FIXME: Check property attributes, getters, setters, etc.?
3350
3351 // Consider these properties to be equivalent.
3352 Importer.Imported(D, FoundProp);
3353 return FoundProp;
3354 }
3355 }
3356
3357 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003358 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3359 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003360 return 0;
3361
3362 // Create the new property.
3363 ObjCPropertyDecl *ToProperty
3364 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3365 Name.getAsIdentifierInfo(),
3366 Importer.Import(D->getAtLoc()),
3367 T,
3368 D->getPropertyImplementation());
3369 Importer.Imported(D, ToProperty);
3370 ToProperty->setLexicalDeclContext(LexicalDC);
3371 LexicalDC->addDecl(ToProperty);
3372
3373 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003374 ToProperty->setPropertyAttributesAsWritten(
3375 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003376 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3377 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3378 ToProperty->setGetterMethodDecl(
3379 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3380 ToProperty->setSetterMethodDecl(
3381 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3382 ToProperty->setPropertyIvarDecl(
3383 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3384 return ToProperty;
3385}
3386
Douglas Gregor954e0c72010-12-07 18:32:03 +00003387Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3388 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3389 Importer.Import(D->getPropertyDecl()));
3390 if (!Property)
3391 return 0;
3392
3393 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3394 if (!DC)
3395 return 0;
3396
3397 // Import the lexical declaration context.
3398 DeclContext *LexicalDC = DC;
3399 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3400 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3401 if (!LexicalDC)
3402 return 0;
3403 }
3404
3405 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3406 if (!InImpl)
3407 return 0;
3408
3409 // Import the ivar (for an @synthesize).
3410 ObjCIvarDecl *Ivar = 0;
3411 if (D->getPropertyIvarDecl()) {
3412 Ivar = cast_or_null<ObjCIvarDecl>(
3413 Importer.Import(D->getPropertyIvarDecl()));
3414 if (!Ivar)
3415 return 0;
3416 }
3417
3418 ObjCPropertyImplDecl *ToImpl
3419 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3420 if (!ToImpl) {
3421 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3422 Importer.Import(D->getLocStart()),
3423 Importer.Import(D->getLocation()),
3424 Property,
3425 D->getPropertyImplementation(),
3426 Ivar,
3427 Importer.Import(D->getPropertyIvarDeclLoc()));
3428 ToImpl->setLexicalDeclContext(LexicalDC);
3429 Importer.Imported(D, ToImpl);
3430 LexicalDC->addDecl(ToImpl);
3431 } else {
3432 // Check that we have the same kind of property implementation (@synthesize
3433 // vs. @dynamic).
3434 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3435 Importer.ToDiag(ToImpl->getLocation(),
3436 diag::err_odr_objc_property_impl_kind_inconsistent)
3437 << Property->getDeclName()
3438 << (ToImpl->getPropertyImplementation()
3439 == ObjCPropertyImplDecl::Dynamic);
3440 Importer.FromDiag(D->getLocation(),
3441 diag::note_odr_objc_property_impl_kind)
3442 << D->getPropertyDecl()->getDeclName()
3443 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3444 return 0;
3445 }
3446
3447 // For @synthesize, check that we have the same
3448 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3449 Ivar != ToImpl->getPropertyIvarDecl()) {
3450 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3451 diag::err_odr_objc_synthesize_ivar_inconsistent)
3452 << Property->getDeclName()
3453 << ToImpl->getPropertyIvarDecl()->getDeclName()
3454 << Ivar->getDeclName();
3455 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3456 diag::note_odr_objc_synthesize_ivar_here)
3457 << D->getPropertyIvarDecl()->getDeclName();
3458 return 0;
3459 }
3460
3461 // Merge the existing implementation with the new implementation.
3462 Importer.Imported(D, ToImpl);
3463 }
3464
3465 return ToImpl;
3466}
3467
Douglas Gregor2b785022010-02-18 02:12:22 +00003468Decl *
3469ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3470 // Import the context of this declaration.
3471 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3472 if (!DC)
3473 return 0;
3474
3475 DeclContext *LexicalDC = DC;
3476 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3477 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3478 if (!LexicalDC)
3479 return 0;
3480 }
3481
3482 // Import the location of this declaration.
3483 SourceLocation Loc = Importer.Import(D->getLocation());
3484
Chris Lattner5f9e2722011-07-23 10:55:15 +00003485 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3486 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003487 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3488 = D->protocol_loc_begin();
3489 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3490 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3491 FromProto != FromProtoEnd;
3492 ++FromProto, ++FromProtoLoc) {
3493 ObjCProtocolDecl *ToProto
3494 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3495 if (!ToProto)
3496 continue;
3497
3498 Protocols.push_back(ToProto);
3499 Locations.push_back(Importer.Import(*FromProtoLoc));
3500 }
3501
3502 ObjCForwardProtocolDecl *ToForward
3503 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3504 Protocols.data(), Protocols.size(),
3505 Locations.data());
3506 ToForward->setLexicalDeclContext(LexicalDC);
3507 LexicalDC->addDecl(ToForward);
3508 Importer.Imported(D, ToForward);
3509 return ToForward;
3510}
3511
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003512Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3513 // Import the context of this declaration.
3514 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3515 if (!DC)
3516 return 0;
3517
3518 DeclContext *LexicalDC = DC;
3519 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3520 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3521 if (!LexicalDC)
3522 return 0;
3523 }
3524
3525 // Import the location of this declaration.
3526 SourceLocation Loc = Importer.Import(D->getLocation());
3527
Chris Lattner5f9e2722011-07-23 10:55:15 +00003528 SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3529 SmallVector<SourceLocation, 4> Locations;
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003530 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3531 From != FromEnd; ++From) {
3532 ObjCInterfaceDecl *ToIface
3533 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3534 if (!ToIface)
3535 continue;
3536
3537 Interfaces.push_back(ToIface);
3538 Locations.push_back(Importer.Import(From->getLocation()));
3539 }
3540
3541 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3542 Loc,
3543 Interfaces.data(),
3544 Locations.data(),
3545 Interfaces.size());
3546 ToClass->setLexicalDeclContext(LexicalDC);
3547 LexicalDC->addDecl(ToClass);
3548 Importer.Imported(D, ToClass);
3549 return ToClass;
3550}
3551
Douglas Gregor040afae2010-11-30 19:14:50 +00003552Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3553 // For template arguments, we adopt the translation unit as our declaration
3554 // context. This context will be fixed when the actual template declaration
3555 // is created.
3556
3557 // FIXME: Import default argument.
3558 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3559 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003560 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003561 Importer.Import(D->getLocation()),
3562 D->getDepth(),
3563 D->getIndex(),
3564 Importer.Import(D->getIdentifier()),
3565 D->wasDeclaredWithTypename(),
3566 D->isParameterPack());
3567}
3568
3569Decl *
3570ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3571 // Import the name of this declaration.
3572 DeclarationName Name = Importer.Import(D->getDeclName());
3573 if (D->getDeclName() && !Name)
3574 return 0;
3575
3576 // Import the location of this declaration.
3577 SourceLocation Loc = Importer.Import(D->getLocation());
3578
3579 // Import the type of this declaration.
3580 QualType T = Importer.Import(D->getType());
3581 if (T.isNull())
3582 return 0;
3583
3584 // Import type-source information.
3585 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3586 if (D->getTypeSourceInfo() && !TInfo)
3587 return 0;
3588
3589 // FIXME: Import default argument.
3590
3591 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3592 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003593 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003594 Loc, D->getDepth(), D->getPosition(),
3595 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003596 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003597}
3598
3599Decl *
3600ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3601 // Import the name of this declaration.
3602 DeclarationName Name = Importer.Import(D->getDeclName());
3603 if (D->getDeclName() && !Name)
3604 return 0;
3605
3606 // Import the location of this declaration.
3607 SourceLocation Loc = Importer.Import(D->getLocation());
3608
3609 // Import template parameters.
3610 TemplateParameterList *TemplateParams
3611 = ImportTemplateParameterList(D->getTemplateParameters());
3612 if (!TemplateParams)
3613 return 0;
3614
3615 // FIXME: Import default argument.
3616
3617 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3618 Importer.getToContext().getTranslationUnitDecl(),
3619 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003620 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003621 Name.getAsIdentifierInfo(),
3622 TemplateParams);
3623}
3624
3625Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3626 // If this record has a definition in the translation unit we're coming from,
3627 // but this particular declaration is not that definition, import the
3628 // definition and map to that.
3629 CXXRecordDecl *Definition
3630 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3631 if (Definition && Definition != D->getTemplatedDecl()) {
3632 Decl *ImportedDef
3633 = Importer.Import(Definition->getDescribedClassTemplate());
3634 if (!ImportedDef)
3635 return 0;
3636
3637 return Importer.Imported(D, ImportedDef);
3638 }
3639
3640 // Import the major distinguishing characteristics of this class template.
3641 DeclContext *DC, *LexicalDC;
3642 DeclarationName Name;
3643 SourceLocation Loc;
3644 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3645 return 0;
3646
3647 // We may already have a template of the same name; try to find and match it.
3648 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003649 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor040afae2010-11-30 19:14:50 +00003650 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3651 Lookup.first != Lookup.second;
3652 ++Lookup.first) {
3653 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3654 continue;
3655
3656 Decl *Found = *Lookup.first;
3657 if (ClassTemplateDecl *FoundTemplate
3658 = dyn_cast<ClassTemplateDecl>(Found)) {
3659 if (IsStructuralMatch(D, FoundTemplate)) {
3660 // The class templates structurally match; call it the same template.
3661 // FIXME: We may be filling in a forward declaration here. Handle
3662 // this case!
3663 Importer.Imported(D->getTemplatedDecl(),
3664 FoundTemplate->getTemplatedDecl());
3665 return Importer.Imported(D, FoundTemplate);
3666 }
3667 }
3668
3669 ConflictingDecls.push_back(*Lookup.first);
3670 }
3671
3672 if (!ConflictingDecls.empty()) {
3673 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3674 ConflictingDecls.data(),
3675 ConflictingDecls.size());
3676 }
3677
3678 if (!Name)
3679 return 0;
3680 }
3681
3682 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3683
3684 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003685 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3686 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003687 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3688 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003689 DC, StartLoc, IdLoc,
3690 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003691 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003692 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003693 D2Templated->setLexicalDeclContext(LexicalDC);
3694
3695 // Create the class template declaration itself.
3696 TemplateParameterList *TemplateParams
3697 = ImportTemplateParameterList(D->getTemplateParameters());
3698 if (!TemplateParams)
3699 return 0;
3700
3701 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3702 Loc, Name, TemplateParams,
3703 D2Templated,
3704 /*PrevDecl=*/0);
3705 D2Templated->setDescribedClassTemplate(D2);
3706
3707 D2->setAccess(D->getAccess());
3708 D2->setLexicalDeclContext(LexicalDC);
3709 LexicalDC->addDecl(D2);
3710
3711 // Note the relationship between the class templates.
3712 Importer.Imported(D, D2);
3713 Importer.Imported(DTemplated, D2Templated);
3714
3715 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3716 // FIXME: Import definition!
3717 }
3718
3719 return D2;
3720}
3721
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003722Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3723 ClassTemplateSpecializationDecl *D) {
3724 // If this record has a definition in the translation unit we're coming from,
3725 // but this particular declaration is not that definition, import the
3726 // definition and map to that.
3727 TagDecl *Definition = D->getDefinition();
3728 if (Definition && Definition != D) {
3729 Decl *ImportedDef = Importer.Import(Definition);
3730 if (!ImportedDef)
3731 return 0;
3732
3733 return Importer.Imported(D, ImportedDef);
3734 }
3735
3736 ClassTemplateDecl *ClassTemplate
3737 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3738 D->getSpecializedTemplate()));
3739 if (!ClassTemplate)
3740 return 0;
3741
3742 // Import the context of this declaration.
3743 DeclContext *DC = ClassTemplate->getDeclContext();
3744 if (!DC)
3745 return 0;
3746
3747 DeclContext *LexicalDC = DC;
3748 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3749 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3750 if (!LexicalDC)
3751 return 0;
3752 }
3753
3754 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003755 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3756 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003757
3758 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003759 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003760 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3761 D->getTemplateArgs().size(),
3762 TemplateArgs))
3763 return 0;
3764
3765 // Try to find an existing specialization with these template arguments.
3766 void *InsertPos = 0;
3767 ClassTemplateSpecializationDecl *D2
3768 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3769 TemplateArgs.size(), InsertPos);
3770 if (D2) {
3771 // We already have a class template specialization with these template
3772 // arguments.
3773
3774 // FIXME: Check for specialization vs. instantiation errors.
3775
3776 if (RecordDecl *FoundDef = D2->getDefinition()) {
3777 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3778 // The record types structurally match, or the "from" translation
3779 // unit only had a forward declaration anyway; call it the same
3780 // function.
3781 return Importer.Imported(D, FoundDef);
3782 }
3783 }
3784 } else {
3785 // Create a new specialization.
3786 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3787 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003788 StartLoc, IdLoc,
3789 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003790 TemplateArgs.data(),
3791 TemplateArgs.size(),
3792 /*PrevDecl=*/0);
3793 D2->setSpecializationKind(D->getSpecializationKind());
3794
3795 // Add this specialization to the class template.
3796 ClassTemplate->AddSpecialization(D2, InsertPos);
3797
3798 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003799 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003800
3801 // Add the specialization to this context.
3802 D2->setLexicalDeclContext(LexicalDC);
3803 LexicalDC->addDecl(D2);
3804 }
3805 Importer.Imported(D, D2);
3806
3807 if (D->isDefinition() && ImportDefinition(D, D2))
3808 return 0;
3809
3810 return D2;
3811}
3812
Douglas Gregor4800d952010-02-11 19:21:55 +00003813//----------------------------------------------------------------------------
3814// Import Statements
3815//----------------------------------------------------------------------------
3816
3817Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3818 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3819 << S->getStmtClassName();
3820 return 0;
3821}
3822
3823//----------------------------------------------------------------------------
3824// Import Expressions
3825//----------------------------------------------------------------------------
3826Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3827 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3828 << E->getStmtClassName();
3829 return 0;
3830}
3831
Douglas Gregor44080632010-02-19 01:17:02 +00003832Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003833 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3834 if (!ToD)
3835 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003836
3837 NamedDecl *FoundD = 0;
3838 if (E->getDecl() != E->getFoundDecl()) {
3839 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3840 if (!FoundD)
3841 return 0;
3842 }
Douglas Gregor44080632010-02-19 01:17:02 +00003843
3844 QualType T = Importer.Import(E->getType());
3845 if (T.isNull())
3846 return 0;
3847
Douglas Gregor40d96a62011-02-28 21:54:11 +00003848 return DeclRefExpr::Create(Importer.getToContext(),
3849 Importer.Import(E->getQualifierLoc()),
Douglas Gregor44080632010-02-19 01:17:02 +00003850 ToD,
3851 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003852 T, E->getValueKind(),
Chandler Carruth3aa81402011-05-01 23:48:14 +00003853 FoundD,
Douglas Gregor44080632010-02-19 01:17:02 +00003854 /*FIXME:TemplateArgs=*/0);
3855}
3856
Douglas Gregor4800d952010-02-11 19:21:55 +00003857Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3858 QualType T = Importer.Import(E->getType());
3859 if (T.isNull())
3860 return 0;
3861
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003862 return IntegerLiteral::Create(Importer.getToContext(),
3863 E->getValue(), T,
3864 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003865}
3866
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003867Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3868 QualType T = Importer.Import(E->getType());
3869 if (T.isNull())
3870 return 0;
3871
Douglas Gregor5cee1192011-07-27 05:40:30 +00003872 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3873 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003874 Importer.Import(E->getLocation()));
3875}
3876
Douglas Gregorf638f952010-02-19 01:07:06 +00003877Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3878 Expr *SubExpr = Importer.Import(E->getSubExpr());
3879 if (!SubExpr)
3880 return 0;
3881
3882 return new (Importer.getToContext())
3883 ParenExpr(Importer.Import(E->getLParen()),
3884 Importer.Import(E->getRParen()),
3885 SubExpr);
3886}
3887
3888Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3889 QualType T = Importer.Import(E->getType());
3890 if (T.isNull())
3891 return 0;
3892
3893 Expr *SubExpr = Importer.Import(E->getSubExpr());
3894 if (!SubExpr)
3895 return 0;
3896
3897 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003898 T, E->getValueKind(),
3899 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003900 Importer.Import(E->getOperatorLoc()));
3901}
3902
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003903Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3904 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003905 QualType ResultType = Importer.Import(E->getType());
3906
3907 if (E->isArgumentType()) {
3908 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3909 if (!TInfo)
3910 return 0;
3911
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003912 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3913 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003914 Importer.Import(E->getOperatorLoc()),
3915 Importer.Import(E->getRParenLoc()));
3916 }
3917
3918 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3919 if (!SubExpr)
3920 return 0;
3921
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003922 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3923 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003924 Importer.Import(E->getOperatorLoc()),
3925 Importer.Import(E->getRParenLoc()));
3926}
3927
Douglas Gregorf638f952010-02-19 01:07:06 +00003928Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3929 QualType T = Importer.Import(E->getType());
3930 if (T.isNull())
3931 return 0;
3932
3933 Expr *LHS = Importer.Import(E->getLHS());
3934 if (!LHS)
3935 return 0;
3936
3937 Expr *RHS = Importer.Import(E->getRHS());
3938 if (!RHS)
3939 return 0;
3940
3941 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003942 T, E->getValueKind(),
3943 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003944 Importer.Import(E->getOperatorLoc()));
3945}
3946
3947Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3948 QualType T = Importer.Import(E->getType());
3949 if (T.isNull())
3950 return 0;
3951
3952 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3953 if (CompLHSType.isNull())
3954 return 0;
3955
3956 QualType CompResultType = Importer.Import(E->getComputationResultType());
3957 if (CompResultType.isNull())
3958 return 0;
3959
3960 Expr *LHS = Importer.Import(E->getLHS());
3961 if (!LHS)
3962 return 0;
3963
3964 Expr *RHS = Importer.Import(E->getRHS());
3965 if (!RHS)
3966 return 0;
3967
3968 return new (Importer.getToContext())
3969 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003970 T, E->getValueKind(),
3971 E->getObjectKind(),
3972 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003973 Importer.Import(E->getOperatorLoc()));
3974}
3975
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003976static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003977 if (E->path_empty()) return false;
3978
3979 // TODO: import cast paths
3980 return true;
3981}
3982
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003983Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3984 QualType T = Importer.Import(E->getType());
3985 if (T.isNull())
3986 return 0;
3987
3988 Expr *SubExpr = Importer.Import(E->getSubExpr());
3989 if (!SubExpr)
3990 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003991
3992 CXXCastPath BasePath;
3993 if (ImportCastPath(E, BasePath))
3994 return 0;
3995
3996 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003997 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003998}
3999
Douglas Gregor008847a2010-02-19 01:32:14 +00004000Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4001 QualType T = Importer.Import(E->getType());
4002 if (T.isNull())
4003 return 0;
4004
4005 Expr *SubExpr = Importer.Import(E->getSubExpr());
4006 if (!SubExpr)
4007 return 0;
4008
4009 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4010 if (!TInfo && E->getTypeInfoAsWritten())
4011 return 0;
4012
John McCallf871d0c2010-08-07 06:22:56 +00004013 CXXCastPath BasePath;
4014 if (ImportCastPath(E, BasePath))
4015 return 0;
4016
John McCallf89e55a2010-11-18 06:31:45 +00004017 return CStyleCastExpr::Create(Importer.getToContext(), T,
4018 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004019 SubExpr, &BasePath, TInfo,
4020 Importer.Import(E->getLParenLoc()),
4021 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004022}
4023
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004024ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004025 ASTContext &FromContext, FileManager &FromFileManager,
4026 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004027 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004028 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4029 Minimal(MinimalImport)
4030{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004031 ImportedDecls[FromContext.getTranslationUnitDecl()]
4032 = ToContext.getTranslationUnitDecl();
4033}
4034
4035ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004036
4037QualType ASTImporter::Import(QualType FromT) {
4038 if (FromT.isNull())
4039 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004040
4041 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004042
Douglas Gregor169fba52010-02-08 15:18:58 +00004043 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004044 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4045 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004046 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004047 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004048
Douglas Gregor169fba52010-02-08 15:18:58 +00004049 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004050 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004051 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004052 if (ToT.isNull())
4053 return ToT;
4054
Douglas Gregor169fba52010-02-08 15:18:58 +00004055 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004056 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004057
John McCallf4c73712011-01-19 06:33:43 +00004058 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004059}
4060
Douglas Gregor9bed8792010-02-09 19:21:46 +00004061TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004062 if (!FromTSI)
4063 return FromTSI;
4064
4065 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004066 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004067 QualType T = Import(FromTSI->getType());
4068 if (T.isNull())
4069 return 0;
4070
4071 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004072 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004073}
4074
4075Decl *ASTImporter::Import(Decl *FromD) {
4076 if (!FromD)
4077 return 0;
4078
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004079 ASTNodeImporter Importer(*this);
4080
Douglas Gregor9bed8792010-02-09 19:21:46 +00004081 // Check whether we've already imported this declaration.
4082 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004083 if (Pos != ImportedDecls.end()) {
4084 Decl *ToD = Pos->second;
4085 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4086 return ToD;
4087 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004088
4089 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004090 Decl *ToD = Importer.Visit(FromD);
4091 if (!ToD)
4092 return 0;
4093
4094 // Record the imported declaration.
4095 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004096
4097 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4098 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004099 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004100 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004101 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004102 // When we've finished transforming a typedef, see whether it was the
4103 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004104 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004105 FromTag = AnonTagsWithPendingTypedefs.begin(),
4106 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4107 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004108 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004109 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4110 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004111 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004112 AnonTagsWithPendingTypedefs.erase(FromTag);
4113 break;
4114 }
4115 }
4116 }
4117 }
4118
Douglas Gregor9bed8792010-02-09 19:21:46 +00004119 return ToD;
4120}
4121
4122DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4123 if (!FromDC)
4124 return FromDC;
4125
4126 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4127}
4128
4129Expr *ASTImporter::Import(Expr *FromE) {
4130 if (!FromE)
4131 return 0;
4132
4133 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4134}
4135
4136Stmt *ASTImporter::Import(Stmt *FromS) {
4137 if (!FromS)
4138 return 0;
4139
Douglas Gregor4800d952010-02-11 19:21:55 +00004140 // Check whether we've already imported this declaration.
4141 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4142 if (Pos != ImportedStmts.end())
4143 return Pos->second;
4144
4145 // Import the type
4146 ASTNodeImporter Importer(*this);
4147 Stmt *ToS = Importer.Visit(FromS);
4148 if (!ToS)
4149 return 0;
4150
4151 // Record the imported declaration.
4152 ImportedStmts[FromS] = ToS;
4153 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004154}
4155
4156NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4157 if (!FromNNS)
4158 return 0;
4159
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004160 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4161
4162 switch (FromNNS->getKind()) {
4163 case NestedNameSpecifier::Identifier:
4164 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4165 return NestedNameSpecifier::Create(ToContext, prefix, II);
4166 }
4167 return 0;
4168
4169 case NestedNameSpecifier::Namespace:
4170 if (NamespaceDecl *NS =
4171 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4172 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4173 }
4174 return 0;
4175
4176 case NestedNameSpecifier::NamespaceAlias:
4177 if (NamespaceAliasDecl *NSAD =
4178 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4179 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4180 }
4181 return 0;
4182
4183 case NestedNameSpecifier::Global:
4184 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4185
4186 case NestedNameSpecifier::TypeSpec:
4187 case NestedNameSpecifier::TypeSpecWithTemplate: {
4188 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4189 if (!T.isNull()) {
4190 bool bTemplate = FromNNS->getKind() ==
4191 NestedNameSpecifier::TypeSpecWithTemplate;
4192 return NestedNameSpecifier::Create(ToContext, prefix,
4193 bTemplate, T.getTypePtr());
4194 }
4195 }
4196 return 0;
4197 }
4198
4199 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004200 return 0;
4201}
4202
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004203NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4204 // FIXME: Implement!
4205 return NestedNameSpecifierLoc();
4206}
4207
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004208TemplateName ASTImporter::Import(TemplateName From) {
4209 switch (From.getKind()) {
4210 case TemplateName::Template:
4211 if (TemplateDecl *ToTemplate
4212 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4213 return TemplateName(ToTemplate);
4214
4215 return TemplateName();
4216
4217 case TemplateName::OverloadedTemplate: {
4218 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4219 UnresolvedSet<2> ToTemplates;
4220 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4221 E = FromStorage->end();
4222 I != E; ++I) {
4223 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4224 ToTemplates.addDecl(To);
4225 else
4226 return TemplateName();
4227 }
4228 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4229 ToTemplates.end());
4230 }
4231
4232 case TemplateName::QualifiedTemplate: {
4233 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4234 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4235 if (!Qualifier)
4236 return TemplateName();
4237
4238 if (TemplateDecl *ToTemplate
4239 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4240 return ToContext.getQualifiedTemplateName(Qualifier,
4241 QTN->hasTemplateKeyword(),
4242 ToTemplate);
4243
4244 return TemplateName();
4245 }
4246
4247 case TemplateName::DependentTemplate: {
4248 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4249 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4250 if (!Qualifier)
4251 return TemplateName();
4252
4253 if (DTN->isIdentifier()) {
4254 return ToContext.getDependentTemplateName(Qualifier,
4255 Import(DTN->getIdentifier()));
4256 }
4257
4258 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4259 }
John McCall14606042011-06-30 08:33:18 +00004260
4261 case TemplateName::SubstTemplateTemplateParm: {
4262 SubstTemplateTemplateParmStorage *subst
4263 = From.getAsSubstTemplateTemplateParm();
4264 TemplateTemplateParmDecl *param
4265 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4266 if (!param)
4267 return TemplateName();
4268
4269 TemplateName replacement = Import(subst->getReplacement());
4270 if (replacement.isNull()) return TemplateName();
4271
4272 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4273 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004274
4275 case TemplateName::SubstTemplateTemplateParmPack: {
4276 SubstTemplateTemplateParmPackStorage *SubstPack
4277 = From.getAsSubstTemplateTemplateParmPack();
4278 TemplateTemplateParmDecl *Param
4279 = cast_or_null<TemplateTemplateParmDecl>(
4280 Import(SubstPack->getParameterPack()));
4281 if (!Param)
4282 return TemplateName();
4283
4284 ASTNodeImporter Importer(*this);
4285 TemplateArgument ArgPack
4286 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4287 if (ArgPack.isNull())
4288 return TemplateName();
4289
4290 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4291 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004292 }
4293
4294 llvm_unreachable("Invalid template name kind");
4295 return TemplateName();
4296}
4297
Douglas Gregor9bed8792010-02-09 19:21:46 +00004298SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4299 if (FromLoc.isInvalid())
4300 return SourceLocation();
4301
Douglas Gregor88523732010-02-10 00:15:17 +00004302 SourceManager &FromSM = FromContext.getSourceManager();
4303
4304 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004305 // don't have to import macro expansions.
4306 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004307 FromLoc = FromSM.getSpellingLoc(FromLoc);
4308 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4309 SourceManager &ToSM = ToContext.getSourceManager();
4310 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4311 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004312}
4313
4314SourceRange ASTImporter::Import(SourceRange FromRange) {
4315 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4316}
4317
Douglas Gregor88523732010-02-10 00:15:17 +00004318FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004319 llvm::DenseMap<FileID, FileID>::iterator Pos
4320 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004321 if (Pos != ImportedFileIDs.end())
4322 return Pos->second;
4323
4324 SourceManager &FromSM = FromContext.getSourceManager();
4325 SourceManager &ToSM = ToContext.getSourceManager();
4326 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004327 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004328
4329 // Include location of this file.
4330 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4331
4332 // Map the FileID for to the "to" source manager.
4333 FileID ToID;
4334 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004335 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004336 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4337 // disk again
4338 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4339 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004340 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004341 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4342 FromSLoc.getFile().getFileCharacteristic());
4343 } else {
4344 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004345 const llvm::MemoryBuffer *
4346 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004347 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004348 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004349 FromBuf->getBufferIdentifier());
4350 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4351 }
4352
4353
Sebastian Redl535a3e22010-09-30 01:03:06 +00004354 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004355 return ToID;
4356}
4357
Douglas Gregord8868a62011-01-18 03:11:38 +00004358void ASTImporter::ImportDefinition(Decl *From) {
4359 Decl *To = Import(From);
4360 if (!To)
4361 return;
4362
4363 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4364 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004365
4366 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4367 if (!ToRecord->getDefinition()) {
4368 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4369 /*ForceImport=*/true);
4370 return;
4371 }
4372 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004373
4374 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4375 if (!ToEnum->getDefinition()) {
4376 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4377 /*ForceImport=*/true);
4378 return;
4379 }
4380 }
4381
Douglas Gregord8868a62011-01-18 03:11:38 +00004382 Importer.ImportDeclContext(FromDC, true);
4383 }
4384}
4385
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004386DeclarationName ASTImporter::Import(DeclarationName FromName) {
4387 if (!FromName)
4388 return DeclarationName();
4389
4390 switch (FromName.getNameKind()) {
4391 case DeclarationName::Identifier:
4392 return Import(FromName.getAsIdentifierInfo());
4393
4394 case DeclarationName::ObjCZeroArgSelector:
4395 case DeclarationName::ObjCOneArgSelector:
4396 case DeclarationName::ObjCMultiArgSelector:
4397 return Import(FromName.getObjCSelector());
4398
4399 case DeclarationName::CXXConstructorName: {
4400 QualType T = Import(FromName.getCXXNameType());
4401 if (T.isNull())
4402 return DeclarationName();
4403
4404 return ToContext.DeclarationNames.getCXXConstructorName(
4405 ToContext.getCanonicalType(T));
4406 }
4407
4408 case DeclarationName::CXXDestructorName: {
4409 QualType T = Import(FromName.getCXXNameType());
4410 if (T.isNull())
4411 return DeclarationName();
4412
4413 return ToContext.DeclarationNames.getCXXDestructorName(
4414 ToContext.getCanonicalType(T));
4415 }
4416
4417 case DeclarationName::CXXConversionFunctionName: {
4418 QualType T = Import(FromName.getCXXNameType());
4419 if (T.isNull())
4420 return DeclarationName();
4421
4422 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4423 ToContext.getCanonicalType(T));
4424 }
4425
4426 case DeclarationName::CXXOperatorName:
4427 return ToContext.DeclarationNames.getCXXOperatorName(
4428 FromName.getCXXOverloadedOperator());
4429
4430 case DeclarationName::CXXLiteralOperatorName:
4431 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4432 Import(FromName.getCXXLiteralIdentifier()));
4433
4434 case DeclarationName::CXXUsingDirective:
4435 // FIXME: STATICS!
4436 return DeclarationName::getUsingDirectiveName();
4437 }
4438
4439 // Silence bogus GCC warning
4440 return DeclarationName();
4441}
4442
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004443IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004444 if (!FromId)
4445 return 0;
4446
4447 return &ToContext.Idents.get(FromId->getName());
4448}
Douglas Gregor089459a2010-02-08 21:09:39 +00004449
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004450Selector ASTImporter::Import(Selector FromSel) {
4451 if (FromSel.isNull())
4452 return Selector();
4453
Chris Lattner5f9e2722011-07-23 10:55:15 +00004454 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004455 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4456 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4457 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4458 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4459}
4460
Douglas Gregor089459a2010-02-08 21:09:39 +00004461DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4462 DeclContext *DC,
4463 unsigned IDNS,
4464 NamedDecl **Decls,
4465 unsigned NumDecls) {
4466 return Name;
4467}
4468
4469DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004470 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004471}
4472
4473DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004474 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004475}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004476
4477Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4478 ImportedDecls[From] = To;
4479 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004480}
Douglas Gregorea35d112010-02-15 23:54:17 +00004481
4482bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004483 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004484 = ImportedTypes.find(From.getTypePtr());
4485 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4486 return true;
4487
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004488 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004489 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004490}