blob: 315f4feca67f4a45791618e22b2400918d11284b [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000042
43 // Importing types
Douglas Gregore4c83e42010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000045 QualType VisitBuiltinType(BuiltinType *T);
46 QualType VisitComplexType(ComplexType *T);
47 QualType VisitPointerType(PointerType *T);
48 QualType VisitBlockPointerType(BlockPointerType *T);
49 QualType VisitLValueReferenceType(LValueReferenceType *T);
50 QualType VisitRValueReferenceType(RValueReferenceType *T);
51 QualType VisitMemberPointerType(MemberPointerType *T);
52 QualType VisitConstantArrayType(ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
54 QualType VisitVariableArrayType(VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(VectorType *T);
58 QualType VisitExtVectorType(ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitTypedefType(TypedefType *T);
63 QualType VisitTypeOfExprType(TypeOfExprType *T);
64 // FIXME: DependentTypeOfExprType
65 QualType VisitTypeOfType(TypeOfType *T);
66 QualType VisitDecltypeType(DecltypeType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(RecordType *T);
69 QualType VisitEnumType(EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
Douglas Gregore2e50d332010-12-01 01:36:18 +000072 QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
Abramo Bagnara6150c882010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor96e578d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCall8b07ec22010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas 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
Douglas Gregore4c83e42010-02-09 22:48:33 +00001282QualType ASTNodeImporter::VisitType(Type *T) {
1283 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1284 << T->getTypeClassName();
1285 return QualType();
1286}
1287
Douglas Gregor96e578d2010-02-05 17:54:41 +00001288QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1289 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
1368QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1369 QualType ToElementType = Importer.Import(T->getElementType());
1370 if (ToElementType.isNull())
1371 return QualType();
1372
1373 return Importer.getToContext().getComplexType(ToElementType);
1374}
1375
1376QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1377 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1378 if (ToPointeeType.isNull())
1379 return QualType();
1380
1381 return Importer.getToContext().getPointerType(ToPointeeType);
1382}
1383
1384QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1385 // 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
1393QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1394 // FIXME: Check for C++ support in "to" context.
1395 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1396 if (ToPointeeType.isNull())
1397 return QualType();
1398
1399 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1400}
1401
1402QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1403 // FIXME: Check for C++0x support in "to" context.
1404 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1405 if (ToPointeeType.isNull())
1406 return QualType();
1407
1408 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1409}
1410
1411QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1412 // FIXME: Check for C++ support in "to" context.
1413 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1414 if (ToPointeeType.isNull())
1415 return QualType();
1416
1417 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1418 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1419 ClassType.getTypePtr());
1420}
1421
1422QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1423 QualType ToElementType = Importer.Import(T->getElementType());
1424 if (ToElementType.isNull())
1425 return QualType();
1426
1427 return Importer.getToContext().getConstantArrayType(ToElementType,
1428 T->getSize(),
1429 T->getSizeModifier(),
1430 T->getIndexTypeCVRQualifiers());
1431}
1432
1433QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1434 QualType ToElementType = Importer.Import(T->getElementType());
1435 if (ToElementType.isNull())
1436 return QualType();
1437
1438 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1439 T->getSizeModifier(),
1440 T->getIndexTypeCVRQualifiers());
1441}
1442
1443QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1444 QualType ToElementType = Importer.Import(T->getElementType());
1445 if (ToElementType.isNull())
1446 return QualType();
1447
1448 Expr *Size = Importer.Import(T->getSizeExpr());
1449 if (!Size)
1450 return QualType();
1451
1452 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1453 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1454 T->getSizeModifier(),
1455 T->getIndexTypeCVRQualifiers(),
1456 Brackets);
1457}
1458
1459QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1460 QualType ToElementType = Importer.Import(T->getElementType());
1461 if (ToElementType.isNull())
1462 return QualType();
1463
1464 return Importer.getToContext().getVectorType(ToElementType,
1465 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001466 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001467}
1468
1469QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1470 QualType ToElementType = Importer.Import(T->getElementType());
1471 if (ToElementType.isNull())
1472 return QualType();
1473
1474 return Importer.getToContext().getExtVectorType(ToElementType,
1475 T->getNumElements());
1476}
1477
1478QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1479 // FIXME: What happens if we're importing a function without a prototype
1480 // into C++? Should we make it variadic?
1481 QualType ToResultType = Importer.Import(T->getResultType());
1482 if (ToResultType.isNull())
1483 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001484
Douglas Gregor96e578d2010-02-05 17:54:41 +00001485 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001486 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001487}
1488
1489QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1490 QualType ToResultType = Importer.Import(T->getResultType());
1491 if (ToResultType.isNull())
1492 return QualType();
1493
1494 // Import argument types
1495 llvm::SmallVector<QualType, 4> ArgTypes;
1496 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1497 AEnd = T->arg_type_end();
1498 A != AEnd; ++A) {
1499 QualType ArgType = Importer.Import(*A);
1500 if (ArgType.isNull())
1501 return QualType();
1502 ArgTypes.push_back(ArgType);
1503 }
1504
1505 // Import exception types
1506 llvm::SmallVector<QualType, 4> ExceptionTypes;
1507 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1508 EEnd = T->exception_end();
1509 E != EEnd; ++E) {
1510 QualType ExceptionType = Importer.Import(*E);
1511 if (ExceptionType.isNull())
1512 return QualType();
1513 ExceptionTypes.push_back(ExceptionType);
1514 }
John McCalldb40c7f2010-12-14 08:05:40 +00001515
1516 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1517 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001518
1519 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001520 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001521}
1522
1523QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1524 TypedefDecl *ToDecl
1525 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1526 if (!ToDecl)
1527 return QualType();
1528
1529 return Importer.getToContext().getTypeDeclType(ToDecl);
1530}
1531
1532QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1533 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1534 if (!ToExpr)
1535 return QualType();
1536
1537 return Importer.getToContext().getTypeOfExprType(ToExpr);
1538}
1539
1540QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1541 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1542 if (ToUnderlyingType.isNull())
1543 return QualType();
1544
1545 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1546}
1547
1548QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1549 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1550 if (!ToExpr)
1551 return QualType();
1552
1553 return Importer.getToContext().getDecltypeType(ToExpr);
1554}
1555
1556QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1557 RecordDecl *ToDecl
1558 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1559 if (!ToDecl)
1560 return QualType();
1561
1562 return Importer.getToContext().getTagDeclType(ToDecl);
1563}
1564
1565QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1566 EnumDecl *ToDecl
1567 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1568 if (!ToDecl)
1569 return QualType();
1570
1571 return Importer.getToContext().getTagDeclType(ToDecl);
1572}
1573
Douglas Gregore2e50d332010-12-01 01:36:18 +00001574QualType ASTNodeImporter::VisitTemplateSpecializationType(
1575 TemplateSpecializationType *T) {
1576 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1577 if (ToTemplate.isNull())
1578 return QualType();
1579
1580 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1581 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1582 return QualType();
1583
1584 QualType ToCanonType;
1585 if (!QualType(T, 0).isCanonical()) {
1586 QualType FromCanonType
1587 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1588 ToCanonType =Importer.Import(FromCanonType);
1589 if (ToCanonType.isNull())
1590 return QualType();
1591 }
1592 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1593 ToTemplateArgs.data(),
1594 ToTemplateArgs.size(),
1595 ToCanonType);
1596}
1597
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001599 NestedNameSpecifier *ToQualifier = 0;
1600 // Note: the qualifier in an ElaboratedType is optional.
1601 if (T->getQualifier()) {
1602 ToQualifier = Importer.Import(T->getQualifier());
1603 if (!ToQualifier)
1604 return QualType();
1605 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606
1607 QualType ToNamedType = Importer.Import(T->getNamedType());
1608 if (ToNamedType.isNull())
1609 return QualType();
1610
Abramo Bagnara6150c882010-05-11 21:36:43 +00001611 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1612 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001613}
1614
1615QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1616 ObjCInterfaceDecl *Class
1617 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1618 if (!Class)
1619 return QualType();
1620
John McCall8b07ec22010-05-15 11:32:37 +00001621 return Importer.getToContext().getObjCInterfaceType(Class);
1622}
1623
1624QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1625 QualType ToBaseType = Importer.Import(T->getBaseType());
1626 if (ToBaseType.isNull())
1627 return QualType();
1628
Douglas Gregor96e578d2010-02-05 17:54:41 +00001629 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001630 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001631 PEnd = T->qual_end();
1632 P != PEnd; ++P) {
1633 ObjCProtocolDecl *Protocol
1634 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1635 if (!Protocol)
1636 return QualType();
1637 Protocols.push_back(Protocol);
1638 }
1639
John McCall8b07ec22010-05-15 11:32:37 +00001640 return Importer.getToContext().getObjCObjectType(ToBaseType,
1641 Protocols.data(),
1642 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001643}
1644
1645QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1646 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1647 if (ToPointeeType.isNull())
1648 return QualType();
1649
John McCall8b07ec22010-05-15 11:32:37 +00001650 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001651}
1652
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001653//----------------------------------------------------------------------------
1654// Import Declarations
1655//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001656bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1657 DeclContext *&LexicalDC,
1658 DeclarationName &Name,
1659 SourceLocation &Loc) {
1660 // Import the context of this declaration.
1661 DC = Importer.ImportContext(D->getDeclContext());
1662 if (!DC)
1663 return true;
1664
1665 LexicalDC = DC;
1666 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1667 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1668 if (!LexicalDC)
1669 return true;
1670 }
1671
1672 // Import the name of this declaration.
1673 Name = Importer.Import(D->getDeclName());
1674 if (D->getDeclName() && !Name)
1675 return true;
1676
1677 // Import the location of this declaration.
1678 Loc = Importer.Import(D->getLocation());
1679 return false;
1680}
1681
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001682void
1683ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1684 DeclarationNameInfo& To) {
1685 // NOTE: To.Name and To.Loc are already imported.
1686 // We only have to import To.LocInfo.
1687 switch (To.getName().getNameKind()) {
1688 case DeclarationName::Identifier:
1689 case DeclarationName::ObjCZeroArgSelector:
1690 case DeclarationName::ObjCOneArgSelector:
1691 case DeclarationName::ObjCMultiArgSelector:
1692 case DeclarationName::CXXUsingDirective:
1693 return;
1694
1695 case DeclarationName::CXXOperatorName: {
1696 SourceRange Range = From.getCXXOperatorNameRange();
1697 To.setCXXOperatorNameRange(Importer.Import(Range));
1698 return;
1699 }
1700 case DeclarationName::CXXLiteralOperatorName: {
1701 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1702 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1703 return;
1704 }
1705 case DeclarationName::CXXConstructorName:
1706 case DeclarationName::CXXDestructorName:
1707 case DeclarationName::CXXConversionFunctionName: {
1708 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1709 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1710 return;
1711 }
1712 assert(0 && "Unknown name kind.");
1713 }
1714}
1715
Douglas Gregor0a791672011-01-18 03:11:38 +00001716void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1717 if (Importer.isMinimalImport() && !ForceImport) {
1718 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1719 ToDC->setHasExternalLexicalStorage();
1720 ToDC->setHasExternalVisibleStorage();
1721 }
1722 return;
1723 }
1724
Douglas Gregor968d6332010-02-21 18:24:45 +00001725 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1726 FromEnd = FromDC->decls_end();
1727 From != FromEnd;
1728 ++From)
1729 Importer.Import(*From);
1730}
1731
Douglas Gregore2e50d332010-12-01 01:36:18 +00001732bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1733 if (To->getDefinition())
1734 return false;
1735
1736 To->startDefinition();
1737
1738 // Add base classes.
1739 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1740 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1741
1742 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1743 for (CXXRecordDecl::base_class_iterator
1744 Base1 = FromCXX->bases_begin(),
1745 FromBaseEnd = FromCXX->bases_end();
1746 Base1 != FromBaseEnd;
1747 ++Base1) {
1748 QualType T = Importer.Import(Base1->getType());
1749 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001750 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001751
1752 SourceLocation EllipsisLoc;
1753 if (Base1->isPackExpansion())
1754 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001755
1756 Bases.push_back(
1757 new (Importer.getToContext())
1758 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1759 Base1->isVirtual(),
1760 Base1->isBaseOfClass(),
1761 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001762 Importer.Import(Base1->getTypeSourceInfo()),
1763 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001764 }
1765 if (!Bases.empty())
1766 ToCXX->setBases(Bases.data(), Bases.size());
1767 }
1768
1769 ImportDeclContext(From);
1770 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001771 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001772}
1773
Douglas Gregora082a492010-11-30 19:14:50 +00001774TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1775 TemplateParameterList *Params) {
1776 llvm::SmallVector<NamedDecl *, 4> ToParams;
1777 ToParams.reserve(Params->size());
1778 for (TemplateParameterList::iterator P = Params->begin(),
1779 PEnd = Params->end();
1780 P != PEnd; ++P) {
1781 Decl *To = Importer.Import(*P);
1782 if (!To)
1783 return 0;
1784
1785 ToParams.push_back(cast<NamedDecl>(To));
1786 }
1787
1788 return TemplateParameterList::Create(Importer.getToContext(),
1789 Importer.Import(Params->getTemplateLoc()),
1790 Importer.Import(Params->getLAngleLoc()),
1791 ToParams.data(), ToParams.size(),
1792 Importer.Import(Params->getRAngleLoc()));
1793}
1794
Douglas Gregore2e50d332010-12-01 01:36:18 +00001795TemplateArgument
1796ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1797 switch (From.getKind()) {
1798 case TemplateArgument::Null:
1799 return TemplateArgument();
1800
1801 case TemplateArgument::Type: {
1802 QualType ToType = Importer.Import(From.getAsType());
1803 if (ToType.isNull())
1804 return TemplateArgument();
1805 return TemplateArgument(ToType);
1806 }
1807
1808 case TemplateArgument::Integral: {
1809 QualType ToType = Importer.Import(From.getIntegralType());
1810 if (ToType.isNull())
1811 return TemplateArgument();
1812 return TemplateArgument(*From.getAsIntegral(), ToType);
1813 }
1814
1815 case TemplateArgument::Declaration:
1816 if (Decl *To = Importer.Import(From.getAsDecl()))
1817 return TemplateArgument(To);
1818 return TemplateArgument();
1819
1820 case TemplateArgument::Template: {
1821 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1822 if (ToTemplate.isNull())
1823 return TemplateArgument();
1824
1825 return TemplateArgument(ToTemplate);
1826 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001827
1828 case TemplateArgument::TemplateExpansion: {
1829 TemplateName ToTemplate
1830 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1831 if (ToTemplate.isNull())
1832 return TemplateArgument();
1833
Douglas Gregore1d60df2011-01-14 23:41:42 +00001834 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001835 }
1836
Douglas Gregore2e50d332010-12-01 01:36:18 +00001837 case TemplateArgument::Expression:
1838 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1839 return TemplateArgument(ToExpr);
1840 return TemplateArgument();
1841
1842 case TemplateArgument::Pack: {
1843 llvm::SmallVector<TemplateArgument, 2> ToPack;
1844 ToPack.reserve(From.pack_size());
1845 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1846 return TemplateArgument();
1847
1848 TemplateArgument *ToArgs
1849 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1850 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1851 return TemplateArgument(ToArgs, ToPack.size());
1852 }
1853 }
1854
1855 llvm_unreachable("Invalid template argument kind");
1856 return TemplateArgument();
1857}
1858
1859bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1860 unsigned NumFromArgs,
1861 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1862 for (unsigned I = 0; I != NumFromArgs; ++I) {
1863 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1864 if (To.isNull() && !FromArgs[I].isNull())
1865 return true;
1866
1867 ToArgs.push_back(To);
1868 }
1869
1870 return false;
1871}
1872
Douglas Gregor5c73e912010-02-11 00:48:18 +00001873bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001874 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001875 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001876 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001877 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001878 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001879}
1880
Douglas Gregor98c10182010-02-12 22:17:39 +00001881bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001882 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001883 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001884 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001885 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001886}
1887
Douglas Gregora082a492010-11-30 19:14:50 +00001888bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1889 ClassTemplateDecl *To) {
1890 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1891 Importer.getToContext(),
1892 Importer.getNonEquivalentDecls());
1893 return Ctx.IsStructurallyEquivalent(From, To);
1894}
1895
Douglas Gregore4c83e42010-02-09 22:48:33 +00001896Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001897 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001898 << D->getDeclKindName();
1899 return 0;
1900}
1901
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001902Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1903 // Import the major distinguishing characteristics of this namespace.
1904 DeclContext *DC, *LexicalDC;
1905 DeclarationName Name;
1906 SourceLocation Loc;
1907 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1908 return 0;
1909
1910 NamespaceDecl *MergeWithNamespace = 0;
1911 if (!Name) {
1912 // This is an anonymous namespace. Adopt an existing anonymous
1913 // namespace if we can.
1914 // FIXME: Not testable.
1915 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1916 MergeWithNamespace = TU->getAnonymousNamespace();
1917 else
1918 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1919 } else {
1920 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1921 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1922 Lookup.first != Lookup.second;
1923 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001924 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001925 continue;
1926
1927 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1928 MergeWithNamespace = FoundNS;
1929 ConflictingDecls.clear();
1930 break;
1931 }
1932
1933 ConflictingDecls.push_back(*Lookup.first);
1934 }
1935
1936 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001937 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001938 ConflictingDecls.data(),
1939 ConflictingDecls.size());
1940 }
1941 }
1942
1943 // Create the "to" namespace, if needed.
1944 NamespaceDecl *ToNamespace = MergeWithNamespace;
1945 if (!ToNamespace) {
1946 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1947 Name.getAsIdentifierInfo());
1948 ToNamespace->setLexicalDeclContext(LexicalDC);
1949 LexicalDC->addDecl(ToNamespace);
1950
1951 // If this is an anonymous namespace, register it as the anonymous
1952 // namespace within its context.
1953 if (!Name) {
1954 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1955 TU->setAnonymousNamespace(ToNamespace);
1956 else
1957 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1958 }
1959 }
1960 Importer.Imported(D, ToNamespace);
1961
1962 ImportDeclContext(D);
1963
1964 return ToNamespace;
1965}
1966
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001967Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1968 // Import the major distinguishing characteristics of this typedef.
1969 DeclContext *DC, *LexicalDC;
1970 DeclarationName Name;
1971 SourceLocation Loc;
1972 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1973 return 0;
1974
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001975 // If this typedef is not in block scope, determine whether we've
1976 // seen a typedef with the same name (that we can merge with) or any
1977 // other entity by that name (which name lookup could conflict with).
1978 if (!DC->isFunctionOrMethod()) {
1979 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1980 unsigned IDNS = Decl::IDNS_Ordinary;
1981 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1982 Lookup.first != Lookup.second;
1983 ++Lookup.first) {
1984 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1985 continue;
1986 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001987 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1988 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001989 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001990 }
1991
1992 ConflictingDecls.push_back(*Lookup.first);
1993 }
1994
1995 if (!ConflictingDecls.empty()) {
1996 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1997 ConflictingDecls.data(),
1998 ConflictingDecls.size());
1999 if (!Name)
2000 return 0;
2001 }
2002 }
2003
Douglas Gregorb4964f72010-02-15 23:54:17 +00002004 // Import the underlying type of this typedef;
2005 QualType T = Importer.Import(D->getUnderlyingType());
2006 if (T.isNull())
2007 return 0;
2008
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002009 // Create the new typedef node.
2010 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2011 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2012 Loc, Name.getAsIdentifierInfo(),
2013 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002014 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002015 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002016 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002017 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002018
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002019 return ToTypedef;
2020}
2021
Douglas Gregor98c10182010-02-12 22:17:39 +00002022Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2023 // Import the major distinguishing characteristics of this enum.
2024 DeclContext *DC, *LexicalDC;
2025 DeclarationName Name;
2026 SourceLocation Loc;
2027 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2028 return 0;
2029
2030 // Figure out what enum name we're looking for.
2031 unsigned IDNS = Decl::IDNS_Tag;
2032 DeclarationName SearchName = Name;
2033 if (!SearchName && D->getTypedefForAnonDecl()) {
2034 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2035 IDNS = Decl::IDNS_Ordinary;
2036 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2037 IDNS |= Decl::IDNS_Ordinary;
2038
2039 // We may already have an enum of the same name; try to find and match it.
2040 if (!DC->isFunctionOrMethod() && SearchName) {
2041 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2042 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2043 Lookup.first != Lookup.second;
2044 ++Lookup.first) {
2045 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2046 continue;
2047
2048 Decl *Found = *Lookup.first;
2049 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2050 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2051 Found = Tag->getDecl();
2052 }
2053
2054 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002055 if (IsStructuralMatch(D, FoundEnum))
2056 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002057 }
2058
2059 ConflictingDecls.push_back(*Lookup.first);
2060 }
2061
2062 if (!ConflictingDecls.empty()) {
2063 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2064 ConflictingDecls.data(),
2065 ConflictingDecls.size());
2066 }
2067 }
2068
2069 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002070 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002071 Name.getAsIdentifierInfo(),
2072 Importer.Import(D->getTagKeywordLoc()), 0,
2073 D->isScoped(), D->isScopedUsingClassTag(),
2074 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002075 // Import the qualifier, if any.
2076 if (D->getQualifier()) {
2077 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2078 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2079 D2->setQualifierInfo(NNS, NNSRange);
2080 }
Douglas Gregordd483172010-02-22 17:42:47 +00002081 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002082 D2->setLexicalDeclContext(LexicalDC);
2083 Importer.Imported(D, D2);
2084 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002085
2086 // Import the integer type.
2087 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2088 if (ToIntegerType.isNull())
2089 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002090 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002091
2092 // Import the definition
2093 if (D->isDefinition()) {
2094 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2095 if (T.isNull())
2096 return 0;
2097
2098 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2099 if (ToPromotionType.isNull())
2100 return 0;
2101
Douglas Gregor3996e242010-02-15 22:01:00 +00002102 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002103 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002104
2105 // FIXME: we might need to merge the number of positive or negative bits
2106 // if the enumerator lists don't match.
2107 D2->completeDefinition(T, ToPromotionType,
2108 D->getNumPositiveBits(),
2109 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002110 }
2111
Douglas Gregor3996e242010-02-15 22:01:00 +00002112 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002113}
2114
Douglas Gregor5c73e912010-02-11 00:48:18 +00002115Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2116 // If this record has a definition in the translation unit we're coming from,
2117 // but this particular declaration is not that definition, import the
2118 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002119 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002120 if (Definition && Definition != D) {
2121 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002122 if (!ImportedDef)
2123 return 0;
2124
2125 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002126 }
2127
2128 // Import the major distinguishing characteristics of this record.
2129 DeclContext *DC, *LexicalDC;
2130 DeclarationName Name;
2131 SourceLocation Loc;
2132 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2133 return 0;
2134
2135 // Figure out what structure name we're looking for.
2136 unsigned IDNS = Decl::IDNS_Tag;
2137 DeclarationName SearchName = Name;
2138 if (!SearchName && D->getTypedefForAnonDecl()) {
2139 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2140 IDNS = Decl::IDNS_Ordinary;
2141 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2142 IDNS |= Decl::IDNS_Ordinary;
2143
2144 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002145 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002146 if (!DC->isFunctionOrMethod() && SearchName) {
2147 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2148 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2149 Lookup.first != Lookup.second;
2150 ++Lookup.first) {
2151 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2152 continue;
2153
2154 Decl *Found = *Lookup.first;
2155 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2156 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2157 Found = Tag->getDecl();
2158 }
2159
2160 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002161 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2162 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2163 // The record types structurally match, or the "from" translation
2164 // unit only had a forward declaration anyway; call it the same
2165 // function.
2166 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002167 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002168 }
2169 } else {
2170 // We have a forward declaration of this type, so adopt that forward
2171 // declaration rather than building a new one.
2172 AdoptDecl = FoundRecord;
2173 continue;
2174 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002175 }
2176
2177 ConflictingDecls.push_back(*Lookup.first);
2178 }
2179
2180 if (!ConflictingDecls.empty()) {
2181 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2182 ConflictingDecls.data(),
2183 ConflictingDecls.size());
2184 }
2185 }
2186
2187 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002188 RecordDecl *D2 = AdoptDecl;
2189 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002190 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002191 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002192 D->getTagKind(),
2193 DC, Loc,
2194 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002195 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002196 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002197 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002198 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002199 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002200 DC, Loc,
2201 Name.getAsIdentifierInfo(),
2202 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002203 }
John McCall3e11ebe2010-03-15 10:12:16 +00002204 // Import the qualifier, if any.
2205 if (D->getQualifier()) {
2206 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2207 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2208 D2->setQualifierInfo(NNS, NNSRange);
2209 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002210 D2->setLexicalDeclContext(LexicalDC);
2211 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002212 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002213
Douglas Gregor3996e242010-02-15 22:01:00 +00002214 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002215
Douglas Gregore2e50d332010-12-01 01:36:18 +00002216 if (D->isDefinition() && ImportDefinition(D, D2))
2217 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002218
Douglas Gregor3996e242010-02-15 22:01:00 +00002219 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002220}
2221
Douglas Gregor98c10182010-02-12 22:17:39 +00002222Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2223 // Import the major distinguishing characteristics of this enumerator.
2224 DeclContext *DC, *LexicalDC;
2225 DeclarationName Name;
2226 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002227 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002228 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002229
2230 QualType T = Importer.Import(D->getType());
2231 if (T.isNull())
2232 return 0;
2233
Douglas Gregor98c10182010-02-12 22:17:39 +00002234 // Determine whether there are any other declarations with the same name and
2235 // in the same context.
2236 if (!LexicalDC->isFunctionOrMethod()) {
2237 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2238 unsigned IDNS = Decl::IDNS_Ordinary;
2239 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2240 Lookup.first != Lookup.second;
2241 ++Lookup.first) {
2242 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2243 continue;
2244
2245 ConflictingDecls.push_back(*Lookup.first);
2246 }
2247
2248 if (!ConflictingDecls.empty()) {
2249 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2250 ConflictingDecls.data(),
2251 ConflictingDecls.size());
2252 if (!Name)
2253 return 0;
2254 }
2255 }
2256
2257 Expr *Init = Importer.Import(D->getInitExpr());
2258 if (D->getInitExpr() && !Init)
2259 return 0;
2260
2261 EnumConstantDecl *ToEnumerator
2262 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2263 Name.getAsIdentifierInfo(), T,
2264 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002265 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002266 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002267 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002268 LexicalDC->addDecl(ToEnumerator);
2269 return ToEnumerator;
2270}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002271
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002272Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2273 // Import the major distinguishing characteristics of this function.
2274 DeclContext *DC, *LexicalDC;
2275 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002276 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002277 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002278 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002279
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002280 // Try to find a function in our own ("to") context with the same name, same
2281 // type, and in the same context as the function we're importing.
2282 if (!LexicalDC->isFunctionOrMethod()) {
2283 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2284 unsigned IDNS = Decl::IDNS_Ordinary;
2285 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2286 Lookup.first != Lookup.second;
2287 ++Lookup.first) {
2288 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2289 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002290
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002291 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2292 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2293 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002294 if (Importer.IsStructurallyEquivalent(D->getType(),
2295 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002296 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002297 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002298 }
2299
2300 // FIXME: Check for overloading more carefully, e.g., by boosting
2301 // Sema::IsOverload out to the AST library.
2302
2303 // Function overloading is okay in C++.
2304 if (Importer.getToContext().getLangOptions().CPlusPlus)
2305 continue;
2306
2307 // Complain about inconsistent function types.
2308 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002309 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002310 Importer.ToDiag(FoundFunction->getLocation(),
2311 diag::note_odr_value_here)
2312 << FoundFunction->getType();
2313 }
2314 }
2315
2316 ConflictingDecls.push_back(*Lookup.first);
2317 }
2318
2319 if (!ConflictingDecls.empty()) {
2320 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2321 ConflictingDecls.data(),
2322 ConflictingDecls.size());
2323 if (!Name)
2324 return 0;
2325 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002326 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002327
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002328 DeclarationNameInfo NameInfo(Name, Loc);
2329 // Import additional name location/type info.
2330 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2331
Douglas Gregorb4964f72010-02-15 23:54:17 +00002332 // Import the type.
2333 QualType T = Importer.Import(D->getType());
2334 if (T.isNull())
2335 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002336
2337 // Import the function parameters.
2338 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2339 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2340 P != PEnd; ++P) {
2341 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2342 if (!ToP)
2343 return 0;
2344
2345 Parameters.push_back(ToP);
2346 }
2347
2348 // Create the imported function.
2349 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002350 FunctionDecl *ToFunction = 0;
2351 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2352 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2353 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002354 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002355 FromConstructor->isExplicit(),
2356 D->isInlineSpecified(),
2357 D->isImplicit());
2358 } else if (isa<CXXDestructorDecl>(D)) {
2359 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2360 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002361 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002362 D->isInlineSpecified(),
2363 D->isImplicit());
2364 } else if (CXXConversionDecl *FromConversion
2365 = dyn_cast<CXXConversionDecl>(D)) {
2366 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2367 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002368 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002369 D->isInlineSpecified(),
2370 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002371 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2372 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2373 cast<CXXRecordDecl>(DC),
2374 NameInfo, T, TInfo,
2375 Method->isStatic(),
2376 Method->getStorageClassAsWritten(),
2377 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002378 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002379 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2380 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002381 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002382 D->isInlineSpecified(),
2383 D->hasWrittenPrototype());
2384 }
John McCall3e11ebe2010-03-15 10:12:16 +00002385
2386 // Import the qualifier, if any.
2387 if (D->getQualifier()) {
2388 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2389 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2390 ToFunction->setQualifierInfo(NNS, NNSRange);
2391 }
Douglas Gregordd483172010-02-22 17:42:47 +00002392 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002393 ToFunction->setLexicalDeclContext(LexicalDC);
2394 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002395
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002396 // Set the parameters.
2397 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002398 Parameters[I]->setOwningFunction(ToFunction);
2399 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002400 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002401 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002402
2403 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002404
2405 // Add this function to the lexical context.
2406 LexicalDC->addDecl(ToFunction);
2407
Douglas Gregor43f54792010-02-17 02:12:47 +00002408 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002409}
2410
Douglas Gregor00eace12010-02-21 18:29:16 +00002411Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2412 return VisitFunctionDecl(D);
2413}
2414
2415Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2416 return VisitCXXMethodDecl(D);
2417}
2418
2419Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2420 return VisitCXXMethodDecl(D);
2421}
2422
2423Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2424 return VisitCXXMethodDecl(D);
2425}
2426
Douglas Gregor5c73e912010-02-11 00:48:18 +00002427Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2428 // Import the major distinguishing characteristics of a variable.
2429 DeclContext *DC, *LexicalDC;
2430 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002431 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002432 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2433 return 0;
2434
2435 // Import the type.
2436 QualType T = Importer.Import(D->getType());
2437 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002438 return 0;
2439
2440 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2441 Expr *BitWidth = Importer.Import(D->getBitWidth());
2442 if (!BitWidth && D->getBitWidth())
2443 return 0;
2444
2445 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2446 Loc, Name.getAsIdentifierInfo(),
2447 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002448 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002449 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002450 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002451 LexicalDC->addDecl(ToField);
2452 return ToField;
2453}
2454
Francois Pichet783dd6e2010-11-21 06:08:52 +00002455Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2456 // Import the major distinguishing characteristics of a variable.
2457 DeclContext *DC, *LexicalDC;
2458 DeclarationName Name;
2459 SourceLocation Loc;
2460 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2461 return 0;
2462
2463 // Import the type.
2464 QualType T = Importer.Import(D->getType());
2465 if (T.isNull())
2466 return 0;
2467
2468 NamedDecl **NamedChain =
2469 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2470
2471 unsigned i = 0;
2472 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2473 PE = D->chain_end(); PI != PE; ++PI) {
2474 Decl* D = Importer.Import(*PI);
2475 if (!D)
2476 return 0;
2477 NamedChain[i++] = cast<NamedDecl>(D);
2478 }
2479
2480 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2481 Importer.getToContext(), DC,
2482 Loc, Name.getAsIdentifierInfo(), T,
2483 NamedChain, D->getChainingSize());
2484 ToIndirectField->setAccess(D->getAccess());
2485 ToIndirectField->setLexicalDeclContext(LexicalDC);
2486 Importer.Imported(D, ToIndirectField);
2487 LexicalDC->addDecl(ToIndirectField);
2488 return ToIndirectField;
2489}
2490
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002491Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2492 // Import the major distinguishing characteristics of an ivar.
2493 DeclContext *DC, *LexicalDC;
2494 DeclarationName Name;
2495 SourceLocation Loc;
2496 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2497 return 0;
2498
2499 // Determine whether we've already imported this ivar
2500 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2501 Lookup.first != Lookup.second;
2502 ++Lookup.first) {
2503 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2504 if (Importer.IsStructurallyEquivalent(D->getType(),
2505 FoundIvar->getType())) {
2506 Importer.Imported(D, FoundIvar);
2507 return FoundIvar;
2508 }
2509
2510 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2511 << Name << D->getType() << FoundIvar->getType();
2512 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2513 << FoundIvar->getType();
2514 return 0;
2515 }
2516 }
2517
2518 // Import the type.
2519 QualType T = Importer.Import(D->getType());
2520 if (T.isNull())
2521 return 0;
2522
2523 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2524 Expr *BitWidth = Importer.Import(D->getBitWidth());
2525 if (!BitWidth && D->getBitWidth())
2526 return 0;
2527
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002528 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2529 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002530 Loc, Name.getAsIdentifierInfo(),
2531 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002532 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002533 ToIvar->setLexicalDeclContext(LexicalDC);
2534 Importer.Imported(D, ToIvar);
2535 LexicalDC->addDecl(ToIvar);
2536 return ToIvar;
2537
2538}
2539
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002540Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2541 // Import the major distinguishing characteristics of a variable.
2542 DeclContext *DC, *LexicalDC;
2543 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002544 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002545 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002546 return 0;
2547
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002548 // Try to find a variable in our own ("to") context with the same name and
2549 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002550 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002551 VarDecl *MergeWithVar = 0;
2552 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2553 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002554 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002555 Lookup.first != Lookup.second;
2556 ++Lookup.first) {
2557 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2558 continue;
2559
2560 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2561 // We have found a variable that we may need to merge with. Check it.
2562 if (isExternalLinkage(FoundVar->getLinkage()) &&
2563 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002564 if (Importer.IsStructurallyEquivalent(D->getType(),
2565 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002566 MergeWithVar = FoundVar;
2567 break;
2568 }
2569
Douglas Gregor56521c52010-02-12 17:23:39 +00002570 const ArrayType *FoundArray
2571 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2572 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002573 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002574 if (FoundArray && TArray) {
2575 if (isa<IncompleteArrayType>(FoundArray) &&
2576 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002577 // Import the type.
2578 QualType T = Importer.Import(D->getType());
2579 if (T.isNull())
2580 return 0;
2581
Douglas Gregor56521c52010-02-12 17:23:39 +00002582 FoundVar->setType(T);
2583 MergeWithVar = FoundVar;
2584 break;
2585 } else if (isa<IncompleteArrayType>(TArray) &&
2586 isa<ConstantArrayType>(FoundArray)) {
2587 MergeWithVar = FoundVar;
2588 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002589 }
2590 }
2591
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002592 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002593 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002594 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2595 << FoundVar->getType();
2596 }
2597 }
2598
2599 ConflictingDecls.push_back(*Lookup.first);
2600 }
2601
2602 if (MergeWithVar) {
2603 // An equivalent variable with external linkage has been found. Link
2604 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002605 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002606
2607 if (VarDecl *DDef = D->getDefinition()) {
2608 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2609 Importer.ToDiag(ExistingDef->getLocation(),
2610 diag::err_odr_variable_multiple_def)
2611 << Name;
2612 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2613 } else {
2614 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002615 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002616 }
2617 }
2618
2619 return MergeWithVar;
2620 }
2621
2622 if (!ConflictingDecls.empty()) {
2623 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2624 ConflictingDecls.data(),
2625 ConflictingDecls.size());
2626 if (!Name)
2627 return 0;
2628 }
2629 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002630
Douglas Gregorb4964f72010-02-15 23:54:17 +00002631 // Import the type.
2632 QualType T = Importer.Import(D->getType());
2633 if (T.isNull())
2634 return 0;
2635
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002636 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002637 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002638 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2639 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002640 D->getStorageClass(),
2641 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002642 // Import the qualifier, if any.
2643 if (D->getQualifier()) {
2644 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2645 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2646 ToVar->setQualifierInfo(NNS, NNSRange);
2647 }
Douglas Gregordd483172010-02-22 17:42:47 +00002648 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002649 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002650 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002651 LexicalDC->addDecl(ToVar);
2652
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002653 // Merge the initializer.
2654 // FIXME: Can we really import any initializer? Alternatively, we could force
2655 // ourselves to import every declaration of a variable and then only use
2656 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002657 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002658
2659 // FIXME: Other bits to merge?
2660
2661 return ToVar;
2662}
2663
Douglas Gregor8b228d72010-02-17 21:22:52 +00002664Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2665 // Parameters are created in the translation unit's context, then moved
2666 // into the function declaration's context afterward.
2667 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2668
2669 // Import the name of this declaration.
2670 DeclarationName Name = Importer.Import(D->getDeclName());
2671 if (D->getDeclName() && !Name)
2672 return 0;
2673
2674 // Import the location of this declaration.
2675 SourceLocation Loc = Importer.Import(D->getLocation());
2676
2677 // Import the parameter's type.
2678 QualType T = Importer.Import(D->getType());
2679 if (T.isNull())
2680 return 0;
2681
2682 // Create the imported parameter.
2683 ImplicitParamDecl *ToParm
2684 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2685 Loc, Name.getAsIdentifierInfo(),
2686 T);
2687 return Importer.Imported(D, ToParm);
2688}
2689
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002690Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2691 // Parameters are created in the translation unit's context, then moved
2692 // into the function declaration's context afterward.
2693 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2694
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002695 // Import the name of this declaration.
2696 DeclarationName Name = Importer.Import(D->getDeclName());
2697 if (D->getDeclName() && !Name)
2698 return 0;
2699
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002700 // Import the location of this declaration.
2701 SourceLocation Loc = Importer.Import(D->getLocation());
2702
2703 // Import the parameter's type.
2704 QualType T = Importer.Import(D->getType());
2705 if (T.isNull())
2706 return 0;
2707
2708 // Create the imported parameter.
2709 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2710 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2711 Loc, Name.getAsIdentifierInfo(),
2712 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002713 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002714 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002715 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002716 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002717}
2718
Douglas Gregor43f54792010-02-17 02:12:47 +00002719Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2720 // Import the major distinguishing characteristics of a method.
2721 DeclContext *DC, *LexicalDC;
2722 DeclarationName Name;
2723 SourceLocation Loc;
2724 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2725 return 0;
2726
2727 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2728 Lookup.first != Lookup.second;
2729 ++Lookup.first) {
2730 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2731 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2732 continue;
2733
2734 // Check return types.
2735 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2736 FoundMethod->getResultType())) {
2737 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2738 << D->isInstanceMethod() << Name
2739 << D->getResultType() << FoundMethod->getResultType();
2740 Importer.ToDiag(FoundMethod->getLocation(),
2741 diag::note_odr_objc_method_here)
2742 << D->isInstanceMethod() << Name;
2743 return 0;
2744 }
2745
2746 // Check the number of parameters.
2747 if (D->param_size() != FoundMethod->param_size()) {
2748 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2749 << D->isInstanceMethod() << Name
2750 << D->param_size() << FoundMethod->param_size();
2751 Importer.ToDiag(FoundMethod->getLocation(),
2752 diag::note_odr_objc_method_here)
2753 << D->isInstanceMethod() << Name;
2754 return 0;
2755 }
2756
2757 // Check parameter types.
2758 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2759 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2760 P != PEnd; ++P, ++FoundP) {
2761 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2762 (*FoundP)->getType())) {
2763 Importer.FromDiag((*P)->getLocation(),
2764 diag::err_odr_objc_method_param_type_inconsistent)
2765 << D->isInstanceMethod() << Name
2766 << (*P)->getType() << (*FoundP)->getType();
2767 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2768 << (*FoundP)->getType();
2769 return 0;
2770 }
2771 }
2772
2773 // Check variadic/non-variadic.
2774 // Check the number of parameters.
2775 if (D->isVariadic() != FoundMethod->isVariadic()) {
2776 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2777 << D->isInstanceMethod() << Name;
2778 Importer.ToDiag(FoundMethod->getLocation(),
2779 diag::note_odr_objc_method_here)
2780 << D->isInstanceMethod() << Name;
2781 return 0;
2782 }
2783
2784 // FIXME: Any other bits we need to merge?
2785 return Importer.Imported(D, FoundMethod);
2786 }
2787 }
2788
2789 // Import the result type.
2790 QualType ResultTy = Importer.Import(D->getResultType());
2791 if (ResultTy.isNull())
2792 return 0;
2793
Douglas Gregor12852d92010-03-08 14:59:44 +00002794 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2795
Douglas Gregor43f54792010-02-17 02:12:47 +00002796 ObjCMethodDecl *ToMethod
2797 = ObjCMethodDecl::Create(Importer.getToContext(),
2798 Loc,
2799 Importer.Import(D->getLocEnd()),
2800 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002801 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002802 D->isInstanceMethod(),
2803 D->isVariadic(),
2804 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002805 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002806 D->getImplementationControl());
2807
2808 // FIXME: When we decide to merge method definitions, we'll need to
2809 // deal with implicit parameters.
2810
2811 // Import the parameters
2812 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2813 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2814 FromPEnd = D->param_end();
2815 FromP != FromPEnd;
2816 ++FromP) {
2817 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2818 if (!ToP)
2819 return 0;
2820
2821 ToParams.push_back(ToP);
2822 }
2823
2824 // Set the parameters.
2825 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2826 ToParams[I]->setOwningFunction(ToMethod);
2827 ToMethod->addDecl(ToParams[I]);
2828 }
2829 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002830 ToParams.data(), ToParams.size(),
2831 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002832
2833 ToMethod->setLexicalDeclContext(LexicalDC);
2834 Importer.Imported(D, ToMethod);
2835 LexicalDC->addDecl(ToMethod);
2836 return ToMethod;
2837}
2838
Douglas Gregor84c51c32010-02-18 01:47:50 +00002839Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2840 // Import the major distinguishing characteristics of a category.
2841 DeclContext *DC, *LexicalDC;
2842 DeclarationName Name;
2843 SourceLocation Loc;
2844 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2845 return 0;
2846
2847 ObjCInterfaceDecl *ToInterface
2848 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2849 if (!ToInterface)
2850 return 0;
2851
2852 // Determine if we've already encountered this category.
2853 ObjCCategoryDecl *MergeWithCategory
2854 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2855 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2856 if (!ToCategory) {
2857 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2858 Importer.Import(D->getAtLoc()),
2859 Loc,
2860 Importer.Import(D->getCategoryNameLoc()),
2861 Name.getAsIdentifierInfo());
2862 ToCategory->setLexicalDeclContext(LexicalDC);
2863 LexicalDC->addDecl(ToCategory);
2864 Importer.Imported(D, ToCategory);
2865
2866 // Link this category into its class's category list.
2867 ToCategory->setClassInterface(ToInterface);
2868 ToCategory->insertNextClassCategory();
2869
2870 // Import protocols
2871 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2872 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2873 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2874 = D->protocol_loc_begin();
2875 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2876 FromProtoEnd = D->protocol_end();
2877 FromProto != FromProtoEnd;
2878 ++FromProto, ++FromProtoLoc) {
2879 ObjCProtocolDecl *ToProto
2880 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2881 if (!ToProto)
2882 return 0;
2883 Protocols.push_back(ToProto);
2884 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2885 }
2886
2887 // FIXME: If we're merging, make sure that the protocol list is the same.
2888 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2889 ProtocolLocs.data(), Importer.getToContext());
2890
2891 } else {
2892 Importer.Imported(D, ToCategory);
2893 }
2894
2895 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002896 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002897
2898 // If we have an implementation, import it as well.
2899 if (D->getImplementation()) {
2900 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002901 = cast_or_null<ObjCCategoryImplDecl>(
2902 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002903 if (!Impl)
2904 return 0;
2905
2906 ToCategory->setImplementation(Impl);
2907 }
2908
2909 return ToCategory;
2910}
2911
Douglas Gregor98d156a2010-02-17 16:12:00 +00002912Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002913 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002914 DeclContext *DC, *LexicalDC;
2915 DeclarationName Name;
2916 SourceLocation Loc;
2917 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2918 return 0;
2919
2920 ObjCProtocolDecl *MergeWithProtocol = 0;
2921 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2922 Lookup.first != Lookup.second;
2923 ++Lookup.first) {
2924 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2925 continue;
2926
2927 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2928 break;
2929 }
2930
2931 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2932 if (!ToProto || ToProto->isForwardDecl()) {
2933 if (!ToProto) {
2934 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2935 Name.getAsIdentifierInfo());
2936 ToProto->setForwardDecl(D->isForwardDecl());
2937 ToProto->setLexicalDeclContext(LexicalDC);
2938 LexicalDC->addDecl(ToProto);
2939 }
2940 Importer.Imported(D, ToProto);
2941
2942 // Import protocols
2943 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2944 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2945 ObjCProtocolDecl::protocol_loc_iterator
2946 FromProtoLoc = D->protocol_loc_begin();
2947 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2948 FromProtoEnd = D->protocol_end();
2949 FromProto != FromProtoEnd;
2950 ++FromProto, ++FromProtoLoc) {
2951 ObjCProtocolDecl *ToProto
2952 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2953 if (!ToProto)
2954 return 0;
2955 Protocols.push_back(ToProto);
2956 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2957 }
2958
2959 // FIXME: If we're merging, make sure that the protocol list is the same.
2960 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2961 ProtocolLocs.data(), Importer.getToContext());
2962 } else {
2963 Importer.Imported(D, ToProto);
2964 }
2965
Douglas Gregor84c51c32010-02-18 01:47:50 +00002966 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002967 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002968
2969 return ToProto;
2970}
2971
Douglas Gregor45635322010-02-16 01:20:57 +00002972Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2973 // Import the major distinguishing characteristics of an @interface.
2974 DeclContext *DC, *LexicalDC;
2975 DeclarationName Name;
2976 SourceLocation Loc;
2977 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2978 return 0;
2979
2980 ObjCInterfaceDecl *MergeWithIface = 0;
2981 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2982 Lookup.first != Lookup.second;
2983 ++Lookup.first) {
2984 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2985 continue;
2986
2987 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2988 break;
2989 }
2990
2991 ObjCInterfaceDecl *ToIface = MergeWithIface;
2992 if (!ToIface || ToIface->isForwardDecl()) {
2993 if (!ToIface) {
2994 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2995 DC, Loc,
2996 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002997 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002998 D->isForwardDecl(),
2999 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003000 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003001 ToIface->setLexicalDeclContext(LexicalDC);
3002 LexicalDC->addDecl(ToIface);
3003 }
3004 Importer.Imported(D, ToIface);
3005
Douglas Gregor45635322010-02-16 01:20:57 +00003006 if (D->getSuperClass()) {
3007 ObjCInterfaceDecl *Super
3008 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3009 if (!Super)
3010 return 0;
3011
3012 ToIface->setSuperClass(Super);
3013 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3014 }
3015
3016 // Import protocols
3017 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3018 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3019 ObjCInterfaceDecl::protocol_loc_iterator
3020 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003021
3022 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003023 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3024 FromProtoEnd = D->protocol_end();
3025 FromProto != FromProtoEnd;
3026 ++FromProto, ++FromProtoLoc) {
3027 ObjCProtocolDecl *ToProto
3028 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3029 if (!ToProto)
3030 return 0;
3031 Protocols.push_back(ToProto);
3032 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3033 }
3034
3035 // FIXME: If we're merging, make sure that the protocol list is the same.
3036 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3037 ProtocolLocs.data(), Importer.getToContext());
3038
Douglas Gregor45635322010-02-16 01:20:57 +00003039 // Import @end range
3040 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3041 } else {
3042 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003043
3044 // Check for consistency of superclasses.
3045 DeclarationName FromSuperName, ToSuperName;
3046 if (D->getSuperClass())
3047 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3048 if (ToIface->getSuperClass())
3049 ToSuperName = ToIface->getSuperClass()->getDeclName();
3050 if (FromSuperName != ToSuperName) {
3051 Importer.ToDiag(ToIface->getLocation(),
3052 diag::err_odr_objc_superclass_inconsistent)
3053 << ToIface->getDeclName();
3054 if (ToIface->getSuperClass())
3055 Importer.ToDiag(ToIface->getSuperClassLoc(),
3056 diag::note_odr_objc_superclass)
3057 << ToIface->getSuperClass()->getDeclName();
3058 else
3059 Importer.ToDiag(ToIface->getLocation(),
3060 diag::note_odr_objc_missing_superclass);
3061 if (D->getSuperClass())
3062 Importer.FromDiag(D->getSuperClassLoc(),
3063 diag::note_odr_objc_superclass)
3064 << D->getSuperClass()->getDeclName();
3065 else
3066 Importer.FromDiag(D->getLocation(),
3067 diag::note_odr_objc_missing_superclass);
3068 return 0;
3069 }
Douglas Gregor45635322010-02-16 01:20:57 +00003070 }
3071
Douglas Gregor84c51c32010-02-18 01:47:50 +00003072 // Import categories. When the categories themselves are imported, they'll
3073 // hook themselves into this interface.
3074 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3075 FromCat = FromCat->getNextClassCategory())
3076 Importer.Import(FromCat);
3077
Douglas Gregor45635322010-02-16 01:20:57 +00003078 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003079 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003080
3081 // If we have an @implementation, import it as well.
3082 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003083 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3084 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003085 if (!Impl)
3086 return 0;
3087
3088 ToIface->setImplementation(Impl);
3089 }
3090
Douglas Gregor98d156a2010-02-17 16:12:00 +00003091 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003092}
3093
Douglas Gregor4da9d682010-12-07 15:32:12 +00003094Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3095 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3096 Importer.Import(D->getCategoryDecl()));
3097 if (!Category)
3098 return 0;
3099
3100 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3101 if (!ToImpl) {
3102 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3103 if (!DC)
3104 return 0;
3105
3106 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3107 Importer.Import(D->getLocation()),
3108 Importer.Import(D->getIdentifier()),
3109 Category->getClassInterface());
3110
3111 DeclContext *LexicalDC = DC;
3112 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3113 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3114 if (!LexicalDC)
3115 return 0;
3116
3117 ToImpl->setLexicalDeclContext(LexicalDC);
3118 }
3119
3120 LexicalDC->addDecl(ToImpl);
3121 Category->setImplementation(ToImpl);
3122 }
3123
3124 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003125 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003126 return ToImpl;
3127}
3128
Douglas Gregorda8025c2010-12-07 01:26:03 +00003129Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3130 // Find the corresponding interface.
3131 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3132 Importer.Import(D->getClassInterface()));
3133 if (!Iface)
3134 return 0;
3135
3136 // Import the superclass, if any.
3137 ObjCInterfaceDecl *Super = 0;
3138 if (D->getSuperClass()) {
3139 Super = cast_or_null<ObjCInterfaceDecl>(
3140 Importer.Import(D->getSuperClass()));
3141 if (!Super)
3142 return 0;
3143 }
3144
3145 ObjCImplementationDecl *Impl = Iface->getImplementation();
3146 if (!Impl) {
3147 // We haven't imported an implementation yet. Create a new @implementation
3148 // now.
3149 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3150 Importer.ImportContext(D->getDeclContext()),
3151 Importer.Import(D->getLocation()),
3152 Iface, Super);
3153
3154 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3155 DeclContext *LexicalDC
3156 = Importer.ImportContext(D->getLexicalDeclContext());
3157 if (!LexicalDC)
3158 return 0;
3159 Impl->setLexicalDeclContext(LexicalDC);
3160 }
3161
3162 // Associate the implementation with the class it implements.
3163 Iface->setImplementation(Impl);
3164 Importer.Imported(D, Iface->getImplementation());
3165 } else {
3166 Importer.Imported(D, Iface->getImplementation());
3167
3168 // Verify that the existing @implementation has the same superclass.
3169 if ((Super && !Impl->getSuperClass()) ||
3170 (!Super && Impl->getSuperClass()) ||
3171 (Super && Impl->getSuperClass() &&
3172 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3173 Importer.ToDiag(Impl->getLocation(),
3174 diag::err_odr_objc_superclass_inconsistent)
3175 << Iface->getDeclName();
3176 // FIXME: It would be nice to have the location of the superclass
3177 // below.
3178 if (Impl->getSuperClass())
3179 Importer.ToDiag(Impl->getLocation(),
3180 diag::note_odr_objc_superclass)
3181 << Impl->getSuperClass()->getDeclName();
3182 else
3183 Importer.ToDiag(Impl->getLocation(),
3184 diag::note_odr_objc_missing_superclass);
3185 if (D->getSuperClass())
3186 Importer.FromDiag(D->getLocation(),
3187 diag::note_odr_objc_superclass)
3188 << D->getSuperClass()->getDeclName();
3189 else
3190 Importer.FromDiag(D->getLocation(),
3191 diag::note_odr_objc_missing_superclass);
3192 return 0;
3193 }
3194 }
3195
3196 // Import all of the members of this @implementation.
3197 ImportDeclContext(D);
3198
3199 return Impl;
3200}
3201
Douglas Gregora11c4582010-02-17 18:02:10 +00003202Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3203 // Import the major distinguishing characteristics of an @property.
3204 DeclContext *DC, *LexicalDC;
3205 DeclarationName Name;
3206 SourceLocation Loc;
3207 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3208 return 0;
3209
3210 // Check whether we have already imported this property.
3211 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3212 Lookup.first != Lookup.second;
3213 ++Lookup.first) {
3214 if (ObjCPropertyDecl *FoundProp
3215 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3216 // Check property types.
3217 if (!Importer.IsStructurallyEquivalent(D->getType(),
3218 FoundProp->getType())) {
3219 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3220 << Name << D->getType() << FoundProp->getType();
3221 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3222 << FoundProp->getType();
3223 return 0;
3224 }
3225
3226 // FIXME: Check property attributes, getters, setters, etc.?
3227
3228 // Consider these properties to be equivalent.
3229 Importer.Imported(D, FoundProp);
3230 return FoundProp;
3231 }
3232 }
3233
3234 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003235 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3236 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003237 return 0;
3238
3239 // Create the new property.
3240 ObjCPropertyDecl *ToProperty
3241 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3242 Name.getAsIdentifierInfo(),
3243 Importer.Import(D->getAtLoc()),
3244 T,
3245 D->getPropertyImplementation());
3246 Importer.Imported(D, ToProperty);
3247 ToProperty->setLexicalDeclContext(LexicalDC);
3248 LexicalDC->addDecl(ToProperty);
3249
3250 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003251 ToProperty->setPropertyAttributesAsWritten(
3252 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003253 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3254 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3255 ToProperty->setGetterMethodDecl(
3256 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3257 ToProperty->setSetterMethodDecl(
3258 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3259 ToProperty->setPropertyIvarDecl(
3260 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3261 return ToProperty;
3262}
3263
Douglas Gregor14a49e22010-12-07 18:32:03 +00003264Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3265 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3266 Importer.Import(D->getPropertyDecl()));
3267 if (!Property)
3268 return 0;
3269
3270 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3271 if (!DC)
3272 return 0;
3273
3274 // Import the lexical declaration context.
3275 DeclContext *LexicalDC = DC;
3276 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3277 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3278 if (!LexicalDC)
3279 return 0;
3280 }
3281
3282 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3283 if (!InImpl)
3284 return 0;
3285
3286 // Import the ivar (for an @synthesize).
3287 ObjCIvarDecl *Ivar = 0;
3288 if (D->getPropertyIvarDecl()) {
3289 Ivar = cast_or_null<ObjCIvarDecl>(
3290 Importer.Import(D->getPropertyIvarDecl()));
3291 if (!Ivar)
3292 return 0;
3293 }
3294
3295 ObjCPropertyImplDecl *ToImpl
3296 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3297 if (!ToImpl) {
3298 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3299 Importer.Import(D->getLocStart()),
3300 Importer.Import(D->getLocation()),
3301 Property,
3302 D->getPropertyImplementation(),
3303 Ivar,
3304 Importer.Import(D->getPropertyIvarDeclLoc()));
3305 ToImpl->setLexicalDeclContext(LexicalDC);
3306 Importer.Imported(D, ToImpl);
3307 LexicalDC->addDecl(ToImpl);
3308 } else {
3309 // Check that we have the same kind of property implementation (@synthesize
3310 // vs. @dynamic).
3311 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3312 Importer.ToDiag(ToImpl->getLocation(),
3313 diag::err_odr_objc_property_impl_kind_inconsistent)
3314 << Property->getDeclName()
3315 << (ToImpl->getPropertyImplementation()
3316 == ObjCPropertyImplDecl::Dynamic);
3317 Importer.FromDiag(D->getLocation(),
3318 diag::note_odr_objc_property_impl_kind)
3319 << D->getPropertyDecl()->getDeclName()
3320 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3321 return 0;
3322 }
3323
3324 // For @synthesize, check that we have the same
3325 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3326 Ivar != ToImpl->getPropertyIvarDecl()) {
3327 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3328 diag::err_odr_objc_synthesize_ivar_inconsistent)
3329 << Property->getDeclName()
3330 << ToImpl->getPropertyIvarDecl()->getDeclName()
3331 << Ivar->getDeclName();
3332 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3333 diag::note_odr_objc_synthesize_ivar_here)
3334 << D->getPropertyIvarDecl()->getDeclName();
3335 return 0;
3336 }
3337
3338 // Merge the existing implementation with the new implementation.
3339 Importer.Imported(D, ToImpl);
3340 }
3341
3342 return ToImpl;
3343}
3344
Douglas Gregor8661a722010-02-18 02:12:22 +00003345Decl *
3346ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3347 // Import the context of this declaration.
3348 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3349 if (!DC)
3350 return 0;
3351
3352 DeclContext *LexicalDC = DC;
3353 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3354 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3355 if (!LexicalDC)
3356 return 0;
3357 }
3358
3359 // Import the location of this declaration.
3360 SourceLocation Loc = Importer.Import(D->getLocation());
3361
3362 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3363 llvm::SmallVector<SourceLocation, 4> Locations;
3364 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3365 = D->protocol_loc_begin();
3366 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3367 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3368 FromProto != FromProtoEnd;
3369 ++FromProto, ++FromProtoLoc) {
3370 ObjCProtocolDecl *ToProto
3371 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3372 if (!ToProto)
3373 continue;
3374
3375 Protocols.push_back(ToProto);
3376 Locations.push_back(Importer.Import(*FromProtoLoc));
3377 }
3378
3379 ObjCForwardProtocolDecl *ToForward
3380 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3381 Protocols.data(), Protocols.size(),
3382 Locations.data());
3383 ToForward->setLexicalDeclContext(LexicalDC);
3384 LexicalDC->addDecl(ToForward);
3385 Importer.Imported(D, ToForward);
3386 return ToForward;
3387}
3388
Douglas Gregor06537af2010-02-18 02:04:09 +00003389Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3390 // Import the context of this declaration.
3391 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3392 if (!DC)
3393 return 0;
3394
3395 DeclContext *LexicalDC = DC;
3396 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3397 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3398 if (!LexicalDC)
3399 return 0;
3400 }
3401
3402 // Import the location of this declaration.
3403 SourceLocation Loc = Importer.Import(D->getLocation());
3404
3405 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3406 llvm::SmallVector<SourceLocation, 4> Locations;
3407 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3408 From != FromEnd; ++From) {
3409 ObjCInterfaceDecl *ToIface
3410 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3411 if (!ToIface)
3412 continue;
3413
3414 Interfaces.push_back(ToIface);
3415 Locations.push_back(Importer.Import(From->getLocation()));
3416 }
3417
3418 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3419 Loc,
3420 Interfaces.data(),
3421 Locations.data(),
3422 Interfaces.size());
3423 ToClass->setLexicalDeclContext(LexicalDC);
3424 LexicalDC->addDecl(ToClass);
3425 Importer.Imported(D, ToClass);
3426 return ToClass;
3427}
3428
Douglas Gregora082a492010-11-30 19:14:50 +00003429Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3430 // For template arguments, we adopt the translation unit as our declaration
3431 // context. This context will be fixed when the actual template declaration
3432 // is created.
3433
3434 // FIXME: Import default argument.
3435 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3436 Importer.getToContext().getTranslationUnitDecl(),
3437 Importer.Import(D->getLocation()),
3438 D->getDepth(),
3439 D->getIndex(),
3440 Importer.Import(D->getIdentifier()),
3441 D->wasDeclaredWithTypename(),
3442 D->isParameterPack());
3443}
3444
3445Decl *
3446ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3447 // Import the name of this declaration.
3448 DeclarationName Name = Importer.Import(D->getDeclName());
3449 if (D->getDeclName() && !Name)
3450 return 0;
3451
3452 // Import the location of this declaration.
3453 SourceLocation Loc = Importer.Import(D->getLocation());
3454
3455 // Import the type of this declaration.
3456 QualType T = Importer.Import(D->getType());
3457 if (T.isNull())
3458 return 0;
3459
3460 // Import type-source information.
3461 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3462 if (D->getTypeSourceInfo() && !TInfo)
3463 return 0;
3464
3465 // FIXME: Import default argument.
3466
3467 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3468 Importer.getToContext().getTranslationUnitDecl(),
3469 Loc, D->getDepth(), D->getPosition(),
3470 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003471 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003472}
3473
3474Decl *
3475ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3476 // Import the name of this declaration.
3477 DeclarationName Name = Importer.Import(D->getDeclName());
3478 if (D->getDeclName() && !Name)
3479 return 0;
3480
3481 // Import the location of this declaration.
3482 SourceLocation Loc = Importer.Import(D->getLocation());
3483
3484 // Import template parameters.
3485 TemplateParameterList *TemplateParams
3486 = ImportTemplateParameterList(D->getTemplateParameters());
3487 if (!TemplateParams)
3488 return 0;
3489
3490 // FIXME: Import default argument.
3491
3492 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3493 Importer.getToContext().getTranslationUnitDecl(),
3494 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003495 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003496 Name.getAsIdentifierInfo(),
3497 TemplateParams);
3498}
3499
3500Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3501 // If this record has a definition in the translation unit we're coming from,
3502 // but this particular declaration is not that definition, import the
3503 // definition and map to that.
3504 CXXRecordDecl *Definition
3505 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3506 if (Definition && Definition != D->getTemplatedDecl()) {
3507 Decl *ImportedDef
3508 = Importer.Import(Definition->getDescribedClassTemplate());
3509 if (!ImportedDef)
3510 return 0;
3511
3512 return Importer.Imported(D, ImportedDef);
3513 }
3514
3515 // Import the major distinguishing characteristics of this class template.
3516 DeclContext *DC, *LexicalDC;
3517 DeclarationName Name;
3518 SourceLocation Loc;
3519 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3520 return 0;
3521
3522 // We may already have a template of the same name; try to find and match it.
3523 if (!DC->isFunctionOrMethod()) {
3524 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3525 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3526 Lookup.first != Lookup.second;
3527 ++Lookup.first) {
3528 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3529 continue;
3530
3531 Decl *Found = *Lookup.first;
3532 if (ClassTemplateDecl *FoundTemplate
3533 = dyn_cast<ClassTemplateDecl>(Found)) {
3534 if (IsStructuralMatch(D, FoundTemplate)) {
3535 // The class templates structurally match; call it the same template.
3536 // FIXME: We may be filling in a forward declaration here. Handle
3537 // this case!
3538 Importer.Imported(D->getTemplatedDecl(),
3539 FoundTemplate->getTemplatedDecl());
3540 return Importer.Imported(D, FoundTemplate);
3541 }
3542 }
3543
3544 ConflictingDecls.push_back(*Lookup.first);
3545 }
3546
3547 if (!ConflictingDecls.empty()) {
3548 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3549 ConflictingDecls.data(),
3550 ConflictingDecls.size());
3551 }
3552
3553 if (!Name)
3554 return 0;
3555 }
3556
3557 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3558
3559 // Create the declaration that is being templated.
3560 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3561 DTemplated->getTagKind(),
3562 DC,
3563 Importer.Import(DTemplated->getLocation()),
3564 Name.getAsIdentifierInfo(),
3565 Importer.Import(DTemplated->getTagKeywordLoc()));
3566 D2Templated->setAccess(DTemplated->getAccess());
3567
3568
3569 // Import the qualifier, if any.
3570 if (DTemplated->getQualifier()) {
3571 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3572 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3573 D2Templated->setQualifierInfo(NNS, NNSRange);
3574 }
3575 D2Templated->setLexicalDeclContext(LexicalDC);
3576
3577 // Create the class template declaration itself.
3578 TemplateParameterList *TemplateParams
3579 = ImportTemplateParameterList(D->getTemplateParameters());
3580 if (!TemplateParams)
3581 return 0;
3582
3583 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3584 Loc, Name, TemplateParams,
3585 D2Templated,
3586 /*PrevDecl=*/0);
3587 D2Templated->setDescribedClassTemplate(D2);
3588
3589 D2->setAccess(D->getAccess());
3590 D2->setLexicalDeclContext(LexicalDC);
3591 LexicalDC->addDecl(D2);
3592
3593 // Note the relationship between the class templates.
3594 Importer.Imported(D, D2);
3595 Importer.Imported(DTemplated, D2Templated);
3596
3597 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3598 // FIXME: Import definition!
3599 }
3600
3601 return D2;
3602}
3603
Douglas Gregore2e50d332010-12-01 01:36:18 +00003604Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3605 ClassTemplateSpecializationDecl *D) {
3606 // If this record has a definition in the translation unit we're coming from,
3607 // but this particular declaration is not that definition, import the
3608 // definition and map to that.
3609 TagDecl *Definition = D->getDefinition();
3610 if (Definition && Definition != D) {
3611 Decl *ImportedDef = Importer.Import(Definition);
3612 if (!ImportedDef)
3613 return 0;
3614
3615 return Importer.Imported(D, ImportedDef);
3616 }
3617
3618 ClassTemplateDecl *ClassTemplate
3619 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3620 D->getSpecializedTemplate()));
3621 if (!ClassTemplate)
3622 return 0;
3623
3624 // Import the context of this declaration.
3625 DeclContext *DC = ClassTemplate->getDeclContext();
3626 if (!DC)
3627 return 0;
3628
3629 DeclContext *LexicalDC = DC;
3630 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3631 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3632 if (!LexicalDC)
3633 return 0;
3634 }
3635
3636 // Import the location of this declaration.
3637 SourceLocation Loc = Importer.Import(D->getLocation());
3638
3639 // Import template arguments.
3640 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3641 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3642 D->getTemplateArgs().size(),
3643 TemplateArgs))
3644 return 0;
3645
3646 // Try to find an existing specialization with these template arguments.
3647 void *InsertPos = 0;
3648 ClassTemplateSpecializationDecl *D2
3649 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3650 TemplateArgs.size(), InsertPos);
3651 if (D2) {
3652 // We already have a class template specialization with these template
3653 // arguments.
3654
3655 // FIXME: Check for specialization vs. instantiation errors.
3656
3657 if (RecordDecl *FoundDef = D2->getDefinition()) {
3658 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3659 // The record types structurally match, or the "from" translation
3660 // unit only had a forward declaration anyway; call it the same
3661 // function.
3662 return Importer.Imported(D, FoundDef);
3663 }
3664 }
3665 } else {
3666 // Create a new specialization.
3667 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3668 D->getTagKind(), DC,
3669 Loc, ClassTemplate,
3670 TemplateArgs.data(),
3671 TemplateArgs.size(),
3672 /*PrevDecl=*/0);
3673 D2->setSpecializationKind(D->getSpecializationKind());
3674
3675 // Add this specialization to the class template.
3676 ClassTemplate->AddSpecialization(D2, InsertPos);
3677
3678 // Import the qualifier, if any.
3679 if (D->getQualifier()) {
3680 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3681 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3682 D2->setQualifierInfo(NNS, NNSRange);
3683 }
3684
3685
3686 // Add the specialization to this context.
3687 D2->setLexicalDeclContext(LexicalDC);
3688 LexicalDC->addDecl(D2);
3689 }
3690 Importer.Imported(D, D2);
3691
3692 if (D->isDefinition() && ImportDefinition(D, D2))
3693 return 0;
3694
3695 return D2;
3696}
3697
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003698//----------------------------------------------------------------------------
3699// Import Statements
3700//----------------------------------------------------------------------------
3701
3702Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3703 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3704 << S->getStmtClassName();
3705 return 0;
3706}
3707
3708//----------------------------------------------------------------------------
3709// Import Expressions
3710//----------------------------------------------------------------------------
3711Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3712 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3713 << E->getStmtClassName();
3714 return 0;
3715}
3716
Douglas Gregor52f820e2010-02-19 01:17:02 +00003717Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3718 NestedNameSpecifier *Qualifier = 0;
3719 if (E->getQualifier()) {
3720 Qualifier = Importer.Import(E->getQualifier());
3721 if (!E->getQualifier())
3722 return 0;
3723 }
3724
3725 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3726 if (!ToD)
3727 return 0;
3728
3729 QualType T = Importer.Import(E->getType());
3730 if (T.isNull())
3731 return 0;
3732
3733 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3734 Importer.Import(E->getQualifierRange()),
3735 ToD,
3736 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003737 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003738 /*FIXME:TemplateArgs=*/0);
3739}
3740
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003741Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3742 QualType T = Importer.Import(E->getType());
3743 if (T.isNull())
3744 return 0;
3745
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003746 return IntegerLiteral::Create(Importer.getToContext(),
3747 E->getValue(), T,
3748 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003749}
3750
Douglas Gregor623421d2010-02-18 02:21:22 +00003751Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3752 QualType T = Importer.Import(E->getType());
3753 if (T.isNull())
3754 return 0;
3755
3756 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3757 E->isWide(), T,
3758 Importer.Import(E->getLocation()));
3759}
3760
Douglas Gregorc74247e2010-02-19 01:07:06 +00003761Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3762 Expr *SubExpr = Importer.Import(E->getSubExpr());
3763 if (!SubExpr)
3764 return 0;
3765
3766 return new (Importer.getToContext())
3767 ParenExpr(Importer.Import(E->getLParen()),
3768 Importer.Import(E->getRParen()),
3769 SubExpr);
3770}
3771
3772Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3773 QualType T = Importer.Import(E->getType());
3774 if (T.isNull())
3775 return 0;
3776
3777 Expr *SubExpr = Importer.Import(E->getSubExpr());
3778 if (!SubExpr)
3779 return 0;
3780
3781 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003782 T, E->getValueKind(),
3783 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003784 Importer.Import(E->getOperatorLoc()));
3785}
3786
Douglas Gregord8552cd2010-02-19 01:24:23 +00003787Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3788 QualType ResultType = Importer.Import(E->getType());
3789
3790 if (E->isArgumentType()) {
3791 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3792 if (!TInfo)
3793 return 0;
3794
3795 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3796 TInfo, ResultType,
3797 Importer.Import(E->getOperatorLoc()),
3798 Importer.Import(E->getRParenLoc()));
3799 }
3800
3801 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3802 if (!SubExpr)
3803 return 0;
3804
3805 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3806 SubExpr, ResultType,
3807 Importer.Import(E->getOperatorLoc()),
3808 Importer.Import(E->getRParenLoc()));
3809}
3810
Douglas Gregorc74247e2010-02-19 01:07:06 +00003811Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3812 QualType T = Importer.Import(E->getType());
3813 if (T.isNull())
3814 return 0;
3815
3816 Expr *LHS = Importer.Import(E->getLHS());
3817 if (!LHS)
3818 return 0;
3819
3820 Expr *RHS = Importer.Import(E->getRHS());
3821 if (!RHS)
3822 return 0;
3823
3824 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003825 T, E->getValueKind(),
3826 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003827 Importer.Import(E->getOperatorLoc()));
3828}
3829
3830Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3831 QualType T = Importer.Import(E->getType());
3832 if (T.isNull())
3833 return 0;
3834
3835 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3836 if (CompLHSType.isNull())
3837 return 0;
3838
3839 QualType CompResultType = Importer.Import(E->getComputationResultType());
3840 if (CompResultType.isNull())
3841 return 0;
3842
3843 Expr *LHS = Importer.Import(E->getLHS());
3844 if (!LHS)
3845 return 0;
3846
3847 Expr *RHS = Importer.Import(E->getRHS());
3848 if (!RHS)
3849 return 0;
3850
3851 return new (Importer.getToContext())
3852 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003853 T, E->getValueKind(),
3854 E->getObjectKind(),
3855 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003856 Importer.Import(E->getOperatorLoc()));
3857}
3858
John McCallcf142162010-08-07 06:22:56 +00003859bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3860 if (E->path_empty()) return false;
3861
3862 // TODO: import cast paths
3863 return true;
3864}
3865
Douglas Gregor98c10182010-02-12 22:17:39 +00003866Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3867 QualType T = Importer.Import(E->getType());
3868 if (T.isNull())
3869 return 0;
3870
3871 Expr *SubExpr = Importer.Import(E->getSubExpr());
3872 if (!SubExpr)
3873 return 0;
John McCallcf142162010-08-07 06:22:56 +00003874
3875 CXXCastPath BasePath;
3876 if (ImportCastPath(E, BasePath))
3877 return 0;
3878
3879 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003880 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003881}
3882
Douglas Gregor5481d322010-02-19 01:32:14 +00003883Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3884 QualType T = Importer.Import(E->getType());
3885 if (T.isNull())
3886 return 0;
3887
3888 Expr *SubExpr = Importer.Import(E->getSubExpr());
3889 if (!SubExpr)
3890 return 0;
3891
3892 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3893 if (!TInfo && E->getTypeInfoAsWritten())
3894 return 0;
3895
John McCallcf142162010-08-07 06:22:56 +00003896 CXXCastPath BasePath;
3897 if (ImportCastPath(E, BasePath))
3898 return 0;
3899
John McCall7decc9e2010-11-18 06:31:45 +00003900 return CStyleCastExpr::Create(Importer.getToContext(), T,
3901 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003902 SubExpr, &BasePath, TInfo,
3903 Importer.Import(E->getLParenLoc()),
3904 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003905}
3906
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003907ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003908 ASTContext &FromContext, FileManager &FromFileManager,
3909 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003910 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003911 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3912 Minimal(MinimalImport)
3913{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003914 ImportedDecls[FromContext.getTranslationUnitDecl()]
3915 = ToContext.getTranslationUnitDecl();
3916}
3917
3918ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003919
3920QualType ASTImporter::Import(QualType FromT) {
3921 if (FromT.isNull())
3922 return QualType();
3923
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003924 // Check whether we've already imported this type.
3925 llvm::DenseMap<Type *, Type *>::iterator Pos
3926 = ImportedTypes.find(FromT.getTypePtr());
3927 if (Pos != ImportedTypes.end())
3928 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003929
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003930 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003931 ASTNodeImporter Importer(*this);
3932 QualType ToT = Importer.Visit(FromT.getTypePtr());
3933 if (ToT.isNull())
3934 return ToT;
3935
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003936 // Record the imported type.
3937 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3938
Douglas Gregor96e578d2010-02-05 17:54:41 +00003939 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3940}
3941
Douglas Gregor62d311f2010-02-09 19:21:46 +00003942TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003943 if (!FromTSI)
3944 return FromTSI;
3945
3946 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003947 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003948 QualType T = Import(FromTSI->getType());
3949 if (T.isNull())
3950 return 0;
3951
3952 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003953 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003954}
3955
3956Decl *ASTImporter::Import(Decl *FromD) {
3957 if (!FromD)
3958 return 0;
3959
3960 // Check whether we've already imported this declaration.
3961 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3962 if (Pos != ImportedDecls.end())
3963 return Pos->second;
3964
3965 // Import the type
3966 ASTNodeImporter Importer(*this);
3967 Decl *ToD = Importer.Visit(FromD);
3968 if (!ToD)
3969 return 0;
3970
3971 // Record the imported declaration.
3972 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003973
3974 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3975 // Keep track of anonymous tags that have an associated typedef.
3976 if (FromTag->getTypedefForAnonDecl())
3977 AnonTagsWithPendingTypedefs.push_back(FromTag);
3978 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3979 // When we've finished transforming a typedef, see whether it was the
3980 // typedef for an anonymous tag.
3981 for (llvm::SmallVector<TagDecl *, 4>::iterator
3982 FromTag = AnonTagsWithPendingTypedefs.begin(),
3983 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3984 FromTag != FromTagEnd; ++FromTag) {
3985 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3986 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3987 // We found the typedef for an anonymous tag; link them.
3988 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3989 AnonTagsWithPendingTypedefs.erase(FromTag);
3990 break;
3991 }
3992 }
3993 }
3994 }
3995
Douglas Gregor62d311f2010-02-09 19:21:46 +00003996 return ToD;
3997}
3998
3999DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4000 if (!FromDC)
4001 return FromDC;
4002
4003 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4004}
4005
4006Expr *ASTImporter::Import(Expr *FromE) {
4007 if (!FromE)
4008 return 0;
4009
4010 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4011}
4012
4013Stmt *ASTImporter::Import(Stmt *FromS) {
4014 if (!FromS)
4015 return 0;
4016
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004017 // Check whether we've already imported this declaration.
4018 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4019 if (Pos != ImportedStmts.end())
4020 return Pos->second;
4021
4022 // Import the type
4023 ASTNodeImporter Importer(*this);
4024 Stmt *ToS = Importer.Visit(FromS);
4025 if (!ToS)
4026 return 0;
4027
4028 // Record the imported declaration.
4029 ImportedStmts[FromS] = ToS;
4030 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004031}
4032
4033NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4034 if (!FromNNS)
4035 return 0;
4036
4037 // FIXME: Implement!
4038 return 0;
4039}
4040
Douglas Gregore2e50d332010-12-01 01:36:18 +00004041TemplateName ASTImporter::Import(TemplateName From) {
4042 switch (From.getKind()) {
4043 case TemplateName::Template:
4044 if (TemplateDecl *ToTemplate
4045 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4046 return TemplateName(ToTemplate);
4047
4048 return TemplateName();
4049
4050 case TemplateName::OverloadedTemplate: {
4051 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4052 UnresolvedSet<2> ToTemplates;
4053 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4054 E = FromStorage->end();
4055 I != E; ++I) {
4056 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4057 ToTemplates.addDecl(To);
4058 else
4059 return TemplateName();
4060 }
4061 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4062 ToTemplates.end());
4063 }
4064
4065 case TemplateName::QualifiedTemplate: {
4066 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4067 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4068 if (!Qualifier)
4069 return TemplateName();
4070
4071 if (TemplateDecl *ToTemplate
4072 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4073 return ToContext.getQualifiedTemplateName(Qualifier,
4074 QTN->hasTemplateKeyword(),
4075 ToTemplate);
4076
4077 return TemplateName();
4078 }
4079
4080 case TemplateName::DependentTemplate: {
4081 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4082 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4083 if (!Qualifier)
4084 return TemplateName();
4085
4086 if (DTN->isIdentifier()) {
4087 return ToContext.getDependentTemplateName(Qualifier,
4088 Import(DTN->getIdentifier()));
4089 }
4090
4091 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4092 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004093
4094 case TemplateName::SubstTemplateTemplateParmPack: {
4095 SubstTemplateTemplateParmPackStorage *SubstPack
4096 = From.getAsSubstTemplateTemplateParmPack();
4097 TemplateTemplateParmDecl *Param
4098 = cast_or_null<TemplateTemplateParmDecl>(
4099 Import(SubstPack->getParameterPack()));
4100 if (!Param)
4101 return TemplateName();
4102
4103 ASTNodeImporter Importer(*this);
4104 TemplateArgument ArgPack
4105 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4106 if (ArgPack.isNull())
4107 return TemplateName();
4108
4109 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4110 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004111 }
4112
4113 llvm_unreachable("Invalid template name kind");
4114 return TemplateName();
4115}
4116
Douglas Gregor62d311f2010-02-09 19:21:46 +00004117SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4118 if (FromLoc.isInvalid())
4119 return SourceLocation();
4120
Douglas Gregor811663e2010-02-10 00:15:17 +00004121 SourceManager &FromSM = FromContext.getSourceManager();
4122
4123 // For now, map everything down to its spelling location, so that we
4124 // don't have to import macro instantiations.
4125 // FIXME: Import macro instantiations!
4126 FromLoc = FromSM.getSpellingLoc(FromLoc);
4127 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4128 SourceManager &ToSM = ToContext.getSourceManager();
4129 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4130 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004131}
4132
4133SourceRange ASTImporter::Import(SourceRange FromRange) {
4134 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4135}
4136
Douglas Gregor811663e2010-02-10 00:15:17 +00004137FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004138 llvm::DenseMap<FileID, FileID>::iterator Pos
4139 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004140 if (Pos != ImportedFileIDs.end())
4141 return Pos->second;
4142
4143 SourceManager &FromSM = FromContext.getSourceManager();
4144 SourceManager &ToSM = ToContext.getSourceManager();
4145 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4146 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4147
4148 // Include location of this file.
4149 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4150
4151 // Map the FileID for to the "to" source manager.
4152 FileID ToID;
4153 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4154 if (Cache->Entry) {
4155 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4156 // disk again
4157 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4158 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00004159 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004160 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4161 FromSLoc.getFile().getFileCharacteristic());
4162 } else {
4163 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004164 const llvm::MemoryBuffer *
4165 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004166 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004167 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004168 FromBuf->getBufferIdentifier());
4169 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4170 }
4171
4172
Sebastian Redl99219f12010-09-30 01:03:06 +00004173 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004174 return ToID;
4175}
4176
Douglas Gregor0a791672011-01-18 03:11:38 +00004177void ASTImporter::ImportDefinition(Decl *From) {
4178 Decl *To = Import(From);
4179 if (!To)
4180 return;
4181
4182 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4183 ASTNodeImporter Importer(*this);
4184 Importer.ImportDeclContext(FromDC, true);
4185 }
4186}
4187
Douglas Gregor96e578d2010-02-05 17:54:41 +00004188DeclarationName ASTImporter::Import(DeclarationName FromName) {
4189 if (!FromName)
4190 return DeclarationName();
4191
4192 switch (FromName.getNameKind()) {
4193 case DeclarationName::Identifier:
4194 return Import(FromName.getAsIdentifierInfo());
4195
4196 case DeclarationName::ObjCZeroArgSelector:
4197 case DeclarationName::ObjCOneArgSelector:
4198 case DeclarationName::ObjCMultiArgSelector:
4199 return Import(FromName.getObjCSelector());
4200
4201 case DeclarationName::CXXConstructorName: {
4202 QualType T = Import(FromName.getCXXNameType());
4203 if (T.isNull())
4204 return DeclarationName();
4205
4206 return ToContext.DeclarationNames.getCXXConstructorName(
4207 ToContext.getCanonicalType(T));
4208 }
4209
4210 case DeclarationName::CXXDestructorName: {
4211 QualType T = Import(FromName.getCXXNameType());
4212 if (T.isNull())
4213 return DeclarationName();
4214
4215 return ToContext.DeclarationNames.getCXXDestructorName(
4216 ToContext.getCanonicalType(T));
4217 }
4218
4219 case DeclarationName::CXXConversionFunctionName: {
4220 QualType T = Import(FromName.getCXXNameType());
4221 if (T.isNull())
4222 return DeclarationName();
4223
4224 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4225 ToContext.getCanonicalType(T));
4226 }
4227
4228 case DeclarationName::CXXOperatorName:
4229 return ToContext.DeclarationNames.getCXXOperatorName(
4230 FromName.getCXXOverloadedOperator());
4231
4232 case DeclarationName::CXXLiteralOperatorName:
4233 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4234 Import(FromName.getCXXLiteralIdentifier()));
4235
4236 case DeclarationName::CXXUsingDirective:
4237 // FIXME: STATICS!
4238 return DeclarationName::getUsingDirectiveName();
4239 }
4240
4241 // Silence bogus GCC warning
4242 return DeclarationName();
4243}
4244
Douglas Gregore2e50d332010-12-01 01:36:18 +00004245IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004246 if (!FromId)
4247 return 0;
4248
4249 return &ToContext.Idents.get(FromId->getName());
4250}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004251
Douglas Gregor43f54792010-02-17 02:12:47 +00004252Selector ASTImporter::Import(Selector FromSel) {
4253 if (FromSel.isNull())
4254 return Selector();
4255
4256 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4257 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4258 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4259 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4260 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4261}
4262
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004263DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4264 DeclContext *DC,
4265 unsigned IDNS,
4266 NamedDecl **Decls,
4267 unsigned NumDecls) {
4268 return Name;
4269}
4270
4271DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004272 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004273}
4274
4275DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004276 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004277}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004278
4279Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4280 ImportedDecls[From] = To;
4281 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004282}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004283
4284bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4285 llvm::DenseMap<Type *, Type *>::iterator Pos
4286 = ImportedTypes.find(From.getTypePtr());
4287 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4288 return true;
4289
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004290 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004291 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004292}