blob: 129d91be256febadc39814792235c325d6e2068d [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 Gregor089459a2010-02-08 21:09:39 +000093 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +000094 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor4800d952010-02-11 19:21:55 +000095
96 // Importing statements
97 Stmt *VisitStmt(Stmt *S);
98
99 // Importing expressions
100 Expr *VisitExpr(Expr *E);
101 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000102 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000103 };
104}
105
106//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000107// Structural Equivalence
108//----------------------------------------------------------------------------
109
110namespace {
111 struct StructuralEquivalenceContext {
112 /// \brief AST contexts for which we are checking structural equivalence.
113 ASTContext &C1, &C2;
114
115 /// \brief Diagnostic object used to emit diagnostics.
116 Diagnostic &Diags;
117
118 /// \brief The set of "tentative" equivalences between two canonical
119 /// declarations, mapping from a declaration in the first context to the
120 /// declaration in the second context that we believe to be equivalent.
121 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
122
123 /// \brief Queue of declarations in the first context whose equivalence
124 /// with a declaration in the second context still needs to be verified.
125 std::deque<Decl *> DeclsToCheck;
126
Douglas Gregorea35d112010-02-15 23:54:17 +0000127 /// \brief Declaration (from, to) pairs that are known not to be equivalent
128 /// (which we have already complained about).
129 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
130
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000131 /// \brief Whether we're being strict about the spelling of types when
132 /// unifying two types.
133 bool StrictTypeSpelling;
134
135 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
136 Diagnostic &Diags,
Douglas Gregorea35d112010-02-15 23:54:17 +0000137 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000138 bool StrictTypeSpelling = false)
Douglas Gregorea35d112010-02-15 23:54:17 +0000139 : C1(C1), C2(C2), Diags(Diags), NonEquivalentDecls(NonEquivalentDecls),
140 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000141
142 /// \brief Determine whether the two declarations are structurally
143 /// equivalent.
144 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
145
146 /// \brief Determine whether the two types are structurally equivalent.
147 bool IsStructurallyEquivalent(QualType T1, QualType T2);
148
149 private:
150 /// \brief Finish checking all of the structural equivalences.
151 ///
152 /// \returns true if an error occurred, false otherwise.
153 bool Finish();
154
155 public:
156 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
157 return Diags.Report(FullSourceLoc(Loc, C1.getSourceManager()), DiagID);
158 }
159
160 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
161 return Diags.Report(FullSourceLoc(Loc, C2.getSourceManager()), DiagID);
162 }
163 };
164}
165
166static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
167 QualType T1, QualType T2);
168static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
169 Decl *D1, Decl *D2);
170
171/// \brief Determine if two APInts have the same value, after zero-extending
172/// one of them (if needed!) to ensure that the bit-widths match.
173static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
174 if (I1.getBitWidth() == I2.getBitWidth())
175 return I1 == I2;
176
177 if (I1.getBitWidth() > I2.getBitWidth())
178 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
179
180 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
181}
182
183/// \brief Determine if two APSInts have the same value, zero- or sign-extending
184/// as needed.
185static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
186 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
187 return I1 == I2;
188
189 // Check for a bit-width mismatch.
190 if (I1.getBitWidth() > I2.getBitWidth())
191 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
192 else if (I2.getBitWidth() > I1.getBitWidth())
193 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
194
195 // We have a signedness mismatch. Turn the signed value into an unsigned
196 // value.
197 if (I1.isSigned()) {
198 if (I1.isNegative())
199 return false;
200
201 return llvm::APSInt(I1, true) == I2;
202 }
203
204 if (I2.isNegative())
205 return false;
206
207 return I1 == llvm::APSInt(I2, true);
208}
209
210/// \brief Determine structural equivalence of two expressions.
211static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
212 Expr *E1, Expr *E2) {
213 if (!E1 || !E2)
214 return E1 == E2;
215
216 // FIXME: Actually perform a structural comparison!
217 return true;
218}
219
220/// \brief Determine whether two identifiers are equivalent.
221static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
222 const IdentifierInfo *Name2) {
223 if (!Name1 || !Name2)
224 return Name1 == Name2;
225
226 return Name1->getName() == Name2->getName();
227}
228
229/// \brief Determine whether two nested-name-specifiers are equivalent.
230static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
231 NestedNameSpecifier *NNS1,
232 NestedNameSpecifier *NNS2) {
233 // FIXME: Implement!
234 return true;
235}
236
237/// \brief Determine whether two template arguments are equivalent.
238static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
239 const TemplateArgument &Arg1,
240 const TemplateArgument &Arg2) {
241 // FIXME: Implement!
242 return true;
243}
244
245/// \brief Determine structural equivalence for the common part of array
246/// types.
247static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
248 const ArrayType *Array1,
249 const ArrayType *Array2) {
250 if (!IsStructurallyEquivalent(Context,
251 Array1->getElementType(),
252 Array2->getElementType()))
253 return false;
254 if (Array1->getSizeModifier() != Array2->getSizeModifier())
255 return false;
256 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
257 return false;
258
259 return true;
260}
261
262/// \brief Determine structural equivalence of two types.
263static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
264 QualType T1, QualType T2) {
265 if (T1.isNull() || T2.isNull())
266 return T1.isNull() && T2.isNull();
267
268 if (!Context.StrictTypeSpelling) {
269 // We aren't being strict about token-to-token equivalence of types,
270 // so map down to the canonical type.
271 T1 = Context.C1.getCanonicalType(T1);
272 T2 = Context.C2.getCanonicalType(T2);
273 }
274
275 if (T1.getQualifiers() != T2.getQualifiers())
276 return false;
277
Douglas Gregorea35d112010-02-15 23:54:17 +0000278 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000279
Douglas Gregorea35d112010-02-15 23:54:17 +0000280 if (T1->getTypeClass() != T2->getTypeClass()) {
281 // Compare function types with prototypes vs. without prototypes as if
282 // both did not have prototypes.
283 if (T1->getTypeClass() == Type::FunctionProto &&
284 T2->getTypeClass() == Type::FunctionNoProto)
285 TC = Type::FunctionNoProto;
286 else if (T1->getTypeClass() == Type::FunctionNoProto &&
287 T2->getTypeClass() == Type::FunctionProto)
288 TC = Type::FunctionNoProto;
289 else
290 return false;
291 }
292
293 switch (TC) {
294 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000295 // FIXME: Deal with Char_S/Char_U.
296 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
297 return false;
298 break;
299
300 case Type::Complex:
301 if (!IsStructurallyEquivalent(Context,
302 cast<ComplexType>(T1)->getElementType(),
303 cast<ComplexType>(T2)->getElementType()))
304 return false;
305 break;
306
307 case Type::Pointer:
308 if (!IsStructurallyEquivalent(Context,
309 cast<PointerType>(T1)->getPointeeType(),
310 cast<PointerType>(T2)->getPointeeType()))
311 return false;
312 break;
313
314 case Type::BlockPointer:
315 if (!IsStructurallyEquivalent(Context,
316 cast<BlockPointerType>(T1)->getPointeeType(),
317 cast<BlockPointerType>(T2)->getPointeeType()))
318 return false;
319 break;
320
321 case Type::LValueReference:
322 case Type::RValueReference: {
323 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
324 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
325 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
326 return false;
327 if (Ref1->isInnerRef() != Ref2->isInnerRef())
328 return false;
329 if (!IsStructurallyEquivalent(Context,
330 Ref1->getPointeeTypeAsWritten(),
331 Ref2->getPointeeTypeAsWritten()))
332 return false;
333 break;
334 }
335
336 case Type::MemberPointer: {
337 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
338 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
339 if (!IsStructurallyEquivalent(Context,
340 MemPtr1->getPointeeType(),
341 MemPtr2->getPointeeType()))
342 return false;
343 if (!IsStructurallyEquivalent(Context,
344 QualType(MemPtr1->getClass(), 0),
345 QualType(MemPtr2->getClass(), 0)))
346 return false;
347 break;
348 }
349
350 case Type::ConstantArray: {
351 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
352 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
353 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
354 return false;
355
356 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
357 return false;
358 break;
359 }
360
361 case Type::IncompleteArray:
362 if (!IsArrayStructurallyEquivalent(Context,
363 cast<ArrayType>(T1),
364 cast<ArrayType>(T2)))
365 return false;
366 break;
367
368 case Type::VariableArray: {
369 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
370 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
371 if (!IsStructurallyEquivalent(Context,
372 Array1->getSizeExpr(), Array2->getSizeExpr()))
373 return false;
374
375 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
376 return false;
377
378 break;
379 }
380
381 case Type::DependentSizedArray: {
382 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
383 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
384 if (!IsStructurallyEquivalent(Context,
385 Array1->getSizeExpr(), Array2->getSizeExpr()))
386 return false;
387
388 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
389 return false;
390
391 break;
392 }
393
394 case Type::DependentSizedExtVector: {
395 const DependentSizedExtVectorType *Vec1
396 = cast<DependentSizedExtVectorType>(T1);
397 const DependentSizedExtVectorType *Vec2
398 = cast<DependentSizedExtVectorType>(T2);
399 if (!IsStructurallyEquivalent(Context,
400 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
401 return false;
402 if (!IsStructurallyEquivalent(Context,
403 Vec1->getElementType(),
404 Vec2->getElementType()))
405 return false;
406 break;
407 }
408
409 case Type::Vector:
410 case Type::ExtVector: {
411 const VectorType *Vec1 = cast<VectorType>(T1);
412 const VectorType *Vec2 = cast<VectorType>(T2);
413 if (!IsStructurallyEquivalent(Context,
414 Vec1->getElementType(),
415 Vec2->getElementType()))
416 return false;
417 if (Vec1->getNumElements() != Vec2->getNumElements())
418 return false;
419 if (Vec1->isAltiVec() != Vec2->isAltiVec())
420 return false;
421 if (Vec1->isPixel() != Vec2->isPixel())
422 return false;
423 }
424
425 case Type::FunctionProto: {
426 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
427 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
428 if (Proto1->getNumArgs() != Proto2->getNumArgs())
429 return false;
430 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
431 if (!IsStructurallyEquivalent(Context,
432 Proto1->getArgType(I),
433 Proto2->getArgType(I)))
434 return false;
435 }
436 if (Proto1->isVariadic() != Proto2->isVariadic())
437 return false;
438 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
439 return false;
440 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
441 return false;
442 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
443 return false;
444 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
445 if (!IsStructurallyEquivalent(Context,
446 Proto1->getExceptionType(I),
447 Proto2->getExceptionType(I)))
448 return false;
449 }
450 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
451 return false;
452
453 // Fall through to check the bits common with FunctionNoProtoType.
454 }
455
456 case Type::FunctionNoProto: {
457 const FunctionType *Function1 = cast<FunctionType>(T1);
458 const FunctionType *Function2 = cast<FunctionType>(T2);
459 if (!IsStructurallyEquivalent(Context,
460 Function1->getResultType(),
461 Function2->getResultType()))
462 return false;
463 if (Function1->getNoReturnAttr() != Function2->getNoReturnAttr())
464 return false;
465 if (Function1->getCallConv() != Function2->getCallConv())
466 return false;
467 break;
468 }
469
470 case Type::UnresolvedUsing:
471 if (!IsStructurallyEquivalent(Context,
472 cast<UnresolvedUsingType>(T1)->getDecl(),
473 cast<UnresolvedUsingType>(T2)->getDecl()))
474 return false;
475
476 break;
477
478 case Type::Typedef:
479 if (!IsStructurallyEquivalent(Context,
480 cast<TypedefType>(T1)->getDecl(),
481 cast<TypedefType>(T2)->getDecl()))
482 return false;
483 break;
484
485 case Type::TypeOfExpr:
486 if (!IsStructurallyEquivalent(Context,
487 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
488 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
489 return false;
490 break;
491
492 case Type::TypeOf:
493 if (!IsStructurallyEquivalent(Context,
494 cast<TypeOfType>(T1)->getUnderlyingType(),
495 cast<TypeOfType>(T2)->getUnderlyingType()))
496 return false;
497 break;
498
499 case Type::Decltype:
500 if (!IsStructurallyEquivalent(Context,
501 cast<DecltypeType>(T1)->getUnderlyingExpr(),
502 cast<DecltypeType>(T2)->getUnderlyingExpr()))
503 return false;
504 break;
505
506 case Type::Record:
507 case Type::Enum:
508 if (!IsStructurallyEquivalent(Context,
509 cast<TagType>(T1)->getDecl(),
510 cast<TagType>(T2)->getDecl()))
511 return false;
512 break;
513
514 case Type::Elaborated: {
515 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
516 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
517 if (Elab1->getTagKind() != Elab2->getTagKind())
518 return false;
519 if (!IsStructurallyEquivalent(Context,
520 Elab1->getUnderlyingType(),
521 Elab2->getUnderlyingType()))
522 return false;
523 break;
524 }
525
526 case Type::TemplateTypeParm: {
527 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
528 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
529 if (Parm1->getDepth() != Parm2->getDepth())
530 return false;
531 if (Parm1->getIndex() != Parm2->getIndex())
532 return false;
533 if (Parm1->isParameterPack() != Parm2->isParameterPack())
534 return false;
535
536 // Names of template type parameters are never significant.
537 break;
538 }
539
540 case Type::SubstTemplateTypeParm: {
541 const SubstTemplateTypeParmType *Subst1
542 = cast<SubstTemplateTypeParmType>(T1);
543 const SubstTemplateTypeParmType *Subst2
544 = cast<SubstTemplateTypeParmType>(T2);
545 if (!IsStructurallyEquivalent(Context,
546 QualType(Subst1->getReplacedParameter(), 0),
547 QualType(Subst2->getReplacedParameter(), 0)))
548 return false;
549 if (!IsStructurallyEquivalent(Context,
550 Subst1->getReplacementType(),
551 Subst2->getReplacementType()))
552 return false;
553 break;
554 }
555
556 case Type::TemplateSpecialization: {
557 const TemplateSpecializationType *Spec1
558 = cast<TemplateSpecializationType>(T1);
559 const TemplateSpecializationType *Spec2
560 = cast<TemplateSpecializationType>(T2);
561 if (!IsStructurallyEquivalent(Context,
562 Spec1->getTemplateName(),
563 Spec2->getTemplateName()))
564 return false;
565 if (Spec1->getNumArgs() != Spec2->getNumArgs())
566 return false;
567 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
568 if (!IsStructurallyEquivalent(Context,
569 Spec1->getArg(I), Spec2->getArg(I)))
570 return false;
571 }
572 break;
573 }
574
575 case Type::QualifiedName: {
576 const QualifiedNameType *Qual1 = cast<QualifiedNameType>(T1);
577 const QualifiedNameType *Qual2 = cast<QualifiedNameType>(T2);
578 if (!IsStructurallyEquivalent(Context,
579 Qual1->getQualifier(),
580 Qual2->getQualifier()))
581 return false;
582 if (!IsStructurallyEquivalent(Context,
583 Qual1->getNamedType(),
584 Qual2->getNamedType()))
585 return false;
586 break;
587 }
588
589 case Type::Typename: {
590 const TypenameType *Typename1 = cast<TypenameType>(T1);
591 const TypenameType *Typename2 = cast<TypenameType>(T2);
592 if (!IsStructurallyEquivalent(Context,
593 Typename1->getQualifier(),
594 Typename2->getQualifier()))
595 return false;
596 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
597 Typename2->getIdentifier()))
598 return false;
599 if (!IsStructurallyEquivalent(Context,
600 QualType(Typename1->getTemplateId(), 0),
601 QualType(Typename2->getTemplateId(), 0)))
602 return false;
603
604 break;
605 }
606
607 case Type::ObjCInterface: {
608 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
609 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
610 if (!IsStructurallyEquivalent(Context,
611 Iface1->getDecl(), Iface2->getDecl()))
612 return false;
613 if (Iface1->getNumProtocols() != Iface2->getNumProtocols())
614 return false;
615 for (unsigned I = 0, N = Iface1->getNumProtocols(); I != N; ++I) {
616 if (!IsStructurallyEquivalent(Context,
617 Iface1->getProtocol(I),
618 Iface2->getProtocol(I)))
619 return false;
620 }
621 break;
622 }
623
624 case Type::ObjCObjectPointer: {
625 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
626 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
627 if (!IsStructurallyEquivalent(Context,
628 Ptr1->getPointeeType(),
629 Ptr2->getPointeeType()))
630 return false;
631 if (Ptr1->getNumProtocols() != Ptr2->getNumProtocols())
632 return false;
633 for (unsigned I = 0, N = Ptr1->getNumProtocols(); I != N; ++I) {
634 if (!IsStructurallyEquivalent(Context,
635 Ptr1->getProtocol(I),
636 Ptr2->getProtocol(I)))
637 return false;
638 }
639 break;
640 }
641
642 } // end switch
643
644 return true;
645}
646
647/// \brief Determine structural equivalence of two records.
648static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
649 RecordDecl *D1, RecordDecl *D2) {
650 if (D1->isUnion() != D2->isUnion()) {
651 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
652 << Context.C2.getTypeDeclType(D2);
653 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
654 << D1->getDeclName() << (unsigned)D1->getTagKind();
655 return false;
656 }
657
Douglas Gregorea35d112010-02-15 23:54:17 +0000658 // Compare the definitions of these two records. If either or both are
659 // incomplete, we assume that they are equivalent.
660 D1 = D1->getDefinition();
661 D2 = D2->getDefinition();
662 if (!D1 || !D2)
663 return true;
664
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000665 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
666 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
667 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
668 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
669 << Context.C2.getTypeDeclType(D2);
670 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
671 << D2CXX->getNumBases();
672 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
673 << D1CXX->getNumBases();
674 return false;
675 }
676
677 // Check the base classes.
678 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
679 BaseEnd1 = D1CXX->bases_end(),
680 Base2 = D2CXX->bases_begin();
681 Base1 != BaseEnd1;
682 ++Base1, ++Base2) {
683 if (!IsStructurallyEquivalent(Context,
684 Base1->getType(), Base2->getType())) {
685 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
686 << Context.C2.getTypeDeclType(D2);
687 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
688 << Base2->getType()
689 << Base2->getSourceRange();
690 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
691 << Base1->getType()
692 << Base1->getSourceRange();
693 return false;
694 }
695
696 // Check virtual vs. non-virtual inheritance mismatch.
697 if (Base1->isVirtual() != Base2->isVirtual()) {
698 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
699 << Context.C2.getTypeDeclType(D2);
700 Context.Diag2(Base2->getSourceRange().getBegin(),
701 diag::note_odr_virtual_base)
702 << Base2->isVirtual() << Base2->getSourceRange();
703 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
704 << Base1->isVirtual()
705 << Base1->getSourceRange();
706 return false;
707 }
708 }
709 } else if (D1CXX->getNumBases() > 0) {
710 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
711 << Context.C2.getTypeDeclType(D2);
712 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
713 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
714 << Base1->getType()
715 << Base1->getSourceRange();
716 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
717 return false;
718 }
719 }
720
721 // Check the fields for consistency.
722 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
723 Field2End = D2->field_end();
724 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
725 Field1End = D1->field_end();
726 Field1 != Field1End;
727 ++Field1, ++Field2) {
728 if (Field2 == Field2End) {
729 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
730 << Context.C2.getTypeDeclType(D2);
731 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
732 << Field1->getDeclName() << Field1->getType();
733 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
734 return false;
735 }
736
737 if (!IsStructurallyEquivalent(Context,
738 Field1->getType(), Field2->getType())) {
739 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
740 << Context.C2.getTypeDeclType(D2);
741 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
742 << Field2->getDeclName() << Field2->getType();
743 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
744 << Field1->getDeclName() << Field1->getType();
745 return false;
746 }
747
748 if (Field1->isBitField() != Field2->isBitField()) {
749 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
750 << Context.C2.getTypeDeclType(D2);
751 if (Field1->isBitField()) {
752 llvm::APSInt Bits;
753 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
754 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
755 << Field1->getDeclName() << Field1->getType()
756 << Bits.toString(10, false);
757 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
758 << Field2->getDeclName();
759 } else {
760 llvm::APSInt Bits;
761 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
762 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
763 << Field2->getDeclName() << Field2->getType()
764 << Bits.toString(10, false);
765 Context.Diag1(Field1->getLocation(),
766 diag::note_odr_not_bit_field)
767 << Field1->getDeclName();
768 }
769 return false;
770 }
771
772 if (Field1->isBitField()) {
773 // Make sure that the bit-fields are the same length.
774 llvm::APSInt Bits1, Bits2;
775 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
776 return false;
777 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
778 return false;
779
780 if (!IsSameValue(Bits1, Bits2)) {
781 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
782 << Context.C2.getTypeDeclType(D2);
783 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
784 << Field2->getDeclName() << Field2->getType()
785 << Bits2.toString(10, false);
786 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
787 << Field1->getDeclName() << Field1->getType()
788 << Bits1.toString(10, false);
789 return false;
790 }
791 }
792 }
793
794 if (Field2 != Field2End) {
795 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
796 << Context.C2.getTypeDeclType(D2);
797 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
798 << Field2->getDeclName() << Field2->getType();
799 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
800 return false;
801 }
802
803 return true;
804}
805
806/// \brief Determine structural equivalence of two enums.
807static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
808 EnumDecl *D1, EnumDecl *D2) {
809 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
810 EC2End = D2->enumerator_end();
811 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
812 EC1End = D1->enumerator_end();
813 EC1 != EC1End; ++EC1, ++EC2) {
814 if (EC2 == EC2End) {
815 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
816 << Context.C2.getTypeDeclType(D2);
817 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
818 << EC1->getDeclName()
819 << EC1->getInitVal().toString(10);
820 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
821 return false;
822 }
823
824 llvm::APSInt Val1 = EC1->getInitVal();
825 llvm::APSInt Val2 = EC2->getInitVal();
826 if (!IsSameValue(Val1, Val2) ||
827 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
828 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
829 << Context.C2.getTypeDeclType(D2);
830 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
831 << EC2->getDeclName()
832 << EC2->getInitVal().toString(10);
833 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
834 << EC1->getDeclName()
835 << EC1->getInitVal().toString(10);
836 return false;
837 }
838 }
839
840 if (EC2 != EC2End) {
841 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
842 << Context.C2.getTypeDeclType(D2);
843 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
844 << EC2->getDeclName()
845 << EC2->getInitVal().toString(10);
846 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
847 return false;
848 }
849
850 return true;
851}
852
853/// \brief Determine structural equivalence of two declarations.
854static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
855 Decl *D1, Decl *D2) {
856 // FIXME: Check for known structural equivalences via a callback of some sort.
857
Douglas Gregorea35d112010-02-15 23:54:17 +0000858 // Check whether we already know that these two declarations are not
859 // structurally equivalent.
860 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
861 D2->getCanonicalDecl())))
862 return false;
863
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000864 // Determine whether we've already produced a tentative equivalence for D1.
865 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
866 if (EquivToD1)
867 return EquivToD1 == D2->getCanonicalDecl();
868
869 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
870 EquivToD1 = D2->getCanonicalDecl();
871 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
872 return true;
873}
874
875bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
876 Decl *D2) {
877 if (!::IsStructurallyEquivalent(*this, D1, D2))
878 return false;
879
880 return !Finish();
881}
882
883bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
884 QualType T2) {
885 if (!::IsStructurallyEquivalent(*this, T1, T2))
886 return false;
887
888 return !Finish();
889}
890
891bool StructuralEquivalenceContext::Finish() {
892 while (!DeclsToCheck.empty()) {
893 // Check the next declaration.
894 Decl *D1 = DeclsToCheck.front();
895 DeclsToCheck.pop_front();
896
897 Decl *D2 = TentativeEquivalences[D1];
898 assert(D2 && "Unrecorded tentative equivalence?");
899
Douglas Gregorea35d112010-02-15 23:54:17 +0000900 bool Equivalent = true;
901
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000902 // FIXME: Switch on all declaration kinds. For now, we're just going to
903 // check the obvious ones.
904 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
905 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
906 // Check for equivalent structure names.
907 IdentifierInfo *Name1 = Record1->getIdentifier();
908 if (!Name1 && Record1->getTypedefForAnonDecl())
909 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
910 IdentifierInfo *Name2 = Record2->getIdentifier();
911 if (!Name2 && Record2->getTypedefForAnonDecl())
912 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000913 if (!::IsStructurallyEquivalent(Name1, Name2) ||
914 !::IsStructurallyEquivalent(*this, Record1, Record2))
915 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000916 } else {
917 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000918 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000919 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000920 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000921 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
922 // Check for equivalent enum names.
923 IdentifierInfo *Name1 = Enum1->getIdentifier();
924 if (!Name1 && Enum1->getTypedefForAnonDecl())
925 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
926 IdentifierInfo *Name2 = Enum2->getIdentifier();
927 if (!Name2 && Enum2->getTypedefForAnonDecl())
928 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +0000929 if (!::IsStructurallyEquivalent(Name1, Name2) ||
930 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
931 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000932 } else {
933 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +0000934 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000935 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000936 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000937 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
938 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +0000939 Typedef2->getIdentifier()) ||
940 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000941 Typedef1->getUnderlyingType(),
942 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +0000943 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000944 } else {
945 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +0000946 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000947 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000948 }
Douglas Gregorea35d112010-02-15 23:54:17 +0000949
950 if (!Equivalent) {
951 // Note that these two declarations are not equivalent (and we already
952 // know about it).
953 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
954 D2->getCanonicalDecl()));
955 return true;
956 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000957 // FIXME: Check other declaration kinds!
958 }
959
960 return false;
961}
962
963//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000964// Import Types
965//----------------------------------------------------------------------------
966
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000967QualType ASTNodeImporter::VisitType(Type *T) {
968 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
969 << T->getTypeClassName();
970 return QualType();
971}
972
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000973QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
974 switch (T->getKind()) {
975 case BuiltinType::Void: return Importer.getToContext().VoidTy;
976 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
977
978 case BuiltinType::Char_U:
979 // The context we're importing from has an unsigned 'char'. If we're
980 // importing into a context with a signed 'char', translate to
981 // 'unsigned char' instead.
982 if (Importer.getToContext().getLangOptions().CharIsSigned)
983 return Importer.getToContext().UnsignedCharTy;
984
985 return Importer.getToContext().CharTy;
986
987 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
988
989 case BuiltinType::Char16:
990 // FIXME: Make sure that the "to" context supports C++!
991 return Importer.getToContext().Char16Ty;
992
993 case BuiltinType::Char32:
994 // FIXME: Make sure that the "to" context supports C++!
995 return Importer.getToContext().Char32Ty;
996
997 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
998 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
999 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1000 case BuiltinType::ULongLong:
1001 return Importer.getToContext().UnsignedLongLongTy;
1002 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1003
1004 case BuiltinType::Char_S:
1005 // The context we're importing from has an unsigned 'char'. If we're
1006 // importing into a context with a signed 'char', translate to
1007 // 'unsigned char' instead.
1008 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1009 return Importer.getToContext().SignedCharTy;
1010
1011 return Importer.getToContext().CharTy;
1012
1013 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1014 case BuiltinType::WChar:
1015 // FIXME: If not in C++, shall we translate to the C equivalent of
1016 // wchar_t?
1017 return Importer.getToContext().WCharTy;
1018
1019 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1020 case BuiltinType::Int : return Importer.getToContext().IntTy;
1021 case BuiltinType::Long : return Importer.getToContext().LongTy;
1022 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1023 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1024 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1025 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1026 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1027
1028 case BuiltinType::NullPtr:
1029 // FIXME: Make sure that the "to" context supports C++0x!
1030 return Importer.getToContext().NullPtrTy;
1031
1032 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1033 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1034 case BuiltinType::UndeducedAuto:
1035 // FIXME: Make sure that the "to" context supports C++0x!
1036 return Importer.getToContext().UndeducedAutoTy;
1037
1038 case BuiltinType::ObjCId:
1039 // FIXME: Make sure that the "to" context supports Objective-C!
1040 return Importer.getToContext().ObjCBuiltinIdTy;
1041
1042 case BuiltinType::ObjCClass:
1043 return Importer.getToContext().ObjCBuiltinClassTy;
1044
1045 case BuiltinType::ObjCSel:
1046 return Importer.getToContext().ObjCBuiltinSelTy;
1047 }
1048
1049 return QualType();
1050}
1051
1052QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1053 QualType ToElementType = Importer.Import(T->getElementType());
1054 if (ToElementType.isNull())
1055 return QualType();
1056
1057 return Importer.getToContext().getComplexType(ToElementType);
1058}
1059
1060QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1061 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1062 if (ToPointeeType.isNull())
1063 return QualType();
1064
1065 return Importer.getToContext().getPointerType(ToPointeeType);
1066}
1067
1068QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1069 // FIXME: Check for blocks support in "to" context.
1070 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1071 if (ToPointeeType.isNull())
1072 return QualType();
1073
1074 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1075}
1076
1077QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1078 // FIXME: Check for C++ support in "to" context.
1079 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1080 if (ToPointeeType.isNull())
1081 return QualType();
1082
1083 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1084}
1085
1086QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1087 // FIXME: Check for C++0x support in "to" context.
1088 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1089 if (ToPointeeType.isNull())
1090 return QualType();
1091
1092 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1093}
1094
1095QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1096 // FIXME: Check for C++ support in "to" context.
1097 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1098 if (ToPointeeType.isNull())
1099 return QualType();
1100
1101 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1102 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1103 ClassType.getTypePtr());
1104}
1105
1106QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1107 QualType ToElementType = Importer.Import(T->getElementType());
1108 if (ToElementType.isNull())
1109 return QualType();
1110
1111 return Importer.getToContext().getConstantArrayType(ToElementType,
1112 T->getSize(),
1113 T->getSizeModifier(),
1114 T->getIndexTypeCVRQualifiers());
1115}
1116
1117QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1118 QualType ToElementType = Importer.Import(T->getElementType());
1119 if (ToElementType.isNull())
1120 return QualType();
1121
1122 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1123 T->getSizeModifier(),
1124 T->getIndexTypeCVRQualifiers());
1125}
1126
1127QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1128 QualType ToElementType = Importer.Import(T->getElementType());
1129 if (ToElementType.isNull())
1130 return QualType();
1131
1132 Expr *Size = Importer.Import(T->getSizeExpr());
1133 if (!Size)
1134 return QualType();
1135
1136 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1137 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1138 T->getSizeModifier(),
1139 T->getIndexTypeCVRQualifiers(),
1140 Brackets);
1141}
1142
1143QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1144 QualType ToElementType = Importer.Import(T->getElementType());
1145 if (ToElementType.isNull())
1146 return QualType();
1147
1148 return Importer.getToContext().getVectorType(ToElementType,
1149 T->getNumElements(),
1150 T->isAltiVec(),
1151 T->isPixel());
1152}
1153
1154QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1155 QualType ToElementType = Importer.Import(T->getElementType());
1156 if (ToElementType.isNull())
1157 return QualType();
1158
1159 return Importer.getToContext().getExtVectorType(ToElementType,
1160 T->getNumElements());
1161}
1162
1163QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1164 // FIXME: What happens if we're importing a function without a prototype
1165 // into C++? Should we make it variadic?
1166 QualType ToResultType = Importer.Import(T->getResultType());
1167 if (ToResultType.isNull())
1168 return QualType();
1169
1170 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
1171 T->getNoReturnAttr(),
1172 T->getCallConv());
1173}
1174
1175QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1176 QualType ToResultType = Importer.Import(T->getResultType());
1177 if (ToResultType.isNull())
1178 return QualType();
1179
1180 // Import argument types
1181 llvm::SmallVector<QualType, 4> ArgTypes;
1182 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1183 AEnd = T->arg_type_end();
1184 A != AEnd; ++A) {
1185 QualType ArgType = Importer.Import(*A);
1186 if (ArgType.isNull())
1187 return QualType();
1188 ArgTypes.push_back(ArgType);
1189 }
1190
1191 // Import exception types
1192 llvm::SmallVector<QualType, 4> ExceptionTypes;
1193 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1194 EEnd = T->exception_end();
1195 E != EEnd; ++E) {
1196 QualType ExceptionType = Importer.Import(*E);
1197 if (ExceptionType.isNull())
1198 return QualType();
1199 ExceptionTypes.push_back(ExceptionType);
1200 }
1201
1202 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1203 ArgTypes.size(),
1204 T->isVariadic(),
1205 T->getTypeQuals(),
1206 T->hasExceptionSpec(),
1207 T->hasAnyExceptionSpec(),
1208 ExceptionTypes.size(),
1209 ExceptionTypes.data(),
1210 T->getNoReturnAttr(),
1211 T->getCallConv());
1212}
1213
1214QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1215 TypedefDecl *ToDecl
1216 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1217 if (!ToDecl)
1218 return QualType();
1219
1220 return Importer.getToContext().getTypeDeclType(ToDecl);
1221}
1222
1223QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1224 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1225 if (!ToExpr)
1226 return QualType();
1227
1228 return Importer.getToContext().getTypeOfExprType(ToExpr);
1229}
1230
1231QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1232 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1233 if (ToUnderlyingType.isNull())
1234 return QualType();
1235
1236 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1237}
1238
1239QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1240 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1241 if (!ToExpr)
1242 return QualType();
1243
1244 return Importer.getToContext().getDecltypeType(ToExpr);
1245}
1246
1247QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1248 RecordDecl *ToDecl
1249 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1250 if (!ToDecl)
1251 return QualType();
1252
1253 return Importer.getToContext().getTagDeclType(ToDecl);
1254}
1255
1256QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1257 EnumDecl *ToDecl
1258 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1259 if (!ToDecl)
1260 return QualType();
1261
1262 return Importer.getToContext().getTagDeclType(ToDecl);
1263}
1264
1265QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
1266 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1267 if (ToUnderlyingType.isNull())
1268 return QualType();
1269
1270 return Importer.getToContext().getElaboratedType(ToUnderlyingType,
1271 T->getTagKind());
1272}
1273
1274QualType ASTNodeImporter::VisitQualifiedNameType(QualifiedNameType *T) {
1275 NestedNameSpecifier *ToQualifier = Importer.Import(T->getQualifier());
1276 if (!ToQualifier)
1277 return QualType();
1278
1279 QualType ToNamedType = Importer.Import(T->getNamedType());
1280 if (ToNamedType.isNull())
1281 return QualType();
1282
1283 return Importer.getToContext().getQualifiedNameType(ToQualifier, ToNamedType);
1284}
1285
1286QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1287 ObjCInterfaceDecl *Class
1288 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1289 if (!Class)
1290 return QualType();
1291
1292 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1293 for (ObjCInterfaceType::qual_iterator P = T->qual_begin(),
1294 PEnd = T->qual_end();
1295 P != PEnd; ++P) {
1296 ObjCProtocolDecl *Protocol
1297 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1298 if (!Protocol)
1299 return QualType();
1300 Protocols.push_back(Protocol);
1301 }
1302
1303 return Importer.getToContext().getObjCInterfaceType(Class,
1304 Protocols.data(),
1305 Protocols.size());
1306}
1307
1308QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1309 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1310 if (ToPointeeType.isNull())
1311 return QualType();
1312
1313 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
1314 for (ObjCObjectPointerType::qual_iterator P = T->qual_begin(),
1315 PEnd = T->qual_end();
1316 P != PEnd; ++P) {
1317 ObjCProtocolDecl *Protocol
1318 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1319 if (!Protocol)
1320 return QualType();
1321 Protocols.push_back(Protocol);
1322 }
1323
1324 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType,
1325 Protocols.data(),
1326 Protocols.size());
1327}
1328
Douglas Gregor089459a2010-02-08 21:09:39 +00001329//----------------------------------------------------------------------------
1330// Import Declarations
1331//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001332bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1333 DeclContext *&LexicalDC,
1334 DeclarationName &Name,
1335 SourceLocation &Loc) {
1336 // Import the context of this declaration.
1337 DC = Importer.ImportContext(D->getDeclContext());
1338 if (!DC)
1339 return true;
1340
1341 LexicalDC = DC;
1342 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1343 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1344 if (!LexicalDC)
1345 return true;
1346 }
1347
1348 // Import the name of this declaration.
1349 Name = Importer.Import(D->getDeclName());
1350 if (D->getDeclName() && !Name)
1351 return true;
1352
1353 // Import the location of this declaration.
1354 Loc = Importer.Import(D->getLocation());
1355 return false;
1356}
1357
Douglas Gregor96a01b42010-02-11 00:48:18 +00001358bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001359 RecordDecl *ToRecord) {
1360 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1361 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001362 Importer.getDiags(),
1363 Importer.getNonEquivalentDecls());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001364 return SEC.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001365}
1366
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001367bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001368 StructuralEquivalenceContext SEC(Importer.getFromContext(),
1369 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001370 Importer.getDiags(),
1371 Importer.getNonEquivalentDecls());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001372 return SEC.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001373}
1374
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001375Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001376 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001377 << D->getDeclKindName();
1378 return 0;
1379}
1380
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001381Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1382 // Import the major distinguishing characteristics of this typedef.
1383 DeclContext *DC, *LexicalDC;
1384 DeclarationName Name;
1385 SourceLocation Loc;
1386 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1387 return 0;
1388
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001389 // If this typedef is not in block scope, determine whether we've
1390 // seen a typedef with the same name (that we can merge with) or any
1391 // other entity by that name (which name lookup could conflict with).
1392 if (!DC->isFunctionOrMethod()) {
1393 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1394 unsigned IDNS = Decl::IDNS_Ordinary;
1395 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1396 Lookup.first != Lookup.second;
1397 ++Lookup.first) {
1398 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1399 continue;
1400 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001401 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1402 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001403 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001404 }
1405
1406 ConflictingDecls.push_back(*Lookup.first);
1407 }
1408
1409 if (!ConflictingDecls.empty()) {
1410 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1411 ConflictingDecls.data(),
1412 ConflictingDecls.size());
1413 if (!Name)
1414 return 0;
1415 }
1416 }
1417
Douglas Gregorea35d112010-02-15 23:54:17 +00001418 // Import the underlying type of this typedef;
1419 QualType T = Importer.Import(D->getUnderlyingType());
1420 if (T.isNull())
1421 return 0;
1422
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001423 // Create the new typedef node.
1424 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1425 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1426 Loc, Name.getAsIdentifierInfo(),
1427 TInfo);
1428 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001429 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001430 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001431
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001432 return ToTypedef;
1433}
1434
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001435Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1436 // Import the major distinguishing characteristics of this enum.
1437 DeclContext *DC, *LexicalDC;
1438 DeclarationName Name;
1439 SourceLocation Loc;
1440 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1441 return 0;
1442
1443 // Figure out what enum name we're looking for.
1444 unsigned IDNS = Decl::IDNS_Tag;
1445 DeclarationName SearchName = Name;
1446 if (!SearchName && D->getTypedefForAnonDecl()) {
1447 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1448 IDNS = Decl::IDNS_Ordinary;
1449 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1450 IDNS |= Decl::IDNS_Ordinary;
1451
1452 // We may already have an enum of the same name; try to find and match it.
1453 if (!DC->isFunctionOrMethod() && SearchName) {
1454 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1455 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1456 Lookup.first != Lookup.second;
1457 ++Lookup.first) {
1458 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1459 continue;
1460
1461 Decl *Found = *Lookup.first;
1462 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1463 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1464 Found = Tag->getDecl();
1465 }
1466
1467 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001468 if (IsStructuralMatch(D, FoundEnum))
1469 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001470 }
1471
1472 ConflictingDecls.push_back(*Lookup.first);
1473 }
1474
1475 if (!ConflictingDecls.empty()) {
1476 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1477 ConflictingDecls.data(),
1478 ConflictingDecls.size());
1479 }
1480 }
1481
1482 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001483 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001484 Name.getAsIdentifierInfo(),
1485 Importer.Import(D->getTagKeywordLoc()),
1486 0);
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001487 D2->setLexicalDeclContext(LexicalDC);
1488 Importer.Imported(D, D2);
1489 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001490
1491 // Import the integer type.
1492 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1493 if (ToIntegerType.isNull())
1494 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001495 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001496
1497 // Import the definition
1498 if (D->isDefinition()) {
1499 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1500 if (T.isNull())
1501 return 0;
1502
1503 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1504 if (ToPromotionType.isNull())
1505 return 0;
1506
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001507 D2->startDefinition();
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001508 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1509 FromMemEnd = D->decls_end();
1510 FromMem != FromMemEnd;
1511 ++FromMem)
1512 Importer.Import(*FromMem);
1513
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001514 D2->completeDefinition(T, ToPromotionType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001515 }
1516
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001517 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001518}
1519
Douglas Gregor96a01b42010-02-11 00:48:18 +00001520Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1521 // If this record has a definition in the translation unit we're coming from,
1522 // but this particular declaration is not that definition, import the
1523 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00001524 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001525 if (Definition && Definition != D) {
1526 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001527 if (!ImportedDef)
1528 return 0;
1529
1530 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001531 }
1532
1533 // Import the major distinguishing characteristics of this record.
1534 DeclContext *DC, *LexicalDC;
1535 DeclarationName Name;
1536 SourceLocation Loc;
1537 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1538 return 0;
1539
1540 // Figure out what structure name we're looking for.
1541 unsigned IDNS = Decl::IDNS_Tag;
1542 DeclarationName SearchName = Name;
1543 if (!SearchName && D->getTypedefForAnonDecl()) {
1544 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1545 IDNS = Decl::IDNS_Ordinary;
1546 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1547 IDNS |= Decl::IDNS_Ordinary;
1548
1549 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001550 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001551 if (!DC->isFunctionOrMethod() && SearchName) {
1552 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1553 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1554 Lookup.first != Lookup.second;
1555 ++Lookup.first) {
1556 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1557 continue;
1558
1559 Decl *Found = *Lookup.first;
1560 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1561 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1562 Found = Tag->getDecl();
1563 }
1564
1565 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001566 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1567 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1568 // The record types structurally match, or the "from" translation
1569 // unit only had a forward declaration anyway; call it the same
1570 // function.
1571 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001572 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001573 }
1574 } else {
1575 // We have a forward declaration of this type, so adopt that forward
1576 // declaration rather than building a new one.
1577 AdoptDecl = FoundRecord;
1578 continue;
1579 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00001580 }
1581
1582 ConflictingDecls.push_back(*Lookup.first);
1583 }
1584
1585 if (!ConflictingDecls.empty()) {
1586 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1587 ConflictingDecls.data(),
1588 ConflictingDecls.size());
1589 }
1590 }
1591
1592 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001593 RecordDecl *D2 = AdoptDecl;
1594 if (!D2) {
1595 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D)) {
1596 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001597 D->getTagKind(),
1598 DC, Loc,
1599 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001600 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001601 D2 = D2CXX;
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001602
1603 if (D->isDefinition()) {
1604 // Add base classes.
1605 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1606 for (CXXRecordDecl::base_class_iterator
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001607 Base1 = D1CXX->bases_begin(),
1608 FromBaseEnd = D1CXX->bases_end();
1609 Base1 != FromBaseEnd;
1610 ++Base1) {
1611 QualType T = Importer.Import(Base1->getType());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001612 if (T.isNull())
1613 return 0;
1614
1615 Bases.push_back(
1616 new (Importer.getToContext())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001617 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1618 Base1->isVirtual(),
1619 Base1->isBaseOfClass(),
1620 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00001621 T));
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001622 }
1623 if (!Bases.empty())
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001624 D2CXX->setBases(Bases.data(), Bases.size());
Douglas Gregor96a01b42010-02-11 00:48:18 +00001625 }
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001626 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001627 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001628 DC, Loc,
1629 Name.getAsIdentifierInfo(),
1630 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00001631 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001632 D2->setLexicalDeclContext(LexicalDC);
1633 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001634 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001635
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001636 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00001637
Douglas Gregor96a01b42010-02-11 00:48:18 +00001638 if (D->isDefinition()) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001639 D2->startDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001640 for (DeclContext::decl_iterator FromMem = D->decls_begin(),
1641 FromMemEnd = D->decls_end();
1642 FromMem != FromMemEnd;
1643 ++FromMem)
1644 Importer.Import(*FromMem);
1645
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001646 D2->completeDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00001647 }
1648
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001649 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001650}
1651
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001652Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1653 // Import the major distinguishing characteristics of this enumerator.
1654 DeclContext *DC, *LexicalDC;
1655 DeclarationName Name;
1656 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001657 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001658 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00001659
1660 QualType T = Importer.Import(D->getType());
1661 if (T.isNull())
1662 return 0;
1663
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001664 // Determine whether there are any other declarations with the same name and
1665 // in the same context.
1666 if (!LexicalDC->isFunctionOrMethod()) {
1667 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1668 unsigned IDNS = Decl::IDNS_Ordinary;
1669 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1670 Lookup.first != Lookup.second;
1671 ++Lookup.first) {
1672 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1673 continue;
1674
1675 ConflictingDecls.push_back(*Lookup.first);
1676 }
1677
1678 if (!ConflictingDecls.empty()) {
1679 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1680 ConflictingDecls.data(),
1681 ConflictingDecls.size());
1682 if (!Name)
1683 return 0;
1684 }
1685 }
1686
1687 Expr *Init = Importer.Import(D->getInitExpr());
1688 if (D->getInitExpr() && !Init)
1689 return 0;
1690
1691 EnumConstantDecl *ToEnumerator
1692 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1693 Name.getAsIdentifierInfo(), T,
1694 Init, D->getInitVal());
1695 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001696 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001697 LexicalDC->addDecl(ToEnumerator);
1698 return ToEnumerator;
1699}
Douglas Gregor96a01b42010-02-11 00:48:18 +00001700
Douglas Gregora404ea62010-02-10 19:54:31 +00001701Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1702 // Import the major distinguishing characteristics of this function.
1703 DeclContext *DC, *LexicalDC;
1704 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00001705 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001706 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00001707 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001708
1709 // Try to find a function in our own ("to") context with the same name, same
1710 // type, and in the same context as the function we're importing.
1711 if (!LexicalDC->isFunctionOrMethod()) {
1712 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1713 unsigned IDNS = Decl::IDNS_Ordinary;
1714 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1715 Lookup.first != Lookup.second;
1716 ++Lookup.first) {
1717 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1718 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00001719
Douglas Gregora404ea62010-02-10 19:54:31 +00001720 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1721 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1722 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001723 if (Importer.IsStructurallyEquivalent(D->getType(),
1724 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00001725 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001726 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00001727 }
1728
1729 // FIXME: Check for overloading more carefully, e.g., by boosting
1730 // Sema::IsOverload out to the AST library.
1731
1732 // Function overloading is okay in C++.
1733 if (Importer.getToContext().getLangOptions().CPlusPlus)
1734 continue;
1735
1736 // Complain about inconsistent function types.
1737 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00001738 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00001739 Importer.ToDiag(FoundFunction->getLocation(),
1740 diag::note_odr_value_here)
1741 << FoundFunction->getType();
1742 }
1743 }
1744
1745 ConflictingDecls.push_back(*Lookup.first);
1746 }
1747
1748 if (!ConflictingDecls.empty()) {
1749 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1750 ConflictingDecls.data(),
1751 ConflictingDecls.size());
1752 if (!Name)
1753 return 0;
1754 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00001755 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001756
1757 // Import the type.
1758 QualType T = Importer.Import(D->getType());
1759 if (T.isNull())
1760 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00001761
1762 // Import the function parameters.
1763 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1764 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1765 P != PEnd; ++P) {
1766 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1767 if (!ToP)
1768 return 0;
1769
1770 Parameters.push_back(ToP);
1771 }
1772
1773 // Create the imported function.
1774 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001775 FunctionDecl *ToEnumerator
Douglas Gregora404ea62010-02-10 19:54:31 +00001776 = FunctionDecl::Create(Importer.getToContext(), DC, Loc,
1777 Name, T, TInfo, D->getStorageClass(),
1778 D->isInlineSpecified(),
1779 D->hasWrittenPrototype());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001780 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001781 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001782 LexicalDC->addDecl(ToEnumerator);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001783
Douglas Gregora404ea62010-02-10 19:54:31 +00001784 // Set the parameters.
1785 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001786 Parameters[I]->setOwningFunction(ToEnumerator);
1787 ToEnumerator->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00001788 }
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001789 ToEnumerator->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00001790
1791 // FIXME: Other bits to merge?
Douglas Gregor089459a2010-02-08 21:09:39 +00001792
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001793 return ToEnumerator;
Douglas Gregora404ea62010-02-10 19:54:31 +00001794}
1795
Douglas Gregor96a01b42010-02-11 00:48:18 +00001796Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1797 // Import the major distinguishing characteristics of a variable.
1798 DeclContext *DC, *LexicalDC;
1799 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00001800 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001801 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1802 return 0;
1803
1804 // Import the type.
1805 QualType T = Importer.Import(D->getType());
1806 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00001807 return 0;
1808
1809 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1810 Expr *BitWidth = Importer.Import(D->getBitWidth());
1811 if (!BitWidth && D->getBitWidth())
1812 return 0;
1813
1814 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
1815 Loc, Name.getAsIdentifierInfo(),
1816 T, TInfo, BitWidth, D->isMutable());
1817 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001818 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001819 LexicalDC->addDecl(ToField);
1820 return ToField;
1821}
1822
Douglas Gregora404ea62010-02-10 19:54:31 +00001823Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
1824 // Import the major distinguishing characteristics of a variable.
1825 DeclContext *DC, *LexicalDC;
1826 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00001827 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00001828 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00001829 return 0;
1830
Douglas Gregor089459a2010-02-08 21:09:39 +00001831 // Try to find a variable in our own ("to") context with the same name and
1832 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00001833 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00001834 VarDecl *MergeWithVar = 0;
1835 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1836 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00001837 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00001838 Lookup.first != Lookup.second;
1839 ++Lookup.first) {
1840 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1841 continue;
1842
1843 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
1844 // We have found a variable that we may need to merge with. Check it.
1845 if (isExternalLinkage(FoundVar->getLinkage()) &&
1846 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001847 if (Importer.IsStructurallyEquivalent(D->getType(),
1848 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00001849 MergeWithVar = FoundVar;
1850 break;
1851 }
1852
Douglas Gregord0145422010-02-12 17:23:39 +00001853 const ArrayType *FoundArray
1854 = Importer.getToContext().getAsArrayType(FoundVar->getType());
1855 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00001856 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00001857 if (FoundArray && TArray) {
1858 if (isa<IncompleteArrayType>(FoundArray) &&
1859 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001860 // Import the type.
1861 QualType T = Importer.Import(D->getType());
1862 if (T.isNull())
1863 return 0;
1864
Douglas Gregord0145422010-02-12 17:23:39 +00001865 FoundVar->setType(T);
1866 MergeWithVar = FoundVar;
1867 break;
1868 } else if (isa<IncompleteArrayType>(TArray) &&
1869 isa<ConstantArrayType>(FoundArray)) {
1870 MergeWithVar = FoundVar;
1871 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00001872 }
1873 }
1874
Douglas Gregor089459a2010-02-08 21:09:39 +00001875 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00001876 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00001877 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
1878 << FoundVar->getType();
1879 }
1880 }
1881
1882 ConflictingDecls.push_back(*Lookup.first);
1883 }
1884
1885 if (MergeWithVar) {
1886 // An equivalent variable with external linkage has been found. Link
1887 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001888 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00001889
1890 if (VarDecl *DDef = D->getDefinition()) {
1891 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
1892 Importer.ToDiag(ExistingDef->getLocation(),
1893 diag::err_odr_variable_multiple_def)
1894 << Name;
1895 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
1896 } else {
1897 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00001898 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00001899 }
1900 }
1901
1902 return MergeWithVar;
1903 }
1904
1905 if (!ConflictingDecls.empty()) {
1906 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1907 ConflictingDecls.data(),
1908 ConflictingDecls.size());
1909 if (!Name)
1910 return 0;
1911 }
1912 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001913
Douglas Gregorea35d112010-02-15 23:54:17 +00001914 // Import the type.
1915 QualType T = Importer.Import(D->getType());
1916 if (T.isNull())
1917 return 0;
1918
Douglas Gregor089459a2010-02-08 21:09:39 +00001919 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001920 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00001921 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
1922 Name.getAsIdentifierInfo(), T, TInfo,
1923 D->getStorageClass());
Douglas Gregor9bed8792010-02-09 19:21:46 +00001924 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001925 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00001926 LexicalDC->addDecl(ToVar);
1927
Douglas Gregor089459a2010-02-08 21:09:39 +00001928 // Merge the initializer.
1929 // FIXME: Can we really import any initializer? Alternatively, we could force
1930 // ourselves to import every declaration of a variable and then only use
1931 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00001932 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00001933
1934 // FIXME: Other bits to merge?
1935
1936 return ToVar;
1937}
1938
Douglas Gregora404ea62010-02-10 19:54:31 +00001939Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
1940 // Parameters are created in the translation unit's context, then moved
1941 // into the function declaration's context afterward.
1942 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
1943
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00001944 // Import the name of this declaration.
1945 DeclarationName Name = Importer.Import(D->getDeclName());
1946 if (D->getDeclName() && !Name)
1947 return 0;
1948
Douglas Gregora404ea62010-02-10 19:54:31 +00001949 // Import the location of this declaration.
1950 SourceLocation Loc = Importer.Import(D->getLocation());
1951
1952 // Import the parameter's type.
1953 QualType T = Importer.Import(D->getType());
1954 if (T.isNull())
1955 return 0;
1956
1957 // Create the imported parameter.
1958 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1959 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
1960 Loc, Name.getAsIdentifierInfo(),
1961 T, TInfo, D->getStorageClass(),
1962 /*FIXME: Default argument*/ 0);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001963 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00001964}
1965
Douglas Gregor4800d952010-02-11 19:21:55 +00001966//----------------------------------------------------------------------------
1967// Import Statements
1968//----------------------------------------------------------------------------
1969
1970Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
1971 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
1972 << S->getStmtClassName();
1973 return 0;
1974}
1975
1976//----------------------------------------------------------------------------
1977// Import Expressions
1978//----------------------------------------------------------------------------
1979Expr *ASTNodeImporter::VisitExpr(Expr *E) {
1980 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
1981 << E->getStmtClassName();
1982 return 0;
1983}
1984
1985Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
1986 QualType T = Importer.Import(E->getType());
1987 if (T.isNull())
1988 return 0;
1989
1990 return new (Importer.getToContext())
1991 IntegerLiteral(E->getValue(), T, Importer.Import(E->getLocation()));
1992}
1993
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001994Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
1995 QualType T = Importer.Import(E->getType());
1996 if (T.isNull())
1997 return 0;
1998
1999 Expr *SubExpr = Importer.Import(E->getSubExpr());
2000 if (!SubExpr)
2001 return 0;
2002
2003 return new (Importer.getToContext()) ImplicitCastExpr(T, E->getCastKind(),
2004 SubExpr,
2005 E->isLvalueCast());
2006}
2007
Douglas Gregor4800d952010-02-11 19:21:55 +00002008ASTImporter::ASTImporter(Diagnostic &Diags,
2009 ASTContext &ToContext, FileManager &ToFileManager,
2010 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002011 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor88523732010-02-10 00:15:17 +00002012 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Douglas Gregor4800d952010-02-11 19:21:55 +00002013 Diags(Diags) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00002014 ImportedDecls[FromContext.getTranslationUnitDecl()]
2015 = ToContext.getTranslationUnitDecl();
2016}
2017
2018ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002019
2020QualType ASTImporter::Import(QualType FromT) {
2021 if (FromT.isNull())
2022 return QualType();
2023
Douglas Gregor169fba52010-02-08 15:18:58 +00002024 // Check whether we've already imported this type.
2025 llvm::DenseMap<Type *, Type *>::iterator Pos
2026 = ImportedTypes.find(FromT.getTypePtr());
2027 if (Pos != ImportedTypes.end())
2028 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002029
Douglas Gregor169fba52010-02-08 15:18:58 +00002030 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002031 ASTNodeImporter Importer(*this);
2032 QualType ToT = Importer.Visit(FromT.getTypePtr());
2033 if (ToT.isNull())
2034 return ToT;
2035
Douglas Gregor169fba52010-02-08 15:18:58 +00002036 // Record the imported type.
2037 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
2038
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002039 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
2040}
2041
Douglas Gregor9bed8792010-02-09 19:21:46 +00002042TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002043 if (!FromTSI)
2044 return FromTSI;
2045
2046 // FIXME: For now we just create a "trivial" type source info based
2047 // on the type and a seingle location. Implement a real version of
2048 // this.
2049 QualType T = Import(FromTSI->getType());
2050 if (T.isNull())
2051 return 0;
2052
2053 return ToContext.getTrivialTypeSourceInfo(T,
2054 FromTSI->getTypeLoc().getFullSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002055}
2056
2057Decl *ASTImporter::Import(Decl *FromD) {
2058 if (!FromD)
2059 return 0;
2060
2061 // Check whether we've already imported this declaration.
2062 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
2063 if (Pos != ImportedDecls.end())
2064 return Pos->second;
2065
2066 // Import the type
2067 ASTNodeImporter Importer(*this);
2068 Decl *ToD = Importer.Visit(FromD);
2069 if (!ToD)
2070 return 0;
2071
2072 // Record the imported declaration.
2073 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00002074
2075 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
2076 // Keep track of anonymous tags that have an associated typedef.
2077 if (FromTag->getTypedefForAnonDecl())
2078 AnonTagsWithPendingTypedefs.push_back(FromTag);
2079 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
2080 // When we've finished transforming a typedef, see whether it was the
2081 // typedef for an anonymous tag.
2082 for (llvm::SmallVector<TagDecl *, 4>::iterator
2083 FromTag = AnonTagsWithPendingTypedefs.begin(),
2084 FromTagEnd = AnonTagsWithPendingTypedefs.end();
2085 FromTag != FromTagEnd; ++FromTag) {
2086 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
2087 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
2088 // We found the typedef for an anonymous tag; link them.
2089 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
2090 AnonTagsWithPendingTypedefs.erase(FromTag);
2091 break;
2092 }
2093 }
2094 }
2095 }
2096
Douglas Gregor9bed8792010-02-09 19:21:46 +00002097 return ToD;
2098}
2099
2100DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
2101 if (!FromDC)
2102 return FromDC;
2103
2104 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
2105}
2106
2107Expr *ASTImporter::Import(Expr *FromE) {
2108 if (!FromE)
2109 return 0;
2110
2111 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
2112}
2113
2114Stmt *ASTImporter::Import(Stmt *FromS) {
2115 if (!FromS)
2116 return 0;
2117
Douglas Gregor4800d952010-02-11 19:21:55 +00002118 // Check whether we've already imported this declaration.
2119 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
2120 if (Pos != ImportedStmts.end())
2121 return Pos->second;
2122
2123 // Import the type
2124 ASTNodeImporter Importer(*this);
2125 Stmt *ToS = Importer.Visit(FromS);
2126 if (!ToS)
2127 return 0;
2128
2129 // Record the imported declaration.
2130 ImportedStmts[FromS] = ToS;
2131 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002132}
2133
2134NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
2135 if (!FromNNS)
2136 return 0;
2137
2138 // FIXME: Implement!
2139 return 0;
2140}
2141
2142SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
2143 if (FromLoc.isInvalid())
2144 return SourceLocation();
2145
Douglas Gregor88523732010-02-10 00:15:17 +00002146 SourceManager &FromSM = FromContext.getSourceManager();
2147
2148 // For now, map everything down to its spelling location, so that we
2149 // don't have to import macro instantiations.
2150 // FIXME: Import macro instantiations!
2151 FromLoc = FromSM.getSpellingLoc(FromLoc);
2152 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
2153 SourceManager &ToSM = ToContext.getSourceManager();
2154 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
2155 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002156}
2157
2158SourceRange ASTImporter::Import(SourceRange FromRange) {
2159 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
2160}
2161
Douglas Gregor88523732010-02-10 00:15:17 +00002162FileID ASTImporter::Import(FileID FromID) {
2163 llvm::DenseMap<unsigned, FileID>::iterator Pos
2164 = ImportedFileIDs.find(FromID.getHashValue());
2165 if (Pos != ImportedFileIDs.end())
2166 return Pos->second;
2167
2168 SourceManager &FromSM = FromContext.getSourceManager();
2169 SourceManager &ToSM = ToContext.getSourceManager();
2170 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
2171 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
2172
2173 // Include location of this file.
2174 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
2175
2176 // Map the FileID for to the "to" source manager.
2177 FileID ToID;
2178 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
2179 if (Cache->Entry) {
2180 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
2181 // disk again
2182 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
2183 // than mmap the files several times.
2184 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
2185 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
2186 FromSLoc.getFile().getFileCharacteristic());
2187 } else {
2188 // FIXME: We want to re-use the existing MemoryBuffer!
2189 const llvm::MemoryBuffer *FromBuf = Cache->getBuffer();
2190 llvm::MemoryBuffer *ToBuf
2191 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBufferStart(),
2192 FromBuf->getBufferEnd(),
2193 FromBuf->getBufferIdentifier());
2194 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
2195 }
2196
2197
2198 ImportedFileIDs[FromID.getHashValue()] = ToID;
2199 return ToID;
2200}
2201
Douglas Gregor1b2949d2010-02-05 17:54:41 +00002202DeclarationName ASTImporter::Import(DeclarationName FromName) {
2203 if (!FromName)
2204 return DeclarationName();
2205
2206 switch (FromName.getNameKind()) {
2207 case DeclarationName::Identifier:
2208 return Import(FromName.getAsIdentifierInfo());
2209
2210 case DeclarationName::ObjCZeroArgSelector:
2211 case DeclarationName::ObjCOneArgSelector:
2212 case DeclarationName::ObjCMultiArgSelector:
2213 return Import(FromName.getObjCSelector());
2214
2215 case DeclarationName::CXXConstructorName: {
2216 QualType T = Import(FromName.getCXXNameType());
2217 if (T.isNull())
2218 return DeclarationName();
2219
2220 return ToContext.DeclarationNames.getCXXConstructorName(
2221 ToContext.getCanonicalType(T));
2222 }
2223
2224 case DeclarationName::CXXDestructorName: {
2225 QualType T = Import(FromName.getCXXNameType());
2226 if (T.isNull())
2227 return DeclarationName();
2228
2229 return ToContext.DeclarationNames.getCXXDestructorName(
2230 ToContext.getCanonicalType(T));
2231 }
2232
2233 case DeclarationName::CXXConversionFunctionName: {
2234 QualType T = Import(FromName.getCXXNameType());
2235 if (T.isNull())
2236 return DeclarationName();
2237
2238 return ToContext.DeclarationNames.getCXXConversionFunctionName(
2239 ToContext.getCanonicalType(T));
2240 }
2241
2242 case DeclarationName::CXXOperatorName:
2243 return ToContext.DeclarationNames.getCXXOperatorName(
2244 FromName.getCXXOverloadedOperator());
2245
2246 case DeclarationName::CXXLiteralOperatorName:
2247 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
2248 Import(FromName.getCXXLiteralIdentifier()));
2249
2250 case DeclarationName::CXXUsingDirective:
2251 // FIXME: STATICS!
2252 return DeclarationName::getUsingDirectiveName();
2253 }
2254
2255 // Silence bogus GCC warning
2256 return DeclarationName();
2257}
2258
2259IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
2260 if (!FromId)
2261 return 0;
2262
2263 return &ToContext.Idents.get(FromId->getName());
2264}
Douglas Gregor089459a2010-02-08 21:09:39 +00002265
2266DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
2267 DeclContext *DC,
2268 unsigned IDNS,
2269 NamedDecl **Decls,
2270 unsigned NumDecls) {
2271 return Name;
2272}
2273
2274DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00002275 return Diags.Report(FullSourceLoc(Loc, ToContext.getSourceManager()),
2276 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00002277}
2278
2279DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor4800d952010-02-11 19:21:55 +00002280 return Diags.Report(FullSourceLoc(Loc, FromContext.getSourceManager()),
2281 DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00002282}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002283
2284Decl *ASTImporter::Imported(Decl *From, Decl *To) {
2285 ImportedDecls[From] = To;
2286 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00002287}
Douglas Gregorea35d112010-02-15 23:54:17 +00002288
2289bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
2290 llvm::DenseMap<Type *, Type *>::iterator Pos
2291 = ImportedTypes.find(From.getTypePtr());
2292 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
2293 return true;
2294
2295 StructuralEquivalenceContext SEC(FromContext, ToContext, Diags,
2296 NonEquivalentDecls);
2297 return SEC.IsStructurallyEquivalent(From, To);
2298}