blob: f0c61135e1eacc84c8e90555d77331bf7c21fa3f [file] [log] [blame]
Douglas Gregor96e578d2010-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 Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregorfa7a0e52010-02-10 17:47:19 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000023#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
26#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000027#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000028
29using namespace clang;
30
31namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000032 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000033 public DeclVisitor<ASTNodeImporter, Decl *>,
34 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000035 ASTImporter &Importer;
36
37 public:
38 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
39
40 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000041 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000042 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000043
44 // Importing types
Douglas Gregore4c83e42010-02-09 22:48:33 +000045 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000046 QualType VisitBuiltinType(BuiltinType *T);
47 QualType VisitComplexType(ComplexType *T);
48 QualType VisitPointerType(PointerType *T);
49 QualType VisitBlockPointerType(BlockPointerType *T);
50 QualType VisitLValueReferenceType(LValueReferenceType *T);
51 QualType VisitRValueReferenceType(RValueReferenceType *T);
52 QualType VisitMemberPointerType(MemberPointerType *T);
53 QualType VisitConstantArrayType(ConstantArrayType *T);
54 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
55 QualType VisitVariableArrayType(VariableArrayType *T);
56 // FIXME: DependentSizedArrayType
57 // FIXME: DependentSizedExtVectorType
58 QualType VisitVectorType(VectorType *T);
59 QualType VisitExtVectorType(ExtVectorType *T);
60 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
61 QualType VisitFunctionProtoType(FunctionProtoType *T);
62 // FIXME: UnresolvedUsingType
63 QualType VisitTypedefType(TypedefType *T);
64 QualType VisitTypeOfExprType(TypeOfExprType *T);
65 // FIXME: DependentTypeOfExprType
66 QualType VisitTypeOfType(TypeOfType *T);
67 QualType VisitDecltypeType(DecltypeType *T);
68 // FIXME: DependentDecltypeType
69 QualType VisitRecordType(RecordType *T);
70 QualType VisitEnumType(EnumType *T);
71 QualType VisitElaboratedType(ElaboratedType *T);
72 // FIXME: TemplateTypeParmType
73 // FIXME: SubstTemplateTypeParmType
74 // FIXME: TemplateSpecializationType
75 QualType VisitQualifiedNameType(QualifiedNameType *T);
76 // FIXME: TypenameType
77 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
78 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Douglas Gregor968d6332010-02-21 18:24:45 +000084 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregor5c73e912010-02-11 00:48:18 +000085 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000086 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregore4c83e42010-02-09 22:48:33 +000087 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000088 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000089 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000090 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000091 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000092 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000093 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +000094 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
95 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
96 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
97 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000098 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +000099 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000100 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000101 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000102 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000103 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000104 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000105 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000106 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000107 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000108 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000109 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
110
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000111 // Importing statements
112 Stmt *VisitStmt(Stmt *S);
113
114 // Importing expressions
115 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000116 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000117 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000118 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000119 Expr *VisitParenExpr(ParenExpr *E);
120 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000121 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000122 Expr *VisitBinaryOperator(BinaryOperator *E);
123 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000124 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000125 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000126 };
127}
128
129//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000130// Structural Equivalence
131//----------------------------------------------------------------------------
132
133namespace {
134 struct StructuralEquivalenceContext {
135 /// \brief AST contexts for which we are checking structural equivalence.
136 ASTContext &C1, &C2;
137
138 /// \brief Diagnostic object used to emit diagnostics.
139 Diagnostic &Diags;
140
141 /// \brief The set of "tentative" equivalences between two canonical
142 /// declarations, mapping from a declaration in the first context to the
143 /// declaration in the second context that we believe to be equivalent.
144 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
145
146 /// \brief Queue of declarations in the first context whose equivalence
147 /// with a declaration in the second context still needs to be verified.
148 std::deque<Decl *> DeclsToCheck;
149
Douglas Gregorb4964f72010-02-15 23:54:17 +0000150 /// \brief Declaration (from, to) pairs that are known not to be equivalent
151 /// (which we have already complained about).
152 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
153
Douglas Gregor3996e242010-02-15 22:01:00 +0000154 /// \brief Whether we're being strict about the spelling of types when
155 /// unifying two types.
156 bool StrictTypeSpelling;
157
158 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
159 Diagnostic &Diags,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000160 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000161 bool StrictTypeSpelling = false)
Douglas Gregorb4964f72010-02-15 23:54:17 +0000162 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
163 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000164
165 /// \brief Determine whether the two declarations are structurally
166 /// equivalent.
167 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
168
169 /// \brief Determine whether the two types are structurally equivalent.
170 bool IsStructurallyEquivalent(QualType T1, QualType T2);
171
172 private:
173 /// \brief Finish checking all of the structural equivalences.
174 ///
175 /// \returns true if an error occurred, false otherwise.
176 bool Finish();
177
178 public:
179 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
180 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
181 }
182
183 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
184 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
185 }
186 };
187}
188
189static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
190 QualType T1, QualType T2);
191static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
192 Decl *D1, Decl *D2);
193
194/// \brief Determine if two APInts have the same value, after zero-extending
195/// one of them (if needed!) to ensure that the bit-widths match.
196static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
197 if (I1.getBitWidth() == I2.getBitWidth())
198 return I1 == I2;
199
200 if (I1.getBitWidth() > I2.getBitWidth())
201 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
202
203 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
204}
205
206/// \brief Determine if two APSInts have the same value, zero- or sign-extending
207/// as needed.
208static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
209 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
210 return I1 == I2;
211
212 // Check for a bit-width mismatch.
213 if (I1.getBitWidth() > I2.getBitWidth())
214 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
215 else if (I2.getBitWidth() > I1.getBitWidth())
216 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
217
218 // We have a signedness mismatch. Turn the signed value into an unsigned
219 // value.
220 if (I1.isSigned()) {
221 if (I1.isNegative())
222 return false;
223
224 return llvm::APSInt(I1, true) == I2;
225 }
226
227 if (I2.isNegative())
228 return false;
229
230 return I1 == llvm::APSInt(I2, true);
231}
232
233/// \brief Determine structural equivalence of two expressions.
234static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
235 Expr *E1, Expr *E2) {
236 if (!E1 || !E2)
237 return E1 == E2;
238
239 // FIXME: Actually perform a structural comparison!
240 return true;
241}
242
243/// \brief Determine whether two identifiers are equivalent.
244static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
245 const IdentifierInfo *Name2) {
246 if (!Name1 || !Name2)
247 return Name1 == Name2;
248
249 return Name1->getName() == Name2->getName();
250}
251
252/// \brief Determine whether two nested-name-specifiers are equivalent.
253static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
254 NestedNameSpecifier *NNS1,
255 NestedNameSpecifier *NNS2) {
256 // FIXME: Implement!
257 return true;
258}
259
260/// \brief Determine whether two template arguments are equivalent.
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 const TemplateArgument &Arg1,
263 const TemplateArgument &Arg2) {
264 // FIXME: Implement!
265 return true;
266}
267
268/// \brief Determine structural equivalence for the common part of array
269/// types.
270static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
271 const ArrayType *Array1,
272 const ArrayType *Array2) {
273 if (!IsStructurallyEquivalent(Context,
274 Array1->getElementType(),
275 Array2->getElementType()))
276 return false;
277 if (Array1->getSizeModifier() != Array2->getSizeModifier())
278 return false;
279 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
280 return false;
281
282 return true;
283}
284
285/// \brief Determine structural equivalence of two types.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 QualType T1, QualType T2) {
288 if (T1.isNull() || T2.isNull())
289 return T1.isNull() && T2.isNull();
290
291 if (!Context.StrictTypeSpelling) {
292 // We aren't being strict about token-to-token equivalence of types,
293 // so map down to the canonical type.
294 T1 = Context.C1.getCanonicalType(T1);
295 T2 = Context.C2.getCanonicalType(T2);
296 }
297
298 if (T1.getQualifiers() != T2.getQualifiers())
299 return false;
300
Douglas Gregorb4964f72010-02-15 23:54:17 +0000301 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000302
Douglas Gregorb4964f72010-02-15 23:54:17 +0000303 if (T1->getTypeClass() != T2->getTypeClass()) {
304 // Compare function types with prototypes vs. without prototypes as if
305 // both did not have prototypes.
306 if (T1->getTypeClass() == Type::FunctionProto &&
307 T2->getTypeClass() == Type::FunctionNoProto)
308 TC = Type::FunctionNoProto;
309 else if (T1->getTypeClass() == Type::FunctionNoProto &&
310 T2->getTypeClass() == Type::FunctionProto)
311 TC = Type::FunctionNoProto;
312 else
313 return false;
314 }
315
316 switch (TC) {
317 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000318 // FIXME: Deal with Char_S/Char_U.
319 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
320 return false;
321 break;
322
323 case Type::Complex:
324 if (!IsStructurallyEquivalent(Context,
325 cast<ComplexType>(T1)->getElementType(),
326 cast<ComplexType>(T2)->getElementType()))
327 return false;
328 break;
329
330 case Type::Pointer:
331 if (!IsStructurallyEquivalent(Context,
332 cast<PointerType>(T1)->getPointeeType(),
333 cast<PointerType>(T2)->getPointeeType()))
334 return false;
335 break;
336
337 case Type::BlockPointer:
338 if (!IsStructurallyEquivalent(Context,
339 cast<BlockPointerType>(T1)->getPointeeType(),
340 cast<BlockPointerType>(T2)->getPointeeType()))
341 return false;
342 break;
343
344 case Type::LValueReference:
345 case Type::RValueReference: {
346 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
347 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
348 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
349 return false;
350 if (Ref1->isInnerRef() != Ref2->isInnerRef())
351 return false;
352 if (!IsStructurallyEquivalent(Context,
353 Ref1->getPointeeTypeAsWritten(),
354 Ref2->getPointeeTypeAsWritten()))
355 return false;
356 break;
357 }
358
359 case Type::MemberPointer: {
360 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
361 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
362 if (!IsStructurallyEquivalent(Context,
363 MemPtr1->getPointeeType(),
364 MemPtr2->getPointeeType()))
365 return false;
366 if (!IsStructurallyEquivalent(Context,
367 QualType(MemPtr1->getClass(), 0),
368 QualType(MemPtr2->getClass(), 0)))
369 return false;
370 break;
371 }
372
373 case Type::ConstantArray: {
374 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
375 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
376 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
377 return false;
378
379 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
380 return false;
381 break;
382 }
383
384 case Type::IncompleteArray:
385 if (!IsArrayStructurallyEquivalent(Context,
386 cast<ArrayType>(T1),
387 cast<ArrayType>(T2)))
388 return false;
389 break;
390
391 case Type::VariableArray: {
392 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
393 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
394 if (!IsStructurallyEquivalent(Context,
395 Array1->getSizeExpr(), Array2->getSizeExpr()))
396 return false;
397
398 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
399 return false;
400
401 break;
402 }
403
404 case Type::DependentSizedArray: {
405 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
406 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
407 if (!IsStructurallyEquivalent(Context,
408 Array1->getSizeExpr(), Array2->getSizeExpr()))
409 return false;
410
411 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
412 return false;
413
414 break;
415 }
416
417 case Type::DependentSizedExtVector: {
418 const DependentSizedExtVectorType *Vec1
419 = cast<DependentSizedExtVectorType>(T1);
420 const DependentSizedExtVectorType *Vec2
421 = cast<DependentSizedExtVectorType>(T2);
422 if (!IsStructurallyEquivalent(Context,
423 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
424 return false;
425 if (!IsStructurallyEquivalent(Context,
426 Vec1->getElementType(),
427 Vec2->getElementType()))
428 return false;
429 break;
430 }
431
432 case Type::Vector:
433 case Type::ExtVector: {
434 const VectorType *Vec1 = cast<VectorType>(T1);
435 const VectorType *Vec2 = cast<VectorType>(T2);
436 if (!IsStructurallyEquivalent(Context,
437 Vec1->getElementType(),
438 Vec2->getElementType()))
439 return false;
440 if (Vec1->getNumElements() != Vec2->getNumElements())
441 return false;
442 if (Vec1->isAltiVec() != Vec2->isAltiVec())
443 return false;
444 if (Vec1->isPixel() != Vec2->isPixel())
445 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000446 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000447 }
448
449 case Type::FunctionProto: {
450 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
451 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
452 if (Proto1->getNumArgs() != Proto2->getNumArgs())
453 return false;
454 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
455 if (!IsStructurallyEquivalent(Context,
456 Proto1->getArgType(I),
457 Proto2->getArgType(I)))
458 return false;
459 }
460 if (Proto1->isVariadic() != Proto2->isVariadic())
461 return false;
462 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
463 return false;
464 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
465 return false;
466 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
467 return false;
468 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
469 if (!IsStructurallyEquivalent(Context,
470 Proto1->getExceptionType(I),
471 Proto2->getExceptionType(I)))
472 return false;
473 }
474 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
475 return false;
476
477 // Fall through to check the bits common with FunctionNoProtoType.
478 }
479
480 case Type::FunctionNoProto: {
481 const FunctionType *Function1 = cast<FunctionType>(T1);
482 const FunctionType *Function2 = cast<FunctionType>(T2);
483 if (!IsStructurallyEquivalent(Context,
484 Function1->getResultType(),
485 Function2->getResultType()))
486 return false;
487 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
488 return false;
489 if (Function1->getCallConv() != Function2->getCallConv())
490 return false;
491 break;
492 }
493
494 case Type::UnresolvedUsing:
495 if (!IsStructurallyEquivalent(Context,
496 cast<UnresolvedUsingType>(T1)->getDecl(),
497 cast<UnresolvedUsingType>(T2)->getDecl()))
498 return false;
499
500 break;
501
502 case Type::Typedef:
503 if (!IsStructurallyEquivalent(Context,
504 cast<TypedefType>(T1)->getDecl(),
505 cast<TypedefType>(T2)->getDecl()))
506 return false;
507 break;
508
509 case Type::TypeOfExpr:
510 if (!IsStructurallyEquivalent(Context,
511 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
512 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
513 return false;
514 break;
515
516 case Type::TypeOf:
517 if (!IsStructurallyEquivalent(Context,
518 cast<TypeOfType>(T1)->getUnderlyingType(),
519 cast<TypeOfType>(T2)->getUnderlyingType()))
520 return false;
521 break;
522
523 case Type::Decltype:
524 if (!IsStructurallyEquivalent(Context,
525 cast<DecltypeType>(T1)->getUnderlyingExpr(),
526 cast<DecltypeType>(T2)->getUnderlyingExpr()))
527 return false;
528 break;
529
530 case Type::Record:
531 case Type::Enum:
532 if (!IsStructurallyEquivalent(Context,
533 cast<TagType>(T1)->getDecl(),
534 cast<TagType>(T2)->getDecl()))
535 return false;
536 break;
537
538 case Type::Elaborated: {
539 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
540 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
541 if (Elab1->getTagKind() != Elab2->getTagKind())
542 return false;
543 if (!IsStructurallyEquivalent(Context,
544 Elab1->getUnderlyingType(),
545 Elab2->getUnderlyingType()))
546 return false;
547 break;
548 }
549
550 case Type::TemplateTypeParm: {
551 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
552 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
553 if (Parm1->getDepth() != Parm2->getDepth())
554 return false;
555 if (Parm1->getIndex() != Parm2->getIndex())
556 return false;
557 if (Parm1->isParameterPack() != Parm2->isParameterPack())
558 return false;
559
560 // Names of template type parameters are never significant.
561 break;
562 }
563
564 case Type::SubstTemplateTypeParm: {
565 const SubstTemplateTypeParmType *Subst1
566 = cast<SubstTemplateTypeParmType>(T1);
567 const SubstTemplateTypeParmType *Subst2
568 = cast<SubstTemplateTypeParmType>(T2);
569 if (!IsStructurallyEquivalent(Context,
570 QualType(Subst1->getReplacedParameter(), 0),
571 QualType(Subst2->getReplacedParameter(), 0)))
572 return false;
573 if (!IsStructurallyEquivalent(Context,
574 Subst1->getReplacementType(),
575 Subst2->getReplacementType()))
576 return false;
577 break;
578 }
579
580 case Type::TemplateSpecialization: {
581 const TemplateSpecializationType *Spec1
582 = cast<TemplateSpecializationType>(T1);
583 const TemplateSpecializationType *Spec2
584 = cast<TemplateSpecializationType>(T2);
585 if (!IsStructurallyEquivalent(Context,
586 Spec1->getTemplateName(),
587 Spec2->getTemplateName()))
588 return false;
589 if (Spec1->getNumArgs() != Spec2->getNumArgs())
590 return false;
591 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
592 if (!IsStructurallyEquivalent(Context,
593 Spec1->getArg(I), Spec2->getArg(I)))
594 return false;
595 }
596 break;
597 }
598
599 case Type::QualifiedName: {
600 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
601 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
602 if (!IsStructurallyEquivalent(Context,
603 Qual1->getQualifier(),
604 Qual2->getQualifier()))
605 return false;
606 if (!IsStructurallyEquivalent(Context,
607 Qual1->getNamedType(),
608 Qual2->getNamedType()))
609 return false;
610 break;
611 }
612
613 case Type::Typename: {
614 const TypenameType *Typename1 = cast<TypenameType>(T1);
615 const TypenameType *Typename2 = cast<TypenameType>(T2);
616 if (!IsStructurallyEquivalent(Context,
617 Typename1->getQualifier(),
618 Typename2->getQualifier()))
619 return false;
620 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
621 Typename2->getIdentifier()))
622 return false;
623 if (!IsStructurallyEquivalent(Context,
624 QualType(Typename1->getTemplateId(), 0),
625 QualType(Typename2->getTemplateId(), 0)))
626 return false;
627
628 break;
629 }
630
631 case Type::ObjCInterface: {
632 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
633 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
634 if (!IsStructurallyEquivalent(Context,
635 Iface1->getDecl(), Iface2->getDecl()))
636 return false;
637 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
638 return false;
639 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
640 if (!IsStructurallyEquivalent(Context,
641 Iface1->getProtocol(I),
642 Iface2->getProtocol(I)))
643 return false;
644 }
645 break;
646 }
647
648 case Type::ObjCObjectPointer: {
649 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
650 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
651 if (!IsStructurallyEquivalent(Context,
652 Ptr1->getPointeeType(),
653 Ptr2->getPointeeType()))
654 return false;
655 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
656 return false;
657 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
658 if (!IsStructurallyEquivalent(Context,
659 Ptr1->getProtocol(I),
660 Ptr2->getProtocol(I)))
661 return false;
662 }
663 break;
664 }
665
666 } // end switch
667
668 return true;
669}
670
671/// \brief Determine structural equivalence of two records.
672static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
673 RecordDecl *D1, RecordDecl *D2) {
674 if (D1->isUnion() != D2->isUnion()) {
675 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
676 << Context.C2.getTypeDeclType(D2);
677 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
678 << D1->getDeclName() << (unsigned)D1->getTagKind();
679 return false;
680 }
681
Douglas Gregorb4964f72010-02-15 23:54:17 +0000682 // Compare the definitions of these two records. If either or both are
683 // incomplete, we assume that they are equivalent.
684 D1 = D1->getDefinition();
685 D2 = D2->getDefinition();
686 if (!D1 || !D2)
687 return true;
688
Douglas Gregor3996e242010-02-15 22:01:00 +0000689 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
690 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
691 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
692 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
693 << Context.C2.getTypeDeclType(D2);
694 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
695 << D2CXX->getNumBases();
696 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
697 << D1CXX->getNumBases();
698 return false;
699 }
700
701 // Check the base classes.
702 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
703 BaseEnd1 = D1CXX->bases_end(),
704 Base2 = D2CXX->bases_begin();
705 Base1 != BaseEnd1;
706 ++Base1, ++Base2) {
707 if (!IsStructurallyEquivalent(Context,
708 Base1->getType(), Base2->getType())) {
709 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
710 << Context.C2.getTypeDeclType(D2);
711 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
712 << Base2->getType()
713 << Base2->getSourceRange();
714 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
715 << Base1->getType()
716 << Base1->getSourceRange();
717 return false;
718 }
719
720 // Check virtual vs. non-virtual inheritance mismatch.
721 if (Base1->isVirtual() != Base2->isVirtual()) {
722 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
723 << Context.C2.getTypeDeclType(D2);
724 Context.Diag2(Base2->getSourceRange().getBegin(),
725 diag::note_odr_virtual_base)
726 << Base2->isVirtual() << Base2->getSourceRange();
727 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
728 << Base1->isVirtual()
729 << Base1->getSourceRange();
730 return false;
731 }
732 }
733 } else if (D1CXX->getNumBases() > 0) {
734 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
735 << Context.C2.getTypeDeclType(D2);
736 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
737 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
738 << Base1->getType()
739 << Base1->getSourceRange();
740 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
741 return false;
742 }
743 }
744
745 // Check the fields for consistency.
746 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
747 Field2End = D2->field_end();
748 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
749 Field1End = D1->field_end();
750 Field1 != Field1End;
751 ++Field1, ++Field2) {
752 if (Field2 == Field2End) {
753 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
754 << Context.C2.getTypeDeclType(D2);
755 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
756 << Field1->getDeclName() << Field1->getType();
757 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
758 return false;
759 }
760
761 if (!IsStructurallyEquivalent(Context,
762 Field1->getType(), Field2->getType())) {
763 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
764 << Context.C2.getTypeDeclType(D2);
765 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
766 << Field2->getDeclName() << Field2->getType();
767 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
768 << Field1->getDeclName() << Field1->getType();
769 return false;
770 }
771
772 if (Field1->isBitField() != Field2->isBitField()) {
773 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
774 << Context.C2.getTypeDeclType(D2);
775 if (Field1->isBitField()) {
776 llvm::APSInt Bits;
777 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
778 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
779 << Field1->getDeclName() << Field1->getType()
780 << Bits.toString(10, false);
781 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
782 << Field2->getDeclName();
783 } else {
784 llvm::APSInt Bits;
785 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
786 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
787 << Field2->getDeclName() << Field2->getType()
788 << Bits.toString(10, false);
789 Context.Diag1(Field1->getLocation(),
790 diag::note_odr_not_bit_field)
791 << Field1->getDeclName();
792 }
793 return false;
794 }
795
796 if (Field1->isBitField()) {
797 // Make sure that the bit-fields are the same length.
798 llvm::APSInt Bits1, Bits2;
799 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
800 return false;
801 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
802 return false;
803
804 if (!IsSameValue(Bits1, Bits2)) {
805 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
806 << Context.C2.getTypeDeclType(D2);
807 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
808 << Field2->getDeclName() << Field2->getType()
809 << Bits2.toString(10, false);
810 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
811 << Field1->getDeclName() << Field1->getType()
812 << Bits1.toString(10, false);
813 return false;
814 }
815 }
816 }
817
818 if (Field2 != Field2End) {
819 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
820 << Context.C2.getTypeDeclType(D2);
821 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
822 << Field2->getDeclName() << Field2->getType();
823 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
824 return false;
825 }
826
827 return true;
828}
829
830/// \brief Determine structural equivalence of two enums.
831static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
832 EnumDecl *D1, EnumDecl *D2) {
833 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
834 EC2End = D2->enumerator_end();
835 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
836 EC1End = D1->enumerator_end();
837 EC1 != EC1End; ++EC1, ++EC2) {
838 if (EC2 == EC2End) {
839 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
840 << Context.C2.getTypeDeclType(D2);
841 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
842 << EC1->getDeclName()
843 << EC1->getInitVal().toString(10);
844 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
845 return false;
846 }
847
848 llvm::APSInt Val1 = EC1->getInitVal();
849 llvm::APSInt Val2 = EC2->getInitVal();
850 if (!IsSameValue(Val1, Val2) ||
851 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
852 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
853 << Context.C2.getTypeDeclType(D2);
854 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
855 << EC2->getDeclName()
856 << EC2->getInitVal().toString(10);
857 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
858 << EC1->getDeclName()
859 << EC1->getInitVal().toString(10);
860 return false;
861 }
862 }
863
864 if (EC2 != EC2End) {
865 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(D2);
867 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
868 << EC2->getDeclName()
869 << EC2->getInitVal().toString(10);
870 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
871 return false;
872 }
873
874 return true;
875}
876
877/// \brief Determine structural equivalence of two declarations.
878static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
879 Decl *D1, Decl *D2) {
880 // FIXME: Check for known structural equivalences via a callback of some sort.
881
Douglas Gregorb4964f72010-02-15 23:54:17 +0000882 // Check whether we already know that these two declarations are not
883 // structurally equivalent.
884 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
885 D2->getCanonicalDecl())))
886 return false;
887
Douglas Gregor3996e242010-02-15 22:01:00 +0000888 // Determine whether we've already produced a tentative equivalence for D1.
889 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
890 if (EquivToD1)
891 return EquivToD1 == D2->getCanonicalDecl();
892
893 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
894 EquivToD1 = D2->getCanonicalDecl();
895 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
896 return true;
897}
898
899bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
900 Decl *D2) {
901 if (!::IsStructurallyEquivalent(*this, D1, D2))
902 return false;
903
904 return !Finish();
905}
906
907bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
908 QualType T2) {
909 if (!::IsStructurallyEquivalent(*this, T1, T2))
910 return false;
911
912 return !Finish();
913}
914
915bool StructuralEquivalenceContext::Finish() {
916 while (!DeclsToCheck.empty()) {
917 // Check the next declaration.
918 Decl *D1 = DeclsToCheck.front();
919 DeclsToCheck.pop_front();
920
921 Decl *D2 = TentativeEquivalences[D1];
922 assert(D2 && "Unrecorded tentative equivalence?");
923
Douglas Gregorb4964f72010-02-15 23:54:17 +0000924 bool Equivalent = true;
925
Douglas Gregor3996e242010-02-15 22:01:00 +0000926 // FIXME: Switch on all declaration kinds. For now, we're just going to
927 // check the obvious ones.
928 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
929 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
930 // Check for equivalent structure names.
931 IdentifierInfo *Name1 = Record1->getIdentifier();
932 if (!Name1 && Record1->getTypedefForAnonDecl())
933 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
934 IdentifierInfo *Name2 = Record2->getIdentifier();
935 if (!Name2 && Record2->getTypedefForAnonDecl())
936 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000937 if (!::IsStructurallyEquivalent(Name1, Name2) ||
938 !::IsStructurallyEquivalent(*this, Record1, Record2))
939 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000940 } else {
941 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000942 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000943 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000944 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000945 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
946 // Check for equivalent enum names.
947 IdentifierInfo *Name1 = Enum1->getIdentifier();
948 if (!Name1 && Enum1->getTypedefForAnonDecl())
949 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
950 IdentifierInfo *Name2 = Enum2->getIdentifier();
951 if (!Name2 && Enum2->getTypedefForAnonDecl())
952 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000953 if (!::IsStructurallyEquivalent(Name1, Name2) ||
954 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
955 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000956 } else {
957 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +0000958 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000959 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000960 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000961 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
962 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000963 Typedef2->getIdentifier()) ||
964 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +0000965 Typedef1->getUnderlyingType(),
966 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +0000967 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000968 } else {
969 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000970 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000971 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000972 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000973
974 if (!Equivalent) {
975 // Note that these two declarations are not equivalent (and we already
976 // know about it).
977 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
978 D2->getCanonicalDecl()));
979 return true;
980 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000981 // FIXME: Check other declaration kinds!
982 }
983
984 return false;
985}
986
987//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000988// Import Types
989//----------------------------------------------------------------------------
990
Douglas Gregore4c83e42010-02-09 22:48:33 +0000991QualType ASTNodeImporter::VisitType(Type *T) {
992 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
993 << T->getTypeClassName();
994 return QualType();
995}
996
Douglas Gregor96e578d2010-02-05 17:54:41 +0000997QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
998 switch (T->getKind()) {
999 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1000 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1001
1002 case BuiltinType::Char_U:
1003 // The context we're importing from has an unsigned 'char'. If we're
1004 // importing into a context with a signed 'char', translate to
1005 // 'unsigned char' instead.
1006 if (Importer.getToContext().getLangOptions().CharIsSigned)
1007 return Importer.getToContext().UnsignedCharTy;
1008
1009 return Importer.getToContext().CharTy;
1010
1011 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1012
1013 case BuiltinType::Char16:
1014 // FIXME: Make sure that the "to" context supports C++!
1015 return Importer.getToContext().Char16Ty;
1016
1017 case BuiltinType::Char32:
1018 // FIXME: Make sure that the "to" context supports C++!
1019 return Importer.getToContext().Char32Ty;
1020
1021 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1022 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1023 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1024 case BuiltinType::ULongLong:
1025 return Importer.getToContext().UnsignedLongLongTy;
1026 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1027
1028 case BuiltinType::Char_S:
1029 // The context we're importing from has an unsigned 'char'. If we're
1030 // importing into a context with a signed 'char', translate to
1031 // 'unsigned char' instead.
1032 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1033 return Importer.getToContext().SignedCharTy;
1034
1035 return Importer.getToContext().CharTy;
1036
1037 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1038 case BuiltinType::WChar:
1039 // FIXME: If not in C++, shall we translate to the C equivalent of
1040 // wchar_t?
1041 return Importer.getToContext().WCharTy;
1042
1043 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1044 case BuiltinType::Int : return Importer.getToContext().IntTy;
1045 case BuiltinType::Long : return Importer.getToContext().LongTy;
1046 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1047 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1048 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1049 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1050 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1051
1052 case BuiltinType::NullPtr:
1053 // FIXME: Make sure that the "to" context supports C++0x!
1054 return Importer.getToContext().NullPtrTy;
1055
1056 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1057 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1058 case BuiltinType::UndeducedAuto:
1059 // FIXME: Make sure that the "to" context supports C++0x!
1060 return Importer.getToContext().UndeducedAutoTy;
1061
1062 case BuiltinType::ObjCId:
1063 // FIXME: Make sure that the "to" context supports Objective-C!
1064 return Importer.getToContext().ObjCBuiltinIdTy;
1065
1066 case BuiltinType::ObjCClass:
1067 return Importer.getToContext().ObjCBuiltinClassTy;
1068
1069 case BuiltinType::ObjCSel:
1070 return Importer.getToContext().ObjCBuiltinSelTy;
1071 }
1072
1073 return QualType();
1074}
1075
1076QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1077 QualType ToElementType = Importer.Import(T->getElementType());
1078 if (ToElementType.isNull())
1079 return QualType();
1080
1081 return Importer.getToContext().getComplexType(ToElementType);
1082}
1083
1084QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1085 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1086 if (ToPointeeType.isNull())
1087 return QualType();
1088
1089 return Importer.getToContext().getPointerType(ToPointeeType);
1090}
1091
1092QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1093 // FIXME: Check for blocks support in "to" context.
1094 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1095 if (ToPointeeType.isNull())
1096 return QualType();
1097
1098 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1099}
1100
1101QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1102 // FIXME: Check for C++ support in "to" context.
1103 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1104 if (ToPointeeType.isNull())
1105 return QualType();
1106
1107 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1108}
1109
1110QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1111 // FIXME: Check for C++0x support in "to" context.
1112 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1113 if (ToPointeeType.isNull())
1114 return QualType();
1115
1116 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1117}
1118
1119QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1120 // FIXME: Check for C++ support in "to" context.
1121 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1122 if (ToPointeeType.isNull())
1123 return QualType();
1124
1125 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1126 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1127 ClassType.getTypePtr());
1128}
1129
1130QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1131 QualType ToElementType = Importer.Import(T->getElementType());
1132 if (ToElementType.isNull())
1133 return QualType();
1134
1135 return Importer.getToContext().getConstantArrayType(ToElementType,
1136 T->getSize(),
1137 T->getSizeModifier(),
1138 T->getIndexTypeCVRQualifiers());
1139}
1140
1141QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1142 QualType ToElementType = Importer.Import(T->getElementType());
1143 if (ToElementType.isNull())
1144 return QualType();
1145
1146 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1147 T->getSizeModifier(),
1148 T->getIndexTypeCVRQualifiers());
1149}
1150
1151QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1152 QualType ToElementType = Importer.Import(T->getElementType());
1153 if (ToElementType.isNull())
1154 return QualType();
1155
1156 Expr *Size = Importer.Import(T->getSizeExpr());
1157 if (!Size)
1158 return QualType();
1159
1160 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1161 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1162 T->getSizeModifier(),
1163 T->getIndexTypeCVRQualifiers(),
1164 Brackets);
1165}
1166
1167QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1168 QualType ToElementType = Importer.Import(T->getElementType());
1169 if (ToElementType.isNull())
1170 return QualType();
1171
1172 return Importer.getToContext().getVectorType(ToElementType,
1173 T->getNumElements(),
1174 T->isAltiVec(),
1175 T->isPixel());
1176}
1177
1178QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1179 QualType ToElementType = Importer.Import(T->getElementType());
1180 if (ToElementType.isNull())
1181 return QualType();
1182
1183 return Importer.getToContext().getExtVectorType(ToElementType,
1184 T->getNumElements());
1185}
1186
1187QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1188 // FIXME: What happens if we're importing a function without a prototype
1189 // into C++? Should we make it variadic?
1190 QualType ToResultType = Importer.Import(T->getResultType());
1191 if (ToResultType.isNull())
1192 return QualType();
1193
1194 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1195 T->getNoReturnAttr(),
1196 T->getCallConv());
1197}
1198
1199QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1200 QualType ToResultType = Importer.Import(T->getResultType());
1201 if (ToResultType.isNull())
1202 return QualType();
1203
1204 // Import argument types
1205 llvm::SmallVector<QualType, 4> ArgTypes;
1206 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1207 AEnd = T->arg_type_end();
1208 A != AEnd; ++A) {
1209 QualType ArgType = Importer.Import(*A);
1210 if (ArgType.isNull())
1211 return QualType();
1212 ArgTypes.push_back(ArgType);
1213 }
1214
1215 // Import exception types
1216 llvm::SmallVector<QualType, 4> ExceptionTypes;
1217 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1218 EEnd = T->exception_end();
1219 E != EEnd; ++E) {
1220 QualType ExceptionType = Importer.Import(*E);
1221 if (ExceptionType.isNull())
1222 return QualType();
1223 ExceptionTypes.push_back(ExceptionType);
1224 }
1225
1226 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1227 ArgTypes.size(),
1228 T->isVariadic(),
1229 T->getTypeQuals(),
1230 T->hasExceptionSpec(),
1231 T->hasAnyExceptionSpec(),
1232 ExceptionTypes.size(),
1233 ExceptionTypes.data(),
1234 T->getNoReturnAttr(),
1235 T->getCallConv());
1236}
1237
1238QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1239 TypedefDecl *ToDecl
1240 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1241 if (!ToDecl)
1242 return QualType();
1243
1244 return Importer.getToContext().getTypeDeclType(ToDecl);
1245}
1246
1247QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1248 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1249 if (!ToExpr)
1250 return QualType();
1251
1252 return Importer.getToContext().getTypeOfExprType(ToExpr);
1253}
1254
1255QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1256 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1257 if (ToUnderlyingType.isNull())
1258 return QualType();
1259
1260 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1261}
1262
1263QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1264 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1265 if (!ToExpr)
1266 return QualType();
1267
1268 return Importer.getToContext().getDecltypeType(ToExpr);
1269}
1270
1271QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1272 RecordDecl *ToDecl
1273 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1274 if (!ToDecl)
1275 return QualType();
1276
1277 return Importer.getToContext().getTagDeclType(ToDecl);
1278}
1279
1280QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1281 EnumDecl *ToDecl
1282 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1283 if (!ToDecl)
1284 return QualType();
1285
1286 return Importer.getToContext().getTagDeclType(ToDecl);
1287}
1288
1289QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1290 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1291 if (ToUnderlyingType.isNull())
1292 return QualType();
1293
1294 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1295 T->getTagKind());
1296}
1297
1298QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1299 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1300 if (!ToQualifier)
1301 return QualType();
1302
1303 QualType ToNamedType = Importer.Import(T->getNamedType());
1304 if (ToNamedType.isNull())
1305 return QualType();
1306
1307 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1308}
1309
1310QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1311 ObjCInterfaceDecl *Class
1312 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1313 if (!Class)
1314 return QualType();
1315
1316 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1317 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1318 PEnd = T->qual_end();
1319 P != PEnd; ++P) {
1320 ObjCProtocolDecl *Protocol
1321 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1322 if (!Protocol)
1323 return QualType();
1324 Protocols.push_back(Protocol);
1325 }
1326
1327 return Importer.getToContext().getObjCInterfaceType(Class,
1328 Protocols.data(),
1329 Protocols.size());
1330}
1331
1332QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1333 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1334 if (ToPointeeType.isNull())
1335 return QualType();
1336
1337 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1338 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1339 PEnd = T->qual_end();
1340 P != PEnd; ++P) {
1341 ObjCProtocolDecl *Protocol
1342 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1343 if (!Protocol)
1344 return QualType();
1345 Protocols.push_back(Protocol);
1346 }
1347
1348 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1349 Protocols.data(),
1350 Protocols.size());
1351}
1352
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001353//----------------------------------------------------------------------------
1354// Import Declarations
1355//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001356bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1357 DeclContext *&LexicalDC,
1358 DeclarationName &Name,
1359 SourceLocation &Loc) {
1360 // Import the context of this declaration.
1361 DC = Importer.ImportContext(D->getDeclContext());
1362 if (!DC)
1363 return true;
1364
1365 LexicalDC = DC;
1366 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1367 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1368 if (!LexicalDC)
1369 return true;
1370 }
1371
1372 // Import the name of this declaration.
1373 Name = Importer.Import(D->getDeclName());
1374 if (D->getDeclName() && !Name)
1375 return true;
1376
1377 // Import the location of this declaration.
1378 Loc = Importer.Import(D->getLocation());
1379 return false;
1380}
1381
Douglas Gregor968d6332010-02-21 18:24:45 +00001382void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1383 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1384 FromEnd = FromDC->decls_end();
1385 From != FromEnd;
1386 ++From)
1387 Importer.Import(*From);
1388}
1389
Douglas Gregor5c73e912010-02-11 00:48:18 +00001390bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001391 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001392 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001393 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001394 Importer.getDiags(),
1395 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001396 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001397}
1398
Douglas Gregor98c10182010-02-12 22:17:39 +00001399bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001400 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001401 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001402 Importer.getDiags(),
1403 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001404 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001405}
1406
Douglas Gregore4c83e42010-02-09 22:48:33 +00001407Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001408 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001409 << D->getDeclKindName();
1410 return 0;
1411}
1412
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001413Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1414 // Import the major distinguishing characteristics of this namespace.
1415 DeclContext *DC, *LexicalDC;
1416 DeclarationName Name;
1417 SourceLocation Loc;
1418 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1419 return 0;
1420
1421 NamespaceDecl *MergeWithNamespace = 0;
1422 if (!Name) {
1423 // This is an anonymous namespace. Adopt an existing anonymous
1424 // namespace if we can.
1425 // FIXME: Not testable.
1426 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1427 MergeWithNamespace = TU->getAnonymousNamespace();
1428 else
1429 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1430 } else {
1431 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1432 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1433 Lookup.first != Lookup.second;
1434 ++Lookup.first) {
1435 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1436 continue;
1437
1438 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1439 MergeWithNamespace = FoundNS;
1440 ConflictingDecls.clear();
1441 break;
1442 }
1443
1444 ConflictingDecls.push_back(*Lookup.first);
1445 }
1446
1447 if (!ConflictingDecls.empty()) {
1448 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
1449 ConflictingDecls.data(),
1450 ConflictingDecls.size());
1451 }
1452 }
1453
1454 // Create the "to" namespace, if needed.
1455 NamespaceDecl *ToNamespace = MergeWithNamespace;
1456 if (!ToNamespace) {
1457 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1458 Name.getAsIdentifierInfo());
1459 ToNamespace->setLexicalDeclContext(LexicalDC);
1460 LexicalDC->addDecl(ToNamespace);
1461
1462 // If this is an anonymous namespace, register it as the anonymous
1463 // namespace within its context.
1464 if (!Name) {
1465 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1466 TU->setAnonymousNamespace(ToNamespace);
1467 else
1468 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1469 }
1470 }
1471 Importer.Imported(D, ToNamespace);
1472
1473 ImportDeclContext(D);
1474
1475 return ToNamespace;
1476}
1477
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001478Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1479 // Import the major distinguishing characteristics of this typedef.
1480 DeclContext *DC, *LexicalDC;
1481 DeclarationName Name;
1482 SourceLocation Loc;
1483 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1484 return 0;
1485
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001486 // If this typedef is not in block scope, determine whether we've
1487 // seen a typedef with the same name (that we can merge with) or any
1488 // other entity by that name (which name lookup could conflict with).
1489 if (!DC->isFunctionOrMethod()) {
1490 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1491 unsigned IDNS = Decl::IDNS_Ordinary;
1492 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1493 Lookup.first != Lookup.second;
1494 ++Lookup.first) {
1495 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1496 continue;
1497 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001498 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1499 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001500 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001501 }
1502
1503 ConflictingDecls.push_back(*Lookup.first);
1504 }
1505
1506 if (!ConflictingDecls.empty()) {
1507 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1508 ConflictingDecls.data(),
1509 ConflictingDecls.size());
1510 if (!Name)
1511 return 0;
1512 }
1513 }
1514
Douglas Gregorb4964f72010-02-15 23:54:17 +00001515 // Import the underlying type of this typedef;
1516 QualType T = Importer.Import(D->getUnderlyingType());
1517 if (T.isNull())
1518 return 0;
1519
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001520 // Create the new typedef node.
1521 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1522 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1523 Loc, Name.getAsIdentifierInfo(),
1524 TInfo);
1525 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001526 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001527 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001528
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001529 return ToTypedef;
1530}
1531
Douglas Gregor98c10182010-02-12 22:17:39 +00001532Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1533 // Import the major distinguishing characteristics of this enum.
1534 DeclContext *DC, *LexicalDC;
1535 DeclarationName Name;
1536 SourceLocation Loc;
1537 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1538 return 0;
1539
1540 // Figure out what enum name we're looking for.
1541 unsigned IDNS = Decl::IDNS_Tag;
1542 DeclarationName SearchName = Name;
1543 if (!SearchName && D->getTypedefForAnonDecl()) {
1544 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1545 IDNS = Decl::IDNS_Ordinary;
1546 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1547 IDNS |= Decl::IDNS_Ordinary;
1548
1549 // We may already have an enum of the same name; try to find and match it.
1550 if (!DC->isFunctionOrMethod() && SearchName) {
1551 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1552 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1553 Lookup.first != Lookup.second;
1554 ++Lookup.first) {
1555 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1556 continue;
1557
1558 Decl *Found = *Lookup.first;
1559 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1560 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1561 Found = Tag->getDecl();
1562 }
1563
1564 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001565 if (IsStructuralMatch(D, FoundEnum))
1566 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001567 }
1568
1569 ConflictingDecls.push_back(*Lookup.first);
1570 }
1571
1572 if (!ConflictingDecls.empty()) {
1573 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1574 ConflictingDecls.data(),
1575 ConflictingDecls.size());
1576 }
1577 }
1578
1579 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001580 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001581 Name.getAsIdentifierInfo(),
1582 Importer.Import(D->getTagKeywordLoc()),
1583 0);
Douglas Gregor3996e242010-02-15 22:01:00 +00001584 D2->setLexicalDeclContext(LexicalDC);
1585 Importer.Imported(D, D2);
1586 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001587
1588 // Import the integer type.
1589 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1590 if (ToIntegerType.isNull())
1591 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001592 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001593
1594 // Import the definition
1595 if (D->isDefinition()) {
1596 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1597 if (T.isNull())
1598 return 0;
1599
1600 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1601 if (ToPromotionType.isNull())
1602 return 0;
1603
Douglas Gregor3996e242010-02-15 22:01:00 +00001604 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00001605 ImportDeclContext(D);
Douglas Gregor3996e242010-02-15 22:01:00 +00001606 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001607 }
1608
Douglas Gregor3996e242010-02-15 22:01:00 +00001609 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001610}
1611
Douglas Gregor5c73e912010-02-11 00:48:18 +00001612Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1613 // If this record has a definition in the translation unit we're coming from,
1614 // but this particular declaration is not that definition, import the
1615 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001616 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001617 if (Definition && Definition != D) {
1618 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001619 if (!ImportedDef)
1620 return 0;
1621
1622 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001623 }
1624
1625 // Import the major distinguishing characteristics of this record.
1626 DeclContext *DC, *LexicalDC;
1627 DeclarationName Name;
1628 SourceLocation Loc;
1629 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1630 return 0;
1631
1632 // Figure out what structure name we're looking for.
1633 unsigned IDNS = Decl::IDNS_Tag;
1634 DeclarationName SearchName = Name;
1635 if (!SearchName && D->getTypedefForAnonDecl()) {
1636 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1637 IDNS = Decl::IDNS_Ordinary;
1638 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1639 IDNS |= Decl::IDNS_Ordinary;
1640
1641 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001642 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001643 if (!DC->isFunctionOrMethod() && SearchName) {
1644 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1645 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1646 Lookup.first != Lookup.second;
1647 ++Lookup.first) {
1648 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1649 continue;
1650
1651 Decl *Found = *Lookup.first;
1652 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1653 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1654 Found = Tag->getDecl();
1655 }
1656
1657 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001658 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1659 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1660 // The record types structurally match, or the "from" translation
1661 // unit only had a forward declaration anyway; call it the same
1662 // function.
1663 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001664 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001665 }
1666 } else {
1667 // We have a forward declaration of this type, so adopt that forward
1668 // declaration rather than building a new one.
1669 AdoptDecl = FoundRecord;
1670 continue;
1671 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001672 }
1673
1674 ConflictingDecls.push_back(*Lookup.first);
1675 }
1676
1677 if (!ConflictingDecls.empty()) {
1678 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1679 ConflictingDecls.data(),
1680 ConflictingDecls.size());
1681 }
1682 }
1683
1684 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001685 RecordDecl *D2 = AdoptDecl;
1686 if (!D2) {
1687 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1688 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001689 D->getTagKind(),
1690 DC, Loc,
1691 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001692 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001693 D2 = D2CXX;
Douglas Gregor25791052010-02-12 00:09:27 +00001694
1695 if (D->isDefinition()) {
1696 // Add base classes.
1697 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1698 for (CXXRecordDecl::base_class_iterator
Douglas Gregor3996e242010-02-15 22:01:00 +00001699 Base1 = D1CXX->bases_begin(),
1700 FromBaseEnd = D1CXX->bases_end();
1701 Base1 != FromBaseEnd;
1702 ++Base1) {
1703 QualType T = Importer.Import(Base1->getType());
Douglas Gregor25791052010-02-12 00:09:27 +00001704 if (T.isNull())
1705 return 0;
1706
1707 Bases.push_back(
1708 new (Importer.getToContext())
Douglas Gregor3996e242010-02-15 22:01:00 +00001709 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1710 Base1->isVirtual(),
1711 Base1->isBaseOfClass(),
1712 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001713 T));
Douglas Gregor25791052010-02-12 00:09:27 +00001714 }
1715 if (!Bases.empty())
Douglas Gregor3996e242010-02-15 22:01:00 +00001716 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001717 }
Douglas Gregor25791052010-02-12 00:09:27 +00001718 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001719 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001720 DC, Loc,
1721 Name.getAsIdentifierInfo(),
1722 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001723 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001724 D2->setLexicalDeclContext(LexicalDC);
1725 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001726 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001727
Douglas Gregor3996e242010-02-15 22:01:00 +00001728 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001729
Douglas Gregor5c73e912010-02-11 00:48:18 +00001730 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001731 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00001732 ImportDeclContext(D);
Douglas Gregor3996e242010-02-15 22:01:00 +00001733 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001734 }
1735
Douglas Gregor3996e242010-02-15 22:01:00 +00001736 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001737}
1738
Douglas Gregor98c10182010-02-12 22:17:39 +00001739Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1740 // Import the major distinguishing characteristics of this enumerator.
1741 DeclContext *DC, *LexicalDC;
1742 DeclarationName Name;
1743 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001744 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001745 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001746
1747 QualType T = Importer.Import(D->getType());
1748 if (T.isNull())
1749 return 0;
1750
Douglas Gregor98c10182010-02-12 22:17:39 +00001751 // Determine whether there are any other declarations with the same name and
1752 // in the same context.
1753 if (!LexicalDC->isFunctionOrMethod()) {
1754 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1755 unsigned IDNS = Decl::IDNS_Ordinary;
1756 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1757 Lookup.first != Lookup.second;
1758 ++Lookup.first) {
1759 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1760 continue;
1761
1762 ConflictingDecls.push_back(*Lookup.first);
1763 }
1764
1765 if (!ConflictingDecls.empty()) {
1766 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1767 ConflictingDecls.data(),
1768 ConflictingDecls.size());
1769 if (!Name)
1770 return 0;
1771 }
1772 }
1773
1774 Expr *Init = Importer.Import(D->getInitExpr());
1775 if (D->getInitExpr() && !Init)
1776 return 0;
1777
1778 EnumConstantDecl *ToEnumerator
1779 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1780 Name.getAsIdentifierInfo(), T,
1781 Init, D->getInitVal());
1782 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001783 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001784 LexicalDC->addDecl(ToEnumerator);
1785 return ToEnumerator;
1786}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001787
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001788Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1789 // Import the major distinguishing characteristics of this function.
1790 DeclContext *DC, *LexicalDC;
1791 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001792 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001793 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001794 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001795
1796 // Try to find a function in our own ("to") context with the same name, same
1797 // type, and in the same context as the function we're importing.
1798 if (!LexicalDC->isFunctionOrMethod()) {
1799 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1800 unsigned IDNS = Decl::IDNS_Ordinary;
1801 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1802 Lookup.first != Lookup.second;
1803 ++Lookup.first) {
1804 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1805 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001806
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001807 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1808 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1809 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001810 if (Importer.IsStructurallyEquivalent(D->getType(),
1811 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001812 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001813 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001814 }
1815
1816 // FIXME: Check for overloading more carefully, e.g., by boosting
1817 // Sema::IsOverload out to the AST library.
1818
1819 // Function overloading is okay in C++.
1820 if (Importer.getToContext().getLangOptions().CPlusPlus)
1821 continue;
1822
1823 // Complain about inconsistent function types.
1824 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001825 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001826 Importer.ToDiag(FoundFunction->getLocation(),
1827 diag::note_odr_value_here)
1828 << FoundFunction->getType();
1829 }
1830 }
1831
1832 ConflictingDecls.push_back(*Lookup.first);
1833 }
1834
1835 if (!ConflictingDecls.empty()) {
1836 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1837 ConflictingDecls.data(),
1838 ConflictingDecls.size());
1839 if (!Name)
1840 return 0;
1841 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001842 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001843
1844 // Import the type.
1845 QualType T = Importer.Import(D->getType());
1846 if (T.isNull())
1847 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001848
1849 // Import the function parameters.
1850 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1851 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1852 P != PEnd; ++P) {
1853 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1854 if (!ToP)
1855 return 0;
1856
1857 Parameters.push_back(ToP);
1858 }
1859
1860 // Create the imported function.
1861 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00001862 FunctionDecl *ToFunction = 0;
1863 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1864 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1865 cast<CXXRecordDecl>(DC),
1866 Loc, Name, T, TInfo,
1867 FromConstructor->isExplicit(),
1868 D->isInlineSpecified(),
1869 D->isImplicit());
1870 } else if (isa<CXXDestructorDecl>(D)) {
1871 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1872 cast<CXXRecordDecl>(DC),
1873 Loc, Name, T,
1874 D->isInlineSpecified(),
1875 D->isImplicit());
1876 } else if (CXXConversionDecl *FromConversion
1877 = dyn_cast<CXXConversionDecl>(D)) {
1878 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1879 cast<CXXRecordDecl>(DC),
1880 Loc, Name, T, TInfo,
1881 D->isInlineSpecified(),
1882 FromConversion->isExplicit());
1883 } else {
1884 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1885 Name, T, TInfo, D->getStorageClass(),
1886 D->isInlineSpecified(),
1887 D->hasWrittenPrototype());
1888 }
Douglas Gregor43f54792010-02-17 02:12:47 +00001889 ToFunction->setLexicalDeclContext(LexicalDC);
1890 Importer.Imported(D, ToFunction);
1891 LexicalDC->addDecl(ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001892
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001893 // Set the parameters.
1894 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00001895 Parameters[I]->setOwningFunction(ToFunction);
1896 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001897 }
Douglas Gregor43f54792010-02-17 02:12:47 +00001898 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001899
1900 // FIXME: Other bits to merge?
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001901
Douglas Gregor43f54792010-02-17 02:12:47 +00001902 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001903}
1904
Douglas Gregor00eace12010-02-21 18:29:16 +00001905Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1906 return VisitFunctionDecl(D);
1907}
1908
1909Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1910 return VisitCXXMethodDecl(D);
1911}
1912
1913Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1914 return VisitCXXMethodDecl(D);
1915}
1916
1917Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1918 return VisitCXXMethodDecl(D);
1919}
1920
Douglas Gregor5c73e912010-02-11 00:48:18 +00001921Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1922 // Import the major distinguishing characteristics of a variable.
1923 DeclContext *DC, *LexicalDC;
1924 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001925 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001926 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1927 return 0;
1928
1929 // Import the type.
1930 QualType T = Importer.Import(D->getType());
1931 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00001932 return 0;
1933
1934 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1935 Expr *BitWidth = Importer.Import(D->getBitWidth());
1936 if (!BitWidth && D->getBitWidth())
1937 return 0;
1938
1939 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1940 Loc, Name.getAsIdentifierInfo(),
1941 T, TInfo, BitWidth, D->isMutable());
1942 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001943 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001944 LexicalDC->addDecl(ToField);
1945 return ToField;
1946}
1947
Douglas Gregor7244b0b2010-02-17 00:34:30 +00001948Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1949 // Import the major distinguishing characteristics of an ivar.
1950 DeclContext *DC, *LexicalDC;
1951 DeclarationName Name;
1952 SourceLocation Loc;
1953 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1954 return 0;
1955
1956 // Determine whether we've already imported this ivar
1957 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1958 Lookup.first != Lookup.second;
1959 ++Lookup.first) {
1960 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
1961 if (Importer.IsStructurallyEquivalent(D->getType(),
1962 FoundIvar->getType())) {
1963 Importer.Imported(D, FoundIvar);
1964 return FoundIvar;
1965 }
1966
1967 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
1968 << Name << D->getType() << FoundIvar->getType();
1969 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
1970 << FoundIvar->getType();
1971 return 0;
1972 }
1973 }
1974
1975 // Import the type.
1976 QualType T = Importer.Import(D->getType());
1977 if (T.isNull())
1978 return 0;
1979
1980 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1981 Expr *BitWidth = Importer.Import(D->getBitWidth());
1982 if (!BitWidth && D->getBitWidth())
1983 return 0;
1984
1985 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC,
1986 Loc, Name.getAsIdentifierInfo(),
1987 T, TInfo, D->getAccessControl(),
1988 BitWidth);
1989 ToIvar->setLexicalDeclContext(LexicalDC);
1990 Importer.Imported(D, ToIvar);
1991 LexicalDC->addDecl(ToIvar);
1992 return ToIvar;
1993
1994}
1995
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001996Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1997 // Import the major distinguishing characteristics of a variable.
1998 DeclContext *DC, *LexicalDC;
1999 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002000 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002001 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002002 return 0;
2003
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002004 // Try to find a variable in our own ("to") context with the same name and
2005 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002006 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002007 VarDecl *MergeWithVar = 0;
2008 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2009 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002010 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002011 Lookup.first != Lookup.second;
2012 ++Lookup.first) {
2013 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2014 continue;
2015
2016 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2017 // We have found a variable that we may need to merge with. Check it.
2018 if (isExternalLinkage(FoundVar->getLinkage()) &&
2019 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002020 if (Importer.IsStructurallyEquivalent(D->getType(),
2021 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002022 MergeWithVar = FoundVar;
2023 break;
2024 }
2025
Douglas Gregor56521c52010-02-12 17:23:39 +00002026 const ArrayType *FoundArray
2027 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2028 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002029 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002030 if (FoundArray && TArray) {
2031 if (isa<IncompleteArrayType>(FoundArray) &&
2032 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002033 // Import the type.
2034 QualType T = Importer.Import(D->getType());
2035 if (T.isNull())
2036 return 0;
2037
Douglas Gregor56521c52010-02-12 17:23:39 +00002038 FoundVar->setType(T);
2039 MergeWithVar = FoundVar;
2040 break;
2041 } else if (isa<IncompleteArrayType>(TArray) &&
2042 isa<ConstantArrayType>(FoundArray)) {
2043 MergeWithVar = FoundVar;
2044 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002045 }
2046 }
2047
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002048 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002049 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002050 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2051 << FoundVar->getType();
2052 }
2053 }
2054
2055 ConflictingDecls.push_back(*Lookup.first);
2056 }
2057
2058 if (MergeWithVar) {
2059 // An equivalent variable with external linkage has been found. Link
2060 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002061 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002062
2063 if (VarDecl *DDef = D->getDefinition()) {
2064 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2065 Importer.ToDiag(ExistingDef->getLocation(),
2066 diag::err_odr_variable_multiple_def)
2067 << Name;
2068 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2069 } else {
2070 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002071 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002072 }
2073 }
2074
2075 return MergeWithVar;
2076 }
2077
2078 if (!ConflictingDecls.empty()) {
2079 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2080 ConflictingDecls.data(),
2081 ConflictingDecls.size());
2082 if (!Name)
2083 return 0;
2084 }
2085 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002086
Douglas Gregorb4964f72010-02-15 23:54:17 +00002087 // Import the type.
2088 QualType T = Importer.Import(D->getType());
2089 if (T.isNull())
2090 return 0;
2091
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002092 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002093 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002094 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2095 Name.getAsIdentifierInfo(), T, TInfo,
2096 D->getStorageClass());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002097 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002098 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002099 LexicalDC->addDecl(ToVar);
2100
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002101 // Merge the initializer.
2102 // FIXME: Can we really import any initializer? Alternatively, we could force
2103 // ourselves to import every declaration of a variable and then only use
2104 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002105 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002106
2107 // FIXME: Other bits to merge?
2108
2109 return ToVar;
2110}
2111
Douglas Gregor8b228d72010-02-17 21:22:52 +00002112Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2113 // Parameters are created in the translation unit's context, then moved
2114 // into the function declaration's context afterward.
2115 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2116
2117 // Import the name of this declaration.
2118 DeclarationName Name = Importer.Import(D->getDeclName());
2119 if (D->getDeclName() && !Name)
2120 return 0;
2121
2122 // Import the location of this declaration.
2123 SourceLocation Loc = Importer.Import(D->getLocation());
2124
2125 // Import the parameter's type.
2126 QualType T = Importer.Import(D->getType());
2127 if (T.isNull())
2128 return 0;
2129
2130 // Create the imported parameter.
2131 ImplicitParamDecl *ToParm
2132 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2133 Loc, Name.getAsIdentifierInfo(),
2134 T);
2135 return Importer.Imported(D, ToParm);
2136}
2137
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002138Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2139 // Parameters are created in the translation unit's context, then moved
2140 // into the function declaration's context afterward.
2141 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2142
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002143 // Import the name of this declaration.
2144 DeclarationName Name = Importer.Import(D->getDeclName());
2145 if (D->getDeclName() && !Name)
2146 return 0;
2147
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002148 // Import the location of this declaration.
2149 SourceLocation Loc = Importer.Import(D->getLocation());
2150
2151 // Import the parameter's type.
2152 QualType T = Importer.Import(D->getType());
2153 if (T.isNull())
2154 return 0;
2155
2156 // Create the imported parameter.
2157 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2158 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2159 Loc, Name.getAsIdentifierInfo(),
2160 T, TInfo, D->getStorageClass(),
2161 /*FIXME: Default argument*/ 0);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002162 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002163}
2164
Douglas Gregor43f54792010-02-17 02:12:47 +00002165Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2166 // Import the major distinguishing characteristics of a method.
2167 DeclContext *DC, *LexicalDC;
2168 DeclarationName Name;
2169 SourceLocation Loc;
2170 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2171 return 0;
2172
2173 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2174 Lookup.first != Lookup.second;
2175 ++Lookup.first) {
2176 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2177 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2178 continue;
2179
2180 // Check return types.
2181 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2182 FoundMethod->getResultType())) {
2183 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2184 << D->isInstanceMethod() << Name
2185 << D->getResultType() << FoundMethod->getResultType();
2186 Importer.ToDiag(FoundMethod->getLocation(),
2187 diag::note_odr_objc_method_here)
2188 << D->isInstanceMethod() << Name;
2189 return 0;
2190 }
2191
2192 // Check the number of parameters.
2193 if (D->param_size() != FoundMethod->param_size()) {
2194 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2195 << D->isInstanceMethod() << Name
2196 << D->param_size() << FoundMethod->param_size();
2197 Importer.ToDiag(FoundMethod->getLocation(),
2198 diag::note_odr_objc_method_here)
2199 << D->isInstanceMethod() << Name;
2200 return 0;
2201 }
2202
2203 // Check parameter types.
2204 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2205 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2206 P != PEnd; ++P, ++FoundP) {
2207 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2208 (*FoundP)->getType())) {
2209 Importer.FromDiag((*P)->getLocation(),
2210 diag::err_odr_objc_method_param_type_inconsistent)
2211 << D->isInstanceMethod() << Name
2212 << (*P)->getType() << (*FoundP)->getType();
2213 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2214 << (*FoundP)->getType();
2215 return 0;
2216 }
2217 }
2218
2219 // Check variadic/non-variadic.
2220 // Check the number of parameters.
2221 if (D->isVariadic() != FoundMethod->isVariadic()) {
2222 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2223 << D->isInstanceMethod() << Name;
2224 Importer.ToDiag(FoundMethod->getLocation(),
2225 diag::note_odr_objc_method_here)
2226 << D->isInstanceMethod() << Name;
2227 return 0;
2228 }
2229
2230 // FIXME: Any other bits we need to merge?
2231 return Importer.Imported(D, FoundMethod);
2232 }
2233 }
2234
2235 // Import the result type.
2236 QualType ResultTy = Importer.Import(D->getResultType());
2237 if (ResultTy.isNull())
2238 return 0;
2239
2240 ObjCMethodDecl *ToMethod
2241 = ObjCMethodDecl::Create(Importer.getToContext(),
2242 Loc,
2243 Importer.Import(D->getLocEnd()),
2244 Name.getObjCSelector(),
2245 ResultTy, DC,
2246 D->isInstanceMethod(),
2247 D->isVariadic(),
2248 D->isSynthesized(),
2249 D->getImplementationControl());
2250
2251 // FIXME: When we decide to merge method definitions, we'll need to
2252 // deal with implicit parameters.
2253
2254 // Import the parameters
2255 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2256 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2257 FromPEnd = D->param_end();
2258 FromP != FromPEnd;
2259 ++FromP) {
2260 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2261 if (!ToP)
2262 return 0;
2263
2264 ToParams.push_back(ToP);
2265 }
2266
2267 // Set the parameters.
2268 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2269 ToParams[I]->setOwningFunction(ToMethod);
2270 ToMethod->addDecl(ToParams[I]);
2271 }
2272 ToMethod->setMethodParams(Importer.getToContext(),
2273 ToParams.data(), ToParams.size());
2274
2275 ToMethod->setLexicalDeclContext(LexicalDC);
2276 Importer.Imported(D, ToMethod);
2277 LexicalDC->addDecl(ToMethod);
2278 return ToMethod;
2279}
2280
Douglas Gregor84c51c32010-02-18 01:47:50 +00002281Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2282 // Import the major distinguishing characteristics of a category.
2283 DeclContext *DC, *LexicalDC;
2284 DeclarationName Name;
2285 SourceLocation Loc;
2286 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2287 return 0;
2288
2289 ObjCInterfaceDecl *ToInterface
2290 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2291 if (!ToInterface)
2292 return 0;
2293
2294 // Determine if we've already encountered this category.
2295 ObjCCategoryDecl *MergeWithCategory
2296 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2297 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2298 if (!ToCategory) {
2299 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2300 Importer.Import(D->getAtLoc()),
2301 Loc,
2302 Importer.Import(D->getCategoryNameLoc()),
2303 Name.getAsIdentifierInfo());
2304 ToCategory->setLexicalDeclContext(LexicalDC);
2305 LexicalDC->addDecl(ToCategory);
2306 Importer.Imported(D, ToCategory);
2307
2308 // Link this category into its class's category list.
2309 ToCategory->setClassInterface(ToInterface);
2310 ToCategory->insertNextClassCategory();
2311
2312 // Import protocols
2313 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2314 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2315 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2316 = D->protocol_loc_begin();
2317 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2318 FromProtoEnd = D->protocol_end();
2319 FromProto != FromProtoEnd;
2320 ++FromProto, ++FromProtoLoc) {
2321 ObjCProtocolDecl *ToProto
2322 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2323 if (!ToProto)
2324 return 0;
2325 Protocols.push_back(ToProto);
2326 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2327 }
2328
2329 // FIXME: If we're merging, make sure that the protocol list is the same.
2330 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2331 ProtocolLocs.data(), Importer.getToContext());
2332
2333 } else {
2334 Importer.Imported(D, ToCategory);
2335 }
2336
2337 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002338 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002339
2340 // If we have an implementation, import it as well.
2341 if (D->getImplementation()) {
2342 ObjCCategoryImplDecl *Impl
2343 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2344 if (!Impl)
2345 return 0;
2346
2347 ToCategory->setImplementation(Impl);
2348 }
2349
2350 return ToCategory;
2351}
2352
Douglas Gregor98d156a2010-02-17 16:12:00 +00002353Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002354 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002355 DeclContext *DC, *LexicalDC;
2356 DeclarationName Name;
2357 SourceLocation Loc;
2358 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2359 return 0;
2360
2361 ObjCProtocolDecl *MergeWithProtocol = 0;
2362 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2363 Lookup.first != Lookup.second;
2364 ++Lookup.first) {
2365 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2366 continue;
2367
2368 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2369 break;
2370 }
2371
2372 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2373 if (!ToProto || ToProto->isForwardDecl()) {
2374 if (!ToProto) {
2375 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2376 Name.getAsIdentifierInfo());
2377 ToProto->setForwardDecl(D->isForwardDecl());
2378 ToProto->setLexicalDeclContext(LexicalDC);
2379 LexicalDC->addDecl(ToProto);
2380 }
2381 Importer.Imported(D, ToProto);
2382
2383 // Import protocols
2384 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2385 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2386 ObjCProtocolDecl::protocol_loc_iterator
2387 FromProtoLoc = D->protocol_loc_begin();
2388 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2389 FromProtoEnd = D->protocol_end();
2390 FromProto != FromProtoEnd;
2391 ++FromProto, ++FromProtoLoc) {
2392 ObjCProtocolDecl *ToProto
2393 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2394 if (!ToProto)
2395 return 0;
2396 Protocols.push_back(ToProto);
2397 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2398 }
2399
2400 // FIXME: If we're merging, make sure that the protocol list is the same.
2401 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2402 ProtocolLocs.data(), Importer.getToContext());
2403 } else {
2404 Importer.Imported(D, ToProto);
2405 }
2406
Douglas Gregor84c51c32010-02-18 01:47:50 +00002407 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002408 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002409
2410 return ToProto;
2411}
2412
Douglas Gregor45635322010-02-16 01:20:57 +00002413Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2414 // Import the major distinguishing characteristics of an @interface.
2415 DeclContext *DC, *LexicalDC;
2416 DeclarationName Name;
2417 SourceLocation Loc;
2418 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2419 return 0;
2420
2421 ObjCInterfaceDecl *MergeWithIface = 0;
2422 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2423 Lookup.first != Lookup.second;
2424 ++Lookup.first) {
2425 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2426 continue;
2427
2428 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2429 break;
2430 }
2431
2432 ObjCInterfaceDecl *ToIface = MergeWithIface;
2433 if (!ToIface || ToIface->isForwardDecl()) {
2434 if (!ToIface) {
2435 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2436 DC, Loc,
2437 Name.getAsIdentifierInfo(),
2438 Importer.Import(D->getClassLoc()),
2439 D->isForwardDecl(),
2440 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002441 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002442 ToIface->setLexicalDeclContext(LexicalDC);
2443 LexicalDC->addDecl(ToIface);
2444 }
2445 Importer.Imported(D, ToIface);
2446
Douglas Gregor45635322010-02-16 01:20:57 +00002447 if (D->getSuperClass()) {
2448 ObjCInterfaceDecl *Super
2449 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2450 if (!Super)
2451 return 0;
2452
2453 ToIface->setSuperClass(Super);
2454 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2455 }
2456
2457 // Import protocols
2458 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2459 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2460 ObjCInterfaceDecl::protocol_loc_iterator
2461 FromProtoLoc = D->protocol_loc_begin();
2462 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2463 FromProtoEnd = D->protocol_end();
2464 FromProto != FromProtoEnd;
2465 ++FromProto, ++FromProtoLoc) {
2466 ObjCProtocolDecl *ToProto
2467 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2468 if (!ToProto)
2469 return 0;
2470 Protocols.push_back(ToProto);
2471 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2472 }
2473
2474 // FIXME: If we're merging, make sure that the protocol list is the same.
2475 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2476 ProtocolLocs.data(), Importer.getToContext());
2477
Douglas Gregor45635322010-02-16 01:20:57 +00002478 // Import @end range
2479 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2480 } else {
2481 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002482
2483 // Check for consistency of superclasses.
2484 DeclarationName FromSuperName, ToSuperName;
2485 if (D->getSuperClass())
2486 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2487 if (ToIface->getSuperClass())
2488 ToSuperName = ToIface->getSuperClass()->getDeclName();
2489 if (FromSuperName != ToSuperName) {
2490 Importer.ToDiag(ToIface->getLocation(),
2491 diag::err_odr_objc_superclass_inconsistent)
2492 << ToIface->getDeclName();
2493 if (ToIface->getSuperClass())
2494 Importer.ToDiag(ToIface->getSuperClassLoc(),
2495 diag::note_odr_objc_superclass)
2496 << ToIface->getSuperClass()->getDeclName();
2497 else
2498 Importer.ToDiag(ToIface->getLocation(),
2499 diag::note_odr_objc_missing_superclass);
2500 if (D->getSuperClass())
2501 Importer.FromDiag(D->getSuperClassLoc(),
2502 diag::note_odr_objc_superclass)
2503 << D->getSuperClass()->getDeclName();
2504 else
2505 Importer.FromDiag(D->getLocation(),
2506 diag::note_odr_objc_missing_superclass);
2507 return 0;
2508 }
Douglas Gregor45635322010-02-16 01:20:57 +00002509 }
2510
Douglas Gregor84c51c32010-02-18 01:47:50 +00002511 // Import categories. When the categories themselves are imported, they'll
2512 // hook themselves into this interface.
2513 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2514 FromCat = FromCat->getNextClassCategory())
2515 Importer.Import(FromCat);
2516
Douglas Gregor45635322010-02-16 01:20:57 +00002517 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00002518 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00002519
2520 // If we have an @implementation, import it as well.
2521 if (D->getImplementation()) {
2522 ObjCImplementationDecl *Impl
2523 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2524 if (!Impl)
2525 return 0;
2526
2527 ToIface->setImplementation(Impl);
2528 }
2529
Douglas Gregor98d156a2010-02-17 16:12:00 +00002530 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00002531}
2532
Douglas Gregora11c4582010-02-17 18:02:10 +00002533Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2534 // Import the major distinguishing characteristics of an @property.
2535 DeclContext *DC, *LexicalDC;
2536 DeclarationName Name;
2537 SourceLocation Loc;
2538 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2539 return 0;
2540
2541 // Check whether we have already imported this property.
2542 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2543 Lookup.first != Lookup.second;
2544 ++Lookup.first) {
2545 if (ObjCPropertyDecl *FoundProp
2546 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2547 // Check property types.
2548 if (!Importer.IsStructurallyEquivalent(D->getType(),
2549 FoundProp->getType())) {
2550 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2551 << Name << D->getType() << FoundProp->getType();
2552 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2553 << FoundProp->getType();
2554 return 0;
2555 }
2556
2557 // FIXME: Check property attributes, getters, setters, etc.?
2558
2559 // Consider these properties to be equivalent.
2560 Importer.Imported(D, FoundProp);
2561 return FoundProp;
2562 }
2563 }
2564
2565 // Import the type.
2566 QualType T = Importer.Import(D->getType());
2567 if (T.isNull())
2568 return 0;
2569
2570 // Create the new property.
2571 ObjCPropertyDecl *ToProperty
2572 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2573 Name.getAsIdentifierInfo(),
2574 Importer.Import(D->getAtLoc()),
2575 T,
2576 D->getPropertyImplementation());
2577 Importer.Imported(D, ToProperty);
2578 ToProperty->setLexicalDeclContext(LexicalDC);
2579 LexicalDC->addDecl(ToProperty);
2580
2581 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2582 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2583 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2584 ToProperty->setGetterMethodDecl(
2585 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2586 ToProperty->setSetterMethodDecl(
2587 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2588 ToProperty->setPropertyIvarDecl(
2589 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2590 return ToProperty;
2591}
2592
Douglas Gregor8661a722010-02-18 02:12:22 +00002593Decl *
2594ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2595 // Import the context of this declaration.
2596 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2597 if (!DC)
2598 return 0;
2599
2600 DeclContext *LexicalDC = DC;
2601 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2602 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2603 if (!LexicalDC)
2604 return 0;
2605 }
2606
2607 // Import the location of this declaration.
2608 SourceLocation Loc = Importer.Import(D->getLocation());
2609
2610 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2611 llvm::SmallVector<SourceLocation, 4> Locations;
2612 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2613 = D->protocol_loc_begin();
2614 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2615 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2616 FromProto != FromProtoEnd;
2617 ++FromProto, ++FromProtoLoc) {
2618 ObjCProtocolDecl *ToProto
2619 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2620 if (!ToProto)
2621 continue;
2622
2623 Protocols.push_back(ToProto);
2624 Locations.push_back(Importer.Import(*FromProtoLoc));
2625 }
2626
2627 ObjCForwardProtocolDecl *ToForward
2628 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2629 Protocols.data(), Protocols.size(),
2630 Locations.data());
2631 ToForward->setLexicalDeclContext(LexicalDC);
2632 LexicalDC->addDecl(ToForward);
2633 Importer.Imported(D, ToForward);
2634 return ToForward;
2635}
2636
Douglas Gregor06537af2010-02-18 02:04:09 +00002637Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2638 // Import the context of this declaration.
2639 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2640 if (!DC)
2641 return 0;
2642
2643 DeclContext *LexicalDC = DC;
2644 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2645 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2646 if (!LexicalDC)
2647 return 0;
2648 }
2649
2650 // Import the location of this declaration.
2651 SourceLocation Loc = Importer.Import(D->getLocation());
2652
2653 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2654 llvm::SmallVector<SourceLocation, 4> Locations;
2655 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2656 From != FromEnd; ++From) {
2657 ObjCInterfaceDecl *ToIface
2658 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2659 if (!ToIface)
2660 continue;
2661
2662 Interfaces.push_back(ToIface);
2663 Locations.push_back(Importer.Import(From->getLocation()));
2664 }
2665
2666 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2667 Loc,
2668 Interfaces.data(),
2669 Locations.data(),
2670 Interfaces.size());
2671 ToClass->setLexicalDeclContext(LexicalDC);
2672 LexicalDC->addDecl(ToClass);
2673 Importer.Imported(D, ToClass);
2674 return ToClass;
2675}
2676
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002677//----------------------------------------------------------------------------
2678// Import Statements
2679//----------------------------------------------------------------------------
2680
2681Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2682 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2683 << S->getStmtClassName();
2684 return 0;
2685}
2686
2687//----------------------------------------------------------------------------
2688// Import Expressions
2689//----------------------------------------------------------------------------
2690Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2691 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2692 << E->getStmtClassName();
2693 return 0;
2694}
2695
Douglas Gregor52f820e2010-02-19 01:17:02 +00002696Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2697 NestedNameSpecifier *Qualifier = 0;
2698 if (E->getQualifier()) {
2699 Qualifier = Importer.Import(E->getQualifier());
2700 if (!E->getQualifier())
2701 return 0;
2702 }
2703
2704 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2705 if (!ToD)
2706 return 0;
2707
2708 QualType T = Importer.Import(E->getType());
2709 if (T.isNull())
2710 return 0;
2711
2712 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2713 Importer.Import(E->getQualifierRange()),
2714 ToD,
2715 Importer.Import(E->getLocation()),
2716 T,
2717 /*FIXME:TemplateArgs=*/0);
2718}
2719
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002720Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2721 QualType T = Importer.Import(E->getType());
2722 if (T.isNull())
2723 return 0;
2724
2725 return new (Importer.getToContext())
2726 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2727}
2728
Douglas Gregor623421d2010-02-18 02:21:22 +00002729Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2730 QualType T = Importer.Import(E->getType());
2731 if (T.isNull())
2732 return 0;
2733
2734 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2735 E->isWide(), T,
2736 Importer.Import(E->getLocation()));
2737}
2738
Douglas Gregorc74247e2010-02-19 01:07:06 +00002739Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2740 Expr *SubExpr = Importer.Import(E->getSubExpr());
2741 if (!SubExpr)
2742 return 0;
2743
2744 return new (Importer.getToContext())
2745 ParenExpr(Importer.Import(E->getLParen()),
2746 Importer.Import(E->getRParen()),
2747 SubExpr);
2748}
2749
2750Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2751 QualType T = Importer.Import(E->getType());
2752 if (T.isNull())
2753 return 0;
2754
2755 Expr *SubExpr = Importer.Import(E->getSubExpr());
2756 if (!SubExpr)
2757 return 0;
2758
2759 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2760 T,
2761 Importer.Import(E->getOperatorLoc()));
2762}
2763
Douglas Gregord8552cd2010-02-19 01:24:23 +00002764Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2765 QualType ResultType = Importer.Import(E->getType());
2766
2767 if (E->isArgumentType()) {
2768 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2769 if (!TInfo)
2770 return 0;
2771
2772 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2773 TInfo, ResultType,
2774 Importer.Import(E->getOperatorLoc()),
2775 Importer.Import(E->getRParenLoc()));
2776 }
2777
2778 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2779 if (!SubExpr)
2780 return 0;
2781
2782 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2783 SubExpr, ResultType,
2784 Importer.Import(E->getOperatorLoc()),
2785 Importer.Import(E->getRParenLoc()));
2786}
2787
Douglas Gregorc74247e2010-02-19 01:07:06 +00002788Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2789 QualType T = Importer.Import(E->getType());
2790 if (T.isNull())
2791 return 0;
2792
2793 Expr *LHS = Importer.Import(E->getLHS());
2794 if (!LHS)
2795 return 0;
2796
2797 Expr *RHS = Importer.Import(E->getRHS());
2798 if (!RHS)
2799 return 0;
2800
2801 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2802 T,
2803 Importer.Import(E->getOperatorLoc()));
2804}
2805
2806Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2807 QualType T = Importer.Import(E->getType());
2808 if (T.isNull())
2809 return 0;
2810
2811 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2812 if (CompLHSType.isNull())
2813 return 0;
2814
2815 QualType CompResultType = Importer.Import(E->getComputationResultType());
2816 if (CompResultType.isNull())
2817 return 0;
2818
2819 Expr *LHS = Importer.Import(E->getLHS());
2820 if (!LHS)
2821 return 0;
2822
2823 Expr *RHS = Importer.Import(E->getRHS());
2824 if (!RHS)
2825 return 0;
2826
2827 return new (Importer.getToContext())
2828 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2829 T, CompLHSType, CompResultType,
2830 Importer.Import(E->getOperatorLoc()));
2831}
2832
Douglas Gregor98c10182010-02-12 22:17:39 +00002833Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2834 QualType T = Importer.Import(E->getType());
2835 if (T.isNull())
2836 return 0;
2837
2838 Expr *SubExpr = Importer.Import(E->getSubExpr());
2839 if (!SubExpr)
2840 return 0;
2841
2842 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2843 SubExpr,
2844 E->isLvalueCast());
2845}
2846
Douglas Gregor5481d322010-02-19 01:32:14 +00002847Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2848 QualType T = Importer.Import(E->getType());
2849 if (T.isNull())
2850 return 0;
2851
2852 Expr *SubExpr = Importer.Import(E->getSubExpr());
2853 if (!SubExpr)
2854 return 0;
2855
2856 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2857 if (!TInfo && E->getTypeInfoAsWritten())
2858 return 0;
2859
2860 return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
2861 SubExpr, TInfo,
2862 Importer.Import(E->getLParenLoc()),
2863 Importer.Import(E->getRParenLoc()));
2864}
2865
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002866ASTImporter::ASTImporter(Diagnostic &Diags,
2867 ASTContext &ToContext, FileManager &ToFileManager,
2868 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00002869 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00002870 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002871 Diags(Diags) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00002872 ImportedDecls[FromContext.getTranslationUnitDecl()]
2873 = ToContext.getTranslationUnitDecl();
2874}
2875
2876ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002877
2878QualType ASTImporter::Import(QualType FromT) {
2879 if (FromT.isNull())
2880 return QualType();
2881
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002882 // Check whether we've already imported this type.
2883 llvm::DenseMap<Type *, Type *>::iterator Pos
2884 = ImportedTypes.find(FromT.getTypePtr());
2885 if (Pos != ImportedTypes.end())
2886 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002887
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002888 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00002889 ASTNodeImporter Importer(*this);
2890 QualType ToT = Importer.Visit(FromT.getTypePtr());
2891 if (ToT.isNull())
2892 return ToT;
2893
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002894 // Record the imported type.
2895 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2896
Douglas Gregor96e578d2010-02-05 17:54:41 +00002897 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2898}
2899
Douglas Gregor62d311f2010-02-09 19:21:46 +00002900TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002901 if (!FromTSI)
2902 return FromTSI;
2903
2904 // FIXME: For now we just create a "trivial" type source info based
2905 // on the type and a seingle location. Implement a real version of
2906 // this.
2907 QualType T = Import(FromTSI->getType());
2908 if (T.isNull())
2909 return 0;
2910
2911 return ToContext.getTrivialTypeSourceInfo(T,
2912 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002913}
2914
2915Decl *ASTImporter::Import(Decl *FromD) {
2916 if (!FromD)
2917 return 0;
2918
2919 // Check whether we've already imported this declaration.
2920 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2921 if (Pos != ImportedDecls.end())
2922 return Pos->second;
2923
2924 // Import the type
2925 ASTNodeImporter Importer(*this);
2926 Decl *ToD = Importer.Visit(FromD);
2927 if (!ToD)
2928 return 0;
2929
2930 // Record the imported declaration.
2931 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002932
2933 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2934 // Keep track of anonymous tags that have an associated typedef.
2935 if (FromTag->getTypedefForAnonDecl())
2936 AnonTagsWithPendingTypedefs.push_back(FromTag);
2937 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2938 // When we've finished transforming a typedef, see whether it was the
2939 // typedef for an anonymous tag.
2940 for (llvm::SmallVector<TagDecl *, 4>::iterator
2941 FromTag = AnonTagsWithPendingTypedefs.begin(),
2942 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2943 FromTag != FromTagEnd; ++FromTag) {
2944 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2945 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2946 // We found the typedef for an anonymous tag; link them.
2947 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2948 AnonTagsWithPendingTypedefs.erase(FromTag);
2949 break;
2950 }
2951 }
2952 }
2953 }
2954
Douglas Gregor62d311f2010-02-09 19:21:46 +00002955 return ToD;
2956}
2957
2958DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
2959 if (!FromDC)
2960 return FromDC;
2961
2962 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
2963}
2964
2965Expr *ASTImporter::Import(Expr *FromE) {
2966 if (!FromE)
2967 return 0;
2968
2969 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
2970}
2971
2972Stmt *ASTImporter::Import(Stmt *FromS) {
2973 if (!FromS)
2974 return 0;
2975
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002976 // Check whether we've already imported this declaration.
2977 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
2978 if (Pos != ImportedStmts.end())
2979 return Pos->second;
2980
2981 // Import the type
2982 ASTNodeImporter Importer(*this);
2983 Stmt *ToS = Importer.Visit(FromS);
2984 if (!ToS)
2985 return 0;
2986
2987 // Record the imported declaration.
2988 ImportedStmts[FromS] = ToS;
2989 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002990}
2991
2992NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
2993 if (!FromNNS)
2994 return 0;
2995
2996 // FIXME: Implement!
2997 return 0;
2998}
2999
3000SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3001 if (FromLoc.isInvalid())
3002 return SourceLocation();
3003
Douglas Gregor811663e2010-02-10 00:15:17 +00003004 SourceManager &FromSM = FromContext.getSourceManager();
3005
3006 // For now, map everything down to its spelling location, so that we
3007 // don't have to import macro instantiations.
3008 // FIXME: Import macro instantiations!
3009 FromLoc = FromSM.getSpellingLoc(FromLoc);
3010 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3011 SourceManager &ToSM = ToContext.getSourceManager();
3012 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3013 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003014}
3015
3016SourceRange ASTImporter::Import(SourceRange FromRange) {
3017 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3018}
3019
Douglas Gregor811663e2010-02-10 00:15:17 +00003020FileID ASTImporter::Import(FileID FromID) {
3021 llvm::DenseMap<unsigned, FileID>::iterator Pos
3022 = ImportedFileIDs.find(FromID.getHashValue());
3023 if (Pos != ImportedFileIDs.end())
3024 return Pos->second;
3025
3026 SourceManager &FromSM = FromContext.getSourceManager();
3027 SourceManager &ToSM = ToContext.getSourceManager();
3028 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3029 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3030
3031 // Include location of this file.
3032 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3033
3034 // Map the FileID for to the "to" source manager.
3035 FileID ToID;
3036 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3037 if (Cache->Entry) {
3038 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3039 // disk again
3040 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3041 // than mmap the files several times.
3042 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3043 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3044 FromSLoc.getFile().getFileCharacteristic());
3045 } else {
3046 // FIXME: We want to re-use the existing MemoryBuffer!
3047 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
3048 llvm::MemoryBuffer *ToBuf
3049 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
3050 FromBuf->getBufferEnd(),
3051 FromBuf->getBufferIdentifier());
3052 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3053 }
3054
3055
3056 ImportedFileIDs[FromID.getHashValue()] = ToID;
3057 return ToID;
3058}
3059
Douglas Gregor96e578d2010-02-05 17:54:41 +00003060DeclarationName ASTImporter::Import(DeclarationName FromName) {
3061 if (!FromName)
3062 return DeclarationName();
3063
3064 switch (FromName.getNameKind()) {
3065 case DeclarationName::Identifier:
3066 return Import(FromName.getAsIdentifierInfo());
3067
3068 case DeclarationName::ObjCZeroArgSelector:
3069 case DeclarationName::ObjCOneArgSelector:
3070 case DeclarationName::ObjCMultiArgSelector:
3071 return Import(FromName.getObjCSelector());
3072
3073 case DeclarationName::CXXConstructorName: {
3074 QualType T = Import(FromName.getCXXNameType());
3075 if (T.isNull())
3076 return DeclarationName();
3077
3078 return ToContext.DeclarationNames.getCXXConstructorName(
3079 ToContext.getCanonicalType(T));
3080 }
3081
3082 case DeclarationName::CXXDestructorName: {
3083 QualType T = Import(FromName.getCXXNameType());
3084 if (T.isNull())
3085 return DeclarationName();
3086
3087 return ToContext.DeclarationNames.getCXXDestructorName(
3088 ToContext.getCanonicalType(T));
3089 }
3090
3091 case DeclarationName::CXXConversionFunctionName: {
3092 QualType T = Import(FromName.getCXXNameType());
3093 if (T.isNull())
3094 return DeclarationName();
3095
3096 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3097 ToContext.getCanonicalType(T));
3098 }
3099
3100 case DeclarationName::CXXOperatorName:
3101 return ToContext.DeclarationNames.getCXXOperatorName(
3102 FromName.getCXXOverloadedOperator());
3103
3104 case DeclarationName::CXXLiteralOperatorName:
3105 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3106 Import(FromName.getCXXLiteralIdentifier()));
3107
3108 case DeclarationName::CXXUsingDirective:
3109 // FIXME: STATICS!
3110 return DeclarationName::getUsingDirectiveName();
3111 }
3112
3113 // Silence bogus GCC warning
3114 return DeclarationName();
3115}
3116
3117IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3118 if (!FromId)
3119 return 0;
3120
3121 return &ToContext.Idents.get(FromId->getName());
3122}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003123
Douglas Gregor43f54792010-02-17 02:12:47 +00003124Selector ASTImporter::Import(Selector FromSel) {
3125 if (FromSel.isNull())
3126 return Selector();
3127
3128 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3129 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3130 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3131 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3132 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3133}
3134
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003135DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3136 DeclContext *DC,
3137 unsigned IDNS,
3138 NamedDecl **Decls,
3139 unsigned NumDecls) {
3140 return Name;
3141}
3142
3143DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003144 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
3145 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003146}
3147
3148DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003149 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
3150 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003151}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003152
3153Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3154 ImportedDecls[From] = To;
3155 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00003156}
Douglas Gregorb4964f72010-02-15 23:54:17 +00003157
3158bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3159 llvm::DenseMap<Type *, Type *>::iterator Pos
3160 = ImportedTypes.find(From.getTypePtr());
3161 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3162 return true;
3163
Benjamin Kramer26d19c52010-02-18 13:02:13 +00003164 StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
Douglas Gregorb4964f72010-02-15 23:54:17 +00003165 NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00003166 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003167}