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