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