blob: e1c2abd2510105928369fd1627d117f5029fd3c0 [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
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
Douglas Gregor1b2949d2010-02-05 17:54:41 +000077 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCallc12c5bb2010-05-15 11:32:37 +000078 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000079 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregor083a8212010-02-21 18:24:45 +000085 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregor96a01b42010-02-11 00:48:18 +000086 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000087 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000088 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +000089 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor9e5d9962010-02-10 21:10:29 +000090 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +000091 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000092 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +000093 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +000094 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +000095 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
96 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
97 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
98 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +000099 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000100 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000101 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000102 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000103 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000104 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000105 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000106 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000107 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000108 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000109 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000110 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
111
Douglas Gregor4800d952010-02-11 19:21:55 +0000112 // Importing statements
113 Stmt *VisitStmt(Stmt *S);
114
115 // Importing expressions
116 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000117 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000118 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000119 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000120 Expr *VisitParenExpr(ParenExpr *E);
121 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorbd249a52010-02-19 01:24:23 +0000122 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000123 Expr *VisitBinaryOperator(BinaryOperator *E);
124 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000125 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000126 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000127 };
128}
129
130//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000131// Structural Equivalence
132//----------------------------------------------------------------------------
133
134namespace {
135 struct StructuralEquivalenceContext {
136 /// \brief AST contexts for which we are checking structural equivalence.
137 ASTContext &C1, &C2;
138
139 /// \brief Diagnostic object used to emit diagnostics.
140 Diagnostic &Diags;
141
142 /// \brief The set of "tentative" equivalences between two canonical
143 /// declarations, mapping from a declaration in the first context to the
144 /// declaration in the second context that we believe to be equivalent.
145 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
146
147 /// \brief Queue of declarations in the first context whose equivalence
148 /// with a declaration in the second context still needs to be verified.
149 std::deque<Decl *> DeclsToCheck;
150
Douglas Gregorea35d112010-02-15 23:54:17 +0000151 /// \brief Declaration (from, to) pairs that are known not to be equivalent
152 /// (which we have already complained about).
153 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
154
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000155 /// \brief Whether we're being strict about the spelling of types when
156 /// unifying two types.
157 bool StrictTypeSpelling;
158
159 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
160 Diagnostic &Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +0000161 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000162 bool StrictTypeSpelling = false)
Douglas Gregorea35d112010-02-15 23:54:17 +0000163 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
164 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000165
166 /// \brief Determine whether the two declarations are structurally
167 /// equivalent.
168 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
169
170 /// \brief Determine whether the two types are structurally equivalent.
171 bool IsStructurallyEquivalent(QualType T1, QualType T2);
172
173 private:
174 /// \brief Finish checking all of the structural equivalences.
175 ///
176 /// \returns true if an error occurred, false otherwise.
177 bool Finish();
178
179 public:
180 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
181 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
182 }
183
184 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
185 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
186 }
187 };
188}
189
190static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
191 QualType T1, QualType T2);
192static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
193 Decl *D1, Decl *D2);
194
195/// \brief Determine if two APInts have the same value, after zero-extending
196/// one of them (if needed!) to ensure that the bit-widths match.
197static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
198 if (I1.getBitWidth() == I2.getBitWidth())
199 return I1 == I2;
200
201 if (I1.getBitWidth() > I2.getBitWidth())
202 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
203
204 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
205}
206
207/// \brief Determine if two APSInts have the same value, zero- or sign-extending
208/// as needed.
209static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
210 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
211 return I1 == I2;
212
213 // Check for a bit-width mismatch.
214 if (I1.getBitWidth() > I2.getBitWidth())
215 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
216 else if (I2.getBitWidth() > I1.getBitWidth())
217 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
218
219 // We have a signedness mismatch. Turn the signed value into an unsigned
220 // value.
221 if (I1.isSigned()) {
222 if (I1.isNegative())
223 return false;
224
225 return llvm::APSInt(I1, true) == I2;
226 }
227
228 if (I2.isNegative())
229 return false;
230
231 return I1 == llvm::APSInt(I2, true);
232}
233
234/// \brief Determine structural equivalence of two expressions.
235static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
236 Expr *E1, Expr *E2) {
237 if (!E1 || !E2)
238 return E1 == E2;
239
240 // FIXME: Actually perform a structural comparison!
241 return true;
242}
243
244/// \brief Determine whether two identifiers are equivalent.
245static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
246 const IdentifierInfo *Name2) {
247 if (!Name1 || !Name2)
248 return Name1 == Name2;
249
250 return Name1->getName() == Name2->getName();
251}
252
253/// \brief Determine whether two nested-name-specifiers are equivalent.
254static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
255 NestedNameSpecifier *NNS1,
256 NestedNameSpecifier *NNS2) {
257 // FIXME: Implement!
258 return true;
259}
260
261/// \brief Determine whether two template arguments are equivalent.
262static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
263 const TemplateArgument &Arg1,
264 const TemplateArgument &Arg2) {
265 // FIXME: Implement!
266 return true;
267}
268
269/// \brief Determine structural equivalence for the common part of array
270/// types.
271static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
272 const ArrayType *Array1,
273 const ArrayType *Array2) {
274 if (!IsStructurallyEquivalent(Context,
275 Array1->getElementType(),
276 Array2->getElementType()))
277 return false;
278 if (Array1->getSizeModifier() != Array2->getSizeModifier())
279 return false;
280 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
281 return false;
282
283 return true;
284}
285
286/// \brief Determine structural equivalence of two types.
287static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
288 QualType T1, QualType T2) {
289 if (T1.isNull() || T2.isNull())
290 return T1.isNull() && T2.isNull();
291
292 if (!Context.StrictTypeSpelling) {
293 // We aren't being strict about token-to-token equivalence of types,
294 // so map down to the canonical type.
295 T1 = Context.C1.getCanonicalType(T1);
296 T2 = Context.C2.getCanonicalType(T2);
297 }
298
299 if (T1.getQualifiers() != T2.getQualifiers())
300 return false;
301
Douglas Gregorea35d112010-02-15 23:54:17 +0000302 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000303
Douglas Gregorea35d112010-02-15 23:54:17 +0000304 if (T1->getTypeClass() != T2->getTypeClass()) {
305 // Compare function types with prototypes vs. without prototypes as if
306 // both did not have prototypes.
307 if (T1->getTypeClass() == Type::FunctionProto &&
308 T2->getTypeClass() == Type::FunctionNoProto)
309 TC = Type::FunctionNoProto;
310 else if (T1->getTypeClass() == Type::FunctionNoProto &&
311 T2->getTypeClass() == Type::FunctionProto)
312 TC = Type::FunctionNoProto;
313 else
314 return false;
315 }
316
317 switch (TC) {
318 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000319 // FIXME: Deal with Char_S/Char_U.
320 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
321 return false;
322 break;
323
324 case Type::Complex:
325 if (!IsStructurallyEquivalent(Context,
326 cast<ComplexType>(T1)->getElementType(),
327 cast<ComplexType>(T2)->getElementType()))
328 return false;
329 break;
330
331 case Type::Pointer:
332 if (!IsStructurallyEquivalent(Context,
333 cast<PointerType>(T1)->getPointeeType(),
334 cast<PointerType>(T2)->getPointeeType()))
335 return false;
336 break;
337
338 case Type::BlockPointer:
339 if (!IsStructurallyEquivalent(Context,
340 cast<BlockPointerType>(T1)->getPointeeType(),
341 cast<BlockPointerType>(T2)->getPointeeType()))
342 return false;
343 break;
344
345 case Type::LValueReference:
346 case Type::RValueReference: {
347 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
348 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
349 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
350 return false;
351 if (Ref1->isInnerRef() != Ref2->isInnerRef())
352 return false;
353 if (!IsStructurallyEquivalent(Context,
354 Ref1->getPointeeTypeAsWritten(),
355 Ref2->getPointeeTypeAsWritten()))
356 return false;
357 break;
358 }
359
360 case Type::MemberPointer: {
361 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
362 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
363 if (!IsStructurallyEquivalent(Context,
364 MemPtr1->getPointeeType(),
365 MemPtr2->getPointeeType()))
366 return false;
367 if (!IsStructurallyEquivalent(Context,
368 QualType(MemPtr1->getClass(), 0),
369 QualType(MemPtr2->getClass(), 0)))
370 return false;
371 break;
372 }
373
374 case Type::ConstantArray: {
375 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
376 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
377 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
378 return false;
379
380 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
381 return false;
382 break;
383 }
384
385 case Type::IncompleteArray:
386 if (!IsArrayStructurallyEquivalent(Context,
387 cast<ArrayType>(T1),
388 cast<ArrayType>(T2)))
389 return false;
390 break;
391
392 case Type::VariableArray: {
393 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
394 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
395 if (!IsStructurallyEquivalent(Context,
396 Array1->getSizeExpr(), Array2->getSizeExpr()))
397 return false;
398
399 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
400 return false;
401
402 break;
403 }
404
405 case Type::DependentSizedArray: {
406 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
407 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
408 if (!IsStructurallyEquivalent(Context,
409 Array1->getSizeExpr(), Array2->getSizeExpr()))
410 return false;
411
412 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
413 return false;
414
415 break;
416 }
417
418 case Type::DependentSizedExtVector: {
419 const DependentSizedExtVectorType *Vec1
420 = cast<DependentSizedExtVectorType>(T1);
421 const DependentSizedExtVectorType *Vec2
422 = cast<DependentSizedExtVectorType>(T2);
423 if (!IsStructurallyEquivalent(Context,
424 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
425 return false;
426 if (!IsStructurallyEquivalent(Context,
427 Vec1->getElementType(),
428 Vec2->getElementType()))
429 return false;
430 break;
431 }
432
433 case Type::Vector:
434 case Type::ExtVector: {
435 const VectorType *Vec1 = cast<VectorType>(T1);
436 const VectorType *Vec2 = cast<VectorType>(T2);
437 if (!IsStructurallyEquivalent(Context,
438 Vec1->getElementType(),
439 Vec2->getElementType()))
440 return false;
441 if (Vec1->getNumElements() != Vec2->getNumElements())
442 return false;
443 if (Vec1->isAltiVec() != Vec2->isAltiVec())
444 return false;
445 if (Vec1->isPixel() != Vec2->isPixel())
446 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000447 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000448 }
449
450 case Type::FunctionProto: {
451 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
452 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
453 if (Proto1->getNumArgs() != Proto2->getNumArgs())
454 return false;
455 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
456 if (!IsStructurallyEquivalent(Context,
457 Proto1->getArgType(I),
458 Proto2->getArgType(I)))
459 return false;
460 }
461 if (Proto1->isVariadic() != Proto2->isVariadic())
462 return false;
463 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
464 return false;
465 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
466 return false;
467 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
468 return false;
469 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
470 if (!IsStructurallyEquivalent(Context,
471 Proto1->getExceptionType(I),
472 Proto2->getExceptionType(I)))
473 return false;
474 }
475 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
476 return false;
477
478 // Fall through to check the bits common with FunctionNoProtoType.
479 }
480
481 case Type::FunctionNoProto: {
482 const FunctionType *Function1 = cast<FunctionType>(T1);
483 const FunctionType *Function2 = cast<FunctionType>(T2);
484 if (!IsStructurallyEquivalent(Context,
485 Function1->getResultType(),
486 Function2->getResultType()))
487 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000488 if (Function1->getExtInfo() != Function2->getExtInfo())
489 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000490 break;
491 }
492
493 case Type::UnresolvedUsing:
494 if (!IsStructurallyEquivalent(Context,
495 cast<UnresolvedUsingType>(T1)->getDecl(),
496 cast<UnresolvedUsingType>(T2)->getDecl()))
497 return false;
498
499 break;
500
501 case Type::Typedef:
502 if (!IsStructurallyEquivalent(Context,
503 cast<TypedefType>(T1)->getDecl(),
504 cast<TypedefType>(T2)->getDecl()))
505 return false;
506 break;
507
508 case Type::TypeOfExpr:
509 if (!IsStructurallyEquivalent(Context,
510 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
511 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
512 return false;
513 break;
514
515 case Type::TypeOf:
516 if (!IsStructurallyEquivalent(Context,
517 cast<TypeOfType>(T1)->getUnderlyingType(),
518 cast<TypeOfType>(T2)->getUnderlyingType()))
519 return false;
520 break;
521
522 case Type::Decltype:
523 if (!IsStructurallyEquivalent(Context,
524 cast<DecltypeType>(T1)->getUnderlyingExpr(),
525 cast<DecltypeType>(T2)->getUnderlyingExpr()))
526 return false;
527 break;
528
529 case Type::Record:
530 case Type::Enum:
531 if (!IsStructurallyEquivalent(Context,
532 cast<TagType>(T1)->getDecl(),
533 cast<TagType>(T2)->getDecl()))
534 return false;
535 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000536
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000537 case Type::TemplateTypeParm: {
538 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
539 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
540 if (Parm1->getDepth() != Parm2->getDepth())
541 return false;
542 if (Parm1->getIndex() != Parm2->getIndex())
543 return false;
544 if (Parm1->isParameterPack() != Parm2->isParameterPack())
545 return false;
546
547 // Names of template type parameters are never significant.
548 break;
549 }
550
551 case Type::SubstTemplateTypeParm: {
552 const SubstTemplateTypeParmType *Subst1
553 = cast<SubstTemplateTypeParmType>(T1);
554 const SubstTemplateTypeParmType *Subst2
555 = cast<SubstTemplateTypeParmType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 QualType(Subst1->getReplacedParameter(), 0),
558 QualType(Subst2->getReplacedParameter(), 0)))
559 return false;
560 if (!IsStructurallyEquivalent(Context,
561 Subst1->getReplacementType(),
562 Subst2->getReplacementType()))
563 return false;
564 break;
565 }
566
567 case Type::TemplateSpecialization: {
568 const TemplateSpecializationType *Spec1
569 = cast<TemplateSpecializationType>(T1);
570 const TemplateSpecializationType *Spec2
571 = cast<TemplateSpecializationType>(T2);
572 if (!IsStructurallyEquivalent(Context,
573 Spec1->getTemplateName(),
574 Spec2->getTemplateName()))
575 return false;
576 if (Spec1->getNumArgs() != Spec2->getNumArgs())
577 return false;
578 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
579 if (!IsStructurallyEquivalent(Context,
580 Spec1->getArg(I), Spec2->getArg(I)))
581 return false;
582 }
583 break;
584 }
585
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000586 case Type::Elaborated: {
587 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
588 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
589 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
590 if (Elab1->getKeyword() != Elab2->getKeyword())
591 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000592 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000593 Elab1->getQualifier(),
594 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000595 return false;
596 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000597 Elab1->getNamedType(),
598 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000599 return false;
600 break;
601 }
602
John McCall3cb0ebd2010-03-10 03:28:59 +0000603 case Type::InjectedClassName: {
604 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
605 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
606 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000607 Inj1->getInjectedSpecializationType(),
608 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000609 return false;
610 break;
611 }
612
Douglas Gregor4714c122010-03-31 17:34:00 +0000613 case Type::DependentName: {
614 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
615 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000616 if (!IsStructurallyEquivalent(Context,
617 Typename1->getQualifier(),
618 Typename2->getQualifier()))
619 return false;
620 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
621 Typename2->getIdentifier()))
622 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000623
624 break;
625 }
626
John McCall33500952010-06-11 00:33:02 +0000627 case Type::DependentTemplateSpecialization: {
628 const DependentTemplateSpecializationType *Spec1 =
629 cast<DependentTemplateSpecializationType>(T1);
630 const DependentTemplateSpecializationType *Spec2 =
631 cast<DependentTemplateSpecializationType>(T2);
632 if (!IsStructurallyEquivalent(Context,
633 Spec1->getQualifier(),
634 Spec2->getQualifier()))
635 return false;
636 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
637 Spec2->getIdentifier()))
638 return false;
639 if (Spec1->getNumArgs() != Spec2->getNumArgs())
640 return false;
641 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
642 if (!IsStructurallyEquivalent(Context,
643 Spec1->getArg(I), Spec2->getArg(I)))
644 return false;
645 }
646 break;
647 }
648
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000649 case Type::ObjCInterface: {
650 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
651 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
652 if (!IsStructurallyEquivalent(Context,
653 Iface1->getDecl(), Iface2->getDecl()))
654 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000655 break;
656 }
657
658 case Type::ObjCObject: {
659 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
660 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
661 if (!IsStructurallyEquivalent(Context,
662 Obj1->getBaseType(),
663 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000664 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000665 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
666 return false;
667 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000668 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000669 Obj1->getProtocol(I),
670 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000671 return false;
672 }
673 break;
674 }
675
676 case Type::ObjCObjectPointer: {
677 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
678 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
679 if (!IsStructurallyEquivalent(Context,
680 Ptr1->getPointeeType(),
681 Ptr2->getPointeeType()))
682 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000683 break;
684 }
685
686 } // end switch
687
688 return true;
689}
690
691/// \brief Determine structural equivalence of two records.
692static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
693 RecordDecl *D1, RecordDecl *D2) {
694 if (D1->isUnion() != D2->isUnion()) {
695 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
696 << Context.C2.getTypeDeclType(D2);
697 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
698 << D1->getDeclName() << (unsigned)D1->getTagKind();
699 return false;
700 }
701
Douglas Gregorea35d112010-02-15 23:54:17 +0000702 // Compare the definitions of these two records. If either or both are
703 // incomplete, we assume that they are equivalent.
704 D1 = D1->getDefinition();
705 D2 = D2->getDefinition();
706 if (!D1 || !D2)
707 return true;
708
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000709 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
710 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
711 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
712 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
713 << Context.C2.getTypeDeclType(D2);
714 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
715 << D2CXX->getNumBases();
716 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
717 << D1CXX->getNumBases();
718 return false;
719 }
720
721 // Check the base classes.
722 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
723 BaseEnd1 = D1CXX->bases_end(),
724 Base2 = D2CXX->bases_begin();
725 Base1 != BaseEnd1;
726 ++Base1, ++Base2) {
727 if (!IsStructurallyEquivalent(Context,
728 Base1->getType(), Base2->getType())) {
729 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
730 << Context.C2.getTypeDeclType(D2);
731 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
732 << Base2->getType()
733 << Base2->getSourceRange();
734 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
735 << Base1->getType()
736 << Base1->getSourceRange();
737 return false;
738 }
739
740 // Check virtual vs. non-virtual inheritance mismatch.
741 if (Base1->isVirtual() != Base2->isVirtual()) {
742 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
743 << Context.C2.getTypeDeclType(D2);
744 Context.Diag2(Base2->getSourceRange().getBegin(),
745 diag::note_odr_virtual_base)
746 << Base2->isVirtual() << Base2->getSourceRange();
747 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
748 << Base1->isVirtual()
749 << Base1->getSourceRange();
750 return false;
751 }
752 }
753 } else if (D1CXX->getNumBases() > 0) {
754 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
755 << Context.C2.getTypeDeclType(D2);
756 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
757 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
758 << Base1->getType()
759 << Base1->getSourceRange();
760 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
761 return false;
762 }
763 }
764
765 // Check the fields for consistency.
766 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
767 Field2End = D2->field_end();
768 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
769 Field1End = D1->field_end();
770 Field1 != Field1End;
771 ++Field1, ++Field2) {
772 if (Field2 == Field2End) {
773 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
774 << Context.C2.getTypeDeclType(D2);
775 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
776 << Field1->getDeclName() << Field1->getType();
777 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
778 return false;
779 }
780
781 if (!IsStructurallyEquivalent(Context,
782 Field1->getType(), Field2->getType())) {
783 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
784 << Context.C2.getTypeDeclType(D2);
785 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
786 << Field2->getDeclName() << Field2->getType();
787 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
788 << Field1->getDeclName() << Field1->getType();
789 return false;
790 }
791
792 if (Field1->isBitField() != Field2->isBitField()) {
793 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
794 << Context.C2.getTypeDeclType(D2);
795 if (Field1->isBitField()) {
796 llvm::APSInt Bits;
797 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
798 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
799 << Field1->getDeclName() << Field1->getType()
800 << Bits.toString(10, false);
801 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
802 << Field2->getDeclName();
803 } else {
804 llvm::APSInt Bits;
805 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
806 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
807 << Field2->getDeclName() << Field2->getType()
808 << Bits.toString(10, false);
809 Context.Diag1(Field1->getLocation(),
810 diag::note_odr_not_bit_field)
811 << Field1->getDeclName();
812 }
813 return false;
814 }
815
816 if (Field1->isBitField()) {
817 // Make sure that the bit-fields are the same length.
818 llvm::APSInt Bits1, Bits2;
819 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
820 return false;
821 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
822 return false;
823
824 if (!IsSameValue(Bits1, Bits2)) {
825 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
826 << Context.C2.getTypeDeclType(D2);
827 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
828 << Field2->getDeclName() << Field2->getType()
829 << Bits2.toString(10, false);
830 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
831 << Field1->getDeclName() << Field1->getType()
832 << Bits1.toString(10, false);
833 return false;
834 }
835 }
836 }
837
838 if (Field2 != Field2End) {
839 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
840 << Context.C2.getTypeDeclType(D2);
841 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
842 << Field2->getDeclName() << Field2->getType();
843 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
844 return false;
845 }
846
847 return true;
848}
849
850/// \brief Determine structural equivalence of two enums.
851static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
852 EnumDecl *D1, EnumDecl *D2) {
853 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
854 EC2End = D2->enumerator_end();
855 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
856 EC1End = D1->enumerator_end();
857 EC1 != EC1End; ++EC1, ++EC2) {
858 if (EC2 == EC2End) {
859 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
860 << Context.C2.getTypeDeclType(D2);
861 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
862 << EC1->getDeclName()
863 << EC1->getInitVal().toString(10);
864 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
865 return false;
866 }
867
868 llvm::APSInt Val1 = EC1->getInitVal();
869 llvm::APSInt Val2 = EC2->getInitVal();
870 if (!IsSameValue(Val1, Val2) ||
871 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
872 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
873 << Context.C2.getTypeDeclType(D2);
874 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
875 << EC2->getDeclName()
876 << EC2->getInitVal().toString(10);
877 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
878 << EC1->getDeclName()
879 << EC1->getInitVal().toString(10);
880 return false;
881 }
882 }
883
884 if (EC2 != EC2End) {
885 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
886 << Context.C2.getTypeDeclType(D2);
887 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
888 << EC2->getDeclName()
889 << EC2->getInitVal().toString(10);
890 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
891 return false;
892 }
893
894 return true;
895}
896
897/// \brief Determine structural equivalence of two declarations.
898static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
899 Decl *D1, Decl *D2) {
900 // FIXME: Check for known structural equivalences via a callback of some sort.
901
Douglas Gregorea35d112010-02-15 23:54:17 +0000902 // Check whether we already know that these two declarations are not
903 // structurally equivalent.
904 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
905 D2->getCanonicalDecl())))
906 return false;
907
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000908 // Determine whether we've already produced a tentative equivalence for D1.
909 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
910 if (EquivToD1)
911 return EquivToD1 == D2->getCanonicalDecl();
912
913 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
914 EquivToD1 = D2->getCanonicalDecl();
915 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
916 return true;
917}
918
919bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
920 Decl *D2) {
921 if (!::IsStructurallyEquivalent(*this, D1, D2))
922 return false;
923
924 return !Finish();
925}
926
927bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
928 QualType T2) {
929 if (!::IsStructurallyEquivalent(*this, T1, T2))
930 return false;
931
932 return !Finish();
933}
934
935bool StructuralEquivalenceContext::Finish() {
936 while (!DeclsToCheck.empty()) {
937 // Check the next declaration.
938 Decl *D1 = DeclsToCheck.front();
939 DeclsToCheck.pop_front();
940
941 Decl *D2 = TentativeEquivalences[D1];
942 assert(D2 && "Unrecorded tentative equivalence?");
943
Douglas Gregorea35d112010-02-15 23:54:17 +0000944 bool Equivalent = true;
945
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000946 // FIXME: Switch on all declaration kinds. For now, we're just going to
947 // check the obvious ones.
948 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
949 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
950 // Check for equivalent structure names.
951 IdentifierInfo *Name1 = Record1->getIdentifier();
952 if (!Name1 && Record1->getTypedefForAnonDecl())
953 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
954 IdentifierInfo *Name2 = Record2->getIdentifier();
955 if (!Name2 && Record2->getTypedefForAnonDecl())
956 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000957 if (!::IsStructurallyEquivalent(Name1, Name2) ||
958 !::IsStructurallyEquivalent(*this, Record1, Record2))
959 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000960 } else {
961 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000962 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000963 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000964 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000965 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
966 // Check for equivalent enum names.
967 IdentifierInfo *Name1 = Enum1->getIdentifier();
968 if (!Name1 && Enum1->getTypedefForAnonDecl())
969 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
970 IdentifierInfo *Name2 = Enum2->getIdentifier();
971 if (!Name2 && Enum2->getTypedefForAnonDecl())
972 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000973 if (!::IsStructurallyEquivalent(Name1, Name2) ||
974 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
975 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000976 } else {
977 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +0000978 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000979 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000980 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000981 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
982 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +0000983 Typedef2->getIdentifier()) ||
984 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000985 Typedef1->getUnderlyingType(),
986 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +0000987 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000988 } else {
989 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000990 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000991 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000992 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000993
994 if (!Equivalent) {
995 // Note that these two declarations are not equivalent (and we already
996 // know about it).
997 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
998 D2->getCanonicalDecl()));
999 return true;
1000 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001001 // FIXME: Check other declaration kinds!
1002 }
1003
1004 return false;
1005}
1006
1007//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001008// Import Types
1009//----------------------------------------------------------------------------
1010
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001011QualType ASTNodeImporter::VisitType(Type *T) {
1012 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1013 << T->getTypeClassName();
1014 return QualType();
1015}
1016
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001017QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1018 switch (T->getKind()) {
1019 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1020 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1021
1022 case BuiltinType::Char_U:
1023 // The context we're importing from has an unsigned 'char'. If we're
1024 // importing into a context with a signed 'char', translate to
1025 // 'unsigned char' instead.
1026 if (Importer.getToContext().getLangOptions().CharIsSigned)
1027 return Importer.getToContext().UnsignedCharTy;
1028
1029 return Importer.getToContext().CharTy;
1030
1031 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1032
1033 case BuiltinType::Char16:
1034 // FIXME: Make sure that the "to" context supports C++!
1035 return Importer.getToContext().Char16Ty;
1036
1037 case BuiltinType::Char32:
1038 // FIXME: Make sure that the "to" context supports C++!
1039 return Importer.getToContext().Char32Ty;
1040
1041 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1042 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1043 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1044 case BuiltinType::ULongLong:
1045 return Importer.getToContext().UnsignedLongLongTy;
1046 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1047
1048 case BuiltinType::Char_S:
1049 // The context we're importing from has an unsigned 'char'. If we're
1050 // importing into a context with a signed 'char', translate to
1051 // 'unsigned char' instead.
1052 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1053 return Importer.getToContext().SignedCharTy;
1054
1055 return Importer.getToContext().CharTy;
1056
1057 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1058 case BuiltinType::WChar:
1059 // FIXME: If not in C++, shall we translate to the C equivalent of
1060 // wchar_t?
1061 return Importer.getToContext().WCharTy;
1062
1063 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1064 case BuiltinType::Int : return Importer.getToContext().IntTy;
1065 case BuiltinType::Long : return Importer.getToContext().LongTy;
1066 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1067 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1068 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1069 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1070 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1071
1072 case BuiltinType::NullPtr:
1073 // FIXME: Make sure that the "to" context supports C++0x!
1074 return Importer.getToContext().NullPtrTy;
1075
1076 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1077 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1078 case BuiltinType::UndeducedAuto:
1079 // FIXME: Make sure that the "to" context supports C++0x!
1080 return Importer.getToContext().UndeducedAutoTy;
1081
1082 case BuiltinType::ObjCId:
1083 // FIXME: Make sure that the "to" context supports Objective-C!
1084 return Importer.getToContext().ObjCBuiltinIdTy;
1085
1086 case BuiltinType::ObjCClass:
1087 return Importer.getToContext().ObjCBuiltinClassTy;
1088
1089 case BuiltinType::ObjCSel:
1090 return Importer.getToContext().ObjCBuiltinSelTy;
1091 }
1092
1093 return QualType();
1094}
1095
1096QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1097 QualType ToElementType = Importer.Import(T->getElementType());
1098 if (ToElementType.isNull())
1099 return QualType();
1100
1101 return Importer.getToContext().getComplexType(ToElementType);
1102}
1103
1104QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1105 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1106 if (ToPointeeType.isNull())
1107 return QualType();
1108
1109 return Importer.getToContext().getPointerType(ToPointeeType);
1110}
1111
1112QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1113 // FIXME: Check for blocks support in "to" context.
1114 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1115 if (ToPointeeType.isNull())
1116 return QualType();
1117
1118 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1119}
1120
1121QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1122 // FIXME: Check for C++ support in "to" context.
1123 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1124 if (ToPointeeType.isNull())
1125 return QualType();
1126
1127 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1128}
1129
1130QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1131 // FIXME: Check for C++0x support in "to" context.
1132 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1133 if (ToPointeeType.isNull())
1134 return QualType();
1135
1136 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1137}
1138
1139QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1140 // FIXME: Check for C++ support in "to" context.
1141 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1142 if (ToPointeeType.isNull())
1143 return QualType();
1144
1145 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1146 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1147 ClassType.getTypePtr());
1148}
1149
1150QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1151 QualType ToElementType = Importer.Import(T->getElementType());
1152 if (ToElementType.isNull())
1153 return QualType();
1154
1155 return Importer.getToContext().getConstantArrayType(ToElementType,
1156 T->getSize(),
1157 T->getSizeModifier(),
1158 T->getIndexTypeCVRQualifiers());
1159}
1160
1161QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1162 QualType ToElementType = Importer.Import(T->getElementType());
1163 if (ToElementType.isNull())
1164 return QualType();
1165
1166 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1167 T->getSizeModifier(),
1168 T->getIndexTypeCVRQualifiers());
1169}
1170
1171QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1172 QualType ToElementType = Importer.Import(T->getElementType());
1173 if (ToElementType.isNull())
1174 return QualType();
1175
1176 Expr *Size = Importer.Import(T->getSizeExpr());
1177 if (!Size)
1178 return QualType();
1179
1180 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1181 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1182 T->getSizeModifier(),
1183 T->getIndexTypeCVRQualifiers(),
1184 Brackets);
1185}
1186
1187QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1188 QualType ToElementType = Importer.Import(T->getElementType());
1189 if (ToElementType.isNull())
1190 return QualType();
1191
1192 return Importer.getToContext().getVectorType(ToElementType,
1193 T->getNumElements(),
1194 T->isAltiVec(),
1195 T->isPixel());
1196}
1197
1198QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1199 QualType ToElementType = Importer.Import(T->getElementType());
1200 if (ToElementType.isNull())
1201 return QualType();
1202
1203 return Importer.getToContext().getExtVectorType(ToElementType,
1204 T->getNumElements());
1205}
1206
1207QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1208 // FIXME: What happens if we're importing a function without a prototype
1209 // into C++? Should we make it variadic?
1210 QualType ToResultType = Importer.Import(T->getResultType());
1211 if (ToResultType.isNull())
1212 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001213
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001214 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001215 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001216}
1217
1218QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1219 QualType ToResultType = Importer.Import(T->getResultType());
1220 if (ToResultType.isNull())
1221 return QualType();
1222
1223 // Import argument types
1224 llvm::SmallVector<QualType, 4> ArgTypes;
1225 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1226 AEnd = T->arg_type_end();
1227 A != AEnd; ++A) {
1228 QualType ArgType = Importer.Import(*A);
1229 if (ArgType.isNull())
1230 return QualType();
1231 ArgTypes.push_back(ArgType);
1232 }
1233
1234 // Import exception types
1235 llvm::SmallVector<QualType, 4> ExceptionTypes;
1236 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1237 EEnd = T->exception_end();
1238 E != EEnd; ++E) {
1239 QualType ExceptionType = Importer.Import(*E);
1240 if (ExceptionType.isNull())
1241 return QualType();
1242 ExceptionTypes.push_back(ExceptionType);
1243 }
1244
1245 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1246 ArgTypes.size(),
1247 T->isVariadic(),
1248 T->getTypeQuals(),
1249 T->hasExceptionSpec(),
1250 T->hasAnyExceptionSpec(),
1251 ExceptionTypes.size(),
1252 ExceptionTypes.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001253 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001254}
1255
1256QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1257 TypedefDecl *ToDecl
1258 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1259 if (!ToDecl)
1260 return QualType();
1261
1262 return Importer.getToContext().getTypeDeclType(ToDecl);
1263}
1264
1265QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1266 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1267 if (!ToExpr)
1268 return QualType();
1269
1270 return Importer.getToContext().getTypeOfExprType(ToExpr);
1271}
1272
1273QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1274 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1275 if (ToUnderlyingType.isNull())
1276 return QualType();
1277
1278 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1279}
1280
1281QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1282 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1283 if (!ToExpr)
1284 return QualType();
1285
1286 return Importer.getToContext().getDecltypeType(ToExpr);
1287}
1288
1289QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1290 RecordDecl *ToDecl
1291 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1292 if (!ToDecl)
1293 return QualType();
1294
1295 return Importer.getToContext().getTagDeclType(ToDecl);
1296}
1297
1298QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1299 EnumDecl *ToDecl
1300 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1301 if (!ToDecl)
1302 return QualType();
1303
1304 return Importer.getToContext().getTagDeclType(ToDecl);
1305}
1306
1307QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001308 NestedNameSpecifier *ToQualifier = 0;
1309 // Note: the qualifier in an ElaboratedType is optional.
1310 if (T->getQualifier()) {
1311 ToQualifier = Importer.Import(T->getQualifier());
1312 if (!ToQualifier)
1313 return QualType();
1314 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001315
1316 QualType ToNamedType = Importer.Import(T->getNamedType());
1317 if (ToNamedType.isNull())
1318 return QualType();
1319
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001320 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1321 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001322}
1323
1324QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1325 ObjCInterfaceDecl *Class
1326 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1327 if (!Class)
1328 return QualType();
1329
John McCallc12c5bb2010-05-15 11:32:37 +00001330 return Importer.getToContext().getObjCInterfaceType(Class);
1331}
1332
1333QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1334 QualType ToBaseType = Importer.Import(T->getBaseType());
1335 if (ToBaseType.isNull())
1336 return QualType();
1337
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001338 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001339 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001340 PEnd = T->qual_end();
1341 P != PEnd; ++P) {
1342 ObjCProtocolDecl *Protocol
1343 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1344 if (!Protocol)
1345 return QualType();
1346 Protocols.push_back(Protocol);
1347 }
1348
John McCallc12c5bb2010-05-15 11:32:37 +00001349 return Importer.getToContext().getObjCObjectType(ToBaseType,
1350 Protocols.data(),
1351 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001352}
1353
1354QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1355 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1356 if (ToPointeeType.isNull())
1357 return QualType();
1358
John McCallc12c5bb2010-05-15 11:32:37 +00001359 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001360}
1361
Douglas Gregor089459a2010-02-08 21:09:39 +00001362//----------------------------------------------------------------------------
1363// Import Declarations
1364//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001365bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1366 DeclContext *&LexicalDC,
1367 DeclarationName &Name,
1368 SourceLocation &Loc) {
1369 // Import the context of this declaration.
1370 DC = Importer.ImportContext(D->getDeclContext());
1371 if (!DC)
1372 return true;
1373
1374 LexicalDC = DC;
1375 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1376 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1377 if (!LexicalDC)
1378 return true;
1379 }
1380
1381 // Import the name of this declaration.
1382 Name = Importer.Import(D->getDeclName());
1383 if (D->getDeclName() && !Name)
1384 return true;
1385
1386 // Import the location of this declaration.
1387 Loc = Importer.Import(D->getLocation());
1388 return false;
1389}
1390
Douglas Gregor083a8212010-02-21 18:24:45 +00001391void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1392 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1393 FromEnd = FromDC->decls_end();
1394 From != FromEnd;
1395 ++From)
1396 Importer.Import(*From);
1397}
1398
Douglas Gregor96a01b42010-02-11 00:48:18 +00001399bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001400 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001401 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001402 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001403 Importer.getDiags(),
1404 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001405 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001406}
1407
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001408bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001409 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001410 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001411 Importer.getDiags(),
1412 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001413 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001414}
1415
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001416Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001417 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001418 << D->getDeclKindName();
1419 return 0;
1420}
1421
Douglas Gregor788c62d2010-02-21 18:26:36 +00001422Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1423 // Import the major distinguishing characteristics of this namespace.
1424 DeclContext *DC, *LexicalDC;
1425 DeclarationName Name;
1426 SourceLocation Loc;
1427 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1428 return 0;
1429
1430 NamespaceDecl *MergeWithNamespace = 0;
1431 if (!Name) {
1432 // This is an anonymous namespace. Adopt an existing anonymous
1433 // namespace if we can.
1434 // FIXME: Not testable.
1435 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1436 MergeWithNamespace = TU->getAnonymousNamespace();
1437 else
1438 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1439 } else {
1440 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1441 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1442 Lookup.first != Lookup.second;
1443 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001444 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001445 continue;
1446
1447 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1448 MergeWithNamespace = FoundNS;
1449 ConflictingDecls.clear();
1450 break;
1451 }
1452
1453 ConflictingDecls.push_back(*Lookup.first);
1454 }
1455
1456 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001457 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001458 ConflictingDecls.data(),
1459 ConflictingDecls.size());
1460 }
1461 }
1462
1463 // Create the "to" namespace, if needed.
1464 NamespaceDecl *ToNamespace = MergeWithNamespace;
1465 if (!ToNamespace) {
1466 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1467 Name.getAsIdentifierInfo());
1468 ToNamespace->setLexicalDeclContext(LexicalDC);
1469 LexicalDC->addDecl(ToNamespace);
1470
1471 // If this is an anonymous namespace, register it as the anonymous
1472 // namespace within its context.
1473 if (!Name) {
1474 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1475 TU->setAnonymousNamespace(ToNamespace);
1476 else
1477 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1478 }
1479 }
1480 Importer.Imported(D, ToNamespace);
1481
1482 ImportDeclContext(D);
1483
1484 return ToNamespace;
1485}
1486
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001487Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1488 // Import the major distinguishing characteristics of this typedef.
1489 DeclContext *DC, *LexicalDC;
1490 DeclarationName Name;
1491 SourceLocation Loc;
1492 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1493 return 0;
1494
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001495 // If this typedef is not in block scope, determine whether we've
1496 // seen a typedef with the same name (that we can merge with) or any
1497 // other entity by that name (which name lookup could conflict with).
1498 if (!DC->isFunctionOrMethod()) {
1499 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1500 unsigned IDNS = Decl::IDNS_Ordinary;
1501 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1502 Lookup.first != Lookup.second;
1503 ++Lookup.first) {
1504 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1505 continue;
1506 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001507 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1508 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001509 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001510 }
1511
1512 ConflictingDecls.push_back(*Lookup.first);
1513 }
1514
1515 if (!ConflictingDecls.empty()) {
1516 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1517 ConflictingDecls.data(),
1518 ConflictingDecls.size());
1519 if (!Name)
1520 return 0;
1521 }
1522 }
1523
Douglas Gregorea35d112010-02-15 23:54:17 +00001524 // Import the underlying type of this typedef;
1525 QualType T = Importer.Import(D->getUnderlyingType());
1526 if (T.isNull())
1527 return 0;
1528
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001529 // Create the new typedef node.
1530 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1531 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1532 Loc, Name.getAsIdentifierInfo(),
1533 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001534 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001535 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001536 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001537 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001538
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001539 return ToTypedef;
1540}
1541
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001542Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1543 // Import the major distinguishing characteristics of this enum.
1544 DeclContext *DC, *LexicalDC;
1545 DeclarationName Name;
1546 SourceLocation Loc;
1547 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1548 return 0;
1549
1550 // Figure out what enum name we're looking for.
1551 unsigned IDNS = Decl::IDNS_Tag;
1552 DeclarationName SearchName = Name;
1553 if (!SearchName && D->getTypedefForAnonDecl()) {
1554 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1555 IDNS = Decl::IDNS_Ordinary;
1556 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1557 IDNS |= Decl::IDNS_Ordinary;
1558
1559 // We may already have an enum of the same name; try to find and match it.
1560 if (!DC->isFunctionOrMethod() && SearchName) {
1561 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1562 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1563 Lookup.first != Lookup.second;
1564 ++Lookup.first) {
1565 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1566 continue;
1567
1568 Decl *Found = *Lookup.first;
1569 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1570 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1571 Found = Tag->getDecl();
1572 }
1573
1574 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001575 if (IsStructuralMatch(D, FoundEnum))
1576 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001577 }
1578
1579 ConflictingDecls.push_back(*Lookup.first);
1580 }
1581
1582 if (!ConflictingDecls.empty()) {
1583 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1584 ConflictingDecls.data(),
1585 ConflictingDecls.size());
1586 }
1587 }
1588
1589 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001590 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001591 Name.getAsIdentifierInfo(),
1592 Importer.Import(D->getTagKeywordLoc()),
1593 0);
John McCallb6217662010-03-15 10:12:16 +00001594 // Import the qualifier, if any.
1595 if (D->getQualifier()) {
1596 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1597 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1598 D2->setQualifierInfo(NNS, NNSRange);
1599 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001600 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001601 D2->setLexicalDeclContext(LexicalDC);
1602 Importer.Imported(D, D2);
1603 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001604
1605 // Import the integer type.
1606 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1607 if (ToIntegerType.isNull())
1608 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001609 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001610
1611 // Import the definition
1612 if (D->isDefinition()) {
1613 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1614 if (T.isNull())
1615 return 0;
1616
1617 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1618 if (ToPromotionType.isNull())
1619 return 0;
1620
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001621 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00001622 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00001623
1624 // FIXME: we might need to merge the number of positive or negative bits
1625 // if the enumerator lists don't match.
1626 D2->completeDefinition(T, ToPromotionType,
1627 D->getNumPositiveBits(),
1628 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001629 }
1630
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001631 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001632}
1633
Douglas Gregor96a01b42010-02-11 00:48:18 +00001634Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1635 // If this record has a definition in the translation unit we're coming from,
1636 // but this particular declaration is not that definition, import the
1637 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00001638 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001639 if (Definition && Definition != D) {
1640 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001641 if (!ImportedDef)
1642 return 0;
1643
1644 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001645 }
1646
1647 // Import the major distinguishing characteristics of this record.
1648 DeclContext *DC, *LexicalDC;
1649 DeclarationName Name;
1650 SourceLocation Loc;
1651 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1652 return 0;
1653
1654 // Figure out what structure name we're looking for.
1655 unsigned IDNS = Decl::IDNS_Tag;
1656 DeclarationName SearchName = Name;
1657 if (!SearchName && D->getTypedefForAnonDecl()) {
1658 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1659 IDNS = Decl::IDNS_Ordinary;
1660 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1661 IDNS |= Decl::IDNS_Ordinary;
1662
1663 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001664 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001665 if (!DC->isFunctionOrMethod() && SearchName) {
1666 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1667 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1668 Lookup.first != Lookup.second;
1669 ++Lookup.first) {
1670 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1671 continue;
1672
1673 Decl *Found = *Lookup.first;
1674 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1675 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1676 Found = Tag->getDecl();
1677 }
1678
1679 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001680 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1681 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1682 // The record types structurally match, or the "from" translation
1683 // unit only had a forward declaration anyway; call it the same
1684 // function.
1685 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001686 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001687 }
1688 } else {
1689 // We have a forward declaration of this type, so adopt that forward
1690 // declaration rather than building a new one.
1691 AdoptDecl = FoundRecord;
1692 continue;
1693 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00001694 }
1695
1696 ConflictingDecls.push_back(*Lookup.first);
1697 }
1698
1699 if (!ConflictingDecls.empty()) {
1700 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1701 ConflictingDecls.data(),
1702 ConflictingDecls.size());
1703 }
1704 }
1705
1706 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001707 RecordDecl *D2 = AdoptDecl;
1708 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00001709 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001710 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001711 D->getTagKind(),
1712 DC, Loc,
1713 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001714 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001715 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00001716 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001717 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001718 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001719 DC, Loc,
1720 Name.getAsIdentifierInfo(),
1721 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00001722 }
John McCallb6217662010-03-15 10:12:16 +00001723 // Import the qualifier, if any.
1724 if (D->getQualifier()) {
1725 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1726 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1727 D2->setQualifierInfo(NNS, NNSRange);
1728 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001729 D2->setLexicalDeclContext(LexicalDC);
1730 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001731 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001732
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001733 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001734
Douglas Gregor96a01b42010-02-11 00:48:18 +00001735 if (D->isDefinition()) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001736 D2->startDefinition();
John McCall5250f272010-06-03 19:28:45 +00001737
1738 // Add base classes.
1739 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1740 CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D);
1741
1742 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1743 for (CXXRecordDecl::base_class_iterator
1744 Base1 = D1CXX->bases_begin(),
1745 FromBaseEnd = D1CXX->bases_end();
1746 Base1 != FromBaseEnd;
1747 ++Base1) {
1748 QualType T = Importer.Import(Base1->getType());
1749 if (T.isNull())
1750 return 0;
1751
1752 Bases.push_back(
1753 new (Importer.getToContext())
1754 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1755 Base1->isVirtual(),
1756 Base1->isBaseOfClass(),
1757 Base1->getAccessSpecifierAsWritten(),
1758 T));
1759 }
1760 if (!Bases.empty())
1761 D2CXX->setBases(Bases.data(), Bases.size());
1762 }
1763
Douglas Gregor083a8212010-02-21 18:24:45 +00001764 ImportDeclContext(D);
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001765 D2->completeDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001766 }
1767
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001768 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001769}
1770
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001771Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1772 // Import the major distinguishing characteristics of this enumerator.
1773 DeclContext *DC, *LexicalDC;
1774 DeclarationName Name;
1775 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001776 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001777 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00001778
1779 QualType T = Importer.Import(D->getType());
1780 if (T.isNull())
1781 return 0;
1782
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001783 // Determine whether there are any other declarations with the same name and
1784 // in the same context.
1785 if (!LexicalDC->isFunctionOrMethod()) {
1786 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1787 unsigned IDNS = Decl::IDNS_Ordinary;
1788 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1789 Lookup.first != Lookup.second;
1790 ++Lookup.first) {
1791 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1792 continue;
1793
1794 ConflictingDecls.push_back(*Lookup.first);
1795 }
1796
1797 if (!ConflictingDecls.empty()) {
1798 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1799 ConflictingDecls.data(),
1800 ConflictingDecls.size());
1801 if (!Name)
1802 return 0;
1803 }
1804 }
1805
1806 Expr *Init = Importer.Import(D->getInitExpr());
1807 if (D->getInitExpr() && !Init)
1808 return 0;
1809
1810 EnumConstantDecl *ToEnumerator
1811 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1812 Name.getAsIdentifierInfo(), T,
1813 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00001814 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001815 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001816 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001817 LexicalDC->addDecl(ToEnumerator);
1818 return ToEnumerator;
1819}
Douglas Gregor96a01b42010-02-11 00:48:18 +00001820
Douglas Gregora404ea62010-02-10 19:54:31 +00001821Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1822 // Import the major distinguishing characteristics of this function.
1823 DeclContext *DC, *LexicalDC;
1824 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00001825 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001826 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00001827 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001828
1829 // Try to find a function in our own ("to") context with the same name, same
1830 // type, and in the same context as the function we're importing.
1831 if (!LexicalDC->isFunctionOrMethod()) {
1832 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1833 unsigned IDNS = Decl::IDNS_Ordinary;
1834 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1835 Lookup.first != Lookup.second;
1836 ++Lookup.first) {
1837 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1838 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00001839
Douglas Gregora404ea62010-02-10 19:54:31 +00001840 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1841 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1842 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001843 if (Importer.IsStructurallyEquivalent(D->getType(),
1844 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00001845 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001846 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00001847 }
1848
1849 // FIXME: Check for overloading more carefully, e.g., by boosting
1850 // Sema::IsOverload out to the AST library.
1851
1852 // Function overloading is okay in C++.
1853 if (Importer.getToContext().getLangOptions().CPlusPlus)
1854 continue;
1855
1856 // Complain about inconsistent function types.
1857 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00001858 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00001859 Importer.ToDiag(FoundFunction->getLocation(),
1860 diag::note_odr_value_here)
1861 << FoundFunction->getType();
1862 }
1863 }
1864
1865 ConflictingDecls.push_back(*Lookup.first);
1866 }
1867
1868 if (!ConflictingDecls.empty()) {
1869 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1870 ConflictingDecls.data(),
1871 ConflictingDecls.size());
1872 if (!Name)
1873 return 0;
1874 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00001875 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001876
1877 // Import the type.
1878 QualType T = Importer.Import(D->getType());
1879 if (T.isNull())
1880 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001881
1882 // Import the function parameters.
1883 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1884 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1885 P != PEnd; ++P) {
1886 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1887 if (!ToP)
1888 return 0;
1889
1890 Parameters.push_back(ToP);
1891 }
1892
1893 // Create the imported function.
1894 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00001895 FunctionDecl *ToFunction = 0;
1896 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1897 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1898 cast<CXXRecordDecl>(DC),
1899 Loc, Name, T, TInfo,
1900 FromConstructor->isExplicit(),
1901 D->isInlineSpecified(),
1902 D->isImplicit());
1903 } else if (isa<CXXDestructorDecl>(D)) {
1904 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1905 cast<CXXRecordDecl>(DC),
1906 Loc, Name, T,
1907 D->isInlineSpecified(),
1908 D->isImplicit());
1909 } else if (CXXConversionDecl *FromConversion
1910 = dyn_cast<CXXConversionDecl>(D)) {
1911 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1912 cast<CXXRecordDecl>(DC),
1913 Loc, Name, T, TInfo,
1914 D->isInlineSpecified(),
1915 FromConversion->isExplicit());
1916 } else {
1917 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1918 Name, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001919 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00001920 D->isInlineSpecified(),
1921 D->hasWrittenPrototype());
1922 }
John McCallb6217662010-03-15 10:12:16 +00001923
1924 // Import the qualifier, if any.
1925 if (D->getQualifier()) {
1926 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1927 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1928 ToFunction->setQualifierInfo(NNS, NNSRange);
1929 }
Douglas Gregor325bf172010-02-22 17:42:47 +00001930 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001931 ToFunction->setLexicalDeclContext(LexicalDC);
1932 Importer.Imported(D, ToFunction);
1933 LexicalDC->addDecl(ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001934
Douglas Gregora404ea62010-02-10 19:54:31 +00001935 // Set the parameters.
1936 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001937 Parameters[I]->setOwningFunction(ToFunction);
1938 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00001939 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001940 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00001941
1942 // FIXME: Other bits to merge?
Douglas Gregor089459a2010-02-08 21:09:39 +00001943
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00001944 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00001945}
1946
Douglas Gregorc144f352010-02-21 18:29:16 +00001947Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1948 return VisitFunctionDecl(D);
1949}
1950
1951Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1952 return VisitCXXMethodDecl(D);
1953}
1954
1955Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1956 return VisitCXXMethodDecl(D);
1957}
1958
1959Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1960 return VisitCXXMethodDecl(D);
1961}
1962
Douglas Gregor96a01b42010-02-11 00:48:18 +00001963Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1964 // Import the major distinguishing characteristics of a variable.
1965 DeclContext *DC, *LexicalDC;
1966 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001967 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001968 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1969 return 0;
1970
1971 // Import the type.
1972 QualType T = Importer.Import(D->getType());
1973 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00001974 return 0;
1975
1976 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1977 Expr *BitWidth = Importer.Import(D->getBitWidth());
1978 if (!BitWidth && D->getBitWidth())
1979 return 0;
1980
1981 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1982 Loc, Name.getAsIdentifierInfo(),
1983 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00001984 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001985 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001986 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001987 LexicalDC->addDecl(ToField);
1988 return ToField;
1989}
1990
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00001991Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
1992 // Import the major distinguishing characteristics of an ivar.
1993 DeclContext *DC, *LexicalDC;
1994 DeclarationName Name;
1995 SourceLocation Loc;
1996 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1997 return 0;
1998
1999 // Determine whether we've already imported this ivar
2000 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2001 Lookup.first != Lookup.second;
2002 ++Lookup.first) {
2003 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2004 if (Importer.IsStructurallyEquivalent(D->getType(),
2005 FoundIvar->getType())) {
2006 Importer.Imported(D, FoundIvar);
2007 return FoundIvar;
2008 }
2009
2010 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2011 << Name << D->getType() << FoundIvar->getType();
2012 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2013 << FoundIvar->getType();
2014 return 0;
2015 }
2016 }
2017
2018 // Import the type.
2019 QualType T = Importer.Import(D->getType());
2020 if (T.isNull())
2021 return 0;
2022
2023 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2024 Expr *BitWidth = Importer.Import(D->getBitWidth());
2025 if (!BitWidth && D->getBitWidth())
2026 return 0;
2027
Daniel Dunbara0654922010-04-02 20:10:03 +00002028 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2029 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002030 Loc, Name.getAsIdentifierInfo(),
2031 T, TInfo, D->getAccessControl(),
2032 BitWidth);
2033 ToIvar->setLexicalDeclContext(LexicalDC);
2034 Importer.Imported(D, ToIvar);
2035 LexicalDC->addDecl(ToIvar);
2036 return ToIvar;
2037
2038}
2039
Douglas Gregora404ea62010-02-10 19:54:31 +00002040Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2041 // Import the major distinguishing characteristics of a variable.
2042 DeclContext *DC, *LexicalDC;
2043 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002044 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002045 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002046 return 0;
2047
Douglas Gregor089459a2010-02-08 21:09:39 +00002048 // Try to find a variable in our own ("to") context with the same name and
2049 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002050 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002051 VarDecl *MergeWithVar = 0;
2052 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2053 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002054 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002055 Lookup.first != Lookup.second;
2056 ++Lookup.first) {
2057 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2058 continue;
2059
2060 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2061 // We have found a variable that we may need to merge with. Check it.
2062 if (isExternalLinkage(FoundVar->getLinkage()) &&
2063 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002064 if (Importer.IsStructurallyEquivalent(D->getType(),
2065 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002066 MergeWithVar = FoundVar;
2067 break;
2068 }
2069
Douglas Gregord0145422010-02-12 17:23:39 +00002070 const ArrayType *FoundArray
2071 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2072 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002073 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002074 if (FoundArray && TArray) {
2075 if (isa<IncompleteArrayType>(FoundArray) &&
2076 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002077 // Import the type.
2078 QualType T = Importer.Import(D->getType());
2079 if (T.isNull())
2080 return 0;
2081
Douglas Gregord0145422010-02-12 17:23:39 +00002082 FoundVar->setType(T);
2083 MergeWithVar = FoundVar;
2084 break;
2085 } else if (isa<IncompleteArrayType>(TArray) &&
2086 isa<ConstantArrayType>(FoundArray)) {
2087 MergeWithVar = FoundVar;
2088 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002089 }
2090 }
2091
Douglas Gregor089459a2010-02-08 21:09:39 +00002092 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002093 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002094 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2095 << FoundVar->getType();
2096 }
2097 }
2098
2099 ConflictingDecls.push_back(*Lookup.first);
2100 }
2101
2102 if (MergeWithVar) {
2103 // An equivalent variable with external linkage has been found. Link
2104 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002105 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002106
2107 if (VarDecl *DDef = D->getDefinition()) {
2108 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2109 Importer.ToDiag(ExistingDef->getLocation(),
2110 diag::err_odr_variable_multiple_def)
2111 << Name;
2112 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2113 } else {
2114 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002115 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002116 }
2117 }
2118
2119 return MergeWithVar;
2120 }
2121
2122 if (!ConflictingDecls.empty()) {
2123 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2124 ConflictingDecls.data(),
2125 ConflictingDecls.size());
2126 if (!Name)
2127 return 0;
2128 }
2129 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002130
Douglas Gregorea35d112010-02-15 23:54:17 +00002131 // Import the type.
2132 QualType T = Importer.Import(D->getType());
2133 if (T.isNull())
2134 return 0;
2135
Douglas Gregor089459a2010-02-08 21:09:39 +00002136 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002137 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002138 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2139 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002140 D->getStorageClass(),
2141 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002142 // Import the qualifier, if any.
2143 if (D->getQualifier()) {
2144 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2145 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2146 ToVar->setQualifierInfo(NNS, NNSRange);
2147 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002148 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002149 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002150 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002151 LexicalDC->addDecl(ToVar);
2152
Douglas Gregor089459a2010-02-08 21:09:39 +00002153 // Merge the initializer.
2154 // FIXME: Can we really import any initializer? Alternatively, we could force
2155 // ourselves to import every declaration of a variable and then only use
2156 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002157 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002158
2159 // FIXME: Other bits to merge?
2160
2161 return ToVar;
2162}
2163
Douglas Gregor2cd00932010-02-17 21:22:52 +00002164Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2165 // Parameters are created in the translation unit's context, then moved
2166 // into the function declaration's context afterward.
2167 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2168
2169 // Import the name of this declaration.
2170 DeclarationName Name = Importer.Import(D->getDeclName());
2171 if (D->getDeclName() && !Name)
2172 return 0;
2173
2174 // Import the location of this declaration.
2175 SourceLocation Loc = Importer.Import(D->getLocation());
2176
2177 // Import the parameter's type.
2178 QualType T = Importer.Import(D->getType());
2179 if (T.isNull())
2180 return 0;
2181
2182 // Create the imported parameter.
2183 ImplicitParamDecl *ToParm
2184 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2185 Loc, Name.getAsIdentifierInfo(),
2186 T);
2187 return Importer.Imported(D, ToParm);
2188}
2189
Douglas Gregora404ea62010-02-10 19:54:31 +00002190Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2191 // Parameters are created in the translation unit's context, then moved
2192 // into the function declaration's context afterward.
2193 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2194
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002195 // Import the name of this declaration.
2196 DeclarationName Name = Importer.Import(D->getDeclName());
2197 if (D->getDeclName() && !Name)
2198 return 0;
2199
Douglas Gregora404ea62010-02-10 19:54:31 +00002200 // Import the location of this declaration.
2201 SourceLocation Loc = Importer.Import(D->getLocation());
2202
2203 // Import the parameter's type.
2204 QualType T = Importer.Import(D->getType());
2205 if (T.isNull())
2206 return 0;
2207
2208 // Create the imported parameter.
2209 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2210 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2211 Loc, Name.getAsIdentifierInfo(),
2212 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002213 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002214 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002215 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002216 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002217}
2218
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002219Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2220 // Import the major distinguishing characteristics of a method.
2221 DeclContext *DC, *LexicalDC;
2222 DeclarationName Name;
2223 SourceLocation Loc;
2224 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2225 return 0;
2226
2227 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2228 Lookup.first != Lookup.second;
2229 ++Lookup.first) {
2230 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2231 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2232 continue;
2233
2234 // Check return types.
2235 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2236 FoundMethod->getResultType())) {
2237 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2238 << D->isInstanceMethod() << Name
2239 << D->getResultType() << FoundMethod->getResultType();
2240 Importer.ToDiag(FoundMethod->getLocation(),
2241 diag::note_odr_objc_method_here)
2242 << D->isInstanceMethod() << Name;
2243 return 0;
2244 }
2245
2246 // Check the number of parameters.
2247 if (D->param_size() != FoundMethod->param_size()) {
2248 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2249 << D->isInstanceMethod() << Name
2250 << D->param_size() << FoundMethod->param_size();
2251 Importer.ToDiag(FoundMethod->getLocation(),
2252 diag::note_odr_objc_method_here)
2253 << D->isInstanceMethod() << Name;
2254 return 0;
2255 }
2256
2257 // Check parameter types.
2258 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2259 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2260 P != PEnd; ++P, ++FoundP) {
2261 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2262 (*FoundP)->getType())) {
2263 Importer.FromDiag((*P)->getLocation(),
2264 diag::err_odr_objc_method_param_type_inconsistent)
2265 << D->isInstanceMethod() << Name
2266 << (*P)->getType() << (*FoundP)->getType();
2267 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2268 << (*FoundP)->getType();
2269 return 0;
2270 }
2271 }
2272
2273 // Check variadic/non-variadic.
2274 // Check the number of parameters.
2275 if (D->isVariadic() != FoundMethod->isVariadic()) {
2276 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2277 << D->isInstanceMethod() << Name;
2278 Importer.ToDiag(FoundMethod->getLocation(),
2279 diag::note_odr_objc_method_here)
2280 << D->isInstanceMethod() << Name;
2281 return 0;
2282 }
2283
2284 // FIXME: Any other bits we need to merge?
2285 return Importer.Imported(D, FoundMethod);
2286 }
2287 }
2288
2289 // Import the result type.
2290 QualType ResultTy = Importer.Import(D->getResultType());
2291 if (ResultTy.isNull())
2292 return 0;
2293
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002294 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2295
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002296 ObjCMethodDecl *ToMethod
2297 = ObjCMethodDecl::Create(Importer.getToContext(),
2298 Loc,
2299 Importer.Import(D->getLocEnd()),
2300 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002301 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002302 D->isInstanceMethod(),
2303 D->isVariadic(),
2304 D->isSynthesized(),
2305 D->getImplementationControl());
2306
2307 // FIXME: When we decide to merge method definitions, we'll need to
2308 // deal with implicit parameters.
2309
2310 // Import the parameters
2311 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2312 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2313 FromPEnd = D->param_end();
2314 FromP != FromPEnd;
2315 ++FromP) {
2316 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2317 if (!ToP)
2318 return 0;
2319
2320 ToParams.push_back(ToP);
2321 }
2322
2323 // Set the parameters.
2324 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2325 ToParams[I]->setOwningFunction(ToMethod);
2326 ToMethod->addDecl(ToParams[I]);
2327 }
2328 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002329 ToParams.data(), ToParams.size(),
2330 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002331
2332 ToMethod->setLexicalDeclContext(LexicalDC);
2333 Importer.Imported(D, ToMethod);
2334 LexicalDC->addDecl(ToMethod);
2335 return ToMethod;
2336}
2337
Douglas Gregorb4677b62010-02-18 01:47:50 +00002338Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2339 // Import the major distinguishing characteristics of a category.
2340 DeclContext *DC, *LexicalDC;
2341 DeclarationName Name;
2342 SourceLocation Loc;
2343 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2344 return 0;
2345
2346 ObjCInterfaceDecl *ToInterface
2347 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2348 if (!ToInterface)
2349 return 0;
2350
2351 // Determine if we've already encountered this category.
2352 ObjCCategoryDecl *MergeWithCategory
2353 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2354 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2355 if (!ToCategory) {
2356 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2357 Importer.Import(D->getAtLoc()),
2358 Loc,
2359 Importer.Import(D->getCategoryNameLoc()),
2360 Name.getAsIdentifierInfo());
2361 ToCategory->setLexicalDeclContext(LexicalDC);
2362 LexicalDC->addDecl(ToCategory);
2363 Importer.Imported(D, ToCategory);
2364
2365 // Link this category into its class's category list.
2366 ToCategory->setClassInterface(ToInterface);
2367 ToCategory->insertNextClassCategory();
2368
2369 // Import protocols
2370 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2371 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2372 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2373 = D->protocol_loc_begin();
2374 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2375 FromProtoEnd = D->protocol_end();
2376 FromProto != FromProtoEnd;
2377 ++FromProto, ++FromProtoLoc) {
2378 ObjCProtocolDecl *ToProto
2379 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2380 if (!ToProto)
2381 return 0;
2382 Protocols.push_back(ToProto);
2383 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2384 }
2385
2386 // FIXME: If we're merging, make sure that the protocol list is the same.
2387 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2388 ProtocolLocs.data(), Importer.getToContext());
2389
2390 } else {
2391 Importer.Imported(D, ToCategory);
2392 }
2393
2394 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002395 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002396
2397 // If we have an implementation, import it as well.
2398 if (D->getImplementation()) {
2399 ObjCCategoryImplDecl *Impl
2400 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2401 if (!Impl)
2402 return 0;
2403
2404 ToCategory->setImplementation(Impl);
2405 }
2406
2407 return ToCategory;
2408}
2409
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002410Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002411 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002412 DeclContext *DC, *LexicalDC;
2413 DeclarationName Name;
2414 SourceLocation Loc;
2415 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2416 return 0;
2417
2418 ObjCProtocolDecl *MergeWithProtocol = 0;
2419 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2420 Lookup.first != Lookup.second;
2421 ++Lookup.first) {
2422 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2423 continue;
2424
2425 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2426 break;
2427 }
2428
2429 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2430 if (!ToProto || ToProto->isForwardDecl()) {
2431 if (!ToProto) {
2432 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2433 Name.getAsIdentifierInfo());
2434 ToProto->setForwardDecl(D->isForwardDecl());
2435 ToProto->setLexicalDeclContext(LexicalDC);
2436 LexicalDC->addDecl(ToProto);
2437 }
2438 Importer.Imported(D, ToProto);
2439
2440 // Import protocols
2441 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2442 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2443 ObjCProtocolDecl::protocol_loc_iterator
2444 FromProtoLoc = D->protocol_loc_begin();
2445 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2446 FromProtoEnd = D->protocol_end();
2447 FromProto != FromProtoEnd;
2448 ++FromProto, ++FromProtoLoc) {
2449 ObjCProtocolDecl *ToProto
2450 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2451 if (!ToProto)
2452 return 0;
2453 Protocols.push_back(ToProto);
2454 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2455 }
2456
2457 // FIXME: If we're merging, make sure that the protocol list is the same.
2458 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2459 ProtocolLocs.data(), Importer.getToContext());
2460 } else {
2461 Importer.Imported(D, ToProto);
2462 }
2463
Douglas Gregorb4677b62010-02-18 01:47:50 +00002464 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002465 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002466
2467 return ToProto;
2468}
2469
Douglas Gregora12d2942010-02-16 01:20:57 +00002470Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2471 // Import the major distinguishing characteristics of an @interface.
2472 DeclContext *DC, *LexicalDC;
2473 DeclarationName Name;
2474 SourceLocation Loc;
2475 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2476 return 0;
2477
2478 ObjCInterfaceDecl *MergeWithIface = 0;
2479 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2480 Lookup.first != Lookup.second;
2481 ++Lookup.first) {
2482 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2483 continue;
2484
2485 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2486 break;
2487 }
2488
2489 ObjCInterfaceDecl *ToIface = MergeWithIface;
2490 if (!ToIface || ToIface->isForwardDecl()) {
2491 if (!ToIface) {
2492 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2493 DC, Loc,
2494 Name.getAsIdentifierInfo(),
2495 Importer.Import(D->getClassLoc()),
2496 D->isForwardDecl(),
2497 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002498 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002499 ToIface->setLexicalDeclContext(LexicalDC);
2500 LexicalDC->addDecl(ToIface);
2501 }
2502 Importer.Imported(D, ToIface);
2503
Douglas Gregora12d2942010-02-16 01:20:57 +00002504 if (D->getSuperClass()) {
2505 ObjCInterfaceDecl *Super
2506 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2507 if (!Super)
2508 return 0;
2509
2510 ToIface->setSuperClass(Super);
2511 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2512 }
2513
2514 // Import protocols
2515 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2516 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2517 ObjCInterfaceDecl::protocol_loc_iterator
2518 FromProtoLoc = D->protocol_loc_begin();
2519 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2520 FromProtoEnd = D->protocol_end();
2521 FromProto != FromProtoEnd;
2522 ++FromProto, ++FromProtoLoc) {
2523 ObjCProtocolDecl *ToProto
2524 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2525 if (!ToProto)
2526 return 0;
2527 Protocols.push_back(ToProto);
2528 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2529 }
2530
2531 // FIXME: If we're merging, make sure that the protocol list is the same.
2532 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2533 ProtocolLocs.data(), Importer.getToContext());
2534
Douglas Gregora12d2942010-02-16 01:20:57 +00002535 // Import @end range
2536 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2537 } else {
2538 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002539
2540 // Check for consistency of superclasses.
2541 DeclarationName FromSuperName, ToSuperName;
2542 if (D->getSuperClass())
2543 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2544 if (ToIface->getSuperClass())
2545 ToSuperName = ToIface->getSuperClass()->getDeclName();
2546 if (FromSuperName != ToSuperName) {
2547 Importer.ToDiag(ToIface->getLocation(),
2548 diag::err_odr_objc_superclass_inconsistent)
2549 << ToIface->getDeclName();
2550 if (ToIface->getSuperClass())
2551 Importer.ToDiag(ToIface->getSuperClassLoc(),
2552 diag::note_odr_objc_superclass)
2553 << ToIface->getSuperClass()->getDeclName();
2554 else
2555 Importer.ToDiag(ToIface->getLocation(),
2556 diag::note_odr_objc_missing_superclass);
2557 if (D->getSuperClass())
2558 Importer.FromDiag(D->getSuperClassLoc(),
2559 diag::note_odr_objc_superclass)
2560 << D->getSuperClass()->getDeclName();
2561 else
2562 Importer.FromDiag(D->getLocation(),
2563 diag::note_odr_objc_missing_superclass);
2564 return 0;
2565 }
Douglas Gregora12d2942010-02-16 01:20:57 +00002566 }
2567
Douglas Gregorb4677b62010-02-18 01:47:50 +00002568 // Import categories. When the categories themselves are imported, they'll
2569 // hook themselves into this interface.
2570 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2571 FromCat = FromCat->getNextClassCategory())
2572 Importer.Import(FromCat);
2573
Douglas Gregora12d2942010-02-16 01:20:57 +00002574 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00002575 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00002576
2577 // If we have an @implementation, import it as well.
2578 if (D->getImplementation()) {
2579 ObjCImplementationDecl *Impl
2580 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2581 if (!Impl)
2582 return 0;
2583
2584 ToIface->setImplementation(Impl);
2585 }
2586
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002587 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00002588}
2589
Douglas Gregore3261622010-02-17 18:02:10 +00002590Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2591 // Import the major distinguishing characteristics of an @property.
2592 DeclContext *DC, *LexicalDC;
2593 DeclarationName Name;
2594 SourceLocation Loc;
2595 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2596 return 0;
2597
2598 // Check whether we have already imported this property.
2599 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2600 Lookup.first != Lookup.second;
2601 ++Lookup.first) {
2602 if (ObjCPropertyDecl *FoundProp
2603 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2604 // Check property types.
2605 if (!Importer.IsStructurallyEquivalent(D->getType(),
2606 FoundProp->getType())) {
2607 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2608 << Name << D->getType() << FoundProp->getType();
2609 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2610 << FoundProp->getType();
2611 return 0;
2612 }
2613
2614 // FIXME: Check property attributes, getters, setters, etc.?
2615
2616 // Consider these properties to be equivalent.
2617 Importer.Imported(D, FoundProp);
2618 return FoundProp;
2619 }
2620 }
2621
2622 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00002623 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
2624 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00002625 return 0;
2626
2627 // Create the new property.
2628 ObjCPropertyDecl *ToProperty
2629 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2630 Name.getAsIdentifierInfo(),
2631 Importer.Import(D->getAtLoc()),
2632 T,
2633 D->getPropertyImplementation());
2634 Importer.Imported(D, ToProperty);
2635 ToProperty->setLexicalDeclContext(LexicalDC);
2636 LexicalDC->addDecl(ToProperty);
2637
2638 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00002639 ToProperty->setPropertyAttributesAsWritten(
2640 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00002641 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2642 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2643 ToProperty->setGetterMethodDecl(
2644 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2645 ToProperty->setSetterMethodDecl(
2646 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2647 ToProperty->setPropertyIvarDecl(
2648 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2649 return ToProperty;
2650}
2651
Douglas Gregor2b785022010-02-18 02:12:22 +00002652Decl *
2653ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2654 // Import the context of this declaration.
2655 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2656 if (!DC)
2657 return 0;
2658
2659 DeclContext *LexicalDC = DC;
2660 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2661 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2662 if (!LexicalDC)
2663 return 0;
2664 }
2665
2666 // Import the location of this declaration.
2667 SourceLocation Loc = Importer.Import(D->getLocation());
2668
2669 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2670 llvm::SmallVector<SourceLocation, 4> Locations;
2671 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2672 = D->protocol_loc_begin();
2673 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2674 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2675 FromProto != FromProtoEnd;
2676 ++FromProto, ++FromProtoLoc) {
2677 ObjCProtocolDecl *ToProto
2678 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2679 if (!ToProto)
2680 continue;
2681
2682 Protocols.push_back(ToProto);
2683 Locations.push_back(Importer.Import(*FromProtoLoc));
2684 }
2685
2686 ObjCForwardProtocolDecl *ToForward
2687 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2688 Protocols.data(), Protocols.size(),
2689 Locations.data());
2690 ToForward->setLexicalDeclContext(LexicalDC);
2691 LexicalDC->addDecl(ToForward);
2692 Importer.Imported(D, ToForward);
2693 return ToForward;
2694}
2695
Douglas Gregora2bc15b2010-02-18 02:04:09 +00002696Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2697 // Import the context of this declaration.
2698 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2699 if (!DC)
2700 return 0;
2701
2702 DeclContext *LexicalDC = DC;
2703 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2704 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2705 if (!LexicalDC)
2706 return 0;
2707 }
2708
2709 // Import the location of this declaration.
2710 SourceLocation Loc = Importer.Import(D->getLocation());
2711
2712 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2713 llvm::SmallVector<SourceLocation, 4> Locations;
2714 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2715 From != FromEnd; ++From) {
2716 ObjCInterfaceDecl *ToIface
2717 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2718 if (!ToIface)
2719 continue;
2720
2721 Interfaces.push_back(ToIface);
2722 Locations.push_back(Importer.Import(From->getLocation()));
2723 }
2724
2725 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2726 Loc,
2727 Interfaces.data(),
2728 Locations.data(),
2729 Interfaces.size());
2730 ToClass->setLexicalDeclContext(LexicalDC);
2731 LexicalDC->addDecl(ToClass);
2732 Importer.Imported(D, ToClass);
2733 return ToClass;
2734}
2735
Douglas Gregor4800d952010-02-11 19:21:55 +00002736//----------------------------------------------------------------------------
2737// Import Statements
2738//----------------------------------------------------------------------------
2739
2740Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2741 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2742 << S->getStmtClassName();
2743 return 0;
2744}
2745
2746//----------------------------------------------------------------------------
2747// Import Expressions
2748//----------------------------------------------------------------------------
2749Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2750 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2751 << E->getStmtClassName();
2752 return 0;
2753}
2754
Douglas Gregor44080632010-02-19 01:17:02 +00002755Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2756 NestedNameSpecifier *Qualifier = 0;
2757 if (E->getQualifier()) {
2758 Qualifier = Importer.Import(E->getQualifier());
2759 if (!E->getQualifier())
2760 return 0;
2761 }
2762
2763 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2764 if (!ToD)
2765 return 0;
2766
2767 QualType T = Importer.Import(E->getType());
2768 if (T.isNull())
2769 return 0;
2770
2771 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2772 Importer.Import(E->getQualifierRange()),
2773 ToD,
2774 Importer.Import(E->getLocation()),
2775 T,
2776 /*FIXME:TemplateArgs=*/0);
2777}
2778
Douglas Gregor4800d952010-02-11 19:21:55 +00002779Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2780 QualType T = Importer.Import(E->getType());
2781 if (T.isNull())
2782 return 0;
2783
2784 return new (Importer.getToContext())
2785 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
2786}
2787
Douglas Gregorb2e400a2010-02-18 02:21:22 +00002788Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2789 QualType T = Importer.Import(E->getType());
2790 if (T.isNull())
2791 return 0;
2792
2793 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2794 E->isWide(), T,
2795 Importer.Import(E->getLocation()));
2796}
2797
Douglas Gregorf638f952010-02-19 01:07:06 +00002798Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2799 Expr *SubExpr = Importer.Import(E->getSubExpr());
2800 if (!SubExpr)
2801 return 0;
2802
2803 return new (Importer.getToContext())
2804 ParenExpr(Importer.Import(E->getLParen()),
2805 Importer.Import(E->getRParen()),
2806 SubExpr);
2807}
2808
2809Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2810 QualType T = Importer.Import(E->getType());
2811 if (T.isNull())
2812 return 0;
2813
2814 Expr *SubExpr = Importer.Import(E->getSubExpr());
2815 if (!SubExpr)
2816 return 0;
2817
2818 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
2819 T,
2820 Importer.Import(E->getOperatorLoc()));
2821}
2822
Douglas Gregorbd249a52010-02-19 01:24:23 +00002823Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2824 QualType ResultType = Importer.Import(E->getType());
2825
2826 if (E->isArgumentType()) {
2827 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2828 if (!TInfo)
2829 return 0;
2830
2831 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2832 TInfo, ResultType,
2833 Importer.Import(E->getOperatorLoc()),
2834 Importer.Import(E->getRParenLoc()));
2835 }
2836
2837 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2838 if (!SubExpr)
2839 return 0;
2840
2841 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2842 SubExpr, ResultType,
2843 Importer.Import(E->getOperatorLoc()),
2844 Importer.Import(E->getRParenLoc()));
2845}
2846
Douglas Gregorf638f952010-02-19 01:07:06 +00002847Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2848 QualType T = Importer.Import(E->getType());
2849 if (T.isNull())
2850 return 0;
2851
2852 Expr *LHS = Importer.Import(E->getLHS());
2853 if (!LHS)
2854 return 0;
2855
2856 Expr *RHS = Importer.Import(E->getRHS());
2857 if (!RHS)
2858 return 0;
2859
2860 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
2861 T,
2862 Importer.Import(E->getOperatorLoc()));
2863}
2864
2865Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2866 QualType T = Importer.Import(E->getType());
2867 if (T.isNull())
2868 return 0;
2869
2870 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2871 if (CompLHSType.isNull())
2872 return 0;
2873
2874 QualType CompResultType = Importer.Import(E->getComputationResultType());
2875 if (CompResultType.isNull())
2876 return 0;
2877
2878 Expr *LHS = Importer.Import(E->getLHS());
2879 if (!LHS)
2880 return 0;
2881
2882 Expr *RHS = Importer.Import(E->getRHS());
2883 if (!RHS)
2884 return 0;
2885
2886 return new (Importer.getToContext())
2887 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
2888 T, CompLHSType, CompResultType,
2889 Importer.Import(E->getOperatorLoc()));
2890}
2891
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002892Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2893 QualType T = Importer.Import(E->getType());
2894 if (T.isNull())
2895 return 0;
2896
2897 Expr *SubExpr = Importer.Import(E->getSubExpr());
2898 if (!SubExpr)
2899 return 0;
2900
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002901 // FIXME: Initialize the base path.
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002902 assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002903 CXXBaseSpecifierArray BasePath;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002904 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
Anders Carlssonf1b48b72010-04-24 16:57:13 +00002905 SubExpr, BasePath,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002906 E->isLvalueCast());
2907}
2908
Douglas Gregor008847a2010-02-19 01:32:14 +00002909Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2910 QualType T = Importer.Import(E->getType());
2911 if (T.isNull())
2912 return 0;
2913
2914 Expr *SubExpr = Importer.Import(E->getSubExpr());
2915 if (!SubExpr)
2916 return 0;
2917
2918 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2919 if (!TInfo && E->getTypeInfoAsWritten())
2920 return 0;
2921
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002922 // FIXME: Initialize the base path.
2923 assert(E->getBasePath().empty() && "FIXME: Must copy base path!");
2924 CXXBaseSpecifierArray BasePath;
Douglas Gregor008847a2010-02-19 01:32:14 +00002925 return new (Importer.getToContext()) CStyleCastExpr(T, E->getCastKind(),
Anders Carlsson41b2dcd2010-04-24 18:38:56 +00002926 SubExpr, BasePath, TInfo,
Douglas Gregor008847a2010-02-19 01:32:14 +00002927 Importer.Import(E->getLParenLoc()),
2928 Importer.Import(E->getRParenLoc()));
2929}
2930
Douglas Gregor4800d952010-02-11 19:21:55 +00002931ASTImporter::ASTImporter(Diagnostic &Diags,
2932 ASTContext &ToContext, FileManager &ToFileManager,
2933 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002934 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +00002935 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor4800d952010-02-11 19:21:55 +00002936 Diags(Diags) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00002937 ImportedDecls[FromContext.getTranslationUnitDecl()]
2938 = ToContext.getTranslationUnitDecl();
2939}
2940
2941ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002942
2943QualType ASTImporter::Import(QualType FromT) {
2944 if (FromT.isNull())
2945 return QualType();
2946
Douglas Gregor169fba52010-02-08 15:18:58 +00002947 // Check whether we've already imported this type.
2948 llvm::DenseMap<Type *, Type *>::iterator Pos
2949 = ImportedTypes.find(FromT.getTypePtr());
2950 if (Pos != ImportedTypes.end())
2951 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002952
Douglas Gregor169fba52010-02-08 15:18:58 +00002953 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002954 ASTNodeImporter Importer(*this);
2955 QualType ToT = Importer.Visit(FromT.getTypePtr());
2956 if (ToT.isNull())
2957 return ToT;
2958
Douglas Gregor169fba52010-02-08 15:18:58 +00002959 // Record the imported type.
2960 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2961
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002962 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2963}
2964
Douglas Gregor9bed8792010-02-09 19:21:46 +00002965TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002966 if (!FromTSI)
2967 return FromTSI;
2968
2969 // FIXME: For now we just create a "trivial" type source info based
2970 // on the type and a seingle location. Implement a real version of
2971 // this.
2972 QualType T = Import(FromTSI->getType());
2973 if (T.isNull())
2974 return 0;
2975
2976 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00002977 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002978}
2979
2980Decl *ASTImporter::Import(Decl *FromD) {
2981 if (!FromD)
2982 return 0;
2983
2984 // Check whether we've already imported this declaration.
2985 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2986 if (Pos != ImportedDecls.end())
2987 return Pos->second;
2988
2989 // Import the type
2990 ASTNodeImporter Importer(*this);
2991 Decl *ToD = Importer.Visit(FromD);
2992 if (!ToD)
2993 return 0;
2994
2995 // Record the imported declaration.
2996 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00002997
2998 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2999 // Keep track of anonymous tags that have an associated typedef.
3000 if (FromTag->getTypedefForAnonDecl())
3001 AnonTagsWithPendingTypedefs.push_back(FromTag);
3002 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3003 // When we've finished transforming a typedef, see whether it was the
3004 // typedef for an anonymous tag.
3005 for (llvm::SmallVector<TagDecl *, 4>::iterator
3006 FromTag = AnonTagsWithPendingTypedefs.begin(),
3007 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3008 FromTag != FromTagEnd; ++FromTag) {
3009 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3010 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3011 // We found the typedef for an anonymous tag; link them.
3012 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3013 AnonTagsWithPendingTypedefs.erase(FromTag);
3014 break;
3015 }
3016 }
3017 }
3018 }
3019
Douglas Gregor9bed8792010-02-09 19:21:46 +00003020 return ToD;
3021}
3022
3023DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3024 if (!FromDC)
3025 return FromDC;
3026
3027 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3028}
3029
3030Expr *ASTImporter::Import(Expr *FromE) {
3031 if (!FromE)
3032 return 0;
3033
3034 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3035}
3036
3037Stmt *ASTImporter::Import(Stmt *FromS) {
3038 if (!FromS)
3039 return 0;
3040
Douglas Gregor4800d952010-02-11 19:21:55 +00003041 // Check whether we've already imported this declaration.
3042 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3043 if (Pos != ImportedStmts.end())
3044 return Pos->second;
3045
3046 // Import the type
3047 ASTNodeImporter Importer(*this);
3048 Stmt *ToS = Importer.Visit(FromS);
3049 if (!ToS)
3050 return 0;
3051
3052 // Record the imported declaration.
3053 ImportedStmts[FromS] = ToS;
3054 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003055}
3056
3057NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3058 if (!FromNNS)
3059 return 0;
3060
3061 // FIXME: Implement!
3062 return 0;
3063}
3064
3065SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3066 if (FromLoc.isInvalid())
3067 return SourceLocation();
3068
Douglas Gregor88523732010-02-10 00:15:17 +00003069 SourceManager &FromSM = FromContext.getSourceManager();
3070
3071 // For now, map everything down to its spelling location, so that we
3072 // don't have to import macro instantiations.
3073 // FIXME: Import macro instantiations!
3074 FromLoc = FromSM.getSpellingLoc(FromLoc);
3075 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3076 SourceManager &ToSM = ToContext.getSourceManager();
3077 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3078 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00003079}
3080
3081SourceRange ASTImporter::Import(SourceRange FromRange) {
3082 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3083}
3084
Douglas Gregor88523732010-02-10 00:15:17 +00003085FileID ASTImporter::Import(FileID FromID) {
3086 llvm::DenseMap<unsigned, FileID>::iterator Pos
3087 = ImportedFileIDs.find(FromID.getHashValue());
3088 if (Pos != ImportedFileIDs.end())
3089 return Pos->second;
3090
3091 SourceManager &FromSM = FromContext.getSourceManager();
3092 SourceManager &ToSM = ToContext.getSourceManager();
3093 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3094 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3095
3096 // Include location of this file.
3097 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3098
3099 // Map the FileID for to the "to" source manager.
3100 FileID ToID;
3101 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3102 if (Cache->Entry) {
3103 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3104 // disk again
3105 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3106 // than mmap the files several times.
3107 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
3108 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3109 FromSLoc.getFile().getFileCharacteristic());
3110 } else {
3111 // FIXME: We want to re-use the existing MemoryBuffer!
Chris Lattnere127a0d2010-04-20 20:35:58 +00003112 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer(getDiags(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00003113 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00003114 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00003115 FromBuf->getBufferIdentifier());
3116 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3117 }
3118
3119
3120 ImportedFileIDs[FromID.getHashValue()] = ToID;
3121 return ToID;
3122}
3123
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003124DeclarationName ASTImporter::Import(DeclarationName FromName) {
3125 if (!FromName)
3126 return DeclarationName();
3127
3128 switch (FromName.getNameKind()) {
3129 case DeclarationName::Identifier:
3130 return Import(FromName.getAsIdentifierInfo());
3131
3132 case DeclarationName::ObjCZeroArgSelector:
3133 case DeclarationName::ObjCOneArgSelector:
3134 case DeclarationName::ObjCMultiArgSelector:
3135 return Import(FromName.getObjCSelector());
3136
3137 case DeclarationName::CXXConstructorName: {
3138 QualType T = Import(FromName.getCXXNameType());
3139 if (T.isNull())
3140 return DeclarationName();
3141
3142 return ToContext.DeclarationNames.getCXXConstructorName(
3143 ToContext.getCanonicalType(T));
3144 }
3145
3146 case DeclarationName::CXXDestructorName: {
3147 QualType T = Import(FromName.getCXXNameType());
3148 if (T.isNull())
3149 return DeclarationName();
3150
3151 return ToContext.DeclarationNames.getCXXDestructorName(
3152 ToContext.getCanonicalType(T));
3153 }
3154
3155 case DeclarationName::CXXConversionFunctionName: {
3156 QualType T = Import(FromName.getCXXNameType());
3157 if (T.isNull())
3158 return DeclarationName();
3159
3160 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3161 ToContext.getCanonicalType(T));
3162 }
3163
3164 case DeclarationName::CXXOperatorName:
3165 return ToContext.DeclarationNames.getCXXOperatorName(
3166 FromName.getCXXOverloadedOperator());
3167
3168 case DeclarationName::CXXLiteralOperatorName:
3169 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3170 Import(FromName.getCXXLiteralIdentifier()));
3171
3172 case DeclarationName::CXXUsingDirective:
3173 // FIXME: STATICS!
3174 return DeclarationName::getUsingDirectiveName();
3175 }
3176
3177 // Silence bogus GCC warning
3178 return DeclarationName();
3179}
3180
3181IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3182 if (!FromId)
3183 return 0;
3184
3185 return &ToContext.Idents.get(FromId->getName());
3186}
Douglas Gregor089459a2010-02-08 21:09:39 +00003187
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003188Selector ASTImporter::Import(Selector FromSel) {
3189 if (FromSel.isNull())
3190 return Selector();
3191
3192 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3193 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3194 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3195 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3196 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3197}
3198
Douglas Gregor089459a2010-02-08 21:09:39 +00003199DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3200 DeclContext *DC,
3201 unsigned IDNS,
3202 NamedDecl **Decls,
3203 unsigned NumDecls) {
3204 return Name;
3205}
3206
3207DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003208 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
3209 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003210}
3211
3212DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00003213 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
3214 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00003215}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00003216
3217Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3218 ImportedDecls[From] = To;
3219 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00003220}
Douglas Gregorea35d112010-02-15 23:54:17 +00003221
3222bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3223 llvm::DenseMap<Type *, Type *>::iterator Pos
3224 = ImportedTypes.find(From.getTypePtr());
3225 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3226 return true;
3227
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003228 StructuralEquivalenceContext Ctx(FromContext, ToContext, Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +00003229 NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00003230 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00003231}