blob: 7b16809a1f150f8a0761cd9b143403b64f918f32 [file] [log] [blame]
Douglas Gregor96e578d2010-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 Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000042
43 // Importing types
Douglas Gregore4c83e42010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000045 QualType VisitBuiltinType(BuiltinType *T);
46 QualType VisitComplexType(ComplexType *T);
47 QualType VisitPointerType(PointerType *T);
48 QualType VisitBlockPointerType(BlockPointerType *T);
49 QualType VisitLValueReferenceType(LValueReferenceType *T);
50 QualType VisitRValueReferenceType(RValueReferenceType *T);
51 QualType VisitMemberPointerType(MemberPointerType *T);
52 QualType VisitConstantArrayType(ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
54 QualType VisitVariableArrayType(VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(VectorType *T);
58 QualType VisitExtVectorType(ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitTypedefType(TypedefType *T);
63 QualType VisitTypeOfExprType(TypeOfExprType *T);
64 // FIXME: DependentTypeOfExprType
65 QualType VisitTypeOfType(TypeOfType *T);
66 QualType VisitDecltypeType(DecltypeType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(RecordType *T);
69 QualType VisitEnumType(EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
72 // FIXME: TemplateSpecializationType
Abramo Bagnara6150c882010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor96e578d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCall8b07ec22010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas Gregor968d6332010-02-21 18:24:45 +000086 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregor5c73e912010-02-11 00:48:18 +000087 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000088 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregore4c83e42010-02-09 22:48:33 +000089 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000090 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000091 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000092 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000093 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000094 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000095 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +000096 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
97 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
98 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
99 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000100 Decl *VisitFieldDecl(FieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000101 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000102 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000103 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000104 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000105 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000106 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000107 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000108 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000109 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000110 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000111 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
112
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000113 // Importing statements
114 Stmt *VisitStmt(Stmt *S);
115
116 // Importing expressions
117 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000118 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000119 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000120 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000121 Expr *VisitParenExpr(ParenExpr *E);
122 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000123 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000124 Expr *VisitBinaryOperator(BinaryOperator *E);
125 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000126 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000127 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000128 };
129}
130
131//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000132// Structural Equivalence
133//----------------------------------------------------------------------------
134
135namespace {
136 struct StructuralEquivalenceContext {
137 /// \brief AST contexts for which we are checking structural equivalence.
138 ASTContext &C1, &C2;
139
Douglas Gregor3996e242010-02-15 22:01:00 +0000140 /// \brief The set of "tentative" equivalences between two canonical
141 /// declarations, mapping from a declaration in the first context to the
142 /// declaration in the second context that we believe to be equivalent.
143 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
144
145 /// \brief Queue of declarations in the first context whose equivalence
146 /// with a declaration in the second context still needs to be verified.
147 std::deque<Decl *> DeclsToCheck;
148
Douglas Gregorb4964f72010-02-15 23:54:17 +0000149 /// \brief Declaration (from, to) pairs that are known not to be equivalent
150 /// (which we have already complained about).
151 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
152
Douglas Gregor3996e242010-02-15 22:01:00 +0000153 /// \brief Whether we're being strict about the spelling of types when
154 /// unifying two types.
155 bool StrictTypeSpelling;
156
157 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000158 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000159 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000160 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000161 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000162
163 /// \brief Determine whether the two declarations are structurally
164 /// equivalent.
165 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
166
167 /// \brief Determine whether the two types are structurally equivalent.
168 bool IsStructurallyEquivalent(QualType T1, QualType T2);
169
170 private:
171 /// \brief Finish checking all of the structural equivalences.
172 ///
173 /// \returns true if an error occurred, false otherwise.
174 bool Finish();
175
176 public:
177 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000178 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000179 }
180
181 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000182 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000183 }
184 };
185}
186
187static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
188 QualType T1, QualType T2);
189static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
190 Decl *D1, Decl *D2);
191
192/// \brief Determine if two APInts have the same value, after zero-extending
193/// one of them (if needed!) to ensure that the bit-widths match.
194static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
195 if (I1.getBitWidth() == I2.getBitWidth())
196 return I1 == I2;
197
198 if (I1.getBitWidth() > I2.getBitWidth())
199 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
200
201 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
202}
203
204/// \brief Determine if two APSInts have the same value, zero- or sign-extending
205/// as needed.
206static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
207 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
208 return I1 == I2;
209
210 // Check for a bit-width mismatch.
211 if (I1.getBitWidth() > I2.getBitWidth())
212 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
213 else if (I2.getBitWidth() > I1.getBitWidth())
214 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
215
216 // We have a signedness mismatch. Turn the signed value into an unsigned
217 // value.
218 if (I1.isSigned()) {
219 if (I1.isNegative())
220 return false;
221
222 return llvm::APSInt(I1, true) == I2;
223 }
224
225 if (I2.isNegative())
226 return false;
227
228 return I1 == llvm::APSInt(I2, true);
229}
230
231/// \brief Determine structural equivalence of two expressions.
232static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
233 Expr *E1, Expr *E2) {
234 if (!E1 || !E2)
235 return E1 == E2;
236
237 // FIXME: Actually perform a structural comparison!
238 return true;
239}
240
241/// \brief Determine whether two identifiers are equivalent.
242static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
243 const IdentifierInfo *Name2) {
244 if (!Name1 || !Name2)
245 return Name1 == Name2;
246
247 return Name1->getName() == Name2->getName();
248}
249
250/// \brief Determine whether two nested-name-specifiers are equivalent.
251static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
252 NestedNameSpecifier *NNS1,
253 NestedNameSpecifier *NNS2) {
254 // FIXME: Implement!
255 return true;
256}
257
258/// \brief Determine whether two template arguments are equivalent.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 const TemplateArgument &Arg1,
261 const TemplateArgument &Arg2) {
262 // FIXME: Implement!
263 return true;
264}
265
266/// \brief Determine structural equivalence for the common part of array
267/// types.
268static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
269 const ArrayType *Array1,
270 const ArrayType *Array2) {
271 if (!IsStructurallyEquivalent(Context,
272 Array1->getElementType(),
273 Array2->getElementType()))
274 return false;
275 if (Array1->getSizeModifier() != Array2->getSizeModifier())
276 return false;
277 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
278 return false;
279
280 return true;
281}
282
283/// \brief Determine structural equivalence of two types.
284static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
285 QualType T1, QualType T2) {
286 if (T1.isNull() || T2.isNull())
287 return T1.isNull() && T2.isNull();
288
289 if (!Context.StrictTypeSpelling) {
290 // We aren't being strict about token-to-token equivalence of types,
291 // so map down to the canonical type.
292 T1 = Context.C1.getCanonicalType(T1);
293 T2 = Context.C2.getCanonicalType(T2);
294 }
295
296 if (T1.getQualifiers() != T2.getQualifiers())
297 return false;
298
Douglas Gregorb4964f72010-02-15 23:54:17 +0000299 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000300
Douglas Gregorb4964f72010-02-15 23:54:17 +0000301 if (T1->getTypeClass() != T2->getTypeClass()) {
302 // Compare function types with prototypes vs. without prototypes as if
303 // both did not have prototypes.
304 if (T1->getTypeClass() == Type::FunctionProto &&
305 T2->getTypeClass() == Type::FunctionNoProto)
306 TC = Type::FunctionNoProto;
307 else if (T1->getTypeClass() == Type::FunctionNoProto &&
308 T2->getTypeClass() == Type::FunctionProto)
309 TC = Type::FunctionNoProto;
310 else
311 return false;
312 }
313
314 switch (TC) {
315 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000316 // FIXME: Deal with Char_S/Char_U.
317 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
318 return false;
319 break;
320
321 case Type::Complex:
322 if (!IsStructurallyEquivalent(Context,
323 cast<ComplexType>(T1)->getElementType(),
324 cast<ComplexType>(T2)->getElementType()))
325 return false;
326 break;
327
328 case Type::Pointer:
329 if (!IsStructurallyEquivalent(Context,
330 cast<PointerType>(T1)->getPointeeType(),
331 cast<PointerType>(T2)->getPointeeType()))
332 return false;
333 break;
334
335 case Type::BlockPointer:
336 if (!IsStructurallyEquivalent(Context,
337 cast<BlockPointerType>(T1)->getPointeeType(),
338 cast<BlockPointerType>(T2)->getPointeeType()))
339 return false;
340 break;
341
342 case Type::LValueReference:
343 case Type::RValueReference: {
344 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
345 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
346 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
347 return false;
348 if (Ref1->isInnerRef() != Ref2->isInnerRef())
349 return false;
350 if (!IsStructurallyEquivalent(Context,
351 Ref1->getPointeeTypeAsWritten(),
352 Ref2->getPointeeTypeAsWritten()))
353 return false;
354 break;
355 }
356
357 case Type::MemberPointer: {
358 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
359 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
360 if (!IsStructurallyEquivalent(Context,
361 MemPtr1->getPointeeType(),
362 MemPtr2->getPointeeType()))
363 return false;
364 if (!IsStructurallyEquivalent(Context,
365 QualType(MemPtr1->getClass(), 0),
366 QualType(MemPtr2->getClass(), 0)))
367 return false;
368 break;
369 }
370
371 case Type::ConstantArray: {
372 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
373 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
374 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
375 return false;
376
377 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
378 return false;
379 break;
380 }
381
382 case Type::IncompleteArray:
383 if (!IsArrayStructurallyEquivalent(Context,
384 cast<ArrayType>(T1),
385 cast<ArrayType>(T2)))
386 return false;
387 break;
388
389 case Type::VariableArray: {
390 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
391 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
392 if (!IsStructurallyEquivalent(Context,
393 Array1->getSizeExpr(), Array2->getSizeExpr()))
394 return false;
395
396 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
397 return false;
398
399 break;
400 }
401
402 case Type::DependentSizedArray: {
403 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
404 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
405 if (!IsStructurallyEquivalent(Context,
406 Array1->getSizeExpr(), Array2->getSizeExpr()))
407 return false;
408
409 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
410 return false;
411
412 break;
413 }
414
415 case Type::DependentSizedExtVector: {
416 const DependentSizedExtVectorType *Vec1
417 = cast<DependentSizedExtVectorType>(T1);
418 const DependentSizedExtVectorType *Vec2
419 = cast<DependentSizedExtVectorType>(T2);
420 if (!IsStructurallyEquivalent(Context,
421 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
422 return false;
423 if (!IsStructurallyEquivalent(Context,
424 Vec1->getElementType(),
425 Vec2->getElementType()))
426 return false;
427 break;
428 }
429
430 case Type::Vector:
431 case Type::ExtVector: {
432 const VectorType *Vec1 = cast<VectorType>(T1);
433 const VectorType *Vec2 = cast<VectorType>(T2);
434 if (!IsStructurallyEquivalent(Context,
435 Vec1->getElementType(),
436 Vec2->getElementType()))
437 return false;
438 if (Vec1->getNumElements() != Vec2->getNumElements())
439 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000440 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000441 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000442 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000443 }
444
445 case Type::FunctionProto: {
446 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
447 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
448 if (Proto1->getNumArgs() != Proto2->getNumArgs())
449 return false;
450 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
451 if (!IsStructurallyEquivalent(Context,
452 Proto1->getArgType(I),
453 Proto2->getArgType(I)))
454 return false;
455 }
456 if (Proto1->isVariadic() != Proto2->isVariadic())
457 return false;
458 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
459 return false;
460 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
461 return false;
462 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
463 return false;
464 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
465 if (!IsStructurallyEquivalent(Context,
466 Proto1->getExceptionType(I),
467 Proto2->getExceptionType(I)))
468 return false;
469 }
470 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
471 return false;
472
473 // Fall through to check the bits common with FunctionNoProtoType.
474 }
475
476 case Type::FunctionNoProto: {
477 const FunctionType *Function1 = cast<FunctionType>(T1);
478 const FunctionType *Function2 = cast<FunctionType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Function1->getResultType(),
481 Function2->getResultType()))
482 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000483 if (Function1->getExtInfo() != Function2->getExtInfo())
484 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000485 break;
486 }
487
488 case Type::UnresolvedUsing:
489 if (!IsStructurallyEquivalent(Context,
490 cast<UnresolvedUsingType>(T1)->getDecl(),
491 cast<UnresolvedUsingType>(T2)->getDecl()))
492 return false;
493
494 break;
495
496 case Type::Typedef:
497 if (!IsStructurallyEquivalent(Context,
498 cast<TypedefType>(T1)->getDecl(),
499 cast<TypedefType>(T2)->getDecl()))
500 return false;
501 break;
502
503 case Type::TypeOfExpr:
504 if (!IsStructurallyEquivalent(Context,
505 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
506 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
507 return false;
508 break;
509
510 case Type::TypeOf:
511 if (!IsStructurallyEquivalent(Context,
512 cast<TypeOfType>(T1)->getUnderlyingType(),
513 cast<TypeOfType>(T2)->getUnderlyingType()))
514 return false;
515 break;
516
517 case Type::Decltype:
518 if (!IsStructurallyEquivalent(Context,
519 cast<DecltypeType>(T1)->getUnderlyingExpr(),
520 cast<DecltypeType>(T2)->getUnderlyingExpr()))
521 return false;
522 break;
523
524 case Type::Record:
525 case Type::Enum:
526 if (!IsStructurallyEquivalent(Context,
527 cast<TagType>(T1)->getDecl(),
528 cast<TagType>(T2)->getDecl()))
529 return false;
530 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000531
Douglas Gregor3996e242010-02-15 22:01:00 +0000532 case Type::TemplateTypeParm: {
533 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
534 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
535 if (Parm1->getDepth() != Parm2->getDepth())
536 return false;
537 if (Parm1->getIndex() != Parm2->getIndex())
538 return false;
539 if (Parm1->isParameterPack() != Parm2->isParameterPack())
540 return false;
541
542 // Names of template type parameters are never significant.
543 break;
544 }
545
546 case Type::SubstTemplateTypeParm: {
547 const SubstTemplateTypeParmType *Subst1
548 = cast<SubstTemplateTypeParmType>(T1);
549 const SubstTemplateTypeParmType *Subst2
550 = cast<SubstTemplateTypeParmType>(T2);
551 if (!IsStructurallyEquivalent(Context,
552 QualType(Subst1->getReplacedParameter(), 0),
553 QualType(Subst2->getReplacedParameter(), 0)))
554 return false;
555 if (!IsStructurallyEquivalent(Context,
556 Subst1->getReplacementType(),
557 Subst2->getReplacementType()))
558 return false;
559 break;
560 }
561
562 case Type::TemplateSpecialization: {
563 const TemplateSpecializationType *Spec1
564 = cast<TemplateSpecializationType>(T1);
565 const TemplateSpecializationType *Spec2
566 = cast<TemplateSpecializationType>(T2);
567 if (!IsStructurallyEquivalent(Context,
568 Spec1->getTemplateName(),
569 Spec2->getTemplateName()))
570 return false;
571 if (Spec1->getNumArgs() != Spec2->getNumArgs())
572 return false;
573 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
574 if (!IsStructurallyEquivalent(Context,
575 Spec1->getArg(I), Spec2->getArg(I)))
576 return false;
577 }
578 break;
579 }
580
Abramo Bagnara6150c882010-05-11 21:36:43 +0000581 case Type::Elaborated: {
582 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
583 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
584 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
585 if (Elab1->getKeyword() != Elab2->getKeyword())
586 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000587 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000588 Elab1->getQualifier(),
589 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000590 return false;
591 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000592 Elab1->getNamedType(),
593 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000594 return false;
595 break;
596 }
597
John McCalle78aac42010-03-10 03:28:59 +0000598 case Type::InjectedClassName: {
599 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
600 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
601 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000602 Inj1->getInjectedSpecializationType(),
603 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000604 return false;
605 break;
606 }
607
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000608 case Type::DependentName: {
609 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
610 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000611 if (!IsStructurallyEquivalent(Context,
612 Typename1->getQualifier(),
613 Typename2->getQualifier()))
614 return false;
615 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
616 Typename2->getIdentifier()))
617 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000618
619 break;
620 }
621
John McCallc392f372010-06-11 00:33:02 +0000622 case Type::DependentTemplateSpecialization: {
623 const DependentTemplateSpecializationType *Spec1 =
624 cast<DependentTemplateSpecializationType>(T1);
625 const DependentTemplateSpecializationType *Spec2 =
626 cast<DependentTemplateSpecializationType>(T2);
627 if (!IsStructurallyEquivalent(Context,
628 Spec1->getQualifier(),
629 Spec2->getQualifier()))
630 return false;
631 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
632 Spec2->getIdentifier()))
633 return false;
634 if (Spec1->getNumArgs() != Spec2->getNumArgs())
635 return false;
636 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
637 if (!IsStructurallyEquivalent(Context,
638 Spec1->getArg(I), Spec2->getArg(I)))
639 return false;
640 }
641 break;
642 }
643
Douglas Gregor3996e242010-02-15 22:01:00 +0000644 case Type::ObjCInterface: {
645 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
646 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
647 if (!IsStructurallyEquivalent(Context,
648 Iface1->getDecl(), Iface2->getDecl()))
649 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000650 break;
651 }
652
653 case Type::ObjCObject: {
654 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
655 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
656 if (!IsStructurallyEquivalent(Context,
657 Obj1->getBaseType(),
658 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000659 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000660 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
661 return false;
662 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000663 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000664 Obj1->getProtocol(I),
665 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000666 return false;
667 }
668 break;
669 }
670
671 case Type::ObjCObjectPointer: {
672 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
673 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
674 if (!IsStructurallyEquivalent(Context,
675 Ptr1->getPointeeType(),
676 Ptr2->getPointeeType()))
677 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000678 break;
679 }
680
681 } // end switch
682
683 return true;
684}
685
686/// \brief Determine structural equivalence of two records.
687static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
688 RecordDecl *D1, RecordDecl *D2) {
689 if (D1->isUnion() != D2->isUnion()) {
690 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
691 << Context.C2.getTypeDeclType(D2);
692 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
693 << D1->getDeclName() << (unsigned)D1->getTagKind();
694 return false;
695 }
696
Douglas Gregorb4964f72010-02-15 23:54:17 +0000697 // Compare the definitions of these two records. If either or both are
698 // incomplete, we assume that they are equivalent.
699 D1 = D1->getDefinition();
700 D2 = D2->getDefinition();
701 if (!D1 || !D2)
702 return true;
703
Douglas Gregor3996e242010-02-15 22:01:00 +0000704 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
705 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
706 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
707 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
708 << Context.C2.getTypeDeclType(D2);
709 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
710 << D2CXX->getNumBases();
711 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
712 << D1CXX->getNumBases();
713 return false;
714 }
715
716 // Check the base classes.
717 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
718 BaseEnd1 = D1CXX->bases_end(),
719 Base2 = D2CXX->bases_begin();
720 Base1 != BaseEnd1;
721 ++Base1, ++Base2) {
722 if (!IsStructurallyEquivalent(Context,
723 Base1->getType(), Base2->getType())) {
724 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
725 << Context.C2.getTypeDeclType(D2);
726 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
727 << Base2->getType()
728 << Base2->getSourceRange();
729 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
730 << Base1->getType()
731 << Base1->getSourceRange();
732 return false;
733 }
734
735 // Check virtual vs. non-virtual inheritance mismatch.
736 if (Base1->isVirtual() != Base2->isVirtual()) {
737 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
738 << Context.C2.getTypeDeclType(D2);
739 Context.Diag2(Base2->getSourceRange().getBegin(),
740 diag::note_odr_virtual_base)
741 << Base2->isVirtual() << Base2->getSourceRange();
742 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
743 << Base1->isVirtual()
744 << Base1->getSourceRange();
745 return false;
746 }
747 }
748 } else if (D1CXX->getNumBases() > 0) {
749 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
750 << Context.C2.getTypeDeclType(D2);
751 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
752 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
753 << Base1->getType()
754 << Base1->getSourceRange();
755 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
756 return false;
757 }
758 }
759
760 // Check the fields for consistency.
761 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
762 Field2End = D2->field_end();
763 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
764 Field1End = D1->field_end();
765 Field1 != Field1End;
766 ++Field1, ++Field2) {
767 if (Field2 == Field2End) {
768 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
769 << Context.C2.getTypeDeclType(D2);
770 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
771 << Field1->getDeclName() << Field1->getType();
772 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
773 return false;
774 }
775
776 if (!IsStructurallyEquivalent(Context,
777 Field1->getType(), Field2->getType())) {
778 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
779 << Context.C2.getTypeDeclType(D2);
780 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
781 << Field2->getDeclName() << Field2->getType();
782 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
783 << Field1->getDeclName() << Field1->getType();
784 return false;
785 }
786
787 if (Field1->isBitField() != Field2->isBitField()) {
788 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
789 << Context.C2.getTypeDeclType(D2);
790 if (Field1->isBitField()) {
791 llvm::APSInt Bits;
792 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
793 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
794 << Field1->getDeclName() << Field1->getType()
795 << Bits.toString(10, false);
796 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
797 << Field2->getDeclName();
798 } else {
799 llvm::APSInt Bits;
800 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
801 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
802 << Field2->getDeclName() << Field2->getType()
803 << Bits.toString(10, false);
804 Context.Diag1(Field1->getLocation(),
805 diag::note_odr_not_bit_field)
806 << Field1->getDeclName();
807 }
808 return false;
809 }
810
811 if (Field1->isBitField()) {
812 // Make sure that the bit-fields are the same length.
813 llvm::APSInt Bits1, Bits2;
814 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
815 return false;
816 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
817 return false;
818
819 if (!IsSameValue(Bits1, Bits2)) {
820 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
821 << Context.C2.getTypeDeclType(D2);
822 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
823 << Field2->getDeclName() << Field2->getType()
824 << Bits2.toString(10, false);
825 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
826 << Field1->getDeclName() << Field1->getType()
827 << Bits1.toString(10, false);
828 return false;
829 }
830 }
831 }
832
833 if (Field2 != Field2End) {
834 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
835 << Context.C2.getTypeDeclType(D2);
836 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
837 << Field2->getDeclName() << Field2->getType();
838 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
839 return false;
840 }
841
842 return true;
843}
844
845/// \brief Determine structural equivalence of two enums.
846static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
847 EnumDecl *D1, EnumDecl *D2) {
848 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
849 EC2End = D2->enumerator_end();
850 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
851 EC1End = D1->enumerator_end();
852 EC1 != EC1End; ++EC1, ++EC2) {
853 if (EC2 == EC2End) {
854 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
855 << Context.C2.getTypeDeclType(D2);
856 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
857 << EC1->getDeclName()
858 << EC1->getInitVal().toString(10);
859 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
860 return false;
861 }
862
863 llvm::APSInt Val1 = EC1->getInitVal();
864 llvm::APSInt Val2 = EC2->getInitVal();
865 if (!IsSameValue(Val1, Val2) ||
866 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
867 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
868 << Context.C2.getTypeDeclType(D2);
869 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
870 << EC2->getDeclName()
871 << EC2->getInitVal().toString(10);
872 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
873 << EC1->getDeclName()
874 << EC1->getInitVal().toString(10);
875 return false;
876 }
877 }
878
879 if (EC2 != EC2End) {
880 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
881 << Context.C2.getTypeDeclType(D2);
882 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
883 << EC2->getDeclName()
884 << EC2->getInitVal().toString(10);
885 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
886 return false;
887 }
888
889 return true;
890}
891
892/// \brief Determine structural equivalence of two declarations.
893static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
894 Decl *D1, Decl *D2) {
895 // FIXME: Check for known structural equivalences via a callback of some sort.
896
Douglas Gregorb4964f72010-02-15 23:54:17 +0000897 // Check whether we already know that these two declarations are not
898 // structurally equivalent.
899 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
900 D2->getCanonicalDecl())))
901 return false;
902
Douglas Gregor3996e242010-02-15 22:01:00 +0000903 // Determine whether we've already produced a tentative equivalence for D1.
904 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
905 if (EquivToD1)
906 return EquivToD1 == D2->getCanonicalDecl();
907
908 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
909 EquivToD1 = D2->getCanonicalDecl();
910 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
911 return true;
912}
913
914bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
915 Decl *D2) {
916 if (!::IsStructurallyEquivalent(*this, D1, D2))
917 return false;
918
919 return !Finish();
920}
921
922bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
923 QualType T2) {
924 if (!::IsStructurallyEquivalent(*this, T1, T2))
925 return false;
926
927 return !Finish();
928}
929
930bool StructuralEquivalenceContext::Finish() {
931 while (!DeclsToCheck.empty()) {
932 // Check the next declaration.
933 Decl *D1 = DeclsToCheck.front();
934 DeclsToCheck.pop_front();
935
936 Decl *D2 = TentativeEquivalences[D1];
937 assert(D2 && "Unrecorded tentative equivalence?");
938
Douglas Gregorb4964f72010-02-15 23:54:17 +0000939 bool Equivalent = true;
940
Douglas Gregor3996e242010-02-15 22:01:00 +0000941 // FIXME: Switch on all declaration kinds. For now, we're just going to
942 // check the obvious ones.
943 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
944 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
945 // Check for equivalent structure names.
946 IdentifierInfo *Name1 = Record1->getIdentifier();
947 if (!Name1 && Record1->getTypedefForAnonDecl())
948 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
949 IdentifierInfo *Name2 = Record2->getIdentifier();
950 if (!Name2 && Record2->getTypedefForAnonDecl())
951 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000952 if (!::IsStructurallyEquivalent(Name1, Name2) ||
953 !::IsStructurallyEquivalent(*this, Record1, Record2))
954 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000955 } else {
956 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000957 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000958 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000959 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000960 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
961 // Check for equivalent enum names.
962 IdentifierInfo *Name1 = Enum1->getIdentifier();
963 if (!Name1 && Enum1->getTypedefForAnonDecl())
964 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
965 IdentifierInfo *Name2 = Enum2->getIdentifier();
966 if (!Name2 && Enum2->getTypedefForAnonDecl())
967 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +0000968 if (!::IsStructurallyEquivalent(Name1, Name2) ||
969 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
970 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000971 } else {
972 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +0000973 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000974 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000975 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000976 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
977 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000978 Typedef2->getIdentifier()) ||
979 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +0000980 Typedef1->getUnderlyingType(),
981 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +0000982 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000983 } else {
984 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +0000985 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000986 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000987 }
Douglas Gregorb4964f72010-02-15 23:54:17 +0000988
989 if (!Equivalent) {
990 // Note that these two declarations are not equivalent (and we already
991 // know about it).
992 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
993 D2->getCanonicalDecl()));
994 return true;
995 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000996 // FIXME: Check other declaration kinds!
997 }
998
999 return false;
1000}
1001
1002//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001003// Import Types
1004//----------------------------------------------------------------------------
1005
Douglas Gregore4c83e42010-02-09 22:48:33 +00001006QualType ASTNodeImporter::VisitType(Type *T) {
1007 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1008 << T->getTypeClassName();
1009 return QualType();
1010}
1011
Douglas Gregor96e578d2010-02-05 17:54:41 +00001012QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1013 switch (T->getKind()) {
1014 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1015 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1016
1017 case BuiltinType::Char_U:
1018 // The context we're importing from has an unsigned 'char'. If we're
1019 // importing into a context with a signed 'char', translate to
1020 // 'unsigned char' instead.
1021 if (Importer.getToContext().getLangOptions().CharIsSigned)
1022 return Importer.getToContext().UnsignedCharTy;
1023
1024 return Importer.getToContext().CharTy;
1025
1026 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1027
1028 case BuiltinType::Char16:
1029 // FIXME: Make sure that the "to" context supports C++!
1030 return Importer.getToContext().Char16Ty;
1031
1032 case BuiltinType::Char32:
1033 // FIXME: Make sure that the "to" context supports C++!
1034 return Importer.getToContext().Char32Ty;
1035
1036 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1037 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1038 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1039 case BuiltinType::ULongLong:
1040 return Importer.getToContext().UnsignedLongLongTy;
1041 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1042
1043 case BuiltinType::Char_S:
1044 // The context we're importing from has an unsigned 'char'. If we're
1045 // importing into a context with a signed 'char', translate to
1046 // 'unsigned char' instead.
1047 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1048 return Importer.getToContext().SignedCharTy;
1049
1050 return Importer.getToContext().CharTy;
1051
1052 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1053 case BuiltinType::WChar:
1054 // FIXME: If not in C++, shall we translate to the C equivalent of
1055 // wchar_t?
1056 return Importer.getToContext().WCharTy;
1057
1058 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1059 case BuiltinType::Int : return Importer.getToContext().IntTy;
1060 case BuiltinType::Long : return Importer.getToContext().LongTy;
1061 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1062 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1063 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1064 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1065 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1066
1067 case BuiltinType::NullPtr:
1068 // FIXME: Make sure that the "to" context supports C++0x!
1069 return Importer.getToContext().NullPtrTy;
1070
1071 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1072 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1073 case BuiltinType::UndeducedAuto:
1074 // FIXME: Make sure that the "to" context supports C++0x!
1075 return Importer.getToContext().UndeducedAutoTy;
1076
1077 case BuiltinType::ObjCId:
1078 // FIXME: Make sure that the "to" context supports Objective-C!
1079 return Importer.getToContext().ObjCBuiltinIdTy;
1080
1081 case BuiltinType::ObjCClass:
1082 return Importer.getToContext().ObjCBuiltinClassTy;
1083
1084 case BuiltinType::ObjCSel:
1085 return Importer.getToContext().ObjCBuiltinSelTy;
1086 }
1087
1088 return QualType();
1089}
1090
1091QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1092 QualType ToElementType = Importer.Import(T->getElementType());
1093 if (ToElementType.isNull())
1094 return QualType();
1095
1096 return Importer.getToContext().getComplexType(ToElementType);
1097}
1098
1099QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1100 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1101 if (ToPointeeType.isNull())
1102 return QualType();
1103
1104 return Importer.getToContext().getPointerType(ToPointeeType);
1105}
1106
1107QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1108 // FIXME: Check for blocks support in "to" context.
1109 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1110 if (ToPointeeType.isNull())
1111 return QualType();
1112
1113 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1114}
1115
1116QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1117 // FIXME: Check for C++ support in "to" context.
1118 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1119 if (ToPointeeType.isNull())
1120 return QualType();
1121
1122 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1123}
1124
1125QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1126 // FIXME: Check for C++0x support in "to" context.
1127 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1128 if (ToPointeeType.isNull())
1129 return QualType();
1130
1131 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1132}
1133
1134QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1135 // FIXME: Check for C++ support in "to" context.
1136 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1137 if (ToPointeeType.isNull())
1138 return QualType();
1139
1140 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1141 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1142 ClassType.getTypePtr());
1143}
1144
1145QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1146 QualType ToElementType = Importer.Import(T->getElementType());
1147 if (ToElementType.isNull())
1148 return QualType();
1149
1150 return Importer.getToContext().getConstantArrayType(ToElementType,
1151 T->getSize(),
1152 T->getSizeModifier(),
1153 T->getIndexTypeCVRQualifiers());
1154}
1155
1156QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1157 QualType ToElementType = Importer.Import(T->getElementType());
1158 if (ToElementType.isNull())
1159 return QualType();
1160
1161 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1162 T->getSizeModifier(),
1163 T->getIndexTypeCVRQualifiers());
1164}
1165
1166QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1167 QualType ToElementType = Importer.Import(T->getElementType());
1168 if (ToElementType.isNull())
1169 return QualType();
1170
1171 Expr *Size = Importer.Import(T->getSizeExpr());
1172 if (!Size)
1173 return QualType();
1174
1175 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1176 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1177 T->getSizeModifier(),
1178 T->getIndexTypeCVRQualifiers(),
1179 Brackets);
1180}
1181
1182QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1183 QualType ToElementType = Importer.Import(T->getElementType());
1184 if (ToElementType.isNull())
1185 return QualType();
1186
1187 return Importer.getToContext().getVectorType(ToElementType,
1188 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001189 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001190}
1191
1192QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1193 QualType ToElementType = Importer.Import(T->getElementType());
1194 if (ToElementType.isNull())
1195 return QualType();
1196
1197 return Importer.getToContext().getExtVectorType(ToElementType,
1198 T->getNumElements());
1199}
1200
1201QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1202 // FIXME: What happens if we're importing a function without a prototype
1203 // into C++? Should we make it variadic?
1204 QualType ToResultType = Importer.Import(T->getResultType());
1205 if (ToResultType.isNull())
1206 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001207
Douglas Gregor96e578d2010-02-05 17:54:41 +00001208 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001209 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001210}
1211
1212QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1213 QualType ToResultType = Importer.Import(T->getResultType());
1214 if (ToResultType.isNull())
1215 return QualType();
1216
1217 // Import argument types
1218 llvm::SmallVector<QualType, 4> ArgTypes;
1219 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1220 AEnd = T->arg_type_end();
1221 A != AEnd; ++A) {
1222 QualType ArgType = Importer.Import(*A);
1223 if (ArgType.isNull())
1224 return QualType();
1225 ArgTypes.push_back(ArgType);
1226 }
1227
1228 // Import exception types
1229 llvm::SmallVector<QualType, 4> ExceptionTypes;
1230 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1231 EEnd = T->exception_end();
1232 E != EEnd; ++E) {
1233 QualType ExceptionType = Importer.Import(*E);
1234 if (ExceptionType.isNull())
1235 return QualType();
1236 ExceptionTypes.push_back(ExceptionType);
1237 }
1238
1239 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1240 ArgTypes.size(),
1241 T->isVariadic(),
1242 T->getTypeQuals(),
1243 T->hasExceptionSpec(),
1244 T->hasAnyExceptionSpec(),
1245 ExceptionTypes.size(),
1246 ExceptionTypes.data(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001247 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001248}
1249
1250QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1251 TypedefDecl *ToDecl
1252 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1253 if (!ToDecl)
1254 return QualType();
1255
1256 return Importer.getToContext().getTypeDeclType(ToDecl);
1257}
1258
1259QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1260 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1261 if (!ToExpr)
1262 return QualType();
1263
1264 return Importer.getToContext().getTypeOfExprType(ToExpr);
1265}
1266
1267QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1268 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1269 if (ToUnderlyingType.isNull())
1270 return QualType();
1271
1272 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1273}
1274
1275QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1276 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1277 if (!ToExpr)
1278 return QualType();
1279
1280 return Importer.getToContext().getDecltypeType(ToExpr);
1281}
1282
1283QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1284 RecordDecl *ToDecl
1285 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1286 if (!ToDecl)
1287 return QualType();
1288
1289 return Importer.getToContext().getTagDeclType(ToDecl);
1290}
1291
1292QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1293 EnumDecl *ToDecl
1294 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1295 if (!ToDecl)
1296 return QualType();
1297
1298 return Importer.getToContext().getTagDeclType(ToDecl);
1299}
1300
1301QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001302 NestedNameSpecifier *ToQualifier = 0;
1303 // Note: the qualifier in an ElaboratedType is optional.
1304 if (T->getQualifier()) {
1305 ToQualifier = Importer.Import(T->getQualifier());
1306 if (!ToQualifier)
1307 return QualType();
1308 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001309
1310 QualType ToNamedType = Importer.Import(T->getNamedType());
1311 if (ToNamedType.isNull())
1312 return QualType();
1313
Abramo Bagnara6150c882010-05-11 21:36:43 +00001314 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1315 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001316}
1317
1318QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1319 ObjCInterfaceDecl *Class
1320 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1321 if (!Class)
1322 return QualType();
1323
John McCall8b07ec22010-05-15 11:32:37 +00001324 return Importer.getToContext().getObjCInterfaceType(Class);
1325}
1326
1327QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1328 QualType ToBaseType = Importer.Import(T->getBaseType());
1329 if (ToBaseType.isNull())
1330 return QualType();
1331
Douglas Gregor96e578d2010-02-05 17:54:41 +00001332 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001333 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001334 PEnd = T->qual_end();
1335 P != PEnd; ++P) {
1336 ObjCProtocolDecl *Protocol
1337 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1338 if (!Protocol)
1339 return QualType();
1340 Protocols.push_back(Protocol);
1341 }
1342
John McCall8b07ec22010-05-15 11:32:37 +00001343 return Importer.getToContext().getObjCObjectType(ToBaseType,
1344 Protocols.data(),
1345 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346}
1347
1348QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1349 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1350 if (ToPointeeType.isNull())
1351 return QualType();
1352
John McCall8b07ec22010-05-15 11:32:37 +00001353 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001354}
1355
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001356//----------------------------------------------------------------------------
1357// Import Declarations
1358//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001359bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1360 DeclContext *&LexicalDC,
1361 DeclarationName &Name,
1362 SourceLocation &Loc) {
1363 // Import the context of this declaration.
1364 DC = Importer.ImportContext(D->getDeclContext());
1365 if (!DC)
1366 return true;
1367
1368 LexicalDC = DC;
1369 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1370 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1371 if (!LexicalDC)
1372 return true;
1373 }
1374
1375 // Import the name of this declaration.
1376 Name = Importer.Import(D->getDeclName());
1377 if (D->getDeclName() && !Name)
1378 return true;
1379
1380 // Import the location of this declaration.
1381 Loc = Importer.Import(D->getLocation());
1382 return false;
1383}
1384
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001385void
1386ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1387 DeclarationNameInfo& To) {
1388 // NOTE: To.Name and To.Loc are already imported.
1389 // We only have to import To.LocInfo.
1390 switch (To.getName().getNameKind()) {
1391 case DeclarationName::Identifier:
1392 case DeclarationName::ObjCZeroArgSelector:
1393 case DeclarationName::ObjCOneArgSelector:
1394 case DeclarationName::ObjCMultiArgSelector:
1395 case DeclarationName::CXXUsingDirective:
1396 return;
1397
1398 case DeclarationName::CXXOperatorName: {
1399 SourceRange Range = From.getCXXOperatorNameRange();
1400 To.setCXXOperatorNameRange(Importer.Import(Range));
1401 return;
1402 }
1403 case DeclarationName::CXXLiteralOperatorName: {
1404 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1405 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1406 return;
1407 }
1408 case DeclarationName::CXXConstructorName:
1409 case DeclarationName::CXXDestructorName:
1410 case DeclarationName::CXXConversionFunctionName: {
1411 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1412 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1413 return;
1414 }
1415 assert(0 && "Unknown name kind.");
1416 }
1417}
1418
Douglas Gregor968d6332010-02-21 18:24:45 +00001419void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1420 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1421 FromEnd = FromDC->decls_end();
1422 From != FromEnd;
1423 ++From)
1424 Importer.Import(*From);
1425}
1426
Douglas Gregor5c73e912010-02-11 00:48:18 +00001427bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001428 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001429 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001430 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001431 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001432 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001433}
1434
Douglas Gregor98c10182010-02-12 22:17:39 +00001435bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001436 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001437 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001438 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001439 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001440}
1441
Douglas Gregore4c83e42010-02-09 22:48:33 +00001442Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001443 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001444 << D->getDeclKindName();
1445 return 0;
1446}
1447
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001448Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1449 // Import the major distinguishing characteristics of this namespace.
1450 DeclContext *DC, *LexicalDC;
1451 DeclarationName Name;
1452 SourceLocation Loc;
1453 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1454 return 0;
1455
1456 NamespaceDecl *MergeWithNamespace = 0;
1457 if (!Name) {
1458 // This is an anonymous namespace. Adopt an existing anonymous
1459 // namespace if we can.
1460 // FIXME: Not testable.
1461 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1462 MergeWithNamespace = TU->getAnonymousNamespace();
1463 else
1464 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1465 } else {
1466 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1467 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1468 Lookup.first != Lookup.second;
1469 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001470 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001471 continue;
1472
1473 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1474 MergeWithNamespace = FoundNS;
1475 ConflictingDecls.clear();
1476 break;
1477 }
1478
1479 ConflictingDecls.push_back(*Lookup.first);
1480 }
1481
1482 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001483 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001484 ConflictingDecls.data(),
1485 ConflictingDecls.size());
1486 }
1487 }
1488
1489 // Create the "to" namespace, if needed.
1490 NamespaceDecl *ToNamespace = MergeWithNamespace;
1491 if (!ToNamespace) {
1492 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1493 Name.getAsIdentifierInfo());
1494 ToNamespace->setLexicalDeclContext(LexicalDC);
1495 LexicalDC->addDecl(ToNamespace);
1496
1497 // If this is an anonymous namespace, register it as the anonymous
1498 // namespace within its context.
1499 if (!Name) {
1500 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1501 TU->setAnonymousNamespace(ToNamespace);
1502 else
1503 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1504 }
1505 }
1506 Importer.Imported(D, ToNamespace);
1507
1508 ImportDeclContext(D);
1509
1510 return ToNamespace;
1511}
1512
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001513Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1514 // Import the major distinguishing characteristics of this typedef.
1515 DeclContext *DC, *LexicalDC;
1516 DeclarationName Name;
1517 SourceLocation Loc;
1518 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1519 return 0;
1520
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001521 // If this typedef is not in block scope, determine whether we've
1522 // seen a typedef with the same name (that we can merge with) or any
1523 // other entity by that name (which name lookup could conflict with).
1524 if (!DC->isFunctionOrMethod()) {
1525 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1526 unsigned IDNS = Decl::IDNS_Ordinary;
1527 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1528 Lookup.first != Lookup.second;
1529 ++Lookup.first) {
1530 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1531 continue;
1532 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001533 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1534 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001535 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001536 }
1537
1538 ConflictingDecls.push_back(*Lookup.first);
1539 }
1540
1541 if (!ConflictingDecls.empty()) {
1542 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1543 ConflictingDecls.data(),
1544 ConflictingDecls.size());
1545 if (!Name)
1546 return 0;
1547 }
1548 }
1549
Douglas Gregorb4964f72010-02-15 23:54:17 +00001550 // Import the underlying type of this typedef;
1551 QualType T = Importer.Import(D->getUnderlyingType());
1552 if (T.isNull())
1553 return 0;
1554
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001555 // Create the new typedef node.
1556 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1557 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1558 Loc, Name.getAsIdentifierInfo(),
1559 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00001560 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001561 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001562 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001563 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001564
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001565 return ToTypedef;
1566}
1567
Douglas Gregor98c10182010-02-12 22:17:39 +00001568Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1569 // Import the major distinguishing characteristics of this enum.
1570 DeclContext *DC, *LexicalDC;
1571 DeclarationName Name;
1572 SourceLocation Loc;
1573 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1574 return 0;
1575
1576 // Figure out what enum name we're looking for.
1577 unsigned IDNS = Decl::IDNS_Tag;
1578 DeclarationName SearchName = Name;
1579 if (!SearchName && D->getTypedefForAnonDecl()) {
1580 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1581 IDNS = Decl::IDNS_Ordinary;
1582 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1583 IDNS |= Decl::IDNS_Ordinary;
1584
1585 // We may already have an enum of the same name; try to find and match it.
1586 if (!DC->isFunctionOrMethod() && SearchName) {
1587 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1588 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1589 Lookup.first != Lookup.second;
1590 ++Lookup.first) {
1591 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1592 continue;
1593
1594 Decl *Found = *Lookup.first;
1595 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1596 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1597 Found = Tag->getDecl();
1598 }
1599
1600 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001601 if (IsStructuralMatch(D, FoundEnum))
1602 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001603 }
1604
1605 ConflictingDecls.push_back(*Lookup.first);
1606 }
1607
1608 if (!ConflictingDecls.empty()) {
1609 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1610 ConflictingDecls.data(),
1611 ConflictingDecls.size());
1612 }
1613 }
1614
1615 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001616 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001617 Name.getAsIdentifierInfo(),
1618 Importer.Import(D->getTagKeywordLoc()),
Douglas Gregor0bf31402010-10-08 23:50:27 +00001619 0, D->isScoped(), D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001620 // Import the qualifier, if any.
1621 if (D->getQualifier()) {
1622 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1623 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1624 D2->setQualifierInfo(NNS, NNSRange);
1625 }
Douglas Gregordd483172010-02-22 17:42:47 +00001626 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001627 D2->setLexicalDeclContext(LexicalDC);
1628 Importer.Imported(D, D2);
1629 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001630
1631 // Import the integer type.
1632 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1633 if (ToIntegerType.isNull())
1634 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001635 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001636
1637 // Import the definition
1638 if (D->isDefinition()) {
1639 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1640 if (T.isNull())
1641 return 0;
1642
1643 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1644 if (ToPromotionType.isNull())
1645 return 0;
1646
Douglas Gregor3996e242010-02-15 22:01:00 +00001647 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00001648 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00001649
1650 // FIXME: we might need to merge the number of positive or negative bits
1651 // if the enumerator lists don't match.
1652 D2->completeDefinition(T, ToPromotionType,
1653 D->getNumPositiveBits(),
1654 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00001655 }
1656
Douglas Gregor3996e242010-02-15 22:01:00 +00001657 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001658}
1659
Douglas Gregor5c73e912010-02-11 00:48:18 +00001660Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1661 // If this record has a definition in the translation unit we're coming from,
1662 // but this particular declaration is not that definition, import the
1663 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001664 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001665 if (Definition && Definition != D) {
1666 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001667 if (!ImportedDef)
1668 return 0;
1669
1670 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001671 }
1672
1673 // Import the major distinguishing characteristics of this record.
1674 DeclContext *DC, *LexicalDC;
1675 DeclarationName Name;
1676 SourceLocation Loc;
1677 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1678 return 0;
1679
1680 // Figure out what structure name we're looking for.
1681 unsigned IDNS = Decl::IDNS_Tag;
1682 DeclarationName SearchName = Name;
1683 if (!SearchName && D->getTypedefForAnonDecl()) {
1684 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1685 IDNS = Decl::IDNS_Ordinary;
1686 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1687 IDNS |= Decl::IDNS_Ordinary;
1688
1689 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001690 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001691 if (!DC->isFunctionOrMethod() && SearchName) {
1692 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1693 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1694 Lookup.first != Lookup.second;
1695 ++Lookup.first) {
1696 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1697 continue;
1698
1699 Decl *Found = *Lookup.first;
1700 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1701 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1702 Found = Tag->getDecl();
1703 }
1704
1705 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001706 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1707 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1708 // The record types structurally match, or the "from" translation
1709 // unit only had a forward declaration anyway; call it the same
1710 // function.
1711 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001712 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001713 }
1714 } else {
1715 // We have a forward declaration of this type, so adopt that forward
1716 // declaration rather than building a new one.
1717 AdoptDecl = FoundRecord;
1718 continue;
1719 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001720 }
1721
1722 ConflictingDecls.push_back(*Lookup.first);
1723 }
1724
1725 if (!ConflictingDecls.empty()) {
1726 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1727 ConflictingDecls.data(),
1728 ConflictingDecls.size());
1729 }
1730 }
1731
1732 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001733 RecordDecl *D2 = AdoptDecl;
1734 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00001735 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001736 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001737 D->getTagKind(),
1738 DC, Loc,
1739 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001740 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001741 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001742 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00001743 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001744 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001745 DC, Loc,
1746 Name.getAsIdentifierInfo(),
1747 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001748 }
John McCall3e11ebe2010-03-15 10:12:16 +00001749 // Import the qualifier, if any.
1750 if (D->getQualifier()) {
1751 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1752 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1753 D2->setQualifierInfo(NNS, NNSRange);
1754 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001755 D2->setLexicalDeclContext(LexicalDC);
1756 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001757 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001758
Douglas Gregor3996e242010-02-15 22:01:00 +00001759 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001760
Douglas Gregor5c73e912010-02-11 00:48:18 +00001761 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001762 D2->startDefinition();
John McCall1c70e992010-06-03 19:28:45 +00001763
1764 // Add base classes.
1765 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1766 CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D);
1767
1768 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1769 for (CXXRecordDecl::base_class_iterator
1770 Base1 = D1CXX->bases_begin(),
1771 FromBaseEnd = D1CXX->bases_end();
1772 Base1 != FromBaseEnd;
1773 ++Base1) {
1774 QualType T = Importer.Import(Base1->getType());
1775 if (T.isNull())
1776 return 0;
1777
1778 Bases.push_back(
1779 new (Importer.getToContext())
1780 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1781 Base1->isVirtual(),
1782 Base1->isBaseOfClass(),
1783 Base1->getAccessSpecifierAsWritten(),
Nick Lewycky19b9f952010-07-26 16:56:01 +00001784 Importer.Import(Base1->getTypeSourceInfo())));
John McCall1c70e992010-06-03 19:28:45 +00001785 }
1786 if (!Bases.empty())
1787 D2CXX->setBases(Bases.data(), Bases.size());
1788 }
1789
Douglas Gregor968d6332010-02-21 18:24:45 +00001790 ImportDeclContext(D);
Douglas Gregor3996e242010-02-15 22:01:00 +00001791 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001792 }
1793
Douglas Gregor3996e242010-02-15 22:01:00 +00001794 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001795}
1796
Douglas Gregor98c10182010-02-12 22:17:39 +00001797Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1798 // Import the major distinguishing characteristics of this enumerator.
1799 DeclContext *DC, *LexicalDC;
1800 DeclarationName Name;
1801 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001802 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001803 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001804
1805 QualType T = Importer.Import(D->getType());
1806 if (T.isNull())
1807 return 0;
1808
Douglas Gregor98c10182010-02-12 22:17:39 +00001809 // Determine whether there are any other declarations with the same name and
1810 // in the same context.
1811 if (!LexicalDC->isFunctionOrMethod()) {
1812 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1813 unsigned IDNS = Decl::IDNS_Ordinary;
1814 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1815 Lookup.first != Lookup.second;
1816 ++Lookup.first) {
1817 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1818 continue;
1819
1820 ConflictingDecls.push_back(*Lookup.first);
1821 }
1822
1823 if (!ConflictingDecls.empty()) {
1824 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1825 ConflictingDecls.data(),
1826 ConflictingDecls.size());
1827 if (!Name)
1828 return 0;
1829 }
1830 }
1831
1832 Expr *Init = Importer.Import(D->getInitExpr());
1833 if (D->getInitExpr() && !Init)
1834 return 0;
1835
1836 EnumConstantDecl *ToEnumerator
1837 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
1838 Name.getAsIdentifierInfo(), T,
1839 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00001840 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00001841 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001842 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00001843 LexicalDC->addDecl(ToEnumerator);
1844 return ToEnumerator;
1845}
Douglas Gregor5c73e912010-02-11 00:48:18 +00001846
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001847Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
1848 // Import the major distinguishing characteristics of this function.
1849 DeclContext *DC, *LexicalDC;
1850 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001851 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001852 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001853 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001854
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001855 // Try to find a function in our own ("to") context with the same name, same
1856 // type, and in the same context as the function we're importing.
1857 if (!LexicalDC->isFunctionOrMethod()) {
1858 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1859 unsigned IDNS = Decl::IDNS_Ordinary;
1860 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1861 Lookup.first != Lookup.second;
1862 ++Lookup.first) {
1863 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1864 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001865
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001866 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
1867 if (isExternalLinkage(FoundFunction->getLinkage()) &&
1868 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001869 if (Importer.IsStructurallyEquivalent(D->getType(),
1870 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001871 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001872 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001873 }
1874
1875 // FIXME: Check for overloading more carefully, e.g., by boosting
1876 // Sema::IsOverload out to the AST library.
1877
1878 // Function overloading is okay in C++.
1879 if (Importer.getToContext().getLangOptions().CPlusPlus)
1880 continue;
1881
1882 // Complain about inconsistent function types.
1883 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00001884 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001885 Importer.ToDiag(FoundFunction->getLocation(),
1886 diag::note_odr_value_here)
1887 << FoundFunction->getType();
1888 }
1889 }
1890
1891 ConflictingDecls.push_back(*Lookup.first);
1892 }
1893
1894 if (!ConflictingDecls.empty()) {
1895 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1896 ConflictingDecls.data(),
1897 ConflictingDecls.size());
1898 if (!Name)
1899 return 0;
1900 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00001901 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001902
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001903 DeclarationNameInfo NameInfo(Name, Loc);
1904 // Import additional name location/type info.
1905 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
1906
Douglas Gregorb4964f72010-02-15 23:54:17 +00001907 // Import the type.
1908 QualType T = Importer.Import(D->getType());
1909 if (T.isNull())
1910 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001911
1912 // Import the function parameters.
1913 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
1914 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
1915 P != PEnd; ++P) {
1916 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
1917 if (!ToP)
1918 return 0;
1919
1920 Parameters.push_back(ToP);
1921 }
1922
1923 // Create the imported function.
1924 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00001925 FunctionDecl *ToFunction = 0;
1926 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
1927 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
1928 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001929 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001930 FromConstructor->isExplicit(),
1931 D->isInlineSpecified(),
1932 D->isImplicit());
1933 } else if (isa<CXXDestructorDecl>(D)) {
1934 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
1935 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001936 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001937 D->isInlineSpecified(),
1938 D->isImplicit());
1939 } else if (CXXConversionDecl *FromConversion
1940 = dyn_cast<CXXConversionDecl>(D)) {
1941 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
1942 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001943 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00001944 D->isInlineSpecified(),
1945 FromConversion->isExplicit());
1946 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001947 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
1948 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001949 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00001950 D->isInlineSpecified(),
1951 D->hasWrittenPrototype());
1952 }
John McCall3e11ebe2010-03-15 10:12:16 +00001953
1954 // Import the qualifier, if any.
1955 if (D->getQualifier()) {
1956 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1957 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1958 ToFunction->setQualifierInfo(NNS, NNSRange);
1959 }
Douglas Gregordd483172010-02-22 17:42:47 +00001960 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00001961 ToFunction->setLexicalDeclContext(LexicalDC);
1962 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00001963
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001964 // Set the parameters.
1965 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00001966 Parameters[I]->setOwningFunction(ToFunction);
1967 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001968 }
Douglas Gregor43f54792010-02-17 02:12:47 +00001969 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001970
1971 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00001972
1973 // Add this function to the lexical context.
1974 LexicalDC->addDecl(ToFunction);
1975
Douglas Gregor43f54792010-02-17 02:12:47 +00001976 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001977}
1978
Douglas Gregor00eace12010-02-21 18:29:16 +00001979Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1980 return VisitFunctionDecl(D);
1981}
1982
1983Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1984 return VisitCXXMethodDecl(D);
1985}
1986
1987Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1988 return VisitCXXMethodDecl(D);
1989}
1990
1991Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1992 return VisitCXXMethodDecl(D);
1993}
1994
Douglas Gregor5c73e912010-02-11 00:48:18 +00001995Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
1996 // Import the major distinguishing characteristics of a variable.
1997 DeclContext *DC, *LexicalDC;
1998 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001999 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002000 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2001 return 0;
2002
2003 // Import the type.
2004 QualType T = Importer.Import(D->getType());
2005 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002006 return 0;
2007
2008 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2009 Expr *BitWidth = Importer.Import(D->getBitWidth());
2010 if (!BitWidth && D->getBitWidth())
2011 return 0;
2012
2013 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2014 Loc, Name.getAsIdentifierInfo(),
2015 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002016 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002017 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002018 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002019 LexicalDC->addDecl(ToField);
2020 return ToField;
2021}
2022
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002023Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2024 // Import the major distinguishing characteristics of an ivar.
2025 DeclContext *DC, *LexicalDC;
2026 DeclarationName Name;
2027 SourceLocation Loc;
2028 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2029 return 0;
2030
2031 // Determine whether we've already imported this ivar
2032 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2033 Lookup.first != Lookup.second;
2034 ++Lookup.first) {
2035 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2036 if (Importer.IsStructurallyEquivalent(D->getType(),
2037 FoundIvar->getType())) {
2038 Importer.Imported(D, FoundIvar);
2039 return FoundIvar;
2040 }
2041
2042 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2043 << Name << D->getType() << FoundIvar->getType();
2044 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2045 << FoundIvar->getType();
2046 return 0;
2047 }
2048 }
2049
2050 // Import the type.
2051 QualType T = Importer.Import(D->getType());
2052 if (T.isNull())
2053 return 0;
2054
2055 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2056 Expr *BitWidth = Importer.Import(D->getBitWidth());
2057 if (!BitWidth && D->getBitWidth())
2058 return 0;
2059
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002060 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2061 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002062 Loc, Name.getAsIdentifierInfo(),
2063 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002064 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002065 ToIvar->setLexicalDeclContext(LexicalDC);
2066 Importer.Imported(D, ToIvar);
2067 LexicalDC->addDecl(ToIvar);
2068 return ToIvar;
2069
2070}
2071
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002072Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2073 // Import the major distinguishing characteristics of a variable.
2074 DeclContext *DC, *LexicalDC;
2075 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002076 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002077 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002078 return 0;
2079
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002080 // Try to find a variable in our own ("to") context with the same name and
2081 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002082 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002083 VarDecl *MergeWithVar = 0;
2084 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2085 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002086 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002087 Lookup.first != Lookup.second;
2088 ++Lookup.first) {
2089 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2090 continue;
2091
2092 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2093 // We have found a variable that we may need to merge with. Check it.
2094 if (isExternalLinkage(FoundVar->getLinkage()) &&
2095 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002096 if (Importer.IsStructurallyEquivalent(D->getType(),
2097 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002098 MergeWithVar = FoundVar;
2099 break;
2100 }
2101
Douglas Gregor56521c52010-02-12 17:23:39 +00002102 const ArrayType *FoundArray
2103 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2104 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002105 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002106 if (FoundArray && TArray) {
2107 if (isa<IncompleteArrayType>(FoundArray) &&
2108 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002109 // Import the type.
2110 QualType T = Importer.Import(D->getType());
2111 if (T.isNull())
2112 return 0;
2113
Douglas Gregor56521c52010-02-12 17:23:39 +00002114 FoundVar->setType(T);
2115 MergeWithVar = FoundVar;
2116 break;
2117 } else if (isa<IncompleteArrayType>(TArray) &&
2118 isa<ConstantArrayType>(FoundArray)) {
2119 MergeWithVar = FoundVar;
2120 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002121 }
2122 }
2123
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002124 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002125 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002126 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2127 << FoundVar->getType();
2128 }
2129 }
2130
2131 ConflictingDecls.push_back(*Lookup.first);
2132 }
2133
2134 if (MergeWithVar) {
2135 // An equivalent variable with external linkage has been found. Link
2136 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002137 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002138
2139 if (VarDecl *DDef = D->getDefinition()) {
2140 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2141 Importer.ToDiag(ExistingDef->getLocation(),
2142 diag::err_odr_variable_multiple_def)
2143 << Name;
2144 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2145 } else {
2146 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002147 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002148 }
2149 }
2150
2151 return MergeWithVar;
2152 }
2153
2154 if (!ConflictingDecls.empty()) {
2155 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2156 ConflictingDecls.data(),
2157 ConflictingDecls.size());
2158 if (!Name)
2159 return 0;
2160 }
2161 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002162
Douglas Gregorb4964f72010-02-15 23:54:17 +00002163 // Import the type.
2164 QualType T = Importer.Import(D->getType());
2165 if (T.isNull())
2166 return 0;
2167
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002168 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002169 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002170 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2171 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002172 D->getStorageClass(),
2173 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002174 // Import the qualifier, if any.
2175 if (D->getQualifier()) {
2176 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2177 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2178 ToVar->setQualifierInfo(NNS, NNSRange);
2179 }
Douglas Gregordd483172010-02-22 17:42:47 +00002180 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002181 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002182 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002183 LexicalDC->addDecl(ToVar);
2184
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002185 // Merge the initializer.
2186 // FIXME: Can we really import any initializer? Alternatively, we could force
2187 // ourselves to import every declaration of a variable and then only use
2188 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002189 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002190
2191 // FIXME: Other bits to merge?
2192
2193 return ToVar;
2194}
2195
Douglas Gregor8b228d72010-02-17 21:22:52 +00002196Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2197 // Parameters are created in the translation unit's context, then moved
2198 // into the function declaration's context afterward.
2199 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2200
2201 // Import the name of this declaration.
2202 DeclarationName Name = Importer.Import(D->getDeclName());
2203 if (D->getDeclName() && !Name)
2204 return 0;
2205
2206 // Import the location of this declaration.
2207 SourceLocation Loc = Importer.Import(D->getLocation());
2208
2209 // Import the parameter's type.
2210 QualType T = Importer.Import(D->getType());
2211 if (T.isNull())
2212 return 0;
2213
2214 // Create the imported parameter.
2215 ImplicitParamDecl *ToParm
2216 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2217 Loc, Name.getAsIdentifierInfo(),
2218 T);
2219 return Importer.Imported(D, ToParm);
2220}
2221
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002222Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2223 // Parameters are created in the translation unit's context, then moved
2224 // into the function declaration's context afterward.
2225 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2226
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002227 // Import the name of this declaration.
2228 DeclarationName Name = Importer.Import(D->getDeclName());
2229 if (D->getDeclName() && !Name)
2230 return 0;
2231
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002232 // Import the location of this declaration.
2233 SourceLocation Loc = Importer.Import(D->getLocation());
2234
2235 // Import the parameter's type.
2236 QualType T = Importer.Import(D->getType());
2237 if (T.isNull())
2238 return 0;
2239
2240 // Create the imported parameter.
2241 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2242 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2243 Loc, Name.getAsIdentifierInfo(),
2244 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002245 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002246 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002247 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002248 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002249}
2250
Douglas Gregor43f54792010-02-17 02:12:47 +00002251Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2252 // Import the major distinguishing characteristics of a method.
2253 DeclContext *DC, *LexicalDC;
2254 DeclarationName Name;
2255 SourceLocation Loc;
2256 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2257 return 0;
2258
2259 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2260 Lookup.first != Lookup.second;
2261 ++Lookup.first) {
2262 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2263 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2264 continue;
2265
2266 // Check return types.
2267 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2268 FoundMethod->getResultType())) {
2269 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2270 << D->isInstanceMethod() << Name
2271 << D->getResultType() << FoundMethod->getResultType();
2272 Importer.ToDiag(FoundMethod->getLocation(),
2273 diag::note_odr_objc_method_here)
2274 << D->isInstanceMethod() << Name;
2275 return 0;
2276 }
2277
2278 // Check the number of parameters.
2279 if (D->param_size() != FoundMethod->param_size()) {
2280 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2281 << D->isInstanceMethod() << Name
2282 << D->param_size() << FoundMethod->param_size();
2283 Importer.ToDiag(FoundMethod->getLocation(),
2284 diag::note_odr_objc_method_here)
2285 << D->isInstanceMethod() << Name;
2286 return 0;
2287 }
2288
2289 // Check parameter types.
2290 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2291 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2292 P != PEnd; ++P, ++FoundP) {
2293 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2294 (*FoundP)->getType())) {
2295 Importer.FromDiag((*P)->getLocation(),
2296 diag::err_odr_objc_method_param_type_inconsistent)
2297 << D->isInstanceMethod() << Name
2298 << (*P)->getType() << (*FoundP)->getType();
2299 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2300 << (*FoundP)->getType();
2301 return 0;
2302 }
2303 }
2304
2305 // Check variadic/non-variadic.
2306 // Check the number of parameters.
2307 if (D->isVariadic() != FoundMethod->isVariadic()) {
2308 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2309 << D->isInstanceMethod() << Name;
2310 Importer.ToDiag(FoundMethod->getLocation(),
2311 diag::note_odr_objc_method_here)
2312 << D->isInstanceMethod() << Name;
2313 return 0;
2314 }
2315
2316 // FIXME: Any other bits we need to merge?
2317 return Importer.Imported(D, FoundMethod);
2318 }
2319 }
2320
2321 // Import the result type.
2322 QualType ResultTy = Importer.Import(D->getResultType());
2323 if (ResultTy.isNull())
2324 return 0;
2325
Douglas Gregor12852d92010-03-08 14:59:44 +00002326 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2327
Douglas Gregor43f54792010-02-17 02:12:47 +00002328 ObjCMethodDecl *ToMethod
2329 = ObjCMethodDecl::Create(Importer.getToContext(),
2330 Loc,
2331 Importer.Import(D->getLocEnd()),
2332 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002333 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002334 D->isInstanceMethod(),
2335 D->isVariadic(),
2336 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002337 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002338 D->getImplementationControl());
2339
2340 // FIXME: When we decide to merge method definitions, we'll need to
2341 // deal with implicit parameters.
2342
2343 // Import the parameters
2344 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2345 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2346 FromPEnd = D->param_end();
2347 FromP != FromPEnd;
2348 ++FromP) {
2349 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2350 if (!ToP)
2351 return 0;
2352
2353 ToParams.push_back(ToP);
2354 }
2355
2356 // Set the parameters.
2357 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2358 ToParams[I]->setOwningFunction(ToMethod);
2359 ToMethod->addDecl(ToParams[I]);
2360 }
2361 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002362 ToParams.data(), ToParams.size(),
2363 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002364
2365 ToMethod->setLexicalDeclContext(LexicalDC);
2366 Importer.Imported(D, ToMethod);
2367 LexicalDC->addDecl(ToMethod);
2368 return ToMethod;
2369}
2370
Douglas Gregor84c51c32010-02-18 01:47:50 +00002371Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2372 // Import the major distinguishing characteristics of a category.
2373 DeclContext *DC, *LexicalDC;
2374 DeclarationName Name;
2375 SourceLocation Loc;
2376 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2377 return 0;
2378
2379 ObjCInterfaceDecl *ToInterface
2380 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2381 if (!ToInterface)
2382 return 0;
2383
2384 // Determine if we've already encountered this category.
2385 ObjCCategoryDecl *MergeWithCategory
2386 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2387 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2388 if (!ToCategory) {
2389 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2390 Importer.Import(D->getAtLoc()),
2391 Loc,
2392 Importer.Import(D->getCategoryNameLoc()),
2393 Name.getAsIdentifierInfo());
2394 ToCategory->setLexicalDeclContext(LexicalDC);
2395 LexicalDC->addDecl(ToCategory);
2396 Importer.Imported(D, ToCategory);
2397
2398 // Link this category into its class's category list.
2399 ToCategory->setClassInterface(ToInterface);
2400 ToCategory->insertNextClassCategory();
2401
2402 // Import protocols
2403 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2404 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2405 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2406 = D->protocol_loc_begin();
2407 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2408 FromProtoEnd = D->protocol_end();
2409 FromProto != FromProtoEnd;
2410 ++FromProto, ++FromProtoLoc) {
2411 ObjCProtocolDecl *ToProto
2412 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2413 if (!ToProto)
2414 return 0;
2415 Protocols.push_back(ToProto);
2416 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2417 }
2418
2419 // FIXME: If we're merging, make sure that the protocol list is the same.
2420 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2421 ProtocolLocs.data(), Importer.getToContext());
2422
2423 } else {
2424 Importer.Imported(D, ToCategory);
2425 }
2426
2427 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002428 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002429
2430 // If we have an implementation, import it as well.
2431 if (D->getImplementation()) {
2432 ObjCCategoryImplDecl *Impl
2433 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2434 if (!Impl)
2435 return 0;
2436
2437 ToCategory->setImplementation(Impl);
2438 }
2439
2440 return ToCategory;
2441}
2442
Douglas Gregor98d156a2010-02-17 16:12:00 +00002443Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002444 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002445 DeclContext *DC, *LexicalDC;
2446 DeclarationName Name;
2447 SourceLocation Loc;
2448 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2449 return 0;
2450
2451 ObjCProtocolDecl *MergeWithProtocol = 0;
2452 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2453 Lookup.first != Lookup.second;
2454 ++Lookup.first) {
2455 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2456 continue;
2457
2458 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2459 break;
2460 }
2461
2462 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2463 if (!ToProto || ToProto->isForwardDecl()) {
2464 if (!ToProto) {
2465 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2466 Name.getAsIdentifierInfo());
2467 ToProto->setForwardDecl(D->isForwardDecl());
2468 ToProto->setLexicalDeclContext(LexicalDC);
2469 LexicalDC->addDecl(ToProto);
2470 }
2471 Importer.Imported(D, ToProto);
2472
2473 // Import protocols
2474 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2475 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2476 ObjCProtocolDecl::protocol_loc_iterator
2477 FromProtoLoc = D->protocol_loc_begin();
2478 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2479 FromProtoEnd = D->protocol_end();
2480 FromProto != FromProtoEnd;
2481 ++FromProto, ++FromProtoLoc) {
2482 ObjCProtocolDecl *ToProto
2483 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2484 if (!ToProto)
2485 return 0;
2486 Protocols.push_back(ToProto);
2487 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2488 }
2489
2490 // FIXME: If we're merging, make sure that the protocol list is the same.
2491 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2492 ProtocolLocs.data(), Importer.getToContext());
2493 } else {
2494 Importer.Imported(D, ToProto);
2495 }
2496
Douglas Gregor84c51c32010-02-18 01:47:50 +00002497 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002498 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002499
2500 return ToProto;
2501}
2502
Douglas Gregor45635322010-02-16 01:20:57 +00002503Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2504 // Import the major distinguishing characteristics of an @interface.
2505 DeclContext *DC, *LexicalDC;
2506 DeclarationName Name;
2507 SourceLocation Loc;
2508 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2509 return 0;
2510
2511 ObjCInterfaceDecl *MergeWithIface = 0;
2512 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2513 Lookup.first != Lookup.second;
2514 ++Lookup.first) {
2515 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2516 continue;
2517
2518 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2519 break;
2520 }
2521
2522 ObjCInterfaceDecl *ToIface = MergeWithIface;
2523 if (!ToIface || ToIface->isForwardDecl()) {
2524 if (!ToIface) {
2525 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2526 DC, Loc,
2527 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002528 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002529 D->isForwardDecl(),
2530 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002531 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002532 ToIface->setLexicalDeclContext(LexicalDC);
2533 LexicalDC->addDecl(ToIface);
2534 }
2535 Importer.Imported(D, ToIface);
2536
Douglas Gregor45635322010-02-16 01:20:57 +00002537 if (D->getSuperClass()) {
2538 ObjCInterfaceDecl *Super
2539 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2540 if (!Super)
2541 return 0;
2542
2543 ToIface->setSuperClass(Super);
2544 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2545 }
2546
2547 // Import protocols
2548 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2549 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2550 ObjCInterfaceDecl::protocol_loc_iterator
2551 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002552
2553 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00002554 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2555 FromProtoEnd = D->protocol_end();
2556 FromProto != FromProtoEnd;
2557 ++FromProto, ++FromProtoLoc) {
2558 ObjCProtocolDecl *ToProto
2559 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2560 if (!ToProto)
2561 return 0;
2562 Protocols.push_back(ToProto);
2563 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2564 }
2565
2566 // FIXME: If we're merging, make sure that the protocol list is the same.
2567 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2568 ProtocolLocs.data(), Importer.getToContext());
2569
Douglas Gregor45635322010-02-16 01:20:57 +00002570 // Import @end range
2571 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2572 } else {
2573 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002574
2575 // Check for consistency of superclasses.
2576 DeclarationName FromSuperName, ToSuperName;
2577 if (D->getSuperClass())
2578 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2579 if (ToIface->getSuperClass())
2580 ToSuperName = ToIface->getSuperClass()->getDeclName();
2581 if (FromSuperName != ToSuperName) {
2582 Importer.ToDiag(ToIface->getLocation(),
2583 diag::err_odr_objc_superclass_inconsistent)
2584 << ToIface->getDeclName();
2585 if (ToIface->getSuperClass())
2586 Importer.ToDiag(ToIface->getSuperClassLoc(),
2587 diag::note_odr_objc_superclass)
2588 << ToIface->getSuperClass()->getDeclName();
2589 else
2590 Importer.ToDiag(ToIface->getLocation(),
2591 diag::note_odr_objc_missing_superclass);
2592 if (D->getSuperClass())
2593 Importer.FromDiag(D->getSuperClassLoc(),
2594 diag::note_odr_objc_superclass)
2595 << D->getSuperClass()->getDeclName();
2596 else
2597 Importer.FromDiag(D->getLocation(),
2598 diag::note_odr_objc_missing_superclass);
2599 return 0;
2600 }
Douglas Gregor45635322010-02-16 01:20:57 +00002601 }
2602
Douglas Gregor84c51c32010-02-18 01:47:50 +00002603 // Import categories. When the categories themselves are imported, they'll
2604 // hook themselves into this interface.
2605 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2606 FromCat = FromCat->getNextClassCategory())
2607 Importer.Import(FromCat);
2608
Douglas Gregor45635322010-02-16 01:20:57 +00002609 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00002610 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00002611
2612 // If we have an @implementation, import it as well.
2613 if (D->getImplementation()) {
2614 ObjCImplementationDecl *Impl
2615 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2616 if (!Impl)
2617 return 0;
2618
2619 ToIface->setImplementation(Impl);
2620 }
2621
Douglas Gregor98d156a2010-02-17 16:12:00 +00002622 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00002623}
2624
Douglas Gregora11c4582010-02-17 18:02:10 +00002625Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2626 // Import the major distinguishing characteristics of an @property.
2627 DeclContext *DC, *LexicalDC;
2628 DeclarationName Name;
2629 SourceLocation Loc;
2630 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2631 return 0;
2632
2633 // Check whether we have already imported this property.
2634 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2635 Lookup.first != Lookup.second;
2636 ++Lookup.first) {
2637 if (ObjCPropertyDecl *FoundProp
2638 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2639 // Check property types.
2640 if (!Importer.IsStructurallyEquivalent(D->getType(),
2641 FoundProp->getType())) {
2642 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2643 << Name << D->getType() << FoundProp->getType();
2644 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2645 << FoundProp->getType();
2646 return 0;
2647 }
2648
2649 // FIXME: Check property attributes, getters, setters, etc.?
2650
2651 // Consider these properties to be equivalent.
2652 Importer.Imported(D, FoundProp);
2653 return FoundProp;
2654 }
2655 }
2656
2657 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00002658 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
2659 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00002660 return 0;
2661
2662 // Create the new property.
2663 ObjCPropertyDecl *ToProperty
2664 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2665 Name.getAsIdentifierInfo(),
2666 Importer.Import(D->getAtLoc()),
2667 T,
2668 D->getPropertyImplementation());
2669 Importer.Imported(D, ToProperty);
2670 ToProperty->setLexicalDeclContext(LexicalDC);
2671 LexicalDC->addDecl(ToProperty);
2672
2673 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00002674 ToProperty->setPropertyAttributesAsWritten(
2675 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00002676 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2677 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2678 ToProperty->setGetterMethodDecl(
2679 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2680 ToProperty->setSetterMethodDecl(
2681 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2682 ToProperty->setPropertyIvarDecl(
2683 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2684 return ToProperty;
2685}
2686
Douglas Gregor8661a722010-02-18 02:12:22 +00002687Decl *
2688ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2689 // Import the context of this declaration.
2690 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2691 if (!DC)
2692 return 0;
2693
2694 DeclContext *LexicalDC = DC;
2695 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2696 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2697 if (!LexicalDC)
2698 return 0;
2699 }
2700
2701 // Import the location of this declaration.
2702 SourceLocation Loc = Importer.Import(D->getLocation());
2703
2704 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2705 llvm::SmallVector<SourceLocation, 4> Locations;
2706 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2707 = D->protocol_loc_begin();
2708 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2709 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2710 FromProto != FromProtoEnd;
2711 ++FromProto, ++FromProtoLoc) {
2712 ObjCProtocolDecl *ToProto
2713 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2714 if (!ToProto)
2715 continue;
2716
2717 Protocols.push_back(ToProto);
2718 Locations.push_back(Importer.Import(*FromProtoLoc));
2719 }
2720
2721 ObjCForwardProtocolDecl *ToForward
2722 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2723 Protocols.data(), Protocols.size(),
2724 Locations.data());
2725 ToForward->setLexicalDeclContext(LexicalDC);
2726 LexicalDC->addDecl(ToForward);
2727 Importer.Imported(D, ToForward);
2728 return ToForward;
2729}
2730
Douglas Gregor06537af2010-02-18 02:04:09 +00002731Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2732 // Import the context of this declaration.
2733 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2734 if (!DC)
2735 return 0;
2736
2737 DeclContext *LexicalDC = DC;
2738 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2739 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2740 if (!LexicalDC)
2741 return 0;
2742 }
2743
2744 // Import the location of this declaration.
2745 SourceLocation Loc = Importer.Import(D->getLocation());
2746
2747 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2748 llvm::SmallVector<SourceLocation, 4> Locations;
2749 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2750 From != FromEnd; ++From) {
2751 ObjCInterfaceDecl *ToIface
2752 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2753 if (!ToIface)
2754 continue;
2755
2756 Interfaces.push_back(ToIface);
2757 Locations.push_back(Importer.Import(From->getLocation()));
2758 }
2759
2760 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2761 Loc,
2762 Interfaces.data(),
2763 Locations.data(),
2764 Interfaces.size());
2765 ToClass->setLexicalDeclContext(LexicalDC);
2766 LexicalDC->addDecl(ToClass);
2767 Importer.Imported(D, ToClass);
2768 return ToClass;
2769}
2770
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002771//----------------------------------------------------------------------------
2772// Import Statements
2773//----------------------------------------------------------------------------
2774
2775Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
2776 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
2777 << S->getStmtClassName();
2778 return 0;
2779}
2780
2781//----------------------------------------------------------------------------
2782// Import Expressions
2783//----------------------------------------------------------------------------
2784Expr *ASTNodeImporter::VisitExpr(Expr *E) {
2785 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
2786 << E->getStmtClassName();
2787 return 0;
2788}
2789
Douglas Gregor52f820e2010-02-19 01:17:02 +00002790Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
2791 NestedNameSpecifier *Qualifier = 0;
2792 if (E->getQualifier()) {
2793 Qualifier = Importer.Import(E->getQualifier());
2794 if (!E->getQualifier())
2795 return 0;
2796 }
2797
2798 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
2799 if (!ToD)
2800 return 0;
2801
2802 QualType T = Importer.Import(E->getType());
2803 if (T.isNull())
2804 return 0;
2805
2806 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
2807 Importer.Import(E->getQualifierRange()),
2808 ToD,
2809 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00002810 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00002811 /*FIXME:TemplateArgs=*/0);
2812}
2813
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002814Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
2815 QualType T = Importer.Import(E->getType());
2816 if (T.isNull())
2817 return 0;
2818
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00002819 return IntegerLiteral::Create(Importer.getToContext(),
2820 E->getValue(), T,
2821 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00002822}
2823
Douglas Gregor623421d2010-02-18 02:21:22 +00002824Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
2825 QualType T = Importer.Import(E->getType());
2826 if (T.isNull())
2827 return 0;
2828
2829 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
2830 E->isWide(), T,
2831 Importer.Import(E->getLocation()));
2832}
2833
Douglas Gregorc74247e2010-02-19 01:07:06 +00002834Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
2835 Expr *SubExpr = Importer.Import(E->getSubExpr());
2836 if (!SubExpr)
2837 return 0;
2838
2839 return new (Importer.getToContext())
2840 ParenExpr(Importer.Import(E->getLParen()),
2841 Importer.Import(E->getRParen()),
2842 SubExpr);
2843}
2844
2845Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
2846 QualType T = Importer.Import(E->getType());
2847 if (T.isNull())
2848 return 0;
2849
2850 Expr *SubExpr = Importer.Import(E->getSubExpr());
2851 if (!SubExpr)
2852 return 0;
2853
2854 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00002855 T, E->getValueKind(),
2856 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00002857 Importer.Import(E->getOperatorLoc()));
2858}
2859
Douglas Gregord8552cd2010-02-19 01:24:23 +00002860Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
2861 QualType ResultType = Importer.Import(E->getType());
2862
2863 if (E->isArgumentType()) {
2864 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
2865 if (!TInfo)
2866 return 0;
2867
2868 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2869 TInfo, ResultType,
2870 Importer.Import(E->getOperatorLoc()),
2871 Importer.Import(E->getRParenLoc()));
2872 }
2873
2874 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
2875 if (!SubExpr)
2876 return 0;
2877
2878 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
2879 SubExpr, ResultType,
2880 Importer.Import(E->getOperatorLoc()),
2881 Importer.Import(E->getRParenLoc()));
2882}
2883
Douglas Gregorc74247e2010-02-19 01:07:06 +00002884Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
2885 QualType T = Importer.Import(E->getType());
2886 if (T.isNull())
2887 return 0;
2888
2889 Expr *LHS = Importer.Import(E->getLHS());
2890 if (!LHS)
2891 return 0;
2892
2893 Expr *RHS = Importer.Import(E->getRHS());
2894 if (!RHS)
2895 return 0;
2896
2897 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00002898 T, E->getValueKind(),
2899 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00002900 Importer.Import(E->getOperatorLoc()));
2901}
2902
2903Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
2904 QualType T = Importer.Import(E->getType());
2905 if (T.isNull())
2906 return 0;
2907
2908 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
2909 if (CompLHSType.isNull())
2910 return 0;
2911
2912 QualType CompResultType = Importer.Import(E->getComputationResultType());
2913 if (CompResultType.isNull())
2914 return 0;
2915
2916 Expr *LHS = Importer.Import(E->getLHS());
2917 if (!LHS)
2918 return 0;
2919
2920 Expr *RHS = Importer.Import(E->getRHS());
2921 if (!RHS)
2922 return 0;
2923
2924 return new (Importer.getToContext())
2925 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00002926 T, E->getValueKind(),
2927 E->getObjectKind(),
2928 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00002929 Importer.Import(E->getOperatorLoc()));
2930}
2931
John McCallcf142162010-08-07 06:22:56 +00002932bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
2933 if (E->path_empty()) return false;
2934
2935 // TODO: import cast paths
2936 return true;
2937}
2938
Douglas Gregor98c10182010-02-12 22:17:39 +00002939Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
2940 QualType T = Importer.Import(E->getType());
2941 if (T.isNull())
2942 return 0;
2943
2944 Expr *SubExpr = Importer.Import(E->getSubExpr());
2945 if (!SubExpr)
2946 return 0;
John McCallcf142162010-08-07 06:22:56 +00002947
2948 CXXCastPath BasePath;
2949 if (ImportCastPath(E, BasePath))
2950 return 0;
2951
2952 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00002953 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00002954}
2955
Douglas Gregor5481d322010-02-19 01:32:14 +00002956Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
2957 QualType T = Importer.Import(E->getType());
2958 if (T.isNull())
2959 return 0;
2960
2961 Expr *SubExpr = Importer.Import(E->getSubExpr());
2962 if (!SubExpr)
2963 return 0;
2964
2965 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
2966 if (!TInfo && E->getTypeInfoAsWritten())
2967 return 0;
2968
John McCallcf142162010-08-07 06:22:56 +00002969 CXXCastPath BasePath;
2970 if (ImportCastPath(E, BasePath))
2971 return 0;
2972
John McCall7decc9e2010-11-18 06:31:45 +00002973 return CStyleCastExpr::Create(Importer.getToContext(), T,
2974 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00002975 SubExpr, &BasePath, TInfo,
2976 Importer.Import(E->getLParenLoc()),
2977 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00002978}
2979
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002980ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002981 const FileSystemOptions &ToFileSystemOpts,
2982 ASTContext &FromContext, FileManager &FromFileManager,
2983 const FileSystemOptions &FromFileSystemOpts)
Douglas Gregor96e578d2010-02-05 17:54:41 +00002984 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor811663e2010-02-10 00:15:17 +00002985 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00002986 ToFileSystemOpts(ToFileSystemOpts), FromFileSystemOpts(FromFileSystemOpts) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00002987 ImportedDecls[FromContext.getTranslationUnitDecl()]
2988 = ToContext.getTranslationUnitDecl();
2989}
2990
2991ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00002992
2993QualType ASTImporter::Import(QualType FromT) {
2994 if (FromT.isNull())
2995 return QualType();
2996
Douglas Gregorf65bbb32010-02-08 15:18:58 +00002997 // Check whether we've already imported this type.
2998 llvm::DenseMap<Type *, Type *>::iterator Pos
2999 = ImportedTypes.find(FromT.getTypePtr());
3000 if (Pos != ImportedTypes.end())
3001 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003002
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003003 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003004 ASTNodeImporter Importer(*this);
3005 QualType ToT = Importer.Visit(FromT.getTypePtr());
3006 if (ToT.isNull())
3007 return ToT;
3008
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003009 // Record the imported type.
3010 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3011
Douglas Gregor96e578d2010-02-05 17:54:41 +00003012 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3013}
3014
Douglas Gregor62d311f2010-02-09 19:21:46 +00003015TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003016 if (!FromTSI)
3017 return FromTSI;
3018
3019 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003020 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003021 QualType T = Import(FromTSI->getType());
3022 if (T.isNull())
3023 return 0;
3024
3025 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003026 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003027}
3028
3029Decl *ASTImporter::Import(Decl *FromD) {
3030 if (!FromD)
3031 return 0;
3032
3033 // Check whether we've already imported this declaration.
3034 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3035 if (Pos != ImportedDecls.end())
3036 return Pos->second;
3037
3038 // Import the type
3039 ASTNodeImporter Importer(*this);
3040 Decl *ToD = Importer.Visit(FromD);
3041 if (!ToD)
3042 return 0;
3043
3044 // Record the imported declaration.
3045 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003046
3047 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3048 // Keep track of anonymous tags that have an associated typedef.
3049 if (FromTag->getTypedefForAnonDecl())
3050 AnonTagsWithPendingTypedefs.push_back(FromTag);
3051 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3052 // When we've finished transforming a typedef, see whether it was the
3053 // typedef for an anonymous tag.
3054 for (llvm::SmallVector<TagDecl *, 4>::iterator
3055 FromTag = AnonTagsWithPendingTypedefs.begin(),
3056 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3057 FromTag != FromTagEnd; ++FromTag) {
3058 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3059 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3060 // We found the typedef for an anonymous tag; link them.
3061 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3062 AnonTagsWithPendingTypedefs.erase(FromTag);
3063 break;
3064 }
3065 }
3066 }
3067 }
3068
Douglas Gregor62d311f2010-02-09 19:21:46 +00003069 return ToD;
3070}
3071
3072DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3073 if (!FromDC)
3074 return FromDC;
3075
3076 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3077}
3078
3079Expr *ASTImporter::Import(Expr *FromE) {
3080 if (!FromE)
3081 return 0;
3082
3083 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3084}
3085
3086Stmt *ASTImporter::Import(Stmt *FromS) {
3087 if (!FromS)
3088 return 0;
3089
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003090 // Check whether we've already imported this declaration.
3091 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3092 if (Pos != ImportedStmts.end())
3093 return Pos->second;
3094
3095 // Import the type
3096 ASTNodeImporter Importer(*this);
3097 Stmt *ToS = Importer.Visit(FromS);
3098 if (!ToS)
3099 return 0;
3100
3101 // Record the imported declaration.
3102 ImportedStmts[FromS] = ToS;
3103 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003104}
3105
3106NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3107 if (!FromNNS)
3108 return 0;
3109
3110 // FIXME: Implement!
3111 return 0;
3112}
3113
3114SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3115 if (FromLoc.isInvalid())
3116 return SourceLocation();
3117
Douglas Gregor811663e2010-02-10 00:15:17 +00003118 SourceManager &FromSM = FromContext.getSourceManager();
3119
3120 // For now, map everything down to its spelling location, so that we
3121 // don't have to import macro instantiations.
3122 // FIXME: Import macro instantiations!
3123 FromLoc = FromSM.getSpellingLoc(FromLoc);
3124 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3125 SourceManager &ToSM = ToContext.getSourceManager();
3126 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3127 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003128}
3129
3130SourceRange ASTImporter::Import(SourceRange FromRange) {
3131 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3132}
3133
Douglas Gregor811663e2010-02-10 00:15:17 +00003134FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00003135 llvm::DenseMap<FileID, FileID>::iterator Pos
3136 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00003137 if (Pos != ImportedFileIDs.end())
3138 return Pos->second;
3139
3140 SourceManager &FromSM = FromContext.getSourceManager();
3141 SourceManager &ToSM = ToContext.getSourceManager();
3142 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3143 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3144
3145 // Include location of this file.
3146 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3147
3148 // Map the FileID for to the "to" source manager.
3149 FileID ToID;
3150 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3151 if (Cache->Entry) {
3152 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3153 // disk again
3154 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3155 // than mmap the files several times.
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003156 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName(),
3157 ToFileSystemOpts);
Douglas Gregor811663e2010-02-10 00:15:17 +00003158 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3159 FromSLoc.getFile().getFileCharacteristic());
3160 } else {
3161 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003162 const llvm::MemoryBuffer *
3163 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00003164 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00003165 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00003166 FromBuf->getBufferIdentifier());
3167 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3168 }
3169
3170
Sebastian Redl99219f12010-09-30 01:03:06 +00003171 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00003172 return ToID;
3173}
3174
Douglas Gregor96e578d2010-02-05 17:54:41 +00003175DeclarationName ASTImporter::Import(DeclarationName FromName) {
3176 if (!FromName)
3177 return DeclarationName();
3178
3179 switch (FromName.getNameKind()) {
3180 case DeclarationName::Identifier:
3181 return Import(FromName.getAsIdentifierInfo());
3182
3183 case DeclarationName::ObjCZeroArgSelector:
3184 case DeclarationName::ObjCOneArgSelector:
3185 case DeclarationName::ObjCMultiArgSelector:
3186 return Import(FromName.getObjCSelector());
3187
3188 case DeclarationName::CXXConstructorName: {
3189 QualType T = Import(FromName.getCXXNameType());
3190 if (T.isNull())
3191 return DeclarationName();
3192
3193 return ToContext.DeclarationNames.getCXXConstructorName(
3194 ToContext.getCanonicalType(T));
3195 }
3196
3197 case DeclarationName::CXXDestructorName: {
3198 QualType T = Import(FromName.getCXXNameType());
3199 if (T.isNull())
3200 return DeclarationName();
3201
3202 return ToContext.DeclarationNames.getCXXDestructorName(
3203 ToContext.getCanonicalType(T));
3204 }
3205
3206 case DeclarationName::CXXConversionFunctionName: {
3207 QualType T = Import(FromName.getCXXNameType());
3208 if (T.isNull())
3209 return DeclarationName();
3210
3211 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3212 ToContext.getCanonicalType(T));
3213 }
3214
3215 case DeclarationName::CXXOperatorName:
3216 return ToContext.DeclarationNames.getCXXOperatorName(
3217 FromName.getCXXOverloadedOperator());
3218
3219 case DeclarationName::CXXLiteralOperatorName:
3220 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3221 Import(FromName.getCXXLiteralIdentifier()));
3222
3223 case DeclarationName::CXXUsingDirective:
3224 // FIXME: STATICS!
3225 return DeclarationName::getUsingDirectiveName();
3226 }
3227
3228 // Silence bogus GCC warning
3229 return DeclarationName();
3230}
3231
3232IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3233 if (!FromId)
3234 return 0;
3235
3236 return &ToContext.Idents.get(FromId->getName());
3237}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003238
Douglas Gregor43f54792010-02-17 02:12:47 +00003239Selector ASTImporter::Import(Selector FromSel) {
3240 if (FromSel.isNull())
3241 return Selector();
3242
3243 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3244 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3245 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3246 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3247 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3248}
3249
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003250DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3251 DeclContext *DC,
3252 unsigned IDNS,
3253 NamedDecl **Decls,
3254 unsigned NumDecls) {
3255 return Name;
3256}
3257
3258DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003259 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003260}
3261
3262DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003263 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003264}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003265
3266Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3267 ImportedDecls[From] = To;
3268 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00003269}
Douglas Gregorb4964f72010-02-15 23:54:17 +00003270
3271bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3272 llvm::DenseMap<Type *, Type *>::iterator Pos
3273 = ImportedTypes.find(From.getTypePtr());
3274 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3275 return true;
3276
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003277 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00003278 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003279}