blob: dee0d2b342fce6db002b8104395fb663c2bfadb1 [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,
83 SourceLocation &Loc);
Douglas Gregor5c73e912010-02-11 00:48:18 +000084 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000085 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregore4c83e42010-02-09 22:48:33 +000086 Decl *VisitDecl(Decl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000087 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000088 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000089 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000090 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000091 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000092 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000093 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000094 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +000095 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
96
Douglas Gregor7eeb5972010-02-11 19:21:55 +000097 // Importing statements
98 Stmt *VisitStmt(Stmt *S);
99
100 // Importing expressions
101 Expr *VisitExpr(Expr *E);
102 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000103 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000104 };
105}
106
107//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000108// Structural Equivalence
109//----------------------------------------------------------------------------
110
111namespace {
112 struct StructuralEquivalenceContext {
113 /// \brief AST contexts for which we are checking structural equivalence.
114 ASTContext &C1, &C2;
115
116 /// \brief Diagnostic object used to emit diagnostics.
117 Diagnostic &Diags;
118
119 /// \brief The set of "tentative" equivalences between two canonical
120 /// declarations, mapping from a declaration in the first context to the
121 /// declaration in the second context that we believe to be equivalent.
122 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
123
124 /// \brief Queue of declarations in the first context whose equivalence
125 /// with a declaration in the second context still needs to be verified.
126 std::deque<Decl *> DeclsToCheck;
127
Douglas Gregorb4964f72010-02-15 23:54:17 +0000128 /// \brief Declaration (from, to) pairs that are known not to be equivalent
129 /// (which we have already complained about).
130 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
131
Douglas Gregor3996e242010-02-15 22:01:00 +0000132 /// \brief Whether we're being strict about the spelling of types when
133 /// unifying two types.
134 bool StrictTypeSpelling;
135
136 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
137 Diagnostic &Diags,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000138 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000139 bool StrictTypeSpelling = false)
Douglas Gregorb4964f72010-02-15 23:54:17 +0000140 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
141 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000142
143 /// \brief Determine whether the two declarations are structurally
144 /// equivalent.
145 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
146
147 /// \brief Determine whether the two types are structurally equivalent.
148 bool IsStructurallyEquivalent(QualType T1, QualType T2);
149
150 private:
151 /// \brief Finish checking all of the structural equivalences.
152 ///
153 /// \returns true if an error occurred, false otherwise.
154 bool Finish();
155
156 public:
157 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
158 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
159 }
160
161 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
162 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
163 }
164 };
165}
166
167static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
168 QualType T1, QualType T2);
169static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
170 Decl *D1, Decl *D2);
171
172/// \brief Determine if two APInts have the same value, after zero-extending
173/// one of them (if needed!) to ensure that the bit-widths match.
174static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
175 if (I1.getBitWidth() == I2.getBitWidth())
176 return I1 == I2;
177
178 if (I1.getBitWidth() > I2.getBitWidth())
179 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
180
181 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
182}
183
184/// \brief Determine if two APSInts have the same value, zero- or sign-extending
185/// as needed.
186static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
187 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
188 return I1 == I2;
189
190 // Check for a bit-width mismatch.
191 if (I1.getBitWidth() > I2.getBitWidth())
192 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
193 else if (I2.getBitWidth() > I1.getBitWidth())
194 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
195
196 // We have a signedness mismatch. Turn the signed value into an unsigned
197 // value.
198 if (I1.isSigned()) {
199 if (I1.isNegative())
200 return false;
201
202 return llvm::APSInt(I1, true) == I2;
203 }
204
205 if (I2.isNegative())
206 return false;
207
208 return I1 == llvm::APSInt(I2, true);
209}
210
211/// \brief Determine structural equivalence of two expressions.
212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
213 Expr *E1, Expr *E2) {
214 if (!E1 || !E2)
215 return E1 == E2;
216
217 // FIXME: Actually perform a structural comparison!
218 return true;
219}
220
221/// \brief Determine whether two identifiers are equivalent.
222static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
223 const IdentifierInfo *Name2) {
224 if (!Name1 || !Name2)
225 return Name1 == Name2;
226
227 return Name1->getName() == Name2->getName();
228}
229
230/// \brief Determine whether two nested-name-specifiers are equivalent.
231static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
232 NestedNameSpecifier *NNS1,
233 NestedNameSpecifier *NNS2) {
234 // FIXME: Implement!
235 return true;
236}
237
238/// \brief Determine whether two template arguments are equivalent.
239static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
240 const TemplateArgument &Arg1,
241 const TemplateArgument &Arg2) {
242 // FIXME: Implement!
243 return true;
244}
245
246/// \brief Determine structural equivalence for the common part of array
247/// types.
248static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
249 const ArrayType *Array1,
250 const ArrayType *Array2) {
251 if (!IsStructurallyEquivalent(Context,
252 Array1->getElementType(),
253 Array2->getElementType()))
254 return false;
255 if (Array1->getSizeModifier() != Array2->getSizeModifier())
256 return false;
257 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
258 return false;
259
260 return true;
261}
262
263/// \brief Determine structural equivalence of two types.
264static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
265 QualType T1, QualType T2) {
266 if (T1.isNull() || T2.isNull())
267 return T1.isNull() && T2.isNull();
268
269 if (!Context.StrictTypeSpelling) {
270 // We aren't being strict about token-to-token equivalence of types,
271 // so map down to the canonical type.
272 T1 = Context.C1.getCanonicalType(T1);
273 T2 = Context.C2.getCanonicalType(T2);
274 }
275
276 if (T1.getQualifiers() != T2.getQualifiers())
277 return false;
278
Douglas Gregorb4964f72010-02-15 23:54:17 +0000279 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000280
Douglas Gregorb4964f72010-02-15 23:54:17 +0000281 if (T1->getTypeClass() != T2->getTypeClass()) {
282 // Compare function types with prototypes vs. without prototypes as if
283 // both did not have prototypes.
284 if (T1->getTypeClass() == Type::FunctionProto &&
285 T2->getTypeClass() == Type::FunctionNoProto)
286 TC = Type::FunctionNoProto;
287 else if (T1->getTypeClass() == Type::FunctionNoProto &&
288 T2->getTypeClass() == Type::FunctionProto)
289 TC = Type::FunctionNoProto;
290 else
291 return false;
292 }
293
294 switch (TC) {
295 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000296 // FIXME: Deal with Char_S/Char_U.
297 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
298 return false;
299 break;
300
301 case Type::Complex:
302 if (!IsStructurallyEquivalent(Context,
303 cast<ComplexType>(T1)->getElementType(),
304 cast<ComplexType>(T2)->getElementType()))
305 return false;
306 break;
307
308 case Type::Pointer:
309 if (!IsStructurallyEquivalent(Context,
310 cast<PointerType>(T1)->getPointeeType(),
311 cast<PointerType>(T2)->getPointeeType()))
312 return false;
313 break;
314
315 case Type::BlockPointer:
316 if (!IsStructurallyEquivalent(Context,
317 cast<BlockPointerType>(T1)->getPointeeType(),
318 cast<BlockPointerType>(T2)->getPointeeType()))
319 return false;
320 break;
321
322 case Type::LValueReference:
323 case Type::RValueReference: {
324 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
325 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
326 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
327 return false;
328 if (Ref1->isInnerRef() != Ref2->isInnerRef())
329 return false;
330 if (!IsStructurallyEquivalent(Context,
331 Ref1->getPointeeTypeAsWritten(),
332 Ref2->getPointeeTypeAsWritten()))
333 return false;
334 break;
335 }
336
337 case Type::MemberPointer: {
338 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
339 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
340 if (!IsStructurallyEquivalent(Context,
341 MemPtr1->getPointeeType(),
342 MemPtr2->getPointeeType()))
343 return false;
344 if (!IsStructurallyEquivalent(Context,
345 QualType(MemPtr1->getClass(), 0),
346 QualType(MemPtr2->getClass(), 0)))
347 return false;
348 break;
349 }
350
351 case Type::ConstantArray: {
352 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
353 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
354 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
355 return false;
356
357 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
358 return false;
359 break;
360 }
361
362 case Type::IncompleteArray:
363 if (!IsArrayStructurallyEquivalent(Context,
364 cast<ArrayType>(T1),
365 cast<ArrayType>(T2)))
366 return false;
367 break;
368
369 case Type::VariableArray: {
370 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
371 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
372 if (!IsStructurallyEquivalent(Context,
373 Array1->getSizeExpr(), Array2->getSizeExpr()))
374 return false;
375
376 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
377 return false;
378
379 break;
380 }
381
382 case Type::DependentSizedArray: {
383 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
384 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
385 if (!IsStructurallyEquivalent(Context,
386 Array1->getSizeExpr(), Array2->getSizeExpr()))
387 return false;
388
389 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
390 return false;
391
392 break;
393 }
394
395 case Type::DependentSizedExtVector: {
396 const DependentSizedExtVectorType *Vec1
397 = cast<DependentSizedExtVectorType>(T1);
398 const DependentSizedExtVectorType *Vec2
399 = cast<DependentSizedExtVectorType>(T2);
400 if (!IsStructurallyEquivalent(Context,
401 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
402 return false;
403 if (!IsStructurallyEquivalent(Context,
404 Vec1->getElementType(),
405 Vec2->getElementType()))
406 return false;
407 break;
408 }
409
410 case Type::Vector:
411 case Type::ExtVector: {
412 const VectorType *Vec1 = cast<VectorType>(T1);
413 const VectorType *Vec2 = cast<VectorType>(T2);
414 if (!IsStructurallyEquivalent(Context,
415 Vec1->getElementType(),
416 Vec2->getElementType()))
417 return false;
418 if (Vec1->getNumElements() != Vec2->getNumElements())
419 return false;
420 if (Vec1->isAltiVec() != Vec2->isAltiVec())
421 return false;
422 if (Vec1->isPixel() != Vec2->isPixel())
423 return false;
424 }
425
426 case Type::FunctionProto: {
427 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
428 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
429 if (Proto1->getNumArgs() != Proto2->getNumArgs())
430 return false;
431 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
432 if (!IsStructurallyEquivalent(Context,
433 Proto1->getArgType(I),
434 Proto2->getArgType(I)))
435 return false;
436 }
437 if (Proto1->isVariadic() != Proto2->isVariadic())
438 return false;
439 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
440 return false;
441 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
442 return false;
443 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
444 return false;
445 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
446 if (!IsStructurallyEquivalent(Context,
447 Proto1->getExceptionType(I),
448 Proto2->getExceptionType(I)))
449 return false;
450 }
451 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
452 return false;
453
454 // Fall through to check the bits common with FunctionNoProtoType.
455 }
456
457 case Type::FunctionNoProto: {
458 const FunctionType *Function1 = cast<FunctionType>(T1);
459 const FunctionType *Function2 = cast<FunctionType>(T2);
460 if (!IsStructurallyEquivalent(Context,
461 Function1->getResultType(),
462 Function2->getResultType()))
463 return false;
464 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
465 return false;
466 if (Function1->getCallConv() != Function2->getCallConv())
467 return false;
468 break;
469 }
470
471 case Type::UnresolvedUsing:
472 if (!IsStructurallyEquivalent(Context,
473 cast<UnresolvedUsingType>(T1)->getDecl(),
474 cast<UnresolvedUsingType>(T2)->getDecl()))
475 return false;
476
477 break;
478
479 case Type::Typedef:
480 if (!IsStructurallyEquivalent(Context,
481 cast<TypedefType>(T1)->getDecl(),
482 cast<TypedefType>(T2)->getDecl()))
483 return false;
484 break;
485
486 case Type::TypeOfExpr:
487 if (!IsStructurallyEquivalent(Context,
488 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
489 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
490 return false;
491 break;
492
493 case Type::TypeOf:
494 if (!IsStructurallyEquivalent(Context,
495 cast<TypeOfType>(T1)->getUnderlyingType(),
496 cast<TypeOfType>(T2)->getUnderlyingType()))
497 return false;
498 break;
499
500 case Type::Decltype:
501 if (!IsStructurallyEquivalent(Context,
502 cast<DecltypeType>(T1)->getUnderlyingExpr(),
503 cast<DecltypeType>(T2)->getUnderlyingExpr()))
504 return false;
505 break;
506
507 case Type::Record:
508 case Type::Enum:
509 if (!IsStructurallyEquivalent(Context,
510 cast<TagType>(T1)->getDecl(),
511 cast<TagType>(T2)->getDecl()))
512 return false;
513 break;
514
515 case Type::Elaborated: {
516 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
517 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
518 if (Elab1->getTagKind() != Elab2->getTagKind())
519 return false;
520 if (!IsStructurallyEquivalent(Context,
521 Elab1->getUnderlyingType(),
522 Elab2->getUnderlyingType()))
523 return false;
524 break;
525 }
526
527 case Type::TemplateTypeParm: {
528 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
529 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
530 if (Parm1->getDepth() != Parm2->getDepth())
531 return false;
532 if (Parm1->getIndex() != Parm2->getIndex())
533 return false;
534 if (Parm1->isParameterPack() != Parm2->isParameterPack())
535 return false;
536
537 // Names of template type parameters are never significant.
538 break;
539 }
540
541 case Type::SubstTemplateTypeParm: {
542 const SubstTemplateTypeParmType *Subst1
543 = cast<SubstTemplateTypeParmType>(T1);
544 const SubstTemplateTypeParmType *Subst2
545 = cast<SubstTemplateTypeParmType>(T2);
546 if (!IsStructurallyEquivalent(Context,
547 QualType(Subst1->getReplacedParameter(), 0),
548 QualType(Subst2->getReplacedParameter(), 0)))
549 return false;
550 if (!IsStructurallyEquivalent(Context,
551 Subst1->getReplacementType(),
552 Subst2->getReplacementType()))
553 return false;
554 break;
555 }
556
557 case Type::TemplateSpecialization: {
558 const TemplateSpecializationType *Spec1
559 = cast<TemplateSpecializationType>(T1);
560 const TemplateSpecializationType *Spec2
561 = cast<TemplateSpecializationType>(T2);
562 if (!IsStructurallyEquivalent(Context,
563 Spec1->getTemplateName(),
564 Spec2->getTemplateName()))
565 return false;
566 if (Spec1->getNumArgs() != Spec2->getNumArgs())
567 return false;
568 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
569 if (!IsStructurallyEquivalent(Context,
570 Spec1->getArg(I), Spec2->getArg(I)))
571 return false;
572 }
573 break;
574 }
575
576 case Type::QualifiedName: {
577 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
578 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
579 if (!IsStructurallyEquivalent(Context,
580 Qual1->getQualifier(),
581 Qual2->getQualifier()))
582 return false;
583 if (!IsStructurallyEquivalent(Context,
584 Qual1->getNamedType(),
585 Qual2->getNamedType()))
586 return false;
587 break;
588 }
589
590 case Type::Typename: {
591 const TypenameType *Typename1 = cast<TypenameType>(T1);
592 const TypenameType *Typename2 = cast<TypenameType>(T2);
593 if (!IsStructurallyEquivalent(Context,
594 Typename1->getQualifier(),
595 Typename2->getQualifier()))
596 return false;
597 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
598 Typename2->getIdentifier()))
599 return false;
600 if (!IsStructurallyEquivalent(Context,
601 QualType(Typename1->getTemplateId(), 0),
602 QualType(Typename2->getTemplateId(), 0)))
603 return false;
604
605 break;
606 }
607
608 case Type::ObjCInterface: {
609 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
610 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
611 if (!IsStructurallyEquivalent(Context,
612 Iface1->getDecl(), Iface2->getDecl()))
613 return false;
614 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
615 return false;
616 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
617 if (!IsStructurallyEquivalent(Context,
618 Iface1->getProtocol(I),
619 Iface2->getProtocol(I)))
620 return false;
621 }
622 break;
623 }
624
625 case Type::ObjCObjectPointer: {
626 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
627 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
628 if (!IsStructurallyEquivalent(Context,
629 Ptr1->getPointeeType(),
630 Ptr2->getPointeeType()))
631 return false;
632 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
633 return false;
634 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
635 if (!IsStructurallyEquivalent(Context,
636 Ptr1->getProtocol(I),
637 Ptr2->getProtocol(I)))
638 return false;
639 }
640 break;
641 }
642
643 } // end switch
644
645 return true;
646}
647
648/// \brief Determine structural equivalence of two records.
649static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
650 RecordDecl *D1, RecordDecl *D2) {
651 if (D1->isUnion() != D2->isUnion()) {
652 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
653 << Context.C2.getTypeDeclType(D2);
654 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
655 << D1->getDeclName() << (unsigned)D1->getTagKind();
656 return false;
657 }
658
Douglas Gregorb4964f72010-02-15 23:54:17 +0000659 // Compare the definitions of these two records. If either or both are
660 // incomplete, we assume that they are equivalent.
661 D1 = D1->getDefinition();
662 D2 = D2->getDefinition();
663 if (!D1 || !D2)
664 return true;
665
Douglas Gregor3996e242010-02-15 22:01:00 +0000666 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
667 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
668 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
669 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
670 << Context.C2.getTypeDeclType(D2);
671 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
672 << D2CXX->getNumBases();
673 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
674 << D1CXX->getNumBases();
675 return false;
676 }
677
678 // Check the base classes.
679 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
680 BaseEnd1 = D1CXX->bases_end(),
681 Base2 = D2CXX->bases_begin();
682 Base1 != BaseEnd1;
683 ++Base1, ++Base2) {
684 if (!IsStructurallyEquivalent(Context,
685 Base1->getType(), Base2->getType())) {
686 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
687 << Context.C2.getTypeDeclType(D2);
688 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
689 << Base2->getType()
690 << Base2->getSourceRange();
691 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
692 << Base1->getType()
693 << Base1->getSourceRange();
694 return false;
695 }
696
697 // Check virtual vs. non-virtual inheritance mismatch.
698 if (Base1->isVirtual() != Base2->isVirtual()) {
699 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
700 << Context.C2.getTypeDeclType(D2);
701 Context.Diag2(Base2->getSourceRange().getBegin(),
702 diag::note_odr_virtual_base)
703 << Base2->isVirtual() << Base2->getSourceRange();
704 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
705 << Base1->isVirtual()
706 << Base1->getSourceRange();
707 return false;
708 }
709 }
710 } else if (D1CXX->getNumBases() > 0) {
711 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
712 << Context.C2.getTypeDeclType(D2);
713 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
714 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
715 << Base1->getType()
716 << Base1->getSourceRange();
717 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
718 return false;
719 }
720 }
721
722 // Check the fields for consistency.
723 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
724 Field2End = D2->field_end();
725 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
726 Field1End = D1->field_end();
727 Field1 != Field1End;
728 ++Field1, ++Field2) {
729 if (Field2 == Field2End) {
730 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
731 << Context.C2.getTypeDeclType(D2);
732 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
733 << Field1->getDeclName() << Field1->getType();
734 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
735 return false;
736 }
737
738 if (!IsStructurallyEquivalent(Context,
739 Field1->getType(), Field2->getType())) {
740 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
741 << Context.C2.getTypeDeclType(D2);
742 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
743 << Field2->getDeclName() << Field2->getType();
744 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
745 << Field1->getDeclName() << Field1->getType();
746 return false;
747 }
748
749 if (Field1->isBitField() != Field2->isBitField()) {
750 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
751 << Context.C2.getTypeDeclType(D2);
752 if (Field1->isBitField()) {
753 llvm::APSInt Bits;
754 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
755 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
756 << Field1->getDeclName() << Field1->getType()
757 << Bits.toString(10, false);
758 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
759 << Field2->getDeclName();
760 } else {
761 llvm::APSInt Bits;
762 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
763 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
764 << Field2->getDeclName() << Field2->getType()
765 << Bits.toString(10, false);
766 Context.Diag1(Field1->getLocation(),
767 diag::note_odr_not_bit_field)
768 << Field1->getDeclName();
769 }
770 return false;
771 }
772
773 if (Field1->isBitField()) {
774 // Make sure that the bit-fields are the same length.
775 llvm::APSInt Bits1, Bits2;
776 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
777 return false;
778 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
779 return false;
780
781 if (!IsSameValue(Bits1, Bits2)) {
782 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
783 << Context.C2.getTypeDeclType(D2);
784 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
785 << Field2->getDeclName() << Field2->getType()
786 << Bits2.toString(10, false);
787 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
788 << Field1->getDeclName() << Field1->getType()
789 << Bits1.toString(10, false);
790 return false;
791 }
792 }
793 }
794
795 if (Field2 != Field2End) {
796 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
797 << Context.C2.getTypeDeclType(D2);
798 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
799 << Field2->getDeclName() << Field2->getType();
800 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
801 return false;
802 }
803
804 return true;
805}
806
807/// \brief Determine structural equivalence of two enums.
808static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
809 EnumDecl *D1, EnumDecl *D2) {
810 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
811 EC2End = D2->enumerator_end();
812 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
813 EC1End = D1->enumerator_end();
814 EC1 != EC1End; ++EC1, ++EC2) {
815 if (EC2 == EC2End) {
816 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
817 << Context.C2.getTypeDeclType(D2);
818 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
819 << EC1->getDeclName()
820 << EC1->getInitVal().toString(10);
821 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
822 return false;
823 }
824
825 llvm::APSInt Val1 = EC1->getInitVal();
826 llvm::APSInt Val2 = EC2->getInitVal();
827 if (!IsSameValue(Val1, Val2) ||
828 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
829 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
830 << Context.C2.getTypeDeclType(D2);
831 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
832 << EC2->getDeclName()
833 << EC2->getInitVal().toString(10);
834 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
835 << EC1->getDeclName()
836 << EC1->getInitVal().toString(10);
837 return false;
838 }
839 }
840
841 if (EC2 != EC2End) {
842 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
843 << Context.C2.getTypeDeclType(D2);
844 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
845 << EC2->getDeclName()
846 << EC2->getInitVal().toString(10);
847 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
848 return false;
849 }
850
851 return true;
852}
853
854/// \brief Determine structural equivalence of two declarations.
855static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
856 Decl *D1, Decl *D2) {
857 // FIXME: Check for known structural equivalences via a callback of some sort.
858
Douglas Gregorb4964f72010-02-15 23:54:17 +0000859 // Check whether we already know that these two declarations are not
860 // structurally equivalent.
861 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
862 D2->getCanonicalDecl())))
863 return false;
864
Douglas Gregor3996e242010-02-15 22:01:00 +0000865 // Determine whether we've already produced a tentative equivalence for D1.
866 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
867 if (EquivToD1)
868 return EquivToD1 == D2->getCanonicalDecl();
869
870 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
871 EquivToD1 = D2->getCanonicalDecl();
872 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
873 return true;
874}
875
876bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
877 Decl *D2) {
878 if (!::IsStructurallyEquivalent(*this, D1, D2))
879 return false;
880
881 return !Finish();
882}
883
884bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
885 QualType T2) {
886 if (!::IsStructurallyEquivalent(*this, T1, T2))
887 return false;
888
889 return !Finish();
890}
891
892bool StructuralEquivalenceContext::Finish() {
893 while (!DeclsToCheck.empty()) {
894 // Check the next declaration.
895 Decl *D1 = DeclsToCheck.front();
896 DeclsToCheck.pop_front();
897
898 Decl *D2 = TentativeEquivalences[D1];
899 assert(D2 && "Unrecorded tentative equivalence?");
900
Douglas Gregorb4964f72010-02-15 23:54:17 +0000901 bool Equivalent = true;
902
Douglas Gregor3996e242010-02-15 22:01:00 +0000903 // FIXME: Switch on all declaration kinds. For now, we're just going to
904 // check the obvious ones.
905 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
906 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
907 // Check for equivalent structure names.
908 IdentifierInfo *Name1 = Record1->getIdentifier();
909 if (!Name1 && Record1->getTypedefForAnonDecl())
910 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
911 IdentifierInfo *Name2 = Record2->getIdentifier();
912 if (!Name2 && Record2->getTypedefForAnonDecl())
913 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000914 if (!::IsStructurallyEquivalent(Name1, Name2) ||
915 !::IsStructurallyEquivalent(*this, Record1, Record2))
916 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000917 } else {
918 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000919 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000920 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000921 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000922 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
923 // Check for equivalent enum names.
924 IdentifierInfo *Name1 = Enum1->getIdentifier();
925 if (!Name1 && Enum1->getTypedefForAnonDecl())
926 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
927 IdentifierInfo *Name2 = Enum2->getIdentifier();
928 if (!Name2 && Enum2->getTypedefForAnonDecl())
929 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000930 if (!::IsStructurallyEquivalent(Name1, Name2) ||
931 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
932 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000933 } else {
934 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +0000935 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000936 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000937 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000938 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
939 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000940 Typedef2->getIdentifier()) ||
941 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +0000942 Typedef1->getUnderlyingType(),
943 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +0000944 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000945 } else {
946 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000947 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000948 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000949 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000950
951 if (!Equivalent) {
952 // Note that these two declarations are not equivalent (and we already
953 // know about it).
954 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
955 D2->getCanonicalDecl()));
956 return true;
957 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000958 // FIXME: Check other declaration kinds!
959 }
960
961 return false;
962}
963
964//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000965// Import Types
966//----------------------------------------------------------------------------
967
Douglas Gregore4c83e42010-02-09 22:48:33 +0000968QualType ASTNodeImporter::VisitType(Type *T) {
969 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
970 << T->getTypeClassName();
971 return QualType();
972}
973
Douglas Gregor96e578d2010-02-05 17:54:41 +0000974QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
975 switch (T->getKind()) {
976 case BuiltinType::Void: return Importer.getToContext().VoidTy;
977 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
978
979 case BuiltinType::Char_U:
980 // The context we're importing from has an unsigned 'char'. If we're
981 // importing into a context with a signed 'char', translate to
982 // 'unsigned char' instead.
983 if (Importer.getToContext().getLangOptions().CharIsSigned)
984 return Importer.getToContext().UnsignedCharTy;
985
986 return Importer.getToContext().CharTy;
987
988 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
989
990 case BuiltinType::Char16:
991 // FIXME: Make sure that the "to" context supports C++!
992 return Importer.getToContext().Char16Ty;
993
994 case BuiltinType::Char32:
995 // FIXME: Make sure that the "to" context supports C++!
996 return Importer.getToContext().Char32Ty;
997
998 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
999 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1000 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1001 case BuiltinType::ULongLong:
1002 return Importer.getToContext().UnsignedLongLongTy;
1003 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1004
1005 case BuiltinType::Char_S:
1006 // The context we're importing from has an unsigned 'char'. If we're
1007 // importing into a context with a signed 'char', translate to
1008 // 'unsigned char' instead.
1009 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1010 return Importer.getToContext().SignedCharTy;
1011
1012 return Importer.getToContext().CharTy;
1013
1014 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1015 case BuiltinType::WChar:
1016 // FIXME: If not in C++, shall we translate to the C equivalent of
1017 // wchar_t?
1018 return Importer.getToContext().WCharTy;
1019
1020 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1021 case BuiltinType::Int : return Importer.getToContext().IntTy;
1022 case BuiltinType::Long : return Importer.getToContext().LongTy;
1023 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1024 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1025 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1026 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1027 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1028
1029 case BuiltinType::NullPtr:
1030 // FIXME: Make sure that the "to" context supports C++0x!
1031 return Importer.getToContext().NullPtrTy;
1032
1033 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1034 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1035 case BuiltinType::UndeducedAuto:
1036 // FIXME: Make sure that the "to" context supports C++0x!
1037 return Importer.getToContext().UndeducedAutoTy;
1038
1039 case BuiltinType::ObjCId:
1040 // FIXME: Make sure that the "to" context supports Objective-C!
1041 return Importer.getToContext().ObjCBuiltinIdTy;
1042
1043 case BuiltinType::ObjCClass:
1044 return Importer.getToContext().ObjCBuiltinClassTy;
1045
1046 case BuiltinType::ObjCSel:
1047 return Importer.getToContext().ObjCBuiltinSelTy;
1048 }
1049
1050 return QualType();
1051}
1052
1053QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1054 QualType ToElementType = Importer.Import(T->getElementType());
1055 if (ToElementType.isNull())
1056 return QualType();
1057
1058 return Importer.getToContext().getComplexType(ToElementType);
1059}
1060
1061QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1062 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1063 if (ToPointeeType.isNull())
1064 return QualType();
1065
1066 return Importer.getToContext().getPointerType(ToPointeeType);
1067}
1068
1069QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1070 // FIXME: Check for blocks support in "to" context.
1071 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1072 if (ToPointeeType.isNull())
1073 return QualType();
1074
1075 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1076}
1077
1078QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1079 // FIXME: Check for C++ support in "to" context.
1080 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1081 if (ToPointeeType.isNull())
1082 return QualType();
1083
1084 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1085}
1086
1087QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1088 // FIXME: Check for C++0x support in "to" context.
1089 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1090 if (ToPointeeType.isNull())
1091 return QualType();
1092
1093 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1094}
1095
1096QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1097 // FIXME: Check for C++ support in "to" context.
1098 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1099 if (ToPointeeType.isNull())
1100 return QualType();
1101
1102 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1103 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1104 ClassType.getTypePtr());
1105}
1106
1107QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1108 QualType ToElementType = Importer.Import(T->getElementType());
1109 if (ToElementType.isNull())
1110 return QualType();
1111
1112 return Importer.getToContext().getConstantArrayType(ToElementType,
1113 T->getSize(),
1114 T->getSizeModifier(),
1115 T->getIndexTypeCVRQualifiers());
1116}
1117
1118QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1119 QualType ToElementType = Importer.Import(T->getElementType());
1120 if (ToElementType.isNull())
1121 return QualType();
1122
1123 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1124 T->getSizeModifier(),
1125 T->getIndexTypeCVRQualifiers());
1126}
1127
1128QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1129 QualType ToElementType = Importer.Import(T->getElementType());
1130 if (ToElementType.isNull())
1131 return QualType();
1132
1133 Expr *Size = Importer.Import(T->getSizeExpr());
1134 if (!Size)
1135 return QualType();
1136
1137 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1138 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1139 T->getSizeModifier(),
1140 T->getIndexTypeCVRQualifiers(),
1141 Brackets);
1142}
1143
1144QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1145 QualType ToElementType = Importer.Import(T->getElementType());
1146 if (ToElementType.isNull())
1147 return QualType();
1148
1149 return Importer.getToContext().getVectorType(ToElementType,
1150 T->getNumElements(),
1151 T->isAltiVec(),
1152 T->isPixel());
1153}
1154
1155QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1156 QualType ToElementType = Importer.Import(T->getElementType());
1157 if (ToElementType.isNull())
1158 return QualType();
1159
1160 return Importer.getToContext().getExtVectorType(ToElementType,
1161 T->getNumElements());
1162}
1163
1164QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1165 // FIXME: What happens if we're importing a function without a prototype
1166 // into C++? Should we make it variadic?
1167 QualType ToResultType = Importer.Import(T->getResultType());
1168 if (ToResultType.isNull())
1169 return QualType();
1170
1171 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1172 T->getNoReturnAttr(),
1173 T->getCallConv());
1174}
1175
1176QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1177 QualType ToResultType = Importer.Import(T->getResultType());
1178 if (ToResultType.isNull())
1179 return QualType();
1180
1181 // Import argument types
1182 llvm::SmallVector<QualType, 4> ArgTypes;
1183 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1184 AEnd = T->arg_type_end();
1185 A != AEnd; ++A) {
1186 QualType ArgType = Importer.Import(*A);
1187 if (ArgType.isNull())
1188 return QualType();
1189 ArgTypes.push_back(ArgType);
1190 }
1191
1192 // Import exception types
1193 llvm::SmallVector<QualType, 4> ExceptionTypes;
1194 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1195 EEnd = T->exception_end();
1196 E != EEnd; ++E) {
1197 QualType ExceptionType = Importer.Import(*E);
1198 if (ExceptionType.isNull())
1199 return QualType();
1200 ExceptionTypes.push_back(ExceptionType);
1201 }
1202
1203 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1204 ArgTypes.size(),
1205 T->isVariadic(),
1206 T->getTypeQuals(),
1207 T->hasExceptionSpec(),
1208 T->hasAnyExceptionSpec(),
1209 ExceptionTypes.size(),
1210 ExceptionTypes.data(),
1211 T->getNoReturnAttr(),
1212 T->getCallConv());
1213}
1214
1215QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1216 TypedefDecl *ToDecl
1217 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1218 if (!ToDecl)
1219 return QualType();
1220
1221 return Importer.getToContext().getTypeDeclType(ToDecl);
1222}
1223
1224QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1225 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1226 if (!ToExpr)
1227 return QualType();
1228
1229 return Importer.getToContext().getTypeOfExprType(ToExpr);
1230}
1231
1232QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1233 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1234 if (ToUnderlyingType.isNull())
1235 return QualType();
1236
1237 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1238}
1239
1240QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1241 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1242 if (!ToExpr)
1243 return QualType();
1244
1245 return Importer.getToContext().getDecltypeType(ToExpr);
1246}
1247
1248QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1249 RecordDecl *ToDecl
1250 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1251 if (!ToDecl)
1252 return QualType();
1253
1254 return Importer.getToContext().getTagDeclType(ToDecl);
1255}
1256
1257QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1258 EnumDecl *ToDecl
1259 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1260 if (!ToDecl)
1261 return QualType();
1262
1263 return Importer.getToContext().getTagDeclType(ToDecl);
1264}
1265
1266QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1267 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1268 if (ToUnderlyingType.isNull())
1269 return QualType();
1270
1271 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1272 T->getTagKind());
1273}
1274
1275QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1276 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1277 if (!ToQualifier)
1278 return QualType();
1279
1280 QualType ToNamedType = Importer.Import(T->getNamedType());
1281 if (ToNamedType.isNull())
1282 return QualType();
1283
1284 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1285}
1286
1287QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1288 ObjCInterfaceDecl *Class
1289 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1290 if (!Class)
1291 return QualType();
1292
1293 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1294 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1295 PEnd = T->qual_end();
1296 P != PEnd; ++P) {
1297 ObjCProtocolDecl *Protocol
1298 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1299 if (!Protocol)
1300 return QualType();
1301 Protocols.push_back(Protocol);
1302 }
1303
1304 return Importer.getToContext().getObjCInterfaceType(Class,
1305 Protocols.data(),
1306 Protocols.size());
1307}
1308
1309QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1310 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1311 if (ToPointeeType.isNull())
1312 return QualType();
1313
1314 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1315 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1316 PEnd = T->qual_end();
1317 P != PEnd; ++P) {
1318 ObjCProtocolDecl *Protocol
1319 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1320 if (!Protocol)
1321 return QualType();
1322 Protocols.push_back(Protocol);
1323 }
1324
1325 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1326 Protocols.data(),
1327 Protocols.size());
1328}
1329
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001330//----------------------------------------------------------------------------
1331// Import Declarations
1332//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001333bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1334 DeclContext *&LexicalDC,
1335 DeclarationName &Name,
1336 SourceLocation &Loc) {
1337 // Import the context of this declaration.
1338 DC = Importer.ImportContext(D->getDeclContext());
1339 if (!DC)
1340 return true;
1341
1342 LexicalDC = DC;
1343 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1344 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1345 if (!LexicalDC)
1346 return true;
1347 }
1348
1349 // Import the name of this declaration.
1350 Name = Importer.Import(D->getDeclName());
1351 if (D->getDeclName() && !Name)
1352 return true;
1353
1354 // Import the location of this declaration.
1355 Loc = Importer.Import(D->getLocation());
1356 return false;
1357}
1358
Douglas Gregor5c73e912010-02-11 00:48:18 +00001359bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001360 RecordDecl *ToRecord) {
1361 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1362 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001363 Importer.getDiags(),
1364 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001365 return SEC.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001366}
1367
Douglas Gregor98c10182010-02-12 22:17:39 +00001368bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001369 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1370 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001371 Importer.getDiags(),
1372 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001373 return SEC.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001374}
1375
Douglas Gregore4c83e42010-02-09 22:48:33 +00001376Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001377 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001378 << D->getDeclKindName();
1379 return 0;
1380}
1381
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001382Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1383 // Import the major distinguishing characteristics of this typedef.
1384 DeclContext *DC, *LexicalDC;
1385 DeclarationName Name;
1386 SourceLocation Loc;
1387 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1388 return 0;
1389
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001390 // If this typedef is not in block scope, determine whether we've
1391 // seen a typedef with the same name (that we can merge with) or any
1392 // other entity by that name (which name lookup could conflict with).
1393 if (!DC->isFunctionOrMethod()) {
1394 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1395 unsigned IDNS = Decl::IDNS_Ordinary;
1396 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1397 Lookup.first != Lookup.second;
1398 ++Lookup.first) {
1399 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1400 continue;
1401 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001402 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1403 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001404 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001405 }
1406
1407 ConflictingDecls.push_back(*Lookup.first);
1408 }
1409
1410 if (!ConflictingDecls.empty()) {
1411 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1412 ConflictingDecls.data(),
1413 ConflictingDecls.size());
1414 if (!Name)
1415 return 0;
1416 }
1417 }
1418
Douglas Gregorb4964f72010-02-15 23:54:17 +00001419 // Import the underlying type of this typedef;
1420 QualType T = Importer.Import(D->getUnderlyingType());
1421 if (T.isNull())
1422 return 0;
1423
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001424 // Create the new typedef node.
1425 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1426 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1427 Loc, Name.getAsIdentifierInfo(),
1428 TInfo);
1429 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001430 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001431 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001432
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001433 return ToTypedef;
1434}
1435
Douglas Gregor98c10182010-02-12 22:17:39 +00001436Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1437 // Import the major distinguishing characteristics of this enum.
1438 DeclContext *DC, *LexicalDC;
1439 DeclarationName Name;
1440 SourceLocation Loc;
1441 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1442 return 0;
1443
1444 // Figure out what enum name we're looking for.
1445 unsigned IDNS = Decl::IDNS_Tag;
1446 DeclarationName SearchName = Name;
1447 if (!SearchName && D->getTypedefForAnonDecl()) {
1448 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1449 IDNS = Decl::IDNS_Ordinary;
1450 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1451 IDNS |= Decl::IDNS_Ordinary;
1452
1453 // We may already have an enum of the same name; try to find and match it.
1454 if (!DC->isFunctionOrMethod() && SearchName) {
1455 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1456 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1457 Lookup.first != Lookup.second;
1458 ++Lookup.first) {
1459 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1460 continue;
1461
1462 Decl *Found = *Lookup.first;
1463 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1464 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1465 Found = Tag->getDecl();
1466 }
1467
1468 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001469 if (IsStructuralMatch(D, FoundEnum))
1470 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001471 }
1472
1473 ConflictingDecls.push_back(*Lookup.first);
1474 }
1475
1476 if (!ConflictingDecls.empty()) {
1477 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1478 ConflictingDecls.data(),
1479 ConflictingDecls.size());
1480 }
1481 }
1482
1483 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001484 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001485 Name.getAsIdentifierInfo(),
1486 Importer.Import(D->getTagKeywordLoc()),
1487 0);
Douglas Gregor3996e242010-02-15 22:01:00 +00001488 D2->setLexicalDeclContext(LexicalDC);
1489 Importer.Imported(D, D2);
1490 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001491
1492 // Import the integer type.
1493 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1494 if (ToIntegerType.isNull())
1495 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001496 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001497
1498 // Import the definition
1499 if (D->isDefinition()) {
1500 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1501 if (T.isNull())
1502 return 0;
1503
1504 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1505 if (ToPromotionType.isNull())
1506 return 0;
1507
Douglas Gregor3996e242010-02-15 22:01:00 +00001508 D2->startDefinition();
Douglas Gregor98c10182010-02-12 22:17:39 +00001509 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1510 FromMemEnd = D->decls_end();
1511 FromMem != FromMemEnd;
1512 ++FromMem)
1513 Importer.Import(*FromMem);
1514
Douglas Gregor3996e242010-02-15 22:01:00 +00001515 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001516 }
1517
Douglas Gregor3996e242010-02-15 22:01:00 +00001518 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001519}
1520
Douglas Gregor5c73e912010-02-11 00:48:18 +00001521Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1522 // If this record has a definition in the translation unit we're coming from,
1523 // but this particular declaration is not that definition, import the
1524 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001525 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001526 if (Definition && Definition != D) {
1527 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001528 if (!ImportedDef)
1529 return 0;
1530
1531 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001532 }
1533
1534 // Import the major distinguishing characteristics of this record.
1535 DeclContext *DC, *LexicalDC;
1536 DeclarationName Name;
1537 SourceLocation Loc;
1538 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1539 return 0;
1540
1541 // Figure out what structure name we're looking for.
1542 unsigned IDNS = Decl::IDNS_Tag;
1543 DeclarationName SearchName = Name;
1544 if (!SearchName && D->getTypedefForAnonDecl()) {
1545 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1546 IDNS = Decl::IDNS_Ordinary;
1547 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1548 IDNS |= Decl::IDNS_Ordinary;
1549
1550 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001551 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001552 if (!DC->isFunctionOrMethod() && SearchName) {
1553 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1554 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1555 Lookup.first != Lookup.second;
1556 ++Lookup.first) {
1557 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1558 continue;
1559
1560 Decl *Found = *Lookup.first;
1561 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1562 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1563 Found = Tag->getDecl();
1564 }
1565
1566 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001567 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1568 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1569 // The record types structurally match, or the "from" translation
1570 // unit only had a forward declaration anyway; call it the same
1571 // function.
1572 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001573 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001574 }
1575 } else {
1576 // We have a forward declaration of this type, so adopt that forward
1577 // declaration rather than building a new one.
1578 AdoptDecl = FoundRecord;
1579 continue;
1580 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001581 }
1582
1583 ConflictingDecls.push_back(*Lookup.first);
1584 }
1585
1586 if (!ConflictingDecls.empty()) {
1587 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1588 ConflictingDecls.data(),
1589 ConflictingDecls.size());
1590 }
1591 }
1592
1593 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001594 RecordDecl *D2 = AdoptDecl;
1595 if (!D2) {
1596 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1597 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001598 D->getTagKind(),
1599 DC, Loc,
1600 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001601 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001602 D2 = D2CXX;
Douglas Gregor25791052010-02-12 00:09:27 +00001603
1604 if (D->isDefinition()) {
1605 // Add base classes.
1606 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1607 for (CXXRecordDecl::base_class_iterator
Douglas Gregor3996e242010-02-15 22:01:00 +00001608 Base1 = D1CXX->bases_begin(),
1609 FromBaseEnd = D1CXX->bases_end();
1610 Base1 != FromBaseEnd;
1611 ++Base1) {
1612 QualType T = Importer.Import(Base1->getType());
Douglas Gregor25791052010-02-12 00:09:27 +00001613 if (T.isNull())
1614 return 0;
1615
1616 Bases.push_back(
1617 new (Importer.getToContext())
Douglas Gregor3996e242010-02-15 22:01:00 +00001618 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1619 Base1->isVirtual(),
1620 Base1->isBaseOfClass(),
1621 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001622 T));
Douglas Gregor25791052010-02-12 00:09:27 +00001623 }
1624 if (!Bases.empty())
Douglas Gregor3996e242010-02-15 22:01:00 +00001625 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001626 }
Douglas Gregor25791052010-02-12 00:09:27 +00001627 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001628 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001629 DC, Loc,
1630 Name.getAsIdentifierInfo(),
1631 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001632 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001633 D2->setLexicalDeclContext(LexicalDC);
1634 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001635 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001636
Douglas Gregor3996e242010-02-15 22:01:00 +00001637 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001638
Douglas Gregor5c73e912010-02-11 00:48:18 +00001639 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001640 D2->startDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001641 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1642 FromMemEnd = D->decls_end();
1643 FromMem != FromMemEnd;
1644 ++FromMem)
1645 Importer.Import(*FromMem);
1646
Douglas Gregor3996e242010-02-15 22:01:00 +00001647 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001648 }
1649
Douglas Gregor3996e242010-02-15 22:01:00 +00001650 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001651}
1652
Douglas Gregor98c10182010-02-12 22:17:39 +00001653Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1654 // Import the major distinguishing characteristics of this enumerator.
1655 DeclContext *DC, *LexicalDC;
1656 DeclarationName Name;
1657 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001658 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001659 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001660
1661 QualType T = Importer.Import(D->getType());
1662 if (T.isNull())
1663 return 0;
1664
Douglas Gregor98c10182010-02-12 22:17:39 +00001665 // Determine whether there are any other declarations with the same name and
1666 // in the same context.
1667 if (!LexicalDC->isFunctionOrMethod()) {
1668 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1669 unsigned IDNS = Decl::IDNS_Ordinary;
1670 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1671 Lookup.first != Lookup.second;
1672 ++Lookup.first) {
1673 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1674 continue;
1675
1676 ConflictingDecls.push_back(*Lookup.first);
1677 }
1678
1679 if (!ConflictingDecls.empty()) {
1680 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1681 ConflictingDecls.data(),
1682 ConflictingDecls.size());
1683 if (!Name)
1684 return 0;
1685 }
1686 }
1687
1688 Expr *Init = Importer.Import(D->getInitExpr());
1689 if (D->getInitExpr() && !Init)
1690 return 0;
1691
1692 EnumConstantDecl *ToEnumerator
1693 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1694 Name.getAsIdentifierInfo(), T,
1695 Init, D->getInitVal());
1696 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001697 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001698 LexicalDC->addDecl(ToEnumerator);
1699 return ToEnumerator;
1700}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001701
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001702Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1703 // Import the major distinguishing characteristics of this function.
1704 DeclContext *DC, *LexicalDC;
1705 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001706 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001707 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001708 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001709
1710 // Try to find a function in our own ("to") context with the same name, same
1711 // type, and in the same context as the function we're importing.
1712 if (!LexicalDC->isFunctionOrMethod()) {
1713 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1714 unsigned IDNS = Decl::IDNS_Ordinary;
1715 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1716 Lookup.first != Lookup.second;
1717 ++Lookup.first) {
1718 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1719 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001720
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001721 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1722 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1723 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001724 if (Importer.IsStructurallyEquivalent(D->getType(),
1725 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001726 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001727 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001728 }
1729
1730 // FIXME: Check for overloading more carefully, e.g., by boosting
1731 // Sema::IsOverload out to the AST library.
1732
1733 // Function overloading is okay in C++.
1734 if (Importer.getToContext().getLangOptions().CPlusPlus)
1735 continue;
1736
1737 // Complain about inconsistent function types.
1738 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001739 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001740 Importer.ToDiag(FoundFunction->getLocation(),
1741 diag::note_odr_value_here)
1742 << FoundFunction->getType();
1743 }
1744 }
1745
1746 ConflictingDecls.push_back(*Lookup.first);
1747 }
1748
1749 if (!ConflictingDecls.empty()) {
1750 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1751 ConflictingDecls.data(),
1752 ConflictingDecls.size());
1753 if (!Name)
1754 return 0;
1755 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001756 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001757
1758 // Import the type.
1759 QualType T = Importer.Import(D->getType());
1760 if (T.isNull())
1761 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001762
1763 // Import the function parameters.
1764 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1765 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1766 P != PEnd; ++P) {
1767 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1768 if (!ToP)
1769 return 0;
1770
1771 Parameters.push_back(ToP);
1772 }
1773
1774 // Create the imported function.
1775 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor98c10182010-02-12 22:17:39 +00001776 FunctionDecl *ToEnumerator
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001777 = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1778 Name, T, TInfo, D->getStorageClass(),
1779 D->isInlineSpecified(),
1780 D->hasWrittenPrototype());
Douglas Gregor98c10182010-02-12 22:17:39 +00001781 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001782 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001783 LexicalDC->addDecl(ToEnumerator);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001784
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001785 // Set the parameters.
1786 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor98c10182010-02-12 22:17:39 +00001787 Parameters[I]->setOwningFunction(ToEnumerator);
1788 ToEnumerator->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001789 }
Douglas Gregor98c10182010-02-12 22:17:39 +00001790 ToEnumerator->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001791
1792 // FIXME: Other bits to merge?
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001793
Douglas Gregor98c10182010-02-12 22:17:39 +00001794 return ToEnumerator;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001795}
1796
Douglas Gregor5c73e912010-02-11 00:48:18 +00001797Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1798 // Import the major distinguishing characteristics of a variable.
1799 DeclContext *DC, *LexicalDC;
1800 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001801 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001802 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1803 return 0;
1804
1805 // Import the type.
1806 QualType T = Importer.Import(D->getType());
1807 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00001808 return 0;
1809
1810 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1811 Expr *BitWidth = Importer.Import(D->getBitWidth());
1812 if (!BitWidth && D->getBitWidth())
1813 return 0;
1814
1815 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1816 Loc, Name.getAsIdentifierInfo(),
1817 T, TInfo, BitWidth, D->isMutable());
1818 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001819 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001820 LexicalDC->addDecl(ToField);
1821 return ToField;
1822}
1823
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001824Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1825 // Import the major distinguishing characteristics of a variable.
1826 DeclContext *DC, *LexicalDC;
1827 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001828 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001829 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001830 return 0;
1831
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001832 // Try to find a variable in our own ("to") context with the same name and
1833 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00001834 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001835 VarDecl *MergeWithVar = 0;
1836 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1837 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001838 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001839 Lookup.first != Lookup.second;
1840 ++Lookup.first) {
1841 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1842 continue;
1843
1844 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1845 // We have found a variable that we may need to merge with. Check it.
1846 if (isExternalLinkage(FoundVar->getLinkage()) &&
1847 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001848 if (Importer.IsStructurallyEquivalent(D->getType(),
1849 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001850 MergeWithVar = FoundVar;
1851 break;
1852 }
1853
Douglas Gregor56521c52010-02-12 17:23:39 +00001854 const ArrayType *FoundArray
1855 = Importer.getToContext().getAsArrayType(FoundVar->getType());
1856 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00001857 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00001858 if (FoundArray && TArray) {
1859 if (isa<IncompleteArrayType>(FoundArray) &&
1860 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001861 // Import the type.
1862 QualType T = Importer.Import(D->getType());
1863 if (T.isNull())
1864 return 0;
1865
Douglas Gregor56521c52010-02-12 17:23:39 +00001866 FoundVar->setType(T);
1867 MergeWithVar = FoundVar;
1868 break;
1869 } else if (isa<IncompleteArrayType>(TArray) &&
1870 isa<ConstantArrayType>(FoundArray)) {
1871 MergeWithVar = FoundVar;
1872 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00001873 }
1874 }
1875
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001876 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001877 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001878 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1879 << FoundVar->getType();
1880 }
1881 }
1882
1883 ConflictingDecls.push_back(*Lookup.first);
1884 }
1885
1886 if (MergeWithVar) {
1887 // An equivalent variable with external linkage has been found. Link
1888 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001889 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001890
1891 if (VarDecl *DDef = D->getDefinition()) {
1892 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1893 Importer.ToDiag(ExistingDef->getLocation(),
1894 diag::err_odr_variable_multiple_def)
1895 << Name;
1896 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1897 } else {
1898 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00001899 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001900 }
1901 }
1902
1903 return MergeWithVar;
1904 }
1905
1906 if (!ConflictingDecls.empty()) {
1907 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1908 ConflictingDecls.data(),
1909 ConflictingDecls.size());
1910 if (!Name)
1911 return 0;
1912 }
1913 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001914
Douglas Gregorb4964f72010-02-15 23:54:17 +00001915 // Import the type.
1916 QualType T = Importer.Import(D->getType());
1917 if (T.isNull())
1918 return 0;
1919
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001920 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001921 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001922 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1923 Name.getAsIdentifierInfo(), T, TInfo,
1924 D->getStorageClass());
Douglas Gregor62d311f2010-02-09 19:21:46 +00001925 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001926 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001927 LexicalDC->addDecl(ToVar);
1928
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001929 // Merge the initializer.
1930 // FIXME: Can we really import any initializer? Alternatively, we could force
1931 // ourselves to import every declaration of a variable and then only use
1932 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00001933 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001934
1935 // FIXME: Other bits to merge?
1936
1937 return ToVar;
1938}
1939
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001940Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
1941 // Parameters are created in the translation unit's context, then moved
1942 // into the function declaration's context afterward.
1943 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
1944
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001945 // Import the name of this declaration.
1946 DeclarationName Name = Importer.Import(D->getDeclName());
1947 if (D->getDeclName() && !Name)
1948 return 0;
1949
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001950 // Import the location of this declaration.
1951 SourceLocation Loc = Importer.Import(D->getLocation());
1952
1953 // Import the parameter's type.
1954 QualType T = Importer.Import(D->getType());
1955 if (T.isNull())
1956 return 0;
1957
1958 // Create the imported parameter.
1959 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1960 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
1961 Loc, Name.getAsIdentifierInfo(),
1962 T, TInfo, D->getStorageClass(),
1963 /*FIXME: Default argument*/ 0);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001964 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001965}
1966
Douglas Gregor45635322010-02-16 01:20:57 +00001967Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
1968 // Import the major distinguishing characteristics of an @interface.
1969 DeclContext *DC, *LexicalDC;
1970 DeclarationName Name;
1971 SourceLocation Loc;
1972 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1973 return 0;
1974
1975 ObjCInterfaceDecl *MergeWithIface = 0;
1976 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1977 Lookup.first != Lookup.second;
1978 ++Lookup.first) {
1979 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1980 continue;
1981
1982 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
1983 break;
1984 }
1985
1986 ObjCInterfaceDecl *ToIface = MergeWithIface;
1987 if (!ToIface || ToIface->isForwardDecl()) {
1988 if (!ToIface) {
1989 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
1990 DC, Loc,
1991 Name.getAsIdentifierInfo(),
1992 Importer.Import(D->getClassLoc()),
1993 D->isForwardDecl(),
1994 D->isImplicitInterfaceDecl());
1995 ToIface->setLexicalDeclContext(LexicalDC);
1996 LexicalDC->addDecl(ToIface);
1997 }
1998 Importer.Imported(D, ToIface);
1999
2000 // Import superclass
2001 // FIXME: If we're merging, make sure that both decls have the same
2002 // superclass.
2003 if (D->getSuperClass()) {
2004 ObjCInterfaceDecl *Super
2005 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2006 if (!Super)
2007 return 0;
2008
2009 ToIface->setSuperClass(Super);
2010 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2011 }
2012
2013 // Import protocols
2014 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2015 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2016 ObjCInterfaceDecl::protocol_loc_iterator
2017 FromProtoLoc = D->protocol_loc_begin();
2018 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2019 FromProtoEnd = D->protocol_end();
2020 FromProto != FromProtoEnd;
2021 ++FromProto, ++FromProtoLoc) {
2022 ObjCProtocolDecl *ToProto
2023 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2024 if (!ToProto)
2025 return 0;
2026 Protocols.push_back(ToProto);
2027 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2028 }
2029
2030 // FIXME: If we're merging, make sure that the protocol list is the same.
2031 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2032 ProtocolLocs.data(), Importer.getToContext());
2033
2034 // FIXME: Import categories
2035
2036 // Import @end range
2037 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2038 } else {
2039 Importer.Imported(D, ToIface);
2040 }
2041
2042 // Import all of the members of this class.
2043 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2044 FromMemEnd = D->decls_end();
2045 FromMem != FromMemEnd;
2046 ++FromMem)
2047 Importer.Import(*FromMem);
2048
2049 // If we have an @implementation, import it as well.
2050 if (D->getImplementation()) {
2051 ObjCImplementationDecl *Impl
2052 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2053 if (!Impl)
2054 return 0;
2055
2056 ToIface->setImplementation(Impl);
2057 }
2058
2059 return 0;
2060}
2061
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002062//----------------------------------------------------------------------------
2063// Import Statements
2064//----------------------------------------------------------------------------
2065
2066Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2067 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2068 << S->getStmtClassName();
2069 return 0;
2070}
2071
2072//----------------------------------------------------------------------------
2073// Import Expressions
2074//----------------------------------------------------------------------------
2075Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2076 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2077 << E->getStmtClassName();
2078 return 0;
2079}
2080
2081Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2082 QualType T = Importer.Import(E->getType());
2083 if (T.isNull())
2084 return 0;
2085
2086 return new (Importer.getToContext())
2087 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2088}
2089
Douglas Gregor98c10182010-02-12 22:17:39 +00002090Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2091 QualType T = Importer.Import(E->getType());
2092 if (T.isNull())
2093 return 0;
2094
2095 Expr *SubExpr = Importer.Import(E->getSubExpr());
2096 if (!SubExpr)
2097 return 0;
2098
2099 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2100 SubExpr,
2101 E->isLvalueCast());
2102}
2103
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002104ASTImporter::ASTImporter(Diagnostic &Diags,
2105 ASTContext &ToContext, FileManager &ToFileManager,
2106 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00002107 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00002108 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002109 Diags(Diags) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00002110 ImportedDecls[FromContext.getTranslationUnitDecl()]
2111 = ToContext.getTranslationUnitDecl();
2112}
2113
2114ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002115
2116QualType ASTImporter::Import(QualType FromT) {
2117 if (FromT.isNull())
2118 return QualType();
2119
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002120 // Check whether we've already imported this type.
2121 llvm::DenseMap<Type *, Type *>::iterator Pos
2122 = ImportedTypes.find(FromT.getTypePtr());
2123 if (Pos != ImportedTypes.end())
2124 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002125
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002126 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00002127 ASTNodeImporter Importer(*this);
2128 QualType ToT = Importer.Visit(FromT.getTypePtr());
2129 if (ToT.isNull())
2130 return ToT;
2131
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002132 // Record the imported type.
2133 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2134
Douglas Gregor96e578d2010-02-05 17:54:41 +00002135 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2136}
2137
Douglas Gregor62d311f2010-02-09 19:21:46 +00002138TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002139 if (!FromTSI)
2140 return FromTSI;
2141
2142 // FIXME: For now we just create a "trivial" type source info based
2143 // on the type and a seingle location. Implement a real version of
2144 // this.
2145 QualType T = Import(FromTSI->getType());
2146 if (T.isNull())
2147 return 0;
2148
2149 return ToContext.getTrivialTypeSourceInfo(T,
2150 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002151}
2152
2153Decl *ASTImporter::Import(Decl *FromD) {
2154 if (!FromD)
2155 return 0;
2156
2157 // Check whether we've already imported this declaration.
2158 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2159 if (Pos != ImportedDecls.end())
2160 return Pos->second;
2161
2162 // Import the type
2163 ASTNodeImporter Importer(*this);
2164 Decl *ToD = Importer.Visit(FromD);
2165 if (!ToD)
2166 return 0;
2167
2168 // Record the imported declaration.
2169 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002170
2171 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2172 // Keep track of anonymous tags that have an associated typedef.
2173 if (FromTag->getTypedefForAnonDecl())
2174 AnonTagsWithPendingTypedefs.push_back(FromTag);
2175 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2176 // When we've finished transforming a typedef, see whether it was the
2177 // typedef for an anonymous tag.
2178 for (llvm::SmallVector<TagDecl *, 4>::iterator
2179 FromTag = AnonTagsWithPendingTypedefs.begin(),
2180 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2181 FromTag != FromTagEnd; ++FromTag) {
2182 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2183 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2184 // We found the typedef for an anonymous tag; link them.
2185 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2186 AnonTagsWithPendingTypedefs.erase(FromTag);
2187 break;
2188 }
2189 }
2190 }
2191 }
2192
Douglas Gregor62d311f2010-02-09 19:21:46 +00002193 return ToD;
2194}
2195
2196DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
2197 if (!FromDC)
2198 return FromDC;
2199
2200 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
2201}
2202
2203Expr *ASTImporter::Import(Expr *FromE) {
2204 if (!FromE)
2205 return 0;
2206
2207 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
2208}
2209
2210Stmt *ASTImporter::Import(Stmt *FromS) {
2211 if (!FromS)
2212 return 0;
2213
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002214 // Check whether we've already imported this declaration.
2215 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
2216 if (Pos != ImportedStmts.end())
2217 return Pos->second;
2218
2219 // Import the type
2220 ASTNodeImporter Importer(*this);
2221 Stmt *ToS = Importer.Visit(FromS);
2222 if (!ToS)
2223 return 0;
2224
2225 // Record the imported declaration.
2226 ImportedStmts[FromS] = ToS;
2227 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002228}
2229
2230NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
2231 if (!FromNNS)
2232 return 0;
2233
2234 // FIXME: Implement!
2235 return 0;
2236}
2237
2238SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
2239 if (FromLoc.isInvalid())
2240 return SourceLocation();
2241
Douglas Gregor811663e2010-02-10 00:15:17 +00002242 SourceManager &FromSM = FromContext.getSourceManager();
2243
2244 // For now, map everything down to its spelling location, so that we
2245 // don't have to import macro instantiations.
2246 // FIXME: Import macro instantiations!
2247 FromLoc = FromSM.getSpellingLoc(FromLoc);
2248 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
2249 SourceManager &ToSM = ToContext.getSourceManager();
2250 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
2251 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002252}
2253
2254SourceRange ASTImporter::Import(SourceRange FromRange) {
2255 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
2256}
2257
Douglas Gregor811663e2010-02-10 00:15:17 +00002258FileID ASTImporter::Import(FileID FromID) {
2259 llvm::DenseMap<unsigned, FileID>::iterator Pos
2260 = ImportedFileIDs.find(FromID.getHashValue());
2261 if (Pos != ImportedFileIDs.end())
2262 return Pos->second;
2263
2264 SourceManager &FromSM = FromContext.getSourceManager();
2265 SourceManager &ToSM = ToContext.getSourceManager();
2266 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
2267 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
2268
2269 // Include location of this file.
2270 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
2271
2272 // Map the FileID for to the "to" source manager.
2273 FileID ToID;
2274 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
2275 if (Cache->Entry) {
2276 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
2277 // disk again
2278 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
2279 // than mmap the files several times.
2280 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
2281 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
2282 FromSLoc.getFile().getFileCharacteristic());
2283 } else {
2284 // FIXME: We want to re-use the existing MemoryBuffer!
2285 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
2286 llvm::MemoryBuffer *ToBuf
2287 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
2288 FromBuf->getBufferEnd(),
2289 FromBuf->getBufferIdentifier());
2290 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
2291 }
2292
2293
2294 ImportedFileIDs[FromID.getHashValue()] = ToID;
2295 return ToID;
2296}
2297
Douglas Gregor96e578d2010-02-05 17:54:41 +00002298DeclarationName ASTImporter::Import(DeclarationName FromName) {
2299 if (!FromName)
2300 return DeclarationName();
2301
2302 switch (FromName.getNameKind()) {
2303 case DeclarationName::Identifier:
2304 return Import(FromName.getAsIdentifierInfo());
2305
2306 case DeclarationName::ObjCZeroArgSelector:
2307 case DeclarationName::ObjCOneArgSelector:
2308 case DeclarationName::ObjCMultiArgSelector:
2309 return Import(FromName.getObjCSelector());
2310
2311 case DeclarationName::CXXConstructorName: {
2312 QualType T = Import(FromName.getCXXNameType());
2313 if (T.isNull())
2314 return DeclarationName();
2315
2316 return ToContext.DeclarationNames.getCXXConstructorName(
2317 ToContext.getCanonicalType(T));
2318 }
2319
2320 case DeclarationName::CXXDestructorName: {
2321 QualType T = Import(FromName.getCXXNameType());
2322 if (T.isNull())
2323 return DeclarationName();
2324
2325 return ToContext.DeclarationNames.getCXXDestructorName(
2326 ToContext.getCanonicalType(T));
2327 }
2328
2329 case DeclarationName::CXXConversionFunctionName: {
2330 QualType T = Import(FromName.getCXXNameType());
2331 if (T.isNull())
2332 return DeclarationName();
2333
2334 return ToContext.DeclarationNames.getCXXConversionFunctionName(
2335 ToContext.getCanonicalType(T));
2336 }
2337
2338 case DeclarationName::CXXOperatorName:
2339 return ToContext.DeclarationNames.getCXXOperatorName(
2340 FromName.getCXXOverloadedOperator());
2341
2342 case DeclarationName::CXXLiteralOperatorName:
2343 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
2344 Import(FromName.getCXXLiteralIdentifier()));
2345
2346 case DeclarationName::CXXUsingDirective:
2347 // FIXME: STATICS!
2348 return DeclarationName::getUsingDirectiveName();
2349 }
2350
2351 // Silence bogus GCC warning
2352 return DeclarationName();
2353}
2354
2355IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
2356 if (!FromId)
2357 return 0;
2358
2359 return &ToContext.Idents.get(FromId->getName());
2360}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002361
2362DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
2363 DeclContext *DC,
2364 unsigned IDNS,
2365 NamedDecl **Decls,
2366 unsigned NumDecls) {
2367 return Name;
2368}
2369
2370DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002371 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
2372 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002373}
2374
2375DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002376 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
2377 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002378}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002379
2380Decl *ASTImporter::Imported(Decl *From, Decl *To) {
2381 ImportedDecls[From] = To;
2382 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00002383}
Douglas Gregorb4964f72010-02-15 23:54:17 +00002384
2385bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
2386 llvm::DenseMap<Type *, Type *>::iterator Pos
2387 = ImportedTypes.find(From.getTypePtr());
2388 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
2389 return true;
2390
2391 StructuralEquivalenceContext SEC(FromContext, ToContext, Diags,
2392 NonEquivalentDecls);
2393 return SEC.IsStructurallyEquivalent(From, To);
2394}