blob: 4094c88e13ba66871a935eca5f4b6d39c69414cc [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
Douglas Gregor89cc9d62010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000045 QualType VisitBuiltinType(BuiltinType *T);
46 QualType VisitComplexType(ComplexType *T);
47 QualType VisitPointerType(PointerType *T);
48 QualType VisitBlockPointerType(BlockPointerType *T);
49 QualType VisitLValueReferenceType(LValueReferenceType *T);
50 QualType VisitRValueReferenceType(RValueReferenceType *T);
51 QualType VisitMemberPointerType(MemberPointerType *T);
52 QualType VisitConstantArrayType(ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
54 QualType VisitVariableArrayType(VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(VectorType *T);
58 QualType VisitExtVectorType(ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitTypedefType(TypedefType *T);
63 QualType VisitTypeOfExprType(TypeOfExprType *T);
64 // FIXME: DependentTypeOfExprType
65 QualType VisitTypeOfType(TypeOfType *T);
66 QualType VisitDecltypeType(DecltypeType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(RecordType *T);
69 QualType VisitEnumType(EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
Douglas Gregord5dc83a2010-12-01 01:36:18 +000072 QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor1b2949d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCallc12c5bb2010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnara25777432010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas Gregor083a8212010-02-21 18:24:45 +000086 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000087 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregor040afae2010-11-30 19:14:50 +000088 TemplateParameterList *ImportTemplateParameterList(
89 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000090 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
91 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
92 unsigned NumFromArgs,
93 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +000094 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000095 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +000096 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000097 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +000098 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor9e5d9962010-02-10 21:10:29 +000099 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000100 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000102 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000103 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000104 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
105 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
106 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
107 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000108 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000109 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000110 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000111 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000112 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000113 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000114 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000115 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000116 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000117 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000118 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000119 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000120 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000121 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000122 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000123 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000124 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
125 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
126 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
127 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000128 Decl *VisitClassTemplateSpecializationDecl(
129 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000130
Douglas Gregor4800d952010-02-11 19:21:55 +0000131 // Importing statements
132 Stmt *VisitStmt(Stmt *S);
133
134 // Importing expressions
135 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000136 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000137 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000138 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000139 Expr *VisitParenExpr(ParenExpr *E);
140 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorbd249a52010-02-19 01:24:23 +0000141 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000142 Expr *VisitBinaryOperator(BinaryOperator *E);
143 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000144 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000145 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000146 };
147}
148
149//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000150// Structural Equivalence
151//----------------------------------------------------------------------------
152
153namespace {
154 struct StructuralEquivalenceContext {
155 /// \brief AST contexts for which we are checking structural equivalence.
156 ASTContext &C1, &C2;
157
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000158 /// \brief The set of "tentative" equivalences between two canonical
159 /// declarations, mapping from a declaration in the first context to the
160 /// declaration in the second context that we believe to be equivalent.
161 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
162
163 /// \brief Queue of declarations in the first context whose equivalence
164 /// with a declaration in the second context still needs to be verified.
165 std::deque<Decl *> DeclsToCheck;
166
Douglas Gregorea35d112010-02-15 23:54:17 +0000167 /// \brief Declaration (from, to) pairs that are known not to be equivalent
168 /// (which we have already complained about).
169 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
170
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000171 /// \brief Whether we're being strict about the spelling of types when
172 /// unifying two types.
173 bool StrictTypeSpelling;
174
175 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000176 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000177 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000178 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000179 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180
181 /// \brief Determine whether the two declarations are structurally
182 /// equivalent.
183 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
184
185 /// \brief Determine whether the two types are structurally equivalent.
186 bool IsStructurallyEquivalent(QualType T1, QualType T2);
187
188 private:
189 /// \brief Finish checking all of the structural equivalences.
190 ///
191 /// \returns true if an error occurred, false otherwise.
192 bool Finish();
193
194 public:
195 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000196 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000197 }
198
199 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000200 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000201 }
202 };
203}
204
205static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
206 QualType T1, QualType T2);
207static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
208 Decl *D1, Decl *D2);
209
210/// \brief Determine if two APInts have the same value, after zero-extending
211/// one of them (if needed!) to ensure that the bit-widths match.
212static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
213 if (I1.getBitWidth() == I2.getBitWidth())
214 return I1 == I2;
215
216 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000217 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000218
Jay Foad9f71a8f2010-12-07 08:25:34 +0000219 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000220}
221
222/// \brief Determine if two APSInts have the same value, zero- or sign-extending
223/// as needed.
224static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
225 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
226 return I1 == I2;
227
228 // Check for a bit-width mismatch.
229 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000230 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000231 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000232 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000233
234 // We have a signedness mismatch. Turn the signed value into an unsigned
235 // value.
236 if (I1.isSigned()) {
237 if (I1.isNegative())
238 return false;
239
240 return llvm::APSInt(I1, true) == I2;
241 }
242
243 if (I2.isNegative())
244 return false;
245
246 return I1 == llvm::APSInt(I2, true);
247}
248
249/// \brief Determine structural equivalence of two expressions.
250static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
251 Expr *E1, Expr *E2) {
252 if (!E1 || !E2)
253 return E1 == E2;
254
255 // FIXME: Actually perform a structural comparison!
256 return true;
257}
258
259/// \brief Determine whether two identifiers are equivalent.
260static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
261 const IdentifierInfo *Name2) {
262 if (!Name1 || !Name2)
263 return Name1 == Name2;
264
265 return Name1->getName() == Name2->getName();
266}
267
268/// \brief Determine whether two nested-name-specifiers are equivalent.
269static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
270 NestedNameSpecifier *NNS1,
271 NestedNameSpecifier *NNS2) {
272 // FIXME: Implement!
273 return true;
274}
275
276/// \brief Determine whether two template arguments are equivalent.
277static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
278 const TemplateArgument &Arg1,
279 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000280 if (Arg1.getKind() != Arg2.getKind())
281 return false;
282
283 switch (Arg1.getKind()) {
284 case TemplateArgument::Null:
285 return true;
286
287 case TemplateArgument::Type:
288 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
289
290 case TemplateArgument::Integral:
291 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
292 Arg2.getIntegralType()))
293 return false;
294
295 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
296
297 case TemplateArgument::Declaration:
298 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
299
300 case TemplateArgument::Template:
301 return IsStructurallyEquivalent(Context,
302 Arg1.getAsTemplate(),
303 Arg2.getAsTemplate());
304
305 case TemplateArgument::Expression:
306 return IsStructurallyEquivalent(Context,
307 Arg1.getAsExpr(), Arg2.getAsExpr());
308
309 case TemplateArgument::Pack:
310 if (Arg1.pack_size() != Arg2.pack_size())
311 return false;
312
313 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
314 if (!IsStructurallyEquivalent(Context,
315 Arg1.pack_begin()[I],
316 Arg2.pack_begin()[I]))
317 return false;
318
319 return true;
320 }
321
322 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000323 return true;
324}
325
326/// \brief Determine structural equivalence for the common part of array
327/// types.
328static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
329 const ArrayType *Array1,
330 const ArrayType *Array2) {
331 if (!IsStructurallyEquivalent(Context,
332 Array1->getElementType(),
333 Array2->getElementType()))
334 return false;
335 if (Array1->getSizeModifier() != Array2->getSizeModifier())
336 return false;
337 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
338 return false;
339
340 return true;
341}
342
343/// \brief Determine structural equivalence of two types.
344static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
345 QualType T1, QualType T2) {
346 if (T1.isNull() || T2.isNull())
347 return T1.isNull() && T2.isNull();
348
349 if (!Context.StrictTypeSpelling) {
350 // We aren't being strict about token-to-token equivalence of types,
351 // so map down to the canonical type.
352 T1 = Context.C1.getCanonicalType(T1);
353 T2 = Context.C2.getCanonicalType(T2);
354 }
355
356 if (T1.getQualifiers() != T2.getQualifiers())
357 return false;
358
Douglas Gregorea35d112010-02-15 23:54:17 +0000359 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000360
Douglas Gregorea35d112010-02-15 23:54:17 +0000361 if (T1->getTypeClass() != T2->getTypeClass()) {
362 // Compare function types with prototypes vs. without prototypes as if
363 // both did not have prototypes.
364 if (T1->getTypeClass() == Type::FunctionProto &&
365 T2->getTypeClass() == Type::FunctionNoProto)
366 TC = Type::FunctionNoProto;
367 else if (T1->getTypeClass() == Type::FunctionNoProto &&
368 T2->getTypeClass() == Type::FunctionProto)
369 TC = Type::FunctionNoProto;
370 else
371 return false;
372 }
373
374 switch (TC) {
375 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000376 // FIXME: Deal with Char_S/Char_U.
377 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
378 return false;
379 break;
380
381 case Type::Complex:
382 if (!IsStructurallyEquivalent(Context,
383 cast<ComplexType>(T1)->getElementType(),
384 cast<ComplexType>(T2)->getElementType()))
385 return false;
386 break;
387
388 case Type::Pointer:
389 if (!IsStructurallyEquivalent(Context,
390 cast<PointerType>(T1)->getPointeeType(),
391 cast<PointerType>(T2)->getPointeeType()))
392 return false;
393 break;
394
395 case Type::BlockPointer:
396 if (!IsStructurallyEquivalent(Context,
397 cast<BlockPointerType>(T1)->getPointeeType(),
398 cast<BlockPointerType>(T2)->getPointeeType()))
399 return false;
400 break;
401
402 case Type::LValueReference:
403 case Type::RValueReference: {
404 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
405 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
406 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
407 return false;
408 if (Ref1->isInnerRef() != Ref2->isInnerRef())
409 return false;
410 if (!IsStructurallyEquivalent(Context,
411 Ref1->getPointeeTypeAsWritten(),
412 Ref2->getPointeeTypeAsWritten()))
413 return false;
414 break;
415 }
416
417 case Type::MemberPointer: {
418 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
419 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
420 if (!IsStructurallyEquivalent(Context,
421 MemPtr1->getPointeeType(),
422 MemPtr2->getPointeeType()))
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 QualType(MemPtr1->getClass(), 0),
426 QualType(MemPtr2->getClass(), 0)))
427 return false;
428 break;
429 }
430
431 case Type::ConstantArray: {
432 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
433 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
434 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
435 return false;
436
437 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
438 return false;
439 break;
440 }
441
442 case Type::IncompleteArray:
443 if (!IsArrayStructurallyEquivalent(Context,
444 cast<ArrayType>(T1),
445 cast<ArrayType>(T2)))
446 return false;
447 break;
448
449 case Type::VariableArray: {
450 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
451 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
452 if (!IsStructurallyEquivalent(Context,
453 Array1->getSizeExpr(), Array2->getSizeExpr()))
454 return false;
455
456 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
457 return false;
458
459 break;
460 }
461
462 case Type::DependentSizedArray: {
463 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
464 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
465 if (!IsStructurallyEquivalent(Context,
466 Array1->getSizeExpr(), Array2->getSizeExpr()))
467 return false;
468
469 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
470 return false;
471
472 break;
473 }
474
475 case Type::DependentSizedExtVector: {
476 const DependentSizedExtVectorType *Vec1
477 = cast<DependentSizedExtVectorType>(T1);
478 const DependentSizedExtVectorType *Vec2
479 = cast<DependentSizedExtVectorType>(T2);
480 if (!IsStructurallyEquivalent(Context,
481 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
482 return false;
483 if (!IsStructurallyEquivalent(Context,
484 Vec1->getElementType(),
485 Vec2->getElementType()))
486 return false;
487 break;
488 }
489
490 case Type::Vector:
491 case Type::ExtVector: {
492 const VectorType *Vec1 = cast<VectorType>(T1);
493 const VectorType *Vec2 = cast<VectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getElementType(),
496 Vec2->getElementType()))
497 return false;
498 if (Vec1->getNumElements() != Vec2->getNumElements())
499 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000500 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000501 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000502 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000503 }
504
505 case Type::FunctionProto: {
506 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
507 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
508 if (Proto1->getNumArgs() != Proto2->getNumArgs())
509 return false;
510 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
511 if (!IsStructurallyEquivalent(Context,
512 Proto1->getArgType(I),
513 Proto2->getArgType(I)))
514 return false;
515 }
516 if (Proto1->isVariadic() != Proto2->isVariadic())
517 return false;
518 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
519 return false;
520 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
521 return false;
522 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getExceptionType(I),
527 Proto2->getExceptionType(I)))
528 return false;
529 }
530 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
531 return false;
532
533 // Fall through to check the bits common with FunctionNoProtoType.
534 }
535
536 case Type::FunctionNoProto: {
537 const FunctionType *Function1 = cast<FunctionType>(T1);
538 const FunctionType *Function2 = cast<FunctionType>(T2);
539 if (!IsStructurallyEquivalent(Context,
540 Function1->getResultType(),
541 Function2->getResultType()))
542 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000543 if (Function1->getExtInfo() != Function2->getExtInfo())
544 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 break;
546 }
547
548 case Type::UnresolvedUsing:
549 if (!IsStructurallyEquivalent(Context,
550 cast<UnresolvedUsingType>(T1)->getDecl(),
551 cast<UnresolvedUsingType>(T2)->getDecl()))
552 return false;
553
554 break;
555
556 case Type::Typedef:
557 if (!IsStructurallyEquivalent(Context,
558 cast<TypedefType>(T1)->getDecl(),
559 cast<TypedefType>(T2)->getDecl()))
560 return false;
561 break;
562
563 case Type::TypeOfExpr:
564 if (!IsStructurallyEquivalent(Context,
565 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
566 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
567 return false;
568 break;
569
570 case Type::TypeOf:
571 if (!IsStructurallyEquivalent(Context,
572 cast<TypeOfType>(T1)->getUnderlyingType(),
573 cast<TypeOfType>(T2)->getUnderlyingType()))
574 return false;
575 break;
576
577 case Type::Decltype:
578 if (!IsStructurallyEquivalent(Context,
579 cast<DecltypeType>(T1)->getUnderlyingExpr(),
580 cast<DecltypeType>(T2)->getUnderlyingExpr()))
581 return false;
582 break;
583
584 case Type::Record:
585 case Type::Enum:
586 if (!IsStructurallyEquivalent(Context,
587 cast<TagType>(T1)->getDecl(),
588 cast<TagType>(T2)->getDecl()))
589 return false;
590 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000591
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000592 case Type::TemplateTypeParm: {
593 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
594 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
595 if (Parm1->getDepth() != Parm2->getDepth())
596 return false;
597 if (Parm1->getIndex() != Parm2->getIndex())
598 return false;
599 if (Parm1->isParameterPack() != Parm2->isParameterPack())
600 return false;
601
602 // Names of template type parameters are never significant.
603 break;
604 }
605
606 case Type::SubstTemplateTypeParm: {
607 const SubstTemplateTypeParmType *Subst1
608 = cast<SubstTemplateTypeParmType>(T1);
609 const SubstTemplateTypeParmType *Subst2
610 = cast<SubstTemplateTypeParmType>(T2);
611 if (!IsStructurallyEquivalent(Context,
612 QualType(Subst1->getReplacedParameter(), 0),
613 QualType(Subst2->getReplacedParameter(), 0)))
614 return false;
615 if (!IsStructurallyEquivalent(Context,
616 Subst1->getReplacementType(),
617 Subst2->getReplacementType()))
618 return false;
619 break;
620 }
621
622 case Type::TemplateSpecialization: {
623 const TemplateSpecializationType *Spec1
624 = cast<TemplateSpecializationType>(T1);
625 const TemplateSpecializationType *Spec2
626 = cast<TemplateSpecializationType>(T2);
627 if (!IsStructurallyEquivalent(Context,
628 Spec1->getTemplateName(),
629 Spec2->getTemplateName()))
630 return false;
631 if (Spec1->getNumArgs() != Spec2->getNumArgs())
632 return false;
633 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
634 if (!IsStructurallyEquivalent(Context,
635 Spec1->getArg(I), Spec2->getArg(I)))
636 return false;
637 }
638 break;
639 }
640
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000641 case Type::Elaborated: {
642 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
643 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
644 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
645 if (Elab1->getKeyword() != Elab2->getKeyword())
646 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000647 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000648 Elab1->getQualifier(),
649 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000650 return false;
651 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000652 Elab1->getNamedType(),
653 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000654 return false;
655 break;
656 }
657
John McCall3cb0ebd2010-03-10 03:28:59 +0000658 case Type::InjectedClassName: {
659 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
660 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
661 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000662 Inj1->getInjectedSpecializationType(),
663 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000664 return false;
665 break;
666 }
667
Douglas Gregor4714c122010-03-31 17:34:00 +0000668 case Type::DependentName: {
669 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
670 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000671 if (!IsStructurallyEquivalent(Context,
672 Typename1->getQualifier(),
673 Typename2->getQualifier()))
674 return false;
675 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
676 Typename2->getIdentifier()))
677 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000678
679 break;
680 }
681
John McCall33500952010-06-11 00:33:02 +0000682 case Type::DependentTemplateSpecialization: {
683 const DependentTemplateSpecializationType *Spec1 =
684 cast<DependentTemplateSpecializationType>(T1);
685 const DependentTemplateSpecializationType *Spec2 =
686 cast<DependentTemplateSpecializationType>(T2);
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getQualifier(),
689 Spec2->getQualifier()))
690 return false;
691 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
692 Spec2->getIdentifier()))
693 return false;
694 if (Spec1->getNumArgs() != Spec2->getNumArgs())
695 return false;
696 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
697 if (!IsStructurallyEquivalent(Context,
698 Spec1->getArg(I), Spec2->getArg(I)))
699 return false;
700 }
701 break;
702 }
703
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000704 case Type::ObjCInterface: {
705 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
706 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
707 if (!IsStructurallyEquivalent(Context,
708 Iface1->getDecl(), Iface2->getDecl()))
709 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000710 break;
711 }
712
713 case Type::ObjCObject: {
714 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
715 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
716 if (!IsStructurallyEquivalent(Context,
717 Obj1->getBaseType(),
718 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000719 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000720 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
721 return false;
722 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000723 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000724 Obj1->getProtocol(I),
725 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000726 return false;
727 }
728 break;
729 }
730
731 case Type::ObjCObjectPointer: {
732 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
733 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
734 if (!IsStructurallyEquivalent(Context,
735 Ptr1->getPointeeType(),
736 Ptr2->getPointeeType()))
737 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000738 break;
739 }
740
741 } // end switch
742
743 return true;
744}
745
746/// \brief Determine structural equivalence of two records.
747static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
748 RecordDecl *D1, RecordDecl *D2) {
749 if (D1->isUnion() != D2->isUnion()) {
750 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
751 << Context.C2.getTypeDeclType(D2);
752 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
753 << D1->getDeclName() << (unsigned)D1->getTagKind();
754 return false;
755 }
756
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000757 // If both declarations are class template specializations, we know
758 // the ODR applies, so check the template and template arguments.
759 ClassTemplateSpecializationDecl *Spec1
760 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
761 ClassTemplateSpecializationDecl *Spec2
762 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
763 if (Spec1 && Spec2) {
764 // Check that the specialized templates are the same.
765 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
766 Spec2->getSpecializedTemplate()))
767 return false;
768
769 // Check that the template arguments are the same.
770 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
771 return false;
772
773 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
774 if (!IsStructurallyEquivalent(Context,
775 Spec1->getTemplateArgs().get(I),
776 Spec2->getTemplateArgs().get(I)))
777 return false;
778 }
779 // If one is a class template specialization and the other is not, these
780 // structures are diferent.
781 else if (Spec1 || Spec2)
782 return false;
783
Douglas Gregorea35d112010-02-15 23:54:17 +0000784 // Compare the definitions of these two records. If either or both are
785 // incomplete, we assume that they are equivalent.
786 D1 = D1->getDefinition();
787 D2 = D2->getDefinition();
788 if (!D1 || !D2)
789 return true;
790
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000791 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
792 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
793 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
794 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000795 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000797 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000799 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000800 return false;
801 }
802
803 // Check the base classes.
804 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
805 BaseEnd1 = D1CXX->bases_end(),
806 Base2 = D2CXX->bases_begin();
807 Base1 != BaseEnd1;
808 ++Base1, ++Base2) {
809 if (!IsStructurallyEquivalent(Context,
810 Base1->getType(), Base2->getType())) {
811 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
812 << Context.C2.getTypeDeclType(D2);
813 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
814 << Base2->getType()
815 << Base2->getSourceRange();
816 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
817 << Base1->getType()
818 << Base1->getSourceRange();
819 return false;
820 }
821
822 // Check virtual vs. non-virtual inheritance mismatch.
823 if (Base1->isVirtual() != Base2->isVirtual()) {
824 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
825 << Context.C2.getTypeDeclType(D2);
826 Context.Diag2(Base2->getSourceRange().getBegin(),
827 diag::note_odr_virtual_base)
828 << Base2->isVirtual() << Base2->getSourceRange();
829 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
830 << Base1->isVirtual()
831 << Base1->getSourceRange();
832 return false;
833 }
834 }
835 } else if (D1CXX->getNumBases() > 0) {
836 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
837 << Context.C2.getTypeDeclType(D2);
838 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
839 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
840 << Base1->getType()
841 << Base1->getSourceRange();
842 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
843 return false;
844 }
845 }
846
847 // Check the fields for consistency.
848 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
849 Field2End = D2->field_end();
850 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
851 Field1End = D1->field_end();
852 Field1 != Field1End;
853 ++Field1, ++Field2) {
854 if (Field2 == Field2End) {
855 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
856 << Context.C2.getTypeDeclType(D2);
857 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
858 << Field1->getDeclName() << Field1->getType();
859 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
860 return false;
861 }
862
863 if (!IsStructurallyEquivalent(Context,
864 Field1->getType(), Field2->getType())) {
865 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(D2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
868 << Field2->getDeclName() << Field2->getType();
869 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
870 << Field1->getDeclName() << Field1->getType();
871 return false;
872 }
873
874 if (Field1->isBitField() != Field2->isBitField()) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876 << Context.C2.getTypeDeclType(D2);
877 if (Field1->isBitField()) {
878 llvm::APSInt Bits;
879 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
880 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
881 << Field1->getDeclName() << Field1->getType()
882 << Bits.toString(10, false);
883 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
884 << Field2->getDeclName();
885 } else {
886 llvm::APSInt Bits;
887 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
888 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
889 << Field2->getDeclName() << Field2->getType()
890 << Bits.toString(10, false);
891 Context.Diag1(Field1->getLocation(),
892 diag::note_odr_not_bit_field)
893 << Field1->getDeclName();
894 }
895 return false;
896 }
897
898 if (Field1->isBitField()) {
899 // Make sure that the bit-fields are the same length.
900 llvm::APSInt Bits1, Bits2;
901 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
902 return false;
903 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
904 return false;
905
906 if (!IsSameValue(Bits1, Bits2)) {
907 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
908 << Context.C2.getTypeDeclType(D2);
909 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
910 << Field2->getDeclName() << Field2->getType()
911 << Bits2.toString(10, false);
912 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
913 << Field1->getDeclName() << Field1->getType()
914 << Bits1.toString(10, false);
915 return false;
916 }
917 }
918 }
919
920 if (Field2 != Field2End) {
921 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
922 << Context.C2.getTypeDeclType(D2);
923 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
924 << Field2->getDeclName() << Field2->getType();
925 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
926 return false;
927 }
928
929 return true;
930}
931
932/// \brief Determine structural equivalence of two enums.
933static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
934 EnumDecl *D1, EnumDecl *D2) {
935 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
936 EC2End = D2->enumerator_end();
937 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
938 EC1End = D1->enumerator_end();
939 EC1 != EC1End; ++EC1, ++EC2) {
940 if (EC2 == EC2End) {
941 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
942 << Context.C2.getTypeDeclType(D2);
943 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
944 << EC1->getDeclName()
945 << EC1->getInitVal().toString(10);
946 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
947 return false;
948 }
949
950 llvm::APSInt Val1 = EC1->getInitVal();
951 llvm::APSInt Val2 = EC2->getInitVal();
952 if (!IsSameValue(Val1, Val2) ||
953 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
956 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
957 << EC2->getDeclName()
958 << EC2->getInitVal().toString(10);
959 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
960 << EC1->getDeclName()
961 << EC1->getInitVal().toString(10);
962 return false;
963 }
964 }
965
966 if (EC2 != EC2End) {
967 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
968 << Context.C2.getTypeDeclType(D2);
969 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
970 << EC2->getDeclName()
971 << EC2->getInitVal().toString(10);
972 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
973 return false;
974 }
975
976 return true;
977}
Douglas Gregor040afae2010-11-30 19:14:50 +0000978
979static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
980 TemplateParameterList *Params1,
981 TemplateParameterList *Params2) {
982 if (Params1->size() != Params2->size()) {
983 Context.Diag2(Params2->getTemplateLoc(),
984 diag::err_odr_different_num_template_parameters)
985 << Params1->size() << Params2->size();
986 Context.Diag1(Params1->getTemplateLoc(),
987 diag::note_odr_template_parameter_list);
988 return false;
989 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000990
Douglas Gregor040afae2010-11-30 19:14:50 +0000991 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
992 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
993 Context.Diag2(Params2->getParam(I)->getLocation(),
994 diag::err_odr_different_template_parameter_kind);
995 Context.Diag1(Params1->getParam(I)->getLocation(),
996 diag::note_odr_template_parameter_here);
997 return false;
998 }
999
1000 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1001 Params2->getParam(I))) {
1002
1003 return false;
1004 }
1005 }
1006
1007 return true;
1008}
1009
1010static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1011 TemplateTypeParmDecl *D1,
1012 TemplateTypeParmDecl *D2) {
1013 if (D1->isParameterPack() != D2->isParameterPack()) {
1014 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1015 << D2->isParameterPack();
1016 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1017 << D1->isParameterPack();
1018 return false;
1019 }
1020
1021 return true;
1022}
1023
1024static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1025 NonTypeTemplateParmDecl *D1,
1026 NonTypeTemplateParmDecl *D2) {
1027 // FIXME: Enable once we have variadic templates.
1028#if 0
1029 if (D1->isParameterPack() != D2->isParameterPack()) {
1030 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1031 << D2->isParameterPack();
1032 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1033 << D1->isParameterPack();
1034 return false;
1035 }
1036#endif
1037
1038 // Check types.
1039 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1040 Context.Diag2(D2->getLocation(),
1041 diag::err_odr_non_type_parameter_type_inconsistent)
1042 << D2->getType() << D1->getType();
1043 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1044 << D1->getType();
1045 return false;
1046 }
1047
1048 return true;
1049}
1050
1051static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1052 TemplateTemplateParmDecl *D1,
1053 TemplateTemplateParmDecl *D2) {
1054 // FIXME: Enable once we have variadic templates.
1055#if 0
1056 if (D1->isParameterPack() != D2->isParameterPack()) {
1057 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1058 << D2->isParameterPack();
1059 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1060 << D1->isParameterPack();
1061 return false;
1062 }
1063#endif
1064
1065 // Check template parameter lists.
1066 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1067 D2->getTemplateParameters());
1068}
1069
1070static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1071 ClassTemplateDecl *D1,
1072 ClassTemplateDecl *D2) {
1073 // Check template parameters.
1074 if (!IsStructurallyEquivalent(Context,
1075 D1->getTemplateParameters(),
1076 D2->getTemplateParameters()))
1077 return false;
1078
1079 // Check the templated declaration.
1080 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1081 D2->getTemplatedDecl());
1082}
1083
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001084/// \brief Determine structural equivalence of two declarations.
1085static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1086 Decl *D1, Decl *D2) {
1087 // FIXME: Check for known structural equivalences via a callback of some sort.
1088
Douglas Gregorea35d112010-02-15 23:54:17 +00001089 // Check whether we already know that these two declarations are not
1090 // structurally equivalent.
1091 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1092 D2->getCanonicalDecl())))
1093 return false;
1094
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001095 // Determine whether we've already produced a tentative equivalence for D1.
1096 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1097 if (EquivToD1)
1098 return EquivToD1 == D2->getCanonicalDecl();
1099
1100 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1101 EquivToD1 = D2->getCanonicalDecl();
1102 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1103 return true;
1104}
1105
1106bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1107 Decl *D2) {
1108 if (!::IsStructurallyEquivalent(*this, D1, D2))
1109 return false;
1110
1111 return !Finish();
1112}
1113
1114bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1115 QualType T2) {
1116 if (!::IsStructurallyEquivalent(*this, T1, T2))
1117 return false;
1118
1119 return !Finish();
1120}
1121
1122bool StructuralEquivalenceContext::Finish() {
1123 while (!DeclsToCheck.empty()) {
1124 // Check the next declaration.
1125 Decl *D1 = DeclsToCheck.front();
1126 DeclsToCheck.pop_front();
1127
1128 Decl *D2 = TentativeEquivalences[D1];
1129 assert(D2 && "Unrecorded tentative equivalence?");
1130
Douglas Gregorea35d112010-02-15 23:54:17 +00001131 bool Equivalent = true;
1132
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001133 // FIXME: Switch on all declaration kinds. For now, we're just going to
1134 // check the obvious ones.
1135 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1136 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1137 // Check for equivalent structure names.
1138 IdentifierInfo *Name1 = Record1->getIdentifier();
1139 if (!Name1 && Record1->getTypedefForAnonDecl())
1140 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1141 IdentifierInfo *Name2 = Record2->getIdentifier();
1142 if (!Name2 && Record2->getTypedefForAnonDecl())
1143 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001144 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1145 !::IsStructurallyEquivalent(*this, Record1, Record2))
1146 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001147 } else {
1148 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001149 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001150 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001151 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001152 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1153 // Check for equivalent enum names.
1154 IdentifierInfo *Name1 = Enum1->getIdentifier();
1155 if (!Name1 && Enum1->getTypedefForAnonDecl())
1156 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1157 IdentifierInfo *Name2 = Enum2->getIdentifier();
1158 if (!Name2 && Enum2->getTypedefForAnonDecl())
1159 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001160 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1161 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1162 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001163 } else {
1164 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001165 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001166 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001167 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001168 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1169 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001170 Typedef2->getIdentifier()) ||
1171 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001172 Typedef1->getUnderlyingType(),
1173 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001174 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001175 } else {
1176 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001177 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001178 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001179 } else if (ClassTemplateDecl *ClassTemplate1
1180 = dyn_cast<ClassTemplateDecl>(D1)) {
1181 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1182 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1183 ClassTemplate2->getIdentifier()) ||
1184 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1185 Equivalent = false;
1186 } else {
1187 // Class template/non-class-template mismatch.
1188 Equivalent = false;
1189 }
1190 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1191 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1192 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1193 Equivalent = false;
1194 } else {
1195 // Kind mismatch.
1196 Equivalent = false;
1197 }
1198 } else if (NonTypeTemplateParmDecl *NTTP1
1199 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1200 if (NonTypeTemplateParmDecl *NTTP2
1201 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1202 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1203 Equivalent = false;
1204 } else {
1205 // Kind mismatch.
1206 Equivalent = false;
1207 }
1208 } else if (TemplateTemplateParmDecl *TTP1
1209 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1210 if (TemplateTemplateParmDecl *TTP2
1211 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1212 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1213 Equivalent = false;
1214 } else {
1215 // Kind mismatch.
1216 Equivalent = false;
1217 }
1218 }
1219
Douglas Gregorea35d112010-02-15 23:54:17 +00001220 if (!Equivalent) {
1221 // Note that these two declarations are not equivalent (and we already
1222 // know about it).
1223 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1224 D2->getCanonicalDecl()));
1225 return true;
1226 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001227 // FIXME: Check other declaration kinds!
1228 }
1229
1230 return false;
1231}
1232
1233//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001234// Import Types
1235//----------------------------------------------------------------------------
1236
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001237QualType ASTNodeImporter::VisitType(Type *T) {
1238 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1239 << T->getTypeClassName();
1240 return QualType();
1241}
1242
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001243QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1244 switch (T->getKind()) {
1245 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1246 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1247
1248 case BuiltinType::Char_U:
1249 // The context we're importing from has an unsigned 'char'. If we're
1250 // importing into a context with a signed 'char', translate to
1251 // 'unsigned char' instead.
1252 if (Importer.getToContext().getLangOptions().CharIsSigned)
1253 return Importer.getToContext().UnsignedCharTy;
1254
1255 return Importer.getToContext().CharTy;
1256
1257 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1258
1259 case BuiltinType::Char16:
1260 // FIXME: Make sure that the "to" context supports C++!
1261 return Importer.getToContext().Char16Ty;
1262
1263 case BuiltinType::Char32:
1264 // FIXME: Make sure that the "to" context supports C++!
1265 return Importer.getToContext().Char32Ty;
1266
1267 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1268 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1269 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1270 case BuiltinType::ULongLong:
1271 return Importer.getToContext().UnsignedLongLongTy;
1272 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1273
1274 case BuiltinType::Char_S:
1275 // The context we're importing from has an unsigned 'char'. If we're
1276 // importing into a context with a signed 'char', translate to
1277 // 'unsigned char' instead.
1278 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1279 return Importer.getToContext().SignedCharTy;
1280
1281 return Importer.getToContext().CharTy;
1282
1283 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1284 case BuiltinType::WChar:
1285 // FIXME: If not in C++, shall we translate to the C equivalent of
1286 // wchar_t?
1287 return Importer.getToContext().WCharTy;
1288
1289 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1290 case BuiltinType::Int : return Importer.getToContext().IntTy;
1291 case BuiltinType::Long : return Importer.getToContext().LongTy;
1292 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1293 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1294 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1295 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1296 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1297
1298 case BuiltinType::NullPtr:
1299 // FIXME: Make sure that the "to" context supports C++0x!
1300 return Importer.getToContext().NullPtrTy;
1301
1302 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1303 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1304 case BuiltinType::UndeducedAuto:
1305 // FIXME: Make sure that the "to" context supports C++0x!
1306 return Importer.getToContext().UndeducedAutoTy;
1307
1308 case BuiltinType::ObjCId:
1309 // FIXME: Make sure that the "to" context supports Objective-C!
1310 return Importer.getToContext().ObjCBuiltinIdTy;
1311
1312 case BuiltinType::ObjCClass:
1313 return Importer.getToContext().ObjCBuiltinClassTy;
1314
1315 case BuiltinType::ObjCSel:
1316 return Importer.getToContext().ObjCBuiltinSelTy;
1317 }
1318
1319 return QualType();
1320}
1321
1322QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1323 QualType ToElementType = Importer.Import(T->getElementType());
1324 if (ToElementType.isNull())
1325 return QualType();
1326
1327 return Importer.getToContext().getComplexType(ToElementType);
1328}
1329
1330QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1331 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1332 if (ToPointeeType.isNull())
1333 return QualType();
1334
1335 return Importer.getToContext().getPointerType(ToPointeeType);
1336}
1337
1338QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1339 // FIXME: Check for blocks support in "to" context.
1340 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1341 if (ToPointeeType.isNull())
1342 return QualType();
1343
1344 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1345}
1346
1347QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1348 // FIXME: Check for C++ support in "to" context.
1349 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1350 if (ToPointeeType.isNull())
1351 return QualType();
1352
1353 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1354}
1355
1356QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1357 // FIXME: Check for C++0x support in "to" context.
1358 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1359 if (ToPointeeType.isNull())
1360 return QualType();
1361
1362 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1363}
1364
1365QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1366 // FIXME: Check for C++ support in "to" context.
1367 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1368 if (ToPointeeType.isNull())
1369 return QualType();
1370
1371 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1372 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1373 ClassType.getTypePtr());
1374}
1375
1376QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1377 QualType ToElementType = Importer.Import(T->getElementType());
1378 if (ToElementType.isNull())
1379 return QualType();
1380
1381 return Importer.getToContext().getConstantArrayType(ToElementType,
1382 T->getSize(),
1383 T->getSizeModifier(),
1384 T->getIndexTypeCVRQualifiers());
1385}
1386
1387QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1388 QualType ToElementType = Importer.Import(T->getElementType());
1389 if (ToElementType.isNull())
1390 return QualType();
1391
1392 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1393 T->getSizeModifier(),
1394 T->getIndexTypeCVRQualifiers());
1395}
1396
1397QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1398 QualType ToElementType = Importer.Import(T->getElementType());
1399 if (ToElementType.isNull())
1400 return QualType();
1401
1402 Expr *Size = Importer.Import(T->getSizeExpr());
1403 if (!Size)
1404 return QualType();
1405
1406 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1407 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1408 T->getSizeModifier(),
1409 T->getIndexTypeCVRQualifiers(),
1410 Brackets);
1411}
1412
1413QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1414 QualType ToElementType = Importer.Import(T->getElementType());
1415 if (ToElementType.isNull())
1416 return QualType();
1417
1418 return Importer.getToContext().getVectorType(ToElementType,
1419 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001420 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421}
1422
1423QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1424 QualType ToElementType = Importer.Import(T->getElementType());
1425 if (ToElementType.isNull())
1426 return QualType();
1427
1428 return Importer.getToContext().getExtVectorType(ToElementType,
1429 T->getNumElements());
1430}
1431
1432QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1433 // FIXME: What happens if we're importing a function without a prototype
1434 // into C++? Should we make it variadic?
1435 QualType ToResultType = Importer.Import(T->getResultType());
1436 if (ToResultType.isNull())
1437 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001438
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001439 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001440 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001441}
1442
1443QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1444 QualType ToResultType = Importer.Import(T->getResultType());
1445 if (ToResultType.isNull())
1446 return QualType();
1447
1448 // Import argument types
1449 llvm::SmallVector<QualType, 4> ArgTypes;
1450 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1451 AEnd = T->arg_type_end();
1452 A != AEnd; ++A) {
1453 QualType ArgType = Importer.Import(*A);
1454 if (ArgType.isNull())
1455 return QualType();
1456 ArgTypes.push_back(ArgType);
1457 }
1458
1459 // Import exception types
1460 llvm::SmallVector<QualType, 4> ExceptionTypes;
1461 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1462 EEnd = T->exception_end();
1463 E != EEnd; ++E) {
1464 QualType ExceptionType = Importer.Import(*E);
1465 if (ExceptionType.isNull())
1466 return QualType();
1467 ExceptionTypes.push_back(ExceptionType);
1468 }
1469
1470 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1471 ArgTypes.size(),
1472 T->isVariadic(),
1473 T->getTypeQuals(),
1474 T->hasExceptionSpec(),
1475 T->hasAnyExceptionSpec(),
1476 ExceptionTypes.size(),
1477 ExceptionTypes.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001478 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001479}
1480
1481QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1482 TypedefDecl *ToDecl
1483 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1484 if (!ToDecl)
1485 return QualType();
1486
1487 return Importer.getToContext().getTypeDeclType(ToDecl);
1488}
1489
1490QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1491 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1492 if (!ToExpr)
1493 return QualType();
1494
1495 return Importer.getToContext().getTypeOfExprType(ToExpr);
1496}
1497
1498QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1499 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1500 if (ToUnderlyingType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1504}
1505
1506QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1507 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1508 if (!ToExpr)
1509 return QualType();
1510
1511 return Importer.getToContext().getDecltypeType(ToExpr);
1512}
1513
1514QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1515 RecordDecl *ToDecl
1516 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1517 if (!ToDecl)
1518 return QualType();
1519
1520 return Importer.getToContext().getTagDeclType(ToDecl);
1521}
1522
1523QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1524 EnumDecl *ToDecl
1525 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1526 if (!ToDecl)
1527 return QualType();
1528
1529 return Importer.getToContext().getTagDeclType(ToDecl);
1530}
1531
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001532QualType ASTNodeImporter::VisitTemplateSpecializationType(
1533 TemplateSpecializationType *T) {
1534 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1535 if (ToTemplate.isNull())
1536 return QualType();
1537
1538 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1539 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1540 return QualType();
1541
1542 QualType ToCanonType;
1543 if (!QualType(T, 0).isCanonical()) {
1544 QualType FromCanonType
1545 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1546 ToCanonType =Importer.Import(FromCanonType);
1547 if (ToCanonType.isNull())
1548 return QualType();
1549 }
1550 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1551 ToTemplateArgs.data(),
1552 ToTemplateArgs.size(),
1553 ToCanonType);
1554}
1555
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001556QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001557 NestedNameSpecifier *ToQualifier = 0;
1558 // Note: the qualifier in an ElaboratedType is optional.
1559 if (T->getQualifier()) {
1560 ToQualifier = Importer.Import(T->getQualifier());
1561 if (!ToQualifier)
1562 return QualType();
1563 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001564
1565 QualType ToNamedType = Importer.Import(T->getNamedType());
1566 if (ToNamedType.isNull())
1567 return QualType();
1568
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001569 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1570 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001571}
1572
1573QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1574 ObjCInterfaceDecl *Class
1575 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1576 if (!Class)
1577 return QualType();
1578
John McCallc12c5bb2010-05-15 11:32:37 +00001579 return Importer.getToContext().getObjCInterfaceType(Class);
1580}
1581
1582QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1583 QualType ToBaseType = Importer.Import(T->getBaseType());
1584 if (ToBaseType.isNull())
1585 return QualType();
1586
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001587 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001588 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 PEnd = T->qual_end();
1590 P != PEnd; ++P) {
1591 ObjCProtocolDecl *Protocol
1592 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1593 if (!Protocol)
1594 return QualType();
1595 Protocols.push_back(Protocol);
1596 }
1597
John McCallc12c5bb2010-05-15 11:32:37 +00001598 return Importer.getToContext().getObjCObjectType(ToBaseType,
1599 Protocols.data(),
1600 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001601}
1602
1603QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1604 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1605 if (ToPointeeType.isNull())
1606 return QualType();
1607
John McCallc12c5bb2010-05-15 11:32:37 +00001608 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001609}
1610
Douglas Gregor089459a2010-02-08 21:09:39 +00001611//----------------------------------------------------------------------------
1612// Import Declarations
1613//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001614bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1615 DeclContext *&LexicalDC,
1616 DeclarationName &Name,
1617 SourceLocation &Loc) {
1618 // Import the context of this declaration.
1619 DC = Importer.ImportContext(D->getDeclContext());
1620 if (!DC)
1621 return true;
1622
1623 LexicalDC = DC;
1624 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1625 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1626 if (!LexicalDC)
1627 return true;
1628 }
1629
1630 // Import the name of this declaration.
1631 Name = Importer.Import(D->getDeclName());
1632 if (D->getDeclName() && !Name)
1633 return true;
1634
1635 // Import the location of this declaration.
1636 Loc = Importer.Import(D->getLocation());
1637 return false;
1638}
1639
Abramo Bagnara25777432010-08-11 22:01:17 +00001640void
1641ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1642 DeclarationNameInfo& To) {
1643 // NOTE: To.Name and To.Loc are already imported.
1644 // We only have to import To.LocInfo.
1645 switch (To.getName().getNameKind()) {
1646 case DeclarationName::Identifier:
1647 case DeclarationName::ObjCZeroArgSelector:
1648 case DeclarationName::ObjCOneArgSelector:
1649 case DeclarationName::ObjCMultiArgSelector:
1650 case DeclarationName::CXXUsingDirective:
1651 return;
1652
1653 case DeclarationName::CXXOperatorName: {
1654 SourceRange Range = From.getCXXOperatorNameRange();
1655 To.setCXXOperatorNameRange(Importer.Import(Range));
1656 return;
1657 }
1658 case DeclarationName::CXXLiteralOperatorName: {
1659 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1660 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1661 return;
1662 }
1663 case DeclarationName::CXXConstructorName:
1664 case DeclarationName::CXXDestructorName:
1665 case DeclarationName::CXXConversionFunctionName: {
1666 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1667 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1668 return;
1669 }
1670 assert(0 && "Unknown name kind.");
1671 }
1672}
1673
Douglas Gregor083a8212010-02-21 18:24:45 +00001674void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1675 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1676 FromEnd = FromDC->decls_end();
1677 From != FromEnd;
1678 ++From)
1679 Importer.Import(*From);
1680}
1681
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001682bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1683 if (To->getDefinition())
1684 return false;
1685
1686 To->startDefinition();
1687
1688 // Add base classes.
1689 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1690 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1691
1692 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1693 for (CXXRecordDecl::base_class_iterator
1694 Base1 = FromCXX->bases_begin(),
1695 FromBaseEnd = FromCXX->bases_end();
1696 Base1 != FromBaseEnd;
1697 ++Base1) {
1698 QualType T = Importer.Import(Base1->getType());
1699 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001700 return true;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001701
1702 Bases.push_back(
1703 new (Importer.getToContext())
1704 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1705 Base1->isVirtual(),
1706 Base1->isBaseOfClass(),
1707 Base1->getAccessSpecifierAsWritten(),
1708 Importer.Import(Base1->getTypeSourceInfo())));
1709 }
1710 if (!Bases.empty())
1711 ToCXX->setBases(Bases.data(), Bases.size());
1712 }
1713
1714 ImportDeclContext(From);
1715 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001716 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001717}
1718
Douglas Gregor040afae2010-11-30 19:14:50 +00001719TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1720 TemplateParameterList *Params) {
1721 llvm::SmallVector<NamedDecl *, 4> ToParams;
1722 ToParams.reserve(Params->size());
1723 for (TemplateParameterList::iterator P = Params->begin(),
1724 PEnd = Params->end();
1725 P != PEnd; ++P) {
1726 Decl *To = Importer.Import(*P);
1727 if (!To)
1728 return 0;
1729
1730 ToParams.push_back(cast<NamedDecl>(To));
1731 }
1732
1733 return TemplateParameterList::Create(Importer.getToContext(),
1734 Importer.Import(Params->getTemplateLoc()),
1735 Importer.Import(Params->getLAngleLoc()),
1736 ToParams.data(), ToParams.size(),
1737 Importer.Import(Params->getRAngleLoc()));
1738}
1739
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001740TemplateArgument
1741ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1742 switch (From.getKind()) {
1743 case TemplateArgument::Null:
1744 return TemplateArgument();
1745
1746 case TemplateArgument::Type: {
1747 QualType ToType = Importer.Import(From.getAsType());
1748 if (ToType.isNull())
1749 return TemplateArgument();
1750 return TemplateArgument(ToType);
1751 }
1752
1753 case TemplateArgument::Integral: {
1754 QualType ToType = Importer.Import(From.getIntegralType());
1755 if (ToType.isNull())
1756 return TemplateArgument();
1757 return TemplateArgument(*From.getAsIntegral(), ToType);
1758 }
1759
1760 case TemplateArgument::Declaration:
1761 if (Decl *To = Importer.Import(From.getAsDecl()))
1762 return TemplateArgument(To);
1763 return TemplateArgument();
1764
1765 case TemplateArgument::Template: {
1766 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1767 if (ToTemplate.isNull())
1768 return TemplateArgument();
1769
1770 return TemplateArgument(ToTemplate);
1771 }
1772
1773 case TemplateArgument::Expression:
1774 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1775 return TemplateArgument(ToExpr);
1776 return TemplateArgument();
1777
1778 case TemplateArgument::Pack: {
1779 llvm::SmallVector<TemplateArgument, 2> ToPack;
1780 ToPack.reserve(From.pack_size());
1781 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1782 return TemplateArgument();
1783
1784 TemplateArgument *ToArgs
1785 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1786 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1787 return TemplateArgument(ToArgs, ToPack.size());
1788 }
1789 }
1790
1791 llvm_unreachable("Invalid template argument kind");
1792 return TemplateArgument();
1793}
1794
1795bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1796 unsigned NumFromArgs,
1797 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1798 for (unsigned I = 0; I != NumFromArgs; ++I) {
1799 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1800 if (To.isNull() && !FromArgs[I].isNull())
1801 return true;
1802
1803 ToArgs.push_back(To);
1804 }
1805
1806 return false;
1807}
1808
Douglas Gregor96a01b42010-02-11 00:48:18 +00001809bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001810 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001811 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001812 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001813 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001814 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001815}
1816
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001817bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001818 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001819 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001820 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001821 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001822}
1823
Douglas Gregor040afae2010-11-30 19:14:50 +00001824bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1825 ClassTemplateDecl *To) {
1826 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1827 Importer.getToContext(),
1828 Importer.getNonEquivalentDecls());
1829 return Ctx.IsStructurallyEquivalent(From, To);
1830}
1831
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001832Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001833 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001834 << D->getDeclKindName();
1835 return 0;
1836}
1837
Douglas Gregor788c62d2010-02-21 18:26:36 +00001838Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1839 // Import the major distinguishing characteristics of this namespace.
1840 DeclContext *DC, *LexicalDC;
1841 DeclarationName Name;
1842 SourceLocation Loc;
1843 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1844 return 0;
1845
1846 NamespaceDecl *MergeWithNamespace = 0;
1847 if (!Name) {
1848 // This is an anonymous namespace. Adopt an existing anonymous
1849 // namespace if we can.
1850 // FIXME: Not testable.
1851 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1852 MergeWithNamespace = TU->getAnonymousNamespace();
1853 else
1854 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1855 } else {
1856 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1857 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1858 Lookup.first != Lookup.second;
1859 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001860 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001861 continue;
1862
1863 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1864 MergeWithNamespace = FoundNS;
1865 ConflictingDecls.clear();
1866 break;
1867 }
1868
1869 ConflictingDecls.push_back(*Lookup.first);
1870 }
1871
1872 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001873 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001874 ConflictingDecls.data(),
1875 ConflictingDecls.size());
1876 }
1877 }
1878
1879 // Create the "to" namespace, if needed.
1880 NamespaceDecl *ToNamespace = MergeWithNamespace;
1881 if (!ToNamespace) {
1882 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1883 Name.getAsIdentifierInfo());
1884 ToNamespace->setLexicalDeclContext(LexicalDC);
1885 LexicalDC->addDecl(ToNamespace);
1886
1887 // If this is an anonymous namespace, register it as the anonymous
1888 // namespace within its context.
1889 if (!Name) {
1890 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1891 TU->setAnonymousNamespace(ToNamespace);
1892 else
1893 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1894 }
1895 }
1896 Importer.Imported(D, ToNamespace);
1897
1898 ImportDeclContext(D);
1899
1900 return ToNamespace;
1901}
1902
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001903Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1904 // Import the major distinguishing characteristics of this typedef.
1905 DeclContext *DC, *LexicalDC;
1906 DeclarationName Name;
1907 SourceLocation Loc;
1908 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1909 return 0;
1910
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001911 // If this typedef is not in block scope, determine whether we've
1912 // seen a typedef with the same name (that we can merge with) or any
1913 // other entity by that name (which name lookup could conflict with).
1914 if (!DC->isFunctionOrMethod()) {
1915 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1916 unsigned IDNS = Decl::IDNS_Ordinary;
1917 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1918 Lookup.first != Lookup.second;
1919 ++Lookup.first) {
1920 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1921 continue;
1922 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001923 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1924 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001925 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001926 }
1927
1928 ConflictingDecls.push_back(*Lookup.first);
1929 }
1930
1931 if (!ConflictingDecls.empty()) {
1932 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1933 ConflictingDecls.data(),
1934 ConflictingDecls.size());
1935 if (!Name)
1936 return 0;
1937 }
1938 }
1939
Douglas Gregorea35d112010-02-15 23:54:17 +00001940 // Import the underlying type of this typedef;
1941 QualType T = Importer.Import(D->getUnderlyingType());
1942 if (T.isNull())
1943 return 0;
1944
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001945 // Create the new typedef node.
1946 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1947 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1948 Loc, Name.getAsIdentifierInfo(),
1949 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001950 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001951 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001952 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001953 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001954
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001955 return ToTypedef;
1956}
1957
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001958Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1959 // Import the major distinguishing characteristics of this enum.
1960 DeclContext *DC, *LexicalDC;
1961 DeclarationName Name;
1962 SourceLocation Loc;
1963 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1964 return 0;
1965
1966 // Figure out what enum name we're looking for.
1967 unsigned IDNS = Decl::IDNS_Tag;
1968 DeclarationName SearchName = Name;
1969 if (!SearchName && D->getTypedefForAnonDecl()) {
1970 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1971 IDNS = Decl::IDNS_Ordinary;
1972 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1973 IDNS |= Decl::IDNS_Ordinary;
1974
1975 // We may already have an enum of the same name; try to find and match it.
1976 if (!DC->isFunctionOrMethod() && SearchName) {
1977 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1978 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1979 Lookup.first != Lookup.second;
1980 ++Lookup.first) {
1981 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1982 continue;
1983
1984 Decl *Found = *Lookup.first;
1985 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1986 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1987 Found = Tag->getDecl();
1988 }
1989
1990 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001991 if (IsStructuralMatch(D, FoundEnum))
1992 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001993 }
1994
1995 ConflictingDecls.push_back(*Lookup.first);
1996 }
1997
1998 if (!ConflictingDecls.empty()) {
1999 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2000 ConflictingDecls.data(),
2001 ConflictingDecls.size());
2002 }
2003 }
2004
2005 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002007 Name.getAsIdentifierInfo(),
2008 Importer.Import(D->getTagKeywordLoc()), 0,
2009 D->isScoped(), D->isScopedUsingClassTag(),
2010 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002011 // Import the qualifier, if any.
2012 if (D->getQualifier()) {
2013 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2014 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2015 D2->setQualifierInfo(NNS, NNSRange);
2016 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002017 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002018 D2->setLexicalDeclContext(LexicalDC);
2019 Importer.Imported(D, D2);
2020 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002021
2022 // Import the integer type.
2023 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2024 if (ToIntegerType.isNull())
2025 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002026 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002027
2028 // Import the definition
2029 if (D->isDefinition()) {
2030 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2031 if (T.isNull())
2032 return 0;
2033
2034 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2035 if (ToPromotionType.isNull())
2036 return 0;
2037
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002038 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002039 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002040
2041 // FIXME: we might need to merge the number of positive or negative bits
2042 // if the enumerator lists don't match.
2043 D2->completeDefinition(T, ToPromotionType,
2044 D->getNumPositiveBits(),
2045 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002046 }
2047
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002048 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002049}
2050
Douglas Gregor96a01b42010-02-11 00:48:18 +00002051Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2052 // If this record has a definition in the translation unit we're coming from,
2053 // but this particular declaration is not that definition, import the
2054 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002055 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002056 if (Definition && Definition != D) {
2057 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002058 if (!ImportedDef)
2059 return 0;
2060
2061 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002062 }
2063
2064 // Import the major distinguishing characteristics of this record.
2065 DeclContext *DC, *LexicalDC;
2066 DeclarationName Name;
2067 SourceLocation Loc;
2068 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2069 return 0;
2070
2071 // Figure out what structure name we're looking for.
2072 unsigned IDNS = Decl::IDNS_Tag;
2073 DeclarationName SearchName = Name;
2074 if (!SearchName && D->getTypedefForAnonDecl()) {
2075 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2076 IDNS = Decl::IDNS_Ordinary;
2077 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2078 IDNS |= Decl::IDNS_Ordinary;
2079
2080 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002081 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002082 if (!DC->isFunctionOrMethod() && SearchName) {
2083 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2084 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2085 Lookup.first != Lookup.second;
2086 ++Lookup.first) {
2087 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2088 continue;
2089
2090 Decl *Found = *Lookup.first;
2091 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2092 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2093 Found = Tag->getDecl();
2094 }
2095
2096 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002097 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2098 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2099 // The record types structurally match, or the "from" translation
2100 // unit only had a forward declaration anyway; call it the same
2101 // function.
2102 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002103 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002104 }
2105 } else {
2106 // We have a forward declaration of this type, so adopt that forward
2107 // declaration rather than building a new one.
2108 AdoptDecl = FoundRecord;
2109 continue;
2110 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002111 }
2112
2113 ConflictingDecls.push_back(*Lookup.first);
2114 }
2115
2116 if (!ConflictingDecls.empty()) {
2117 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2118 ConflictingDecls.data(),
2119 ConflictingDecls.size());
2120 }
2121 }
2122
2123 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002124 RecordDecl *D2 = AdoptDecl;
2125 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002126 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002127 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002128 D->getTagKind(),
2129 DC, Loc,
2130 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002131 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002132 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002133 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002134 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002135 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002136 DC, Loc,
2137 Name.getAsIdentifierInfo(),
2138 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002139 }
John McCallb6217662010-03-15 10:12:16 +00002140 // Import the qualifier, if any.
2141 if (D->getQualifier()) {
2142 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2143 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2144 D2->setQualifierInfo(NNS, NNSRange);
2145 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002146 D2->setLexicalDeclContext(LexicalDC);
2147 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002148 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002149
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002150 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002151
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002152 if (D->isDefinition() && ImportDefinition(D, D2))
2153 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002154
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002155 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002156}
2157
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002158Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2159 // Import the major distinguishing characteristics of this enumerator.
2160 DeclContext *DC, *LexicalDC;
2161 DeclarationName Name;
2162 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002163 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002164 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002165
2166 QualType T = Importer.Import(D->getType());
2167 if (T.isNull())
2168 return 0;
2169
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002170 // Determine whether there are any other declarations with the same name and
2171 // in the same context.
2172 if (!LexicalDC->isFunctionOrMethod()) {
2173 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2174 unsigned IDNS = Decl::IDNS_Ordinary;
2175 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2176 Lookup.first != Lookup.second;
2177 ++Lookup.first) {
2178 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2179 continue;
2180
2181 ConflictingDecls.push_back(*Lookup.first);
2182 }
2183
2184 if (!ConflictingDecls.empty()) {
2185 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2186 ConflictingDecls.data(),
2187 ConflictingDecls.size());
2188 if (!Name)
2189 return 0;
2190 }
2191 }
2192
2193 Expr *Init = Importer.Import(D->getInitExpr());
2194 if (D->getInitExpr() && !Init)
2195 return 0;
2196
2197 EnumConstantDecl *ToEnumerator
2198 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2199 Name.getAsIdentifierInfo(), T,
2200 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002201 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002202 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002203 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002204 LexicalDC->addDecl(ToEnumerator);
2205 return ToEnumerator;
2206}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002207
Douglas Gregora404ea62010-02-10 19:54:31 +00002208Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2209 // Import the major distinguishing characteristics of this function.
2210 DeclContext *DC, *LexicalDC;
2211 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002212 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002213 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002214 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002215
Douglas Gregora404ea62010-02-10 19:54:31 +00002216 // Try to find a function in our own ("to") context with the same name, same
2217 // type, and in the same context as the function we're importing.
2218 if (!LexicalDC->isFunctionOrMethod()) {
2219 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2220 unsigned IDNS = Decl::IDNS_Ordinary;
2221 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2222 Lookup.first != Lookup.second;
2223 ++Lookup.first) {
2224 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2225 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002226
Douglas Gregora404ea62010-02-10 19:54:31 +00002227 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2228 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2229 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002230 if (Importer.IsStructurallyEquivalent(D->getType(),
2231 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002232 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002233 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002234 }
2235
2236 // FIXME: Check for overloading more carefully, e.g., by boosting
2237 // Sema::IsOverload out to the AST library.
2238
2239 // Function overloading is okay in C++.
2240 if (Importer.getToContext().getLangOptions().CPlusPlus)
2241 continue;
2242
2243 // Complain about inconsistent function types.
2244 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002245 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002246 Importer.ToDiag(FoundFunction->getLocation(),
2247 diag::note_odr_value_here)
2248 << FoundFunction->getType();
2249 }
2250 }
2251
2252 ConflictingDecls.push_back(*Lookup.first);
2253 }
2254
2255 if (!ConflictingDecls.empty()) {
2256 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2257 ConflictingDecls.data(),
2258 ConflictingDecls.size());
2259 if (!Name)
2260 return 0;
2261 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002262 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002263
Abramo Bagnara25777432010-08-11 22:01:17 +00002264 DeclarationNameInfo NameInfo(Name, Loc);
2265 // Import additional name location/type info.
2266 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2267
Douglas Gregorea35d112010-02-15 23:54:17 +00002268 // Import the type.
2269 QualType T = Importer.Import(D->getType());
2270 if (T.isNull())
2271 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002272
2273 // Import the function parameters.
2274 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2275 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2276 P != PEnd; ++P) {
2277 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2278 if (!ToP)
2279 return 0;
2280
2281 Parameters.push_back(ToP);
2282 }
2283
2284 // Create the imported function.
2285 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002286 FunctionDecl *ToFunction = 0;
2287 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2288 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2289 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002290 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002291 FromConstructor->isExplicit(),
2292 D->isInlineSpecified(),
2293 D->isImplicit());
2294 } else if (isa<CXXDestructorDecl>(D)) {
2295 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2296 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002297 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002298 D->isInlineSpecified(),
2299 D->isImplicit());
2300 } else if (CXXConversionDecl *FromConversion
2301 = dyn_cast<CXXConversionDecl>(D)) {
2302 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2303 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002304 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002305 D->isInlineSpecified(),
2306 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002307 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2308 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2309 cast<CXXRecordDecl>(DC),
2310 NameInfo, T, TInfo,
2311 Method->isStatic(),
2312 Method->getStorageClassAsWritten(),
2313 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002314 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002315 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2316 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002317 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002318 D->isInlineSpecified(),
2319 D->hasWrittenPrototype());
2320 }
John McCallb6217662010-03-15 10:12:16 +00002321
2322 // Import the qualifier, if any.
2323 if (D->getQualifier()) {
2324 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2325 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2326 ToFunction->setQualifierInfo(NNS, NNSRange);
2327 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002328 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002329 ToFunction->setLexicalDeclContext(LexicalDC);
2330 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002331
Douglas Gregora404ea62010-02-10 19:54:31 +00002332 // Set the parameters.
2333 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002334 Parameters[I]->setOwningFunction(ToFunction);
2335 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002336 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002337 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002338
2339 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002340
2341 // Add this function to the lexical context.
2342 LexicalDC->addDecl(ToFunction);
2343
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002344 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002345}
2346
Douglas Gregorc144f352010-02-21 18:29:16 +00002347Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2348 return VisitFunctionDecl(D);
2349}
2350
2351Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2352 return VisitCXXMethodDecl(D);
2353}
2354
2355Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2356 return VisitCXXMethodDecl(D);
2357}
2358
2359Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2360 return VisitCXXMethodDecl(D);
2361}
2362
Douglas Gregor96a01b42010-02-11 00:48:18 +00002363Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2364 // Import the major distinguishing characteristics of a variable.
2365 DeclContext *DC, *LexicalDC;
2366 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002367 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002368 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2369 return 0;
2370
2371 // Import the type.
2372 QualType T = Importer.Import(D->getType());
2373 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002374 return 0;
2375
2376 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2377 Expr *BitWidth = Importer.Import(D->getBitWidth());
2378 if (!BitWidth && D->getBitWidth())
2379 return 0;
2380
2381 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2382 Loc, Name.getAsIdentifierInfo(),
2383 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002384 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002385 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002386 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002387 LexicalDC->addDecl(ToField);
2388 return ToField;
2389}
2390
Francois Pichet87c2e122010-11-21 06:08:52 +00002391Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2392 // Import the major distinguishing characteristics of a variable.
2393 DeclContext *DC, *LexicalDC;
2394 DeclarationName Name;
2395 SourceLocation Loc;
2396 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2397 return 0;
2398
2399 // Import the type.
2400 QualType T = Importer.Import(D->getType());
2401 if (T.isNull())
2402 return 0;
2403
2404 NamedDecl **NamedChain =
2405 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2406
2407 unsigned i = 0;
2408 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2409 PE = D->chain_end(); PI != PE; ++PI) {
2410 Decl* D = Importer.Import(*PI);
2411 if (!D)
2412 return 0;
2413 NamedChain[i++] = cast<NamedDecl>(D);
2414 }
2415
2416 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2417 Importer.getToContext(), DC,
2418 Loc, Name.getAsIdentifierInfo(), T,
2419 NamedChain, D->getChainingSize());
2420 ToIndirectField->setAccess(D->getAccess());
2421 ToIndirectField->setLexicalDeclContext(LexicalDC);
2422 Importer.Imported(D, ToIndirectField);
2423 LexicalDC->addDecl(ToIndirectField);
2424 return ToIndirectField;
2425}
2426
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002427Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2428 // Import the major distinguishing characteristics of an ivar.
2429 DeclContext *DC, *LexicalDC;
2430 DeclarationName Name;
2431 SourceLocation Loc;
2432 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2433 return 0;
2434
2435 // Determine whether we've already imported this ivar
2436 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2437 Lookup.first != Lookup.second;
2438 ++Lookup.first) {
2439 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2440 if (Importer.IsStructurallyEquivalent(D->getType(),
2441 FoundIvar->getType())) {
2442 Importer.Imported(D, FoundIvar);
2443 return FoundIvar;
2444 }
2445
2446 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2447 << Name << D->getType() << FoundIvar->getType();
2448 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2449 << FoundIvar->getType();
2450 return 0;
2451 }
2452 }
2453
2454 // Import the type.
2455 QualType T = Importer.Import(D->getType());
2456 if (T.isNull())
2457 return 0;
2458
2459 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2460 Expr *BitWidth = Importer.Import(D->getBitWidth());
2461 if (!BitWidth && D->getBitWidth())
2462 return 0;
2463
Daniel Dunbara0654922010-04-02 20:10:03 +00002464 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2465 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002466 Loc, Name.getAsIdentifierInfo(),
2467 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002468 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002469 ToIvar->setLexicalDeclContext(LexicalDC);
2470 Importer.Imported(D, ToIvar);
2471 LexicalDC->addDecl(ToIvar);
2472 return ToIvar;
2473
2474}
2475
Douglas Gregora404ea62010-02-10 19:54:31 +00002476Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2477 // Import the major distinguishing characteristics of a variable.
2478 DeclContext *DC, *LexicalDC;
2479 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002480 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002481 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002482 return 0;
2483
Douglas Gregor089459a2010-02-08 21:09:39 +00002484 // Try to find a variable in our own ("to") context with the same name and
2485 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002486 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002487 VarDecl *MergeWithVar = 0;
2488 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2489 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002490 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002491 Lookup.first != Lookup.second;
2492 ++Lookup.first) {
2493 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2494 continue;
2495
2496 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2497 // We have found a variable that we may need to merge with. Check it.
2498 if (isExternalLinkage(FoundVar->getLinkage()) &&
2499 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002500 if (Importer.IsStructurallyEquivalent(D->getType(),
2501 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002502 MergeWithVar = FoundVar;
2503 break;
2504 }
2505
Douglas Gregord0145422010-02-12 17:23:39 +00002506 const ArrayType *FoundArray
2507 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2508 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002509 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002510 if (FoundArray && TArray) {
2511 if (isa<IncompleteArrayType>(FoundArray) &&
2512 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002513 // Import the type.
2514 QualType T = Importer.Import(D->getType());
2515 if (T.isNull())
2516 return 0;
2517
Douglas Gregord0145422010-02-12 17:23:39 +00002518 FoundVar->setType(T);
2519 MergeWithVar = FoundVar;
2520 break;
2521 } else if (isa<IncompleteArrayType>(TArray) &&
2522 isa<ConstantArrayType>(FoundArray)) {
2523 MergeWithVar = FoundVar;
2524 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002525 }
2526 }
2527
Douglas Gregor089459a2010-02-08 21:09:39 +00002528 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002529 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002530 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2531 << FoundVar->getType();
2532 }
2533 }
2534
2535 ConflictingDecls.push_back(*Lookup.first);
2536 }
2537
2538 if (MergeWithVar) {
2539 // An equivalent variable with external linkage has been found. Link
2540 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002541 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002542
2543 if (VarDecl *DDef = D->getDefinition()) {
2544 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2545 Importer.ToDiag(ExistingDef->getLocation(),
2546 diag::err_odr_variable_multiple_def)
2547 << Name;
2548 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2549 } else {
2550 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002551 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002552 }
2553 }
2554
2555 return MergeWithVar;
2556 }
2557
2558 if (!ConflictingDecls.empty()) {
2559 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2560 ConflictingDecls.data(),
2561 ConflictingDecls.size());
2562 if (!Name)
2563 return 0;
2564 }
2565 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002566
Douglas Gregorea35d112010-02-15 23:54:17 +00002567 // Import the type.
2568 QualType T = Importer.Import(D->getType());
2569 if (T.isNull())
2570 return 0;
2571
Douglas Gregor089459a2010-02-08 21:09:39 +00002572 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002573 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002574 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2575 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002576 D->getStorageClass(),
2577 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002578 // Import the qualifier, if any.
2579 if (D->getQualifier()) {
2580 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2581 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2582 ToVar->setQualifierInfo(NNS, NNSRange);
2583 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002584 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002585 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002586 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002587 LexicalDC->addDecl(ToVar);
2588
Douglas Gregor089459a2010-02-08 21:09:39 +00002589 // Merge the initializer.
2590 // FIXME: Can we really import any initializer? Alternatively, we could force
2591 // ourselves to import every declaration of a variable and then only use
2592 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002593 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002594
2595 // FIXME: Other bits to merge?
2596
2597 return ToVar;
2598}
2599
Douglas Gregor2cd00932010-02-17 21:22:52 +00002600Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2601 // Parameters are created in the translation unit's context, then moved
2602 // into the function declaration's context afterward.
2603 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2604
2605 // Import the name of this declaration.
2606 DeclarationName Name = Importer.Import(D->getDeclName());
2607 if (D->getDeclName() && !Name)
2608 return 0;
2609
2610 // Import the location of this declaration.
2611 SourceLocation Loc = Importer.Import(D->getLocation());
2612
2613 // Import the parameter's type.
2614 QualType T = Importer.Import(D->getType());
2615 if (T.isNull())
2616 return 0;
2617
2618 // Create the imported parameter.
2619 ImplicitParamDecl *ToParm
2620 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2621 Loc, Name.getAsIdentifierInfo(),
2622 T);
2623 return Importer.Imported(D, ToParm);
2624}
2625
Douglas Gregora404ea62010-02-10 19:54:31 +00002626Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2627 // Parameters are created in the translation unit's context, then moved
2628 // into the function declaration's context afterward.
2629 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2630
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002631 // Import the name of this declaration.
2632 DeclarationName Name = Importer.Import(D->getDeclName());
2633 if (D->getDeclName() && !Name)
2634 return 0;
2635
Douglas Gregora404ea62010-02-10 19:54:31 +00002636 // Import the location of this declaration.
2637 SourceLocation Loc = Importer.Import(D->getLocation());
2638
2639 // Import the parameter's type.
2640 QualType T = Importer.Import(D->getType());
2641 if (T.isNull())
2642 return 0;
2643
2644 // Create the imported parameter.
2645 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2646 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2647 Loc, Name.getAsIdentifierInfo(),
2648 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002649 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002650 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002651 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002652 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002653}
2654
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002655Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2656 // Import the major distinguishing characteristics of a method.
2657 DeclContext *DC, *LexicalDC;
2658 DeclarationName Name;
2659 SourceLocation Loc;
2660 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2661 return 0;
2662
2663 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2664 Lookup.first != Lookup.second;
2665 ++Lookup.first) {
2666 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2667 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2668 continue;
2669
2670 // Check return types.
2671 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2672 FoundMethod->getResultType())) {
2673 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2674 << D->isInstanceMethod() << Name
2675 << D->getResultType() << FoundMethod->getResultType();
2676 Importer.ToDiag(FoundMethod->getLocation(),
2677 diag::note_odr_objc_method_here)
2678 << D->isInstanceMethod() << Name;
2679 return 0;
2680 }
2681
2682 // Check the number of parameters.
2683 if (D->param_size() != FoundMethod->param_size()) {
2684 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2685 << D->isInstanceMethod() << Name
2686 << D->param_size() << FoundMethod->param_size();
2687 Importer.ToDiag(FoundMethod->getLocation(),
2688 diag::note_odr_objc_method_here)
2689 << D->isInstanceMethod() << Name;
2690 return 0;
2691 }
2692
2693 // Check parameter types.
2694 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2695 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2696 P != PEnd; ++P, ++FoundP) {
2697 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2698 (*FoundP)->getType())) {
2699 Importer.FromDiag((*P)->getLocation(),
2700 diag::err_odr_objc_method_param_type_inconsistent)
2701 << D->isInstanceMethod() << Name
2702 << (*P)->getType() << (*FoundP)->getType();
2703 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2704 << (*FoundP)->getType();
2705 return 0;
2706 }
2707 }
2708
2709 // Check variadic/non-variadic.
2710 // Check the number of parameters.
2711 if (D->isVariadic() != FoundMethod->isVariadic()) {
2712 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2713 << D->isInstanceMethod() << Name;
2714 Importer.ToDiag(FoundMethod->getLocation(),
2715 diag::note_odr_objc_method_here)
2716 << D->isInstanceMethod() << Name;
2717 return 0;
2718 }
2719
2720 // FIXME: Any other bits we need to merge?
2721 return Importer.Imported(D, FoundMethod);
2722 }
2723 }
2724
2725 // Import the result type.
2726 QualType ResultTy = Importer.Import(D->getResultType());
2727 if (ResultTy.isNull())
2728 return 0;
2729
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002730 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2731
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002732 ObjCMethodDecl *ToMethod
2733 = ObjCMethodDecl::Create(Importer.getToContext(),
2734 Loc,
2735 Importer.Import(D->getLocEnd()),
2736 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002737 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002738 D->isInstanceMethod(),
2739 D->isVariadic(),
2740 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002741 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002742 D->getImplementationControl());
2743
2744 // FIXME: When we decide to merge method definitions, we'll need to
2745 // deal with implicit parameters.
2746
2747 // Import the parameters
2748 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2749 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2750 FromPEnd = D->param_end();
2751 FromP != FromPEnd;
2752 ++FromP) {
2753 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2754 if (!ToP)
2755 return 0;
2756
2757 ToParams.push_back(ToP);
2758 }
2759
2760 // Set the parameters.
2761 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2762 ToParams[I]->setOwningFunction(ToMethod);
2763 ToMethod->addDecl(ToParams[I]);
2764 }
2765 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002766 ToParams.data(), ToParams.size(),
2767 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002768
2769 ToMethod->setLexicalDeclContext(LexicalDC);
2770 Importer.Imported(D, ToMethod);
2771 LexicalDC->addDecl(ToMethod);
2772 return ToMethod;
2773}
2774
Douglas Gregorb4677b62010-02-18 01:47:50 +00002775Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2776 // Import the major distinguishing characteristics of a category.
2777 DeclContext *DC, *LexicalDC;
2778 DeclarationName Name;
2779 SourceLocation Loc;
2780 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2781 return 0;
2782
2783 ObjCInterfaceDecl *ToInterface
2784 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2785 if (!ToInterface)
2786 return 0;
2787
2788 // Determine if we've already encountered this category.
2789 ObjCCategoryDecl *MergeWithCategory
2790 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2791 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2792 if (!ToCategory) {
2793 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2794 Importer.Import(D->getAtLoc()),
2795 Loc,
2796 Importer.Import(D->getCategoryNameLoc()),
2797 Name.getAsIdentifierInfo());
2798 ToCategory->setLexicalDeclContext(LexicalDC);
2799 LexicalDC->addDecl(ToCategory);
2800 Importer.Imported(D, ToCategory);
2801
2802 // Link this category into its class's category list.
2803 ToCategory->setClassInterface(ToInterface);
2804 ToCategory->insertNextClassCategory();
2805
2806 // Import protocols
2807 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2808 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2809 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2810 = D->protocol_loc_begin();
2811 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2812 FromProtoEnd = D->protocol_end();
2813 FromProto != FromProtoEnd;
2814 ++FromProto, ++FromProtoLoc) {
2815 ObjCProtocolDecl *ToProto
2816 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2817 if (!ToProto)
2818 return 0;
2819 Protocols.push_back(ToProto);
2820 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2821 }
2822
2823 // FIXME: If we're merging, make sure that the protocol list is the same.
2824 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2825 ProtocolLocs.data(), Importer.getToContext());
2826
2827 } else {
2828 Importer.Imported(D, ToCategory);
2829 }
2830
2831 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002832 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002833
2834 // If we have an implementation, import it as well.
2835 if (D->getImplementation()) {
2836 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002837 = cast_or_null<ObjCCategoryImplDecl>(
2838 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002839 if (!Impl)
2840 return 0;
2841
2842 ToCategory->setImplementation(Impl);
2843 }
2844
2845 return ToCategory;
2846}
2847
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002848Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002849 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002850 DeclContext *DC, *LexicalDC;
2851 DeclarationName Name;
2852 SourceLocation Loc;
2853 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2854 return 0;
2855
2856 ObjCProtocolDecl *MergeWithProtocol = 0;
2857 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2858 Lookup.first != Lookup.second;
2859 ++Lookup.first) {
2860 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2861 continue;
2862
2863 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2864 break;
2865 }
2866
2867 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2868 if (!ToProto || ToProto->isForwardDecl()) {
2869 if (!ToProto) {
2870 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2871 Name.getAsIdentifierInfo());
2872 ToProto->setForwardDecl(D->isForwardDecl());
2873 ToProto->setLexicalDeclContext(LexicalDC);
2874 LexicalDC->addDecl(ToProto);
2875 }
2876 Importer.Imported(D, ToProto);
2877
2878 // Import protocols
2879 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2880 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2881 ObjCProtocolDecl::protocol_loc_iterator
2882 FromProtoLoc = D->protocol_loc_begin();
2883 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2884 FromProtoEnd = D->protocol_end();
2885 FromProto != FromProtoEnd;
2886 ++FromProto, ++FromProtoLoc) {
2887 ObjCProtocolDecl *ToProto
2888 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2889 if (!ToProto)
2890 return 0;
2891 Protocols.push_back(ToProto);
2892 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2893 }
2894
2895 // FIXME: If we're merging, make sure that the protocol list is the same.
2896 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2897 ProtocolLocs.data(), Importer.getToContext());
2898 } else {
2899 Importer.Imported(D, ToProto);
2900 }
2901
Douglas Gregorb4677b62010-02-18 01:47:50 +00002902 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002903 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002904
2905 return ToProto;
2906}
2907
Douglas Gregora12d2942010-02-16 01:20:57 +00002908Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2909 // Import the major distinguishing characteristics of an @interface.
2910 DeclContext *DC, *LexicalDC;
2911 DeclarationName Name;
2912 SourceLocation Loc;
2913 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2914 return 0;
2915
2916 ObjCInterfaceDecl *MergeWithIface = 0;
2917 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2918 Lookup.first != Lookup.second;
2919 ++Lookup.first) {
2920 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2921 continue;
2922
2923 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2924 break;
2925 }
2926
2927 ObjCInterfaceDecl *ToIface = MergeWithIface;
2928 if (!ToIface || ToIface->isForwardDecl()) {
2929 if (!ToIface) {
2930 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2931 DC, Loc,
2932 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002933 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002934 D->isForwardDecl(),
2935 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002936 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002937 ToIface->setLexicalDeclContext(LexicalDC);
2938 LexicalDC->addDecl(ToIface);
2939 }
2940 Importer.Imported(D, ToIface);
2941
Douglas Gregora12d2942010-02-16 01:20:57 +00002942 if (D->getSuperClass()) {
2943 ObjCInterfaceDecl *Super
2944 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2945 if (!Super)
2946 return 0;
2947
2948 ToIface->setSuperClass(Super);
2949 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2950 }
2951
2952 // Import protocols
2953 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2954 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2955 ObjCInterfaceDecl::protocol_loc_iterator
2956 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002957
2958 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00002959 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2960 FromProtoEnd = D->protocol_end();
2961 FromProto != FromProtoEnd;
2962 ++FromProto, ++FromProtoLoc) {
2963 ObjCProtocolDecl *ToProto
2964 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2965 if (!ToProto)
2966 return 0;
2967 Protocols.push_back(ToProto);
2968 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2969 }
2970
2971 // FIXME: If we're merging, make sure that the protocol list is the same.
2972 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2973 ProtocolLocs.data(), Importer.getToContext());
2974
Douglas Gregora12d2942010-02-16 01:20:57 +00002975 // Import @end range
2976 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2977 } else {
2978 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002979
2980 // Check for consistency of superclasses.
2981 DeclarationName FromSuperName, ToSuperName;
2982 if (D->getSuperClass())
2983 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2984 if (ToIface->getSuperClass())
2985 ToSuperName = ToIface->getSuperClass()->getDeclName();
2986 if (FromSuperName != ToSuperName) {
2987 Importer.ToDiag(ToIface->getLocation(),
2988 diag::err_odr_objc_superclass_inconsistent)
2989 << ToIface->getDeclName();
2990 if (ToIface->getSuperClass())
2991 Importer.ToDiag(ToIface->getSuperClassLoc(),
2992 diag::note_odr_objc_superclass)
2993 << ToIface->getSuperClass()->getDeclName();
2994 else
2995 Importer.ToDiag(ToIface->getLocation(),
2996 diag::note_odr_objc_missing_superclass);
2997 if (D->getSuperClass())
2998 Importer.FromDiag(D->getSuperClassLoc(),
2999 diag::note_odr_objc_superclass)
3000 << D->getSuperClass()->getDeclName();
3001 else
3002 Importer.FromDiag(D->getLocation(),
3003 diag::note_odr_objc_missing_superclass);
3004 return 0;
3005 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003006 }
3007
Douglas Gregorb4677b62010-02-18 01:47:50 +00003008 // Import categories. When the categories themselves are imported, they'll
3009 // hook themselves into this interface.
3010 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3011 FromCat = FromCat->getNextClassCategory())
3012 Importer.Import(FromCat);
3013
Douglas Gregora12d2942010-02-16 01:20:57 +00003014 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003015 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003016
3017 // If we have an @implementation, import it as well.
3018 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003019 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3020 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003021 if (!Impl)
3022 return 0;
3023
3024 ToIface->setImplementation(Impl);
3025 }
3026
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003027 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003028}
3029
Douglas Gregor3daef292010-12-07 15:32:12 +00003030Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3031 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3032 Importer.Import(D->getCategoryDecl()));
3033 if (!Category)
3034 return 0;
3035
3036 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3037 if (!ToImpl) {
3038 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3039 if (!DC)
3040 return 0;
3041
3042 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3043 Importer.Import(D->getLocation()),
3044 Importer.Import(D->getIdentifier()),
3045 Category->getClassInterface());
3046
3047 DeclContext *LexicalDC = DC;
3048 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3049 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3050 if (!LexicalDC)
3051 return 0;
3052
3053 ToImpl->setLexicalDeclContext(LexicalDC);
3054 }
3055
3056 LexicalDC->addDecl(ToImpl);
3057 Category->setImplementation(ToImpl);
3058 }
3059
3060 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003061 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003062 return ToImpl;
3063}
3064
Douglas Gregordd182ff2010-12-07 01:26:03 +00003065Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3066 // Find the corresponding interface.
3067 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3068 Importer.Import(D->getClassInterface()));
3069 if (!Iface)
3070 return 0;
3071
3072 // Import the superclass, if any.
3073 ObjCInterfaceDecl *Super = 0;
3074 if (D->getSuperClass()) {
3075 Super = cast_or_null<ObjCInterfaceDecl>(
3076 Importer.Import(D->getSuperClass()));
3077 if (!Super)
3078 return 0;
3079 }
3080
3081 ObjCImplementationDecl *Impl = Iface->getImplementation();
3082 if (!Impl) {
3083 // We haven't imported an implementation yet. Create a new @implementation
3084 // now.
3085 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3086 Importer.ImportContext(D->getDeclContext()),
3087 Importer.Import(D->getLocation()),
3088 Iface, Super);
3089
3090 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3091 DeclContext *LexicalDC
3092 = Importer.ImportContext(D->getLexicalDeclContext());
3093 if (!LexicalDC)
3094 return 0;
3095 Impl->setLexicalDeclContext(LexicalDC);
3096 }
3097
3098 // Associate the implementation with the class it implements.
3099 Iface->setImplementation(Impl);
3100 Importer.Imported(D, Iface->getImplementation());
3101 } else {
3102 Importer.Imported(D, Iface->getImplementation());
3103
3104 // Verify that the existing @implementation has the same superclass.
3105 if ((Super && !Impl->getSuperClass()) ||
3106 (!Super && Impl->getSuperClass()) ||
3107 (Super && Impl->getSuperClass() &&
3108 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3109 Importer.ToDiag(Impl->getLocation(),
3110 diag::err_odr_objc_superclass_inconsistent)
3111 << Iface->getDeclName();
3112 // FIXME: It would be nice to have the location of the superclass
3113 // below.
3114 if (Impl->getSuperClass())
3115 Importer.ToDiag(Impl->getLocation(),
3116 diag::note_odr_objc_superclass)
3117 << Impl->getSuperClass()->getDeclName();
3118 else
3119 Importer.ToDiag(Impl->getLocation(),
3120 diag::note_odr_objc_missing_superclass);
3121 if (D->getSuperClass())
3122 Importer.FromDiag(D->getLocation(),
3123 diag::note_odr_objc_superclass)
3124 << D->getSuperClass()->getDeclName();
3125 else
3126 Importer.FromDiag(D->getLocation(),
3127 diag::note_odr_objc_missing_superclass);
3128 return 0;
3129 }
3130 }
3131
3132 // Import all of the members of this @implementation.
3133 ImportDeclContext(D);
3134
3135 return Impl;
3136}
3137
Douglas Gregore3261622010-02-17 18:02:10 +00003138Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3139 // Import the major distinguishing characteristics of an @property.
3140 DeclContext *DC, *LexicalDC;
3141 DeclarationName Name;
3142 SourceLocation Loc;
3143 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3144 return 0;
3145
3146 // Check whether we have already imported this property.
3147 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3148 Lookup.first != Lookup.second;
3149 ++Lookup.first) {
3150 if (ObjCPropertyDecl *FoundProp
3151 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3152 // Check property types.
3153 if (!Importer.IsStructurallyEquivalent(D->getType(),
3154 FoundProp->getType())) {
3155 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3156 << Name << D->getType() << FoundProp->getType();
3157 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3158 << FoundProp->getType();
3159 return 0;
3160 }
3161
3162 // FIXME: Check property attributes, getters, setters, etc.?
3163
3164 // Consider these properties to be equivalent.
3165 Importer.Imported(D, FoundProp);
3166 return FoundProp;
3167 }
3168 }
3169
3170 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003171 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3172 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003173 return 0;
3174
3175 // Create the new property.
3176 ObjCPropertyDecl *ToProperty
3177 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3178 Name.getAsIdentifierInfo(),
3179 Importer.Import(D->getAtLoc()),
3180 T,
3181 D->getPropertyImplementation());
3182 Importer.Imported(D, ToProperty);
3183 ToProperty->setLexicalDeclContext(LexicalDC);
3184 LexicalDC->addDecl(ToProperty);
3185
3186 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003187 ToProperty->setPropertyAttributesAsWritten(
3188 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003189 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3190 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3191 ToProperty->setGetterMethodDecl(
3192 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3193 ToProperty->setSetterMethodDecl(
3194 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3195 ToProperty->setPropertyIvarDecl(
3196 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3197 return ToProperty;
3198}
3199
Douglas Gregor954e0c72010-12-07 18:32:03 +00003200Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3201 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3202 Importer.Import(D->getPropertyDecl()));
3203 if (!Property)
3204 return 0;
3205
3206 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3207 if (!DC)
3208 return 0;
3209
3210 // Import the lexical declaration context.
3211 DeclContext *LexicalDC = DC;
3212 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3213 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3214 if (!LexicalDC)
3215 return 0;
3216 }
3217
3218 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3219 if (!InImpl)
3220 return 0;
3221
3222 // Import the ivar (for an @synthesize).
3223 ObjCIvarDecl *Ivar = 0;
3224 if (D->getPropertyIvarDecl()) {
3225 Ivar = cast_or_null<ObjCIvarDecl>(
3226 Importer.Import(D->getPropertyIvarDecl()));
3227 if (!Ivar)
3228 return 0;
3229 }
3230
3231 ObjCPropertyImplDecl *ToImpl
3232 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3233 if (!ToImpl) {
3234 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3235 Importer.Import(D->getLocStart()),
3236 Importer.Import(D->getLocation()),
3237 Property,
3238 D->getPropertyImplementation(),
3239 Ivar,
3240 Importer.Import(D->getPropertyIvarDeclLoc()));
3241 ToImpl->setLexicalDeclContext(LexicalDC);
3242 Importer.Imported(D, ToImpl);
3243 LexicalDC->addDecl(ToImpl);
3244 } else {
3245 // Check that we have the same kind of property implementation (@synthesize
3246 // vs. @dynamic).
3247 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3248 Importer.ToDiag(ToImpl->getLocation(),
3249 diag::err_odr_objc_property_impl_kind_inconsistent)
3250 << Property->getDeclName()
3251 << (ToImpl->getPropertyImplementation()
3252 == ObjCPropertyImplDecl::Dynamic);
3253 Importer.FromDiag(D->getLocation(),
3254 diag::note_odr_objc_property_impl_kind)
3255 << D->getPropertyDecl()->getDeclName()
3256 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3257 return 0;
3258 }
3259
3260 // For @synthesize, check that we have the same
3261 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3262 Ivar != ToImpl->getPropertyIvarDecl()) {
3263 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3264 diag::err_odr_objc_synthesize_ivar_inconsistent)
3265 << Property->getDeclName()
3266 << ToImpl->getPropertyIvarDecl()->getDeclName()
3267 << Ivar->getDeclName();
3268 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3269 diag::note_odr_objc_synthesize_ivar_here)
3270 << D->getPropertyIvarDecl()->getDeclName();
3271 return 0;
3272 }
3273
3274 // Merge the existing implementation with the new implementation.
3275 Importer.Imported(D, ToImpl);
3276 }
3277
3278 return ToImpl;
3279}
3280
Douglas Gregor2b785022010-02-18 02:12:22 +00003281Decl *
3282ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3283 // Import the context of this declaration.
3284 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3285 if (!DC)
3286 return 0;
3287
3288 DeclContext *LexicalDC = DC;
3289 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3290 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3291 if (!LexicalDC)
3292 return 0;
3293 }
3294
3295 // Import the location of this declaration.
3296 SourceLocation Loc = Importer.Import(D->getLocation());
3297
3298 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3299 llvm::SmallVector<SourceLocation, 4> Locations;
3300 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3301 = D->protocol_loc_begin();
3302 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3303 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3304 FromProto != FromProtoEnd;
3305 ++FromProto, ++FromProtoLoc) {
3306 ObjCProtocolDecl *ToProto
3307 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3308 if (!ToProto)
3309 continue;
3310
3311 Protocols.push_back(ToProto);
3312 Locations.push_back(Importer.Import(*FromProtoLoc));
3313 }
3314
3315 ObjCForwardProtocolDecl *ToForward
3316 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3317 Protocols.data(), Protocols.size(),
3318 Locations.data());
3319 ToForward->setLexicalDeclContext(LexicalDC);
3320 LexicalDC->addDecl(ToForward);
3321 Importer.Imported(D, ToForward);
3322 return ToForward;
3323}
3324
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003325Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3326 // Import the context of this declaration.
3327 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3328 if (!DC)
3329 return 0;
3330
3331 DeclContext *LexicalDC = DC;
3332 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3333 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3334 if (!LexicalDC)
3335 return 0;
3336 }
3337
3338 // Import the location of this declaration.
3339 SourceLocation Loc = Importer.Import(D->getLocation());
3340
3341 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3342 llvm::SmallVector<SourceLocation, 4> Locations;
3343 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3344 From != FromEnd; ++From) {
3345 ObjCInterfaceDecl *ToIface
3346 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3347 if (!ToIface)
3348 continue;
3349
3350 Interfaces.push_back(ToIface);
3351 Locations.push_back(Importer.Import(From->getLocation()));
3352 }
3353
3354 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3355 Loc,
3356 Interfaces.data(),
3357 Locations.data(),
3358 Interfaces.size());
3359 ToClass->setLexicalDeclContext(LexicalDC);
3360 LexicalDC->addDecl(ToClass);
3361 Importer.Imported(D, ToClass);
3362 return ToClass;
3363}
3364
Douglas Gregor040afae2010-11-30 19:14:50 +00003365Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3366 // For template arguments, we adopt the translation unit as our declaration
3367 // context. This context will be fixed when the actual template declaration
3368 // is created.
3369
3370 // FIXME: Import default argument.
3371 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3372 Importer.getToContext().getTranslationUnitDecl(),
3373 Importer.Import(D->getLocation()),
3374 D->getDepth(),
3375 D->getIndex(),
3376 Importer.Import(D->getIdentifier()),
3377 D->wasDeclaredWithTypename(),
3378 D->isParameterPack());
3379}
3380
3381Decl *
3382ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3383 // Import the name of this declaration.
3384 DeclarationName Name = Importer.Import(D->getDeclName());
3385 if (D->getDeclName() && !Name)
3386 return 0;
3387
3388 // Import the location of this declaration.
3389 SourceLocation Loc = Importer.Import(D->getLocation());
3390
3391 // Import the type of this declaration.
3392 QualType T = Importer.Import(D->getType());
3393 if (T.isNull())
3394 return 0;
3395
3396 // Import type-source information.
3397 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3398 if (D->getTypeSourceInfo() && !TInfo)
3399 return 0;
3400
3401 // FIXME: Import default argument.
3402
3403 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3404 Importer.getToContext().getTranslationUnitDecl(),
3405 Loc, D->getDepth(), D->getPosition(),
3406 Name.getAsIdentifierInfo(),
3407 T, TInfo);
3408}
3409
3410Decl *
3411ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3412 // Import the name of this declaration.
3413 DeclarationName Name = Importer.Import(D->getDeclName());
3414 if (D->getDeclName() && !Name)
3415 return 0;
3416
3417 // Import the location of this declaration.
3418 SourceLocation Loc = Importer.Import(D->getLocation());
3419
3420 // Import template parameters.
3421 TemplateParameterList *TemplateParams
3422 = ImportTemplateParameterList(D->getTemplateParameters());
3423 if (!TemplateParams)
3424 return 0;
3425
3426 // FIXME: Import default argument.
3427
3428 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3429 Importer.getToContext().getTranslationUnitDecl(),
3430 Loc, D->getDepth(), D->getPosition(),
3431 Name.getAsIdentifierInfo(),
3432 TemplateParams);
3433}
3434
3435Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3436 // If this record has a definition in the translation unit we're coming from,
3437 // but this particular declaration is not that definition, import the
3438 // definition and map to that.
3439 CXXRecordDecl *Definition
3440 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3441 if (Definition && Definition != D->getTemplatedDecl()) {
3442 Decl *ImportedDef
3443 = Importer.Import(Definition->getDescribedClassTemplate());
3444 if (!ImportedDef)
3445 return 0;
3446
3447 return Importer.Imported(D, ImportedDef);
3448 }
3449
3450 // Import the major distinguishing characteristics of this class template.
3451 DeclContext *DC, *LexicalDC;
3452 DeclarationName Name;
3453 SourceLocation Loc;
3454 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3455 return 0;
3456
3457 // We may already have a template of the same name; try to find and match it.
3458 if (!DC->isFunctionOrMethod()) {
3459 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3460 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3461 Lookup.first != Lookup.second;
3462 ++Lookup.first) {
3463 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3464 continue;
3465
3466 Decl *Found = *Lookup.first;
3467 if (ClassTemplateDecl *FoundTemplate
3468 = dyn_cast<ClassTemplateDecl>(Found)) {
3469 if (IsStructuralMatch(D, FoundTemplate)) {
3470 // The class templates structurally match; call it the same template.
3471 // FIXME: We may be filling in a forward declaration here. Handle
3472 // this case!
3473 Importer.Imported(D->getTemplatedDecl(),
3474 FoundTemplate->getTemplatedDecl());
3475 return Importer.Imported(D, FoundTemplate);
3476 }
3477 }
3478
3479 ConflictingDecls.push_back(*Lookup.first);
3480 }
3481
3482 if (!ConflictingDecls.empty()) {
3483 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3484 ConflictingDecls.data(),
3485 ConflictingDecls.size());
3486 }
3487
3488 if (!Name)
3489 return 0;
3490 }
3491
3492 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3493
3494 // Create the declaration that is being templated.
3495 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3496 DTemplated->getTagKind(),
3497 DC,
3498 Importer.Import(DTemplated->getLocation()),
3499 Name.getAsIdentifierInfo(),
3500 Importer.Import(DTemplated->getTagKeywordLoc()));
3501 D2Templated->setAccess(DTemplated->getAccess());
3502
3503
3504 // Import the qualifier, if any.
3505 if (DTemplated->getQualifier()) {
3506 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3507 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3508 D2Templated->setQualifierInfo(NNS, NNSRange);
3509 }
3510 D2Templated->setLexicalDeclContext(LexicalDC);
3511
3512 // Create the class template declaration itself.
3513 TemplateParameterList *TemplateParams
3514 = ImportTemplateParameterList(D->getTemplateParameters());
3515 if (!TemplateParams)
3516 return 0;
3517
3518 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3519 Loc, Name, TemplateParams,
3520 D2Templated,
3521 /*PrevDecl=*/0);
3522 D2Templated->setDescribedClassTemplate(D2);
3523
3524 D2->setAccess(D->getAccess());
3525 D2->setLexicalDeclContext(LexicalDC);
3526 LexicalDC->addDecl(D2);
3527
3528 // Note the relationship between the class templates.
3529 Importer.Imported(D, D2);
3530 Importer.Imported(DTemplated, D2Templated);
3531
3532 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3533 // FIXME: Import definition!
3534 }
3535
3536 return D2;
3537}
3538
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003539Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3540 ClassTemplateSpecializationDecl *D) {
3541 // If this record has a definition in the translation unit we're coming from,
3542 // but this particular declaration is not that definition, import the
3543 // definition and map to that.
3544 TagDecl *Definition = D->getDefinition();
3545 if (Definition && Definition != D) {
3546 Decl *ImportedDef = Importer.Import(Definition);
3547 if (!ImportedDef)
3548 return 0;
3549
3550 return Importer.Imported(D, ImportedDef);
3551 }
3552
3553 ClassTemplateDecl *ClassTemplate
3554 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3555 D->getSpecializedTemplate()));
3556 if (!ClassTemplate)
3557 return 0;
3558
3559 // Import the context of this declaration.
3560 DeclContext *DC = ClassTemplate->getDeclContext();
3561 if (!DC)
3562 return 0;
3563
3564 DeclContext *LexicalDC = DC;
3565 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3566 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3567 if (!LexicalDC)
3568 return 0;
3569 }
3570
3571 // Import the location of this declaration.
3572 SourceLocation Loc = Importer.Import(D->getLocation());
3573
3574 // Import template arguments.
3575 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3576 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3577 D->getTemplateArgs().size(),
3578 TemplateArgs))
3579 return 0;
3580
3581 // Try to find an existing specialization with these template arguments.
3582 void *InsertPos = 0;
3583 ClassTemplateSpecializationDecl *D2
3584 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3585 TemplateArgs.size(), InsertPos);
3586 if (D2) {
3587 // We already have a class template specialization with these template
3588 // arguments.
3589
3590 // FIXME: Check for specialization vs. instantiation errors.
3591
3592 if (RecordDecl *FoundDef = D2->getDefinition()) {
3593 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3594 // The record types structurally match, or the "from" translation
3595 // unit only had a forward declaration anyway; call it the same
3596 // function.
3597 return Importer.Imported(D, FoundDef);
3598 }
3599 }
3600 } else {
3601 // Create a new specialization.
3602 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3603 D->getTagKind(), DC,
3604 Loc, ClassTemplate,
3605 TemplateArgs.data(),
3606 TemplateArgs.size(),
3607 /*PrevDecl=*/0);
3608 D2->setSpecializationKind(D->getSpecializationKind());
3609
3610 // Add this specialization to the class template.
3611 ClassTemplate->AddSpecialization(D2, InsertPos);
3612
3613 // Import the qualifier, if any.
3614 if (D->getQualifier()) {
3615 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3616 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3617 D2->setQualifierInfo(NNS, NNSRange);
3618 }
3619
3620
3621 // Add the specialization to this context.
3622 D2->setLexicalDeclContext(LexicalDC);
3623 LexicalDC->addDecl(D2);
3624 }
3625 Importer.Imported(D, D2);
3626
3627 if (D->isDefinition() && ImportDefinition(D, D2))
3628 return 0;
3629
3630 return D2;
3631}
3632
Douglas Gregor4800d952010-02-11 19:21:55 +00003633//----------------------------------------------------------------------------
3634// Import Statements
3635//----------------------------------------------------------------------------
3636
3637Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3638 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3639 << S->getStmtClassName();
3640 return 0;
3641}
3642
3643//----------------------------------------------------------------------------
3644// Import Expressions
3645//----------------------------------------------------------------------------
3646Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3647 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3648 << E->getStmtClassName();
3649 return 0;
3650}
3651
Douglas Gregor44080632010-02-19 01:17:02 +00003652Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3653 NestedNameSpecifier *Qualifier = 0;
3654 if (E->getQualifier()) {
3655 Qualifier = Importer.Import(E->getQualifier());
3656 if (!E->getQualifier())
3657 return 0;
3658 }
3659
3660 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3661 if (!ToD)
3662 return 0;
3663
3664 QualType T = Importer.Import(E->getType());
3665 if (T.isNull())
3666 return 0;
3667
3668 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3669 Importer.Import(E->getQualifierRange()),
3670 ToD,
3671 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003672 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003673 /*FIXME:TemplateArgs=*/0);
3674}
3675
Douglas Gregor4800d952010-02-11 19:21:55 +00003676Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3677 QualType T = Importer.Import(E->getType());
3678 if (T.isNull())
3679 return 0;
3680
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003681 return IntegerLiteral::Create(Importer.getToContext(),
3682 E->getValue(), T,
3683 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003684}
3685
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003686Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3687 QualType T = Importer.Import(E->getType());
3688 if (T.isNull())
3689 return 0;
3690
3691 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3692 E->isWide(), T,
3693 Importer.Import(E->getLocation()));
3694}
3695
Douglas Gregorf638f952010-02-19 01:07:06 +00003696Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3697 Expr *SubExpr = Importer.Import(E->getSubExpr());
3698 if (!SubExpr)
3699 return 0;
3700
3701 return new (Importer.getToContext())
3702 ParenExpr(Importer.Import(E->getLParen()),
3703 Importer.Import(E->getRParen()),
3704 SubExpr);
3705}
3706
3707Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3708 QualType T = Importer.Import(E->getType());
3709 if (T.isNull())
3710 return 0;
3711
3712 Expr *SubExpr = Importer.Import(E->getSubExpr());
3713 if (!SubExpr)
3714 return 0;
3715
3716 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003717 T, E->getValueKind(),
3718 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003719 Importer.Import(E->getOperatorLoc()));
3720}
3721
Douglas Gregorbd249a52010-02-19 01:24:23 +00003722Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3723 QualType ResultType = Importer.Import(E->getType());
3724
3725 if (E->isArgumentType()) {
3726 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3727 if (!TInfo)
3728 return 0;
3729
3730 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3731 TInfo, ResultType,
3732 Importer.Import(E->getOperatorLoc()),
3733 Importer.Import(E->getRParenLoc()));
3734 }
3735
3736 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3737 if (!SubExpr)
3738 return 0;
3739
3740 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3741 SubExpr, ResultType,
3742 Importer.Import(E->getOperatorLoc()),
3743 Importer.Import(E->getRParenLoc()));
3744}
3745
Douglas Gregorf638f952010-02-19 01:07:06 +00003746Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3747 QualType T = Importer.Import(E->getType());
3748 if (T.isNull())
3749 return 0;
3750
3751 Expr *LHS = Importer.Import(E->getLHS());
3752 if (!LHS)
3753 return 0;
3754
3755 Expr *RHS = Importer.Import(E->getRHS());
3756 if (!RHS)
3757 return 0;
3758
3759 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003760 T, E->getValueKind(),
3761 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003762 Importer.Import(E->getOperatorLoc()));
3763}
3764
3765Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3766 QualType T = Importer.Import(E->getType());
3767 if (T.isNull())
3768 return 0;
3769
3770 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3771 if (CompLHSType.isNull())
3772 return 0;
3773
3774 QualType CompResultType = Importer.Import(E->getComputationResultType());
3775 if (CompResultType.isNull())
3776 return 0;
3777
3778 Expr *LHS = Importer.Import(E->getLHS());
3779 if (!LHS)
3780 return 0;
3781
3782 Expr *RHS = Importer.Import(E->getRHS());
3783 if (!RHS)
3784 return 0;
3785
3786 return new (Importer.getToContext())
3787 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003788 T, E->getValueKind(),
3789 E->getObjectKind(),
3790 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003791 Importer.Import(E->getOperatorLoc()));
3792}
3793
John McCallf871d0c2010-08-07 06:22:56 +00003794bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3795 if (E->path_empty()) return false;
3796
3797 // TODO: import cast paths
3798 return true;
3799}
3800
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003801Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3802 QualType T = Importer.Import(E->getType());
3803 if (T.isNull())
3804 return 0;
3805
3806 Expr *SubExpr = Importer.Import(E->getSubExpr());
3807 if (!SubExpr)
3808 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003809
3810 CXXCastPath BasePath;
3811 if (ImportCastPath(E, BasePath))
3812 return 0;
3813
3814 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003815 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003816}
3817
Douglas Gregor008847a2010-02-19 01:32:14 +00003818Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3819 QualType T = Importer.Import(E->getType());
3820 if (T.isNull())
3821 return 0;
3822
3823 Expr *SubExpr = Importer.Import(E->getSubExpr());
3824 if (!SubExpr)
3825 return 0;
3826
3827 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3828 if (!TInfo && E->getTypeInfoAsWritten())
3829 return 0;
3830
John McCallf871d0c2010-08-07 06:22:56 +00003831 CXXCastPath BasePath;
3832 if (ImportCastPath(E, BasePath))
3833 return 0;
3834
John McCallf89e55a2010-11-18 06:31:45 +00003835 return CStyleCastExpr::Create(Importer.getToContext(), T,
3836 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003837 SubExpr, &BasePath, TInfo,
3838 Importer.Import(E->getLParenLoc()),
3839 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003840}
3841
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003842ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003843 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003844 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003845 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003846 ImportedDecls[FromContext.getTranslationUnitDecl()]
3847 = ToContext.getTranslationUnitDecl();
3848}
3849
3850ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003851
3852QualType ASTImporter::Import(QualType FromT) {
3853 if (FromT.isNull())
3854 return QualType();
3855
Douglas Gregor169fba52010-02-08 15:18:58 +00003856 // Check whether we've already imported this type.
3857 llvm::DenseMap<Type *, Type *>::iterator Pos
3858 = ImportedTypes.find(FromT.getTypePtr());
3859 if (Pos != ImportedTypes.end())
3860 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003861
Douglas Gregor169fba52010-02-08 15:18:58 +00003862 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003863 ASTNodeImporter Importer(*this);
3864 QualType ToT = Importer.Visit(FromT.getTypePtr());
3865 if (ToT.isNull())
3866 return ToT;
3867
Douglas Gregor169fba52010-02-08 15:18:58 +00003868 // Record the imported type.
3869 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3870
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003871 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3872}
3873
Douglas Gregor9bed8792010-02-09 19:21:46 +00003874TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003875 if (!FromTSI)
3876 return FromTSI;
3877
3878 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003879 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003880 QualType T = Import(FromTSI->getType());
3881 if (T.isNull())
3882 return 0;
3883
3884 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003885 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003886}
3887
3888Decl *ASTImporter::Import(Decl *FromD) {
3889 if (!FromD)
3890 return 0;
3891
3892 // Check whether we've already imported this declaration.
3893 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3894 if (Pos != ImportedDecls.end())
3895 return Pos->second;
3896
3897 // Import the type
3898 ASTNodeImporter Importer(*this);
3899 Decl *ToD = Importer.Visit(FromD);
3900 if (!ToD)
3901 return 0;
3902
3903 // Record the imported declaration.
3904 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003905
3906 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3907 // Keep track of anonymous tags that have an associated typedef.
3908 if (FromTag->getTypedefForAnonDecl())
3909 AnonTagsWithPendingTypedefs.push_back(FromTag);
3910 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3911 // When we've finished transforming a typedef, see whether it was the
3912 // typedef for an anonymous tag.
3913 for (llvm::SmallVector<TagDecl *, 4>::iterator
3914 FromTag = AnonTagsWithPendingTypedefs.begin(),
3915 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3916 FromTag != FromTagEnd; ++FromTag) {
3917 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3918 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3919 // We found the typedef for an anonymous tag; link them.
3920 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3921 AnonTagsWithPendingTypedefs.erase(FromTag);
3922 break;
3923 }
3924 }
3925 }
3926 }
3927
Douglas Gregor9bed8792010-02-09 19:21:46 +00003928 return ToD;
3929}
3930
3931DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3932 if (!FromDC)
3933 return FromDC;
3934
3935 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3936}
3937
3938Expr *ASTImporter::Import(Expr *FromE) {
3939 if (!FromE)
3940 return 0;
3941
3942 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3943}
3944
3945Stmt *ASTImporter::Import(Stmt *FromS) {
3946 if (!FromS)
3947 return 0;
3948
Douglas Gregor4800d952010-02-11 19:21:55 +00003949 // Check whether we've already imported this declaration.
3950 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3951 if (Pos != ImportedStmts.end())
3952 return Pos->second;
3953
3954 // Import the type
3955 ASTNodeImporter Importer(*this);
3956 Stmt *ToS = Importer.Visit(FromS);
3957 if (!ToS)
3958 return 0;
3959
3960 // Record the imported declaration.
3961 ImportedStmts[FromS] = ToS;
3962 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003963}
3964
3965NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3966 if (!FromNNS)
3967 return 0;
3968
3969 // FIXME: Implement!
3970 return 0;
3971}
3972
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003973TemplateName ASTImporter::Import(TemplateName From) {
3974 switch (From.getKind()) {
3975 case TemplateName::Template:
3976 if (TemplateDecl *ToTemplate
3977 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3978 return TemplateName(ToTemplate);
3979
3980 return TemplateName();
3981
3982 case TemplateName::OverloadedTemplate: {
3983 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3984 UnresolvedSet<2> ToTemplates;
3985 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3986 E = FromStorage->end();
3987 I != E; ++I) {
3988 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3989 ToTemplates.addDecl(To);
3990 else
3991 return TemplateName();
3992 }
3993 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3994 ToTemplates.end());
3995 }
3996
3997 case TemplateName::QualifiedTemplate: {
3998 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3999 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4000 if (!Qualifier)
4001 return TemplateName();
4002
4003 if (TemplateDecl *ToTemplate
4004 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4005 return ToContext.getQualifiedTemplateName(Qualifier,
4006 QTN->hasTemplateKeyword(),
4007 ToTemplate);
4008
4009 return TemplateName();
4010 }
4011
4012 case TemplateName::DependentTemplate: {
4013 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4014 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4015 if (!Qualifier)
4016 return TemplateName();
4017
4018 if (DTN->isIdentifier()) {
4019 return ToContext.getDependentTemplateName(Qualifier,
4020 Import(DTN->getIdentifier()));
4021 }
4022
4023 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4024 }
4025 }
4026
4027 llvm_unreachable("Invalid template name kind");
4028 return TemplateName();
4029}
4030
Douglas Gregor9bed8792010-02-09 19:21:46 +00004031SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4032 if (FromLoc.isInvalid())
4033 return SourceLocation();
4034
Douglas Gregor88523732010-02-10 00:15:17 +00004035 SourceManager &FromSM = FromContext.getSourceManager();
4036
4037 // For now, map everything down to its spelling location, so that we
4038 // don't have to import macro instantiations.
4039 // FIXME: Import macro instantiations!
4040 FromLoc = FromSM.getSpellingLoc(FromLoc);
4041 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4042 SourceManager &ToSM = ToContext.getSourceManager();
4043 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4044 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004045}
4046
4047SourceRange ASTImporter::Import(SourceRange FromRange) {
4048 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4049}
4050
Douglas Gregor88523732010-02-10 00:15:17 +00004051FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004052 llvm::DenseMap<FileID, FileID>::iterator Pos
4053 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004054 if (Pos != ImportedFileIDs.end())
4055 return Pos->second;
4056
4057 SourceManager &FromSM = FromContext.getSourceManager();
4058 SourceManager &ToSM = ToContext.getSourceManager();
4059 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4060 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4061
4062 // Include location of this file.
4063 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4064
4065 // Map the FileID for to the "to" source manager.
4066 FileID ToID;
4067 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4068 if (Cache->Entry) {
4069 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4070 // disk again
4071 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4072 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004073 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004074 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4075 FromSLoc.getFile().getFileCharacteristic());
4076 } else {
4077 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004078 const llvm::MemoryBuffer *
4079 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004080 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004081 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004082 FromBuf->getBufferIdentifier());
4083 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4084 }
4085
4086
Sebastian Redl535a3e22010-09-30 01:03:06 +00004087 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004088 return ToID;
4089}
4090
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004091DeclarationName ASTImporter::Import(DeclarationName FromName) {
4092 if (!FromName)
4093 return DeclarationName();
4094
4095 switch (FromName.getNameKind()) {
4096 case DeclarationName::Identifier:
4097 return Import(FromName.getAsIdentifierInfo());
4098
4099 case DeclarationName::ObjCZeroArgSelector:
4100 case DeclarationName::ObjCOneArgSelector:
4101 case DeclarationName::ObjCMultiArgSelector:
4102 return Import(FromName.getObjCSelector());
4103
4104 case DeclarationName::CXXConstructorName: {
4105 QualType T = Import(FromName.getCXXNameType());
4106 if (T.isNull())
4107 return DeclarationName();
4108
4109 return ToContext.DeclarationNames.getCXXConstructorName(
4110 ToContext.getCanonicalType(T));
4111 }
4112
4113 case DeclarationName::CXXDestructorName: {
4114 QualType T = Import(FromName.getCXXNameType());
4115 if (T.isNull())
4116 return DeclarationName();
4117
4118 return ToContext.DeclarationNames.getCXXDestructorName(
4119 ToContext.getCanonicalType(T));
4120 }
4121
4122 case DeclarationName::CXXConversionFunctionName: {
4123 QualType T = Import(FromName.getCXXNameType());
4124 if (T.isNull())
4125 return DeclarationName();
4126
4127 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4128 ToContext.getCanonicalType(T));
4129 }
4130
4131 case DeclarationName::CXXOperatorName:
4132 return ToContext.DeclarationNames.getCXXOperatorName(
4133 FromName.getCXXOverloadedOperator());
4134
4135 case DeclarationName::CXXLiteralOperatorName:
4136 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4137 Import(FromName.getCXXLiteralIdentifier()));
4138
4139 case DeclarationName::CXXUsingDirective:
4140 // FIXME: STATICS!
4141 return DeclarationName::getUsingDirectiveName();
4142 }
4143
4144 // Silence bogus GCC warning
4145 return DeclarationName();
4146}
4147
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004148IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004149 if (!FromId)
4150 return 0;
4151
4152 return &ToContext.Idents.get(FromId->getName());
4153}
Douglas Gregor089459a2010-02-08 21:09:39 +00004154
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004155Selector ASTImporter::Import(Selector FromSel) {
4156 if (FromSel.isNull())
4157 return Selector();
4158
4159 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4160 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4161 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4162 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4163 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4164}
4165
Douglas Gregor089459a2010-02-08 21:09:39 +00004166DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4167 DeclContext *DC,
4168 unsigned IDNS,
4169 NamedDecl **Decls,
4170 unsigned NumDecls) {
4171 return Name;
4172}
4173
4174DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004175 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004176}
4177
4178DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004179 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004180}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004181
4182Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4183 ImportedDecls[From] = To;
4184 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004185}
Douglas Gregorea35d112010-02-15 23:54:17 +00004186
4187bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4188 llvm::DenseMap<Type *, Type *>::iterator Pos
4189 = ImportedTypes.find(From.getTypePtr());
4190 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4191 return true;
4192
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004193 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004194 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004195}