blob: dc0aeaa83b283a59d73402600c2fb2de3b3991fb [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 Gregora082a492010-11-30 19:14:50 +000087 TemplateParameterList *ImportTemplateParameterList(
88 TemplateParameterList *Params);
Douglas Gregor5c73e912010-02-11 00:48:18 +000089 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000090 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000091 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000092 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000093 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000094 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000095 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +000096 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +000097 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +000098 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +000099 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
100 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
101 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
102 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000103 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000104 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000105 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000106 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000107 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000108 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000109 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000110 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000111 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000112 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000113 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000114 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000115 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000116 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
117 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
118 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
119 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000120
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000121 // Importing statements
122 Stmt *VisitStmt(Stmt *S);
123
124 // Importing expressions
125 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000126 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000127 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000128 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000129 Expr *VisitParenExpr(ParenExpr *E);
130 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000131 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000132 Expr *VisitBinaryOperator(BinaryOperator *E);
133 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000134 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000135 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000136 };
137}
138
139//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000140// Structural Equivalence
141//----------------------------------------------------------------------------
142
143namespace {
144 struct StructuralEquivalenceContext {
145 /// \brief AST contexts for which we are checking structural equivalence.
146 ASTContext &C1, &C2;
147
Douglas Gregor3996e242010-02-15 22:01:00 +0000148 /// \brief The set of "tentative" equivalences between two canonical
149 /// declarations, mapping from a declaration in the first context to the
150 /// declaration in the second context that we believe to be equivalent.
151 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
152
153 /// \brief Queue of declarations in the first context whose equivalence
154 /// with a declaration in the second context still needs to be verified.
155 std::deque<Decl *> DeclsToCheck;
156
Douglas Gregorb4964f72010-02-15 23:54:17 +0000157 /// \brief Declaration (from, to) pairs that are known not to be equivalent
158 /// (which we have already complained about).
159 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
160
Douglas Gregor3996e242010-02-15 22:01:00 +0000161 /// \brief Whether we're being strict about the spelling of types when
162 /// unifying two types.
163 bool StrictTypeSpelling;
164
165 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000166 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000167 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000168 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000169 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000170
171 /// \brief Determine whether the two declarations are structurally
172 /// equivalent.
173 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
174
175 /// \brief Determine whether the two types are structurally equivalent.
176 bool IsStructurallyEquivalent(QualType T1, QualType T2);
177
178 private:
179 /// \brief Finish checking all of the structural equivalences.
180 ///
181 /// \returns true if an error occurred, false otherwise.
182 bool Finish();
183
184 public:
185 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000186 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000187 }
188
189 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000190 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000191 }
192 };
193}
194
195static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
196 QualType T1, QualType T2);
197static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
198 Decl *D1, Decl *D2);
199
200/// \brief Determine if two APInts have the same value, after zero-extending
201/// one of them (if needed!) to ensure that the bit-widths match.
202static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
203 if (I1.getBitWidth() == I2.getBitWidth())
204 return I1 == I2;
205
206 if (I1.getBitWidth() > I2.getBitWidth())
207 return I1 == llvm::APInt(I2).zext(I1.getBitWidth());
208
209 return llvm::APInt(I1).zext(I2.getBitWidth()) == I2;
210}
211
212/// \brief Determine if two APSInts have the same value, zero- or sign-extending
213/// as needed.
214static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
215 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
216 return I1 == I2;
217
218 // Check for a bit-width mismatch.
219 if (I1.getBitWidth() > I2.getBitWidth())
220 return IsSameValue(I1, llvm::APSInt(I2).extend(I1.getBitWidth()));
221 else if (I2.getBitWidth() > I1.getBitWidth())
222 return IsSameValue(llvm::APSInt(I1).extend(I2.getBitWidth()), I2);
223
224 // We have a signedness mismatch. Turn the signed value into an unsigned
225 // value.
226 if (I1.isSigned()) {
227 if (I1.isNegative())
228 return false;
229
230 return llvm::APSInt(I1, true) == I2;
231 }
232
233 if (I2.isNegative())
234 return false;
235
236 return I1 == llvm::APSInt(I2, true);
237}
238
239/// \brief Determine structural equivalence of two expressions.
240static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
241 Expr *E1, Expr *E2) {
242 if (!E1 || !E2)
243 return E1 == E2;
244
245 // FIXME: Actually perform a structural comparison!
246 return true;
247}
248
249/// \brief Determine whether two identifiers are equivalent.
250static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
251 const IdentifierInfo *Name2) {
252 if (!Name1 || !Name2)
253 return Name1 == Name2;
254
255 return Name1->getName() == Name2->getName();
256}
257
258/// \brief Determine whether two nested-name-specifiers are equivalent.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 NestedNameSpecifier *NNS1,
261 NestedNameSpecifier *NNS2) {
262 // FIXME: Implement!
263 return true;
264}
265
266/// \brief Determine whether two template arguments are equivalent.
267static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
268 const TemplateArgument &Arg1,
269 const TemplateArgument &Arg2) {
270 // FIXME: Implement!
271 return true;
272}
273
274/// \brief Determine structural equivalence for the common part of array
275/// types.
276static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
277 const ArrayType *Array1,
278 const ArrayType *Array2) {
279 if (!IsStructurallyEquivalent(Context,
280 Array1->getElementType(),
281 Array2->getElementType()))
282 return false;
283 if (Array1->getSizeModifier() != Array2->getSizeModifier())
284 return false;
285 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
286 return false;
287
288 return true;
289}
290
291/// \brief Determine structural equivalence of two types.
292static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
293 QualType T1, QualType T2) {
294 if (T1.isNull() || T2.isNull())
295 return T1.isNull() && T2.isNull();
296
297 if (!Context.StrictTypeSpelling) {
298 // We aren't being strict about token-to-token equivalence of types,
299 // so map down to the canonical type.
300 T1 = Context.C1.getCanonicalType(T1);
301 T2 = Context.C2.getCanonicalType(T2);
302 }
303
304 if (T1.getQualifiers() != T2.getQualifiers())
305 return false;
306
Douglas Gregorb4964f72010-02-15 23:54:17 +0000307 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000308
Douglas Gregorb4964f72010-02-15 23:54:17 +0000309 if (T1->getTypeClass() != T2->getTypeClass()) {
310 // Compare function types with prototypes vs. without prototypes as if
311 // both did not have prototypes.
312 if (T1->getTypeClass() == Type::FunctionProto &&
313 T2->getTypeClass() == Type::FunctionNoProto)
314 TC = Type::FunctionNoProto;
315 else if (T1->getTypeClass() == Type::FunctionNoProto &&
316 T2->getTypeClass() == Type::FunctionProto)
317 TC = Type::FunctionNoProto;
318 else
319 return false;
320 }
321
322 switch (TC) {
323 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000324 // FIXME: Deal with Char_S/Char_U.
325 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
326 return false;
327 break;
328
329 case Type::Complex:
330 if (!IsStructurallyEquivalent(Context,
331 cast<ComplexType>(T1)->getElementType(),
332 cast<ComplexType>(T2)->getElementType()))
333 return false;
334 break;
335
336 case Type::Pointer:
337 if (!IsStructurallyEquivalent(Context,
338 cast<PointerType>(T1)->getPointeeType(),
339 cast<PointerType>(T2)->getPointeeType()))
340 return false;
341 break;
342
343 case Type::BlockPointer:
344 if (!IsStructurallyEquivalent(Context,
345 cast<BlockPointerType>(T1)->getPointeeType(),
346 cast<BlockPointerType>(T2)->getPointeeType()))
347 return false;
348 break;
349
350 case Type::LValueReference:
351 case Type::RValueReference: {
352 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
353 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
354 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
355 return false;
356 if (Ref1->isInnerRef() != Ref2->isInnerRef())
357 return false;
358 if (!IsStructurallyEquivalent(Context,
359 Ref1->getPointeeTypeAsWritten(),
360 Ref2->getPointeeTypeAsWritten()))
361 return false;
362 break;
363 }
364
365 case Type::MemberPointer: {
366 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
367 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
368 if (!IsStructurallyEquivalent(Context,
369 MemPtr1->getPointeeType(),
370 MemPtr2->getPointeeType()))
371 return false;
372 if (!IsStructurallyEquivalent(Context,
373 QualType(MemPtr1->getClass(), 0),
374 QualType(MemPtr2->getClass(), 0)))
375 return false;
376 break;
377 }
378
379 case Type::ConstantArray: {
380 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
381 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
382 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
383 return false;
384
385 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
386 return false;
387 break;
388 }
389
390 case Type::IncompleteArray:
391 if (!IsArrayStructurallyEquivalent(Context,
392 cast<ArrayType>(T1),
393 cast<ArrayType>(T2)))
394 return false;
395 break;
396
397 case Type::VariableArray: {
398 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
399 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
400 if (!IsStructurallyEquivalent(Context,
401 Array1->getSizeExpr(), Array2->getSizeExpr()))
402 return false;
403
404 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
405 return false;
406
407 break;
408 }
409
410 case Type::DependentSizedArray: {
411 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
412 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
413 if (!IsStructurallyEquivalent(Context,
414 Array1->getSizeExpr(), Array2->getSizeExpr()))
415 return false;
416
417 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
418 return false;
419
420 break;
421 }
422
423 case Type::DependentSizedExtVector: {
424 const DependentSizedExtVectorType *Vec1
425 = cast<DependentSizedExtVectorType>(T1);
426 const DependentSizedExtVectorType *Vec2
427 = cast<DependentSizedExtVectorType>(T2);
428 if (!IsStructurallyEquivalent(Context,
429 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
430 return false;
431 if (!IsStructurallyEquivalent(Context,
432 Vec1->getElementType(),
433 Vec2->getElementType()))
434 return false;
435 break;
436 }
437
438 case Type::Vector:
439 case Type::ExtVector: {
440 const VectorType *Vec1 = cast<VectorType>(T1);
441 const VectorType *Vec2 = cast<VectorType>(T2);
442 if (!IsStructurallyEquivalent(Context,
443 Vec1->getElementType(),
444 Vec2->getElementType()))
445 return false;
446 if (Vec1->getNumElements() != Vec2->getNumElements())
447 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000448 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000449 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000450 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000451 }
452
453 case Type::FunctionProto: {
454 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
455 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
456 if (Proto1->getNumArgs() != Proto2->getNumArgs())
457 return false;
458 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
459 if (!IsStructurallyEquivalent(Context,
460 Proto1->getArgType(I),
461 Proto2->getArgType(I)))
462 return false;
463 }
464 if (Proto1->isVariadic() != Proto2->isVariadic())
465 return false;
466 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
467 return false;
468 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
469 return false;
470 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
471 return false;
472 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
473 if (!IsStructurallyEquivalent(Context,
474 Proto1->getExceptionType(I),
475 Proto2->getExceptionType(I)))
476 return false;
477 }
478 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
479 return false;
480
481 // Fall through to check the bits common with FunctionNoProtoType.
482 }
483
484 case Type::FunctionNoProto: {
485 const FunctionType *Function1 = cast<FunctionType>(T1);
486 const FunctionType *Function2 = cast<FunctionType>(T2);
487 if (!IsStructurallyEquivalent(Context,
488 Function1->getResultType(),
489 Function2->getResultType()))
490 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000491 if (Function1->getExtInfo() != Function2->getExtInfo())
492 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000493 break;
494 }
495
496 case Type::UnresolvedUsing:
497 if (!IsStructurallyEquivalent(Context,
498 cast<UnresolvedUsingType>(T1)->getDecl(),
499 cast<UnresolvedUsingType>(T2)->getDecl()))
500 return false;
501
502 break;
503
504 case Type::Typedef:
505 if (!IsStructurallyEquivalent(Context,
506 cast<TypedefType>(T1)->getDecl(),
507 cast<TypedefType>(T2)->getDecl()))
508 return false;
509 break;
510
511 case Type::TypeOfExpr:
512 if (!IsStructurallyEquivalent(Context,
513 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
514 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
515 return false;
516 break;
517
518 case Type::TypeOf:
519 if (!IsStructurallyEquivalent(Context,
520 cast<TypeOfType>(T1)->getUnderlyingType(),
521 cast<TypeOfType>(T2)->getUnderlyingType()))
522 return false;
523 break;
524
525 case Type::Decltype:
526 if (!IsStructurallyEquivalent(Context,
527 cast<DecltypeType>(T1)->getUnderlyingExpr(),
528 cast<DecltypeType>(T2)->getUnderlyingExpr()))
529 return false;
530 break;
531
532 case Type::Record:
533 case Type::Enum:
534 if (!IsStructurallyEquivalent(Context,
535 cast<TagType>(T1)->getDecl(),
536 cast<TagType>(T2)->getDecl()))
537 return false;
538 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000539
Douglas Gregor3996e242010-02-15 22:01:00 +0000540 case Type::TemplateTypeParm: {
541 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
542 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
543 if (Parm1->getDepth() != Parm2->getDepth())
544 return false;
545 if (Parm1->getIndex() != Parm2->getIndex())
546 return false;
547 if (Parm1->isParameterPack() != Parm2->isParameterPack())
548 return false;
549
550 // Names of template type parameters are never significant.
551 break;
552 }
553
554 case Type::SubstTemplateTypeParm: {
555 const SubstTemplateTypeParmType *Subst1
556 = cast<SubstTemplateTypeParmType>(T1);
557 const SubstTemplateTypeParmType *Subst2
558 = cast<SubstTemplateTypeParmType>(T2);
559 if (!IsStructurallyEquivalent(Context,
560 QualType(Subst1->getReplacedParameter(), 0),
561 QualType(Subst2->getReplacedParameter(), 0)))
562 return false;
563 if (!IsStructurallyEquivalent(Context,
564 Subst1->getReplacementType(),
565 Subst2->getReplacementType()))
566 return false;
567 break;
568 }
569
570 case Type::TemplateSpecialization: {
571 const TemplateSpecializationType *Spec1
572 = cast<TemplateSpecializationType>(T1);
573 const TemplateSpecializationType *Spec2
574 = cast<TemplateSpecializationType>(T2);
575 if (!IsStructurallyEquivalent(Context,
576 Spec1->getTemplateName(),
577 Spec2->getTemplateName()))
578 return false;
579 if (Spec1->getNumArgs() != Spec2->getNumArgs())
580 return false;
581 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
582 if (!IsStructurallyEquivalent(Context,
583 Spec1->getArg(I), Spec2->getArg(I)))
584 return false;
585 }
586 break;
587 }
588
Abramo Bagnara6150c882010-05-11 21:36:43 +0000589 case Type::Elaborated: {
590 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
591 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
592 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
593 if (Elab1->getKeyword() != Elab2->getKeyword())
594 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000595 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000596 Elab1->getQualifier(),
597 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000598 return false;
599 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000600 Elab1->getNamedType(),
601 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000602 return false;
603 break;
604 }
605
John McCalle78aac42010-03-10 03:28:59 +0000606 case Type::InjectedClassName: {
607 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
608 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
609 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000610 Inj1->getInjectedSpecializationType(),
611 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000612 return false;
613 break;
614 }
615
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000616 case Type::DependentName: {
617 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
618 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000619 if (!IsStructurallyEquivalent(Context,
620 Typename1->getQualifier(),
621 Typename2->getQualifier()))
622 return false;
623 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
624 Typename2->getIdentifier()))
625 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000626
627 break;
628 }
629
John McCallc392f372010-06-11 00:33:02 +0000630 case Type::DependentTemplateSpecialization: {
631 const DependentTemplateSpecializationType *Spec1 =
632 cast<DependentTemplateSpecializationType>(T1);
633 const DependentTemplateSpecializationType *Spec2 =
634 cast<DependentTemplateSpecializationType>(T2);
635 if (!IsStructurallyEquivalent(Context,
636 Spec1->getQualifier(),
637 Spec2->getQualifier()))
638 return false;
639 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
640 Spec2->getIdentifier()))
641 return false;
642 if (Spec1->getNumArgs() != Spec2->getNumArgs())
643 return false;
644 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
645 if (!IsStructurallyEquivalent(Context,
646 Spec1->getArg(I), Spec2->getArg(I)))
647 return false;
648 }
649 break;
650 }
651
Douglas Gregor3996e242010-02-15 22:01:00 +0000652 case Type::ObjCInterface: {
653 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
654 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
655 if (!IsStructurallyEquivalent(Context,
656 Iface1->getDecl(), Iface2->getDecl()))
657 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000658 break;
659 }
660
661 case Type::ObjCObject: {
662 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
663 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
664 if (!IsStructurallyEquivalent(Context,
665 Obj1->getBaseType(),
666 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000667 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000668 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
669 return false;
670 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000671 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000672 Obj1->getProtocol(I),
673 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000674 return false;
675 }
676 break;
677 }
678
679 case Type::ObjCObjectPointer: {
680 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
681 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
682 if (!IsStructurallyEquivalent(Context,
683 Ptr1->getPointeeType(),
684 Ptr2->getPointeeType()))
685 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000686 break;
687 }
688
689 } // end switch
690
691 return true;
692}
693
694/// \brief Determine structural equivalence of two records.
695static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
696 RecordDecl *D1, RecordDecl *D2) {
697 if (D1->isUnion() != D2->isUnion()) {
698 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
699 << Context.C2.getTypeDeclType(D2);
700 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
701 << D1->getDeclName() << (unsigned)D1->getTagKind();
702 return false;
703 }
704
Douglas Gregorb4964f72010-02-15 23:54:17 +0000705 // Compare the definitions of these two records. If either or both are
706 // incomplete, we assume that they are equivalent.
707 D1 = D1->getDefinition();
708 D2 = D2->getDefinition();
709 if (!D1 || !D2)
710 return true;
711
Douglas Gregor3996e242010-02-15 22:01:00 +0000712 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
713 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
714 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
715 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000716 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000717 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000718 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000719 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000720 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000721 return false;
722 }
723
724 // Check the base classes.
725 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
726 BaseEnd1 = D1CXX->bases_end(),
727 Base2 = D2CXX->bases_begin();
728 Base1 != BaseEnd1;
729 ++Base1, ++Base2) {
730 if (!IsStructurallyEquivalent(Context,
731 Base1->getType(), Base2->getType())) {
732 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
733 << Context.C2.getTypeDeclType(D2);
734 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
735 << Base2->getType()
736 << Base2->getSourceRange();
737 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
738 << Base1->getType()
739 << Base1->getSourceRange();
740 return false;
741 }
742
743 // Check virtual vs. non-virtual inheritance mismatch.
744 if (Base1->isVirtual() != Base2->isVirtual()) {
745 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
746 << Context.C2.getTypeDeclType(D2);
747 Context.Diag2(Base2->getSourceRange().getBegin(),
748 diag::note_odr_virtual_base)
749 << Base2->isVirtual() << Base2->getSourceRange();
750 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
751 << Base1->isVirtual()
752 << Base1->getSourceRange();
753 return false;
754 }
755 }
756 } else if (D1CXX->getNumBases() > 0) {
757 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
758 << Context.C2.getTypeDeclType(D2);
759 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
760 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
761 << Base1->getType()
762 << Base1->getSourceRange();
763 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
764 return false;
765 }
766 }
767
768 // Check the fields for consistency.
769 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
770 Field2End = D2->field_end();
771 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
772 Field1End = D1->field_end();
773 Field1 != Field1End;
774 ++Field1, ++Field2) {
775 if (Field2 == Field2End) {
776 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
777 << Context.C2.getTypeDeclType(D2);
778 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
779 << Field1->getDeclName() << Field1->getType();
780 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
781 return false;
782 }
783
784 if (!IsStructurallyEquivalent(Context,
785 Field1->getType(), Field2->getType())) {
786 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
787 << Context.C2.getTypeDeclType(D2);
788 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
789 << Field2->getDeclName() << Field2->getType();
790 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
791 << Field1->getDeclName() << Field1->getType();
792 return false;
793 }
794
795 if (Field1->isBitField() != Field2->isBitField()) {
796 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
797 << Context.C2.getTypeDeclType(D2);
798 if (Field1->isBitField()) {
799 llvm::APSInt Bits;
800 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
801 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
802 << Field1->getDeclName() << Field1->getType()
803 << Bits.toString(10, false);
804 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
805 << Field2->getDeclName();
806 } else {
807 llvm::APSInt Bits;
808 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
809 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
810 << Field2->getDeclName() << Field2->getType()
811 << Bits.toString(10, false);
812 Context.Diag1(Field1->getLocation(),
813 diag::note_odr_not_bit_field)
814 << Field1->getDeclName();
815 }
816 return false;
817 }
818
819 if (Field1->isBitField()) {
820 // Make sure that the bit-fields are the same length.
821 llvm::APSInt Bits1, Bits2;
822 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
823 return false;
824 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
825 return false;
826
827 if (!IsSameValue(Bits1, Bits2)) {
828 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
829 << Context.C2.getTypeDeclType(D2);
830 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
831 << Field2->getDeclName() << Field2->getType()
832 << Bits2.toString(10, false);
833 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
834 << Field1->getDeclName() << Field1->getType()
835 << Bits1.toString(10, false);
836 return false;
837 }
838 }
839 }
840
841 if (Field2 != Field2End) {
842 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
843 << Context.C2.getTypeDeclType(D2);
844 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
845 << Field2->getDeclName() << Field2->getType();
846 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
847 return false;
848 }
849
850 return true;
851}
852
853/// \brief Determine structural equivalence of two enums.
854static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
855 EnumDecl *D1, EnumDecl *D2) {
856 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
857 EC2End = D2->enumerator_end();
858 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
859 EC1End = D1->enumerator_end();
860 EC1 != EC1End; ++EC1, ++EC2) {
861 if (EC2 == EC2End) {
862 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(D2);
864 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
865 << EC1->getDeclName()
866 << EC1->getInitVal().toString(10);
867 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
868 return false;
869 }
870
871 llvm::APSInt Val1 = EC1->getInitVal();
872 llvm::APSInt Val2 = EC2->getInitVal();
873 if (!IsSameValue(Val1, Val2) ||
874 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876 << Context.C2.getTypeDeclType(D2);
877 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
878 << EC2->getDeclName()
879 << EC2->getInitVal().toString(10);
880 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
881 << EC1->getDeclName()
882 << EC1->getInitVal().toString(10);
883 return false;
884 }
885 }
886
887 if (EC2 != EC2End) {
888 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
889 << Context.C2.getTypeDeclType(D2);
890 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
891 << EC2->getDeclName()
892 << EC2->getInitVal().toString(10);
893 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
894 return false;
895 }
896
897 return true;
898}
Douglas Gregora082a492010-11-30 19:14:50 +0000899
900static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
901 TemplateParameterList *Params1,
902 TemplateParameterList *Params2) {
903 if (Params1->size() != Params2->size()) {
904 Context.Diag2(Params2->getTemplateLoc(),
905 diag::err_odr_different_num_template_parameters)
906 << Params1->size() << Params2->size();
907 Context.Diag1(Params1->getTemplateLoc(),
908 diag::note_odr_template_parameter_list);
909 return false;
910 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000911
Douglas Gregora082a492010-11-30 19:14:50 +0000912 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
913 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
914 Context.Diag2(Params2->getParam(I)->getLocation(),
915 diag::err_odr_different_template_parameter_kind);
916 Context.Diag1(Params1->getParam(I)->getLocation(),
917 diag::note_odr_template_parameter_here);
918 return false;
919 }
920
921 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
922 Params2->getParam(I))) {
923
924 return false;
925 }
926 }
927
928 return true;
929}
930
931static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
932 TemplateTypeParmDecl *D1,
933 TemplateTypeParmDecl *D2) {
934 if (D1->isParameterPack() != D2->isParameterPack()) {
935 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
936 << D2->isParameterPack();
937 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
938 << D1->isParameterPack();
939 return false;
940 }
941
942 return true;
943}
944
945static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
946 NonTypeTemplateParmDecl *D1,
947 NonTypeTemplateParmDecl *D2) {
948 // FIXME: Enable once we have variadic templates.
949#if 0
950 if (D1->isParameterPack() != D2->isParameterPack()) {
951 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
952 << D2->isParameterPack();
953 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
954 << D1->isParameterPack();
955 return false;
956 }
957#endif
958
959 // Check types.
960 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
961 Context.Diag2(D2->getLocation(),
962 diag::err_odr_non_type_parameter_type_inconsistent)
963 << D2->getType() << D1->getType();
964 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
965 << D1->getType();
966 return false;
967 }
968
969 return true;
970}
971
972static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
973 TemplateTemplateParmDecl *D1,
974 TemplateTemplateParmDecl *D2) {
975 // FIXME: Enable once we have variadic templates.
976#if 0
977 if (D1->isParameterPack() != D2->isParameterPack()) {
978 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
979 << D2->isParameterPack();
980 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
981 << D1->isParameterPack();
982 return false;
983 }
984#endif
985
986 // Check template parameter lists.
987 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
988 D2->getTemplateParameters());
989}
990
991static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
992 ClassTemplateDecl *D1,
993 ClassTemplateDecl *D2) {
994 // Check template parameters.
995 if (!IsStructurallyEquivalent(Context,
996 D1->getTemplateParameters(),
997 D2->getTemplateParameters()))
998 return false;
999
1000 // Check the templated declaration.
1001 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1002 D2->getTemplatedDecl());
1003}
1004
Douglas Gregor3996e242010-02-15 22:01:00 +00001005/// \brief Determine structural equivalence of two declarations.
1006static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1007 Decl *D1, Decl *D2) {
1008 // FIXME: Check for known structural equivalences via a callback of some sort.
1009
Douglas Gregorb4964f72010-02-15 23:54:17 +00001010 // Check whether we already know that these two declarations are not
1011 // structurally equivalent.
1012 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1013 D2->getCanonicalDecl())))
1014 return false;
1015
Douglas Gregor3996e242010-02-15 22:01:00 +00001016 // Determine whether we've already produced a tentative equivalence for D1.
1017 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1018 if (EquivToD1)
1019 return EquivToD1 == D2->getCanonicalDecl();
1020
1021 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1022 EquivToD1 = D2->getCanonicalDecl();
1023 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1024 return true;
1025}
1026
1027bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1028 Decl *D2) {
1029 if (!::IsStructurallyEquivalent(*this, D1, D2))
1030 return false;
1031
1032 return !Finish();
1033}
1034
1035bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1036 QualType T2) {
1037 if (!::IsStructurallyEquivalent(*this, T1, T2))
1038 return false;
1039
1040 return !Finish();
1041}
1042
1043bool StructuralEquivalenceContext::Finish() {
1044 while (!DeclsToCheck.empty()) {
1045 // Check the next declaration.
1046 Decl *D1 = DeclsToCheck.front();
1047 DeclsToCheck.pop_front();
1048
1049 Decl *D2 = TentativeEquivalences[D1];
1050 assert(D2 && "Unrecorded tentative equivalence?");
1051
Douglas Gregorb4964f72010-02-15 23:54:17 +00001052 bool Equivalent = true;
1053
Douglas Gregor3996e242010-02-15 22:01:00 +00001054 // FIXME: Switch on all declaration kinds. For now, we're just going to
1055 // check the obvious ones.
1056 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1057 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1058 // Check for equivalent structure names.
1059 IdentifierInfo *Name1 = Record1->getIdentifier();
1060 if (!Name1 && Record1->getTypedefForAnonDecl())
1061 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1062 IdentifierInfo *Name2 = Record2->getIdentifier();
1063 if (!Name2 && Record2->getTypedefForAnonDecl())
1064 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001065 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1066 !::IsStructurallyEquivalent(*this, Record1, Record2))
1067 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001068 } else {
1069 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001070 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001071 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001072 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001073 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1074 // Check for equivalent enum names.
1075 IdentifierInfo *Name1 = Enum1->getIdentifier();
1076 if (!Name1 && Enum1->getTypedefForAnonDecl())
1077 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1078 IdentifierInfo *Name2 = Enum2->getIdentifier();
1079 if (!Name2 && Enum2->getTypedefForAnonDecl())
1080 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001081 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1082 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1083 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001084 } else {
1085 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001086 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001087 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001088 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001089 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1090 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001091 Typedef2->getIdentifier()) ||
1092 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001093 Typedef1->getUnderlyingType(),
1094 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001095 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001096 } else {
1097 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001098 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001099 }
Douglas Gregora082a492010-11-30 19:14:50 +00001100 } else if (ClassTemplateDecl *ClassTemplate1
1101 = dyn_cast<ClassTemplateDecl>(D1)) {
1102 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1103 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1104 ClassTemplate2->getIdentifier()) ||
1105 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1106 Equivalent = false;
1107 } else {
1108 // Class template/non-class-template mismatch.
1109 Equivalent = false;
1110 }
1111 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1112 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1113 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1114 Equivalent = false;
1115 } else {
1116 // Kind mismatch.
1117 Equivalent = false;
1118 }
1119 } else if (NonTypeTemplateParmDecl *NTTP1
1120 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1121 if (NonTypeTemplateParmDecl *NTTP2
1122 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1123 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1124 Equivalent = false;
1125 } else {
1126 // Kind mismatch.
1127 Equivalent = false;
1128 }
1129 } else if (TemplateTemplateParmDecl *TTP1
1130 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1131 if (TemplateTemplateParmDecl *TTP2
1132 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1133 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1134 Equivalent = false;
1135 } else {
1136 // Kind mismatch.
1137 Equivalent = false;
1138 }
1139 }
1140
Douglas Gregorb4964f72010-02-15 23:54:17 +00001141 if (!Equivalent) {
1142 // Note that these two declarations are not equivalent (and we already
1143 // know about it).
1144 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1145 D2->getCanonicalDecl()));
1146 return true;
1147 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001148 // FIXME: Check other declaration kinds!
1149 }
1150
1151 return false;
1152}
1153
1154//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001155// Import Types
1156//----------------------------------------------------------------------------
1157
Douglas Gregore4c83e42010-02-09 22:48:33 +00001158QualType ASTNodeImporter::VisitType(Type *T) {
1159 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1160 << T->getTypeClassName();
1161 return QualType();
1162}
1163
Douglas Gregor96e578d2010-02-05 17:54:41 +00001164QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1165 switch (T->getKind()) {
1166 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1167 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1168
1169 case BuiltinType::Char_U:
1170 // The context we're importing from has an unsigned 'char'. If we're
1171 // importing into a context with a signed 'char', translate to
1172 // 'unsigned char' instead.
1173 if (Importer.getToContext().getLangOptions().CharIsSigned)
1174 return Importer.getToContext().UnsignedCharTy;
1175
1176 return Importer.getToContext().CharTy;
1177
1178 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1179
1180 case BuiltinType::Char16:
1181 // FIXME: Make sure that the "to" context supports C++!
1182 return Importer.getToContext().Char16Ty;
1183
1184 case BuiltinType::Char32:
1185 // FIXME: Make sure that the "to" context supports C++!
1186 return Importer.getToContext().Char32Ty;
1187
1188 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1189 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1190 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1191 case BuiltinType::ULongLong:
1192 return Importer.getToContext().UnsignedLongLongTy;
1193 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1194
1195 case BuiltinType::Char_S:
1196 // The context we're importing from has an unsigned 'char'. If we're
1197 // importing into a context with a signed 'char', translate to
1198 // 'unsigned char' instead.
1199 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1200 return Importer.getToContext().SignedCharTy;
1201
1202 return Importer.getToContext().CharTy;
1203
1204 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1205 case BuiltinType::WChar:
1206 // FIXME: If not in C++, shall we translate to the C equivalent of
1207 // wchar_t?
1208 return Importer.getToContext().WCharTy;
1209
1210 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1211 case BuiltinType::Int : return Importer.getToContext().IntTy;
1212 case BuiltinType::Long : return Importer.getToContext().LongTy;
1213 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1214 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1215 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1216 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1217 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1218
1219 case BuiltinType::NullPtr:
1220 // FIXME: Make sure that the "to" context supports C++0x!
1221 return Importer.getToContext().NullPtrTy;
1222
1223 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1224 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1225 case BuiltinType::UndeducedAuto:
1226 // FIXME: Make sure that the "to" context supports C++0x!
1227 return Importer.getToContext().UndeducedAutoTy;
1228
1229 case BuiltinType::ObjCId:
1230 // FIXME: Make sure that the "to" context supports Objective-C!
1231 return Importer.getToContext().ObjCBuiltinIdTy;
1232
1233 case BuiltinType::ObjCClass:
1234 return Importer.getToContext().ObjCBuiltinClassTy;
1235
1236 case BuiltinType::ObjCSel:
1237 return Importer.getToContext().ObjCBuiltinSelTy;
1238 }
1239
1240 return QualType();
1241}
1242
1243QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1244 QualType ToElementType = Importer.Import(T->getElementType());
1245 if (ToElementType.isNull())
1246 return QualType();
1247
1248 return Importer.getToContext().getComplexType(ToElementType);
1249}
1250
1251QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1252 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1253 if (ToPointeeType.isNull())
1254 return QualType();
1255
1256 return Importer.getToContext().getPointerType(ToPointeeType);
1257}
1258
1259QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1260 // FIXME: Check for blocks support in "to" context.
1261 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1262 if (ToPointeeType.isNull())
1263 return QualType();
1264
1265 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1266}
1267
1268QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1269 // FIXME: Check for C++ support in "to" context.
1270 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1271 if (ToPointeeType.isNull())
1272 return QualType();
1273
1274 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1275}
1276
1277QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1278 // FIXME: Check for C++0x support in "to" context.
1279 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1280 if (ToPointeeType.isNull())
1281 return QualType();
1282
1283 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1284}
1285
1286QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1287 // FIXME: Check for C++ support in "to" context.
1288 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1289 if (ToPointeeType.isNull())
1290 return QualType();
1291
1292 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1293 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1294 ClassType.getTypePtr());
1295}
1296
1297QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1298 QualType ToElementType = Importer.Import(T->getElementType());
1299 if (ToElementType.isNull())
1300 return QualType();
1301
1302 return Importer.getToContext().getConstantArrayType(ToElementType,
1303 T->getSize(),
1304 T->getSizeModifier(),
1305 T->getIndexTypeCVRQualifiers());
1306}
1307
1308QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1309 QualType ToElementType = Importer.Import(T->getElementType());
1310 if (ToElementType.isNull())
1311 return QualType();
1312
1313 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1314 T->getSizeModifier(),
1315 T->getIndexTypeCVRQualifiers());
1316}
1317
1318QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1319 QualType ToElementType = Importer.Import(T->getElementType());
1320 if (ToElementType.isNull())
1321 return QualType();
1322
1323 Expr *Size = Importer.Import(T->getSizeExpr());
1324 if (!Size)
1325 return QualType();
1326
1327 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1328 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1329 T->getSizeModifier(),
1330 T->getIndexTypeCVRQualifiers(),
1331 Brackets);
1332}
1333
1334QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1335 QualType ToElementType = Importer.Import(T->getElementType());
1336 if (ToElementType.isNull())
1337 return QualType();
1338
1339 return Importer.getToContext().getVectorType(ToElementType,
1340 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001341 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001342}
1343
1344QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1345 QualType ToElementType = Importer.Import(T->getElementType());
1346 if (ToElementType.isNull())
1347 return QualType();
1348
1349 return Importer.getToContext().getExtVectorType(ToElementType,
1350 T->getNumElements());
1351}
1352
1353QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1354 // FIXME: What happens if we're importing a function without a prototype
1355 // into C++? Should we make it variadic?
1356 QualType ToResultType = Importer.Import(T->getResultType());
1357 if (ToResultType.isNull())
1358 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001359
Douglas Gregor96e578d2010-02-05 17:54:41 +00001360 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001361 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001362}
1363
1364QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1365 QualType ToResultType = Importer.Import(T->getResultType());
1366 if (ToResultType.isNull())
1367 return QualType();
1368
1369 // Import argument types
1370 llvm::SmallVector<QualType, 4> ArgTypes;
1371 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1372 AEnd = T->arg_type_end();
1373 A != AEnd; ++A) {
1374 QualType ArgType = Importer.Import(*A);
1375 if (ArgType.isNull())
1376 return QualType();
1377 ArgTypes.push_back(ArgType);
1378 }
1379
1380 // Import exception types
1381 llvm::SmallVector<QualType, 4> ExceptionTypes;
1382 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1383 EEnd = T->exception_end();
1384 E != EEnd; ++E) {
1385 QualType ExceptionType = Importer.Import(*E);
1386 if (ExceptionType.isNull())
1387 return QualType();
1388 ExceptionTypes.push_back(ExceptionType);
1389 }
1390
1391 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1392 ArgTypes.size(),
1393 T->isVariadic(),
1394 T->getTypeQuals(),
1395 T->hasExceptionSpec(),
1396 T->hasAnyExceptionSpec(),
1397 ExceptionTypes.size(),
1398 ExceptionTypes.data(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001399 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001400}
1401
1402QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1403 TypedefDecl *ToDecl
1404 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1405 if (!ToDecl)
1406 return QualType();
1407
1408 return Importer.getToContext().getTypeDeclType(ToDecl);
1409}
1410
1411QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1412 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1413 if (!ToExpr)
1414 return QualType();
1415
1416 return Importer.getToContext().getTypeOfExprType(ToExpr);
1417}
1418
1419QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1420 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1421 if (ToUnderlyingType.isNull())
1422 return QualType();
1423
1424 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1425}
1426
1427QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1428 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1429 if (!ToExpr)
1430 return QualType();
1431
1432 return Importer.getToContext().getDecltypeType(ToExpr);
1433}
1434
1435QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1436 RecordDecl *ToDecl
1437 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1438 if (!ToDecl)
1439 return QualType();
1440
1441 return Importer.getToContext().getTagDeclType(ToDecl);
1442}
1443
1444QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1445 EnumDecl *ToDecl
1446 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1447 if (!ToDecl)
1448 return QualType();
1449
1450 return Importer.getToContext().getTagDeclType(ToDecl);
1451}
1452
1453QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001454 NestedNameSpecifier *ToQualifier = 0;
1455 // Note: the qualifier in an ElaboratedType is optional.
1456 if (T->getQualifier()) {
1457 ToQualifier = Importer.Import(T->getQualifier());
1458 if (!ToQualifier)
1459 return QualType();
1460 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001461
1462 QualType ToNamedType = Importer.Import(T->getNamedType());
1463 if (ToNamedType.isNull())
1464 return QualType();
1465
Abramo Bagnara6150c882010-05-11 21:36:43 +00001466 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1467 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001468}
1469
1470QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1471 ObjCInterfaceDecl *Class
1472 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1473 if (!Class)
1474 return QualType();
1475
John McCall8b07ec22010-05-15 11:32:37 +00001476 return Importer.getToContext().getObjCInterfaceType(Class);
1477}
1478
1479QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1480 QualType ToBaseType = Importer.Import(T->getBaseType());
1481 if (ToBaseType.isNull())
1482 return QualType();
1483
Douglas Gregor96e578d2010-02-05 17:54:41 +00001484 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001485 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001486 PEnd = T->qual_end();
1487 P != PEnd; ++P) {
1488 ObjCProtocolDecl *Protocol
1489 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1490 if (!Protocol)
1491 return QualType();
1492 Protocols.push_back(Protocol);
1493 }
1494
John McCall8b07ec22010-05-15 11:32:37 +00001495 return Importer.getToContext().getObjCObjectType(ToBaseType,
1496 Protocols.data(),
1497 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001498}
1499
1500QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1501 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1502 if (ToPointeeType.isNull())
1503 return QualType();
1504
John McCall8b07ec22010-05-15 11:32:37 +00001505 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001506}
1507
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001508//----------------------------------------------------------------------------
1509// Import Declarations
1510//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001511bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1512 DeclContext *&LexicalDC,
1513 DeclarationName &Name,
1514 SourceLocation &Loc) {
1515 // Import the context of this declaration.
1516 DC = Importer.ImportContext(D->getDeclContext());
1517 if (!DC)
1518 return true;
1519
1520 LexicalDC = DC;
1521 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1522 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1523 if (!LexicalDC)
1524 return true;
1525 }
1526
1527 // Import the name of this declaration.
1528 Name = Importer.Import(D->getDeclName());
1529 if (D->getDeclName() && !Name)
1530 return true;
1531
1532 // Import the location of this declaration.
1533 Loc = Importer.Import(D->getLocation());
1534 return false;
1535}
1536
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001537void
1538ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1539 DeclarationNameInfo& To) {
1540 // NOTE: To.Name and To.Loc are already imported.
1541 // We only have to import To.LocInfo.
1542 switch (To.getName().getNameKind()) {
1543 case DeclarationName::Identifier:
1544 case DeclarationName::ObjCZeroArgSelector:
1545 case DeclarationName::ObjCOneArgSelector:
1546 case DeclarationName::ObjCMultiArgSelector:
1547 case DeclarationName::CXXUsingDirective:
1548 return;
1549
1550 case DeclarationName::CXXOperatorName: {
1551 SourceRange Range = From.getCXXOperatorNameRange();
1552 To.setCXXOperatorNameRange(Importer.Import(Range));
1553 return;
1554 }
1555 case DeclarationName::CXXLiteralOperatorName: {
1556 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1557 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1558 return;
1559 }
1560 case DeclarationName::CXXConstructorName:
1561 case DeclarationName::CXXDestructorName:
1562 case DeclarationName::CXXConversionFunctionName: {
1563 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1564 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1565 return;
1566 }
1567 assert(0 && "Unknown name kind.");
1568 }
1569}
1570
Douglas Gregor968d6332010-02-21 18:24:45 +00001571void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1572 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1573 FromEnd = FromDC->decls_end();
1574 From != FromEnd;
1575 ++From)
1576 Importer.Import(*From);
1577}
1578
Douglas Gregora082a492010-11-30 19:14:50 +00001579TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1580 TemplateParameterList *Params) {
1581 llvm::SmallVector<NamedDecl *, 4> ToParams;
1582 ToParams.reserve(Params->size());
1583 for (TemplateParameterList::iterator P = Params->begin(),
1584 PEnd = Params->end();
1585 P != PEnd; ++P) {
1586 Decl *To = Importer.Import(*P);
1587 if (!To)
1588 return 0;
1589
1590 ToParams.push_back(cast<NamedDecl>(To));
1591 }
1592
1593 return TemplateParameterList::Create(Importer.getToContext(),
1594 Importer.Import(Params->getTemplateLoc()),
1595 Importer.Import(Params->getLAngleLoc()),
1596 ToParams.data(), ToParams.size(),
1597 Importer.Import(Params->getRAngleLoc()));
1598}
1599
Douglas Gregor5c73e912010-02-11 00:48:18 +00001600bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001601 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001602 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001603 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001604 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001605 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001606}
1607
Douglas Gregor98c10182010-02-12 22:17:39 +00001608bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001609 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001610 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001611 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001612 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001613}
1614
Douglas Gregora082a492010-11-30 19:14:50 +00001615bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1616 ClassTemplateDecl *To) {
1617 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1618 Importer.getToContext(),
1619 Importer.getNonEquivalentDecls());
1620 return Ctx.IsStructurallyEquivalent(From, To);
1621}
1622
Douglas Gregore4c83e42010-02-09 22:48:33 +00001623Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001624 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001625 << D->getDeclKindName();
1626 return 0;
1627}
1628
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001629Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1630 // Import the major distinguishing characteristics of this namespace.
1631 DeclContext *DC, *LexicalDC;
1632 DeclarationName Name;
1633 SourceLocation Loc;
1634 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1635 return 0;
1636
1637 NamespaceDecl *MergeWithNamespace = 0;
1638 if (!Name) {
1639 // This is an anonymous namespace. Adopt an existing anonymous
1640 // namespace if we can.
1641 // FIXME: Not testable.
1642 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1643 MergeWithNamespace = TU->getAnonymousNamespace();
1644 else
1645 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1646 } else {
1647 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1648 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1649 Lookup.first != Lookup.second;
1650 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001651 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001652 continue;
1653
1654 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1655 MergeWithNamespace = FoundNS;
1656 ConflictingDecls.clear();
1657 break;
1658 }
1659
1660 ConflictingDecls.push_back(*Lookup.first);
1661 }
1662
1663 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001664 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001665 ConflictingDecls.data(),
1666 ConflictingDecls.size());
1667 }
1668 }
1669
1670 // Create the "to" namespace, if needed.
1671 NamespaceDecl *ToNamespace = MergeWithNamespace;
1672 if (!ToNamespace) {
1673 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1674 Name.getAsIdentifierInfo());
1675 ToNamespace->setLexicalDeclContext(LexicalDC);
1676 LexicalDC->addDecl(ToNamespace);
1677
1678 // If this is an anonymous namespace, register it as the anonymous
1679 // namespace within its context.
1680 if (!Name) {
1681 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1682 TU->setAnonymousNamespace(ToNamespace);
1683 else
1684 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1685 }
1686 }
1687 Importer.Imported(D, ToNamespace);
1688
1689 ImportDeclContext(D);
1690
1691 return ToNamespace;
1692}
1693
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001694Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1695 // Import the major distinguishing characteristics of this typedef.
1696 DeclContext *DC, *LexicalDC;
1697 DeclarationName Name;
1698 SourceLocation Loc;
1699 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1700 return 0;
1701
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001702 // If this typedef is not in block scope, determine whether we've
1703 // seen a typedef with the same name (that we can merge with) or any
1704 // other entity by that name (which name lookup could conflict with).
1705 if (!DC->isFunctionOrMethod()) {
1706 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1707 unsigned IDNS = Decl::IDNS_Ordinary;
1708 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1709 Lookup.first != Lookup.second;
1710 ++Lookup.first) {
1711 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1712 continue;
1713 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001714 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1715 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001716 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001717 }
1718
1719 ConflictingDecls.push_back(*Lookup.first);
1720 }
1721
1722 if (!ConflictingDecls.empty()) {
1723 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1724 ConflictingDecls.data(),
1725 ConflictingDecls.size());
1726 if (!Name)
1727 return 0;
1728 }
1729 }
1730
Douglas Gregorb4964f72010-02-15 23:54:17 +00001731 // Import the underlying type of this typedef;
1732 QualType T = Importer.Import(D->getUnderlyingType());
1733 if (T.isNull())
1734 return 0;
1735
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001736 // Create the new typedef node.
1737 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1738 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1739 Loc, Name.getAsIdentifierInfo(),
1740 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00001741 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001742 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001743 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001744 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001745
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001746 return ToTypedef;
1747}
1748
Douglas Gregor98c10182010-02-12 22:17:39 +00001749Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1750 // Import the major distinguishing characteristics of this enum.
1751 DeclContext *DC, *LexicalDC;
1752 DeclarationName Name;
1753 SourceLocation Loc;
1754 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1755 return 0;
1756
1757 // Figure out what enum name we're looking for.
1758 unsigned IDNS = Decl::IDNS_Tag;
1759 DeclarationName SearchName = Name;
1760 if (!SearchName && D->getTypedefForAnonDecl()) {
1761 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1762 IDNS = Decl::IDNS_Ordinary;
1763 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1764 IDNS |= Decl::IDNS_Ordinary;
1765
1766 // We may already have an enum of the same name; try to find and match it.
1767 if (!DC->isFunctionOrMethod() && SearchName) {
1768 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1769 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1770 Lookup.first != Lookup.second;
1771 ++Lookup.first) {
1772 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1773 continue;
1774
1775 Decl *Found = *Lookup.first;
1776 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1777 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1778 Found = Tag->getDecl();
1779 }
1780
1781 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001782 if (IsStructuralMatch(D, FoundEnum))
1783 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001784 }
1785
1786 ConflictingDecls.push_back(*Lookup.first);
1787 }
1788
1789 if (!ConflictingDecls.empty()) {
1790 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1791 ConflictingDecls.data(),
1792 ConflictingDecls.size());
1793 }
1794 }
1795
1796 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001797 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Douglas Gregor98c10182010-02-12 22:17:39 +00001798 Name.getAsIdentifierInfo(),
1799 Importer.Import(D->getTagKeywordLoc()),
Douglas Gregor0bf31402010-10-08 23:50:27 +00001800 0, D->isScoped(), D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00001801 // Import the qualifier, if any.
1802 if (D->getQualifier()) {
1803 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1804 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1805 D2->setQualifierInfo(NNS, NNSRange);
1806 }
Douglas Gregordd483172010-02-22 17:42:47 +00001807 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00001808 D2->setLexicalDeclContext(LexicalDC);
1809 Importer.Imported(D, D2);
1810 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00001811
1812 // Import the integer type.
1813 QualType ToIntegerType = Importer.Import(D->getIntegerType());
1814 if (ToIntegerType.isNull())
1815 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00001816 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00001817
1818 // Import the definition
1819 if (D->isDefinition()) {
1820 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
1821 if (T.isNull())
1822 return 0;
1823
1824 QualType ToPromotionType = Importer.Import(D->getPromotionType());
1825 if (ToPromotionType.isNull())
1826 return 0;
1827
Douglas Gregor3996e242010-02-15 22:01:00 +00001828 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00001829 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00001830
1831 // FIXME: we might need to merge the number of positive or negative bits
1832 // if the enumerator lists don't match.
1833 D2->completeDefinition(T, ToPromotionType,
1834 D->getNumPositiveBits(),
1835 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00001836 }
1837
Douglas Gregor3996e242010-02-15 22:01:00 +00001838 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00001839}
1840
Douglas Gregor5c73e912010-02-11 00:48:18 +00001841Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
1842 // If this record has a definition in the translation unit we're coming from,
1843 // but this particular declaration is not that definition, import the
1844 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001845 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001846 if (Definition && Definition != D) {
1847 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001848 if (!ImportedDef)
1849 return 0;
1850
1851 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001852 }
1853
1854 // Import the major distinguishing characteristics of this record.
1855 DeclContext *DC, *LexicalDC;
1856 DeclarationName Name;
1857 SourceLocation Loc;
1858 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1859 return 0;
1860
1861 // Figure out what structure name we're looking for.
1862 unsigned IDNS = Decl::IDNS_Tag;
1863 DeclarationName SearchName = Name;
1864 if (!SearchName && D->getTypedefForAnonDecl()) {
1865 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1866 IDNS = Decl::IDNS_Ordinary;
1867 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1868 IDNS |= Decl::IDNS_Ordinary;
1869
1870 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00001871 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001872 if (!DC->isFunctionOrMethod() && SearchName) {
1873 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1874 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1875 Lookup.first != Lookup.second;
1876 ++Lookup.first) {
1877 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1878 continue;
1879
1880 Decl *Found = *Lookup.first;
1881 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1882 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1883 Found = Tag->getDecl();
1884 }
1885
1886 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00001887 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
1888 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
1889 // The record types structurally match, or the "from" translation
1890 // unit only had a forward declaration anyway; call it the same
1891 // function.
1892 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001893 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00001894 }
1895 } else {
1896 // We have a forward declaration of this type, so adopt that forward
1897 // declaration rather than building a new one.
1898 AdoptDecl = FoundRecord;
1899 continue;
1900 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00001901 }
1902
1903 ConflictingDecls.push_back(*Lookup.first);
1904 }
1905
1906 if (!ConflictingDecls.empty()) {
1907 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1908 ConflictingDecls.data(),
1909 ConflictingDecls.size());
1910 }
1911 }
1912
1913 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00001914 RecordDecl *D2 = AdoptDecl;
1915 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00001916 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001917 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00001918 D->getTagKind(),
1919 DC, Loc,
1920 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00001921 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00001922 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00001923 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00001924 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00001925 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00001926 DC, Loc,
1927 Name.getAsIdentifierInfo(),
1928 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00001929 }
John McCall3e11ebe2010-03-15 10:12:16 +00001930 // Import the qualifier, if any.
1931 if (D->getQualifier()) {
1932 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
1933 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
1934 D2->setQualifierInfo(NNS, NNSRange);
1935 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001936 D2->setLexicalDeclContext(LexicalDC);
1937 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001938 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001939
Douglas Gregor3996e242010-02-15 22:01:00 +00001940 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00001941
Douglas Gregor5c73e912010-02-11 00:48:18 +00001942 if (D->isDefinition()) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001943 D2->startDefinition();
John McCall1c70e992010-06-03 19:28:45 +00001944
1945 // Add base classes.
1946 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1947 CXXRecordDecl *D1CXX = cast<CXXRecordDecl>(D);
1948
1949 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1950 for (CXXRecordDecl::base_class_iterator
1951 Base1 = D1CXX->bases_begin(),
1952 FromBaseEnd = D1CXX->bases_end();
1953 Base1 != FromBaseEnd;
1954 ++Base1) {
1955 QualType T = Importer.Import(Base1->getType());
1956 if (T.isNull())
1957 return 0;
1958
1959 Bases.push_back(
1960 new (Importer.getToContext())
1961 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1962 Base1->isVirtual(),
1963 Base1->isBaseOfClass(),
1964 Base1->getAccessSpecifierAsWritten(),
Nick Lewycky19b9f952010-07-26 16:56:01 +00001965 Importer.Import(Base1->getTypeSourceInfo())));
John McCall1c70e992010-06-03 19:28:45 +00001966 }
1967 if (!Bases.empty())
1968 D2CXX->setBases(Bases.data(), Bases.size());
1969 }
1970
Douglas Gregor968d6332010-02-21 18:24:45 +00001971 ImportDeclContext(D);
Douglas Gregor3996e242010-02-15 22:01:00 +00001972 D2->completeDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00001973 }
1974
Douglas Gregor3996e242010-02-15 22:01:00 +00001975 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00001976}
1977
Douglas Gregor98c10182010-02-12 22:17:39 +00001978Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
1979 // Import the major distinguishing characteristics of this enumerator.
1980 DeclContext *DC, *LexicalDC;
1981 DeclarationName Name;
1982 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001983 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00001984 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00001985
1986 QualType T = Importer.Import(D->getType());
1987 if (T.isNull())
1988 return 0;
1989
Douglas Gregor98c10182010-02-12 22:17:39 +00001990 // Determine whether there are any other declarations with the same name and
1991 // in the same context.
1992 if (!LexicalDC->isFunctionOrMethod()) {
1993 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1994 unsigned IDNS = Decl::IDNS_Ordinary;
1995 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1996 Lookup.first != Lookup.second;
1997 ++Lookup.first) {
1998 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1999 continue;
2000
2001 ConflictingDecls.push_back(*Lookup.first);
2002 }
2003
2004 if (!ConflictingDecls.empty()) {
2005 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2006 ConflictingDecls.data(),
2007 ConflictingDecls.size());
2008 if (!Name)
2009 return 0;
2010 }
2011 }
2012
2013 Expr *Init = Importer.Import(D->getInitExpr());
2014 if (D->getInitExpr() && !Init)
2015 return 0;
2016
2017 EnumConstantDecl *ToEnumerator
2018 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2019 Name.getAsIdentifierInfo(), T,
2020 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002021 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002022 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002023 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002024 LexicalDC->addDecl(ToEnumerator);
2025 return ToEnumerator;
2026}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002027
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002028Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2029 // Import the major distinguishing characteristics of this function.
2030 DeclContext *DC, *LexicalDC;
2031 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002032 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002033 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002034 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002035
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002036 // Try to find a function in our own ("to") context with the same name, same
2037 // type, and in the same context as the function we're importing.
2038 if (!LexicalDC->isFunctionOrMethod()) {
2039 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2040 unsigned IDNS = Decl::IDNS_Ordinary;
2041 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2042 Lookup.first != Lookup.second;
2043 ++Lookup.first) {
2044 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2045 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002046
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002047 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2048 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2049 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002050 if (Importer.IsStructurallyEquivalent(D->getType(),
2051 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002052 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002053 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002054 }
2055
2056 // FIXME: Check for overloading more carefully, e.g., by boosting
2057 // Sema::IsOverload out to the AST library.
2058
2059 // Function overloading is okay in C++.
2060 if (Importer.getToContext().getLangOptions().CPlusPlus)
2061 continue;
2062
2063 // Complain about inconsistent function types.
2064 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002065 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002066 Importer.ToDiag(FoundFunction->getLocation(),
2067 diag::note_odr_value_here)
2068 << FoundFunction->getType();
2069 }
2070 }
2071
2072 ConflictingDecls.push_back(*Lookup.first);
2073 }
2074
2075 if (!ConflictingDecls.empty()) {
2076 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2077 ConflictingDecls.data(),
2078 ConflictingDecls.size());
2079 if (!Name)
2080 return 0;
2081 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002082 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002083
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002084 DeclarationNameInfo NameInfo(Name, Loc);
2085 // Import additional name location/type info.
2086 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2087
Douglas Gregorb4964f72010-02-15 23:54:17 +00002088 // Import the type.
2089 QualType T = Importer.Import(D->getType());
2090 if (T.isNull())
2091 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002092
2093 // Import the function parameters.
2094 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2095 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2096 P != PEnd; ++P) {
2097 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2098 if (!ToP)
2099 return 0;
2100
2101 Parameters.push_back(ToP);
2102 }
2103
2104 // Create the imported function.
2105 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002106 FunctionDecl *ToFunction = 0;
2107 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2108 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2109 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002110 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002111 FromConstructor->isExplicit(),
2112 D->isInlineSpecified(),
2113 D->isImplicit());
2114 } else if (isa<CXXDestructorDecl>(D)) {
2115 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2116 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002117 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002118 D->isInlineSpecified(),
2119 D->isImplicit());
2120 } else if (CXXConversionDecl *FromConversion
2121 = dyn_cast<CXXConversionDecl>(D)) {
2122 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2123 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002124 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002125 D->isInlineSpecified(),
2126 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002127 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2128 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2129 cast<CXXRecordDecl>(DC),
2130 NameInfo, T, TInfo,
2131 Method->isStatic(),
2132 Method->getStorageClassAsWritten(),
2133 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002134 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002135 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2136 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002137 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002138 D->isInlineSpecified(),
2139 D->hasWrittenPrototype());
2140 }
John McCall3e11ebe2010-03-15 10:12:16 +00002141
2142 // Import the qualifier, if any.
2143 if (D->getQualifier()) {
2144 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2145 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2146 ToFunction->setQualifierInfo(NNS, NNSRange);
2147 }
Douglas Gregordd483172010-02-22 17:42:47 +00002148 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002149 ToFunction->setLexicalDeclContext(LexicalDC);
2150 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002151
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002152 // Set the parameters.
2153 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002154 Parameters[I]->setOwningFunction(ToFunction);
2155 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002156 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002157 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002158
2159 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002160
2161 // Add this function to the lexical context.
2162 LexicalDC->addDecl(ToFunction);
2163
Douglas Gregor43f54792010-02-17 02:12:47 +00002164 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002165}
2166
Douglas Gregor00eace12010-02-21 18:29:16 +00002167Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2168 return VisitFunctionDecl(D);
2169}
2170
2171Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2172 return VisitCXXMethodDecl(D);
2173}
2174
2175Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2176 return VisitCXXMethodDecl(D);
2177}
2178
2179Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2180 return VisitCXXMethodDecl(D);
2181}
2182
Douglas Gregor5c73e912010-02-11 00:48:18 +00002183Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2184 // Import the major distinguishing characteristics of a variable.
2185 DeclContext *DC, *LexicalDC;
2186 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002187 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002188 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2189 return 0;
2190
2191 // Import the type.
2192 QualType T = Importer.Import(D->getType());
2193 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002194 return 0;
2195
2196 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2197 Expr *BitWidth = Importer.Import(D->getBitWidth());
2198 if (!BitWidth && D->getBitWidth())
2199 return 0;
2200
2201 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2202 Loc, Name.getAsIdentifierInfo(),
2203 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002204 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002205 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002206 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002207 LexicalDC->addDecl(ToField);
2208 return ToField;
2209}
2210
Francois Pichet783dd6e2010-11-21 06:08:52 +00002211Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2212 // Import the major distinguishing characteristics of a variable.
2213 DeclContext *DC, *LexicalDC;
2214 DeclarationName Name;
2215 SourceLocation Loc;
2216 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2217 return 0;
2218
2219 // Import the type.
2220 QualType T = Importer.Import(D->getType());
2221 if (T.isNull())
2222 return 0;
2223
2224 NamedDecl **NamedChain =
2225 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2226
2227 unsigned i = 0;
2228 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2229 PE = D->chain_end(); PI != PE; ++PI) {
2230 Decl* D = Importer.Import(*PI);
2231 if (!D)
2232 return 0;
2233 NamedChain[i++] = cast<NamedDecl>(D);
2234 }
2235
2236 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2237 Importer.getToContext(), DC,
2238 Loc, Name.getAsIdentifierInfo(), T,
2239 NamedChain, D->getChainingSize());
2240 ToIndirectField->setAccess(D->getAccess());
2241 ToIndirectField->setLexicalDeclContext(LexicalDC);
2242 Importer.Imported(D, ToIndirectField);
2243 LexicalDC->addDecl(ToIndirectField);
2244 return ToIndirectField;
2245}
2246
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002247Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2248 // Import the major distinguishing characteristics of an ivar.
2249 DeclContext *DC, *LexicalDC;
2250 DeclarationName Name;
2251 SourceLocation Loc;
2252 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2253 return 0;
2254
2255 // Determine whether we've already imported this ivar
2256 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2257 Lookup.first != Lookup.second;
2258 ++Lookup.first) {
2259 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2260 if (Importer.IsStructurallyEquivalent(D->getType(),
2261 FoundIvar->getType())) {
2262 Importer.Imported(D, FoundIvar);
2263 return FoundIvar;
2264 }
2265
2266 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2267 << Name << D->getType() << FoundIvar->getType();
2268 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2269 << FoundIvar->getType();
2270 return 0;
2271 }
2272 }
2273
2274 // Import the type.
2275 QualType T = Importer.Import(D->getType());
2276 if (T.isNull())
2277 return 0;
2278
2279 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2280 Expr *BitWidth = Importer.Import(D->getBitWidth());
2281 if (!BitWidth && D->getBitWidth())
2282 return 0;
2283
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002284 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2285 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002286 Loc, Name.getAsIdentifierInfo(),
2287 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002288 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002289 ToIvar->setLexicalDeclContext(LexicalDC);
2290 Importer.Imported(D, ToIvar);
2291 LexicalDC->addDecl(ToIvar);
2292 return ToIvar;
2293
2294}
2295
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002296Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2297 // Import the major distinguishing characteristics of a variable.
2298 DeclContext *DC, *LexicalDC;
2299 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002300 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002301 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002302 return 0;
2303
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002304 // Try to find a variable in our own ("to") context with the same name and
2305 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002306 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002307 VarDecl *MergeWithVar = 0;
2308 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2309 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002310 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002311 Lookup.first != Lookup.second;
2312 ++Lookup.first) {
2313 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2314 continue;
2315
2316 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2317 // We have found a variable that we may need to merge with. Check it.
2318 if (isExternalLinkage(FoundVar->getLinkage()) &&
2319 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002320 if (Importer.IsStructurallyEquivalent(D->getType(),
2321 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002322 MergeWithVar = FoundVar;
2323 break;
2324 }
2325
Douglas Gregor56521c52010-02-12 17:23:39 +00002326 const ArrayType *FoundArray
2327 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2328 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002329 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002330 if (FoundArray && TArray) {
2331 if (isa<IncompleteArrayType>(FoundArray) &&
2332 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002333 // Import the type.
2334 QualType T = Importer.Import(D->getType());
2335 if (T.isNull())
2336 return 0;
2337
Douglas Gregor56521c52010-02-12 17:23:39 +00002338 FoundVar->setType(T);
2339 MergeWithVar = FoundVar;
2340 break;
2341 } else if (isa<IncompleteArrayType>(TArray) &&
2342 isa<ConstantArrayType>(FoundArray)) {
2343 MergeWithVar = FoundVar;
2344 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002345 }
2346 }
2347
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002348 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002349 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002350 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2351 << FoundVar->getType();
2352 }
2353 }
2354
2355 ConflictingDecls.push_back(*Lookup.first);
2356 }
2357
2358 if (MergeWithVar) {
2359 // An equivalent variable with external linkage has been found. Link
2360 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002361 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002362
2363 if (VarDecl *DDef = D->getDefinition()) {
2364 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2365 Importer.ToDiag(ExistingDef->getLocation(),
2366 diag::err_odr_variable_multiple_def)
2367 << Name;
2368 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2369 } else {
2370 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002371 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002372 }
2373 }
2374
2375 return MergeWithVar;
2376 }
2377
2378 if (!ConflictingDecls.empty()) {
2379 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2380 ConflictingDecls.data(),
2381 ConflictingDecls.size());
2382 if (!Name)
2383 return 0;
2384 }
2385 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002386
Douglas Gregorb4964f72010-02-15 23:54:17 +00002387 // Import the type.
2388 QualType T = Importer.Import(D->getType());
2389 if (T.isNull())
2390 return 0;
2391
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002392 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002393 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002394 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2395 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002396 D->getStorageClass(),
2397 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002398 // Import the qualifier, if any.
2399 if (D->getQualifier()) {
2400 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2401 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2402 ToVar->setQualifierInfo(NNS, NNSRange);
2403 }
Douglas Gregordd483172010-02-22 17:42:47 +00002404 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002405 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002406 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002407 LexicalDC->addDecl(ToVar);
2408
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002409 // Merge the initializer.
2410 // FIXME: Can we really import any initializer? Alternatively, we could force
2411 // ourselves to import every declaration of a variable and then only use
2412 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002413 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002414
2415 // FIXME: Other bits to merge?
2416
2417 return ToVar;
2418}
2419
Douglas Gregor8b228d72010-02-17 21:22:52 +00002420Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2421 // Parameters are created in the translation unit's context, then moved
2422 // into the function declaration's context afterward.
2423 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2424
2425 // Import the name of this declaration.
2426 DeclarationName Name = Importer.Import(D->getDeclName());
2427 if (D->getDeclName() && !Name)
2428 return 0;
2429
2430 // Import the location of this declaration.
2431 SourceLocation Loc = Importer.Import(D->getLocation());
2432
2433 // Import the parameter's type.
2434 QualType T = Importer.Import(D->getType());
2435 if (T.isNull())
2436 return 0;
2437
2438 // Create the imported parameter.
2439 ImplicitParamDecl *ToParm
2440 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2441 Loc, Name.getAsIdentifierInfo(),
2442 T);
2443 return Importer.Imported(D, ToParm);
2444}
2445
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002446Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2447 // Parameters are created in the translation unit's context, then moved
2448 // into the function declaration's context afterward.
2449 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2450
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002451 // Import the name of this declaration.
2452 DeclarationName Name = Importer.Import(D->getDeclName());
2453 if (D->getDeclName() && !Name)
2454 return 0;
2455
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002456 // Import the location of this declaration.
2457 SourceLocation Loc = Importer.Import(D->getLocation());
2458
2459 // Import the parameter's type.
2460 QualType T = Importer.Import(D->getType());
2461 if (T.isNull())
2462 return 0;
2463
2464 // Create the imported parameter.
2465 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2466 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2467 Loc, Name.getAsIdentifierInfo(),
2468 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002469 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002470 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002471 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002472 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002473}
2474
Douglas Gregor43f54792010-02-17 02:12:47 +00002475Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2476 // Import the major distinguishing characteristics of a method.
2477 DeclContext *DC, *LexicalDC;
2478 DeclarationName Name;
2479 SourceLocation Loc;
2480 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2481 return 0;
2482
2483 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2484 Lookup.first != Lookup.second;
2485 ++Lookup.first) {
2486 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2487 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2488 continue;
2489
2490 // Check return types.
2491 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2492 FoundMethod->getResultType())) {
2493 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2494 << D->isInstanceMethod() << Name
2495 << D->getResultType() << FoundMethod->getResultType();
2496 Importer.ToDiag(FoundMethod->getLocation(),
2497 diag::note_odr_objc_method_here)
2498 << D->isInstanceMethod() << Name;
2499 return 0;
2500 }
2501
2502 // Check the number of parameters.
2503 if (D->param_size() != FoundMethod->param_size()) {
2504 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2505 << D->isInstanceMethod() << Name
2506 << D->param_size() << FoundMethod->param_size();
2507 Importer.ToDiag(FoundMethod->getLocation(),
2508 diag::note_odr_objc_method_here)
2509 << D->isInstanceMethod() << Name;
2510 return 0;
2511 }
2512
2513 // Check parameter types.
2514 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2515 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2516 P != PEnd; ++P, ++FoundP) {
2517 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2518 (*FoundP)->getType())) {
2519 Importer.FromDiag((*P)->getLocation(),
2520 diag::err_odr_objc_method_param_type_inconsistent)
2521 << D->isInstanceMethod() << Name
2522 << (*P)->getType() << (*FoundP)->getType();
2523 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2524 << (*FoundP)->getType();
2525 return 0;
2526 }
2527 }
2528
2529 // Check variadic/non-variadic.
2530 // Check the number of parameters.
2531 if (D->isVariadic() != FoundMethod->isVariadic()) {
2532 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2533 << D->isInstanceMethod() << Name;
2534 Importer.ToDiag(FoundMethod->getLocation(),
2535 diag::note_odr_objc_method_here)
2536 << D->isInstanceMethod() << Name;
2537 return 0;
2538 }
2539
2540 // FIXME: Any other bits we need to merge?
2541 return Importer.Imported(D, FoundMethod);
2542 }
2543 }
2544
2545 // Import the result type.
2546 QualType ResultTy = Importer.Import(D->getResultType());
2547 if (ResultTy.isNull())
2548 return 0;
2549
Douglas Gregor12852d92010-03-08 14:59:44 +00002550 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2551
Douglas Gregor43f54792010-02-17 02:12:47 +00002552 ObjCMethodDecl *ToMethod
2553 = ObjCMethodDecl::Create(Importer.getToContext(),
2554 Loc,
2555 Importer.Import(D->getLocEnd()),
2556 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002557 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002558 D->isInstanceMethod(),
2559 D->isVariadic(),
2560 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002561 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002562 D->getImplementationControl());
2563
2564 // FIXME: When we decide to merge method definitions, we'll need to
2565 // deal with implicit parameters.
2566
2567 // Import the parameters
2568 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2569 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2570 FromPEnd = D->param_end();
2571 FromP != FromPEnd;
2572 ++FromP) {
2573 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2574 if (!ToP)
2575 return 0;
2576
2577 ToParams.push_back(ToP);
2578 }
2579
2580 // Set the parameters.
2581 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2582 ToParams[I]->setOwningFunction(ToMethod);
2583 ToMethod->addDecl(ToParams[I]);
2584 }
2585 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002586 ToParams.data(), ToParams.size(),
2587 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002588
2589 ToMethod->setLexicalDeclContext(LexicalDC);
2590 Importer.Imported(D, ToMethod);
2591 LexicalDC->addDecl(ToMethod);
2592 return ToMethod;
2593}
2594
Douglas Gregor84c51c32010-02-18 01:47:50 +00002595Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2596 // Import the major distinguishing characteristics of a category.
2597 DeclContext *DC, *LexicalDC;
2598 DeclarationName Name;
2599 SourceLocation Loc;
2600 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2601 return 0;
2602
2603 ObjCInterfaceDecl *ToInterface
2604 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2605 if (!ToInterface)
2606 return 0;
2607
2608 // Determine if we've already encountered this category.
2609 ObjCCategoryDecl *MergeWithCategory
2610 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2611 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2612 if (!ToCategory) {
2613 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2614 Importer.Import(D->getAtLoc()),
2615 Loc,
2616 Importer.Import(D->getCategoryNameLoc()),
2617 Name.getAsIdentifierInfo());
2618 ToCategory->setLexicalDeclContext(LexicalDC);
2619 LexicalDC->addDecl(ToCategory);
2620 Importer.Imported(D, ToCategory);
2621
2622 // Link this category into its class's category list.
2623 ToCategory->setClassInterface(ToInterface);
2624 ToCategory->insertNextClassCategory();
2625
2626 // Import protocols
2627 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2628 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2629 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2630 = D->protocol_loc_begin();
2631 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2632 FromProtoEnd = D->protocol_end();
2633 FromProto != FromProtoEnd;
2634 ++FromProto, ++FromProtoLoc) {
2635 ObjCProtocolDecl *ToProto
2636 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2637 if (!ToProto)
2638 return 0;
2639 Protocols.push_back(ToProto);
2640 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2641 }
2642
2643 // FIXME: If we're merging, make sure that the protocol list is the same.
2644 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2645 ProtocolLocs.data(), Importer.getToContext());
2646
2647 } else {
2648 Importer.Imported(D, ToCategory);
2649 }
2650
2651 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002652 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002653
2654 // If we have an implementation, import it as well.
2655 if (D->getImplementation()) {
2656 ObjCCategoryImplDecl *Impl
2657 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2658 if (!Impl)
2659 return 0;
2660
2661 ToCategory->setImplementation(Impl);
2662 }
2663
2664 return ToCategory;
2665}
2666
Douglas Gregor98d156a2010-02-17 16:12:00 +00002667Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002668 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002669 DeclContext *DC, *LexicalDC;
2670 DeclarationName Name;
2671 SourceLocation Loc;
2672 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2673 return 0;
2674
2675 ObjCProtocolDecl *MergeWithProtocol = 0;
2676 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2677 Lookup.first != Lookup.second;
2678 ++Lookup.first) {
2679 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2680 continue;
2681
2682 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2683 break;
2684 }
2685
2686 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2687 if (!ToProto || ToProto->isForwardDecl()) {
2688 if (!ToProto) {
2689 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2690 Name.getAsIdentifierInfo());
2691 ToProto->setForwardDecl(D->isForwardDecl());
2692 ToProto->setLexicalDeclContext(LexicalDC);
2693 LexicalDC->addDecl(ToProto);
2694 }
2695 Importer.Imported(D, ToProto);
2696
2697 // Import protocols
2698 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2699 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2700 ObjCProtocolDecl::protocol_loc_iterator
2701 FromProtoLoc = D->protocol_loc_begin();
2702 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2703 FromProtoEnd = D->protocol_end();
2704 FromProto != FromProtoEnd;
2705 ++FromProto, ++FromProtoLoc) {
2706 ObjCProtocolDecl *ToProto
2707 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2708 if (!ToProto)
2709 return 0;
2710 Protocols.push_back(ToProto);
2711 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2712 }
2713
2714 // FIXME: If we're merging, make sure that the protocol list is the same.
2715 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2716 ProtocolLocs.data(), Importer.getToContext());
2717 } else {
2718 Importer.Imported(D, ToProto);
2719 }
2720
Douglas Gregor84c51c32010-02-18 01:47:50 +00002721 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002722 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002723
2724 return ToProto;
2725}
2726
Douglas Gregor45635322010-02-16 01:20:57 +00002727Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2728 // Import the major distinguishing characteristics of an @interface.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
2731 SourceLocation Loc;
2732 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2733 return 0;
2734
2735 ObjCInterfaceDecl *MergeWithIface = 0;
2736 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2737 Lookup.first != Lookup.second;
2738 ++Lookup.first) {
2739 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2740 continue;
2741
2742 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2743 break;
2744 }
2745
2746 ObjCInterfaceDecl *ToIface = MergeWithIface;
2747 if (!ToIface || ToIface->isForwardDecl()) {
2748 if (!ToIface) {
2749 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2750 DC, Loc,
2751 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002752 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002753 D->isForwardDecl(),
2754 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002755 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002756 ToIface->setLexicalDeclContext(LexicalDC);
2757 LexicalDC->addDecl(ToIface);
2758 }
2759 Importer.Imported(D, ToIface);
2760
Douglas Gregor45635322010-02-16 01:20:57 +00002761 if (D->getSuperClass()) {
2762 ObjCInterfaceDecl *Super
2763 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2764 if (!Super)
2765 return 0;
2766
2767 ToIface->setSuperClass(Super);
2768 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2769 }
2770
2771 // Import protocols
2772 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2773 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2774 ObjCInterfaceDecl::protocol_loc_iterator
2775 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002776
2777 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00002778 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2779 FromProtoEnd = D->protocol_end();
2780 FromProto != FromProtoEnd;
2781 ++FromProto, ++FromProtoLoc) {
2782 ObjCProtocolDecl *ToProto
2783 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2784 if (!ToProto)
2785 return 0;
2786 Protocols.push_back(ToProto);
2787 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2788 }
2789
2790 // FIXME: If we're merging, make sure that the protocol list is the same.
2791 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2792 ProtocolLocs.data(), Importer.getToContext());
2793
Douglas Gregor45635322010-02-16 01:20:57 +00002794 // Import @end range
2795 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2796 } else {
2797 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002798
2799 // Check for consistency of superclasses.
2800 DeclarationName FromSuperName, ToSuperName;
2801 if (D->getSuperClass())
2802 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2803 if (ToIface->getSuperClass())
2804 ToSuperName = ToIface->getSuperClass()->getDeclName();
2805 if (FromSuperName != ToSuperName) {
2806 Importer.ToDiag(ToIface->getLocation(),
2807 diag::err_odr_objc_superclass_inconsistent)
2808 << ToIface->getDeclName();
2809 if (ToIface->getSuperClass())
2810 Importer.ToDiag(ToIface->getSuperClassLoc(),
2811 diag::note_odr_objc_superclass)
2812 << ToIface->getSuperClass()->getDeclName();
2813 else
2814 Importer.ToDiag(ToIface->getLocation(),
2815 diag::note_odr_objc_missing_superclass);
2816 if (D->getSuperClass())
2817 Importer.FromDiag(D->getSuperClassLoc(),
2818 diag::note_odr_objc_superclass)
2819 << D->getSuperClass()->getDeclName();
2820 else
2821 Importer.FromDiag(D->getLocation(),
2822 diag::note_odr_objc_missing_superclass);
2823 return 0;
2824 }
Douglas Gregor45635322010-02-16 01:20:57 +00002825 }
2826
Douglas Gregor84c51c32010-02-18 01:47:50 +00002827 // Import categories. When the categories themselves are imported, they'll
2828 // hook themselves into this interface.
2829 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
2830 FromCat = FromCat->getNextClassCategory())
2831 Importer.Import(FromCat);
2832
Douglas Gregor45635322010-02-16 01:20:57 +00002833 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00002834 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00002835
2836 // If we have an @implementation, import it as well.
2837 if (D->getImplementation()) {
2838 ObjCImplementationDecl *Impl
2839 = cast<ObjCImplementationDecl>(Importer.Import(D->getImplementation()));
2840 if (!Impl)
2841 return 0;
2842
2843 ToIface->setImplementation(Impl);
2844 }
2845
Douglas Gregor98d156a2010-02-17 16:12:00 +00002846 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00002847}
2848
Douglas Gregora11c4582010-02-17 18:02:10 +00002849Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
2850 // Import the major distinguishing characteristics of an @property.
2851 DeclContext *DC, *LexicalDC;
2852 DeclarationName Name;
2853 SourceLocation Loc;
2854 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2855 return 0;
2856
2857 // Check whether we have already imported this property.
2858 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2859 Lookup.first != Lookup.second;
2860 ++Lookup.first) {
2861 if (ObjCPropertyDecl *FoundProp
2862 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
2863 // Check property types.
2864 if (!Importer.IsStructurallyEquivalent(D->getType(),
2865 FoundProp->getType())) {
2866 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
2867 << Name << D->getType() << FoundProp->getType();
2868 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
2869 << FoundProp->getType();
2870 return 0;
2871 }
2872
2873 // FIXME: Check property attributes, getters, setters, etc.?
2874
2875 // Consider these properties to be equivalent.
2876 Importer.Imported(D, FoundProp);
2877 return FoundProp;
2878 }
2879 }
2880
2881 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00002882 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
2883 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00002884 return 0;
2885
2886 // Create the new property.
2887 ObjCPropertyDecl *ToProperty
2888 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
2889 Name.getAsIdentifierInfo(),
2890 Importer.Import(D->getAtLoc()),
2891 T,
2892 D->getPropertyImplementation());
2893 Importer.Imported(D, ToProperty);
2894 ToProperty->setLexicalDeclContext(LexicalDC);
2895 LexicalDC->addDecl(ToProperty);
2896
2897 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00002898 ToProperty->setPropertyAttributesAsWritten(
2899 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00002900 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
2901 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
2902 ToProperty->setGetterMethodDecl(
2903 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
2904 ToProperty->setSetterMethodDecl(
2905 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
2906 ToProperty->setPropertyIvarDecl(
2907 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
2908 return ToProperty;
2909}
2910
Douglas Gregor8661a722010-02-18 02:12:22 +00002911Decl *
2912ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
2913 // Import the context of this declaration.
2914 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2915 if (!DC)
2916 return 0;
2917
2918 DeclContext *LexicalDC = DC;
2919 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2920 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2921 if (!LexicalDC)
2922 return 0;
2923 }
2924
2925 // Import the location of this declaration.
2926 SourceLocation Loc = Importer.Import(D->getLocation());
2927
2928 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2929 llvm::SmallVector<SourceLocation, 4> Locations;
2930 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
2931 = D->protocol_loc_begin();
2932 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
2933 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
2934 FromProto != FromProtoEnd;
2935 ++FromProto, ++FromProtoLoc) {
2936 ObjCProtocolDecl *ToProto
2937 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2938 if (!ToProto)
2939 continue;
2940
2941 Protocols.push_back(ToProto);
2942 Locations.push_back(Importer.Import(*FromProtoLoc));
2943 }
2944
2945 ObjCForwardProtocolDecl *ToForward
2946 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2947 Protocols.data(), Protocols.size(),
2948 Locations.data());
2949 ToForward->setLexicalDeclContext(LexicalDC);
2950 LexicalDC->addDecl(ToForward);
2951 Importer.Imported(D, ToForward);
2952 return ToForward;
2953}
2954
Douglas Gregor06537af2010-02-18 02:04:09 +00002955Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
2956 // Import the context of this declaration.
2957 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2958 if (!DC)
2959 return 0;
2960
2961 DeclContext *LexicalDC = DC;
2962 if (D->getDeclContext() != D->getLexicalDeclContext()) {
2963 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
2964 if (!LexicalDC)
2965 return 0;
2966 }
2967
2968 // Import the location of this declaration.
2969 SourceLocation Loc = Importer.Import(D->getLocation());
2970
2971 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
2972 llvm::SmallVector<SourceLocation, 4> Locations;
2973 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
2974 From != FromEnd; ++From) {
2975 ObjCInterfaceDecl *ToIface
2976 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
2977 if (!ToIface)
2978 continue;
2979
2980 Interfaces.push_back(ToIface);
2981 Locations.push_back(Importer.Import(From->getLocation()));
2982 }
2983
2984 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
2985 Loc,
2986 Interfaces.data(),
2987 Locations.data(),
2988 Interfaces.size());
2989 ToClass->setLexicalDeclContext(LexicalDC);
2990 LexicalDC->addDecl(ToClass);
2991 Importer.Imported(D, ToClass);
2992 return ToClass;
2993}
2994
Douglas Gregora082a492010-11-30 19:14:50 +00002995Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
2996 // For template arguments, we adopt the translation unit as our declaration
2997 // context. This context will be fixed when the actual template declaration
2998 // is created.
2999
3000 // FIXME: Import default argument.
3001 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3002 Importer.getToContext().getTranslationUnitDecl(),
3003 Importer.Import(D->getLocation()),
3004 D->getDepth(),
3005 D->getIndex(),
3006 Importer.Import(D->getIdentifier()),
3007 D->wasDeclaredWithTypename(),
3008 D->isParameterPack());
3009}
3010
3011Decl *
3012ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3013 // Import the name of this declaration.
3014 DeclarationName Name = Importer.Import(D->getDeclName());
3015 if (D->getDeclName() && !Name)
3016 return 0;
3017
3018 // Import the location of this declaration.
3019 SourceLocation Loc = Importer.Import(D->getLocation());
3020
3021 // Import the type of this declaration.
3022 QualType T = Importer.Import(D->getType());
3023 if (T.isNull())
3024 return 0;
3025
3026 // Import type-source information.
3027 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3028 if (D->getTypeSourceInfo() && !TInfo)
3029 return 0;
3030
3031 // FIXME: Import default argument.
3032
3033 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3034 Importer.getToContext().getTranslationUnitDecl(),
3035 Loc, D->getDepth(), D->getPosition(),
3036 Name.getAsIdentifierInfo(),
3037 T, TInfo);
3038}
3039
3040Decl *
3041ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3042 // Import the name of this declaration.
3043 DeclarationName Name = Importer.Import(D->getDeclName());
3044 if (D->getDeclName() && !Name)
3045 return 0;
3046
3047 // Import the location of this declaration.
3048 SourceLocation Loc = Importer.Import(D->getLocation());
3049
3050 // Import template parameters.
3051 TemplateParameterList *TemplateParams
3052 = ImportTemplateParameterList(D->getTemplateParameters());
3053 if (!TemplateParams)
3054 return 0;
3055
3056 // FIXME: Import default argument.
3057
3058 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3059 Importer.getToContext().getTranslationUnitDecl(),
3060 Loc, D->getDepth(), D->getPosition(),
3061 Name.getAsIdentifierInfo(),
3062 TemplateParams);
3063}
3064
3065Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3066 // If this record has a definition in the translation unit we're coming from,
3067 // but this particular declaration is not that definition, import the
3068 // definition and map to that.
3069 CXXRecordDecl *Definition
3070 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3071 if (Definition && Definition != D->getTemplatedDecl()) {
3072 Decl *ImportedDef
3073 = Importer.Import(Definition->getDescribedClassTemplate());
3074 if (!ImportedDef)
3075 return 0;
3076
3077 return Importer.Imported(D, ImportedDef);
3078 }
3079
3080 // Import the major distinguishing characteristics of this class template.
3081 DeclContext *DC, *LexicalDC;
3082 DeclarationName Name;
3083 SourceLocation Loc;
3084 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3085 return 0;
3086
3087 // We may already have a template of the same name; try to find and match it.
3088 if (!DC->isFunctionOrMethod()) {
3089 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3090 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3091 Lookup.first != Lookup.second;
3092 ++Lookup.first) {
3093 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3094 continue;
3095
3096 Decl *Found = *Lookup.first;
3097 if (ClassTemplateDecl *FoundTemplate
3098 = dyn_cast<ClassTemplateDecl>(Found)) {
3099 if (IsStructuralMatch(D, FoundTemplate)) {
3100 // The class templates structurally match; call it the same template.
3101 // FIXME: We may be filling in a forward declaration here. Handle
3102 // this case!
3103 Importer.Imported(D->getTemplatedDecl(),
3104 FoundTemplate->getTemplatedDecl());
3105 return Importer.Imported(D, FoundTemplate);
3106 }
3107 }
3108
3109 ConflictingDecls.push_back(*Lookup.first);
3110 }
3111
3112 if (!ConflictingDecls.empty()) {
3113 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3114 ConflictingDecls.data(),
3115 ConflictingDecls.size());
3116 }
3117
3118 if (!Name)
3119 return 0;
3120 }
3121
3122 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3123
3124 // Create the declaration that is being templated.
3125 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3126 DTemplated->getTagKind(),
3127 DC,
3128 Importer.Import(DTemplated->getLocation()),
3129 Name.getAsIdentifierInfo(),
3130 Importer.Import(DTemplated->getTagKeywordLoc()));
3131 D2Templated->setAccess(DTemplated->getAccess());
3132
3133
3134 // Import the qualifier, if any.
3135 if (DTemplated->getQualifier()) {
3136 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3137 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3138 D2Templated->setQualifierInfo(NNS, NNSRange);
3139 }
3140 D2Templated->setLexicalDeclContext(LexicalDC);
3141
3142 // Create the class template declaration itself.
3143 TemplateParameterList *TemplateParams
3144 = ImportTemplateParameterList(D->getTemplateParameters());
3145 if (!TemplateParams)
3146 return 0;
3147
3148 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3149 Loc, Name, TemplateParams,
3150 D2Templated,
3151 /*PrevDecl=*/0);
3152 D2Templated->setDescribedClassTemplate(D2);
3153
3154 D2->setAccess(D->getAccess());
3155 D2->setLexicalDeclContext(LexicalDC);
3156 LexicalDC->addDecl(D2);
3157
3158 // Note the relationship between the class templates.
3159 Importer.Imported(D, D2);
3160 Importer.Imported(DTemplated, D2Templated);
3161
3162 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3163 // FIXME: Import definition!
3164 }
3165
3166 return D2;
3167}
3168
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003169//----------------------------------------------------------------------------
3170// Import Statements
3171//----------------------------------------------------------------------------
3172
3173Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3174 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3175 << S->getStmtClassName();
3176 return 0;
3177}
3178
3179//----------------------------------------------------------------------------
3180// Import Expressions
3181//----------------------------------------------------------------------------
3182Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3183 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3184 << E->getStmtClassName();
3185 return 0;
3186}
3187
Douglas Gregor52f820e2010-02-19 01:17:02 +00003188Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3189 NestedNameSpecifier *Qualifier = 0;
3190 if (E->getQualifier()) {
3191 Qualifier = Importer.Import(E->getQualifier());
3192 if (!E->getQualifier())
3193 return 0;
3194 }
3195
3196 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3197 if (!ToD)
3198 return 0;
3199
3200 QualType T = Importer.Import(E->getType());
3201 if (T.isNull())
3202 return 0;
3203
3204 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3205 Importer.Import(E->getQualifierRange()),
3206 ToD,
3207 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003208 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003209 /*FIXME:TemplateArgs=*/0);
3210}
3211
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003212Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3213 QualType T = Importer.Import(E->getType());
3214 if (T.isNull())
3215 return 0;
3216
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003217 return IntegerLiteral::Create(Importer.getToContext(),
3218 E->getValue(), T,
3219 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003220}
3221
Douglas Gregor623421d2010-02-18 02:21:22 +00003222Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3223 QualType T = Importer.Import(E->getType());
3224 if (T.isNull())
3225 return 0;
3226
3227 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3228 E->isWide(), T,
3229 Importer.Import(E->getLocation()));
3230}
3231
Douglas Gregorc74247e2010-02-19 01:07:06 +00003232Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3233 Expr *SubExpr = Importer.Import(E->getSubExpr());
3234 if (!SubExpr)
3235 return 0;
3236
3237 return new (Importer.getToContext())
3238 ParenExpr(Importer.Import(E->getLParen()),
3239 Importer.Import(E->getRParen()),
3240 SubExpr);
3241}
3242
3243Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3244 QualType T = Importer.Import(E->getType());
3245 if (T.isNull())
3246 return 0;
3247
3248 Expr *SubExpr = Importer.Import(E->getSubExpr());
3249 if (!SubExpr)
3250 return 0;
3251
3252 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003253 T, E->getValueKind(),
3254 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003255 Importer.Import(E->getOperatorLoc()));
3256}
3257
Douglas Gregord8552cd2010-02-19 01:24:23 +00003258Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3259 QualType ResultType = Importer.Import(E->getType());
3260
3261 if (E->isArgumentType()) {
3262 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3263 if (!TInfo)
3264 return 0;
3265
3266 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3267 TInfo, ResultType,
3268 Importer.Import(E->getOperatorLoc()),
3269 Importer.Import(E->getRParenLoc()));
3270 }
3271
3272 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3273 if (!SubExpr)
3274 return 0;
3275
3276 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3277 SubExpr, ResultType,
3278 Importer.Import(E->getOperatorLoc()),
3279 Importer.Import(E->getRParenLoc()));
3280}
3281
Douglas Gregorc74247e2010-02-19 01:07:06 +00003282Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3283 QualType T = Importer.Import(E->getType());
3284 if (T.isNull())
3285 return 0;
3286
3287 Expr *LHS = Importer.Import(E->getLHS());
3288 if (!LHS)
3289 return 0;
3290
3291 Expr *RHS = Importer.Import(E->getRHS());
3292 if (!RHS)
3293 return 0;
3294
3295 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003296 T, E->getValueKind(),
3297 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003298 Importer.Import(E->getOperatorLoc()));
3299}
3300
3301Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3302 QualType T = Importer.Import(E->getType());
3303 if (T.isNull())
3304 return 0;
3305
3306 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3307 if (CompLHSType.isNull())
3308 return 0;
3309
3310 QualType CompResultType = Importer.Import(E->getComputationResultType());
3311 if (CompResultType.isNull())
3312 return 0;
3313
3314 Expr *LHS = Importer.Import(E->getLHS());
3315 if (!LHS)
3316 return 0;
3317
3318 Expr *RHS = Importer.Import(E->getRHS());
3319 if (!RHS)
3320 return 0;
3321
3322 return new (Importer.getToContext())
3323 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003324 T, E->getValueKind(),
3325 E->getObjectKind(),
3326 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003327 Importer.Import(E->getOperatorLoc()));
3328}
3329
John McCallcf142162010-08-07 06:22:56 +00003330bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3331 if (E->path_empty()) return false;
3332
3333 // TODO: import cast paths
3334 return true;
3335}
3336
Douglas Gregor98c10182010-02-12 22:17:39 +00003337Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3338 QualType T = Importer.Import(E->getType());
3339 if (T.isNull())
3340 return 0;
3341
3342 Expr *SubExpr = Importer.Import(E->getSubExpr());
3343 if (!SubExpr)
3344 return 0;
John McCallcf142162010-08-07 06:22:56 +00003345
3346 CXXCastPath BasePath;
3347 if (ImportCastPath(E, BasePath))
3348 return 0;
3349
3350 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003351 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003352}
3353
Douglas Gregor5481d322010-02-19 01:32:14 +00003354Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3355 QualType T = Importer.Import(E->getType());
3356 if (T.isNull())
3357 return 0;
3358
3359 Expr *SubExpr = Importer.Import(E->getSubExpr());
3360 if (!SubExpr)
3361 return 0;
3362
3363 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3364 if (!TInfo && E->getTypeInfoAsWritten())
3365 return 0;
3366
John McCallcf142162010-08-07 06:22:56 +00003367 CXXCastPath BasePath;
3368 if (ImportCastPath(E, BasePath))
3369 return 0;
3370
John McCall7decc9e2010-11-18 06:31:45 +00003371 return CStyleCastExpr::Create(Importer.getToContext(), T,
3372 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003373 SubExpr, &BasePath, TInfo,
3374 Importer.Import(E->getLParenLoc()),
3375 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003376}
3377
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003378ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner5159f612010-11-23 08:35:12 +00003379 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003380 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner5159f612010-11-23 08:35:12 +00003381 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00003382 ImportedDecls[FromContext.getTranslationUnitDecl()]
3383 = ToContext.getTranslationUnitDecl();
3384}
3385
3386ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003387
3388QualType ASTImporter::Import(QualType FromT) {
3389 if (FromT.isNull())
3390 return QualType();
3391
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003392 // Check whether we've already imported this type.
3393 llvm::DenseMap<Type *, Type *>::iterator Pos
3394 = ImportedTypes.find(FromT.getTypePtr());
3395 if (Pos != ImportedTypes.end())
3396 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003397
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003398 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003399 ASTNodeImporter Importer(*this);
3400 QualType ToT = Importer.Visit(FromT.getTypePtr());
3401 if (ToT.isNull())
3402 return ToT;
3403
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003404 // Record the imported type.
3405 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3406
Douglas Gregor96e578d2010-02-05 17:54:41 +00003407 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3408}
3409
Douglas Gregor62d311f2010-02-09 19:21:46 +00003410TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003411 if (!FromTSI)
3412 return FromTSI;
3413
3414 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003415 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003416 QualType T = Import(FromTSI->getType());
3417 if (T.isNull())
3418 return 0;
3419
3420 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003421 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003422}
3423
3424Decl *ASTImporter::Import(Decl *FromD) {
3425 if (!FromD)
3426 return 0;
3427
3428 // Check whether we've already imported this declaration.
3429 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3430 if (Pos != ImportedDecls.end())
3431 return Pos->second;
3432
3433 // Import the type
3434 ASTNodeImporter Importer(*this);
3435 Decl *ToD = Importer.Visit(FromD);
3436 if (!ToD)
3437 return 0;
3438
3439 // Record the imported declaration.
3440 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003441
3442 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3443 // Keep track of anonymous tags that have an associated typedef.
3444 if (FromTag->getTypedefForAnonDecl())
3445 AnonTagsWithPendingTypedefs.push_back(FromTag);
3446 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3447 // When we've finished transforming a typedef, see whether it was the
3448 // typedef for an anonymous tag.
3449 for (llvm::SmallVector<TagDecl *, 4>::iterator
3450 FromTag = AnonTagsWithPendingTypedefs.begin(),
3451 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3452 FromTag != FromTagEnd; ++FromTag) {
3453 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3454 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3455 // We found the typedef for an anonymous tag; link them.
3456 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3457 AnonTagsWithPendingTypedefs.erase(FromTag);
3458 break;
3459 }
3460 }
3461 }
3462 }
3463
Douglas Gregor62d311f2010-02-09 19:21:46 +00003464 return ToD;
3465}
3466
3467DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3468 if (!FromDC)
3469 return FromDC;
3470
3471 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3472}
3473
3474Expr *ASTImporter::Import(Expr *FromE) {
3475 if (!FromE)
3476 return 0;
3477
3478 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3479}
3480
3481Stmt *ASTImporter::Import(Stmt *FromS) {
3482 if (!FromS)
3483 return 0;
3484
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003485 // Check whether we've already imported this declaration.
3486 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3487 if (Pos != ImportedStmts.end())
3488 return Pos->second;
3489
3490 // Import the type
3491 ASTNodeImporter Importer(*this);
3492 Stmt *ToS = Importer.Visit(FromS);
3493 if (!ToS)
3494 return 0;
3495
3496 // Record the imported declaration.
3497 ImportedStmts[FromS] = ToS;
3498 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003499}
3500
3501NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3502 if (!FromNNS)
3503 return 0;
3504
3505 // FIXME: Implement!
3506 return 0;
3507}
3508
3509SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3510 if (FromLoc.isInvalid())
3511 return SourceLocation();
3512
Douglas Gregor811663e2010-02-10 00:15:17 +00003513 SourceManager &FromSM = FromContext.getSourceManager();
3514
3515 // For now, map everything down to its spelling location, so that we
3516 // don't have to import macro instantiations.
3517 // FIXME: Import macro instantiations!
3518 FromLoc = FromSM.getSpellingLoc(FromLoc);
3519 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3520 SourceManager &ToSM = ToContext.getSourceManager();
3521 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3522 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003523}
3524
3525SourceRange ASTImporter::Import(SourceRange FromRange) {
3526 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3527}
3528
Douglas Gregor811663e2010-02-10 00:15:17 +00003529FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00003530 llvm::DenseMap<FileID, FileID>::iterator Pos
3531 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00003532 if (Pos != ImportedFileIDs.end())
3533 return Pos->second;
3534
3535 SourceManager &FromSM = FromContext.getSourceManager();
3536 SourceManager &ToSM = ToContext.getSourceManager();
3537 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3538 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3539
3540 // Include location of this file.
3541 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3542
3543 // Map the FileID for to the "to" source manager.
3544 FileID ToID;
3545 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3546 if (Cache->Entry) {
3547 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3548 // disk again
3549 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3550 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00003551 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00003552 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3553 FromSLoc.getFile().getFileCharacteristic());
3554 } else {
3555 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003556 const llvm::MemoryBuffer *
3557 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00003558 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00003559 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00003560 FromBuf->getBufferIdentifier());
3561 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
3562 }
3563
3564
Sebastian Redl99219f12010-09-30 01:03:06 +00003565 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00003566 return ToID;
3567}
3568
Douglas Gregor96e578d2010-02-05 17:54:41 +00003569DeclarationName ASTImporter::Import(DeclarationName FromName) {
3570 if (!FromName)
3571 return DeclarationName();
3572
3573 switch (FromName.getNameKind()) {
3574 case DeclarationName::Identifier:
3575 return Import(FromName.getAsIdentifierInfo());
3576
3577 case DeclarationName::ObjCZeroArgSelector:
3578 case DeclarationName::ObjCOneArgSelector:
3579 case DeclarationName::ObjCMultiArgSelector:
3580 return Import(FromName.getObjCSelector());
3581
3582 case DeclarationName::CXXConstructorName: {
3583 QualType T = Import(FromName.getCXXNameType());
3584 if (T.isNull())
3585 return DeclarationName();
3586
3587 return ToContext.DeclarationNames.getCXXConstructorName(
3588 ToContext.getCanonicalType(T));
3589 }
3590
3591 case DeclarationName::CXXDestructorName: {
3592 QualType T = Import(FromName.getCXXNameType());
3593 if (T.isNull())
3594 return DeclarationName();
3595
3596 return ToContext.DeclarationNames.getCXXDestructorName(
3597 ToContext.getCanonicalType(T));
3598 }
3599
3600 case DeclarationName::CXXConversionFunctionName: {
3601 QualType T = Import(FromName.getCXXNameType());
3602 if (T.isNull())
3603 return DeclarationName();
3604
3605 return ToContext.DeclarationNames.getCXXConversionFunctionName(
3606 ToContext.getCanonicalType(T));
3607 }
3608
3609 case DeclarationName::CXXOperatorName:
3610 return ToContext.DeclarationNames.getCXXOperatorName(
3611 FromName.getCXXOverloadedOperator());
3612
3613 case DeclarationName::CXXLiteralOperatorName:
3614 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
3615 Import(FromName.getCXXLiteralIdentifier()));
3616
3617 case DeclarationName::CXXUsingDirective:
3618 // FIXME: STATICS!
3619 return DeclarationName::getUsingDirectiveName();
3620 }
3621
3622 // Silence bogus GCC warning
3623 return DeclarationName();
3624}
3625
3626IdentifierInfo *ASTImporter::Import(IdentifierInfo *FromId) {
3627 if (!FromId)
3628 return 0;
3629
3630 return &ToContext.Idents.get(FromId->getName());
3631}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003632
Douglas Gregor43f54792010-02-17 02:12:47 +00003633Selector ASTImporter::Import(Selector FromSel) {
3634 if (FromSel.isNull())
3635 return Selector();
3636
3637 llvm::SmallVector<IdentifierInfo *, 4> Idents;
3638 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
3639 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
3640 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
3641 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
3642}
3643
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003644DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
3645 DeclContext *DC,
3646 unsigned IDNS,
3647 NamedDecl **Decls,
3648 unsigned NumDecls) {
3649 return Name;
3650}
3651
3652DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003653 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003654}
3655
3656DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003657 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003658}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003659
3660Decl *ASTImporter::Imported(Decl *From, Decl *To) {
3661 ImportedDecls[From] = To;
3662 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00003663}
Douglas Gregorb4964f72010-02-15 23:54:17 +00003664
3665bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
3666 llvm::DenseMap<Type *, Type *>::iterator Pos
3667 = ImportedTypes.find(From.getTypePtr());
3668 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
3669 return true;
3670
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003671 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00003672 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00003673}