blob: 02be81b8ee16563bf407db5372e340dfee8ca586 [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);
2399 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002400
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002401 // Set the parameters.
2402 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002403 Parameters[I]->setOwningFunction(ToFunction);
2404 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002405 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002406 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002407
2408 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002409
2410 // Add this function to the lexical context.
2411 LexicalDC->addDecl(ToFunction);
2412
Douglas Gregor43f54792010-02-17 02:12:47 +00002413 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002414}
2415
Douglas Gregor00eace12010-02-21 18:29:16 +00002416Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2417 return VisitFunctionDecl(D);
2418}
2419
2420Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2421 return VisitCXXMethodDecl(D);
2422}
2423
2424Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2425 return VisitCXXMethodDecl(D);
2426}
2427
2428Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2429 return VisitCXXMethodDecl(D);
2430}
2431
Douglas Gregor5c73e912010-02-11 00:48:18 +00002432Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2433 // Import the major distinguishing characteristics of a variable.
2434 DeclContext *DC, *LexicalDC;
2435 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002436 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002437 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2438 return 0;
2439
2440 // Import the type.
2441 QualType T = Importer.Import(D->getType());
2442 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002443 return 0;
2444
2445 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2446 Expr *BitWidth = Importer.Import(D->getBitWidth());
2447 if (!BitWidth && D->getBitWidth())
2448 return 0;
2449
2450 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2451 Loc, Name.getAsIdentifierInfo(),
2452 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002453 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002454 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002455 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002456 LexicalDC->addDecl(ToField);
2457 return ToField;
2458}
2459
Francois Pichet783dd6e2010-11-21 06:08:52 +00002460Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2461 // Import the major distinguishing characteristics of a variable.
2462 DeclContext *DC, *LexicalDC;
2463 DeclarationName Name;
2464 SourceLocation Loc;
2465 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2466 return 0;
2467
2468 // Import the type.
2469 QualType T = Importer.Import(D->getType());
2470 if (T.isNull())
2471 return 0;
2472
2473 NamedDecl **NamedChain =
2474 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2475
2476 unsigned i = 0;
2477 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2478 PE = D->chain_end(); PI != PE; ++PI) {
2479 Decl* D = Importer.Import(*PI);
2480 if (!D)
2481 return 0;
2482 NamedChain[i++] = cast<NamedDecl>(D);
2483 }
2484
2485 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2486 Importer.getToContext(), DC,
2487 Loc, Name.getAsIdentifierInfo(), T,
2488 NamedChain, D->getChainingSize());
2489 ToIndirectField->setAccess(D->getAccess());
2490 ToIndirectField->setLexicalDeclContext(LexicalDC);
2491 Importer.Imported(D, ToIndirectField);
2492 LexicalDC->addDecl(ToIndirectField);
2493 return ToIndirectField;
2494}
2495
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002496Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2497 // Import the major distinguishing characteristics of an ivar.
2498 DeclContext *DC, *LexicalDC;
2499 DeclarationName Name;
2500 SourceLocation Loc;
2501 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2502 return 0;
2503
2504 // Determine whether we've already imported this ivar
2505 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2506 Lookup.first != Lookup.second;
2507 ++Lookup.first) {
2508 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2509 if (Importer.IsStructurallyEquivalent(D->getType(),
2510 FoundIvar->getType())) {
2511 Importer.Imported(D, FoundIvar);
2512 return FoundIvar;
2513 }
2514
2515 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2516 << Name << D->getType() << FoundIvar->getType();
2517 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2518 << FoundIvar->getType();
2519 return 0;
2520 }
2521 }
2522
2523 // Import the type.
2524 QualType T = Importer.Import(D->getType());
2525 if (T.isNull())
2526 return 0;
2527
2528 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2529 Expr *BitWidth = Importer.Import(D->getBitWidth());
2530 if (!BitWidth && D->getBitWidth())
2531 return 0;
2532
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002533 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2534 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002535 Loc, Name.getAsIdentifierInfo(),
2536 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002537 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002538 ToIvar->setLexicalDeclContext(LexicalDC);
2539 Importer.Imported(D, ToIvar);
2540 LexicalDC->addDecl(ToIvar);
2541 return ToIvar;
2542
2543}
2544
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002545Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2546 // Import the major distinguishing characteristics of a variable.
2547 DeclContext *DC, *LexicalDC;
2548 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002549 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002550 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002551 return 0;
2552
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002553 // Try to find a variable in our own ("to") context with the same name and
2554 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002555 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002556 VarDecl *MergeWithVar = 0;
2557 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2558 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002559 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002560 Lookup.first != Lookup.second;
2561 ++Lookup.first) {
2562 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2563 continue;
2564
2565 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2566 // We have found a variable that we may need to merge with. Check it.
2567 if (isExternalLinkage(FoundVar->getLinkage()) &&
2568 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002569 if (Importer.IsStructurallyEquivalent(D->getType(),
2570 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002571 MergeWithVar = FoundVar;
2572 break;
2573 }
2574
Douglas Gregor56521c52010-02-12 17:23:39 +00002575 const ArrayType *FoundArray
2576 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2577 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002578 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002579 if (FoundArray && TArray) {
2580 if (isa<IncompleteArrayType>(FoundArray) &&
2581 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002582 // Import the type.
2583 QualType T = Importer.Import(D->getType());
2584 if (T.isNull())
2585 return 0;
2586
Douglas Gregor56521c52010-02-12 17:23:39 +00002587 FoundVar->setType(T);
2588 MergeWithVar = FoundVar;
2589 break;
2590 } else if (isa<IncompleteArrayType>(TArray) &&
2591 isa<ConstantArrayType>(FoundArray)) {
2592 MergeWithVar = FoundVar;
2593 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002594 }
2595 }
2596
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002597 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002598 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002599 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2600 << FoundVar->getType();
2601 }
2602 }
2603
2604 ConflictingDecls.push_back(*Lookup.first);
2605 }
2606
2607 if (MergeWithVar) {
2608 // An equivalent variable with external linkage has been found. Link
2609 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002610 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002611
2612 if (VarDecl *DDef = D->getDefinition()) {
2613 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2614 Importer.ToDiag(ExistingDef->getLocation(),
2615 diag::err_odr_variable_multiple_def)
2616 << Name;
2617 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2618 } else {
2619 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002620 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002621 }
2622 }
2623
2624 return MergeWithVar;
2625 }
2626
2627 if (!ConflictingDecls.empty()) {
2628 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2629 ConflictingDecls.data(),
2630 ConflictingDecls.size());
2631 if (!Name)
2632 return 0;
2633 }
2634 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002635
Douglas Gregorb4964f72010-02-15 23:54:17 +00002636 // Import the type.
2637 QualType T = Importer.Import(D->getType());
2638 if (T.isNull())
2639 return 0;
2640
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002641 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002642 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002643 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2644 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002645 D->getStorageClass(),
2646 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002647 // Import the qualifier, if any.
2648 if (D->getQualifier()) {
2649 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2650 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2651 ToVar->setQualifierInfo(NNS, NNSRange);
2652 }
Douglas Gregordd483172010-02-22 17:42:47 +00002653 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002654 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002655 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002656 LexicalDC->addDecl(ToVar);
2657
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002658 // Merge the initializer.
2659 // FIXME: Can we really import any initializer? Alternatively, we could force
2660 // ourselves to import every declaration of a variable and then only use
2661 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002662 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002663
2664 // FIXME: Other bits to merge?
2665
2666 return ToVar;
2667}
2668
Douglas Gregor8b228d72010-02-17 21:22:52 +00002669Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2670 // Parameters are created in the translation unit's context, then moved
2671 // into the function declaration's context afterward.
2672 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2673
2674 // Import the name of this declaration.
2675 DeclarationName Name = Importer.Import(D->getDeclName());
2676 if (D->getDeclName() && !Name)
2677 return 0;
2678
2679 // Import the location of this declaration.
2680 SourceLocation Loc = Importer.Import(D->getLocation());
2681
2682 // Import the parameter's type.
2683 QualType T = Importer.Import(D->getType());
2684 if (T.isNull())
2685 return 0;
2686
2687 // Create the imported parameter.
2688 ImplicitParamDecl *ToParm
2689 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2690 Loc, Name.getAsIdentifierInfo(),
2691 T);
2692 return Importer.Imported(D, ToParm);
2693}
2694
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002695Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2696 // Parameters are created in the translation unit's context, then moved
2697 // into the function declaration's context afterward.
2698 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2699
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002700 // Import the name of this declaration.
2701 DeclarationName Name = Importer.Import(D->getDeclName());
2702 if (D->getDeclName() && !Name)
2703 return 0;
2704
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002705 // Import the location of this declaration.
2706 SourceLocation Loc = Importer.Import(D->getLocation());
2707
2708 // Import the parameter's type.
2709 QualType T = Importer.Import(D->getType());
2710 if (T.isNull())
2711 return 0;
2712
2713 // Create the imported parameter.
2714 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2715 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2716 Loc, Name.getAsIdentifierInfo(),
2717 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002718 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002719 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002720 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002721 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002722}
2723
Douglas Gregor43f54792010-02-17 02:12:47 +00002724Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2725 // Import the major distinguishing characteristics of a method.
2726 DeclContext *DC, *LexicalDC;
2727 DeclarationName Name;
2728 SourceLocation Loc;
2729 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2730 return 0;
2731
2732 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2733 Lookup.first != Lookup.second;
2734 ++Lookup.first) {
2735 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2736 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2737 continue;
2738
2739 // Check return types.
2740 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2741 FoundMethod->getResultType())) {
2742 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2743 << D->isInstanceMethod() << Name
2744 << D->getResultType() << FoundMethod->getResultType();
2745 Importer.ToDiag(FoundMethod->getLocation(),
2746 diag::note_odr_objc_method_here)
2747 << D->isInstanceMethod() << Name;
2748 return 0;
2749 }
2750
2751 // Check the number of parameters.
2752 if (D->param_size() != FoundMethod->param_size()) {
2753 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2754 << D->isInstanceMethod() << Name
2755 << D->param_size() << FoundMethod->param_size();
2756 Importer.ToDiag(FoundMethod->getLocation(),
2757 diag::note_odr_objc_method_here)
2758 << D->isInstanceMethod() << Name;
2759 return 0;
2760 }
2761
2762 // Check parameter types.
2763 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2764 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2765 P != PEnd; ++P, ++FoundP) {
2766 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2767 (*FoundP)->getType())) {
2768 Importer.FromDiag((*P)->getLocation(),
2769 diag::err_odr_objc_method_param_type_inconsistent)
2770 << D->isInstanceMethod() << Name
2771 << (*P)->getType() << (*FoundP)->getType();
2772 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2773 << (*FoundP)->getType();
2774 return 0;
2775 }
2776 }
2777
2778 // Check variadic/non-variadic.
2779 // Check the number of parameters.
2780 if (D->isVariadic() != FoundMethod->isVariadic()) {
2781 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2782 << D->isInstanceMethod() << Name;
2783 Importer.ToDiag(FoundMethod->getLocation(),
2784 diag::note_odr_objc_method_here)
2785 << D->isInstanceMethod() << Name;
2786 return 0;
2787 }
2788
2789 // FIXME: Any other bits we need to merge?
2790 return Importer.Imported(D, FoundMethod);
2791 }
2792 }
2793
2794 // Import the result type.
2795 QualType ResultTy = Importer.Import(D->getResultType());
2796 if (ResultTy.isNull())
2797 return 0;
2798
Douglas Gregor12852d92010-03-08 14:59:44 +00002799 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2800
Douglas Gregor43f54792010-02-17 02:12:47 +00002801 ObjCMethodDecl *ToMethod
2802 = ObjCMethodDecl::Create(Importer.getToContext(),
2803 Loc,
2804 Importer.Import(D->getLocEnd()),
2805 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002806 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002807 D->isInstanceMethod(),
2808 D->isVariadic(),
2809 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002810 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002811 D->getImplementationControl());
2812
2813 // FIXME: When we decide to merge method definitions, we'll need to
2814 // deal with implicit parameters.
2815
2816 // Import the parameters
2817 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2818 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2819 FromPEnd = D->param_end();
2820 FromP != FromPEnd;
2821 ++FromP) {
2822 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2823 if (!ToP)
2824 return 0;
2825
2826 ToParams.push_back(ToP);
2827 }
2828
2829 // Set the parameters.
2830 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2831 ToParams[I]->setOwningFunction(ToMethod);
2832 ToMethod->addDecl(ToParams[I]);
2833 }
2834 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002835 ToParams.data(), ToParams.size(),
2836 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002837
2838 ToMethod->setLexicalDeclContext(LexicalDC);
2839 Importer.Imported(D, ToMethod);
2840 LexicalDC->addDecl(ToMethod);
2841 return ToMethod;
2842}
2843
Douglas Gregor84c51c32010-02-18 01:47:50 +00002844Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2845 // Import the major distinguishing characteristics of a category.
2846 DeclContext *DC, *LexicalDC;
2847 DeclarationName Name;
2848 SourceLocation Loc;
2849 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2850 return 0;
2851
2852 ObjCInterfaceDecl *ToInterface
2853 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2854 if (!ToInterface)
2855 return 0;
2856
2857 // Determine if we've already encountered this category.
2858 ObjCCategoryDecl *MergeWithCategory
2859 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2860 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2861 if (!ToCategory) {
2862 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2863 Importer.Import(D->getAtLoc()),
2864 Loc,
2865 Importer.Import(D->getCategoryNameLoc()),
2866 Name.getAsIdentifierInfo());
2867 ToCategory->setLexicalDeclContext(LexicalDC);
2868 LexicalDC->addDecl(ToCategory);
2869 Importer.Imported(D, ToCategory);
2870
2871 // Link this category into its class's category list.
2872 ToCategory->setClassInterface(ToInterface);
2873 ToCategory->insertNextClassCategory();
2874
2875 // Import protocols
2876 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2877 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2878 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2879 = D->protocol_loc_begin();
2880 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2881 FromProtoEnd = D->protocol_end();
2882 FromProto != FromProtoEnd;
2883 ++FromProto, ++FromProtoLoc) {
2884 ObjCProtocolDecl *ToProto
2885 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2886 if (!ToProto)
2887 return 0;
2888 Protocols.push_back(ToProto);
2889 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2890 }
2891
2892 // FIXME: If we're merging, make sure that the protocol list is the same.
2893 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2894 ProtocolLocs.data(), Importer.getToContext());
2895
2896 } else {
2897 Importer.Imported(D, ToCategory);
2898 }
2899
2900 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002901 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002902
2903 // If we have an implementation, import it as well.
2904 if (D->getImplementation()) {
2905 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002906 = cast_or_null<ObjCCategoryImplDecl>(
2907 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002908 if (!Impl)
2909 return 0;
2910
2911 ToCategory->setImplementation(Impl);
2912 }
2913
2914 return ToCategory;
2915}
2916
Douglas Gregor98d156a2010-02-17 16:12:00 +00002917Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002918 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002919 DeclContext *DC, *LexicalDC;
2920 DeclarationName Name;
2921 SourceLocation Loc;
2922 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2923 return 0;
2924
2925 ObjCProtocolDecl *MergeWithProtocol = 0;
2926 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2927 Lookup.first != Lookup.second;
2928 ++Lookup.first) {
2929 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2930 continue;
2931
2932 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2933 break;
2934 }
2935
2936 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2937 if (!ToProto || ToProto->isForwardDecl()) {
2938 if (!ToProto) {
2939 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2940 Name.getAsIdentifierInfo());
2941 ToProto->setForwardDecl(D->isForwardDecl());
2942 ToProto->setLexicalDeclContext(LexicalDC);
2943 LexicalDC->addDecl(ToProto);
2944 }
2945 Importer.Imported(D, ToProto);
2946
2947 // Import protocols
2948 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2949 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2950 ObjCProtocolDecl::protocol_loc_iterator
2951 FromProtoLoc = D->protocol_loc_begin();
2952 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2953 FromProtoEnd = D->protocol_end();
2954 FromProto != FromProtoEnd;
2955 ++FromProto, ++FromProtoLoc) {
2956 ObjCProtocolDecl *ToProto
2957 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2958 if (!ToProto)
2959 return 0;
2960 Protocols.push_back(ToProto);
2961 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2962 }
2963
2964 // FIXME: If we're merging, make sure that the protocol list is the same.
2965 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2966 ProtocolLocs.data(), Importer.getToContext());
2967 } else {
2968 Importer.Imported(D, ToProto);
2969 }
2970
Douglas Gregor84c51c32010-02-18 01:47:50 +00002971 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002972 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002973
2974 return ToProto;
2975}
2976
Douglas Gregor45635322010-02-16 01:20:57 +00002977Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2978 // Import the major distinguishing characteristics of an @interface.
2979 DeclContext *DC, *LexicalDC;
2980 DeclarationName Name;
2981 SourceLocation Loc;
2982 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2983 return 0;
2984
2985 ObjCInterfaceDecl *MergeWithIface = 0;
2986 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2987 Lookup.first != Lookup.second;
2988 ++Lookup.first) {
2989 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2990 continue;
2991
2992 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2993 break;
2994 }
2995
2996 ObjCInterfaceDecl *ToIface = MergeWithIface;
2997 if (!ToIface || ToIface->isForwardDecl()) {
2998 if (!ToIface) {
2999 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3000 DC, Loc,
3001 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003002 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003003 D->isForwardDecl(),
3004 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003005 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003006 ToIface->setLexicalDeclContext(LexicalDC);
3007 LexicalDC->addDecl(ToIface);
3008 }
3009 Importer.Imported(D, ToIface);
3010
Douglas Gregor45635322010-02-16 01:20:57 +00003011 if (D->getSuperClass()) {
3012 ObjCInterfaceDecl *Super
3013 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3014 if (!Super)
3015 return 0;
3016
3017 ToIface->setSuperClass(Super);
3018 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3019 }
3020
3021 // Import protocols
3022 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3023 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3024 ObjCInterfaceDecl::protocol_loc_iterator
3025 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003026
3027 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003028 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3029 FromProtoEnd = D->protocol_end();
3030 FromProto != FromProtoEnd;
3031 ++FromProto, ++FromProtoLoc) {
3032 ObjCProtocolDecl *ToProto
3033 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3034 if (!ToProto)
3035 return 0;
3036 Protocols.push_back(ToProto);
3037 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3038 }
3039
3040 // FIXME: If we're merging, make sure that the protocol list is the same.
3041 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3042 ProtocolLocs.data(), Importer.getToContext());
3043
Douglas Gregor45635322010-02-16 01:20:57 +00003044 // Import @end range
3045 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3046 } else {
3047 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003048
3049 // Check for consistency of superclasses.
3050 DeclarationName FromSuperName, ToSuperName;
3051 if (D->getSuperClass())
3052 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3053 if (ToIface->getSuperClass())
3054 ToSuperName = ToIface->getSuperClass()->getDeclName();
3055 if (FromSuperName != ToSuperName) {
3056 Importer.ToDiag(ToIface->getLocation(),
3057 diag::err_odr_objc_superclass_inconsistent)
3058 << ToIface->getDeclName();
3059 if (ToIface->getSuperClass())
3060 Importer.ToDiag(ToIface->getSuperClassLoc(),
3061 diag::note_odr_objc_superclass)
3062 << ToIface->getSuperClass()->getDeclName();
3063 else
3064 Importer.ToDiag(ToIface->getLocation(),
3065 diag::note_odr_objc_missing_superclass);
3066 if (D->getSuperClass())
3067 Importer.FromDiag(D->getSuperClassLoc(),
3068 diag::note_odr_objc_superclass)
3069 << D->getSuperClass()->getDeclName();
3070 else
3071 Importer.FromDiag(D->getLocation(),
3072 diag::note_odr_objc_missing_superclass);
3073 return 0;
3074 }
Douglas Gregor45635322010-02-16 01:20:57 +00003075 }
3076
Douglas Gregor84c51c32010-02-18 01:47:50 +00003077 // Import categories. When the categories themselves are imported, they'll
3078 // hook themselves into this interface.
3079 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3080 FromCat = FromCat->getNextClassCategory())
3081 Importer.Import(FromCat);
3082
Douglas Gregor45635322010-02-16 01:20:57 +00003083 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003084 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003085
3086 // If we have an @implementation, import it as well.
3087 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003088 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3089 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003090 if (!Impl)
3091 return 0;
3092
3093 ToIface->setImplementation(Impl);
3094 }
3095
Douglas Gregor98d156a2010-02-17 16:12:00 +00003096 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003097}
3098
Douglas Gregor4da9d682010-12-07 15:32:12 +00003099Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3100 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3101 Importer.Import(D->getCategoryDecl()));
3102 if (!Category)
3103 return 0;
3104
3105 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3106 if (!ToImpl) {
3107 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3108 if (!DC)
3109 return 0;
3110
3111 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3112 Importer.Import(D->getLocation()),
3113 Importer.Import(D->getIdentifier()),
3114 Category->getClassInterface());
3115
3116 DeclContext *LexicalDC = DC;
3117 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3118 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3119 if (!LexicalDC)
3120 return 0;
3121
3122 ToImpl->setLexicalDeclContext(LexicalDC);
3123 }
3124
3125 LexicalDC->addDecl(ToImpl);
3126 Category->setImplementation(ToImpl);
3127 }
3128
3129 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003130 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003131 return ToImpl;
3132}
3133
Douglas Gregorda8025c2010-12-07 01:26:03 +00003134Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3135 // Find the corresponding interface.
3136 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3137 Importer.Import(D->getClassInterface()));
3138 if (!Iface)
3139 return 0;
3140
3141 // Import the superclass, if any.
3142 ObjCInterfaceDecl *Super = 0;
3143 if (D->getSuperClass()) {
3144 Super = cast_or_null<ObjCInterfaceDecl>(
3145 Importer.Import(D->getSuperClass()));
3146 if (!Super)
3147 return 0;
3148 }
3149
3150 ObjCImplementationDecl *Impl = Iface->getImplementation();
3151 if (!Impl) {
3152 // We haven't imported an implementation yet. Create a new @implementation
3153 // now.
3154 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3155 Importer.ImportContext(D->getDeclContext()),
3156 Importer.Import(D->getLocation()),
3157 Iface, Super);
3158
3159 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3160 DeclContext *LexicalDC
3161 = Importer.ImportContext(D->getLexicalDeclContext());
3162 if (!LexicalDC)
3163 return 0;
3164 Impl->setLexicalDeclContext(LexicalDC);
3165 }
3166
3167 // Associate the implementation with the class it implements.
3168 Iface->setImplementation(Impl);
3169 Importer.Imported(D, Iface->getImplementation());
3170 } else {
3171 Importer.Imported(D, Iface->getImplementation());
3172
3173 // Verify that the existing @implementation has the same superclass.
3174 if ((Super && !Impl->getSuperClass()) ||
3175 (!Super && Impl->getSuperClass()) ||
3176 (Super && Impl->getSuperClass() &&
3177 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3178 Importer.ToDiag(Impl->getLocation(),
3179 diag::err_odr_objc_superclass_inconsistent)
3180 << Iface->getDeclName();
3181 // FIXME: It would be nice to have the location of the superclass
3182 // below.
3183 if (Impl->getSuperClass())
3184 Importer.ToDiag(Impl->getLocation(),
3185 diag::note_odr_objc_superclass)
3186 << Impl->getSuperClass()->getDeclName();
3187 else
3188 Importer.ToDiag(Impl->getLocation(),
3189 diag::note_odr_objc_missing_superclass);
3190 if (D->getSuperClass())
3191 Importer.FromDiag(D->getLocation(),
3192 diag::note_odr_objc_superclass)
3193 << D->getSuperClass()->getDeclName();
3194 else
3195 Importer.FromDiag(D->getLocation(),
3196 diag::note_odr_objc_missing_superclass);
3197 return 0;
3198 }
3199 }
3200
3201 // Import all of the members of this @implementation.
3202 ImportDeclContext(D);
3203
3204 return Impl;
3205}
3206
Douglas Gregora11c4582010-02-17 18:02:10 +00003207Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3208 // Import the major distinguishing characteristics of an @property.
3209 DeclContext *DC, *LexicalDC;
3210 DeclarationName Name;
3211 SourceLocation Loc;
3212 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3213 return 0;
3214
3215 // Check whether we have already imported this property.
3216 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3217 Lookup.first != Lookup.second;
3218 ++Lookup.first) {
3219 if (ObjCPropertyDecl *FoundProp
3220 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3221 // Check property types.
3222 if (!Importer.IsStructurallyEquivalent(D->getType(),
3223 FoundProp->getType())) {
3224 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3225 << Name << D->getType() << FoundProp->getType();
3226 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3227 << FoundProp->getType();
3228 return 0;
3229 }
3230
3231 // FIXME: Check property attributes, getters, setters, etc.?
3232
3233 // Consider these properties to be equivalent.
3234 Importer.Imported(D, FoundProp);
3235 return FoundProp;
3236 }
3237 }
3238
3239 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003240 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3241 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003242 return 0;
3243
3244 // Create the new property.
3245 ObjCPropertyDecl *ToProperty
3246 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3247 Name.getAsIdentifierInfo(),
3248 Importer.Import(D->getAtLoc()),
3249 T,
3250 D->getPropertyImplementation());
3251 Importer.Imported(D, ToProperty);
3252 ToProperty->setLexicalDeclContext(LexicalDC);
3253 LexicalDC->addDecl(ToProperty);
3254
3255 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003256 ToProperty->setPropertyAttributesAsWritten(
3257 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003258 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3259 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3260 ToProperty->setGetterMethodDecl(
3261 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3262 ToProperty->setSetterMethodDecl(
3263 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3264 ToProperty->setPropertyIvarDecl(
3265 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3266 return ToProperty;
3267}
3268
Douglas Gregor14a49e22010-12-07 18:32:03 +00003269Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3270 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3271 Importer.Import(D->getPropertyDecl()));
3272 if (!Property)
3273 return 0;
3274
3275 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3276 if (!DC)
3277 return 0;
3278
3279 // Import the lexical declaration context.
3280 DeclContext *LexicalDC = DC;
3281 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3282 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3283 if (!LexicalDC)
3284 return 0;
3285 }
3286
3287 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3288 if (!InImpl)
3289 return 0;
3290
3291 // Import the ivar (for an @synthesize).
3292 ObjCIvarDecl *Ivar = 0;
3293 if (D->getPropertyIvarDecl()) {
3294 Ivar = cast_or_null<ObjCIvarDecl>(
3295 Importer.Import(D->getPropertyIvarDecl()));
3296 if (!Ivar)
3297 return 0;
3298 }
3299
3300 ObjCPropertyImplDecl *ToImpl
3301 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3302 if (!ToImpl) {
3303 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3304 Importer.Import(D->getLocStart()),
3305 Importer.Import(D->getLocation()),
3306 Property,
3307 D->getPropertyImplementation(),
3308 Ivar,
3309 Importer.Import(D->getPropertyIvarDeclLoc()));
3310 ToImpl->setLexicalDeclContext(LexicalDC);
3311 Importer.Imported(D, ToImpl);
3312 LexicalDC->addDecl(ToImpl);
3313 } else {
3314 // Check that we have the same kind of property implementation (@synthesize
3315 // vs. @dynamic).
3316 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3317 Importer.ToDiag(ToImpl->getLocation(),
3318 diag::err_odr_objc_property_impl_kind_inconsistent)
3319 << Property->getDeclName()
3320 << (ToImpl->getPropertyImplementation()
3321 == ObjCPropertyImplDecl::Dynamic);
3322 Importer.FromDiag(D->getLocation(),
3323 diag::note_odr_objc_property_impl_kind)
3324 << D->getPropertyDecl()->getDeclName()
3325 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3326 return 0;
3327 }
3328
3329 // For @synthesize, check that we have the same
3330 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3331 Ivar != ToImpl->getPropertyIvarDecl()) {
3332 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3333 diag::err_odr_objc_synthesize_ivar_inconsistent)
3334 << Property->getDeclName()
3335 << ToImpl->getPropertyIvarDecl()->getDeclName()
3336 << Ivar->getDeclName();
3337 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3338 diag::note_odr_objc_synthesize_ivar_here)
3339 << D->getPropertyIvarDecl()->getDeclName();
3340 return 0;
3341 }
3342
3343 // Merge the existing implementation with the new implementation.
3344 Importer.Imported(D, ToImpl);
3345 }
3346
3347 return ToImpl;
3348}
3349
Douglas Gregor8661a722010-02-18 02:12:22 +00003350Decl *
3351ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3352 // Import the context of this declaration.
3353 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3354 if (!DC)
3355 return 0;
3356
3357 DeclContext *LexicalDC = DC;
3358 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3359 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3360 if (!LexicalDC)
3361 return 0;
3362 }
3363
3364 // Import the location of this declaration.
3365 SourceLocation Loc = Importer.Import(D->getLocation());
3366
3367 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3368 llvm::SmallVector<SourceLocation, 4> Locations;
3369 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3370 = D->protocol_loc_begin();
3371 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3372 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3373 FromProto != FromProtoEnd;
3374 ++FromProto, ++FromProtoLoc) {
3375 ObjCProtocolDecl *ToProto
3376 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3377 if (!ToProto)
3378 continue;
3379
3380 Protocols.push_back(ToProto);
3381 Locations.push_back(Importer.Import(*FromProtoLoc));
3382 }
3383
3384 ObjCForwardProtocolDecl *ToForward
3385 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3386 Protocols.data(), Protocols.size(),
3387 Locations.data());
3388 ToForward->setLexicalDeclContext(LexicalDC);
3389 LexicalDC->addDecl(ToForward);
3390 Importer.Imported(D, ToForward);
3391 return ToForward;
3392}
3393
Douglas Gregor06537af2010-02-18 02:04:09 +00003394Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3395 // Import the context of this declaration.
3396 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3397 if (!DC)
3398 return 0;
3399
3400 DeclContext *LexicalDC = DC;
3401 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3402 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3403 if (!LexicalDC)
3404 return 0;
3405 }
3406
3407 // Import the location of this declaration.
3408 SourceLocation Loc = Importer.Import(D->getLocation());
3409
3410 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3411 llvm::SmallVector<SourceLocation, 4> Locations;
3412 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3413 From != FromEnd; ++From) {
3414 ObjCInterfaceDecl *ToIface
3415 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3416 if (!ToIface)
3417 continue;
3418
3419 Interfaces.push_back(ToIface);
3420 Locations.push_back(Importer.Import(From->getLocation()));
3421 }
3422
3423 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3424 Loc,
3425 Interfaces.data(),
3426 Locations.data(),
3427 Interfaces.size());
3428 ToClass->setLexicalDeclContext(LexicalDC);
3429 LexicalDC->addDecl(ToClass);
3430 Importer.Imported(D, ToClass);
3431 return ToClass;
3432}
3433
Douglas Gregora082a492010-11-30 19:14:50 +00003434Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3435 // For template arguments, we adopt the translation unit as our declaration
3436 // context. This context will be fixed when the actual template declaration
3437 // is created.
3438
3439 // FIXME: Import default argument.
3440 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3441 Importer.getToContext().getTranslationUnitDecl(),
3442 Importer.Import(D->getLocation()),
3443 D->getDepth(),
3444 D->getIndex(),
3445 Importer.Import(D->getIdentifier()),
3446 D->wasDeclaredWithTypename(),
3447 D->isParameterPack());
3448}
3449
3450Decl *
3451ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3452 // Import the name of this declaration.
3453 DeclarationName Name = Importer.Import(D->getDeclName());
3454 if (D->getDeclName() && !Name)
3455 return 0;
3456
3457 // Import the location of this declaration.
3458 SourceLocation Loc = Importer.Import(D->getLocation());
3459
3460 // Import the type of this declaration.
3461 QualType T = Importer.Import(D->getType());
3462 if (T.isNull())
3463 return 0;
3464
3465 // Import type-source information.
3466 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3467 if (D->getTypeSourceInfo() && !TInfo)
3468 return 0;
3469
3470 // FIXME: Import default argument.
3471
3472 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3473 Importer.getToContext().getTranslationUnitDecl(),
3474 Loc, D->getDepth(), D->getPosition(),
3475 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003476 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003477}
3478
3479Decl *
3480ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3481 // Import the name of this declaration.
3482 DeclarationName Name = Importer.Import(D->getDeclName());
3483 if (D->getDeclName() && !Name)
3484 return 0;
3485
3486 // Import the location of this declaration.
3487 SourceLocation Loc = Importer.Import(D->getLocation());
3488
3489 // Import template parameters.
3490 TemplateParameterList *TemplateParams
3491 = ImportTemplateParameterList(D->getTemplateParameters());
3492 if (!TemplateParams)
3493 return 0;
3494
3495 // FIXME: Import default argument.
3496
3497 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3498 Importer.getToContext().getTranslationUnitDecl(),
3499 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003500 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003501 Name.getAsIdentifierInfo(),
3502 TemplateParams);
3503}
3504
3505Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3506 // If this record has a definition in the translation unit we're coming from,
3507 // but this particular declaration is not that definition, import the
3508 // definition and map to that.
3509 CXXRecordDecl *Definition
3510 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3511 if (Definition && Definition != D->getTemplatedDecl()) {
3512 Decl *ImportedDef
3513 = Importer.Import(Definition->getDescribedClassTemplate());
3514 if (!ImportedDef)
3515 return 0;
3516
3517 return Importer.Imported(D, ImportedDef);
3518 }
3519
3520 // Import the major distinguishing characteristics of this class template.
3521 DeclContext *DC, *LexicalDC;
3522 DeclarationName Name;
3523 SourceLocation Loc;
3524 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3525 return 0;
3526
3527 // We may already have a template of the same name; try to find and match it.
3528 if (!DC->isFunctionOrMethod()) {
3529 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3530 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3531 Lookup.first != Lookup.second;
3532 ++Lookup.first) {
3533 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3534 continue;
3535
3536 Decl *Found = *Lookup.first;
3537 if (ClassTemplateDecl *FoundTemplate
3538 = dyn_cast<ClassTemplateDecl>(Found)) {
3539 if (IsStructuralMatch(D, FoundTemplate)) {
3540 // The class templates structurally match; call it the same template.
3541 // FIXME: We may be filling in a forward declaration here. Handle
3542 // this case!
3543 Importer.Imported(D->getTemplatedDecl(),
3544 FoundTemplate->getTemplatedDecl());
3545 return Importer.Imported(D, FoundTemplate);
3546 }
3547 }
3548
3549 ConflictingDecls.push_back(*Lookup.first);
3550 }
3551
3552 if (!ConflictingDecls.empty()) {
3553 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3554 ConflictingDecls.data(),
3555 ConflictingDecls.size());
3556 }
3557
3558 if (!Name)
3559 return 0;
3560 }
3561
3562 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3563
3564 // Create the declaration that is being templated.
3565 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3566 DTemplated->getTagKind(),
3567 DC,
3568 Importer.Import(DTemplated->getLocation()),
3569 Name.getAsIdentifierInfo(),
3570 Importer.Import(DTemplated->getTagKeywordLoc()));
3571 D2Templated->setAccess(DTemplated->getAccess());
3572
3573
3574 // Import the qualifier, if any.
3575 if (DTemplated->getQualifier()) {
3576 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3577 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3578 D2Templated->setQualifierInfo(NNS, NNSRange);
3579 }
3580 D2Templated->setLexicalDeclContext(LexicalDC);
3581
3582 // Create the class template declaration itself.
3583 TemplateParameterList *TemplateParams
3584 = ImportTemplateParameterList(D->getTemplateParameters());
3585 if (!TemplateParams)
3586 return 0;
3587
3588 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3589 Loc, Name, TemplateParams,
3590 D2Templated,
3591 /*PrevDecl=*/0);
3592 D2Templated->setDescribedClassTemplate(D2);
3593
3594 D2->setAccess(D->getAccess());
3595 D2->setLexicalDeclContext(LexicalDC);
3596 LexicalDC->addDecl(D2);
3597
3598 // Note the relationship between the class templates.
3599 Importer.Imported(D, D2);
3600 Importer.Imported(DTemplated, D2Templated);
3601
3602 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3603 // FIXME: Import definition!
3604 }
3605
3606 return D2;
3607}
3608
Douglas Gregore2e50d332010-12-01 01:36:18 +00003609Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3610 ClassTemplateSpecializationDecl *D) {
3611 // If this record has a definition in the translation unit we're coming from,
3612 // but this particular declaration is not that definition, import the
3613 // definition and map to that.
3614 TagDecl *Definition = D->getDefinition();
3615 if (Definition && Definition != D) {
3616 Decl *ImportedDef = Importer.Import(Definition);
3617 if (!ImportedDef)
3618 return 0;
3619
3620 return Importer.Imported(D, ImportedDef);
3621 }
3622
3623 ClassTemplateDecl *ClassTemplate
3624 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3625 D->getSpecializedTemplate()));
3626 if (!ClassTemplate)
3627 return 0;
3628
3629 // Import the context of this declaration.
3630 DeclContext *DC = ClassTemplate->getDeclContext();
3631 if (!DC)
3632 return 0;
3633
3634 DeclContext *LexicalDC = DC;
3635 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3636 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3637 if (!LexicalDC)
3638 return 0;
3639 }
3640
3641 // Import the location of this declaration.
3642 SourceLocation Loc = Importer.Import(D->getLocation());
3643
3644 // Import template arguments.
3645 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3646 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3647 D->getTemplateArgs().size(),
3648 TemplateArgs))
3649 return 0;
3650
3651 // Try to find an existing specialization with these template arguments.
3652 void *InsertPos = 0;
3653 ClassTemplateSpecializationDecl *D2
3654 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3655 TemplateArgs.size(), InsertPos);
3656 if (D2) {
3657 // We already have a class template specialization with these template
3658 // arguments.
3659
3660 // FIXME: Check for specialization vs. instantiation errors.
3661
3662 if (RecordDecl *FoundDef = D2->getDefinition()) {
3663 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3664 // The record types structurally match, or the "from" translation
3665 // unit only had a forward declaration anyway; call it the same
3666 // function.
3667 return Importer.Imported(D, FoundDef);
3668 }
3669 }
3670 } else {
3671 // Create a new specialization.
3672 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3673 D->getTagKind(), DC,
3674 Loc, ClassTemplate,
3675 TemplateArgs.data(),
3676 TemplateArgs.size(),
3677 /*PrevDecl=*/0);
3678 D2->setSpecializationKind(D->getSpecializationKind());
3679
3680 // Add this specialization to the class template.
3681 ClassTemplate->AddSpecialization(D2, InsertPos);
3682
3683 // Import the qualifier, if any.
3684 if (D->getQualifier()) {
3685 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3686 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3687 D2->setQualifierInfo(NNS, NNSRange);
3688 }
3689
3690
3691 // Add the specialization to this context.
3692 D2->setLexicalDeclContext(LexicalDC);
3693 LexicalDC->addDecl(D2);
3694 }
3695 Importer.Imported(D, D2);
3696
3697 if (D->isDefinition() && ImportDefinition(D, D2))
3698 return 0;
3699
3700 return D2;
3701}
3702
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003703//----------------------------------------------------------------------------
3704// Import Statements
3705//----------------------------------------------------------------------------
3706
3707Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3708 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3709 << S->getStmtClassName();
3710 return 0;
3711}
3712
3713//----------------------------------------------------------------------------
3714// Import Expressions
3715//----------------------------------------------------------------------------
3716Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3717 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3718 << E->getStmtClassName();
3719 return 0;
3720}
3721
Douglas Gregor52f820e2010-02-19 01:17:02 +00003722Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3723 NestedNameSpecifier *Qualifier = 0;
3724 if (E->getQualifier()) {
3725 Qualifier = Importer.Import(E->getQualifier());
3726 if (!E->getQualifier())
3727 return 0;
3728 }
3729
3730 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3731 if (!ToD)
3732 return 0;
3733
3734 QualType T = Importer.Import(E->getType());
3735 if (T.isNull())
3736 return 0;
3737
3738 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3739 Importer.Import(E->getQualifierRange()),
3740 ToD,
3741 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003742 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003743 /*FIXME:TemplateArgs=*/0);
3744}
3745
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003746Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3747 QualType T = Importer.Import(E->getType());
3748 if (T.isNull())
3749 return 0;
3750
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003751 return IntegerLiteral::Create(Importer.getToContext(),
3752 E->getValue(), T,
3753 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003754}
3755
Douglas Gregor623421d2010-02-18 02:21:22 +00003756Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3757 QualType T = Importer.Import(E->getType());
3758 if (T.isNull())
3759 return 0;
3760
3761 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3762 E->isWide(), T,
3763 Importer.Import(E->getLocation()));
3764}
3765
Douglas Gregorc74247e2010-02-19 01:07:06 +00003766Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3767 Expr *SubExpr = Importer.Import(E->getSubExpr());
3768 if (!SubExpr)
3769 return 0;
3770
3771 return new (Importer.getToContext())
3772 ParenExpr(Importer.Import(E->getLParen()),
3773 Importer.Import(E->getRParen()),
3774 SubExpr);
3775}
3776
3777Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3778 QualType T = Importer.Import(E->getType());
3779 if (T.isNull())
3780 return 0;
3781
3782 Expr *SubExpr = Importer.Import(E->getSubExpr());
3783 if (!SubExpr)
3784 return 0;
3785
3786 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003787 T, E->getValueKind(),
3788 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003789 Importer.Import(E->getOperatorLoc()));
3790}
3791
Douglas Gregord8552cd2010-02-19 01:24:23 +00003792Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3793 QualType ResultType = Importer.Import(E->getType());
3794
3795 if (E->isArgumentType()) {
3796 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3797 if (!TInfo)
3798 return 0;
3799
3800 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3801 TInfo, ResultType,
3802 Importer.Import(E->getOperatorLoc()),
3803 Importer.Import(E->getRParenLoc()));
3804 }
3805
3806 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3807 if (!SubExpr)
3808 return 0;
3809
3810 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3811 SubExpr, ResultType,
3812 Importer.Import(E->getOperatorLoc()),
3813 Importer.Import(E->getRParenLoc()));
3814}
3815
Douglas Gregorc74247e2010-02-19 01:07:06 +00003816Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3817 QualType T = Importer.Import(E->getType());
3818 if (T.isNull())
3819 return 0;
3820
3821 Expr *LHS = Importer.Import(E->getLHS());
3822 if (!LHS)
3823 return 0;
3824
3825 Expr *RHS = Importer.Import(E->getRHS());
3826 if (!RHS)
3827 return 0;
3828
3829 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003830 T, E->getValueKind(),
3831 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003832 Importer.Import(E->getOperatorLoc()));
3833}
3834
3835Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3836 QualType T = Importer.Import(E->getType());
3837 if (T.isNull())
3838 return 0;
3839
3840 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3841 if (CompLHSType.isNull())
3842 return 0;
3843
3844 QualType CompResultType = Importer.Import(E->getComputationResultType());
3845 if (CompResultType.isNull())
3846 return 0;
3847
3848 Expr *LHS = Importer.Import(E->getLHS());
3849 if (!LHS)
3850 return 0;
3851
3852 Expr *RHS = Importer.Import(E->getRHS());
3853 if (!RHS)
3854 return 0;
3855
3856 return new (Importer.getToContext())
3857 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003858 T, E->getValueKind(),
3859 E->getObjectKind(),
3860 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003861 Importer.Import(E->getOperatorLoc()));
3862}
3863
John McCallcf142162010-08-07 06:22:56 +00003864bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3865 if (E->path_empty()) return false;
3866
3867 // TODO: import cast paths
3868 return true;
3869}
3870
Douglas Gregor98c10182010-02-12 22:17:39 +00003871Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3872 QualType T = Importer.Import(E->getType());
3873 if (T.isNull())
3874 return 0;
3875
3876 Expr *SubExpr = Importer.Import(E->getSubExpr());
3877 if (!SubExpr)
3878 return 0;
John McCallcf142162010-08-07 06:22:56 +00003879
3880 CXXCastPath BasePath;
3881 if (ImportCastPath(E, BasePath))
3882 return 0;
3883
3884 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003885 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003886}
3887
Douglas Gregor5481d322010-02-19 01:32:14 +00003888Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3889 QualType T = Importer.Import(E->getType());
3890 if (T.isNull())
3891 return 0;
3892
3893 Expr *SubExpr = Importer.Import(E->getSubExpr());
3894 if (!SubExpr)
3895 return 0;
3896
3897 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3898 if (!TInfo && E->getTypeInfoAsWritten())
3899 return 0;
3900
John McCallcf142162010-08-07 06:22:56 +00003901 CXXCastPath BasePath;
3902 if (ImportCastPath(E, BasePath))
3903 return 0;
3904
John McCall7decc9e2010-11-18 06:31:45 +00003905 return CStyleCastExpr::Create(Importer.getToContext(), T,
3906 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003907 SubExpr, &BasePath, TInfo,
3908 Importer.Import(E->getLParenLoc()),
3909 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003910}
3911
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003912ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003913 ASTContext &FromContext, FileManager &FromFileManager,
3914 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003915 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003916 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3917 Minimal(MinimalImport)
3918{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003919 ImportedDecls[FromContext.getTranslationUnitDecl()]
3920 = ToContext.getTranslationUnitDecl();
3921}
3922
3923ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003924
3925QualType ASTImporter::Import(QualType FromT) {
3926 if (FromT.isNull())
3927 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003928
3929 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003930
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003931 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003932 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3933 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003934 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003935 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003936
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003937 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003938 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003939 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003940 if (ToT.isNull())
3941 return ToT;
3942
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003943 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003944 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003945
John McCall424cec92011-01-19 06:33:43 +00003946 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003947}
3948
Douglas Gregor62d311f2010-02-09 19:21:46 +00003949TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003950 if (!FromTSI)
3951 return FromTSI;
3952
3953 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003954 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003955 QualType T = Import(FromTSI->getType());
3956 if (T.isNull())
3957 return 0;
3958
3959 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003960 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003961}
3962
3963Decl *ASTImporter::Import(Decl *FromD) {
3964 if (!FromD)
3965 return 0;
3966
3967 // Check whether we've already imported this declaration.
3968 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3969 if (Pos != ImportedDecls.end())
3970 return Pos->second;
3971
3972 // Import the type
3973 ASTNodeImporter Importer(*this);
3974 Decl *ToD = Importer.Visit(FromD);
3975 if (!ToD)
3976 return 0;
3977
3978 // Record the imported declaration.
3979 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003980
3981 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3982 // Keep track of anonymous tags that have an associated typedef.
3983 if (FromTag->getTypedefForAnonDecl())
3984 AnonTagsWithPendingTypedefs.push_back(FromTag);
3985 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3986 // When we've finished transforming a typedef, see whether it was the
3987 // typedef for an anonymous tag.
3988 for (llvm::SmallVector<TagDecl *, 4>::iterator
3989 FromTag = AnonTagsWithPendingTypedefs.begin(),
3990 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3991 FromTag != FromTagEnd; ++FromTag) {
3992 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3993 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3994 // We found the typedef for an anonymous tag; link them.
3995 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3996 AnonTagsWithPendingTypedefs.erase(FromTag);
3997 break;
3998 }
3999 }
4000 }
4001 }
4002
Douglas Gregor62d311f2010-02-09 19:21:46 +00004003 return ToD;
4004}
4005
4006DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4007 if (!FromDC)
4008 return FromDC;
4009
4010 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4011}
4012
4013Expr *ASTImporter::Import(Expr *FromE) {
4014 if (!FromE)
4015 return 0;
4016
4017 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4018}
4019
4020Stmt *ASTImporter::Import(Stmt *FromS) {
4021 if (!FromS)
4022 return 0;
4023
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004024 // Check whether we've already imported this declaration.
4025 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4026 if (Pos != ImportedStmts.end())
4027 return Pos->second;
4028
4029 // Import the type
4030 ASTNodeImporter Importer(*this);
4031 Stmt *ToS = Importer.Visit(FromS);
4032 if (!ToS)
4033 return 0;
4034
4035 // Record the imported declaration.
4036 ImportedStmts[FromS] = ToS;
4037 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004038}
4039
4040NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4041 if (!FromNNS)
4042 return 0;
4043
4044 // FIXME: Implement!
4045 return 0;
4046}
4047
Douglas Gregore2e50d332010-12-01 01:36:18 +00004048TemplateName ASTImporter::Import(TemplateName From) {
4049 switch (From.getKind()) {
4050 case TemplateName::Template:
4051 if (TemplateDecl *ToTemplate
4052 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4053 return TemplateName(ToTemplate);
4054
4055 return TemplateName();
4056
4057 case TemplateName::OverloadedTemplate: {
4058 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4059 UnresolvedSet<2> ToTemplates;
4060 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4061 E = FromStorage->end();
4062 I != E; ++I) {
4063 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4064 ToTemplates.addDecl(To);
4065 else
4066 return TemplateName();
4067 }
4068 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4069 ToTemplates.end());
4070 }
4071
4072 case TemplateName::QualifiedTemplate: {
4073 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4074 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4075 if (!Qualifier)
4076 return TemplateName();
4077
4078 if (TemplateDecl *ToTemplate
4079 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4080 return ToContext.getQualifiedTemplateName(Qualifier,
4081 QTN->hasTemplateKeyword(),
4082 ToTemplate);
4083
4084 return TemplateName();
4085 }
4086
4087 case TemplateName::DependentTemplate: {
4088 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4089 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4090 if (!Qualifier)
4091 return TemplateName();
4092
4093 if (DTN->isIdentifier()) {
4094 return ToContext.getDependentTemplateName(Qualifier,
4095 Import(DTN->getIdentifier()));
4096 }
4097
4098 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4099 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004100
4101 case TemplateName::SubstTemplateTemplateParmPack: {
4102 SubstTemplateTemplateParmPackStorage *SubstPack
4103 = From.getAsSubstTemplateTemplateParmPack();
4104 TemplateTemplateParmDecl *Param
4105 = cast_or_null<TemplateTemplateParmDecl>(
4106 Import(SubstPack->getParameterPack()));
4107 if (!Param)
4108 return TemplateName();
4109
4110 ASTNodeImporter Importer(*this);
4111 TemplateArgument ArgPack
4112 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4113 if (ArgPack.isNull())
4114 return TemplateName();
4115
4116 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4117 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004118 }
4119
4120 llvm_unreachable("Invalid template name kind");
4121 return TemplateName();
4122}
4123
Douglas Gregor62d311f2010-02-09 19:21:46 +00004124SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4125 if (FromLoc.isInvalid())
4126 return SourceLocation();
4127
Douglas Gregor811663e2010-02-10 00:15:17 +00004128 SourceManager &FromSM = FromContext.getSourceManager();
4129
4130 // For now, map everything down to its spelling location, so that we
4131 // don't have to import macro instantiations.
4132 // FIXME: Import macro instantiations!
4133 FromLoc = FromSM.getSpellingLoc(FromLoc);
4134 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4135 SourceManager &ToSM = ToContext.getSourceManager();
4136 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4137 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004138}
4139
4140SourceRange ASTImporter::Import(SourceRange FromRange) {
4141 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4142}
4143
Douglas Gregor811663e2010-02-10 00:15:17 +00004144FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004145 llvm::DenseMap<FileID, FileID>::iterator Pos
4146 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004147 if (Pos != ImportedFileIDs.end())
4148 return Pos->second;
4149
4150 SourceManager &FromSM = FromContext.getSourceManager();
4151 SourceManager &ToSM = ToContext.getSourceManager();
4152 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4153 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4154
4155 // Include location of this file.
4156 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4157
4158 // Map the FileID for to the "to" source manager.
4159 FileID ToID;
4160 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4161 if (Cache->Entry) {
4162 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4163 // disk again
4164 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4165 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00004166 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004167 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4168 FromSLoc.getFile().getFileCharacteristic());
4169 } else {
4170 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004171 const llvm::MemoryBuffer *
4172 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004173 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004174 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004175 FromBuf->getBufferIdentifier());
4176 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4177 }
4178
4179
Sebastian Redl99219f12010-09-30 01:03:06 +00004180 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004181 return ToID;
4182}
4183
Douglas Gregor0a791672011-01-18 03:11:38 +00004184void ASTImporter::ImportDefinition(Decl *From) {
4185 Decl *To = Import(From);
4186 if (!To)
4187 return;
4188
4189 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4190 ASTNodeImporter Importer(*this);
4191 Importer.ImportDeclContext(FromDC, true);
4192 }
4193}
4194
Douglas Gregor96e578d2010-02-05 17:54:41 +00004195DeclarationName ASTImporter::Import(DeclarationName FromName) {
4196 if (!FromName)
4197 return DeclarationName();
4198
4199 switch (FromName.getNameKind()) {
4200 case DeclarationName::Identifier:
4201 return Import(FromName.getAsIdentifierInfo());
4202
4203 case DeclarationName::ObjCZeroArgSelector:
4204 case DeclarationName::ObjCOneArgSelector:
4205 case DeclarationName::ObjCMultiArgSelector:
4206 return Import(FromName.getObjCSelector());
4207
4208 case DeclarationName::CXXConstructorName: {
4209 QualType T = Import(FromName.getCXXNameType());
4210 if (T.isNull())
4211 return DeclarationName();
4212
4213 return ToContext.DeclarationNames.getCXXConstructorName(
4214 ToContext.getCanonicalType(T));
4215 }
4216
4217 case DeclarationName::CXXDestructorName: {
4218 QualType T = Import(FromName.getCXXNameType());
4219 if (T.isNull())
4220 return DeclarationName();
4221
4222 return ToContext.DeclarationNames.getCXXDestructorName(
4223 ToContext.getCanonicalType(T));
4224 }
4225
4226 case DeclarationName::CXXConversionFunctionName: {
4227 QualType T = Import(FromName.getCXXNameType());
4228 if (T.isNull())
4229 return DeclarationName();
4230
4231 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4232 ToContext.getCanonicalType(T));
4233 }
4234
4235 case DeclarationName::CXXOperatorName:
4236 return ToContext.DeclarationNames.getCXXOperatorName(
4237 FromName.getCXXOverloadedOperator());
4238
4239 case DeclarationName::CXXLiteralOperatorName:
4240 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4241 Import(FromName.getCXXLiteralIdentifier()));
4242
4243 case DeclarationName::CXXUsingDirective:
4244 // FIXME: STATICS!
4245 return DeclarationName::getUsingDirectiveName();
4246 }
4247
4248 // Silence bogus GCC warning
4249 return DeclarationName();
4250}
4251
Douglas Gregore2e50d332010-12-01 01:36:18 +00004252IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004253 if (!FromId)
4254 return 0;
4255
4256 return &ToContext.Idents.get(FromId->getName());
4257}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004258
Douglas Gregor43f54792010-02-17 02:12:47 +00004259Selector ASTImporter::Import(Selector FromSel) {
4260 if (FromSel.isNull())
4261 return Selector();
4262
4263 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4264 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4265 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4266 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4267 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4268}
4269
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004270DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4271 DeclContext *DC,
4272 unsigned IDNS,
4273 NamedDecl **Decls,
4274 unsigned NumDecls) {
4275 return Name;
4276}
4277
4278DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004279 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004280}
4281
4282DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004283 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004284}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004285
4286Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4287 ImportedDecls[From] = To;
4288 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004289}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004290
4291bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004292 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004293 = ImportedTypes.find(From.getTypePtr());
4294 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4295 return true;
4296
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004297 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004298 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004299}