blob: dd2528a6b3b3e21ab75d689713eb85ad0c41a9b0 [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor82fc4bf2010-02-10 17:47:19 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000023#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
26#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000027#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000028
29using namespace clang;
30
31namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000032 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000033 public DeclVisitor<ASTNodeImporter, Decl *>,
34 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000041 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000042 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000043
44 // Importing types
Douglas Gregor89cc9d62010-02-09 22:48:33 +000045 QualType VisitType(Type *T);
Douglas Gregor1b2949d2010-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 Gregor089459a2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000083 SourceLocation &Loc);
Douglas Gregor083a8212010-02-21 18:24:45 +000084 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregor96a01b42010-02-11 00:48:18 +000085 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000086 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000087 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +000088 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor9e5d9962010-02-10 21:10:29 +000089 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +000090 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000091 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +000092 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +000093 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +000094 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
95 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
96 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
97 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000098 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +000099 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000100 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000101 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000102 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000103 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000104 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000105 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000106 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000107 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000108 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000109 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
110
Douglas Gregor4800d952010-02-11 19:21:55 +0000111 // Importing statements
112 Stmt *VisitStmt(Stmt *S);
113
114 // Importing expressions
115 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000116 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000117 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000118 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000119 Expr *VisitParenExpr(ParenExpr *E);
120 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorbd249a52010-02-19 01:24:23 +0000121 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000122 Expr *VisitBinaryOperator(BinaryOperator *E);
123 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000124 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000125 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000126 };
127}
128
129//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000130// Structural Equivalence
131//----------------------------------------------------------------------------
132
133namespace {
134 struct StructuralEquivalenceContext {
135 /// \brief AST contexts for which we are checking structural equivalence.
136 ASTContext &C1, &C2;
137
138 /// \brief Diagnostic object used to emit diagnostics.
139 Diagnostic &Diags;
140
141 /// \brief The set of "tentative" equivalences between two canonical
142 /// declarations, mapping from a declaration in the first context to the
143 /// declaration in the second context that we believe to be equivalent.
144 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
145
146 /// \brief Queue of declarations in the first context whose equivalence
147 /// with a declaration in the second context still needs to be verified.
148 std::deque<Decl *> DeclsToCheck;
149
Douglas Gregorea35d112010-02-15 23:54:17 +0000150 /// \brief Declaration (from, to) pairs that are known not to be equivalent
151 /// (which we have already complained about).
152 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
153
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000154 /// \brief Whether we're being strict about the spelling of types when
155 /// unifying two types.
156 bool StrictTypeSpelling;
157
158 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
159 Diagnostic &Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +0000160 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000161 bool StrictTypeSpelling = false)
Douglas Gregorea35d112010-02-15 23:54:17 +0000162 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
163 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000164
165 /// \brief Determine whether the two declarations are structurally
166 /// equivalent.
167 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
168
169 /// \brief Determine whether the two types are structurally equivalent.
170 bool IsStructurallyEquivalent(QualType T1, QualType T2);
171
172 private:
173 /// \brief Finish checking all of the structural equivalences.
174 ///
175 /// \returns true if an error occurred, false otherwise.
176 bool Finish();
177
178 public:
179 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
180 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
181 }
182
183 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
184 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
185 }
186 };
187}
188
189static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
190 QualType T1, QualType T2);
191static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
192 Decl *D1, Decl *D2);
193
194/// \brief Determine if two APInts have the same value, after zero-extending
195/// one of them (if needed!) to ensure that the bit-widths match.
196static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
197 if (I1.getBitWidth() == I2.getBitWidth())
198 return I1 == I2;
199
200 if (I1.getBitWidth() > I2.getBitWidth())
201 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
202
203 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
204}
205
206/// \brief Determine if two APSInts have the same value, zero- or sign-extending
207/// as needed.
208static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
209 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
210 return I1 == I2;
211
212 // Check for a bit-width mismatch.
213 if (I1.getBitWidth() > I2.getBitWidth())
214 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
215 else if (I2.getBitWidth() > I1.getBitWidth())
216 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
217
218 // We have a signedness mismatch. Turn the signed value into an unsigned
219 // value.
220 if (I1.isSigned()) {
221 if (I1.isNegative())
222 return false;
223
224 return llvm::APSInt(I1, true) == I2;
225 }
226
227 if (I2.isNegative())
228 return false;
229
230 return I1 == llvm::APSInt(I2, true);
231}
232
233/// \brief Determine structural equivalence of two expressions.
234static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
235 Expr *E1, Expr *E2) {
236 if (!E1 || !E2)
237 return E1 == E2;
238
239 // FIXME: Actually perform a structural comparison!
240 return true;
241}
242
243/// \brief Determine whether two identifiers are equivalent.
244static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
245 const IdentifierInfo *Name2) {
246 if (!Name1 || !Name2)
247 return Name1 == Name2;
248
249 return Name1->getName() == Name2->getName();
250}
251
252/// \brief Determine whether two nested-name-specifiers are equivalent.
253static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
254 NestedNameSpecifier *NNS1,
255 NestedNameSpecifier *NNS2) {
256 // FIXME: Implement!
257 return true;
258}
259
260/// \brief Determine whether two template arguments are equivalent.
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 const TemplateArgument &Arg1,
263 const TemplateArgument &Arg2) {
264 // FIXME: Implement!
265 return true;
266}
267
268/// \brief Determine structural equivalence for the common part of array
269/// types.
270static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
271 const ArrayType *Array1,
272 const ArrayType *Array2) {
273 if (!IsStructurallyEquivalent(Context,
274 Array1->getElementType(),
275 Array2->getElementType()))
276 return false;
277 if (Array1->getSizeModifier() != Array2->getSizeModifier())
278 return false;
279 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
280 return false;
281
282 return true;
283}
284
285/// \brief Determine structural equivalence of two types.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 QualType T1, QualType T2) {
288 if (T1.isNull() || T2.isNull())
289 return T1.isNull() && T2.isNull();
290
291 if (!Context.StrictTypeSpelling) {
292 // We aren't being strict about token-to-token equivalence of types,
293 // so map down to the canonical type.
294 T1 = Context.C1.getCanonicalType(T1);
295 T2 = Context.C2.getCanonicalType(T2);
296 }
297
298 if (T1.getQualifiers() != T2.getQualifiers())
299 return false;
300
Douglas Gregorea35d112010-02-15 23:54:17 +0000301 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000302
Douglas Gregorea35d112010-02-15 23:54:17 +0000303 if (T1->getTypeClass() != T2->getTypeClass()) {
304 // Compare function types with prototypes vs. without prototypes as if
305 // both did not have prototypes.
306 if (T1->getTypeClass() == Type::FunctionProto &&
307 T2->getTypeClass() == Type::FunctionNoProto)
308 TC = Type::FunctionNoProto;
309 else if (T1->getTypeClass() == Type::FunctionNoProto &&
310 T2->getTypeClass() == Type::FunctionProto)
311 TC = Type::FunctionNoProto;
312 else
313 return false;
314 }
315
316 switch (TC) {
317 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000318 // FIXME: Deal with Char_S/Char_U.
319 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
320 return false;
321 break;
322
323 case Type::Complex:
324 if (!IsStructurallyEquivalent(Context,
325 cast<ComplexType>(T1)->getElementType(),
326 cast<ComplexType>(T2)->getElementType()))
327 return false;
328 break;
329
330 case Type::Pointer:
331 if (!IsStructurallyEquivalent(Context,
332 cast<PointerType>(T1)->getPointeeType(),
333 cast<PointerType>(T2)->getPointeeType()))
334 return false;
335 break;
336
337 case Type::BlockPointer:
338 if (!IsStructurallyEquivalent(Context,
339 cast<BlockPointerType>(T1)->getPointeeType(),
340 cast<BlockPointerType>(T2)->getPointeeType()))
341 return false;
342 break;
343
344 case Type::LValueReference:
345 case Type::RValueReference: {
346 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
347 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
348 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
349 return false;
350 if (Ref1->isInnerRef() != Ref2->isInnerRef())
351 return false;
352 if (!IsStructurallyEquivalent(Context,
353 Ref1->getPointeeTypeAsWritten(),
354 Ref2->getPointeeTypeAsWritten()))
355 return false;
356 break;
357 }
358
359 case Type::MemberPointer: {
360 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
361 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
362 if (!IsStructurallyEquivalent(Context,
363 MemPtr1->getPointeeType(),
364 MemPtr2->getPointeeType()))
365 return false;
366 if (!IsStructurallyEquivalent(Context,
367 QualType(MemPtr1->getClass(), 0),
368 QualType(MemPtr2->getClass(), 0)))
369 return false;
370 break;
371 }
372
373 case Type::ConstantArray: {
374 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
375 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
376 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
377 return false;
378
379 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
380 return false;
381 break;
382 }
383
384 case Type::IncompleteArray:
385 if (!IsArrayStructurallyEquivalent(Context,
386 cast<ArrayType>(T1),
387 cast<ArrayType>(T2)))
388 return false;
389 break;
390
391 case Type::VariableArray: {
392 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
393 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
394 if (!IsStructurallyEquivalent(Context,
395 Array1->getSizeExpr(), Array2->getSizeExpr()))
396 return false;
397
398 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
399 return false;
400
401 break;
402 }
403
404 case Type::DependentSizedArray: {
405 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
406 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
407 if (!IsStructurallyEquivalent(Context,
408 Array1->getSizeExpr(), Array2->getSizeExpr()))
409 return false;
410
411 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
412 return false;
413
414 break;
415 }
416
417 case Type::DependentSizedExtVector: {
418 const DependentSizedExtVectorType *Vec1
419 = cast<DependentSizedExtVectorType>(T1);
420 const DependentSizedExtVectorType *Vec2
421 = cast<DependentSizedExtVectorType>(T2);
422 if (!IsStructurallyEquivalent(Context,
423 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
424 return false;
425 if (!IsStructurallyEquivalent(Context,
426 Vec1->getElementType(),
427 Vec2->getElementType()))
428 return false;
429 break;
430 }
431
432 case Type::Vector:
433 case Type::ExtVector: {
434 const VectorType *Vec1 = cast<VectorType>(T1);
435 const VectorType *Vec2 = cast<VectorType>(T2);
436 if (!IsStructurallyEquivalent(Context,
437 Vec1->getElementType(),
438 Vec2->getElementType()))
439 return false;
440 if (Vec1->getNumElements() != Vec2->getNumElements())
441 return false;
442 if (Vec1->isAltiVec() != Vec2->isAltiVec())
443 return false;
444 if (Vec1->isPixel() != Vec2->isPixel())
445 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000446 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000447 }
448
449 case Type::FunctionProto: {
450 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
451 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
452 if (Proto1->getNumArgs() != Proto2->getNumArgs())
453 return false;
454 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
455 if (!IsStructurallyEquivalent(Context,
456 Proto1->getArgType(I),
457 Proto2->getArgType(I)))
458 return false;
459 }
460 if (Proto1->isVariadic() != Proto2->isVariadic())
461 return false;
462 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
463 return false;
464 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
465 return false;
466 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
467 return false;
468 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
469 if (!IsStructurallyEquivalent(Context,
470 Proto1->getExceptionType(I),
471 Proto2->getExceptionType(I)))
472 return false;
473 }
474 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
475 return false;
476
477 // Fall through to check the bits common with FunctionNoProtoType.
478 }
479
480 case Type::FunctionNoProto: {
481 const FunctionType *Function1 = cast<FunctionType>(T1);
482 const FunctionType *Function2 = cast<FunctionType>(T2);
483 if (!IsStructurallyEquivalent(Context,
484 Function1->getResultType(),
485 Function2->getResultType()))
486 return false;
487 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
488 return false;
489 if (Function1->getCallConv() != Function2->getCallConv())
490 return false;
491 break;
492 }
493
494 case Type::UnresolvedUsing:
495 if (!IsStructurallyEquivalent(Context,
496 cast<UnresolvedUsingType>(T1)->getDecl(),
497 cast<UnresolvedUsingType>(T2)->getDecl()))
498 return false;
499
500 break;
501
502 case Type::Typedef:
503 if (!IsStructurallyEquivalent(Context,
504 cast<TypedefType>(T1)->getDecl(),
505 cast<TypedefType>(T2)->getDecl()))
506 return false;
507 break;
508
509 case Type::TypeOfExpr:
510 if (!IsStructurallyEquivalent(Context,
511 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
512 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
513 return false;
514 break;
515
516 case Type::TypeOf:
517 if (!IsStructurallyEquivalent(Context,
518 cast<TypeOfType>(T1)->getUnderlyingType(),
519 cast<TypeOfType>(T2)->getUnderlyingType()))
520 return false;
521 break;
522
523 case Type::Decltype:
524 if (!IsStructurallyEquivalent(Context,
525 cast<DecltypeType>(T1)->getUnderlyingExpr(),
526 cast<DecltypeType>(T2)->getUnderlyingExpr()))
527 return false;
528 break;
529
530 case Type::Record:
531 case Type::Enum:
532 if (!IsStructurallyEquivalent(Context,
533 cast<TagType>(T1)->getDecl(),
534 cast<TagType>(T2)->getDecl()))
535 return false;
536 break;
537
538 case Type::Elaborated: {
539 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
540 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
541 if (Elab1->getTagKind() != Elab2->getTagKind())
542 return false;
543 if (!IsStructurallyEquivalent(Context,
544 Elab1->getUnderlyingType(),
545 Elab2->getUnderlyingType()))
546 return false;
547 break;
548 }
549
550 case Type::TemplateTypeParm: {
551 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
552 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
553 if (Parm1->getDepth() != Parm2->getDepth())
554 return false;
555 if (Parm1->getIndex() != Parm2->getIndex())
556 return false;
557 if (Parm1->isParameterPack() != Parm2->isParameterPack())
558 return false;
559
560 // Names of template type parameters are never significant.
561 break;
562 }
563
564 case Type::SubstTemplateTypeParm: {
565 const SubstTemplateTypeParmType *Subst1
566 = cast<SubstTemplateTypeParmType>(T1);
567 const SubstTemplateTypeParmType *Subst2
568 = cast<SubstTemplateTypeParmType>(T2);
569 if (!IsStructurallyEquivalent(Context,
570 QualType(Subst1->getReplacedParameter(), 0),
571 QualType(Subst2->getReplacedParameter(), 0)))
572 return false;
573 if (!IsStructurallyEquivalent(Context,
574 Subst1->getReplacementType(),
575 Subst2->getReplacementType()))
576 return false;
577 break;
578 }
579
580 case Type::TemplateSpecialization: {
581 const TemplateSpecializationType *Spec1
582 = cast<TemplateSpecializationType>(T1);
583 const TemplateSpecializationType *Spec2
584 = cast<TemplateSpecializationType>(T2);
585 if (!IsStructurallyEquivalent(Context,
586 Spec1->getTemplateName(),
587 Spec2->getTemplateName()))
588 return false;
589 if (Spec1->getNumArgs() != Spec2->getNumArgs())
590 return false;
591 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
592 if (!IsStructurallyEquivalent(Context,
593 Spec1->getArg(I), Spec2->getArg(I)))
594 return false;
595 }
596 break;
597 }
598
599 case Type::QualifiedName: {
600 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
601 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
602 if (!IsStructurallyEquivalent(Context,
603 Qual1->getQualifier(),
604 Qual2->getQualifier()))
605 return false;
606 if (!IsStructurallyEquivalent(Context,
607 Qual1->getNamedType(),
608 Qual2->getNamedType()))
609 return false;
610 break;
611 }
612
John McCall3cb0ebd2010-03-10 03:28:59 +0000613 case Type::InjectedClassName: {
614 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
615 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
616 if (!IsStructurallyEquivalent(Context,
617 Inj1->getUnderlyingType(),
618 Inj2->getUnderlyingType()))
619 return false;
620 break;
621 }
622
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000623 case Type::Typename: {
624 const TypenameType *Typename1 = cast<TypenameType>(T1);
625 const TypenameType *Typename2 = cast<TypenameType>(T2);
626 if (!IsStructurallyEquivalent(Context,
627 Typename1->getQualifier(),
628 Typename2->getQualifier()))
629 return false;
630 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
631 Typename2->getIdentifier()))
632 return false;
633 if (!IsStructurallyEquivalent(Context,
634 QualType(Typename1->getTemplateId(), 0),
635 QualType(Typename2->getTemplateId(), 0)))
636 return false;
637
638 break;
639 }
640
641 case Type::ObjCInterface: {
642 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
643 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
644 if (!IsStructurallyEquivalent(Context,
645 Iface1->getDecl(), Iface2->getDecl()))
646 return false;
647 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
648 return false;
649 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
650 if (!IsStructurallyEquivalent(Context,
651 Iface1->getProtocol(I),
652 Iface2->getProtocol(I)))
653 return false;
654 }
655 break;
656 }
657
658 case Type::ObjCObjectPointer: {
659 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
660 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
661 if (!IsStructurallyEquivalent(Context,
662 Ptr1->getPointeeType(),
663 Ptr2->getPointeeType()))
664 return false;
665 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
666 return false;
667 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
668 if (!IsStructurallyEquivalent(Context,
669 Ptr1->getProtocol(I),
670 Ptr2->getProtocol(I)))
671 return false;
672 }
673 break;
674 }
675
676 } // end switch
677
678 return true;
679}
680
681/// \brief Determine structural equivalence of two records.
682static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
683 RecordDecl *D1, RecordDecl *D2) {
684 if (D1->isUnion() != D2->isUnion()) {
685 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
686 << Context.C2.getTypeDeclType(D2);
687 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
688 << D1->getDeclName() << (unsigned)D1->getTagKind();
689 return false;
690 }
691
Douglas Gregorea35d112010-02-15 23:54:17 +0000692 // Compare the definitions of these two records. If either or both are
693 // incomplete, we assume that they are equivalent.
694 D1 = D1->getDefinition();
695 D2 = D2->getDefinition();
696 if (!D1 || !D2)
697 return true;
698
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000699 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
700 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
701 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
702 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
703 << Context.C2.getTypeDeclType(D2);
704 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
705 << D2CXX->getNumBases();
706 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
707 << D1CXX->getNumBases();
708 return false;
709 }
710
711 // Check the base classes.
712 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
713 BaseEnd1 = D1CXX->bases_end(),
714 Base2 = D2CXX->bases_begin();
715 Base1 != BaseEnd1;
716 ++Base1, ++Base2) {
717 if (!IsStructurallyEquivalent(Context,
718 Base1->getType(), Base2->getType())) {
719 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
720 << Context.C2.getTypeDeclType(D2);
721 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
722 << Base2->getType()
723 << Base2->getSourceRange();
724 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
725 << Base1->getType()
726 << Base1->getSourceRange();
727 return false;
728 }
729
730 // Check virtual vs. non-virtual inheritance mismatch.
731 if (Base1->isVirtual() != Base2->isVirtual()) {
732 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
733 << Context.C2.getTypeDeclType(D2);
734 Context.Diag2(Base2->getSourceRange().getBegin(),
735 diag::note_odr_virtual_base)
736 << Base2->isVirtual() << Base2->getSourceRange();
737 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
738 << Base1->isVirtual()
739 << Base1->getSourceRange();
740 return false;
741 }
742 }
743 } else if (D1CXX->getNumBases() > 0) {
744 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
745 << Context.C2.getTypeDeclType(D2);
746 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
747 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
748 << Base1->getType()
749 << Base1->getSourceRange();
750 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
751 return false;
752 }
753 }
754
755 // Check the fields for consistency.
756 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
757 Field2End = D2->field_end();
758 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
759 Field1End = D1->field_end();
760 Field1 != Field1End;
761 ++Field1, ++Field2) {
762 if (Field2 == Field2End) {
763 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
764 << Context.C2.getTypeDeclType(D2);
765 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
766 << Field1->getDeclName() << Field1->getType();
767 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
768 return false;
769 }
770
771 if (!IsStructurallyEquivalent(Context,
772 Field1->getType(), Field2->getType())) {
773 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
774 << Context.C2.getTypeDeclType(D2);
775 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
776 << Field2->getDeclName() << Field2->getType();
777 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
778 << Field1->getDeclName() << Field1->getType();
779 return false;
780 }
781
782 if (Field1->isBitField() != Field2->isBitField()) {
783 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
784 << Context.C2.getTypeDeclType(D2);
785 if (Field1->isBitField()) {
786 llvm::APSInt Bits;
787 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
788 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
789 << Field1->getDeclName() << Field1->getType()
790 << Bits.toString(10, false);
791 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
792 << Field2->getDeclName();
793 } else {
794 llvm::APSInt Bits;
795 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
796 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
797 << Field2->getDeclName() << Field2->getType()
798 << Bits.toString(10, false);
799 Context.Diag1(Field1->getLocation(),
800 diag::note_odr_not_bit_field)
801 << Field1->getDeclName();
802 }
803 return false;
804 }
805
806 if (Field1->isBitField()) {
807 // Make sure that the bit-fields are the same length.
808 llvm::APSInt Bits1, Bits2;
809 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
810 return false;
811 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
812 return false;
813
814 if (!IsSameValue(Bits1, Bits2)) {
815 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
816 << Context.C2.getTypeDeclType(D2);
817 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
818 << Field2->getDeclName() << Field2->getType()
819 << Bits2.toString(10, false);
820 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
821 << Field1->getDeclName() << Field1->getType()
822 << Bits1.toString(10, false);
823 return false;
824 }
825 }
826 }
827
828 if (Field2 != Field2End) {
829 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
830 << Context.C2.getTypeDeclType(D2);
831 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
832 << Field2->getDeclName() << Field2->getType();
833 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
834 return false;
835 }
836
837 return true;
838}
839
840/// \brief Determine structural equivalence of two enums.
841static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
842 EnumDecl *D1, EnumDecl *D2) {
843 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
844 EC2End = D2->enumerator_end();
845 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
846 EC1End = D1->enumerator_end();
847 EC1 != EC1End; ++EC1, ++EC2) {
848 if (EC2 == EC2End) {
849 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
850 << Context.C2.getTypeDeclType(D2);
851 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
852 << EC1->getDeclName()
853 << EC1->getInitVal().toString(10);
854 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
855 return false;
856 }
857
858 llvm::APSInt Val1 = EC1->getInitVal();
859 llvm::APSInt Val2 = EC2->getInitVal();
860 if (!IsSameValue(Val1, Val2) ||
861 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
862 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(D2);
864 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
865 << EC2->getDeclName()
866 << EC2->getInitVal().toString(10);
867 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
868 << EC1->getDeclName()
869 << EC1->getInitVal().toString(10);
870 return false;
871 }
872 }
873
874 if (EC2 != EC2End) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876 << Context.C2.getTypeDeclType(D2);
877 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
878 << EC2->getDeclName()
879 << EC2->getInitVal().toString(10);
880 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
881 return false;
882 }
883
884 return true;
885}
886
887/// \brief Determine structural equivalence of two declarations.
888static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
889 Decl *D1, Decl *D2) {
890 // FIXME: Check for known structural equivalences via a callback of some sort.
891
Douglas Gregorea35d112010-02-15 23:54:17 +0000892 // Check whether we already know that these two declarations are not
893 // structurally equivalent.
894 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
895 D2->getCanonicalDecl())))
896 return false;
897
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000898 // Determine whether we've already produced a tentative equivalence for D1.
899 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
900 if (EquivToD1)
901 return EquivToD1 == D2->getCanonicalDecl();
902
903 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
904 EquivToD1 = D2->getCanonicalDecl();
905 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
906 return true;
907}
908
909bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
910 Decl *D2) {
911 if (!::IsStructurallyEquivalent(*this, D1, D2))
912 return false;
913
914 return !Finish();
915}
916
917bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
918 QualType T2) {
919 if (!::IsStructurallyEquivalent(*this, T1, T2))
920 return false;
921
922 return !Finish();
923}
924
925bool StructuralEquivalenceContext::Finish() {
926 while (!DeclsToCheck.empty()) {
927 // Check the next declaration.
928 Decl *D1 = DeclsToCheck.front();
929 DeclsToCheck.pop_front();
930
931 Decl *D2 = TentativeEquivalences[D1];
932 assert(D2 && "Unrecorded tentative equivalence?");
933
Douglas Gregorea35d112010-02-15 23:54:17 +0000934 bool Equivalent = true;
935
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000936 // FIXME: Switch on all declaration kinds. For now, we're just going to
937 // check the obvious ones.
938 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
939 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
940 // Check for equivalent structure names.
941 IdentifierInfo *Name1 = Record1->getIdentifier();
942 if (!Name1 && Record1->getTypedefForAnonDecl())
943 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
944 IdentifierInfo *Name2 = Record2->getIdentifier();
945 if (!Name2 && Record2->getTypedefForAnonDecl())
946 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000947 if (!::IsStructurallyEquivalent(Name1, Name2) ||
948 !::IsStructurallyEquivalent(*this, Record1, Record2))
949 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000950 } else {
951 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000952 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000953 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000954 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000955 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
956 // Check for equivalent enum names.
957 IdentifierInfo *Name1 = Enum1->getIdentifier();
958 if (!Name1 && Enum1->getTypedefForAnonDecl())
959 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
960 IdentifierInfo *Name2 = Enum2->getIdentifier();
961 if (!Name2 && Enum2->getTypedefForAnonDecl())
962 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000963 if (!::IsStructurallyEquivalent(Name1, Name2) ||
964 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
965 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000966 } else {
967 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +0000968 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000969 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000970 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000971 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
972 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +0000973 Typedef2->getIdentifier()) ||
974 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000975 Typedef1->getUnderlyingType(),
976 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +0000977 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000978 } else {
979 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000980 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000981 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000982 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000983
984 if (!Equivalent) {
985 // Note that these two declarations are not equivalent (and we already
986 // know about it).
987 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
988 D2->getCanonicalDecl()));
989 return true;
990 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000991 // FIXME: Check other declaration kinds!
992 }
993
994 return false;
995}
996
997//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000998// Import Types
999//----------------------------------------------------------------------------
1000
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001001QualType ASTNodeImporter::VisitType(Type *T) {
1002 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1003 << T->getTypeClassName();
1004 return QualType();
1005}
1006
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001007QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1008 switch (T->getKind()) {
1009 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1010 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1011
1012 case BuiltinType::Char_U:
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().UnsignedCharTy;
1018
1019 return Importer.getToContext().CharTy;
1020
1021 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1022
1023 case BuiltinType::Char16:
1024 // FIXME: Make sure that the "to" context supports C++!
1025 return Importer.getToContext().Char16Ty;
1026
1027 case BuiltinType::Char32:
1028 // FIXME: Make sure that the "to" context supports C++!
1029 return Importer.getToContext().Char32Ty;
1030
1031 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1032 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1033 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1034 case BuiltinType::ULongLong:
1035 return Importer.getToContext().UnsignedLongLongTy;
1036 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1037
1038 case BuiltinType::Char_S:
1039 // The context we're importing from has an unsigned 'char'. If we're
1040 // importing into a context with a signed 'char', translate to
1041 // 'unsigned char' instead.
1042 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1043 return Importer.getToContext().SignedCharTy;
1044
1045 return Importer.getToContext().CharTy;
1046
1047 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1048 case BuiltinType::WChar:
1049 // FIXME: If not in C++, shall we translate to the C equivalent of
1050 // wchar_t?
1051 return Importer.getToContext().WCharTy;
1052
1053 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1054 case BuiltinType::Int : return Importer.getToContext().IntTy;
1055 case BuiltinType::Long : return Importer.getToContext().LongTy;
1056 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1057 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1058 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1059 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1060 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1061
1062 case BuiltinType::NullPtr:
1063 // FIXME: Make sure that the "to" context supports C++0x!
1064 return Importer.getToContext().NullPtrTy;
1065
1066 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1067 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1068 case BuiltinType::UndeducedAuto:
1069 // FIXME: Make sure that the "to" context supports C++0x!
1070 return Importer.getToContext().UndeducedAutoTy;
1071
1072 case BuiltinType::ObjCId:
1073 // FIXME: Make sure that the "to" context supports Objective-C!
1074 return Importer.getToContext().ObjCBuiltinIdTy;
1075
1076 case BuiltinType::ObjCClass:
1077 return Importer.getToContext().ObjCBuiltinClassTy;
1078
1079 case BuiltinType::ObjCSel:
1080 return Importer.getToContext().ObjCBuiltinSelTy;
1081 }
1082
1083 return QualType();
1084}
1085
1086QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1087 QualType ToElementType = Importer.Import(T->getElementType());
1088 if (ToElementType.isNull())
1089 return QualType();
1090
1091 return Importer.getToContext().getComplexType(ToElementType);
1092}
1093
1094QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1095 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1096 if (ToPointeeType.isNull())
1097 return QualType();
1098
1099 return Importer.getToContext().getPointerType(ToPointeeType);
1100}
1101
1102QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1103 // FIXME: Check for blocks support in "to" context.
1104 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1105 if (ToPointeeType.isNull())
1106 return QualType();
1107
1108 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1109}
1110
1111QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1112 // FIXME: Check for C++ support in "to" context.
1113 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1114 if (ToPointeeType.isNull())
1115 return QualType();
1116
1117 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1118}
1119
1120QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1121 // FIXME: Check for C++0x support in "to" context.
1122 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1123 if (ToPointeeType.isNull())
1124 return QualType();
1125
1126 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1127}
1128
1129QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1130 // FIXME: Check for C++ support in "to" context.
1131 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1132 if (ToPointeeType.isNull())
1133 return QualType();
1134
1135 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1136 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1137 ClassType.getTypePtr());
1138}
1139
1140QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1141 QualType ToElementType = Importer.Import(T->getElementType());
1142 if (ToElementType.isNull())
1143 return QualType();
1144
1145 return Importer.getToContext().getConstantArrayType(ToElementType,
1146 T->getSize(),
1147 T->getSizeModifier(),
1148 T->getIndexTypeCVRQualifiers());
1149}
1150
1151QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1152 QualType ToElementType = Importer.Import(T->getElementType());
1153 if (ToElementType.isNull())
1154 return QualType();
1155
1156 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1157 T->getSizeModifier(),
1158 T->getIndexTypeCVRQualifiers());
1159}
1160
1161QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1162 QualType ToElementType = Importer.Import(T->getElementType());
1163 if (ToElementType.isNull())
1164 return QualType();
1165
1166 Expr *Size = Importer.Import(T->getSizeExpr());
1167 if (!Size)
1168 return QualType();
1169
1170 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1171 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1172 T->getSizeModifier(),
1173 T->getIndexTypeCVRQualifiers(),
1174 Brackets);
1175}
1176
1177QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1178 QualType ToElementType = Importer.Import(T->getElementType());
1179 if (ToElementType.isNull())
1180 return QualType();
1181
1182 return Importer.getToContext().getVectorType(ToElementType,
1183 T->getNumElements(),
1184 T->isAltiVec(),
1185 T->isPixel());
1186}
1187
1188QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1189 QualType ToElementType = Importer.Import(T->getElementType());
1190 if (ToElementType.isNull())
1191 return QualType();
1192
1193 return Importer.getToContext().getExtVectorType(ToElementType,
1194 T->getNumElements());
1195}
1196
1197QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1198 // FIXME: What happens if we're importing a function without a prototype
1199 // into C++? Should we make it variadic?
1200 QualType ToResultType = Importer.Import(T->getResultType());
1201 if (ToResultType.isNull())
1202 return QualType();
1203
1204 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1205 T->getNoReturnAttr(),
1206 T->getCallConv());
1207}
1208
1209QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1210 QualType ToResultType = Importer.Import(T->getResultType());
1211 if (ToResultType.isNull())
1212 return QualType();
1213
1214 // Import argument types
1215 llvm::SmallVector<QualType, 4> ArgTypes;
1216 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1217 AEnd = T->arg_type_end();
1218 A != AEnd; ++A) {
1219 QualType ArgType = Importer.Import(*A);
1220 if (ArgType.isNull())
1221 return QualType();
1222 ArgTypes.push_back(ArgType);
1223 }
1224
1225 // Import exception types
1226 llvm::SmallVector<QualType, 4> ExceptionTypes;
1227 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1228 EEnd = T->exception_end();
1229 E != EEnd; ++E) {
1230 QualType ExceptionType = Importer.Import(*E);
1231 if (ExceptionType.isNull())
1232 return QualType();
1233 ExceptionTypes.push_back(ExceptionType);
1234 }
1235
1236 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1237 ArgTypes.size(),
1238 T->isVariadic(),
1239 T->getTypeQuals(),
1240 T->hasExceptionSpec(),
1241 T->hasAnyExceptionSpec(),
1242 ExceptionTypes.size(),
1243 ExceptionTypes.data(),
1244 T->getNoReturnAttr(),
1245 T->getCallConv());
1246}
1247
1248QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1249 TypedefDecl *ToDecl
1250 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1251 if (!ToDecl)
1252 return QualType();
1253
1254 return Importer.getToContext().getTypeDeclType(ToDecl);
1255}
1256
1257QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1258 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1259 if (!ToExpr)
1260 return QualType();
1261
1262 return Importer.getToContext().getTypeOfExprType(ToExpr);
1263}
1264
1265QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1266 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1267 if (ToUnderlyingType.isNull())
1268 return QualType();
1269
1270 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1271}
1272
1273QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1274 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1275 if (!ToExpr)
1276 return QualType();
1277
1278 return Importer.getToContext().getDecltypeType(ToExpr);
1279}
1280
1281QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1282 RecordDecl *ToDecl
1283 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1284 if (!ToDecl)
1285 return QualType();
1286
1287 return Importer.getToContext().getTagDeclType(ToDecl);
1288}
1289
1290QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1291 EnumDecl *ToDecl
1292 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1293 if (!ToDecl)
1294 return QualType();
1295
1296 return Importer.getToContext().getTagDeclType(ToDecl);
1297}
1298
1299QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1300 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1301 if (ToUnderlyingType.isNull())
1302 return QualType();
1303
1304 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1305 T->getTagKind());
1306}
1307
1308QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1309 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1310 if (!ToQualifier)
1311 return QualType();
1312
1313 QualType ToNamedType = Importer.Import(T->getNamedType());
1314 if (ToNamedType.isNull())
1315 return QualType();
1316
1317 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1318}
1319
1320QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1321 ObjCInterfaceDecl *Class
1322 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1323 if (!Class)
1324 return QualType();
1325
1326 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1327 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1328 PEnd = T->qual_end();
1329 P != PEnd; ++P) {
1330 ObjCProtocolDecl *Protocol
1331 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1332 if (!Protocol)
1333 return QualType();
1334 Protocols.push_back(Protocol);
1335 }
1336
1337 return Importer.getToContext().getObjCInterfaceType(Class,
1338 Protocols.data(),
1339 Protocols.size());
1340}
1341
1342QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1343 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1344 if (ToPointeeType.isNull())
1345 return QualType();
1346
1347 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1348 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1349 PEnd = T->qual_end();
1350 P != PEnd; ++P) {
1351 ObjCProtocolDecl *Protocol
1352 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1353 if (!Protocol)
1354 return QualType();
1355 Protocols.push_back(Protocol);
1356 }
1357
1358 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1359 Protocols.data(),
1360 Protocols.size());
1361}
1362
Douglas Gregor089459a2010-02-08 21:09:39 +00001363//----------------------------------------------------------------------------
1364// Import Declarations
1365//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001366bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1367 DeclContext *&LexicalDC,
1368 DeclarationName &Name,
1369 SourceLocation &Loc) {
1370 // Import the context of this declaration.
1371 DC = Importer.ImportContext(D->getDeclContext());
1372 if (!DC)
1373 return true;
1374
1375 LexicalDC = DC;
1376 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1377 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1378 if (!LexicalDC)
1379 return true;
1380 }
1381
1382 // Import the name of this declaration.
1383 Name = Importer.Import(D->getDeclName());
1384 if (D->getDeclName() && !Name)
1385 return true;
1386
1387 // Import the location of this declaration.
1388 Loc = Importer.Import(D->getLocation());
1389 return false;
1390}
1391
Douglas Gregor083a8212010-02-21 18:24:45 +00001392void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1393 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1394 FromEnd = FromDC->decls_end();
1395 From != FromEnd;
1396 ++From)
1397 Importer.Import(*From);
1398}
1399
Douglas Gregor96a01b42010-02-11 00:48:18 +00001400bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001401 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001402 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001403 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001404 Importer.getDiags(),
1405 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001406 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001407}
1408
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001409bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001410 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001411 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001412 Importer.getDiags(),
1413 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001414 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001415}
1416
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001417Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001418 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001419 << D->getDeclKindName();
1420 return 0;
1421}
1422
Douglas Gregor788c62d2010-02-21 18:26:36 +00001423Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1424 // Import the major distinguishing characteristics of this namespace.
1425 DeclContext *DC, *LexicalDC;
1426 DeclarationName Name;
1427 SourceLocation Loc;
1428 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1429 return 0;
1430
1431 NamespaceDecl *MergeWithNamespace = 0;
1432 if (!Name) {
1433 // This is an anonymous namespace. Adopt an existing anonymous
1434 // namespace if we can.
1435 // FIXME: Not testable.
1436 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1437 MergeWithNamespace = TU->getAnonymousNamespace();
1438 else
1439 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1440 } else {
1441 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1442 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1443 Lookup.first != Lookup.second;
1444 ++Lookup.first) {
1445 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1446 continue;
1447
1448 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1449 MergeWithNamespace = FoundNS;
1450 ConflictingDecls.clear();
1451 break;
1452 }
1453
1454 ConflictingDecls.push_back(*Lookup.first);
1455 }
1456
1457 if (!ConflictingDecls.empty()) {
1458 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
1459 ConflictingDecls.data(),
1460 ConflictingDecls.size());
1461 }
1462 }
1463
1464 // Create the "to" namespace, if needed.
1465 NamespaceDecl *ToNamespace = MergeWithNamespace;
1466 if (!ToNamespace) {
1467 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1468 Name.getAsIdentifierInfo());
1469 ToNamespace->setLexicalDeclContext(LexicalDC);
1470 LexicalDC->addDecl(ToNamespace);
1471
1472 // If this is an anonymous namespace, register it as the anonymous
1473 // namespace within its context.
1474 if (!Name) {
1475 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1476 TU->setAnonymousNamespace(ToNamespace);
1477 else
1478 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1479 }
1480 }
1481 Importer.Imported(D, ToNamespace);
1482
1483 ImportDeclContext(D);
1484
1485 return ToNamespace;
1486}
1487
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001488Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1489 // Import the major distinguishing characteristics of this typedef.
1490 DeclContext *DC, *LexicalDC;
1491 DeclarationName Name;
1492 SourceLocation Loc;
1493 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1494 return 0;
1495
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001496 // If this typedef is not in block scope, determine whether we've
1497 // seen a typedef with the same name (that we can merge with) or any
1498 // other entity by that name (which name lookup could conflict with).
1499 if (!DC->isFunctionOrMethod()) {
1500 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1501 unsigned IDNS = Decl::IDNS_Ordinary;
1502 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1503 Lookup.first != Lookup.second;
1504 ++Lookup.first) {
1505 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1506 continue;
1507 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001508 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1509 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001510 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001511 }
1512
1513 ConflictingDecls.push_back(*Lookup.first);
1514 }
1515
1516 if (!ConflictingDecls.empty()) {
1517 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1518 ConflictingDecls.data(),
1519 ConflictingDecls.size());
1520 if (!Name)
1521 return 0;
1522 }
1523 }
1524
Douglas Gregorea35d112010-02-15 23:54:17 +00001525 // Import the underlying type of this typedef;
1526 QualType T = Importer.Import(D->getUnderlyingType());
1527 if (T.isNull())
1528 return 0;
1529
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001530 // Create the new typedef node.
1531 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1532 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1533 Loc, Name.getAsIdentifierInfo(),
1534 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001535 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001536 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001537 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001538 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001539
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001540 return ToTypedef;
1541}
1542
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001543Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1544 // Import the major distinguishing characteristics of this enum.
1545 DeclContext *DC, *LexicalDC;
1546 DeclarationName Name;
1547 SourceLocation Loc;
1548 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1549 return 0;
1550
1551 // Figure out what enum name we're looking for.
1552 unsigned IDNS = Decl::IDNS_Tag;
1553 DeclarationName SearchName = Name;
1554 if (!SearchName && D->getTypedefForAnonDecl()) {
1555 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1556 IDNS = Decl::IDNS_Ordinary;
1557 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1558 IDNS |= Decl::IDNS_Ordinary;
1559
1560 // We may already have an enum of the same name; try to find and match it.
1561 if (!DC->isFunctionOrMethod() && SearchName) {
1562 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1563 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1564 Lookup.first != Lookup.second;
1565 ++Lookup.first) {
1566 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1567 continue;
1568
1569 Decl *Found = *Lookup.first;
1570 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1571 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1572 Found = Tag->getDecl();
1573 }
1574
1575 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001576 if (IsStructuralMatch(D, FoundEnum))
1577 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001578 }
1579
1580 ConflictingDecls.push_back(*Lookup.first);
1581 }
1582
1583 if (!ConflictingDecls.empty()) {
1584 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1585 ConflictingDecls.data(),
1586 ConflictingDecls.size());
1587 }
1588 }
1589
1590 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001591 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001592 Name.getAsIdentifierInfo(),
1593 Importer.Import(D->getTagKeywordLoc()),
1594 0);
John McCallb6217662010-03-15 10:12:16 +00001595 // Import the qualifier, if any.
1596 if (D->getQualifier()) {
1597 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1598 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1599 D2->setQualifierInfo(NNS, NNSRange);
1600 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001601 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001602 D2->setLexicalDeclContext(LexicalDC);
1603 Importer.Imported(D, D2);
1604 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001605
1606 // Import the integer type.
1607 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1608 if (ToIntegerType.isNull())
1609 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001610 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001611
1612 // Import the definition
1613 if (D->isDefinition()) {
1614 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1615 if (T.isNull())
1616 return 0;
1617
1618 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1619 if (ToPromotionType.isNull())
1620 return 0;
1621
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001622 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00001623 ImportDeclContext(D);
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001624 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001625 }
1626
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001627 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001628}
1629
Douglas Gregor96a01b42010-02-11 00:48:18 +00001630Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1631 // If this record has a definition in the translation unit we're coming from,
1632 // but this particular declaration is not that definition, import the
1633 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00001634 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001635 if (Definition && Definition != D) {
1636 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001637 if (!ImportedDef)
1638 return 0;
1639
1640 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001641 }
1642
1643 // Import the major distinguishing characteristics of this record.
1644 DeclContext *DC, *LexicalDC;
1645 DeclarationName Name;
1646 SourceLocation Loc;
1647 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1648 return 0;
1649
1650 // Figure out what structure name we're looking for.
1651 unsigned IDNS = Decl::IDNS_Tag;
1652 DeclarationName SearchName = Name;
1653 if (!SearchName && D->getTypedefForAnonDecl()) {
1654 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1655 IDNS = Decl::IDNS_Ordinary;
1656 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1657 IDNS |= Decl::IDNS_Ordinary;
1658
1659 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001660 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001661 if (!DC->isFunctionOrMethod() && SearchName) {
1662 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1663 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1664 Lookup.first != Lookup.second;
1665 ++Lookup.first) {
1666 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1667 continue;
1668
1669 Decl *Found = *Lookup.first;
1670 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1671 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1672 Found = Tag->getDecl();
1673 }
1674
1675 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001676 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1677 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1678 // The record types structurally match, or the "from" translation
1679 // unit only had a forward declaration anyway; call it the same
1680 // function.
1681 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001682 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001683 }
1684 } else {
1685 // We have a forward declaration of this type, so adopt that forward
1686 // declaration rather than building a new one.
1687 AdoptDecl = FoundRecord;
1688 continue;
1689 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00001690 }
1691
1692 ConflictingDecls.push_back(*Lookup.first);
1693 }
1694
1695 if (!ConflictingDecls.empty()) {
1696 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1697 ConflictingDecls.data(),
1698 ConflictingDecls.size());
1699 }
1700 }
1701
1702 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001703 RecordDecl *D2 = AdoptDecl;
1704 if (!D2) {
1705 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1706 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001707 D->getTagKind(),
1708 DC, Loc,
1709 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001710 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001711 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00001712 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001713
1714 if (D->isDefinition()) {
1715 // Add base classes.
1716 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1717 for (CXXRecordDecl::base_class_iterator
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001718 Base1 = D1CXX->bases_begin(),
1719 FromBaseEnd = D1CXX->bases_end();
1720 Base1 != FromBaseEnd;
1721 ++Base1) {
1722 QualType T = Importer.Import(Base1->getType());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001723 if (T.isNull())
1724 return 0;
1725
1726 Bases.push_back(
1727 new (Importer.getToContext())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001728 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1729 Base1->isVirtual(),
1730 Base1->isBaseOfClass(),
1731 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001732 T));
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001733 }
1734 if (!Bases.empty())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001735 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001736 }
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001737 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001738 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001739 DC, Loc,
1740 Name.getAsIdentifierInfo(),
1741 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00001742 }
John McCallb6217662010-03-15 10:12:16 +00001743 // Import the qualifier, if any.
1744 if (D->getQualifier()) {
1745 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1746 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1747 D2->setQualifierInfo(NNS, NNSRange);
1748 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001749 D2->setLexicalDeclContext(LexicalDC);
1750 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001751 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001752
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001753 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001754
Douglas Gregor96a01b42010-02-11 00:48:18 +00001755 if (D->isDefinition()) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001756 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00001757 ImportDeclContext(D);
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001758 D2->completeDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001759 }
1760
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001761 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001762}
1763
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001764Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1765 // Import the major distinguishing characteristics of this enumerator.
1766 DeclContext *DC, *LexicalDC;
1767 DeclarationName Name;
1768 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001769 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001770 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00001771
1772 QualType T = Importer.Import(D->getType());
1773 if (T.isNull())
1774 return 0;
1775
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001776 // Determine whether there are any other declarations with the same name and
1777 // in the same context.
1778 if (!LexicalDC->isFunctionOrMethod()) {
1779 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1780 unsigned IDNS = Decl::IDNS_Ordinary;
1781 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1782 Lookup.first != Lookup.second;
1783 ++Lookup.first) {
1784 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1785 continue;
1786
1787 ConflictingDecls.push_back(*Lookup.first);
1788 }
1789
1790 if (!ConflictingDecls.empty()) {
1791 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1792 ConflictingDecls.data(),
1793 ConflictingDecls.size());
1794 if (!Name)
1795 return 0;
1796 }
1797 }
1798
1799 Expr *Init = Importer.Import(D->getInitExpr());
1800 if (D->getInitExpr() && !Init)
1801 return 0;
1802
1803 EnumConstantDecl *ToEnumerator
1804 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1805 Name.getAsIdentifierInfo(), T,
1806 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00001807 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001808 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001809 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001810 LexicalDC->addDecl(ToEnumerator);
1811 return ToEnumerator;
1812}
Douglas Gregor96a01b42010-02-11 00:48:18 +00001813
Douglas Gregora404ea62010-02-10 19:54:31 +00001814Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1815 // Import the major distinguishing characteristics of this function.
1816 DeclContext *DC, *LexicalDC;
1817 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00001818 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001819 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00001820 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001821
1822 // Try to find a function in our own ("to") context with the same name, same
1823 // type, and in the same context as the function we're importing.
1824 if (!LexicalDC->isFunctionOrMethod()) {
1825 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1826 unsigned IDNS = Decl::IDNS_Ordinary;
1827 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1828 Lookup.first != Lookup.second;
1829 ++Lookup.first) {
1830 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1831 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00001832
Douglas Gregora404ea62010-02-10 19:54:31 +00001833 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1834 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1835 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001836 if (Importer.IsStructurallyEquivalent(D->getType(),
1837 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00001838 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001839 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00001840 }
1841
1842 // FIXME: Check for overloading more carefully, e.g., by boosting
1843 // Sema::IsOverload out to the AST library.
1844
1845 // Function overloading is okay in C++.
1846 if (Importer.getToContext().getLangOptions().CPlusPlus)
1847 continue;
1848
1849 // Complain about inconsistent function types.
1850 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00001851 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00001852 Importer.ToDiag(FoundFunction->getLocation(),
1853 diag::note_odr_value_here)
1854 << FoundFunction->getType();
1855 }
1856 }
1857
1858 ConflictingDecls.push_back(*Lookup.first);
1859 }
1860
1861 if (!ConflictingDecls.empty()) {
1862 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1863 ConflictingDecls.data(),
1864 ConflictingDecls.size());
1865 if (!Name)
1866 return 0;
1867 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00001868 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001869
1870 // Import the type.
1871 QualType T = Importer.Import(D->getType());
1872 if (T.isNull())
1873 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001874
1875 // Import the function parameters.
1876 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1877 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1878 P != PEnd; ++P) {
1879 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1880 if (!ToP)
1881 return 0;
1882
1883 Parameters.push_back(ToP);
1884 }
1885
1886 // Create the imported function.
1887 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00001888 FunctionDecl *ToFunction = 0;
1889 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1890 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1891 cast<CXXRecordDecl>(DC),
1892 Loc, Name, T, TInfo,
1893 FromConstructor->isExplicit(),
1894 D->isInlineSpecified(),
1895 D->isImplicit());
1896 } else if (isa<CXXDestructorDecl>(D)) {
1897 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1898 cast<CXXRecordDecl>(DC),
1899 Loc, Name, T,
1900 D->isInlineSpecified(),
1901 D->isImplicit());
1902 } else if (CXXConversionDecl *FromConversion
1903 = dyn_cast<CXXConversionDecl>(D)) {
1904 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1905 cast<CXXRecordDecl>(DC),
1906 Loc, Name, T, TInfo,
1907 D->isInlineSpecified(),
1908 FromConversion->isExplicit());
1909 } else {
1910 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1911 Name, T, TInfo, D->getStorageClass(),
1912 D->isInlineSpecified(),
1913 D->hasWrittenPrototype());
1914 }
John McCallb6217662010-03-15 10:12:16 +00001915
1916 // Import the qualifier, if any.
1917 if (D->getQualifier()) {
1918 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1919 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1920 ToFunction->setQualifierInfo(NNS, NNSRange);
1921 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001922 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001923 ToFunction->setLexicalDeclContext(LexicalDC);
1924 Importer.Imported(D, ToFunction);
1925 LexicalDC->addDecl(ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001926
Douglas Gregora404ea62010-02-10 19:54:31 +00001927 // Set the parameters.
1928 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001929 Parameters[I]->setOwningFunction(ToFunction);
1930 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00001931 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001932 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00001933
1934 // FIXME: Other bits to merge?
Douglas Gregor089459a2010-02-08 21:09:39 +00001935
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001936 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00001937}
1938
Douglas Gregorc144f352010-02-21 18:29:16 +00001939Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1940 return VisitFunctionDecl(D);
1941}
1942
1943Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1944 return VisitCXXMethodDecl(D);
1945}
1946
1947Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1948 return VisitCXXMethodDecl(D);
1949}
1950
1951Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1952 return VisitCXXMethodDecl(D);
1953}
1954
Douglas Gregor96a01b42010-02-11 00:48:18 +00001955Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1956 // Import the major distinguishing characteristics of a variable.
1957 DeclContext *DC, *LexicalDC;
1958 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001959 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001960 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1961 return 0;
1962
1963 // Import the type.
1964 QualType T = Importer.Import(D->getType());
1965 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00001966 return 0;
1967
1968 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1969 Expr *BitWidth = Importer.Import(D->getBitWidth());
1970 if (!BitWidth && D->getBitWidth())
1971 return 0;
1972
1973 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1974 Loc, Name.getAsIdentifierInfo(),
1975 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00001976 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001977 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001978 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001979 LexicalDC->addDecl(ToField);
1980 return ToField;
1981}
1982
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00001983Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1984 // Import the major distinguishing characteristics of an ivar.
1985 DeclContext *DC, *LexicalDC;
1986 DeclarationName Name;
1987 SourceLocation Loc;
1988 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1989 return 0;
1990
1991 // Determine whether we've already imported this ivar
1992 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1993 Lookup.first != Lookup.second;
1994 ++Lookup.first) {
1995 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
1996 if (Importer.IsStructurallyEquivalent(D->getType(),
1997 FoundIvar->getType())) {
1998 Importer.Imported(D, FoundIvar);
1999 return FoundIvar;
2000 }
2001
2002 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2003 << Name << D->getType() << FoundIvar->getType();
2004 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2005 << FoundIvar->getType();
2006 return 0;
2007 }
2008 }
2009
2010 // Import the type.
2011 QualType T = Importer.Import(D->getType());
2012 if (T.isNull())
2013 return 0;
2014
2015 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2016 Expr *BitWidth = Importer.Import(D->getBitWidth());
2017 if (!BitWidth && D->getBitWidth())
2018 return 0;
2019
2020 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(), DC,
2021 Loc, Name.getAsIdentifierInfo(),
2022 T, TInfo, D->getAccessControl(),
2023 BitWidth);
2024 ToIvar->setLexicalDeclContext(LexicalDC);
2025 Importer.Imported(D, ToIvar);
2026 LexicalDC->addDecl(ToIvar);
2027 return ToIvar;
2028
2029}
2030
Douglas Gregora404ea62010-02-10 19:54:31 +00002031Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2032 // Import the major distinguishing characteristics of a variable.
2033 DeclContext *DC, *LexicalDC;
2034 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002035 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002036 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002037 return 0;
2038
Douglas Gregor089459a2010-02-08 21:09:39 +00002039 // Try to find a variable in our own ("to") context with the same name and
2040 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002041 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002042 VarDecl *MergeWithVar = 0;
2043 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2044 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002045 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002046 Lookup.first != Lookup.second;
2047 ++Lookup.first) {
2048 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2049 continue;
2050
2051 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2052 // We have found a variable that we may need to merge with. Check it.
2053 if (isExternalLinkage(FoundVar->getLinkage()) &&
2054 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002055 if (Importer.IsStructurallyEquivalent(D->getType(),
2056 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002057 MergeWithVar = FoundVar;
2058 break;
2059 }
2060
Douglas Gregord0145422010-02-12 17:23:39 +00002061 const ArrayType *FoundArray
2062 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2063 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002064 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002065 if (FoundArray && TArray) {
2066 if (isa<IncompleteArrayType>(FoundArray) &&
2067 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002068 // Import the type.
2069 QualType T = Importer.Import(D->getType());
2070 if (T.isNull())
2071 return 0;
2072
Douglas Gregord0145422010-02-12 17:23:39 +00002073 FoundVar->setType(T);
2074 MergeWithVar = FoundVar;
2075 break;
2076 } else if (isa<IncompleteArrayType>(TArray) &&
2077 isa<ConstantArrayType>(FoundArray)) {
2078 MergeWithVar = FoundVar;
2079 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002080 }
2081 }
2082
Douglas Gregor089459a2010-02-08 21:09:39 +00002083 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002084 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002085 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2086 << FoundVar->getType();
2087 }
2088 }
2089
2090 ConflictingDecls.push_back(*Lookup.first);
2091 }
2092
2093 if (MergeWithVar) {
2094 // An equivalent variable with external linkage has been found. Link
2095 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002096 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002097
2098 if (VarDecl *DDef = D->getDefinition()) {
2099 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2100 Importer.ToDiag(ExistingDef->getLocation(),
2101 diag::err_odr_variable_multiple_def)
2102 << Name;
2103 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2104 } else {
2105 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002106 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002107 }
2108 }
2109
2110 return MergeWithVar;
2111 }
2112
2113 if (!ConflictingDecls.empty()) {
2114 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2115 ConflictingDecls.data(),
2116 ConflictingDecls.size());
2117 if (!Name)
2118 return 0;
2119 }
2120 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002121
Douglas Gregorea35d112010-02-15 23:54:17 +00002122 // Import the type.
2123 QualType T = Importer.Import(D->getType());
2124 if (T.isNull())
2125 return 0;
2126
Douglas Gregor089459a2010-02-08 21:09:39 +00002127 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002128 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002129 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2130 Name.getAsIdentifierInfo(), T, TInfo,
2131 D->getStorageClass());
John McCallb6217662010-03-15 10:12:16 +00002132 // Import the qualifier, if any.
2133 if (D->getQualifier()) {
2134 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2135 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2136 ToVar->setQualifierInfo(NNS, NNSRange);
2137 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002138 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002139 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002140 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002141 LexicalDC->addDecl(ToVar);
2142
Douglas Gregor089459a2010-02-08 21:09:39 +00002143 // Merge the initializer.
2144 // FIXME: Can we really import any initializer? Alternatively, we could force
2145 // ourselves to import every declaration of a variable and then only use
2146 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002147 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002148
2149 // FIXME: Other bits to merge?
2150
2151 return ToVar;
2152}
2153
Douglas Gregor2cd00932010-02-17 21:22:52 +00002154Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2155 // Parameters are created in the translation unit's context, then moved
2156 // into the function declaration's context afterward.
2157 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2158
2159 // Import the name of this declaration.
2160 DeclarationName Name = Importer.Import(D->getDeclName());
2161 if (D->getDeclName() && !Name)
2162 return 0;
2163
2164 // Import the location of this declaration.
2165 SourceLocation Loc = Importer.Import(D->getLocation());
2166
2167 // Import the parameter's type.
2168 QualType T = Importer.Import(D->getType());
2169 if (T.isNull())
2170 return 0;
2171
2172 // Create the imported parameter.
2173 ImplicitParamDecl *ToParm
2174 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2175 Loc, Name.getAsIdentifierInfo(),
2176 T);
2177 return Importer.Imported(D, ToParm);
2178}
2179
Douglas Gregora404ea62010-02-10 19:54:31 +00002180Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2181 // Parameters are created in the translation unit's context, then moved
2182 // into the function declaration's context afterward.
2183 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2184
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002185 // Import the name of this declaration.
2186 DeclarationName Name = Importer.Import(D->getDeclName());
2187 if (D->getDeclName() && !Name)
2188 return 0;
2189
Douglas Gregora404ea62010-02-10 19:54:31 +00002190 // Import the location of this declaration.
2191 SourceLocation Loc = Importer.Import(D->getLocation());
2192
2193 // Import the parameter's type.
2194 QualType T = Importer.Import(D->getType());
2195 if (T.isNull())
2196 return 0;
2197
2198 // Create the imported parameter.
2199 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2200 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2201 Loc, Name.getAsIdentifierInfo(),
2202 T, TInfo, D->getStorageClass(),
2203 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002204 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002205 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002206}
2207
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002208Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2209 // Import the major distinguishing characteristics of a method.
2210 DeclContext *DC, *LexicalDC;
2211 DeclarationName Name;
2212 SourceLocation Loc;
2213 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2214 return 0;
2215
2216 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2217 Lookup.first != Lookup.second;
2218 ++Lookup.first) {
2219 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2220 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2221 continue;
2222
2223 // Check return types.
2224 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2225 FoundMethod->getResultType())) {
2226 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2227 << D->isInstanceMethod() << Name
2228 << D->getResultType() << FoundMethod->getResultType();
2229 Importer.ToDiag(FoundMethod->getLocation(),
2230 diag::note_odr_objc_method_here)
2231 << D->isInstanceMethod() << Name;
2232 return 0;
2233 }
2234
2235 // Check the number of parameters.
2236 if (D->param_size() != FoundMethod->param_size()) {
2237 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2238 << D->isInstanceMethod() << Name
2239 << D->param_size() << FoundMethod->param_size();
2240 Importer.ToDiag(FoundMethod->getLocation(),
2241 diag::note_odr_objc_method_here)
2242 << D->isInstanceMethod() << Name;
2243 return 0;
2244 }
2245
2246 // Check parameter types.
2247 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2248 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2249 P != PEnd; ++P, ++FoundP) {
2250 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2251 (*FoundP)->getType())) {
2252 Importer.FromDiag((*P)->getLocation(),
2253 diag::err_odr_objc_method_param_type_inconsistent)
2254 << D->isInstanceMethod() << Name
2255 << (*P)->getType() << (*FoundP)->getType();
2256 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2257 << (*FoundP)->getType();
2258 return 0;
2259 }
2260 }
2261
2262 // Check variadic/non-variadic.
2263 // Check the number of parameters.
2264 if (D->isVariadic() != FoundMethod->isVariadic()) {
2265 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2266 << D->isInstanceMethod() << Name;
2267 Importer.ToDiag(FoundMethod->getLocation(),
2268 diag::note_odr_objc_method_here)
2269 << D->isInstanceMethod() << Name;
2270 return 0;
2271 }
2272
2273 // FIXME: Any other bits we need to merge?
2274 return Importer.Imported(D, FoundMethod);
2275 }
2276 }
2277
2278 // Import the result type.
2279 QualType ResultTy = Importer.Import(D->getResultType());
2280 if (ResultTy.isNull())
2281 return 0;
2282
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002283 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2284
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002285 ObjCMethodDecl *ToMethod
2286 = ObjCMethodDecl::Create(Importer.getToContext(),
2287 Loc,
2288 Importer.Import(D->getLocEnd()),
2289 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002290 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002291 D->isInstanceMethod(),
2292 D->isVariadic(),
2293 D->isSynthesized(),
2294 D->getImplementationControl());
2295
2296 // FIXME: When we decide to merge method definitions, we'll need to
2297 // deal with implicit parameters.
2298
2299 // Import the parameters
2300 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2301 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2302 FromPEnd = D->param_end();
2303 FromP != FromPEnd;
2304 ++FromP) {
2305 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2306 if (!ToP)
2307 return 0;
2308
2309 ToParams.push_back(ToP);
2310 }
2311
2312 // Set the parameters.
2313 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2314 ToParams[I]->setOwningFunction(ToMethod);
2315 ToMethod->addDecl(ToParams[I]);
2316 }
2317 ToMethod->setMethodParams(Importer.getToContext(),
2318 ToParams.data(), ToParams.size());
2319
2320 ToMethod->setLexicalDeclContext(LexicalDC);
2321 Importer.Imported(D, ToMethod);
2322 LexicalDC->addDecl(ToMethod);
2323 return ToMethod;
2324}
2325
Douglas Gregorb4677b62010-02-18 01:47:50 +00002326Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2327 // Import the major distinguishing characteristics of a category.
2328 DeclContext *DC, *LexicalDC;
2329 DeclarationName Name;
2330 SourceLocation Loc;
2331 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2332 return 0;
2333
2334 ObjCInterfaceDecl *ToInterface
2335 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2336 if (!ToInterface)
2337 return 0;
2338
2339 // Determine if we've already encountered this category.
2340 ObjCCategoryDecl *MergeWithCategory
2341 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2342 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2343 if (!ToCategory) {
2344 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2345 Importer.Import(D->getAtLoc()),
2346 Loc,
2347 Importer.Import(D->getCategoryNameLoc()),
2348 Name.getAsIdentifierInfo());
2349 ToCategory->setLexicalDeclContext(LexicalDC);
2350 LexicalDC->addDecl(ToCategory);
2351 Importer.Imported(D, ToCategory);
2352
2353 // Link this category into its class's category list.
2354 ToCategory->setClassInterface(ToInterface);
2355 ToCategory->insertNextClassCategory();
2356
2357 // Import protocols
2358 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2359 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2360 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2361 = D->protocol_loc_begin();
2362 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2363 FromProtoEnd = D->protocol_end();
2364 FromProto != FromProtoEnd;
2365 ++FromProto, ++FromProtoLoc) {
2366 ObjCProtocolDecl *ToProto
2367 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2368 if (!ToProto)
2369 return 0;
2370 Protocols.push_back(ToProto);
2371 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2372 }
2373
2374 // FIXME: If we're merging, make sure that the protocol list is the same.
2375 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2376 ProtocolLocs.data(), Importer.getToContext());
2377
2378 } else {
2379 Importer.Imported(D, ToCategory);
2380 }
2381
2382 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002383 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002384
2385 // If we have an implementation, import it as well.
2386 if (D->getImplementation()) {
2387 ObjCCategoryImplDecl *Impl
2388 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2389 if (!Impl)
2390 return 0;
2391
2392 ToCategory->setImplementation(Impl);
2393 }
2394
2395 return ToCategory;
2396}
2397
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002398Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002399 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002400 DeclContext *DC, *LexicalDC;
2401 DeclarationName Name;
2402 SourceLocation Loc;
2403 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2404 return 0;
2405
2406 ObjCProtocolDecl *MergeWithProtocol = 0;
2407 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2408 Lookup.first != Lookup.second;
2409 ++Lookup.first) {
2410 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2411 continue;
2412
2413 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2414 break;
2415 }
2416
2417 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2418 if (!ToProto || ToProto->isForwardDecl()) {
2419 if (!ToProto) {
2420 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2421 Name.getAsIdentifierInfo());
2422 ToProto->setForwardDecl(D->isForwardDecl());
2423 ToProto->setLexicalDeclContext(LexicalDC);
2424 LexicalDC->addDecl(ToProto);
2425 }
2426 Importer.Imported(D, ToProto);
2427
2428 // Import protocols
2429 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2430 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2431 ObjCProtocolDecl::protocol_loc_iterator
2432 FromProtoLoc = D->protocol_loc_begin();
2433 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2434 FromProtoEnd = D->protocol_end();
2435 FromProto != FromProtoEnd;
2436 ++FromProto, ++FromProtoLoc) {
2437 ObjCProtocolDecl *ToProto
2438 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2439 if (!ToProto)
2440 return 0;
2441 Protocols.push_back(ToProto);
2442 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2443 }
2444
2445 // FIXME: If we're merging, make sure that the protocol list is the same.
2446 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2447 ProtocolLocs.data(), Importer.getToContext());
2448 } else {
2449 Importer.Imported(D, ToProto);
2450 }
2451
Douglas Gregorb4677b62010-02-18 01:47:50 +00002452 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002453 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002454
2455 return ToProto;
2456}
2457
Douglas Gregora12d2942010-02-16 01:20:57 +00002458Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2459 // Import the major distinguishing characteristics of an @interface.
2460 DeclContext *DC, *LexicalDC;
2461 DeclarationName Name;
2462 SourceLocation Loc;
2463 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2464 return 0;
2465
2466 ObjCInterfaceDecl *MergeWithIface = 0;
2467 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2468 Lookup.first != Lookup.second;
2469 ++Lookup.first) {
2470 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2471 continue;
2472
2473 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2474 break;
2475 }
2476
2477 ObjCInterfaceDecl *ToIface = MergeWithIface;
2478 if (!ToIface || ToIface->isForwardDecl()) {
2479 if (!ToIface) {
2480 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2481 DC, Loc,
2482 Name.getAsIdentifierInfo(),
2483 Importer.Import(D->getClassLoc()),
2484 D->isForwardDecl(),
2485 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002486 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002487 ToIface->setLexicalDeclContext(LexicalDC);
2488 LexicalDC->addDecl(ToIface);
2489 }
2490 Importer.Imported(D, ToIface);
2491
Douglas Gregora12d2942010-02-16 01:20:57 +00002492 if (D->getSuperClass()) {
2493 ObjCInterfaceDecl *Super
2494 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2495 if (!Super)
2496 return 0;
2497
2498 ToIface->setSuperClass(Super);
2499 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2500 }
2501
2502 // Import protocols
2503 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2504 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2505 ObjCInterfaceDecl::protocol_loc_iterator
2506 FromProtoLoc = D->protocol_loc_begin();
2507 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2508 FromProtoEnd = D->protocol_end();
2509 FromProto != FromProtoEnd;
2510 ++FromProto, ++FromProtoLoc) {
2511 ObjCProtocolDecl *ToProto
2512 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2513 if (!ToProto)
2514 return 0;
2515 Protocols.push_back(ToProto);
2516 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2517 }
2518
2519 // FIXME: If we're merging, make sure that the protocol list is the same.
2520 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2521 ProtocolLocs.data(), Importer.getToContext());
2522
Douglas Gregora12d2942010-02-16 01:20:57 +00002523 // Import @end range
2524 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2525 } else {
2526 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002527
2528 // Check for consistency of superclasses.
2529 DeclarationName FromSuperName, ToSuperName;
2530 if (D->getSuperClass())
2531 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2532 if (ToIface->getSuperClass())
2533 ToSuperName = ToIface->getSuperClass()->getDeclName();
2534 if (FromSuperName != ToSuperName) {
2535 Importer.ToDiag(ToIface->getLocation(),
2536 diag::err_odr_objc_superclass_inconsistent)
2537 << ToIface->getDeclName();
2538 if (ToIface->getSuperClass())
2539 Importer.ToDiag(ToIface->getSuperClassLoc(),
2540 diag::note_odr_objc_superclass)
2541 << ToIface->getSuperClass()->getDeclName();
2542 else
2543 Importer.ToDiag(ToIface->getLocation(),
2544 diag::note_odr_objc_missing_superclass);
2545 if (D->getSuperClass())
2546 Importer.FromDiag(D->getSuperClassLoc(),
2547 diag::note_odr_objc_superclass)
2548 << D->getSuperClass()->getDeclName();
2549 else
2550 Importer.FromDiag(D->getLocation(),
2551 diag::note_odr_objc_missing_superclass);
2552 return 0;
2553 }
Douglas Gregora12d2942010-02-16 01:20:57 +00002554 }
2555
Douglas Gregorb4677b62010-02-18 01:47:50 +00002556 // Import categories. When the categories themselves are imported, they'll
2557 // hook themselves into this interface.
2558 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2559 FromCat = FromCat->getNextClassCategory())
2560 Importer.Import(FromCat);
2561
Douglas Gregora12d2942010-02-16 01:20:57 +00002562 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00002563 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00002564
2565 // If we have an @implementation, import it as well.
2566 if (D->getImplementation()) {
2567 ObjCImplementationDecl *Impl
2568 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2569 if (!Impl)
2570 return 0;
2571
2572 ToIface->setImplementation(Impl);
2573 }
2574
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002575 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00002576}
2577
Douglas Gregore3261622010-02-17 18:02:10 +00002578Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2579 // Import the major distinguishing characteristics of an @property.
2580 DeclContext *DC, *LexicalDC;
2581 DeclarationName Name;
2582 SourceLocation Loc;
2583 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2584 return 0;
2585
2586 // Check whether we have already imported this property.
2587 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2588 Lookup.first != Lookup.second;
2589 ++Lookup.first) {
2590 if (ObjCPropertyDecl *FoundProp
2591 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2592 // Check property types.
2593 if (!Importer.IsStructurallyEquivalent(D->getType(),
2594 FoundProp->getType())) {
2595 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2596 << Name << D->getType() << FoundProp->getType();
2597 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2598 << FoundProp->getType();
2599 return 0;
2600 }
2601
2602 // FIXME: Check property attributes, getters, setters, etc.?
2603
2604 // Consider these properties to be equivalent.
2605 Importer.Imported(D, FoundProp);
2606 return FoundProp;
2607 }
2608 }
2609
2610 // Import the type.
2611 QualType T = Importer.Import(D->getType());
2612 if (T.isNull())
2613 return 0;
2614
2615 // Create the new property.
2616 ObjCPropertyDecl *ToProperty
2617 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2618 Name.getAsIdentifierInfo(),
2619 Importer.Import(D->getAtLoc()),
2620 T,
2621 D->getPropertyImplementation());
2622 Importer.Imported(D, ToProperty);
2623 ToProperty->setLexicalDeclContext(LexicalDC);
2624 LexicalDC->addDecl(ToProperty);
2625
2626 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2627 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2628 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2629 ToProperty->setGetterMethodDecl(
2630 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2631 ToProperty->setSetterMethodDecl(
2632 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2633 ToProperty->setPropertyIvarDecl(
2634 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2635 return ToProperty;
2636}
2637
Douglas Gregor2b785022010-02-18 02:12:22 +00002638Decl *
2639ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2640 // Import the context of this declaration.
2641 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2642 if (!DC)
2643 return 0;
2644
2645 DeclContext *LexicalDC = DC;
2646 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2647 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2648 if (!LexicalDC)
2649 return 0;
2650 }
2651
2652 // Import the location of this declaration.
2653 SourceLocation Loc = Importer.Import(D->getLocation());
2654
2655 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2656 llvm::SmallVector<SourceLocation, 4> Locations;
2657 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2658 = D->protocol_loc_begin();
2659 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2660 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2661 FromProto != FromProtoEnd;
2662 ++FromProto, ++FromProtoLoc) {
2663 ObjCProtocolDecl *ToProto
2664 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2665 if (!ToProto)
2666 continue;
2667
2668 Protocols.push_back(ToProto);
2669 Locations.push_back(Importer.Import(*FromProtoLoc));
2670 }
2671
2672 ObjCForwardProtocolDecl *ToForward
2673 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2674 Protocols.data(), Protocols.size(),
2675 Locations.data());
2676 ToForward->setLexicalDeclContext(LexicalDC);
2677 LexicalDC->addDecl(ToForward);
2678 Importer.Imported(D, ToForward);
2679 return ToForward;
2680}
2681
Douglas Gregora2bc15b2010-02-18 02:04:09 +00002682Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2683 // Import the context of this declaration.
2684 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2685 if (!DC)
2686 return 0;
2687
2688 DeclContext *LexicalDC = DC;
2689 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2690 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2691 if (!LexicalDC)
2692 return 0;
2693 }
2694
2695 // Import the location of this declaration.
2696 SourceLocation Loc = Importer.Import(D->getLocation());
2697
2698 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2699 llvm::SmallVector<SourceLocation, 4> Locations;
2700 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2701 From != FromEnd; ++From) {
2702 ObjCInterfaceDecl *ToIface
2703 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2704 if (!ToIface)
2705 continue;
2706
2707 Interfaces.push_back(ToIface);
2708 Locations.push_back(Importer.Import(From->getLocation()));
2709 }
2710
2711 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2712 Loc,
2713 Interfaces.data(),
2714 Locations.data(),
2715 Interfaces.size());
2716 ToClass->setLexicalDeclContext(LexicalDC);
2717 LexicalDC->addDecl(ToClass);
2718 Importer.Imported(D, ToClass);
2719 return ToClass;
2720}
2721
Douglas Gregor4800d952010-02-11 19:21:55 +00002722//----------------------------------------------------------------------------
2723// Import Statements
2724//----------------------------------------------------------------------------
2725
2726Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2727 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2728 << S->getStmtClassName();
2729 return 0;
2730}
2731
2732//----------------------------------------------------------------------------
2733// Import Expressions
2734//----------------------------------------------------------------------------
2735Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2736 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2737 << E->getStmtClassName();
2738 return 0;
2739}
2740
Douglas Gregor44080632010-02-19 01:17:02 +00002741Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2742 NestedNameSpecifier *Qualifier = 0;
2743 if (E->getQualifier()) {
2744 Qualifier = Importer.Import(E->getQualifier());
2745 if (!E->getQualifier())
2746 return 0;
2747 }
2748
2749 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2750 if (!ToD)
2751 return 0;
2752
2753 QualType T = Importer.Import(E->getType());
2754 if (T.isNull())
2755 return 0;
2756
2757 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2758 Importer.Import(E->getQualifierRange()),
2759 ToD,
2760 Importer.Import(E->getLocation()),
2761 T,
2762 /*FIXME:TemplateArgs=*/0);
2763}
2764
Douglas Gregor4800d952010-02-11 19:21:55 +00002765Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2766 QualType T = Importer.Import(E->getType());
2767 if (T.isNull())
2768 return 0;
2769
2770 return new (Importer.getToContext())
2771 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2772}
2773
Douglas Gregorb2e400a2010-02-18 02:21:22 +00002774Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2775 QualType T = Importer.Import(E->getType());
2776 if (T.isNull())
2777 return 0;
2778
2779 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2780 E->isWide(), T,
2781 Importer.Import(E->getLocation()));
2782}
2783
Douglas Gregorf638f952010-02-19 01:07:06 +00002784Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2785 Expr *SubExpr = Importer.Import(E->getSubExpr());
2786 if (!SubExpr)
2787 return 0;
2788
2789 return new (Importer.getToContext())
2790 ParenExpr(Importer.Import(E->getLParen()),
2791 Importer.Import(E->getRParen()),
2792 SubExpr);
2793}
2794
2795Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2796 QualType T = Importer.Import(E->getType());
2797 if (T.isNull())
2798 return 0;
2799
2800 Expr *SubExpr = Importer.Import(E->getSubExpr());
2801 if (!SubExpr)
2802 return 0;
2803
2804 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2805 T,
2806 Importer.Import(E->getOperatorLoc()));
2807}
2808
Douglas Gregorbd249a52010-02-19 01:24:23 +00002809Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2810 QualType ResultType = Importer.Import(E->getType());
2811
2812 if (E->isArgumentType()) {
2813 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2814 if (!TInfo)
2815 return 0;
2816
2817 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2818 TInfo, ResultType,
2819 Importer.Import(E->getOperatorLoc()),
2820 Importer.Import(E->getRParenLoc()));
2821 }
2822
2823 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2824 if (!SubExpr)
2825 return 0;
2826
2827 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2828 SubExpr, ResultType,
2829 Importer.Import(E->getOperatorLoc()),
2830 Importer.Import(E->getRParenLoc()));
2831}
2832
Douglas Gregorf638f952010-02-19 01:07:06 +00002833Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2834 QualType T = Importer.Import(E->getType());
2835 if (T.isNull())
2836 return 0;
2837
2838 Expr *LHS = Importer.Import(E->getLHS());
2839 if (!LHS)
2840 return 0;
2841
2842 Expr *RHS = Importer.Import(E->getRHS());
2843 if (!RHS)
2844 return 0;
2845
2846 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2847 T,
2848 Importer.Import(E->getOperatorLoc()));
2849}
2850
2851Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2852 QualType T = Importer.Import(E->getType());
2853 if (T.isNull())
2854 return 0;
2855
2856 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2857 if (CompLHSType.isNull())
2858 return 0;
2859
2860 QualType CompResultType = Importer.Import(E->getComputationResultType());
2861 if (CompResultType.isNull())
2862 return 0;
2863
2864 Expr *LHS = Importer.Import(E->getLHS());
2865 if (!LHS)
2866 return 0;
2867
2868 Expr *RHS = Importer.Import(E->getRHS());
2869 if (!RHS)
2870 return 0;
2871
2872 return new (Importer.getToContext())
2873 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2874 T, CompLHSType, CompResultType,
2875 Importer.Import(E->getOperatorLoc()));
2876}
2877
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002878Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2879 QualType T = Importer.Import(E->getType());
2880 if (T.isNull())
2881 return 0;
2882
2883 Expr *SubExpr = Importer.Import(E->getSubExpr());
2884 if (!SubExpr)
2885 return 0;
2886
2887 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2888 SubExpr,
2889 E->isLvalueCast());
2890}
2891
Douglas Gregor008847a2010-02-19 01:32:14 +00002892Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2893 QualType T = Importer.Import(E->getType());
2894 if (T.isNull())
2895 return 0;
2896
2897 Expr *SubExpr = Importer.Import(E->getSubExpr());
2898 if (!SubExpr)
2899 return 0;
2900
2901 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2902 if (!TInfo && E->getTypeInfoAsWritten())
2903 return 0;
2904
2905 return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
2906 SubExpr, TInfo,
2907 Importer.Import(E->getLParenLoc()),
2908 Importer.Import(E->getRParenLoc()));
2909}
2910
Douglas Gregor4800d952010-02-11 19:21:55 +00002911ASTImporter::ASTImporter(Diagnostic &Diags,
2912 ASTContext &ToContext, FileManager &ToFileManager,
2913 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002914 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +00002915 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor4800d952010-02-11 19:21:55 +00002916 Diags(Diags) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00002917 ImportedDecls[FromContext.getTranslationUnitDecl()]
2918 = ToContext.getTranslationUnitDecl();
2919}
2920
2921ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002922
2923QualType ASTImporter::Import(QualType FromT) {
2924 if (FromT.isNull())
2925 return QualType();
2926
Douglas Gregor169fba52010-02-08 15:18:58 +00002927 // Check whether we've already imported this type.
2928 llvm::DenseMap<Type *, Type *>::iterator Pos
2929 = ImportedTypes.find(FromT.getTypePtr());
2930 if (Pos != ImportedTypes.end())
2931 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002932
Douglas Gregor169fba52010-02-08 15:18:58 +00002933 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002934 ASTNodeImporter Importer(*this);
2935 QualType ToT = Importer.Visit(FromT.getTypePtr());
2936 if (ToT.isNull())
2937 return ToT;
2938
Douglas Gregor169fba52010-02-08 15:18:58 +00002939 // Record the imported type.
2940 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2941
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002942 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2943}
2944
Douglas Gregor9bed8792010-02-09 19:21:46 +00002945TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002946 if (!FromTSI)
2947 return FromTSI;
2948
2949 // FIXME: For now we just create a "trivial" type source info based
2950 // on the type and a seingle location. Implement a real version of
2951 // this.
2952 QualType T = Import(FromTSI->getType());
2953 if (T.isNull())
2954 return 0;
2955
2956 return ToContext.getTrivialTypeSourceInfo(T,
2957 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002958}
2959
2960Decl *ASTImporter::Import(Decl *FromD) {
2961 if (!FromD)
2962 return 0;
2963
2964 // Check whether we've already imported this declaration.
2965 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2966 if (Pos != ImportedDecls.end())
2967 return Pos->second;
2968
2969 // Import the type
2970 ASTNodeImporter Importer(*this);
2971 Decl *ToD = Importer.Visit(FromD);
2972 if (!ToD)
2973 return 0;
2974
2975 // Record the imported declaration.
2976 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00002977
2978 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2979 // Keep track of anonymous tags that have an associated typedef.
2980 if (FromTag->getTypedefForAnonDecl())
2981 AnonTagsWithPendingTypedefs.push_back(FromTag);
2982 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2983 // When we've finished transforming a typedef, see whether it was the
2984 // typedef for an anonymous tag.
2985 for (llvm::SmallVector<TagDecl *, 4>::iterator
2986 FromTag = AnonTagsWithPendingTypedefs.begin(),
2987 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2988 FromTag != FromTagEnd; ++FromTag) {
2989 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2990 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2991 // We found the typedef for an anonymous tag; link them.
2992 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2993 AnonTagsWithPendingTypedefs.erase(FromTag);
2994 break;
2995 }
2996 }
2997 }
2998 }
2999
Douglas Gregor9bed8792010-02-09 19:21:46 +00003000 return ToD;
3001}
3002
3003DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3004 if (!FromDC)
3005 return FromDC;
3006
3007 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3008}
3009
3010Expr *ASTImporter::Import(Expr *FromE) {
3011 if (!FromE)
3012 return 0;
3013
3014 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3015}
3016
3017Stmt *ASTImporter::Import(Stmt *FromS) {
3018 if (!FromS)
3019 return 0;
3020
Douglas Gregor4800d952010-02-11 19:21:55 +00003021 // Check whether we've already imported this declaration.
3022 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3023 if (Pos != ImportedStmts.end())
3024 return Pos->second;
3025
3026 // Import the type
3027 ASTNodeImporter Importer(*this);
3028 Stmt *ToS = Importer.Visit(FromS);
3029 if (!ToS)
3030 return 0;
3031
3032 // Record the imported declaration.
3033 ImportedStmts[FromS] = ToS;
3034 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003035}
3036
3037NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3038 if (!FromNNS)
3039 return 0;
3040
3041 // FIXME: Implement!
3042 return 0;
3043}
3044
3045SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3046 if (FromLoc.isInvalid())
3047 return SourceLocation();
3048
Douglas Gregor88523732010-02-10 00:15:17 +00003049 SourceManager &FromSM = FromContext.getSourceManager();
3050
3051 // For now, map everything down to its spelling location, so that we
3052 // don't have to import macro instantiations.
3053 // FIXME: Import macro instantiations!
3054 FromLoc = FromSM.getSpellingLoc(FromLoc);
3055 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3056 SourceManager &ToSM = ToContext.getSourceManager();
3057 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3058 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00003059}
3060
3061SourceRange ASTImporter::Import(SourceRange FromRange) {
3062 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3063}
3064
Douglas Gregor88523732010-02-10 00:15:17 +00003065FileID ASTImporter::Import(FileID FromID) {
3066 llvm::DenseMap<unsigned, FileID>::iterator Pos
3067 = ImportedFileIDs.find(FromID.getHashValue());
3068 if (Pos != ImportedFileIDs.end())
3069 return Pos->second;
3070
3071 SourceManager &FromSM = FromContext.getSourceManager();
3072 SourceManager &ToSM = ToContext.getSourceManager();
3073 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3074 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3075
3076 // Include location of this file.
3077 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3078
3079 // Map the FileID for to the "to" source manager.
3080 FileID ToID;
3081 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3082 if (Cache->Entry) {
3083 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3084 // disk again
3085 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3086 // than mmap the files several times.
3087 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3088 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3089 FromSLoc.getFile().getFileCharacteristic());
3090 } else {
3091 // FIXME: We want to re-use the existing MemoryBuffer!
Douglas Gregor36c35ba2010-03-16 00:35:39 +00003092 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags());
Douglas Gregor88523732010-02-10 00:15:17 +00003093 llvm::MemoryBuffer *ToBuf
3094 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
3095 FromBuf->getBufferEnd(),
3096 FromBuf->getBufferIdentifier());
3097 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3098 }
3099
3100
3101 ImportedFileIDs[FromID.getHashValue()] = ToID;
3102 return ToID;
3103}
3104
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003105DeclarationName ASTImporter::Import(DeclarationName FromName) {
3106 if (!FromName)
3107 return DeclarationName();
3108
3109 switch (FromName.getNameKind()) {
3110 case DeclarationName::Identifier:
3111 return Import(FromName.getAsIdentifierInfo());
3112
3113 case DeclarationName::ObjCZeroArgSelector:
3114 case DeclarationName::ObjCOneArgSelector:
3115 case DeclarationName::ObjCMultiArgSelector:
3116 return Import(FromName.getObjCSelector());
3117
3118 case DeclarationName::CXXConstructorName: {
3119 QualType T = Import(FromName.getCXXNameType());
3120 if (T.isNull())
3121 return DeclarationName();
3122
3123 return ToContext.DeclarationNames.getCXXConstructorName(
3124 ToContext.getCanonicalType(T));
3125 }
3126
3127 case DeclarationName::CXXDestructorName: {
3128 QualType T = Import(FromName.getCXXNameType());
3129 if (T.isNull())
3130 return DeclarationName();
3131
3132 return ToContext.DeclarationNames.getCXXDestructorName(
3133 ToContext.getCanonicalType(T));
3134 }
3135
3136 case DeclarationName::CXXConversionFunctionName: {
3137 QualType T = Import(FromName.getCXXNameType());
3138 if (T.isNull())
3139 return DeclarationName();
3140
3141 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3142 ToContext.getCanonicalType(T));
3143 }
3144
3145 case DeclarationName::CXXOperatorName:
3146 return ToContext.DeclarationNames.getCXXOperatorName(
3147 FromName.getCXXOverloadedOperator());
3148
3149 case DeclarationName::CXXLiteralOperatorName:
3150 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3151 Import(FromName.getCXXLiteralIdentifier()));
3152
3153 case DeclarationName::CXXUsingDirective:
3154 // FIXME: STATICS!
3155 return DeclarationName::getUsingDirectiveName();
3156 }
3157
3158 // Silence bogus GCC warning
3159 return DeclarationName();
3160}
3161
3162IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3163 if (!FromId)
3164 return 0;
3165
3166 return &ToContext.Idents.get(FromId->getName());
3167}
Douglas Gregor089459a2010-02-08 21:09:39 +00003168
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003169Selector ASTImporter::Import(Selector FromSel) {
3170 if (FromSel.isNull())
3171 return Selector();
3172
3173 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3174 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3175 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3176 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3177 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3178}
3179
Douglas Gregor089459a2010-02-08 21:09:39 +00003180DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3181 DeclContext *DC,
3182 unsigned IDNS,
3183 NamedDecl **Decls,
3184 unsigned NumDecls) {
3185 return Name;
3186}
3187
3188DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003189 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
3190 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003191}
3192
3193DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003194 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
3195 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003196}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003197
3198Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3199 ImportedDecls[From] = To;
3200 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00003201}
Douglas Gregorea35d112010-02-15 23:54:17 +00003202
3203bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3204 llvm::DenseMap<Type *, Type *>::iterator Pos
3205 = ImportedTypes.find(From.getTypePtr());
3206 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3207 return true;
3208
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003209 StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +00003210 NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003211 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00003212}