blob: 9d756950fa97a3ef5a9c4e245a11c735868a228c [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);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
73 // FIXME: TemplateSpecializationType
Abramo Bagnara465d41b2010-05-11 21:36:43 +000074 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
Douglas Gregor1b2949d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCallc12c5bb2010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000078 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;
Rafael Espindola264ba482010-03-30 20:24:48 +0000487 if (Function1->getExtInfo() != Function2->getExtInfo())
488 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000489 break;
490 }
491
492 case Type::UnresolvedUsing:
493 if (!IsStructurallyEquivalent(Context,
494 cast<UnresolvedUsingType>(T1)->getDecl(),
495 cast<UnresolvedUsingType>(T2)->getDecl()))
496 return false;
497
498 break;
499
500 case Type::Typedef:
501 if (!IsStructurallyEquivalent(Context,
502 cast<TypedefType>(T1)->getDecl(),
503 cast<TypedefType>(T2)->getDecl()))
504 return false;
505 break;
506
507 case Type::TypeOfExpr:
508 if (!IsStructurallyEquivalent(Context,
509 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
510 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
511 return false;
512 break;
513
514 case Type::TypeOf:
515 if (!IsStructurallyEquivalent(Context,
516 cast<TypeOfType>(T1)->getUnderlyingType(),
517 cast<TypeOfType>(T2)->getUnderlyingType()))
518 return false;
519 break;
520
521 case Type::Decltype:
522 if (!IsStructurallyEquivalent(Context,
523 cast<DecltypeType>(T1)->getUnderlyingExpr(),
524 cast<DecltypeType>(T2)->getUnderlyingExpr()))
525 return false;
526 break;
527
528 case Type::Record:
529 case Type::Enum:
530 if (!IsStructurallyEquivalent(Context,
531 cast<TagType>(T1)->getDecl(),
532 cast<TagType>(T2)->getDecl()))
533 return false;
534 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000535
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000536 case Type::TemplateTypeParm: {
537 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
538 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
539 if (Parm1->getDepth() != Parm2->getDepth())
540 return false;
541 if (Parm1->getIndex() != Parm2->getIndex())
542 return false;
543 if (Parm1->isParameterPack() != Parm2->isParameterPack())
544 return false;
545
546 // Names of template type parameters are never significant.
547 break;
548 }
549
550 case Type::SubstTemplateTypeParm: {
551 const SubstTemplateTypeParmType *Subst1
552 = cast<SubstTemplateTypeParmType>(T1);
553 const SubstTemplateTypeParmType *Subst2
554 = cast<SubstTemplateTypeParmType>(T2);
555 if (!IsStructurallyEquivalent(Context,
556 QualType(Subst1->getReplacedParameter(), 0),
557 QualType(Subst2->getReplacedParameter(), 0)))
558 return false;
559 if (!IsStructurallyEquivalent(Context,
560 Subst1->getReplacementType(),
561 Subst2->getReplacementType()))
562 return false;
563 break;
564 }
565
566 case Type::TemplateSpecialization: {
567 const TemplateSpecializationType *Spec1
568 = cast<TemplateSpecializationType>(T1);
569 const TemplateSpecializationType *Spec2
570 = cast<TemplateSpecializationType>(T2);
571 if (!IsStructurallyEquivalent(Context,
572 Spec1->getTemplateName(),
573 Spec2->getTemplateName()))
574 return false;
575 if (Spec1->getNumArgs() != Spec2->getNumArgs())
576 return false;
577 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
578 if (!IsStructurallyEquivalent(Context,
579 Spec1->getArg(I), Spec2->getArg(I)))
580 return false;
581 }
582 break;
583 }
584
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000585 case Type::Elaborated: {
586 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
587 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
588 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
589 if (Elab1->getKeyword() != Elab2->getKeyword())
590 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000591 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000592 Elab1->getQualifier(),
593 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000594 return false;
595 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000596 Elab1->getNamedType(),
597 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000598 return false;
599 break;
600 }
601
John McCall3cb0ebd2010-03-10 03:28:59 +0000602 case Type::InjectedClassName: {
603 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
604 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
605 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000606 Inj1->getInjectedSpecializationType(),
607 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000608 return false;
609 break;
610 }
611
Douglas Gregor4714c122010-03-31 17:34:00 +0000612 case Type::DependentName: {
613 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
614 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000615 if (!IsStructurallyEquivalent(Context,
616 Typename1->getQualifier(),
617 Typename2->getQualifier()))
618 return false;
619 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
620 Typename2->getIdentifier()))
621 return false;
622 if (!IsStructurallyEquivalent(Context,
623 QualType(Typename1->getTemplateId(), 0),
624 QualType(Typename2->getTemplateId(), 0)))
625 return false;
626
627 break;
628 }
629
630 case Type::ObjCInterface: {
631 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
632 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
633 if (!IsStructurallyEquivalent(Context,
634 Iface1->getDecl(), Iface2->getDecl()))
635 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000636 break;
637 }
638
639 case Type::ObjCObject: {
640 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
641 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
642 if (!IsStructurallyEquivalent(Context,
643 Obj1->getBaseType(),
644 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000645 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000646 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
647 return false;
648 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000649 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000650 Obj1->getProtocol(I),
651 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000652 return false;
653 }
654 break;
655 }
656
657 case Type::ObjCObjectPointer: {
658 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
659 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
660 if (!IsStructurallyEquivalent(Context,
661 Ptr1->getPointeeType(),
662 Ptr2->getPointeeType()))
663 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000664 break;
665 }
666
667 } // end switch
668
669 return true;
670}
671
672/// \brief Determine structural equivalence of two records.
673static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
674 RecordDecl *D1, RecordDecl *D2) {
675 if (D1->isUnion() != D2->isUnion()) {
676 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
677 << Context.C2.getTypeDeclType(D2);
678 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
679 << D1->getDeclName() << (unsigned)D1->getTagKind();
680 return false;
681 }
682
Douglas Gregorea35d112010-02-15 23:54:17 +0000683 // Compare the definitions of these two records. If either or both are
684 // incomplete, we assume that they are equivalent.
685 D1 = D1->getDefinition();
686 D2 = D2->getDefinition();
687 if (!D1 || !D2)
688 return true;
689
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000690 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
691 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
692 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
693 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
694 << Context.C2.getTypeDeclType(D2);
695 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
696 << D2CXX->getNumBases();
697 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
698 << D1CXX->getNumBases();
699 return false;
700 }
701
702 // Check the base classes.
703 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
704 BaseEnd1 = D1CXX->bases_end(),
705 Base2 = D2CXX->bases_begin();
706 Base1 != BaseEnd1;
707 ++Base1, ++Base2) {
708 if (!IsStructurallyEquivalent(Context,
709 Base1->getType(), Base2->getType())) {
710 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
711 << Context.C2.getTypeDeclType(D2);
712 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
713 << Base2->getType()
714 << Base2->getSourceRange();
715 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
716 << Base1->getType()
717 << Base1->getSourceRange();
718 return false;
719 }
720
721 // Check virtual vs. non-virtual inheritance mismatch.
722 if (Base1->isVirtual() != Base2->isVirtual()) {
723 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
724 << Context.C2.getTypeDeclType(D2);
725 Context.Diag2(Base2->getSourceRange().getBegin(),
726 diag::note_odr_virtual_base)
727 << Base2->isVirtual() << Base2->getSourceRange();
728 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
729 << Base1->isVirtual()
730 << Base1->getSourceRange();
731 return false;
732 }
733 }
734 } else if (D1CXX->getNumBases() > 0) {
735 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
736 << Context.C2.getTypeDeclType(D2);
737 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
738 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
739 << Base1->getType()
740 << Base1->getSourceRange();
741 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
742 return false;
743 }
744 }
745
746 // Check the fields for consistency.
747 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
748 Field2End = D2->field_end();
749 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
750 Field1End = D1->field_end();
751 Field1 != Field1End;
752 ++Field1, ++Field2) {
753 if (Field2 == Field2End) {
754 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
755 << Context.C2.getTypeDeclType(D2);
756 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
757 << Field1->getDeclName() << Field1->getType();
758 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
759 return false;
760 }
761
762 if (!IsStructurallyEquivalent(Context,
763 Field1->getType(), Field2->getType())) {
764 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
765 << Context.C2.getTypeDeclType(D2);
766 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
767 << Field2->getDeclName() << Field2->getType();
768 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
769 << Field1->getDeclName() << Field1->getType();
770 return false;
771 }
772
773 if (Field1->isBitField() != Field2->isBitField()) {
774 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
775 << Context.C2.getTypeDeclType(D2);
776 if (Field1->isBitField()) {
777 llvm::APSInt Bits;
778 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
779 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
780 << Field1->getDeclName() << Field1->getType()
781 << Bits.toString(10, false);
782 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
783 << Field2->getDeclName();
784 } else {
785 llvm::APSInt Bits;
786 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
787 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
788 << Field2->getDeclName() << Field2->getType()
789 << Bits.toString(10, false);
790 Context.Diag1(Field1->getLocation(),
791 diag::note_odr_not_bit_field)
792 << Field1->getDeclName();
793 }
794 return false;
795 }
796
797 if (Field1->isBitField()) {
798 // Make sure that the bit-fields are the same length.
799 llvm::APSInt Bits1, Bits2;
800 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
801 return false;
802 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
803 return false;
804
805 if (!IsSameValue(Bits1, Bits2)) {
806 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
807 << Context.C2.getTypeDeclType(D2);
808 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
809 << Field2->getDeclName() << Field2->getType()
810 << Bits2.toString(10, false);
811 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
812 << Field1->getDeclName() << Field1->getType()
813 << Bits1.toString(10, false);
814 return false;
815 }
816 }
817 }
818
819 if (Field2 != Field2End) {
820 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
821 << Context.C2.getTypeDeclType(D2);
822 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
823 << Field2->getDeclName() << Field2->getType();
824 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
825 return false;
826 }
827
828 return true;
829}
830
831/// \brief Determine structural equivalence of two enums.
832static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
833 EnumDecl *D1, EnumDecl *D2) {
834 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
835 EC2End = D2->enumerator_end();
836 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
837 EC1End = D1->enumerator_end();
838 EC1 != EC1End; ++EC1, ++EC2) {
839 if (EC2 == EC2End) {
840 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
841 << Context.C2.getTypeDeclType(D2);
842 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
843 << EC1->getDeclName()
844 << EC1->getInitVal().toString(10);
845 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
846 return false;
847 }
848
849 llvm::APSInt Val1 = EC1->getInitVal();
850 llvm::APSInt Val2 = EC2->getInitVal();
851 if (!IsSameValue(Val1, Val2) ||
852 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
853 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
854 << Context.C2.getTypeDeclType(D2);
855 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
856 << EC2->getDeclName()
857 << EC2->getInitVal().toString(10);
858 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
859 << EC1->getDeclName()
860 << EC1->getInitVal().toString(10);
861 return false;
862 }
863 }
864
865 if (EC2 != EC2End) {
866 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
867 << Context.C2.getTypeDeclType(D2);
868 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
869 << EC2->getDeclName()
870 << EC2->getInitVal().toString(10);
871 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
872 return false;
873 }
874
875 return true;
876}
877
878/// \brief Determine structural equivalence of two declarations.
879static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
880 Decl *D1, Decl *D2) {
881 // FIXME: Check for known structural equivalences via a callback of some sort.
882
Douglas Gregorea35d112010-02-15 23:54:17 +0000883 // Check whether we already know that these two declarations are not
884 // structurally equivalent.
885 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
886 D2->getCanonicalDecl())))
887 return false;
888
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000889 // Determine whether we've already produced a tentative equivalence for D1.
890 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
891 if (EquivToD1)
892 return EquivToD1 == D2->getCanonicalDecl();
893
894 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
895 EquivToD1 = D2->getCanonicalDecl();
896 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
897 return true;
898}
899
900bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
901 Decl *D2) {
902 if (!::IsStructurallyEquivalent(*this, D1, D2))
903 return false;
904
905 return !Finish();
906}
907
908bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
909 QualType T2) {
910 if (!::IsStructurallyEquivalent(*this, T1, T2))
911 return false;
912
913 return !Finish();
914}
915
916bool StructuralEquivalenceContext::Finish() {
917 while (!DeclsToCheck.empty()) {
918 // Check the next declaration.
919 Decl *D1 = DeclsToCheck.front();
920 DeclsToCheck.pop_front();
921
922 Decl *D2 = TentativeEquivalences[D1];
923 assert(D2 && "Unrecorded tentative equivalence?");
924
Douglas Gregorea35d112010-02-15 23:54:17 +0000925 bool Equivalent = true;
926
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000927 // FIXME: Switch on all declaration kinds. For now, we're just going to
928 // check the obvious ones.
929 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
930 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
931 // Check for equivalent structure names.
932 IdentifierInfo *Name1 = Record1->getIdentifier();
933 if (!Name1 && Record1->getTypedefForAnonDecl())
934 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
935 IdentifierInfo *Name2 = Record2->getIdentifier();
936 if (!Name2 && Record2->getTypedefForAnonDecl())
937 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000938 if (!::IsStructurallyEquivalent(Name1, Name2) ||
939 !::IsStructurallyEquivalent(*this, Record1, Record2))
940 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000941 } else {
942 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000943 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000944 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000945 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000946 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
947 // Check for equivalent enum names.
948 IdentifierInfo *Name1 = Enum1->getIdentifier();
949 if (!Name1 && Enum1->getTypedefForAnonDecl())
950 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
951 IdentifierInfo *Name2 = Enum2->getIdentifier();
952 if (!Name2 && Enum2->getTypedefForAnonDecl())
953 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000954 if (!::IsStructurallyEquivalent(Name1, Name2) ||
955 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
956 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000957 } else {
958 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +0000959 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000960 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000961 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000962 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
963 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +0000964 Typedef2->getIdentifier()) ||
965 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000966 Typedef1->getUnderlyingType(),
967 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +0000968 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000969 } else {
970 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000971 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000972 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000973 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000974
975 if (!Equivalent) {
976 // Note that these two declarations are not equivalent (and we already
977 // know about it).
978 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
979 D2->getCanonicalDecl()));
980 return true;
981 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000982 // FIXME: Check other declaration kinds!
983 }
984
985 return false;
986}
987
988//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000989// Import Types
990//----------------------------------------------------------------------------
991
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000992QualType ASTNodeImporter::VisitType(Type *T) {
993 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
994 << T->getTypeClassName();
995 return QualType();
996}
997
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000998QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
999 switch (T->getKind()) {
1000 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1001 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1002
1003 case BuiltinType::Char_U:
1004 // The context we're importing from has an unsigned 'char'. If we're
1005 // importing into a context with a signed 'char', translate to
1006 // 'unsigned char' instead.
1007 if (Importer.getToContext().getLangOptions().CharIsSigned)
1008 return Importer.getToContext().UnsignedCharTy;
1009
1010 return Importer.getToContext().CharTy;
1011
1012 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1013
1014 case BuiltinType::Char16:
1015 // FIXME: Make sure that the "to" context supports C++!
1016 return Importer.getToContext().Char16Ty;
1017
1018 case BuiltinType::Char32:
1019 // FIXME: Make sure that the "to" context supports C++!
1020 return Importer.getToContext().Char32Ty;
1021
1022 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1023 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1024 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1025 case BuiltinType::ULongLong:
1026 return Importer.getToContext().UnsignedLongLongTy;
1027 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1028
1029 case BuiltinType::Char_S:
1030 // The context we're importing from has an unsigned 'char'. If we're
1031 // importing into a context with a signed 'char', translate to
1032 // 'unsigned char' instead.
1033 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1034 return Importer.getToContext().SignedCharTy;
1035
1036 return Importer.getToContext().CharTy;
1037
1038 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1039 case BuiltinType::WChar:
1040 // FIXME: If not in C++, shall we translate to the C equivalent of
1041 // wchar_t?
1042 return Importer.getToContext().WCharTy;
1043
1044 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1045 case BuiltinType::Int : return Importer.getToContext().IntTy;
1046 case BuiltinType::Long : return Importer.getToContext().LongTy;
1047 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1048 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1049 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1050 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1051 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1052
1053 case BuiltinType::NullPtr:
1054 // FIXME: Make sure that the "to" context supports C++0x!
1055 return Importer.getToContext().NullPtrTy;
1056
1057 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1058 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1059 case BuiltinType::UndeducedAuto:
1060 // FIXME: Make sure that the "to" context supports C++0x!
1061 return Importer.getToContext().UndeducedAutoTy;
1062
1063 case BuiltinType::ObjCId:
1064 // FIXME: Make sure that the "to" context supports Objective-C!
1065 return Importer.getToContext().ObjCBuiltinIdTy;
1066
1067 case BuiltinType::ObjCClass:
1068 return Importer.getToContext().ObjCBuiltinClassTy;
1069
1070 case BuiltinType::ObjCSel:
1071 return Importer.getToContext().ObjCBuiltinSelTy;
1072 }
1073
1074 return QualType();
1075}
1076
1077QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1078 QualType ToElementType = Importer.Import(T->getElementType());
1079 if (ToElementType.isNull())
1080 return QualType();
1081
1082 return Importer.getToContext().getComplexType(ToElementType);
1083}
1084
1085QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1086 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1087 if (ToPointeeType.isNull())
1088 return QualType();
1089
1090 return Importer.getToContext().getPointerType(ToPointeeType);
1091}
1092
1093QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1094 // FIXME: Check for blocks support in "to" context.
1095 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1096 if (ToPointeeType.isNull())
1097 return QualType();
1098
1099 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1100}
1101
1102QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1103 // FIXME: Check for C++ support in "to" context.
1104 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1105 if (ToPointeeType.isNull())
1106 return QualType();
1107
1108 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1109}
1110
1111QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1112 // FIXME: Check for C++0x support in "to" context.
1113 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1114 if (ToPointeeType.isNull())
1115 return QualType();
1116
1117 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1118}
1119
1120QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1121 // FIXME: Check for C++ support in "to" context.
1122 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1123 if (ToPointeeType.isNull())
1124 return QualType();
1125
1126 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1127 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1128 ClassType.getTypePtr());
1129}
1130
1131QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1132 QualType ToElementType = Importer.Import(T->getElementType());
1133 if (ToElementType.isNull())
1134 return QualType();
1135
1136 return Importer.getToContext().getConstantArrayType(ToElementType,
1137 T->getSize(),
1138 T->getSizeModifier(),
1139 T->getIndexTypeCVRQualifiers());
1140}
1141
1142QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1143 QualType ToElementType = Importer.Import(T->getElementType());
1144 if (ToElementType.isNull())
1145 return QualType();
1146
1147 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1148 T->getSizeModifier(),
1149 T->getIndexTypeCVRQualifiers());
1150}
1151
1152QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1153 QualType ToElementType = Importer.Import(T->getElementType());
1154 if (ToElementType.isNull())
1155 return QualType();
1156
1157 Expr *Size = Importer.Import(T->getSizeExpr());
1158 if (!Size)
1159 return QualType();
1160
1161 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1162 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1163 T->getSizeModifier(),
1164 T->getIndexTypeCVRQualifiers(),
1165 Brackets);
1166}
1167
1168QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1169 QualType ToElementType = Importer.Import(T->getElementType());
1170 if (ToElementType.isNull())
1171 return QualType();
1172
1173 return Importer.getToContext().getVectorType(ToElementType,
1174 T->getNumElements(),
1175 T->isAltiVec(),
1176 T->isPixel());
1177}
1178
1179QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1180 QualType ToElementType = Importer.Import(T->getElementType());
1181 if (ToElementType.isNull())
1182 return QualType();
1183
1184 return Importer.getToContext().getExtVectorType(ToElementType,
1185 T->getNumElements());
1186}
1187
1188QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1189 // FIXME: What happens if we're importing a function without a prototype
1190 // into C++? Should we make it variadic?
1191 QualType ToResultType = Importer.Import(T->getResultType());
1192 if (ToResultType.isNull())
1193 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001194
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001195 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001196 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001197}
1198
1199QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1200 QualType ToResultType = Importer.Import(T->getResultType());
1201 if (ToResultType.isNull())
1202 return QualType();
1203
1204 // Import argument types
1205 llvm::SmallVector<QualType, 4> ArgTypes;
1206 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1207 AEnd = T->arg_type_end();
1208 A != AEnd; ++A) {
1209 QualType ArgType = Importer.Import(*A);
1210 if (ArgType.isNull())
1211 return QualType();
1212 ArgTypes.push_back(ArgType);
1213 }
1214
1215 // Import exception types
1216 llvm::SmallVector<QualType, 4> ExceptionTypes;
1217 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1218 EEnd = T->exception_end();
1219 E != EEnd; ++E) {
1220 QualType ExceptionType = Importer.Import(*E);
1221 if (ExceptionType.isNull())
1222 return QualType();
1223 ExceptionTypes.push_back(ExceptionType);
1224 }
1225
1226 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1227 ArgTypes.size(),
1228 T->isVariadic(),
1229 T->getTypeQuals(),
1230 T->hasExceptionSpec(),
1231 T->hasAnyExceptionSpec(),
1232 ExceptionTypes.size(),
1233 ExceptionTypes.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001234 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001235}
1236
1237QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1238 TypedefDecl *ToDecl
1239 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1240 if (!ToDecl)
1241 return QualType();
1242
1243 return Importer.getToContext().getTypeDeclType(ToDecl);
1244}
1245
1246QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1247 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1248 if (!ToExpr)
1249 return QualType();
1250
1251 return Importer.getToContext().getTypeOfExprType(ToExpr);
1252}
1253
1254QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1255 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1256 if (ToUnderlyingType.isNull())
1257 return QualType();
1258
1259 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1260}
1261
1262QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1263 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1264 if (!ToExpr)
1265 return QualType();
1266
1267 return Importer.getToContext().getDecltypeType(ToExpr);
1268}
1269
1270QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1271 RecordDecl *ToDecl
1272 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1273 if (!ToDecl)
1274 return QualType();
1275
1276 return Importer.getToContext().getTagDeclType(ToDecl);
1277}
1278
1279QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1280 EnumDecl *ToDecl
1281 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1282 if (!ToDecl)
1283 return QualType();
1284
1285 return Importer.getToContext().getTagDeclType(ToDecl);
1286}
1287
1288QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001289 NestedNameSpecifier *ToQualifier = 0;
1290 // Note: the qualifier in an ElaboratedType is optional.
1291 if (T->getQualifier()) {
1292 ToQualifier = Importer.Import(T->getQualifier());
1293 if (!ToQualifier)
1294 return QualType();
1295 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001296
1297 QualType ToNamedType = Importer.Import(T->getNamedType());
1298 if (ToNamedType.isNull())
1299 return QualType();
1300
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001301 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1302 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001303}
1304
1305QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1306 ObjCInterfaceDecl *Class
1307 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1308 if (!Class)
1309 return QualType();
1310
John McCallc12c5bb2010-05-15 11:32:37 +00001311 return Importer.getToContext().getObjCInterfaceType(Class);
1312}
1313
1314QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1315 QualType ToBaseType = Importer.Import(T->getBaseType());
1316 if (ToBaseType.isNull())
1317 return QualType();
1318
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001319 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001320 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001321 PEnd = T->qual_end();
1322 P != PEnd; ++P) {
1323 ObjCProtocolDecl *Protocol
1324 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1325 if (!Protocol)
1326 return QualType();
1327 Protocols.push_back(Protocol);
1328 }
1329
John McCallc12c5bb2010-05-15 11:32:37 +00001330 return Importer.getToContext().getObjCObjectType(ToBaseType,
1331 Protocols.data(),
1332 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001333}
1334
1335QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1336 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1337 if (ToPointeeType.isNull())
1338 return QualType();
1339
John McCallc12c5bb2010-05-15 11:32:37 +00001340 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001341}
1342
Douglas Gregor089459a2010-02-08 21:09:39 +00001343//----------------------------------------------------------------------------
1344// Import Declarations
1345//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001346bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1347 DeclContext *&LexicalDC,
1348 DeclarationName &Name,
1349 SourceLocation &Loc) {
1350 // Import the context of this declaration.
1351 DC = Importer.ImportContext(D->getDeclContext());
1352 if (!DC)
1353 return true;
1354
1355 LexicalDC = DC;
1356 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1357 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1358 if (!LexicalDC)
1359 return true;
1360 }
1361
1362 // Import the name of this declaration.
1363 Name = Importer.Import(D->getDeclName());
1364 if (D->getDeclName() && !Name)
1365 return true;
1366
1367 // Import the location of this declaration.
1368 Loc = Importer.Import(D->getLocation());
1369 return false;
1370}
1371
Douglas Gregor083a8212010-02-21 18:24:45 +00001372void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1373 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1374 FromEnd = FromDC->decls_end();
1375 From != FromEnd;
1376 ++From)
1377 Importer.Import(*From);
1378}
1379
Douglas Gregor96a01b42010-02-11 00:48:18 +00001380bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001381 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001382 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001383 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001384 Importer.getDiags(),
1385 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001386 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001387}
1388
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001389bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001390 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001391 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001392 Importer.getDiags(),
1393 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001394 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001395}
1396
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001397Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001398 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001399 << D->getDeclKindName();
1400 return 0;
1401}
1402
Douglas Gregor788c62d2010-02-21 18:26:36 +00001403Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1404 // Import the major distinguishing characteristics of this namespace.
1405 DeclContext *DC, *LexicalDC;
1406 DeclarationName Name;
1407 SourceLocation Loc;
1408 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1409 return 0;
1410
1411 NamespaceDecl *MergeWithNamespace = 0;
1412 if (!Name) {
1413 // This is an anonymous namespace. Adopt an existing anonymous
1414 // namespace if we can.
1415 // FIXME: Not testable.
1416 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1417 MergeWithNamespace = TU->getAnonymousNamespace();
1418 else
1419 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1420 } else {
1421 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1422 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1423 Lookup.first != Lookup.second;
1424 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001425 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001426 continue;
1427
1428 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1429 MergeWithNamespace = FoundNS;
1430 ConflictingDecls.clear();
1431 break;
1432 }
1433
1434 ConflictingDecls.push_back(*Lookup.first);
1435 }
1436
1437 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001438 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001439 ConflictingDecls.data(),
1440 ConflictingDecls.size());
1441 }
1442 }
1443
1444 // Create the "to" namespace, if needed.
1445 NamespaceDecl *ToNamespace = MergeWithNamespace;
1446 if (!ToNamespace) {
1447 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1448 Name.getAsIdentifierInfo());
1449 ToNamespace->setLexicalDeclContext(LexicalDC);
1450 LexicalDC->addDecl(ToNamespace);
1451
1452 // If this is an anonymous namespace, register it as the anonymous
1453 // namespace within its context.
1454 if (!Name) {
1455 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1456 TU->setAnonymousNamespace(ToNamespace);
1457 else
1458 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1459 }
1460 }
1461 Importer.Imported(D, ToNamespace);
1462
1463 ImportDeclContext(D);
1464
1465 return ToNamespace;
1466}
1467
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001468Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1469 // Import the major distinguishing characteristics of this typedef.
1470 DeclContext *DC, *LexicalDC;
1471 DeclarationName Name;
1472 SourceLocation Loc;
1473 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1474 return 0;
1475
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001476 // If this typedef is not in block scope, determine whether we've
1477 // seen a typedef with the same name (that we can merge with) or any
1478 // other entity by that name (which name lookup could conflict with).
1479 if (!DC->isFunctionOrMethod()) {
1480 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1481 unsigned IDNS = Decl::IDNS_Ordinary;
1482 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1483 Lookup.first != Lookup.second;
1484 ++Lookup.first) {
1485 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1486 continue;
1487 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001488 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1489 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001490 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001491 }
1492
1493 ConflictingDecls.push_back(*Lookup.first);
1494 }
1495
1496 if (!ConflictingDecls.empty()) {
1497 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1498 ConflictingDecls.data(),
1499 ConflictingDecls.size());
1500 if (!Name)
1501 return 0;
1502 }
1503 }
1504
Douglas Gregorea35d112010-02-15 23:54:17 +00001505 // Import the underlying type of this typedef;
1506 QualType T = Importer.Import(D->getUnderlyingType());
1507 if (T.isNull())
1508 return 0;
1509
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001510 // Create the new typedef node.
1511 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1512 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1513 Loc, Name.getAsIdentifierInfo(),
1514 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001515 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001516 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001517 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001518 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001519
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001520 return ToTypedef;
1521}
1522
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001523Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1524 // Import the major distinguishing characteristics of this enum.
1525 DeclContext *DC, *LexicalDC;
1526 DeclarationName Name;
1527 SourceLocation Loc;
1528 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1529 return 0;
1530
1531 // Figure out what enum name we're looking for.
1532 unsigned IDNS = Decl::IDNS_Tag;
1533 DeclarationName SearchName = Name;
1534 if (!SearchName && D->getTypedefForAnonDecl()) {
1535 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1536 IDNS = Decl::IDNS_Ordinary;
1537 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1538 IDNS |= Decl::IDNS_Ordinary;
1539
1540 // We may already have an enum of the same name; try to find and match it.
1541 if (!DC->isFunctionOrMethod() && SearchName) {
1542 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1543 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1544 Lookup.first != Lookup.second;
1545 ++Lookup.first) {
1546 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1547 continue;
1548
1549 Decl *Found = *Lookup.first;
1550 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1551 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1552 Found = Tag->getDecl();
1553 }
1554
1555 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001556 if (IsStructuralMatch(D, FoundEnum))
1557 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001558 }
1559
1560 ConflictingDecls.push_back(*Lookup.first);
1561 }
1562
1563 if (!ConflictingDecls.empty()) {
1564 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1565 ConflictingDecls.data(),
1566 ConflictingDecls.size());
1567 }
1568 }
1569
1570 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001571 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001572 Name.getAsIdentifierInfo(),
1573 Importer.Import(D->getTagKeywordLoc()),
1574 0);
John McCallb6217662010-03-15 10:12:16 +00001575 // Import the qualifier, if any.
1576 if (D->getQualifier()) {
1577 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1578 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1579 D2->setQualifierInfo(NNS, NNSRange);
1580 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001581 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001582 D2->setLexicalDeclContext(LexicalDC);
1583 Importer.Imported(D, D2);
1584 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001585
1586 // Import the integer type.
1587 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1588 if (ToIntegerType.isNull())
1589 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001590 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001591
1592 // Import the definition
1593 if (D->isDefinition()) {
1594 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1595 if (T.isNull())
1596 return 0;
1597
1598 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1599 if (ToPromotionType.isNull())
1600 return 0;
1601
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001602 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00001603 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00001604
1605 // FIXME: we might need to merge the number of positive or negative bits
1606 // if the enumerator lists don't match.
1607 D2->completeDefinition(T, ToPromotionType,
1608 D->getNumPositiveBits(),
1609 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001610 }
1611
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001612 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001613}
1614
Douglas Gregor96a01b42010-02-11 00:48:18 +00001615Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1616 // If this record has a definition in the translation unit we're coming from,
1617 // but this particular declaration is not that definition, import the
1618 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00001619 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001620 if (Definition && Definition != D) {
1621 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001622 if (!ImportedDef)
1623 return 0;
1624
1625 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001626 }
1627
1628 // Import the major distinguishing characteristics of this record.
1629 DeclContext *DC, *LexicalDC;
1630 DeclarationName Name;
1631 SourceLocation Loc;
1632 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1633 return 0;
1634
1635 // Figure out what structure name we're looking for.
1636 unsigned IDNS = Decl::IDNS_Tag;
1637 DeclarationName SearchName = Name;
1638 if (!SearchName && D->getTypedefForAnonDecl()) {
1639 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1640 IDNS = Decl::IDNS_Ordinary;
1641 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1642 IDNS |= Decl::IDNS_Ordinary;
1643
1644 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001645 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001646 if (!DC->isFunctionOrMethod() && SearchName) {
1647 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1648 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1649 Lookup.first != Lookup.second;
1650 ++Lookup.first) {
1651 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1652 continue;
1653
1654 Decl *Found = *Lookup.first;
1655 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1656 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1657 Found = Tag->getDecl();
1658 }
1659
1660 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001661 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1662 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1663 // The record types structurally match, or the "from" translation
1664 // unit only had a forward declaration anyway; call it the same
1665 // function.
1666 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001667 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001668 }
1669 } else {
1670 // We have a forward declaration of this type, so adopt that forward
1671 // declaration rather than building a new one.
1672 AdoptDecl = FoundRecord;
1673 continue;
1674 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00001675 }
1676
1677 ConflictingDecls.push_back(*Lookup.first);
1678 }
1679
1680 if (!ConflictingDecls.empty()) {
1681 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1682 ConflictingDecls.data(),
1683 ConflictingDecls.size());
1684 }
1685 }
1686
1687 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001688 RecordDecl *D2 = AdoptDecl;
1689 if (!D2) {
1690 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1691 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001692 D->getTagKind(),
1693 DC, Loc,
1694 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001695 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001696 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00001697 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001698
1699 if (D->isDefinition()) {
1700 // Add base classes.
1701 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1702 for (CXXRecordDecl::base_class_iterator
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001703 Base1 = D1CXX->bases_begin(),
1704 FromBaseEnd = D1CXX->bases_end();
1705 Base1 != FromBaseEnd;
1706 ++Base1) {
1707 QualType T = Importer.Import(Base1->getType());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001708 if (T.isNull())
1709 return 0;
1710
1711 Bases.push_back(
1712 new (Importer.getToContext())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001713 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1714 Base1->isVirtual(),
1715 Base1->isBaseOfClass(),
1716 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001717 T));
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001718 }
1719 if (!Bases.empty())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001720 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001721 }
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001722 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001723 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001724 DC, Loc,
1725 Name.getAsIdentifierInfo(),
1726 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00001727 }
John McCallb6217662010-03-15 10:12:16 +00001728 // Import the qualifier, if any.
1729 if (D->getQualifier()) {
1730 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1731 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1732 D2->setQualifierInfo(NNS, NNSRange);
1733 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001734 D2->setLexicalDeclContext(LexicalDC);
1735 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001736 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001737
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001738 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001739
Douglas Gregor96a01b42010-02-11 00:48:18 +00001740 if (D->isDefinition()) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001741 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00001742 ImportDeclContext(D);
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001743 D2->completeDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001744 }
1745
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001746 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001747}
1748
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001749Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1750 // Import the major distinguishing characteristics of this enumerator.
1751 DeclContext *DC, *LexicalDC;
1752 DeclarationName Name;
1753 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001754 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001755 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00001756
1757 QualType T = Importer.Import(D->getType());
1758 if (T.isNull())
1759 return 0;
1760
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001761 // Determine whether there are any other declarations with the same name and
1762 // in the same context.
1763 if (!LexicalDC->isFunctionOrMethod()) {
1764 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1765 unsigned IDNS = Decl::IDNS_Ordinary;
1766 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1767 Lookup.first != Lookup.second;
1768 ++Lookup.first) {
1769 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1770 continue;
1771
1772 ConflictingDecls.push_back(*Lookup.first);
1773 }
1774
1775 if (!ConflictingDecls.empty()) {
1776 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1777 ConflictingDecls.data(),
1778 ConflictingDecls.size());
1779 if (!Name)
1780 return 0;
1781 }
1782 }
1783
1784 Expr *Init = Importer.Import(D->getInitExpr());
1785 if (D->getInitExpr() && !Init)
1786 return 0;
1787
1788 EnumConstantDecl *ToEnumerator
1789 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1790 Name.getAsIdentifierInfo(), T,
1791 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00001792 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001793 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001794 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001795 LexicalDC->addDecl(ToEnumerator);
1796 return ToEnumerator;
1797}
Douglas Gregor96a01b42010-02-11 00:48:18 +00001798
Douglas Gregora404ea62010-02-10 19:54:31 +00001799Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1800 // Import the major distinguishing characteristics of this function.
1801 DeclContext *DC, *LexicalDC;
1802 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00001803 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001804 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00001805 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001806
1807 // Try to find a function in our own ("to") context with the same name, same
1808 // type, and in the same context as the function we're importing.
1809 if (!LexicalDC->isFunctionOrMethod()) {
1810 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1811 unsigned IDNS = Decl::IDNS_Ordinary;
1812 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1813 Lookup.first != Lookup.second;
1814 ++Lookup.first) {
1815 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1816 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00001817
Douglas Gregora404ea62010-02-10 19:54:31 +00001818 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1819 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1820 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001821 if (Importer.IsStructurallyEquivalent(D->getType(),
1822 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00001823 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001824 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00001825 }
1826
1827 // FIXME: Check for overloading more carefully, e.g., by boosting
1828 // Sema::IsOverload out to the AST library.
1829
1830 // Function overloading is okay in C++.
1831 if (Importer.getToContext().getLangOptions().CPlusPlus)
1832 continue;
1833
1834 // Complain about inconsistent function types.
1835 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00001836 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00001837 Importer.ToDiag(FoundFunction->getLocation(),
1838 diag::note_odr_value_here)
1839 << FoundFunction->getType();
1840 }
1841 }
1842
1843 ConflictingDecls.push_back(*Lookup.first);
1844 }
1845
1846 if (!ConflictingDecls.empty()) {
1847 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1848 ConflictingDecls.data(),
1849 ConflictingDecls.size());
1850 if (!Name)
1851 return 0;
1852 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00001853 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001854
1855 // Import the type.
1856 QualType T = Importer.Import(D->getType());
1857 if (T.isNull())
1858 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001859
1860 // Import the function parameters.
1861 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1862 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1863 P != PEnd; ++P) {
1864 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1865 if (!ToP)
1866 return 0;
1867
1868 Parameters.push_back(ToP);
1869 }
1870
1871 // Create the imported function.
1872 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00001873 FunctionDecl *ToFunction = 0;
1874 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1875 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1876 cast<CXXRecordDecl>(DC),
1877 Loc, Name, T, TInfo,
1878 FromConstructor->isExplicit(),
1879 D->isInlineSpecified(),
1880 D->isImplicit());
1881 } else if (isa<CXXDestructorDecl>(D)) {
1882 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1883 cast<CXXRecordDecl>(DC),
1884 Loc, Name, T,
1885 D->isInlineSpecified(),
1886 D->isImplicit());
1887 } else if (CXXConversionDecl *FromConversion
1888 = dyn_cast<CXXConversionDecl>(D)) {
1889 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1890 cast<CXXRecordDecl>(DC),
1891 Loc, Name, T, TInfo,
1892 D->isInlineSpecified(),
1893 FromConversion->isExplicit());
1894 } else {
1895 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1896 Name, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001897 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00001898 D->isInlineSpecified(),
1899 D->hasWrittenPrototype());
1900 }
John McCallb6217662010-03-15 10:12:16 +00001901
1902 // Import the qualifier, if any.
1903 if (D->getQualifier()) {
1904 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1905 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1906 ToFunction->setQualifierInfo(NNS, NNSRange);
1907 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001908 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001909 ToFunction->setLexicalDeclContext(LexicalDC);
1910 Importer.Imported(D, ToFunction);
1911 LexicalDC->addDecl(ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001912
Douglas Gregora404ea62010-02-10 19:54:31 +00001913 // Set the parameters.
1914 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001915 Parameters[I]->setOwningFunction(ToFunction);
1916 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00001917 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001918 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00001919
1920 // FIXME: Other bits to merge?
Douglas Gregor089459a2010-02-08 21:09:39 +00001921
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001922 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00001923}
1924
Douglas Gregorc144f352010-02-21 18:29:16 +00001925Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1926 return VisitFunctionDecl(D);
1927}
1928
1929Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1930 return VisitCXXMethodDecl(D);
1931}
1932
1933Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1934 return VisitCXXMethodDecl(D);
1935}
1936
1937Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1938 return VisitCXXMethodDecl(D);
1939}
1940
Douglas Gregor96a01b42010-02-11 00:48:18 +00001941Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1942 // Import the major distinguishing characteristics of a variable.
1943 DeclContext *DC, *LexicalDC;
1944 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001945 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001946 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1947 return 0;
1948
1949 // Import the type.
1950 QualType T = Importer.Import(D->getType());
1951 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00001952 return 0;
1953
1954 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1955 Expr *BitWidth = Importer.Import(D->getBitWidth());
1956 if (!BitWidth && D->getBitWidth())
1957 return 0;
1958
1959 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1960 Loc, Name.getAsIdentifierInfo(),
1961 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00001962 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001963 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001964 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001965 LexicalDC->addDecl(ToField);
1966 return ToField;
1967}
1968
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00001969Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1970 // Import the major distinguishing characteristics of an ivar.
1971 DeclContext *DC, *LexicalDC;
1972 DeclarationName Name;
1973 SourceLocation Loc;
1974 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1975 return 0;
1976
1977 // Determine whether we've already imported this ivar
1978 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1979 Lookup.first != Lookup.second;
1980 ++Lookup.first) {
1981 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
1982 if (Importer.IsStructurallyEquivalent(D->getType(),
1983 FoundIvar->getType())) {
1984 Importer.Imported(D, FoundIvar);
1985 return FoundIvar;
1986 }
1987
1988 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
1989 << Name << D->getType() << FoundIvar->getType();
1990 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
1991 << FoundIvar->getType();
1992 return 0;
1993 }
1994 }
1995
1996 // Import the type.
1997 QualType T = Importer.Import(D->getType());
1998 if (T.isNull())
1999 return 0;
2000
2001 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2002 Expr *BitWidth = Importer.Import(D->getBitWidth());
2003 if (!BitWidth && D->getBitWidth())
2004 return 0;
2005
Daniel Dunbara0654922010-04-02 20:10:03 +00002006 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2007 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002008 Loc, Name.getAsIdentifierInfo(),
2009 T, TInfo, D->getAccessControl(),
2010 BitWidth);
2011 ToIvar->setLexicalDeclContext(LexicalDC);
2012 Importer.Imported(D, ToIvar);
2013 LexicalDC->addDecl(ToIvar);
2014 return ToIvar;
2015
2016}
2017
Douglas Gregora404ea62010-02-10 19:54:31 +00002018Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2019 // Import the major distinguishing characteristics of a variable.
2020 DeclContext *DC, *LexicalDC;
2021 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002022 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002023 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002024 return 0;
2025
Douglas Gregor089459a2010-02-08 21:09:39 +00002026 // Try to find a variable in our own ("to") context with the same name and
2027 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002028 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002029 VarDecl *MergeWithVar = 0;
2030 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2031 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002032 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002033 Lookup.first != Lookup.second;
2034 ++Lookup.first) {
2035 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2036 continue;
2037
2038 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2039 // We have found a variable that we may need to merge with. Check it.
2040 if (isExternalLinkage(FoundVar->getLinkage()) &&
2041 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002042 if (Importer.IsStructurallyEquivalent(D->getType(),
2043 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002044 MergeWithVar = FoundVar;
2045 break;
2046 }
2047
Douglas Gregord0145422010-02-12 17:23:39 +00002048 const ArrayType *FoundArray
2049 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2050 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002051 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002052 if (FoundArray && TArray) {
2053 if (isa<IncompleteArrayType>(FoundArray) &&
2054 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002055 // Import the type.
2056 QualType T = Importer.Import(D->getType());
2057 if (T.isNull())
2058 return 0;
2059
Douglas Gregord0145422010-02-12 17:23:39 +00002060 FoundVar->setType(T);
2061 MergeWithVar = FoundVar;
2062 break;
2063 } else if (isa<IncompleteArrayType>(TArray) &&
2064 isa<ConstantArrayType>(FoundArray)) {
2065 MergeWithVar = FoundVar;
2066 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002067 }
2068 }
2069
Douglas Gregor089459a2010-02-08 21:09:39 +00002070 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002071 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002072 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2073 << FoundVar->getType();
2074 }
2075 }
2076
2077 ConflictingDecls.push_back(*Lookup.first);
2078 }
2079
2080 if (MergeWithVar) {
2081 // An equivalent variable with external linkage has been found. Link
2082 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002083 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002084
2085 if (VarDecl *DDef = D->getDefinition()) {
2086 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2087 Importer.ToDiag(ExistingDef->getLocation(),
2088 diag::err_odr_variable_multiple_def)
2089 << Name;
2090 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2091 } else {
2092 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002093 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002094 }
2095 }
2096
2097 return MergeWithVar;
2098 }
2099
2100 if (!ConflictingDecls.empty()) {
2101 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2102 ConflictingDecls.data(),
2103 ConflictingDecls.size());
2104 if (!Name)
2105 return 0;
2106 }
2107 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002108
Douglas Gregorea35d112010-02-15 23:54:17 +00002109 // Import the type.
2110 QualType T = Importer.Import(D->getType());
2111 if (T.isNull())
2112 return 0;
2113
Douglas Gregor089459a2010-02-08 21:09:39 +00002114 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002115 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002116 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2117 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002118 D->getStorageClass(),
2119 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002120 // Import the qualifier, if any.
2121 if (D->getQualifier()) {
2122 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2123 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2124 ToVar->setQualifierInfo(NNS, NNSRange);
2125 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002126 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002127 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002128 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002129 LexicalDC->addDecl(ToVar);
2130
Douglas Gregor089459a2010-02-08 21:09:39 +00002131 // Merge the initializer.
2132 // FIXME: Can we really import any initializer? Alternatively, we could force
2133 // ourselves to import every declaration of a variable and then only use
2134 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002135 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002136
2137 // FIXME: Other bits to merge?
2138
2139 return ToVar;
2140}
2141
Douglas Gregor2cd00932010-02-17 21:22:52 +00002142Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2143 // Parameters are created in the translation unit's context, then moved
2144 // into the function declaration's context afterward.
2145 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2146
2147 // Import the name of this declaration.
2148 DeclarationName Name = Importer.Import(D->getDeclName());
2149 if (D->getDeclName() && !Name)
2150 return 0;
2151
2152 // Import the location of this declaration.
2153 SourceLocation Loc = Importer.Import(D->getLocation());
2154
2155 // Import the parameter's type.
2156 QualType T = Importer.Import(D->getType());
2157 if (T.isNull())
2158 return 0;
2159
2160 // Create the imported parameter.
2161 ImplicitParamDecl *ToParm
2162 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2163 Loc, Name.getAsIdentifierInfo(),
2164 T);
2165 return Importer.Imported(D, ToParm);
2166}
2167
Douglas Gregora404ea62010-02-10 19:54:31 +00002168Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2169 // Parameters are created in the translation unit's context, then moved
2170 // into the function declaration's context afterward.
2171 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2172
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002173 // Import the name of this declaration.
2174 DeclarationName Name = Importer.Import(D->getDeclName());
2175 if (D->getDeclName() && !Name)
2176 return 0;
2177
Douglas Gregora404ea62010-02-10 19:54:31 +00002178 // Import the location of this declaration.
2179 SourceLocation Loc = Importer.Import(D->getLocation());
2180
2181 // Import the parameter's type.
2182 QualType T = Importer.Import(D->getType());
2183 if (T.isNull())
2184 return 0;
2185
2186 // Create the imported parameter.
2187 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2188 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2189 Loc, Name.getAsIdentifierInfo(),
2190 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002191 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002192 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002193 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002194 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002195}
2196
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002197Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2198 // Import the major distinguishing characteristics of a method.
2199 DeclContext *DC, *LexicalDC;
2200 DeclarationName Name;
2201 SourceLocation Loc;
2202 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2203 return 0;
2204
2205 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2206 Lookup.first != Lookup.second;
2207 ++Lookup.first) {
2208 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2209 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2210 continue;
2211
2212 // Check return types.
2213 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2214 FoundMethod->getResultType())) {
2215 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2216 << D->isInstanceMethod() << Name
2217 << D->getResultType() << FoundMethod->getResultType();
2218 Importer.ToDiag(FoundMethod->getLocation(),
2219 diag::note_odr_objc_method_here)
2220 << D->isInstanceMethod() << Name;
2221 return 0;
2222 }
2223
2224 // Check the number of parameters.
2225 if (D->param_size() != FoundMethod->param_size()) {
2226 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2227 << D->isInstanceMethod() << Name
2228 << D->param_size() << FoundMethod->param_size();
2229 Importer.ToDiag(FoundMethod->getLocation(),
2230 diag::note_odr_objc_method_here)
2231 << D->isInstanceMethod() << Name;
2232 return 0;
2233 }
2234
2235 // Check parameter types.
2236 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2237 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2238 P != PEnd; ++P, ++FoundP) {
2239 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2240 (*FoundP)->getType())) {
2241 Importer.FromDiag((*P)->getLocation(),
2242 diag::err_odr_objc_method_param_type_inconsistent)
2243 << D->isInstanceMethod() << Name
2244 << (*P)->getType() << (*FoundP)->getType();
2245 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2246 << (*FoundP)->getType();
2247 return 0;
2248 }
2249 }
2250
2251 // Check variadic/non-variadic.
2252 // Check the number of parameters.
2253 if (D->isVariadic() != FoundMethod->isVariadic()) {
2254 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2255 << D->isInstanceMethod() << Name;
2256 Importer.ToDiag(FoundMethod->getLocation(),
2257 diag::note_odr_objc_method_here)
2258 << D->isInstanceMethod() << Name;
2259 return 0;
2260 }
2261
2262 // FIXME: Any other bits we need to merge?
2263 return Importer.Imported(D, FoundMethod);
2264 }
2265 }
2266
2267 // Import the result type.
2268 QualType ResultTy = Importer.Import(D->getResultType());
2269 if (ResultTy.isNull())
2270 return 0;
2271
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002272 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2273
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002274 ObjCMethodDecl *ToMethod
2275 = ObjCMethodDecl::Create(Importer.getToContext(),
2276 Loc,
2277 Importer.Import(D->getLocEnd()),
2278 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002279 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002280 D->isInstanceMethod(),
2281 D->isVariadic(),
2282 D->isSynthesized(),
2283 D->getImplementationControl());
2284
2285 // FIXME: When we decide to merge method definitions, we'll need to
2286 // deal with implicit parameters.
2287
2288 // Import the parameters
2289 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2290 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2291 FromPEnd = D->param_end();
2292 FromP != FromPEnd;
2293 ++FromP) {
2294 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2295 if (!ToP)
2296 return 0;
2297
2298 ToParams.push_back(ToP);
2299 }
2300
2301 // Set the parameters.
2302 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2303 ToParams[I]->setOwningFunction(ToMethod);
2304 ToMethod->addDecl(ToParams[I]);
2305 }
2306 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002307 ToParams.data(), ToParams.size(),
2308 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002309
2310 ToMethod->setLexicalDeclContext(LexicalDC);
2311 Importer.Imported(D, ToMethod);
2312 LexicalDC->addDecl(ToMethod);
2313 return ToMethod;
2314}
2315
Douglas Gregorb4677b62010-02-18 01:47:50 +00002316Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2317 // Import the major distinguishing characteristics of a category.
2318 DeclContext *DC, *LexicalDC;
2319 DeclarationName Name;
2320 SourceLocation Loc;
2321 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2322 return 0;
2323
2324 ObjCInterfaceDecl *ToInterface
2325 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2326 if (!ToInterface)
2327 return 0;
2328
2329 // Determine if we've already encountered this category.
2330 ObjCCategoryDecl *MergeWithCategory
2331 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2332 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2333 if (!ToCategory) {
2334 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2335 Importer.Import(D->getAtLoc()),
2336 Loc,
2337 Importer.Import(D->getCategoryNameLoc()),
2338 Name.getAsIdentifierInfo());
2339 ToCategory->setLexicalDeclContext(LexicalDC);
2340 LexicalDC->addDecl(ToCategory);
2341 Importer.Imported(D, ToCategory);
2342
2343 // Link this category into its class's category list.
2344 ToCategory->setClassInterface(ToInterface);
2345 ToCategory->insertNextClassCategory();
2346
2347 // Import protocols
2348 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2349 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2350 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2351 = D->protocol_loc_begin();
2352 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2353 FromProtoEnd = D->protocol_end();
2354 FromProto != FromProtoEnd;
2355 ++FromProto, ++FromProtoLoc) {
2356 ObjCProtocolDecl *ToProto
2357 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2358 if (!ToProto)
2359 return 0;
2360 Protocols.push_back(ToProto);
2361 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2362 }
2363
2364 // FIXME: If we're merging, make sure that the protocol list is the same.
2365 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2366 ProtocolLocs.data(), Importer.getToContext());
2367
2368 } else {
2369 Importer.Imported(D, ToCategory);
2370 }
2371
2372 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002373 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002374
2375 // If we have an implementation, import it as well.
2376 if (D->getImplementation()) {
2377 ObjCCategoryImplDecl *Impl
2378 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2379 if (!Impl)
2380 return 0;
2381
2382 ToCategory->setImplementation(Impl);
2383 }
2384
2385 return ToCategory;
2386}
2387
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002388Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002389 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002390 DeclContext *DC, *LexicalDC;
2391 DeclarationName Name;
2392 SourceLocation Loc;
2393 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2394 return 0;
2395
2396 ObjCProtocolDecl *MergeWithProtocol = 0;
2397 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2398 Lookup.first != Lookup.second;
2399 ++Lookup.first) {
2400 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2401 continue;
2402
2403 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2404 break;
2405 }
2406
2407 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2408 if (!ToProto || ToProto->isForwardDecl()) {
2409 if (!ToProto) {
2410 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2411 Name.getAsIdentifierInfo());
2412 ToProto->setForwardDecl(D->isForwardDecl());
2413 ToProto->setLexicalDeclContext(LexicalDC);
2414 LexicalDC->addDecl(ToProto);
2415 }
2416 Importer.Imported(D, ToProto);
2417
2418 // Import protocols
2419 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2420 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2421 ObjCProtocolDecl::protocol_loc_iterator
2422 FromProtoLoc = D->protocol_loc_begin();
2423 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2424 FromProtoEnd = D->protocol_end();
2425 FromProto != FromProtoEnd;
2426 ++FromProto, ++FromProtoLoc) {
2427 ObjCProtocolDecl *ToProto
2428 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2429 if (!ToProto)
2430 return 0;
2431 Protocols.push_back(ToProto);
2432 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2433 }
2434
2435 // FIXME: If we're merging, make sure that the protocol list is the same.
2436 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2437 ProtocolLocs.data(), Importer.getToContext());
2438 } else {
2439 Importer.Imported(D, ToProto);
2440 }
2441
Douglas Gregorb4677b62010-02-18 01:47:50 +00002442 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002443 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002444
2445 return ToProto;
2446}
2447
Douglas Gregora12d2942010-02-16 01:20:57 +00002448Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2449 // Import the major distinguishing characteristics of an @interface.
2450 DeclContext *DC, *LexicalDC;
2451 DeclarationName Name;
2452 SourceLocation Loc;
2453 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2454 return 0;
2455
2456 ObjCInterfaceDecl *MergeWithIface = 0;
2457 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2458 Lookup.first != Lookup.second;
2459 ++Lookup.first) {
2460 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2461 continue;
2462
2463 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2464 break;
2465 }
2466
2467 ObjCInterfaceDecl *ToIface = MergeWithIface;
2468 if (!ToIface || ToIface->isForwardDecl()) {
2469 if (!ToIface) {
2470 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2471 DC, Loc,
2472 Name.getAsIdentifierInfo(),
2473 Importer.Import(D->getClassLoc()),
2474 D->isForwardDecl(),
2475 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002476 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002477 ToIface->setLexicalDeclContext(LexicalDC);
2478 LexicalDC->addDecl(ToIface);
2479 }
2480 Importer.Imported(D, ToIface);
2481
Douglas Gregora12d2942010-02-16 01:20:57 +00002482 if (D->getSuperClass()) {
2483 ObjCInterfaceDecl *Super
2484 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2485 if (!Super)
2486 return 0;
2487
2488 ToIface->setSuperClass(Super);
2489 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2490 }
2491
2492 // Import protocols
2493 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2494 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2495 ObjCInterfaceDecl::protocol_loc_iterator
2496 FromProtoLoc = D->protocol_loc_begin();
2497 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2498 FromProtoEnd = D->protocol_end();
2499 FromProto != FromProtoEnd;
2500 ++FromProto, ++FromProtoLoc) {
2501 ObjCProtocolDecl *ToProto
2502 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2503 if (!ToProto)
2504 return 0;
2505 Protocols.push_back(ToProto);
2506 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2507 }
2508
2509 // FIXME: If we're merging, make sure that the protocol list is the same.
2510 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2511 ProtocolLocs.data(), Importer.getToContext());
2512
Douglas Gregora12d2942010-02-16 01:20:57 +00002513 // Import @end range
2514 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2515 } else {
2516 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002517
2518 // Check for consistency of superclasses.
2519 DeclarationName FromSuperName, ToSuperName;
2520 if (D->getSuperClass())
2521 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2522 if (ToIface->getSuperClass())
2523 ToSuperName = ToIface->getSuperClass()->getDeclName();
2524 if (FromSuperName != ToSuperName) {
2525 Importer.ToDiag(ToIface->getLocation(),
2526 diag::err_odr_objc_superclass_inconsistent)
2527 << ToIface->getDeclName();
2528 if (ToIface->getSuperClass())
2529 Importer.ToDiag(ToIface->getSuperClassLoc(),
2530 diag::note_odr_objc_superclass)
2531 << ToIface->getSuperClass()->getDeclName();
2532 else
2533 Importer.ToDiag(ToIface->getLocation(),
2534 diag::note_odr_objc_missing_superclass);
2535 if (D->getSuperClass())
2536 Importer.FromDiag(D->getSuperClassLoc(),
2537 diag::note_odr_objc_superclass)
2538 << D->getSuperClass()->getDeclName();
2539 else
2540 Importer.FromDiag(D->getLocation(),
2541 diag::note_odr_objc_missing_superclass);
2542 return 0;
2543 }
Douglas Gregora12d2942010-02-16 01:20:57 +00002544 }
2545
Douglas Gregorb4677b62010-02-18 01:47:50 +00002546 // Import categories. When the categories themselves are imported, they'll
2547 // hook themselves into this interface.
2548 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2549 FromCat = FromCat->getNextClassCategory())
2550 Importer.Import(FromCat);
2551
Douglas Gregora12d2942010-02-16 01:20:57 +00002552 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00002553 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00002554
2555 // If we have an @implementation, import it as well.
2556 if (D->getImplementation()) {
2557 ObjCImplementationDecl *Impl
2558 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2559 if (!Impl)
2560 return 0;
2561
2562 ToIface->setImplementation(Impl);
2563 }
2564
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002565 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00002566}
2567
Douglas Gregore3261622010-02-17 18:02:10 +00002568Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2569 // Import the major distinguishing characteristics of an @property.
2570 DeclContext *DC, *LexicalDC;
2571 DeclarationName Name;
2572 SourceLocation Loc;
2573 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2574 return 0;
2575
2576 // Check whether we have already imported this property.
2577 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2578 Lookup.first != Lookup.second;
2579 ++Lookup.first) {
2580 if (ObjCPropertyDecl *FoundProp
2581 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2582 // Check property types.
2583 if (!Importer.IsStructurallyEquivalent(D->getType(),
2584 FoundProp->getType())) {
2585 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2586 << Name << D->getType() << FoundProp->getType();
2587 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2588 << FoundProp->getType();
2589 return 0;
2590 }
2591
2592 // FIXME: Check property attributes, getters, setters, etc.?
2593
2594 // Consider these properties to be equivalent.
2595 Importer.Imported(D, FoundProp);
2596 return FoundProp;
2597 }
2598 }
2599
2600 // Import the type.
2601 QualType T = Importer.Import(D->getType());
2602 if (T.isNull())
2603 return 0;
2604
2605 // Create the new property.
2606 ObjCPropertyDecl *ToProperty
2607 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2608 Name.getAsIdentifierInfo(),
2609 Importer.Import(D->getAtLoc()),
2610 T,
2611 D->getPropertyImplementation());
2612 Importer.Imported(D, ToProperty);
2613 ToProperty->setLexicalDeclContext(LexicalDC);
2614 LexicalDC->addDecl(ToProperty);
2615
2616 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
2617 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2618 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2619 ToProperty->setGetterMethodDecl(
2620 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2621 ToProperty->setSetterMethodDecl(
2622 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2623 ToProperty->setPropertyIvarDecl(
2624 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2625 return ToProperty;
2626}
2627
Douglas Gregor2b785022010-02-18 02:12:22 +00002628Decl *
2629ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2630 // Import the context of this declaration.
2631 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2632 if (!DC)
2633 return 0;
2634
2635 DeclContext *LexicalDC = DC;
2636 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2637 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2638 if (!LexicalDC)
2639 return 0;
2640 }
2641
2642 // Import the location of this declaration.
2643 SourceLocation Loc = Importer.Import(D->getLocation());
2644
2645 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2646 llvm::SmallVector<SourceLocation, 4> Locations;
2647 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2648 = D->protocol_loc_begin();
2649 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2650 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2651 FromProto != FromProtoEnd;
2652 ++FromProto, ++FromProtoLoc) {
2653 ObjCProtocolDecl *ToProto
2654 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2655 if (!ToProto)
2656 continue;
2657
2658 Protocols.push_back(ToProto);
2659 Locations.push_back(Importer.Import(*FromProtoLoc));
2660 }
2661
2662 ObjCForwardProtocolDecl *ToForward
2663 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2664 Protocols.data(), Protocols.size(),
2665 Locations.data());
2666 ToForward->setLexicalDeclContext(LexicalDC);
2667 LexicalDC->addDecl(ToForward);
2668 Importer.Imported(D, ToForward);
2669 return ToForward;
2670}
2671
Douglas Gregora2bc15b2010-02-18 02:04:09 +00002672Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2673 // Import the context of this declaration.
2674 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2675 if (!DC)
2676 return 0;
2677
2678 DeclContext *LexicalDC = DC;
2679 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2680 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2681 if (!LexicalDC)
2682 return 0;
2683 }
2684
2685 // Import the location of this declaration.
2686 SourceLocation Loc = Importer.Import(D->getLocation());
2687
2688 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2689 llvm::SmallVector<SourceLocation, 4> Locations;
2690 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2691 From != FromEnd; ++From) {
2692 ObjCInterfaceDecl *ToIface
2693 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2694 if (!ToIface)
2695 continue;
2696
2697 Interfaces.push_back(ToIface);
2698 Locations.push_back(Importer.Import(From->getLocation()));
2699 }
2700
2701 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2702 Loc,
2703 Interfaces.data(),
2704 Locations.data(),
2705 Interfaces.size());
2706 ToClass->setLexicalDeclContext(LexicalDC);
2707 LexicalDC->addDecl(ToClass);
2708 Importer.Imported(D, ToClass);
2709 return ToClass;
2710}
2711
Douglas Gregor4800d952010-02-11 19:21:55 +00002712//----------------------------------------------------------------------------
2713// Import Statements
2714//----------------------------------------------------------------------------
2715
2716Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2717 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2718 << S->getStmtClassName();
2719 return 0;
2720}
2721
2722//----------------------------------------------------------------------------
2723// Import Expressions
2724//----------------------------------------------------------------------------
2725Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2726 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2727 << E->getStmtClassName();
2728 return 0;
2729}
2730
Douglas Gregor44080632010-02-19 01:17:02 +00002731Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2732 NestedNameSpecifier *Qualifier = 0;
2733 if (E->getQualifier()) {
2734 Qualifier = Importer.Import(E->getQualifier());
2735 if (!E->getQualifier())
2736 return 0;
2737 }
2738
2739 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2740 if (!ToD)
2741 return 0;
2742
2743 QualType T = Importer.Import(E->getType());
2744 if (T.isNull())
2745 return 0;
2746
2747 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2748 Importer.Import(E->getQualifierRange()),
2749 ToD,
2750 Importer.Import(E->getLocation()),
2751 T,
2752 /*FIXME:TemplateArgs=*/0);
2753}
2754
Douglas Gregor4800d952010-02-11 19:21:55 +00002755Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2756 QualType T = Importer.Import(E->getType());
2757 if (T.isNull())
2758 return 0;
2759
2760 return new (Importer.getToContext())
2761 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2762}
2763
Douglas Gregorb2e400a2010-02-18 02:21:22 +00002764Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2765 QualType T = Importer.Import(E->getType());
2766 if (T.isNull())
2767 return 0;
2768
2769 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2770 E->isWide(), T,
2771 Importer.Import(E->getLocation()));
2772}
2773
Douglas Gregorf638f952010-02-19 01:07:06 +00002774Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2775 Expr *SubExpr = Importer.Import(E->getSubExpr());
2776 if (!SubExpr)
2777 return 0;
2778
2779 return new (Importer.getToContext())
2780 ParenExpr(Importer.Import(E->getLParen()),
2781 Importer.Import(E->getRParen()),
2782 SubExpr);
2783}
2784
2785Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2786 QualType T = Importer.Import(E->getType());
2787 if (T.isNull())
2788 return 0;
2789
2790 Expr *SubExpr = Importer.Import(E->getSubExpr());
2791 if (!SubExpr)
2792 return 0;
2793
2794 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2795 T,
2796 Importer.Import(E->getOperatorLoc()));
2797}
2798
Douglas Gregorbd249a52010-02-19 01:24:23 +00002799Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2800 QualType ResultType = Importer.Import(E->getType());
2801
2802 if (E->isArgumentType()) {
2803 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2804 if (!TInfo)
2805 return 0;
2806
2807 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2808 TInfo, ResultType,
2809 Importer.Import(E->getOperatorLoc()),
2810 Importer.Import(E->getRParenLoc()));
2811 }
2812
2813 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2814 if (!SubExpr)
2815 return 0;
2816
2817 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2818 SubExpr, ResultType,
2819 Importer.Import(E->getOperatorLoc()),
2820 Importer.Import(E->getRParenLoc()));
2821}
2822
Douglas Gregorf638f952010-02-19 01:07:06 +00002823Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2824 QualType T = Importer.Import(E->getType());
2825 if (T.isNull())
2826 return 0;
2827
2828 Expr *LHS = Importer.Import(E->getLHS());
2829 if (!LHS)
2830 return 0;
2831
2832 Expr *RHS = Importer.Import(E->getRHS());
2833 if (!RHS)
2834 return 0;
2835
2836 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2837 T,
2838 Importer.Import(E->getOperatorLoc()));
2839}
2840
2841Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2842 QualType T = Importer.Import(E->getType());
2843 if (T.isNull())
2844 return 0;
2845
2846 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2847 if (CompLHSType.isNull())
2848 return 0;
2849
2850 QualType CompResultType = Importer.Import(E->getComputationResultType());
2851 if (CompResultType.isNull())
2852 return 0;
2853
2854 Expr *LHS = Importer.Import(E->getLHS());
2855 if (!LHS)
2856 return 0;
2857
2858 Expr *RHS = Importer.Import(E->getRHS());
2859 if (!RHS)
2860 return 0;
2861
2862 return new (Importer.getToContext())
2863 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2864 T, CompLHSType, CompResultType,
2865 Importer.Import(E->getOperatorLoc()));
2866}
2867
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002868Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2869 QualType T = Importer.Import(E->getType());
2870 if (T.isNull())
2871 return 0;
2872
2873 Expr *SubExpr = Importer.Import(E->getSubExpr());
2874 if (!SubExpr)
2875 return 0;
2876
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002877 // FIXME: Initialize the base path.
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002878 assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002879 CXXBaseSpecifierArray BasePath;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002880 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002881 SubExpr, BasePath,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002882 E->isLvalueCast());
2883}
2884
Douglas Gregor008847a2010-02-19 01:32:14 +00002885Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2886 QualType T = Importer.Import(E->getType());
2887 if (T.isNull())
2888 return 0;
2889
2890 Expr *SubExpr = Importer.Import(E->getSubExpr());
2891 if (!SubExpr)
2892 return 0;
2893
2894 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2895 if (!TInfo && E->getTypeInfoAsWritten())
2896 return 0;
2897
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002898 // FIXME: Initialize the base path.
2899 assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
2900 CXXBaseSpecifierArray BasePath;
Douglas Gregor008847a2010-02-19 01:32:14 +00002901 return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002902 SubExpr, BasePath, TInfo,
Douglas Gregor008847a2010-02-19 01:32:14 +00002903 Importer.Import(E->getLParenLoc()),
2904 Importer.Import(E->getRParenLoc()));
2905}
2906
Douglas Gregor4800d952010-02-11 19:21:55 +00002907ASTImporter::ASTImporter(Diagnostic &Diags,
2908 ASTContext &ToContext, FileManager &ToFileManager,
2909 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002910 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +00002911 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor4800d952010-02-11 19:21:55 +00002912 Diags(Diags) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00002913 ImportedDecls[FromContext.getTranslationUnitDecl()]
2914 = ToContext.getTranslationUnitDecl();
2915}
2916
2917ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002918
2919QualType ASTImporter::Import(QualType FromT) {
2920 if (FromT.isNull())
2921 return QualType();
2922
Douglas Gregor169fba52010-02-08 15:18:58 +00002923 // Check whether we've already imported this type.
2924 llvm::DenseMap<Type *, Type *>::iterator Pos
2925 = ImportedTypes.find(FromT.getTypePtr());
2926 if (Pos != ImportedTypes.end())
2927 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002928
Douglas Gregor169fba52010-02-08 15:18:58 +00002929 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002930 ASTNodeImporter Importer(*this);
2931 QualType ToT = Importer.Visit(FromT.getTypePtr());
2932 if (ToT.isNull())
2933 return ToT;
2934
Douglas Gregor169fba52010-02-08 15:18:58 +00002935 // Record the imported type.
2936 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2937
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002938 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2939}
2940
Douglas Gregor9bed8792010-02-09 19:21:46 +00002941TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002942 if (!FromTSI)
2943 return FromTSI;
2944
2945 // FIXME: For now we just create a "trivial" type source info based
2946 // on the type and a seingle location. Implement a real version of
2947 // this.
2948 QualType T = Import(FromTSI->getType());
2949 if (T.isNull())
2950 return 0;
2951
2952 return ToContext.getTrivialTypeSourceInfo(T,
2953 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002954}
2955
2956Decl *ASTImporter::Import(Decl *FromD) {
2957 if (!FromD)
2958 return 0;
2959
2960 // Check whether we've already imported this declaration.
2961 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2962 if (Pos != ImportedDecls.end())
2963 return Pos->second;
2964
2965 // Import the type
2966 ASTNodeImporter Importer(*this);
2967 Decl *ToD = Importer.Visit(FromD);
2968 if (!ToD)
2969 return 0;
2970
2971 // Record the imported declaration.
2972 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00002973
2974 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2975 // Keep track of anonymous tags that have an associated typedef.
2976 if (FromTag->getTypedefForAnonDecl())
2977 AnonTagsWithPendingTypedefs.push_back(FromTag);
2978 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2979 // When we've finished transforming a typedef, see whether it was the
2980 // typedef for an anonymous tag.
2981 for (llvm::SmallVector<TagDecl *, 4>::iterator
2982 FromTag = AnonTagsWithPendingTypedefs.begin(),
2983 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2984 FromTag != FromTagEnd; ++FromTag) {
2985 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2986 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2987 // We found the typedef for an anonymous tag; link them.
2988 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2989 AnonTagsWithPendingTypedefs.erase(FromTag);
2990 break;
2991 }
2992 }
2993 }
2994 }
2995
Douglas Gregor9bed8792010-02-09 19:21:46 +00002996 return ToD;
2997}
2998
2999DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3000 if (!FromDC)
3001 return FromDC;
3002
3003 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3004}
3005
3006Expr *ASTImporter::Import(Expr *FromE) {
3007 if (!FromE)
3008 return 0;
3009
3010 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3011}
3012
3013Stmt *ASTImporter::Import(Stmt *FromS) {
3014 if (!FromS)
3015 return 0;
3016
Douglas Gregor4800d952010-02-11 19:21:55 +00003017 // Check whether we've already imported this declaration.
3018 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3019 if (Pos != ImportedStmts.end())
3020 return Pos->second;
3021
3022 // Import the type
3023 ASTNodeImporter Importer(*this);
3024 Stmt *ToS = Importer.Visit(FromS);
3025 if (!ToS)
3026 return 0;
3027
3028 // Record the imported declaration.
3029 ImportedStmts[FromS] = ToS;
3030 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003031}
3032
3033NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3034 if (!FromNNS)
3035 return 0;
3036
3037 // FIXME: Implement!
3038 return 0;
3039}
3040
3041SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3042 if (FromLoc.isInvalid())
3043 return SourceLocation();
3044
Douglas Gregor88523732010-02-10 00:15:17 +00003045 SourceManager &FromSM = FromContext.getSourceManager();
3046
3047 // For now, map everything down to its spelling location, so that we
3048 // don't have to import macro instantiations.
3049 // FIXME: Import macro instantiations!
3050 FromLoc = FromSM.getSpellingLoc(FromLoc);
3051 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3052 SourceManager &ToSM = ToContext.getSourceManager();
3053 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3054 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00003055}
3056
3057SourceRange ASTImporter::Import(SourceRange FromRange) {
3058 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3059}
3060
Douglas Gregor88523732010-02-10 00:15:17 +00003061FileID ASTImporter::Import(FileID FromID) {
3062 llvm::DenseMap<unsigned, FileID>::iterator Pos
3063 = ImportedFileIDs.find(FromID.getHashValue());
3064 if (Pos != ImportedFileIDs.end())
3065 return Pos->second;
3066
3067 SourceManager &FromSM = FromContext.getSourceManager();
3068 SourceManager &ToSM = ToContext.getSourceManager();
3069 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3070 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3071
3072 // Include location of this file.
3073 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3074
3075 // Map the FileID for to the "to" source manager.
3076 FileID ToID;
3077 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3078 if (Cache->Entry) {
3079 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3080 // disk again
3081 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3082 // than mmap the files several times.
3083 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3084 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3085 FromSLoc.getFile().getFileCharacteristic());
3086 } else {
3087 // FIXME: We want to re-use the existing MemoryBuffer!
Chris Lattnere127a0d2010-04-20 20:35:58 +00003088 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00003089 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00003090 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00003091 FromBuf->getBufferIdentifier());
3092 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3093 }
3094
3095
3096 ImportedFileIDs[FromID.getHashValue()] = ToID;
3097 return ToID;
3098}
3099
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003100DeclarationName ASTImporter::Import(DeclarationName FromName) {
3101 if (!FromName)
3102 return DeclarationName();
3103
3104 switch (FromName.getNameKind()) {
3105 case DeclarationName::Identifier:
3106 return Import(FromName.getAsIdentifierInfo());
3107
3108 case DeclarationName::ObjCZeroArgSelector:
3109 case DeclarationName::ObjCOneArgSelector:
3110 case DeclarationName::ObjCMultiArgSelector:
3111 return Import(FromName.getObjCSelector());
3112
3113 case DeclarationName::CXXConstructorName: {
3114 QualType T = Import(FromName.getCXXNameType());
3115 if (T.isNull())
3116 return DeclarationName();
3117
3118 return ToContext.DeclarationNames.getCXXConstructorName(
3119 ToContext.getCanonicalType(T));
3120 }
3121
3122 case DeclarationName::CXXDestructorName: {
3123 QualType T = Import(FromName.getCXXNameType());
3124 if (T.isNull())
3125 return DeclarationName();
3126
3127 return ToContext.DeclarationNames.getCXXDestructorName(
3128 ToContext.getCanonicalType(T));
3129 }
3130
3131 case DeclarationName::CXXConversionFunctionName: {
3132 QualType T = Import(FromName.getCXXNameType());
3133 if (T.isNull())
3134 return DeclarationName();
3135
3136 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3137 ToContext.getCanonicalType(T));
3138 }
3139
3140 case DeclarationName::CXXOperatorName:
3141 return ToContext.DeclarationNames.getCXXOperatorName(
3142 FromName.getCXXOverloadedOperator());
3143
3144 case DeclarationName::CXXLiteralOperatorName:
3145 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3146 Import(FromName.getCXXLiteralIdentifier()));
3147
3148 case DeclarationName::CXXUsingDirective:
3149 // FIXME: STATICS!
3150 return DeclarationName::getUsingDirectiveName();
3151 }
3152
3153 // Silence bogus GCC warning
3154 return DeclarationName();
3155}
3156
3157IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3158 if (!FromId)
3159 return 0;
3160
3161 return &ToContext.Idents.get(FromId->getName());
3162}
Douglas Gregor089459a2010-02-08 21:09:39 +00003163
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003164Selector ASTImporter::Import(Selector FromSel) {
3165 if (FromSel.isNull())
3166 return Selector();
3167
3168 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3169 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3170 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3171 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3172 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3173}
3174
Douglas Gregor089459a2010-02-08 21:09:39 +00003175DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3176 DeclContext *DC,
3177 unsigned IDNS,
3178 NamedDecl **Decls,
3179 unsigned NumDecls) {
3180 return Name;
3181}
3182
3183DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003184 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
3185 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003186}
3187
3188DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003189 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
3190 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003191}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003192
3193Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3194 ImportedDecls[From] = To;
3195 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00003196}
Douglas Gregorea35d112010-02-15 23:54:17 +00003197
3198bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3199 llvm::DenseMap<Type *, Type *>::iterator Pos
3200 = ImportedTypes.find(From.getTypePtr());
3201 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3202 return true;
3203
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003204 StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +00003205 NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003206 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00003207}