blob: e064e78c1ce862c8931310bb6554940e75b0bf2c [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregorfa7a0e52010-02-10 17:47:19 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000023#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
26#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000027#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000028
29using namespace clang;
30
31namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000032 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000033 public DeclVisitor<ASTNodeImporter, Decl *>,
34 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000035 ASTImporter &Importer;
36
37 public:
38 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
39
40 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000041 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000042 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000043
44 // Importing types
Douglas Gregore4c83e42010-02-09 22:48:33 +000045 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000046 QualType VisitBuiltinType(BuiltinType *T);
47 QualType VisitComplexType(ComplexType *T);
48 QualType VisitPointerType(PointerType *T);
49 QualType VisitBlockPointerType(BlockPointerType *T);
50 QualType VisitLValueReferenceType(LValueReferenceType *T);
51 QualType VisitRValueReferenceType(RValueReferenceType *T);
52 QualType VisitMemberPointerType(MemberPointerType *T);
53 QualType VisitConstantArrayType(ConstantArrayType *T);
54 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
55 QualType VisitVariableArrayType(VariableArrayType *T);
56 // FIXME: DependentSizedArrayType
57 // FIXME: DependentSizedExtVectorType
58 QualType VisitVectorType(VectorType *T);
59 QualType VisitExtVectorType(ExtVectorType *T);
60 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
61 QualType VisitFunctionProtoType(FunctionProtoType *T);
62 // FIXME: UnresolvedUsingType
63 QualType VisitTypedefType(TypedefType *T);
64 QualType VisitTypeOfExprType(TypeOfExprType *T);
65 // FIXME: DependentTypeOfExprType
66 QualType VisitTypeOfType(TypeOfType *T);
67 QualType VisitDecltypeType(DecltypeType *T);
68 // FIXME: DependentDecltypeType
69 QualType VisitRecordType(RecordType *T);
70 QualType VisitEnumType(EnumType *T);
71 QualType VisitElaboratedType(ElaboratedType *T);
72 // FIXME: TemplateTypeParmType
73 // FIXME: SubstTemplateTypeParmType
74 // FIXME: TemplateSpecializationType
75 QualType VisitQualifiedNameType(QualifiedNameType *T);
76 // FIXME: TypenameType
77 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
78 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
83 SourceLocation &Loc);
Douglas Gregor5c73e912010-02-11 00:48:18 +000084 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000085 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregore4c83e42010-02-09 22:48:33 +000086 Decl *VisitDecl(Decl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000087 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000088 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000089 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000090 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000091 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000092 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +000093 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000094 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +000095 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000096 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +000097 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +000098 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +000099 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000100 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000101 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000102 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000103 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
104
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000105 // Importing statements
106 Stmt *VisitStmt(Stmt *S);
107
108 // Importing expressions
109 Expr *VisitExpr(Expr *E);
110 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000111 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000112 };
113}
114
115//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000116// Structural Equivalence
117//----------------------------------------------------------------------------
118
119namespace {
120 struct StructuralEquivalenceContext {
121 /// \brief AST contexts for which we are checking structural equivalence.
122 ASTContext &C1, &C2;
123
124 /// \brief Diagnostic object used to emit diagnostics.
125 Diagnostic &Diags;
126
127 /// \brief The set of "tentative" equivalences between two canonical
128 /// declarations, mapping from a declaration in the first context to the
129 /// declaration in the second context that we believe to be equivalent.
130 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
131
132 /// \brief Queue of declarations in the first context whose equivalence
133 /// with a declaration in the second context still needs to be verified.
134 std::deque<Decl *> DeclsToCheck;
135
Douglas Gregorb4964f72010-02-15 23:54:17 +0000136 /// \brief Declaration (from, to) pairs that are known not to be equivalent
137 /// (which we have already complained about).
138 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
139
Douglas Gregor3996e242010-02-15 22:01:00 +0000140 /// \brief Whether we're being strict about the spelling of types when
141 /// unifying two types.
142 bool StrictTypeSpelling;
143
144 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
145 Diagnostic &Diags,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000146 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000147 bool StrictTypeSpelling = false)
Douglas Gregorb4964f72010-02-15 23:54:17 +0000148 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
149 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000150
151 /// \brief Determine whether the two declarations are structurally
152 /// equivalent.
153 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
154
155 /// \brief Determine whether the two types are structurally equivalent.
156 bool IsStructurallyEquivalent(QualType T1, QualType T2);
157
158 private:
159 /// \brief Finish checking all of the structural equivalences.
160 ///
161 /// \returns true if an error occurred, false otherwise.
162 bool Finish();
163
164 public:
165 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
166 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
167 }
168
169 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
170 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
171 }
172 };
173}
174
175static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
176 QualType T1, QualType T2);
177static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
178 Decl *D1, Decl *D2);
179
180/// \brief Determine if two APInts have the same value, after zero-extending
181/// one of them (if needed!) to ensure that the bit-widths match.
182static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
183 if (I1.getBitWidth() == I2.getBitWidth())
184 return I1 == I2;
185
186 if (I1.getBitWidth() > I2.getBitWidth())
187 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
188
189 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
190}
191
192/// \brief Determine if two APSInts have the same value, zero- or sign-extending
193/// as needed.
194static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
195 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
196 return I1 == I2;
197
198 // Check for a bit-width mismatch.
199 if (I1.getBitWidth() > I2.getBitWidth())
200 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
201 else if (I2.getBitWidth() > I1.getBitWidth())
202 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
203
204 // We have a signedness mismatch. Turn the signed value into an unsigned
205 // value.
206 if (I1.isSigned()) {
207 if (I1.isNegative())
208 return false;
209
210 return llvm::APSInt(I1, true) == I2;
211 }
212
213 if (I2.isNegative())
214 return false;
215
216 return I1 == llvm::APSInt(I2, true);
217}
218
219/// \brief Determine structural equivalence of two expressions.
220static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
221 Expr *E1, Expr *E2) {
222 if (!E1 || !E2)
223 return E1 == E2;
224
225 // FIXME: Actually perform a structural comparison!
226 return true;
227}
228
229/// \brief Determine whether two identifiers are equivalent.
230static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
231 const IdentifierInfo *Name2) {
232 if (!Name1 || !Name2)
233 return Name1 == Name2;
234
235 return Name1->getName() == Name2->getName();
236}
237
238/// \brief Determine whether two nested-name-specifiers are equivalent.
239static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
240 NestedNameSpecifier *NNS1,
241 NestedNameSpecifier *NNS2) {
242 // FIXME: Implement!
243 return true;
244}
245
246/// \brief Determine whether two template arguments are equivalent.
247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
248 const TemplateArgument &Arg1,
249 const TemplateArgument &Arg2) {
250 // FIXME: Implement!
251 return true;
252}
253
254/// \brief Determine structural equivalence for the common part of array
255/// types.
256static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
257 const ArrayType *Array1,
258 const ArrayType *Array2) {
259 if (!IsStructurallyEquivalent(Context,
260 Array1->getElementType(),
261 Array2->getElementType()))
262 return false;
263 if (Array1->getSizeModifier() != Array2->getSizeModifier())
264 return false;
265 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
266 return false;
267
268 return true;
269}
270
271/// \brief Determine structural equivalence of two types.
272static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
273 QualType T1, QualType T2) {
274 if (T1.isNull() || T2.isNull())
275 return T1.isNull() && T2.isNull();
276
277 if (!Context.StrictTypeSpelling) {
278 // We aren't being strict about token-to-token equivalence of types,
279 // so map down to the canonical type.
280 T1 = Context.C1.getCanonicalType(T1);
281 T2 = Context.C2.getCanonicalType(T2);
282 }
283
284 if (T1.getQualifiers() != T2.getQualifiers())
285 return false;
286
Douglas Gregorb4964f72010-02-15 23:54:17 +0000287 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000288
Douglas Gregorb4964f72010-02-15 23:54:17 +0000289 if (T1->getTypeClass() != T2->getTypeClass()) {
290 // Compare function types with prototypes vs. without prototypes as if
291 // both did not have prototypes.
292 if (T1->getTypeClass() == Type::FunctionProto &&
293 T2->getTypeClass() == Type::FunctionNoProto)
294 TC = Type::FunctionNoProto;
295 else if (T1->getTypeClass() == Type::FunctionNoProto &&
296 T2->getTypeClass() == Type::FunctionProto)
297 TC = Type::FunctionNoProto;
298 else
299 return false;
300 }
301
302 switch (TC) {
303 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000304 // FIXME: Deal with Char_S/Char_U.
305 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
306 return false;
307 break;
308
309 case Type::Complex:
310 if (!IsStructurallyEquivalent(Context,
311 cast<ComplexType>(T1)->getElementType(),
312 cast<ComplexType>(T2)->getElementType()))
313 return false;
314 break;
315
316 case Type::Pointer:
317 if (!IsStructurallyEquivalent(Context,
318 cast<PointerType>(T1)->getPointeeType(),
319 cast<PointerType>(T2)->getPointeeType()))
320 return false;
321 break;
322
323 case Type::BlockPointer:
324 if (!IsStructurallyEquivalent(Context,
325 cast<BlockPointerType>(T1)->getPointeeType(),
326 cast<BlockPointerType>(T2)->getPointeeType()))
327 return false;
328 break;
329
330 case Type::LValueReference:
331 case Type::RValueReference: {
332 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
333 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
334 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
335 return false;
336 if (Ref1->isInnerRef() != Ref2->isInnerRef())
337 return false;
338 if (!IsStructurallyEquivalent(Context,
339 Ref1->getPointeeTypeAsWritten(),
340 Ref2->getPointeeTypeAsWritten()))
341 return false;
342 break;
343 }
344
345 case Type::MemberPointer: {
346 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
347 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
348 if (!IsStructurallyEquivalent(Context,
349 MemPtr1->getPointeeType(),
350 MemPtr2->getPointeeType()))
351 return false;
352 if (!IsStructurallyEquivalent(Context,
353 QualType(MemPtr1->getClass(), 0),
354 QualType(MemPtr2->getClass(), 0)))
355 return false;
356 break;
357 }
358
359 case Type::ConstantArray: {
360 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
361 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
362 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
363 return false;
364
365 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
366 return false;
367 break;
368 }
369
370 case Type::IncompleteArray:
371 if (!IsArrayStructurallyEquivalent(Context,
372 cast<ArrayType>(T1),
373 cast<ArrayType>(T2)))
374 return false;
375 break;
376
377 case Type::VariableArray: {
378 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
379 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
380 if (!IsStructurallyEquivalent(Context,
381 Array1->getSizeExpr(), Array2->getSizeExpr()))
382 return false;
383
384 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
385 return false;
386
387 break;
388 }
389
390 case Type::DependentSizedArray: {
391 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
392 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
393 if (!IsStructurallyEquivalent(Context,
394 Array1->getSizeExpr(), Array2->getSizeExpr()))
395 return false;
396
397 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
398 return false;
399
400 break;
401 }
402
403 case Type::DependentSizedExtVector: {
404 const DependentSizedExtVectorType *Vec1
405 = cast<DependentSizedExtVectorType>(T1);
406 const DependentSizedExtVectorType *Vec2
407 = cast<DependentSizedExtVectorType>(T2);
408 if (!IsStructurallyEquivalent(Context,
409 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
410 return false;
411 if (!IsStructurallyEquivalent(Context,
412 Vec1->getElementType(),
413 Vec2->getElementType()))
414 return false;
415 break;
416 }
417
418 case Type::Vector:
419 case Type::ExtVector: {
420 const VectorType *Vec1 = cast<VectorType>(T1);
421 const VectorType *Vec2 = cast<VectorType>(T2);
422 if (!IsStructurallyEquivalent(Context,
423 Vec1->getElementType(),
424 Vec2->getElementType()))
425 return false;
426 if (Vec1->getNumElements() != Vec2->getNumElements())
427 return false;
428 if (Vec1->isAltiVec() != Vec2->isAltiVec())
429 return false;
430 if (Vec1->isPixel() != Vec2->isPixel())
431 return false;
432 }
433
434 case Type::FunctionProto: {
435 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
436 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
437 if (Proto1->getNumArgs() != Proto2->getNumArgs())
438 return false;
439 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
440 if (!IsStructurallyEquivalent(Context,
441 Proto1->getArgType(I),
442 Proto2->getArgType(I)))
443 return false;
444 }
445 if (Proto1->isVariadic() != Proto2->isVariadic())
446 return false;
447 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
448 return false;
449 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
450 return false;
451 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
452 return false;
453 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
454 if (!IsStructurallyEquivalent(Context,
455 Proto1->getExceptionType(I),
456 Proto2->getExceptionType(I)))
457 return false;
458 }
459 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
460 return false;
461
462 // Fall through to check the bits common with FunctionNoProtoType.
463 }
464
465 case Type::FunctionNoProto: {
466 const FunctionType *Function1 = cast<FunctionType>(T1);
467 const FunctionType *Function2 = cast<FunctionType>(T2);
468 if (!IsStructurallyEquivalent(Context,
469 Function1->getResultType(),
470 Function2->getResultType()))
471 return false;
472 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
473 return false;
474 if (Function1->getCallConv() != Function2->getCallConv())
475 return false;
476 break;
477 }
478
479 case Type::UnresolvedUsing:
480 if (!IsStructurallyEquivalent(Context,
481 cast<UnresolvedUsingType>(T1)->getDecl(),
482 cast<UnresolvedUsingType>(T2)->getDecl()))
483 return false;
484
485 break;
486
487 case Type::Typedef:
488 if (!IsStructurallyEquivalent(Context,
489 cast<TypedefType>(T1)->getDecl(),
490 cast<TypedefType>(T2)->getDecl()))
491 return false;
492 break;
493
494 case Type::TypeOfExpr:
495 if (!IsStructurallyEquivalent(Context,
496 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
497 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
498 return false;
499 break;
500
501 case Type::TypeOf:
502 if (!IsStructurallyEquivalent(Context,
503 cast<TypeOfType>(T1)->getUnderlyingType(),
504 cast<TypeOfType>(T2)->getUnderlyingType()))
505 return false;
506 break;
507
508 case Type::Decltype:
509 if (!IsStructurallyEquivalent(Context,
510 cast<DecltypeType>(T1)->getUnderlyingExpr(),
511 cast<DecltypeType>(T2)->getUnderlyingExpr()))
512 return false;
513 break;
514
515 case Type::Record:
516 case Type::Enum:
517 if (!IsStructurallyEquivalent(Context,
518 cast<TagType>(T1)->getDecl(),
519 cast<TagType>(T2)->getDecl()))
520 return false;
521 break;
522
523 case Type::Elaborated: {
524 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
525 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
526 if (Elab1->getTagKind() != Elab2->getTagKind())
527 return false;
528 if (!IsStructurallyEquivalent(Context,
529 Elab1->getUnderlyingType(),
530 Elab2->getUnderlyingType()))
531 return false;
532 break;
533 }
534
535 case Type::TemplateTypeParm: {
536 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
537 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
538 if (Parm1->getDepth() != Parm2->getDepth())
539 return false;
540 if (Parm1->getIndex() != Parm2->getIndex())
541 return false;
542 if (Parm1->isParameterPack() != Parm2->isParameterPack())
543 return false;
544
545 // Names of template type parameters are never significant.
546 break;
547 }
548
549 case Type::SubstTemplateTypeParm: {
550 const SubstTemplateTypeParmType *Subst1
551 = cast<SubstTemplateTypeParmType>(T1);
552 const SubstTemplateTypeParmType *Subst2
553 = cast<SubstTemplateTypeParmType>(T2);
554 if (!IsStructurallyEquivalent(Context,
555 QualType(Subst1->getReplacedParameter(), 0),
556 QualType(Subst2->getReplacedParameter(), 0)))
557 return false;
558 if (!IsStructurallyEquivalent(Context,
559 Subst1->getReplacementType(),
560 Subst2->getReplacementType()))
561 return false;
562 break;
563 }
564
565 case Type::TemplateSpecialization: {
566 const TemplateSpecializationType *Spec1
567 = cast<TemplateSpecializationType>(T1);
568 const TemplateSpecializationType *Spec2
569 = cast<TemplateSpecializationType>(T2);
570 if (!IsStructurallyEquivalent(Context,
571 Spec1->getTemplateName(),
572 Spec2->getTemplateName()))
573 return false;
574 if (Spec1->getNumArgs() != Spec2->getNumArgs())
575 return false;
576 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
577 if (!IsStructurallyEquivalent(Context,
578 Spec1->getArg(I), Spec2->getArg(I)))
579 return false;
580 }
581 break;
582 }
583
584 case Type::QualifiedName: {
585 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
586 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
587 if (!IsStructurallyEquivalent(Context,
588 Qual1->getQualifier(),
589 Qual2->getQualifier()))
590 return false;
591 if (!IsStructurallyEquivalent(Context,
592 Qual1->getNamedType(),
593 Qual2->getNamedType()))
594 return false;
595 break;
596 }
597
598 case Type::Typename: {
599 const TypenameType *Typename1 = cast<TypenameType>(T1);
600 const TypenameType *Typename2 = cast<TypenameType>(T2);
601 if (!IsStructurallyEquivalent(Context,
602 Typename1->getQualifier(),
603 Typename2->getQualifier()))
604 return false;
605 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
606 Typename2->getIdentifier()))
607 return false;
608 if (!IsStructurallyEquivalent(Context,
609 QualType(Typename1->getTemplateId(), 0),
610 QualType(Typename2->getTemplateId(), 0)))
611 return false;
612
613 break;
614 }
615
616 case Type::ObjCInterface: {
617 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
618 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
619 if (!IsStructurallyEquivalent(Context,
620 Iface1->getDecl(), Iface2->getDecl()))
621 return false;
622 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
623 return false;
624 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
625 if (!IsStructurallyEquivalent(Context,
626 Iface1->getProtocol(I),
627 Iface2->getProtocol(I)))
628 return false;
629 }
630 break;
631 }
632
633 case Type::ObjCObjectPointer: {
634 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
635 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
636 if (!IsStructurallyEquivalent(Context,
637 Ptr1->getPointeeType(),
638 Ptr2->getPointeeType()))
639 return false;
640 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
641 return false;
642 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
643 if (!IsStructurallyEquivalent(Context,
644 Ptr1->getProtocol(I),
645 Ptr2->getProtocol(I)))
646 return false;
647 }
648 break;
649 }
650
651 } // end switch
652
653 return true;
654}
655
656/// \brief Determine structural equivalence of two records.
657static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
658 RecordDecl *D1, RecordDecl *D2) {
659 if (D1->isUnion() != D2->isUnion()) {
660 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
661 << Context.C2.getTypeDeclType(D2);
662 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
663 << D1->getDeclName() << (unsigned)D1->getTagKind();
664 return false;
665 }
666
Douglas Gregorb4964f72010-02-15 23:54:17 +0000667 // Compare the definitions of these two records. If either or both are
668 // incomplete, we assume that they are equivalent.
669 D1 = D1->getDefinition();
670 D2 = D2->getDefinition();
671 if (!D1 || !D2)
672 return true;
673
Douglas Gregor3996e242010-02-15 22:01:00 +0000674 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
675 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
676 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
677 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
678 << Context.C2.getTypeDeclType(D2);
679 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
680 << D2CXX->getNumBases();
681 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
682 << D1CXX->getNumBases();
683 return false;
684 }
685
686 // Check the base classes.
687 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
688 BaseEnd1 = D1CXX->bases_end(),
689 Base2 = D2CXX->bases_begin();
690 Base1 != BaseEnd1;
691 ++Base1, ++Base2) {
692 if (!IsStructurallyEquivalent(Context,
693 Base1->getType(), Base2->getType())) {
694 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
695 << Context.C2.getTypeDeclType(D2);
696 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
697 << Base2->getType()
698 << Base2->getSourceRange();
699 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
700 << Base1->getType()
701 << Base1->getSourceRange();
702 return false;
703 }
704
705 // Check virtual vs. non-virtual inheritance mismatch.
706 if (Base1->isVirtual() != Base2->isVirtual()) {
707 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
708 << Context.C2.getTypeDeclType(D2);
709 Context.Diag2(Base2->getSourceRange().getBegin(),
710 diag::note_odr_virtual_base)
711 << Base2->isVirtual() << Base2->getSourceRange();
712 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
713 << Base1->isVirtual()
714 << Base1->getSourceRange();
715 return false;
716 }
717 }
718 } else if (D1CXX->getNumBases() > 0) {
719 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
720 << Context.C2.getTypeDeclType(D2);
721 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
722 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
723 << Base1->getType()
724 << Base1->getSourceRange();
725 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
726 return false;
727 }
728 }
729
730 // Check the fields for consistency.
731 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
732 Field2End = D2->field_end();
733 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
734 Field1End = D1->field_end();
735 Field1 != Field1End;
736 ++Field1, ++Field2) {
737 if (Field2 == Field2End) {
738 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
739 << Context.C2.getTypeDeclType(D2);
740 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
741 << Field1->getDeclName() << Field1->getType();
742 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
743 return false;
744 }
745
746 if (!IsStructurallyEquivalent(Context,
747 Field1->getType(), Field2->getType())) {
748 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
749 << Context.C2.getTypeDeclType(D2);
750 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
751 << Field2->getDeclName() << Field2->getType();
752 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
753 << Field1->getDeclName() << Field1->getType();
754 return false;
755 }
756
757 if (Field1->isBitField() != Field2->isBitField()) {
758 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
759 << Context.C2.getTypeDeclType(D2);
760 if (Field1->isBitField()) {
761 llvm::APSInt Bits;
762 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
763 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
764 << Field1->getDeclName() << Field1->getType()
765 << Bits.toString(10, false);
766 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
767 << Field2->getDeclName();
768 } else {
769 llvm::APSInt Bits;
770 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
771 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
772 << Field2->getDeclName() << Field2->getType()
773 << Bits.toString(10, false);
774 Context.Diag1(Field1->getLocation(),
775 diag::note_odr_not_bit_field)
776 << Field1->getDeclName();
777 }
778 return false;
779 }
780
781 if (Field1->isBitField()) {
782 // Make sure that the bit-fields are the same length.
783 llvm::APSInt Bits1, Bits2;
784 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
785 return false;
786 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
787 return false;
788
789 if (!IsSameValue(Bits1, Bits2)) {
790 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
791 << Context.C2.getTypeDeclType(D2);
792 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
793 << Field2->getDeclName() << Field2->getType()
794 << Bits2.toString(10, false);
795 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
796 << Field1->getDeclName() << Field1->getType()
797 << Bits1.toString(10, false);
798 return false;
799 }
800 }
801 }
802
803 if (Field2 != Field2End) {
804 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
805 << Context.C2.getTypeDeclType(D2);
806 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
807 << Field2->getDeclName() << Field2->getType();
808 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
809 return false;
810 }
811
812 return true;
813}
814
815/// \brief Determine structural equivalence of two enums.
816static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
817 EnumDecl *D1, EnumDecl *D2) {
818 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
819 EC2End = D2->enumerator_end();
820 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
821 EC1End = D1->enumerator_end();
822 EC1 != EC1End; ++EC1, ++EC2) {
823 if (EC2 == EC2End) {
824 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
825 << Context.C2.getTypeDeclType(D2);
826 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
827 << EC1->getDeclName()
828 << EC1->getInitVal().toString(10);
829 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
830 return false;
831 }
832
833 llvm::APSInt Val1 = EC1->getInitVal();
834 llvm::APSInt Val2 = EC2->getInitVal();
835 if (!IsSameValue(Val1, Val2) ||
836 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
837 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
838 << Context.C2.getTypeDeclType(D2);
839 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
840 << EC2->getDeclName()
841 << EC2->getInitVal().toString(10);
842 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
843 << EC1->getDeclName()
844 << EC1->getInitVal().toString(10);
845 return false;
846 }
847 }
848
849 if (EC2 != EC2End) {
850 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
851 << Context.C2.getTypeDeclType(D2);
852 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
853 << EC2->getDeclName()
854 << EC2->getInitVal().toString(10);
855 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
856 return false;
857 }
858
859 return true;
860}
861
862/// \brief Determine structural equivalence of two declarations.
863static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
864 Decl *D1, Decl *D2) {
865 // FIXME: Check for known structural equivalences via a callback of some sort.
866
Douglas Gregorb4964f72010-02-15 23:54:17 +0000867 // Check whether we already know that these two declarations are not
868 // structurally equivalent.
869 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
870 D2->getCanonicalDecl())))
871 return false;
872
Douglas Gregor3996e242010-02-15 22:01:00 +0000873 // Determine whether we've already produced a tentative equivalence for D1.
874 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
875 if (EquivToD1)
876 return EquivToD1 == D2->getCanonicalDecl();
877
878 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
879 EquivToD1 = D2->getCanonicalDecl();
880 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
881 return true;
882}
883
884bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
885 Decl *D2) {
886 if (!::IsStructurallyEquivalent(*this, D1, D2))
887 return false;
888
889 return !Finish();
890}
891
892bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
893 QualType T2) {
894 if (!::IsStructurallyEquivalent(*this, T1, T2))
895 return false;
896
897 return !Finish();
898}
899
900bool StructuralEquivalenceContext::Finish() {
901 while (!DeclsToCheck.empty()) {
902 // Check the next declaration.
903 Decl *D1 = DeclsToCheck.front();
904 DeclsToCheck.pop_front();
905
906 Decl *D2 = TentativeEquivalences[D1];
907 assert(D2 && "Unrecorded tentative equivalence?");
908
Douglas Gregorb4964f72010-02-15 23:54:17 +0000909 bool Equivalent = true;
910
Douglas Gregor3996e242010-02-15 22:01:00 +0000911 // FIXME: Switch on all declaration kinds. For now, we're just going to
912 // check the obvious ones.
913 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
914 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
915 // Check for equivalent structure names.
916 IdentifierInfo *Name1 = Record1->getIdentifier();
917 if (!Name1 && Record1->getTypedefForAnonDecl())
918 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
919 IdentifierInfo *Name2 = Record2->getIdentifier();
920 if (!Name2 && Record2->getTypedefForAnonDecl())
921 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000922 if (!::IsStructurallyEquivalent(Name1, Name2) ||
923 !::IsStructurallyEquivalent(*this, Record1, Record2))
924 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000925 } else {
926 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000927 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000928 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000929 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000930 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
931 // Check for equivalent enum names.
932 IdentifierInfo *Name1 = Enum1->getIdentifier();
933 if (!Name1 && Enum1->getTypedefForAnonDecl())
934 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
935 IdentifierInfo *Name2 = Enum2->getIdentifier();
936 if (!Name2 && Enum2->getTypedefForAnonDecl())
937 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000938 if (!::IsStructurallyEquivalent(Name1, Name2) ||
939 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
940 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000941 } else {
942 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +0000943 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000944 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000945 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000946 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
947 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000948 Typedef2->getIdentifier()) ||
949 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +0000950 Typedef1->getUnderlyingType(),
951 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +0000952 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000953 } else {
954 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000955 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000956 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000957 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000958
959 if (!Equivalent) {
960 // Note that these two declarations are not equivalent (and we already
961 // know about it).
962 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
963 D2->getCanonicalDecl()));
964 return true;
965 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000966 // FIXME: Check other declaration kinds!
967 }
968
969 return false;
970}
971
972//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +0000973// Import Types
974//----------------------------------------------------------------------------
975
Douglas Gregore4c83e42010-02-09 22:48:33 +0000976QualType ASTNodeImporter::VisitType(Type *T) {
977 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
978 << T->getTypeClassName();
979 return QualType();
980}
981
Douglas Gregor96e578d2010-02-05 17:54:41 +0000982QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
983 switch (T->getKind()) {
984 case BuiltinType::Void: return Importer.getToContext().VoidTy;
985 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
986
987 case BuiltinType::Char_U:
988 // The context we're importing from has an unsigned 'char'. If we're
989 // importing into a context with a signed 'char', translate to
990 // 'unsigned char' instead.
991 if (Importer.getToContext().getLangOptions().CharIsSigned)
992 return Importer.getToContext().UnsignedCharTy;
993
994 return Importer.getToContext().CharTy;
995
996 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
997
998 case BuiltinType::Char16:
999 // FIXME: Make sure that the "to" context supports C++!
1000 return Importer.getToContext().Char16Ty;
1001
1002 case BuiltinType::Char32:
1003 // FIXME: Make sure that the "to" context supports C++!
1004 return Importer.getToContext().Char32Ty;
1005
1006 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1007 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1008 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1009 case BuiltinType::ULongLong:
1010 return Importer.getToContext().UnsignedLongLongTy;
1011 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1012
1013 case BuiltinType::Char_S:
1014 // The context we're importing from has an unsigned 'char'. If we're
1015 // importing into a context with a signed 'char', translate to
1016 // 'unsigned char' instead.
1017 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1018 return Importer.getToContext().SignedCharTy;
1019
1020 return Importer.getToContext().CharTy;
1021
1022 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1023 case BuiltinType::WChar:
1024 // FIXME: If not in C++, shall we translate to the C equivalent of
1025 // wchar_t?
1026 return Importer.getToContext().WCharTy;
1027
1028 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1029 case BuiltinType::Int : return Importer.getToContext().IntTy;
1030 case BuiltinType::Long : return Importer.getToContext().LongTy;
1031 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1032 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1033 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1034 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1035 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1036
1037 case BuiltinType::NullPtr:
1038 // FIXME: Make sure that the "to" context supports C++0x!
1039 return Importer.getToContext().NullPtrTy;
1040
1041 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1042 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1043 case BuiltinType::UndeducedAuto:
1044 // FIXME: Make sure that the "to" context supports C++0x!
1045 return Importer.getToContext().UndeducedAutoTy;
1046
1047 case BuiltinType::ObjCId:
1048 // FIXME: Make sure that the "to" context supports Objective-C!
1049 return Importer.getToContext().ObjCBuiltinIdTy;
1050
1051 case BuiltinType::ObjCClass:
1052 return Importer.getToContext().ObjCBuiltinClassTy;
1053
1054 case BuiltinType::ObjCSel:
1055 return Importer.getToContext().ObjCBuiltinSelTy;
1056 }
1057
1058 return QualType();
1059}
1060
1061QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1062 QualType ToElementType = Importer.Import(T->getElementType());
1063 if (ToElementType.isNull())
1064 return QualType();
1065
1066 return Importer.getToContext().getComplexType(ToElementType);
1067}
1068
1069QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1070 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1071 if (ToPointeeType.isNull())
1072 return QualType();
1073
1074 return Importer.getToContext().getPointerType(ToPointeeType);
1075}
1076
1077QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1078 // FIXME: Check for blocks support in "to" context.
1079 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1080 if (ToPointeeType.isNull())
1081 return QualType();
1082
1083 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1084}
1085
1086QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1087 // FIXME: Check for C++ support in "to" context.
1088 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1089 if (ToPointeeType.isNull())
1090 return QualType();
1091
1092 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1093}
1094
1095QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1096 // FIXME: Check for C++0x support in "to" context.
1097 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1098 if (ToPointeeType.isNull())
1099 return QualType();
1100
1101 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1102}
1103
1104QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1105 // FIXME: Check for C++ support in "to" context.
1106 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1107 if (ToPointeeType.isNull())
1108 return QualType();
1109
1110 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1111 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1112 ClassType.getTypePtr());
1113}
1114
1115QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1116 QualType ToElementType = Importer.Import(T->getElementType());
1117 if (ToElementType.isNull())
1118 return QualType();
1119
1120 return Importer.getToContext().getConstantArrayType(ToElementType,
1121 T->getSize(),
1122 T->getSizeModifier(),
1123 T->getIndexTypeCVRQualifiers());
1124}
1125
1126QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1127 QualType ToElementType = Importer.Import(T->getElementType());
1128 if (ToElementType.isNull())
1129 return QualType();
1130
1131 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1132 T->getSizeModifier(),
1133 T->getIndexTypeCVRQualifiers());
1134}
1135
1136QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1137 QualType ToElementType = Importer.Import(T->getElementType());
1138 if (ToElementType.isNull())
1139 return QualType();
1140
1141 Expr *Size = Importer.Import(T->getSizeExpr());
1142 if (!Size)
1143 return QualType();
1144
1145 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1146 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1147 T->getSizeModifier(),
1148 T->getIndexTypeCVRQualifiers(),
1149 Brackets);
1150}
1151
1152QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1153 QualType ToElementType = Importer.Import(T->getElementType());
1154 if (ToElementType.isNull())
1155 return QualType();
1156
1157 return Importer.getToContext().getVectorType(ToElementType,
1158 T->getNumElements(),
1159 T->isAltiVec(),
1160 T->isPixel());
1161}
1162
1163QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1164 QualType ToElementType = Importer.Import(T->getElementType());
1165 if (ToElementType.isNull())
1166 return QualType();
1167
1168 return Importer.getToContext().getExtVectorType(ToElementType,
1169 T->getNumElements());
1170}
1171
1172QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1173 // FIXME: What happens if we're importing a function without a prototype
1174 // into C++? Should we make it variadic?
1175 QualType ToResultType = Importer.Import(T->getResultType());
1176 if (ToResultType.isNull())
1177 return QualType();
1178
1179 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1180 T->getNoReturnAttr(),
1181 T->getCallConv());
1182}
1183
1184QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1185 QualType ToResultType = Importer.Import(T->getResultType());
1186 if (ToResultType.isNull())
1187 return QualType();
1188
1189 // Import argument types
1190 llvm::SmallVector<QualType, 4> ArgTypes;
1191 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1192 AEnd = T->arg_type_end();
1193 A != AEnd; ++A) {
1194 QualType ArgType = Importer.Import(*A);
1195 if (ArgType.isNull())
1196 return QualType();
1197 ArgTypes.push_back(ArgType);
1198 }
1199
1200 // Import exception types
1201 llvm::SmallVector<QualType, 4> ExceptionTypes;
1202 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1203 EEnd = T->exception_end();
1204 E != EEnd; ++E) {
1205 QualType ExceptionType = Importer.Import(*E);
1206 if (ExceptionType.isNull())
1207 return QualType();
1208 ExceptionTypes.push_back(ExceptionType);
1209 }
1210
1211 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1212 ArgTypes.size(),
1213 T->isVariadic(),
1214 T->getTypeQuals(),
1215 T->hasExceptionSpec(),
1216 T->hasAnyExceptionSpec(),
1217 ExceptionTypes.size(),
1218 ExceptionTypes.data(),
1219 T->getNoReturnAttr(),
1220 T->getCallConv());
1221}
1222
1223QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1224 TypedefDecl *ToDecl
1225 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1226 if (!ToDecl)
1227 return QualType();
1228
1229 return Importer.getToContext().getTypeDeclType(ToDecl);
1230}
1231
1232QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1233 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1234 if (!ToExpr)
1235 return QualType();
1236
1237 return Importer.getToContext().getTypeOfExprType(ToExpr);
1238}
1239
1240QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1241 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1242 if (ToUnderlyingType.isNull())
1243 return QualType();
1244
1245 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1246}
1247
1248QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1249 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1250 if (!ToExpr)
1251 return QualType();
1252
1253 return Importer.getToContext().getDecltypeType(ToExpr);
1254}
1255
1256QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1257 RecordDecl *ToDecl
1258 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1259 if (!ToDecl)
1260 return QualType();
1261
1262 return Importer.getToContext().getTagDeclType(ToDecl);
1263}
1264
1265QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1266 EnumDecl *ToDecl
1267 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1268 if (!ToDecl)
1269 return QualType();
1270
1271 return Importer.getToContext().getTagDeclType(ToDecl);
1272}
1273
1274QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1275 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1276 if (ToUnderlyingType.isNull())
1277 return QualType();
1278
1279 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1280 T->getTagKind());
1281}
1282
1283QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1284 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1285 if (!ToQualifier)
1286 return QualType();
1287
1288 QualType ToNamedType = Importer.Import(T->getNamedType());
1289 if (ToNamedType.isNull())
1290 return QualType();
1291
1292 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1293}
1294
1295QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1296 ObjCInterfaceDecl *Class
1297 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1298 if (!Class)
1299 return QualType();
1300
1301 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1302 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1303 PEnd = T->qual_end();
1304 P != PEnd; ++P) {
1305 ObjCProtocolDecl *Protocol
1306 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1307 if (!Protocol)
1308 return QualType();
1309 Protocols.push_back(Protocol);
1310 }
1311
1312 return Importer.getToContext().getObjCInterfaceType(Class,
1313 Protocols.data(),
1314 Protocols.size());
1315}
1316
1317QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1318 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1319 if (ToPointeeType.isNull())
1320 return QualType();
1321
1322 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1323 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1324 PEnd = T->qual_end();
1325 P != PEnd; ++P) {
1326 ObjCProtocolDecl *Protocol
1327 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1328 if (!Protocol)
1329 return QualType();
1330 Protocols.push_back(Protocol);
1331 }
1332
1333 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1334 Protocols.data(),
1335 Protocols.size());
1336}
1337
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001338//----------------------------------------------------------------------------
1339// Import Declarations
1340//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001341bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1342 DeclContext *&LexicalDC,
1343 DeclarationName &Name,
1344 SourceLocation &Loc) {
1345 // Import the context of this declaration.
1346 DC = Importer.ImportContext(D->getDeclContext());
1347 if (!DC)
1348 return true;
1349
1350 LexicalDC = DC;
1351 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1352 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1353 if (!LexicalDC)
1354 return true;
1355 }
1356
1357 // Import the name of this declaration.
1358 Name = Importer.Import(D->getDeclName());
1359 if (D->getDeclName() && !Name)
1360 return true;
1361
1362 // Import the location of this declaration.
1363 Loc = Importer.Import(D->getLocation());
1364 return false;
1365}
1366
Douglas Gregor5c73e912010-02-11 00:48:18 +00001367bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001368 RecordDecl *ToRecord) {
1369 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1370 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001371 Importer.getDiags(),
1372 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001373 return SEC.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001374}
1375
Douglas Gregor98c10182010-02-12 22:17:39 +00001376bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001377 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1378 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001379 Importer.getDiags(),
1380 Importer.getNonEquivalentDecls());
Douglas Gregor3996e242010-02-15 22:01:00 +00001381 return SEC.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001382}
1383
Douglas Gregore4c83e42010-02-09 22:48:33 +00001384Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001385 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001386 << D->getDeclKindName();
1387 return 0;
1388}
1389
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001390Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1391 // Import the major distinguishing characteristics of this typedef.
1392 DeclContext *DC, *LexicalDC;
1393 DeclarationName Name;
1394 SourceLocation Loc;
1395 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1396 return 0;
1397
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001398 // If this typedef is not in block scope, determine whether we've
1399 // seen a typedef with the same name (that we can merge with) or any
1400 // other entity by that name (which name lookup could conflict with).
1401 if (!DC->isFunctionOrMethod()) {
1402 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1403 unsigned IDNS = Decl::IDNS_Ordinary;
1404 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1405 Lookup.first != Lookup.second;
1406 ++Lookup.first) {
1407 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1408 continue;
1409 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001410 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1411 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001412 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001413 }
1414
1415 ConflictingDecls.push_back(*Lookup.first);
1416 }
1417
1418 if (!ConflictingDecls.empty()) {
1419 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1420 ConflictingDecls.data(),
1421 ConflictingDecls.size());
1422 if (!Name)
1423 return 0;
1424 }
1425 }
1426
Douglas Gregorb4964f72010-02-15 23:54:17 +00001427 // Import the underlying type of this typedef;
1428 QualType T = Importer.Import(D->getUnderlyingType());
1429 if (T.isNull())
1430 return 0;
1431
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001432 // Create the new typedef node.
1433 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1434 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1435 Loc, Name.getAsIdentifierInfo(),
1436 TInfo);
1437 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001438 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001439 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001440
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001441 return ToTypedef;
1442}
1443
Douglas Gregor98c10182010-02-12 22:17:39 +00001444Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1445 // Import the major distinguishing characteristics of this enum.
1446 DeclContext *DC, *LexicalDC;
1447 DeclarationName Name;
1448 SourceLocation Loc;
1449 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1450 return 0;
1451
1452 // Figure out what enum name we're looking for.
1453 unsigned IDNS = Decl::IDNS_Tag;
1454 DeclarationName SearchName = Name;
1455 if (!SearchName && D->getTypedefForAnonDecl()) {
1456 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1457 IDNS = Decl::IDNS_Ordinary;
1458 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1459 IDNS |= Decl::IDNS_Ordinary;
1460
1461 // We may already have an enum of the same name; try to find and match it.
1462 if (!DC->isFunctionOrMethod() && SearchName) {
1463 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1464 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1465 Lookup.first != Lookup.second;
1466 ++Lookup.first) {
1467 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1468 continue;
1469
1470 Decl *Found = *Lookup.first;
1471 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1472 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1473 Found = Tag->getDecl();
1474 }
1475
1476 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001477 if (IsStructuralMatch(D, FoundEnum))
1478 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001479 }
1480
1481 ConflictingDecls.push_back(*Lookup.first);
1482 }
1483
1484 if (!ConflictingDecls.empty()) {
1485 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1486 ConflictingDecls.data(),
1487 ConflictingDecls.size());
1488 }
1489 }
1490
1491 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001492 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001493 Name.getAsIdentifierInfo(),
1494 Importer.Import(D->getTagKeywordLoc()),
1495 0);
Douglas Gregor3996e242010-02-15 22:01:00 +00001496 D2->setLexicalDeclContext(LexicalDC);
1497 Importer.Imported(D, D2);
1498 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001499
1500 // Import the integer type.
1501 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1502 if (ToIntegerType.isNull())
1503 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001504 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001505
1506 // Import the definition
1507 if (D->isDefinition()) {
1508 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1509 if (T.isNull())
1510 return 0;
1511
1512 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1513 if (ToPromotionType.isNull())
1514 return 0;
1515
Douglas Gregor3996e242010-02-15 22:01:00 +00001516 D2->startDefinition();
Douglas Gregor98c10182010-02-12 22:17:39 +00001517 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1518 FromMemEnd = D->decls_end();
1519 FromMem != FromMemEnd;
1520 ++FromMem)
1521 Importer.Import(*FromMem);
1522
Douglas Gregor3996e242010-02-15 22:01:00 +00001523 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001524 }
1525
Douglas Gregor3996e242010-02-15 22:01:00 +00001526 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001527}
1528
Douglas Gregor5c73e912010-02-11 00:48:18 +00001529Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1530 // If this record has a definition in the translation unit we're coming from,
1531 // but this particular declaration is not that definition, import the
1532 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001533 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001534 if (Definition && Definition != D) {
1535 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001536 if (!ImportedDef)
1537 return 0;
1538
1539 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001540 }
1541
1542 // Import the major distinguishing characteristics of this record.
1543 DeclContext *DC, *LexicalDC;
1544 DeclarationName Name;
1545 SourceLocation Loc;
1546 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1547 return 0;
1548
1549 // Figure out what structure name we're looking for.
1550 unsigned IDNS = Decl::IDNS_Tag;
1551 DeclarationName SearchName = Name;
1552 if (!SearchName && D->getTypedefForAnonDecl()) {
1553 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1554 IDNS = Decl::IDNS_Ordinary;
1555 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1556 IDNS |= Decl::IDNS_Ordinary;
1557
1558 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001559 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001560 if (!DC->isFunctionOrMethod() && SearchName) {
1561 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1562 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1563 Lookup.first != Lookup.second;
1564 ++Lookup.first) {
1565 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1566 continue;
1567
1568 Decl *Found = *Lookup.first;
1569 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1570 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1571 Found = Tag->getDecl();
1572 }
1573
1574 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001575 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1576 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1577 // The record types structurally match, or the "from" translation
1578 // unit only had a forward declaration anyway; call it the same
1579 // function.
1580 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001581 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001582 }
1583 } else {
1584 // We have a forward declaration of this type, so adopt that forward
1585 // declaration rather than building a new one.
1586 AdoptDecl = FoundRecord;
1587 continue;
1588 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001589 }
1590
1591 ConflictingDecls.push_back(*Lookup.first);
1592 }
1593
1594 if (!ConflictingDecls.empty()) {
1595 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1596 ConflictingDecls.data(),
1597 ConflictingDecls.size());
1598 }
1599 }
1600
1601 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001602 RecordDecl *D2 = AdoptDecl;
1603 if (!D2) {
1604 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1605 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001606 D->getTagKind(),
1607 DC, Loc,
1608 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001609 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001610 D2 = D2CXX;
Douglas Gregor25791052010-02-12 00:09:27 +00001611
1612 if (D->isDefinition()) {
1613 // Add base classes.
1614 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1615 for (CXXRecordDecl::base_class_iterator
Douglas Gregor3996e242010-02-15 22:01:00 +00001616 Base1 = D1CXX->bases_begin(),
1617 FromBaseEnd = D1CXX->bases_end();
1618 Base1 != FromBaseEnd;
1619 ++Base1) {
1620 QualType T = Importer.Import(Base1->getType());
Douglas Gregor25791052010-02-12 00:09:27 +00001621 if (T.isNull())
1622 return 0;
1623
1624 Bases.push_back(
1625 new (Importer.getToContext())
Douglas Gregor3996e242010-02-15 22:01:00 +00001626 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1627 Base1->isVirtual(),
1628 Base1->isBaseOfClass(),
1629 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001630 T));
Douglas Gregor25791052010-02-12 00:09:27 +00001631 }
1632 if (!Bases.empty())
Douglas Gregor3996e242010-02-15 22:01:00 +00001633 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor5c73e912010-02-11 00:48:18 +00001634 }
Douglas Gregor25791052010-02-12 00:09:27 +00001635 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001636 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001637 DC, Loc,
1638 Name.getAsIdentifierInfo(),
1639 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001640 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001641 D2->setLexicalDeclContext(LexicalDC);
1642 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001643 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001644
Douglas Gregor3996e242010-02-15 22:01:00 +00001645 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001646
Douglas Gregor5c73e912010-02-11 00:48:18 +00001647 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001648 D2->startDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001649 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1650 FromMemEnd = D->decls_end();
1651 FromMem != FromMemEnd;
1652 ++FromMem)
1653 Importer.Import(*FromMem);
1654
Douglas Gregor3996e242010-02-15 22:01:00 +00001655 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001656 }
1657
Douglas Gregor3996e242010-02-15 22:01:00 +00001658 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001659}
1660
Douglas Gregor98c10182010-02-12 22:17:39 +00001661Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1662 // Import the major distinguishing characteristics of this enumerator.
1663 DeclContext *DC, *LexicalDC;
1664 DeclarationName Name;
1665 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001666 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001667 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001668
1669 QualType T = Importer.Import(D->getType());
1670 if (T.isNull())
1671 return 0;
1672
Douglas Gregor98c10182010-02-12 22:17:39 +00001673 // Determine whether there are any other declarations with the same name and
1674 // in the same context.
1675 if (!LexicalDC->isFunctionOrMethod()) {
1676 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1677 unsigned IDNS = Decl::IDNS_Ordinary;
1678 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1679 Lookup.first != Lookup.second;
1680 ++Lookup.first) {
1681 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1682 continue;
1683
1684 ConflictingDecls.push_back(*Lookup.first);
1685 }
1686
1687 if (!ConflictingDecls.empty()) {
1688 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1689 ConflictingDecls.data(),
1690 ConflictingDecls.size());
1691 if (!Name)
1692 return 0;
1693 }
1694 }
1695
1696 Expr *Init = Importer.Import(D->getInitExpr());
1697 if (D->getInitExpr() && !Init)
1698 return 0;
1699
1700 EnumConstantDecl *ToEnumerator
1701 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1702 Name.getAsIdentifierInfo(), T,
1703 Init, D->getInitVal());
1704 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001705 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001706 LexicalDC->addDecl(ToEnumerator);
1707 return ToEnumerator;
1708}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001709
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001710Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1711 // Import the major distinguishing characteristics of this function.
1712 DeclContext *DC, *LexicalDC;
1713 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001714 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001715 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001716 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001717
1718 // Try to find a function in our own ("to") context with the same name, same
1719 // type, and in the same context as the function we're importing.
1720 if (!LexicalDC->isFunctionOrMethod()) {
1721 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1722 unsigned IDNS = Decl::IDNS_Ordinary;
1723 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1724 Lookup.first != Lookup.second;
1725 ++Lookup.first) {
1726 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1727 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001728
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001729 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1730 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1731 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001732 if (Importer.IsStructurallyEquivalent(D->getType(),
1733 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001734 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001735 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001736 }
1737
1738 // FIXME: Check for overloading more carefully, e.g., by boosting
1739 // Sema::IsOverload out to the AST library.
1740
1741 // Function overloading is okay in C++.
1742 if (Importer.getToContext().getLangOptions().CPlusPlus)
1743 continue;
1744
1745 // Complain about inconsistent function types.
1746 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001747 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001748 Importer.ToDiag(FoundFunction->getLocation(),
1749 diag::note_odr_value_here)
1750 << FoundFunction->getType();
1751 }
1752 }
1753
1754 ConflictingDecls.push_back(*Lookup.first);
1755 }
1756
1757 if (!ConflictingDecls.empty()) {
1758 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1759 ConflictingDecls.data(),
1760 ConflictingDecls.size());
1761 if (!Name)
1762 return 0;
1763 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001764 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001765
1766 // Import the type.
1767 QualType T = Importer.Import(D->getType());
1768 if (T.isNull())
1769 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001770
1771 // Import the function parameters.
1772 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1773 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1774 P != PEnd; ++P) {
1775 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1776 if (!ToP)
1777 return 0;
1778
1779 Parameters.push_back(ToP);
1780 }
1781
1782 // Create the imported function.
1783 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor43f54792010-02-17 02:12:47 +00001784 FunctionDecl *ToFunction
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001785 = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1786 Name, T, TInfo, D->getStorageClass(),
1787 D->isInlineSpecified(),
1788 D->hasWrittenPrototype());
Douglas Gregor43f54792010-02-17 02:12:47 +00001789 ToFunction->setLexicalDeclContext(LexicalDC);
1790 Importer.Imported(D, ToFunction);
1791 LexicalDC->addDecl(ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001792
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001793 // Set the parameters.
1794 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00001795 Parameters[I]->setOwningFunction(ToFunction);
1796 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001797 }
Douglas Gregor43f54792010-02-17 02:12:47 +00001798 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001799
1800 // FIXME: Other bits to merge?
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001801
Douglas Gregor43f54792010-02-17 02:12:47 +00001802 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001803}
1804
Douglas Gregor5c73e912010-02-11 00:48:18 +00001805Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1806 // Import the major distinguishing characteristics of a variable.
1807 DeclContext *DC, *LexicalDC;
1808 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001809 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001810 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1811 return 0;
1812
1813 // Import the type.
1814 QualType T = Importer.Import(D->getType());
1815 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00001816 return 0;
1817
1818 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1819 Expr *BitWidth = Importer.Import(D->getBitWidth());
1820 if (!BitWidth && D->getBitWidth())
1821 return 0;
1822
1823 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1824 Loc, Name.getAsIdentifierInfo(),
1825 T, TInfo, BitWidth, D->isMutable());
1826 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001827 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001828 LexicalDC->addDecl(ToField);
1829 return ToField;
1830}
1831
Douglas Gregor7244b0b2010-02-17 00:34:30 +00001832Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1833 // Import the major distinguishing characteristics of an ivar.
1834 DeclContext *DC, *LexicalDC;
1835 DeclarationName Name;
1836 SourceLocation Loc;
1837 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1838 return 0;
1839
1840 // Determine whether we've already imported this ivar
1841 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1842 Lookup.first != Lookup.second;
1843 ++Lookup.first) {
1844 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
1845 if (Importer.IsStructurallyEquivalent(D->getType(),
1846 FoundIvar->getType())) {
1847 Importer.Imported(D, FoundIvar);
1848 return FoundIvar;
1849 }
1850
1851 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
1852 << Name << D->getType() << FoundIvar->getType();
1853 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
1854 << FoundIvar->getType();
1855 return 0;
1856 }
1857 }
1858
1859 // Import the type.
1860 QualType T = Importer.Import(D->getType());
1861 if (T.isNull())
1862 return 0;
1863
1864 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1865 Expr *BitWidth = Importer.Import(D->getBitWidth());
1866 if (!BitWidth && D->getBitWidth())
1867 return 0;
1868
1869 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC,
1870 Loc, Name.getAsIdentifierInfo(),
1871 T, TInfo, D->getAccessControl(),
1872 BitWidth);
1873 ToIvar->setLexicalDeclContext(LexicalDC);
1874 Importer.Imported(D, ToIvar);
1875 LexicalDC->addDecl(ToIvar);
1876 return ToIvar;
1877
1878}
1879
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001880Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1881 // Import the major distinguishing characteristics of a variable.
1882 DeclContext *DC, *LexicalDC;
1883 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001884 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001885 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001886 return 0;
1887
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001888 // Try to find a variable in our own ("to") context with the same name and
1889 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00001890 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001891 VarDecl *MergeWithVar = 0;
1892 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1893 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00001894 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001895 Lookup.first != Lookup.second;
1896 ++Lookup.first) {
1897 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1898 continue;
1899
1900 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1901 // We have found a variable that we may need to merge with. Check it.
1902 if (isExternalLinkage(FoundVar->getLinkage()) &&
1903 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001904 if (Importer.IsStructurallyEquivalent(D->getType(),
1905 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001906 MergeWithVar = FoundVar;
1907 break;
1908 }
1909
Douglas Gregor56521c52010-02-12 17:23:39 +00001910 const ArrayType *FoundArray
1911 = Importer.getToContext().getAsArrayType(FoundVar->getType());
1912 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00001913 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00001914 if (FoundArray && TArray) {
1915 if (isa<IncompleteArrayType>(FoundArray) &&
1916 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001917 // Import the type.
1918 QualType T = Importer.Import(D->getType());
1919 if (T.isNull())
1920 return 0;
1921
Douglas Gregor56521c52010-02-12 17:23:39 +00001922 FoundVar->setType(T);
1923 MergeWithVar = FoundVar;
1924 break;
1925 } else if (isa<IncompleteArrayType>(TArray) &&
1926 isa<ConstantArrayType>(FoundArray)) {
1927 MergeWithVar = FoundVar;
1928 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00001929 }
1930 }
1931
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001932 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001933 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001934 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1935 << FoundVar->getType();
1936 }
1937 }
1938
1939 ConflictingDecls.push_back(*Lookup.first);
1940 }
1941
1942 if (MergeWithVar) {
1943 // An equivalent variable with external linkage has been found. Link
1944 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001945 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001946
1947 if (VarDecl *DDef = D->getDefinition()) {
1948 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1949 Importer.ToDiag(ExistingDef->getLocation(),
1950 diag::err_odr_variable_multiple_def)
1951 << Name;
1952 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1953 } else {
1954 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00001955 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001956 }
1957 }
1958
1959 return MergeWithVar;
1960 }
1961
1962 if (!ConflictingDecls.empty()) {
1963 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1964 ConflictingDecls.data(),
1965 ConflictingDecls.size());
1966 if (!Name)
1967 return 0;
1968 }
1969 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001970
Douglas Gregorb4964f72010-02-15 23:54:17 +00001971 // Import the type.
1972 QualType T = Importer.Import(D->getType());
1973 if (T.isNull())
1974 return 0;
1975
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001976 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00001977 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001978 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1979 Name.getAsIdentifierInfo(), T, TInfo,
1980 D->getStorageClass());
Douglas Gregor62d311f2010-02-09 19:21:46 +00001981 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001982 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001983 LexicalDC->addDecl(ToVar);
1984
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001985 // Merge the initializer.
1986 // FIXME: Can we really import any initializer? Alternatively, we could force
1987 // ourselves to import every declaration of a variable and then only use
1988 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00001989 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001990
1991 // FIXME: Other bits to merge?
1992
1993 return ToVar;
1994}
1995
Douglas Gregor8b228d72010-02-17 21:22:52 +00001996Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
1997 // Parameters are created in the translation unit's context, then moved
1998 // into the function declaration's context afterward.
1999 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2000
2001 // Import the name of this declaration.
2002 DeclarationName Name = Importer.Import(D->getDeclName());
2003 if (D->getDeclName() && !Name)
2004 return 0;
2005
2006 // Import the location of this declaration.
2007 SourceLocation Loc = Importer.Import(D->getLocation());
2008
2009 // Import the parameter's type.
2010 QualType T = Importer.Import(D->getType());
2011 if (T.isNull())
2012 return 0;
2013
2014 // Create the imported parameter.
2015 ImplicitParamDecl *ToParm
2016 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2017 Loc, Name.getAsIdentifierInfo(),
2018 T);
2019 return Importer.Imported(D, ToParm);
2020}
2021
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002022Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2023 // Parameters are created in the translation unit's context, then moved
2024 // into the function declaration's context afterward.
2025 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2026
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002027 // Import the name of this declaration.
2028 DeclarationName Name = Importer.Import(D->getDeclName());
2029 if (D->getDeclName() && !Name)
2030 return 0;
2031
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002032 // Import the location of this declaration.
2033 SourceLocation Loc = Importer.Import(D->getLocation());
2034
2035 // Import the parameter's type.
2036 QualType T = Importer.Import(D->getType());
2037 if (T.isNull())
2038 return 0;
2039
2040 // Create the imported parameter.
2041 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2042 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2043 Loc, Name.getAsIdentifierInfo(),
2044 T, TInfo, D->getStorageClass(),
2045 /*FIXME: Default argument*/ 0);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002046 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002047}
2048
Douglas Gregor43f54792010-02-17 02:12:47 +00002049Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2050 // Import the major distinguishing characteristics of a method.
2051 DeclContext *DC, *LexicalDC;
2052 DeclarationName Name;
2053 SourceLocation Loc;
2054 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2055 return 0;
2056
2057 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2058 Lookup.first != Lookup.second;
2059 ++Lookup.first) {
2060 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2061 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2062 continue;
2063
2064 // Check return types.
2065 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2066 FoundMethod->getResultType())) {
2067 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2068 << D->isInstanceMethod() << Name
2069 << D->getResultType() << FoundMethod->getResultType();
2070 Importer.ToDiag(FoundMethod->getLocation(),
2071 diag::note_odr_objc_method_here)
2072 << D->isInstanceMethod() << Name;
2073 return 0;
2074 }
2075
2076 // Check the number of parameters.
2077 if (D->param_size() != FoundMethod->param_size()) {
2078 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2079 << D->isInstanceMethod() << Name
2080 << D->param_size() << FoundMethod->param_size();
2081 Importer.ToDiag(FoundMethod->getLocation(),
2082 diag::note_odr_objc_method_here)
2083 << D->isInstanceMethod() << Name;
2084 return 0;
2085 }
2086
2087 // Check parameter types.
2088 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2089 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2090 P != PEnd; ++P, ++FoundP) {
2091 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2092 (*FoundP)->getType())) {
2093 Importer.FromDiag((*P)->getLocation(),
2094 diag::err_odr_objc_method_param_type_inconsistent)
2095 << D->isInstanceMethod() << Name
2096 << (*P)->getType() << (*FoundP)->getType();
2097 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2098 << (*FoundP)->getType();
2099 return 0;
2100 }
2101 }
2102
2103 // Check variadic/non-variadic.
2104 // Check the number of parameters.
2105 if (D->isVariadic() != FoundMethod->isVariadic()) {
2106 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2107 << D->isInstanceMethod() << Name;
2108 Importer.ToDiag(FoundMethod->getLocation(),
2109 diag::note_odr_objc_method_here)
2110 << D->isInstanceMethod() << Name;
2111 return 0;
2112 }
2113
2114 // FIXME: Any other bits we need to merge?
2115 return Importer.Imported(D, FoundMethod);
2116 }
2117 }
2118
2119 // Import the result type.
2120 QualType ResultTy = Importer.Import(D->getResultType());
2121 if (ResultTy.isNull())
2122 return 0;
2123
2124 ObjCMethodDecl *ToMethod
2125 = ObjCMethodDecl::Create(Importer.getToContext(),
2126 Loc,
2127 Importer.Import(D->getLocEnd()),
2128 Name.getObjCSelector(),
2129 ResultTy, DC,
2130 D->isInstanceMethod(),
2131 D->isVariadic(),
2132 D->isSynthesized(),
2133 D->getImplementationControl());
2134
2135 // FIXME: When we decide to merge method definitions, we'll need to
2136 // deal with implicit parameters.
2137
2138 // Import the parameters
2139 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2140 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2141 FromPEnd = D->param_end();
2142 FromP != FromPEnd;
2143 ++FromP) {
2144 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2145 if (!ToP)
2146 return 0;
2147
2148 ToParams.push_back(ToP);
2149 }
2150
2151 // Set the parameters.
2152 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2153 ToParams[I]->setOwningFunction(ToMethod);
2154 ToMethod->addDecl(ToParams[I]);
2155 }
2156 ToMethod->setMethodParams(Importer.getToContext(),
2157 ToParams.data(), ToParams.size());
2158
2159 ToMethod->setLexicalDeclContext(LexicalDC);
2160 Importer.Imported(D, ToMethod);
2161 LexicalDC->addDecl(ToMethod);
2162 return ToMethod;
2163}
2164
Douglas Gregor84c51c32010-02-18 01:47:50 +00002165Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2166 // Import the major distinguishing characteristics of a category.
2167 DeclContext *DC, *LexicalDC;
2168 DeclarationName Name;
2169 SourceLocation Loc;
2170 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2171 return 0;
2172
2173 ObjCInterfaceDecl *ToInterface
2174 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2175 if (!ToInterface)
2176 return 0;
2177
2178 // Determine if we've already encountered this category.
2179 ObjCCategoryDecl *MergeWithCategory
2180 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2181 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2182 if (!ToCategory) {
2183 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2184 Importer.Import(D->getAtLoc()),
2185 Loc,
2186 Importer.Import(D->getCategoryNameLoc()),
2187 Name.getAsIdentifierInfo());
2188 ToCategory->setLexicalDeclContext(LexicalDC);
2189 LexicalDC->addDecl(ToCategory);
2190 Importer.Imported(D, ToCategory);
2191
2192 // Link this category into its class's category list.
2193 ToCategory->setClassInterface(ToInterface);
2194 ToCategory->insertNextClassCategory();
2195
2196 // Import protocols
2197 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2198 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2199 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2200 = D->protocol_loc_begin();
2201 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2202 FromProtoEnd = D->protocol_end();
2203 FromProto != FromProtoEnd;
2204 ++FromProto, ++FromProtoLoc) {
2205 ObjCProtocolDecl *ToProto
2206 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2207 if (!ToProto)
2208 return 0;
2209 Protocols.push_back(ToProto);
2210 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2211 }
2212
2213 // FIXME: If we're merging, make sure that the protocol list is the same.
2214 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2215 ProtocolLocs.data(), Importer.getToContext());
2216
2217 } else {
2218 Importer.Imported(D, ToCategory);
2219 }
2220
2221 // Import all of the members of this category.
2222 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2223 FromMemEnd = D->decls_end();
2224 FromMem != FromMemEnd;
2225 ++FromMem)
2226 Importer.Import(*FromMem);
2227
2228 // If we have an implementation, import it as well.
2229 if (D->getImplementation()) {
2230 ObjCCategoryImplDecl *Impl
2231 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2232 if (!Impl)
2233 return 0;
2234
2235 ToCategory->setImplementation(Impl);
2236 }
2237
2238 return ToCategory;
2239}
2240
Douglas Gregor98d156a2010-02-17 16:12:00 +00002241Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002242 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002243 DeclContext *DC, *LexicalDC;
2244 DeclarationName Name;
2245 SourceLocation Loc;
2246 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2247 return 0;
2248
2249 ObjCProtocolDecl *MergeWithProtocol = 0;
2250 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2251 Lookup.first != Lookup.second;
2252 ++Lookup.first) {
2253 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2254 continue;
2255
2256 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2257 break;
2258 }
2259
2260 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2261 if (!ToProto || ToProto->isForwardDecl()) {
2262 if (!ToProto) {
2263 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2264 Name.getAsIdentifierInfo());
2265 ToProto->setForwardDecl(D->isForwardDecl());
2266 ToProto->setLexicalDeclContext(LexicalDC);
2267 LexicalDC->addDecl(ToProto);
2268 }
2269 Importer.Imported(D, ToProto);
2270
2271 // Import protocols
2272 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2273 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2274 ObjCProtocolDecl::protocol_loc_iterator
2275 FromProtoLoc = D->protocol_loc_begin();
2276 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2277 FromProtoEnd = D->protocol_end();
2278 FromProto != FromProtoEnd;
2279 ++FromProto, ++FromProtoLoc) {
2280 ObjCProtocolDecl *ToProto
2281 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2282 if (!ToProto)
2283 return 0;
2284 Protocols.push_back(ToProto);
2285 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2286 }
2287
2288 // FIXME: If we're merging, make sure that the protocol list is the same.
2289 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2290 ProtocolLocs.data(), Importer.getToContext());
2291 } else {
2292 Importer.Imported(D, ToProto);
2293 }
2294
Douglas Gregor84c51c32010-02-18 01:47:50 +00002295 // Import all of the members of this protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002296 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2297 FromMemEnd = D->decls_end();
2298 FromMem != FromMemEnd;
2299 ++FromMem)
2300 Importer.Import(*FromMem);
2301
2302 return ToProto;
2303}
2304
Douglas Gregor45635322010-02-16 01:20:57 +00002305Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2306 // Import the major distinguishing characteristics of an @interface.
2307 DeclContext *DC, *LexicalDC;
2308 DeclarationName Name;
2309 SourceLocation Loc;
2310 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2311 return 0;
2312
2313 ObjCInterfaceDecl *MergeWithIface = 0;
2314 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2315 Lookup.first != Lookup.second;
2316 ++Lookup.first) {
2317 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2318 continue;
2319
2320 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2321 break;
2322 }
2323
2324 ObjCInterfaceDecl *ToIface = MergeWithIface;
2325 if (!ToIface || ToIface->isForwardDecl()) {
2326 if (!ToIface) {
2327 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2328 DC, Loc,
2329 Name.getAsIdentifierInfo(),
2330 Importer.Import(D->getClassLoc()),
2331 D->isForwardDecl(),
2332 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002333 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002334 ToIface->setLexicalDeclContext(LexicalDC);
2335 LexicalDC->addDecl(ToIface);
2336 }
2337 Importer.Imported(D, ToIface);
2338
Douglas Gregor45635322010-02-16 01:20:57 +00002339 if (D->getSuperClass()) {
2340 ObjCInterfaceDecl *Super
2341 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2342 if (!Super)
2343 return 0;
2344
2345 ToIface->setSuperClass(Super);
2346 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2347 }
2348
2349 // Import protocols
2350 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2351 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2352 ObjCInterfaceDecl::protocol_loc_iterator
2353 FromProtoLoc = D->protocol_loc_begin();
2354 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2355 FromProtoEnd = D->protocol_end();
2356 FromProto != FromProtoEnd;
2357 ++FromProto, ++FromProtoLoc) {
2358 ObjCProtocolDecl *ToProto
2359 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2360 if (!ToProto)
2361 return 0;
2362 Protocols.push_back(ToProto);
2363 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2364 }
2365
2366 // FIXME: If we're merging, make sure that the protocol list is the same.
2367 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2368 ProtocolLocs.data(), Importer.getToContext());
2369
Douglas Gregor45635322010-02-16 01:20:57 +00002370 // Import @end range
2371 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2372 } else {
2373 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002374
2375 // Check for consistency of superclasses.
2376 DeclarationName FromSuperName, ToSuperName;
2377 if (D->getSuperClass())
2378 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2379 if (ToIface->getSuperClass())
2380 ToSuperName = ToIface->getSuperClass()->getDeclName();
2381 if (FromSuperName != ToSuperName) {
2382 Importer.ToDiag(ToIface->getLocation(),
2383 diag::err_odr_objc_superclass_inconsistent)
2384 << ToIface->getDeclName();
2385 if (ToIface->getSuperClass())
2386 Importer.ToDiag(ToIface->getSuperClassLoc(),
2387 diag::note_odr_objc_superclass)
2388 << ToIface->getSuperClass()->getDeclName();
2389 else
2390 Importer.ToDiag(ToIface->getLocation(),
2391 diag::note_odr_objc_missing_superclass);
2392 if (D->getSuperClass())
2393 Importer.FromDiag(D->getSuperClassLoc(),
2394 diag::note_odr_objc_superclass)
2395 << D->getSuperClass()->getDeclName();
2396 else
2397 Importer.FromDiag(D->getLocation(),
2398 diag::note_odr_objc_missing_superclass);
2399 return 0;
2400 }
Douglas Gregor45635322010-02-16 01:20:57 +00002401 }
2402
Douglas Gregor84c51c32010-02-18 01:47:50 +00002403 // Import categories. When the categories themselves are imported, they'll
2404 // hook themselves into this interface.
2405 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2406 FromCat = FromCat->getNextClassCategory())
2407 Importer.Import(FromCat);
2408
Douglas Gregor45635322010-02-16 01:20:57 +00002409 // Import all of the members of this class.
2410 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
2411 FromMemEnd = D->decls_end();
2412 FromMem != FromMemEnd;
2413 ++FromMem)
2414 Importer.Import(*FromMem);
2415
2416 // If we have an @implementation, import it as well.
2417 if (D->getImplementation()) {
2418 ObjCImplementationDecl *Impl
2419 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2420 if (!Impl)
2421 return 0;
2422
2423 ToIface->setImplementation(Impl);
2424 }
2425
Douglas Gregor98d156a2010-02-17 16:12:00 +00002426 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00002427}
2428
Douglas Gregora11c4582010-02-17 18:02:10 +00002429Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2430 // Import the major distinguishing characteristics of an @property.
2431 DeclContext *DC, *LexicalDC;
2432 DeclarationName Name;
2433 SourceLocation Loc;
2434 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2435 return 0;
2436
2437 // Check whether we have already imported this property.
2438 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2439 Lookup.first != Lookup.second;
2440 ++Lookup.first) {
2441 if (ObjCPropertyDecl *FoundProp
2442 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2443 // Check property types.
2444 if (!Importer.IsStructurallyEquivalent(D->getType(),
2445 FoundProp->getType())) {
2446 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2447 << Name << D->getType() << FoundProp->getType();
2448 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2449 << FoundProp->getType();
2450 return 0;
2451 }
2452
2453 // FIXME: Check property attributes, getters, setters, etc.?
2454
2455 // Consider these properties to be equivalent.
2456 Importer.Imported(D, FoundProp);
2457 return FoundProp;
2458 }
2459 }
2460
2461 // Import the type.
2462 QualType T = Importer.Import(D->getType());
2463 if (T.isNull())
2464 return 0;
2465
2466 // Create the new property.
2467 ObjCPropertyDecl *ToProperty
2468 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2469 Name.getAsIdentifierInfo(),
2470 Importer.Import(D->getAtLoc()),
2471 T,
2472 D->getPropertyImplementation());
2473 Importer.Imported(D, ToProperty);
2474 ToProperty->setLexicalDeclContext(LexicalDC);
2475 LexicalDC->addDecl(ToProperty);
2476
2477 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2478 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2479 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2480 ToProperty->setGetterMethodDecl(
2481 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2482 ToProperty->setSetterMethodDecl(
2483 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2484 ToProperty->setPropertyIvarDecl(
2485 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2486 return ToProperty;
2487}
2488
Douglas Gregor8661a722010-02-18 02:12:22 +00002489Decl *
2490ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2491 // Import the context of this declaration.
2492 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2493 if (!DC)
2494 return 0;
2495
2496 DeclContext *LexicalDC = DC;
2497 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2498 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2499 if (!LexicalDC)
2500 return 0;
2501 }
2502
2503 // Import the location of this declaration.
2504 SourceLocation Loc = Importer.Import(D->getLocation());
2505
2506 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2507 llvm::SmallVector<SourceLocation, 4> Locations;
2508 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2509 = D->protocol_loc_begin();
2510 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2511 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2512 FromProto != FromProtoEnd;
2513 ++FromProto, ++FromProtoLoc) {
2514 ObjCProtocolDecl *ToProto
2515 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2516 if (!ToProto)
2517 continue;
2518
2519 Protocols.push_back(ToProto);
2520 Locations.push_back(Importer.Import(*FromProtoLoc));
2521 }
2522
2523 ObjCForwardProtocolDecl *ToForward
2524 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2525 Protocols.data(), Protocols.size(),
2526 Locations.data());
2527 ToForward->setLexicalDeclContext(LexicalDC);
2528 LexicalDC->addDecl(ToForward);
2529 Importer.Imported(D, ToForward);
2530 return ToForward;
2531}
2532
Douglas Gregor06537af2010-02-18 02:04:09 +00002533Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2534 // Import the context of this declaration.
2535 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2536 if (!DC)
2537 return 0;
2538
2539 DeclContext *LexicalDC = DC;
2540 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2541 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2542 if (!LexicalDC)
2543 return 0;
2544 }
2545
2546 // Import the location of this declaration.
2547 SourceLocation Loc = Importer.Import(D->getLocation());
2548
2549 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2550 llvm::SmallVector<SourceLocation, 4> Locations;
2551 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2552 From != FromEnd; ++From) {
2553 ObjCInterfaceDecl *ToIface
2554 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2555 if (!ToIface)
2556 continue;
2557
2558 Interfaces.push_back(ToIface);
2559 Locations.push_back(Importer.Import(From->getLocation()));
2560 }
2561
2562 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2563 Loc,
2564 Interfaces.data(),
2565 Locations.data(),
2566 Interfaces.size());
2567 ToClass->setLexicalDeclContext(LexicalDC);
2568 LexicalDC->addDecl(ToClass);
2569 Importer.Imported(D, ToClass);
2570 return ToClass;
2571}
2572
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002573//----------------------------------------------------------------------------
2574// Import Statements
2575//----------------------------------------------------------------------------
2576
2577Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2578 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2579 << S->getStmtClassName();
2580 return 0;
2581}
2582
2583//----------------------------------------------------------------------------
2584// Import Expressions
2585//----------------------------------------------------------------------------
2586Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2587 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2588 << E->getStmtClassName();
2589 return 0;
2590}
2591
2592Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2593 QualType T = Importer.Import(E->getType());
2594 if (T.isNull())
2595 return 0;
2596
2597 return new (Importer.getToContext())
2598 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2599}
2600
Douglas Gregor98c10182010-02-12 22:17:39 +00002601Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2602 QualType T = Importer.Import(E->getType());
2603 if (T.isNull())
2604 return 0;
2605
2606 Expr *SubExpr = Importer.Import(E->getSubExpr());
2607 if (!SubExpr)
2608 return 0;
2609
2610 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2611 SubExpr,
2612 E->isLvalueCast());
2613}
2614
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002615ASTImporter::ASTImporter(Diagnostic &Diags,
2616 ASTContext &ToContext, FileManager &ToFileManager,
2617 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00002618 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00002619 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002620 Diags(Diags) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00002621 ImportedDecls[FromContext.getTranslationUnitDecl()]
2622 = ToContext.getTranslationUnitDecl();
2623}
2624
2625ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002626
2627QualType ASTImporter::Import(QualType FromT) {
2628 if (FromT.isNull())
2629 return QualType();
2630
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002631 // Check whether we've already imported this type.
2632 llvm::DenseMap<Type *, Type *>::iterator Pos
2633 = ImportedTypes.find(FromT.getTypePtr());
2634 if (Pos != ImportedTypes.end())
2635 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00002636
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002637 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00002638 ASTNodeImporter Importer(*this);
2639 QualType ToT = Importer.Visit(FromT.getTypePtr());
2640 if (ToT.isNull())
2641 return ToT;
2642
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002643 // Record the imported type.
2644 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2645
Douglas Gregor96e578d2010-02-05 17:54:41 +00002646 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2647}
2648
Douglas Gregor62d311f2010-02-09 19:21:46 +00002649TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002650 if (!FromTSI)
2651 return FromTSI;
2652
2653 // FIXME: For now we just create a "trivial" type source info based
2654 // on the type and a seingle location. Implement a real version of
2655 // this.
2656 QualType T = Import(FromTSI->getType());
2657 if (T.isNull())
2658 return 0;
2659
2660 return ToContext.getTrivialTypeSourceInfo(T,
2661 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002662}
2663
2664Decl *ASTImporter::Import(Decl *FromD) {
2665 if (!FromD)
2666 return 0;
2667
2668 // Check whether we've already imported this declaration.
2669 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2670 if (Pos != ImportedDecls.end())
2671 return Pos->second;
2672
2673 // Import the type
2674 ASTNodeImporter Importer(*this);
2675 Decl *ToD = Importer.Visit(FromD);
2676 if (!ToD)
2677 return 0;
2678
2679 // Record the imported declaration.
2680 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002681
2682 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2683 // Keep track of anonymous tags that have an associated typedef.
2684 if (FromTag->getTypedefForAnonDecl())
2685 AnonTagsWithPendingTypedefs.push_back(FromTag);
2686 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2687 // When we've finished transforming a typedef, see whether it was the
2688 // typedef for an anonymous tag.
2689 for (llvm::SmallVector<TagDecl *, 4>::iterator
2690 FromTag = AnonTagsWithPendingTypedefs.begin(),
2691 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2692 FromTag != FromTagEnd; ++FromTag) {
2693 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2694 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2695 // We found the typedef for an anonymous tag; link them.
2696 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2697 AnonTagsWithPendingTypedefs.erase(FromTag);
2698 break;
2699 }
2700 }
2701 }
2702 }
2703
Douglas Gregor62d311f2010-02-09 19:21:46 +00002704 return ToD;
2705}
2706
2707DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
2708 if (!FromDC)
2709 return FromDC;
2710
2711 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
2712}
2713
2714Expr *ASTImporter::Import(Expr *FromE) {
2715 if (!FromE)
2716 return 0;
2717
2718 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
2719}
2720
2721Stmt *ASTImporter::Import(Stmt *FromS) {
2722 if (!FromS)
2723 return 0;
2724
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002725 // Check whether we've already imported this declaration.
2726 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
2727 if (Pos != ImportedStmts.end())
2728 return Pos->second;
2729
2730 // Import the type
2731 ASTNodeImporter Importer(*this);
2732 Stmt *ToS = Importer.Visit(FromS);
2733 if (!ToS)
2734 return 0;
2735
2736 // Record the imported declaration.
2737 ImportedStmts[FromS] = ToS;
2738 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002739}
2740
2741NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
2742 if (!FromNNS)
2743 return 0;
2744
2745 // FIXME: Implement!
2746 return 0;
2747}
2748
2749SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
2750 if (FromLoc.isInvalid())
2751 return SourceLocation();
2752
Douglas Gregor811663e2010-02-10 00:15:17 +00002753 SourceManager &FromSM = FromContext.getSourceManager();
2754
2755 // For now, map everything down to its spelling location, so that we
2756 // don't have to import macro instantiations.
2757 // FIXME: Import macro instantiations!
2758 FromLoc = FromSM.getSpellingLoc(FromLoc);
2759 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
2760 SourceManager &ToSM = ToContext.getSourceManager();
2761 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
2762 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002763}
2764
2765SourceRange ASTImporter::Import(SourceRange FromRange) {
2766 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
2767}
2768
Douglas Gregor811663e2010-02-10 00:15:17 +00002769FileID ASTImporter::Import(FileID FromID) {
2770 llvm::DenseMap<unsigned, FileID>::iterator Pos
2771 = ImportedFileIDs.find(FromID.getHashValue());
2772 if (Pos != ImportedFileIDs.end())
2773 return Pos->second;
2774
2775 SourceManager &FromSM = FromContext.getSourceManager();
2776 SourceManager &ToSM = ToContext.getSourceManager();
2777 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
2778 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
2779
2780 // Include location of this file.
2781 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
2782
2783 // Map the FileID for to the "to" source manager.
2784 FileID ToID;
2785 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
2786 if (Cache->Entry) {
2787 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
2788 // disk again
2789 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
2790 // than mmap the files several times.
2791 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
2792 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
2793 FromSLoc.getFile().getFileCharacteristic());
2794 } else {
2795 // FIXME: We want to re-use the existing MemoryBuffer!
2796 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
2797 llvm::MemoryBuffer *ToBuf
2798 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
2799 FromBuf->getBufferEnd(),
2800 FromBuf->getBufferIdentifier());
2801 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
2802 }
2803
2804
2805 ImportedFileIDs[FromID.getHashValue()] = ToID;
2806 return ToID;
2807}
2808
Douglas Gregor96e578d2010-02-05 17:54:41 +00002809DeclarationName ASTImporter::Import(DeclarationName FromName) {
2810 if (!FromName)
2811 return DeclarationName();
2812
2813 switch (FromName.getNameKind()) {
2814 case DeclarationName::Identifier:
2815 return Import(FromName.getAsIdentifierInfo());
2816
2817 case DeclarationName::ObjCZeroArgSelector:
2818 case DeclarationName::ObjCOneArgSelector:
2819 case DeclarationName::ObjCMultiArgSelector:
2820 return Import(FromName.getObjCSelector());
2821
2822 case DeclarationName::CXXConstructorName: {
2823 QualType T = Import(FromName.getCXXNameType());
2824 if (T.isNull())
2825 return DeclarationName();
2826
2827 return ToContext.DeclarationNames.getCXXConstructorName(
2828 ToContext.getCanonicalType(T));
2829 }
2830
2831 case DeclarationName::CXXDestructorName: {
2832 QualType T = Import(FromName.getCXXNameType());
2833 if (T.isNull())
2834 return DeclarationName();
2835
2836 return ToContext.DeclarationNames.getCXXDestructorName(
2837 ToContext.getCanonicalType(T));
2838 }
2839
2840 case DeclarationName::CXXConversionFunctionName: {
2841 QualType T = Import(FromName.getCXXNameType());
2842 if (T.isNull())
2843 return DeclarationName();
2844
2845 return ToContext.DeclarationNames.getCXXConversionFunctionName(
2846 ToContext.getCanonicalType(T));
2847 }
2848
2849 case DeclarationName::CXXOperatorName:
2850 return ToContext.DeclarationNames.getCXXOperatorName(
2851 FromName.getCXXOverloadedOperator());
2852
2853 case DeclarationName::CXXLiteralOperatorName:
2854 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
2855 Import(FromName.getCXXLiteralIdentifier()));
2856
2857 case DeclarationName::CXXUsingDirective:
2858 // FIXME: STATICS!
2859 return DeclarationName::getUsingDirectiveName();
2860 }
2861
2862 // Silence bogus GCC warning
2863 return DeclarationName();
2864}
2865
2866IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
2867 if (!FromId)
2868 return 0;
2869
2870 return &ToContext.Idents.get(FromId->getName());
2871}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002872
Douglas Gregor43f54792010-02-17 02:12:47 +00002873Selector ASTImporter::Import(Selector FromSel) {
2874 if (FromSel.isNull())
2875 return Selector();
2876
2877 llvm::SmallVector<IdentifierInfo *, 4> Idents;
2878 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
2879 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
2880 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
2881 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
2882}
2883
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002884DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
2885 DeclContext *DC,
2886 unsigned IDNS,
2887 NamedDecl **Decls,
2888 unsigned NumDecls) {
2889 return Name;
2890}
2891
2892DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002893 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
2894 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002895}
2896
2897DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002898 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
2899 DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002900}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002901
2902Decl *ASTImporter::Imported(Decl *From, Decl *To) {
2903 ImportedDecls[From] = To;
2904 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00002905}
Douglas Gregorb4964f72010-02-15 23:54:17 +00002906
2907bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
2908 llvm::DenseMap<Type *, Type *>::iterator Pos
2909 = ImportedTypes.find(From.getTypePtr());
2910 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
2911 return true;
2912
2913 StructuralEquivalenceContext SEC(FromContext, ToContext, Diags,
2914 NonEquivalentDecls);
2915 return SEC.IsStructurallyEquivalent(From, To);
2916}