blob: cfcf460e930d0fc4a73ea4f88109fbd042b4cbaf [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 Gregor7244b0b2010-02-17 00:34:30 +000093 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000094 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +000095 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000096 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +000097 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +000098 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +000099 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000100 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
101
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000102 // Importing statements
103 Stmt *VisitStmt(Stmt *S);
104
105 // Importing expressions
106 Expr *VisitExpr(Expr *E);
107 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000108 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000109 };
110}
111
112//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000113// Structural Equivalence
114//----------------------------------------------------------------------------
115
116namespace {
117 struct StructuralEquivalenceContext {
118 /// \brief AST contexts for which we are checking structural equivalence.
119 ASTContext &C1, &C2;
120
121 /// \brief Diagnostic object used to emit diagnostics.
122 Diagnostic &Diags;
123
124 /// \brief The set of "tentative" equivalences between two canonical
125 /// declarations, mapping from a declaration in the first context to the
126 /// declaration in the second context that we believe to be equivalent.
127 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
128
129 /// \brief Queue of declarations in the first context whose equivalence
130 /// with a declaration in the second context still needs to be verified.
131 std::deque<Decl *> DeclsToCheck;
132
Douglas Gregorb4964f72010-02-15 23:54:17 +0000133 /// \brief Declaration (from, to) pairs that are known not to be equivalent
134 /// (which we have already complained about).
135 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
136
Douglas Gregor3996e242010-02-15 22:01:00 +0000137 /// \brief Whether we're being strict about the spelling of types when
138 /// unifying two types.
139 bool StrictTypeSpelling;
140
141 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
142 Diagnostic &Diags,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000143 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000144 bool StrictTypeSpelling = false)
Douglas Gregorb4964f72010-02-15 23:54:17 +0000145 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
146 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000147
148 /// \brief Determine whether the two declarations are structurally
149 /// equivalent.
150 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
151
152 /// \brief Determine whether the two types are structurally equivalent.
153 bool IsStructurallyEquivalent(QualType T1, QualType T2);
154
155 private:
156 /// \brief Finish checking all of the structural equivalences.
157 ///
158 /// \returns true if an error occurred, false otherwise.
159 bool Finish();
160
161 public:
162 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
163 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
164 }
165
166 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
167 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
168 }
169 };
170}
171
172static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
173 QualType T1, QualType T2);
174static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
175 Decl *D1, Decl *D2);
176
177/// \brief Determine if two APInts have the same value, after zero-extending
178/// one of them (if needed!) to ensure that the bit-widths match.
179static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
180 if (I1.getBitWidth() == I2.getBitWidth())
181 return I1 == I2;
182
183 if (I1.getBitWidth() > I2.getBitWidth())
184 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
185
186 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
187}
188
189/// \brief Determine if two APSInts have the same value, zero- or sign-extending
190/// as needed.
191static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
192 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
193 return I1 == I2;
194
195 // Check for a bit-width mismatch.
196 if (I1.getBitWidth() > I2.getBitWidth())
197 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
198 else if (I2.getBitWidth() > I1.getBitWidth())
199 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
200
201 // We have a signedness mismatch. Turn the signed value into an unsigned
202 // value.
203 if (I1.isSigned()) {
204 if (I1.isNegative())
205 return false;
206
207 return llvm::APSInt(I1, true) == I2;
208 }
209
210 if (I2.isNegative())
211 return false;
212
213 return I1 == llvm::APSInt(I2, true);
214}
215
216/// \brief Determine structural equivalence of two expressions.
217static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
218 Expr *E1, Expr *E2) {
219 if (!E1 || !E2)
220 return E1 == E2;
221
222 // FIXME: Actually perform a structural comparison!
223 return true;
224}
225
226/// \brief Determine whether two identifiers are equivalent.
227static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
228 const IdentifierInfo *Name2) {
229 if (!Name1 || !Name2)
230 return Name1 == Name2;
231
232 return Name1->getName() == Name2->getName();
233}
234
235/// \brief Determine whether two nested-name-specifiers are equivalent.
236static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
237 NestedNameSpecifier *NNS1,
238 NestedNameSpecifier *NNS2) {
239 // FIXME: Implement!
240 return true;
241}
242
243/// \brief Determine whether two template arguments are equivalent.
244static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
245 const TemplateArgument &Arg1,
246 const TemplateArgument &Arg2) {
247 // FIXME: Implement!
248 return true;
249}
250
251/// \brief Determine structural equivalence for the common part of array
252/// types.
253static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
254 const ArrayType *Array1,
255 const ArrayType *Array2) {
256 if (!IsStructurallyEquivalent(Context,
257 Array1->getElementType(),
258 Array2->getElementType()))
259 return false;
260 if (Array1->getSizeModifier() != Array2->getSizeModifier())
261 return false;
262 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
263 return false;
264
265 return true;
266}
267
268/// \brief Determine structural equivalence of two types.
269static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
270 QualType T1, QualType T2) {
271 if (T1.isNull() || T2.isNull())
272 return T1.isNull() && T2.isNull();
273
274 if (!Context.StrictTypeSpelling) {
275 // We aren't being strict about token-to-token equivalence of types,
276 // so map down to the canonical type.
277 T1 = Context.C1.getCanonicalType(T1);
278 T2 = Context.C2.getCanonicalType(T2);
279 }
280
281 if (T1.getQualifiers() != T2.getQualifiers())
282 return false;
283
Douglas Gregorb4964f72010-02-15 23:54:17 +0000284 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000285
Douglas Gregorb4964f72010-02-15 23:54:17 +0000286 if (T1->getTypeClass() != T2->getTypeClass()) {
287 // Compare function types with prototypes vs. without prototypes as if
288 // both did not have prototypes.
289 if (T1->getTypeClass() == Type::FunctionProto &&
290 T2->getTypeClass() == Type::FunctionNoProto)
291 TC = Type::FunctionNoProto;
292 else if (T1->getTypeClass() == Type::FunctionNoProto &&
293 T2->getTypeClass() == Type::FunctionProto)
294 TC = Type::FunctionNoProto;
295 else
296 return false;
297 }
298
299 switch (TC) {
300 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000301 // FIXME: Deal with Char_S/Char_U.
302 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
303 return false;
304 break;
305
306 case Type::Complex:
307 if (!IsStructurallyEquivalent(Context,
308 cast<ComplexType>(T1)->getElementType(),
309 cast<ComplexType>(T2)->getElementType()))
310 return false;
311 break;
312
313 case Type::Pointer:
314 if (!IsStructurallyEquivalent(Context,
315 cast<PointerType>(T1)->getPointeeType(),
316 cast<PointerType>(T2)->getPointeeType()))
317 return false;
318 break;
319
320 case Type::BlockPointer:
321 if (!IsStructurallyEquivalent(Context,
322 cast<BlockPointerType>(T1)->getPointeeType(),
323 cast<BlockPointerType>(T2)->getPointeeType()))
324 return false;
325 break;
326
327 case Type::LValueReference:
328 case Type::RValueReference: {
329 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
330 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
331 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
332 return false;
333 if (Ref1->isInnerRef() != Ref2->isInnerRef())
334 return false;
335 if (!IsStructurallyEquivalent(Context,
336 Ref1->getPointeeTypeAsWritten(),
337 Ref2->getPointeeTypeAsWritten()))
338 return false;
339 break;
340 }
341
342 case Type::MemberPointer: {
343 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
344 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
345 if (!IsStructurallyEquivalent(Context,
346 MemPtr1->getPointeeType(),
347 MemPtr2->getPointeeType()))
348 return false;
349 if (!IsStructurallyEquivalent(Context,
350 QualType(MemPtr1->getClass(), 0),
351 QualType(MemPtr2->getClass(), 0)))
352 return false;
353 break;
354 }
355
356 case Type::ConstantArray: {
357 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
358 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
359 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
360 return false;
361
362 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
363 return false;
364 break;
365 }
366
367 case Type::IncompleteArray:
368 if (!IsArrayStructurallyEquivalent(Context,
369 cast<ArrayType>(T1),
370 cast<ArrayType>(T2)))
371 return false;
372 break;
373
374 case Type::VariableArray: {
375 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
376 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
377 if (!IsStructurallyEquivalent(Context,
378 Array1->getSizeExpr(), Array2->getSizeExpr()))
379 return false;
380
381 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
382 return false;
383
384 break;
385 }
386
387 case Type::DependentSizedArray: {
388 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
389 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
390 if (!IsStructurallyEquivalent(Context,
391 Array1->getSizeExpr(), Array2->getSizeExpr()))
392 return false;
393
394 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
395 return false;
396
397 break;
398 }
399
400 case Type::DependentSizedExtVector: {
401 const DependentSizedExtVectorType *Vec1
402 = cast<DependentSizedExtVectorType>(T1);
403 const DependentSizedExtVectorType *Vec2
404 = cast<DependentSizedExtVectorType>(T2);
405 if (!IsStructurallyEquivalent(Context,
406 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
407 return false;
408 if (!IsStructurallyEquivalent(Context,
409 Vec1->getElementType(),
410 Vec2->getElementType()))
411 return false;
412 break;
413 }
414
415 case Type::Vector:
416 case Type::ExtVector: {
417 const VectorType *Vec1 = cast<VectorType>(T1);
418 const VectorType *Vec2 = cast<VectorType>(T2);
419 if (!IsStructurallyEquivalent(Context,
420 Vec1->getElementType(),
421 Vec2->getElementType()))
422 return false;
423 if (Vec1->getNumElements() != Vec2->getNumElements())
424 return false;
425 if (Vec1->isAltiVec() != Vec2->isAltiVec())
426 return false;
427 if (Vec1->isPixel() != Vec2->isPixel())
428 return false;
429 }
430
431 case Type::FunctionProto: {
432 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
433 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
434 if (Proto1->getNumArgs() != Proto2->getNumArgs())
435 return false;
436 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
437 if (!IsStructurallyEquivalent(Context,
438 Proto1->getArgType(I),
439 Proto2->getArgType(I)))
440 return false;
441 }
442 if (Proto1->isVariadic() != Proto2->isVariadic())
443 return false;
444 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
445 return false;
446 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
447 return false;
448 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
449 return false;
450 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
451 if (!IsStructurallyEquivalent(Context,
452 Proto1->getExceptionType(I),
453 Proto2->getExceptionType(I)))
454 return false;
455 }
456 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
457 return false;
458
459 // Fall through to check the bits common with FunctionNoProtoType.
460 }
461
462 case Type::FunctionNoProto: {
463 const FunctionType *Function1 = cast<FunctionType>(T1);
464 const FunctionType *Function2 = cast<FunctionType>(T2);
465 if (!IsStructurallyEquivalent(Context,
466 Function1->getResultType(),
467 Function2->getResultType()))
468 return false;
469 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
470 return false;
471 if (Function1->getCallConv() != Function2->getCallConv())
472 return false;
473 break;
474 }
475
476 case Type::UnresolvedUsing:
477 if (!IsStructurallyEquivalent(Context,
478 cast<UnresolvedUsingType>(T1)->getDecl(),
479 cast<UnresolvedUsingType>(T2)->getDecl()))
480 return false;
481
482 break;
483
484 case Type::Typedef:
485 if (!IsStructurallyEquivalent(Context,
486 cast<TypedefType>(T1)->getDecl(),
487 cast<TypedefType>(T2)->getDecl()))
488 return false;
489 break;
490
491 case Type::TypeOfExpr:
492 if (!IsStructurallyEquivalent(Context,
493 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
494 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
495 return false;
496 break;
497
498 case Type::TypeOf:
499 if (!IsStructurallyEquivalent(Context,
500 cast<TypeOfType>(T1)->getUnderlyingType(),
501 cast<TypeOfType>(T2)->getUnderlyingType()))
502 return false;
503 break;
504
505 case Type::Decltype:
506 if (!IsStructurallyEquivalent(Context,
507 cast<DecltypeType>(T1)->getUnderlyingExpr(),
508 cast<DecltypeType>(T2)->getUnderlyingExpr()))
509 return false;
510 break;
511
512 case Type::Record:
513 case Type::Enum:
514 if (!IsStructurallyEquivalent(Context,
515 cast<TagType>(T1)->getDecl(),
516 cast<TagType>(T2)->getDecl()))
517 return false;
518 break;
519
520 case Type::Elaborated: {
521 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
522 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
523 if (Elab1->getTagKind() != Elab2->getTagKind())
524 return false;
525 if (!IsStructurallyEquivalent(Context,
526 Elab1->getUnderlyingType(),
527 Elab2->getUnderlyingType()))
528 return false;
529 break;
530 }
531
532 case Type::TemplateTypeParm: {
533 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
534 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
535 if (Parm1->getDepth() != Parm2->getDepth())
536 return false;
537 if (Parm1->getIndex() != Parm2->getIndex())
538 return false;
539 if (Parm1->isParameterPack() != Parm2->isParameterPack())
540 return false;
541
542 // Names of template type parameters are never significant.
543 break;
544 }
545
546 case Type::SubstTemplateTypeParm: {
547 const SubstTemplateTypeParmType *Subst1
548 = cast<SubstTemplateTypeParmType>(T1);
549 const SubstTemplateTypeParmType *Subst2
550 = cast<SubstTemplateTypeParmType>(T2);
551 if (!IsStructurallyEquivalent(Context,
552 QualType(Subst1->getReplacedParameter(), 0),
553 QualType(Subst2->getReplacedParameter(), 0)))
554 return false;
555 if (!IsStructurallyEquivalent(Context,
556 Subst1->getReplacementType(),
557 Subst2->getReplacementType()))
558 return false;
559 break;
560 }
561
562 case Type::TemplateSpecialization: {
563 const TemplateSpecializationType *Spec1
564 = cast<TemplateSpecializationType>(T1);
565 const TemplateSpecializationType *Spec2
566 = cast<TemplateSpecializationType>(T2);
567 if (!IsStructurallyEquivalent(Context,
568 Spec1->getTemplateName(),
569 Spec2->getTemplateName()))
570 return false;
571 if (Spec1->getNumArgs() != Spec2->getNumArgs())
572 return false;
573 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
574 if (!IsStructurallyEquivalent(Context,
575 Spec1->getArg(I), Spec2->getArg(I)))
576 return false;
577 }
578 break;
579 }
580
581 case Type::QualifiedName: {
582 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
583 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
584 if (!IsStructurallyEquivalent(Context,
585 Qual1->getQualifier(),
586 Qual2->getQualifier()))
587 return false;
588 if (!IsStructurallyEquivalent(Context,
589 Qual1->getNamedType(),
590 Qual2->getNamedType()))
591 return false;
592 break;
593 }
594
595 case Type::Typename: {
596 const TypenameType *Typename1 = cast<TypenameType>(T1);
597 const TypenameType *Typename2 = cast<TypenameType>(T2);
598 if (!IsStructurallyEquivalent(Context,
599 Typename1->getQualifier(),
600 Typename2->getQualifier()))
601 return false;
602 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
603 Typename2->getIdentifier()))
604 return false;
605 if (!IsStructurallyEquivalent(Context,
606 QualType(Typename1->getTemplateId(), 0),
607 QualType(Typename2->getTemplateId(), 0)))
608 return false;
609
610 break;
611 }
612
613 case Type::ObjCInterface: {
614 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
615 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
616 if (!IsStructurallyEquivalent(Context,
617 Iface1->getDecl(), Iface2->getDecl()))
618 return false;
619 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
620 return false;
621 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
622 if (!IsStructurallyEquivalent(Context,
623 Iface1->getProtocol(I),
624 Iface2->getProtocol(I)))
625 return false;
626 }
627 break;
628 }
629
630 case Type::ObjCObjectPointer: {
631 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
632 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
633 if (!IsStructurallyEquivalent(Context,
634 Ptr1->getPointeeType(),
635 Ptr2->getPointeeType()))
636 return false;
637 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
638 return false;
639 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
640 if (!IsStructurallyEquivalent(Context,
641 Ptr1->getProtocol(I),
642 Ptr2->getProtocol(I)))
643 return false;
644 }
645 break;
646 }
647
648 } // end switch
649
650 return true;
651}
652
653/// \brief Determine structural equivalence of two records.
654static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
655 RecordDecl *D1, RecordDecl *D2) {
656 if (D1->isUnion() != D2->isUnion()) {
657 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
658 << Context.C2.getTypeDeclType(D2);
659 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
660 << D1->getDeclName() << (unsigned)D1->getTagKind();
661 return false;
662 }
663
Douglas Gregorb4964f72010-02-15 23:54:17 +0000664 // Compare the definitions of these two records. If either or both are
665 // incomplete, we assume that they are equivalent.
666 D1 = D1->getDefinition();
667 D2 = D2->getDefinition();
668 if (!D1 || !D2)
669 return true;
670
Douglas Gregor3996e242010-02-15 22:01:00 +0000671 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
672 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
673 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
674 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
675 << Context.C2.getTypeDeclType(D2);
676 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
677 << D2CXX->getNumBases();
678 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
679 << D1CXX->getNumBases();
680 return false;
681 }
682
683 // Check the base classes.
684 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
685 BaseEnd1 = D1CXX->bases_end(),
686 Base2 = D2CXX->bases_begin();
687 Base1 != BaseEnd1;
688 ++Base1, ++Base2) {
689 if (!IsStructurallyEquivalent(Context,
690 Base1->getType(), Base2->getType())) {
691 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
692 << Context.C2.getTypeDeclType(D2);
693 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
694 << Base2->getType()
695 << Base2->getSourceRange();
696 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
697 << Base1->getType()
698 << Base1->getSourceRange();
699 return false;
700 }
701
702 // Check virtual vs. non-virtual inheritance mismatch.
703 if (Base1->isVirtual() != Base2->isVirtual()) {
704 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
705 << Context.C2.getTypeDeclType(D2);
706 Context.Diag2(Base2->getSourceRange().getBegin(),
707 diag::note_odr_virtual_base)
708 << Base2->isVirtual() << Base2->getSourceRange();
709 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
710 << Base1->isVirtual()
711 << Base1->getSourceRange();
712 return false;
713 }
714 }
715 } else if (D1CXX->getNumBases() > 0) {
716 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
717 << Context.C2.getTypeDeclType(D2);
718 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
719 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
720 << Base1->getType()
721 << Base1->getSourceRange();
722 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
723 return false;
724 }
725 }
726
727 // Check the fields for consistency.
728 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
729 Field2End = D2->field_end();
730 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
731 Field1End = D1->field_end();
732 Field1 != Field1End;
733 ++Field1, ++Field2) {
734 if (Field2 == Field2End) {
735 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
736 << Context.C2.getTypeDeclType(D2);
737 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
738 << Field1->getDeclName() << Field1->getType();
739 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
740 return false;
741 }
742
743 if (!IsStructurallyEquivalent(Context,
744 Field1->getType(), Field2->getType())) {
745 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
746 << Context.C2.getTypeDeclType(D2);
747 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
748 << Field2->getDeclName() << Field2->getType();
749 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
750 << Field1->getDeclName() << Field1->getType();
751 return false;
752 }
753
754 if (Field1->isBitField() != Field2->isBitField()) {
755 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
756 << Context.C2.getTypeDeclType(D2);
757 if (Field1->isBitField()) {
758 llvm::APSInt Bits;
759 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
760 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
761 << Field1->getDeclName() << Field1->getType()
762 << Bits.toString(10, false);
763 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
764 << Field2->getDeclName();
765 } else {
766 llvm::APSInt Bits;
767 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
768 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
769 << Field2->getDeclName() << Field2->getType()
770 << Bits.toString(10, false);
771 Context.Diag1(Field1->getLocation(),
772 diag::note_odr_not_bit_field)
773 << Field1->getDeclName();
774 }
775 return false;
776 }
777
778 if (Field1->isBitField()) {
779 // Make sure that the bit-fields are the same length.
780 llvm::APSInt Bits1, Bits2;
781 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
782 return false;
783 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
784 return false;
785
786 if (!IsSameValue(Bits1, Bits2)) {
787 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
788 << Context.C2.getTypeDeclType(D2);
789 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
790 << Field2->getDeclName() << Field2->getType()
791 << Bits2.toString(10, false);
792 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
793 << Field1->getDeclName() << Field1->getType()
794 << Bits1.toString(10, false);
795 return false;
796 }
797 }
798 }
799
800 if (Field2 != Field2End) {
801 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
802 << Context.C2.getTypeDeclType(D2);
803 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
804 << Field2->getDeclName() << Field2->getType();
805 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
806 return false;
807 }
808
809 return true;
810}
811
812/// \brief Determine structural equivalence of two enums.
813static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
814 EnumDecl *D1, EnumDecl *D2) {
815 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
816 EC2End = D2->enumerator_end();
817 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
818 EC1End = D1->enumerator_end();
819 EC1 != EC1End; ++EC1, ++EC2) {
820 if (EC2 == EC2End) {
821 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
822 << Context.C2.getTypeDeclType(D2);
823 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
824 << EC1->getDeclName()
825 << EC1->getInitVal().toString(10);
826 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
827 return false;
828 }
829
830 llvm::APSInt Val1 = EC1->getInitVal();
831 llvm::APSInt Val2 = EC2->getInitVal();
832 if (!IsSameValue(Val1, Val2) ||
833 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
834 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
835 << Context.C2.getTypeDeclType(D2);
836 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
837 << EC2->getDeclName()
838 << EC2->getInitVal().toString(10);
839 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
840 << EC1->getDeclName()
841 << EC1->getInitVal().toString(10);
842 return false;
843 }
844 }
845
846 if (EC2 != EC2End) {
847 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
848 << Context.C2.getTypeDeclType(D2);
849 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
850 << EC2->getDeclName()
851 << EC2->getInitVal().toString(10);
852 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
853 return false;
854 }
855
856 return true;
857}
858
859/// \brief Determine structural equivalence of two declarations.
860static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
861 Decl *D1, Decl *D2) {
862 // FIXME: Check for known structural equivalences via a callback of some sort.
863
Douglas Gregorb4964f72010-02-15 23:54:17 +0000864 // Check whether we already know that these two declarations are not
865 // structurally equivalent.
866 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
867 D2->getCanonicalDecl())))
868 return false;
869
Douglas Gregor3996e242010-02-15 22:01:00 +0000870 // Determine whether we've already produced a tentative equivalence for D1.
871 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
872 if (EquivToD1)
873 return EquivToD1 == D2->getCanonicalDecl();
874
875 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
876 EquivToD1 = D2->getCanonicalDecl();
877 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
878 return true;
879}
880
881bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
882 Decl *D2) {
883 if (!::IsStructurallyEquivalent(*this, D1, D2))
884 return false;
885
886 return !Finish();
887}
888
889bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
890 QualType T2) {
891 if (!::IsStructurallyEquivalent(*this, T1, T2))
892 return false;
893
894 return !Finish();
895}
896
897bool StructuralEquivalenceContext::Finish() {
898 while (!DeclsToCheck.empty()) {
899 // Check the next declaration.
900 Decl *D1 = DeclsToCheck.front();
901 DeclsToCheck.pop_front();
902
903 Decl *D2 = TentativeEquivalences[D1];
904 assert(D2 && "Unrecorded tentative equivalence?");
905
Douglas Gregorb4964f72010-02-15 23:54:17 +0000906 bool Equivalent = true;
907
Douglas Gregor3996e242010-02-15 22:01:00 +0000908 // FIXME: Switch on all declaration kinds. For now, we're just going to
909 // check the obvious ones.
910 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
911 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
912 // Check for equivalent structure names.
913 IdentifierInfo *Name1 = Record1->getIdentifier();
914 if (!Name1 && Record1->getTypedefForAnonDecl())
915 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
916 IdentifierInfo *Name2 = Record2->getIdentifier();
917 if (!Name2 && Record2->getTypedefForAnonDecl())
918 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000919 if (!::IsStructurallyEquivalent(Name1, Name2) ||
920 !::IsStructurallyEquivalent(*this, Record1, Record2))
921 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000922 } else {
923 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000924 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000926 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000927 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
928 // Check for equivalent enum names.
929 IdentifierInfo *Name1 = Enum1->getIdentifier();
930 if (!Name1 && Enum1->getTypedefForAnonDecl())
931 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
932 IdentifierInfo *Name2 = Enum2->getIdentifier();
933 if (!Name2 && Enum2->getTypedefForAnonDecl())
934 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000935 if (!::IsStructurallyEquivalent(Name1, Name2) ||
936 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
937 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000938 } else {
939 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +0000940 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000941 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000942 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000943 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
944 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000945 Typedef2->getIdentifier()) ||
946 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +0000947 Typedef1->getUnderlyingType(),
948 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +0000949 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000950 } else {
951 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000952 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000953 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000954 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000955
956 if (!Equivalent) {
957 // Note that these two declarations are not equivalent (and we already
958 // know about it).
959 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
960 D2->getCanonicalDecl()));
961 return true;
962 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000963 // FIXME: Check other declaration kinds!
964 }
965
966 return false;
967}
968
969//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000970// Import Types
971//----------------------------------------------------------------------------
972
Douglas Gregore4c83e42010-02-09 22:48:33 +0000973QualType ASTNodeImporter::VisitType(Type *T) {
974 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
975 << T->getTypeClassName();
976 return QualType();
977}
978
Douglas Gregor96e578d2010-02-05 17:54:41 +0000979QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
980 switch (T->getKind()) {
981 case BuiltinType::Void: return Importer.getToContext().VoidTy;
982 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
983
984 case BuiltinType::Char_U:
985 // The context we're importing from has an unsigned 'char'. If we're
986 // importing into a context with a signed 'char', translate to
987 // 'unsigned char' instead.
988 if (Importer.getToContext().getLangOptions().CharIsSigned)
989 return Importer.getToContext().UnsignedCharTy;
990
991 return Importer.getToContext().CharTy;
992
993 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
994
995 case BuiltinType::Char16:
996 // FIXME: Make sure that the "to" context supports C++!
997 return Importer.getToContext().Char16Ty;
998
999 case BuiltinType::Char32:
1000 // FIXME: Make sure that the "to" context supports C++!
1001 return Importer.getToContext().Char32Ty;
1002
1003 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1004 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1005 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1006 case BuiltinType::ULongLong:
1007 return Importer.getToContext().UnsignedLongLongTy;
1008 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1009
1010 case BuiltinType::Char_S:
1011 // The context we're importing from has an unsigned 'char'. If we're
1012 // importing into a context with a signed 'char', translate to
1013 // 'unsigned char' instead.
1014 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1015 return Importer.getToContext().SignedCharTy;
1016
1017 return Importer.getToContext().CharTy;
1018
1019 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1020 case BuiltinType::WChar:
1021 // FIXME: If not in C++, shall we translate to the C equivalent of
1022 // wchar_t?
1023 return Importer.getToContext().WCharTy;
1024
1025 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1026 case BuiltinType::Int : return Importer.getToContext().IntTy;
1027 case BuiltinType::Long : return Importer.getToContext().LongTy;
1028 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1029 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1030 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1031 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1032 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1033
1034 case BuiltinType::NullPtr:
1035 // FIXME: Make sure that the "to" context supports C++0x!
1036 return Importer.getToContext().NullPtrTy;
1037
1038 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1039 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1040 case BuiltinType::UndeducedAuto:
1041 // FIXME: Make sure that the "to" context supports C++0x!
1042 return Importer.getToContext().UndeducedAutoTy;
1043
1044 case BuiltinType::ObjCId:
1045 // FIXME: Make sure that the "to" context supports Objective-C!
1046 return Importer.getToContext().ObjCBuiltinIdTy;
1047
1048 case BuiltinType::ObjCClass:
1049 return Importer.getToContext().ObjCBuiltinClassTy;
1050
1051 case BuiltinType::ObjCSel:
1052 return Importer.getToContext().ObjCBuiltinSelTy;
1053 }
1054
1055 return QualType();
1056}
1057
1058QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1059 QualType ToElementType = Importer.Import(T->getElementType());
1060 if (ToElementType.isNull())
1061 return QualType();
1062
1063 return Importer.getToContext().getComplexType(ToElementType);
1064}
1065
1066QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1067 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1068 if (ToPointeeType.isNull())
1069 return QualType();
1070
1071 return Importer.getToContext().getPointerType(ToPointeeType);
1072}
1073
1074QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1075 // FIXME: Check for blocks support in "to" context.
1076 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1077 if (ToPointeeType.isNull())
1078 return QualType();
1079
1080 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1081}
1082
1083QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1084 // FIXME: Check for C++ support in "to" context.
1085 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1086 if (ToPointeeType.isNull())
1087 return QualType();
1088
1089 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1090}
1091
1092QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1093 // FIXME: Check for C++0x support in "to" context.
1094 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1095 if (ToPointeeType.isNull())
1096 return QualType();
1097
1098 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1099}
1100
1101QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1102 // FIXME: Check for C++ support in "to" context.
1103 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1104 if (ToPointeeType.isNull())
1105 return QualType();
1106
1107 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1108 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1109 ClassType.getTypePtr());
1110}
1111
1112QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1113 QualType ToElementType = Importer.Import(T->getElementType());
1114 if (ToElementType.isNull())
1115 return QualType();
1116
1117 return Importer.getToContext().getConstantArrayType(ToElementType,
1118 T->getSize(),
1119 T->getSizeModifier(),
1120 T->getIndexTypeCVRQualifiers());
1121}
1122
1123QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1124 QualType ToElementType = Importer.Import(T->getElementType());
1125 if (ToElementType.isNull())
1126 return QualType();
1127
1128 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1129 T->getSizeModifier(),
1130 T->getIndexTypeCVRQualifiers());
1131}
1132
1133QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1134 QualType ToElementType = Importer.Import(T->getElementType());
1135 if (ToElementType.isNull())
1136 return QualType();
1137
1138 Expr *Size = Importer.Import(T->getSizeExpr());
1139 if (!Size)
1140 return QualType();
1141
1142 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1143 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1144 T->getSizeModifier(),
1145 T->getIndexTypeCVRQualifiers(),
1146 Brackets);
1147}
1148
1149QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1150 QualType ToElementType = Importer.Import(T->getElementType());
1151 if (ToElementType.isNull())
1152 return QualType();
1153
1154 return Importer.getToContext().getVectorType(ToElementType,
1155 T->getNumElements(),
1156 T->isAltiVec(),
1157 T->isPixel());
1158}
1159
1160QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1161 QualType ToElementType = Importer.Import(T->getElementType());
1162 if (ToElementType.isNull())
1163 return QualType();
1164
1165 return Importer.getToContext().getExtVectorType(ToElementType,
1166 T->getNumElements());
1167}
1168
1169QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1170 // FIXME: What happens if we're importing a function without a prototype
1171 // into C++? Should we make it variadic?
1172 QualType ToResultType = Importer.Import(T->getResultType());
1173 if (ToResultType.isNull())
1174 return QualType();
1175
1176 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1177 T->getNoReturnAttr(),
1178 T->getCallConv());
1179}
1180
1181QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1182 QualType ToResultType = Importer.Import(T->getResultType());
1183 if (ToResultType.isNull())
1184 return QualType();
1185
1186 // Import argument types
1187 llvm::SmallVector<QualType, 4> ArgTypes;
1188 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1189 AEnd = T->arg_type_end();
1190 A != AEnd; ++A) {
1191 QualType ArgType = Importer.Import(*A);
1192 if (ArgType.isNull())
1193 return QualType();
1194 ArgTypes.push_back(ArgType);
1195 }
1196
1197 // Import exception types
1198 llvm::SmallVector<QualType, 4> ExceptionTypes;
1199 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1200 EEnd = T->exception_end();
1201 E != EEnd; ++E) {
1202 QualType ExceptionType = Importer.Import(*E);
1203 if (ExceptionType.isNull())
1204 return QualType();
1205 ExceptionTypes.push_back(ExceptionType);
1206 }
1207
1208 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1209 ArgTypes.size(),
1210 T->isVariadic(),
1211 T->getTypeQuals(),
1212 T->hasExceptionSpec(),
1213 T->hasAnyExceptionSpec(),
1214 ExceptionTypes.size(),
1215 ExceptionTypes.data(),
1216 T->getNoReturnAttr(),
1217 T->getCallConv());
1218}
1219
1220QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1221 TypedefDecl *ToDecl
1222 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1223 if (!ToDecl)
1224 return QualType();
1225
1226 return Importer.getToContext().getTypeDeclType(ToDecl);
1227}
1228
1229QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1230 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1231 if (!ToExpr)
1232 return QualType();
1233
1234 return Importer.getToContext().getTypeOfExprType(ToExpr);
1235}
1236
1237QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1238 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1239 if (ToUnderlyingType.isNull())
1240 return QualType();
1241
1242 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1243}
1244
1245QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1246 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1247 if (!ToExpr)
1248 return QualType();
1249
1250 return Importer.getToContext().getDecltypeType(ToExpr);
1251}
1252
1253QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1254 RecordDecl *ToDecl
1255 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1256 if (!ToDecl)
1257 return QualType();
1258
1259 return Importer.getToContext().getTagDeclType(ToDecl);
1260}
1261
1262QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1263 EnumDecl *ToDecl
1264 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1265 if (!ToDecl)
1266 return QualType();
1267
1268 return Importer.getToContext().getTagDeclType(ToDecl);
1269}
1270
1271QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1272 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1273 if (ToUnderlyingType.isNull())
1274 return QualType();
1275
1276 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1277 T->getTagKind());
1278}
1279
1280QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1281 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1282 if (!ToQualifier)
1283 return QualType();
1284
1285 QualType ToNamedType = Importer.Import(T->getNamedType());
1286 if (ToNamedType.isNull())
1287 return QualType();
1288
1289 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1290}
1291
1292QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1293 ObjCInterfaceDecl *Class
1294 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1295 if (!Class)
1296 return QualType();
1297
1298 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1299 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1300 PEnd = T->qual_end();
1301 P != PEnd; ++P) {
1302 ObjCProtocolDecl *Protocol
1303 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1304 if (!Protocol)
1305 return QualType();
1306 Protocols.push_back(Protocol);
1307 }
1308
1309 return Importer.getToContext().getObjCInterfaceType(Class,
1310 Protocols.data(),
1311 Protocols.size());
1312}
1313
1314QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1315 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1316 if (ToPointeeType.isNull())
1317 return QualType();
1318
1319 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1320 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1321 PEnd = T->qual_end();
1322 P != PEnd; ++P) {
1323 ObjCProtocolDecl *Protocol
1324 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1325 if (!Protocol)
1326 return QualType();
1327 Protocols.push_back(Protocol);
1328 }
1329
1330 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1331 Protocols.data(),
1332 Protocols.size());
1333}
1334
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001335//----------------------------------------------------------------------------
1336// Import Declarations
1337//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001338bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1339 DeclContext *&LexicalDC,
1340 DeclarationName &Name,
1341 SourceLocation &Loc) {
1342 // Import the context of this declaration.
1343 DC = Importer.ImportContext(D->getDeclContext());
1344 if (!DC)
1345 return true;
1346
1347 LexicalDC = DC;
1348 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1349 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1350 if (!LexicalDC)
1351 return true;
1352 }
1353
1354 // Import the name of this declaration.
1355 Name = Importer.Import(D->getDeclName());
1356 if (D->getDeclName() && !Name)
1357 return true;
1358
1359 // Import the location of this declaration.
1360 Loc = Importer.Import(D->getLocation());
1361 return false;
1362}
1363
Douglas Gregor5c73e912010-02-11 00:48:18 +00001364bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001365 RecordDecl *ToRecord) {
1366 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1367 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001368 Importer.getDiags(),
1369 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001370 return SEC.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001371}
1372
Douglas Gregor98c10182010-02-12 22:17:39 +00001373bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001374 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1375 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001376 Importer.getDiags(),
1377 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001378 return SEC.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001379}
1380
Douglas Gregore4c83e42010-02-09 22:48:33 +00001381Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001382 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001383 << D->getDeclKindName();
1384 return 0;
1385}
1386
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001387Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1388 // Import the major distinguishing characteristics of this typedef.
1389 DeclContext *DC, *LexicalDC;
1390 DeclarationName Name;
1391 SourceLocation Loc;
1392 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1393 return 0;
1394
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001395 // If this typedef is not in block scope, determine whether we've
1396 // seen a typedef with the same name (that we can merge with) or any
1397 // other entity by that name (which name lookup could conflict with).
1398 if (!DC->isFunctionOrMethod()) {
1399 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1400 unsigned IDNS = Decl::IDNS_Ordinary;
1401 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1402 Lookup.first != Lookup.second;
1403 ++Lookup.first) {
1404 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1405 continue;
1406 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001407 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1408 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001409 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001410 }
1411
1412 ConflictingDecls.push_back(*Lookup.first);
1413 }
1414
1415 if (!ConflictingDecls.empty()) {
1416 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1417 ConflictingDecls.data(),
1418 ConflictingDecls.size());
1419 if (!Name)
1420 return 0;
1421 }
1422 }
1423
Douglas Gregorb4964f72010-02-15 23:54:17 +00001424 // Import the underlying type of this typedef;
1425 QualType T = Importer.Import(D->getUnderlyingType());
1426 if (T.isNull())
1427 return 0;
1428
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001429 // Create the new typedef node.
1430 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1431 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1432 Loc, Name.getAsIdentifierInfo(),
1433 TInfo);
1434 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001435 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001436 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001437
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001438 return ToTypedef;
1439}
1440
Douglas Gregor98c10182010-02-12 22:17:39 +00001441Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1442 // Import the major distinguishing characteristics of this enum.
1443 DeclContext *DC, *LexicalDC;
1444 DeclarationName Name;
1445 SourceLocation Loc;
1446 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1447 return 0;
1448
1449 // Figure out what enum name we're looking for.
1450 unsigned IDNS = Decl::IDNS_Tag;
1451 DeclarationName SearchName = Name;
1452 if (!SearchName && D->getTypedefForAnonDecl()) {
1453 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1454 IDNS = Decl::IDNS_Ordinary;
1455 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1456 IDNS |= Decl::IDNS_Ordinary;
1457
1458 // We may already have an enum of the same name; try to find and match it.
1459 if (!DC->isFunctionOrMethod() && SearchName) {
1460 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1461 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1462 Lookup.first != Lookup.second;
1463 ++Lookup.first) {
1464 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1465 continue;
1466
1467 Decl *Found = *Lookup.first;
1468 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1469 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1470 Found = Tag->getDecl();
1471 }
1472
1473 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001474 if (IsStructuralMatch(D, FoundEnum))
1475 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001476 }
1477
1478 ConflictingDecls.push_back(*Lookup.first);
1479 }
1480
1481 if (!ConflictingDecls.empty()) {
1482 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1483 ConflictingDecls.data(),
1484 ConflictingDecls.size());
1485 }
1486 }
1487
1488 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001489 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001490 Name.getAsIdentifierInfo(),
1491 Importer.Import(D->getTagKeywordLoc()),
1492 0);
Douglas Gregor3996e242010-02-15 22:01:00 +00001493 D2->setLexicalDeclContext(LexicalDC);
1494 Importer.Imported(D, D2);
1495 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001496
1497 // Import the integer type.
1498 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1499 if (ToIntegerType.isNull())
1500 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001501 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001502
1503 // Import the definition
1504 if (D->isDefinition()) {
1505 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1506 if (T.isNull())
1507 return 0;
1508
1509 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1510 if (ToPromotionType.isNull())
1511 return 0;
1512
Douglas Gregor3996e242010-02-15 22:01:00 +00001513 D2->startDefinition();
Douglas Gregor98c10182010-02-12 22:17:39 +00001514 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1515 FromMemEnd = D->decls_end();
1516 FromMem != FromMemEnd;
1517 ++FromMem)
1518 Importer.Import(*FromMem);
1519
Douglas Gregor3996e242010-02-15 22:01:00 +00001520 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001521 }
1522
Douglas Gregor3996e242010-02-15 22:01:00 +00001523 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001524}
1525
Douglas Gregor5c73e912010-02-11 00:48:18 +00001526Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1527 // If this record has a definition in the translation unit we're coming from,
1528 // but this particular declaration is not that definition, import the
1529 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001530 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001531 if (Definition && Definition != D) {
1532 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001533 if (!ImportedDef)
1534 return 0;
1535
1536 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001537 }
1538
1539 // Import the major distinguishing characteristics of this record.
1540 DeclContext *DC, *LexicalDC;
1541 DeclarationName Name;
1542 SourceLocation Loc;
1543 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1544 return 0;
1545
1546 // Figure out what structure name we're looking for.
1547 unsigned IDNS = Decl::IDNS_Tag;
1548 DeclarationName SearchName = Name;
1549 if (!SearchName && D->getTypedefForAnonDecl()) {
1550 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1551 IDNS = Decl::IDNS_Ordinary;
1552 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1553 IDNS |= Decl::IDNS_Ordinary;
1554
1555 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001556 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001557 if (!DC->isFunctionOrMethod() && SearchName) {
1558 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1559 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1560 Lookup.first != Lookup.second;
1561 ++Lookup.first) {
1562 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1563 continue;
1564
1565 Decl *Found = *Lookup.first;
1566 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1567 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1568 Found = Tag->getDecl();
1569 }
1570
1571 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001572 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1573 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1574 // The record types structurally match, or the "from" translation
1575 // unit only had a forward declaration anyway; call it the same
1576 // function.
1577 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001578 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001579 }
1580 } else {
1581 // We have a forward declaration of this type, so adopt that forward
1582 // declaration rather than building a new one.
1583 AdoptDecl = FoundRecord;
1584 continue;
1585 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001586 }
1587
1588 ConflictingDecls.push_back(*Lookup.first);
1589 }
1590
1591 if (!ConflictingDecls.empty()) {
1592 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1593 ConflictingDecls.data(),
1594 ConflictingDecls.size());
1595 }
1596 }
1597
1598 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001599 RecordDecl *D2 = AdoptDecl;
1600 if (!D2) {
1601 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1602 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001603 D->getTagKind(),
1604 DC, Loc,
1605 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001606 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001607 D2 = D2CXX;
Douglas Gregor25791052010-02-12 00:09:27 +00001608
1609 if (D->isDefinition()) {
1610 // Add base classes.
1611 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1612 for (CXXRecordDecl::base_class_iterator
Douglas Gregor3996e242010-02-15 22:01:00 +00001613 Base1 = D1CXX->bases_begin(),
1614 FromBaseEnd = D1CXX->bases_end();
1615 Base1 != FromBaseEnd;
1616 ++Base1) {
1617 QualType T = Importer.Import(Base1->getType());
Douglas Gregor25791052010-02-12 00:09:27 +00001618 if (T.isNull())
1619 return 0;
1620
1621 Bases.push_back(
1622 new (Importer.getToContext())
Douglas Gregor3996e242010-02-15 22:01:00 +00001623 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1624 Base1->isVirtual(),
1625 Base1->isBaseOfClass(),
1626 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001627 T));
Douglas Gregor25791052010-02-12 00:09:27 +00001628 }
1629 if (!Bases.empty())
Douglas Gregor3996e242010-02-15 22:01:00 +00001630 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001631 }
Douglas Gregor25791052010-02-12 00:09:27 +00001632 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001633 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001634 DC, Loc,
1635 Name.getAsIdentifierInfo(),
1636 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001637 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001638 D2->setLexicalDeclContext(LexicalDC);
1639 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001640 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001641
Douglas Gregor3996e242010-02-15 22:01:00 +00001642 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001643
Douglas Gregor5c73e912010-02-11 00:48:18 +00001644 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001645 D2->startDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001646 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1647 FromMemEnd = D->decls_end();
1648 FromMem != FromMemEnd;
1649 ++FromMem)
1650 Importer.Import(*FromMem);
1651
Douglas Gregor3996e242010-02-15 22:01:00 +00001652 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001653 }
1654
Douglas Gregor3996e242010-02-15 22:01:00 +00001655 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001656}
1657
Douglas Gregor98c10182010-02-12 22:17:39 +00001658Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1659 // Import the major distinguishing characteristics of this enumerator.
1660 DeclContext *DC, *LexicalDC;
1661 DeclarationName Name;
1662 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001663 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001664 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001665
1666 QualType T = Importer.Import(D->getType());
1667 if (T.isNull())
1668 return 0;
1669
Douglas Gregor98c10182010-02-12 22:17:39 +00001670 // Determine whether there are any other declarations with the same name and
1671 // in the same context.
1672 if (!LexicalDC->isFunctionOrMethod()) {
1673 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1674 unsigned IDNS = Decl::IDNS_Ordinary;
1675 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1676 Lookup.first != Lookup.second;
1677 ++Lookup.first) {
1678 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1679 continue;
1680
1681 ConflictingDecls.push_back(*Lookup.first);
1682 }
1683
1684 if (!ConflictingDecls.empty()) {
1685 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1686 ConflictingDecls.data(),
1687 ConflictingDecls.size());
1688 if (!Name)
1689 return 0;
1690 }
1691 }
1692
1693 Expr *Init = Importer.Import(D->getInitExpr());
1694 if (D->getInitExpr() && !Init)
1695 return 0;
1696
1697 EnumConstantDecl *ToEnumerator
1698 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1699 Name.getAsIdentifierInfo(), T,
1700 Init, D->getInitVal());
1701 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001702 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001703 LexicalDC->addDecl(ToEnumerator);
1704 return ToEnumerator;
1705}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001706
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001707Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1708 // Import the major distinguishing characteristics of this function.
1709 DeclContext *DC, *LexicalDC;
1710 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001711 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001712 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001713 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001714
1715 // Try to find a function in our own ("to") context with the same name, same
1716 // type, and in the same context as the function we're importing.
1717 if (!LexicalDC->isFunctionOrMethod()) {
1718 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1719 unsigned IDNS = Decl::IDNS_Ordinary;
1720 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1721 Lookup.first != Lookup.second;
1722 ++Lookup.first) {
1723 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1724 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001725
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001726 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1727 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1728 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001729 if (Importer.IsStructurallyEquivalent(D->getType(),
1730 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001731 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001732 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001733 }
1734
1735 // FIXME: Check for overloading more carefully, e.g., by boosting
1736 // Sema::IsOverload out to the AST library.
1737
1738 // Function overloading is okay in C++.
1739 if (Importer.getToContext().getLangOptions().CPlusPlus)
1740 continue;
1741
1742 // Complain about inconsistent function types.
1743 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001744 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001745 Importer.ToDiag(FoundFunction->getLocation(),
1746 diag::note_odr_value_here)
1747 << FoundFunction->getType();
1748 }
1749 }
1750
1751 ConflictingDecls.push_back(*Lookup.first);
1752 }
1753
1754 if (!ConflictingDecls.empty()) {
1755 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1756 ConflictingDecls.data(),
1757 ConflictingDecls.size());
1758 if (!Name)
1759 return 0;
1760 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001761 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001762
1763 // Import the type.
1764 QualType T = Importer.Import(D->getType());
1765 if (T.isNull())
1766 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001767
1768 // Import the function parameters.
1769 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1770 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1771 P != PEnd; ++P) {
1772 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1773 if (!ToP)
1774 return 0;
1775
1776 Parameters.push_back(ToP);
1777 }
1778
1779 // Create the imported function.
1780 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor43f54792010-02-17 02:12:47 +00001781 FunctionDecl *ToFunction
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001782 = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1783 Name, T, TInfo, D->getStorageClass(),
1784 D->isInlineSpecified(),
1785 D->hasWrittenPrototype());
Douglas Gregor43f54792010-02-17 02:12:47 +00001786 ToFunction->setLexicalDeclContext(LexicalDC);
1787 Importer.Imported(D, ToFunction);
1788 LexicalDC->addDecl(ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001789
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001790 // Set the parameters.
1791 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00001792 Parameters[I]->setOwningFunction(ToFunction);
1793 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001794 }
Douglas Gregor43f54792010-02-17 02:12:47 +00001795 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001796
1797 // FIXME: Other bits to merge?
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001798
Douglas Gregor43f54792010-02-17 02:12:47 +00001799 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001800}
1801
Douglas Gregor5c73e912010-02-11 00:48:18 +00001802Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1803 // Import the major distinguishing characteristics of a variable.
1804 DeclContext *DC, *LexicalDC;
1805 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001806 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001807 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1808 return 0;
1809
1810 // Import the type.
1811 QualType T = Importer.Import(D->getType());
1812 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00001813 return 0;
1814
1815 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1816 Expr *BitWidth = Importer.Import(D->getBitWidth());
1817 if (!BitWidth && D->getBitWidth())
1818 return 0;
1819
1820 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1821 Loc, Name.getAsIdentifierInfo(),
1822 T, TInfo, BitWidth, D->isMutable());
1823 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001824 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001825 LexicalDC->addDecl(ToField);
1826 return ToField;
1827}
1828
Douglas Gregor7244b0b2010-02-17 00:34:30 +00001829Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1830 // Import the major distinguishing characteristics of an ivar.
1831 DeclContext *DC, *LexicalDC;
1832 DeclarationName Name;
1833 SourceLocation Loc;
1834 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1835 return 0;
1836
1837 // Determine whether we've already imported this ivar
1838 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1839 Lookup.first != Lookup.second;
1840 ++Lookup.first) {
1841 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
1842 if (Importer.IsStructurallyEquivalent(D->getType(),
1843 FoundIvar->getType())) {
1844 Importer.Imported(D, FoundIvar);
1845 return FoundIvar;
1846 }
1847
1848 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
1849 << Name << D->getType() << FoundIvar->getType();
1850 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
1851 << FoundIvar->getType();
1852 return 0;
1853 }
1854 }
1855
1856 // Import the type.
1857 QualType T = Importer.Import(D->getType());
1858 if (T.isNull())
1859 return 0;
1860
1861 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1862 Expr *BitWidth = Importer.Import(D->getBitWidth());
1863 if (!BitWidth && D->getBitWidth())
1864 return 0;
1865
1866 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC,
1867 Loc, Name.getAsIdentifierInfo(),
1868 T, TInfo, D->getAccessControl(),
1869 BitWidth);
1870 ToIvar->setLexicalDeclContext(LexicalDC);
1871 Importer.Imported(D, ToIvar);
1872 LexicalDC->addDecl(ToIvar);
1873 return ToIvar;
1874
1875}
1876
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001877Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1878 // Import the major distinguishing characteristics of a variable.
1879 DeclContext *DC, *LexicalDC;
1880 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001881 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001882 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001883 return 0;
1884
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001885 // Try to find a variable in our own ("to") context with the same name and
1886 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00001887 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001888 VarDecl *MergeWithVar = 0;
1889 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1890 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001891 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001892 Lookup.first != Lookup.second;
1893 ++Lookup.first) {
1894 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1895 continue;
1896
1897 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1898 // We have found a variable that we may need to merge with. Check it.
1899 if (isExternalLinkage(FoundVar->getLinkage()) &&
1900 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001901 if (Importer.IsStructurallyEquivalent(D->getType(),
1902 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001903 MergeWithVar = FoundVar;
1904 break;
1905 }
1906
Douglas Gregor56521c52010-02-12 17:23:39 +00001907 const ArrayType *FoundArray
1908 = Importer.getToContext().getAsArrayType(FoundVar->getType());
1909 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00001910 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00001911 if (FoundArray && TArray) {
1912 if (isa<IncompleteArrayType>(FoundArray) &&
1913 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001914 // Import the type.
1915 QualType T = Importer.Import(D->getType());
1916 if (T.isNull())
1917 return 0;
1918
Douglas Gregor56521c52010-02-12 17:23:39 +00001919 FoundVar->setType(T);
1920 MergeWithVar = FoundVar;
1921 break;
1922 } else if (isa<IncompleteArrayType>(TArray) &&
1923 isa<ConstantArrayType>(FoundArray)) {
1924 MergeWithVar = FoundVar;
1925 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00001926 }
1927 }
1928
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001929 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001930 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001931 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1932 << FoundVar->getType();
1933 }
1934 }
1935
1936 ConflictingDecls.push_back(*Lookup.first);
1937 }
1938
1939 if (MergeWithVar) {
1940 // An equivalent variable with external linkage has been found. Link
1941 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001942 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001943
1944 if (VarDecl *DDef = D->getDefinition()) {
1945 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1946 Importer.ToDiag(ExistingDef->getLocation(),
1947 diag::err_odr_variable_multiple_def)
1948 << Name;
1949 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1950 } else {
1951 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00001952 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001953 }
1954 }
1955
1956 return MergeWithVar;
1957 }
1958
1959 if (!ConflictingDecls.empty()) {
1960 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1961 ConflictingDecls.data(),
1962 ConflictingDecls.size());
1963 if (!Name)
1964 return 0;
1965 }
1966 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001967
Douglas Gregorb4964f72010-02-15 23:54:17 +00001968 // Import the type.
1969 QualType T = Importer.Import(D->getType());
1970 if (T.isNull())
1971 return 0;
1972
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001973 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001974 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001975 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1976 Name.getAsIdentifierInfo(), T, TInfo,
1977 D->getStorageClass());
Douglas Gregor62d311f2010-02-09 19:21:46 +00001978 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001979 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001980 LexicalDC->addDecl(ToVar);
1981
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001982 // Merge the initializer.
1983 // FIXME: Can we really import any initializer? Alternatively, we could force
1984 // ourselves to import every declaration of a variable and then only use
1985 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00001986 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001987
1988 // FIXME: Other bits to merge?
1989
1990 return ToVar;
1991}
1992
Douglas Gregor8b228d72010-02-17 21:22:52 +00001993Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
1994 // Parameters are created in the translation unit's context, then moved
1995 // into the function declaration's context afterward.
1996 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
1997
1998 // Import the name of this declaration.
1999 DeclarationName Name = Importer.Import(D->getDeclName());
2000 if (D->getDeclName() && !Name)
2001 return 0;
2002
2003 // Import the location of this declaration.
2004 SourceLocation Loc = Importer.Import(D->getLocation());
2005
2006 // Import the parameter's type.
2007 QualType T = Importer.Import(D->getType());
2008 if (T.isNull())
2009 return 0;
2010
2011 // Create the imported parameter.
2012 ImplicitParamDecl *ToParm
2013 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2014 Loc, Name.getAsIdentifierInfo(),
2015 T);
2016 return Importer.Imported(D, ToParm);
2017}
2018
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002019Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2020 // Parameters are created in the translation unit's context, then moved
2021 // into the function declaration's context afterward.
2022 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2023
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002024 // Import the name of this declaration.
2025 DeclarationName Name = Importer.Import(D->getDeclName());
2026 if (D->getDeclName() && !Name)
2027 return 0;
2028
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002029 // Import the location of this declaration.
2030 SourceLocation Loc = Importer.Import(D->getLocation());
2031
2032 // Import the parameter's type.
2033 QualType T = Importer.Import(D->getType());
2034 if (T.isNull())
2035 return 0;
2036
2037 // Create the imported parameter.
2038 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2039 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2040 Loc, Name.getAsIdentifierInfo(),
2041 T, TInfo, D->getStorageClass(),
2042 /*FIXME: Default argument*/ 0);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002043 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002044}
2045
Douglas Gregor43f54792010-02-17 02:12:47 +00002046Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2047 // Import the major distinguishing characteristics of a method.
2048 DeclContext *DC, *LexicalDC;
2049 DeclarationName Name;
2050 SourceLocation Loc;
2051 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2052 return 0;
2053
2054 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2055 Lookup.first != Lookup.second;
2056 ++Lookup.first) {
2057 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2058 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2059 continue;
2060
2061 // Check return types.
2062 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2063 FoundMethod->getResultType())) {
2064 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2065 << D->isInstanceMethod() << Name
2066 << D->getResultType() << FoundMethod->getResultType();
2067 Importer.ToDiag(FoundMethod->getLocation(),
2068 diag::note_odr_objc_method_here)
2069 << D->isInstanceMethod() << Name;
2070 return 0;
2071 }
2072
2073 // Check the number of parameters.
2074 if (D->param_size() != FoundMethod->param_size()) {
2075 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2076 << D->isInstanceMethod() << Name
2077 << D->param_size() << FoundMethod->param_size();
2078 Importer.ToDiag(FoundMethod->getLocation(),
2079 diag::note_odr_objc_method_here)
2080 << D->isInstanceMethod() << Name;
2081 return 0;
2082 }
2083
2084 // Check parameter types.
2085 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2086 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2087 P != PEnd; ++P, ++FoundP) {
2088 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2089 (*FoundP)->getType())) {
2090 Importer.FromDiag((*P)->getLocation(),
2091 diag::err_odr_objc_method_param_type_inconsistent)
2092 << D->isInstanceMethod() << Name
2093 << (*P)->getType() << (*FoundP)->getType();
2094 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2095 << (*FoundP)->getType();
2096 return 0;
2097 }
2098 }
2099
2100 // Check variadic/non-variadic.
2101 // Check the number of parameters.
2102 if (D->isVariadic() != FoundMethod->isVariadic()) {
2103 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2104 << D->isInstanceMethod() << Name;
2105 Importer.ToDiag(FoundMethod->getLocation(),
2106 diag::note_odr_objc_method_here)
2107 << D->isInstanceMethod() << Name;
2108 return 0;
2109 }
2110
2111 // FIXME: Any other bits we need to merge?
2112 return Importer.Imported(D, FoundMethod);
2113 }
2114 }
2115
2116 // Import the result type.
2117 QualType ResultTy = Importer.Import(D->getResultType());
2118 if (ResultTy.isNull())
2119 return 0;
2120
2121 ObjCMethodDecl *ToMethod
2122 = ObjCMethodDecl::Create(Importer.getToContext(),
2123 Loc,
2124 Importer.Import(D->getLocEnd()),
2125 Name.getObjCSelector(),
2126 ResultTy, DC,
2127 D->isInstanceMethod(),
2128 D->isVariadic(),
2129 D->isSynthesized(),
2130 D->getImplementationControl());
2131
2132 // FIXME: When we decide to merge method definitions, we'll need to
2133 // deal with implicit parameters.
2134
2135 // Import the parameters
2136 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2137 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2138 FromPEnd = D->param_end();
2139 FromP != FromPEnd;
2140 ++FromP) {
2141 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2142 if (!ToP)
2143 return 0;
2144
2145 ToParams.push_back(ToP);
2146 }
2147
2148 // Set the parameters.
2149 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2150 ToParams[I]->setOwningFunction(ToMethod);
2151 ToMethod->addDecl(ToParams[I]);
2152 }
2153 ToMethod->setMethodParams(Importer.getToContext(),
2154 ToParams.data(), ToParams.size());
2155
2156 ToMethod->setLexicalDeclContext(LexicalDC);
2157 Importer.Imported(D, ToMethod);
2158 LexicalDC->addDecl(ToMethod);
2159 return ToMethod;
2160}
2161
Douglas Gregor98d156a2010-02-17 16:12:00 +00002162Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
2163 // Import the major distinguishing characteristics of an @protocol.
2164 DeclContext *DC, *LexicalDC;
2165 DeclarationName Name;
2166 SourceLocation Loc;
2167 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2168 return 0;
2169
2170 ObjCProtocolDecl *MergeWithProtocol = 0;
2171 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2172 Lookup.first != Lookup.second;
2173 ++Lookup.first) {
2174 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2175 continue;
2176
2177 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2178 break;
2179 }
2180
2181 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2182 if (!ToProto || ToProto->isForwardDecl()) {
2183 if (!ToProto) {
2184 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2185 Name.getAsIdentifierInfo());
2186 ToProto->setForwardDecl(D->isForwardDecl());
2187 ToProto->setLexicalDeclContext(LexicalDC);
2188 LexicalDC->addDecl(ToProto);
2189 }
2190 Importer.Imported(D, ToProto);
2191
2192 // Import protocols
2193 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2194 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2195 ObjCProtocolDecl::protocol_loc_iterator
2196 FromProtoLoc = D->protocol_loc_begin();
2197 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2198 FromProtoEnd = D->protocol_end();
2199 FromProto != FromProtoEnd;
2200 ++FromProto, ++FromProtoLoc) {
2201 ObjCProtocolDecl *ToProto
2202 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2203 if (!ToProto)
2204 return 0;
2205 Protocols.push_back(ToProto);
2206 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2207 }
2208
2209 // FIXME: If we're merging, make sure that the protocol list is the same.
2210 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2211 ProtocolLocs.data(), Importer.getToContext());
2212 } else {
2213 Importer.Imported(D, ToProto);
2214 }
2215
2216 // Import all of the members of this class.
2217 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2218 FromMemEnd = D->decls_end();
2219 FromMem != FromMemEnd;
2220 ++FromMem)
2221 Importer.Import(*FromMem);
2222
2223 return ToProto;
2224}
2225
Douglas Gregor45635322010-02-16 01:20:57 +00002226Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2227 // Import the major distinguishing characteristics of an @interface.
2228 DeclContext *DC, *LexicalDC;
2229 DeclarationName Name;
2230 SourceLocation Loc;
2231 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2232 return 0;
2233
2234 ObjCInterfaceDecl *MergeWithIface = 0;
2235 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2236 Lookup.first != Lookup.second;
2237 ++Lookup.first) {
2238 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2239 continue;
2240
2241 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2242 break;
2243 }
2244
2245 ObjCInterfaceDecl *ToIface = MergeWithIface;
2246 if (!ToIface || ToIface->isForwardDecl()) {
2247 if (!ToIface) {
2248 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2249 DC, Loc,
2250 Name.getAsIdentifierInfo(),
2251 Importer.Import(D->getClassLoc()),
2252 D->isForwardDecl(),
2253 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002254 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002255 ToIface->setLexicalDeclContext(LexicalDC);
2256 LexicalDC->addDecl(ToIface);
2257 }
2258 Importer.Imported(D, ToIface);
2259
Douglas Gregor45635322010-02-16 01:20:57 +00002260 if (D->getSuperClass()) {
2261 ObjCInterfaceDecl *Super
2262 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2263 if (!Super)
2264 return 0;
2265
2266 ToIface->setSuperClass(Super);
2267 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2268 }
2269
2270 // Import protocols
2271 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2272 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2273 ObjCInterfaceDecl::protocol_loc_iterator
2274 FromProtoLoc = D->protocol_loc_begin();
2275 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2276 FromProtoEnd = D->protocol_end();
2277 FromProto != FromProtoEnd;
2278 ++FromProto, ++FromProtoLoc) {
2279 ObjCProtocolDecl *ToProto
2280 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2281 if (!ToProto)
2282 return 0;
2283 Protocols.push_back(ToProto);
2284 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2285 }
2286
2287 // FIXME: If we're merging, make sure that the protocol list is the same.
2288 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2289 ProtocolLocs.data(), Importer.getToContext());
2290
2291 // FIXME: Import categories
2292
2293 // Import @end range
2294 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2295 } else {
2296 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002297
2298 // Check for consistency of superclasses.
2299 DeclarationName FromSuperName, ToSuperName;
2300 if (D->getSuperClass())
2301 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2302 if (ToIface->getSuperClass())
2303 ToSuperName = ToIface->getSuperClass()->getDeclName();
2304 if (FromSuperName != ToSuperName) {
2305 Importer.ToDiag(ToIface->getLocation(),
2306 diag::err_odr_objc_superclass_inconsistent)
2307 << ToIface->getDeclName();
2308 if (ToIface->getSuperClass())
2309 Importer.ToDiag(ToIface->getSuperClassLoc(),
2310 diag::note_odr_objc_superclass)
2311 << ToIface->getSuperClass()->getDeclName();
2312 else
2313 Importer.ToDiag(ToIface->getLocation(),
2314 diag::note_odr_objc_missing_superclass);
2315 if (D->getSuperClass())
2316 Importer.FromDiag(D->getSuperClassLoc(),
2317 diag::note_odr_objc_superclass)
2318 << D->getSuperClass()->getDeclName();
2319 else
2320 Importer.FromDiag(D->getLocation(),
2321 diag::note_odr_objc_missing_superclass);
2322 return 0;
2323 }
Douglas Gregor45635322010-02-16 01:20:57 +00002324 }
2325
2326 // Import all of the members of this class.
2327 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2328 FromMemEnd = D->decls_end();
2329 FromMem != FromMemEnd;
2330 ++FromMem)
2331 Importer.Import(*FromMem);
2332
2333 // If we have an @implementation, import it as well.
2334 if (D->getImplementation()) {
2335 ObjCImplementationDecl *Impl
2336 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2337 if (!Impl)
2338 return 0;
2339
2340 ToIface->setImplementation(Impl);
2341 }
2342
Douglas Gregor98d156a2010-02-17 16:12:00 +00002343 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00002344}
2345
Douglas Gregora11c4582010-02-17 18:02:10 +00002346Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2347 // Import the major distinguishing characteristics of an @property.
2348 DeclContext *DC, *LexicalDC;
2349 DeclarationName Name;
2350 SourceLocation Loc;
2351 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2352 return 0;
2353
2354 // Check whether we have already imported this property.
2355 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2356 Lookup.first != Lookup.second;
2357 ++Lookup.first) {
2358 if (ObjCPropertyDecl *FoundProp
2359 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2360 // Check property types.
2361 if (!Importer.IsStructurallyEquivalent(D->getType(),
2362 FoundProp->getType())) {
2363 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2364 << Name << D->getType() << FoundProp->getType();
2365 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2366 << FoundProp->getType();
2367 return 0;
2368 }
2369
2370 // FIXME: Check property attributes, getters, setters, etc.?
2371
2372 // Consider these properties to be equivalent.
2373 Importer.Imported(D, FoundProp);
2374 return FoundProp;
2375 }
2376 }
2377
2378 // Import the type.
2379 QualType T = Importer.Import(D->getType());
2380 if (T.isNull())
2381 return 0;
2382
2383 // Create the new property.
2384 ObjCPropertyDecl *ToProperty
2385 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2386 Name.getAsIdentifierInfo(),
2387 Importer.Import(D->getAtLoc()),
2388 T,
2389 D->getPropertyImplementation());
2390 Importer.Imported(D, ToProperty);
2391 ToProperty->setLexicalDeclContext(LexicalDC);
2392 LexicalDC->addDecl(ToProperty);
2393
2394 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2395 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2396 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2397 ToProperty->setGetterMethodDecl(
2398 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2399 ToProperty->setSetterMethodDecl(
2400 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2401 ToProperty->setPropertyIvarDecl(
2402 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2403 return ToProperty;
2404}
2405
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002406//----------------------------------------------------------------------------
2407// Import Statements
2408//----------------------------------------------------------------------------
2409
2410Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2411 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2412 << S->getStmtClassName();
2413 return 0;
2414}
2415
2416//----------------------------------------------------------------------------
2417// Import Expressions
2418//----------------------------------------------------------------------------
2419Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2420 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2421 << E->getStmtClassName();
2422 return 0;
2423}
2424
2425Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2426 QualType T = Importer.Import(E->getType());
2427 if (T.isNull())
2428 return 0;
2429
2430 return new (Importer.getToContext())
2431 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2432}
2433
Douglas Gregor98c10182010-02-12 22:17:39 +00002434Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2435 QualType T = Importer.Import(E->getType());
2436 if (T.isNull())
2437 return 0;
2438
2439 Expr *SubExpr = Importer.Import(E->getSubExpr());
2440 if (!SubExpr)
2441 return 0;
2442
2443 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2444 SubExpr,
2445 E->isLvalueCast());
2446}
2447
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002448ASTImporter::ASTImporter(Diagnostic &Diags,
2449 ASTContext &ToContext, FileManager &ToFileManager,
2450 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00002451 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00002452 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002453 Diags(Diags) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00002454 ImportedDecls[FromContext.getTranslationUnitDecl()]
2455 = ToContext.getTranslationUnitDecl();
2456}
2457
2458ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002459
2460QualType ASTImporter::Import(QualType FromT) {
2461 if (FromT.isNull())
2462 return QualType();
2463
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002464 // Check whether we've already imported this type.
2465 llvm::DenseMap<Type *, Type *>::iterator Pos
2466 = ImportedTypes.find(FromT.getTypePtr());
2467 if (Pos != ImportedTypes.end())
2468 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002469
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002470 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00002471 ASTNodeImporter Importer(*this);
2472 QualType ToT = Importer.Visit(FromT.getTypePtr());
2473 if (ToT.isNull())
2474 return ToT;
2475
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002476 // Record the imported type.
2477 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2478
Douglas Gregor96e578d2010-02-05 17:54:41 +00002479 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2480}
2481
Douglas Gregor62d311f2010-02-09 19:21:46 +00002482TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002483 if (!FromTSI)
2484 return FromTSI;
2485
2486 // FIXME: For now we just create a "trivial" type source info based
2487 // on the type and a seingle location. Implement a real version of
2488 // this.
2489 QualType T = Import(FromTSI->getType());
2490 if (T.isNull())
2491 return 0;
2492
2493 return ToContext.getTrivialTypeSourceInfo(T,
2494 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002495}
2496
2497Decl *ASTImporter::Import(Decl *FromD) {
2498 if (!FromD)
2499 return 0;
2500
2501 // Check whether we've already imported this declaration.
2502 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2503 if (Pos != ImportedDecls.end())
2504 return Pos->second;
2505
2506 // Import the type
2507 ASTNodeImporter Importer(*this);
2508 Decl *ToD = Importer.Visit(FromD);
2509 if (!ToD)
2510 return 0;
2511
2512 // Record the imported declaration.
2513 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002514
2515 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2516 // Keep track of anonymous tags that have an associated typedef.
2517 if (FromTag->getTypedefForAnonDecl())
2518 AnonTagsWithPendingTypedefs.push_back(FromTag);
2519 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2520 // When we've finished transforming a typedef, see whether it was the
2521 // typedef for an anonymous tag.
2522 for (llvm::SmallVector<TagDecl *, 4>::iterator
2523 FromTag = AnonTagsWithPendingTypedefs.begin(),
2524 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2525 FromTag != FromTagEnd; ++FromTag) {
2526 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2527 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2528 // We found the typedef for an anonymous tag; link them.
2529 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2530 AnonTagsWithPendingTypedefs.erase(FromTag);
2531 break;
2532 }
2533 }
2534 }
2535 }
2536
Douglas Gregor62d311f2010-02-09 19:21:46 +00002537 return ToD;
2538}
2539
2540DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
2541 if (!FromDC)
2542 return FromDC;
2543
2544 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
2545}
2546
2547Expr *ASTImporter::Import(Expr *FromE) {
2548 if (!FromE)
2549 return 0;
2550
2551 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
2552}
2553
2554Stmt *ASTImporter::Import(Stmt *FromS) {
2555 if (!FromS)
2556 return 0;
2557
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002558 // Check whether we've already imported this declaration.
2559 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
2560 if (Pos != ImportedStmts.end())
2561 return Pos->second;
2562
2563 // Import the type
2564 ASTNodeImporter Importer(*this);
2565 Stmt *ToS = Importer.Visit(FromS);
2566 if (!ToS)
2567 return 0;
2568
2569 // Record the imported declaration.
2570 ImportedStmts[FromS] = ToS;
2571 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002572}
2573
2574NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
2575 if (!FromNNS)
2576 return 0;
2577
2578 // FIXME: Implement!
2579 return 0;
2580}
2581
2582SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
2583 if (FromLoc.isInvalid())
2584 return SourceLocation();
2585
Douglas Gregor811663e2010-02-10 00:15:17 +00002586 SourceManager &FromSM = FromContext.getSourceManager();
2587
2588 // For now, map everything down to its spelling location, so that we
2589 // don't have to import macro instantiations.
2590 // FIXME: Import macro instantiations!
2591 FromLoc = FromSM.getSpellingLoc(FromLoc);
2592 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
2593 SourceManager &ToSM = ToContext.getSourceManager();
2594 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
2595 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002596}
2597
2598SourceRange ASTImporter::Import(SourceRange FromRange) {
2599 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
2600}
2601
Douglas Gregor811663e2010-02-10 00:15:17 +00002602FileID ASTImporter::Import(FileID FromID) {
2603 llvm::DenseMap<unsigned, FileID>::iterator Pos
2604 = ImportedFileIDs.find(FromID.getHashValue());
2605 if (Pos != ImportedFileIDs.end())
2606 return Pos->second;
2607
2608 SourceManager &FromSM = FromContext.getSourceManager();
2609 SourceManager &ToSM = ToContext.getSourceManager();
2610 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
2611 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
2612
2613 // Include location of this file.
2614 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
2615
2616 // Map the FileID for to the "to" source manager.
2617 FileID ToID;
2618 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
2619 if (Cache->Entry) {
2620 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
2621 // disk again
2622 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
2623 // than mmap the files several times.
2624 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
2625 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
2626 FromSLoc.getFile().getFileCharacteristic());
2627 } else {
2628 // FIXME: We want to re-use the existing MemoryBuffer!
2629 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
2630 llvm::MemoryBuffer *ToBuf
2631 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
2632 FromBuf->getBufferEnd(),
2633 FromBuf->getBufferIdentifier());
2634 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
2635 }
2636
2637
2638 ImportedFileIDs[FromID.getHashValue()] = ToID;
2639 return ToID;
2640}
2641
Douglas Gregor96e578d2010-02-05 17:54:41 +00002642DeclarationName ASTImporter::Import(DeclarationName FromName) {
2643 if (!FromName)
2644 return DeclarationName();
2645
2646 switch (FromName.getNameKind()) {
2647 case DeclarationName::Identifier:
2648 return Import(FromName.getAsIdentifierInfo());
2649
2650 case DeclarationName::ObjCZeroArgSelector:
2651 case DeclarationName::ObjCOneArgSelector:
2652 case DeclarationName::ObjCMultiArgSelector:
2653 return Import(FromName.getObjCSelector());
2654
2655 case DeclarationName::CXXConstructorName: {
2656 QualType T = Import(FromName.getCXXNameType());
2657 if (T.isNull())
2658 return DeclarationName();
2659
2660 return ToContext.DeclarationNames.getCXXConstructorName(
2661 ToContext.getCanonicalType(T));
2662 }
2663
2664 case DeclarationName::CXXDestructorName: {
2665 QualType T = Import(FromName.getCXXNameType());
2666 if (T.isNull())
2667 return DeclarationName();
2668
2669 return ToContext.DeclarationNames.getCXXDestructorName(
2670 ToContext.getCanonicalType(T));
2671 }
2672
2673 case DeclarationName::CXXConversionFunctionName: {
2674 QualType T = Import(FromName.getCXXNameType());
2675 if (T.isNull())
2676 return DeclarationName();
2677
2678 return ToContext.DeclarationNames.getCXXConversionFunctionName(
2679 ToContext.getCanonicalType(T));
2680 }
2681
2682 case DeclarationName::CXXOperatorName:
2683 return ToContext.DeclarationNames.getCXXOperatorName(
2684 FromName.getCXXOverloadedOperator());
2685
2686 case DeclarationName::CXXLiteralOperatorName:
2687 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
2688 Import(FromName.getCXXLiteralIdentifier()));
2689
2690 case DeclarationName::CXXUsingDirective:
2691 // FIXME: STATICS!
2692 return DeclarationName::getUsingDirectiveName();
2693 }
2694
2695 // Silence bogus GCC warning
2696 return DeclarationName();
2697}
2698
2699IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
2700 if (!FromId)
2701 return 0;
2702
2703 return &ToContext.Idents.get(FromId->getName());
2704}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002705
Douglas Gregor43f54792010-02-17 02:12:47 +00002706Selector ASTImporter::Import(Selector FromSel) {
2707 if (FromSel.isNull())
2708 return Selector();
2709
2710 llvm::SmallVector<IdentifierInfo *, 4> Idents;
2711 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
2712 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
2713 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
2714 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
2715}
2716
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002717DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
2718 DeclContext *DC,
2719 unsigned IDNS,
2720 NamedDecl **Decls,
2721 unsigned NumDecls) {
2722 return Name;
2723}
2724
2725DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002726 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
2727 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002728}
2729
2730DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002731 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
2732 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002733}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002734
2735Decl *ASTImporter::Imported(Decl *From, Decl *To) {
2736 ImportedDecls[From] = To;
2737 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00002738}
Douglas Gregorb4964f72010-02-15 23:54:17 +00002739
2740bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
2741 llvm::DenseMap<Type *, Type *>::iterator Pos
2742 = ImportedTypes.find(From.getTypePtr());
2743 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
2744 return true;
2745
2746 StructuralEquivalenceContext SEC(FromContext, ToContext, Diags,
2747 NonEquivalentDecls);
2748 return SEC.IsStructurallyEquivalent(From, To);
2749}