blob: a1e0070422e3f8a6ba1edc6d63b9a634eb9de488 [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
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
John McCall424cec92011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
73 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000076 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
77 QualType VisitObjCObjectType(const ObjCObjectType *T);
78 QualType VisitObjCObjectPointerType(const 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 Gregor0a791672011-01-18 03:11:38 +000086 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregore2e50d332010-12-01 01:36:18 +000087 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +000088 TemplateParameterList *ImportTemplateParameterList(
89 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +000090 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
91 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
92 unsigned NumFromArgs,
93 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor5c73e912010-02-11 00:48:18 +000094 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000095 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000096 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000097 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000098 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000099 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000100 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000101 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000102 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000103 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000104 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
105 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
106 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
107 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000108 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000109 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000110 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000111 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000112 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000113 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000114 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000115 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000116 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000117 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000118 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000119 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000120 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000121 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000122 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000123 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000124 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
125 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
126 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
127 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000128 Decl *VisitClassTemplateSpecializationDecl(
129 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000130
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000131 // Importing statements
132 Stmt *VisitStmt(Stmt *S);
133
134 // Importing expressions
135 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000136 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000137 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000138 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000139 Expr *VisitParenExpr(ParenExpr *E);
140 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000141 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000142 Expr *VisitBinaryOperator(BinaryOperator *E);
143 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000144 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000145 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000146 };
147}
148
149//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000150// Structural Equivalence
151//----------------------------------------------------------------------------
152
153namespace {
154 struct StructuralEquivalenceContext {
155 /// \brief AST contexts for which we are checking structural equivalence.
156 ASTContext &C1, &C2;
157
Douglas Gregor3996e242010-02-15 22:01:00 +0000158 /// \brief The set of "tentative" equivalences between two canonical
159 /// declarations, mapping from a declaration in the first context to the
160 /// declaration in the second context that we believe to be equivalent.
161 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
162
163 /// \brief Queue of declarations in the first context whose equivalence
164 /// with a declaration in the second context still needs to be verified.
165 std::deque<Decl *> DeclsToCheck;
166
Douglas Gregorb4964f72010-02-15 23:54:17 +0000167 /// \brief Declaration (from, to) pairs that are known not to be equivalent
168 /// (which we have already complained about).
169 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
170
Douglas Gregor3996e242010-02-15 22:01:00 +0000171 /// \brief Whether we're being strict about the spelling of types when
172 /// unifying two types.
173 bool StrictTypeSpelling;
174
175 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000176 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000177 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000178 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000179 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000180
181 /// \brief Determine whether the two declarations are structurally
182 /// equivalent.
183 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
184
185 /// \brief Determine whether the two types are structurally equivalent.
186 bool IsStructurallyEquivalent(QualType T1, QualType T2);
187
188 private:
189 /// \brief Finish checking all of the structural equivalences.
190 ///
191 /// \returns true if an error occurred, false otherwise.
192 bool Finish();
193
194 public:
195 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000196 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000197 }
198
199 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000200 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000201 }
202 };
203}
204
205static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
206 QualType T1, QualType T2);
207static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
208 Decl *D1, Decl *D2);
209
210/// \brief Determine if two APInts have the same value, after zero-extending
211/// one of them (if needed!) to ensure that the bit-widths match.
212static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
213 if (I1.getBitWidth() == I2.getBitWidth())
214 return I1 == I2;
215
216 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000217 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor3996e242010-02-15 22:01:00 +0000218
Jay Foad6d4db0c2010-12-07 08:25:34 +0000219 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor3996e242010-02-15 22:01:00 +0000220}
221
222/// \brief Determine if two APSInts have the same value, zero- or sign-extending
223/// as needed.
224static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
225 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
226 return I1 == I2;
227
228 // Check for a bit-width mismatch.
229 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000230 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor3996e242010-02-15 22:01:00 +0000231 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000232 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000233
234 // We have a signedness mismatch. Turn the signed value into an unsigned
235 // value.
236 if (I1.isSigned()) {
237 if (I1.isNegative())
238 return false;
239
240 return llvm::APSInt(I1, true) == I2;
241 }
242
243 if (I2.isNegative())
244 return false;
245
246 return I1 == llvm::APSInt(I2, true);
247}
248
249/// \brief Determine structural equivalence of two expressions.
250static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
251 Expr *E1, Expr *E2) {
252 if (!E1 || !E2)
253 return E1 == E2;
254
255 // FIXME: Actually perform a structural comparison!
256 return true;
257}
258
259/// \brief Determine whether two identifiers are equivalent.
260static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
261 const IdentifierInfo *Name2) {
262 if (!Name1 || !Name2)
263 return Name1 == Name2;
264
265 return Name1->getName() == Name2->getName();
266}
267
268/// \brief Determine whether two nested-name-specifiers are equivalent.
269static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
270 NestedNameSpecifier *NNS1,
271 NestedNameSpecifier *NNS2) {
272 // FIXME: Implement!
273 return true;
274}
275
276/// \brief Determine whether two template arguments are equivalent.
277static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
278 const TemplateArgument &Arg1,
279 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000280 if (Arg1.getKind() != Arg2.getKind())
281 return false;
282
283 switch (Arg1.getKind()) {
284 case TemplateArgument::Null:
285 return true;
286
287 case TemplateArgument::Type:
288 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
289
290 case TemplateArgument::Integral:
291 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
292 Arg2.getIntegralType()))
293 return false;
294
295 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
296
297 case TemplateArgument::Declaration:
298 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
299
300 case TemplateArgument::Template:
301 return IsStructurallyEquivalent(Context,
302 Arg1.getAsTemplate(),
303 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000304
305 case TemplateArgument::TemplateExpansion:
306 return IsStructurallyEquivalent(Context,
307 Arg1.getAsTemplateOrTemplatePattern(),
308 Arg2.getAsTemplateOrTemplatePattern());
309
Douglas Gregore2e50d332010-12-01 01:36:18 +0000310 case TemplateArgument::Expression:
311 return IsStructurallyEquivalent(Context,
312 Arg1.getAsExpr(), Arg2.getAsExpr());
313
314 case TemplateArgument::Pack:
315 if (Arg1.pack_size() != Arg2.pack_size())
316 return false;
317
318 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
319 if (!IsStructurallyEquivalent(Context,
320 Arg1.pack_begin()[I],
321 Arg2.pack_begin()[I]))
322 return false;
323
324 return true;
325 }
326
327 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000328 return true;
329}
330
331/// \brief Determine structural equivalence for the common part of array
332/// types.
333static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
334 const ArrayType *Array1,
335 const ArrayType *Array2) {
336 if (!IsStructurallyEquivalent(Context,
337 Array1->getElementType(),
338 Array2->getElementType()))
339 return false;
340 if (Array1->getSizeModifier() != Array2->getSizeModifier())
341 return false;
342 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
343 return false;
344
345 return true;
346}
347
348/// \brief Determine structural equivalence of two types.
349static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
350 QualType T1, QualType T2) {
351 if (T1.isNull() || T2.isNull())
352 return T1.isNull() && T2.isNull();
353
354 if (!Context.StrictTypeSpelling) {
355 // We aren't being strict about token-to-token equivalence of types,
356 // so map down to the canonical type.
357 T1 = Context.C1.getCanonicalType(T1);
358 T2 = Context.C2.getCanonicalType(T2);
359 }
360
361 if (T1.getQualifiers() != T2.getQualifiers())
362 return false;
363
Douglas Gregorb4964f72010-02-15 23:54:17 +0000364 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000365
Douglas Gregorb4964f72010-02-15 23:54:17 +0000366 if (T1->getTypeClass() != T2->getTypeClass()) {
367 // Compare function types with prototypes vs. without prototypes as if
368 // both did not have prototypes.
369 if (T1->getTypeClass() == Type::FunctionProto &&
370 T2->getTypeClass() == Type::FunctionNoProto)
371 TC = Type::FunctionNoProto;
372 else if (T1->getTypeClass() == Type::FunctionNoProto &&
373 T2->getTypeClass() == Type::FunctionProto)
374 TC = Type::FunctionNoProto;
375 else
376 return false;
377 }
378
379 switch (TC) {
380 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000381 // FIXME: Deal with Char_S/Char_U.
382 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
383 return false;
384 break;
385
386 case Type::Complex:
387 if (!IsStructurallyEquivalent(Context,
388 cast<ComplexType>(T1)->getElementType(),
389 cast<ComplexType>(T2)->getElementType()))
390 return false;
391 break;
392
393 case Type::Pointer:
394 if (!IsStructurallyEquivalent(Context,
395 cast<PointerType>(T1)->getPointeeType(),
396 cast<PointerType>(T2)->getPointeeType()))
397 return false;
398 break;
399
400 case Type::BlockPointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<BlockPointerType>(T1)->getPointeeType(),
403 cast<BlockPointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::LValueReference:
408 case Type::RValueReference: {
409 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
410 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
411 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
412 return false;
413 if (Ref1->isInnerRef() != Ref2->isInnerRef())
414 return false;
415 if (!IsStructurallyEquivalent(Context,
416 Ref1->getPointeeTypeAsWritten(),
417 Ref2->getPointeeTypeAsWritten()))
418 return false;
419 break;
420 }
421
422 case Type::MemberPointer: {
423 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
424 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
425 if (!IsStructurallyEquivalent(Context,
426 MemPtr1->getPointeeType(),
427 MemPtr2->getPointeeType()))
428 return false;
429 if (!IsStructurallyEquivalent(Context,
430 QualType(MemPtr1->getClass(), 0),
431 QualType(MemPtr2->getClass(), 0)))
432 return false;
433 break;
434 }
435
436 case Type::ConstantArray: {
437 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
438 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
439 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
440 return false;
441
442 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
443 return false;
444 break;
445 }
446
447 case Type::IncompleteArray:
448 if (!IsArrayStructurallyEquivalent(Context,
449 cast<ArrayType>(T1),
450 cast<ArrayType>(T2)))
451 return false;
452 break;
453
454 case Type::VariableArray: {
455 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
456 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
457 if (!IsStructurallyEquivalent(Context,
458 Array1->getSizeExpr(), Array2->getSizeExpr()))
459 return false;
460
461 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
462 return false;
463
464 break;
465 }
466
467 case Type::DependentSizedArray: {
468 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
469 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
470 if (!IsStructurallyEquivalent(Context,
471 Array1->getSizeExpr(), Array2->getSizeExpr()))
472 return false;
473
474 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
475 return false;
476
477 break;
478 }
479
480 case Type::DependentSizedExtVector: {
481 const DependentSizedExtVectorType *Vec1
482 = cast<DependentSizedExtVectorType>(T1);
483 const DependentSizedExtVectorType *Vec2
484 = cast<DependentSizedExtVectorType>(T2);
485 if (!IsStructurallyEquivalent(Context,
486 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
487 return false;
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getElementType(),
490 Vec2->getElementType()))
491 return false;
492 break;
493 }
494
495 case Type::Vector:
496 case Type::ExtVector: {
497 const VectorType *Vec1 = cast<VectorType>(T1);
498 const VectorType *Vec2 = cast<VectorType>(T2);
499 if (!IsStructurallyEquivalent(Context,
500 Vec1->getElementType(),
501 Vec2->getElementType()))
502 return false;
503 if (Vec1->getNumElements() != Vec2->getNumElements())
504 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000505 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000506 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000507 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000508 }
509
510 case Type::FunctionProto: {
511 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
512 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
513 if (Proto1->getNumArgs() != Proto2->getNumArgs())
514 return false;
515 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
516 if (!IsStructurallyEquivalent(Context,
517 Proto1->getArgType(I),
518 Proto2->getArgType(I)))
519 return false;
520 }
521 if (Proto1->isVariadic() != Proto2->isVariadic())
522 return false;
523 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
524 return false;
525 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
526 return false;
527 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
528 return false;
529 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
530 if (!IsStructurallyEquivalent(Context,
531 Proto1->getExceptionType(I),
532 Proto2->getExceptionType(I)))
533 return false;
534 }
535 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
536 return false;
537
538 // Fall through to check the bits common with FunctionNoProtoType.
539 }
540
541 case Type::FunctionNoProto: {
542 const FunctionType *Function1 = cast<FunctionType>(T1);
543 const FunctionType *Function2 = cast<FunctionType>(T2);
544 if (!IsStructurallyEquivalent(Context,
545 Function1->getResultType(),
546 Function2->getResultType()))
547 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000548 if (Function1->getExtInfo() != Function2->getExtInfo())
549 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000550 break;
551 }
552
553 case Type::UnresolvedUsing:
554 if (!IsStructurallyEquivalent(Context,
555 cast<UnresolvedUsingType>(T1)->getDecl(),
556 cast<UnresolvedUsingType>(T2)->getDecl()))
557 return false;
558
559 break;
John McCall81904512011-01-06 01:58:22 +0000560
561 case Type::Attributed:
562 if (!IsStructurallyEquivalent(Context,
563 cast<AttributedType>(T1)->getModifiedType(),
564 cast<AttributedType>(T2)->getModifiedType()))
565 return false;
566 if (!IsStructurallyEquivalent(Context,
567 cast<AttributedType>(T1)->getEquivalentType(),
568 cast<AttributedType>(T2)->getEquivalentType()))
569 return false;
570 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000571
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000572 case Type::Paren:
573 if (!IsStructurallyEquivalent(Context,
574 cast<ParenType>(T1)->getInnerType(),
575 cast<ParenType>(T2)->getInnerType()))
576 return false;
577 break;
578
Douglas Gregor3996e242010-02-15 22:01:00 +0000579 case Type::Typedef:
580 if (!IsStructurallyEquivalent(Context,
581 cast<TypedefType>(T1)->getDecl(),
582 cast<TypedefType>(T2)->getDecl()))
583 return false;
584 break;
585
586 case Type::TypeOfExpr:
587 if (!IsStructurallyEquivalent(Context,
588 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
589 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
590 return false;
591 break;
592
593 case Type::TypeOf:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypeOfType>(T1)->getUnderlyingType(),
596 cast<TypeOfType>(T2)->getUnderlyingType()))
597 return false;
598 break;
599
600 case Type::Decltype:
601 if (!IsStructurallyEquivalent(Context,
602 cast<DecltypeType>(T1)->getUnderlyingExpr(),
603 cast<DecltypeType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::Record:
608 case Type::Enum:
609 if (!IsStructurallyEquivalent(Context,
610 cast<TagType>(T1)->getDecl(),
611 cast<TagType>(T2)->getDecl()))
612 return false;
613 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000614
Douglas Gregor3996e242010-02-15 22:01:00 +0000615 case Type::TemplateTypeParm: {
616 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
617 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
618 if (Parm1->getDepth() != Parm2->getDepth())
619 return false;
620 if (Parm1->getIndex() != Parm2->getIndex())
621 return false;
622 if (Parm1->isParameterPack() != Parm2->isParameterPack())
623 return false;
624
625 // Names of template type parameters are never significant.
626 break;
627 }
628
629 case Type::SubstTemplateTypeParm: {
630 const SubstTemplateTypeParmType *Subst1
631 = cast<SubstTemplateTypeParmType>(T1);
632 const SubstTemplateTypeParmType *Subst2
633 = cast<SubstTemplateTypeParmType>(T2);
634 if (!IsStructurallyEquivalent(Context,
635 QualType(Subst1->getReplacedParameter(), 0),
636 QualType(Subst2->getReplacedParameter(), 0)))
637 return false;
638 if (!IsStructurallyEquivalent(Context,
639 Subst1->getReplacementType(),
640 Subst2->getReplacementType()))
641 return false;
642 break;
643 }
644
Douglas Gregorfb322d82011-01-14 05:11:40 +0000645 case Type::SubstTemplateTypeParmPack: {
646 const SubstTemplateTypeParmPackType *Subst1
647 = cast<SubstTemplateTypeParmPackType>(T1);
648 const SubstTemplateTypeParmPackType *Subst2
649 = cast<SubstTemplateTypeParmPackType>(T2);
650 if (!IsStructurallyEquivalent(Context,
651 QualType(Subst1->getReplacedParameter(), 0),
652 QualType(Subst2->getReplacedParameter(), 0)))
653 return false;
654 if (!IsStructurallyEquivalent(Context,
655 Subst1->getArgumentPack(),
656 Subst2->getArgumentPack()))
657 return false;
658 break;
659 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000660 case Type::TemplateSpecialization: {
661 const TemplateSpecializationType *Spec1
662 = cast<TemplateSpecializationType>(T1);
663 const TemplateSpecializationType *Spec2
664 = cast<TemplateSpecializationType>(T2);
665 if (!IsStructurallyEquivalent(Context,
666 Spec1->getTemplateName(),
667 Spec2->getTemplateName()))
668 return false;
669 if (Spec1->getNumArgs() != Spec2->getNumArgs())
670 return false;
671 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
672 if (!IsStructurallyEquivalent(Context,
673 Spec1->getArg(I), Spec2->getArg(I)))
674 return false;
675 }
676 break;
677 }
678
Abramo Bagnara6150c882010-05-11 21:36:43 +0000679 case Type::Elaborated: {
680 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
681 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
682 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
683 if (Elab1->getKeyword() != Elab2->getKeyword())
684 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000685 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000686 Elab1->getQualifier(),
687 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000688 return false;
689 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000690 Elab1->getNamedType(),
691 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000692 return false;
693 break;
694 }
695
John McCalle78aac42010-03-10 03:28:59 +0000696 case Type::InjectedClassName: {
697 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
698 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
699 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000700 Inj1->getInjectedSpecializationType(),
701 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000702 return false;
703 break;
704 }
705
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000706 case Type::DependentName: {
707 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
708 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000709 if (!IsStructurallyEquivalent(Context,
710 Typename1->getQualifier(),
711 Typename2->getQualifier()))
712 return false;
713 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
714 Typename2->getIdentifier()))
715 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000716
717 break;
718 }
719
John McCallc392f372010-06-11 00:33:02 +0000720 case Type::DependentTemplateSpecialization: {
721 const DependentTemplateSpecializationType *Spec1 =
722 cast<DependentTemplateSpecializationType>(T1);
723 const DependentTemplateSpecializationType *Spec2 =
724 cast<DependentTemplateSpecializationType>(T2);
725 if (!IsStructurallyEquivalent(Context,
726 Spec1->getQualifier(),
727 Spec2->getQualifier()))
728 return false;
729 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
730 Spec2->getIdentifier()))
731 return false;
732 if (Spec1->getNumArgs() != Spec2->getNumArgs())
733 return false;
734 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
735 if (!IsStructurallyEquivalent(Context,
736 Spec1->getArg(I), Spec2->getArg(I)))
737 return false;
738 }
739 break;
740 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000741
742 case Type::PackExpansion:
743 if (!IsStructurallyEquivalent(Context,
744 cast<PackExpansionType>(T1)->getPattern(),
745 cast<PackExpansionType>(T2)->getPattern()))
746 return false;
747 break;
748
Douglas Gregor3996e242010-02-15 22:01:00 +0000749 case Type::ObjCInterface: {
750 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
751 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
752 if (!IsStructurallyEquivalent(Context,
753 Iface1->getDecl(), Iface2->getDecl()))
754 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000755 break;
756 }
757
758 case Type::ObjCObject: {
759 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
760 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
761 if (!IsStructurallyEquivalent(Context,
762 Obj1->getBaseType(),
763 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000764 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000765 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
766 return false;
767 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000768 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000769 Obj1->getProtocol(I),
770 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000771 return false;
772 }
773 break;
774 }
775
776 case Type::ObjCObjectPointer: {
777 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
778 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
779 if (!IsStructurallyEquivalent(Context,
780 Ptr1->getPointeeType(),
781 Ptr2->getPointeeType()))
782 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000783 break;
784 }
785
786 } // end switch
787
788 return true;
789}
790
791/// \brief Determine structural equivalence of two records.
792static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
793 RecordDecl *D1, RecordDecl *D2) {
794 if (D1->isUnion() != D2->isUnion()) {
795 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
796 << Context.C2.getTypeDeclType(D2);
797 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
798 << D1->getDeclName() << (unsigned)D1->getTagKind();
799 return false;
800 }
801
Douglas Gregore2e50d332010-12-01 01:36:18 +0000802 // If both declarations are class template specializations, we know
803 // the ODR applies, so check the template and template arguments.
804 ClassTemplateSpecializationDecl *Spec1
805 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
806 ClassTemplateSpecializationDecl *Spec2
807 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
808 if (Spec1 && Spec2) {
809 // Check that the specialized templates are the same.
810 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
811 Spec2->getSpecializedTemplate()))
812 return false;
813
814 // Check that the template arguments are the same.
815 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
816 return false;
817
818 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
819 if (!IsStructurallyEquivalent(Context,
820 Spec1->getTemplateArgs().get(I),
821 Spec2->getTemplateArgs().get(I)))
822 return false;
823 }
824 // If one is a class template specialization and the other is not, these
825 // structures are diferent.
826 else if (Spec1 || Spec2)
827 return false;
828
Douglas Gregorb4964f72010-02-15 23:54:17 +0000829 // Compare the definitions of these two records. If either or both are
830 // incomplete, we assume that they are equivalent.
831 D1 = D1->getDefinition();
832 D2 = D2->getDefinition();
833 if (!D1 || !D2)
834 return true;
835
Douglas Gregor3996e242010-02-15 22:01:00 +0000836 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
837 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
838 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
839 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000840 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000841 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000842 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000843 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000844 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000845 return false;
846 }
847
848 // Check the base classes.
849 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
850 BaseEnd1 = D1CXX->bases_end(),
851 Base2 = D2CXX->bases_begin();
852 Base1 != BaseEnd1;
853 ++Base1, ++Base2) {
854 if (!IsStructurallyEquivalent(Context,
855 Base1->getType(), Base2->getType())) {
856 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
857 << Context.C2.getTypeDeclType(D2);
858 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
859 << Base2->getType()
860 << Base2->getSourceRange();
861 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
862 << Base1->getType()
863 << Base1->getSourceRange();
864 return false;
865 }
866
867 // Check virtual vs. non-virtual inheritance mismatch.
868 if (Base1->isVirtual() != Base2->isVirtual()) {
869 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
870 << Context.C2.getTypeDeclType(D2);
871 Context.Diag2(Base2->getSourceRange().getBegin(),
872 diag::note_odr_virtual_base)
873 << Base2->isVirtual() << Base2->getSourceRange();
874 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
875 << Base1->isVirtual()
876 << Base1->getSourceRange();
877 return false;
878 }
879 }
880 } else if (D1CXX->getNumBases() > 0) {
881 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
882 << Context.C2.getTypeDeclType(D2);
883 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
884 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
885 << Base1->getType()
886 << Base1->getSourceRange();
887 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
888 return false;
889 }
890 }
891
892 // Check the fields for consistency.
893 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
894 Field2End = D2->field_end();
895 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
896 Field1End = D1->field_end();
897 Field1 != Field1End;
898 ++Field1, ++Field2) {
899 if (Field2 == Field2End) {
900 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
901 << Context.C2.getTypeDeclType(D2);
902 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
903 << Field1->getDeclName() << Field1->getType();
904 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
905 return false;
906 }
907
908 if (!IsStructurallyEquivalent(Context,
909 Field1->getType(), Field2->getType())) {
910 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
911 << Context.C2.getTypeDeclType(D2);
912 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
913 << Field2->getDeclName() << Field2->getType();
914 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
915 << Field1->getDeclName() << Field1->getType();
916 return false;
917 }
918
919 if (Field1->isBitField() != Field2->isBitField()) {
920 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
921 << Context.C2.getTypeDeclType(D2);
922 if (Field1->isBitField()) {
923 llvm::APSInt Bits;
924 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
925 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
926 << Field1->getDeclName() << Field1->getType()
927 << Bits.toString(10, false);
928 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
929 << Field2->getDeclName();
930 } else {
931 llvm::APSInt Bits;
932 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
933 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
934 << Field2->getDeclName() << Field2->getType()
935 << Bits.toString(10, false);
936 Context.Diag1(Field1->getLocation(),
937 diag::note_odr_not_bit_field)
938 << Field1->getDeclName();
939 }
940 return false;
941 }
942
943 if (Field1->isBitField()) {
944 // Make sure that the bit-fields are the same length.
945 llvm::APSInt Bits1, Bits2;
946 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
947 return false;
948 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
949 return false;
950
951 if (!IsSameValue(Bits1, Bits2)) {
952 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
953 << Context.C2.getTypeDeclType(D2);
954 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
955 << Field2->getDeclName() << Field2->getType()
956 << Bits2.toString(10, false);
957 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
958 << Field1->getDeclName() << Field1->getType()
959 << Bits1.toString(10, false);
960 return false;
961 }
962 }
963 }
964
965 if (Field2 != Field2End) {
966 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
967 << Context.C2.getTypeDeclType(D2);
968 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
969 << Field2->getDeclName() << Field2->getType();
970 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
971 return false;
972 }
973
974 return true;
975}
976
977/// \brief Determine structural equivalence of two enums.
978static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
979 EnumDecl *D1, EnumDecl *D2) {
980 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
981 EC2End = D2->enumerator_end();
982 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
983 EC1End = D1->enumerator_end();
984 EC1 != EC1End; ++EC1, ++EC2) {
985 if (EC2 == EC2End) {
986 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
987 << Context.C2.getTypeDeclType(D2);
988 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
989 << EC1->getDeclName()
990 << EC1->getInitVal().toString(10);
991 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
992 return false;
993 }
994
995 llvm::APSInt Val1 = EC1->getInitVal();
996 llvm::APSInt Val2 = EC2->getInitVal();
997 if (!IsSameValue(Val1, Val2) ||
998 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
999 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1000 << Context.C2.getTypeDeclType(D2);
1001 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1002 << EC2->getDeclName()
1003 << EC2->getInitVal().toString(10);
1004 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1005 << EC1->getDeclName()
1006 << EC1->getInitVal().toString(10);
1007 return false;
1008 }
1009 }
1010
1011 if (EC2 != EC2End) {
1012 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1013 << Context.C2.getTypeDeclType(D2);
1014 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1015 << EC2->getDeclName()
1016 << EC2->getInitVal().toString(10);
1017 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1018 return false;
1019 }
1020
1021 return true;
1022}
Douglas Gregora082a492010-11-30 19:14:50 +00001023
1024static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1025 TemplateParameterList *Params1,
1026 TemplateParameterList *Params2) {
1027 if (Params1->size() != Params2->size()) {
1028 Context.Diag2(Params2->getTemplateLoc(),
1029 diag::err_odr_different_num_template_parameters)
1030 << Params1->size() << Params2->size();
1031 Context.Diag1(Params1->getTemplateLoc(),
1032 diag::note_odr_template_parameter_list);
1033 return false;
1034 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001035
Douglas Gregora082a492010-11-30 19:14:50 +00001036 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1037 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1038 Context.Diag2(Params2->getParam(I)->getLocation(),
1039 diag::err_odr_different_template_parameter_kind);
1040 Context.Diag1(Params1->getParam(I)->getLocation(),
1041 diag::note_odr_template_parameter_here);
1042 return false;
1043 }
1044
1045 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1046 Params2->getParam(I))) {
1047
1048 return false;
1049 }
1050 }
1051
1052 return true;
1053}
1054
1055static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1056 TemplateTypeParmDecl *D1,
1057 TemplateTypeParmDecl *D2) {
1058 if (D1->isParameterPack() != D2->isParameterPack()) {
1059 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1060 << D2->isParameterPack();
1061 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1062 << D1->isParameterPack();
1063 return false;
1064 }
1065
1066 return true;
1067}
1068
1069static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1070 NonTypeTemplateParmDecl *D1,
1071 NonTypeTemplateParmDecl *D2) {
1072 // FIXME: Enable once we have variadic templates.
1073#if 0
1074 if (D1->isParameterPack() != D2->isParameterPack()) {
1075 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1076 << D2->isParameterPack();
1077 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1078 << D1->isParameterPack();
1079 return false;
1080 }
1081#endif
1082
1083 // Check types.
1084 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1085 Context.Diag2(D2->getLocation(),
1086 diag::err_odr_non_type_parameter_type_inconsistent)
1087 << D2->getType() << D1->getType();
1088 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1089 << D1->getType();
1090 return false;
1091 }
1092
1093 return true;
1094}
1095
1096static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1097 TemplateTemplateParmDecl *D1,
1098 TemplateTemplateParmDecl *D2) {
1099 // FIXME: Enable once we have variadic templates.
1100#if 0
1101 if (D1->isParameterPack() != D2->isParameterPack()) {
1102 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1103 << D2->isParameterPack();
1104 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1105 << D1->isParameterPack();
1106 return false;
1107 }
1108#endif
1109
1110 // Check template parameter lists.
1111 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1112 D2->getTemplateParameters());
1113}
1114
1115static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1116 ClassTemplateDecl *D1,
1117 ClassTemplateDecl *D2) {
1118 // Check template parameters.
1119 if (!IsStructurallyEquivalent(Context,
1120 D1->getTemplateParameters(),
1121 D2->getTemplateParameters()))
1122 return false;
1123
1124 // Check the templated declaration.
1125 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1126 D2->getTemplatedDecl());
1127}
1128
Douglas Gregor3996e242010-02-15 22:01:00 +00001129/// \brief Determine structural equivalence of two declarations.
1130static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1131 Decl *D1, Decl *D2) {
1132 // FIXME: Check for known structural equivalences via a callback of some sort.
1133
Douglas Gregorb4964f72010-02-15 23:54:17 +00001134 // Check whether we already know that these two declarations are not
1135 // structurally equivalent.
1136 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1137 D2->getCanonicalDecl())))
1138 return false;
1139
Douglas Gregor3996e242010-02-15 22:01:00 +00001140 // Determine whether we've already produced a tentative equivalence for D1.
1141 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1142 if (EquivToD1)
1143 return EquivToD1 == D2->getCanonicalDecl();
1144
1145 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1146 EquivToD1 = D2->getCanonicalDecl();
1147 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1148 return true;
1149}
1150
1151bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1152 Decl *D2) {
1153 if (!::IsStructurallyEquivalent(*this, D1, D2))
1154 return false;
1155
1156 return !Finish();
1157}
1158
1159bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1160 QualType T2) {
1161 if (!::IsStructurallyEquivalent(*this, T1, T2))
1162 return false;
1163
1164 return !Finish();
1165}
1166
1167bool StructuralEquivalenceContext::Finish() {
1168 while (!DeclsToCheck.empty()) {
1169 // Check the next declaration.
1170 Decl *D1 = DeclsToCheck.front();
1171 DeclsToCheck.pop_front();
1172
1173 Decl *D2 = TentativeEquivalences[D1];
1174 assert(D2 && "Unrecorded tentative equivalence?");
1175
Douglas Gregorb4964f72010-02-15 23:54:17 +00001176 bool Equivalent = true;
1177
Douglas Gregor3996e242010-02-15 22:01:00 +00001178 // FIXME: Switch on all declaration kinds. For now, we're just going to
1179 // check the obvious ones.
1180 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1181 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1182 // Check for equivalent structure names.
1183 IdentifierInfo *Name1 = Record1->getIdentifier();
1184 if (!Name1 && Record1->getTypedefForAnonDecl())
1185 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1186 IdentifierInfo *Name2 = Record2->getIdentifier();
1187 if (!Name2 && Record2->getTypedefForAnonDecl())
1188 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001189 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1190 !::IsStructurallyEquivalent(*this, Record1, Record2))
1191 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001192 } else {
1193 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001194 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001195 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001196 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001197 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1198 // Check for equivalent enum names.
1199 IdentifierInfo *Name1 = Enum1->getIdentifier();
1200 if (!Name1 && Enum1->getTypedefForAnonDecl())
1201 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1202 IdentifierInfo *Name2 = Enum2->getIdentifier();
1203 if (!Name2 && Enum2->getTypedefForAnonDecl())
1204 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001205 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1206 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1207 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001208 } else {
1209 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001210 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001211 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001212 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001213 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1214 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001215 Typedef2->getIdentifier()) ||
1216 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001217 Typedef1->getUnderlyingType(),
1218 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001219 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001220 } else {
1221 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001222 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001223 }
Douglas Gregora082a492010-11-30 19:14:50 +00001224 } else if (ClassTemplateDecl *ClassTemplate1
1225 = dyn_cast<ClassTemplateDecl>(D1)) {
1226 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1227 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1228 ClassTemplate2->getIdentifier()) ||
1229 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1230 Equivalent = false;
1231 } else {
1232 // Class template/non-class-template mismatch.
1233 Equivalent = false;
1234 }
1235 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1236 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1237 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1238 Equivalent = false;
1239 } else {
1240 // Kind mismatch.
1241 Equivalent = false;
1242 }
1243 } else if (NonTypeTemplateParmDecl *NTTP1
1244 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1245 if (NonTypeTemplateParmDecl *NTTP2
1246 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1247 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1248 Equivalent = false;
1249 } else {
1250 // Kind mismatch.
1251 Equivalent = false;
1252 }
1253 } else if (TemplateTemplateParmDecl *TTP1
1254 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1255 if (TemplateTemplateParmDecl *TTP2
1256 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1257 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1258 Equivalent = false;
1259 } else {
1260 // Kind mismatch.
1261 Equivalent = false;
1262 }
1263 }
1264
Douglas Gregorb4964f72010-02-15 23:54:17 +00001265 if (!Equivalent) {
1266 // Note that these two declarations are not equivalent (and we already
1267 // know about it).
1268 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1269 D2->getCanonicalDecl()));
1270 return true;
1271 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001272 // FIXME: Check other declaration kinds!
1273 }
1274
1275 return false;
1276}
1277
1278//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001279// Import Types
1280//----------------------------------------------------------------------------
1281
John McCall424cec92011-01-19 06:33:43 +00001282QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001283 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1284 << T->getTypeClassName();
1285 return QualType();
1286}
1287
John McCall424cec92011-01-19 06:33:43 +00001288QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001289 switch (T->getKind()) {
1290 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1291 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1292
1293 case BuiltinType::Char_U:
1294 // The context we're importing from has an unsigned 'char'. If we're
1295 // importing into a context with a signed 'char', translate to
1296 // 'unsigned char' instead.
1297 if (Importer.getToContext().getLangOptions().CharIsSigned)
1298 return Importer.getToContext().UnsignedCharTy;
1299
1300 return Importer.getToContext().CharTy;
1301
1302 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1303
1304 case BuiltinType::Char16:
1305 // FIXME: Make sure that the "to" context supports C++!
1306 return Importer.getToContext().Char16Ty;
1307
1308 case BuiltinType::Char32:
1309 // FIXME: Make sure that the "to" context supports C++!
1310 return Importer.getToContext().Char32Ty;
1311
1312 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1313 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1314 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1315 case BuiltinType::ULongLong:
1316 return Importer.getToContext().UnsignedLongLongTy;
1317 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1318
1319 case BuiltinType::Char_S:
1320 // The context we're importing from has an unsigned 'char'. If we're
1321 // importing into a context with a signed 'char', translate to
1322 // 'unsigned char' instead.
1323 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1324 return Importer.getToContext().SignedCharTy;
1325
1326 return Importer.getToContext().CharTy;
1327
1328 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001329 case BuiltinType::WChar_S:
1330 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001331 // FIXME: If not in C++, shall we translate to the C equivalent of
1332 // wchar_t?
1333 return Importer.getToContext().WCharTy;
1334
1335 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1336 case BuiltinType::Int : return Importer.getToContext().IntTy;
1337 case BuiltinType::Long : return Importer.getToContext().LongTy;
1338 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1339 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1340 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1341 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1342 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1343
1344 case BuiltinType::NullPtr:
1345 // FIXME: Make sure that the "to" context supports C++0x!
1346 return Importer.getToContext().NullPtrTy;
1347
1348 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1349 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1350 case BuiltinType::UndeducedAuto:
1351 // FIXME: Make sure that the "to" context supports C++0x!
1352 return Importer.getToContext().UndeducedAutoTy;
1353
1354 case BuiltinType::ObjCId:
1355 // FIXME: Make sure that the "to" context supports Objective-C!
1356 return Importer.getToContext().ObjCBuiltinIdTy;
1357
1358 case BuiltinType::ObjCClass:
1359 return Importer.getToContext().ObjCBuiltinClassTy;
1360
1361 case BuiltinType::ObjCSel:
1362 return Importer.getToContext().ObjCBuiltinSelTy;
1363 }
1364
1365 return QualType();
1366}
1367
John McCall424cec92011-01-19 06:33:43 +00001368QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001369 QualType ToElementType = Importer.Import(T->getElementType());
1370 if (ToElementType.isNull())
1371 return QualType();
1372
1373 return Importer.getToContext().getComplexType(ToElementType);
1374}
1375
John McCall424cec92011-01-19 06:33:43 +00001376QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001377 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1378 if (ToPointeeType.isNull())
1379 return QualType();
1380
1381 return Importer.getToContext().getPointerType(ToPointeeType);
1382}
1383
John McCall424cec92011-01-19 06:33:43 +00001384QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001385 // FIXME: Check for blocks support in "to" context.
1386 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1387 if (ToPointeeType.isNull())
1388 return QualType();
1389
1390 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1391}
1392
John McCall424cec92011-01-19 06:33:43 +00001393QualType
1394ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001395 // FIXME: Check for C++ support in "to" context.
1396 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1397 if (ToPointeeType.isNull())
1398 return QualType();
1399
1400 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1401}
1402
John McCall424cec92011-01-19 06:33:43 +00001403QualType
1404ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001405 // FIXME: Check for C++0x support in "to" context.
1406 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1407 if (ToPointeeType.isNull())
1408 return QualType();
1409
1410 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1411}
1412
John McCall424cec92011-01-19 06:33:43 +00001413QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001414 // FIXME: Check for C++ support in "to" context.
1415 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1416 if (ToPointeeType.isNull())
1417 return QualType();
1418
1419 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1420 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1421 ClassType.getTypePtr());
1422}
1423
John McCall424cec92011-01-19 06:33:43 +00001424QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001425 QualType ToElementType = Importer.Import(T->getElementType());
1426 if (ToElementType.isNull())
1427 return QualType();
1428
1429 return Importer.getToContext().getConstantArrayType(ToElementType,
1430 T->getSize(),
1431 T->getSizeModifier(),
1432 T->getIndexTypeCVRQualifiers());
1433}
1434
John McCall424cec92011-01-19 06:33:43 +00001435QualType
1436ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001437 QualType ToElementType = Importer.Import(T->getElementType());
1438 if (ToElementType.isNull())
1439 return QualType();
1440
1441 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1442 T->getSizeModifier(),
1443 T->getIndexTypeCVRQualifiers());
1444}
1445
John McCall424cec92011-01-19 06:33:43 +00001446QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001447 QualType ToElementType = Importer.Import(T->getElementType());
1448 if (ToElementType.isNull())
1449 return QualType();
1450
1451 Expr *Size = Importer.Import(T->getSizeExpr());
1452 if (!Size)
1453 return QualType();
1454
1455 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1456 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1457 T->getSizeModifier(),
1458 T->getIndexTypeCVRQualifiers(),
1459 Brackets);
1460}
1461
John McCall424cec92011-01-19 06:33:43 +00001462QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001463 QualType ToElementType = Importer.Import(T->getElementType());
1464 if (ToElementType.isNull())
1465 return QualType();
1466
1467 return Importer.getToContext().getVectorType(ToElementType,
1468 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001469 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001470}
1471
John McCall424cec92011-01-19 06:33:43 +00001472QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001473 QualType ToElementType = Importer.Import(T->getElementType());
1474 if (ToElementType.isNull())
1475 return QualType();
1476
1477 return Importer.getToContext().getExtVectorType(ToElementType,
1478 T->getNumElements());
1479}
1480
John McCall424cec92011-01-19 06:33:43 +00001481QualType
1482ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 // FIXME: What happens if we're importing a function without a prototype
1484 // into C++? Should we make it variadic?
1485 QualType ToResultType = Importer.Import(T->getResultType());
1486 if (ToResultType.isNull())
1487 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001488
Douglas Gregor96e578d2010-02-05 17:54:41 +00001489 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001490 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001491}
1492
John McCall424cec92011-01-19 06:33:43 +00001493QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494 QualType ToResultType = Importer.Import(T->getResultType());
1495 if (ToResultType.isNull())
1496 return QualType();
1497
1498 // Import argument types
1499 llvm::SmallVector<QualType, 4> ArgTypes;
1500 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1501 AEnd = T->arg_type_end();
1502 A != AEnd; ++A) {
1503 QualType ArgType = Importer.Import(*A);
1504 if (ArgType.isNull())
1505 return QualType();
1506 ArgTypes.push_back(ArgType);
1507 }
1508
1509 // Import exception types
1510 llvm::SmallVector<QualType, 4> ExceptionTypes;
1511 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1512 EEnd = T->exception_end();
1513 E != EEnd; ++E) {
1514 QualType ExceptionType = Importer.Import(*E);
1515 if (ExceptionType.isNull())
1516 return QualType();
1517 ExceptionTypes.push_back(ExceptionType);
1518 }
John McCalldb40c7f2010-12-14 08:05:40 +00001519
1520 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1521 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001522
1523 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001524 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001525}
1526
John McCall424cec92011-01-19 06:33:43 +00001527QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001528 TypedefDecl *ToDecl
1529 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1530 if (!ToDecl)
1531 return QualType();
1532
1533 return Importer.getToContext().getTypeDeclType(ToDecl);
1534}
1535
John McCall424cec92011-01-19 06:33:43 +00001536QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001537 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1538 if (!ToExpr)
1539 return QualType();
1540
1541 return Importer.getToContext().getTypeOfExprType(ToExpr);
1542}
1543
John McCall424cec92011-01-19 06:33:43 +00001544QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001545 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1546 if (ToUnderlyingType.isNull())
1547 return QualType();
1548
1549 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1550}
1551
John McCall424cec92011-01-19 06:33:43 +00001552QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1554 if (!ToExpr)
1555 return QualType();
1556
1557 return Importer.getToContext().getDecltypeType(ToExpr);
1558}
1559
John McCall424cec92011-01-19 06:33:43 +00001560QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001561 RecordDecl *ToDecl
1562 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1563 if (!ToDecl)
1564 return QualType();
1565
1566 return Importer.getToContext().getTagDeclType(ToDecl);
1567}
1568
John McCall424cec92011-01-19 06:33:43 +00001569QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001570 EnumDecl *ToDecl
1571 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1572 if (!ToDecl)
1573 return QualType();
1574
1575 return Importer.getToContext().getTagDeclType(ToDecl);
1576}
1577
Douglas Gregore2e50d332010-12-01 01:36:18 +00001578QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001579 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001580 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1581 if (ToTemplate.isNull())
1582 return QualType();
1583
1584 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1585 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1586 return QualType();
1587
1588 QualType ToCanonType;
1589 if (!QualType(T, 0).isCanonical()) {
1590 QualType FromCanonType
1591 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1592 ToCanonType =Importer.Import(FromCanonType);
1593 if (ToCanonType.isNull())
1594 return QualType();
1595 }
1596 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1597 ToTemplateArgs.data(),
1598 ToTemplateArgs.size(),
1599 ToCanonType);
1600}
1601
John McCall424cec92011-01-19 06:33:43 +00001602QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001603 NestedNameSpecifier *ToQualifier = 0;
1604 // Note: the qualifier in an ElaboratedType is optional.
1605 if (T->getQualifier()) {
1606 ToQualifier = Importer.Import(T->getQualifier());
1607 if (!ToQualifier)
1608 return QualType();
1609 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001610
1611 QualType ToNamedType = Importer.Import(T->getNamedType());
1612 if (ToNamedType.isNull())
1613 return QualType();
1614
Abramo Bagnara6150c882010-05-11 21:36:43 +00001615 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1616 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001617}
1618
John McCall424cec92011-01-19 06:33:43 +00001619QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001620 ObjCInterfaceDecl *Class
1621 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1622 if (!Class)
1623 return QualType();
1624
John McCall8b07ec22010-05-15 11:32:37 +00001625 return Importer.getToContext().getObjCInterfaceType(Class);
1626}
1627
John McCall424cec92011-01-19 06:33:43 +00001628QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001629 QualType ToBaseType = Importer.Import(T->getBaseType());
1630 if (ToBaseType.isNull())
1631 return QualType();
1632
Douglas Gregor96e578d2010-02-05 17:54:41 +00001633 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001634 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001635 PEnd = T->qual_end();
1636 P != PEnd; ++P) {
1637 ObjCProtocolDecl *Protocol
1638 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1639 if (!Protocol)
1640 return QualType();
1641 Protocols.push_back(Protocol);
1642 }
1643
John McCall8b07ec22010-05-15 11:32:37 +00001644 return Importer.getToContext().getObjCObjectType(ToBaseType,
1645 Protocols.data(),
1646 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001647}
1648
John McCall424cec92011-01-19 06:33:43 +00001649QualType
1650ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001651 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1652 if (ToPointeeType.isNull())
1653 return QualType();
1654
John McCall8b07ec22010-05-15 11:32:37 +00001655 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001656}
1657
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001658//----------------------------------------------------------------------------
1659// Import Declarations
1660//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001661bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1662 DeclContext *&LexicalDC,
1663 DeclarationName &Name,
1664 SourceLocation &Loc) {
1665 // Import the context of this declaration.
1666 DC = Importer.ImportContext(D->getDeclContext());
1667 if (!DC)
1668 return true;
1669
1670 LexicalDC = DC;
1671 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1672 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1673 if (!LexicalDC)
1674 return true;
1675 }
1676
1677 // Import the name of this declaration.
1678 Name = Importer.Import(D->getDeclName());
1679 if (D->getDeclName() && !Name)
1680 return true;
1681
1682 // Import the location of this declaration.
1683 Loc = Importer.Import(D->getLocation());
1684 return false;
1685}
1686
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001687void
1688ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1689 DeclarationNameInfo& To) {
1690 // NOTE: To.Name and To.Loc are already imported.
1691 // We only have to import To.LocInfo.
1692 switch (To.getName().getNameKind()) {
1693 case DeclarationName::Identifier:
1694 case DeclarationName::ObjCZeroArgSelector:
1695 case DeclarationName::ObjCOneArgSelector:
1696 case DeclarationName::ObjCMultiArgSelector:
1697 case DeclarationName::CXXUsingDirective:
1698 return;
1699
1700 case DeclarationName::CXXOperatorName: {
1701 SourceRange Range = From.getCXXOperatorNameRange();
1702 To.setCXXOperatorNameRange(Importer.Import(Range));
1703 return;
1704 }
1705 case DeclarationName::CXXLiteralOperatorName: {
1706 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1707 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1708 return;
1709 }
1710 case DeclarationName::CXXConstructorName:
1711 case DeclarationName::CXXDestructorName:
1712 case DeclarationName::CXXConversionFunctionName: {
1713 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1714 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1715 return;
1716 }
1717 assert(0 && "Unknown name kind.");
1718 }
1719}
1720
Douglas Gregor0a791672011-01-18 03:11:38 +00001721void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1722 if (Importer.isMinimalImport() && !ForceImport) {
1723 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1724 ToDC->setHasExternalLexicalStorage();
1725 ToDC->setHasExternalVisibleStorage();
1726 }
1727 return;
1728 }
1729
Douglas Gregor968d6332010-02-21 18:24:45 +00001730 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1731 FromEnd = FromDC->decls_end();
1732 From != FromEnd;
1733 ++From)
1734 Importer.Import(*From);
1735}
1736
Douglas Gregore2e50d332010-12-01 01:36:18 +00001737bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1738 if (To->getDefinition())
1739 return false;
1740
1741 To->startDefinition();
1742
1743 // Add base classes.
1744 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1745 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1746
1747 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1748 for (CXXRecordDecl::base_class_iterator
1749 Base1 = FromCXX->bases_begin(),
1750 FromBaseEnd = FromCXX->bases_end();
1751 Base1 != FromBaseEnd;
1752 ++Base1) {
1753 QualType T = Importer.Import(Base1->getType());
1754 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001755 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001756
1757 SourceLocation EllipsisLoc;
1758 if (Base1->isPackExpansion())
1759 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001760
1761 Bases.push_back(
1762 new (Importer.getToContext())
1763 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1764 Base1->isVirtual(),
1765 Base1->isBaseOfClass(),
1766 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001767 Importer.Import(Base1->getTypeSourceInfo()),
1768 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001769 }
1770 if (!Bases.empty())
1771 ToCXX->setBases(Bases.data(), Bases.size());
1772 }
1773
1774 ImportDeclContext(From);
1775 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001776 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001777}
1778
Douglas Gregora082a492010-11-30 19:14:50 +00001779TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1780 TemplateParameterList *Params) {
1781 llvm::SmallVector<NamedDecl *, 4> ToParams;
1782 ToParams.reserve(Params->size());
1783 for (TemplateParameterList::iterator P = Params->begin(),
1784 PEnd = Params->end();
1785 P != PEnd; ++P) {
1786 Decl *To = Importer.Import(*P);
1787 if (!To)
1788 return 0;
1789
1790 ToParams.push_back(cast<NamedDecl>(To));
1791 }
1792
1793 return TemplateParameterList::Create(Importer.getToContext(),
1794 Importer.Import(Params->getTemplateLoc()),
1795 Importer.Import(Params->getLAngleLoc()),
1796 ToParams.data(), ToParams.size(),
1797 Importer.Import(Params->getRAngleLoc()));
1798}
1799
Douglas Gregore2e50d332010-12-01 01:36:18 +00001800TemplateArgument
1801ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1802 switch (From.getKind()) {
1803 case TemplateArgument::Null:
1804 return TemplateArgument();
1805
1806 case TemplateArgument::Type: {
1807 QualType ToType = Importer.Import(From.getAsType());
1808 if (ToType.isNull())
1809 return TemplateArgument();
1810 return TemplateArgument(ToType);
1811 }
1812
1813 case TemplateArgument::Integral: {
1814 QualType ToType = Importer.Import(From.getIntegralType());
1815 if (ToType.isNull())
1816 return TemplateArgument();
1817 return TemplateArgument(*From.getAsIntegral(), ToType);
1818 }
1819
1820 case TemplateArgument::Declaration:
1821 if (Decl *To = Importer.Import(From.getAsDecl()))
1822 return TemplateArgument(To);
1823 return TemplateArgument();
1824
1825 case TemplateArgument::Template: {
1826 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1827 if (ToTemplate.isNull())
1828 return TemplateArgument();
1829
1830 return TemplateArgument(ToTemplate);
1831 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001832
1833 case TemplateArgument::TemplateExpansion: {
1834 TemplateName ToTemplate
1835 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1836 if (ToTemplate.isNull())
1837 return TemplateArgument();
1838
Douglas Gregore1d60df2011-01-14 23:41:42 +00001839 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001840 }
1841
Douglas Gregore2e50d332010-12-01 01:36:18 +00001842 case TemplateArgument::Expression:
1843 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1844 return TemplateArgument(ToExpr);
1845 return TemplateArgument();
1846
1847 case TemplateArgument::Pack: {
1848 llvm::SmallVector<TemplateArgument, 2> ToPack;
1849 ToPack.reserve(From.pack_size());
1850 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1851 return TemplateArgument();
1852
1853 TemplateArgument *ToArgs
1854 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1855 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1856 return TemplateArgument(ToArgs, ToPack.size());
1857 }
1858 }
1859
1860 llvm_unreachable("Invalid template argument kind");
1861 return TemplateArgument();
1862}
1863
1864bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1865 unsigned NumFromArgs,
1866 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1867 for (unsigned I = 0; I != NumFromArgs; ++I) {
1868 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1869 if (To.isNull() && !FromArgs[I].isNull())
1870 return true;
1871
1872 ToArgs.push_back(To);
1873 }
1874
1875 return false;
1876}
1877
Douglas Gregor5c73e912010-02-11 00:48:18 +00001878bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001879 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001880 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001881 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001882 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001883 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001884}
1885
Douglas Gregor98c10182010-02-12 22:17:39 +00001886bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001887 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001888 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001889 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001890 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001891}
1892
Douglas Gregora082a492010-11-30 19:14:50 +00001893bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1894 ClassTemplateDecl *To) {
1895 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1896 Importer.getToContext(),
1897 Importer.getNonEquivalentDecls());
1898 return Ctx.IsStructurallyEquivalent(From, To);
1899}
1900
Douglas Gregore4c83e42010-02-09 22:48:33 +00001901Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001902 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001903 << D->getDeclKindName();
1904 return 0;
1905}
1906
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001907Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1908 // Import the major distinguishing characteristics of this namespace.
1909 DeclContext *DC, *LexicalDC;
1910 DeclarationName Name;
1911 SourceLocation Loc;
1912 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1913 return 0;
1914
1915 NamespaceDecl *MergeWithNamespace = 0;
1916 if (!Name) {
1917 // This is an anonymous namespace. Adopt an existing anonymous
1918 // namespace if we can.
1919 // FIXME: Not testable.
1920 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1921 MergeWithNamespace = TU->getAnonymousNamespace();
1922 else
1923 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1924 } else {
1925 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1926 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1927 Lookup.first != Lookup.second;
1928 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001929 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001930 continue;
1931
1932 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1933 MergeWithNamespace = FoundNS;
1934 ConflictingDecls.clear();
1935 break;
1936 }
1937
1938 ConflictingDecls.push_back(*Lookup.first);
1939 }
1940
1941 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001942 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001943 ConflictingDecls.data(),
1944 ConflictingDecls.size());
1945 }
1946 }
1947
1948 // Create the "to" namespace, if needed.
1949 NamespaceDecl *ToNamespace = MergeWithNamespace;
1950 if (!ToNamespace) {
1951 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1952 Name.getAsIdentifierInfo());
1953 ToNamespace->setLexicalDeclContext(LexicalDC);
1954 LexicalDC->addDecl(ToNamespace);
1955
1956 // If this is an anonymous namespace, register it as the anonymous
1957 // namespace within its context.
1958 if (!Name) {
1959 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1960 TU->setAnonymousNamespace(ToNamespace);
1961 else
1962 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1963 }
1964 }
1965 Importer.Imported(D, ToNamespace);
1966
1967 ImportDeclContext(D);
1968
1969 return ToNamespace;
1970}
1971
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001972Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1973 // Import the major distinguishing characteristics of this typedef.
1974 DeclContext *DC, *LexicalDC;
1975 DeclarationName Name;
1976 SourceLocation Loc;
1977 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1978 return 0;
1979
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001980 // If this typedef is not in block scope, determine whether we've
1981 // seen a typedef with the same name (that we can merge with) or any
1982 // other entity by that name (which name lookup could conflict with).
1983 if (!DC->isFunctionOrMethod()) {
1984 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1985 unsigned IDNS = Decl::IDNS_Ordinary;
1986 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1987 Lookup.first != Lookup.second;
1988 ++Lookup.first) {
1989 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1990 continue;
1991 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001992 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1993 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001994 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001995 }
1996
1997 ConflictingDecls.push_back(*Lookup.first);
1998 }
1999
2000 if (!ConflictingDecls.empty()) {
2001 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2002 ConflictingDecls.data(),
2003 ConflictingDecls.size());
2004 if (!Name)
2005 return 0;
2006 }
2007 }
2008
Douglas Gregorb4964f72010-02-15 23:54:17 +00002009 // Import the underlying type of this typedef;
2010 QualType T = Importer.Import(D->getUnderlyingType());
2011 if (T.isNull())
2012 return 0;
2013
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002014 // Create the new typedef node.
2015 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2016 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2017 Loc, Name.getAsIdentifierInfo(),
2018 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002019 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002020 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002021 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002022 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002023
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002024 return ToTypedef;
2025}
2026
Douglas Gregor98c10182010-02-12 22:17:39 +00002027Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2028 // Import the major distinguishing characteristics of this enum.
2029 DeclContext *DC, *LexicalDC;
2030 DeclarationName Name;
2031 SourceLocation Loc;
2032 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2033 return 0;
2034
2035 // Figure out what enum name we're looking for.
2036 unsigned IDNS = Decl::IDNS_Tag;
2037 DeclarationName SearchName = Name;
2038 if (!SearchName && D->getTypedefForAnonDecl()) {
2039 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2040 IDNS = Decl::IDNS_Ordinary;
2041 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2042 IDNS |= Decl::IDNS_Ordinary;
2043
2044 // We may already have an enum of the same name; try to find and match it.
2045 if (!DC->isFunctionOrMethod() && SearchName) {
2046 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2047 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2048 Lookup.first != Lookup.second;
2049 ++Lookup.first) {
2050 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2051 continue;
2052
2053 Decl *Found = *Lookup.first;
2054 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2055 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2056 Found = Tag->getDecl();
2057 }
2058
2059 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002060 if (IsStructuralMatch(D, FoundEnum))
2061 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002062 }
2063
2064 ConflictingDecls.push_back(*Lookup.first);
2065 }
2066
2067 if (!ConflictingDecls.empty()) {
2068 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2069 ConflictingDecls.data(),
2070 ConflictingDecls.size());
2071 }
2072 }
2073
2074 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002075 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002076 Name.getAsIdentifierInfo(),
2077 Importer.Import(D->getTagKeywordLoc()), 0,
2078 D->isScoped(), D->isScopedUsingClassTag(),
2079 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002080 // Import the qualifier, if any.
2081 if (D->getQualifier()) {
2082 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2083 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2084 D2->setQualifierInfo(NNS, NNSRange);
2085 }
Douglas Gregordd483172010-02-22 17:42:47 +00002086 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002087 D2->setLexicalDeclContext(LexicalDC);
2088 Importer.Imported(D, D2);
2089 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002090
2091 // Import the integer type.
2092 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2093 if (ToIntegerType.isNull())
2094 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002095 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002096
2097 // Import the definition
2098 if (D->isDefinition()) {
2099 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2100 if (T.isNull())
2101 return 0;
2102
2103 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2104 if (ToPromotionType.isNull())
2105 return 0;
2106
Douglas Gregor3996e242010-02-15 22:01:00 +00002107 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002108 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002109
2110 // FIXME: we might need to merge the number of positive or negative bits
2111 // if the enumerator lists don't match.
2112 D2->completeDefinition(T, ToPromotionType,
2113 D->getNumPositiveBits(),
2114 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002115 }
2116
Douglas Gregor3996e242010-02-15 22:01:00 +00002117 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002118}
2119
Douglas Gregor5c73e912010-02-11 00:48:18 +00002120Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2121 // If this record has a definition in the translation unit we're coming from,
2122 // but this particular declaration is not that definition, import the
2123 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002124 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002125 if (Definition && Definition != D) {
2126 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002127 if (!ImportedDef)
2128 return 0;
2129
2130 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002131 }
2132
2133 // Import the major distinguishing characteristics of this record.
2134 DeclContext *DC, *LexicalDC;
2135 DeclarationName Name;
2136 SourceLocation Loc;
2137 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2138 return 0;
2139
2140 // Figure out what structure name we're looking for.
2141 unsigned IDNS = Decl::IDNS_Tag;
2142 DeclarationName SearchName = Name;
2143 if (!SearchName && D->getTypedefForAnonDecl()) {
2144 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2145 IDNS = Decl::IDNS_Ordinary;
2146 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2147 IDNS |= Decl::IDNS_Ordinary;
2148
2149 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002150 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002151 if (!DC->isFunctionOrMethod() && SearchName) {
2152 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2153 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2154 Lookup.first != Lookup.second;
2155 ++Lookup.first) {
2156 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2157 continue;
2158
2159 Decl *Found = *Lookup.first;
2160 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2161 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2162 Found = Tag->getDecl();
2163 }
2164
2165 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002166 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2167 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2168 // The record types structurally match, or the "from" translation
2169 // unit only had a forward declaration anyway; call it the same
2170 // function.
2171 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002172 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002173 }
2174 } else {
2175 // We have a forward declaration of this type, so adopt that forward
2176 // declaration rather than building a new one.
2177 AdoptDecl = FoundRecord;
2178 continue;
2179 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002180 }
2181
2182 ConflictingDecls.push_back(*Lookup.first);
2183 }
2184
2185 if (!ConflictingDecls.empty()) {
2186 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2187 ConflictingDecls.data(),
2188 ConflictingDecls.size());
2189 }
2190 }
2191
2192 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002193 RecordDecl *D2 = AdoptDecl;
2194 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002195 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002196 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002197 D->getTagKind(),
2198 DC, Loc,
2199 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002200 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002201 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002202 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002203 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002204 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002205 DC, Loc,
2206 Name.getAsIdentifierInfo(),
2207 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002208 }
John McCall3e11ebe2010-03-15 10:12:16 +00002209 // Import the qualifier, if any.
2210 if (D->getQualifier()) {
2211 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2212 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2213 D2->setQualifierInfo(NNS, NNSRange);
2214 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002215 D2->setLexicalDeclContext(LexicalDC);
2216 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002217 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002218
Douglas Gregor3996e242010-02-15 22:01:00 +00002219 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002220
Douglas Gregore2e50d332010-12-01 01:36:18 +00002221 if (D->isDefinition() && ImportDefinition(D, D2))
2222 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002223
Douglas Gregor3996e242010-02-15 22:01:00 +00002224 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002225}
2226
Douglas Gregor98c10182010-02-12 22:17:39 +00002227Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2228 // Import the major distinguishing characteristics of this enumerator.
2229 DeclContext *DC, *LexicalDC;
2230 DeclarationName Name;
2231 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002232 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002233 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002234
2235 QualType T = Importer.Import(D->getType());
2236 if (T.isNull())
2237 return 0;
2238
Douglas Gregor98c10182010-02-12 22:17:39 +00002239 // Determine whether there are any other declarations with the same name and
2240 // in the same context.
2241 if (!LexicalDC->isFunctionOrMethod()) {
2242 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2243 unsigned IDNS = Decl::IDNS_Ordinary;
2244 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2245 Lookup.first != Lookup.second;
2246 ++Lookup.first) {
2247 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2248 continue;
2249
2250 ConflictingDecls.push_back(*Lookup.first);
2251 }
2252
2253 if (!ConflictingDecls.empty()) {
2254 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2255 ConflictingDecls.data(),
2256 ConflictingDecls.size());
2257 if (!Name)
2258 return 0;
2259 }
2260 }
2261
2262 Expr *Init = Importer.Import(D->getInitExpr());
2263 if (D->getInitExpr() && !Init)
2264 return 0;
2265
2266 EnumConstantDecl *ToEnumerator
2267 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2268 Name.getAsIdentifierInfo(), T,
2269 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002270 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002271 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002272 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002273 LexicalDC->addDecl(ToEnumerator);
2274 return ToEnumerator;
2275}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002276
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002277Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2278 // Import the major distinguishing characteristics of this function.
2279 DeclContext *DC, *LexicalDC;
2280 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002281 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002282 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002283 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002284
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002285 // Try to find a function in our own ("to") context with the same name, same
2286 // type, and in the same context as the function we're importing.
2287 if (!LexicalDC->isFunctionOrMethod()) {
2288 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2289 unsigned IDNS = Decl::IDNS_Ordinary;
2290 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2291 Lookup.first != Lookup.second;
2292 ++Lookup.first) {
2293 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2294 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002295
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002296 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2297 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2298 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002299 if (Importer.IsStructurallyEquivalent(D->getType(),
2300 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002301 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002302 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002303 }
2304
2305 // FIXME: Check for overloading more carefully, e.g., by boosting
2306 // Sema::IsOverload out to the AST library.
2307
2308 // Function overloading is okay in C++.
2309 if (Importer.getToContext().getLangOptions().CPlusPlus)
2310 continue;
2311
2312 // Complain about inconsistent function types.
2313 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002314 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002315 Importer.ToDiag(FoundFunction->getLocation(),
2316 diag::note_odr_value_here)
2317 << FoundFunction->getType();
2318 }
2319 }
2320
2321 ConflictingDecls.push_back(*Lookup.first);
2322 }
2323
2324 if (!ConflictingDecls.empty()) {
2325 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2326 ConflictingDecls.data(),
2327 ConflictingDecls.size());
2328 if (!Name)
2329 return 0;
2330 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002331 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002332
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002333 DeclarationNameInfo NameInfo(Name, Loc);
2334 // Import additional name location/type info.
2335 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2336
Douglas Gregorb4964f72010-02-15 23:54:17 +00002337 // Import the type.
2338 QualType T = Importer.Import(D->getType());
2339 if (T.isNull())
2340 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002341
2342 // Import the function parameters.
2343 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2344 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2345 P != PEnd; ++P) {
2346 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2347 if (!ToP)
2348 return 0;
2349
2350 Parameters.push_back(ToP);
2351 }
2352
2353 // Create the imported function.
2354 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002355 FunctionDecl *ToFunction = 0;
2356 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2357 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2358 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002359 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002360 FromConstructor->isExplicit(),
2361 D->isInlineSpecified(),
2362 D->isImplicit());
2363 } else if (isa<CXXDestructorDecl>(D)) {
2364 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2365 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002366 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002367 D->isInlineSpecified(),
2368 D->isImplicit());
2369 } else if (CXXConversionDecl *FromConversion
2370 = dyn_cast<CXXConversionDecl>(D)) {
2371 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2372 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002373 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002374 D->isInlineSpecified(),
2375 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002376 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2377 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2378 cast<CXXRecordDecl>(DC),
2379 NameInfo, T, TInfo,
2380 Method->isStatic(),
2381 Method->getStorageClassAsWritten(),
2382 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002383 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002384 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2385 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002386 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002387 D->isInlineSpecified(),
2388 D->hasWrittenPrototype());
2389 }
John McCall3e11ebe2010-03-15 10:12:16 +00002390
2391 // Import the qualifier, if any.
2392 if (D->getQualifier()) {
2393 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2394 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2395 ToFunction->setQualifierInfo(NNS, NNSRange);
2396 }
Douglas Gregordd483172010-02-22 17:42:47 +00002397 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002398 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002399 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2400 ToFunction->setTrivial(D->isTrivial());
2401 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002402 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002403
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002404 // Set the parameters.
2405 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002406 Parameters[I]->setOwningFunction(ToFunction);
2407 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002408 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002409 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002410
2411 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002412
2413 // Add this function to the lexical context.
2414 LexicalDC->addDecl(ToFunction);
2415
Douglas Gregor43f54792010-02-17 02:12:47 +00002416 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002417}
2418
Douglas Gregor00eace12010-02-21 18:29:16 +00002419Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2420 return VisitFunctionDecl(D);
2421}
2422
2423Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2424 return VisitCXXMethodDecl(D);
2425}
2426
2427Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2428 return VisitCXXMethodDecl(D);
2429}
2430
2431Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2432 return VisitCXXMethodDecl(D);
2433}
2434
Douglas Gregor5c73e912010-02-11 00:48:18 +00002435Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2436 // Import the major distinguishing characteristics of a variable.
2437 DeclContext *DC, *LexicalDC;
2438 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002439 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002440 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2441 return 0;
2442
2443 // Import the type.
2444 QualType T = Importer.Import(D->getType());
2445 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002446 return 0;
2447
2448 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2449 Expr *BitWidth = Importer.Import(D->getBitWidth());
2450 if (!BitWidth && D->getBitWidth())
2451 return 0;
2452
2453 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2454 Loc, Name.getAsIdentifierInfo(),
2455 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002456 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002457 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002458 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002459 LexicalDC->addDecl(ToField);
2460 return ToField;
2461}
2462
Francois Pichet783dd6e2010-11-21 06:08:52 +00002463Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2464 // Import the major distinguishing characteristics of a variable.
2465 DeclContext *DC, *LexicalDC;
2466 DeclarationName Name;
2467 SourceLocation Loc;
2468 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2469 return 0;
2470
2471 // Import the type.
2472 QualType T = Importer.Import(D->getType());
2473 if (T.isNull())
2474 return 0;
2475
2476 NamedDecl **NamedChain =
2477 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2478
2479 unsigned i = 0;
2480 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2481 PE = D->chain_end(); PI != PE; ++PI) {
2482 Decl* D = Importer.Import(*PI);
2483 if (!D)
2484 return 0;
2485 NamedChain[i++] = cast<NamedDecl>(D);
2486 }
2487
2488 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2489 Importer.getToContext(), DC,
2490 Loc, Name.getAsIdentifierInfo(), T,
2491 NamedChain, D->getChainingSize());
2492 ToIndirectField->setAccess(D->getAccess());
2493 ToIndirectField->setLexicalDeclContext(LexicalDC);
2494 Importer.Imported(D, ToIndirectField);
2495 LexicalDC->addDecl(ToIndirectField);
2496 return ToIndirectField;
2497}
2498
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002499Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2500 // Import the major distinguishing characteristics of an ivar.
2501 DeclContext *DC, *LexicalDC;
2502 DeclarationName Name;
2503 SourceLocation Loc;
2504 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2505 return 0;
2506
2507 // Determine whether we've already imported this ivar
2508 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2509 Lookup.first != Lookup.second;
2510 ++Lookup.first) {
2511 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2512 if (Importer.IsStructurallyEquivalent(D->getType(),
2513 FoundIvar->getType())) {
2514 Importer.Imported(D, FoundIvar);
2515 return FoundIvar;
2516 }
2517
2518 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2519 << Name << D->getType() << FoundIvar->getType();
2520 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2521 << FoundIvar->getType();
2522 return 0;
2523 }
2524 }
2525
2526 // Import the type.
2527 QualType T = Importer.Import(D->getType());
2528 if (T.isNull())
2529 return 0;
2530
2531 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2532 Expr *BitWidth = Importer.Import(D->getBitWidth());
2533 if (!BitWidth && D->getBitWidth())
2534 return 0;
2535
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002536 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2537 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002538 Loc, Name.getAsIdentifierInfo(),
2539 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002540 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002541 ToIvar->setLexicalDeclContext(LexicalDC);
2542 Importer.Imported(D, ToIvar);
2543 LexicalDC->addDecl(ToIvar);
2544 return ToIvar;
2545
2546}
2547
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002548Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2549 // Import the major distinguishing characteristics of a variable.
2550 DeclContext *DC, *LexicalDC;
2551 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002552 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002553 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002554 return 0;
2555
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002556 // Try to find a variable in our own ("to") context with the same name and
2557 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002558 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002559 VarDecl *MergeWithVar = 0;
2560 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2561 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002562 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002563 Lookup.first != Lookup.second;
2564 ++Lookup.first) {
2565 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2566 continue;
2567
2568 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2569 // We have found a variable that we may need to merge with. Check it.
2570 if (isExternalLinkage(FoundVar->getLinkage()) &&
2571 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002572 if (Importer.IsStructurallyEquivalent(D->getType(),
2573 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002574 MergeWithVar = FoundVar;
2575 break;
2576 }
2577
Douglas Gregor56521c52010-02-12 17:23:39 +00002578 const ArrayType *FoundArray
2579 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2580 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002581 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002582 if (FoundArray && TArray) {
2583 if (isa<IncompleteArrayType>(FoundArray) &&
2584 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002585 // Import the type.
2586 QualType T = Importer.Import(D->getType());
2587 if (T.isNull())
2588 return 0;
2589
Douglas Gregor56521c52010-02-12 17:23:39 +00002590 FoundVar->setType(T);
2591 MergeWithVar = FoundVar;
2592 break;
2593 } else if (isa<IncompleteArrayType>(TArray) &&
2594 isa<ConstantArrayType>(FoundArray)) {
2595 MergeWithVar = FoundVar;
2596 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002597 }
2598 }
2599
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002600 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002601 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002602 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2603 << FoundVar->getType();
2604 }
2605 }
2606
2607 ConflictingDecls.push_back(*Lookup.first);
2608 }
2609
2610 if (MergeWithVar) {
2611 // An equivalent variable with external linkage has been found. Link
2612 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002613 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002614
2615 if (VarDecl *DDef = D->getDefinition()) {
2616 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2617 Importer.ToDiag(ExistingDef->getLocation(),
2618 diag::err_odr_variable_multiple_def)
2619 << Name;
2620 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2621 } else {
2622 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002623 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002624 }
2625 }
2626
2627 return MergeWithVar;
2628 }
2629
2630 if (!ConflictingDecls.empty()) {
2631 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2632 ConflictingDecls.data(),
2633 ConflictingDecls.size());
2634 if (!Name)
2635 return 0;
2636 }
2637 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002638
Douglas Gregorb4964f72010-02-15 23:54:17 +00002639 // Import the type.
2640 QualType T = Importer.Import(D->getType());
2641 if (T.isNull())
2642 return 0;
2643
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002644 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002645 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002646 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2647 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002648 D->getStorageClass(),
2649 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002650 // Import the qualifier, if any.
2651 if (D->getQualifier()) {
2652 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2653 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2654 ToVar->setQualifierInfo(NNS, NNSRange);
2655 }
Douglas Gregordd483172010-02-22 17:42:47 +00002656 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002657 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002658 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002659 LexicalDC->addDecl(ToVar);
2660
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002661 // Merge the initializer.
2662 // FIXME: Can we really import any initializer? Alternatively, we could force
2663 // ourselves to import every declaration of a variable and then only use
2664 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002665 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002666
2667 // FIXME: Other bits to merge?
2668
2669 return ToVar;
2670}
2671
Douglas Gregor8b228d72010-02-17 21:22:52 +00002672Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2673 // Parameters are created in the translation unit's context, then moved
2674 // into the function declaration's context afterward.
2675 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2676
2677 // Import the name of this declaration.
2678 DeclarationName Name = Importer.Import(D->getDeclName());
2679 if (D->getDeclName() && !Name)
2680 return 0;
2681
2682 // Import the location of this declaration.
2683 SourceLocation Loc = Importer.Import(D->getLocation());
2684
2685 // Import the parameter's type.
2686 QualType T = Importer.Import(D->getType());
2687 if (T.isNull())
2688 return 0;
2689
2690 // Create the imported parameter.
2691 ImplicitParamDecl *ToParm
2692 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2693 Loc, Name.getAsIdentifierInfo(),
2694 T);
2695 return Importer.Imported(D, ToParm);
2696}
2697
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002698Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2699 // Parameters are created in the translation unit's context, then moved
2700 // into the function declaration's context afterward.
2701 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2702
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002703 // Import the name of this declaration.
2704 DeclarationName Name = Importer.Import(D->getDeclName());
2705 if (D->getDeclName() && !Name)
2706 return 0;
2707
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002708 // Import the location of this declaration.
2709 SourceLocation Loc = Importer.Import(D->getLocation());
2710
2711 // Import the parameter's type.
2712 QualType T = Importer.Import(D->getType());
2713 if (T.isNull())
2714 return 0;
2715
2716 // Create the imported parameter.
2717 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2718 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2719 Loc, Name.getAsIdentifierInfo(),
2720 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002721 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002722 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002723 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002724 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002725}
2726
Douglas Gregor43f54792010-02-17 02:12:47 +00002727Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2728 // Import the major distinguishing characteristics of a method.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
2731 SourceLocation Loc;
2732 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2733 return 0;
2734
2735 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2736 Lookup.first != Lookup.second;
2737 ++Lookup.first) {
2738 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2739 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2740 continue;
2741
2742 // Check return types.
2743 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2744 FoundMethod->getResultType())) {
2745 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2746 << D->isInstanceMethod() << Name
2747 << D->getResultType() << FoundMethod->getResultType();
2748 Importer.ToDiag(FoundMethod->getLocation(),
2749 diag::note_odr_objc_method_here)
2750 << D->isInstanceMethod() << Name;
2751 return 0;
2752 }
2753
2754 // Check the number of parameters.
2755 if (D->param_size() != FoundMethod->param_size()) {
2756 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2757 << D->isInstanceMethod() << Name
2758 << D->param_size() << FoundMethod->param_size();
2759 Importer.ToDiag(FoundMethod->getLocation(),
2760 diag::note_odr_objc_method_here)
2761 << D->isInstanceMethod() << Name;
2762 return 0;
2763 }
2764
2765 // Check parameter types.
2766 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2767 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2768 P != PEnd; ++P, ++FoundP) {
2769 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2770 (*FoundP)->getType())) {
2771 Importer.FromDiag((*P)->getLocation(),
2772 diag::err_odr_objc_method_param_type_inconsistent)
2773 << D->isInstanceMethod() << Name
2774 << (*P)->getType() << (*FoundP)->getType();
2775 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2776 << (*FoundP)->getType();
2777 return 0;
2778 }
2779 }
2780
2781 // Check variadic/non-variadic.
2782 // Check the number of parameters.
2783 if (D->isVariadic() != FoundMethod->isVariadic()) {
2784 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2785 << D->isInstanceMethod() << Name;
2786 Importer.ToDiag(FoundMethod->getLocation(),
2787 diag::note_odr_objc_method_here)
2788 << D->isInstanceMethod() << Name;
2789 return 0;
2790 }
2791
2792 // FIXME: Any other bits we need to merge?
2793 return Importer.Imported(D, FoundMethod);
2794 }
2795 }
2796
2797 // Import the result type.
2798 QualType ResultTy = Importer.Import(D->getResultType());
2799 if (ResultTy.isNull())
2800 return 0;
2801
Douglas Gregor12852d92010-03-08 14:59:44 +00002802 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2803
Douglas Gregor43f54792010-02-17 02:12:47 +00002804 ObjCMethodDecl *ToMethod
2805 = ObjCMethodDecl::Create(Importer.getToContext(),
2806 Loc,
2807 Importer.Import(D->getLocEnd()),
2808 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002809 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002810 D->isInstanceMethod(),
2811 D->isVariadic(),
2812 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002813 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002814 D->getImplementationControl());
2815
2816 // FIXME: When we decide to merge method definitions, we'll need to
2817 // deal with implicit parameters.
2818
2819 // Import the parameters
2820 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2821 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2822 FromPEnd = D->param_end();
2823 FromP != FromPEnd;
2824 ++FromP) {
2825 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2826 if (!ToP)
2827 return 0;
2828
2829 ToParams.push_back(ToP);
2830 }
2831
2832 // Set the parameters.
2833 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2834 ToParams[I]->setOwningFunction(ToMethod);
2835 ToMethod->addDecl(ToParams[I]);
2836 }
2837 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002838 ToParams.data(), ToParams.size(),
2839 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002840
2841 ToMethod->setLexicalDeclContext(LexicalDC);
2842 Importer.Imported(D, ToMethod);
2843 LexicalDC->addDecl(ToMethod);
2844 return ToMethod;
2845}
2846
Douglas Gregor84c51c32010-02-18 01:47:50 +00002847Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2848 // Import the major distinguishing characteristics of a category.
2849 DeclContext *DC, *LexicalDC;
2850 DeclarationName Name;
2851 SourceLocation Loc;
2852 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2853 return 0;
2854
2855 ObjCInterfaceDecl *ToInterface
2856 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2857 if (!ToInterface)
2858 return 0;
2859
2860 // Determine if we've already encountered this category.
2861 ObjCCategoryDecl *MergeWithCategory
2862 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2863 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2864 if (!ToCategory) {
2865 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2866 Importer.Import(D->getAtLoc()),
2867 Loc,
2868 Importer.Import(D->getCategoryNameLoc()),
2869 Name.getAsIdentifierInfo());
2870 ToCategory->setLexicalDeclContext(LexicalDC);
2871 LexicalDC->addDecl(ToCategory);
2872 Importer.Imported(D, ToCategory);
2873
2874 // Link this category into its class's category list.
2875 ToCategory->setClassInterface(ToInterface);
2876 ToCategory->insertNextClassCategory();
2877
2878 // Import protocols
2879 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2880 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2881 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2882 = D->protocol_loc_begin();
2883 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2884 FromProtoEnd = D->protocol_end();
2885 FromProto != FromProtoEnd;
2886 ++FromProto, ++FromProtoLoc) {
2887 ObjCProtocolDecl *ToProto
2888 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2889 if (!ToProto)
2890 return 0;
2891 Protocols.push_back(ToProto);
2892 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2893 }
2894
2895 // FIXME: If we're merging, make sure that the protocol list is the same.
2896 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2897 ProtocolLocs.data(), Importer.getToContext());
2898
2899 } else {
2900 Importer.Imported(D, ToCategory);
2901 }
2902
2903 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002904 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002905
2906 // If we have an implementation, import it as well.
2907 if (D->getImplementation()) {
2908 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002909 = cast_or_null<ObjCCategoryImplDecl>(
2910 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002911 if (!Impl)
2912 return 0;
2913
2914 ToCategory->setImplementation(Impl);
2915 }
2916
2917 return ToCategory;
2918}
2919
Douglas Gregor98d156a2010-02-17 16:12:00 +00002920Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002921 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002922 DeclContext *DC, *LexicalDC;
2923 DeclarationName Name;
2924 SourceLocation Loc;
2925 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2926 return 0;
2927
2928 ObjCProtocolDecl *MergeWithProtocol = 0;
2929 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2930 Lookup.first != Lookup.second;
2931 ++Lookup.first) {
2932 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2933 continue;
2934
2935 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2936 break;
2937 }
2938
2939 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2940 if (!ToProto || ToProto->isForwardDecl()) {
2941 if (!ToProto) {
2942 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2943 Name.getAsIdentifierInfo());
2944 ToProto->setForwardDecl(D->isForwardDecl());
2945 ToProto->setLexicalDeclContext(LexicalDC);
2946 LexicalDC->addDecl(ToProto);
2947 }
2948 Importer.Imported(D, ToProto);
2949
2950 // Import protocols
2951 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2952 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2953 ObjCProtocolDecl::protocol_loc_iterator
2954 FromProtoLoc = D->protocol_loc_begin();
2955 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2956 FromProtoEnd = D->protocol_end();
2957 FromProto != FromProtoEnd;
2958 ++FromProto, ++FromProtoLoc) {
2959 ObjCProtocolDecl *ToProto
2960 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2961 if (!ToProto)
2962 return 0;
2963 Protocols.push_back(ToProto);
2964 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2965 }
2966
2967 // FIXME: If we're merging, make sure that the protocol list is the same.
2968 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2969 ProtocolLocs.data(), Importer.getToContext());
2970 } else {
2971 Importer.Imported(D, ToProto);
2972 }
2973
Douglas Gregor84c51c32010-02-18 01:47:50 +00002974 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002975 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002976
2977 return ToProto;
2978}
2979
Douglas Gregor45635322010-02-16 01:20:57 +00002980Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2981 // Import the major distinguishing characteristics of an @interface.
2982 DeclContext *DC, *LexicalDC;
2983 DeclarationName Name;
2984 SourceLocation Loc;
2985 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2986 return 0;
2987
2988 ObjCInterfaceDecl *MergeWithIface = 0;
2989 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2990 Lookup.first != Lookup.second;
2991 ++Lookup.first) {
2992 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2993 continue;
2994
2995 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2996 break;
2997 }
2998
2999 ObjCInterfaceDecl *ToIface = MergeWithIface;
3000 if (!ToIface || ToIface->isForwardDecl()) {
3001 if (!ToIface) {
3002 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3003 DC, Loc,
3004 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003005 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003006 D->isForwardDecl(),
3007 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003008 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003009 ToIface->setLexicalDeclContext(LexicalDC);
3010 LexicalDC->addDecl(ToIface);
3011 }
3012 Importer.Imported(D, ToIface);
3013
Douglas Gregor45635322010-02-16 01:20:57 +00003014 if (D->getSuperClass()) {
3015 ObjCInterfaceDecl *Super
3016 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3017 if (!Super)
3018 return 0;
3019
3020 ToIface->setSuperClass(Super);
3021 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3022 }
3023
3024 // Import protocols
3025 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3026 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3027 ObjCInterfaceDecl::protocol_loc_iterator
3028 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003029
3030 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003031 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3032 FromProtoEnd = D->protocol_end();
3033 FromProto != FromProtoEnd;
3034 ++FromProto, ++FromProtoLoc) {
3035 ObjCProtocolDecl *ToProto
3036 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3037 if (!ToProto)
3038 return 0;
3039 Protocols.push_back(ToProto);
3040 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3041 }
3042
3043 // FIXME: If we're merging, make sure that the protocol list is the same.
3044 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3045 ProtocolLocs.data(), Importer.getToContext());
3046
Douglas Gregor45635322010-02-16 01:20:57 +00003047 // Import @end range
3048 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3049 } else {
3050 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003051
3052 // Check for consistency of superclasses.
3053 DeclarationName FromSuperName, ToSuperName;
3054 if (D->getSuperClass())
3055 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3056 if (ToIface->getSuperClass())
3057 ToSuperName = ToIface->getSuperClass()->getDeclName();
3058 if (FromSuperName != ToSuperName) {
3059 Importer.ToDiag(ToIface->getLocation(),
3060 diag::err_odr_objc_superclass_inconsistent)
3061 << ToIface->getDeclName();
3062 if (ToIface->getSuperClass())
3063 Importer.ToDiag(ToIface->getSuperClassLoc(),
3064 diag::note_odr_objc_superclass)
3065 << ToIface->getSuperClass()->getDeclName();
3066 else
3067 Importer.ToDiag(ToIface->getLocation(),
3068 diag::note_odr_objc_missing_superclass);
3069 if (D->getSuperClass())
3070 Importer.FromDiag(D->getSuperClassLoc(),
3071 diag::note_odr_objc_superclass)
3072 << D->getSuperClass()->getDeclName();
3073 else
3074 Importer.FromDiag(D->getLocation(),
3075 diag::note_odr_objc_missing_superclass);
3076 return 0;
3077 }
Douglas Gregor45635322010-02-16 01:20:57 +00003078 }
3079
Douglas Gregor84c51c32010-02-18 01:47:50 +00003080 // Import categories. When the categories themselves are imported, they'll
3081 // hook themselves into this interface.
3082 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3083 FromCat = FromCat->getNextClassCategory())
3084 Importer.Import(FromCat);
3085
Douglas Gregor45635322010-02-16 01:20:57 +00003086 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003087 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003088
3089 // If we have an @implementation, import it as well.
3090 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003091 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3092 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003093 if (!Impl)
3094 return 0;
3095
3096 ToIface->setImplementation(Impl);
3097 }
3098
Douglas Gregor98d156a2010-02-17 16:12:00 +00003099 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003100}
3101
Douglas Gregor4da9d682010-12-07 15:32:12 +00003102Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3103 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3104 Importer.Import(D->getCategoryDecl()));
3105 if (!Category)
3106 return 0;
3107
3108 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3109 if (!ToImpl) {
3110 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3111 if (!DC)
3112 return 0;
3113
3114 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3115 Importer.Import(D->getLocation()),
3116 Importer.Import(D->getIdentifier()),
3117 Category->getClassInterface());
3118
3119 DeclContext *LexicalDC = DC;
3120 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3121 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3122 if (!LexicalDC)
3123 return 0;
3124
3125 ToImpl->setLexicalDeclContext(LexicalDC);
3126 }
3127
3128 LexicalDC->addDecl(ToImpl);
3129 Category->setImplementation(ToImpl);
3130 }
3131
3132 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003133 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003134 return ToImpl;
3135}
3136
Douglas Gregorda8025c2010-12-07 01:26:03 +00003137Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3138 // Find the corresponding interface.
3139 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3140 Importer.Import(D->getClassInterface()));
3141 if (!Iface)
3142 return 0;
3143
3144 // Import the superclass, if any.
3145 ObjCInterfaceDecl *Super = 0;
3146 if (D->getSuperClass()) {
3147 Super = cast_or_null<ObjCInterfaceDecl>(
3148 Importer.Import(D->getSuperClass()));
3149 if (!Super)
3150 return 0;
3151 }
3152
3153 ObjCImplementationDecl *Impl = Iface->getImplementation();
3154 if (!Impl) {
3155 // We haven't imported an implementation yet. Create a new @implementation
3156 // now.
3157 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3158 Importer.ImportContext(D->getDeclContext()),
3159 Importer.Import(D->getLocation()),
3160 Iface, Super);
3161
3162 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3163 DeclContext *LexicalDC
3164 = Importer.ImportContext(D->getLexicalDeclContext());
3165 if (!LexicalDC)
3166 return 0;
3167 Impl->setLexicalDeclContext(LexicalDC);
3168 }
3169
3170 // Associate the implementation with the class it implements.
3171 Iface->setImplementation(Impl);
3172 Importer.Imported(D, Iface->getImplementation());
3173 } else {
3174 Importer.Imported(D, Iface->getImplementation());
3175
3176 // Verify that the existing @implementation has the same superclass.
3177 if ((Super && !Impl->getSuperClass()) ||
3178 (!Super && Impl->getSuperClass()) ||
3179 (Super && Impl->getSuperClass() &&
3180 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3181 Importer.ToDiag(Impl->getLocation(),
3182 diag::err_odr_objc_superclass_inconsistent)
3183 << Iface->getDeclName();
3184 // FIXME: It would be nice to have the location of the superclass
3185 // below.
3186 if (Impl->getSuperClass())
3187 Importer.ToDiag(Impl->getLocation(),
3188 diag::note_odr_objc_superclass)
3189 << Impl->getSuperClass()->getDeclName();
3190 else
3191 Importer.ToDiag(Impl->getLocation(),
3192 diag::note_odr_objc_missing_superclass);
3193 if (D->getSuperClass())
3194 Importer.FromDiag(D->getLocation(),
3195 diag::note_odr_objc_superclass)
3196 << D->getSuperClass()->getDeclName();
3197 else
3198 Importer.FromDiag(D->getLocation(),
3199 diag::note_odr_objc_missing_superclass);
3200 return 0;
3201 }
3202 }
3203
3204 // Import all of the members of this @implementation.
3205 ImportDeclContext(D);
3206
3207 return Impl;
3208}
3209
Douglas Gregora11c4582010-02-17 18:02:10 +00003210Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3211 // Import the major distinguishing characteristics of an @property.
3212 DeclContext *DC, *LexicalDC;
3213 DeclarationName Name;
3214 SourceLocation Loc;
3215 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3216 return 0;
3217
3218 // Check whether we have already imported this property.
3219 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3220 Lookup.first != Lookup.second;
3221 ++Lookup.first) {
3222 if (ObjCPropertyDecl *FoundProp
3223 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3224 // Check property types.
3225 if (!Importer.IsStructurallyEquivalent(D->getType(),
3226 FoundProp->getType())) {
3227 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3228 << Name << D->getType() << FoundProp->getType();
3229 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3230 << FoundProp->getType();
3231 return 0;
3232 }
3233
3234 // FIXME: Check property attributes, getters, setters, etc.?
3235
3236 // Consider these properties to be equivalent.
3237 Importer.Imported(D, FoundProp);
3238 return FoundProp;
3239 }
3240 }
3241
3242 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003243 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3244 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003245 return 0;
3246
3247 // Create the new property.
3248 ObjCPropertyDecl *ToProperty
3249 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3250 Name.getAsIdentifierInfo(),
3251 Importer.Import(D->getAtLoc()),
3252 T,
3253 D->getPropertyImplementation());
3254 Importer.Imported(D, ToProperty);
3255 ToProperty->setLexicalDeclContext(LexicalDC);
3256 LexicalDC->addDecl(ToProperty);
3257
3258 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003259 ToProperty->setPropertyAttributesAsWritten(
3260 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003261 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3262 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3263 ToProperty->setGetterMethodDecl(
3264 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3265 ToProperty->setSetterMethodDecl(
3266 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3267 ToProperty->setPropertyIvarDecl(
3268 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3269 return ToProperty;
3270}
3271
Douglas Gregor14a49e22010-12-07 18:32:03 +00003272Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3273 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3274 Importer.Import(D->getPropertyDecl()));
3275 if (!Property)
3276 return 0;
3277
3278 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3279 if (!DC)
3280 return 0;
3281
3282 // Import the lexical declaration context.
3283 DeclContext *LexicalDC = DC;
3284 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3285 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3286 if (!LexicalDC)
3287 return 0;
3288 }
3289
3290 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3291 if (!InImpl)
3292 return 0;
3293
3294 // Import the ivar (for an @synthesize).
3295 ObjCIvarDecl *Ivar = 0;
3296 if (D->getPropertyIvarDecl()) {
3297 Ivar = cast_or_null<ObjCIvarDecl>(
3298 Importer.Import(D->getPropertyIvarDecl()));
3299 if (!Ivar)
3300 return 0;
3301 }
3302
3303 ObjCPropertyImplDecl *ToImpl
3304 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3305 if (!ToImpl) {
3306 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3307 Importer.Import(D->getLocStart()),
3308 Importer.Import(D->getLocation()),
3309 Property,
3310 D->getPropertyImplementation(),
3311 Ivar,
3312 Importer.Import(D->getPropertyIvarDeclLoc()));
3313 ToImpl->setLexicalDeclContext(LexicalDC);
3314 Importer.Imported(D, ToImpl);
3315 LexicalDC->addDecl(ToImpl);
3316 } else {
3317 // Check that we have the same kind of property implementation (@synthesize
3318 // vs. @dynamic).
3319 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3320 Importer.ToDiag(ToImpl->getLocation(),
3321 diag::err_odr_objc_property_impl_kind_inconsistent)
3322 << Property->getDeclName()
3323 << (ToImpl->getPropertyImplementation()
3324 == ObjCPropertyImplDecl::Dynamic);
3325 Importer.FromDiag(D->getLocation(),
3326 diag::note_odr_objc_property_impl_kind)
3327 << D->getPropertyDecl()->getDeclName()
3328 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3329 return 0;
3330 }
3331
3332 // For @synthesize, check that we have the same
3333 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3334 Ivar != ToImpl->getPropertyIvarDecl()) {
3335 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3336 diag::err_odr_objc_synthesize_ivar_inconsistent)
3337 << Property->getDeclName()
3338 << ToImpl->getPropertyIvarDecl()->getDeclName()
3339 << Ivar->getDeclName();
3340 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3341 diag::note_odr_objc_synthesize_ivar_here)
3342 << D->getPropertyIvarDecl()->getDeclName();
3343 return 0;
3344 }
3345
3346 // Merge the existing implementation with the new implementation.
3347 Importer.Imported(D, ToImpl);
3348 }
3349
3350 return ToImpl;
3351}
3352
Douglas Gregor8661a722010-02-18 02:12:22 +00003353Decl *
3354ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3355 // Import the context of this declaration.
3356 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3357 if (!DC)
3358 return 0;
3359
3360 DeclContext *LexicalDC = DC;
3361 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3362 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3363 if (!LexicalDC)
3364 return 0;
3365 }
3366
3367 // Import the location of this declaration.
3368 SourceLocation Loc = Importer.Import(D->getLocation());
3369
3370 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3371 llvm::SmallVector<SourceLocation, 4> Locations;
3372 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3373 = D->protocol_loc_begin();
3374 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3375 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3376 FromProto != FromProtoEnd;
3377 ++FromProto, ++FromProtoLoc) {
3378 ObjCProtocolDecl *ToProto
3379 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3380 if (!ToProto)
3381 continue;
3382
3383 Protocols.push_back(ToProto);
3384 Locations.push_back(Importer.Import(*FromProtoLoc));
3385 }
3386
3387 ObjCForwardProtocolDecl *ToForward
3388 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3389 Protocols.data(), Protocols.size(),
3390 Locations.data());
3391 ToForward->setLexicalDeclContext(LexicalDC);
3392 LexicalDC->addDecl(ToForward);
3393 Importer.Imported(D, ToForward);
3394 return ToForward;
3395}
3396
Douglas Gregor06537af2010-02-18 02:04:09 +00003397Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3398 // Import the context of this declaration.
3399 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3400 if (!DC)
3401 return 0;
3402
3403 DeclContext *LexicalDC = DC;
3404 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3405 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3406 if (!LexicalDC)
3407 return 0;
3408 }
3409
3410 // Import the location of this declaration.
3411 SourceLocation Loc = Importer.Import(D->getLocation());
3412
3413 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3414 llvm::SmallVector<SourceLocation, 4> Locations;
3415 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3416 From != FromEnd; ++From) {
3417 ObjCInterfaceDecl *ToIface
3418 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3419 if (!ToIface)
3420 continue;
3421
3422 Interfaces.push_back(ToIface);
3423 Locations.push_back(Importer.Import(From->getLocation()));
3424 }
3425
3426 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3427 Loc,
3428 Interfaces.data(),
3429 Locations.data(),
3430 Interfaces.size());
3431 ToClass->setLexicalDeclContext(LexicalDC);
3432 LexicalDC->addDecl(ToClass);
3433 Importer.Imported(D, ToClass);
3434 return ToClass;
3435}
3436
Douglas Gregora082a492010-11-30 19:14:50 +00003437Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3438 // For template arguments, we adopt the translation unit as our declaration
3439 // context. This context will be fixed when the actual template declaration
3440 // is created.
3441
3442 // FIXME: Import default argument.
3443 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3444 Importer.getToContext().getTranslationUnitDecl(),
3445 Importer.Import(D->getLocation()),
3446 D->getDepth(),
3447 D->getIndex(),
3448 Importer.Import(D->getIdentifier()),
3449 D->wasDeclaredWithTypename(),
3450 D->isParameterPack());
3451}
3452
3453Decl *
3454ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3455 // Import the name of this declaration.
3456 DeclarationName Name = Importer.Import(D->getDeclName());
3457 if (D->getDeclName() && !Name)
3458 return 0;
3459
3460 // Import the location of this declaration.
3461 SourceLocation Loc = Importer.Import(D->getLocation());
3462
3463 // Import the type of this declaration.
3464 QualType T = Importer.Import(D->getType());
3465 if (T.isNull())
3466 return 0;
3467
3468 // Import type-source information.
3469 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3470 if (D->getTypeSourceInfo() && !TInfo)
3471 return 0;
3472
3473 // FIXME: Import default argument.
3474
3475 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3476 Importer.getToContext().getTranslationUnitDecl(),
3477 Loc, D->getDepth(), D->getPosition(),
3478 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003479 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003480}
3481
3482Decl *
3483ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3484 // Import the name of this declaration.
3485 DeclarationName Name = Importer.Import(D->getDeclName());
3486 if (D->getDeclName() && !Name)
3487 return 0;
3488
3489 // Import the location of this declaration.
3490 SourceLocation Loc = Importer.Import(D->getLocation());
3491
3492 // Import template parameters.
3493 TemplateParameterList *TemplateParams
3494 = ImportTemplateParameterList(D->getTemplateParameters());
3495 if (!TemplateParams)
3496 return 0;
3497
3498 // FIXME: Import default argument.
3499
3500 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3501 Importer.getToContext().getTranslationUnitDecl(),
3502 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003503 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003504 Name.getAsIdentifierInfo(),
3505 TemplateParams);
3506}
3507
3508Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3509 // If this record has a definition in the translation unit we're coming from,
3510 // but this particular declaration is not that definition, import the
3511 // definition and map to that.
3512 CXXRecordDecl *Definition
3513 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3514 if (Definition && Definition != D->getTemplatedDecl()) {
3515 Decl *ImportedDef
3516 = Importer.Import(Definition->getDescribedClassTemplate());
3517 if (!ImportedDef)
3518 return 0;
3519
3520 return Importer.Imported(D, ImportedDef);
3521 }
3522
3523 // Import the major distinguishing characteristics of this class template.
3524 DeclContext *DC, *LexicalDC;
3525 DeclarationName Name;
3526 SourceLocation Loc;
3527 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3528 return 0;
3529
3530 // We may already have a template of the same name; try to find and match it.
3531 if (!DC->isFunctionOrMethod()) {
3532 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3533 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3534 Lookup.first != Lookup.second;
3535 ++Lookup.first) {
3536 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3537 continue;
3538
3539 Decl *Found = *Lookup.first;
3540 if (ClassTemplateDecl *FoundTemplate
3541 = dyn_cast<ClassTemplateDecl>(Found)) {
3542 if (IsStructuralMatch(D, FoundTemplate)) {
3543 // The class templates structurally match; call it the same template.
3544 // FIXME: We may be filling in a forward declaration here. Handle
3545 // this case!
3546 Importer.Imported(D->getTemplatedDecl(),
3547 FoundTemplate->getTemplatedDecl());
3548 return Importer.Imported(D, FoundTemplate);
3549 }
3550 }
3551
3552 ConflictingDecls.push_back(*Lookup.first);
3553 }
3554
3555 if (!ConflictingDecls.empty()) {
3556 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3557 ConflictingDecls.data(),
3558 ConflictingDecls.size());
3559 }
3560
3561 if (!Name)
3562 return 0;
3563 }
3564
3565 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3566
3567 // Create the declaration that is being templated.
3568 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3569 DTemplated->getTagKind(),
3570 DC,
3571 Importer.Import(DTemplated->getLocation()),
3572 Name.getAsIdentifierInfo(),
3573 Importer.Import(DTemplated->getTagKeywordLoc()));
3574 D2Templated->setAccess(DTemplated->getAccess());
3575
3576
3577 // Import the qualifier, if any.
3578 if (DTemplated->getQualifier()) {
3579 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3580 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3581 D2Templated->setQualifierInfo(NNS, NNSRange);
3582 }
3583 D2Templated->setLexicalDeclContext(LexicalDC);
3584
3585 // Create the class template declaration itself.
3586 TemplateParameterList *TemplateParams
3587 = ImportTemplateParameterList(D->getTemplateParameters());
3588 if (!TemplateParams)
3589 return 0;
3590
3591 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3592 Loc, Name, TemplateParams,
3593 D2Templated,
3594 /*PrevDecl=*/0);
3595 D2Templated->setDescribedClassTemplate(D2);
3596
3597 D2->setAccess(D->getAccess());
3598 D2->setLexicalDeclContext(LexicalDC);
3599 LexicalDC->addDecl(D2);
3600
3601 // Note the relationship between the class templates.
3602 Importer.Imported(D, D2);
3603 Importer.Imported(DTemplated, D2Templated);
3604
3605 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3606 // FIXME: Import definition!
3607 }
3608
3609 return D2;
3610}
3611
Douglas Gregore2e50d332010-12-01 01:36:18 +00003612Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3613 ClassTemplateSpecializationDecl *D) {
3614 // If this record has a definition in the translation unit we're coming from,
3615 // but this particular declaration is not that definition, import the
3616 // definition and map to that.
3617 TagDecl *Definition = D->getDefinition();
3618 if (Definition && Definition != D) {
3619 Decl *ImportedDef = Importer.Import(Definition);
3620 if (!ImportedDef)
3621 return 0;
3622
3623 return Importer.Imported(D, ImportedDef);
3624 }
3625
3626 ClassTemplateDecl *ClassTemplate
3627 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3628 D->getSpecializedTemplate()));
3629 if (!ClassTemplate)
3630 return 0;
3631
3632 // Import the context of this declaration.
3633 DeclContext *DC = ClassTemplate->getDeclContext();
3634 if (!DC)
3635 return 0;
3636
3637 DeclContext *LexicalDC = DC;
3638 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3639 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3640 if (!LexicalDC)
3641 return 0;
3642 }
3643
3644 // Import the location of this declaration.
3645 SourceLocation Loc = Importer.Import(D->getLocation());
3646
3647 // Import template arguments.
3648 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3649 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3650 D->getTemplateArgs().size(),
3651 TemplateArgs))
3652 return 0;
3653
3654 // Try to find an existing specialization with these template arguments.
3655 void *InsertPos = 0;
3656 ClassTemplateSpecializationDecl *D2
3657 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3658 TemplateArgs.size(), InsertPos);
3659 if (D2) {
3660 // We already have a class template specialization with these template
3661 // arguments.
3662
3663 // FIXME: Check for specialization vs. instantiation errors.
3664
3665 if (RecordDecl *FoundDef = D2->getDefinition()) {
3666 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3667 // The record types structurally match, or the "from" translation
3668 // unit only had a forward declaration anyway; call it the same
3669 // function.
3670 return Importer.Imported(D, FoundDef);
3671 }
3672 }
3673 } else {
3674 // Create a new specialization.
3675 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3676 D->getTagKind(), DC,
3677 Loc, ClassTemplate,
3678 TemplateArgs.data(),
3679 TemplateArgs.size(),
3680 /*PrevDecl=*/0);
3681 D2->setSpecializationKind(D->getSpecializationKind());
3682
3683 // Add this specialization to the class template.
3684 ClassTemplate->AddSpecialization(D2, InsertPos);
3685
3686 // Import the qualifier, if any.
3687 if (D->getQualifier()) {
3688 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3689 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3690 D2->setQualifierInfo(NNS, NNSRange);
3691 }
3692
3693
3694 // Add the specialization to this context.
3695 D2->setLexicalDeclContext(LexicalDC);
3696 LexicalDC->addDecl(D2);
3697 }
3698 Importer.Imported(D, D2);
3699
3700 if (D->isDefinition() && ImportDefinition(D, D2))
3701 return 0;
3702
3703 return D2;
3704}
3705
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003706//----------------------------------------------------------------------------
3707// Import Statements
3708//----------------------------------------------------------------------------
3709
3710Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3711 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3712 << S->getStmtClassName();
3713 return 0;
3714}
3715
3716//----------------------------------------------------------------------------
3717// Import Expressions
3718//----------------------------------------------------------------------------
3719Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3720 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3721 << E->getStmtClassName();
3722 return 0;
3723}
3724
Douglas Gregor52f820e2010-02-19 01:17:02 +00003725Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3726 NestedNameSpecifier *Qualifier = 0;
3727 if (E->getQualifier()) {
3728 Qualifier = Importer.Import(E->getQualifier());
3729 if (!E->getQualifier())
3730 return 0;
3731 }
3732
3733 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3734 if (!ToD)
3735 return 0;
3736
3737 QualType T = Importer.Import(E->getType());
3738 if (T.isNull())
3739 return 0;
3740
3741 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3742 Importer.Import(E->getQualifierRange()),
3743 ToD,
3744 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003745 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003746 /*FIXME:TemplateArgs=*/0);
3747}
3748
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003749Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3750 QualType T = Importer.Import(E->getType());
3751 if (T.isNull())
3752 return 0;
3753
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003754 return IntegerLiteral::Create(Importer.getToContext(),
3755 E->getValue(), T,
3756 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003757}
3758
Douglas Gregor623421d2010-02-18 02:21:22 +00003759Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3760 QualType T = Importer.Import(E->getType());
3761 if (T.isNull())
3762 return 0;
3763
3764 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3765 E->isWide(), T,
3766 Importer.Import(E->getLocation()));
3767}
3768
Douglas Gregorc74247e2010-02-19 01:07:06 +00003769Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3770 Expr *SubExpr = Importer.Import(E->getSubExpr());
3771 if (!SubExpr)
3772 return 0;
3773
3774 return new (Importer.getToContext())
3775 ParenExpr(Importer.Import(E->getLParen()),
3776 Importer.Import(E->getRParen()),
3777 SubExpr);
3778}
3779
3780Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3781 QualType T = Importer.Import(E->getType());
3782 if (T.isNull())
3783 return 0;
3784
3785 Expr *SubExpr = Importer.Import(E->getSubExpr());
3786 if (!SubExpr)
3787 return 0;
3788
3789 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003790 T, E->getValueKind(),
3791 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003792 Importer.Import(E->getOperatorLoc()));
3793}
3794
Douglas Gregord8552cd2010-02-19 01:24:23 +00003795Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3796 QualType ResultType = Importer.Import(E->getType());
3797
3798 if (E->isArgumentType()) {
3799 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3800 if (!TInfo)
3801 return 0;
3802
3803 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3804 TInfo, ResultType,
3805 Importer.Import(E->getOperatorLoc()),
3806 Importer.Import(E->getRParenLoc()));
3807 }
3808
3809 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3810 if (!SubExpr)
3811 return 0;
3812
3813 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3814 SubExpr, ResultType,
3815 Importer.Import(E->getOperatorLoc()),
3816 Importer.Import(E->getRParenLoc()));
3817}
3818
Douglas Gregorc74247e2010-02-19 01:07:06 +00003819Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3820 QualType T = Importer.Import(E->getType());
3821 if (T.isNull())
3822 return 0;
3823
3824 Expr *LHS = Importer.Import(E->getLHS());
3825 if (!LHS)
3826 return 0;
3827
3828 Expr *RHS = Importer.Import(E->getRHS());
3829 if (!RHS)
3830 return 0;
3831
3832 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003833 T, E->getValueKind(),
3834 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003835 Importer.Import(E->getOperatorLoc()));
3836}
3837
3838Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3839 QualType T = Importer.Import(E->getType());
3840 if (T.isNull())
3841 return 0;
3842
3843 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3844 if (CompLHSType.isNull())
3845 return 0;
3846
3847 QualType CompResultType = Importer.Import(E->getComputationResultType());
3848 if (CompResultType.isNull())
3849 return 0;
3850
3851 Expr *LHS = Importer.Import(E->getLHS());
3852 if (!LHS)
3853 return 0;
3854
3855 Expr *RHS = Importer.Import(E->getRHS());
3856 if (!RHS)
3857 return 0;
3858
3859 return new (Importer.getToContext())
3860 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003861 T, E->getValueKind(),
3862 E->getObjectKind(),
3863 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003864 Importer.Import(E->getOperatorLoc()));
3865}
3866
John McCallcf142162010-08-07 06:22:56 +00003867bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3868 if (E->path_empty()) return false;
3869
3870 // TODO: import cast paths
3871 return true;
3872}
3873
Douglas Gregor98c10182010-02-12 22:17:39 +00003874Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3875 QualType T = Importer.Import(E->getType());
3876 if (T.isNull())
3877 return 0;
3878
3879 Expr *SubExpr = Importer.Import(E->getSubExpr());
3880 if (!SubExpr)
3881 return 0;
John McCallcf142162010-08-07 06:22:56 +00003882
3883 CXXCastPath BasePath;
3884 if (ImportCastPath(E, BasePath))
3885 return 0;
3886
3887 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003888 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003889}
3890
Douglas Gregor5481d322010-02-19 01:32:14 +00003891Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3892 QualType T = Importer.Import(E->getType());
3893 if (T.isNull())
3894 return 0;
3895
3896 Expr *SubExpr = Importer.Import(E->getSubExpr());
3897 if (!SubExpr)
3898 return 0;
3899
3900 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3901 if (!TInfo && E->getTypeInfoAsWritten())
3902 return 0;
3903
John McCallcf142162010-08-07 06:22:56 +00003904 CXXCastPath BasePath;
3905 if (ImportCastPath(E, BasePath))
3906 return 0;
3907
John McCall7decc9e2010-11-18 06:31:45 +00003908 return CStyleCastExpr::Create(Importer.getToContext(), T,
3909 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003910 SubExpr, &BasePath, TInfo,
3911 Importer.Import(E->getLParenLoc()),
3912 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003913}
3914
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003915ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003916 ASTContext &FromContext, FileManager &FromFileManager,
3917 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003918 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003919 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3920 Minimal(MinimalImport)
3921{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003922 ImportedDecls[FromContext.getTranslationUnitDecl()]
3923 = ToContext.getTranslationUnitDecl();
3924}
3925
3926ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003927
3928QualType ASTImporter::Import(QualType FromT) {
3929 if (FromT.isNull())
3930 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003931
3932 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003933
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003934 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003935 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3936 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003937 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003938 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003939
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003940 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003941 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003942 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003943 if (ToT.isNull())
3944 return ToT;
3945
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003946 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003947 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003948
John McCall424cec92011-01-19 06:33:43 +00003949 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003950}
3951
Douglas Gregor62d311f2010-02-09 19:21:46 +00003952TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003953 if (!FromTSI)
3954 return FromTSI;
3955
3956 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003957 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003958 QualType T = Import(FromTSI->getType());
3959 if (T.isNull())
3960 return 0;
3961
3962 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003963 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003964}
3965
3966Decl *ASTImporter::Import(Decl *FromD) {
3967 if (!FromD)
3968 return 0;
3969
3970 // Check whether we've already imported this declaration.
3971 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3972 if (Pos != ImportedDecls.end())
3973 return Pos->second;
3974
3975 // Import the type
3976 ASTNodeImporter Importer(*this);
3977 Decl *ToD = Importer.Visit(FromD);
3978 if (!ToD)
3979 return 0;
3980
3981 // Record the imported declaration.
3982 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003983
3984 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3985 // Keep track of anonymous tags that have an associated typedef.
3986 if (FromTag->getTypedefForAnonDecl())
3987 AnonTagsWithPendingTypedefs.push_back(FromTag);
3988 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3989 // When we've finished transforming a typedef, see whether it was the
3990 // typedef for an anonymous tag.
3991 for (llvm::SmallVector<TagDecl *, 4>::iterator
3992 FromTag = AnonTagsWithPendingTypedefs.begin(),
3993 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3994 FromTag != FromTagEnd; ++FromTag) {
3995 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3996 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3997 // We found the typedef for an anonymous tag; link them.
3998 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3999 AnonTagsWithPendingTypedefs.erase(FromTag);
4000 break;
4001 }
4002 }
4003 }
4004 }
4005
Douglas Gregor62d311f2010-02-09 19:21:46 +00004006 return ToD;
4007}
4008
4009DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4010 if (!FromDC)
4011 return FromDC;
4012
4013 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4014}
4015
4016Expr *ASTImporter::Import(Expr *FromE) {
4017 if (!FromE)
4018 return 0;
4019
4020 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4021}
4022
4023Stmt *ASTImporter::Import(Stmt *FromS) {
4024 if (!FromS)
4025 return 0;
4026
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004027 // Check whether we've already imported this declaration.
4028 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4029 if (Pos != ImportedStmts.end())
4030 return Pos->second;
4031
4032 // Import the type
4033 ASTNodeImporter Importer(*this);
4034 Stmt *ToS = Importer.Visit(FromS);
4035 if (!ToS)
4036 return 0;
4037
4038 // Record the imported declaration.
4039 ImportedStmts[FromS] = ToS;
4040 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004041}
4042
4043NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4044 if (!FromNNS)
4045 return 0;
4046
4047 // FIXME: Implement!
4048 return 0;
4049}
4050
Douglas Gregore2e50d332010-12-01 01:36:18 +00004051TemplateName ASTImporter::Import(TemplateName From) {
4052 switch (From.getKind()) {
4053 case TemplateName::Template:
4054 if (TemplateDecl *ToTemplate
4055 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4056 return TemplateName(ToTemplate);
4057
4058 return TemplateName();
4059
4060 case TemplateName::OverloadedTemplate: {
4061 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4062 UnresolvedSet<2> ToTemplates;
4063 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4064 E = FromStorage->end();
4065 I != E; ++I) {
4066 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4067 ToTemplates.addDecl(To);
4068 else
4069 return TemplateName();
4070 }
4071 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4072 ToTemplates.end());
4073 }
4074
4075 case TemplateName::QualifiedTemplate: {
4076 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4077 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4078 if (!Qualifier)
4079 return TemplateName();
4080
4081 if (TemplateDecl *ToTemplate
4082 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4083 return ToContext.getQualifiedTemplateName(Qualifier,
4084 QTN->hasTemplateKeyword(),
4085 ToTemplate);
4086
4087 return TemplateName();
4088 }
4089
4090 case TemplateName::DependentTemplate: {
4091 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4092 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4093 if (!Qualifier)
4094 return TemplateName();
4095
4096 if (DTN->isIdentifier()) {
4097 return ToContext.getDependentTemplateName(Qualifier,
4098 Import(DTN->getIdentifier()));
4099 }
4100
4101 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4102 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004103
4104 case TemplateName::SubstTemplateTemplateParmPack: {
4105 SubstTemplateTemplateParmPackStorage *SubstPack
4106 = From.getAsSubstTemplateTemplateParmPack();
4107 TemplateTemplateParmDecl *Param
4108 = cast_or_null<TemplateTemplateParmDecl>(
4109 Import(SubstPack->getParameterPack()));
4110 if (!Param)
4111 return TemplateName();
4112
4113 ASTNodeImporter Importer(*this);
4114 TemplateArgument ArgPack
4115 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4116 if (ArgPack.isNull())
4117 return TemplateName();
4118
4119 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4120 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004121 }
4122
4123 llvm_unreachable("Invalid template name kind");
4124 return TemplateName();
4125}
4126
Douglas Gregor62d311f2010-02-09 19:21:46 +00004127SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4128 if (FromLoc.isInvalid())
4129 return SourceLocation();
4130
Douglas Gregor811663e2010-02-10 00:15:17 +00004131 SourceManager &FromSM = FromContext.getSourceManager();
4132
4133 // For now, map everything down to its spelling location, so that we
4134 // don't have to import macro instantiations.
4135 // FIXME: Import macro instantiations!
4136 FromLoc = FromSM.getSpellingLoc(FromLoc);
4137 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4138 SourceManager &ToSM = ToContext.getSourceManager();
4139 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4140 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004141}
4142
4143SourceRange ASTImporter::Import(SourceRange FromRange) {
4144 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4145}
4146
Douglas Gregor811663e2010-02-10 00:15:17 +00004147FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004148 llvm::DenseMap<FileID, FileID>::iterator Pos
4149 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004150 if (Pos != ImportedFileIDs.end())
4151 return Pos->second;
4152
4153 SourceManager &FromSM = FromContext.getSourceManager();
4154 SourceManager &ToSM = ToContext.getSourceManager();
4155 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4156 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4157
4158 // Include location of this file.
4159 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4160
4161 // Map the FileID for to the "to" source manager.
4162 FileID ToID;
4163 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4164 if (Cache->Entry) {
4165 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4166 // disk again
4167 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4168 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00004169 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004170 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4171 FromSLoc.getFile().getFileCharacteristic());
4172 } else {
4173 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004174 const llvm::MemoryBuffer *
4175 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004176 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004177 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004178 FromBuf->getBufferIdentifier());
4179 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4180 }
4181
4182
Sebastian Redl99219f12010-09-30 01:03:06 +00004183 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004184 return ToID;
4185}
4186
Douglas Gregor0a791672011-01-18 03:11:38 +00004187void ASTImporter::ImportDefinition(Decl *From) {
4188 Decl *To = Import(From);
4189 if (!To)
4190 return;
4191
4192 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4193 ASTNodeImporter Importer(*this);
4194 Importer.ImportDeclContext(FromDC, true);
4195 }
4196}
4197
Douglas Gregor96e578d2010-02-05 17:54:41 +00004198DeclarationName ASTImporter::Import(DeclarationName FromName) {
4199 if (!FromName)
4200 return DeclarationName();
4201
4202 switch (FromName.getNameKind()) {
4203 case DeclarationName::Identifier:
4204 return Import(FromName.getAsIdentifierInfo());
4205
4206 case DeclarationName::ObjCZeroArgSelector:
4207 case DeclarationName::ObjCOneArgSelector:
4208 case DeclarationName::ObjCMultiArgSelector:
4209 return Import(FromName.getObjCSelector());
4210
4211 case DeclarationName::CXXConstructorName: {
4212 QualType T = Import(FromName.getCXXNameType());
4213 if (T.isNull())
4214 return DeclarationName();
4215
4216 return ToContext.DeclarationNames.getCXXConstructorName(
4217 ToContext.getCanonicalType(T));
4218 }
4219
4220 case DeclarationName::CXXDestructorName: {
4221 QualType T = Import(FromName.getCXXNameType());
4222 if (T.isNull())
4223 return DeclarationName();
4224
4225 return ToContext.DeclarationNames.getCXXDestructorName(
4226 ToContext.getCanonicalType(T));
4227 }
4228
4229 case DeclarationName::CXXConversionFunctionName: {
4230 QualType T = Import(FromName.getCXXNameType());
4231 if (T.isNull())
4232 return DeclarationName();
4233
4234 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4235 ToContext.getCanonicalType(T));
4236 }
4237
4238 case DeclarationName::CXXOperatorName:
4239 return ToContext.DeclarationNames.getCXXOperatorName(
4240 FromName.getCXXOverloadedOperator());
4241
4242 case DeclarationName::CXXLiteralOperatorName:
4243 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4244 Import(FromName.getCXXLiteralIdentifier()));
4245
4246 case DeclarationName::CXXUsingDirective:
4247 // FIXME: STATICS!
4248 return DeclarationName::getUsingDirectiveName();
4249 }
4250
4251 // Silence bogus GCC warning
4252 return DeclarationName();
4253}
4254
Douglas Gregore2e50d332010-12-01 01:36:18 +00004255IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004256 if (!FromId)
4257 return 0;
4258
4259 return &ToContext.Idents.get(FromId->getName());
4260}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004261
Douglas Gregor43f54792010-02-17 02:12:47 +00004262Selector ASTImporter::Import(Selector FromSel) {
4263 if (FromSel.isNull())
4264 return Selector();
4265
4266 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4267 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4268 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4269 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4270 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4271}
4272
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004273DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4274 DeclContext *DC,
4275 unsigned IDNS,
4276 NamedDecl **Decls,
4277 unsigned NumDecls) {
4278 return Name;
4279}
4280
4281DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004282 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004283}
4284
4285DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004286 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004287}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004288
4289Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4290 ImportedDecls[From] = To;
4291 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004292}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004293
4294bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004295 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004296 = ImportedTypes.find(From.getTypePtr());
4297 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4298 return true;
4299
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004300 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004301 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004302}