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