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